Boot loader fixes - fix recursive malloc()/free() errors, NULL freed fields
[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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)subr_prf.c  8.3 (Berkeley) 1/21/94
39  * $FreeBSD: src/lib/libstand/printf.c,v 1.4 1999/12/27 08:45:14 peter Exp $
40  * $DragonFly: src/lib/libstand/printf.c,v 1.6 2005/12/11 02:27:26 swildner Exp $
41  */
42
43 /*
44  * Standaloneified version of the FreeBSD kernel printf family.
45  */
46
47 #include <sys/types.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 <stdarg.h>
56
57 struct snprintf_arg {
58         char    *buf;
59         size_t  remain;
60 };
61
62 static char     *ksprintn(u_long, int, int *);
63 static int      kvprintf(const char *, void (*)(int, void *), void *, int,
64                          va_list);
65 static void     putchar_wrapper(int, void *);
66 static void     snprintf_func(int, void *);
67
68 static void
69 putchar_wrapper(int ch, void *arg __unused)
70 {
71         putchar(ch);
72 }
73
74 int
75 printf(const char *fmt, ...)
76 {
77         va_list ap;
78         int retval;
79
80         va_start(ap, fmt);
81         retval = kvprintf(fmt, putchar_wrapper, NULL, 10, ap);
82         va_end(ap);
83         return retval;
84 }
85
86 void
87 vprintf(const char *fmt, va_list ap)
88 {
89
90         kvprintf(fmt, putchar_wrapper, NULL, 10, ap);
91 }
92
93 int
94 sprintf(char *buf, const char *cfmt, ...)
95 {
96         int retval;
97         va_list ap;
98
99         va_start(ap, cfmt);
100         retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
101         buf[retval] = '\0';
102         va_end(ap);
103         return retval;
104 }
105
106 void
107 vsprintf(char *buf, const char *cfmt, va_list ap)
108 {
109         int     retval;
110         
111         retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
112         buf[retval] = '\0';
113 }
114
115 int
116 snprintf(char *buf, size_t size, const char *cfmt, ...)
117 {
118         int retval;
119         va_list ap;
120
121         va_start(ap, cfmt);
122         retval = vsnprintf(buf, size, cfmt, ap);
123         __va_end(ap);
124         return(retval);
125 }
126
127 int
128 vsnprintf(char *buf, size_t size, const char *cfmt, va_list ap)
129 {
130         struct snprintf_arg info;
131         int retval;
132
133         info.buf = buf;
134         info.remain = size;
135         retval = kvprintf(cfmt, snprintf_func, &info, 10, ap);
136         if (info.remain >= 1)
137                 *info.buf++ = '\0';
138         return(retval);
139 }
140
141 static void
142 snprintf_func(int ch, void *arg)
143 {
144         struct snprintf_arg * const info = arg;
145
146         if (info->remain >= 2) {
147                 *info->buf++ = ch;
148                 info->remain--;
149         }
150 }
151
152 /*
153  * Put a number (base <= 16) in a buffer in reverse order; return an
154  * optional length and a pointer to the NULL terminated (preceded?)
155  * buffer.
156  */
157 static char *
158 ksprintn(u_long ul, int base, int *lenp)
159 {                                       /* A long in base 8, plus NULL. */
160         static char buf[sizeof(long) * NBBY / 3 + 2];
161         char *p;
162
163         p = buf;
164         do {
165                 *++p = hex2ascii(ul % base);
166         } while (ul /= base);
167         if (lenp)
168                 *lenp = p - buf;
169         return (p);
170 }
171
172 /*
173  * Scaled down version of printf(3).
174  *
175  * Two additional formats:
176  *
177  * The format %b is supported to decode error registers.
178  * Its usage is:
179  *
180  *      printf("reg=%b\n", regval, "<base><arg>*");
181  *
182  * where <base> is the output base expressed as a control character, e.g.
183  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
184  * the first of which gives the bit number to be inspected (origin 1), and
185  * the next characters (up to a control character, i.e. a character <= 32),
186  * give the name of the register.  Thus:
187  *
188  *      kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
189  *
190  * would produce output:
191  *
192  *      reg=3<BITTWO,BITONE>
193  *
194  * XXX:  %D  -- Hexdump, takes pointer and separator string:
195  *              ("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
196  *              ("%*D", len, ptr, " " -> XX XX XX XX ...
197  */
198 static int
199 kvprintf(char const *fmt, void (*func)(int, void *), void *arg, int radix,
200          va_list ap)
201 {
202 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc, arg); else *d++ = cc; retval++; }
203         char *p, *q, *d;
204         u_char *up;
205         int ch, n;
206         u_long ul;
207         int base, lflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
208         int dwidth;
209         char padc;
210         int retval = 0;
211
212         if (!func)
213                 d = (char *) arg;
214         else
215                 d = NULL;
216
217         if (fmt == NULL)
218                 fmt = "(fmt null)\n";
219
220         if (radix < 2 || radix > 36)
221                 radix = 10;
222
223         for (;;) {
224                 padc = ' ';
225                 width = 0;
226                 while ((ch = (u_char)*fmt++) != '%') {
227                         if (ch == '\0') 
228                                 return retval;
229                         PCHAR(ch);
230                 }
231                 lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
232                 sign = 0; dot = 0; dwidth = 0;
233 reswitch:       switch (ch = (u_char)*fmt++) {
234                 case '.':
235                         dot = 1;
236                         goto reswitch;
237                 case '#':
238                         sharpflag = 1;
239                         goto reswitch;
240                 case '+':
241                         sign = 1;
242                         goto reswitch;
243                 case '-':
244                         ladjust = 1;
245                         goto reswitch;
246                 case '%':
247                         PCHAR(ch);
248                         break;
249                 case '*':
250                         if (!dot) {
251                                 width = va_arg(ap, int);
252                                 if (width < 0) {
253                                         ladjust = !ladjust;
254                                         width = -width;
255                                 }
256                         } else {
257                                 dwidth = va_arg(ap, int);
258                         }
259                         goto reswitch;
260                 case '0':
261                         if (!dot) {
262                                 padc = '0';
263                                 goto reswitch;
264                         }
265                 case '1': case '2': case '3': case '4':
266                 case '5': case '6': case '7': case '8': case '9':
267                                 for (n = 0;; ++fmt) {
268                                         n = n * 10 + ch - '0';
269                                         ch = *fmt;
270                                         if (ch < '0' || ch > '9')
271                                                 break;
272                                 }
273                         if (dot)
274                                 dwidth = n;
275                         else
276                                 width = n;
277                         goto reswitch;
278                 case 'b':
279                         ul = va_arg(ap, int);
280                         p = va_arg(ap, char *);
281                         for (q = ksprintn(ul, *p++, NULL); *q;)
282                                 PCHAR(*q--);
283
284                         if (!ul)
285                                 break;
286
287                         for (tmp = 0; *p;) {
288                                 n = *p++;
289                                 if (ul & (1 << (n - 1))) {
290                                         PCHAR(tmp ? ',' : '<');
291                                         for (; (n = *p) > ' '; ++p)
292                                                 PCHAR(n);
293                                         tmp = 1;
294                                 } else
295                                         for (; *p > ' '; ++p)
296                                                 continue;
297                         }
298                         if (tmp)
299                                 PCHAR('>');
300                         break;
301                 case 'c':
302                         PCHAR(va_arg(ap, int));
303                         break;
304                 case 'D':
305                         up = va_arg(ap, u_char *);
306                         p = va_arg(ap, char *);
307                         if (!width)
308                                 width = 16;
309                         while(width--) {
310                                 PCHAR(hex2ascii(*up >> 4));
311                                 PCHAR(hex2ascii(*up & 0x0f));
312                                 up++;
313                                 if (width)
314                                         for (q=p;*q;q++)
315                                                 PCHAR(*q);
316                         }
317                         break;
318                 case 'd':
319                         ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
320                         sign = 1;
321                         base = 10;
322                         goto number;
323                 case 'l':
324                         lflag = 1;
325                         goto reswitch;
326                 case 'n':
327                         ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
328                         base = radix;
329                         goto number;
330                 case 'o':
331                         ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
332                         base = 8;
333                         goto number;
334                 case 'p':
335                         ul = (u_long)va_arg(ap, void *);
336                         base = 16;
337                         sharpflag = 1;
338                         goto number;
339                 case 's':
340                         p = va_arg(ap, char *);
341                         if (p == NULL)
342                                 p = "(null)";
343                         if (!dot)
344                                 n = strlen (p);
345                         else
346                                 for (n = 0; n < dwidth && p[n]; n++)
347                                         continue;
348
349                         width -= n;
350
351                         if (!ladjust && width > 0)
352                                 while (width--)
353                                         PCHAR(padc);
354                         while (n--)
355                                 PCHAR(*p++);
356                         if (ladjust && width > 0)
357                                 while (width--)
358                                         PCHAR(padc);
359                         break;
360                 case 'u':
361                         ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
362                         base = 10;
363                         goto number;
364                 case 'x':
365                         ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
366                         base = 16;
367 number:                 if (sign && (long)ul < 0L) {
368                                 neg = 1;
369                                 ul = -(long)ul;
370                         }
371                         p = ksprintn(ul, base, &tmp);
372                         if (sharpflag && ul != 0) {
373                                 if (base == 8)
374                                         tmp++;
375                                 else if (base == 16)
376                                         tmp += 2;
377                         }
378                         if (neg)
379                                 tmp++;
380
381                         if (!ladjust && width && (width -= tmp) > 0)
382                                 while (width--)
383                                         PCHAR(padc);
384                         if (neg)
385                                 PCHAR('-');
386                         if (sharpflag && ul != 0) {
387                                 if (base == 8) {
388                                         PCHAR('0');
389                                 } else if (base == 16) {
390                                         PCHAR('0');
391                                         PCHAR('x');
392                                 }
393                         }
394
395                         while (*p)
396                                 PCHAR(*p--);
397
398                         if (ladjust && width && (width -= tmp) > 0)
399                                 while (width--)
400                                         PCHAR(padc);
401
402                         break;
403                 default:
404                         PCHAR('%');
405                         if (lflag)
406                                 PCHAR('l');
407                         PCHAR(ch);
408                         break;
409                 }
410         }
411 #undef PCHAR
412 }
413