]>
Commit | Line | Data |
---|---|---|
153d511e WD |
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> | |
8bde7f77 WD |
20 | /*cmd_boot.c*/ |
21 | extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); | |
153d511e WD |
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 | ||
6d0f6bcf | 58 | #ifdef CONFIG_SYS_64BIT_STRTOUL |
c40b2956 WD |
59 | unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base) |
60 | { | |
61 | unsigned long long result = 0, value; | |
62 | ||
63 | if (*cp == '0') { | |
64 | cp++; | |
65 | if ((*cp == 'x') && isxdigit (cp[1])) { | |
66 | base = 16; | |
67 | cp++; | |
68 | } | |
69 | if (!base) { | |
70 | base = 8; | |
71 | } | |
72 | } | |
73 | if (!base) { | |
74 | base = 10; | |
75 | } | |
76 | while (isxdigit (*cp) && (value = isdigit (*cp) | |
77 | ? *cp - '0' | |
78 | : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) { | |
79 | result = result * base + value; | |
80 | cp++; | |
81 | } | |
82 | if (endp) | |
83 | *endp = (char *) cp; | |
84 | return result; | |
85 | } | |
6d0f6bcf | 86 | #endif /* CONFIG_SYS_64BIT_STRTOUL */ |
c40b2956 | 87 | |
153d511e WD |
88 | /* we use this so that we can do without the ctype library */ |
89 | #define is_digit(c) ((c) >= '0' && (c) <= '9') | |
90 | ||
91 | static int skip_atoi(const char **s) | |
92 | { | |
93 | int i=0; | |
94 | ||
95 | while (is_digit(**s)) | |
96 | i = i*10 + *((*s)++) - '0'; | |
97 | return i; | |
98 | } | |
99 | ||
100 | #define ZEROPAD 1 /* pad with zero */ | |
101 | #define SIGN 2 /* unsigned/signed long */ | |
102 | #define PLUS 4 /* show plus */ | |
103 | #define SPACE 8 /* space if plus */ | |
104 | #define LEFT 16 /* left justified */ | |
105 | #define SPECIAL 32 /* 0x */ | |
106 | #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */ | |
107 | ||
6d0f6bcf | 108 | #ifdef CONFIG_SYS_64BIT_VSPRINTF |
bf052939 JY |
109 | #define do_div(n,base) ({ \ |
110 | unsigned int __res; \ | |
111 | __res = ((unsigned long long) n) % base; \ | |
112 | n = ((unsigned long long) n) / base; \ | |
113 | __res; \ | |
114 | }) | |
115 | #else | |
153d511e | 116 | #define do_div(n,base) ({ \ |
c40b2956 | 117 | int __res; \ |
bf052939 JY |
118 | __res = ((unsigned long) n) % base; \ |
119 | n = ((unsigned long) n) / base; \ | |
c40b2956 WD |
120 | __res; \ |
121 | }) | |
bf052939 | 122 | #endif |
c40b2956 | 123 | |
6d0f6bcf | 124 | #ifdef CONFIG_SYS_64BIT_VSPRINTF |
bf052939 | 125 | static char * number(char * str, long long num, unsigned int base, int size, int precision ,int type) |
c40b2956 | 126 | #else |
bf052939 | 127 | static char * number(char * str, long num, unsigned int base, int size, int precision ,int type) |
c40b2956 | 128 | #endif |
153d511e WD |
129 | { |
130 | char c,sign,tmp[66]; | |
131 | const char *digits="0123456789abcdefghijklmnopqrstuvwxyz"; | |
132 | int i; | |
133 | ||
134 | if (type & LARGE) | |
135 | digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
136 | if (type & LEFT) | |
137 | type &= ~ZEROPAD; | |
138 | if (base < 2 || base > 36) | |
139 | return 0; | |
140 | c = (type & ZEROPAD) ? '0' : ' '; | |
141 | sign = 0; | |
142 | if (type & SIGN) { | |
143 | if (num < 0) { | |
144 | sign = '-'; | |
145 | num = -num; | |
146 | size--; | |
147 | } else if (type & PLUS) { | |
148 | sign = '+'; | |
149 | size--; | |
150 | } else if (type & SPACE) { | |
151 | sign = ' '; | |
152 | size--; | |
153 | } | |
154 | } | |
155 | if (type & SPECIAL) { | |
156 | if (base == 16) | |
157 | size -= 2; | |
158 | else if (base == 8) | |
159 | size--; | |
160 | } | |
161 | i = 0; | |
162 | if (num == 0) | |
163 | tmp[i++]='0'; | |
164 | else while (num != 0) | |
165 | tmp[i++] = digits[do_div(num,base)]; | |
166 | if (i > precision) | |
167 | precision = i; | |
168 | size -= precision; | |
169 | if (!(type&(ZEROPAD+LEFT))) | |
170 | while(size-->0) | |
171 | *str++ = ' '; | |
172 | if (sign) | |
173 | *str++ = sign; | |
174 | if (type & SPECIAL) { | |
175 | if (base==8) | |
176 | *str++ = '0'; | |
177 | else if (base==16) { | |
178 | *str++ = '0'; | |
179 | *str++ = digits[33]; | |
180 | } | |
181 | } | |
182 | if (!(type & LEFT)) | |
183 | while (size-- > 0) | |
184 | *str++ = c; | |
185 | while (i < precision--) | |
186 | *str++ = '0'; | |
187 | while (i-- > 0) | |
188 | *str++ = tmp[i]; | |
189 | while (size-- > 0) | |
190 | *str++ = ' '; | |
191 | return str; | |
192 | } | |
193 | ||
194 | /* Forward decl. needed for IP address printing stuff... */ | |
195 | int sprintf(char * buf, const char *fmt, ...); | |
196 | ||
197 | int vsprintf(char *buf, const char *fmt, va_list args) | |
198 | { | |
199 | int len; | |
6d0f6bcf | 200 | #ifdef CONFIG_SYS_64BIT_VSPRINTF |
c40b2956 WD |
201 | unsigned long long num; |
202 | #else | |
153d511e | 203 | unsigned long num; |
c40b2956 | 204 | #endif |
153d511e WD |
205 | int i, base; |
206 | char * str; | |
207 | const char *s; | |
208 | ||
209 | int flags; /* flags to number() */ | |
210 | ||
211 | int field_width; /* width of output field */ | |
212 | int precision; /* min. # of digits for integers; max | |
213 | number of chars for from string */ | |
c40b2956 | 214 | int qualifier; /* 'h', 'l', or 'q' for integer fields */ |
153d511e WD |
215 | |
216 | for (str=buf ; *fmt ; ++fmt) { | |
217 | if (*fmt != '%') { | |
218 | *str++ = *fmt; | |
219 | continue; | |
220 | } | |
221 | ||
222 | /* process flags */ | |
223 | flags = 0; | |
224 | repeat: | |
225 | ++fmt; /* this also skips first '%' */ | |
226 | switch (*fmt) { | |
227 | case '-': flags |= LEFT; goto repeat; | |
228 | case '+': flags |= PLUS; goto repeat; | |
229 | case ' ': flags |= SPACE; goto repeat; | |
230 | case '#': flags |= SPECIAL; goto repeat; | |
231 | case '0': flags |= ZEROPAD; goto repeat; | |
232 | } | |
233 | ||
234 | /* get field width */ | |
235 | field_width = -1; | |
236 | if (is_digit(*fmt)) | |
237 | field_width = skip_atoi(&fmt); | |
238 | else if (*fmt == '*') { | |
239 | ++fmt; | |
240 | /* it's the next argument */ | |
241 | field_width = va_arg(args, int); | |
242 | if (field_width < 0) { | |
243 | field_width = -field_width; | |
244 | flags |= LEFT; | |
245 | } | |
246 | } | |
247 | ||
248 | /* get the precision */ | |
249 | precision = -1; | |
250 | if (*fmt == '.') { | |
251 | ++fmt; | |
252 | if (is_digit(*fmt)) | |
253 | precision = skip_atoi(&fmt); | |
254 | else if (*fmt == '*') { | |
255 | ++fmt; | |
256 | /* it's the next argument */ | |
257 | precision = va_arg(args, int); | |
258 | } | |
259 | if (precision < 0) | |
260 | precision = 0; | |
261 | } | |
262 | ||
263 | /* get the conversion qualifier */ | |
264 | qualifier = -1; | |
f354b73e JCPV |
265 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || |
266 | *fmt == 'Z' || *fmt == 'z' || *fmt == 't' || | |
267 | *fmt == 'q' ) { | |
153d511e | 268 | qualifier = *fmt; |
bf052939 JY |
269 | if (qualifier == 'l' && *(fmt+1) == 'l') { |
270 | qualifier = 'q'; | |
271 | ++fmt; | |
272 | } | |
153d511e WD |
273 | ++fmt; |
274 | } | |
275 | ||
276 | /* default base */ | |
277 | base = 10; | |
278 | ||
279 | switch (*fmt) { | |
280 | case 'c': | |
281 | if (!(flags & LEFT)) | |
282 | while (--field_width > 0) | |
283 | *str++ = ' '; | |
284 | *str++ = (unsigned char) va_arg(args, int); | |
285 | while (--field_width > 0) | |
286 | *str++ = ' '; | |
287 | continue; | |
288 | ||
289 | case 's': | |
290 | s = va_arg(args, char *); | |
291 | if (!s) | |
292 | s = "<NULL>"; | |
293 | ||
294 | len = strnlen(s, precision); | |
295 | ||
296 | if (!(flags & LEFT)) | |
297 | while (len < field_width--) | |
298 | *str++ = ' '; | |
299 | for (i = 0; i < len; ++i) | |
300 | *str++ = *s++; | |
301 | while (len < field_width--) | |
302 | *str++ = ' '; | |
303 | continue; | |
304 | ||
305 | case 'p': | |
306 | if (field_width == -1) { | |
307 | field_width = 2*sizeof(void *); | |
308 | flags |= ZEROPAD; | |
309 | } | |
310 | str = number(str, | |
311 | (unsigned long) va_arg(args, void *), 16, | |
312 | field_width, precision, flags); | |
313 | continue; | |
314 | ||
315 | ||
316 | case 'n': | |
317 | if (qualifier == 'l') { | |
318 | long * ip = va_arg(args, long *); | |
319 | *ip = (str - buf); | |
320 | } else { | |
321 | int * ip = va_arg(args, int *); | |
322 | *ip = (str - buf); | |
323 | } | |
324 | continue; | |
325 | ||
326 | case '%': | |
327 | *str++ = '%'; | |
328 | continue; | |
329 | ||
330 | /* integer number formats - set up the flags and "break" */ | |
331 | case 'o': | |
332 | base = 8; | |
333 | break; | |
334 | ||
335 | case 'X': | |
336 | flags |= LARGE; | |
337 | case 'x': | |
338 | base = 16; | |
339 | break; | |
340 | ||
341 | case 'd': | |
342 | case 'i': | |
343 | flags |= SIGN; | |
344 | case 'u': | |
345 | break; | |
346 | ||
347 | default: | |
348 | *str++ = '%'; | |
349 | if (*fmt) | |
350 | *str++ = *fmt; | |
351 | else | |
352 | --fmt; | |
353 | continue; | |
354 | } | |
6d0f6bcf | 355 | #ifdef CONFIG_SYS_64BIT_VSPRINTF |
c40b2956 WD |
356 | if (qualifier == 'q') /* "quad" for 64 bit variables */ |
357 | num = va_arg(args, unsigned long long); | |
358 | else | |
359 | #endif | |
f354b73e | 360 | if (qualifier == 'l') { |
153d511e | 361 | num = va_arg(args, unsigned long); |
f354b73e JCPV |
362 | } else if (qualifier == 'Z' || qualifier == 'z') { |
363 | num = va_arg(args, size_t); | |
364 | } else if (qualifier == 't') { | |
365 | num = va_arg(args, ptrdiff_t); | |
366 | } else if (qualifier == 'h') { | |
153d511e WD |
367 | num = (unsigned short) va_arg(args, int); |
368 | if (flags & SIGN) | |
369 | num = (short) num; | |
370 | } else if (flags & SIGN) | |
371 | num = va_arg(args, int); | |
372 | else | |
373 | num = va_arg(args, unsigned int); | |
374 | str = number(str, num, base, field_width, precision, flags); | |
375 | } | |
376 | *str = '\0'; | |
377 | return str-buf; | |
378 | } | |
379 | ||
380 | int sprintf(char * buf, const char *fmt, ...) | |
381 | { | |
382 | va_list args; | |
383 | int i; | |
384 | ||
385 | va_start(args, fmt); | |
386 | i=vsprintf(buf,fmt,args); | |
387 | va_end(args); | |
388 | return i; | |
389 | } | |
390 | ||
391 | void panic(const char *fmt, ...) | |
392 | { | |
393 | va_list args; | |
394 | va_start(args, fmt); | |
6dd652fa | 395 | vprintf(fmt, args); |
153d511e WD |
396 | putc('\n'); |
397 | va_end(args); | |
398 | #if defined (CONFIG_PANIC_HANG) | |
399 | hang(); | |
400 | #else | |
401 | udelay (100000); /* allow messages to go out */ | |
402 | do_reset (NULL, 0, 0, NULL); | |
403 | #endif | |
404 | } |