Remove advertising clause from all that isn't contrib or userland bin.
[dragonfly.git] / lib / libstand / printf.c
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  * 4. 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  * $FreeBSD: src/lib/libstand/printf.c,v 1.14 2010/07/12 15:32:45 jkim Exp $
36  */
37
38 /*
39  * Standaloneified version of the FreeBSD kernel printf family.
40  */
41
42 #include <sys/types.h>
43 #include <sys/stdint.h>
44 #include <limits.h>
45 #include <string.h>
46 #include "stand.h"
47
48 /*
49  * Note that stdarg.h and the ANSI style va_start macro is used for both
50  * ANSI and traditional C compilers.
51  */
52 #include <stdarg.h>
53
54 struct snprintf_arg {
55         char    *buf;
56         size_t  remain;
57 };
58
59 #define MAXNBUF (sizeof(intmax_t) * CHAR_BIT + 1)
60
61 static char     *ksprintn (char *buf, uintmax_t num, int base, int *len, int upper);
62 static int      kvprintf(const char *, void (*)(int, void *), void *, int,
63                          va_list);
64 static void     putchar_wrapper(int, void *);
65 static void     snprintf_func(int, void *);
66
67 static void
68 putchar_wrapper(int ch, void *arg __unused)
69 {
70         putchar(ch);
71 }
72
73 int
74 printf(const char *fmt, ...)
75 {
76         va_list ap;
77         int retval;
78
79         va_start(ap, fmt);
80         retval = kvprintf(fmt, putchar_wrapper, NULL, 10, ap);
81         va_end(ap);
82         return retval;
83 }
84
85 void
86 vprintf(const char *fmt, va_list ap)
87 {
88
89         kvprintf(fmt, putchar_wrapper, NULL, 10, ap);
90 }
91
92 int
93 sprintf(char *buf, const char *cfmt, ...)
94 {
95         int retval;
96         va_list ap;
97
98         va_start(ap, cfmt);
99         retval = kvprintf(cfmt, NULL, buf, 10, ap);
100         buf[retval] = '\0';
101         va_end(ap);
102         return retval;
103 }
104
105 void
106 vsprintf(char *buf, const char *cfmt, va_list ap)
107 {
108         int     retval;
109         
110         retval = kvprintf(cfmt, NULL, buf, 10, ap);
111         buf[retval] = '\0';
112 }
113
114 int
115 snprintf(char *buf, size_t size, const char *cfmt, ...)
116 {
117         int retval;
118         va_list ap;
119
120         va_start(ap, cfmt);
121         retval = vsnprintf(buf, size, cfmt, ap);
122         __va_end(ap);
123         return(retval);
124 }
125
126 int
127 vsnprintf(char *buf, size_t size, const char *cfmt, va_list ap)
128 {
129         struct snprintf_arg info;
130         int retval;
131
132         info.buf = buf;
133         info.remain = size;
134         retval = kvprintf(cfmt, snprintf_func, &info, 10, ap);
135         if (info.remain >= 1)
136                 *info.buf++ = '\0';
137         return(retval);
138 }
139
140 static void
141 snprintf_func(int ch, void *arg)
142 {
143         struct snprintf_arg * const info = arg;
144
145         if (info->remain >= 2) {
146                 *info->buf++ = ch;
147                 info->remain--;
148         }
149 }
150
151 /*
152  * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
153  * order; return an optional length and a pointer to the last character
154  * written in the buffer (i.e., the first character of the string).
155  * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
156  */
157 static char *
158 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
159 {
160         char *p, c;
161
162         p = nbuf;
163         *p = '\0';
164         do {
165                 c = hex2ascii(num % base);
166                 *++p = upper ? toupper(c) : c;
167         } while (num /= base);
168         if (lenp)
169                 *lenp = p - nbuf;
170         return (p);
171 }
172
173 /*
174  * Scaled down version of printf(3).
175  *
176  * Two additional formats:
177  *
178  * The format %b is supported to decode error registers.
179  * Its usage is:
180  *
181  *      printf("reg=%b\n", regval, "<base><arg>*");
182  *
183  * where <base> is the output base expressed as a control character, e.g.
184  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
185  * the first of which gives the bit number to be inspected (origin 1), and
186  * the next characters (up to a control character, i.e. a character <= 32),
187  * give the name of the register.  Thus:
188  *
189  *      kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
190  *
191  * would produce output:
192  *
193  *      reg=3<BITTWO,BITONE>
194  *
195  */
196 static int
197 kvprintf(char const *fmt, void (*func)(int, void *), void *arg, int radix,
198          va_list ap)
199 {
200 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc, arg); else *d++ = cc; retval++; }
201         char nbuf[MAXNBUF];
202         char *d;
203         const char *p, *percent, *q;
204         int ch, n;
205         uintmax_t num;
206         int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
207         int cflag, hflag, jflag, tflag, zflag;
208         int dwidth, upper;
209         char padc;
210         int stop = 0, retval = 0;
211
212         num = 0;
213         if (!func)
214                 d = (char *) arg;
215         else
216                 d = NULL;
217
218         if (fmt == NULL)
219                 fmt = "(fmt null)\n";
220
221         if (radix < 2 || radix > 36)
222                 radix = 10;
223
224         for (;;) {
225                 padc = ' ';
226                 width = 0;
227                 while ((ch = (u_char)*fmt++) != '%' || stop) {
228                         if (ch == '\0')
229                                 return (retval);
230                         PCHAR(ch);
231                 }
232                 percent = fmt - 1;
233                 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
234                 sign = 0; dot = 0; dwidth = 0; upper = 0;
235                 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
236 reswitch:       switch (ch = (u_char)*fmt++) {
237                 case '.':
238                         dot = 1;
239                         goto reswitch;
240                 case '#':
241                         sharpflag = 1;
242                         goto reswitch;
243                 case '+':
244                         sign = 1;
245                         goto reswitch;
246                 case '-':
247                         ladjust = 1;
248                         goto reswitch;
249                 case '%':
250                         PCHAR(ch);
251                         break;
252                 case '*':
253                         if (!dot) {
254                                 width = va_arg(ap, int);
255                                 if (width < 0) {
256                                         ladjust = !ladjust;
257                                         width = -width;
258                                 }
259                         } else {
260                                 dwidth = va_arg(ap, int);
261                         }
262                         goto reswitch;
263                 case '0':
264                         if (!dot) {
265                                 padc = '0';
266                                 goto reswitch;
267                         }
268                 case '1': case '2': case '3': case '4':
269                 case '5': case '6': case '7': case '8': case '9':
270                                 for (n = 0;; ++fmt) {
271                                         n = n * 10 + ch - '0';
272                                         ch = *fmt;
273                                         if (ch < '0' || ch > '9')
274                                                 break;
275                                 }
276                         if (dot)
277                                 dwidth = n;
278                         else
279                                 width = n;
280                         goto reswitch;
281                 case 'b':
282                         num = (u_int)va_arg(ap, int);
283                         p = va_arg(ap, char *);
284                         for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
285                                 PCHAR(*q--);
286
287                         if (num == 0)
288                                 break;
289
290                         for (tmp = 0; *p;) {
291                                 n = *p++;
292                                 if (num & (1 << (n - 1))) {
293                                         PCHAR(tmp ? ',' : '<');
294                                         for (; (n = *p) > ' '; ++p)
295                                                 PCHAR(n);
296                                         tmp = 1;
297                                 } else
298                                         for (; *p > ' '; ++p)
299                                                 continue;
300                         }
301                         if (tmp)
302                                 PCHAR('>');
303                         break;
304                 case 'c':
305                         PCHAR(va_arg(ap, int));
306                         break;
307                 case 'd':
308                 case 'i':
309                         base = 10;
310                         sign = 1;
311                         goto handle_sign;
312                 case 'h':
313                         if (hflag) {
314                                 hflag = 0;
315                                 cflag = 1;
316                         } else
317                                 hflag = 1;
318                         goto reswitch;
319                 case 'j':
320                         jflag = 1;
321                         goto reswitch;
322                 case 'l':
323                         if (lflag) {
324                                 lflag = 0;
325                                 qflag = 1;
326                         } else
327                                 lflag = 1;
328                         goto reswitch;
329                 case 'n':
330                         if (jflag)
331                                 *(va_arg(ap, intmax_t *)) = retval;
332                         else if (qflag)
333                                 *(va_arg(ap, quad_t *)) = retval;
334                         else if (lflag)
335                                 *(va_arg(ap, long *)) = retval;
336                         else if (zflag)
337                                 *(va_arg(ap, size_t *)) = retval;
338                         else if (hflag)
339                                 *(va_arg(ap, short *)) = retval;
340                         else if (cflag)
341                                 *(va_arg(ap, char *)) = retval;
342                         else
343                                 *(va_arg(ap, int *)) = retval;
344                         break;
345                 case 'o':
346                         base = 8;
347                         goto handle_nosign;
348                 case 'p':
349                         base = 16;
350                         sharpflag = (width == 0);
351                         sign = 0;
352                         num = (uintptr_t)va_arg(ap, void *);
353                         goto number;
354                 case 'q':
355                         qflag = 1;
356                         goto reswitch;
357                 case 'r':
358                         base = radix;
359                         if (sign)
360                                 goto handle_sign;
361                         goto handle_nosign;
362                 case 's':
363                         p = va_arg(ap, char *);
364                         if (p == NULL)
365                                 p = "(null)";
366                         if (!dot)
367                                 n = strlen (p);
368                         else
369                                 for (n = 0; n < dwidth && p[n]; n++)
370                                         continue;
371
372                         width -= n;
373
374                         if (!ladjust && width > 0)
375                                 while (width--)
376                                         PCHAR(padc);
377                         while (n--)
378                                 PCHAR(*p++);
379                         if (ladjust && width > 0)
380                                 while (width--)
381                                         PCHAR(padc);
382                         break;
383                 case 't':
384                         tflag = 1;
385                         goto reswitch;
386                 case 'u':
387                         base = 10;
388                         goto handle_nosign;
389                 case 'X':
390                         upper = 1;
391                 case 'x':
392                         base = 16;
393                         goto handle_nosign;
394                 case 'y':
395                         base = 16;
396                         sign = 1;
397                         goto handle_sign;
398                 case 'z':
399                         zflag = 1;
400                         goto reswitch;
401 handle_nosign:
402                         sign = 0;
403                         if (jflag)
404                                 num = va_arg(ap, uintmax_t);
405                         else if (qflag)
406                                 num = va_arg(ap, u_quad_t);
407                         else if (tflag)
408                                 num = va_arg(ap, ptrdiff_t);
409                         else if (lflag)
410                                 num = va_arg(ap, u_long);
411                         else if (zflag)
412                                 num = va_arg(ap, size_t);
413                         else if (hflag)
414                                 num = (u_short)va_arg(ap, int);
415                         else if (cflag)
416                                 num = (u_char)va_arg(ap, int);
417                         else
418                                 num = va_arg(ap, u_int);
419                         goto number;
420 handle_sign:
421                         if (jflag)
422                                 num = va_arg(ap, intmax_t);
423                         else if (qflag)
424                                 num = va_arg(ap, quad_t);
425                         else if (tflag)
426                                 num = va_arg(ap, ptrdiff_t);
427                         else if (lflag)
428                                 num = va_arg(ap, long);
429                         else if (zflag)
430                                 num = va_arg(ap, ssize_t);
431                         else if (hflag)
432                                 num = (short)va_arg(ap, int);
433                         else if (cflag)
434                                 num = (char)va_arg(ap, int);
435                         else
436                                 num = va_arg(ap, int);
437 number:
438                         if (sign && (intmax_t)num < 0) {
439                                 neg = 1;
440                                 num = -(intmax_t)num;
441                         }
442                         p = ksprintn(nbuf, num, base, &n, upper);
443                         tmp = 0;
444                         if (sharpflag && num != 0) {
445                                 if (base == 8)
446                                         tmp++;
447                                 else if (base == 16)
448                                         tmp += 2;
449                         }
450                         if (neg)
451                                 tmp++;
452
453                         if (!ladjust && padc == '0')
454                                 dwidth = width - tmp;
455                         width -= tmp + imax(dwidth, n);
456                         dwidth -= n;
457                         if (!ladjust)
458                                 while (width-- > 0)
459                                         PCHAR(' ');
460                         if (neg)
461                                 PCHAR('-');
462                         if (sharpflag && num != 0) {
463                                 if (base == 8) {
464                                         PCHAR('0');
465                                 } else if (base == 16) {
466                                         PCHAR('0');
467                                         PCHAR('x');
468                                 }
469                         }
470                         while (dwidth-- > 0)
471                                 PCHAR('0');
472
473                         while (*p)
474                                 PCHAR(*p--);
475
476                         if (ladjust)
477                                 while (width-- > 0)
478                                         PCHAR(' ');
479
480                         break;
481                 default:
482                         while (percent < fmt)
483                                 PCHAR(*percent++);
484                         /*
485                          * Since we ignore an formatting argument it is no
486                          * longer safe to obey the remaining formatting
487                          * arguments as the arguments will no longer match
488                          * the format specs.
489                          */
490                         stop = 1;
491                         break;
492                 }
493         }
494 #undef PCHAR
495 }