xref: /openbsd-src/libexec/ld.so/dl_printf.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: dl_printf.c,v 1.2 2001/06/06 12:38:44 art 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 #include "syscall.h"
69 
70 static void kprintn __P((void (*)(int), u_long, int));
71 static void kdoprnt __P((void (*)(int), const char *, va_list));
72 
73 static void putchar __P((int));
74 static void sputchar __P((int));
75 static char *sbuf;
76 
77 static void
78 putchar(int c)
79 {
80 	char b;
81 	b = c;
82 	_dl_write(2, &b, 1);
83 }
84 
85 static void
86 sputchar(int c)
87 {
88 	*sbuf++ = c;
89 }
90 
91 void
92 _dl_sprintf(char *buf, const char *fmt, ...)
93 {
94 	va_list ap;
95 
96 	sbuf = buf;
97 	va_start(ap, fmt);
98 	kdoprnt(sputchar, fmt, ap);
99 	va_end(ap);
100 	*sbuf = '\0';
101 }
102 
103 void
104 _dl_printf(const char *fmt, ...)
105 {
106 	va_list ap;
107 
108 	va_start(ap, fmt);
109 	kdoprnt(putchar, fmt, ap);
110 	va_end(ap);
111 }
112 
113 void
114 _dl_vprintf(const char *fmt, va_list ap)
115 {
116 	kdoprnt(putchar, fmt, ap);
117 }
118 
119 static void
120 kdoprnt(void (*put)(int), const char *fmt, va_list ap)
121 {
122 	char *p;
123 	int ch;
124 	unsigned long ul;
125 	int lflag;
126 
127 	for (;;) {
128 		while ((ch = *fmt++) != '%') {
129 			if (ch == '\0')
130 				return;
131 			put(ch);
132 		}
133 		lflag = 0;
134 reswitch:
135 		switch (ch = *fmt++) {
136 		case 'l':
137 			lflag = 1;
138 			goto reswitch;
139 		case 'b':
140 		{
141 			int set, n;
142 			ul = va_arg(ap, int);
143 			p = va_arg(ap, char *);
144 			kprintn(put, ul, *p++);
145 
146 			if (!ul)
147 				break;
148 
149 			for (set = 0; (n = *p++);) {
150 				if (ul & (1 << (n - 1))) {
151 					put(set ? ',' : '<');
152 					for (; (n = *p) > ' '; ++p)
153 						put(n);
154 					set = 1;
155 				} else
156 					for (; *p > ' '; ++p);
157 			}
158 			if (set)
159 				put('>');
160 		}
161 			break;
162 		case 'c':
163 			ch = va_arg(ap, int);
164 				put(ch & 0x7f);
165 			break;
166 		case 's':
167 			p = va_arg(ap, char *);
168 			while ((ch = *p++))
169 				put(ch);
170 			break;
171 		case 'd':
172 			ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
173 			if ((long)ul < 0) {
174 				put('-');
175 				ul = -(long)ul;
176 			}
177 			kprintn(put, ul, 10);
178 			break;
179 		case 'o':
180 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
181 			kprintn(put, ul, 8);
182 			break;
183 		case 'u':
184 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
185 			kprintn(put, ul, 10);
186 			break;
187 		case 'p':
188 			put('0');
189 			put('x');
190 			lflag += sizeof(void *)==sizeof(u_long)? 1 : 0;
191 		case 'x':
192 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
193 			kprintn(put, ul, 16);
194 			break;
195 		case 'X':
196 		{
197 			int l = 28;
198 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
199 			while(l >= 0) {
200 				put("0123456789abcdef"[(ul >> l) & 0xf]);
201 				l -= 4;
202 			}
203 			break;
204 		}
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(void (*put)(int), unsigned long ul, int base)
217 {
218 					/* hold a long in base 8 */
219 	char *p, buf[(sizeof(long) * NBBY / 3) + 1];
220 
221 	p = buf;
222 	do {
223 		*p++ = "0123456789abcdef"[ul % base];
224 	} while (ul /= base);
225 	do {
226 		put(*--p);
227 	} while (p > buf);
228 }
229