Block extra_data
[akaros.git] / kern / src / printfmt.c
1 // Stripped-down primitive printf-style formatting routines,
2 // used in common by printf, sprintf, fprintf, etc.
3 // This code is also used by both the kernel and user programs.
4
5 #ifdef __SHARC__
6 #pragma nosharc
7 #endif
8
9 #include <ros/common.h>
10 #include <error.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdarg.h>
14 #include <kthread.h>
15 #include <ns.h>
16 #include <acpi.h>
17
18 /* Print a number (base <= 16) in reverse order,
19  * using specified putch function and associated pointer putdat. */
20 void printnum(void (*putch)(int, void**), void **putdat,
21               unsigned long long num, unsigned base, int width, int padc)
22 {
23         unsigned long long temp = num;
24         int nr_digits = 1;
25         /* Determine how many leading zeros we need.
26          * For every digit/nibble beyond base, we do one less width padding */
27         while ((temp /= base)) {
28                 nr_digits++;
29                 width--;
30         }
31         /* And another one less, since we'll always print the last digit */
32         while (--width > 0)
33                 putch(padc, putdat);
34         for (int i = nr_digits; i > 0; i--) {
35                 temp = num;
36                 /* To get digit i, we only div (i-1) times */
37                 for (int j = 0; j < i - 1; j++) {
38                         temp /= base;
39                 }
40                 putch("0123456789abcdef"[temp % base], putdat);
41         }
42 }
43
44 // Main function to format and print a string.
45 #ifdef __DEPUTY__
46 void printfmt(void (*putch)(int, TV(t)), TV(t) putdat, const char *fmt, ...);
47 #else
48 void printfmt(void (*putch)(int, void**), void **putdat, const char *fmt, ...);
49 #endif
50
51 #ifdef __DEPUTY__
52 void vprintfmt(void (*putch)(int, TV(t)), TV(t) putdat, const char *fmt, va_list ap)
53 #else
54 void vprintfmt(void (*putch)(int, void**), void **putdat, const char *fmt, va_list ap)
55 #endif
56 {
57         register const char *NTS p;
58         const char *NTS last_fmt;
59         register int ch, err;
60         unsigned long long num;
61         int base, lflag, width, precision, altflag;
62         char padc;
63         uint8_t *mac, *ip, *mask;
64         struct Gas *g;
65         int i;
66         uint32_t *lp;
67
68         while (1) {
69                 while ((ch = *(unsigned char *) fmt) != '%') {
70                         if (ch == '\0')
71                                 return;
72                         fmt++;
73                         putch(ch, putdat);
74                 }
75                 fmt++;
76
77                 // Process a %-escape sequence
78                 last_fmt = fmt;
79                 padc = ' ';
80                 width = -1;
81                 precision = -1;
82                 lflag = 0;
83                 altflag = 0;
84         reswitch:
85                 switch (ch = *(unsigned char *) fmt++) {
86
87                 // flag to pad on the right
88                 case '-':
89                         padc = '-';
90                         goto reswitch;
91
92                 // flag to pad with 0's instead of spaces
93                 case '0':
94                         padc = '0';
95                         goto reswitch;
96
97                 // width field
98                 case '1':
99                 case '2':
100                 case '3':
101                 case '4':
102                 case '5':
103                 case '6':
104                 case '7':
105                 case '8':
106                 case '9':
107                         for (precision = 0; ; ++fmt) {
108                                 precision = precision * 10 + ch - '0';
109                                 ch = *fmt;
110                                 if (ch < '0' || ch > '9')
111                                         break;
112                         }
113                         goto process_precision;
114
115                 case '*':
116                         precision = va_arg(ap, int);
117                         goto process_precision;
118
119                 case '.':
120                         if (width < 0)
121                                 width = 0;
122                         goto reswitch;
123
124                 case '#':
125                         altflag = 1;
126                         goto reswitch;
127
128                 process_precision:
129                         if (width < 0)
130                                 width = precision, precision = -1;
131                         goto reswitch;
132
133                 // long flag (doubled for long long)
134                 case 'l':
135                         lflag++;
136                         goto reswitch;
137
138                 // character
139                 case 'c':
140                         putch(va_arg(ap, int), putdat);
141                         break;
142
143                 // error message
144                 case 'e':
145                         err = va_arg(ap, int);
146                         if (err < 0)
147                                 err = -err;
148                         if (err >= NUMERRORS)
149                                 printfmt(putch, putdat, "error %d", err);
150                         else
151                                 printfmt(putch, putdat, "%s", error_string[err]);
152                         break;
153
154                 case 'E': // ENET MAC
155                         if ((mac = va_arg(ap, uint8_t *)) == NULL){
156                                 char *s = "00:00:00:00:00:00";
157                                 while(*s)
158                                         putch(*s++, putdat);
159                         }
160                         printemac(putch, putdat, mac);
161                         break;
162                 case 'i':
163                         /* what to do if they screw up? */
164                         if ((lp = va_arg(ap, uint32_t *)) != NULL){
165                                 uint32_t hostfmt;
166                                 for(i = 0; i < 4; i++){
167                                         hnputl(&hostfmt, lp[i]);
168                                         printfmt(putch, putdat, "%08lx", hostfmt);
169                                 }
170                         }
171                         break;
172                 case 'I':
173                         /* what to do if they screw up? */
174                         if ((ip = va_arg(ap, uint8_t *)) != NULL)
175                                 printip(putch, putdat, ip);
176                         break;
177                 case 'M':
178                         /* what to do if they screw up? */
179                         if ((mask = va_arg(ap, uint8_t *)) != NULL)
180                                 printipmask(putch, putdat, mask);
181                         break;
182                 case 'V':
183                         /* what to do if they screw up? */
184                         if ((ip = va_arg(ap, uint8_t *)) != NULL)
185                                 printipv4(putch, putdat, ip);
186                         break;
187
188                 // string
189                 case 's':
190                         if ((p = va_arg(ap, char *)) == NULL)
191                                 p = "(null)";
192                         if (width > 0 && padc != '-')
193                                 for (width -= strnlen(p, precision); width > 0; width--)
194                                         putch(padc, putdat);
195                         for (; (ch = *p) != '\0' && (precision < 0 || --precision >= 0); width--) {
196                                 if (altflag && (ch < ' ' || ch > '~'))
197                                         putch('?', putdat);
198                                 else
199                                         putch(ch, putdat);
200                                 // zra: make sure *p isn't '\0' before inc'ing
201                                 p++;
202                         }
203                         for (; width > 0; width--)
204                                 putch(' ', putdat);
205                         break;
206
207                 case 'd': /* (signed) decimal */
208                         if (lflag >= 2)
209                                 num = va_arg(ap, long long);
210                         else if (lflag)
211                                 num = va_arg(ap, long);
212                         else
213                                 num = va_arg(ap, int);
214                         if ((long long) num < 0) {
215                                 putch('-', putdat);
216                                 num = -(long long) num;
217                         }
218                         base = 10;
219                         goto number;
220
221                 case 'u': /* unsigned decimal */
222                 case 'o': /* (unsigned) octal */
223                 case 'x': /* (unsigned) hexadecimal */
224                         if (lflag >= 2)
225                                 num = va_arg(ap, unsigned long long);
226                         else if (lflag)
227                                 num = va_arg(ap, unsigned long);
228                         else
229                                 num = va_arg(ap, unsigned int);
230                         if (ch == 'u')
231                                 base = 10;
232                         else if (ch == 'o')
233                                 base = 8;
234                         else    /* x */
235                                 base = 16;
236                         goto number;
237
238                 // pointer
239                 case 'p':
240                         putch('0', putdat);
241                         putch('x', putdat);
242                         /* automatically zero-pad pointers, out to the length of a ptr */
243                         padc = '0';
244                         width = sizeof(void*) * 2;      /* 8 bits per byte / 4 bits per char */
245                         num = (unsigned long long)
246                                 (uintptr_t) va_arg(ap, void *);
247                         base = 16;
248                         goto number;
249
250                 number:
251                         printnum(putch, putdat, num, base, width, padc);
252                         break;
253
254                 // escaped '%' character
255                 case '%':
256                         putch(ch, putdat);
257                         break;
258
259                 // unrecognized escape sequence - just print it literally
260                 default:
261                         putch('%', putdat);
262                         fmt = last_fmt;
263                         //for (fmt--; fmt[-1] != '%'; fmt--)
264                                 /* do nothing */;
265                         break;
266                 }
267         }
268 }
269
270 #ifdef __DEPUTY__
271 void printfmt(void (*putch)(int, TV(t)), TV(t) putdat, const char *fmt, ...)
272 #else
273 void printfmt(void (*putch)(int, void**), void **putdat, const char *fmt, ...)
274 #endif
275 {
276         va_list ap;
277
278         va_start(ap, fmt);
279         vprintfmt(putch, putdat, fmt, ap);
280         va_end(ap);
281         check_poison("printfmt");
282 }
283
284 typedef struct sprintbuf {
285         char *BND(__this,ebuf) buf;
286         char *SNT ebuf;
287         int cnt;
288 } sprintbuf_t;
289
290 static void sprintputch(int ch, sprintbuf_t *NONNULL *NONNULL b)
291 {
292         (*b)->cnt++;
293         if ((*b)->buf < (*b)->ebuf)
294                 *((*b)->buf++) = ch;
295 }
296
297 int vsnprintf(char *buf, int n, const char *fmt, va_list ap)
298 {
299         sprintbuf_t b;// = {buf, buf+n-1, 0};
300         sprintbuf_t *COUNT(1) NONNULL bp = &b;
301
302         if (buf == NULL || n < 1)
303                 return -EINVAL;
304
305         b.buf = NULL; // zra : help out the Deputy optimizer a bit
306         b.ebuf = buf+n-1;
307         b.cnt = 0;
308         b.buf = buf;
309
310         // print the string to the buffer
311         #ifdef __DEPUTY__
312         vprintfmt((void*)sprintputch, (sprintbuf_t *NONNULL*NONNULL)&bp, fmt, ap);
313         #else
314         vprintfmt((void*)sprintputch, (void*)&bp, fmt, ap);
315         #endif
316
317         // null terminate the buffer
318         *b.buf = '\0';
319
320         return b.cnt;
321 }
322
323 int snprintf(char *buf, int n, const char *fmt, ...)
324 {
325         va_list ap;
326         int rc;
327
328         va_start(ap, fmt);
329         rc = vsnprintf(buf, n, fmt, ap);
330         va_end(ap);
331
332         check_poison("snprintf");
333         return rc;
334 }
335
336 /* convenience function: do a print, return the pointer to the end. */
337 char *seprintf(char *buf, char *end, const char *fmt, ...)
338 {
339         va_list ap;
340         int rc;
341         int n = end - buf;
342
343         if (n <= 0)
344                 return buf;
345
346         va_start(ap, fmt);
347         rc = vsnprintf(buf, n, fmt, ap);
348         va_end(ap);
349         check_poison("seprintf");
350
351         if (rc >= 0)
352                 return buf + rc;
353         else
354                 return buf;
355 }