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