-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.c
306 lines (250 loc) · 7.36 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
#include "print.h"
static void put_cnt_char(int c,int * cnt);
static int strnlen(const char *s, unsigned int size);
static void vprintfmt(void (*putch)(int, int*), void *putdat, const char *fmt, va_list ap);
static void put_cnt_char(int c,int * cnt)
{
uart_putchar(c);
(*cnt) ++;
}
static int strnlen(const char *s, unsigned int size)
{
int n;
for (n = 0; size > 0 && *s != '\0'; s++, size--) n++;
return n;
}
// Get an unsigned int of various possible sizes from a varargs list,
// depending on the lflag parameter.
static unsigned long long getuint(va_list *ap, int lflag)
{
if (lflag >= 2)
return va_arg(*ap, unsigned long long);
else if (lflag)
return va_arg(*ap, unsigned long);
else
return va_arg(*ap, unsigned int);
}
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
static void printnum(void (*putch)(int, int*), void *putdat, unsigned long long num, unsigned base, int width, int padc)
{
if (num >= 16)
{
printnum(putch, putdat, num >> 4, 16, width - 1, padc);
}
else
{
while (--width > 0) putch(padc, putdat);
}
putch("0123456789abcdef"[num & 0x00ffff], putdat);
}
//reduce uneccserssy functions
#else
#if defined( PRINT_ERROR_ENABLE )
/*
* Space or zero padding and a field width are supported for the numeric
* formats only.
*
* The special format %e takes an integer error code
* and prints a string describing the error.
* The integer may be positive or negative,
* so that -E_NO_MEM and E_NO_MEM are equivalent.
*/
static const char * const error_string[MAXERROR] =
{
"unspecified error",
"bad environment",
"invalid parameter",
"out of memory",
"out of environments",
"segmentation fault",
NULL
};
static void printfmt(void (*putch)(int, int*), void *putdat, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintfmt(putch, putdat, fmt, ap);
va_end(ap);
}
#endif
/*
* Print a number (base <= 16) in reverse order,
* using specified putch function and associated pointer putdat.
*/
static void printnum(void (*putch)(int, int*), void *putdat, unsigned long long num, unsigned base, int width, int padc)
{
// first recursively print all preceding (more significant) digits
if (num >= base) {
printnum(putch, putdat, num / base, base, width - 1, padc);
} else {
// print any needed pad characters before first digit
while (--width > 0)
putch(padc, putdat);
}
// then print this (the least significant) digit
putch("0123456789abcdef"[num % base], putdat);
}
// Same as getuint but signed - can't use getuint
// because of sign extension
static long long getint(va_list *ap, int lflag)
{
if (lflag >= 2)
return va_arg(*ap, long long);
else if (lflag)
return va_arg(*ap, long);
else
return va_arg(*ap, int);
}
#endif
static void vprintfmt(void (*putch)(int, int*), void *putdat, const char *fmt, va_list ap)
{
register const char *p;
register int ch;
#if defined( PRINT_ERROR_ENABLE )
register int err;
#endif
unsigned long long num;
int base, lflag, width, precision, altflag;
char padc;
while (1)
{
while ((ch = *(unsigned char *) fmt++) != '%')
{
if (ch == '\0') return;
putch(ch, putdat);
}
// Process a %-escape sequence
padc = ' ';
width = -1;
precision = -1;
lflag = 0;
altflag = 0;
reswitch:
switch (ch = *(unsigned char *) fmt++)
{
// flag to pad on the right
case '-':
padc = '-';
goto reswitch;
// flag to pad with 0's instead of spaces
case '0':
padc = '0';
goto reswitch;
// width field
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
for (precision = 0; ; ++fmt)
{
precision = precision * 10 + ch - '0';
ch = *fmt;
if (ch < '0' || ch > '9') break;
}
goto process_precision;
process_precision:
if (width < 0) width = precision, precision = -1;
goto reswitch;
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
// reduce the uneccerssy format for the AVR chip
#else
case '*':
precision = va_arg(ap, int);
goto process_precision;
case '.':
if (width < 0) width = 0;
goto reswitch;
case '#':
altflag = 1;
goto reswitch;
#if defined( PRINT_ERROR_ENABLE )
// error message
case 'e':
err = va_arg(ap, int);
if (err < 0) err = -err;
if (err >= MAXERROR || (p = error_string[err]) == NULL)
printfmt(putch, putdat, "error %d", err);
else
printfmt(putch, putdat, "%s", p);
break;
#endif
// (signed) decimal
case 'd':
num = getint(&ap, lflag);
if ((long long) num < 0)
{
putch('-', putdat);
num = -(long long) num;
}
base = 10;
goto number;
// unsigned decimal
case 'u':
num = getuint(&ap, lflag);
base = 10;
goto number;
// (unsigned) octal
case 'o':
num = getuint(&ap, lflag);
base = 8;
printnum(putch, putdat, num, base, width, padc);
break;
// long flag (doubled for long long)
case 'l':
lflag++;
goto reswitch;
#endif
// character
case 'c':
putch(va_arg(ap, int), putdat);
break;
// string
case 's':
if ((p = va_arg(ap, char *)) == NULL) p = "(null)";
if (width > 0 && padc != '-')
for (width -= strnlen(p, precision); width > 0; width--) putch(padc, putdat);
for (; (ch = *p++) != '\0' && (precision < 0 || --precision >= 0); width--)
if (altflag && (ch < ' ' || ch > '~')) putch('?', putdat);
else putch(ch, putdat);
for (; width > 0; width--) putch(' ', putdat);
break;
// pointer
case 'p':
putch('0', putdat);
putch('x', putdat);
num = (unsigned long long) (uintptr_t) va_arg(ap, void *);
base = 16;
goto number;
// (unsigned) hexadecimal
case 'x':
num = getuint(&ap, lflag);
base = 16;
number:
printnum(putch, putdat, num, base, width, padc);
break;
// escaped '%' character
case '%':
putch(ch, putdat);
break;
// unrecognized escape sequence - just print it literally
default:
putch('%', putdat);
for (fmt--; fmt[-1] != '%'; fmt--) break;
}
}
}
int print(const char * fmt, ...)
{
va_list ap;
int cnt;
cnt = 0;
va_start(ap,fmt);
vprintfmt(put_cnt_char, &cnt, fmt, ap) ;
va_end(ap);
return cnt;
}