printf.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. /* Digits used for conversion */
  47. static const char hexdigits[] = "0123456789abcdef";
  48. /* Temporary buffer used for numbers - just large enough for 32 bit in octal */
  49. static char buffer[12];
  50. /* Output string length */
  51. static unsigned int outlength;
  52. /* Output pointer */
  53. static char *outptr;
  54. static int maxlen;
  55. /* printf */
  56. static void outchar( char x )
  57. {
  58. if ( maxlen )
  59. {
  60. maxlen--;
  61. outfunc( x );
  62. outlength++;
  63. }
  64. }
  65. /* sprintf */
  66. static void outstr( char x )
  67. {
  68. if ( maxlen )
  69. {
  70. maxlen--;
  71. *outptr++ = x;
  72. outlength++;
  73. }
  74. }
  75. static int internal_nprintf( void ( *output_function )( char c ), const char *fmt, va_list ap )
  76. {
  77. unsigned int width;
  78. unsigned int flags;
  79. unsigned int base = 0;
  80. char *ptr = NULL;
  81. outlength = 0;
  82. while ( *fmt )
  83. {
  84. while ( 1 )
  85. {
  86. if ( *fmt == 0 )
  87. {
  88. goto end;
  89. }
  90. if ( *fmt == '%' )
  91. {
  92. fmt++;
  93. if ( *fmt != '%' )
  94. {
  95. break;
  96. }
  97. }
  98. output_function( *fmt++ );
  99. }
  100. flags = 0;
  101. width = 0;
  102. /* read all flags */
  103. do
  104. {
  105. if ( flags < FLAG_WIDTH )
  106. {
  107. switch ( *fmt )
  108. {
  109. case '0':
  110. flags |= FLAG_ZEROPAD;
  111. continue;
  112. case '-':
  113. flags |= FLAG_LEFTADJ;
  114. continue;
  115. case ' ':
  116. flags |= FLAG_BLANK;
  117. continue;
  118. case '+':
  119. flags |= FLAG_FORCESIGN;
  120. continue;
  121. }
  122. }
  123. if ( flags < FLAG_LONG )
  124. {
  125. if ( *fmt >= '0' && *fmt <= '9' )
  126. {
  127. unsigned char tmp = *fmt - '0';
  128. width = 10 * width + tmp;
  129. flags |= FLAG_WIDTH;
  130. continue;
  131. }
  132. if ( *fmt == 'h' )
  133. {
  134. continue;
  135. }
  136. if ( *fmt == 'l' )
  137. {
  138. flags |= FLAG_LONG;
  139. continue;
  140. }
  141. }
  142. break;
  143. }
  144. while ( *fmt++ );
  145. /* Strings */
  146. if ( *fmt == 'c' || *fmt == 's' )
  147. {
  148. switch ( *fmt )
  149. {
  150. case 'c':
  151. buffer[0] = va_arg( ap, int );
  152. ptr = buffer;
  153. break;
  154. case 's':
  155. ptr = va_arg( ap, char * );
  156. break;
  157. }
  158. goto output;
  159. }
  160. /* Numbers */
  161. switch ( *fmt )
  162. {
  163. case 'u':
  164. flags |= FLAG_UNSIGNED;
  165. case 'd':
  166. base = 10;
  167. break;
  168. case 'o':
  169. base = 8;
  170. flags |= FLAG_UNSIGNED;
  171. break;
  172. case 'p': // pointer
  173. output_function( '0' );
  174. output_function( 'x' );
  175. width -= 2;
  176. case 'x':
  177. case 'X':
  178. base = 16;
  179. flags |= FLAG_UNSIGNED;
  180. break;
  181. }
  182. unsigned int num;
  183. if ( !( flags & FLAG_UNSIGNED ) )
  184. {
  185. int tmp = va_arg( ap, int );
  186. if ( tmp < 0 )
  187. {
  188. num = -tmp;
  189. flags |= FLAG_NEGATIVE;
  190. }
  191. else
  192. {
  193. num = tmp;
  194. }
  195. }
  196. else
  197. {
  198. num = va_arg( ap, unsigned int );
  199. }
  200. /* Convert number into buffer */
  201. ptr = buffer + sizeof( buffer );
  202. *--ptr = 0;
  203. do
  204. {
  205. *--ptr = hexdigits[num % base];
  206. num /= base;
  207. }
  208. while ( num != 0 );
  209. /* Sign */
  210. if ( flags & FLAG_NEGATIVE )
  211. {
  212. output_function( '-' );
  213. width--;
  214. }
  215. else if ( flags & FLAG_FORCESIGN )
  216. {
  217. output_function( '+' );
  218. width--;
  219. }
  220. else if ( flags & FLAG_BLANK )
  221. {
  222. output_function( ' ' );
  223. width--;
  224. }
  225. output:
  226. /* left padding */
  227. if ( ( flags & FLAG_WIDTH ) && !( flags & FLAG_LEFTADJ ) )
  228. {
  229. while ( strlen( ptr ) < width )
  230. {
  231. if ( flags & FLAG_ZEROPAD )
  232. {
  233. output_function( '0' );
  234. }
  235. else
  236. {
  237. output_function( ' ' );
  238. }
  239. width--;
  240. }
  241. }
  242. /* data */
  243. while ( *ptr )
  244. {
  245. output_function( *ptr++ );
  246. if ( width )
  247. {
  248. width--;
  249. }
  250. }
  251. /* right padding */
  252. if ( flags & FLAG_WIDTH )
  253. {
  254. while ( width )
  255. {
  256. output_function( ' ' );
  257. width--;
  258. }
  259. }
  260. fmt++;
  261. }
  262. end:
  263. return outlength;
  264. }
  265. int printf( const char *format, ... )
  266. {
  267. va_list ap;
  268. int res;
  269. maxlen = -1;
  270. va_start( ap, format );
  271. res = internal_nprintf( outchar, format, ap );
  272. va_end( ap );
  273. return res;
  274. }
  275. int snprintf( char *str, size_t size, const char *format, ... )
  276. {
  277. va_list ap;
  278. int res;
  279. maxlen = size;
  280. outptr = str;
  281. va_start( ap, format );
  282. res = internal_nprintf( outstr, format, ap );
  283. va_end( ap );
  284. if ( res < size )
  285. {
  286. str[res] = 0;
  287. }
  288. return res;
  289. }
  290. /* Required for gcc compatibility */
  291. int puts( const char *str )
  292. {
  293. uart_puts( str );
  294. uart_putc( '\n' );
  295. return 0;
  296. }
  297. #undef putchar
  298. int putchar( int c )
  299. {
  300. uart_putc( c );
  301. return 0;
  302. }