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 <sys/param.h>␊ |
42 | ␊ |
43 | #define SPACE␉1␊ |
44 | #define ZERO␉2␊ |
45 | #define UCASE 16␊ |
46 | ␊ |
47 | /*␊ |
48 | * Scaled down version of C Library printf.␊ |
49 | * Used to print diagnostic information directly on console tty.␊ |
50 | * Since it is not interrupt driven, all system activities are␊ |
51 | * suspended.␊ |
52 | *␊ |
53 | */␊ |
54 | ␊ |
55 | /*␊ |
56 | * Printn prints a number n in base b.␊ |
57 | * We don't use recursion to avoid deep kernel stacks.␊ |
58 | */␊ |
59 | static void printn(n, b, flag, minwidth, putfn_p, putfn_arg)␊ |
60 | ␉u_long n;␊ |
61 | ␉int b, flag, minwidth;␊ |
62 | ␉void (*putfn_p)();␊ |
63 | ␉void *putfn_arg;␊ |
64 | {␊ |
65 | ␉char prbuf[11];␊ |
66 | ␉register char *cp;␊ |
67 | ␉int width = 0, neg = 0;␊ |
68 | ␊ |
69 | ␉if (b == 10 && (int)n < 0) {␊ |
70 | ␉␉neg = 1;␊ |
71 | ␉␉n = (unsigned)(-(int)n);␊ |
72 | ␉}␊ |
73 | ␉cp = prbuf;␊ |
74 | ␉do {␊ |
75 | ␉␉*cp++ = "0123456789abcdef0123456789ABCDEF"[(flag & UCASE) + n%b];␊ |
76 | ␉␉n /= b;␊ |
77 | ␉␉width++;␊ |
78 | ␉} while (n);␊ |
79 | ␉␊ |
80 | ␉if (neg) {␊ |
81 | ␉␉(*putfn_p)('-', putfn_arg);␊ |
82 | ␉␉width++;␊ |
83 | ␉}␊ |
84 | ␉while (width++ < minwidth)␊ |
85 | ␉␉(*putfn_p)( (flag & ZERO) ? '0' : ' ', putfn_arg);␊ |
86 | ␉␊ |
87 | ␉do␊ |
88 | ␉␉(*putfn_p)(*--cp, putfn_arg);␊ |
89 | ␉while (cp > prbuf);␊ |
90 | }␊ |
91 | ␊ |
92 | int prf(␊ |
93 | ␉char *fmt,␊ |
94 | ␉unsigned int *adx,␊ |
95 | ␉void (*putfn_p)(),␊ |
96 | ␉void *putfn_arg␊ |
97 | )␊ |
98 | {␊ |
99 | ␉int b, c, len =0;␊ |
100 | ␉char *s;␊ |
101 | ␉int flag = 0, width = 0;␊ |
102 | ␉int minwidth;␊ |
103 | ␊ |
104 | loop:␊ |
105 | ␉while ((c = *fmt++) != '%') {␊ |
106 | ␉␉if(c == '\0')␊ |
107 | ␉␉{␊ |
108 | ␉␉␉return len;␊ |
109 | ␉␉}␊ |
110 | ␊ |
111 | ␉␉if (putfn_p)␊ |
112 | ␉␉{␊ |
113 | ␉␉␉(*putfn_p)(c, putfn_arg);␊ |
114 | ␉␉}␊ |
115 | ␉␉len++;␊ |
116 | ␉}␊ |
117 | ␉minwidth = 0;␊ |
118 | again:␊ |
119 | ␉c = *fmt++;␊ |
120 | ␉switch (c) {␊ |
121 | ␉␉case 'l':␊ |
122 | ␉␉␉goto again;␊ |
123 | ␉␉case ' ':␊ |
124 | ␉␉␉flag |= SPACE;␊ |
125 | ␉␉␉goto again;␊ |
126 | ␉␉case '0':␊ |
127 | ␉␉␉if (minwidth == 0)␊ |
128 | ␉␉␉{␊ |
129 | ␉␉␉␉/* this is a flag */␊ |
130 | ␉␉␉␉flag |= ZERO;␊ |
131 | ␉␉␉␉goto again;␊ |
132 | ␉␉␉} /* fall through */␊ |
133 | ␉␉case '1':␊ |
134 | ␉␉case '2':␊ |
135 | ␉␉case '3':␊ |
136 | ␉␉case '4':␊ |
137 | ␉␉case '5':␊ |
138 | ␉␉case '6':␊ |
139 | ␉␉case '7':␊ |
140 | ␉␉case '8':␊ |
141 | ␉␉case '9':␊ |
142 | ␉␉␉minwidth *= 10;␊ |
143 | ␉␉␉minwidth += c - '0';␊ |
144 | ␉␉␉goto again;␊ |
145 | ␉␉case 'X':␊ |
146 | ␉␉␉flag |= UCASE;␊ |
147 | ␉␉␉/* fall through */␊ |
148 | ␉␉case 'x':␊ |
149 | ␉␉␉b = 16;␊ |
150 | ␉␉␉goto number;␊ |
151 | ␉␉case 'd':␊ |
152 | ␉␉␉b = 10;␊ |
153 | ␉␉␉goto number;␊ |
154 | ␉␉case 'o': case 'O':␊ |
155 | ␉␉␉b = 8;␊ |
156 | ␉␉number:␊ |
157 | ␉␉␉if (putfn_p)␊ |
158 | ␉␉␉{␊ |
159 | ␉␉␉␉printn((u_long)*adx, b, flag, minwidth, putfn_p, putfn_arg);␊ |
160 | ␉␉␉}␊ |
161 | ␉␉␉len++;␊ |
162 | ␉␉␉break;␊ |
163 | ␉␉case 's':␊ |
164 | ␉␉␉s = (char *)*adx;␊ |
165 | ␉␉␉while ((c = *s++)) {␊ |
166 | ␉␉␉␉if (putfn_p)␊ |
167 | ␉␉␉␉{␊ |
168 | ␉␉␉␉␉(*putfn_p)(c, putfn_arg);␊ |
169 | ␉␉␉␉}␊ |
170 | ␉␉␉␉len++;␊ |
171 | ␉␉␉␉width++;␊ |
172 | ␉␉␉}␊ |
173 | ␉␉␉while (width++ < minwidth)␊ |
174 | ␉␉␉{␊ |
175 | ␉␉␉␉if (putfn_p)␊ |
176 | ␉␉␉␉{␊ |
177 | ␉␉␉␉␉(*putfn_p)(' ', putfn_arg);␊ |
178 | ␉␉␉␉}␊ |
179 | ␉␉␉␉len++;␊ |
180 | ␉␉␉}␊ |
181 | ␉␉␉break;␊ |
182 | ␉␉case 'c':␊ |
183 | ␉␉␉if (putfn_p)␊ |
184 | ␉␉␉{␊ |
185 | ␉␉␉␉(*putfn_p)((char)*adx, putfn_arg);␊ |
186 | ␉␉␉}␊ |
187 | ␉␉␉len++;␊ |
188 | ␉␉␉break;␊ |
189 | ␉␉default:␊ |
190 | ␉␉␉break;␊ |
191 | ␉}␊ |
192 | ␉adx++;␊ |
193 | ␉goto loop;␊ |
194 | }␊ |
195 | |