forked from wryun/es-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.c
362 lines (305 loc) · 7.21 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
/* print.c -- formatted printing routines ($Revision: 1.1.1.1 $) */
#include "es.h"
#include "print.h"
#define MAXCONV 256
/*
* conversion functions
* true return -> flag changes only, not a conversion
*/
#define Flag(name, flag) \
static Boolean name(Format *format) { \
format->flags |= flag; \
return TRUE; \
}
Flag(uconv, FMT_unsigned)
Flag(hconv, FMT_short)
Flag(longconv, FMT_long)
Flag(altconv, FMT_altform)
Flag(leftconv, FMT_leftside)
Flag(dotconv, FMT_f2set)
static Boolean digitconv(Format *format) {
int c = format->invoker;
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 Boolean zeroconv(Format *format) {
if (format->flags & (FMT_f1set | FMT_f2set))
return digitconv(format);
format->flags |= FMT_zeropad;
return TRUE;
}
static void pad(Format *format, long len, int c) {
while (len-- > 0)
fmtputc(format, c);
}
static Boolean sconv(Format *format) {
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 *utoa(unsigned long u, char *t, unsigned int radix, 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, char *altform) {
static char * 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_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 = 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 Boolean cconv(Format *format) {
fmtputc(format, va_arg(format->args, int));
return FALSE;
}
static Boolean dconv(Format *format) {
intconv(format, 10, 0, "");
return FALSE;
}
static Boolean oconv(Format *format) {
intconv(format, 8, 0, "0");
return FALSE;
}
static Boolean xconv(Format *format) {
intconv(format, 16, 0, "0x");
return FALSE;
}
static Boolean pctconv(Format *format) {
fmtputc(format, '%');
return FALSE;
}
static Boolean badconv(Format *format) {
panic("bad conversion character in printfmt: %%%c", format->invoker);
return FALSE; /* hush up gcc -Wall */
}
/*
* conversion table management
*/
static Conv *fmttab;
static void inittab(void) {
int i;
fmttab = ealloc(MAXCONV * sizeof (Conv));
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['h'] = hconv;
fmttab['l'] = longconv;
fmttab['#'] = altconv;
fmttab['-'] = leftconv;
fmttab['.'] = dotconv;
fmttab['0'] = zeroconv;
for (i = '1'; i <= '9'; i++)
fmttab[i] = digitconv;
}
Conv fmtinstall(int c, Conv f) {
Conv oldf;
if (fmttab == 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 char *s = (unsigned char *) fmt;
if (fmttab[0] == NULL)
inittab();
for (;;) {
int c = *s++;
switch (c) {
case '%':
format->flags = format->f1 = format->f2 = 0;
do
format->invoker = c = *s++;
while ((*fmttab[c])(format));
break;
case '\0':
return format->buf - format->bufbegin + format->flushed;
default:
fmtputc(format, c);
break;
}
}
}
/*
* the public entry points
*/
extern int fmtprint VARARGS2(Format *, format, const char *, fmt) {
int n = -format->flushed;
#if NO_VA_LIST_ASSIGN
va_list saveargs;
memcpy(saveargs, format->args, sizeof(va_list));
#else
va_list saveargs = format->args;
#endif
VA_START(format->args, fmt);
n += printfmt(format, fmt);
va_end(format->args);
va_copy(format->args, saveargs);
return n + format->flushed;
}
static int fprint_flush(Format *format, size_t UNUSED more) {
size_t n = format->buf - format->bufbegin;
char *buf = format->bufbegin;
format->flushed += n;
format->buf = format->bufbegin;
while (n != 0) {
int written = write(format->u.n, buf, n);
if (written == -1)
return errno;
n -= written;
}
return 0;
}
static int fdprint(Format *format, int fd, const char *fmt) {
char buf[FPRINT_BUFSIZ];
int err;
format->buf = buf;
format->bufbegin = buf;
format->bufend = buf + sizeof buf;
format->grow = fprint_flush;
format->flushed = 0;
format->u.n = fdmap(fd);
gcdisable();
printfmt(format, fmt);
err = fprint_flush(format, 0);
gcenable();
return err;
}
extern int fprint VARARGS2(int, fd, const char *, fmt) {
int err;
Format format;
VA_START(format.args, fmt);
err = fdprint(&format, fd, fmt);
va_end(format.args);
if (err != 0)
fail("es:fprint", "fprint: %s", esstrerror(err));
return format.flushed;
}
extern int print VARARGS1(const char *, fmt) {
int err;
Format format;
VA_START(format.args, fmt);
err = fdprint(&format, 1, fmt);
va_end(format.args);
if (err != 0)
fail("es:print", "print: %s", esstrerror(err));
return format.flushed;
}
extern int eprint VARARGS1(const char *, fmt) {
int err;
Format format;
VA_START(format.args, fmt);
err = fdprint(&format, 2, fmt);
va_end(format.args);
if (err != 0)
fail("es:eprint", "eprint: %s", esstrerror(err));
return format.flushed;
}
extern Noreturn panic VARARGS1(const char *, fmt) {
Format format;
gcdisable();
VA_START(format.args, fmt);
/* ignore the exception, we're already busy dying */
ExceptionHandler
eprint("es panic: ");
EndExceptionHandler
fdprint(&format, 2, fmt);
va_end(format.args);
eprint("\n");
esexit(1);
}