printfmt.c 8.5 KB

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