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 | ␉{␊ |
76 | ␉␉*cp++ = "0123456789abcdef0123456789ABCDEF"[(flag & UCASE) + n%b];␊ |
77 | ␉␉n /= b;␊ |
78 | ␉␉width++;␊ |
79 | ␉} while (n);␊ |
80 | ␉␊ |
81 | ␉if (neg) {␊ |
82 | ␉␉(*putfn_p)('-', putfn_arg);␊ |
83 | ␉␉width++;␊ |
84 | ␉}␊ |
85 | ␉while (width++ < minwidth)␊ |
86 | ␉␉(*putfn_p)( (flag & ZERO) ? '0' : ' ', putfn_arg);␊ |
87 | ␉␊ |
88 | ␉do␊ |
89 | ␉␉(*putfn_p)(*--cp, putfn_arg);␊ |
90 | ␉while (cp > prbuf);␊ |
91 | }␊ |
92 | ␊ |
93 | int prf(char *fmt, unsigned int *adx, void (*putfn_p)(), void *putfn_arg )␊ |
94 | {␊ |
95 | ␉int b, c, len =0;␊ |
96 | ␉char *s;␊ |
97 | ␉int flag = 0, width = 0;␊ |
98 | ␉int minwidth;␊ |
99 | ␊ |
100 | loop:␊ |
101 | ␉while ((c = *fmt++) != '%')␊ |
102 | ␉{␊ |
103 | ␉␉if(c == '\0')␊ |
104 | ␉␉{␊ |
105 | ␉␉␉return len;␊ |
106 | ␉␉}␊ |
107 | ␉␉if (putfn_p)␊ |
108 | ␉␉{␊ |
109 | ␉␉␉(*putfn_p)(c, putfn_arg);␊ |
110 | ␉␉}␊ |
111 | ␉␉len++;␊ |
112 | ␉}␊ |
113 | ␉minwidth = 0;␊ |
114 | again:␊ |
115 | ␉c = *fmt++;␊ |
116 | ␉switch (c) {␊ |
117 | ␉␉case 'l':␊ |
118 | ␉␉␉goto again;␊ |
119 | ␉␉case ' ':␊ |
120 | ␉␉␉flag |= SPACE;␊ |
121 | ␉␉␉goto again;␊ |
122 | ␉␉case '0':␊ |
123 | ␉␉␉if (minwidth == 0)␊ |
124 | ␉␉␉{␊ |
125 | ␉␉␉␉/* this is a flag */␊ |
126 | ␉␉␉␉flag |= ZERO;␊ |
127 | ␉␉␉␉goto again;␊ |
128 | ␉␉␉} /* fall through */␊ |
129 | ␉␉case '1':␊ |
130 | ␉␉case '2':␊ |
131 | ␉␉case '3':␊ |
132 | ␉␉case '4':␊ |
133 | ␉␉case '5':␊ |
134 | ␉␉case '6':␊ |
135 | ␉␉case '7':␊ |
136 | ␉␉case '8':␊ |
137 | ␉␉case '9':␊ |
138 | ␉␉␉minwidth *= 10;␊ |
139 | ␉␉␉minwidth += c - '0';␊ |
140 | ␉␉␉goto again;␊ |
141 | case 'X':␊ |
142 | ␉␉␉flag |= UCASE;␊ |
143 | ␉␉␉/* fall through */␊ |
144 | ␉␉case 'x':␊ |
145 | ␉␉␉b = 16;␊ |
146 | ␉␉␉goto number;␊ |
147 | ␉␉case 'd':␊ |
148 | ␉␉␉b = 10;␊ |
149 | ␉␉␉goto number;␊ |
150 | ␉␉case 'o': case 'O':␊ |
151 | ␉␉␉b = 8;␊ |
152 | ␉␉number:␊ |
153 | ␉␉␉if (putfn_p)␊ |
154 | ␉␉␉{␊ |
155 | ␉␉␉␉printn((u_long)*adx, b, flag, minwidth, putfn_p, putfn_arg);␊ |
156 | ␉␉␉}␊ |
157 | ␉␉␉len++;␊ |
158 | ␉␉␉break;␊ |
159 | ␉␉case 's':␊ |
160 | ␉␉␉s = (char *)*adx;␊ |
161 | ␉␉␉while ((c = *s++))␊ |
162 | ␉␉␉{␊ |
163 | ␉␉␉␉if (putfn_p)␊ |
164 | ␉␉␉␉{␊ |
165 | ␉␉␉␉␉(*putfn_p)(c, putfn_arg);␊ |
166 | ␉␉␉␉}␊ |
167 | ␉␉␉␉len++;␊ |
168 | ␉␉␉␉width++;␊ |
169 | ␉␉␉}␊ |
170 | ␉␉␉while (width++ < minwidth)␊ |
171 | ␉␉␉{␊ |
172 | ␉␉␉␉if (putfn_p)␊ |
173 | ␉␉␉␉{␊ |
174 | ␉␉␉␉␉(*putfn_p)(' ', putfn_arg);␊ |
175 | ␉␉␉␉}␊ |
176 | ␉␉␉␉len++;␊ |
177 | ␉␉␉}␊ |
178 | ␉␉␉break;␊ |
179 | ␉␉case 'c':␊ |
180 | ␉␉␉if (putfn_p)␊ |
181 | ␉␉␉{␊ |
182 | ␉␉␉␉(*putfn_p)((char)*adx, putfn_arg);␊ |
183 | ␉␉␉}␊ |
184 | ␉␉␉len++;␊ |
185 | ␉␉␉break;␊ |
186 | ␉␉default:␊ |
187 | ␉␉␉break;␊ |
188 | ␉}␊ |
189 | ␉adx++;␊ |
190 | ␉goto loop;␊ |
191 | }␊ |
192 | |