libc_port.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  1. /*
  2. * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /******************************************************************************
  17. * @file libc_port.c
  18. * @brief libc port
  19. * @version V1.0
  20. * @date 26. Dec 2017
  21. ******************************************************************************/
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <stdarg.h>
  27. #include <stdlib.h>
  28. #include <limits.h>
  29. #include "stddef.h"
  30. #include <csi_config.h>
  31. #include "wm_regs.h"
  32. static unsigned char printf_port = 0;
  33. //0 uart0; 1: uart1; other: close;
  34. unsigned char get_printf_port(void)
  35. {
  36. return printf_port;
  37. }
  38. void set_printf_port(unsigned char port)
  39. {
  40. if(port == 0)
  41. {
  42. printf_port = 0;
  43. }
  44. else if(port == 1)
  45. {
  46. printf_port = 1;
  47. }
  48. else
  49. {
  50. printf_port = 0xff;
  51. }
  52. }
  53. int sendchar(int ch)
  54. {
  55. //tls_reg_write32(HR_UART0_INT_MASK, 0x3);
  56. if (printf_port == 0)
  57. {
  58. if(ch == '\n')
  59. {
  60. while (tls_reg_read32(HR_UART0_FIFO_STATUS) & 0x3F);
  61. tls_reg_write32(HR_UART0_TX_WIN, '\r');
  62. }
  63. while(tls_reg_read32(HR_UART0_FIFO_STATUS) & 0x3F);
  64. tls_reg_write32(HR_UART0_TX_WIN, (char)ch);
  65. }
  66. else if (printf_port == 1)
  67. {
  68. if(ch == '\n')
  69. {
  70. while (tls_reg_read32(HR_UART1_FIFO_STATUS) & 0x3F);
  71. tls_reg_write32(HR_UART1_TX_WIN, '\r');
  72. }
  73. while(tls_reg_read32(HR_UART1_FIFO_STATUS) & 0x3F);
  74. tls_reg_write32(HR_UART1_TX_WIN, (char)ch);
  75. }
  76. //tls_reg_write32(HR_UART0_INT_MASK, 0x0);
  77. return ch;
  78. }
  79. int fputc(int ch, FILE *stream)
  80. {
  81. (void)stream;
  82. sendchar(ch);
  83. return 0;
  84. }
  85. #if 1//defined(__MINILIBC__)
  86. int fgetc(FILE *stream)
  87. {
  88. (void)stream;
  89. return 0;
  90. }
  91. #endif
  92. #if 1//defined(_NEWLIB_VERSION_H__)
  93. //_ssize_t _write_r (struct _reent *, int, const void *, size_t) /* ignore warning */
  94. int _write_r(void *r, int file, const void *ptr, size_t len)
  95. {
  96. size_t i;
  97. char *p;
  98. p = (char*) ptr;
  99. for (i = 0; i < len; i++)
  100. {
  101. (void)fputc(*p++, r); /* r: ignore warning */
  102. }
  103. return len;
  104. }
  105. #endif
  106. static int __ip2str(unsigned char v4v6, unsigned int *inuint, char *outtxt)
  107. {
  108. unsigned char i;
  109. unsigned char j = 0;
  110. unsigned char k;
  111. unsigned char h;
  112. unsigned char m;
  113. unsigned char l;
  114. unsigned char bit;
  115. if (4 == v4v6)
  116. {
  117. for(i = 0; i < 4; i++)
  118. {
  119. bit = (*inuint >> (8 * i)) & 0xff;
  120. h = bit / 100;
  121. if (h)
  122. outtxt[j++] = '0' + h;
  123. m = (bit % 100) / 10;
  124. if (m)
  125. {
  126. outtxt[j++] = '0' + m;
  127. }
  128. else
  129. {
  130. if (h)
  131. outtxt[j++] = '0';
  132. }
  133. l = (bit % 100) % 10;
  134. outtxt[j++] = '0' + l;
  135. outtxt[j++] = '.';
  136. }
  137. }
  138. else
  139. {
  140. for (k = 0; k < 4; k++)
  141. {
  142. for(i = 0; i < 4; i++)
  143. {
  144. m = (*inuint >> (8 * i)) & 0xff;
  145. h = m >> 4;
  146. l = m & 0xf;
  147. if (h > 9)
  148. outtxt[j++] = 'A' + h - 10;
  149. else
  150. outtxt[j++]= '0' + h;
  151. if (l > 9)
  152. outtxt[j++] = 'A' + l - 10;
  153. else
  154. outtxt[j++] = '0' + l;
  155. if (0 != (i % 2))
  156. outtxt[j++] = ':';
  157. }
  158. inuint++;
  159. }
  160. }
  161. outtxt[j - 1] = 0;
  162. return j - 1;
  163. }
  164. static int __mac2str(unsigned char *inchar, char *outtxt)
  165. {
  166. unsigned char hbit,lbit;
  167. unsigned int i;
  168. for(i = 0; i < 6; i++)/* mac length */
  169. {
  170. hbit = (*(inchar + i) & 0xf0) >> 4;
  171. lbit = *(inchar + i ) & 0x0f;
  172. if (hbit > 9)
  173. outtxt[3 * i] = 'A' + hbit - 10;
  174. else
  175. outtxt[3 * i]= '0' + hbit;
  176. if (lbit > 9)
  177. outtxt[3 * i + 1] = 'A' + lbit - 10;
  178. else
  179. outtxt[3 * i + 1] = '0' + lbit;
  180. outtxt[3 * i + 2] = '-';
  181. }
  182. outtxt[3 * (i - 1) + 2] = 0;
  183. return 3 * (i - 1) + 2;
  184. }
  185. static inline int digitval(int ch)
  186. {
  187. if ( ch >= '0' && ch <= '9' ) {
  188. return ch-'0';
  189. } else if ( ch >= 'A' && ch <= 'Z' ) {
  190. return ch-'A'+10;
  191. } else if ( ch >= 'a' && ch <= 'z' ) {
  192. return ch-'a'+10;
  193. } else {
  194. return -1;
  195. }
  196. }
  197. uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n)
  198. {
  199. int minus = 0;
  200. uintmax_t v = 0;
  201. int d;
  202. while ( n && isspace((unsigned char)*nptr) ) {
  203. nptr++;
  204. n--;
  205. }
  206. /* Single optional + or - */
  207. if ( n && *nptr == '-' ) {
  208. minus = 1;
  209. nptr++;
  210. n--;
  211. } else if ( n && *nptr == '+' ) {
  212. nptr++;
  213. }
  214. if ( base == 0 ) {
  215. if ( n >= 2 && nptr[0] == '0' &&
  216. (nptr[1] == 'x' || nptr[1] == 'X') ) {
  217. n -= 2;
  218. nptr += 2;
  219. base = 16;
  220. } else if ( n >= 1 && nptr[0] == '0' ) {
  221. n--;
  222. nptr++;
  223. base = 8;
  224. } else {
  225. base = 10;
  226. }
  227. } else if ( base == 16 ) {
  228. if ( n >= 2 && nptr[0] == '0' &&
  229. (nptr[1] == 'x' || nptr[1] == 'X') ) {
  230. n -= 2;
  231. nptr += 2;
  232. }
  233. }
  234. while ( n && (d = digitval(*nptr)) >= 0 && d < base ) {
  235. v = v*base + d;
  236. n--;
  237. nptr++;
  238. }
  239. if ( endptr )
  240. *endptr = (char *)nptr;
  241. return minus ? -v : v;
  242. }
  243. #ifndef LONG_BIT
  244. #define LONG_BIT (CHAR_BIT*sizeof(long))
  245. #endif
  246. enum flags {
  247. FL_SPLAT = 0x01, /* Drop the value, do not assign */
  248. FL_INV = 0x02, /* Character-set with inverse */
  249. FL_WIDTH = 0x04, /* Field width specified */
  250. FL_MINUS = 0x08, /* Negative number */
  251. };
  252. enum ranks {
  253. rank_char = -2,
  254. rank_short = -1,
  255. rank_int = 0,
  256. rank_long = 1,
  257. rank_longlong = 2,
  258. rank_ptr = INT_MAX /* Special value used for pointers */
  259. };
  260. #define MIN_RANK rank_char
  261. #define MAX_RANK rank_longlong
  262. #define INTMAX_RANK rank_longlong
  263. #define SIZE_T_RANK rank_long
  264. #define PTRDIFF_T_RANK rank_long
  265. enum bail {
  266. bail_none = 0, /* No error condition */
  267. bail_eof, /* Hit EOF */
  268. bail_err /* Conversion mismatch */
  269. };
  270. static inline const char *
  271. skipspace(const char *p)
  272. {
  273. while ( isspace((unsigned char)*p) ) p++;
  274. return p;
  275. }
  276. #undef set_bit
  277. static inline void
  278. set_bit(unsigned long *bitmap, unsigned int bit)
  279. {
  280. bitmap[bit/LONG_BIT] |= 1UL << (bit%LONG_BIT);
  281. }
  282. #undef test_bit
  283. static inline int
  284. test_bit(unsigned long *bitmap, unsigned int bit)
  285. {
  286. return (int)(bitmap[bit/LONG_BIT] >> (bit%LONG_BIT)) & 1;
  287. }
  288. int wm_vsscanf(const char *buffer, const char *format, va_list ap)
  289. {
  290. const char *p = format;
  291. char ch;
  292. const char *q = buffer;
  293. const char *qq;
  294. uintmax_t val = 0;
  295. int rank = rank_int; /* Default rank */
  296. unsigned int width = UINT_MAX;
  297. int base;
  298. enum flags flags = 0;
  299. enum {
  300. st_normal, /* Ground state */
  301. st_flags, /* Special flags */
  302. st_width, /* Field width */
  303. st_modifiers, /* Length or conversion modifiers */
  304. st_match_init, /* Initial state of %[ sequence */
  305. st_match, /* Main state of %[ sequence */
  306. st_match_range, /* After - in a %[ sequence */
  307. } state = st_normal;
  308. char *sarg = NULL; /* %s %c or %[ string argument */
  309. enum bail bail = bail_none;
  310. int sign = 0;
  311. int converted = 0; /* Successful conversions */
  312. unsigned long matchmap[((1 << CHAR_BIT)+(LONG_BIT-1))/LONG_BIT];
  313. int matchinv = 0; /* Is match map inverted? */
  314. unsigned char range_start = 0;
  315. sign = sign; /* gcc warning */
  316. while ( (ch = *p++) && !bail ) {
  317. switch ( state ) {
  318. case st_normal:
  319. if ( ch == '%' ) {
  320. state = st_flags;
  321. flags = 0; rank = rank_int; width = UINT_MAX;
  322. } else if ( isspace((unsigned char)ch) ) {
  323. q = skipspace(q);
  324. } else {
  325. if ( *q == ch )
  326. q++;
  327. else
  328. bail = bail_err; /* Match failure */
  329. }
  330. break;
  331. case st_flags:
  332. switch ( ch ) {
  333. case '*':
  334. flags |= FL_SPLAT;
  335. break;
  336. case '0' ... '9':
  337. width = (ch-'0');
  338. state = st_width;
  339. flags |= FL_WIDTH;
  340. break;
  341. default:
  342. state = st_modifiers;
  343. p--; /* Process this character again */
  344. break;
  345. }
  346. break;
  347. case st_width:
  348. if ( ch >= '0' && ch <= '9' ) {
  349. width = width*10+(ch-'0');
  350. } else {
  351. state = st_modifiers;
  352. p--; /* Process this character again */
  353. }
  354. break;
  355. case st_modifiers:
  356. switch ( ch ) {
  357. /* Length modifiers - nonterminal sequences */
  358. case 'h':
  359. rank--; /* Shorter rank */
  360. break;
  361. case 'l':
  362. rank++; /* Longer rank */
  363. break;
  364. case 'j':
  365. rank = INTMAX_RANK;
  366. break;
  367. case 'z':
  368. rank = SIZE_T_RANK;
  369. break;
  370. case 't':
  371. rank = PTRDIFF_T_RANK;
  372. break;
  373. case 'L':
  374. case 'q':
  375. rank = rank_longlong; /* long double/long long */
  376. break;
  377. default:
  378. /* Output modifiers - terminal sequences */
  379. state = st_normal; /* Next state will be normal */
  380. if ( rank < MIN_RANK ) /* Canonicalize rank */
  381. rank = MIN_RANK;
  382. else if ( rank > MAX_RANK )
  383. rank = MAX_RANK;
  384. switch ( ch ) {
  385. case 'P': /* Upper case pointer */
  386. case 'p': /* Pointer */
  387. #if 0 /* Enable this to allow null pointers by name */
  388. q = skipspace(q);
  389. if ( !isdigit((unsigned char)*q) ) {
  390. static const char * const nullnames[] =
  391. { "null", "nul", "nil", "(null)", "(nul)", "(nil)", 0 };
  392. const char * const *np;
  393. /* Check to see if it's a null pointer by name */
  394. for ( np = nullnames ; *np ; np++ ) {
  395. if ( !strncasecmp(q, *np, strlen(*np)) ) {
  396. val = (uintmax_t)((void *)NULL);
  397. goto set_integer;
  398. }
  399. }
  400. /* Failure */
  401. bail = bail_err;
  402. break;
  403. }
  404. /* else */
  405. #endif
  406. rank = rank_ptr;
  407. base = 0; sign = 0;
  408. goto scan_int;
  409. case 'i': /* Base-independent integer */
  410. base = 0; sign = 1;
  411. goto scan_int;
  412. case 'd': /* Decimal integer */
  413. base = 10; sign = 1;
  414. goto scan_int;
  415. case 'o': /* Octal integer */
  416. base = 8; sign = 0;
  417. goto scan_int;
  418. case 'u': /* Unsigned decimal integer */
  419. base = 10; sign = 0;
  420. goto scan_int;
  421. case 'x': /* Hexadecimal integer */
  422. case 'X':
  423. base = 16; sign = 0;
  424. goto scan_int;
  425. case 'n': /* Number of characters consumed */
  426. val = (q-buffer);
  427. goto set_integer;
  428. scan_int:
  429. q = skipspace(q);
  430. if ( !*q ) {
  431. bail = bail_eof;
  432. break;
  433. }
  434. val = strntoumax(q, (char **)&qq, base, width);
  435. if ( qq == q ) {
  436. bail = bail_err;
  437. break;
  438. }
  439. q = qq;
  440. converted++;
  441. /* fall through */
  442. set_integer:
  443. if ( !(flags & FL_SPLAT) ) {
  444. switch(rank) {
  445. case rank_char:
  446. *va_arg(ap, unsigned char *) = (unsigned char)val;
  447. break;
  448. case rank_short:
  449. *va_arg(ap, unsigned short *) = (unsigned short)val;
  450. break;
  451. case rank_int:
  452. *va_arg(ap, unsigned int *) = (unsigned int)val;
  453. break;
  454. case rank_long:
  455. *va_arg(ap, unsigned long *) = (unsigned long)val;
  456. break;
  457. case rank_longlong:
  458. *va_arg(ap, unsigned long long *) = (unsigned long long)val;
  459. break;
  460. case rank_ptr:
  461. *va_arg(ap, void **) = (void *)(uintptr_t)val;
  462. break;
  463. }
  464. }
  465. break;
  466. case 'c': /* Character */
  467. width = (flags & FL_WIDTH) ? width : 1; /* Default width == 1 */
  468. sarg = va_arg(ap, char *);
  469. while ( width-- ) {
  470. if ( !*q ) {
  471. bail = bail_eof;
  472. break;
  473. }
  474. *sarg++ = *q++;
  475. }
  476. if ( !bail )
  477. converted++;
  478. break;
  479. case 's': /* String */
  480. {
  481. char *sp;
  482. sp = sarg = va_arg(ap, char *);
  483. while ( width-- && *q && !isspace((unsigned char)*q) ) {
  484. *sp++ = *q++;
  485. }
  486. if ( sarg != sp ) {
  487. *sp = '\0'; /* Terminate output */
  488. converted++;
  489. } else {
  490. bail = bail_eof;
  491. }
  492. }
  493. break;
  494. case 'f': /* float */
  495. {
  496. float *vp = (float *) va_arg(ap, float *);
  497. const char *vpq = q;
  498. *vp = strtof(vpq, (char **)&q);
  499. if ( vpq != q ) {
  500. converted++;
  501. } else {
  502. //bail = bail_eof;
  503. bail = bail_err;
  504. }
  505. }
  506. break;
  507. case 'g': /* double */
  508. {
  509. double *vp = (double *) va_arg(ap, double *);
  510. const char *vpq = q;
  511. *vp = strtod(vpq, (char **)&q);
  512. if ( vpq != q ) {
  513. converted++;
  514. } else {
  515. //bail = bail_eof;
  516. bail = bail_err;
  517. }
  518. }
  519. break;
  520. case '[': /* Character range */
  521. sarg = va_arg(ap, char *);
  522. state = st_match_init;
  523. matchinv = 0;
  524. memset(matchmap, 0, sizeof matchmap);
  525. break;
  526. case '%': /* %% sequence */
  527. if ( *q == '%' )
  528. q++;
  529. else
  530. bail = bail_err;
  531. break;
  532. default: /* Anything else */
  533. bail = bail_err; /* Unknown sequence */
  534. break;
  535. }
  536. }
  537. break;
  538. case st_match_init: /* Initial state for %[ match */
  539. if ( ch == '^' && !(flags & FL_INV) ) {
  540. matchinv = 1;
  541. } else {
  542. set_bit(matchmap, (unsigned char)ch);
  543. state = st_match;
  544. }
  545. break;
  546. case st_match: /* Main state for %[ match */
  547. if ( ch == ']' ) {
  548. goto match_run;
  549. } else if ( ch == '-' ) {
  550. range_start = (unsigned char)ch;
  551. state = st_match_range;
  552. } else {
  553. set_bit(matchmap, (unsigned char)ch);
  554. }
  555. break;
  556. case st_match_range: /* %[ match after - */
  557. if ( ch == ']' ) {
  558. set_bit(matchmap, (unsigned char)'-'); /* - was last character */
  559. goto match_run;
  560. } else {
  561. int i;
  562. for ( i = range_start ; i < (unsigned char)ch ; i++ )
  563. set_bit(matchmap, i);
  564. state = st_match;
  565. }
  566. break;
  567. match_run: /* Match expression finished */
  568. qq = q;
  569. while ( width && *q && test_bit(matchmap, (unsigned char)*q)^matchinv ) {
  570. *sarg++ = *q++;
  571. }
  572. if ( q != qq ) {
  573. *sarg = '\0';
  574. converted++;
  575. } else {
  576. bail = *q ? bail_err : bail_eof;
  577. }
  578. break;
  579. }
  580. }
  581. if ( bail == bail_eof && !converted )
  582. converted = -1; /* Return EOF (-1) */
  583. return converted;
  584. }
  585. #define PRINTF_NTOA_BUFFER_SIZE 32U
  586. #define PRINTF_FTOA_BUFFER_SIZE 32U
  587. #define PRINTF_SUPPORT_FLOAT
  588. #define PRINTF_SUPPORT_EXPONENTIAL
  589. #define PRINTF_DEFAULT_FLOAT_PRECISION 6U
  590. #define PRINTF_MAX_FLOAT 1e9
  591. #define PRINTF_SUPPORT_LONG_LONG
  592. #define PRINTF_SUPPORT_PTRDIFF_T
  593. // import float.h for DBL_MAX
  594. #if defined(PRINTF_SUPPORT_FLOAT)
  595. #include <float.h>
  596. #endif
  597. // internal flag definitions
  598. #define FLAGS_ZEROPAD (1U << 0U)
  599. #define FLAGS_LEFT (1U << 1U)
  600. #define FLAGS_PLUS (1U << 2U)
  601. #define FLAGS_SPACE (1U << 3U)
  602. #define FLAGS_HASH (1U << 4U)
  603. #define FLAGS_UPPERCASE (1U << 5U)
  604. #define FLAGS_CHAR (1U << 6U)
  605. #define FLAGS_SHORT (1U << 7U)
  606. #define FLAGS_LONG (1U << 8U)
  607. #define FLAGS_LONG_LONG (1U << 9U)
  608. #define FLAGS_PRECISION (1U << 10U)
  609. #define FLAGS_ADAPT_EXP (1U << 11U)
  610. // output function type
  611. typedef void (*out_fct_type)(char character, void* buffer, size_t idx, size_t maxlen);
  612. // internal buffer output
  613. static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen)
  614. {
  615. if (idx < maxlen) {
  616. ((char*)buffer)[idx] = character;
  617. }
  618. }
  619. static inline void _out_uart(char character, void* buffer, size_t idx, size_t maxlen)
  620. {
  621. _write_r(NULL, 0, &character, 1);
  622. }
  623. // internal null output
  624. static inline void _out_null(char character, void* buffer, size_t idx, size_t maxlen)
  625. {
  626. (void)character; (void)buffer; (void)idx; (void)maxlen;
  627. }
  628. // internal secure strlen
  629. // \return The length of the string (excluding the terminating 0) limited by 'maxsize'
  630. static inline unsigned int _strnlen_s(const char* str, size_t maxsize)
  631. {
  632. const char* s;
  633. for (s = str; *s && maxsize--; ++s);
  634. return (unsigned int)(s - str);
  635. }
  636. // internal test if char is a digit (0-9)
  637. // \return true if char is a digit
  638. static inline bool _is_digit(char ch)
  639. {
  640. return (ch >= '0') && (ch <= '9');
  641. }
  642. // internal ASCII string to unsigned int conversion
  643. static unsigned int _atoi(const char** str)
  644. {
  645. unsigned int i = 0U;
  646. while (_is_digit(**str)) {
  647. i = i * 10U + (unsigned int)(*((*str)++) - '0');
  648. }
  649. return i;
  650. }
  651. // output the specified string in reverse, taking care of any zero-padding
  652. static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen, const char* buf, size_t len, unsigned int width, unsigned int flags)
  653. {
  654. const size_t start_idx = idx;
  655. // pad spaces up to given width
  656. if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) {
  657. for (size_t i = len; i < width; i++) {
  658. out(' ', buffer, idx++, maxlen);
  659. }
  660. }
  661. // reverse string
  662. while (len) {
  663. out(buf[--len], buffer, idx++, maxlen);
  664. }
  665. // append pad spaces up to given width
  666. if (flags & FLAGS_LEFT) {
  667. while (idx - start_idx < width) {
  668. out(' ', buffer, idx++, maxlen);
  669. }
  670. }
  671. return idx;
  672. }
  673. // internal itoa format
  674. static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags)
  675. {
  676. // pad leading zeros
  677. if (!(flags & FLAGS_LEFT)) {
  678. if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
  679. width--;
  680. }
  681. while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
  682. buf[len++] = '0';
  683. }
  684. while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
  685. buf[len++] = '0';
  686. }
  687. }
  688. // handle hash
  689. if (flags & FLAGS_HASH) {
  690. if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) {
  691. len--;
  692. if (len && (base == 16U)) {
  693. len--;
  694. }
  695. }
  696. if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
  697. buf[len++] = 'x';
  698. }
  699. else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
  700. buf[len++] = 'X';
  701. }
  702. else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
  703. buf[len++] = 'b';
  704. }
  705. if (len < PRINTF_NTOA_BUFFER_SIZE) {
  706. buf[len++] = '0';
  707. }
  708. }
  709. if (len < PRINTF_NTOA_BUFFER_SIZE) {
  710. if (negative) {
  711. buf[len++] = '-';
  712. }
  713. else if (flags & FLAGS_PLUS) {
  714. buf[len++] = '+'; // ignore the space if the '+' exists
  715. }
  716. else if (flags & FLAGS_SPACE) {
  717. buf[len++] = ' ';
  718. }
  719. }
  720. return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
  721. }
  722. // internal itoa for 'long' type
  723. static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base, unsigned int prec, unsigned int width, unsigned int flags)
  724. {
  725. char buf[PRINTF_NTOA_BUFFER_SIZE];
  726. size_t len = 0U;
  727. // no hash for 0 values
  728. if (!value) {
  729. flags &= ~FLAGS_HASH;
  730. }
  731. // write if precision != 0 and value is != 0
  732. if (!(flags & FLAGS_PRECISION) || value) {
  733. do {
  734. const char digit = (char)(value % base);
  735. buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;
  736. value /= base;
  737. } while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
  738. }
  739. return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
  740. }
  741. // internal itoa for 'long long' type
  742. #if defined(PRINTF_SUPPORT_LONG_LONG)
  743. static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags)
  744. {
  745. char buf[PRINTF_NTOA_BUFFER_SIZE];
  746. size_t len = 0U;
  747. // no hash for 0 values
  748. if (!value) {
  749. flags &= ~FLAGS_HASH;
  750. }
  751. // write if precision != 0 and value is != 0
  752. if (!(flags & FLAGS_PRECISION) || value) {
  753. do {
  754. const char digit = (char)(value % base);
  755. buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;
  756. value /= base;
  757. } while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
  758. }
  759. return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
  760. }
  761. #endif // PRINTF_SUPPORT_LONG_LONG
  762. #if defined(PRINTF_SUPPORT_FLOAT)
  763. #if defined(PRINTF_SUPPORT_EXPONENTIAL)
  764. // forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT
  765. static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags);
  766. #endif
  767. // internal ftoa for fixed decimal floating point
  768. static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
  769. {
  770. char buf[PRINTF_FTOA_BUFFER_SIZE];
  771. size_t len = 0U;
  772. double diff = 0.0;
  773. // powers of 10
  774. static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
  775. // test for special values
  776. if (value != value)
  777. return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags);
  778. if (value < -DBL_MAX)
  779. return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags);
  780. if (value > DBL_MAX)
  781. return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, width, flags);
  782. // test for very large values
  783. // standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad
  784. if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) {
  785. #if defined(PRINTF_SUPPORT_EXPONENTIAL)
  786. return _etoa(out, buffer, idx, maxlen, value, prec, width, flags);
  787. #else
  788. return 0U;
  789. #endif
  790. }
  791. // test for negative
  792. bool negative = false;
  793. if (value < 0) {
  794. negative = true;
  795. value = 0 - value;
  796. }
  797. // set default precision, if not set explicitly
  798. if (!(flags & FLAGS_PRECISION)) {
  799. prec = PRINTF_DEFAULT_FLOAT_PRECISION;
  800. }
  801. // limit precision to 9, cause a prec >= 10 can lead to overflow errors
  802. while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) {
  803. buf[len++] = '0';
  804. prec--;
  805. }
  806. int whole = (int)value;
  807. double tmp = (value - whole) * pow10[prec];
  808. unsigned long frac = (unsigned long)tmp;
  809. diff = tmp - frac;
  810. if (diff > 0.5) {
  811. ++frac;
  812. // handle rollover, e.g. case 0.99 with prec 1 is 1.0
  813. if (frac >= pow10[prec]) {
  814. frac = 0;
  815. ++whole;
  816. }
  817. }
  818. else if (diff < 0.5) {
  819. }
  820. else if ((frac == 0U) || (frac & 1U)) {
  821. // if halfway, round up if odd OR if last digit is 0
  822. ++frac;
  823. }
  824. if (prec == 0U) {
  825. diff = value - (double)whole;
  826. if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) {
  827. // exactly 0.5 and ODD, then round up
  828. // 1.5 -> 2, but 2.5 -> 2
  829. ++whole;
  830. }
  831. }
  832. else {
  833. unsigned int count = prec;
  834. // now do fractional part, as an unsigned number
  835. while (len < PRINTF_FTOA_BUFFER_SIZE) {
  836. --count;
  837. buf[len++] = (char)(48U + (frac % 10U));
  838. if (!(frac /= 10U)) {
  839. break;
  840. }
  841. }
  842. // add extra 0s
  843. while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) {
  844. buf[len++] = '0';
  845. }
  846. if (len < PRINTF_FTOA_BUFFER_SIZE) {
  847. // add decimal
  848. buf[len++] = '.';
  849. }
  850. }
  851. // do whole part, number is reversed
  852. while (len < PRINTF_FTOA_BUFFER_SIZE) {
  853. buf[len++] = (char)(48 + (whole % 10));
  854. if (!(whole /= 10)) {
  855. break;
  856. }
  857. }
  858. // pad leading zeros
  859. if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) {
  860. if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
  861. width--;
  862. }
  863. while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) {
  864. buf[len++] = '0';
  865. }
  866. }
  867. if (len < PRINTF_FTOA_BUFFER_SIZE) {
  868. if (negative) {
  869. buf[len++] = '-';
  870. }
  871. else if (flags & FLAGS_PLUS) {
  872. buf[len++] = '+'; // ignore the space if the '+' exists
  873. }
  874. else if (flags & FLAGS_SPACE) {
  875. buf[len++] = ' ';
  876. }
  877. }
  878. return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
  879. }
  880. #if defined(PRINTF_SUPPORT_EXPONENTIAL)
  881. // internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <m.jasperse@gmail.com>
  882. static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
  883. {
  884. // check for NaN and special values
  885. if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) {
  886. return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags);
  887. }
  888. // determine the sign
  889. const bool negative = value < 0;
  890. if (negative) {
  891. value = -value;
  892. }
  893. // default precision
  894. if (!(flags & FLAGS_PRECISION)) {
  895. prec = PRINTF_DEFAULT_FLOAT_PRECISION;
  896. }
  897. // determine the decimal exponent
  898. // based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c)
  899. union {
  900. uint64_t U;
  901. double F;
  902. } conv;
  903. conv.F = value;
  904. int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023; // effectively log2
  905. conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U); // drop the exponent so conv.F is now in [1,2)
  906. // now approximate log10 from the log2 integer part and an expansion of ln around 1.5
  907. int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168);
  908. // now we want to compute 10^expval but we want to be sure it won't overflow
  909. exp2 = (int)(expval * 3.321928094887362 + 0.5);
  910. const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453;
  911. const double z2 = z * z;
  912. conv.U = (uint64_t)(exp2 + 1023) << 52U;
  913. // compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex
  914. conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));
  915. // correct for rounding errors
  916. if (value < conv.F) {
  917. expval--;
  918. conv.F /= 10;
  919. }
  920. // the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters
  921. unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;
  922. // in "%g" mode, "prec" is the number of *significant figures* not decimals
  923. if (flags & FLAGS_ADAPT_EXP) {
  924. // do we want to fall-back to "%f" mode?
  925. if ((value >= 1e-4) && (value < 1e6)) {
  926. if ((int)prec > expval) {
  927. prec = (unsigned)((int)prec - expval - 1);
  928. }
  929. else {
  930. prec = 0;
  931. }
  932. flags |= FLAGS_PRECISION; // make sure _ftoa respects precision
  933. // no characters in exponent
  934. minwidth = 0U;
  935. expval = 0;
  936. }
  937. else {
  938. // we use one sigfig for the whole part
  939. if ((prec > 0) && (flags & FLAGS_PRECISION)) {
  940. --prec;
  941. }
  942. }
  943. }
  944. // will everything fit?
  945. unsigned int fwidth = width;
  946. if (width > minwidth) {
  947. // we didn't fall-back so subtract the characters required for the exponent
  948. fwidth -= minwidth;
  949. } else {
  950. // not enough characters, so go back to default sizing
  951. fwidth = 0U;
  952. }
  953. if ((flags & FLAGS_LEFT) && minwidth) {
  954. // if we're padding on the right, DON'T pad the floating part
  955. fwidth = 0U;
  956. }
  957. // rescale the float value
  958. if (expval) {
  959. value /= conv.F;
  960. }
  961. // output the floating part
  962. const size_t start_idx = idx;
  963. idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP);
  964. // output the exponent part
  965. if (!prec && minwidth) {
  966. // output the exponential symbol
  967. out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen);
  968. // output the exponent value
  969. idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth-1, FLAGS_ZEROPAD | FLAGS_PLUS);
  970. // might need to right-pad spaces
  971. if (flags & FLAGS_LEFT) {
  972. while (idx - start_idx < width) out(' ', buffer, idx++, maxlen);
  973. }
  974. }
  975. return idx;
  976. }
  977. #endif // PRINTF_SUPPORT_EXPONENTIAL
  978. #endif // PRINTF_SUPPORT_FLOAT
  979. // internal vsnprintf
  980. static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va)
  981. {
  982. unsigned int flags, width, precision, n;
  983. size_t idx = 0U;
  984. if (!buffer) {
  985. // use null output function
  986. out = _out_null;
  987. }
  988. while (*format)
  989. {
  990. // format specifier? %[flags][width][.precision][length]
  991. if (*format != '%') {
  992. // no
  993. out(*format, buffer, idx++, maxlen);
  994. format++;
  995. continue;
  996. }
  997. else {
  998. // yes, evaluate it
  999. format++;
  1000. }
  1001. // evaluate flags
  1002. flags = 0U;
  1003. do {
  1004. switch (*format) {
  1005. case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break;
  1006. case '-': flags |= FLAGS_LEFT; format++; n = 1U; break;
  1007. case '+': flags |= FLAGS_PLUS; format++; n = 1U; break;
  1008. case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break;
  1009. case '#': flags |= FLAGS_HASH; format++; n = 1U; break;
  1010. default : n = 0U; break;
  1011. }
  1012. } while (n);
  1013. // evaluate width field
  1014. width = 0U;
  1015. if (_is_digit(*format)) {
  1016. width = _atoi(&format);
  1017. }
  1018. else if (*format == '*') {
  1019. const int w = va_arg(va, int);
  1020. if (w < 0) {
  1021. flags |= FLAGS_LEFT; // reverse padding
  1022. width = (unsigned int)-w;
  1023. }
  1024. else {
  1025. width = (unsigned int)w;
  1026. }
  1027. format++;
  1028. }
  1029. // evaluate precision field
  1030. precision = 0U;
  1031. if (*format == '.') {
  1032. flags |= FLAGS_PRECISION;
  1033. format++;
  1034. if (_is_digit(*format)) {
  1035. precision = _atoi(&format);
  1036. }
  1037. else if (*format == '*') {
  1038. const int prec = (int)va_arg(va, int);
  1039. precision = prec > 0 ? (unsigned int)prec : 0U;
  1040. format++;
  1041. }
  1042. }
  1043. // evaluate length field
  1044. switch (*format) {
  1045. case 'l' :
  1046. flags |= FLAGS_LONG;
  1047. format++;
  1048. if (*format == 'l') {
  1049. flags |= FLAGS_LONG_LONG;
  1050. format++;
  1051. }
  1052. break;
  1053. case 'h' :
  1054. flags |= FLAGS_SHORT;
  1055. format++;
  1056. if (*format == 'h') {
  1057. flags |= FLAGS_CHAR;
  1058. format++;
  1059. }
  1060. break;
  1061. #if defined(PRINTF_SUPPORT_PTRDIFF_T)
  1062. case 't' :
  1063. flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
  1064. format++;
  1065. break;
  1066. #endif
  1067. case 'j' :
  1068. flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
  1069. format++;
  1070. break;
  1071. case 'z' :
  1072. flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
  1073. format++;
  1074. break;
  1075. default :
  1076. break;
  1077. }
  1078. // evaluate specifier
  1079. switch (*format) {
  1080. case 'd' :
  1081. case 'i' :
  1082. case 'u' :
  1083. case 'x' :
  1084. case 'X' :
  1085. case 'o' :
  1086. case 'b' : {
  1087. // set the base
  1088. unsigned int base;
  1089. if (*format == 'x' || *format == 'X') {
  1090. base = 16U;
  1091. }
  1092. else if (*format == 'o') {
  1093. base = 8U;
  1094. }
  1095. else if (*format == 'b') {
  1096. base = 2U;
  1097. }
  1098. else {
  1099. base = 10U;
  1100. flags &= ~FLAGS_HASH; // no hash for dec format
  1101. }
  1102. // uppercase
  1103. if (*format == 'X') {
  1104. flags |= FLAGS_UPPERCASE;
  1105. }
  1106. // no plus or space flag for u, x, X, o, b
  1107. if ((*format != 'i') && (*format != 'd')) {
  1108. flags &= ~(FLAGS_PLUS | FLAGS_SPACE);
  1109. }
  1110. // ignore '0' flag when precision is given
  1111. if (flags & FLAGS_PRECISION) {
  1112. flags &= ~FLAGS_ZEROPAD;
  1113. }
  1114. // convert the integer
  1115. if ((*format == 'i') || (*format == 'd')) {
  1116. // signed
  1117. if (flags & FLAGS_LONG_LONG) {
  1118. #if defined(PRINTF_SUPPORT_LONG_LONG)
  1119. const long long value = va_arg(va, long long);
  1120. idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
  1121. #endif
  1122. }
  1123. else if (flags & FLAGS_LONG) {
  1124. const long value = va_arg(va, long);
  1125. idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
  1126. }
  1127. else {
  1128. const int value = (flags & FLAGS_CHAR) ? (char)va_arg(va, int) : (flags & FLAGS_SHORT) ? (short int)va_arg(va, int) : va_arg(va, int);
  1129. idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
  1130. }
  1131. }
  1132. else {
  1133. // unsigned
  1134. if (flags & FLAGS_LONG_LONG) {
  1135. #if defined(PRINTF_SUPPORT_LONG_LONG)
  1136. idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision, width, flags);
  1137. #endif
  1138. }
  1139. else if (flags & FLAGS_LONG) {
  1140. idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width, flags);
  1141. }
  1142. else {
  1143. const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va_arg(va, unsigned int) : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(va, unsigned int) : va_arg(va, unsigned int);
  1144. idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags);
  1145. }
  1146. }
  1147. format++;
  1148. break;
  1149. }
  1150. #if defined(PRINTF_SUPPORT_FLOAT)
  1151. case 'f' :
  1152. case 'F' :
  1153. if (*format == 'F') flags |= FLAGS_UPPERCASE;
  1154. idx = _ftoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);
  1155. format++;
  1156. break;
  1157. #if defined(PRINTF_SUPPORT_EXPONENTIAL)
  1158. case 'e':
  1159. case 'E':
  1160. case 'g':
  1161. case 'G':
  1162. if ((*format == 'g')||(*format == 'G')) flags |= FLAGS_ADAPT_EXP;
  1163. if ((*format == 'E')||(*format == 'G')) flags |= FLAGS_UPPERCASE;
  1164. idx = _etoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);
  1165. format++;
  1166. break;
  1167. #endif // PRINTF_SUPPORT_EXPONENTIAL
  1168. #endif // PRINTF_SUPPORT_FLOAT
  1169. case 'c' : {
  1170. unsigned int l = 1U;
  1171. // pre padding
  1172. if (!(flags & FLAGS_LEFT)) {
  1173. while (l++ < width) {
  1174. out(' ', buffer, idx++, maxlen);
  1175. }
  1176. }
  1177. // char output
  1178. out((char)va_arg(va, int), buffer, idx++, maxlen);
  1179. // post padding
  1180. if (flags & FLAGS_LEFT) {
  1181. while (l++ < width) {
  1182. out(' ', buffer, idx++, maxlen);
  1183. }
  1184. }
  1185. format++;
  1186. break;
  1187. }
  1188. case 's' : {
  1189. const char* p = va_arg(va, char*);
  1190. unsigned int l = _strnlen_s(p, precision ? precision : (size_t)-1);
  1191. // pre padding
  1192. if (flags & FLAGS_PRECISION) {
  1193. l = (l < precision ? l : precision);
  1194. }
  1195. if (!(flags & FLAGS_LEFT)) {
  1196. while (l++ < width) {
  1197. out(' ', buffer, idx++, maxlen);
  1198. }
  1199. }
  1200. // string output
  1201. while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) {
  1202. out(*(p++), buffer, idx++, maxlen);
  1203. }
  1204. // post padding
  1205. if (flags & FLAGS_LEFT) {
  1206. while (l++ < width) {
  1207. out(' ', buffer, idx++, maxlen);
  1208. }
  1209. }
  1210. format++;
  1211. break;
  1212. }
  1213. case 'p' : {
  1214. width = sizeof(void*) * 2U;
  1215. flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE;
  1216. #if defined(PRINTF_SUPPORT_LONG_LONG)
  1217. const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
  1218. if (is_ll) {
  1219. idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void*), false, 16U, precision, width, flags);
  1220. }
  1221. else {
  1222. #endif
  1223. idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags);
  1224. #if defined(PRINTF_SUPPORT_LONG_LONG)
  1225. }
  1226. #endif
  1227. format++;
  1228. break;
  1229. }
  1230. case 'M' : {
  1231. const char* p = va_arg(va, char*);
  1232. char store[40];
  1233. unsigned int l = __mac2str((unsigned char *)p, store);
  1234. const char* pstr = &store[0];
  1235. // pre padding
  1236. if (flags & FLAGS_PRECISION) {
  1237. l = (l < precision ? l : precision);
  1238. }
  1239. if (!(flags & FLAGS_LEFT)) {
  1240. while (l++ < width) {
  1241. out(' ', buffer, idx++, maxlen);
  1242. }
  1243. }
  1244. // string output
  1245. while ((*pstr != 0) && (!(flags & FLAGS_PRECISION) || precision--)) {
  1246. out(*(pstr++), buffer, idx++, maxlen);
  1247. }
  1248. // post padding
  1249. if (flags & FLAGS_LEFT) {
  1250. while (l++ < width) {
  1251. out(' ', buffer, idx++, maxlen);
  1252. }
  1253. }
  1254. format++;
  1255. break;
  1256. }
  1257. case 'v' : {
  1258. uint32_t ipv4 = va_arg(va, uint32_t);
  1259. char store[40];
  1260. unsigned int l = __ip2str(4, &ipv4, store);
  1261. const char* pstr = &store[0];
  1262. // pre padding
  1263. if (flags & FLAGS_PRECISION) {
  1264. l = (l < precision ? l : precision);
  1265. }
  1266. if (!(flags & FLAGS_LEFT)) {
  1267. while (l++ < width) {
  1268. out(' ', buffer, idx++, maxlen);
  1269. }
  1270. }
  1271. // string output
  1272. while ((*pstr != 0) && (!(flags & FLAGS_PRECISION) || precision--)) {
  1273. out(*(pstr++), buffer, idx++, maxlen);
  1274. }
  1275. // post padding
  1276. if (flags & FLAGS_LEFT) {
  1277. while (l++ < width) {
  1278. out(' ', buffer, idx++, maxlen);
  1279. }
  1280. }
  1281. format++;
  1282. break;
  1283. }
  1284. case 'V' : {
  1285. char *ipv6 = va_arg(va, char*);
  1286. char store[40];
  1287. unsigned int l = __ip2str(6, (unsigned int *)ipv6, store);
  1288. const char* pstr = &store[0];
  1289. // pre padding
  1290. if (flags & FLAGS_PRECISION) {
  1291. l = (l < precision ? l : precision);
  1292. }
  1293. if (!(flags & FLAGS_LEFT)) {
  1294. while (l++ < width) {
  1295. out(' ', buffer, idx++, maxlen);
  1296. }
  1297. }
  1298. // string output
  1299. while ((*pstr != 0) && (!(flags & FLAGS_PRECISION) || precision--)) {
  1300. out(*(pstr++), buffer, idx++, maxlen);
  1301. }
  1302. // post padding
  1303. if (flags & FLAGS_LEFT) {
  1304. while (l++ < width) {
  1305. out(' ', buffer, idx++, maxlen);
  1306. }
  1307. }
  1308. format++;
  1309. break;
  1310. }
  1311. case '%' :
  1312. out('%', buffer, idx++, maxlen);
  1313. format++;
  1314. break;
  1315. default :
  1316. out(*format, buffer, idx++, maxlen);
  1317. format++;
  1318. break;
  1319. }
  1320. }
  1321. // termination
  1322. out((char)0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen);
  1323. // return written chars without terminating \0
  1324. return (int)idx;
  1325. }
  1326. int wm_vsnprintf(char* buffer, size_t count, const char* format, va_list va)
  1327. {
  1328. return _vsnprintf(_out_buffer, buffer, count, format, va);
  1329. }
  1330. int wm_printf(const char *fmt,...)
  1331. {
  1332. va_list args;
  1333. size_t length;
  1334. va_start(args, fmt);
  1335. length = _vsnprintf(_out_uart, (char*)fmt, (size_t) - 1, fmt, args);
  1336. va_end(args);
  1337. return length;
  1338. }
  1339. int wm_vprintf(const char *fmt, va_list arg_ptr)
  1340. {
  1341. size_t length;
  1342. length = _vsnprintf(_out_uart, (char*)fmt, (size_t) - 1, fmt, arg_ptr);
  1343. return length;
  1344. }
  1345. #if 1//defined(_NEWLIB_VERSION_H__)
  1346. __attribute__((weak)) int sscanf(const char *str, const char *format, ...) /* bug: replace 3.10.21 newlib */
  1347. {
  1348. va_list args;
  1349. int i;
  1350. va_start(args,format);
  1351. i = wm_vsscanf(str, format, args);
  1352. va_end(args);
  1353. return i;
  1354. }
  1355. __attribute__((weak)) int __cskyvscanfsscanf(const char *str, const char *format, ...)
  1356. {
  1357. va_list args;
  1358. int i;
  1359. va_start(args,format);
  1360. i = wm_vsscanf(str, format, args);
  1361. va_end(args);
  1362. return i;
  1363. }
  1364. __attribute__((weak)) int __cskyvprintfsprintf(char *str, const char *format, ...)
  1365. {
  1366. va_list ap;
  1367. int i;
  1368. va_start(ap, format);
  1369. i = wm_vsnprintf(str, (size_t) - 1, format, ap);
  1370. va_end(ap);
  1371. return i;
  1372. }
  1373. __attribute__((weak)) int __cskyvprintfsnprintf(char *str, size_t size, const char *format, ...)
  1374. {
  1375. va_list ap;
  1376. int i;
  1377. va_start(ap, format);
  1378. i = wm_vsnprintf(str, size, format, ap);
  1379. va_end(ap);
  1380. return i;
  1381. }
  1382. __attribute__((weak)) int __cskyvprintfvsnprintf(char *str, size_t size, const char *format, va_list ap)
  1383. {
  1384. return wm_vsnprintf(str, size, format, ap);
  1385. }
  1386. __attribute__((weak)) int __cskyvprintfvsprintf(char *str, const char *format, va_list ap)
  1387. {
  1388. return wm_vsnprintf(str, (size_t) - 1, format, ap);
  1389. }
  1390. int __cskyvprintfprintf(const char *fmt, ...) __attribute__((weak, alias("wm_printf")));
  1391. __attribute__((weak)) void __assert_fail(const char *file,
  1392. int line,
  1393. const char *func,
  1394. const char *failedexpr)
  1395. {
  1396. wm_printf("assertion \"%s\" failed: file \"%s\", line %d%s%s\r\n",
  1397. failedexpr, file, line,
  1398. func ? ", function: " : "", func ? func : "");
  1399. while(1);
  1400. }
  1401. /*For CPP type used, you first call this function in main entry*/
  1402. extern int __dtor_end__;
  1403. extern int __ctor_end__;
  1404. extern int __ctor_start__;
  1405. typedef void (*func_ptr)(void);
  1406. __attribute__((weak)) void cxx_system_init(void)
  1407. {
  1408. func_ptr *p;
  1409. for (p = (func_ptr *)&__ctor_end__ -1; p >= (func_ptr *)&__ctor_start__; p--)
  1410. {
  1411. (*p)();
  1412. }
  1413. }
  1414. #endif