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