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