Chameleon

Chameleon Svn Source Tree

Root/branches/zenith432/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 <stdarg.h>
42
43#define SPACE1
44#define ZERO2
45#define UCASE16
46#define SIGNED32
47
48/*
49 * Scaled down version of C Library printf.
50 * Used to print diagnostic information directly on console tty.
51 * Since it is not interrupt driven, all system activities are
52 * suspended.
53 *
54 */
55
56/*
57 * Printn prints a number n in base b.
58 * We don't use recursion to avoid deep kernel stacks.
59 */
60static int printn(
61unsigned long long n,
62int b,
63int flag,
64int minwidth,
65int (*putfn_p)(),
66void* putfn_arg
67)
68{
69char prbuf[22];
70register char *cp;
71int width = 0, neg = 0;
72
73if ((flag & SIGNED) && (long long)n < 0)
74{
75neg = 1;
76n = (unsigned long long)(-(long long)n);
77}
78cp = prbuf;
79do
80{
81*cp++ = "0123456789abcdef0123456789ABCDEF"[(flag & UCASE) + (int) (n%b)];
82n /= b;
83width++;
84}
85while (n);
86
87if (neg)
88{
89if (putfn_p)
90{
91(void)(*putfn_p)('-', putfn_arg);
92}
93width++;
94}
95if (!putfn_p)
96{
97return (width < minwidth) ? minwidth : width;
98}
99for (;width < minwidth; width++)
100(void)(*putfn_p)( (flag & ZERO) ? '0' : ' ', putfn_arg);
101
102do
103(void)(*putfn_p)(*--cp, putfn_arg);
104while (cp > prbuf);
105return width;
106}
107
108/*
109 * Printp prints a pointer.
110 */
111static int printp(
112const void* p,
113int minwidth,
114int (*putfn_p)(),
115void* putfn_arg
116)
117{
118int width = 0;
119
120if (p)
121{
122if (putfn_p)
123{
124(void)(*putfn_p)('0', putfn_arg);
125(void)(*putfn_p)('x', putfn_arg);
126}
127width = 2;
128minwidth = (minwidth >= 2) ? (minwidth - 2) : 0;
129}
130return width + printn((unsigned long long) p, 16, ZERO, minwidth, putfn_p, putfn_arg);
131}
132
133int prf(
134const char *fmt,
135va_list ap,
136int (*putfn_p)(),
137void *putfn_arg
138)
139{
140int b, c, len = 0;
141const char *s;
142int flag, width, ells;
143int minwidth;
144
145loop:
146while ((c = *fmt++) != '%')
147{
148if(c == '\0')
149{
150return len;
151}
152
153if (putfn_p)
154{
155(void)(*putfn_p)(c, putfn_arg);
156}
157len++;
158}
159minwidth = 0;
160flag = 0;
161ells = 0;
162again:
163c = *fmt++;
164switch (c)
165{
166case 'l':
167if (ells < 2)
168{
169++ells;
170}
171goto again;
172case ' ':
173flag |= SPACE;
174goto again;
175case '0':
176if (minwidth == 0)
177{
178/* this is a flag */
179flag |= ZERO;
180goto again;
181} /* fall through */
182case '1':
183case '2':
184case '3':
185case '4':
186case '5':
187case '6':
188case '7':
189case '8':
190case '9':
191minwidth *= 10;
192minwidth += c - '0';
193goto again;
194case 'X':
195flag |= UCASE;
196/* fall through */
197case 'x':
198b = 16;
199goto number;
200case 'd':
201case 'i':
202flag |= SIGNED;
203/* fall through */
204case 'u':
205b = 10;
206goto number;
207case 'o': case 'O':
208b = 8;
209number:
210switch (ells)
211{
212case 2:
213len += printn(va_arg(ap, unsigned long long), b, flag, minwidth, putfn_p, putfn_arg);
214break;
215case 1:
216len += printn(va_arg(ap, unsigned long), b, flag, minwidth, putfn_p, putfn_arg);
217break;
218default:
219len += printn(va_arg(ap, unsigned int), b, flag, minwidth, putfn_p, putfn_arg);
220break;
221}
222break;
223case 's':
224s = va_arg(ap, const char*);
225width = 0;
226while (s && (c = *s++))
227{
228if (putfn_p)
229{
230(void)(*putfn_p)(c, putfn_arg);
231}
232len++;
233width++;
234}
235while (width++ < minwidth)
236{
237if (putfn_p)
238{
239(void)(*putfn_p)(' ', putfn_arg);
240}
241len++;
242}
243break;
244case 'c':
245if (putfn_p)
246{
247(void)(*putfn_p)((char) va_arg(ap, int), putfn_arg);
248}
249len++;
250break;
251case '%':
252if (putfn_p)
253{
254(void)(*putfn_p)('%', putfn_arg);
255}
256len++;
257break;
258case 'p':
259len += printp(va_arg(ap, const void*), minwidth, putfn_p, putfn_arg);
260break;
261case 'n':
262s = va_arg(ap, const char*);
263if (s)
264{
265*(int*) s = len;
266}
267break;
268default:
269break;
270}
271goto loop;
272}
273

Archive Download this file

Revision: 2821