]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_SmartFusion2_M2S050_SoftConsole/RTOSDemo/printf-stdarg.c
Add missing +TCP code.
[freertos] / FreeRTOS / Demo / CORTEX_SmartFusion2_M2S050_SoftConsole / RTOSDemo / 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 #include "FreeRTOS.h"\r
28 #include <stdarg.h>\r
29 #include <stdint.h>\r
30 \r
31 static void printchar(char **str, int c)\r
32 {\r
33         if (str) {\r
34                 **str = (char)c;\r
35                 ++(*str);\r
36         }\r
37         else\r
38         {\r
39                 /* Output char here. */\r
40         }\r
41 }\r
42 \r
43 #define PAD_RIGHT 1\r
44 #define PAD_ZERO 2\r
45 \r
46 static int prints(char **out, const char *string, int width, int pad)\r
47 {\r
48         register int pc = 0, padchar = ' ';\r
49 \r
50         if (width > 0) {\r
51                 register int len = 0;\r
52                 register const char *ptr;\r
53                 for (ptr = string; *ptr; ++ptr) ++len;\r
54                 if (len >= width) width = 0;\r
55                 else width -= len;\r
56                 if (pad & PAD_ZERO) padchar = '0';\r
57         }\r
58         if (!(pad & PAD_RIGHT)) {\r
59                 for ( ; width > 0; --width) {\r
60                         printchar (out, padchar);\r
61                         ++pc;\r
62                 }\r
63         }\r
64         for ( ; *string ; ++string) {\r
65                 printchar (out, *string);\r
66                 ++pc;\r
67         }\r
68         for ( ; width > 0; --width) {\r
69                 printchar (out, padchar);\r
70                 ++pc;\r
71         }\r
72 \r
73         return pc;\r
74 }\r
75 \r
76 /* the following should be enough for 32 bit int */\r
77 #define PRINT_BUF_LEN 12\r
78 \r
79 static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)\r
80 {\r
81         char print_buf[PRINT_BUF_LEN];\r
82         register char *s;\r
83         register int t, neg = 0, pc = 0;\r
84         register unsigned int u = (unsigned int)i;\r
85 \r
86         if (i == 0) {\r
87                 print_buf[0] = '0';\r
88                 print_buf[1] = '\0';\r
89                 return prints (out, print_buf, width, pad);\r
90         }\r
91 \r
92         if (sg && b == 10 && i < 0) {\r
93                 neg = 1;\r
94                 u = (unsigned int)-i;\r
95         }\r
96 \r
97         s = print_buf + PRINT_BUF_LEN-1;\r
98         *s = '\0';\r
99 \r
100         while (u) {\r
101                 t = (unsigned int)u % b;\r
102                 if( t >= 10 )\r
103                         t += letbase - '0' - 10;\r
104                 *--s = (char)(t + '0');\r
105                 u /= b;\r
106         }\r
107 \r
108         if (neg) {\r
109                 if( width && (pad & PAD_ZERO) ) {\r
110                         printchar (out, '-');\r
111                         ++pc;\r
112                         --width;\r
113                 }\r
114                 else {\r
115                         *--s = '-';\r
116                 }\r
117         }\r
118 \r
119         return pc + prints (out, s, width, pad);\r
120 }\r
121 \r
122 static int print( char **out, const char *format, va_list args )\r
123 {\r
124         register int width, pad;\r
125         register int pc = 0;\r
126         char scr[2];\r
127 \r
128         for (; *format != 0; ++format) {\r
129                 if (*format == '%') {\r
130                         ++format;\r
131                         width = pad = 0;\r
132                         if (*format == '\0') break;\r
133                         if (*format == '%') goto out;\r
134                         if (*format == '-') {\r
135                                 ++format;\r
136                                 pad = PAD_RIGHT;\r
137                         }\r
138                         while (*format == '0') {\r
139                                 ++format;\r
140                                 pad |= PAD_ZERO;\r
141                         }\r
142                         for ( ; *format >= '0' && *format <= '9'; ++format) {\r
143                                 width *= 10;\r
144                                 width += *format - '0';\r
145                         }\r
146                         if( *format == 's' ) {\r
147                                 register char *s = (char *)va_arg( args, int );\r
148                                 pc += prints (out, s?s:"(null)", width, pad);\r
149                                 continue;\r
150                         }\r
151                         if( *format == 'd' || *format == 'i' ) {\r
152                                 pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a');\r
153                                 continue;\r
154                         }\r
155                         if( *format == 'x' ) {\r
156                                 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a');\r
157                                 continue;\r
158                         }\r
159                         if( *format == 'X' ) {\r
160                                 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A');\r
161                                 continue;\r
162                         }\r
163                         if( *format == 'u' ) {\r
164                                 pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a');\r
165                                 continue;\r
166                         }\r
167                         if( *format == 'c' ) {\r
168                                 /* char are converted to int then pushed on the stack */\r
169                                 scr[0] = (char)va_arg( args, int );\r
170                                 scr[1] = '\0';\r
171                                 pc += prints (out, scr, width, pad);\r
172                                 continue;\r
173                         }\r
174                 }\r
175                 else {\r
176                 out:\r
177                         printchar (out, *format);\r
178                         ++pc;\r
179                 }\r
180         }\r
181         if (out) **out = '\0';\r
182         va_end( args );\r
183         return pc;\r
184 }\r
185 \r
186 int printf(const char *format, ...)\r
187 {\r
188         va_list args;\r
189 \r
190         va_start( args, format );\r
191         return print( 0, format, args );\r
192 }\r
193 \r
194 int sprintf(char *out, const char *format, ...)\r
195 {\r
196         va_list args;\r
197 \r
198         va_start( args, format );\r
199         return print( &out, format, args );\r
200 }\r
201 \r
202 \r
203 int snprintf( char *buf, unsigned int count, const char *format, ... )\r
204 {\r
205         va_list args;\r
206 \r
207         ( void ) count;\r
208 \r
209         va_start( args, format );\r
210         return print( &buf, format, args );\r
211 }\r
212 \r
213 #ifdef TEST_PRINTF\r
214 int main(void)\r
215 {\r
216         char *ptr = "Hello world!";\r
217         char *np = 0;\r
218         int i = 5;\r
219         unsigned int bs = sizeof(int)*8;\r
220         int mi;\r
221         char buf[80];\r
222 \r
223         mi = (1 << (bs-1)) + 1;\r
224         printf("%s\n", ptr);\r
225         printf("printf test\n");\r
226         printf("%s is null pointer\n", np);\r
227         printf("%d = 5\n", i);\r
228         printf("%d = - max int\n", mi);\r
229         printf("char %c = 'a'\n", 'a');\r
230         printf("hex %x = ff\n", 0xff);\r
231         printf("hex %02x = 00\n", 0);\r
232         printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3);\r
233         printf("%d %s(s)%", 0, "message");\r
234         printf("\n");\r
235         printf("%d %s(s) with %%\n", 0, "message");\r
236         sprintf(buf, "justif: \"%-10s\"\n", "left"); printf("%s", buf);\r
237         sprintf(buf, "justif: \"%10s\"\n", "right"); printf("%s", buf);\r
238         sprintf(buf, " 3: %04d zero padded\n", 3); printf("%s", buf);\r
239         sprintf(buf, " 3: %-4d left justif.\n", 3); printf("%s", buf);\r
240         sprintf(buf, " 3: %4d right justif.\n", 3); printf("%s", buf);\r
241         sprintf(buf, "-3: %04d zero padded\n", -3); printf("%s", buf);\r
242         sprintf(buf, "-3: %-4d left justif.\n", -3); printf("%s", buf);\r
243         sprintf(buf, "-3: %4d right justif.\n", -3); printf("%s", buf);\r
244 \r
245         return 0;\r
246 }\r
247 \r
248 /*\r
249  * if you compile this file with\r
250  *   gcc -Wall $(YOUR_C_OPTIONS) -DTEST_PRINTF -c printf.c\r
251  * you will get a normal warning:\r
252  *   printf.c:214: warning: spurious trailing `%' in format\r
253  * this line is testing an invalid % at the end of the format string.\r
254  *\r
255  * this should display (on 32bit int machine) :\r
256  *\r
257  * Hello world!\r
258  * printf test\r
259  * (null) is null pointer\r
260  * 5 = 5\r
261  * -2147483647 = - max int\r
262  * char a = 'a'\r
263  * hex ff = ff\r
264  * hex 00 = 00\r
265  * signed -3 = unsigned 4294967293 = hex fffffffd\r
266  * 0 message(s)\r
267  * 0 message(s) with %\r
268  * justif: "left      "\r
269  * justif: "     right"\r
270  *  3: 0003 zero padded\r
271  *  3: 3    left justif.\r
272  *  3:    3 right justif.\r
273  * -3: -003 zero padded\r
274  * -3: -3   left justif.\r
275  * -3:   -3 right justif.\r
276  */\r
277 \r
278 #endif\r
279 \r
280 \r
281 /* To keep linker happy. */\r
282 int     write( int i, char* c, int n)\r
283 {\r
284         (void)i;\r
285         (void)n;\r
286         (void)c;\r
287         return 0;\r
288 }\r
289 \r