1 /*-
2 * Copyright (c) 1986, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
35 */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 /*
41 * Standaloneified version of the FreeBSD kernel printf family.
42 */
43
44 #include <sys/types.h>
45 #include <sys/stddef.h>
46 #include <sys/stdint.h>
47 #include <limits.h>
48 #include <string.h>
49 #include "stand.h"
50
51 /*
52 * Note that stdarg.h and the ANSI style va_start macro is used for both
53 * ANSI and traditional C compilers.
54 */
55 #include <machine/stdarg.h>
56
57 #define MAXNBUF (sizeof(intmax_t) * CHAR_BIT + 1)
58
59 typedef void (kvprintf_fn_t)(int, void *);
60
61 static char *ksprintn (char *buf, uintmax_t num, int base, int *len, int upper);
62 static int kvprintf(char const *fmt, kvprintf_fn_t *func, void *arg, int radix, va_list ap);
63
64 static void
putchar_wrapper(int cc,void * arg)65 putchar_wrapper(int cc, void *arg)
66 {
67
68 putchar(cc);
69 }
70
71 int
printf(const char * fmt,...)72 printf(const char *fmt, ...)
73 {
74 va_list ap;
75 int retval;
76
77 va_start(ap, fmt);
78 retval = kvprintf(fmt, putchar_wrapper, NULL, 10, ap);
79 va_end(ap);
80 return retval;
81 }
82
83 int
vprintf(const char * fmt,va_list ap)84 vprintf(const char *fmt, va_list ap)
85 {
86
87 return (kvprintf(fmt, putchar_wrapper, NULL, 10, ap));
88 }
89
90 int
sprintf(char * buf,const char * cfmt,...)91 sprintf(char *buf, const char *cfmt, ...)
92 {
93 int retval;
94 va_list ap;
95
96 va_start(ap, cfmt);
97 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
98 buf[retval] = '\0';
99 va_end(ap);
100 return retval;
101 }
102
103 struct print_buf {
104 char *buf;
105 size_t size;
106 };
107
108 static void
snprint_func(int ch,void * arg)109 snprint_func(int ch, void *arg)
110 {
111 struct print_buf *pbuf = arg;
112
113 if (pbuf->size < 2) {
114 /*
115 * Reserve last buffer position for the terminating
116 * character:
117 */
118 return;
119 }
120 *(pbuf->buf)++ = ch;
121 pbuf->size--;
122 }
123
124 int
asprintf(char ** buf,const char * cfmt,...)125 asprintf(char **buf, const char *cfmt, ...)
126 {
127 int retval;
128 struct print_buf arg;
129 va_list ap;
130
131 *buf = NULL;
132 va_start(ap, cfmt);
133 retval = kvprintf(cfmt, NULL, NULL, 10, ap);
134 va_end(ap);
135 if (retval <= 0)
136 return (-1);
137
138 arg.size = retval + 1;
139 arg.buf = *buf = malloc(arg.size);
140 if (*buf == NULL)
141 return (-1);
142
143 va_start(ap, cfmt);
144 retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
145 va_end(ap);
146
147 if (arg.size >= 1)
148 *(arg.buf)++ = 0;
149 return (retval);
150 }
151
152 int
snprintf(char * buf,size_t size,const char * cfmt,...)153 snprintf(char *buf, size_t size, const char *cfmt, ...)
154 {
155 int retval;
156 va_list ap;
157 struct print_buf arg;
158
159 arg.buf = buf;
160 arg.size = size;
161
162 va_start(ap, cfmt);
163 retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
164 va_end(ap);
165
166 if (arg.size >= 1)
167 *(arg.buf)++ = 0;
168 return retval;
169 }
170
171 int
vsnprintf(char * buf,size_t size,const char * cfmt,va_list ap)172 vsnprintf(char *buf, size_t size, const char *cfmt, va_list ap)
173 {
174 struct print_buf arg;
175 int retval;
176
177 arg.buf = buf;
178 arg.size = size;
179
180 retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
181
182 if (arg.size >= 1)
183 *(arg.buf)++ = 0;
184
185 return (retval);
186 }
187
188 int
vsprintf(char * buf,const char * cfmt,va_list ap)189 vsprintf(char *buf, const char *cfmt, va_list ap)
190 {
191 int retval;
192
193 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
194 buf[retval] = '\0';
195
196 return (retval);
197 }
198
199 /*
200 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
201 * order; return an optional length and a pointer to the last character
202 * written in the buffer (i.e., the first character of the string).
203 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
204 */
205 static char *
ksprintn(char * nbuf,uintmax_t num,int base,int * lenp,int upper)206 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
207 {
208 char *p, c;
209
210 p = nbuf;
211 *p = '\0';
212 do {
213 c = hex2ascii(num % base);
214 *++p = upper ? toupper(c) : c;
215 } while (num /= base);
216 if (lenp)
217 *lenp = p - nbuf;
218 return (p);
219 }
220
221 /*
222 * Scaled down version of printf(3).
223 *
224 * Two additional formats:
225 *
226 * The format %b is supported to decode error registers.
227 * Its usage is:
228 *
229 * printf("reg=%b\n", regval, "<base><arg>*");
230 *
231 * where <base> is the output base expressed as a control character, e.g.
232 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
233 * the first of which gives the bit number to be inspected (origin 1), and
234 * the next characters (up to a control character, i.e. a character <= 32),
235 * give the name of the register. Thus:
236 *
237 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE");
238 *
239 * would produce output:
240 *
241 * reg=3<BITTWO,BITONE>
242 *
243 * XXX: %D -- Hexdump, takes pointer and separator string:
244 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
245 * ("%*D", len, ptr, " " -> XX XX XX XX ...
246 */
247 static int
kvprintf(char const * fmt,kvprintf_fn_t * func,void * arg,int radix,va_list ap)248 kvprintf(char const *fmt, kvprintf_fn_t *func, void *arg, int radix, va_list ap)
249 {
250 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc, arg); else *d++ = cc; retval++; }
251 char nbuf[MAXNBUF];
252 char *d;
253 const char *p, *percent, *q;
254 uint16_t *S;
255 u_char *up;
256 int ch, n;
257 uintmax_t num;
258 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
259 int cflag, hflag, jflag, tflag, zflag;
260 int dwidth, upper;
261 char padc;
262 int stop = 0, retval = 0;
263
264 num = 0;
265 if (!func)
266 d = (char *) arg;
267 else
268 d = NULL;
269
270 if (fmt == NULL)
271 fmt = "(fmt null)\n";
272
273 if (radix < 2 || radix > 36)
274 radix = 10;
275
276 for (;;) {
277 padc = ' ';
278 width = 0;
279 while ((ch = (u_char)*fmt++) != '%' || stop) {
280 if (ch == '\0')
281 return (retval);
282 PCHAR(ch);
283 }
284 percent = fmt - 1;
285 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
286 sign = 0; dot = 0; dwidth = 0; upper = 0;
287 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
288 reswitch: switch (ch = (u_char)*fmt++) {
289 case '.':
290 dot = 1;
291 goto reswitch;
292 case '#':
293 sharpflag = 1;
294 goto reswitch;
295 case '+':
296 sign = 1;
297 goto reswitch;
298 case '-':
299 ladjust = 1;
300 goto reswitch;
301 case '%':
302 PCHAR(ch);
303 break;
304 case '*':
305 if (!dot) {
306 width = va_arg(ap, int);
307 if (width < 0) {
308 ladjust = !ladjust;
309 width = -width;
310 }
311 } else {
312 dwidth = va_arg(ap, int);
313 }
314 goto reswitch;
315 case '0':
316 if (!dot) {
317 padc = '0';
318 goto reswitch;
319 }
320 case '1': case '2': case '3': case '4':
321 case '5': case '6': case '7': case '8': case '9':
322 for (n = 0;; ++fmt) {
323 n = n * 10 + ch - '0';
324 ch = *fmt;
325 if (ch < '0' || ch > '9')
326 break;
327 }
328 if (dot)
329 dwidth = n;
330 else
331 width = n;
332 goto reswitch;
333 case 'b':
334 num = (u_int)va_arg(ap, int);
335 p = va_arg(ap, char *);
336 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
337 PCHAR(*q--);
338
339 if (num == 0)
340 break;
341
342 for (tmp = 0; *p;) {
343 n = *p++;
344 if (num & (1 << (n - 1))) {
345 PCHAR(tmp ? ',' : '<');
346 for (; (n = *p) > ' '; ++p)
347 PCHAR(n);
348 tmp = 1;
349 } else
350 for (; *p > ' '; ++p)
351 continue;
352 }
353 if (tmp)
354 PCHAR('>');
355 break;
356 case 'c':
357 PCHAR(va_arg(ap, int));
358 break;
359 case 'D':
360 up = va_arg(ap, u_char *);
361 p = va_arg(ap, char *);
362 if (!width)
363 width = 16;
364 while(width--) {
365 PCHAR(hex2ascii(*up >> 4));
366 PCHAR(hex2ascii(*up & 0x0f));
367 up++;
368 if (width)
369 for (q=p;*q;q++)
370 PCHAR(*q);
371 }
372 break;
373 case 'd':
374 case 'i':
375 base = 10;
376 sign = 1;
377 goto handle_sign;
378 case 'h':
379 if (hflag) {
380 hflag = 0;
381 cflag = 1;
382 } else
383 hflag = 1;
384 goto reswitch;
385 case 'j':
386 jflag = 1;
387 goto reswitch;
388 case 'l':
389 if (lflag) {
390 lflag = 0;
391 qflag = 1;
392 } else
393 lflag = 1;
394 goto reswitch;
395 case 'n':
396 if (jflag)
397 *(va_arg(ap, intmax_t *)) = retval;
398 else if (qflag)
399 *(va_arg(ap, quad_t *)) = retval;
400 else if (lflag)
401 *(va_arg(ap, long *)) = retval;
402 else if (zflag)
403 *(va_arg(ap, size_t *)) = retval;
404 else if (hflag)
405 *(va_arg(ap, short *)) = retval;
406 else if (cflag)
407 *(va_arg(ap, char *)) = retval;
408 else
409 *(va_arg(ap, int *)) = retval;
410 break;
411 case 'o':
412 base = 8;
413 goto handle_nosign;
414 case 'p':
415 base = 16;
416 sharpflag = (width == 0);
417 sign = 0;
418 num = (uintptr_t)va_arg(ap, void *);
419 goto number;
420 case 'q':
421 qflag = 1;
422 goto reswitch;
423 case 'r':
424 base = radix;
425 if (sign)
426 goto handle_sign;
427 goto handle_nosign;
428 case 's':
429 p = va_arg(ap, char *);
430 if (p == NULL)
431 p = "(null)";
432 if (!dot)
433 n = strlen (p);
434 else
435 for (n = 0; n < dwidth && p[n]; n++)
436 continue;
437
438 width -= n;
439
440 if (!ladjust && width > 0)
441 while (width--)
442 PCHAR(padc);
443 while (n--)
444 PCHAR(*p++);
445 if (ladjust && width > 0)
446 while (width--)
447 PCHAR(padc);
448 break;
449 case 'S': /* Assume console can cope with wide chars */
450 for (S = va_arg(ap, uint16_t *); *S != 0; S++)
451 PCHAR(*S);
452 break;
453 case 't':
454 tflag = 1;
455 goto reswitch;
456 case 'u':
457 base = 10;
458 goto handle_nosign;
459 case 'X':
460 upper = 1;
461 case 'x':
462 base = 16;
463 goto handle_nosign;
464 case 'y':
465 base = 16;
466 sign = 1;
467 goto handle_sign;
468 case 'z':
469 zflag = 1;
470 goto reswitch;
471 handle_nosign:
472 sign = 0;
473 if (jflag)
474 num = va_arg(ap, uintmax_t);
475 else if (qflag)
476 num = va_arg(ap, u_quad_t);
477 else if (tflag)
478 num = va_arg(ap, ptrdiff_t);
479 else if (lflag)
480 num = va_arg(ap, u_long);
481 else if (zflag)
482 num = va_arg(ap, size_t);
483 else if (hflag)
484 num = (u_short)va_arg(ap, int);
485 else if (cflag)
486 num = (u_char)va_arg(ap, int);
487 else
488 num = va_arg(ap, u_int);
489 goto number;
490 handle_sign:
491 if (jflag)
492 num = va_arg(ap, intmax_t);
493 else if (qflag)
494 num = va_arg(ap, quad_t);
495 else if (tflag)
496 num = va_arg(ap, ptrdiff_t);
497 else if (lflag)
498 num = va_arg(ap, long);
499 else if (zflag)
500 num = va_arg(ap, ssize_t);
501 else if (hflag)
502 num = (short)va_arg(ap, int);
503 else if (cflag)
504 num = (char)va_arg(ap, int);
505 else
506 num = va_arg(ap, int);
507 number:
508 if (sign && (intmax_t)num < 0) {
509 neg = 1;
510 num = -(intmax_t)num;
511 }
512 p = ksprintn(nbuf, num, base, &n, upper);
513 tmp = 0;
514 if (sharpflag && num != 0) {
515 if (base == 8)
516 tmp++;
517 else if (base == 16)
518 tmp += 2;
519 }
520 if (neg)
521 tmp++;
522
523 if (!ladjust && padc == '0')
524 dwidth = width - tmp;
525 width -= tmp + imax(dwidth, n);
526 dwidth -= n;
527 if (!ladjust)
528 while (width-- > 0)
529 PCHAR(' ');
530 if (neg)
531 PCHAR('-');
532 if (sharpflag && num != 0) {
533 if (base == 8) {
534 PCHAR('0');
535 } else if (base == 16) {
536 PCHAR('0');
537 PCHAR('x');
538 }
539 }
540 while (dwidth-- > 0)
541 PCHAR('0');
542
543 while (*p)
544 PCHAR(*p--);
545
546 if (ladjust)
547 while (width-- > 0)
548 PCHAR(' ');
549
550 break;
551 default:
552 while (percent < fmt)
553 PCHAR(*percent++);
554 /*
555 * Since we ignore a formatting argument it is no
556 * longer safe to obey the remaining formatting
557 * arguments as the arguments will no longer match
558 * the format specs.
559 */
560 stop = 1;
561 break;
562 }
563 }
564 #undef PCHAR
565 }
566