fprintk.c 10 KB

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