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