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