1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Print Formatted Output |
---|
5 | * @ingroup libcsupport |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * (C) Copyright 1997 - |
---|
10 | * - NavIST Group - Real-Time Distributed Systems and Industrial Automation |
---|
11 | * |
---|
12 | * http://pandora.ist.utl.pt |
---|
13 | * |
---|
14 | * Instituto Superior Tecnico * Lisboa * PORTUGAL |
---|
15 | * |
---|
16 | * Disclaimer: |
---|
17 | * |
---|
18 | * This file is provided "AS IS" without warranty of any kind, either |
---|
19 | * expressed or implied. |
---|
20 | * |
---|
21 | * This code is based on code by: Jose Rufino - IST |
---|
22 | */ |
---|
23 | |
---|
24 | #if HAVE_CONFIG_H |
---|
25 | #include "config.h" |
---|
26 | #endif |
---|
27 | |
---|
28 | #include <stdarg.h> |
---|
29 | #include <stdio.h> |
---|
30 | #include <stdbool.h> |
---|
31 | #include <rtems/bspIo.h> |
---|
32 | |
---|
33 | static int printNum( |
---|
34 | long long num, |
---|
35 | unsigned base, |
---|
36 | bool sign, |
---|
37 | unsigned maxwidth, |
---|
38 | char lead |
---|
39 | ); |
---|
40 | |
---|
41 | /** |
---|
42 | * A simplified version of printf intended for use when the |
---|
43 | * console is not yet initialized or in ISR's. |
---|
44 | * |
---|
45 | * Arguments: |
---|
46 | * as in printf: fmt - format string, ... - unnamed arguments. |
---|
47 | */ |
---|
48 | int vprintk( |
---|
49 | const char *fmt, |
---|
50 | va_list ap |
---|
51 | ) |
---|
52 | { |
---|
53 | int len_out = 0; |
---|
54 | for (; *fmt != '\0'; fmt++) { |
---|
55 | unsigned base = 0; |
---|
56 | unsigned width = 0; |
---|
57 | enum { |
---|
58 | LFLAG_INT, |
---|
59 | LFLAG_LONG, |
---|
60 | LFLAG_LONG_LONG |
---|
61 | } lflag = LFLAG_INT; |
---|
62 | bool minus = false; |
---|
63 | bool sign = false; |
---|
64 | char lead = ' '; |
---|
65 | char c = *fmt; |
---|
66 | long long num; |
---|
67 | |
---|
68 | if (c != '%') { |
---|
69 | rtems_putc(c); |
---|
70 | ++len_out; |
---|
71 | continue; |
---|
72 | } |
---|
73 | |
---|
74 | ++fmt; c = *fmt; |
---|
75 | |
---|
76 | if (c == '0') { |
---|
77 | lead = '0'; |
---|
78 | ++fmt; c = *fmt; |
---|
79 | } |
---|
80 | |
---|
81 | if (c == '-') { |
---|
82 | minus = true; |
---|
83 | ++fmt; c = *fmt; |
---|
84 | } |
---|
85 | |
---|
86 | while (c >= '0' && c <= '9' ) { |
---|
87 | width *= 10; |
---|
88 | width += ((unsigned) c - '0'); |
---|
89 | ++fmt; c = *fmt; |
---|
90 | } |
---|
91 | |
---|
92 | if (c == 'l') { |
---|
93 | lflag = LFLAG_LONG; |
---|
94 | ++fmt; c = *fmt; |
---|
95 | |
---|
96 | if (c == 'l') { |
---|
97 | lflag = LFLAG_LONG_LONG; |
---|
98 | ++fmt; c = *fmt; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | if ( c == 'c' ) { |
---|
103 | /* need a cast here since va_arg() only takes fully promoted types */ |
---|
104 | char chr = (char) va_arg(ap, int); |
---|
105 | rtems_putc(chr); |
---|
106 | ++len_out; |
---|
107 | continue; |
---|
108 | } |
---|
109 | |
---|
110 | if ( c == 's' ) { |
---|
111 | unsigned i, len; |
---|
112 | char *s, *str; |
---|
113 | |
---|
114 | str = va_arg(ap, char *); |
---|
115 | |
---|
116 | if ( str == NULL ) { |
---|
117 | str = ""; |
---|
118 | } |
---|
119 | |
---|
120 | /* calculate length of string */ |
---|
121 | for ( len=0, s=str ; *s ; len++, s++ ) |
---|
122 | ; |
---|
123 | |
---|
124 | /* leading spaces */ |
---|
125 | if ( !minus ) |
---|
126 | for ( i=len ; i<width ; i++, len_out++ ) |
---|
127 | rtems_putc(' '); |
---|
128 | |
---|
129 | /* no width option */ |
---|
130 | if (width == 0) { |
---|
131 | width = len; |
---|
132 | } |
---|
133 | |
---|
134 | /* output the string */ |
---|
135 | for ( i=0 ; i<width && *str ; str++, len_out++ ) |
---|
136 | rtems_putc(*str); |
---|
137 | |
---|
138 | /* trailing spaces */ |
---|
139 | if ( minus ) |
---|
140 | for ( i=len ; i<width ; i++, len_out++ ) |
---|
141 | rtems_putc(' '); |
---|
142 | |
---|
143 | continue; |
---|
144 | } |
---|
145 | |
---|
146 | /* must be a numeric format or something unsupported */ |
---|
147 | if ( c == 'o' || c == 'O' ) { |
---|
148 | base = 8; sign = false; |
---|
149 | } else if ( c == 'i' || c == 'I' || |
---|
150 | c == 'd' || c == 'D' ) { |
---|
151 | base = 10; sign = true; |
---|
152 | } else if ( c == 'u' || c == 'U' ) { |
---|
153 | base = 10; sign = false; |
---|
154 | } else if ( c == 'x' || c == 'X' ) { |
---|
155 | base = 16; sign = false; |
---|
156 | } else if ( c == 'p' ) { |
---|
157 | base = 16; sign = false; lflag = LFLAG_LONG; |
---|
158 | } else { |
---|
159 | rtems_putc(c); |
---|
160 | ++len_out; |
---|
161 | continue; |
---|
162 | } |
---|
163 | |
---|
164 | switch (lflag) { |
---|
165 | case LFLAG_LONG: |
---|
166 | num = sign ? (long long) va_arg(ap, long) |
---|
167 | : (long long) va_arg(ap, unsigned long); |
---|
168 | break; |
---|
169 | case LFLAG_LONG_LONG: |
---|
170 | num = va_arg(ap, long long); |
---|
171 | break; |
---|
172 | case LFLAG_INT: |
---|
173 | default: |
---|
174 | num = sign ? (long long) va_arg(ap, int) |
---|
175 | : (long long) va_arg(ap, unsigned int); |
---|
176 | break; |
---|
177 | } |
---|
178 | |
---|
179 | len_out += printNum(num, base, sign, width, lead); |
---|
180 | } |
---|
181 | |
---|
182 | return len_out; |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * @brief Print Number in a Given Base |
---|
187 | * @param[in] num is the number to print |
---|
188 | * @param[in] base is the base used to print the number |
---|
189 | */ |
---|
190 | static int printNum( |
---|
191 | long long num, |
---|
192 | unsigned base, |
---|
193 | bool sign, |
---|
194 | unsigned maxwidth, |
---|
195 | char lead |
---|
196 | ) |
---|
197 | { |
---|
198 | unsigned long long unsigned_num; |
---|
199 | unsigned long long n; |
---|
200 | unsigned count; |
---|
201 | #define UINT64_MAX_IN_OCTAL_FORMAT "1777777777777777777777" |
---|
202 | char toPrint[sizeof(UINT64_MAX_IN_OCTAL_FORMAT)]; |
---|
203 | int len_out = 0; |
---|
204 | |
---|
205 | if ( sign && (num < 0) ) { |
---|
206 | rtems_putc('-'); |
---|
207 | ++len_out; |
---|
208 | unsigned_num = (unsigned long long) -num; |
---|
209 | if (maxwidth) maxwidth--; |
---|
210 | } else { |
---|
211 | unsigned_num = (unsigned long long) num; |
---|
212 | } |
---|
213 | |
---|
214 | count = 0; |
---|
215 | while ((n = unsigned_num / base) > 0) { |
---|
216 | toPrint[count++] = (char) (unsigned_num - (n * base)); |
---|
217 | unsigned_num = n; |
---|
218 | } |
---|
219 | toPrint[count++] = (char) unsigned_num; |
---|
220 | |
---|
221 | for (n=maxwidth ; n > count; n--, len_out++ ) |
---|
222 | rtems_putc(lead); |
---|
223 | |
---|
224 | for (n = 0; n < count; n++, len_out++) { |
---|
225 | rtems_putc("0123456789ABCDEF"[(int)(toPrint[count-(n+1)])]); |
---|
226 | } |
---|
227 | |
---|
228 | return len_out; |
---|
229 | } |
---|