-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathprint.c
381 lines (315 loc) · 7.65 KB
/
print.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* print.c -- formatted printing routines (Paul Haahr, 12/91) */
#include "rc.h"
#include <setjmp.h>
#define PRINT_ALLOCSIZE ((size_t)64)
#define SPRINT_BUFSIZ ((size_t)1024)
#define MAXCONV 256
/*
* conversion functions
* true return -> flag changes only, not a conversion
*/
#define Flag(name, flag) \
static bool name(Format *format, int ignore) { \
format->flags |= flag; \
return TRUE; \
}
Flag(uconv, FMT_unsigned)
Flag(rc_lconv, FMT_long)
#if HAVE_QUAD_T
Flag(qconv, FMT_quad)
#endif
Flag(altconv, FMT_altform)
Flag(leftconv, FMT_leftside)
Flag(dotconv, FMT_f2set)
static bool digitconv(Format *format, int c) {
if (format->flags & FMT_f2set)
format->f2 = 10 * format->f2 + c - '0';
else {
format->flags |= FMT_f1set;
format->f1 = 10 * format->f1 + c - '0';
}
return TRUE;
}
static bool zeroconv(Format *format, int ignore) {
if (format->flags & (FMT_f1set | FMT_f2set))
return digitconv(format, '0');
format->flags |= FMT_zeropad;
return TRUE;
}
static void pad(Format *format, size_t len, int c) {
while (len-- != 0)
fmtputc(format, c);
}
static bool sconv(Format *format, int ignore) {
char *s = va_arg(format->args, char *);
if ((format->flags & FMT_f1set) == 0)
fmtcat(format, s);
else {
size_t len = strlen(s), width = format->f1 - len;
if (format->flags & FMT_leftside) {
fmtappend(format, s, len);
pad(format, width, ' ');
} else {
pad(format, width, ' ');
fmtappend(format, s, len);
}
}
return FALSE;
}
static char *rc_utoa(unsigned long u, char *t, unsigned int radix, const char *digit) {
if (u >= radix) {
t = rc_utoa(u / radix, t, radix, digit);
u %= radix;
}
*t++ = digit[u];
return t;
}
static void intconv(Format *format, unsigned int radix, int upper, const char *altform) {
static const char * const table[] = {
"0123456789abcdefghijklmnopqrstuvwxyz",
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
};
char padchar;
size_t len, pre, zeroes, padding, width;
long n, flags;
unsigned long u;
char number[64], prefix[20];
if (radix > 36)
return;
flags = format->flags;
#if HAVE_QUAD_T
if (flags & FMT_quad)
n = va_arg(format->args, quad_t);
else
#endif
if (flags & FMT_long)
n = va_arg(format->args, long);
else
n = va_arg(format->args, int);
pre = 0;
if ((flags & FMT_unsigned) || n >= 0)
u = n;
else {
prefix[pre++] = '-';
u = -n;
}
if (flags & FMT_altform)
while (*altform != '\0')
prefix[pre++] = *altform++;
len = rc_utoa(u, number, radix, table[upper]) - number;
if ((flags & FMT_f2set) && (size_t) format->f2 > len)
zeroes = format->f2 - len;
else
zeroes = 0;
width = pre + zeroes + len;
if ((flags & FMT_f1set) && (size_t) format->f1 > width) {
padding = format->f1 - width;
} else
padding = 0;
padchar = ' ';
if (padding > 0 && flags & FMT_zeropad) {
padchar = '0';
if ((flags & FMT_leftside) == 0) {
zeroes += padding;
padding = 0;
}
}
if ((flags & FMT_leftside) == 0)
pad(format, padding, padchar);
fmtappend(format, prefix, pre);
pad(format, zeroes, '0');
fmtappend(format, number, len);
if (flags & FMT_leftside)
pad(format, padding, padchar);
}
static bool cconv(Format *format, int ignore) {
fmtputc(format, va_arg(format->args, int));
return FALSE;
}
static bool dconv(Format *format, int ignore) {
intconv(format, 10, 0, "");
return FALSE;
}
static bool oconv(Format *format, int ignore) {
intconv(format, 8, 0, "0");
return FALSE;
}
static bool xconv(Format *format, int ignore) {
intconv(format, 16, 0, "0x");
return FALSE;
}
static bool pctconv(Format *format, int ignore) {
fmtputc(format, '%');
return FALSE;
}
static bool badconv(Format *ignore, int ign0re) {
panic("bad conversion character in printfmt");
/* NOTREACHED */
return FALSE; /* hush up gcc -Wall */
}
/*
* conversion table management
*/
static Conv fmttab[MAXCONV];
static void inittab(void) {
int i;
for (i = 0; i < MAXCONV; i++)
fmttab[i] = badconv;
fmttab['s'] = sconv;
fmttab['c'] = cconv;
fmttab['d'] = dconv;
fmttab['o'] = oconv;
fmttab['x'] = xconv;
fmttab['%'] = pctconv;
fmttab['u'] = uconv;
fmttab['l'] = rc_lconv;
fmttab['#'] = altconv;
fmttab['-'] = leftconv;
fmttab['.'] = dotconv;
#if HAVE_QUAD_T
fmttab['q'] = qconv;
#endif
fmttab['0'] = zeroconv;
for (i = '1'; i <= '9'; i++)
fmttab[i] = digitconv;
}
extern bool (*fmtinstall(int c, bool (*f)(Format *, int)))(Format *, int) {
/*Conv fmtinstall(int c, Conv f) {*/
Conv oldf;
if (fmttab[0] == NULL)
inittab();
c &= MAXCONV - 1;
oldf = fmttab[c];
if (f != NULL)
fmttab[c] = f;
return oldf;
}
/*
* functions for inserting strings in the format buffer
*/
extern void fmtappend(Format *format, const char *s, size_t len) {
while (format->buf + len > format->bufend) {
size_t split = format->bufend - format->buf;
memcpy(format->buf, s, split);
format->buf += split;
s += split;
len -= split;
(*format->grow)(format, len);
}
memcpy(format->buf, s, len);
format->buf += len;
}
extern void fmtcat(Format *format, const char *s) {
fmtappend(format, s, strlen(s));
}
/*
* printfmt -- the driver routine
*/
extern int printfmt(Format *format, const char *fmt) {
unsigned const char *s = (unsigned const char *) fmt;
if (fmttab[0] == NULL)
inittab();
for (;;) {
int c = *s++;
switch (c) {
case '%':
format->flags = format->f1 = format->f2 = 0;
do
c = *s++;
while ((*fmttab[c])(format, c));
break;
case '\0':
return format->buf - format->bufbegin + format->flushed;
default:
fmtputc(format, c);
break;
}
}
}
/*
* the public entry points
*/
extern int fmtprint(Format *format, const char *fmt,...) {
int n = -format->flushed;
va_list ap, saveargs;
va_start(ap, fmt);
va_copy(saveargs, format->args);
va_copy(format->args, ap);
n += printfmt(format, fmt);
va_end(format->args);
va_copy(format->args, saveargs);
return n + format->flushed;
}
static void fprint_flush(Format *format, size_t ignore) {
size_t n = format->buf - format->bufbegin;
char *buf = format->bufbegin;
format->flushed += n;
format->buf = format->bufbegin;
writeall(format->u.n, buf, n);
}
extern int fprint(int fd, const char *fmt,...) {
char buf[1024];
Format format;
va_list ap;
format.buf = buf;
format.bufbegin = buf;
format.bufend = buf + sizeof buf;
format.grow = fprint_flush;
format.flushed = 0;
format.u.n = fd;
va_start(ap, fmt);
va_copy(format.args, ap);
printfmt(&format, fmt);
va_end(format.args);
fprint_flush(&format, 0);
return format.flushed;
}
static void memprint_grow(Format *format, size_t more) {
char *buf;
size_t len = format->bufend - format->bufbegin + 1;
size_t used = format->buf - format->bufbegin;
len = (len >= more)
? len * 2
: ((len + more) + PRINT_ALLOCSIZE) &~ (PRINT_ALLOCSIZE - 1);
if (format->u.n)
buf = erealloc(format->bufbegin, len);
else {
buf = nalloc(len);
memcpy(buf, format->bufbegin, used);
}
format->buf = buf + used;
format->bufbegin = buf;
format->bufend = buf + len - 1;
}
static char *memprint(Format *format, const char *fmt, char *buf, size_t len) {
format->buf = buf;
format->bufbegin = buf;
format->bufend = buf + len - 1;
format->grow = memprint_grow;
format->flushed = 0;
printfmt(format, fmt);
*format->buf = '\0';
return format->bufbegin;
}
extern char *mprint(const char *fmt,...) {
Format format;
char *result;
va_list ap;
format.u.n = 1;
va_start(ap, fmt);
va_copy(format.args, ap);
result = memprint(&format, fmt, ealloc(PRINT_ALLOCSIZE), PRINT_ALLOCSIZE);
va_end(format.args);
return result;
}
extern char *nprint(const char *fmt,...) {
Format format;
char *result;
va_list ap;
format.u.n = 0;
va_start(ap, fmt);
va_copy(format.args, ap);
result = memprint(&format, fmt, nalloc(PRINT_ALLOCSIZE), PRINT_ALLOCSIZE);
va_end(format.args);
return result;
}