fprintk.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: f_printk.c
  15. * Date: 2022-02-10 14:53:42
  16. * LastEditTime: 2022-02-17 18:01:29
  17. * Description:  This files is for
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. */
  23. #include <stdarg.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include "fkernel.h"
  27. #include "ftypes.h"
  28. #include "fearly_uart.h"
  29. #ifndef __fallthrough
  30. #if __GNUC__ >= 7
  31. #define __fallthrough __attribute__((fallthrough))
  32. #else
  33. #define __fallthrough
  34. #endif /* __GNUC__ >= 7 */
  35. #endif
  36. typedef int (*cbprintf_cb)(int c, void *ctx);
  37. #define CONFIG_CBPRINTF_FULL_INTEGRAL
  38. #ifdef CONFIG_CBPRINTF_FULL_INTEGRAL
  39. typedef intmax_t int_value_type;
  40. typedef uintmax_t uint_value_type;
  41. #define DIGITS_BUFLEN 21
  42. #else
  43. typedef s32 int_value_type;
  44. typedef u32 uint_value_type;
  45. #define DIGITS_BUFLEN 10
  46. #endif
  47. #define ALPHA(fmt) (((fmt)&0x60) - '0' - 10 + 1)
  48. struct str_context
  49. {
  50. char *str;
  51. int max;
  52. int count;
  53. };
  54. static int char_out(int c, void *ctx_p)
  55. {
  56. struct str_context *ctx = ctx_p;
  57. ctx->count++;
  58. OutByte((s8)c);
  59. return 1;
  60. }
  61. /* Convert value to string, storing characters downwards */
  62. static inline int convert_value(uint_value_type num, unsigned int base,
  63. unsigned int alpha, char *buftop)
  64. {
  65. int i = 0;
  66. do
  67. {
  68. unsigned int c = num % base;
  69. if (c >= 10)
  70. {
  71. c += alpha;
  72. }
  73. buftop[--i] = c + '0';
  74. num /= base;
  75. }
  76. while (num);
  77. return -i;
  78. }
  79. #define OUTC(_c) \
  80. do \
  81. { \
  82. out((int)(_c), ctx); \
  83. count++; \
  84. } while (0)
  85. #define PAD_ZERO BIT(0)
  86. #define PAD_TAIL BIT(1)
  87. /**
  88. * @brief Printk internals
  89. *
  90. * See printk() for description.
  91. * @param fmt Format string
  92. * @param ap Variable parameters
  93. *
  94. * @return printed byte count if CONFIG_CBPRINTF_LIBC_SUBSTS is set
  95. */
  96. int cbvprintf(cbprintf_cb out, void *ctx, const char *fmt, va_list ap)
  97. {
  98. size_t count = 0;
  99. char buf[DIGITS_BUFLEN];
  100. char *prefix, *data;
  101. int min_width, precision, data_len;
  102. char padding_mode, length_mod, special;
  103. /* we pre-increment in the loop afterwards */
  104. fmt--;
  105. start:
  106. while (*++fmt != '%')
  107. {
  108. if (*fmt == '\0')
  109. {
  110. return count;
  111. }
  112. OUTC(*fmt);
  113. }
  114. min_width = -1;
  115. precision = -1;
  116. prefix = "";
  117. padding_mode = 0;
  118. length_mod = 0;
  119. special = 0;
  120. for (fmt++;; fmt++)
  121. {
  122. switch (*fmt)
  123. {
  124. case 0:
  125. return count;
  126. case '%':
  127. OUTC('%');
  128. goto start;
  129. case '-':
  130. padding_mode = PAD_TAIL;
  131. continue;
  132. case '.':
  133. precision = 0;
  134. padding_mode &= (char)~PAD_ZERO;
  135. continue;
  136. case '0':
  137. if (min_width < 0 && precision < 0 && !padding_mode)
  138. {
  139. padding_mode = PAD_ZERO;
  140. continue;
  141. }
  142. __fallthrough;
  143. case '1':
  144. case '2':
  145. case '3':
  146. case '4':
  147. case '5':
  148. case '6':
  149. case '7':
  150. case '8':
  151. case '9':
  152. if (precision >= 0)
  153. {
  154. precision = 10 * precision + *fmt - '0';
  155. }
  156. else
  157. {
  158. if (min_width < 0)
  159. {
  160. min_width = 0;
  161. }
  162. min_width = 10 * min_width + *fmt - '0';
  163. }
  164. continue;
  165. case '*':
  166. if (precision >= 0)
  167. {
  168. precision = va_arg(ap, int);
  169. }
  170. else
  171. {
  172. min_width = va_arg(ap, int);
  173. if (min_width < 0)
  174. {
  175. min_width = -min_width;
  176. padding_mode = PAD_TAIL;
  177. }
  178. }
  179. continue;
  180. case '+':
  181. case ' ':
  182. case '#':
  183. special = *fmt;
  184. continue;
  185. case 'h':
  186. case 'l':
  187. case 'z':
  188. if (*fmt == 'h' && length_mod == 'h')
  189. {
  190. length_mod = 'H';
  191. }
  192. else if (*fmt == 'l' && length_mod == 'l')
  193. {
  194. length_mod = 'L';
  195. }
  196. else if (length_mod == '\0')
  197. {
  198. length_mod = *fmt;
  199. }
  200. else
  201. {
  202. OUTC('%');
  203. OUTC(*fmt);
  204. goto start;
  205. }
  206. continue;
  207. case 'd':
  208. case 'i':
  209. case 'u':
  210. {
  211. uint_value_type d;
  212. if (length_mod == 'z')
  213. {
  214. d = va_arg(ap, ssize_t);
  215. }
  216. else if (length_mod == 'l')
  217. {
  218. d = va_arg(ap, long);
  219. }
  220. else if (length_mod == 'L')
  221. {
  222. long long lld = va_arg(ap, long long);
  223. if (sizeof(int_value_type) < 8U &&
  224. lld != (int_value_type)lld)
  225. {
  226. data = "ERR";
  227. data_len = 3;
  228. precision = 0;
  229. break;
  230. }
  231. d = (uint_value_type)lld;
  232. }
  233. else if (*fmt == 'u')
  234. {
  235. d = va_arg(ap, unsigned int);
  236. }
  237. else
  238. {
  239. d = va_arg(ap, int);
  240. }
  241. if (*fmt != 'u' && (int_value_type)d < 0)
  242. {
  243. d = -d;
  244. prefix = "-";
  245. min_width--;
  246. }
  247. else if (special == ' ')
  248. {
  249. prefix = " ";
  250. min_width--;
  251. }
  252. else if (special == '+')
  253. {
  254. prefix = "+";
  255. min_width--;
  256. }
  257. else
  258. {
  259. ;
  260. }
  261. data_len = convert_value(d, 10, 0, buf + sizeof(buf));
  262. data = buf + sizeof(buf) - data_len;
  263. break;
  264. }
  265. case 'p':
  266. case 'x':
  267. case 'X':
  268. {
  269. uint_value_type x;
  270. if (*fmt == 'p')
  271. {
  272. x = (uintptr_t)va_arg(ap, void *);
  273. if (x == (uint_value_type)0)
  274. {
  275. data = "(nil)";
  276. data_len = 5;
  277. precision = 0;
  278. break;
  279. }
  280. special = '#';
  281. }
  282. else if (length_mod == 'l')
  283. {
  284. x = va_arg(ap, unsigned long);
  285. }
  286. else if (length_mod == 'L')
  287. {
  288. x = va_arg(ap, unsigned long long);
  289. }
  290. else
  291. {
  292. x = va_arg(ap, unsigned int);
  293. }
  294. if (special == '#')
  295. {
  296. prefix = (*fmt & 0x20) ? "0x" : "0x";
  297. min_width -= 2;
  298. }
  299. data_len = convert_value(x, 16, ALPHA(*fmt),
  300. buf + sizeof(buf));
  301. data = buf + sizeof(buf) - data_len;
  302. break;
  303. }
  304. case 's':
  305. {
  306. data = va_arg(ap, char *);
  307. data_len = strlen(data);
  308. if (precision >= 0 && data_len > precision)
  309. {
  310. data_len = precision;
  311. }
  312. precision = 0;
  313. break;
  314. }
  315. case 'c':
  316. {
  317. int c = va_arg(ap, int);
  318. buf[0] = c;
  319. data = buf;
  320. data_len = 1;
  321. precision = 0;
  322. break;
  323. }
  324. default:
  325. OUTC('%');
  326. OUTC(*fmt);
  327. goto start;
  328. }
  329. if (precision < 0 && (padding_mode & PAD_ZERO))
  330. {
  331. precision = min_width;
  332. }
  333. min_width -= data_len;
  334. precision -= data_len;
  335. if (precision > 0)
  336. {
  337. min_width -= precision;
  338. }
  339. if (!(padding_mode & PAD_TAIL))
  340. {
  341. while (--min_width >= 0)
  342. {
  343. OUTC(' ');
  344. }
  345. }
  346. while (*prefix)
  347. {
  348. OUTC(*prefix++);
  349. }
  350. while (--precision >= 0)
  351. {
  352. OUTC('0');
  353. }
  354. while (--data_len >= 0)
  355. {
  356. OUTC(*data++);
  357. }
  358. while (--min_width >= 0)
  359. {
  360. OUTC(' ');
  361. }
  362. goto start;
  363. }
  364. }
  365. static int f_vprintf(const char *restrict format, va_list vargs)
  366. {
  367. struct str_context ctx = {0};
  368. cbvprintf(char_out, &ctx, format, vargs);
  369. }
  370. void f_printk(const char *fmt, ...)
  371. {
  372. va_list ap;
  373. va_start(ap, fmt);
  374. f_vprintf(fmt, ap);
  375. va_end(ap);
  376. }