printf.c revision 1.12 1 /* $NetBSD: printf.c,v 1.12 1997/06/26 19:11:48 drochner 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 #ifdef __STDC__
64 #include <machine/stdarg.h>
65 #else
66 #include <machine/varargs.h>
67 #endif
68
69 #include "stand.h"
70
71 static void kprintn __P((void (*)(int), u_long, int));
72 static void sputchar __P((int));
73 static void kdoprnt __P((void (*)(int), const char *, va_list));
74
75 static char *sbuf;
76
77 static void
78 sputchar(c)
79 int c;
80 {
81 *sbuf++ = c;
82 }
83
84 void
85 #ifdef __STDC__
86 sprintf(char *buf, const char *fmt, ...)
87 #else
88 sprintf(buf, fmt, va_alist)
89 char *buf, *fmt;
90 #endif
91 {
92 va_list ap;
93
94 sbuf = buf;
95 #ifdef __STDC__
96 va_start(ap, fmt);
97 #else
98 va_start(ap);
99 #endif
100 kdoprnt(sputchar, fmt, ap);
101 va_end(ap);
102 *sbuf = '\0';
103 }
104
105 void
106 #ifdef __STDC__
107 printf(const char *fmt, ...)
108 #else
109 printf(fmt, va_alist)
110 char *fmt;
111 #endif
112 {
113 va_list ap;
114
115 #ifdef __STDC__
116 va_start(ap, fmt);
117 #else
118 va_start(ap);
119 #endif
120 kdoprnt(putchar, fmt, ap);
121 va_end(ap);
122 }
123
124 void
125 vprintf(const char *fmt, va_list ap)
126 {
127 kdoprnt(putchar, fmt, ap);
128 }
129
130 void
131 kdoprnt(put, fmt, ap)
132 void (*put)__P((int));
133 const char *fmt;
134 va_list ap;
135 {
136 register char *p;
137 register int ch, n;
138 unsigned long ul;
139 int lflag, set;
140
141 for (;;) {
142 while ((ch = *fmt++) != '%') {
143 if (ch == '\0')
144 return;
145 put(ch);
146 }
147 lflag = 0;
148 reswitch: switch (ch = *fmt++) {
149 case '\0':
150 /* XXX print the last format character? */
151 return;
152 case 'l':
153 lflag = 1;
154 goto reswitch;
155 case 'b':
156 ul = va_arg(ap, int);
157 p = va_arg(ap, char *);
158 kprintn(put, ul, *p++);
159
160 if (!ul)
161 break;
162
163 for (set = 0; (n = *p++);) {
164 if (ul & (1 << (n - 1))) {
165 put(set ? ',' : '<');
166 for (; (n = *p) > ' '; ++p)
167 put(n);
168 set = 1;
169 } else
170 for (; *p > ' '; ++p);
171 }
172 if (set)
173 put('>');
174 break;
175 case 'c':
176 ch = va_arg(ap, int);
177 put(ch & 0x7f);
178 break;
179 case 's':
180 p = va_arg(ap, char *);
181 while ((ch = *p++))
182 put(ch);
183 break;
184 case 'd':
185 ul = lflag ?
186 va_arg(ap, long) : va_arg(ap, int);
187 if ((long)ul < 0) {
188 put('-');
189 ul = -(long)ul;
190 }
191 kprintn(put, ul, 10);
192 break;
193 case 'o':
194 ul = lflag ?
195 va_arg(ap, u_long) : va_arg(ap, u_int);
196 kprintn(put, ul, 8);
197 break;
198 case 'u':
199 ul = lflag ?
200 va_arg(ap, u_long) : va_arg(ap, u_int);
201 kprintn(put, ul, 10);
202 break;
203 case 'x':
204 ul = lflag ?
205 va_arg(ap, u_long) : va_arg(ap, u_int);
206 kprintn(put, ul, 16);
207 break;
208 default:
209 put('%');
210 if (lflag)
211 put('l');
212 put(ch);
213 }
214 }
215 va_end(ap);
216 }
217
218 static void
219 kprintn(put, ul, base)
220 void (*put)__P((int));
221 unsigned long ul;
222 int base;
223 {
224 /* hold a long in base 8 */
225 char *p, buf[(sizeof(long) * NBBY / 3) + 1];
226
227 p = buf;
228 do {
229 *p++ = "0123456789abcdef"[ul % base];
230 } while (ul /= base);
231 do {
232 put(*--p);
233 } while (p > buf);
234 }
235
236 void
237 twiddle()
238 {
239 static int pos;
240
241 putchar("|/-\\"[pos++ & 3]);
242 putchar('\b');
243 }
244