]>
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 | ||
51 | /* | |
52 | * Scaled down version of C Library printf. | |
53 | * Used to print diagnostic information directly on console tty. | |
54 | * Since it is not interrupt driven, all system activities are | |
55 | * suspended. | |
56 | * | |
57 | */ | |
58 | ||
59 | /* | |
60 | * Printn prints a number n in base b. | |
61 | * We don't use recursion to avoid deep kernel stacks. | |
62 | */ | |
63 | static void | |
64 | printn(n, b, flag, minwidth, putfn_p, putfn_arg) | |
65 | u_long n; | |
66 | int b, flag, minwidth; | |
67 | void (*putfn_p)(); | |
68 | void *putfn_arg; | |
69 | { | |
70 | char prbuf[11]; | |
71 | register char *cp; | |
72 | int width = 0, neg = 0; | |
73 | ||
74 | if (b == 10 && (int)n < 0) { | |
75 | neg = 1; | |
76 | n = (unsigned)(-(int)n); | |
77 | } | |
78 | cp = prbuf; | |
79 | do { | |
80 | *cp++ = "0123456789abcdef"[n%b]; | |
81 | n /= b; | |
82 | width++; | |
83 | } while (n); | |
84 | ||
85 | if (neg) { | |
86 | (*putfn_p)('-', putfn_arg); | |
87 | width++; | |
88 | } | |
89 | while (width++ < minwidth) | |
90 | (*putfn_p)( (flag & ZERO) ? '0' : ' ', putfn_arg); | |
91 | ||
92 | do | |
93 | (*putfn_p)(*--cp, putfn_arg); | |
94 | while (cp > prbuf); | |
95 | } | |
96 | ||
97 | void prf( | |
98 | const char *fmt, | |
99 | unsigned int *adx, | |
100 | void (*putfn_p)(), | |
101 | void *putfn_arg | |
102 | ) | |
103 | { | |
104 | int b, c; | |
105 | char *s; | |
106 | int flag = 0, minwidth = 0, width = 0; | |
107 | ||
108 | loop: | |
109 | while ((c = *fmt++) != '%') { | |
110 | if(c == '\0') | |
111 | return; | |
112 | (*putfn_p)(c, putfn_arg); | |
113 | } | |
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 | /* this is a flag */ | |
125 | flag |= ZERO; | |
126 | goto again; | |
127 | } /* fall through */ | |
128 | case '1': | |
129 | case '2': | |
130 | case '3': | |
131 | case '4': | |
132 | case '5': | |
133 | case '6': | |
134 | case '7': | |
135 | case '8': | |
136 | case '9': | |
137 | minwidth *= 10; | |
138 | minwidth += c - '0'; | |
139 | goto again; | |
140 | case 'x': case 'X': | |
141 | b = 16; | |
142 | goto number; | |
143 | case 'd': | |
144 | b = 10; | |
145 | goto number; | |
146 | case 'o': case 'O': | |
147 | b = 8; | |
148 | number: | |
149 | printn((u_long)*adx, b, flag, minwidth, putfn_p, putfn_arg); | |
150 | break; | |
151 | case 's': | |
152 | s = (char *)*adx; | |
153 | while (c = *s++) { | |
154 | (*putfn_p)(c, putfn_arg); | |
155 | width++; | |
156 | } | |
157 | while (width++ < minwidth) | |
158 | (*putfn_p)(' ', putfn_arg); | |
159 | break; | |
160 | case 'c': | |
161 | (*putfn_p)((char)*adx, putfn_arg); | |
162 | break; | |
163 | } | |
164 | adx++; | |
165 | goto loop; | |
166 | } |