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 | /*␊ |
57 | * Printn prints a number n in base b.␊ |
58 | * We don't use recursion to avoid deep kernel stacks.␊ |
59 | */␊ |
60 | static int printn(␊ |
61 | ␉unsigned long long n,␊ |
62 | ␉int b,␊ |
63 | ␉int flag,␊ |
64 | ␉int minwidth,␊ |
65 | ␉int (*putfn_p)(),␊ |
66 | ␉void* putfn_arg␊ |
67 | ␉)␊ |
68 | {␊ |
69 | ␉char prbuf[22];␊ |
70 | ␉register char *cp;␊ |
71 | ␉int width = 0, neg = 0;␊ |
72 | ␊ |
73 | ␉if ((flag & SIGNED) && (long long)n < 0)␊ |
74 | ␉{␊ |
75 | ␉␉neg = 1;␊ |
76 | ␉␉n = (unsigned long long)(-(long long)n);␊ |
77 | ␉}␊ |
78 | ␉cp = prbuf;␊ |
79 | ␉do␊ |
80 | ␉{␊ |
81 | ␉␉*cp++ = "0123456789abcdef0123456789ABCDEF"[(flag & UCASE) + (int) (n%b)];␊ |
82 | ␉␉n /= b;␊ |
83 | ␉␉width++;␊ |
84 | ␉}␊ |
85 | ␉while (n);␊ |
86 | ␊ |
87 | ␉if (neg)␊ |
88 | ␉{␊ |
89 | ␉␉if (putfn_p)␊ |
90 | ␉␉{␊ |
91 | ␉␉␉(void)(*putfn_p)('-', putfn_arg);␊ |
92 | ␉␉}␊ |
93 | ␉␉width++;␊ |
94 | ␉}␊ |
95 | ␉if (!putfn_p)␊ |
96 | ␉{␊ |
97 | ␉␉return (width < minwidth) ? minwidth : width;␊ |
98 | ␉}␊ |
99 | ␉for (;width < minwidth; width++)␊ |
100 | ␉␉(void)(*putfn_p)( (flag & ZERO) ? '0' : ' ', putfn_arg);␊ |
101 | ␊ |
102 | ␉do␊ |
103 | ␉␉(void)(*putfn_p)(*--cp, putfn_arg);␊ |
104 | ␉while (cp > prbuf);␊ |
105 | ␉return width;␊ |
106 | }␊ |
107 | ␊ |
108 | /*␊ |
109 | * Printp prints a pointer.␊ |
110 | */␊ |
111 | static int printp(␊ |
112 | ␉const void* p,␊ |
113 | ␉int minwidth,␊ |
114 | ␉int (*putfn_p)(),␊ |
115 | ␉void* putfn_arg␊ |
116 | ␉)␊ |
117 | {␊ |
118 | ␉int width = 0;␊ |
119 | ␊ |
120 | ␉if (p)␊ |
121 | ␉{␊ |
122 | ␉␉if (putfn_p)␊ |
123 | ␉␉{␊ |
124 | ␉␉␉(void)(*putfn_p)('0', putfn_arg);␊ |
125 | ␉␉␉(void)(*putfn_p)('x', putfn_arg);␊ |
126 | ␉␉}␊ |
127 | ␉␉width = 2;␊ |
128 | ␉␉minwidth = (minwidth >= 2) ? (minwidth - 2) : 0;␊ |
129 | ␉}␊ |
130 | ␉return width + printn((unsigned long long) p, 16, ZERO, minwidth, putfn_p, putfn_arg);␊ |
131 | }␊ |
132 | ␊ |
133 | int prf(␊ |
134 | ␉const char *fmt,␊ |
135 | ␉va_list ap,␊ |
136 | ␉int (*putfn_p)(),␊ |
137 | ␉void *putfn_arg␊ |
138 | ␉)␊ |
139 | {␊ |
140 | ␉int b, c, len = 0;␊ |
141 | ␉const char *s;␊ |
142 | ␉int flag, width, ells;␊ |
143 | ␉int minwidth;␊ |
144 | ␊ |
145 | loop:␊ |
146 | ␉while ((c = *fmt++) != '%')␊ |
147 | ␉{␊ |
148 | ␉␉if(c == '\0')␊ |
149 | ␉␉{␊ |
150 | ␉␉␉return len;␊ |
151 | ␉␉}␊ |
152 | ␊ |
153 | ␉␉if (putfn_p)␊ |
154 | ␉␉{␊ |
155 | ␉␉␉(void)(*putfn_p)(c, putfn_arg);␊ |
156 | ␉␉}␊ |
157 | ␉␉len++;␊ |
158 | ␉}␊ |
159 | ␉minwidth = 0;␊ |
160 | ␉flag = 0;␊ |
161 | ␉ells = 0;␊ |
162 | again:␊ |
163 | ␉c = *fmt++;␊ |
164 | ␉switch (c)␊ |
165 | ␉{␊ |
166 | ␉␉case 'l':␊ |
167 | ␉␉␉if (ells < 2)␊ |
168 | ␉␉␉{␊ |
169 | ␉␉␉␉++ells;␊ |
170 | ␉␉␉}␊ |
171 | ␉␉␉goto again;␊ |
172 | ␉␉case ' ':␊ |
173 | ␉␉␉flag |= SPACE;␊ |
174 | ␉␉␉goto again;␊ |
175 | ␉␉case '0':␊ |
176 | ␉␉␉if (minwidth == 0)␊ |
177 | ␉␉␉{␊ |
178 | ␉␉␉␉/* this is a flag */␊ |
179 | ␉␉␉␉flag |= ZERO;␊ |
180 | ␉␉␉␉goto again;␊ |
181 | ␉␉␉} /* fall through */␊ |
182 | ␉␉case '1':␊ |
183 | ␉␉case '2':␊ |
184 | ␉␉case '3':␊ |
185 | ␉␉case '4':␊ |
186 | ␉␉case '5':␊ |
187 | ␉␉case '6':␊ |
188 | ␉␉case '7':␊ |
189 | ␉␉case '8':␊ |
190 | ␉␉case '9':␊ |
191 | ␉␉␉minwidth *= 10;␊ |
192 | ␉␉␉minwidth += c - '0';␊ |
193 | ␉␉␉goto again;␊ |
194 | ␉␉case 'X':␊ |
195 | ␉␉␉flag |= UCASE;␊ |
196 | ␉␉␉/* fall through */␊ |
197 | ␉␉case 'x':␊ |
198 | ␉␉␉b = 16;␊ |
199 | ␉␉␉goto number;␊ |
200 | ␉␉case 'd':␊ |
201 | ␉␉case 'i':␊ |
202 | ␉␉␉flag |= SIGNED;␊ |
203 | ␉␉␉/* fall through */␊ |
204 | ␉␉case 'u':␊ |
205 | ␉␉␉b = 10;␊ |
206 | ␉␉␉goto number;␊ |
207 | ␉␉case 'o': case 'O':␊ |
208 | ␉␉␉b = 8;␊ |
209 | ␉␉number:␊ |
210 | ␉␉␉switch (ells)␊ |
211 | ␉␉␉{␊ |
212 | ␉␉␉case 2:␊ |
213 | ␉␉␉␉len += printn(va_arg(ap, unsigned long long), b, flag, minwidth, putfn_p, putfn_arg);␊ |
214 | ␉␉␉␉break;␊ |
215 | ␉␉␉case 1:␊ |
216 | ␉␉␉␉len += printn(va_arg(ap, unsigned long), b, flag, minwidth, putfn_p, putfn_arg);␊ |
217 | ␉␉␉␉break;␊ |
218 | ␉␉␉default:␊ |
219 | ␉␉␉␉len += printn(va_arg(ap, unsigned int), b, flag, minwidth, putfn_p, putfn_arg);␊ |
220 | ␉␉␉␉break;␊ |
221 | ␉␉␉}␊ |
222 | ␉␉␉break;␊ |
223 | ␉␉case 's':␊ |
224 | ␉␉␉s = va_arg(ap, const char*);␊ |
225 | ␉␉␉width = 0;␊ |
226 | ␉␉␉while (s && (c = *s++))␊ |
227 | ␉␉␉{␊ |
228 | ␉␉␉␉if (putfn_p)␊ |
229 | ␉␉␉␉{␊ |
230 | ␉␉␉␉␉(void)(*putfn_p)(c, putfn_arg);␊ |
231 | ␉␉␉␉}␊ |
232 | ␉␉␉␉len++;␊ |
233 | ␉␉␉␉width++;␊ |
234 | ␉␉␉}␊ |
235 | ␉␉␉while (width++ < minwidth)␊ |
236 | ␉␉␉{␊ |
237 | ␉␉␉␉if (putfn_p)␊ |
238 | ␉␉␉␉{␊ |
239 | ␉␉␉␉␉(void)(*putfn_p)(' ', putfn_arg);␊ |
240 | ␉␉␉␉}␊ |
241 | ␉␉␉␉len++;␊ |
242 | ␉␉␉}␊ |
243 | ␉␉␉break;␊ |
244 | ␉␉case 'c':␊ |
245 | ␉␉␉if (putfn_p)␊ |
246 | ␉␉␉{␊ |
247 | ␉␉␉␉(void)(*putfn_p)((char) va_arg(ap, int), putfn_arg);␊ |
248 | ␉␉␉}␊ |
249 | ␉␉␉len++;␊ |
250 | ␉␉␉break;␊ |
251 | ␉␉case '%':␊ |
252 | ␉␉␉if (putfn_p)␊ |
253 | ␉␉␉{␊ |
254 | ␉␉␉␉(void)(*putfn_p)('%', putfn_arg);␊ |
255 | ␉␉␉}␊ |
256 | ␉␉␉len++;␊ |
257 | ␉␉␉break;␊ |
258 | ␉␉case 'p':␊ |
259 | ␉␉␉len += printp(va_arg(ap, const void*), minwidth, putfn_p, putfn_arg);␊ |
260 | ␉␉␉break;␊ |
261 | ␉␉case 'n':␊ |
262 | ␉␉␉s = va_arg(ap, const char*);␊ |
263 | ␉␉␉if (s)␊ |
264 | ␉␉␉{␊ |
265 | ␉␉␉␉*(int*) s = len;␊ |
266 | ␉␉␉}␊ |
267 | ␉␉␉break;␊ |
268 | ␉␉default:␊ |
269 | ␉␉␉break;␊ |
270 | ␉}␊ |
271 | ␉goto loop;␊ |
272 | }␊ |
273 | |