4 * Copyright (C) 1991, 1992 Linus Torvalds
9 #include <console/console.h>
10 #include <console/vtxprintf.h>
12 /* haha, don't need ctype.c */
13 #define isdigit(c) ((c) >= '0' && (c) <= '9')
14 #define is_digit isdigit
15 #define isxdigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
17 static int skip_atoi(const char **s)
22 i = i*10 + *((*s)++) - '0';
26 #define ZEROPAD 1 /* pad with zero */
27 #define SIGN 2 /* unsigned/signed long */
28 #define PLUS 4 /* show plus */
29 #define SPACE 8 /* space if plus */
30 #define LEFT 16 /* left justified */
31 #define SPECIAL 32 /* 0x */
32 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
34 static int number(void (*tx_byte)(unsigned char byte),
35 unsigned long long num, int base, int size, int precision, int type)
38 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
43 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
46 if (base < 2 || base > 36)
48 c = (type & ZEROPAD) ? '0' : ' ';
51 if ((signed long long)num < 0) {
55 } else if (type & PLUS) {
58 } else if (type & SPACE) {
73 tmp[i++] = digits[do_div(num,base)];
77 if (!(type&(ZEROPAD+LEFT)))
79 tx_byte(' '), count++;
81 tx_byte(sign), count++;
84 tx_byte('0'), count++;
86 tx_byte('0'), count++;
87 tx_byte(digits[33]), count++;
93 while (i < precision--)
94 tx_byte('0'), count++;
96 tx_byte(tmp[i]), count++;
98 tx_byte(' '), count++;
103 int vtxprintf(void (*tx_byte)(unsigned char byte), const char *fmt, va_list args)
106 unsigned long long num;
110 int flags; /* flags to number() */
112 int field_width; /* width of output field */
113 int precision; /* min. # of digits for integers; max
114 number of chars for from string */
115 int qualifier; /* 'h', 'l', or 'L' for integer fields */
119 #if defined(__SMM__) && CONFIG_SMM_TSEG
120 /* Fix pointer in TSEG */
121 tx_byte = console_tx_byte;
124 for (count=0; *fmt ; ++fmt) {
126 tx_byte(*fmt), count++;
133 ++fmt; /* this also skips first '%' */
135 case '-': flags |= LEFT; goto repeat;
136 case '+': flags |= PLUS; goto repeat;
137 case ' ': flags |= SPACE; goto repeat;
138 case '#': flags |= SPECIAL; goto repeat;
139 case '0': flags |= ZEROPAD; goto repeat;
142 /* get field width */
145 field_width = skip_atoi(&fmt);
146 else if (*fmt == '*') {
148 /* it's the next argument */
149 field_width = va_arg(args, int);
150 if (field_width < 0) {
151 field_width = -field_width;
156 /* get the precision */
161 precision = skip_atoi(&fmt);
162 else if (*fmt == '*') {
164 /* it's the next argument */
165 precision = va_arg(args, int);
171 /* get the conversion qualifier */
173 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
188 while (--field_width > 0)
189 tx_byte(' '), count++;
190 tx_byte((unsigned char) va_arg(args, int)), count++;
191 while (--field_width > 0)
192 tx_byte(' '), count++;
196 s = va_arg(args, char *);
200 len = strnlen(s, precision);
203 while (len < field_width--)
204 tx_byte(' '), count++;
205 for (i = 0; i < len; ++i)
206 tx_byte(*s++), count++;
207 while (len < field_width--)
208 tx_byte(' '), count++;
212 if (field_width == -1) {
213 field_width = 2*sizeof(void *);
216 count += number(tx_byte,
217 (unsigned long) va_arg(args, void *), 16,
218 field_width, precision, flags);
223 if (qualifier == 'L') {
224 long long *ip = va_arg(args, long long *);
226 } else if (qualifier == 'l') {
227 long * ip = va_arg(args, long *);
230 int * ip = va_arg(args, int *);
236 tx_byte('%'), count++;
239 /* integer number formats - set up the flags and "break" */
257 tx_byte('%'), count++;
259 tx_byte(*fmt), count++;
264 if (qualifier == 'L') {
265 num = va_arg(args, unsigned long long);
266 } else if (qualifier == 'l') {
267 num = va_arg(args, unsigned long);
268 } else if (qualifier == 'h') {
269 num = (unsigned short) va_arg(args, int);
272 } else if (flags & SIGN) {
273 num = va_arg(args, int);
275 num = va_arg(args, unsigned int);
277 count += number(tx_byte, num, base, field_width, precision, flags);