You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

printf_x.c 7.9KB

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