xref: /netbsd-src/sys/lib/libsa/subr_prf.c (revision 6cf6fe02a981b55727c49c3d37b0d8191a98c0ee)
1 /*	$NetBSD: subr_prf.c,v 1.27 2014/08/30 14:24:02 tsutsui 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)printf.c	8.1 (Berkeley) 6/11/93
32  */
33 
34 /*
35  * Scaled down version of printf(3).
36  */
37 
38 #include <sys/cdefs.h>
39 #include <sys/types.h>
40 #include <sys/stdint.h>		/* XXX: for intptr_t */
41 
42 #include "stand.h"
43 
44 #ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
45 #define INTMAX_T	longlong_t
46 #define UINTMAX_T	u_longlong_t
47 #else
48 #define INTMAX_T	long
49 #define UINTMAX_T	u_long
50 #endif
51 
52 #if 0 /* XXX: abuse intptr_t until the situation with ptrdiff_t is clear */
53 #define PTRDIFF_T	ptrdiff_t
54 #else
55 #define PTRDIFF_T	intptr_t
56 #endif
57 
58 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
59 static void kprintn(void (*)(int), UINTMAX_T, int, int, int);
60 #else
61 static void kprintn(void (*)(int), UINTMAX_T, int);
62 #endif
63 static void sputchar(int);
64 static void kdoprnt(void (*)(int), const char *, va_list);
65 
66 static char *sbuf, *ebuf;
67 
68 const char hexdigits[16] = "0123456789abcdef";
69 
70 #define LONG		0x01
71 #ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
72 #define LLONG		0x02
73 #endif
74 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
75 #define ALT		0x04
76 #define SPACE		0x08
77 #define LADJUST		0x10
78 #define SIGN		0x20
79 #define ZEROPAD		0x40
80 #define NEGATIVE	0x80
81 #define KPRINTN(base)	kprintn(put, ul, base, lflag, width)
82 #define RADJUSTZEROPAD()					\
83 do {								\
84 	if ((lflag & (ZEROPAD|LADJUST)) == ZEROPAD) {		\
85 		while (width-- > 0)				\
86 			put('0');				\
87 	}							\
88 } while (/*CONSTCOND*/0)
89 #define LADJUSTPAD()						\
90 do {								\
91 	if (lflag & LADJUST) {					\
92 		while (width-- > 0)				\
93 			put(' ');				\
94 	}							\
95 } while (/*CONSTCOND*/0)
96 #define RADJUSTPAD()						\
97 do {								\
98 	if ((lflag & (ZEROPAD|LADJUST)) == 0) {			\
99 		while (width-- > 0)				\
100 			put(' ');				\
101 	}							\
102 } while (/*CONSTCOND*/0)
103 #else	/* LIBSA_PRINTF_WIDTH_SUPPORT */
104 #define KPRINTN(base)	kprintn(put, ul, base)
105 #define RADJUSTZEROPAD()	/**/
106 #define LADJUSTPAD()		/**/
107 #define RADJUSTPAD()		/**/
108 #endif	/* LIBSA_PRINTF_WIDTH_SUPPORT */
109 
110 #ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
111 #define KPRINT(base)						\
112 do {								\
113 	ul = (lflag & LLONG)					\
114 	    ? va_arg(ap, u_longlong_t)				\
115 	    : (lflag & LONG)					\
116 		? va_arg(ap, u_long)				\
117 		: va_arg(ap, u_int);				\
118 	KPRINTN(base);						\
119 } while (/*CONSTCOND*/0)
120 #else	/* LIBSA_PRINTF_LONGLONG_SUPPORT */
121 #define KPRINT(base)						\
122 do {								\
123 	ul = (lflag & LONG)					\
124 	    ? va_arg(ap, u_long) : va_arg(ap, u_int);		\
125 	KPRINTN(base);						\
126 } while (/*CONSTCOND*/0)
127 #endif	/* LIBSA_PRINTF_LONGLONG_SUPPORT */
128 
129 static void
130 sputchar(int c)
131 {
132 
133 	if (sbuf < ebuf)
134 		*sbuf++ = c;
135 }
136 
137 void
138 vprintf(const char *fmt, va_list ap)
139 {
140 
141 	kdoprnt(putchar, fmt, ap);
142 }
143 
144 int
145 vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
146 {
147 
148 	sbuf = buf;
149 	ebuf = buf + size - 1;
150 	kdoprnt(sputchar, fmt, ap);
151 	*sbuf = '\0';
152 	return sbuf - buf;
153 }
154 
155 static void
156 kdoprnt(void (*put)(int), const char *fmt, va_list ap)
157 {
158 	char *p;
159 	int ch;
160 	UINTMAX_T ul;
161 	int lflag;
162 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
163 	int width;
164 	char *q;
165 #endif
166 
167 	for (;;) {
168 		while ((ch = *fmt++) != '%') {
169 			if (ch == '\0')
170 				return;
171 			put(ch);
172 		}
173 		lflag = 0;
174 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
175 		width = 0;
176 #endif
177 reswitch:
178 		switch (ch = *fmt++) {
179 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
180 		case '#':
181 			lflag |= ALT;
182 			goto reswitch;
183 		case ' ':
184 			lflag |= SPACE;
185 			goto reswitch;
186 		case '-':
187 			lflag |= LADJUST;
188 			goto reswitch;
189 		case '+':
190 			lflag |= SIGN;
191 			goto reswitch;
192 		case '0':
193 			lflag |= ZEROPAD;
194 			goto reswitch;
195 		case '1': case '2': case '3': case '4': case '5':
196 		case '6': case '7': case '8': case '9':
197 			for (;;) {
198 				width *= 10;
199 				width += ch - '0';
200 				ch = *fmt;
201 				if ((unsigned)ch - '0' > 9)
202 					break;
203 				++fmt;
204 			}
205 #endif
206 			goto reswitch;
207 		case 'l':
208 #ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
209 			if (*fmt == 'l') {
210 				++fmt;
211 				lflag |= LLONG;
212 			} else
213 #endif
214 				lflag |= LONG;
215 			goto reswitch;
216 		case 'j':
217 #ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
218 			if (sizeof(intmax_t) == sizeof(long long))
219 				lflag |= LLONG;
220 			else
221 #endif
222 			if (sizeof(intmax_t) == sizeof(long))
223 				lflag |= LONG;
224 			goto reswitch;
225 		case 't':
226 			if (sizeof(PTRDIFF_T) == sizeof(long))
227 				lflag |= LONG;
228 			goto reswitch;
229 		case 'z':
230 			if (sizeof(ssize_t) == sizeof(long))
231 				lflag |= LONG;
232 			goto reswitch;
233 		case 'c':
234 			ch = va_arg(ap, int);
235 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
236 			--width;
237 #endif
238 			RADJUSTPAD();
239 			put(ch & 0xFF);
240 			LADJUSTPAD();
241 			break;
242 		case 's':
243 			p = va_arg(ap, char *);
244 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
245 			for (q = p; *q != '\0'; ++q)
246 				continue;
247 			width -= q - p;
248 #endif
249 			RADJUSTPAD();
250 			while ((ch = (unsigned char)*p++))
251 				put(ch);
252 			LADJUSTPAD();
253 			break;
254 		case 'd':
255 			ul =
256 #ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
257 			(lflag & LLONG) ? va_arg(ap, longlong_t) :
258 #endif
259 			(lflag & LONG) ? va_arg(ap, long) : va_arg(ap, int);
260 			if ((INTMAX_T)ul < 0) {
261 				ul = -(INTMAX_T)ul;
262 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
263 				lflag |= NEGATIVE;
264 #else
265 				put('-');
266 #endif
267 			}
268 			KPRINTN(10);
269 			break;
270 		case 'o':
271 			KPRINT(8);
272 			break;
273 		case 'u':
274 			KPRINT(10);
275 			break;
276 		case 'p':
277 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
278 			lflag |= (LONG|ALT);
279 #else
280 			put('0');
281 			put('x');
282 #endif
283 			/* FALLTHROUGH */
284 		case 'x':
285 			KPRINT(16);
286 			break;
287 		default:
288 			if (ch == '\0')
289 				return;
290 			put(ch);
291 			break;
292 		}
293 	}
294 }
295 
296 static void
297 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
298 kprintn(void (*put)(int), UINTMAX_T ul, int base, int lflag, int width)
299 #else
300 kprintn(void (*put)(int), UINTMAX_T ul, int base)
301 #endif
302 {
303 					/* hold a INTMAX_T in base 8 */
304 	char *p, buf[(sizeof(INTMAX_T) * NBBY / 3) + 1 + 2 /* ALT + SIGN */];
305 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
306 	char *q;
307 #endif
308 
309 	p = buf;
310 	do {
311 		*p++ = hexdigits[ul % base];
312 	} while (ul /= base);
313 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
314 	q = p;
315 	if (lflag & ALT && *(p - 1) != '0') {
316 		if (base == 8) {
317 			*p++ = '0';
318 		} else if (base == 16) {
319 			*p++ = 'x';
320 			*p++ = '0';
321 		}
322 	}
323 	if (lflag & NEGATIVE)
324 		*p++ = '-';
325 	else if (lflag & SIGN)
326 		*p++ = '+';
327 	else if (lflag & SPACE)
328 		*p++ = ' ';
329 	width -= p - buf;
330 	if (lflag & ZEROPAD) {
331 		while (p > q)
332 			put(*--p);
333 	}
334 #endif
335 	RADJUSTPAD();
336 	RADJUSTZEROPAD();
337 	do {
338 		put(*--p);
339 	} while (p > buf);
340 	LADJUSTPAD();
341 }
342