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