-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprint.c
634 lines (534 loc) · 11.5 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
// print.c -- formatted printing routines
// this by Scott Schwartz, derived from Paul Haahr's version of 12/91
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include "print.h"
//
// functions for inserting strings in the format buffer
//
extern int
fmtappend(Format *format, const char *s, size_t len)
{
format->reqlen += len;
if (format->error)
return 0;
while (len > format->bufend - format->buf) {
const size_t split = format->bufend - format->buf;
memcpy(format->buf, s, split);
format->buf += split;
s += split;
len -= split;
if ((*format->grow)(format, len))
return 0;
}
memcpy(format->buf, s, len);
format->buf += len;
return 1;
}
extern int
fmtputc(Format *f, const char c)
{
return fmtappend(f, &c, 1);
}
extern int
fmtcat(Format *format, const char *s)
{
return fmtappend(format, s, strlen(s));
}
// conversion functions
// true return -> flag changes only, not a conversion
#define Flag(name, flag) \
static int name(Format *format, int c) { \
format->flags |= flag; \
return FMT_flag; \
}
Flag(uconv, FMT_unsigned)
Flag(hconv, FMT_short)
Flag(lconv, FMT_long)
Flag(altconv, FMT_altform)
Flag(leftconv, FMT_leftside)
Flag(dotconv, FMT_f2set)
static int
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 FMT_flag;
}
static int
zeroconv(Format *format, int c)
{
if (format->flags & (FMT_f1set | FMT_f2set))
return digitconv(format, '0');
format->flags |= FMT_zeropad;
return FMT_flag;
}
static int
starconv(Format *format, int c)
{
int n = va_arg(format->args, int);
if (format->flags & FMT_f2set)
format->f2 = n;
else {
format->flags |= FMT_f1set;
format->f1 = n;
}
return FMT_flag;
}
static void
pad(Format *format, size_t len, int c)
{
do;
while (len-- != 0 && fmtputc(format, c));
}
static int
sconv(Format *format, int c)
{
size_t maxbytes, minwidth, extra;
char *s = va_arg(format->args, char *);
if (!s)
s = "(null)";
if (format->flags & FMT_altform) {
// maxbytes may overrun s, but this way we don't have
// to assume that s is null terminated when f2 is set.
// Maybe use a different format character for this?
maxbytes =
(format->flags & FMT_f2set) ? format->f2 : strlen(s);
} else {
// do f2 like printf
maxbytes = strlen(s);
if ((format->flags & FMT_f2set) && (format->f2 < maxbytes))
maxbytes = format->f2;
}
minwidth = (format->flags & FMT_f1set) ? format->f1 : maxbytes;
extra = (minwidth > maxbytes) ? minwidth - maxbytes : 0;
if (format->flags & FMT_leftside) {
fmtappend(format, s, maxbytes);
pad(format, extra, ' ');
} else {
pad(format, extra, ' ');
fmtappend(format, s, maxbytes);
}
return FMT_verb;
}
static char *
utoa(unsigned long u, char *t, unsigned int radix, const char *digit)
{
if (u >= radix) {
t = 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 (flags & FMT_unsigned) {
if (flags & FMT_long)
n = (unsigned long)va_arg(format->args, long);
else if (flags & FMT_short)
n = (unsigned long)(unsigned short)
va_arg(format->args, int);
else
n = (unsigned long)(unsigned int)
va_arg(format->args, int);
} else {
if (flags & FMT_long)
n = (long)va_arg(format->args, long);
else if (flags & FMT_short)
n = (long)(short)va_arg(format->args, int);
else
n = (long)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 = 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;
padding = 0;
if ((flags & FMT_f1set) && (size_t) format->f1 > width) {
padding = format->f1 - width;
}
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 int
cconv(Format *format, int c)
{
fmtputc(format, va_arg(format->args, int));
return FMT_verb;
}
static int
dconv(Format *format, int c)
{
intconv(format, 10, 0, "");
return FMT_verb;
}
static int
oconv(Format *format, int c)
{
intconv(format, 8, 0, "0");
return FMT_verb;
}
static int
xconv(Format *format, int c)
{
intconv(format, 16, 0, "0x");
return FMT_verb;
}
static int
litconv(Format *format, int c)
{
fmtputc(format, c);
return FMT_verb;
}
extern int
fmt_badconv(Format *format, int c)
{
eprint("libprint: bad conversion char '%c'\n", c);
format->error = 1; // XXX - stop or not? consume an argv?
return FMT_verb;
}
static int
nconv(Format *format, int c)
{
// Write the current write length into an int* argument.
int *p = va_arg(format->args, int *);
if (p)
*p = format->buf - format->bufbegin + format->flushed;
return FMT_verb;
}
static int
rconv(Format *format, int c)
{
int e = errno;
// do we have to store errno in the format structure because
// the grow proc could make a syscall that sets errno before
// we get here?
if (format->flags & FMT_altform) {
fmtprint(format, "%d", e);
} else {
// strerror() might be better, but this way we can range check
extern int sys_nerr;
extern char *sys_errlist[];
if ((0 <= e) && (e < sys_nerr))
fmtcat(format, sys_errlist[e]);
else
fmtprint(format, "unknown error (%d)", e);
}
return FMT_verb;
}
//
// conversion table management
//
static Fmtconv dflt_fmttab[FMT_MAXCONV];
static Fmtconv *
ensure_inittab(Fmtconv fmttab[])
{
int i;
if (fmttab[0] != 0)
return fmttab;
for (i = 0; i < FMT_MAXCONV; i++)
fmttab[i] = fmt_badconv;
fmttab['%'] = litconv;
fmttab['s'] = sconv;
fmttab['c'] = cconv;
fmttab['d'] = dconv;
fmttab['i'] = dconv;
fmttab['o'] = oconv;
fmttab['x'] = xconv;
fmttab['r'] = rconv;
fmttab['m'] = rconv;
fmttab['n'] = nconv;
fmttab['u'] = uconv;
fmttab['h'] = hconv;
fmttab['l'] = lconv;
fmttab['#'] = altconv;
fmttab['-'] = leftconv;
fmttab['.'] = dotconv;
fmttab['*'] = starconv;
fmttab['0'] = zeroconv;
for (i = '1'; i <= '9'; i++)
fmttab[i] = digitconv;
return fmttab;
}
extern Fmtconv
fmtinstall(int c, Fmtconv f)
{
Fmtconv oldf;
ensure_inittab(dflt_fmttab);
c &= FMT_MAXCONV - 1;
oldf = dflt_fmttab[c];
if (f != 0)
dflt_fmttab[c] = f;
return oldf;
}
// ---
static void
fmtinitbuf(Format *format, char *buf, size_t size, int (*grow) (Format *, size_t))
{
format->buf = buf;
format->bufbegin = buf;
format->bufend = buf + size;
format->grow = grow;
format->flushed = 0;
format->error = 0;
format->reqlen = 0;
}
static void
fmtinitarg(Format *format, va_list args, Fmtconv *fmttab, void *client_data)
{
va_copy(format->args, args);
format->fmttab = 0;
format->client_data = 0;
}
//
// the formatting engine
//
extern int
fmtengine(Format *format, const char *fmt)
{
const unsigned char *s = (const unsigned char *)fmt;
Fmtconv *fmttab;
if ((fmttab = format->fmttab) == 0)
fmttab = ensure_inittab(dflt_fmttab);
for (;;) {
int c = *s++;
switch (c) {
case '%':
format->flags = format->f1 = format->f2 = 0;
do
c = *s++;
while ((*fmttab[c])(format, c) == FMT_flag);
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_copy(saveargs, format->args);
va_start(ap, fmt);
va_copy(format->args, ap);
va_end(ap);
n += fmtengine(format, fmt);
va_end(format->args);
va_copy(format->args, saveargs);
return n + format->flushed;
}
// ---
static ssize_t
writeall(int fd, char *buf, size_t n)
{
ssize_t remain = n;
for (int i = 0; remain > 0; buf += i, remain -= i) {
if ((i = write(fd, buf, remain)) <= 0 &&
errno != EAGAIN &&
errno != EWOULDBLOCK)
break;
}
return n - remain;
}
// ---
extern int
fmt_fprint_flush(Format *format, size_t more)
{
size_t n = format->buf - format->bufbegin;
size_t r;
r = writeall(format->u.n, format->bufbegin, n);
format->flushed += r;
format->buf -= r;
format->error |= (r != n);
return format->error;
}
extern int
vfprint(int fd, const char *fmt, va_list ap)
{
char buf[1024];
Format format;
fmtinitbuf(&format, buf, sizeof buf, fmt_fprint_flush);
fmtinitarg(&format, ap, 0, 0);
format.u.n = fd;
fmtengine(&format, fmt);
fmt_fprint_flush(&format, 0);
return format.error ? -1 : format.flushed;
}
extern int
fprint(int fd, const char *fmt, ...)
{
int n;
va_list ap;
va_start(ap, fmt);
n = vfprint(fd, fmt, ap);
va_end(ap);
return n;
}
extern int
print(const char *fmt, ...)
{
int n;
va_list ap;
va_start(ap, fmt);
n = vfprint(1, fmt, ap);
va_end(ap);
return n;
}
extern int
eprint(const char *fmt, ...)
{
int n;
va_list ap;
va_start(ap, fmt);
n = vfprint(2, fmt, ap);
va_end(ap);
return n;
}
// ---
extern int
fmt_memprint_grow(Format *format, size_t more)
{
char *buf = 0;
size_t len = format->bufend - format->bufbegin + 1;
len = (len >= more)
? len * 2
: ((len + more) +
FMT_PRINT_ALLOCSIZE) & ~(FMT_PRINT_ALLOCSIZE - 1);
buf = ((Alloc) format->u.p) (format->bufbegin, len);
if (buf) {
format->buf = buf + (format->buf - format->bufbegin);
format->bufbegin = buf;
format->bufend = buf + len - 1;
return 0;
}
return format->error = 1;
}
extern char *
fmt_memprint(Format *format, const char *fmt, size_t *len)
{
char *buf;
int n;
buf = (*(Alloc) format->u.p) (0, FMT_PRINT_ALLOCSIZE);
if (!buf) {
format->error = 1;
return 0;
}
fmtinitbuf(format, buf, FMT_PRINT_ALLOCSIZE - 1,
fmt_memprint_grow);
n = fmtengine(format, fmt);
*format->buf = '\0';
if (len)
*len = n;
return format->error ? 0 : format->bufbegin;
}
extern char *
vsmprint(Alloc a, size_t *len, const char *fmt, va_list ap)
{
Format format;
fmtinitarg(&format, ap, 0, 0);
format.u.p = (void *)a;
return fmt_memprint(&format, fmt, len);
}
extern char *
smprint(Alloc alloc, size_t *len, const char *fmt, ...)
{
char *result;
va_list ap;
va_start(ap, fmt);
result = vsmprint(alloc, len, fmt, ap);
va_end(ap);
return result;
}
// note -- moved palloc and mprint to mprint.c
// ---
extern int
fmt_snprint_grow(Format *format, size_t more)
{
return format->error = 1;
}
extern int
vsnprint(char *buf, int buflen, const char *fmt, va_list ap)
{
Format format;
fmtinitbuf(&format, buf, buflen - 1, fmt_snprint_grow);
fmtinitarg(&format, ap, 0, 0);
va_copy(format.args, ap);
fmtengine(&format, fmt);
*format.buf = '\0';
return format.reqlen;
}
extern int
snprint(char *buf, int buflen, const char *fmt, ...)
{
int n;
va_list ap;
va_start(ap, fmt);
n = vsnprint(buf, buflen, fmt, ap);
va_end(ap);
return n;
}
extern int
sprint(char *buf, const char *fmt, ...)
{
int n;
va_list ap;
va_start(ap, fmt);
n = vsnprint(buf, FMT_SPRINT_BUFSIZ, fmt, ap);
va_end(ap);
return n;
}