/FreeRTOS/Demo/CORTEX_SmartFusion2_M2S050_SoftConsole/RTOSDemo/printf-stdarg.c

https://bitbucket.org/minux/freertos · C · 289 lines · 199 code · 32 blank · 58 comment · 52 complexity · 94e65386e0c133e0d0e2e1169cd21abb MD5 · raw file

  1. /*
  2. Copyright 2001, 2002 Georges Menie (www.menie.org)
  3. stdarg version contributed by Christian Ettinger
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. /*
  17. putchar is the only external dependency for this file,
  18. if you have a working putchar, leave it commented out.
  19. If not, uncomment the define below and
  20. replace outbyte(c) by your own function call.
  21. */
  22. #include "FreeRTOS.h"
  23. #include <stdarg.h>
  24. #include <stdint.h>
  25. static void printchar(char **str, int c)
  26. {
  27. if (str) {
  28. **str = (char)c;
  29. ++(*str);
  30. }
  31. else
  32. {
  33. /* Output char here. */
  34. }
  35. }
  36. #define PAD_RIGHT 1
  37. #define PAD_ZERO 2
  38. static int prints(char **out, const char *string, int width, int pad)
  39. {
  40. register int pc = 0, padchar = ' ';
  41. if (width > 0) {
  42. register int len = 0;
  43. register const char *ptr;
  44. for (ptr = string; *ptr; ++ptr) ++len;
  45. if (len >= width) width = 0;
  46. else width -= len;
  47. if (pad & PAD_ZERO) padchar = '0';
  48. }
  49. if (!(pad & PAD_RIGHT)) {
  50. for ( ; width > 0; --width) {
  51. printchar (out, padchar);
  52. ++pc;
  53. }
  54. }
  55. for ( ; *string ; ++string) {
  56. printchar (out, *string);
  57. ++pc;
  58. }
  59. for ( ; width > 0; --width) {
  60. printchar (out, padchar);
  61. ++pc;
  62. }
  63. return pc;
  64. }
  65. /* the following should be enough for 32 bit int */
  66. #define PRINT_BUF_LEN 12
  67. static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)
  68. {
  69. char print_buf[PRINT_BUF_LEN];
  70. register char *s;
  71. register int t, neg = 0, pc = 0;
  72. register unsigned int u = (unsigned int)i;
  73. if (i == 0) {
  74. print_buf[0] = '0';
  75. print_buf[1] = '\0';
  76. return prints (out, print_buf, width, pad);
  77. }
  78. if (sg && b == 10 && i < 0) {
  79. neg = 1;
  80. u = (unsigned int)-i;
  81. }
  82. s = print_buf + PRINT_BUF_LEN-1;
  83. *s = '\0';
  84. while (u) {
  85. t = (unsigned int)u % b;
  86. if( t >= 10 )
  87. t += letbase - '0' - 10;
  88. *--s = (char)(t + '0');
  89. u /= b;
  90. }
  91. if (neg) {
  92. if( width && (pad & PAD_ZERO) ) {
  93. printchar (out, '-');
  94. ++pc;
  95. --width;
  96. }
  97. else {
  98. *--s = '-';
  99. }
  100. }
  101. return pc + prints (out, s, width, pad);
  102. }
  103. static int print( char **out, const char *format, va_list args )
  104. {
  105. register int width, pad;
  106. register int pc = 0;
  107. char scr[2];
  108. for (; *format != 0; ++format) {
  109. if (*format == '%') {
  110. ++format;
  111. width = pad = 0;
  112. if (*format == '\0') break;
  113. if (*format == '%') goto out;
  114. if (*format == '-') {
  115. ++format;
  116. pad = PAD_RIGHT;
  117. }
  118. while (*format == '0') {
  119. ++format;
  120. pad |= PAD_ZERO;
  121. }
  122. for ( ; *format >= '0' && *format <= '9'; ++format) {
  123. width *= 10;
  124. width += *format - '0';
  125. }
  126. if( *format == 's' ) {
  127. register char *s = (char *)va_arg( args, int );
  128. pc += prints (out, s?s:"(null)", width, pad);
  129. continue;
  130. }
  131. if( *format == 'd' || *format == 'i' ) {
  132. pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a');
  133. continue;
  134. }
  135. if( *format == 'x' ) {
  136. pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a');
  137. continue;
  138. }
  139. if( *format == 'X' ) {
  140. pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A');
  141. continue;
  142. }
  143. if( *format == 'u' ) {
  144. pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a');
  145. continue;
  146. }
  147. if( *format == 'c' ) {
  148. /* char are converted to int then pushed on the stack */
  149. scr[0] = (char)va_arg( args, int );
  150. scr[1] = '\0';
  151. pc += prints (out, scr, width, pad);
  152. continue;
  153. }
  154. }
  155. else {
  156. out:
  157. printchar (out, *format);
  158. ++pc;
  159. }
  160. }
  161. if (out) **out = '\0';
  162. va_end( args );
  163. return pc;
  164. }
  165. int printf(const char *format, ...)
  166. {
  167. va_list args;
  168. va_start( args, format );
  169. return print( 0, format, args );
  170. }
  171. int sprintf(char *out, const char *format, ...)
  172. {
  173. va_list args;
  174. va_start( args, format );
  175. return print( &out, format, args );
  176. }
  177. int snprintf( char *buf, unsigned int count, const char *format, ... )
  178. {
  179. va_list args;
  180. ( void ) count;
  181. va_start( args, format );
  182. return print( &buf, format, args );
  183. }
  184. #ifdef TEST_PRINTF
  185. int main(void)
  186. {
  187. char *ptr = "Hello world!";
  188. char *np = 0;
  189. int i = 5;
  190. unsigned int bs = sizeof(int)*8;
  191. int mi;
  192. char buf[80];
  193. mi = (1 << (bs-1)) + 1;
  194. printf("%s\n", ptr);
  195. printf("printf test\n");
  196. printf("%s is null pointer\n", np);
  197. printf("%d = 5\n", i);
  198. printf("%d = - max int\n", mi);
  199. printf("char %c = 'a'\n", 'a');
  200. printf("hex %x = ff\n", 0xff);
  201. printf("hex %02x = 00\n", 0);
  202. printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3);
  203. printf("%d %s(s)%", 0, "message");
  204. printf("\n");
  205. printf("%d %s(s) with %%\n", 0, "message");
  206. sprintf(buf, "justif: \"%-10s\"\n", "left"); printf("%s", buf);
  207. sprintf(buf, "justif: \"%10s\"\n", "right"); printf("%s", buf);
  208. sprintf(buf, " 3: %04d zero padded\n", 3); printf("%s", buf);
  209. sprintf(buf, " 3: %-4d left justif.\n", 3); printf("%s", buf);
  210. sprintf(buf, " 3: %4d right justif.\n", 3); printf("%s", buf);
  211. sprintf(buf, "-3: %04d zero padded\n", -3); printf("%s", buf);
  212. sprintf(buf, "-3: %-4d left justif.\n", -3); printf("%s", buf);
  213. sprintf(buf, "-3: %4d right justif.\n", -3); printf("%s", buf);
  214. return 0;
  215. }
  216. /*
  217. * if you compile this file with
  218. * gcc -Wall $(YOUR_C_OPTIONS) -DTEST_PRINTF -c printf.c
  219. * you will get a normal warning:
  220. * printf.c:214: warning: spurious trailing `%' in format
  221. * this line is testing an invalid % at the end of the format string.
  222. *
  223. * this should display (on 32bit int machine) :
  224. *
  225. * Hello world!
  226. * printf test
  227. * (null) is null pointer
  228. * 5 = 5
  229. * -2147483647 = - max int
  230. * char a = 'a'
  231. * hex ff = ff
  232. * hex 00 = 00
  233. * signed -3 = unsigned 4294967293 = hex fffffffd
  234. * 0 message(s)
  235. * 0 message(s) with %
  236. * justif: "left "
  237. * justif: " right"
  238. * 3: 0003 zero padded
  239. * 3: 3 left justif.
  240. * 3: 3 right justif.
  241. * -3: -003 zero padded
  242. * -3: -3 left justif.
  243. * -3: -3 right justif.
  244. */
  245. #endif
  246. /* To keep linker happy. */
  247. int write( int i, char* c, int n)
  248. {
  249. (void)i;
  250. (void)n;
  251. (void)c;
  252. return 0;
  253. }