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