0f12672a5001a2e2618d279e28f228e8c922440d
[coreboot.git] / src / console / vtxprintf.c
1 /*  vtxprintf.c, from
2  *    linux/lib/vsprintf.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 #include <stdarg.h>
8 #include <string.h>
9
10 /* haha, don't need ctype.c */
11 #define isdigit(c)      ((c) >= '0' && (c) <= '9')
12 #define is_digit isdigit
13 #define isxdigit(c)     (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
14
15 static unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
16 {
17         unsigned long result = 0,value;
18
19         if (!base) {
20                 base = 10;
21                 if (*cp == '0') {
22                         base = 8;
23                         cp++;
24                         if ((*cp == 'x') && isxdigit(cp[1])) {
25                                 cp++;
26                                 base = 16;
27                         }
28                 }
29         }
30         while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
31             ? toupper(*cp) : *cp)-'A'+10) < base) {
32                 result = result*base + value;
33                 cp++;
34         }
35         if (endp)
36                 *endp = (char *)cp;
37         return result;
38 }
39
40 static long simple_strtol(const char *cp,char **endp,unsigned int base)
41 {
42         if(*cp=='-')
43                 return -simple_strtoul(cp+1,endp,base);
44         return simple_strtoul(cp,endp,base);
45 }
46
47
48 static int skip_atoi(const char **s)
49 {
50         int i=0;
51
52         while (is_digit(**s))
53                 i = i*10 + *((*s)++) - '0';
54         return i;
55 }
56
57 #define ZEROPAD 1               /* pad with zero */
58 #define SIGN    2               /* unsigned/signed long */
59 #define PLUS    4               /* show plus */
60 #define SPACE   8               /* space if plus */
61 #define LEFT    16              /* left justified */
62 #define SPECIAL 32              /* 0x */
63 #define LARGE   64              /* use 'ABCDEF' instead of 'abcdef' */
64
65 #define do_div(n,base) ({ \
66 int __res; \
67 __res = ((unsigned long) n) % (unsigned) base; \
68 n = ((unsigned long) n) / (unsigned) base; \
69 __res; })
70
71 static int number(void (*tx_byte)(unsigned char byte), long num, int base, int size, int precision
72         ,int type)
73 {
74         char c,sign,tmp[66];
75         const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
76         int i;
77         int count = 0;
78
79         if (type & LARGE)
80                 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
81         if (type & LEFT)
82                 type &= ~ZEROPAD;
83         if (base < 2 || base > 36)
84                 return 0;
85         c = (type & ZEROPAD) ? '0' : ' ';
86         sign = 0;
87         if (type & SIGN) {
88                 if (num < 0) {
89                         sign = '-';
90                         num = -num;
91                         size--;
92                 } else if (type & PLUS) {
93                         sign = '+';
94                         size--;
95                 } else if (type & SPACE) {
96                         sign = ' ';
97                         size--;
98                 }
99         }
100         if (type & SPECIAL) {
101                 if (base == 16)
102                         size -= 2;
103                 else if (base == 8)
104                         size--;
105         }
106         i = 0;
107         if (num == 0)
108                 tmp[i++]='0';
109         else while (num != 0)
110                 tmp[i++] = digits[do_div(num,base)];
111         if (i > precision)
112                 precision = i;
113         size -= precision;
114         if (!(type&(ZEROPAD+LEFT)))
115                 while(size-->0)
116                         tx_byte(' '), count++;
117         if (sign)
118                 tx_byte(sign), count++;
119         if (type & SPECIAL) {
120                 if (base==8)
121                         tx_byte('0'), count++;
122                 else if (base==16) {
123                         tx_byte('0'), count++;
124                         tx_byte(digits[33]), count++;
125                 }
126         }
127         if (!(type & LEFT))
128                 while (size-- > 0)
129                         tx_byte(c), count++;
130         while (i < precision--)
131                 tx_byte('0'), count++;
132         while (i-- > 0)
133                 tx_byte(tmp[i]), count++;
134         while (size-- > 0)
135                 tx_byte(' '), count++;
136         return count;
137 }
138
139
140 int vtxprintf(void (*tx_byte)(unsigned char byte), const char *fmt, va_list args)
141 {
142         int len;
143         unsigned long num;
144         int i, base;
145         const char *s;
146
147         int flags;              /* flags to number() */
148
149         int field_width;        /* width of output field */
150         int precision;          /* min. # of digits for integers; max
151                                    number of chars for from string */
152         int qualifier;          /* 'h', 'l', or 'L' for integer fields */
153         
154         int count;
155
156         for (count=0; *fmt ; ++fmt) {
157                 if (*fmt != '%') {
158                         tx_byte(*fmt), count++;
159                         continue;
160                 }
161                         
162                 /* process flags */
163                 flags = 0;
164                 repeat:
165                         ++fmt;          /* this also skips first '%' */
166                         switch (*fmt) {
167                                 case '-': flags |= LEFT; goto repeat;
168                                 case '+': flags |= PLUS; goto repeat;
169                                 case ' ': flags |= SPACE; goto repeat;
170                                 case '#': flags |= SPECIAL; goto repeat;
171                                 case '0': flags |= ZEROPAD; goto repeat;
172                                 }
173                 
174                 /* get field width */
175                 field_width = -1;
176                 if (is_digit(*fmt))
177                         field_width = skip_atoi(&fmt);
178                 else if (*fmt == '*') {
179                         ++fmt;
180                         /* it's the next argument */
181                         field_width = va_arg(args, int);
182                         if (field_width < 0) {
183                                 field_width = -field_width;
184                                 flags |= LEFT;
185                         }
186                 }
187
188                 /* get the precision */
189                 precision = -1;
190                 if (*fmt == '.') {
191                         ++fmt;  
192                         if (is_digit(*fmt))
193                                 precision = skip_atoi(&fmt);
194                         else if (*fmt == '*') {
195                                 ++fmt;
196                                 /* it's the next argument */
197                                 precision = va_arg(args, int);
198                         }
199                         if (precision < 0)
200                                 precision = 0;
201                 }
202
203                 /* get the conversion qualifier */
204                 qualifier = -1;
205                 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
206                         qualifier = *fmt;
207                         ++fmt;
208                 }
209
210                 /* default base */
211                 base = 10;
212
213                 switch (*fmt) {
214                 case 'c':
215                         if (!(flags & LEFT))
216                                 while (--field_width > 0)
217                                         tx_byte(' '), count++;
218                         tx_byte((unsigned char) va_arg(args, int)), count++;
219                         while (--field_width > 0)
220                                 tx_byte(' '), count++;
221                         continue;
222
223                 case 's':
224                         s = va_arg(args, char *);
225                         if (!s)
226                                 s = "<NULL>";
227
228                         len = strnlen(s, precision);
229
230                         if (!(flags & LEFT))
231                                 while (len < field_width--)
232                                         tx_byte(' '), count++;
233                         for (i = 0; i < len; ++i)
234                                 tx_byte(*s++), count++;
235                         while (len < field_width--)
236                                 tx_byte(' '), count++;
237                         continue;
238
239                 case 'p':
240                         if (field_width == -1) {
241                                 field_width = 2*sizeof(void *);
242                                 flags |= ZEROPAD;
243                         }
244                         count += number(tx_byte,
245                                 (unsigned long) va_arg(args, void *), 16,
246                                 field_width, precision, flags);
247                         continue;
248
249
250                 case 'n':
251                         if (qualifier == 'l') {
252                                 long * ip = va_arg(args, long *);
253                                 *ip = count;
254                         } else {
255                                 int * ip = va_arg(args, int *);
256                                 *ip = count;
257                         }
258                         continue;
259
260                 case '%':
261                         tx_byte('%'), count++;
262                         continue;
263
264                 /* integer number formats - set up the flags and "break" */
265                 case 'o':
266                         base = 8;
267                         break;
268
269                 case 'X':
270                         flags |= LARGE;
271                 case 'x':
272                         base = 16;
273                         break;
274
275                 case 'd':
276                 case 'i':
277                         flags |= SIGN;
278                 case 'u':
279                         break;
280
281                 default:
282                         tx_byte('%'), count++;
283                         if (*fmt)
284                                 tx_byte(*fmt), count++;
285                         else
286                                 --fmt;
287                         continue;
288                 }
289                 if (qualifier == 'l')
290                         num = va_arg(args, unsigned long);
291                 else if (qualifier == 'h') {
292                         num = (unsigned short) va_arg(args, int);
293                         if (flags & SIGN)
294                                 num = (short) num;
295                 } else if (flags & SIGN)
296                         num = va_arg(args, int);
297                 else
298                         num = va_arg(args, unsigned int);
299                 count += number(tx_byte, num, base, field_width, precision, flags);
300         }
301         return count;
302 }
303