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 | ␉} while (n);␊ |
85 | ␊ |
86 | ␉if (neg)␊ |
87 | ␉{␊ |
88 | ␉␉if (putfn_p)␊ |
89 | ␉␉{␊ |
90 | ␉␉␉(void)(*putfn_p)('-', putfn_arg);␊ |
91 | ␉␉}␊ |
92 | ␉␉width++;␊ |
93 | ␉}␊ |
94 | ␉if (!putfn_p)␊ |
95 | ␉{␊ |
96 | ␉␉return (width < minwidth) ? minwidth : width;␊ |
97 | ␉}␊ |
98 | ␉for (;width < minwidth; width++)␊ |
99 | ␉␉(void)(*putfn_p)( (flag & ZERO) ? '0' : ' ', putfn_arg);␊ |
100 | ␊ |
101 | ␉do␊ |
102 | ␉␉(void)(*putfn_p)(*--cp, putfn_arg);␊ |
103 | ␉while (cp > prbuf);␊ |
104 | ␉return width;␊ |
105 | }␊ |
106 | ␊ |
107 | int prf(␊ |
108 | ␉const char *fmt,␊ |
109 | ␉va_list ap,␊ |
110 | ␉int (*putfn_p)(),␊ |
111 | ␉void *putfn_arg␊ |
112 | )␊ |
113 | {␊ |
114 | ␉int b, c, len = 0;␊ |
115 | ␉const char *s;␊ |
116 | ␉int flag, width, ells;␊ |
117 | ␉int minwidth;␊ |
118 | ␊ |
119 | loop:␊ |
120 | ␉while ((c = *fmt++) != '%')␊ |
121 | ␉{␊ |
122 | ␉␉if(c == '\0')␊ |
123 | ␉␉{␊ |
124 | ␉␉␉return len;␊ |
125 | ␉␉}␊ |
126 | ␊ |
127 | ␉␉if (putfn_p)␊ |
128 | ␉␉{␊ |
129 | ␉␉␉(void)(*putfn_p)(c, putfn_arg);␊ |
130 | ␉␉}␊ |
131 | ␉␉len++;␊ |
132 | ␉}␊ |
133 | ␉minwidth = 0;␊ |
134 | ␉flag = 0;␊ |
135 | ␉ells = 0;␊ |
136 | again:␊ |
137 | ␉c = *fmt++;␊ |
138 | ␉switch (c)␊ |
139 | ␉{␊ |
140 | ␉␉case 'l':␊ |
141 | ␉␉␉if (ells < 2)␊ |
142 | ␉␉␉{␊ |
143 | ␉␉␉␉++ells;␊ |
144 | ␉␉␉}␊ |
145 | ␉␉␉goto again;␊ |
146 | ␉␉case ' ':␊ |
147 | ␉␉␉flag |= SPACE;␊ |
148 | ␉␉␉goto again;␊ |
149 | ␉␉case '0':␊ |
150 | ␉␉␉if (minwidth == 0)␊ |
151 | ␉␉␉{␊ |
152 | ␉␉␉␉/* this is a flag */␊ |
153 | ␉␉␉␉flag |= ZERO;␊ |
154 | ␉␉␉␉goto again;␊ |
155 | ␉␉␉} /* fall through */␊ |
156 | ␉␉case '1':␊ |
157 | ␉␉case '2':␊ |
158 | ␉␉case '3':␊ |
159 | ␉␉case '4':␊ |
160 | ␉␉case '5':␊ |
161 | ␉␉case '6':␊ |
162 | ␉␉case '7':␊ |
163 | ␉␉case '8':␊ |
164 | ␉␉case '9':␊ |
165 | ␉␉␉minwidth *= 10;␊ |
166 | ␉␉␉minwidth += c - '0';␊ |
167 | ␉␉␉goto again;␊ |
168 | ␉␉case 'X':␊ |
169 | ␉␉␉flag |= UCASE;␊ |
170 | ␉␉␉/* fall through */␊ |
171 | ␉␉case 'x':␊ |
172 | ␉␉␉b = 16;␊ |
173 | ␉␉␉goto number;␊ |
174 | ␉␉case 'd':␊ |
175 | ␉␉␉flag |= SIGNED;␊ |
176 | ␉␉␉/* fall through */␊ |
177 | ␉␉case 'u':␊ |
178 | ␉␉␉b = 10;␊ |
179 | ␉␉␉goto number;␊ |
180 | ␉␉case 'o': case 'O':␊ |
181 | ␉␉␉b = 8;␊ |
182 | ␉␉number:␊ |
183 | ␉␉␉switch (ells)␊ |
184 | ␉␉␉{␊ |
185 | ␉␉␉case 2:␊ |
186 | ␉␉␉␉len += printn(va_arg(ap, unsigned long long), b, flag, minwidth, putfn_p, putfn_arg);␊ |
187 | ␉␉␉␉break;␊ |
188 | ␉␉␉case 1:␊ |
189 | ␉␉␉␉len += printn(va_arg(ap, unsigned long), b, flag, minwidth, putfn_p, putfn_arg);␊ |
190 | ␉␉␉␉break;␊ |
191 | ␉␉␉default:␊ |
192 | ␉␉␉␉len += printn(va_arg(ap, unsigned int), b, flag, minwidth, putfn_p, putfn_arg);␊ |
193 | ␉␉␉␉break;␊ |
194 | ␉␉␉}␊ |
195 | ␉␉␉break;␊ |
196 | ␉␉case 's':␊ |
197 | ␉␉␉s = va_arg(ap, const char*);␊ |
198 | ␉␉␉width = 0;␊ |
199 | ␉␉␉while (s && (c = *s++))␊ |
200 | ␉␉␉{␊ |
201 | ␉␉␉␉if (putfn_p)␊ |
202 | ␉␉␉␉{␊ |
203 | ␉␉␉␉␉(void)(*putfn_p)(c, putfn_arg);␊ |
204 | ␉␉␉␉}␊ |
205 | ␉␉␉␉len++;␊ |
206 | ␉␉␉␉width++;␊ |
207 | ␉␉␉}␊ |
208 | ␉␉␉while (width++ < minwidth)␊ |
209 | ␉␉␉{␊ |
210 | ␉␉␉␉if (putfn_p)␊ |
211 | ␉␉␉␉{␊ |
212 | ␉␉␉␉␉(void)(*putfn_p)(' ', putfn_arg);␊ |
213 | ␉␉␉␉}␊ |
214 | ␉␉␉␉len++;␊ |
215 | ␉␉␉}␊ |
216 | ␉␉␉break;␊ |
217 | ␉␉case 'c':␊ |
218 | ␉␉␉if (putfn_p)␊ |
219 | ␉␉␉{␊ |
220 | ␉␉␉␉(void)(*putfn_p)((char) va_arg(ap, int), putfn_arg);␊ |
221 | ␉␉␉}␊ |
222 | ␉␉␉len++;␊ |
223 | ␉␉␉break;␊ |
224 | ␉␉default:␊ |
225 | ␉␉␉break;␊ |
226 | ␉}␊ |
227 | ␉goto loop;␊ |
228 | }␊ |
229 | |