subr_prf.c revision 1.6 1 /* $NetBSD: subr_prf.c,v 1.6 2003/04/20 19:31:29 bjh21 Exp $ */
2
3 /*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)printf.c 8.1 (Berkeley) 6/11/93
36 */
37
38 /*
39 * Scaled down version of printf(3).
40 *
41 * One additional format:
42 *
43 * The format %b is supported to decode error registers.
44 * Its usage is:
45 *
46 * printf("reg=%b\n", regval, "<base><arg>*");
47 *
48 * where <base> is the output base expressed as a control character, e.g.
49 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
50 * the first of which gives the bit number to be inspected (origin 1), and
51 * the next characters (up to a control character, i.e. a character <= 32),
52 * give the name of the register. Thus:
53 *
54 * printf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
55 *
56 * would produce output:
57 *
58 * reg=3<BITTWO,BITONE>
59 */
60
61 #include <sys/cdefs.h>
62 #include <sys/types.h>
63 #include <machine/stdarg.h>
64
65 #include "stand.h"
66
67 static void kprintn(void (*)(int), u_long, int);
68 static void sputchar(int);
69 static void kdoprnt(void (*)(int), const char *, va_list);
70
71 static char *sbuf, *ebuf;
72
73 static void
74 sputchar(int c)
75 {
76
77 if (sbuf < ebuf)
78 *sbuf++ = c;
79 }
80
81 void
82 vprintf(const char *fmt, va_list ap)
83 {
84
85 kdoprnt(putchar, fmt, ap);
86 }
87
88 int
89 vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
90 {
91
92 sbuf = buf;
93 ebuf = buf + size - 1;
94 kdoprnt(sputchar, fmt, ap);
95 *sbuf = '\0';
96 return (sbuf - buf);
97 }
98
99 static void
100 kdoprnt(void (*put)(int), const char *fmt, va_list ap)
101 {
102 char *p;
103 int ch, n;
104 unsigned long ul;
105 int lflag, set;
106
107 for (;;) {
108 while ((ch = *fmt++) != '%') {
109 if (ch == '\0')
110 return;
111 put(ch);
112 }
113 lflag = 0;
114 reswitch: switch (ch = *fmt++) {
115 case '\0':
116 /* XXX print the last format character? */
117 return;
118 case 'l':
119 lflag = 1;
120 goto reswitch;
121 case 'b':
122 ul = va_arg(ap, int);
123 p = va_arg(ap, char *);
124 kprintn(put, ul, *p++);
125
126 if (!ul)
127 break;
128
129 for (set = 0; (n = *p++);) {
130 if (ul & (1 << (n - 1))) {
131 put(set ? ',' : '<');
132 for (; (n = *p) > ' '; ++p)
133 put(n);
134 set = 1;
135 } else
136 for (; *p > ' '; ++p);
137 }
138 if (set)
139 put('>');
140 break;
141 case 'c':
142 ch = va_arg(ap, int);
143 put(ch & 0x7f);
144 break;
145 case 's':
146 p = va_arg(ap, char *);
147 while ((ch = *p++))
148 put(ch);
149 break;
150 case 'd':
151 ul = lflag ?
152 va_arg(ap, long) : va_arg(ap, int);
153 if ((long)ul < 0) {
154 put('-');
155 ul = -(long)ul;
156 }
157 kprintn(put, ul, 10);
158 break;
159 case 'o':
160 ul = lflag ?
161 va_arg(ap, u_long) : va_arg(ap, u_int);
162 kprintn(put, ul, 8);
163 break;
164 case 'u':
165 ul = lflag ?
166 va_arg(ap, u_long) : va_arg(ap, u_int);
167 kprintn(put, ul, 10);
168 break;
169 case 'p':
170 put('0');
171 put('x');
172 lflag = 1;
173 /* fall through */
174 case 'x':
175 ul = lflag ?
176 va_arg(ap, u_long) : va_arg(ap, u_int);
177 kprintn(put, ul, 16);
178 break;
179 default:
180 put('%');
181 if (lflag)
182 put('l');
183 put(ch);
184 }
185 }
186 }
187
188 static void
189 kprintn(void (*put)(int), unsigned long ul, int base)
190 {
191 /* hold a long in base 8 */
192 char *p, buf[(sizeof(long) * NBBY / 3) + 1];
193
194 p = buf;
195 do {
196 *p++ = "0123456789abcdef"[ul % base];
197 } while (ul /= base);
198 do {
199 put(*--p);
200 } while (p > buf);
201 }
202