printf.c revision 1.9 1 /* $NetBSD: printf.c,v 1.9 1996/10/13 02:29:05 christos 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 <stdarg.h>
65 #else
66 #include <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 kvprintf(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 'l':
150 lflag = 1;
151 goto reswitch;
152 case 'b':
153 ul = va_arg(ap, int);
154 p = va_arg(ap, char *);
155 kprintn(put, ul, *p++);
156
157 if (!ul)
158 break;
159
160 for (set = 0; (n = *p++);) {
161 if (ul & (1 << (n - 1))) {
162 put(set ? ',' : '<');
163 for (; (n = *p) > ' '; ++p)
164 put(n);
165 set = 1;
166 } else
167 for (; *p > ' '; ++p);
168 }
169 if (set)
170 put('>');
171 break;
172 case 'c':
173 ch = va_arg(ap, int);
174 put(ch & 0x7f);
175 break;
176 case 's':
177 p = va_arg(ap, char *);
178 while ((ch = *p++))
179 put(ch);
180 break;
181 case 'd':
182 ul = lflag ?
183 va_arg(ap, long) : va_arg(ap, int);
184 if ((long)ul < 0) {
185 put('-');
186 ul = -(long)ul;
187 }
188 kprintn(put, ul, 10);
189 break;
190 case 'o':
191 ul = lflag ?
192 va_arg(ap, u_long) : va_arg(ap, u_int);
193 kprintn(put, ul, 8);
194 break;
195 case 'u':
196 ul = lflag ?
197 va_arg(ap, u_long) : va_arg(ap, u_int);
198 kprintn(put, ul, 10);
199 break;
200 case 'x':
201 ul = lflag ?
202 va_arg(ap, u_long) : va_arg(ap, u_int);
203 kprintn(put, ul, 16);
204 break;
205 default:
206 put('%');
207 if (lflag)
208 put('l');
209 put(ch);
210 }
211 }
212 va_end(ap);
213 }
214
215 static void
216 kprintn(put, ul, base)
217 void (*put)__P((int));
218 unsigned long ul;
219 int base;
220 {
221 /* hold a long in base 8 */
222 char *p, buf[(sizeof(long) * NBBY / 3) + 1];
223
224 p = buf;
225 do {
226 *p++ = "0123456789abcdef"[ul % base];
227 } while (ul /= base);
228 do {
229 put(*--p);
230 } while (p > buf);
231 }
232
233 void
234 twiddle()
235 {
236 static int pos;
237
238 putchar("|/-\\"[pos++ & 3]);
239 putchar('\b');
240 }
241