Rev 1224 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1224 | Rev 1226 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
3 | * Copyright (C) 2006 Josef Cejka |
3 | * Copyright (C) 2006 Josef Cejka |
4 | * All rights reserved. |
4 | * All rights reserved. |
5 | * |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
8 | * are met: |
9 | * |
9 | * |
10 | * - Redistributions of source code must retain the above copyright |
10 | * - Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * - Redistributions in binary form must reproduce the above copyright |
12 | * - Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
14 | * documentation and/or other materials provided with the distribution. |
15 | * - The name of the author may not be used to endorse or promote products |
15 | * - The name of the author may not be used to endorse or promote products |
16 | * derived from this software without specific prior written permission. |
16 | * derived from this software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
29 | 29 | ||
30 | #include <putchar.h> |
30 | #include <putchar.h> |
31 | #include <print.h> |
31 | #include <print.h> |
32 | #include <synch/spinlock.h> |
32 | #include <synch/spinlock.h> |
33 | #include <arch/arg.h> |
33 | #include <arch/arg.h> |
34 | #include <arch/asm.h> |
34 | #include <arch/asm.h> |
35 | 35 | ||
36 | #include <arch.h> |
36 | #include <arch.h> |
37 | 37 | ||
38 | SPINLOCK_INITIALIZE(printflock); /**< printf spinlock */ |
38 | SPINLOCK_INITIALIZE(printflock); /**< printf spinlock */ |
39 | 39 | ||
40 | #define __PRINTF_FLAG_PREFIX 0x00000001 /* show prefixes 0x or 0 */ |
40 | #define __PRINTF_FLAG_PREFIX 0x00000001 /* show prefixes 0x or 0 */ |
41 | #define __PRINTF_FLAG_SIGNED 0x00000002 /* signed / unsigned number */ |
41 | #define __PRINTF_FLAG_SIGNED 0x00000002 /* signed / unsigned number */ |
42 | #define __PRINTF_FLAG_ZEROPADDED 0x00000004 /* print leading zeroes */ |
42 | #define __PRINTF_FLAG_ZEROPADDED 0x00000004 /* print leading zeroes */ |
43 | #define __PRINTF_FLAG_LEFTALIGNED 0x00000010 /* align to left */ |
43 | #define __PRINTF_FLAG_LEFTALIGNED 0x00000010 /* align to left */ |
44 | #define __PRINTF_FLAG_SHOWPLUS 0x00000020 /* always show + sign */ |
44 | #define __PRINTF_FLAG_SHOWPLUS 0x00000020 /* always show + sign */ |
45 | #define __PRINTF_FLAG_SPACESIGN 0x00000040 /* print space instead of plus */ |
45 | #define __PRINTF_FLAG_SPACESIGN 0x00000040 /* print space instead of plus */ |
46 | #define __PRINTF_FLAG_BIGCHARS 0x00000080 /* show big characters */ |
46 | #define __PRINTF_FLAG_BIGCHARS 0x00000080 /* show big characters */ |
47 | #define __PRINTF_FLAG_NEGATIVE 0x00000100 /* number has - sign */ |
47 | #define __PRINTF_FLAG_NEGATIVE 0x00000100 /* number has - sign */ |
48 | 48 | ||
49 | #define PRINT_NUMBER_BUFFER_SIZE (64+5) /* Buffer big enought for 64 bit number |
49 | #define PRINT_NUMBER_BUFFER_SIZE (64+5) /* Buffer big enought for 64 bit number |
50 | * printed in base 2, sign, prefix and |
50 | * printed in base 2, sign, prefix and |
51 | * 0 to terminate string.. (last one is only for better testing |
51 | * 0 to terminate string.. (last one is only for better testing |
52 | * end of buffer by zero-filling subroutine) |
52 | * end of buffer by zero-filling subroutine) |
53 | */ |
53 | */ |
54 | typedef enum { |
54 | typedef enum { |
55 | PrintfQualifierByte = 0, |
55 | PrintfQualifierByte = 0, |
56 | PrintfQualifierShort, |
56 | PrintfQualifierShort, |
57 | PrintfQualifierInt, |
57 | PrintfQualifierInt, |
58 | PrintfQualifierLong, |
58 | PrintfQualifierLong, |
59 | PrintfQualifierLongLong, |
59 | PrintfQualifierLongLong, |
60 | PrintfQualifierNative, |
60 | PrintfQualifierNative, |
61 | PrintfQualifierPointer |
61 | PrintfQualifierPointer |
62 | } qualifier_t; |
62 | } qualifier_t; |
63 | 63 | ||
64 | static char digits_small[] = "0123456789abcdef"; /* Small hexadecimal characters */ |
64 | static char digits_small[] = "0123456789abcdef"; /* Small hexadecimal characters */ |
65 | static char digits_big[] = "0123456789ABCDEF"; /* Big hexadecimal characters */ |
65 | static char digits_big[] = "0123456789ABCDEF"; /* Big hexadecimal characters */ |
66 | 66 | ||
67 | static inline int isdigit(int c) |
67 | static inline int isdigit(int c) |
68 | { |
68 | { |
69 | return ((c >= '0' )&&( c <= '9')); |
69 | return ((c >= '0' )&&( c <= '9')); |
70 | } |
70 | } |
71 | 71 | ||
72 | static __native strlen(const char *str) |
72 | static __native strlen(const char *str) |
73 | { |
73 | { |
74 | __native counter = 0; |
74 | __native counter = 0; |
75 | 75 | ||
76 | while (str[counter] != 0) { |
76 | while (str[counter] != 0) { |
77 | counter++; |
77 | counter++; |
78 | } |
78 | } |
79 | 79 | ||
80 | return counter; |
80 | return counter; |
81 | } |
81 | } |
82 | 82 | ||
83 | /** Print one string without appending '\n' to the end |
83 | /** Print one string without appending '\n' to the end |
84 | * |
84 | * |
85 | * Dont use this function directly - printflock is not locked here |
85 | * Dont use this function directly - printflock is not locked here |
86 | * |
86 | * |
87 | */ |
87 | */ |
88 | static int putstr(const char *str) |
88 | static int putstr(const char *str) |
89 | { |
89 | { |
90 | int count; |
90 | int count; |
91 | if (str == NULL) { |
91 | if (str == NULL) { |
92 | str = "(NULL)"; |
92 | str = "(NULL)"; |
93 | } |
93 | } |
94 | 94 | ||
95 | for (count = 0; str[count] != 0; count++) { |
95 | for (count = 0; str[count] != 0; count++) { |
96 | putchar(str[count]); |
96 | putchar(str[count]); |
97 | } |
97 | } |
98 | return count; |
98 | return count; |
99 | } |
99 | } |
100 | 100 | ||
101 | /** Print count characters from buffer to output |
101 | /** Print count characters from buffer to output |
102 | * |
102 | * |
103 | */ |
103 | */ |
104 | static int putnchars(const char *buffer, __native count) |
104 | static int putnchars(const char *buffer, __native count) |
105 | { |
105 | { |
106 | int i; |
106 | int i; |
107 | if (buffer == NULL) { |
107 | if (buffer == NULL) { |
108 | buffer = "(NULL)"; |
108 | buffer = "(NULL)"; |
109 | count = 6; |
109 | count = 6; |
110 | } |
110 | } |
111 | 111 | ||
112 | for (i = 0; i < count; i++) { |
112 | for (i = 0; i < count; i++) { |
113 | putchar(buffer[i]); |
113 | putchar(buffer[i]); |
114 | } |
114 | } |
115 | 115 | ||
116 | return count; |
116 | return count; |
117 | } |
117 | } |
118 | 118 | ||
119 | /** Print one formatted character |
119 | /** Print one formatted character |
120 | * |
120 | * |
121 | * @param c character to print |
121 | * @param c character to print |
122 | * @param width |
122 | * @param width |
123 | * @param flags |
123 | * @param flags |
124 | * @return number of printed characters or EOF |
124 | * @return number of printed characters or EOF |
125 | */ |
125 | */ |
126 | static int print_char(char c, int width, __u64 flags) |
126 | static int print_char(char c, int width, __u64 flags) |
127 | { |
127 | { |
128 | int counter = 0; |
128 | int counter = 0; |
129 | 129 | ||
130 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
130 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
131 | while (--width > 0) { /* one space is consumed by character itself hence the predecrement */ |
131 | while (--width > 0) { /* one space is consumed by character itself hence the predecrement */ |
132 | /* FIXME: painfully slow */ |
132 | /* FIXME: painfully slow */ |
133 | putchar(' '); |
133 | putchar(' '); |
134 | ++counter; |
134 | ++counter; |
135 | } |
135 | } |
136 | } |
136 | } |
137 | 137 | ||
138 | putchar(c); |
138 | putchar(c); |
139 | ++counter; |
139 | ++counter; |
140 | 140 | ||
141 | while (--width > 0) { /* one space is consumed by character itself hence the predecrement */ |
141 | while (--width > 0) { /* one space is consumed by character itself hence the predecrement */ |
142 | putchar(' '); |
142 | putchar(' '); |
143 | ++counter; |
143 | ++counter; |
144 | } |
144 | } |
145 | 145 | ||
146 | return counter; |
146 | return counter; |
147 | } |
147 | } |
148 | 148 | ||
149 | /** Print one string |
149 | /** Print one string |
150 | * @param s string |
150 | * @param s string |
151 | * @param width |
151 | * @param width |
152 | * @param precision |
152 | * @param precision |
153 | * @param flags |
153 | * @param flags |
154 | * @return number of printed characters or EOF |
154 | * @return number of printed characters or EOF |
155 | */ |
155 | */ |
156 | static int print_string(char *s, int width, int precision, __u64 flags) |
156 | static int print_string(char *s, int width, int precision, __u64 flags) |
157 | { |
157 | { |
158 | int counter = 0; |
158 | int counter = 0; |
159 | __native size; |
159 | __native size; |
160 | 160 | ||
161 | if (s == NULL) { |
161 | if (s == NULL) { |
162 | return putstr("(NULL)"); |
162 | return putstr("(NULL)"); |
163 | } |
163 | } |
164 | 164 | ||
165 | size = strlen(s); |
165 | size = strlen(s); |
166 | 166 | ||
167 | /* print leading spaces */ |
167 | /* print leading spaces */ |
168 | 168 | ||
169 | if (precision == 0) |
169 | if (precision == 0) |
170 | precision = size; |
170 | precision = size; |
171 | 171 | ||
172 | width -= precision; |
172 | width -= precision; |
173 | 173 | ||
174 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
174 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
175 | while (width-- > 0) { |
175 | while (width-- > 0) { |
176 | putchar(' '); |
176 | putchar(' '); |
177 | counter++; |
177 | counter++; |
178 | } |
178 | } |
179 | } |
179 | } |
180 | 180 | ||
181 | while (precision > size) { |
181 | while (precision > size) { |
182 | precision--; |
182 | precision--; |
183 | putchar(' '); |
183 | putchar(' '); |
184 | ++counter; |
184 | ++counter; |
185 | } |
185 | } |
186 | 186 | ||
187 | if (putnchars(s, precision) == EOF) { |
187 | if (putnchars(s, precision) == EOF) { |
188 | return EOF; |
188 | return EOF; |
189 | } |
189 | } |
190 | 190 | ||
191 | counter += precision; |
191 | counter += precision; |
192 | 192 | ||
193 | while (width-- > 0) { |
193 | while (width-- > 0) { |
194 | putchar(' '); |
194 | putchar(' '); |
195 | ++counter; |
195 | ++counter; |
196 | } |
196 | } |
197 | 197 | ||
198 | return ++counter; |
198 | return ++counter; |
199 | } |
199 | } |
200 | 200 | ||
201 | 201 | ||
202 | /** Print number in given base |
202 | /** Print number in given base |
203 | * |
203 | * |
204 | * Print significant digits of a number in given |
204 | * Print significant digits of a number in given |
205 | * base. |
205 | * base. |
206 | * |
206 | * |
207 | * @param num Number to print. |
207 | * @param num Number to print. |
208 | * @param width |
208 | * @param width |
209 | * @param precision |
209 | * @param precision |
210 | * @param base Base to print the number in (should |
210 | * @param base Base to print the number in (should |
211 | * be in range 2 .. 16). |
211 | * be in range 2 .. 16). |
212 | * @param flags output modifiers |
212 | * @param flags output modifiers |
213 | * @return number of written characters or EOF. |
213 | * @return number of written characters or EOF. |
214 | */ |
214 | */ |
215 | static int print_number(__u64 num, int width, int precision, int base , __u64 flags) |
215 | static int print_number(__u64 num, int width, int precision, int base , __u64 flags) |
216 | { |
216 | { |
217 | char *digits = digits_small; |
217 | char *digits = digits_small; |
218 | char d[PRINT_NUMBER_BUFFER_SIZE]; /* this is good enough even for base == 2, prefix and sign */ |
218 | char d[PRINT_NUMBER_BUFFER_SIZE]; /* this is good enough even for base == 2, prefix and sign */ |
219 | char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1]; |
219 | char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1]; |
220 | int size = 0; |
220 | int size = 0; |
- | 221 | int number_size; /* size of plain number */ |
|
221 | int written = 0; |
222 | int written = 0; |
222 | char sgn; |
223 | char sgn; |
223 | 224 | ||
224 | if (flags & __PRINTF_FLAG_BIGCHARS) |
225 | if (flags & __PRINTF_FLAG_BIGCHARS) |
225 | digits = digits_big; |
226 | digits = digits_big; |
226 | 227 | ||
227 | *ptr-- = 0; /* Put zero at end of string */ |
228 | *ptr-- = 0; /* Put zero at end of string */ |
228 | 229 | ||
229 | if (num == 0) { |
230 | if (num == 0) { |
230 | *ptr-- = '0'; |
231 | *ptr-- = '0'; |
231 | size++; |
232 | size++; |
232 | } else { |
233 | } else { |
233 | do { |
234 | do { |
234 | *ptr-- = digits[num % base]; |
235 | *ptr-- = digits[num % base]; |
235 | size++; |
236 | size++; |
236 | } while (num /= base); |
237 | } while (num /= base); |
237 | } |
238 | } |
238 | 239 | ||
- | 240 | number_size = size; |
|
- | 241 | ||
239 | /* Collect sum of all prefixes/signs/... to calculate padding and leading zeroes */ |
242 | /* Collect sum of all prefixes/signs/... to calculate padding and leading zeroes */ |
240 | if (flags & __PRINTF_FLAG_PREFIX) { |
243 | if (flags & __PRINTF_FLAG_PREFIX) { |
241 | switch(base) { |
244 | switch(base) { |
242 | case 2: /* Binary formating is not standard, but usefull */ |
245 | case 2: /* Binary formating is not standard, but usefull */ |
243 | size += 2; |
246 | size += 2; |
244 | break; |
247 | break; |
245 | case 8: |
248 | case 8: |
246 | size++; |
249 | size++; |
247 | break; |
250 | break; |
248 | case 16: |
251 | case 16: |
249 | size += 2; |
252 | size += 2; |
250 | break; |
253 | break; |
251 | } |
254 | } |
252 | } |
255 | } |
253 | 256 | ||
254 | sgn = 0; |
257 | sgn = 0; |
255 | if (flags & __PRINTF_FLAG_SIGNED) { |
258 | if (flags & __PRINTF_FLAG_SIGNED) { |
256 | if (flags & __PRINTF_FLAG_NEGATIVE) { |
259 | if (flags & __PRINTF_FLAG_NEGATIVE) { |
257 | sgn = '-'; |
260 | sgn = '-'; |
258 | size++; |
261 | size++; |
259 | } else if (flags & __PRINTF_FLAG_SHOWPLUS) { |
262 | } else if (flags & __PRINTF_FLAG_SHOWPLUS) { |
260 | sgn = '+'; |
263 | sgn = '+'; |
261 | size++; |
264 | size++; |
262 | } else if (flags & __PRINTF_FLAG_SPACESIGN) { |
265 | } else if (flags & __PRINTF_FLAG_SPACESIGN) { |
263 | sgn = ' '; |
266 | sgn = ' '; |
264 | size++; |
267 | size++; |
265 | } |
268 | } |
266 | } |
269 | } |
267 | 270 | ||
268 | if (flags & __PRINTF_FLAG_LEFTALIGNED) { |
271 | if (flags & __PRINTF_FLAG_LEFTALIGNED) { |
269 | flags &= ~__PRINTF_FLAG_ZEROPADDED; |
272 | flags &= ~__PRINTF_FLAG_ZEROPADDED; |
270 | } |
273 | } |
271 | 274 | ||
272 | /* if number is leftaligned or precision is specified then zeropadding is ignored */ |
275 | /* if number is leftaligned or precision is specified then zeropadding is ignored */ |
273 | if (flags & __PRINTF_FLAG_ZEROPADDED) { |
276 | if (flags & __PRINTF_FLAG_ZEROPADDED) { |
274 | if ((precision == 0) && (width > size)) { |
277 | if ((precision == 0) && (width > size)) { |
275 | precision = width - size; |
278 | precision = width - size + number_size; |
276 | } |
279 | } |
277 | } |
280 | } |
278 | 281 | ||
279 | /* print leading spaces */ |
282 | /* print leading spaces */ |
280 | if (size > precision) /* We must print the whole number, not only a part */ |
283 | if (number_size > precision) /* We must print whole number not only a part */ |
281 | precision = size; |
284 | precision = number_size; |
282 | 285 | ||
283 | width -= precision; |
286 | width -= precision + size - number_size; |
284 | 287 | ||
285 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
288 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
286 | while (width-- > 0) { |
289 | while (width-- > 0) { |
287 | putchar(' '); |
290 | putchar(' '); |
288 | written++; |
291 | written++; |
289 | } |
292 | } |
290 | } |
293 | } |
291 | 294 | ||
292 | /* print sign */ |
295 | /* print sign */ |
293 | if (sgn) { |
296 | if (sgn) { |
294 | putchar(sgn); |
297 | putchar(sgn); |
295 | written++; |
298 | written++; |
296 | } |
299 | } |
297 | 300 | ||
298 | /* print prefix */ |
301 | /* print prefix */ |
299 | 302 | ||
300 | if (flags & __PRINTF_FLAG_PREFIX) { |
303 | if (flags & __PRINTF_FLAG_PREFIX) { |
301 | switch(base) { |
304 | switch(base) { |
302 | case 2: /* Binary formating is not standard, but usefull */ |
305 | case 2: /* Binary formating is not standard, but usefull */ |
303 | putchar('0'); |
306 | putchar('0'); |
304 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
307 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
305 | putchar('B'); |
308 | putchar('B'); |
306 | } else { |
309 | } else { |
307 | putchar('b'); |
310 | putchar('b'); |
308 | } |
311 | } |
309 | written += 2; |
312 | written += 2; |
310 | break; |
313 | break; |
311 | case 8: |
314 | case 8: |
312 | putchar('o'); |
315 | putchar('o'); |
313 | written++; |
316 | written++; |
314 | break; |
317 | break; |
315 | case 16: |
318 | case 16: |
316 | putchar('0'); |
319 | putchar('0'); |
317 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
320 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
318 | putchar('X'); |
321 | putchar('X'); |
319 | } else { |
322 | } else { |
320 | putchar('x'); |
323 | putchar('x'); |
321 | } |
324 | } |
322 | written += 2; |
325 | written += 2; |
323 | break; |
326 | break; |
324 | } |
327 | } |
325 | } |
328 | } |
326 | 329 | ||
327 | /* print leading zeroes */ |
330 | /* print leading zeroes */ |
328 | precision -= size; |
331 | precision -= number_size; |
329 | while (precision-- > 0) { |
332 | while (precision-- > 0) { |
330 | putchar('0'); |
333 | putchar('0'); |
331 | written++; |
334 | written++; |
332 | } |
335 | } |
333 | 336 | ||
334 | 337 | ||
335 | /* print number itself */ |
338 | /* print number itself */ |
336 | 339 | ||
337 | written += putstr(++ptr); |
340 | written += putstr(++ptr); |
338 | 341 | ||
339 | /* print ending spaces */ |
342 | /* print ending spaces */ |
340 | 343 | ||
341 | while (width-- > 0) { |
344 | while (width-- > 0) { |
342 | putchar(' '); |
345 | putchar(' '); |
343 | written++; |
346 | written++; |
344 | } |
347 | } |
345 | 348 | ||
346 | return written; |
349 | return written; |
347 | } |
350 | } |
348 | 351 | ||
349 | /** General formatted text print |
352 | /** General formatted text print |
350 | * |
353 | * |
351 | * Print string formatted according to the fmt parameter |
354 | * Print string formatted according to the fmt parameter |
352 | * and variadic arguments. Each formatting directive |
355 | * and variadic arguments. Each formatting directive |
353 | * must have the following form: |
356 | * must have the following form: |
354 | * % [ flags ] [ width ] [ .precision ] [ type ] conversion |
357 | * % [ flags ] [ width ] [ .precision ] [ type ] conversion |
355 | * |
358 | * |
356 | * FLAGS: |
359 | * FLAGS: |
357 | * # Force to print prefix. |
360 | * # Force to print prefix. |
358 | * For conversion %o the prefix is 0, for %x and %X prefixes are 0x and 0X and for conversion %b the prefix is 0b. |
361 | * For conversion %o the prefix is 0, for %x and %X prefixes are 0x and 0X and for conversion %b the prefix is 0b. |
359 | * - Align to left. |
362 | * - Align to left. |
360 | * + Print positive sign just as negative. |
363 | * + Print positive sign just as negative. |
361 | * (space) If printed number is positive and '+' flag is not set, print space in place of sign. |
364 | * (space) If printed number is positive and '+' flag is not set, print space in place of sign. |
362 | * 0 Print 0 as padding instead of spaces. Zeroes are placed between sign and the rest of number. |
365 | * 0 Print 0 as padding instead of spaces. Zeroes are placed between sign and the rest of number. |
363 | * This flag is ignored if '-' flag is specified. |
366 | * This flag is ignored if '-' flag is specified. |
364 | * |
367 | * |
365 | * WIDTH: |
368 | * WIDTH: |
366 | * Specify minimal width of printed argument. If it is bigger, width is ignored. |
369 | * Specify minimal width of printed argument. If it is bigger, width is ignored. |
367 | * If width is specified with a '*' character instead of number, width is taken from parameter list. |
370 | * If width is specified with a '*' character instead of number, width is taken from parameter list. |
368 | * Int parameter expected before parameter for processed conversion specification. |
371 | * Int parameter expected before parameter for processed conversion specification. |
369 | * If this value is negative its absolute value is taken and the '-' flag is set. |
372 | * If this value is negative its absolute value is taken and the '-' flag is set. |
370 | * |
373 | * |
371 | * PRECISION: |
374 | * PRECISION: |
372 | * Value precision. For numbers it specifies minimum valid numbers. |
375 | * Value precision. For numbers it specifies minimum valid numbers. |
373 | * Smaller numbers are printed with leading zeroes. Bigger numbers are not affected. |
376 | * Smaller numbers are printed with leading zeroes. Bigger numbers are not affected. |
374 | * Strings with more than precision characters are cut off. |
377 | * Strings with more than precision characters are cut off. |
375 | * Just as with width, an '*' can be used used instead of a number. |
378 | * Just as with width, an '*' can be used used instead of a number. |
376 | * An integer value is then expected in parameters. When both width and precision are specified using '*', |
379 | * An integer value is then expected in parameters. When both width and precision are specified using '*', |
377 | * first parameter is used for width and second one for precision. |
380 | * first parameter is used for width and second one for precision. |
378 | * |
381 | * |
379 | * TYPE: |
382 | * TYPE: |
380 | * hh signed or unsigned char |
383 | * hh signed or unsigned char |
381 | * h signed or usigned short |
384 | * h signed or usigned short |
382 | * signed or usigned int (default value) |
385 | * signed or usigned int (default value) |
383 | * l signed or usigned long int |
386 | * l signed or usigned long int |
384 | * ll signed or usigned long long int |
387 | * ll signed or usigned long long int |
385 | * z __native (non-standard extension) |
388 | * z __native (non-standard extension) |
386 | * |
389 | * |
387 | * |
390 | * |
388 | * CONVERSIONS: |
391 | * CONVERSIONS: |
389 | * |
392 | * |
390 | * % Print percentage character. |
393 | * % Print percentage character. |
391 | * |
394 | * |
392 | * c Print single character. |
395 | * c Print single character. |
393 | * |
396 | * |
394 | * s Print zero terminated string. If a NULL value is passed as value, "(NULL)" is printed instead. |
397 | * s Print zero terminated string. If a NULL value is passed as value, "(NULL)" is printed instead. |
395 | * |
398 | * |
396 | * P, p Print value of a pointer. Void * value is expected and it is printed in hexadecimal notation with prefix |
399 | * P, p Print value of a pointer. Void * value is expected and it is printed in hexadecimal notation with prefix |
397 | * ( as with %#X or %#x for 32bit or %#X / %#x for 64bit long pointers). |
400 | * ( as with %#X or %#x for 32bit or %#X / %#x for 64bit long pointers). |
398 | * |
401 | * |
399 | * b Print value as unsigned binary number. Prefix is not printed by default. (Nonstandard extension.) |
402 | * b Print value as unsigned binary number. Prefix is not printed by default. (Nonstandard extension.) |
400 | * |
403 | * |
401 | * o Print value as unsigned octal number. Prefix is not printed by default. |
404 | * o Print value as unsigned octal number. Prefix is not printed by default. |
402 | * |
405 | * |
403 | * d,i Print signed decimal number. There is no difference between d and i conversion. |
406 | * d,i Print signed decimal number. There is no difference between d and i conversion. |
404 | * |
407 | * |
405 | * u Print unsigned decimal number. |
408 | * u Print unsigned decimal number. |
406 | * |
409 | * |
407 | * X, x Print hexadecimal number with upper- or lower-case. Prefix is not printed by default. |
410 | * X, x Print hexadecimal number with upper- or lower-case. Prefix is not printed by default. |
408 | * |
411 | * |
409 | * All other characters from fmt except the formatting directives |
412 | * All other characters from fmt except the formatting directives |
410 | * are printed in verbatim. |
413 | * are printed in verbatim. |
411 | * |
414 | * |
412 | * @param fmt Formatting NULL terminated string. |
415 | * @param fmt Formatting NULL terminated string. |
413 | * @return count of printed characters or negative value on fail. |
416 | * @return count of printed characters or negative value on fail. |
414 | */ |
417 | */ |
415 | int printf(const char *fmt, ...) |
418 | int printf(const char *fmt, ...) |
416 | { |
419 | { |
417 | int irqpri; |
420 | int irqpri; |
418 | int i = 0, j = 0; /* i is index of currently processed char from fmt, j is index to the first not printed nonformating character */ |
421 | int i = 0, j = 0; /* i is index of currently processed char from fmt, j is index to the first not printed nonformating character */ |
419 | int end; |
422 | int end; |
420 | int counter; /* counter of printed characters */ |
423 | int counter; /* counter of printed characters */ |
421 | int retval; /* used to store return values from called functions */ |
424 | int retval; /* used to store return values from called functions */ |
422 | va_list ap; |
425 | va_list ap; |
423 | char c; |
426 | char c; |
424 | qualifier_t qualifier; /* type of argument */ |
427 | qualifier_t qualifier; /* type of argument */ |
425 | int base; /* base in which will be parameter (numbers only) printed */ |
428 | int base; /* base in which will be parameter (numbers only) printed */ |
426 | __u64 number; /* argument value */ |
429 | __u64 number; /* argument value */ |
427 | __native size; /* byte size of integer parameter */ |
430 | __native size; /* byte size of integer parameter */ |
428 | int width, precision; |
431 | int width, precision; |
429 | __u64 flags; |
432 | __u64 flags; |
430 | 433 | ||
431 | counter = 0; |
434 | counter = 0; |
432 | 435 | ||
433 | va_start(ap, fmt); |
436 | va_start(ap, fmt); |
434 | 437 | ||
435 | irqpri = interrupts_disable(); |
438 | irqpri = interrupts_disable(); |
436 | spinlock_lock(&printflock); |
439 | spinlock_lock(&printflock); |
437 | 440 | ||
438 | 441 | ||
439 | while ((c = fmt[i])) { |
442 | while ((c = fmt[i])) { |
440 | /* control character */ |
443 | /* control character */ |
441 | if (c == '%' ) { |
444 | if (c == '%' ) { |
442 | /* print common characters if any processed */ |
445 | /* print common characters if any processed */ |
443 | if (i > j) { |
446 | if (i > j) { |
444 | if ((retval = putnchars(&fmt[j], (__native)(i - j))) == EOF) { /* error */ |
447 | if ((retval = putnchars(&fmt[j], (__native)(i - j))) == EOF) { /* error */ |
445 | counter = -counter; |
448 | counter = -counter; |
446 | goto out; |
449 | goto out; |
447 | } |
450 | } |
448 | counter += retval; |
451 | counter += retval; |
449 | } |
452 | } |
450 | 453 | ||
451 | j = i; |
454 | j = i; |
452 | /* parse modifiers */ |
455 | /* parse modifiers */ |
453 | flags = 0; |
456 | flags = 0; |
454 | end = 0; |
457 | end = 0; |
455 | 458 | ||
456 | do { |
459 | do { |
457 | ++i; |
460 | ++i; |
458 | switch (c = fmt[i]) { |
461 | switch (c = fmt[i]) { |
459 | case '#': flags |= __PRINTF_FLAG_PREFIX; break; |
462 | case '#': flags |= __PRINTF_FLAG_PREFIX; break; |
460 | case '-': flags |= __PRINTF_FLAG_LEFTALIGNED; break; |
463 | case '-': flags |= __PRINTF_FLAG_LEFTALIGNED; break; |
461 | case '+': flags |= __PRINTF_FLAG_SHOWPLUS; break; |
464 | case '+': flags |= __PRINTF_FLAG_SHOWPLUS; break; |
462 | case ' ': flags |= __PRINTF_FLAG_SPACESIGN; break; |
465 | case ' ': flags |= __PRINTF_FLAG_SPACESIGN; break; |
463 | case '0': flags |= __PRINTF_FLAG_ZEROPADDED; break; |
466 | case '0': flags |= __PRINTF_FLAG_ZEROPADDED; break; |
464 | default: end = 1; |
467 | default: end = 1; |
465 | }; |
468 | }; |
466 | 469 | ||
467 | } while (end == 0); |
470 | } while (end == 0); |
468 | 471 | ||
469 | /* width & '*' operator */ |
472 | /* width & '*' operator */ |
470 | width = 0; |
473 | width = 0; |
471 | if (isdigit(fmt[i])) { |
474 | if (isdigit(fmt[i])) { |
472 | while (isdigit(fmt[i])) { |
475 | while (isdigit(fmt[i])) { |
473 | width *= 10; |
476 | width *= 10; |
474 | width += fmt[i++] - '0'; |
477 | width += fmt[i++] - '0'; |
475 | } |
478 | } |
476 | } else if (fmt[i] == '*') { |
479 | } else if (fmt[i] == '*') { |
477 | /* get width value from argument list*/ |
480 | /* get width value from argument list*/ |
478 | i++; |
481 | i++; |
479 | width = (int)va_arg(ap, int); |
482 | width = (int)va_arg(ap, int); |
480 | if (width < 0) { |
483 | if (width < 0) { |
481 | /* negative width means to set '-' flag */ |
484 | /* negative width means to set '-' flag */ |
482 | width *= -1; |
485 | width *= -1; |
483 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
486 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
484 | } |
487 | } |
485 | } |
488 | } |
486 | 489 | ||
487 | /* precision and '*' operator */ |
490 | /* precision and '*' operator */ |
488 | precision = 0; |
491 | precision = 0; |
489 | if (fmt[i] == '.') { |
492 | if (fmt[i] == '.') { |
490 | ++i; |
493 | ++i; |
491 | if (isdigit(fmt[i])) { |
494 | if (isdigit(fmt[i])) { |
492 | while (isdigit(fmt[i])) { |
495 | while (isdigit(fmt[i])) { |
493 | precision *= 10; |
496 | precision *= 10; |
494 | precision += fmt[i++] - '0'; |
497 | precision += fmt[i++] - '0'; |
495 | } |
498 | } |
496 | } else if (fmt[i] == '*') { |
499 | } else if (fmt[i] == '*') { |
497 | /* get precision value from argument list*/ |
500 | /* get precision value from argument list*/ |
498 | i++; |
501 | i++; |
499 | precision = (int)va_arg(ap, int); |
502 | precision = (int)va_arg(ap, int); |
500 | if (precision < 0) { |
503 | if (precision < 0) { |
501 | /* negative precision means to ignore it */ |
504 | /* negative precision means to ignore it */ |
502 | precision = 0; |
505 | precision = 0; |
503 | } |
506 | } |
504 | } |
507 | } |
505 | } |
508 | } |
506 | 509 | ||
507 | switch (fmt[i++]) { |
510 | switch (fmt[i++]) { |
508 | /** TODO: unimplemented qualifiers: |
511 | /** TODO: unimplemented qualifiers: |
509 | * t ptrdiff_t - ISO C 99 |
512 | * t ptrdiff_t - ISO C 99 |
510 | */ |
513 | */ |
511 | case 'h': /* char or short */ |
514 | case 'h': /* char or short */ |
512 | qualifier = PrintfQualifierShort; |
515 | qualifier = PrintfQualifierShort; |
513 | if (fmt[i] == 'h') { |
516 | if (fmt[i] == 'h') { |
514 | i++; |
517 | i++; |
515 | qualifier = PrintfQualifierByte; |
518 | qualifier = PrintfQualifierByte; |
516 | } |
519 | } |
517 | break; |
520 | break; |
518 | case 'l': /* long or long long*/ |
521 | case 'l': /* long or long long*/ |
519 | qualifier = PrintfQualifierLong; |
522 | qualifier = PrintfQualifierLong; |
520 | if (fmt[i] == 'l') { |
523 | if (fmt[i] == 'l') { |
521 | i++; |
524 | i++; |
522 | qualifier = PrintfQualifierLongLong; |
525 | qualifier = PrintfQualifierLongLong; |
523 | } |
526 | } |
524 | break; |
527 | break; |
525 | case 'z': /* __native */ |
528 | case 'z': /* __native */ |
526 | qualifier = PrintfQualifierNative; |
529 | qualifier = PrintfQualifierNative; |
527 | break; |
530 | break; |
528 | default: |
531 | default: |
529 | qualifier = PrintfQualifierInt; /* default type */ |
532 | qualifier = PrintfQualifierInt; /* default type */ |
530 | --i; |
533 | --i; |
531 | } |
534 | } |
532 | 535 | ||
533 | base = 10; |
536 | base = 10; |
534 | 537 | ||
535 | switch (c = fmt[i]) { |
538 | switch (c = fmt[i]) { |
536 | 539 | ||
537 | /* |
540 | /* |
538 | * String and character conversions. |
541 | * String and character conversions. |
539 | */ |
542 | */ |
540 | case 's': |
543 | case 's': |
541 | if ((retval = print_string(va_arg(ap, char*), width, precision, flags)) == EOF) { |
544 | if ((retval = print_string(va_arg(ap, char*), width, precision, flags)) == EOF) { |
542 | counter = -counter; |
545 | counter = -counter; |
543 | goto out; |
546 | goto out; |
544 | }; |
547 | }; |
545 | 548 | ||
546 | counter += retval; |
549 | counter += retval; |
547 | j = i + 1; |
550 | j = i + 1; |
548 | goto next_char; |
551 | goto next_char; |
549 | case 'c': |
552 | case 'c': |
550 | c = va_arg(ap, unsigned int); |
553 | c = va_arg(ap, unsigned int); |
551 | if ((retval = print_char(c, width, flags )) == EOF) { |
554 | if ((retval = print_char(c, width, flags )) == EOF) { |
552 | counter = -counter; |
555 | counter = -counter; |
553 | goto out; |
556 | goto out; |
554 | }; |
557 | }; |
555 | 558 | ||
556 | counter += retval; |
559 | counter += retval; |
557 | j = i + 1; |
560 | j = i + 1; |
558 | goto next_char; |
561 | goto next_char; |
559 | 562 | ||
560 | /* |
563 | /* |
561 | * Integer values |
564 | * Integer values |
562 | */ |
565 | */ |
563 | case 'P': /* pointer */ |
566 | case 'P': /* pointer */ |
564 | flags |= __PRINTF_FLAG_BIGCHARS; |
567 | flags |= __PRINTF_FLAG_BIGCHARS; |
565 | case 'p': |
568 | case 'p': |
566 | flags |= __PRINTF_FLAG_PREFIX; |
569 | flags |= __PRINTF_FLAG_PREFIX; |
567 | base = 16; |
570 | base = 16; |
568 | qualifier = PrintfQualifierPointer; |
571 | qualifier = PrintfQualifierPointer; |
569 | break; |
572 | break; |
570 | case 'b': |
573 | case 'b': |
571 | base = 2; |
574 | base = 2; |
572 | break; |
575 | break; |
573 | case 'o': |
576 | case 'o': |
574 | base = 8; |
577 | base = 8; |
575 | break; |
578 | break; |
576 | case 'd': |
579 | case 'd': |
577 | case 'i': |
580 | case 'i': |
578 | flags |= __PRINTF_FLAG_SIGNED; |
581 | flags |= __PRINTF_FLAG_SIGNED; |
579 | case 'u': |
582 | case 'u': |
580 | break; |
583 | break; |
581 | case 'X': |
584 | case 'X': |
582 | flags |= __PRINTF_FLAG_BIGCHARS; |
585 | flags |= __PRINTF_FLAG_BIGCHARS; |
583 | case 'x': |
586 | case 'x': |
584 | base = 16; |
587 | base = 16; |
585 | break; |
588 | break; |
586 | /* percentile itself */ |
589 | /* percentile itself */ |
587 | case '%': |
590 | case '%': |
588 | j = i; |
591 | j = i; |
589 | goto next_char; |
592 | goto next_char; |
590 | /* |
593 | /* |
591 | * Bad formatting. |
594 | * Bad formatting. |
592 | */ |
595 | */ |
593 | default: |
596 | default: |
594 | /* Unknown format |
597 | /* Unknown format |
595 | * now, the j is index of '%' so we will |
598 | * now, the j is index of '%' so we will |
596 | * print whole bad format sequence |
599 | * print whole bad format sequence |
597 | */ |
600 | */ |
598 | goto next_char; |
601 | goto next_char; |
599 | } |
602 | } |
600 | 603 | ||
601 | 604 | ||
602 | /* Print integers */ |
605 | /* Print integers */ |
603 | /* print number */ |
606 | /* print number */ |
604 | switch (qualifier) { |
607 | switch (qualifier) { |
605 | case PrintfQualifierByte: |
608 | case PrintfQualifierByte: |
606 | size = sizeof(unsigned char); |
609 | size = sizeof(unsigned char); |
607 | number = (__u64)va_arg(ap, unsigned int); |
610 | number = (__u64)va_arg(ap, unsigned int); |
608 | break; |
611 | break; |
609 | case PrintfQualifierShort: |
612 | case PrintfQualifierShort: |
610 | size = sizeof(unsigned short); |
613 | size = sizeof(unsigned short); |
611 | number = (__u64)va_arg(ap, unsigned int); |
614 | number = (__u64)va_arg(ap, unsigned int); |
612 | break; |
615 | break; |
613 | case PrintfQualifierInt: |
616 | case PrintfQualifierInt: |
614 | size = sizeof(unsigned int); |
617 | size = sizeof(unsigned int); |
615 | number = (__u64)va_arg(ap, unsigned int); |
618 | number = (__u64)va_arg(ap, unsigned int); |
616 | break; |
619 | break; |
617 | case PrintfQualifierLong: |
620 | case PrintfQualifierLong: |
618 | size = sizeof(unsigned long); |
621 | size = sizeof(unsigned long); |
619 | number = (__u64)va_arg(ap, unsigned long); |
622 | number = (__u64)va_arg(ap, unsigned long); |
620 | break; |
623 | break; |
621 | case PrintfQualifierLongLong: |
624 | case PrintfQualifierLongLong: |
622 | size = sizeof(unsigned long long); |
625 | size = sizeof(unsigned long long); |
623 | number = (__u64)va_arg(ap, unsigned long long); |
626 | number = (__u64)va_arg(ap, unsigned long long); |
624 | break; |
627 | break; |
625 | case PrintfQualifierPointer: |
628 | case PrintfQualifierPointer: |
626 | size = sizeof(void *); |
629 | size = sizeof(void *); |
627 | number = (__u64)(unsigned long)va_arg(ap, void *); |
630 | number = (__u64)(unsigned long)va_arg(ap, void *); |
628 | break; |
631 | break; |
629 | case PrintfQualifierNative: |
632 | case PrintfQualifierNative: |
630 | size = sizeof(__native); |
633 | size = sizeof(__native); |
631 | number = (__u64)va_arg(ap, __native); |
634 | number = (__u64)va_arg(ap, __native); |
632 | break; |
635 | break; |
633 | default: /* Unknown qualifier */ |
636 | default: /* Unknown qualifier */ |
634 | counter = -counter; |
637 | counter = -counter; |
635 | goto out; |
638 | goto out; |
636 | 639 | ||
637 | } |
640 | } |
638 | 641 | ||
639 | if (flags & __PRINTF_FLAG_SIGNED) { |
642 | if (flags & __PRINTF_FLAG_SIGNED) { |
640 | if (number & (0x1 << (size*8 - 1))) { |
643 | if (number & (0x1 << (size*8 - 1))) { |
641 | flags |= __PRINTF_FLAG_NEGATIVE; |
644 | flags |= __PRINTF_FLAG_NEGATIVE; |
642 | 645 | ||
643 | if (size == sizeof(__u64)) { |
646 | if (size == sizeof(__u64)) { |
644 | number = -((__s64)number); |
647 | number = -((__s64)number); |
645 | } else { |
648 | } else { |
646 | number = ~number; |
649 | number = ~number; |
647 | number &= (~((0xFFFFFFFFFFFFFFFFll) << (size * 8))); |
650 | number &= (~((0xFFFFFFFFFFFFFFFFll) << (size * 8))); |
648 | number++; |
651 | number++; |
649 | } |
652 | } |
650 | } |
653 | } |
651 | } |
654 | } |
652 | 655 | ||
653 | if ((retval = print_number(number, width, precision, base, flags)) == EOF ) { |
656 | if ((retval = print_number(number, width, precision, base, flags)) == EOF ) { |
654 | counter = -counter; |
657 | counter = -counter; |
655 | goto out; |
658 | goto out; |
656 | }; |
659 | }; |
657 | 660 | ||
658 | counter += retval; |
661 | counter += retval; |
659 | j = i + 1; |
662 | j = i + 1; |
660 | } |
663 | } |
661 | next_char: |
664 | next_char: |
662 | 665 | ||
663 | ++i; |
666 | ++i; |
664 | } |
667 | } |
665 | 668 | ||
666 | if (i > j) { |
669 | if (i > j) { |
667 | if ((retval = putnchars(&fmt[j], (__native)(i - j))) == EOF) { /* error */ |
670 | if ((retval = putnchars(&fmt[j], (__native)(i - j))) == EOF) { /* error */ |
668 | counter = -counter; |
671 | counter = -counter; |
669 | goto out; |
672 | goto out; |
670 | } |
673 | } |
671 | counter += retval; |
674 | counter += retval; |
672 | } |
675 | } |
673 | out: |
676 | out: |
674 | spinlock_unlock(&printflock); |
677 | spinlock_unlock(&printflock); |
675 | interrupts_restore(irqpri); |
678 | interrupts_restore(irqpri); |
676 | 679 | ||
677 | va_end(ap); |
680 | va_end(ap); |
678 | return counter; |
681 | return counter; |
679 | } |
682 | } |
680 | 683 |