]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libstand/printf.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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  */
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 static char     *ksprintn (char *buf, uintmax_t num, int base, int *len, int upper);
60 static int      kvprintf(char const *fmt, void (*func)(int), void *arg, int radix, va_list ap);
61
62 int
63 printf(const char *fmt, ...)
64 {
65         va_list ap;
66         int retval;
67
68         va_start(ap, fmt);
69         retval = kvprintf(fmt, putchar, NULL, 10, ap);
70         va_end(ap);
71         return retval;
72 }
73
74 void
75 vprintf(const char *fmt, va_list ap)
76 {
77
78         kvprintf(fmt, putchar, NULL, 10, ap);
79 }
80
81 int
82 sprintf(char *buf, const char *cfmt, ...)
83 {
84         int retval;
85         va_list ap;
86
87         va_start(ap, cfmt);
88         retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
89         buf[retval] = '\0';
90         va_end(ap);
91         return retval;
92 }
93
94 void
95 vsprintf(char *buf, const char *cfmt, va_list ap)
96 {
97         int     retval;
98         
99         retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
100         buf[retval] = '\0';
101 }
102
103 /*
104  * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
105  * order; return an optional length and a pointer to the last character
106  * written in the buffer (i.e., the first character of the string).
107  * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
108  */
109 static char *
110 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
111 {
112         char *p, c;
113
114         p = nbuf;
115         *p = '\0';
116         do {
117                 c = hex2ascii(num % base);
118                 *++p = upper ? toupper(c) : c;
119         } while (num /= base);
120         if (lenp)
121                 *lenp = p - nbuf;
122         return (p);
123 }
124
125 /*
126  * Scaled down version of printf(3).
127  *
128  * Two additional formats:
129  *
130  * The format %b is supported to decode error registers.
131  * Its usage is:
132  *
133  *      printf("reg=%b\n", regval, "<base><arg>*");
134  *
135  * where <base> is the output base expressed as a control character, e.g.
136  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
137  * the first of which gives the bit number to be inspected (origin 1), and
138  * the next characters (up to a control character, i.e. a character <= 32),
139  * give the name of the register.  Thus:
140  *
141  *      kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
142  *
143  * would produce output:
144  *
145  *      reg=3<BITTWO,BITONE>
146  *
147  * XXX:  %D  -- Hexdump, takes pointer and separator string:
148  *              ("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
149  *              ("%*D", len, ptr, " " -> XX XX XX XX ...
150  */
151 static int
152 kvprintf(char const *fmt, void (*func)(int), void *arg, int radix, va_list ap)
153 {
154 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc); else *d++ = cc; retval++; }
155         char nbuf[MAXNBUF];
156         char *d;
157         const char *p, *percent, *q;
158         u_char *up;
159         int ch, n;
160         uintmax_t num;
161         int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
162         int jflag, tflag, zflag;
163         int dwidth, upper;
164         char padc;
165         int retval = 0;
166
167         num = 0;
168         if (!func)
169                 d = (char *) arg;
170         else
171                 d = NULL;
172
173         if (fmt == NULL)
174                 fmt = "(fmt null)\n";
175
176         if (radix < 2 || radix > 36)
177                 radix = 10;
178
179         for (;;) {
180                 padc = ' ';
181                 width = 0;
182                 while ((ch = (u_char)*fmt++) != '%') {
183                         if (ch == '\0')
184                                 return (retval);
185                         PCHAR(ch);
186                 }
187                 percent = fmt - 1;
188                 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
189                 sign = 0; dot = 0; dwidth = 0; upper = 0;
190                 jflag = 0; tflag = 0; zflag = 0;
191 reswitch:       switch (ch = (u_char)*fmt++) {
192                 case '.':
193                         dot = 1;
194                         goto reswitch;
195                 case '#':
196                         sharpflag = 1;
197                         goto reswitch;
198                 case '+':
199                         sign = 1;
200                         goto reswitch;
201                 case '-':
202                         ladjust = 1;
203                         goto reswitch;
204                 case '%':
205                         PCHAR(ch);
206                         break;
207                 case '*':
208                         if (!dot) {
209                                 width = va_arg(ap, int);
210                                 if (width < 0) {
211                                         ladjust = !ladjust;
212                                         width = -width;
213                                 }
214                         } else {
215                                 dwidth = va_arg(ap, int);
216                         }
217                         goto reswitch;
218                 case '0':
219                         if (!dot) {
220                                 padc = '0';
221                                 goto reswitch;
222                         }
223                 case '1': case '2': case '3': case '4':
224                 case '5': case '6': case '7': case '8': case '9':
225                                 for (n = 0;; ++fmt) {
226                                         n = n * 10 + ch - '0';
227                                         ch = *fmt;
228                                         if (ch < '0' || ch > '9')
229                                                 break;
230                                 }
231                         if (dot)
232                                 dwidth = n;
233                         else
234                                 width = n;
235                         goto reswitch;
236                 case 'b':
237                         num = va_arg(ap, int);
238                         p = va_arg(ap, char *);
239                         for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
240                                 PCHAR(*q--);
241
242                         if (num == 0)
243                                 break;
244
245                         for (tmp = 0; *p;) {
246                                 n = *p++;
247                                 if (num & (1 << (n - 1))) {
248                                         PCHAR(tmp ? ',' : '<');
249                                         for (; (n = *p) > ' '; ++p)
250                                                 PCHAR(n);
251                                         tmp = 1;
252                                 } else
253                                         for (; *p > ' '; ++p)
254                                                 continue;
255                         }
256                         if (tmp)
257                                 PCHAR('>');
258                         break;
259                 case 'c':
260                         PCHAR(va_arg(ap, int));
261                         break;
262                 case 'D':
263                         up = va_arg(ap, u_char *);
264                         p = va_arg(ap, char *);
265                         if (!width)
266                                 width = 16;
267                         while(width--) {
268                                 PCHAR(hex2ascii(*up >> 4));
269                                 PCHAR(hex2ascii(*up & 0x0f));
270                                 up++;
271                                 if (width)
272                                         for (q=p;*q;q++)
273                                                 PCHAR(*q);
274                         }
275                         break;
276                 case 'd':
277                 case 'i':
278                         base = 10;
279                         sign = 1;
280                         goto handle_sign;
281                 case 'j':
282                         jflag = 1;
283                         goto reswitch;
284                 case 'l':
285                         if (lflag) {
286                                 lflag = 0;
287                                 qflag = 1;
288                         } else
289                                 lflag = 1;
290                         goto reswitch;
291                 case 'n':
292                         if (jflag)
293                                 *(va_arg(ap, intmax_t *)) = retval;
294                         else if (qflag)
295                                 *(va_arg(ap, quad_t *)) = retval;
296                         else if (lflag)
297                                 *(va_arg(ap, long *)) = retval;
298                         else if (zflag)
299                                 *(va_arg(ap, size_t *)) = retval;
300                         else
301                                 *(va_arg(ap, int *)) = retval;
302                         break;
303                 case 'o':
304                         base = 8;
305                         goto handle_nosign;
306                 case 'p':
307                         base = 16;
308                         sharpflag = (width == 0);
309                         sign = 0;
310                         num = (uintptr_t)va_arg(ap, void *);
311                         goto number;
312                 case 'q':
313                         qflag = 1;
314                         goto reswitch;
315                 case 'r':
316                         base = radix;
317                         if (sign)
318                                 goto handle_sign;
319                         goto handle_nosign;
320                 case 's':
321                         p = va_arg(ap, char *);
322                         if (p == NULL)
323                                 p = "(null)";
324                         if (!dot)
325                                 n = strlen (p);
326                         else
327                                 for (n = 0; n < dwidth && p[n]; n++)
328                                         continue;
329
330                         width -= n;
331
332                         if (!ladjust && width > 0)
333                                 while (width--)
334                                         PCHAR(padc);
335                         while (n--)
336                                 PCHAR(*p++);
337                         if (ladjust && width > 0)
338                                 while (width--)
339                                         PCHAR(padc);
340                         break;
341                 case 't':
342                         tflag = 1;
343                         goto reswitch;
344                 case 'u':
345                         base = 10;
346                         goto handle_nosign;
347                 case 'X':
348                         upper = 1;
349                 case 'x':
350                         base = 16;
351                         goto handle_nosign;
352                 case 'y':
353                         base = 16;
354                         sign = 1;
355                         goto handle_sign;
356                 case 'z':
357                         zflag = 1;
358                         goto reswitch;
359 handle_nosign:
360                         sign = 0;
361                         if (jflag)
362                                 num = va_arg(ap, uintmax_t);
363                         else if (qflag)
364                                 num = va_arg(ap, u_quad_t);
365                         else if (tflag)
366                                 num = va_arg(ap, ptrdiff_t);
367                         else if (lflag)
368                                 num = va_arg(ap, u_long);
369                         else if (zflag)
370                                 num = va_arg(ap, size_t);
371                         else
372                                 num = va_arg(ap, u_int);
373                         goto number;
374 handle_sign:
375                         if (jflag)
376                                 num = va_arg(ap, intmax_t);
377                         else if (qflag)
378                                 num = va_arg(ap, quad_t);
379                         else if (tflag)
380                                 num = va_arg(ap, ptrdiff_t);
381                         else if (lflag)
382                                 num = va_arg(ap, long);
383                         else if (zflag)
384                                 num = va_arg(ap, ssize_t);
385                         else
386                                 num = va_arg(ap, int);
387 number:
388                         if (sign && (intmax_t)num < 0) {
389                                 neg = 1;
390                                 num = -(intmax_t)num;
391                         }
392                         p = ksprintn(nbuf, num, base, &tmp, upper);
393                         if (sharpflag && num != 0) {
394                                 if (base == 8)
395                                         tmp++;
396                                 else if (base == 16)
397                                         tmp += 2;
398                         }
399                         if (neg)
400                                 tmp++;
401
402                         if (!ladjust && width && (width -= tmp) > 0)
403                                 while (width--)
404                                         PCHAR(padc);
405                         if (neg)
406                                 PCHAR('-');
407                         if (sharpflag && num != 0) {
408                                 if (base == 8) {
409                                         PCHAR('0');
410                                 } else if (base == 16) {
411                                         PCHAR('0');
412                                         PCHAR('x');
413                                 }
414                         }
415
416                         while (*p)
417                                 PCHAR(*p--);
418
419                         if (ladjust && width && (width -= tmp) > 0)
420                                 while (width--)
421                                         PCHAR(padc);
422
423                         break;
424                 default:
425                         while (percent < fmt)
426                                 PCHAR(*percent++);
427                         break;
428                 }
429         }
430 #undef PCHAR
431 }