]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * The contents of this file constitute Original Code as defined in and | |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
11 | * | |
12 | * This Original Code and all software distributed under the License are | |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* | |
23 | * Mach Operating System | |
24 | * Copyright (c) 1990 Carnegie-Mellon University | |
25 | * Copyright (c) 1989 Carnegie-Mellon University | |
26 | * Copyright (c) 1988 Carnegie-Mellon University | |
27 | * Copyright (c) 1987 Carnegie-Mellon University | |
28 | * All rights reserved. The CMU software License Agreement specifies | |
29 | * the terms and conditions for use and redistribution. | |
30 | */ | |
31 | /* | |
32 | * Copyright (c) 1982, 1986 Regents of the University of California. | |
33 | * All rights reserved. The Berkeley software License Agreement | |
34 | * specifies the terms and conditions for redistribution. | |
35 | * | |
36 | * @(#)prf.c 7.1 (Berkeley) 6/5/86 | |
37 | */ | |
38 | /* | |
39 | * prf.c - Helpers for the printf function. | |
40 | * | |
41 | * Copyright (c) 1998-2002 Apple Computer, Inc. | |
42 | * | |
43 | * DRI: Josh de Cesare | |
44 | */ | |
45 | ||
46 | #include <sys/param.h> | |
47 | ||
48 | #define SPACE 1 | |
49 | #define ZERO 2 | |
50 | #define UCASE 16 | |
51 | ||
52 | /* | |
53 | * Scaled down version of C Library printf. | |
54 | * Used to print diagnostic information directly on console tty. | |
55 | * Since it is not interrupt driven, all system activities are | |
56 | * suspended. | |
57 | * | |
58 | */ | |
59 | ||
60 | /* | |
61 | * Printn prints a number n in base b. | |
62 | * We don't use recursion to avoid deep kernel stacks. | |
63 | */ | |
64 | static void | |
65 | printn(n, b, flag, minwidth, putfn_p, putfn_arg) | |
66 | u_long n; | |
67 | int b, flag, minwidth; | |
68 | void (*putfn_p)(); | |
69 | void *putfn_arg; | |
70 | { | |
71 | char prbuf[11]; | |
72 | register char *cp; | |
73 | int width = 0, neg = 0; | |
74 | ||
75 | if (b == 10 && (int)n < 0) { | |
76 | neg = 1; | |
77 | n = (unsigned)(-(int)n); | |
78 | } | |
79 | cp = prbuf; | |
80 | do { | |
81 | *cp++ = "0123456789abcdef0123456789ABCDEF"[(flag & UCASE) + n%b]; | |
82 | n /= b; | |
83 | width++; | |
84 | } while (n); | |
85 | ||
86 | if (neg) { | |
87 | (*putfn_p)('-', putfn_arg); | |
88 | width++; | |
89 | } | |
90 | while (width++ < minwidth) | |
91 | (*putfn_p)( (flag & ZERO) ? '0' : ' ', putfn_arg); | |
92 | ||
93 | do | |
94 | (*putfn_p)(*--cp, putfn_arg); | |
95 | while (cp > prbuf); | |
96 | } | |
97 | ||
98 | void prf( | |
99 | const char *fmt, | |
100 | unsigned int *adx, | |
101 | void (*putfn_p)(), | |
102 | void *putfn_arg | |
103 | ) | |
104 | { | |
105 | int b, c; | |
106 | char *s; | |
107 | int flag = 0, minwidth = 0, width = 0; | |
108 | ||
109 | loop: | |
110 | while ((c = *fmt++) != '%') { | |
111 | if(c == '\0') | |
112 | return; | |
113 | (*putfn_p)(c, putfn_arg); | |
114 | } | |
115 | again: | |
116 | c = *fmt++; | |
117 | switch (c) { | |
118 | case 'l': | |
119 | goto again; | |
120 | case ' ': | |
121 | flag |= SPACE; | |
122 | goto again; | |
123 | case '0': | |
124 | if (minwidth == 0) { | |
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 | printn((u_long)*adx, b, flag, minwidth, putfn_p, putfn_arg); | |
154 | break; | |
155 | case 's': | |
156 | s = (char *)*adx; | |
157 | while (c = *s++) { | |
158 | (*putfn_p)(c, putfn_arg); | |
159 | width++; | |
160 | } | |
161 | while (width++ < minwidth) | |
162 | (*putfn_p)(' ', putfn_arg); | |
163 | break; | |
164 | case 'c': | |
165 | (*putfn_p)((char)*adx, putfn_arg); | |
166 | break; | |
167 | } | |
168 | adx++; | |
169 | goto loop; | |
170 | } |