Improve coreboot build output and eliminate some warnings:
[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 #include <console/vtxprintf.h>
11
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'))
16
17 static int skip_atoi(const char **s)
18 {
19         int i=0;
20
21         while (is_digit(**s))
22                 i = i*10 + *((*s)++) - '0';
23         return i;
24 }
25
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' */
33
34 static int number(void (*tx_byte)(unsigned char byte), 
35         unsigned long long num, int base, int size, int precision, int type)
36 {
37         char c,sign,tmp[66];
38         const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
39         int i;
40         int count = 0;
41
42         if (type & LARGE)
43                 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
44         if (type & LEFT)
45                 type &= ~ZEROPAD;
46         if (base < 2 || base > 36)
47                 return 0;
48         c = (type & ZEROPAD) ? '0' : ' ';
49         sign = 0;
50         if (type & SIGN) {
51                 if ((signed long long)num < 0) {
52                         sign = '-';
53                         num = -num;
54                         size--;
55                 } else if (type & PLUS) {
56                         sign = '+';
57                         size--;
58                 } else if (type & SPACE) {
59                         sign = ' ';
60                         size--;
61                 }
62         }
63         if (type & SPECIAL) {
64                 if (base == 16)
65                         size -= 2;
66                 else if (base == 8)
67                         size--;
68         }
69         i = 0;
70         if (num == 0)
71                 tmp[i++]='0';
72         else while (num != 0)
73                 tmp[i++] = digits[do_div(num,base)];
74         if (i > precision)
75                 precision = i;
76         size -= precision;
77         if (!(type&(ZEROPAD+LEFT)))
78                 while(size-->0)
79                         tx_byte(' '), count++;
80         if (sign)
81                 tx_byte(sign), count++;
82         if (type & SPECIAL) {
83                 if (base==8)
84                         tx_byte('0'), count++;
85                 else if (base==16) {
86                         tx_byte('0'), count++;
87                         tx_byte(digits[33]), count++;
88                 }
89         }
90         if (!(type & LEFT))
91                 while (size-- > 0)
92                         tx_byte(c), count++;
93         while (i < precision--)
94                 tx_byte('0'), count++;
95         while (i-- > 0)
96                 tx_byte(tmp[i]), count++;
97         while (size-- > 0)
98                 tx_byte(' '), count++;
99         return count;
100 }
101
102
103 int vtxprintf(void (*tx_byte)(unsigned char byte), const char *fmt, va_list args)
104 {
105         int len;
106         unsigned long long num;
107         int i, base;
108         const char *s;
109
110         int flags;              /* flags to number() */
111
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 */
116         
117         int count;
118
119         for (count=0; *fmt ; ++fmt) {
120                 if (*fmt != '%') {
121                         tx_byte(*fmt), count++;
122                         continue;
123                 }
124                         
125                 /* process flags */
126                 flags = 0;
127                 repeat:
128                         ++fmt;          /* this also skips first '%' */
129                         switch (*fmt) {
130                                 case '-': flags |= LEFT; goto repeat;
131                                 case '+': flags |= PLUS; goto repeat;
132                                 case ' ': flags |= SPACE; goto repeat;
133                                 case '#': flags |= SPECIAL; goto repeat;
134                                 case '0': flags |= ZEROPAD; goto repeat;
135                                 }
136                 
137                 /* get field width */
138                 field_width = -1;
139                 if (is_digit(*fmt))
140                         field_width = skip_atoi(&fmt);
141                 else if (*fmt == '*') {
142                         ++fmt;
143                         /* it's the next argument */
144                         field_width = va_arg(args, int);
145                         if (field_width < 0) {
146                                 field_width = -field_width;
147                                 flags |= LEFT;
148                         }
149                 }
150
151                 /* get the precision */
152                 precision = -1;
153                 if (*fmt == '.') {
154                         ++fmt;  
155                         if (is_digit(*fmt))
156                                 precision = skip_atoi(&fmt);
157                         else if (*fmt == '*') {
158                                 ++fmt;
159                                 /* it's the next argument */
160                                 precision = va_arg(args, int);
161                         }
162                         if (precision < 0)
163                                 precision = 0;
164                 }
165
166                 /* get the conversion qualifier */
167                 qualifier = -1;
168                 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
169                         qualifier = *fmt;
170                         ++fmt;
171                         if (*fmt == 'l') {
172                                 qualifier = 'L';
173                                 ++fmt;
174                         }
175                 }
176
177                 /* default base */
178                 base = 10;
179
180                 switch (*fmt) {
181                 case 'c':
182                         if (!(flags & LEFT))
183                                 while (--field_width > 0)
184                                         tx_byte(' '), count++;
185                         tx_byte((unsigned char) va_arg(args, int)), count++;
186                         while (--field_width > 0)
187                                 tx_byte(' '), count++;
188                         continue;
189
190                 case 's':
191                         s = va_arg(args, char *);
192                         if (!s)
193                                 s = "<NULL>";
194
195                         len = strnlen(s, precision);
196
197                         if (!(flags & LEFT))
198                                 while (len < field_width--)
199                                         tx_byte(' '), count++;
200                         for (i = 0; i < len; ++i)
201                                 tx_byte(*s++), count++;
202                         while (len < field_width--)
203                                 tx_byte(' '), count++;
204                         continue;
205
206                 case 'p':
207                         if (field_width == -1) {
208                                 field_width = 2*sizeof(void *);
209                                 flags |= ZEROPAD;
210                         }
211                         count += number(tx_byte,
212                                 (unsigned long) va_arg(args, void *), 16,
213                                 field_width, precision, flags);
214                         continue;
215
216
217                 case 'n':
218                         if (qualifier == 'L') {
219                                 long long *ip = va_arg(args, long long *);
220                                 *ip = count;
221                         } else if (qualifier == 'l') {
222                                 long * ip = va_arg(args, long *);
223                                 *ip = count;
224                         } else {
225                                 int * ip = va_arg(args, int *);
226                                 *ip = count;
227                         }
228                         continue;
229
230                 case '%':
231                         tx_byte('%'), count++;
232                         continue;
233
234                 /* integer number formats - set up the flags and "break" */
235                 case 'o':
236                         base = 8;
237                         break;
238
239                 case 'X':
240                         flags |= LARGE;
241                 case 'x':
242                         base = 16;
243                         break;
244
245                 case 'd':
246                 case 'i':
247                         flags |= SIGN;
248                 case 'u':
249                         break;
250
251                 default:
252                         tx_byte('%'), count++;
253                         if (*fmt)
254                                 tx_byte(*fmt), count++;
255                         else
256                                 --fmt;
257                         continue;
258                 }
259                 if (qualifier == 'L') {
260                         num = va_arg(args, unsigned long long);
261                 } else if (qualifier == 'l') {
262                         num = va_arg(args, unsigned long);
263                 } else if (qualifier == 'h') {
264                         num = (unsigned short) va_arg(args, int);
265                         if (flags & SIGN)
266                                 num = (short) num;
267                 } else if (flags & SIGN) {
268                         num = va_arg(args, int);
269                 } else {
270                         num = va_arg(args, unsigned int);
271                 }
272                 count += number(tx_byte, num, base, field_width, precision, flags);
273         }
274         return count;
275 }
276