subr_prf.c revision 1.3 1 /* $NetBSD: subr_prf.c,v 1.3 2000/03/30 12:19:49 augustss 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, *ebuf;
76
77 static void
78 sputchar(c)
79 int c;
80 {
81 if (sbuf < ebuf)
82 *sbuf++ = c;
83 }
84
85 void
86 vprintf(const char *fmt, va_list ap)
87 {
88
89 kdoprnt(putchar, fmt, ap);
90 }
91
92 int
93 vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
94 {
95
96 sbuf = buf;
97 ebuf = buf + size - 1;
98 kdoprnt(sputchar, fmt, ap);
99 *sbuf = '\0';
100 return (sbuf - buf);
101 }
102
103 void
104 kdoprnt(put, fmt, ap)
105 void (*put)__P((int));
106 const char *fmt;
107 va_list ap;
108 {
109 char *p;
110 int ch, n;
111 unsigned long ul;
112 int lflag, set;
113
114 for (;;) {
115 while ((ch = *fmt++) != '%') {
116 if (ch == '\0')
117 return;
118 put(ch);
119 }
120 lflag = 0;
121 reswitch: switch (ch = *fmt++) {
122 case '\0':
123 /* XXX print the last format character? */
124 return;
125 case 'l':
126 lflag = 1;
127 goto reswitch;
128 case 'b':
129 ul = va_arg(ap, int);
130 p = va_arg(ap, char *);
131 kprintn(put, ul, *p++);
132
133 if (!ul)
134 break;
135
136 for (set = 0; (n = *p++);) {
137 if (ul & (1 << (n - 1))) {
138 put(set ? ',' : '<');
139 for (; (n = *p) > ' '; ++p)
140 put(n);
141 set = 1;
142 } else
143 for (; *p > ' '; ++p);
144 }
145 if (set)
146 put('>');
147 break;
148 case 'c':
149 ch = va_arg(ap, int);
150 put(ch & 0x7f);
151 break;
152 case 's':
153 p = va_arg(ap, char *);
154 while ((ch = *p++))
155 put(ch);
156 break;
157 case 'd':
158 ul = lflag ?
159 va_arg(ap, long) : va_arg(ap, int);
160 if ((long)ul < 0) {
161 put('-');
162 ul = -(long)ul;
163 }
164 kprintn(put, ul, 10);
165 break;
166 case 'o':
167 ul = lflag ?
168 va_arg(ap, u_long) : va_arg(ap, u_int);
169 kprintn(put, ul, 8);
170 break;
171 case 'u':
172 ul = lflag ?
173 va_arg(ap, u_long) : va_arg(ap, u_int);
174 kprintn(put, ul, 10);
175 break;
176 case 'p':
177 put('0');
178 put('x');
179 lflag = 1;
180 /* fall through */
181 case 'x':
182 ul = lflag ?
183 va_arg(ap, u_long) : va_arg(ap, u_int);
184 kprintn(put, ul, 16);
185 break;
186 default:
187 put('%');
188 if (lflag)
189 put('l');
190 put(ch);
191 }
192 }
193 va_end(ap);
194 }
195
196 static void
197 kprintn(put, ul, base)
198 void (*put)__P((int));
199 unsigned long ul;
200 int base;
201 {
202 /* hold a long in base 8 */
203 char *p, buf[(sizeof(long) * NBBY / 3) + 1];
204
205 p = buf;
206 do {
207 *p++ = "0123456789abcdef"[ul % base];
208 } while (ul /= base);
209 do {
210 put(*--p);
211 } while (p > buf);
212 }
213