printf.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* Small, noncompliant, not-full-featured printf implementation
  2. *
  3. *
  4. * Copyright (c) 2010, Ingo Korb
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ingo Korb nor the
  15. * names of the contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. *
  30. * FIXME: Selection of output function should be more flexible
  31. */
  32. #include <stdio.h>
  33. #include <stdarg.h>
  34. #include <string.h>
  35. #include "config.h"
  36. #include "uart.h"
  37. #define outfunc(x) uart_putc(x)
  38. #define FLAG_ZEROPAD 1
  39. #define FLAG_LEFTADJ 2
  40. #define FLAG_BLANK 4
  41. #define FLAG_FORCESIGN 8
  42. #define FLAG_WIDTH 16
  43. #define FLAG_LONG 32
  44. #define FLAG_UNSIGNED 64
  45. #define FLAG_NEGATIVE 128
  46. #ifdef DEBUG_BL
  47. /* Digits used for conversion */
  48. static const char hexdigits[] = "0123456789abcdef";
  49. /* Temporary buffer used for numbers - just large enough for 32 bit in octal */
  50. static char buffer[12];
  51. /* Output string length */
  52. static unsigned int outlength;
  53. /* Output pointer */
  54. static char *outptr;
  55. static int maxlen;
  56. /* printf */
  57. static void outchar( char x )
  58. {
  59. if ( maxlen )
  60. {
  61. maxlen--;
  62. outfunc( x );
  63. outlength++;
  64. }
  65. }
  66. /* sprintf */
  67. static void outstr( char x )
  68. {
  69. if ( maxlen )
  70. {
  71. maxlen--;
  72. *outptr++ = x;
  73. outlength++;
  74. }
  75. }
  76. static int internal_nprintf( void ( *output_function )( char c ), const char *fmt, va_list ap )
  77. {
  78. unsigned int width;
  79. unsigned int flags;
  80. unsigned int base = 0;
  81. char *ptr = NULL;
  82. outlength = 0;
  83. while ( *fmt )
  84. {
  85. while ( 1 )
  86. {
  87. if ( *fmt == 0 )
  88. {
  89. goto end;
  90. }
  91. if ( *fmt == '%' )
  92. {
  93. fmt++;
  94. if ( *fmt != '%' )
  95. {
  96. break;
  97. }
  98. }
  99. output_function( *fmt++ );
  100. }
  101. flags = 0;
  102. width = 0;
  103. /* read all flags */
  104. do
  105. {
  106. if ( flags < FLAG_WIDTH )
  107. {
  108. switch ( *fmt )
  109. {
  110. case '0':
  111. flags |= FLAG_ZEROPAD;
  112. continue;
  113. case '-':
  114. flags |= FLAG_LEFTADJ;
  115. continue;
  116. case ' ':
  117. flags |= FLAG_BLANK;
  118. continue;
  119. case '+':
  120. flags |= FLAG_FORCESIGN;
  121. continue;
  122. }
  123. }
  124. if ( flags < FLAG_LONG )
  125. {
  126. if ( *fmt >= '0' && *fmt <= '9' )
  127. {
  128. unsigned char tmp = *fmt - '0';
  129. width = 10 * width + tmp;
  130. flags |= FLAG_WIDTH;
  131. continue;
  132. }
  133. if ( *fmt == 'h' )
  134. {
  135. continue;
  136. }
  137. if ( *fmt == 'l' )
  138. {
  139. flags |= FLAG_LONG;
  140. continue;
  141. }
  142. }
  143. break;
  144. }
  145. while ( *fmt++ );
  146. /* Strings */
  147. if ( *fmt == 'c' || *fmt == 's' )
  148. {
  149. switch ( *fmt )
  150. {
  151. case 'c':
  152. buffer[0] = va_arg( ap, int );
  153. ptr = buffer;
  154. break;
  155. case 's':
  156. ptr = va_arg( ap, char * );
  157. break;
  158. }
  159. goto output;
  160. }
  161. /* Numbers */
  162. switch ( *fmt )
  163. {
  164. case 'u':
  165. flags |= FLAG_UNSIGNED;
  166. case 'd':
  167. base = 10;
  168. break;
  169. case 'o':
  170. base = 8;
  171. flags |= FLAG_UNSIGNED;
  172. break;
  173. case 'p': // pointer
  174. output_function( '0' );
  175. output_function( 'x' );
  176. width -= 2;
  177. case 'x':
  178. case 'X':
  179. base = 16;
  180. flags |= FLAG_UNSIGNED;
  181. break;
  182. }
  183. unsigned int num;
  184. if ( !( flags & FLAG_UNSIGNED ) )
  185. {
  186. int tmp = va_arg( ap, int );
  187. if ( tmp < 0 )
  188. {
  189. num = -tmp;
  190. flags |= FLAG_NEGATIVE;
  191. }
  192. else
  193. {
  194. num = tmp;
  195. }
  196. }
  197. else
  198. {
  199. num = va_arg( ap, unsigned int );
  200. }
  201. /* Convert number into buffer */
  202. ptr = buffer + sizeof( buffer );
  203. *--ptr = 0;
  204. do
  205. {
  206. *--ptr = hexdigits[num % base];
  207. num /= base;
  208. }
  209. while ( num != 0 );
  210. /* Sign */
  211. if ( flags & FLAG_NEGATIVE )
  212. {
  213. output_function( '-' );
  214. width--;
  215. }
  216. else if ( flags & FLAG_FORCESIGN )
  217. {
  218. output_function( '+' );
  219. width--;
  220. }
  221. else if ( flags & FLAG_BLANK )
  222. {
  223. output_function( ' ' );
  224. width--;
  225. }
  226. output:
  227. /* left padding */
  228. if ( ( flags & FLAG_WIDTH ) && !( flags & FLAG_LEFTADJ ) )
  229. {
  230. while ( strlen( ptr ) < width )
  231. {
  232. if ( flags & FLAG_ZEROPAD )
  233. {
  234. output_function( '0' );
  235. }
  236. else
  237. {
  238. output_function( ' ' );
  239. }
  240. width--;
  241. }
  242. }
  243. /* data */
  244. while ( *ptr )
  245. {
  246. output_function( *ptr++ );
  247. if ( width )
  248. {
  249. width--;
  250. }
  251. }
  252. /* right padding */
  253. if ( flags & FLAG_WIDTH )
  254. {
  255. while ( width )
  256. {
  257. output_function( ' ' );
  258. width--;
  259. }
  260. }
  261. fmt++;
  262. }
  263. end:
  264. return outlength;
  265. }
  266. int printf( const char *format, ... )
  267. {
  268. va_list ap;
  269. int res;
  270. maxlen = -1;
  271. va_start( ap, format );
  272. res = internal_nprintf( outchar, format, ap );
  273. va_end( ap );
  274. return res;
  275. }
  276. int snprintf( char *str, size_t size, const char *format, ... )
  277. {
  278. va_list ap;
  279. int res;
  280. maxlen = size;
  281. outptr = str;
  282. va_start( ap, format );
  283. res = internal_nprintf( outstr, format, ap );
  284. va_end( ap );
  285. if ( res < size )
  286. {
  287. str[res] = 0;
  288. }
  289. return res;
  290. }
  291. /* Required for gcc compatibility */
  292. int puts( const char *str )
  293. {
  294. uart_puts( str );
  295. uart_putc( '\n' );
  296. return 0;
  297. }
  298. #undef putchar
  299. int putchar( int c )
  300. {
  301. uart_putc( c );
  302. return 0;
  303. }
  304. #else
  305. int printf( const char *format, ... )
  306. {
  307. return 0;
  308. }
  309. //int snprintf(char *str, size_t size, const char *format, ...) { return 0; }
  310. int puts( const char *str )
  311. {
  312. return 0;
  313. }
  314. #undef putchar
  315. int putchar( int c )
  316. {
  317. return 0;
  318. }
  319. #endif