smaller changes to silence build warnings. (trivial)
[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 #include <div64.h>
10
11 /* haha, don't need ctype.c */
12 #define isdigit(c)      ((c) >= '0' && (c) <= '9')
13 #define is_digit isdigit
14 #define isxdigit(c)     (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
15
16 #if 0
17 /* We are using number() instead. So this code is obsoleted and should
18  * probably go away.
19  */
20
21 static unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
22 {
23         unsigned long result = 0,value;
24
25         if (!base) {
26                 base = 10;
27                 if (*cp == '0') {
28                         base = 8;
29                         cp++;
30                         if ((*cp == 'x') && isxdigit(cp[1])) {
31                                 cp++;
32                                 base = 16;
33                         }
34                 }
35         }
36         while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
37             ? toupper(*cp) : *cp)-'A'+10) < base) {
38                 result = result*base + value;
39                 cp++;
40         }
41         if (endp)
42                 *endp = (char *)cp;
43         return result;
44 }
45
46 static long simple_strtol(const char *cp,char **endp,unsigned int base)
47 {
48         if(*cp=='-')
49                 return -simple_strtoul(cp+1,endp,base);
50         return simple_strtoul(cp,endp,base);
51 }
52 #endif
53
54 static int skip_atoi(const char **s)
55 {
56         int i=0;
57
58         while (is_digit(**s))
59                 i = i*10 + *((*s)++) - '0';
60         return i;
61 }
62
63 #define ZEROPAD 1               /* pad with zero */
64 #define SIGN    2               /* unsigned/signed long */
65 #define PLUS    4               /* show plus */
66 #define SPACE   8               /* space if plus */
67 #define LEFT    16              /* left justified */
68 #define SPECIAL 32              /* 0x */
69 #define LARGE   64              /* use 'ABCDEF' instead of 'abcdef' */
70
71 static int number(void (*tx_byte)(unsigned char byte), 
72         unsigned long long num, int base, int size, int precision, 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 ((signed long long)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 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                         if (*fmt == 'l') {
209                                 qualifier = 'L';
210                                 ++fmt;
211                         }
212                 }
213
214                 /* default base */
215                 base = 10;
216
217                 switch (*fmt) {
218                 case 'c':
219                         if (!(flags & LEFT))
220                                 while (--field_width > 0)
221                                         tx_byte(' '), count++;
222                         tx_byte((unsigned char) va_arg(args, int)), count++;
223                         while (--field_width > 0)
224                                 tx_byte(' '), count++;
225                         continue;
226
227                 case 's':
228                         s = va_arg(args, char *);
229                         if (!s)
230                                 s = "<NULL>";
231
232                         len = strnlen(s, precision);
233
234                         if (!(flags & LEFT))
235                                 while (len < field_width--)
236                                         tx_byte(' '), count++;
237                         for (i = 0; i < len; ++i)
238                                 tx_byte(*s++), count++;
239                         while (len < field_width--)
240                                 tx_byte(' '), count++;
241                         continue;
242
243                 case 'p':
244                         if (field_width == -1) {
245                                 field_width = 2*sizeof(void *);
246                                 flags |= ZEROPAD;
247                         }
248                         count += number(tx_byte,
249                                 (unsigned long) va_arg(args, void *), 16,
250                                 field_width, precision, flags);
251                         continue;
252
253
254                 case 'n':
255                         if (qualifier == 'L') {
256                                 long long *ip = va_arg(args, long long *);
257                                 *ip = count;
258                         } else if (qualifier == 'l') {
259                                 long * ip = va_arg(args, long *);
260                                 *ip = count;
261                         } else {
262                                 int * ip = va_arg(args, int *);
263                                 *ip = count;
264                         }
265                         continue;
266
267                 case '%':
268                         tx_byte('%'), count++;
269                         continue;
270
271                 /* integer number formats - set up the flags and "break" */
272                 case 'o':
273                         base = 8;
274                         break;
275
276                 case 'X':
277                         flags |= LARGE;
278                 case 'x':
279                         base = 16;
280                         break;
281
282                 case 'd':
283                 case 'i':
284                         flags |= SIGN;
285                 case 'u':
286                         break;
287
288                 default:
289                         tx_byte('%'), count++;
290                         if (*fmt)
291                                 tx_byte(*fmt), count++;
292                         else
293                                 --fmt;
294                         continue;
295                 }
296                 if (qualifier == 'L') {
297                         num = va_arg(args, unsigned long long);
298                 } else if (qualifier == 'l') {
299                         num = va_arg(args, unsigned long);
300                 } else if (qualifier == 'h') {
301                         num = (unsigned short) va_arg(args, int);
302                         if (flags & SIGN)
303                                 num = (short) num;
304                 } else if (flags & SIGN) {
305                         num = va_arg(args, int);
306                 } else {
307                         num = va_arg(args, unsigned int);
308                 }
309                 count += number(tx_byte, num, base, field_width, precision, flags);
310         }
311         return count;
312 }
313