printfmt.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* Implementation of printf console output for user environments.
  2. *
  3. * printf is a debugging statement, not a generic output statement.
  4. * It is very important that it always go to the console, especially when
  5. * debugging file descriptor code! */
  6. #include <stdint.h>
  7. #include <stdarg.h>
  8. #include "api.h"
  9. // Print a number (base <= 16) in reverse order,
  10. // using specified fputch function and associated pointer putdat.
  11. #if !defined(__i386__)
  12. static int
  13. printnum(int (*_fputch)(void *, int, void *), void * f, void * putdat,
  14. unsigned long long num, unsigned base, int width, int padc)
  15. #else
  16. static int
  17. printnum(int (*_fputch)(void *, int, void *), void * f, void * putdat,
  18. unsigned long num, unsigned base, int width, int padc)
  19. #endif
  20. {
  21. // first recursively print all preceding (more significant) digits
  22. if (num >= base) {
  23. if (printnum(_fputch, f, putdat, num / base, base, width - 1, padc) == -1)
  24. return -1;
  25. } else {
  26. // print any needed pad characters before first digit
  27. while (--width > 0)
  28. if ((*_fputch) (f, padc, putdat) == -1)
  29. return -1;
  30. }
  31. // then print this (the least significant) digit
  32. if ((*_fputch) (f, "0123456789abcdef"[num % base], putdat) == -1)
  33. return -1;
  34. return 0;
  35. }
  36. // Get an unsigned int of various possible sizes from a varargs list,
  37. // depending on the lflag parameter.
  38. #if !defined(__i386__)
  39. inline unsigned long long
  40. getuint(va_list *ap, int lflag)
  41. #else
  42. inline unsigned long
  43. getuint(va_list *ap, int lflag)
  44. #endif
  45. {
  46. #if !defined(__i386__)
  47. if (lflag >= 2)
  48. return va_arg(*ap, unsigned long long);
  49. else
  50. #endif
  51. if (lflag)
  52. return va_arg(*ap, unsigned long);
  53. else
  54. return va_arg(*ap, unsigned int);
  55. }
  56. // Same as getuint but signed - can't use getuint
  57. // because of sign extension
  58. #if !defined(__i386__)
  59. inline long long
  60. getint(va_list *ap, int lflag)
  61. #else
  62. inline long
  63. getint(va_list *ap, int lflag)
  64. #endif
  65. {
  66. #if !defined(__i386__)
  67. if (lflag >= 2)
  68. return va_arg(*ap, long long);
  69. else
  70. #endif
  71. if (lflag)
  72. return va_arg(*ap, long);
  73. else
  74. return va_arg(*ap, int);
  75. }
  76. // Main function to format and print a string.
  77. void fprintfmt(int (*_fputch)(void *, int, void *), void * f, void * putdat,
  78. const char * fmt, ...);
  79. void
  80. vfprintfmt(int (*_fputch)(void *, int, void *), void * f, void * putdat,
  81. const char * fmt, va_list *ap)
  82. {
  83. register const char *p;
  84. register int ch;
  85. #if !defined(__i386__)
  86. unsigned long long num;
  87. #else
  88. unsigned long num;
  89. #endif
  90. int base, lflag, width, precision, altflag;
  91. char padc;
  92. while (1) {
  93. while ((ch = *(unsigned char *) (fmt++)) != '%') {
  94. if (ch == '\0')
  95. return;
  96. if ((*_fputch) (f, ch, putdat) < 0)
  97. return;
  98. }
  99. // Process a %-escape sequence
  100. padc = ' ';
  101. width = -1;
  102. precision = -1;
  103. lflag = 0;
  104. altflag = 0;
  105. reswitch:
  106. switch (ch = *(unsigned char *) (fmt++)) {
  107. // flag to pad on the right
  108. case '-':
  109. padc = ' ';
  110. goto reswitch;
  111. // flag to pad with 0's instead of spaces
  112. case '0':
  113. padc = '0';
  114. goto reswitch;
  115. // width field
  116. case '1':
  117. case '2':
  118. case '3':
  119. case '4':
  120. case '5':
  121. case '6':
  122. case '7':
  123. case '8':
  124. case '9':
  125. for (precision = 0; ; ++fmt) {
  126. precision = precision * 10 + ch - '0';
  127. ch = *fmt;
  128. if (ch < '0' || ch > '9')
  129. break;
  130. }
  131. goto process_precision;
  132. case '*':
  133. precision = va_arg(*ap, int);
  134. goto process_precision;
  135. case '.':
  136. if (width < 0)
  137. width = 0;
  138. goto reswitch;
  139. case '#':
  140. altflag = 1;
  141. goto reswitch;
  142. process_precision:
  143. if (width < 0)
  144. width = precision, precision = -1;
  145. goto reswitch;
  146. // long flag (doubled for long long)
  147. case 'l':
  148. lflag++;
  149. goto reswitch;
  150. // character
  151. case 'c':
  152. if ((*_fputch) (f, va_arg(*ap, int), putdat) == -1)
  153. return;
  154. break;
  155. // string
  156. case 's':
  157. if ((p = va_arg(*ap, char *)) == NULL)
  158. p = "(null)";
  159. if (width > 0 && padc != '-')
  160. for (width -= strnlen(p, precision); width > 0; width--)
  161. if ((*_fputch) (f, padc, putdat) == -1)
  162. return;
  163. for (; (ch = *p++) != '\0' && (precision < 0 || --precision >= 0); width--)
  164. if (altflag && (ch < ' ' || ch > '~')) {
  165. if ((*_fputch) (f, '?', putdat) == -1)
  166. return;
  167. } else {
  168. if ((*_fputch) (f, ch, putdat) == -1)
  169. return;
  170. }
  171. for (; width > 0; width--)
  172. if ((*_fputch) (f, ' ', putdat) == -1)
  173. return;
  174. break;
  175. // (signed) decimal
  176. case 'd':
  177. case 'i':
  178. num = getint(ap, lflag);
  179. #if !defined(__i386__)
  180. if ((long long) num < 0) {
  181. if ((*_fputch) (f, '-', putdat) == -1)
  182. return;
  183. num = -(long long) num;
  184. }
  185. #else
  186. if ((long) num < 0) {
  187. if ((*_fputch) (f, '-', putdat) == -1)
  188. return;
  189. num = -(long) num;
  190. }
  191. #endif
  192. base = 10;
  193. goto number;
  194. // unsigned decimal
  195. case 'u':
  196. num = getuint(ap, lflag);
  197. base = 10;
  198. goto number;
  199. // (unsigned) octal
  200. case 'o':
  201. // Replace this with your code.
  202. num = getuint(ap, lflag);
  203. base = 8;
  204. goto number;
  205. // pointer
  206. case 'p':
  207. if ((*_fputch) (f, '0', putdat) == -1)
  208. return;
  209. if ((*_fputch) (f, 'x', putdat) == -1)
  210. return;
  211. #if !defined(__i386__)
  212. num = (unsigned long long)
  213. (uintptr_t) va_arg(*ap, void *);
  214. #else
  215. num = (unsigned long)
  216. (uintptr_t) va_arg(*ap, void *);
  217. #endif
  218. base = 16;
  219. goto number;
  220. // (unsigned) hexadecimal
  221. case 'x':
  222. num = getuint(ap, lflag);
  223. base = 16;
  224. number:
  225. if (printnum(_fputch, f, putdat, num, base, width, padc) == -1)
  226. return;
  227. break;
  228. // escape character
  229. case '^':
  230. if ((*_fputch) (f, 0x1b, putdat) == -1)
  231. return;
  232. break;
  233. // escaped '%' character
  234. case '%':
  235. (*_fputch) (f, ch, putdat);
  236. break;
  237. // unrecognized escape sequence - just print it literally
  238. default:
  239. (*_fputch) (f, '%', putdat);
  240. for (fmt--; fmt[-1] != '%'; fmt--)
  241. /* do nothing */;
  242. break;
  243. }
  244. }
  245. }
  246. void
  247. fprintfmt(int (*_fputch)(void *, int, void *), void * f, void * putdat,
  248. const char * fmt, ...)
  249. {
  250. va_list ap;
  251. va_start(ap, fmt);
  252. vfprintfmt(_fputch, f, putdat, fmt, &ap);
  253. va_end(ap);
  254. }
  255. struct sprintbuf {
  256. int cnt, max;
  257. char * buf;
  258. };
  259. static int
  260. sprintputch(void * f, int ch, struct sprintbuf * b)
  261. {
  262. __UNUSED(f);
  263. if (b->cnt >= b->max)
  264. return -1;
  265. b->buf[b->cnt++] = ch;
  266. return 0;
  267. }
  268. static int
  269. vsprintf(char * buf, int n, const char * fmt, va_list *ap)
  270. {
  271. struct sprintbuf b = { 0, n, buf };
  272. if (!buf || n < 1)
  273. return 0;
  274. // print the string to the buffer
  275. vfprintfmt((void *) sprintputch, (void *) 0, &b, fmt, ap);
  276. // null terminate the buffer
  277. if (b.cnt < n)
  278. b.buf[b.cnt] = '\0';
  279. return b.cnt;
  280. }
  281. int
  282. snprintf(char * buf, int n, const char * fmt, ...)
  283. {
  284. va_list ap;
  285. int rc;
  286. va_start(ap, fmt);
  287. rc = vsprintf(buf, n, fmt, &ap);
  288. va_end(ap);
  289. return rc;
  290. }