1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /* ␊ |
25 | * Mach Operating System␊ |
26 | * Copyright (c) 1990 Carnegie-Mellon University␊ |
27 | * Copyright (c) 1989 Carnegie-Mellon University␊ |
28 | * Copyright (c) 1988 Carnegie-Mellon University␊ |
29 | * Copyright (c) 1987 Carnegie-Mellon University␊ |
30 | * All rights reserved. The CMU software License Agreement specifies␊ |
31 | * the terms and conditions for use and redistribution.␊ |
32 | */␊ |
33 | /*␊ |
34 | * Copyright (c) 1982, 1986 Regents of the University of California.␊ |
35 | * All rights reserved. The Berkeley software License Agreement␊ |
36 | * specifies the terms and conditions for redistribution.␊ |
37 | *␊ |
38 | *␉@(#)prf.c␉7.1 (Berkeley) 6/5/86␊ |
39 | */␊ |
40 | ␊ |
41 | #include <stdarg.h>␊ |
42 | ␊ |
43 | #define SPACE␉1␊ |
44 | #define ZERO␉2␊ |
45 | #define UCASE␉16␊ |
46 | #define SIGNED␉32␊ |
47 | ␊ |
48 | /*␊ |
49 | * Scaled down version of C Library printf.␊ |
50 | * Used to print diagnostic information directly on console tty.␊ |
51 | * Since it is not interrupt driven, all system activities are␊ |
52 | * suspended.␊ |
53 | *␊ |
54 | */␊ |
55 | ␊ |
56 | #define DIVIDEND_LOW *(unsigned int*) dividend␊ |
57 | #define DIVIDEND_HIGH ((unsigned int*) dividend)[1]␊ |
58 | ␊ |
59 | /*␊ |
60 | * Divides 64-bit dividend by 32-bit divisor.␊ |
61 | * Quotient stored in dividend, remainder returned.␊ |
62 | * Assumes little-endian byte order.␊ |
63 | * Assumes divisor is non-zero.␊ |
64 | */␊ |
65 | unsigned int i386_unsigned_div(␊ |
66 | ␉unsigned long long* dividend,␊ |
67 | ␉unsigned int divisor␊ |
68 | ␉)␊ |
69 | {␊ |
70 | ␉unsigned int high = DIVIDEND_HIGH;␊ |
71 | ␊ |
72 | ␉if (high >= divisor)␊ |
73 | ␉{␊ |
74 | ␉␉__asm__ volatile ("xorl %%edx, %%edx; divl %2" : "=a"(DIVIDEND_HIGH), "=d"(high) : "r"(divisor), "a"(high));␊ |
75 | ␉}␊ |
76 | ␉else␊ |
77 | ␉{␊ |
78 | ␉␉DIVIDEND_HIGH = 0;␊ |
79 | ␉}␊ |
80 | ␉__asm__ volatile("divl %2" : "+a"(DIVIDEND_LOW), "+d"(high) : "r"(divisor));␊ |
81 | ␉return high;␊ |
82 | }␊ |
83 | ␊ |
84 | #undef DIVIDEND_HIGH␊ |
85 | #undef DIVIDEND_LOW␊ |
86 | ␊ |
87 | /*␊ |
88 | * Printn prints a number n in base b.␊ |
89 | * We don't use recursion to avoid deep kernel stacks.␊ |
90 | */␊ |
91 | static int printn(␊ |
92 | ␉unsigned long long n,␊ |
93 | ␉int b,␊ |
94 | ␉int flag,␊ |
95 | ␉int minwidth,␊ |
96 | ␉int (*putfn_p)(),␊ |
97 | ␉void* putfn_arg␊ |
98 | ␉)␊ |
99 | {␊ |
100 | ␉char prbuf[22];␊ |
101 | ␉register char *cp;␊ |
102 | ␉int width = 0, neg = 0;␊ |
103 | ␉static const char hexdig[] = "0123456789abcdef0123456789ABCDEF";␊ |
104 | ␊ |
105 | ␉if ((flag & SIGNED) && (long long)n < 0)␊ |
106 | ␉{␊ |
107 | ␉␉neg = 1;␊ |
108 | ␉␉n = (unsigned long long)(-(long long)n);␊ |
109 | ␉}␊ |
110 | ␉cp = prbuf;␊ |
111 | ␉if ((b & -b) == b)␉// b is a power of 2␊ |
112 | ␉{␊ |
113 | ␉␉unsigned int log2b = (unsigned int) (__builtin_ctz((unsigned int) b) & 31);␊ |
114 | ␉␉unsigned int mask = (unsigned int) (b - 1);␊ |
115 | ␉␉do␊ |
116 | ␉␉{␊ |
117 | ␉␉␉*cp++ = hexdig[(flag & UCASE) + (int) (n & mask)];␊ |
118 | ␉␉␉n >>= log2b;␊ |
119 | ␉␉␉width++;␊ |
120 | ␉␉}␊ |
121 | ␉␉while (n);␊ |
122 | ␉}␊ |
123 | ␉else␉// b is not a power of 2␊ |
124 | ␉{␊ |
125 | ␉␉do␊ |
126 | ␉␉{␊ |
127 | ␉␉␉*cp++ = hexdig[(flag & UCASE) + (int) i386_unsigned_div(&n, (unsigned int) b)];␊ |
128 | ␉␉␉width++;␊ |
129 | ␉␉}␊ |
130 | ␉␉while (n);␊ |
131 | ␉}␊ |
132 | ␊ |
133 | ␉if (neg)␊ |
134 | ␉{␊ |
135 | ␉␉if (putfn_p)␊ |
136 | ␉␉{␊ |
137 | ␉␉␉(void)(*putfn_p)('-', putfn_arg);␊ |
138 | ␉␉}␊ |
139 | ␉␉width++;␊ |
140 | ␉}␊ |
141 | ␉if (!putfn_p)␊ |
142 | ␉{␊ |
143 | ␉␉return (width < minwidth) ? minwidth : width;␊ |
144 | ␉}␊ |
145 | ␉for (;width < minwidth; width++)␊ |
146 | ␉␉(void)(*putfn_p)( (flag & ZERO) ? '0' : ' ', putfn_arg);␊ |
147 | ␊ |
148 | ␉do␊ |
149 | ␉␉(void)(*putfn_p)(*--cp, putfn_arg);␊ |
150 | ␉while (cp > prbuf);␊ |
151 | ␉return width;␊ |
152 | }␊ |
153 | ␊ |
154 | /*␊ |
155 | * Printp prints a pointer.␊ |
156 | */␊ |
157 | static int printp(␊ |
158 | ␉const void* p,␊ |
159 | ␉int minwidth,␊ |
160 | ␉int (*putfn_p)(),␊ |
161 | ␉void* putfn_arg␊ |
162 | ␉)␊ |
163 | {␊ |
164 | ␉int width = 0;␊ |
165 | ␊ |
166 | ␉if (p)␊ |
167 | ␉{␊ |
168 | ␉␉if (putfn_p)␊ |
169 | ␉␉{␊ |
170 | ␉␉␉(void)(*putfn_p)('0', putfn_arg);␊ |
171 | ␉␉␉(void)(*putfn_p)('x', putfn_arg);␊ |
172 | ␉␉}␊ |
173 | ␉␉width = 2;␊ |
174 | ␉␉minwidth = ((minwidth >= 2) ? (minwidth - 2) : 0);␊ |
175 | ␉}␊ |
176 | ␉return width + printn((unsigned long long) p, 16, ZERO, minwidth, putfn_p, putfn_arg);␊ |
177 | }␊ |
178 | ␊ |
179 | int prf(␊ |
180 | ␉const char *fmt,␊ |
181 | ␉va_list ap,␊ |
182 | ␉int (*putfn_p)(),␊ |
183 | ␉void *putfn_arg␊ |
184 | ␉)␊ |
185 | {␊ |
186 | ␉int b, c, len = 0;␊ |
187 | ␉const char *s;␊ |
188 | ␉int flag, width, ells;␊ |
189 | ␉int minwidth;␊ |
190 | ␊ |
191 | loop:␊ |
192 | ␉while ((c = *fmt++) != '%')␊ |
193 | ␉{␊ |
194 | ␉␉if(c == '\0')␊ |
195 | ␉␉{␊ |
196 | ␉␉␉return len;␊ |
197 | ␉␉}␊ |
198 | ␊ |
199 | ␉␉if (putfn_p)␊ |
200 | ␉␉{␊ |
201 | ␉␉␉(void)(*putfn_p)(c, putfn_arg);␊ |
202 | ␉␉}␊ |
203 | ␉␉len++;␊ |
204 | ␉}␊ |
205 | ␉minwidth = 0;␊ |
206 | ␉flag = 0;␊ |
207 | ␉ells = 0;␊ |
208 | again:␊ |
209 | ␉c = *fmt++;␊ |
210 | ␉switch (c)␊ |
211 | ␉{␊ |
212 | ␉␉case 'l':␊ |
213 | ␉␉␉if (ells < 2)␊ |
214 | ␉␉␉{␊ |
215 | ␉␉␉␉++ells;␊ |
216 | ␉␉␉}␊ |
217 | ␉␉␉goto again;␊ |
218 | ␉␉case ' ':␊ |
219 | ␉␉␉flag |= SPACE;␊ |
220 | ␉␉␉goto again;␊ |
221 | ␉␉case '0':␊ |
222 | ␉␉␉if (minwidth == 0)␊ |
223 | ␉␉␉{␊ |
224 | ␉␉␉␉/* this is a flag */␊ |
225 | ␉␉␉␉flag |= ZERO;␊ |
226 | ␉␉␉␉goto again;␊ |
227 | ␉␉␉} /* fall through */␊ |
228 | ␉␉case '1':␊ |
229 | ␉␉case '2':␊ |
230 | ␉␉case '3':␊ |
231 | ␉␉case '4':␊ |
232 | ␉␉case '5':␊ |
233 | ␉␉case '6':␊ |
234 | ␉␉case '7':␊ |
235 | ␉␉case '8':␊ |
236 | ␉␉case '9':␊ |
237 | ␉␉␉minwidth *= 10;␊ |
238 | ␉␉␉minwidth += c - '0';␊ |
239 | ␉␉␉goto again;␊ |
240 | ␉␉case 'X':␊ |
241 | ␉␉␉flag |= UCASE;␊ |
242 | ␉␉␉/* fall through */␊ |
243 | ␉␉case 'x':␊ |
244 | ␉␉␉b = 16;␊ |
245 | ␉␉␉goto number;␊ |
246 | ␉␉case 'd':␊ |
247 | ␉␉case 'i':␊ |
248 | ␉␉␉flag |= SIGNED;␊ |
249 | ␉␉␉/* fall through */␊ |
250 | ␉␉case 'u':␊ |
251 | ␉␉␉b = 10;␊ |
252 | ␉␉␉goto number;␊ |
253 | ␉␉case 'o': case 'O':␊ |
254 | ␉␉␉b = 8;␊ |
255 | ␉␉number:␊ |
256 | ␉␉␉switch (ells)␊ |
257 | ␉␉␉{␊ |
258 | ␉␉␉case 2:␊ |
259 | ␉␉␉␉len += printn(va_arg(ap, unsigned long long), b, flag, minwidth, putfn_p, putfn_arg);␊ |
260 | ␉␉␉␉break;␊ |
261 | ␉␉␉case 1:␊ |
262 | ␉␉␉␉len += printn(va_arg(ap, unsigned long), b, flag, minwidth, putfn_p, putfn_arg);␊ |
263 | ␉␉␉␉break;␊ |
264 | ␉␉␉default:␊ |
265 | ␉␉␉␉len += printn(va_arg(ap, unsigned int), b, flag, minwidth, putfn_p, putfn_arg);␊ |
266 | ␉␉␉␉break;␊ |
267 | ␉␉␉}␊ |
268 | ␉␉␉break;␊ |
269 | ␉␉case 's':␊ |
270 | ␉␉␉s = va_arg(ap, const char*);␊ |
271 | ␉␉␉if (!s)␊ |
272 | ␉␉␉{␊ |
273 | ␉␉␉␉s = "(null)";␊ |
274 | ␉␉␉}␊ |
275 | ␉␉␉width = 0;␊ |
276 | ␉␉␉if (!putfn_p)␊ |
277 | ␉␉␉{␊ |
278 | ␉␉␉␉while ((c = *s++))␊ |
279 | ␉␉␉␉{␊ |
280 | ␉␉␉␉␉width++;␊ |
281 | ␉␉␉␉}␊ |
282 | ␉␉␉␉len += ((width < minwidth) ? minwidth : width);␊ |
283 | ␉␉␉␉break;␊ |
284 | ␉␉␉}␊ |
285 | ␉␉␉while ((c = *s++))␊ |
286 | ␉␉␉{␊ |
287 | ␉␉␉␉(void)(*putfn_p)(c, putfn_arg);␊ |
288 | ␉␉␉␉len++;␊ |
289 | ␉␉␉␉width++;␊ |
290 | ␉␉␉}␊ |
291 | ␉␉␉while (width++ < minwidth)␊ |
292 | ␉␉␉{␊ |
293 | ␉␉␉␉(void)(*putfn_p)(' ', putfn_arg);␊ |
294 | ␉␉␉␉len++;␊ |
295 | ␉␉␉}␊ |
296 | ␉␉␉break;␊ |
297 | ␉␉case 'c':␊ |
298 | ␉␉␉if (putfn_p)␊ |
299 | ␉␉␉{␊ |
300 | ␉␉␉␉(void)(*putfn_p)((char) va_arg(ap, int), putfn_arg);␊ |
301 | ␉␉␉}␊ |
302 | ␉␉␉len++;␊ |
303 | ␉␉␉break;␊ |
304 | ␉␉case '%':␊ |
305 | ␉␉␉if (putfn_p)␊ |
306 | ␉␉␉{␊ |
307 | ␉␉␉␉(void)(*putfn_p)('%', putfn_arg);␊ |
308 | ␉␉␉}␊ |
309 | ␉␉␉len++;␊ |
310 | ␉␉␉break;␊ |
311 | ␉␉case 'p':␊ |
312 | ␉␉␉len += printp(va_arg(ap, const void*), minwidth, putfn_p, putfn_arg);␊ |
313 | ␉␉␉break;␊ |
314 | ␉␉case 'n':␊ |
315 | ␉␉␉s = va_arg(ap, const char*);␊ |
316 | ␉␉␉if (s)␊ |
317 | ␉␉␉{␊ |
318 | ␉␉␉␉*(int*) s = len;␊ |
319 | ␉␉␉}␊ |
320 | ␉␉␉break;␊ |
321 | ␉␉default:␊ |
322 | ␉␉␉break;␊ |
323 | ␉}␊ |
324 | ␉goto loop;␊ |
325 | }␊ |
326 | |