Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/libsa/prf.c

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.c7.1 (Berkeley) 6/5/86
39 */
40
41#include <sys/param.h>
42
43#define SPACE1
44#define ZERO2
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 */
59static void
60printn(n, b, flag, minwidth, putfn_p, putfn_arg)
61u_long n;
62int b, flag, minwidth;
63void (*putfn_p)();
64void *putfn_arg;
65{
66char prbuf[11];
67register char *cp;
68int width = 0, neg = 0;
69
70if (b == 10 && (int)n < 0) {
71neg = 1;
72n = (unsigned)(-(int)n);
73}
74cp = prbuf;
75do {
76*cp++ = "0123456789abcdef0123456789ABCDEF"[(flag & UCASE) + n%b];
77n /= b;
78width++;
79} while (n);
80
81if (neg) {
82(*putfn_p)('-', putfn_arg);
83width++;
84}
85while (width++ < minwidth)
86(*putfn_p)( (flag & ZERO) ? '0' : ' ', putfn_arg);
87
88do
89(*putfn_p)(*--cp, putfn_arg);
90while (cp > prbuf);
91}
92
93void prf(
94 char *fmt,
95 unsigned int *adx,
96 void (*putfn_p)(),
97 void *putfn_arg
98 )
99{
100int b, c;
101char *s;
102int flag = 0, width = 0;
103int minwidth;
104
105loop:
106while ((c = *fmt++) != '%') {
107if(c == '\0')
108return;
109(*putfn_p)(c, putfn_arg);
110}
111minwidth = 0;
112again:
113c = *fmt++;
114switch (c) {
115case 'l':
116goto again;
117case ' ':
118flag |= SPACE;
119goto again;
120case '0':
121if (minwidth == 0) {
122/* this is a flag */
123flag |= ZERO;
124goto again;
125} /* fall through */
126case '1':
127case '2':
128case '3':
129case '4':
130case '5':
131case '6':
132case '7':
133case '8':
134case '9':
135minwidth *= 10;
136minwidth += c - '0';
137goto again;
138 case 'X':
139flag |= UCASE;
140/* fall through */
141case 'x':
142b = 16;
143goto number;
144case 'd':
145b = 10;
146goto number;
147case 'o': case 'O':
148b = 8;
149number:
150printn((u_long)*adx, b, flag, minwidth, putfn_p, putfn_arg);
151break;
152case 's':
153s = (char *)*adx;
154while ((c = *s++)) {
155(*putfn_p)(c, putfn_arg);
156width++;
157}
158while (width++ < minwidth)
159(*putfn_p)(' ', putfn_arg);
160break;
161case 'c':
162(*putfn_p)((char)*adx, putfn_arg);
163break;
164default:
165break;
166}
167adx++;
168goto loop;
169}
170
171int prf_fmt_str_len(
172char *fmt,
173unsigned int *adx
174)
175{
176
177int b, c, len =0;
178char *s;
179int flag = 0, width = 0;
180int minwidth;
181
182loop:
183while ((c = *fmt++) != '%') {
184if(c == '\0')
185return len;
186len++;
187}
188minwidth = 0;
189again:
190c = *fmt++;
191switch (c) {
192case 'l':
193goto again;
194case ' ':
195flag |= SPACE;
196goto again;
197case '0':
198if (minwidth == 0) {
199/* this is a flag */
200flag |= ZERO;
201goto again;
202} /* fall through */
203case '1':
204case '2':
205case '3':
206case '4':
207case '5':
208case '6':
209case '7':
210case '8':
211case '9':
212minwidth *= 10;
213minwidth += c - '0';
214goto again;
215 case 'X':
216flag |= UCASE;
217/* fall through */
218case 'x':
219b = 16;
220goto number;
221case 'd':
222b = 10;
223goto number;
224case 'o': case 'O':
225b = 8;
226number:
227len++;
228break;
229case 's':
230s = (char *)*adx;
231while ((c = *s++)) {
232len++;
233width++;
234}
235while (width++ < minwidth)
236len++;
237break;
238case 'c':
239len++;
240break;
241default:
242break;
243}
244adx++;
245goto loop;
246}
247

Archive Download this file

Revision: 1804