Rev 3424 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3424 | Rev 4377 | ||
---|---|---|---|
Line 1... | Line 1... | ||
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 | * Copyright (c) 2009 Martin Decky |
|
4 | * All rights reserved. |
5 | * All rights reserved. |
5 | * |
6 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * are met: |
Line 25... | Line 26... | ||
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * (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. |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
29 | */ |
29 | 30 | ||
30 | /** @addtogroup generic |
31 | /** @addtogroup generic |
31 | * @{ |
32 | * @{ |
32 | */ |
33 | */ |
33 | /** |
34 | /** |
34 | * @file |
35 | * @file |
35 | * @brief Printing functions. |
36 | * @brief Printing functions. |
36 | */ |
37 | */ |
37 | 38 | ||
38 | #include <printf/printf_core.h> |
39 | #include <printf/printf_core.h> |
39 | #include <putchar.h> |
- | |
40 | #include <print.h> |
40 | #include <print.h> |
41 | #include <arch/arg.h> |
41 | #include <arch/arg.h> |
42 | #include <macros.h> |
42 | #include <macros.h> |
43 | #include <func.h> |
43 | #include <string.h> |
44 | #include <arch.h> |
44 | #include <arch.h> |
45 | 45 | ||
46 | /** show prefixes 0x or 0 */ |
46 | /** show prefixes 0x or 0 */ |
47 | #define __PRINTF_FLAG_PREFIX 0x00000001 |
47 | #define __PRINTF_FLAG_PREFIX 0x00000001 |
48 | /** signed / unsigned number */ |
48 | /** signed / unsigned number */ |
49 | #define __PRINTF_FLAG_SIGNED 0x00000002 |
49 | #define __PRINTF_FLAG_SIGNED 0x00000002 |
50 | /** print leading zeroes */ |
50 | /** print leading zeroes */ |
51 | #define __PRINTF_FLAG_ZEROPADDED 0x00000004 |
51 | #define __PRINTF_FLAG_ZEROPADDED 0x00000004 |
52 | /** align to left */ |
52 | /** align to left */ |
53 | #define __PRINTF_FLAG_LEFTALIGNED 0x00000010 |
53 | #define __PRINTF_FLAG_LEFTALIGNED 0x00000010 |
54 | /** always show + sign */ |
54 | /** always show + sign */ |
55 | #define __PRINTF_FLAG_SHOWPLUS 0x00000020 |
55 | #define __PRINTF_FLAG_SHOWPLUS 0x00000020 |
56 | /** print space instead of plus */ |
56 | /** print space instead of plus */ |
57 | #define __PRINTF_FLAG_SPACESIGN 0x00000040 |
57 | #define __PRINTF_FLAG_SPACESIGN 0x00000040 |
58 | /** show big characters */ |
58 | /** show big characters */ |
59 | #define __PRINTF_FLAG_BIGCHARS 0x00000080 |
59 | #define __PRINTF_FLAG_BIGCHARS 0x00000080 |
60 | /** number has - sign */ |
60 | /** number has - sign */ |
61 | #define __PRINTF_FLAG_NEGATIVE 0x00000100 |
61 | #define __PRINTF_FLAG_NEGATIVE 0x00000100 |
62 | 62 | ||
63 | /** |
63 | /** |
64 | * Buffer big enough for 64-bit number printed in base 2, sign, prefix and 0 |
64 | * Buffer big enough for 64-bit number printed in base 2, sign, prefix and 0 |
65 | * to terminate string... (last one is only for better testing end of buffer by |
65 | * to terminate string... (last one is only for better testing end of buffer by |
66 | * zero-filling subroutine) |
66 | * zero-filling subroutine) |
67 | */ |
67 | */ |
68 | #define PRINT_NUMBER_BUFFER_SIZE (64 + 5) |
68 | #define PRINT_NUMBER_BUFFER_SIZE (64 + 5) |
69 | 69 | ||
70 | /** Enumeration of possible arguments types. |
70 | /** Enumeration of possible arguments types. |
71 | */ |
71 | */ |
72 | typedef enum { |
72 | typedef enum { |
73 | PrintfQualifierByte = 0, |
73 | PrintfQualifierByte = 0, |
Line 76... | Line 76... | ||
76 | PrintfQualifierLong, |
76 | PrintfQualifierLong, |
77 | PrintfQualifierLongLong, |
77 | PrintfQualifierLongLong, |
78 | PrintfQualifierPointer |
78 | PrintfQualifierPointer |
79 | } qualifier_t; |
79 | } qualifier_t; |
80 | 80 | ||
- | 81 | static char nullstr[] = "(NULL)"; |
|
81 | static char digits_small[] = "0123456789abcdef"; |
82 | static char digits_small[] = "0123456789abcdef"; |
82 | static char digits_big[] = "0123456789ABCDEF"; |
83 | static char digits_big[] = "0123456789ABCDEF"; |
- | 84 | static char invalch = U_SPECIAL; |
|
83 | 85 | ||
84 | /** Print one or more characters without adding newline. |
86 | /** Print one or more characters without adding newline. |
85 | * |
87 | * |
86 | * @param buf Buffer with size at least count bytes. NULL pointer is |
88 | * @param buf Buffer holding characters with size of |
87 | * not allowed! |
89 | * at least size bytes. NULL is not allowed! |
88 | * @param count Number of characters to print. |
90 | * @param size Size of the buffer in bytes. |
89 | * @param ps Output method and its data. |
91 | * @param ps Output method and its data. |
- | 92 | * |
|
90 | * @return Number of characters printed. |
93 | * @return Number of characters printed. |
- | 94 | * |
|
91 | */ |
95 | */ |
92 | static int printf_putnchars(const char * buf, size_t count, |
96 | static int printf_putnchars(const char *buf, size_t size, |
93 | struct printf_spec *ps) |
97 | printf_spec_t *ps) |
94 | { |
98 | { |
95 | return ps->write((void *) buf, count, ps->data); |
99 | return ps->str_write((void *) buf, size, ps->data); |
96 | } |
100 | } |
97 | 101 | ||
98 | /** Print a string without adding a newline. |
102 | /** Print one or more wide characters without adding newline. |
- | 103 | * |
|
- | 104 | * @param buf Buffer holding wide characters with size of |
|
- | 105 | * at least size bytes. NULL is not allowed! |
|
- | 106 | * @param size Size of the buffer in bytes. |
|
- | 107 | * @param ps Output method and its data. |
|
- | 108 | * |
|
- | 109 | * @return Number of wide characters printed. |
|
99 | * |
110 | * |
100 | * @param str String to print. |
- | |
101 | * @param ps Write function specification and support data. |
- | |
102 | * @return Number of characters printed. |
- | |
103 | */ |
111 | */ |
104 | static int printf_putstr(const char * str, struct printf_spec *ps) |
112 | static int printf_wputnchars(const wchar_t *buf, size_t size, |
- | 113 | printf_spec_t *ps) |
|
105 | { |
114 | { |
106 | size_t count; |
- | |
107 | - | ||
108 | if (str == NULL) { |
- | |
109 | char *nullstr = "(NULL)"; |
- | |
110 | return printf_putnchars(nullstr, strlen(nullstr), ps); |
115 | return ps->wstr_write((void *) buf, size, ps->data); |
111 | } |
116 | } |
112 | - | ||
113 | count = strlen(str); |
- | |
114 | 117 | ||
- | 118 | /** Print string without adding a newline. |
|
- | 119 | * |
|
- | 120 | * @param str String to print. |
|
- | 121 | * @param ps Write function specification and support data. |
|
- | 122 | * |
|
- | 123 | * @return Number of characters printed. |
|
- | 124 | * |
|
- | 125 | */ |
|
- | 126 | static int printf_putstr(const char *str, printf_spec_t *ps) |
|
- | 127 | { |
|
- | 128 | if (str == NULL) |
|
- | 129 | return printf_putnchars(nullstr, str_size(nullstr), ps); |
|
- | 130 | ||
115 | return ps->write((void *) str, count, ps->data); |
131 | return ps->str_write((void *) str, str_size(str), ps->data); |
116 | } |
132 | } |
117 | 133 | ||
118 | /** Print one character. |
134 | /** Print one ASCII character. |
119 | * |
135 | * |
120 | * @param c Character to be printed. |
136 | * @param c ASCII character to be printed. |
121 | * @param ps Output method. |
137 | * @param ps Output method. |
- | 138 | * |
|
- | 139 | * @return Number of characters printed. |
|
122 | * |
140 | * |
123 | * @return Number of characters printed. |
- | |
124 | */ |
141 | */ |
125 | static int printf_putchar(int c, struct printf_spec *ps) |
142 | static int printf_putchar(const char ch, printf_spec_t *ps) |
126 | { |
143 | { |
127 | unsigned char ch = c; |
144 | if (!ascii_check(ch)) |
- | 145 | return ps->str_write((void *) &invalch, 1, ps->data); |
|
128 | 146 | ||
129 | return ps->write((void *) &ch, 1, ps->data); |
147 | return ps->str_write(&ch, 1, ps->data); |
130 | } |
148 | } |
131 | 149 | ||
132 | /** Print one formatted character. |
150 | /** Print one wide character. |
- | 151 | * |
|
- | 152 | * @param c Wide character to be printed. |
|
- | 153 | * @param ps Output method. |
|
133 | * |
154 | * |
134 | * @param c Character to print. |
155 | * @return Number of characters printed. |
135 | * @param width Width modifier. |
- | |
136 | * @param flags Flags that change the way the character is printed. |
- | |
137 | * |
156 | * |
138 | * @return Number of characters printed, negative value on failure. |
- | |
139 | */ |
157 | */ |
140 | static int print_char(char c, int width, uint64_t flags, struct printf_spec *ps) |
158 | static int printf_putwchar(const wchar_t ch, printf_spec_t *ps) |
141 | { |
159 | { |
142 | int counter = 0; |
160 | if (!chr_check(ch)) |
- | 161 | return ps->str_write((void *) &invalch, 1, ps->data); |
|
143 | 162 | ||
- | 163 | return ps->wstr_write(&ch, sizeof(wchar_t), ps->data); |
|
- | 164 | } |
|
- | 165 | ||
- | 166 | /** Print one formatted ASCII character. |
|
- | 167 | * |
|
- | 168 | * @param ch Character to print. |
|
- | 169 | * @param width Width modifier. |
|
- | 170 | * @param flags Flags that change the way the character is printed. |
|
- | 171 | * |
|
- | 172 | * @return Number of characters printed, negative value on failure. |
|
- | 173 | * |
|
- | 174 | */ |
|
- | 175 | static int print_char(const char ch, int width, uint32_t flags, printf_spec_t *ps) |
|
- | 176 | { |
|
- | 177 | count_t counter = 0; |
|
144 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
178 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
145 | while (--width > 0) { |
179 | while (--width > 0) { |
146 | /* |
180 | /* |
147 | * One space is consumed by the character itself, hence |
181 | * One space is consumed by the character itself, hence |
148 | * the predecrement. |
182 | * the predecrement. |
149 | */ |
183 | */ |
150 | if (printf_putchar(' ', ps) > 0) |
184 | if (printf_putchar(' ', ps) > 0) |
151 | ++counter; |
185 | counter++; |
152 | } |
186 | } |
153 | } |
187 | } |
154 | 188 | ||
155 | if (printf_putchar(c, ps) > 0) |
189 | if (printf_putchar(ch, ps) > 0) |
156 | counter++; |
190 | counter++; |
157 | 191 | ||
158 | while (--width > 0) { |
192 | while (--width > 0) { |
- | 193 | /* |
|
- | 194 | * One space is consumed by the character itself, hence |
|
- | 195 | * the predecrement. |
|
- | 196 | */ |
|
- | 197 | if (printf_putchar(' ', ps) > 0) |
|
- | 198 | counter++; |
|
- | 199 | } |
|
- | 200 | ||
- | 201 | return (int) (counter + 1); |
|
- | 202 | } |
|
- | 203 | ||
- | 204 | /** Print one formatted wide character. |
|
- | 205 | * |
|
- | 206 | * @param ch Character to print. |
|
- | 207 | * @param width Width modifier. |
|
- | 208 | * @param flags Flags that change the way the character is printed. |
|
- | 209 | * |
|
- | 210 | * @return Number of characters printed, negative value on failure. |
|
- | 211 | * |
|
- | 212 | */ |
|
- | 213 | static int print_wchar(const wchar_t ch, int width, uint32_t flags, printf_spec_t *ps) |
|
- | 214 | { |
|
- | 215 | count_t counter = 0; |
|
- | 216 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
|
- | 217 | while (--width > 0) { |
|
159 | /* |
218 | /* |
160 | * One space is consumed by the character itself, hence |
219 | * One space is consumed by the character itself, hence |
161 | * the predecrement. |
220 | * the predecrement. |
162 | */ |
221 | */ |
- | 222 | if (printf_putchar(' ', ps) > 0) |
|
- | 223 | counter++; |
|
- | 224 | } |
|
- | 225 | } |
|
- | 226 | ||
- | 227 | if (printf_putwchar(ch, ps) > 0) |
|
- | 228 | counter++; |
|
- | 229 | ||
- | 230 | while (--width > 0) { |
|
- | 231 | /* |
|
- | 232 | * One space is consumed by the character itself, hence |
|
- | 233 | * the predecrement. |
|
- | 234 | */ |
|
163 | if (printf_putchar(' ', ps) > 0) |
235 | if (printf_putchar(' ', ps) > 0) |
164 | ++counter; |
236 | counter++; |
165 | } |
237 | } |
166 | 238 | ||
167 | return ++counter; |
239 | return (int) (counter + 1); |
168 | } |
240 | } |
169 | 241 | ||
170 | /** Print string. |
242 | /** Print string. |
171 | * |
243 | * |
172 | * @param s String to be printed. |
244 | * @param str String to be printed. |
173 | * @param width Width modifier. |
245 | * @param width Width modifier. |
174 | * @param precision Precision modifier. |
246 | * @param precision Precision modifier. |
175 | * @param flags Flags that modify the way the string is printed. |
247 | * @param flags Flags that modify the way the string is printed. |
176 | * |
248 | * |
177 | * @return Number of characters printed, negative value on failure. |
249 | * @return Number of characters printed, negative value on failure. |
178 | */ |
250 | */ |
179 | static int print_string(char *s, int width, unsigned int precision, |
251 | static int print_str(char *str, int width, unsigned int precision, |
180 | uint64_t flags, struct printf_spec *ps) |
252 | uint32_t flags, printf_spec_t *ps) |
181 | { |
253 | { |
- | 254 | if (str == NULL) |
|
- | 255 | return printf_putstr(nullstr, ps); |
|
- | 256 | ||
- | 257 | /* Print leading spaces. */ |
|
- | 258 | count_t strw = str_length(str); |
|
- | 259 | if (precision == 0) |
|
- | 260 | precision = strw; |
|
- | 261 | ||
- | 262 | /* Left padding */ |
|
182 | int counter = 0; |
263 | count_t counter = 0; |
183 | size_t size; |
264 | width -= precision; |
- | 265 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
|
- | 266 | while (width-- > 0) { |
|
- | 267 | if (printf_putchar(' ', ps) == 1) |
|
- | 268 | counter++; |
|
- | 269 | } |
|
- | 270 | } |
|
- | 271 | ||
- | 272 | /* Part of @a str fitting into the alloted space. */ |
|
184 | int retval; |
273 | int retval; |
- | 274 | size_t size = str_lsize(str, precision); |
|
- | 275 | if ((retval = printf_putnchars(str, size, ps)) < 0) |
|
- | 276 | return -counter; |
|
185 | 277 | ||
- | 278 | counter += retval; |
|
- | 279 | ||
- | 280 | /* Right padding */ |
|
186 | if (s == NULL) { |
281 | while (width-- > 0) { |
187 | return printf_putstr("(NULL)", ps); |
282 | if (printf_putchar(' ', ps) == 1) |
- | 283 | counter++; |
|
188 | } |
284 | } |
189 | - | ||
190 | size = strlen(s); |
- | |
191 | 285 | ||
192 | /* print leading spaces */ |
286 | return ((int) counter); |
193 | 287 | ||
194 | if (precision == 0) |
- | |
195 | precision = size; |
288 | } |
196 | 289 | ||
197 | width -= precision; |
290 | /** Print wide string. |
- | 291 | * |
|
- | 292 | * @param str Wide string to be printed. |
|
- | 293 | * @param width Width modifier. |
|
- | 294 | * @param precision Precision modifier. |
|
- | 295 | * @param flags Flags that modify the way the string is printed. |
|
- | 296 | * |
|
- | 297 | * @return Number of wide characters printed, negative value on failure. |
|
- | 298 | */ |
|
- | 299 | static int print_wstr(wchar_t *str, int width, unsigned int precision, |
|
- | 300 | uint32_t flags, printf_spec_t *ps) |
|
- | 301 | { |
|
- | 302 | if (str == NULL) |
|
- | 303 | return printf_putstr(nullstr, ps); |
|
- | 304 | ||
- | 305 | if (*str == U_BOM) |
|
- | 306 | str++; |
|
198 | 307 | ||
- | 308 | /* Print leading spaces. */ |
|
- | 309 | size_t strw = wstr_length(str); |
|
- | 310 | if (precision == 0) |
|
- | 311 | precision = strw; |
|
- | 312 | ||
- | 313 | /* Left padding */ |
|
- | 314 | count_t counter = 0; |
|
- | 315 | width -= precision; |
|
199 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
316 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
200 | while (width-- > 0) { |
317 | while (width-- > 0) { |
201 | if (printf_putchar(' ', ps) == 1) |
318 | if (printf_putchar(' ', ps) == 1) |
202 | counter++; |
319 | counter++; |
203 | } |
320 | } |
204 | } |
321 | } |
205 | 322 | ||
- | 323 | /* Part of @a wstr fitting into the alloted space. */ |
|
- | 324 | int retval; |
|
- | 325 | size_t size = wstr_lsize(str, precision); |
|
206 | if ((retval = printf_putnchars(s, min(size, precision), ps)) < 0) { |
326 | if ((retval = printf_wputnchars(str, size, ps)) < 0) |
207 | return -counter; |
327 | return -counter; |
208 | } |
328 | |
209 | counter += retval; |
329 | counter += retval; |
210 | 330 | ||
- | 331 | /* Right padding */ |
|
211 | while (width-- > 0) { |
332 | while (width-- > 0) { |
212 | if (printf_putchar(' ', ps) == 1) |
333 | if (printf_putchar(' ', ps) == 1) |
213 | ++counter; |
334 | counter++; |
214 | } |
335 | } |
215 | - | ||
216 | return counter; |
- | |
217 | } |
- | |
218 | 336 | ||
- | 337 | return ((int) counter); |
|
- | 338 | } |
|
219 | 339 | ||
220 | /** Print a number in a given base. |
340 | /** Print a number in a given base. |
221 | * |
341 | * |
222 | * Print significant digits of a number in given base. |
342 | * Print significant digits of a number in given base. |
223 | * |
343 | * |
224 | * @param num Number to print. |
344 | * @param num Number to print. |
225 | * @param widt Width modifier.h |
345 | * @param width Width modifier. |
226 | * @param precision Precision modifier. |
346 | * @param precision Precision modifier. |
227 | * @param base Base to print the number in (must be between 2 and 16). |
347 | * @param base Base to print the number in (must be between 2 and 16). |
228 | * @param flags Flags that modify the way the number is printed. |
348 | * @param flags Flags that modify the way the number is printed. |
229 | * |
349 | * |
230 | * @return Number of characters printed. |
350 | * @return Number of characters printed. |
231 | * |
351 | * |
232 | */ |
352 | */ |
233 | static int print_number(uint64_t num, int width, int precision, int base, |
353 | static int print_number(uint64_t num, int width, int precision, int base, |
234 | uint64_t flags, struct printf_spec *ps) |
354 | uint32_t flags, printf_spec_t *ps) |
235 | { |
355 | { |
236 | char *digits = digits_small; |
356 | char *digits; |
237 | char d[PRINT_NUMBER_BUFFER_SIZE]; |
- | |
238 | char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1]; |
357 | if (flags & __PRINTF_FLAG_BIGCHARS) |
239 | int size = 0; /* size of number with all prefixes and signs */ |
- | |
240 | int number_size; /* size of plain number */ |
358 | digits = digits_big; |
241 | char sgn; |
359 | else |
242 | int retval; |
- | |
243 | int counter = 0; |
360 | digits = digits_small; |
244 | 361 | ||
245 | if (flags & __PRINTF_FLAG_BIGCHARS) |
362 | char data[PRINT_NUMBER_BUFFER_SIZE]; |
- | 363 | char *ptr = &data[PRINT_NUMBER_BUFFER_SIZE - 1]; |
|
- | 364 | ||
- | 365 | /* Size of number with all prefixes and signs */ |
|
- | 366 | int size = 0; |
|
- | 367 | ||
246 | digits = digits_big; |
368 | /* Put zero at end of string */ |
- | 369 | *ptr-- = 0; |
|
247 | 370 | ||
248 | *ptr-- = 0; /* Put zero at end of string */ |
- | |
249 | - | ||
250 | if (num == 0) { |
371 | if (num == 0) { |
251 | *ptr-- = '0'; |
372 | *ptr-- = '0'; |
252 | size++; |
373 | size++; |
253 | } else { |
374 | } else { |
254 | do { |
375 | do { |
255 | *ptr-- = digits[num % base]; |
376 | *ptr-- = digits[num % base]; |
256 | size++; |
377 | size++; |
257 | } while (num /= base); |
378 | } while (num /= base); |
258 | } |
379 | } |
259 | 380 | ||
- | 381 | /* Size of plain number */ |
|
260 | number_size = size; |
382 | int number_size = size; |
261 | 383 | ||
262 | /* |
384 | /* |
263 | * Collect the sum of all prefixes/signs/... to calculate padding and |
385 | * Collect the sum of all prefixes/signs/etc. to calculate padding and |
264 | * leading zeroes. |
386 | * leading zeroes. |
265 | */ |
387 | */ |
266 | if (flags & __PRINTF_FLAG_PREFIX) { |
388 | if (flags & __PRINTF_FLAG_PREFIX) { |
267 | switch(base) { |
389 | switch(base) { |
- | 390 | case 2: |
|
268 | case 2: /* Binary formating is not standard, but usefull */ |
391 | /* Binary formating is not standard, but usefull */ |
269 | size += 2; |
392 | size += 2; |
270 | break; |
393 | break; |
271 | case 8: |
394 | case 8: |
272 | size++; |
395 | size++; |
273 | break; |
396 | break; |
274 | case 16: |
397 | case 16: |
275 | size += 2; |
398 | size += 2; |
276 | break; |
399 | break; |
277 | } |
400 | } |
278 | } |
401 | } |
279 | 402 | ||
280 | sgn = 0; |
403 | char sgn = 0; |
281 | if (flags & __PRINTF_FLAG_SIGNED) { |
404 | if (flags & __PRINTF_FLAG_SIGNED) { |
282 | if (flags & __PRINTF_FLAG_NEGATIVE) { |
405 | if (flags & __PRINTF_FLAG_NEGATIVE) { |
283 | sgn = '-'; |
406 | sgn = '-'; |
284 | size++; |
407 | size++; |
285 | } else if (flags & __PRINTF_FLAG_SHOWPLUS) { |
408 | } else if (flags & __PRINTF_FLAG_SHOWPLUS) { |
Line 288... | Line 411... | ||
288 | } else if (flags & __PRINTF_FLAG_SPACESIGN) { |
411 | } else if (flags & __PRINTF_FLAG_SPACESIGN) { |
289 | sgn = ' '; |
412 | sgn = ' '; |
290 | size++; |
413 | size++; |
291 | } |
414 | } |
292 | } |
415 | } |
293 | 416 | ||
294 | if (flags & __PRINTF_FLAG_LEFTALIGNED) { |
417 | if (flags & __PRINTF_FLAG_LEFTALIGNED) |
295 | flags &= ~__PRINTF_FLAG_ZEROPADDED; |
418 | flags &= ~__PRINTF_FLAG_ZEROPADDED; |
296 | } |
419 | |
297 | - | ||
298 | /* |
420 | /* |
299 | * If the number is leftaligned or precision is specified then |
421 | * If the number is left-aligned or precision is specified then |
300 | * zeropadding is ignored. |
422 | * padding with zeros is ignored. |
301 | */ |
423 | */ |
302 | if (flags & __PRINTF_FLAG_ZEROPADDED) { |
424 | if (flags & __PRINTF_FLAG_ZEROPADDED) { |
303 | if ((precision == 0) && (width > size)) { |
425 | if ((precision == 0) && (width > size)) |
304 | precision = width - size + number_size; |
426 | precision = width - size + number_size; |
305 | } |
- | |
306 | } |
427 | } |
307 | 428 | ||
308 | /* print leading spaces */ |
429 | /* Print leading spaces */ |
309 | if (number_size > precision) { |
430 | if (number_size > precision) { |
310 | /* print the whole number not only a part */ |
431 | /* Print the whole number, not only a part */ |
311 | precision = number_size; |
432 | precision = number_size; |
312 | } |
433 | } |
313 | 434 | ||
314 | width -= precision + size - number_size; |
435 | width -= precision + size - number_size; |
- | 436 | count_t counter = 0; |
|
315 | 437 | ||
316 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
438 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
317 | while (width-- > 0) { |
439 | while (width-- > 0) { |
318 | if (printf_putchar(' ', ps) == 1) |
440 | if (printf_putchar(' ', ps) == 1) |
319 | counter++; |
441 | counter++; |
320 | } |
442 | } |
321 | } |
443 | } |
322 | 444 | ||
323 | - | ||
324 | /* print sign */ |
445 | /* Print sign */ |
325 | if (sgn) { |
446 | if (sgn) { |
326 | if (printf_putchar(sgn, ps) == 1) |
447 | if (printf_putchar(sgn, ps) == 1) |
327 | counter++; |
448 | counter++; |
328 | } |
449 | } |
329 | 450 | ||
330 | /* print prefix */ |
451 | /* Print prefix */ |
331 | - | ||
332 | if (flags & __PRINTF_FLAG_PREFIX) { |
452 | if (flags & __PRINTF_FLAG_PREFIX) { |
333 | switch(base) { |
453 | switch(base) { |
- | 454 | case 2: |
|
334 | case 2: /* Binary formating is not standard, but usefull */ |
455 | /* Binary formating is not standard, but usefull */ |
335 | if (printf_putchar('0', ps) == 1) |
456 | if (printf_putchar('0', ps) == 1) |
336 | counter++; |
457 | counter++; |
337 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
458 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
338 | if (printf_putchar('B', ps) == 1) |
459 | if (printf_putchar('B', ps) == 1) |
339 | counter++; |
460 | counter++; |
Line 357... | Line 478... | ||
357 | counter++; |
478 | counter++; |
358 | } |
479 | } |
359 | break; |
480 | break; |
360 | } |
481 | } |
361 | } |
482 | } |
362 | 483 | ||
363 | /* print leading zeroes */ |
484 | /* Print leading zeroes */ |
364 | precision -= number_size; |
485 | precision -= number_size; |
365 | while (precision-- > 0) { |
486 | while (precision-- > 0) { |
366 | if (printf_putchar('0', ps) == 1) |
487 | if (printf_putchar('0', ps) == 1) |
367 | counter++; |
488 | counter++; |
368 | } |
489 | } |
369 | - | ||
370 | 490 | ||
371 | /* print number itself */ |
491 | /* Print the number itself */ |
372 | 492 | int retval; |
|
373 | if ((retval = printf_putstr(++ptr, ps)) > 0) { |
493 | if ((retval = printf_putstr(++ptr, ps)) > 0) |
374 | counter += retval; |
494 | counter += retval; |
375 | } |
- | |
376 | 495 | ||
377 | /* print ending spaces */ |
496 | /* Print tailing spaces */ |
378 | 497 | ||
379 | while (width-- > 0) { |
498 | while (width-- > 0) { |
380 | if (printf_putchar(' ', ps) == 1) |
499 | if (printf_putchar(' ', ps) == 1) |
381 | counter++; |
500 | counter++; |
382 | } |
501 | } |
383 | 502 | ||
384 | return counter; |
503 | return ((int) counter); |
385 | } |
504 | } |
386 | 505 | ||
387 | - | ||
388 | /** Print formatted string. |
506 | /** Print formatted string. |
389 | * |
507 | * |
390 | * Print string formatted according to the fmt parameter and variadic arguments. |
508 | * Print string formatted according to the fmt parameter and variadic arguments. |
391 | * Each formatting directive must have the following form: |
509 | * Each formatting directive must have the following form: |
392 | * |
510 | * |
393 | * \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION |
511 | * \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION |
394 | * |
512 | * |
395 | * FLAGS:@n |
513 | * FLAGS:@n |
396 | * - "#" Force to print prefix.For \%o conversion, the prefix is 0, for |
514 | * - "#" Force to print prefix. For \%o conversion, the prefix is 0, for |
397 | * \%x and \%X prefixes are 0x and 0X and for conversion \%b the |
515 | * \%x and \%X prefixes are 0x and 0X and for conversion \%b the |
398 | * prefix is 0b. |
516 | * prefix is 0b. |
- | 517 | * |
|
- | 518 | * - "-" Align to left. |
|
399 | * |
519 | * |
400 | * - "-" Align to left. |
520 | * - "+" Print positive sign just as negative. |
401 | * |
521 | * |
- | 522 | * - " " If the printed number is positive and "+" flag is not set, |
|
402 | * - "+" Print positive sign just as negative. |
523 | * print space in place of sign. |
403 | * |
524 | * |
404 | * - " " If the printed number is positive and "+" flag is not set, |
525 | * - "0" Print 0 as padding instead of spaces. Zeroes are placed between |
- | 526 | * sign and the rest of the number. This flag is ignored if "-" |
|
405 | * print space in place of sign. |
527 | * flag is specified. |
406 | * |
528 | * |
407 | * - "0" Print 0 as padding instead of spaces. Zeroes are placed between |
- | |
408 | * sign and the rest of the number. This flag is ignored if "-" |
- | |
409 | * flag is specified. |
- | |
410 | * |
- | |
411 | * WIDTH:@n |
529 | * WIDTH:@n |
412 | * - Specify the minimal width of a printed argument. If it is bigger, |
530 | * - Specify the minimal width of a printed argument. If it is bigger, |
413 | * width is ignored. If width is specified with a "*" character instead of |
531 | * width is ignored. If width is specified with a "*" character instead of |
414 | * number, width is taken from parameter list. And integer parameter is |
532 | * number, width is taken from parameter list. And integer parameter is |
415 | * expected before parameter for processed conversion specification. If |
533 | * expected before parameter for processed conversion specification. If |
416 | * this value is negative its absolute value is taken and the "-" flag is |
534 | * this value is negative its absolute value is taken and the "-" flag is |
417 | * set. |
535 | * set. |
418 | * |
536 | * |
419 | * PRECISION:@n |
537 | * PRECISION:@n |
420 | * - Value precision. For numbers it specifies minimum valid numbers. |
538 | * - Value precision. For numbers it specifies minimum valid numbers. |
421 | * Smaller numbers are printed with leading zeroes. Bigger numbers are not |
539 | * Smaller numbers are printed with leading zeroes. Bigger numbers are not |
422 | * affected. Strings with more than precision characters are cut off. Just |
540 | * affected. Strings with more than precision characters are cut off. Just |
423 | * as with width, an "*" can be used used instead of a number. An integer |
541 | * as with width, an "*" can be used used instead of a number. An integer |
424 | * value is then expected in parameters. When both width and precision are |
542 | * value is then expected in parameters. When both width and precision are |
425 | * specified using "*", the first parameter is used for width and the |
543 | * specified using "*", the first parameter is used for width and the |
426 | * second one for precision. |
544 | * second one for precision. |
427 | * |
545 | * |
428 | * TYPE:@n |
546 | * TYPE:@n |
429 | * - "hh" Signed or unsigned char.@n |
547 | * - "hh" Signed or unsigned char.@n |
430 | * - "h" Signed or unsigned short.@n |
548 | * - "h" Signed or unsigned short.@n |
431 | * - "" Signed or unsigned int (default value).@n |
549 | * - "" Signed or unsigned int (default value).@n |
432 | * - "l" Signed or unsigned long int.@n |
550 | * - "l" Signed or unsigned long int.@n |
- | 551 | * If conversion is "c", the character is wchar_t (wide character).@n |
|
- | 552 | * If conversion is "s", the string is wchar_t * (wide string).@n |
|
433 | * - "ll" Signed or unsigned long long int.@n |
553 | * - "ll" Signed or unsigned long long int.@n |
434 | * |
- | |
435 | * |
554 | * |
436 | * CONVERSION:@n |
555 | * CONVERSION:@n |
437 | * - % Print percentile character itself. |
556 | * - % Print percentile character itself. |
- | 557 | * |
|
- | 558 | * - c Print single character. The character is expected to be plain |
|
- | 559 | * ASCII (e.g. only values 0 .. 127 are valid).@n |
|
- | 560 | * If type is "l", then the character is expected to be wide character |
|
- | 561 | * (e.g. values 0 .. 0x10ffff are valid). |
|
- | 562 | * |
|
- | 563 | * - s Print zero terminated string. If a NULL value is passed as |
|
- | 564 | * value, "(NULL)" is printed instead.@n |
|
- | 565 | * If type is "l", then the string is expected to be wide string. |
|
- | 566 | * |
|
- | 567 | * - P, p Print value of a pointer. Void * value is expected and it is |
|
- | 568 | * printed in hexadecimal notation with prefix (as with \%#X / \%#x |
|
- | 569 | * for 32-bit or \%#X / \%#x for 64-bit long pointers). |
|
- | 570 | * |
|
- | 571 | * - b Print value as unsigned binary number. Prefix is not printed by |
|
- | 572 | * default. (Nonstandard extension.) |
|
438 | * |
573 | * |
- | 574 | * - o Print value as unsigned octal number. Prefix is not printed by |
|
439 | * - c Print single character. |
575 | * default. |
440 | * |
576 | * |
441 | * - s Print zero terminated string. If a NULL value is passed as |
- | |
442 | * value, "(NULL)" is printed instead. |
- | |
443 | * |
- | |
444 | * - P, p Print value of a pointer. Void * value is expected and it is |
- | |
445 | * printed in hexadecimal notation with prefix (as with \%#X / \%#x |
- | |
446 | * for 32-bit or \%#X / \%#x for 64-bit long pointers). |
- | |
447 | * |
- | |
448 | * - b Print value as unsigned binary number. Prefix is not printed by |
- | |
449 | * default. (Nonstandard extension.) |
- | |
450 | * |
- | |
451 | * - o Print value as unsigned octal number. Prefix is not printed by |
- | |
452 | * default. |
- | |
453 | * |
- | |
454 | * - d, i Print signed decimal number. There is no difference between d |
577 | * - d, i Print signed decimal number. There is no difference between d |
455 | * and i conversion. |
578 | * and i conversion. |
456 | * |
579 | * |
457 | * - u Print unsigned decimal number. |
580 | * - u Print unsigned decimal number. |
458 | * |
581 | * |
459 | * - X, x Print hexadecimal number with upper- or lower-case. Prefix is |
582 | * - X, x Print hexadecimal number with upper- or lower-case. Prefix is |
460 | * not printed by default. |
583 | * not printed by default. |
461 | * |
584 | * |
462 | * All other characters from fmt except the formatting directives are printed in |
585 | * All other characters from fmt except the formatting directives are printed |
463 | * verbatim. |
586 | * verbatim. |
464 | * |
587 | * |
465 | * @param fmt Formatting NULL terminated string. |
588 | * @param fmt Format NULL-terminated string. |
- | 589 | * |
|
466 | * @return Number of characters printed, negative value on failure. |
590 | * @return Number of characters printed, negative value on failure. |
- | 591 | * |
|
467 | */ |
592 | */ |
468 | int printf_core(const char *fmt, struct printf_spec *ps, va_list ap) |
593 | int printf_core(const char *fmt, printf_spec_t *ps, va_list ap) |
469 | { |
594 | { |
470 | int i = 0; /* index of the currently processed char from fmt */ |
595 | size_t i; /* Index of the currently processed character from fmt */ |
- | 596 | size_t nxt = 0; /* Index of the next character from fmt */ |
|
471 | int j = 0; /* index to the first not printed nonformating character */ |
597 | size_t j = 0; /* Index to the first not printed nonformating character */ |
472 | int end; |
598 | |
473 | int counter; /* counter of printed characters */ |
599 | count_t counter = 0; /* Number of characters printed */ |
474 | int retval; /* used to store return values from called functions */ |
600 | int retval; /* Return values from nested functions */ |
475 | char c; |
601 | |
476 | qualifier_t qualifier; /* type of argument */ |
602 | while (true) { |
477 | int base; /* base in which a numeric parameter will be printed */ |
- | |
478 | uint64_t number; /* argument value */ |
603 | i = nxt; |
479 | size_t size; /* byte size of integer parameter */ |
604 | wchar_t uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
480 | int width, precision; |
- | |
481 | uint64_t flags; |
- | |
482 | 605 | ||
483 | counter = 0; |
606 | if (uc == 0) |
- | 607 | break; |
|
484 | 608 | ||
485 | while ((c = fmt[i])) { |
- | |
486 | /* control character */ |
609 | /* Control character */ |
487 | if (c == '%') { |
610 | if (uc == '%') { |
488 | /* print common characters if any processed */ |
611 | /* Print common characters if any processed */ |
489 | if (i > j) { |
612 | if (i > j) { |
490 | if ((retval = printf_putnchars(&fmt[j], |
613 | if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) { |
491 | (size_t)(i - j), ps)) < 0) { /* error */ |
614 | /* Error */ |
492 | counter = -counter; |
615 | counter = -counter; |
493 | goto out; |
616 | goto out; |
494 | } |
617 | } |
495 | counter += retval; |
618 | counter += retval; |
496 | } |
619 | } |
497 | 620 | ||
498 | j = i; |
621 | j = i; |
- | 622 | ||
499 | /* parse modifiers */ |
623 | /* Parse modifiers */ |
500 | flags = 0; |
624 | uint32_t flags = 0; |
501 | end = 0; |
625 | bool end = false; |
502 | 626 | ||
503 | do { |
627 | do { |
504 | ++i; |
628 | i = nxt; |
- | 629 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
505 | switch (c = fmt[i]) { |
630 | switch (uc) { |
506 | case '#': |
631 | case '#': |
507 | flags |= __PRINTF_FLAG_PREFIX; |
632 | flags |= __PRINTF_FLAG_PREFIX; |
508 | break; |
633 | break; |
509 | case '-': |
634 | case '-': |
510 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
635 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
Line 517... | Line 642... | ||
517 | break; |
642 | break; |
518 | case '0': |
643 | case '0': |
519 | flags |= __PRINTF_FLAG_ZEROPADDED; |
644 | flags |= __PRINTF_FLAG_ZEROPADDED; |
520 | break; |
645 | break; |
521 | default: |
646 | default: |
522 | end = 1; |
647 | end = true; |
523 | }; |
648 | }; |
524 | - | ||
525 | } while (end == 0); |
649 | } while (!end); |
526 | 650 | ||
527 | /* width & '*' operator */ |
651 | /* Width & '*' operator */ |
528 | width = 0; |
652 | int width = 0; |
529 | if (isdigit(fmt[i])) { |
653 | if (isdigit(uc)) { |
530 | while (isdigit(fmt[i])) { |
654 | while (true) { |
531 | width *= 10; |
655 | width *= 10; |
532 | width += fmt[i++] - '0'; |
656 | width += uc - '0'; |
- | 657 | ||
- | 658 | i = nxt; |
|
- | 659 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
- | 660 | if (uc == 0) |
|
- | 661 | break; |
|
- | 662 | if (!isdigit(uc)) |
|
- | 663 | break; |
|
533 | } |
664 | } |
534 | } else if (fmt[i] == '*') { |
665 | } else if (uc == '*') { |
535 | /* get width value from argument list */ |
666 | /* Get width value from argument list */ |
536 | i++; |
667 | i = nxt; |
- | 668 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
537 | width = (int) va_arg(ap, int); |
669 | width = (int) va_arg(ap, int); |
538 | if (width < 0) { |
670 | if (width < 0) { |
539 | /* negative width sets '-' flag */ |
671 | /* Negative width sets '-' flag */ |
540 | width *= -1; |
672 | width *= -1; |
541 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
673 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
542 | } |
674 | } |
543 | } |
675 | } |
544 | 676 | ||
545 | /* precision and '*' operator */ |
677 | /* Precision and '*' operator */ |
546 | precision = 0; |
678 | int precision = 0; |
547 | if (fmt[i] == '.') { |
679 | if (uc == '.') { |
548 | ++i; |
680 | i = nxt; |
- | 681 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
549 | if (isdigit(fmt[i])) { |
682 | if (isdigit(uc)) { |
550 | while (isdigit(fmt[i])) { |
683 | while (true) { |
551 | precision *= 10; |
684 | precision *= 10; |
552 | precision += fmt[i++] - '0'; |
685 | precision += uc - '0'; |
- | 686 | ||
- | 687 | i = nxt; |
|
- | 688 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
- | 689 | if (uc == 0) |
|
- | 690 | break; |
|
- | 691 | if (!isdigit(uc)) |
|
- | 692 | break; |
|
553 | } |
693 | } |
554 | } else if (fmt[i] == '*') { |
694 | } else if (uc == '*') { |
555 | /* |
- | |
556 | * Get precision value from the argument |
695 | /* Get precision value from the argument list */ |
557 | * list. |
696 | i = nxt; |
558 | */ |
- | |
559 | i++; |
697 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
560 | precision = (int) va_arg(ap, int); |
698 | precision = (int) va_arg(ap, int); |
561 | if (precision < 0) { |
699 | if (precision < 0) { |
562 | /* ignore negative precision */ |
700 | /* Ignore negative precision */ |
563 | precision = 0; |
701 | precision = 0; |
564 | } |
702 | } |
565 | } |
703 | } |
566 | } |
704 | } |
- | 705 | ||
- | 706 | qualifier_t qualifier; |
|
567 | 707 | ||
568 | switch (fmt[i++]) { |
708 | switch (uc) { |
569 | /** @todo unimplemented qualifiers: |
709 | /** @todo Unimplemented qualifiers: |
570 | * t ptrdiff_t - ISO C 99 |
710 | * t ptrdiff_t - ISO C 99 |
571 | */ |
711 | */ |
- | 712 | case 'h': |
|
572 | case 'h': /* char or short */ |
713 | /* Char or short */ |
573 | qualifier = PrintfQualifierShort; |
714 | qualifier = PrintfQualifierShort; |
- | 715 | i = nxt; |
|
- | 716 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
574 | if (fmt[i] == 'h') { |
717 | if (uc == 'h') { |
575 | i++; |
718 | i = nxt; |
- | 719 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
576 | qualifier = PrintfQualifierByte; |
720 | qualifier = PrintfQualifierByte; |
577 | } |
721 | } |
578 | break; |
722 | break; |
- | 723 | case 'l': |
|
579 | case 'l': /* long or long long*/ |
724 | /* Long or long long */ |
580 | qualifier = PrintfQualifierLong; |
725 | qualifier = PrintfQualifierLong; |
- | 726 | i = nxt; |
|
- | 727 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
581 | if (fmt[i] == 'l') { |
728 | if (uc == 'l') { |
582 | i++; |
729 | i = nxt; |
- | 730 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
583 | qualifier = PrintfQualifierLongLong; |
731 | qualifier = PrintfQualifierLongLong; |
584 | } |
732 | } |
585 | break; |
733 | break; |
586 | default: |
734 | default: |
587 | /* default type */ |
735 | /* Default type */ |
588 | qualifier = PrintfQualifierInt; |
736 | qualifier = PrintfQualifierInt; |
589 | --i; |
- | |
590 | } |
737 | } |
591 | 738 | ||
592 | base = 10; |
739 | unsigned int base = 10; |
593 | 740 | ||
594 | switch (c = fmt[i]) { |
741 | switch (uc) { |
595 | - | ||
596 | /* |
742 | /* |
597 | * String and character conversions. |
743 | * String and character conversions. |
598 | */ |
744 | */ |
599 | case 's': |
745 | case 's': |
- | 746 | if (qualifier == PrintfQualifierLong) |
|
600 | if ((retval = print_string(va_arg(ap, char *), |
747 | retval = print_wstr(va_arg(ap, wchar_t *), width, precision, flags, ps); |
- | 748 | else |
|
601 | width, precision, flags, ps)) < 0) { |
749 | retval = print_str(va_arg(ap, char *), width, precision, flags, ps); |
- | 750 | ||
- | 751 | if (retval < 0) { |
|
602 | counter = -counter; |
752 | counter = -counter; |
603 | goto out; |
753 | goto out; |
604 | }; |
754 | } |
605 | 755 | ||
606 | counter += retval; |
756 | counter += retval; |
607 | j = i + 1; |
757 | j = nxt; |
608 | goto next_char; |
758 | goto next_char; |
609 | case 'c': |
759 | case 'c': |
610 | c = va_arg(ap, unsigned int); |
760 | if (qualifier == PrintfQualifierLong) |
611 | retval = print_char(c, width, flags, ps); |
761 | retval = print_wchar(va_arg(ap, wchar_t), width, flags, ps); |
- | 762 | else |
|
- | 763 | retval = print_char(va_arg(ap, unsigned int), width, flags, ps); |
|
- | 764 | ||
612 | if (retval < 0) { |
765 | if (retval < 0) { |
613 | counter = -counter; |
766 | counter = -counter; |
614 | goto out; |
767 | goto out; |
615 | }; |
768 | }; |
616 | 769 | ||
617 | counter += retval; |
770 | counter += retval; |
618 | j = i + 1; |
771 | j = nxt; |
619 | goto next_char; |
772 | goto next_char; |
620 | 773 | ||
621 | /* |
774 | /* |
622 | * Integer values |
775 | * Integer values |
623 | */ |
776 | */ |
- | 777 | case 'P': |
|
624 | case 'P': /* pointer */ |
778 | /* Pointer */ |
625 | flags |= __PRINTF_FLAG_BIGCHARS; |
779 | flags |= __PRINTF_FLAG_BIGCHARS; |
626 | case 'p': |
780 | case 'p': |
627 | flags |= __PRINTF_FLAG_PREFIX; |
781 | flags |= __PRINTF_FLAG_PREFIX; |
628 | base = 16; |
782 | base = 16; |
629 | qualifier = PrintfQualifierPointer; |
783 | qualifier = PrintfQualifierPointer; |
630 | break; |
784 | break; |
631 | case 'b': |
785 | case 'b': |
632 | base = 2; |
786 | base = 2; |
633 | break; |
787 | break; |
634 | case 'o': |
788 | case 'o': |
635 | base = 8; |
789 | base = 8; |
636 | break; |
790 | break; |
637 | case 'd': |
791 | case 'd': |
638 | case 'i': |
792 | case 'i': |
639 | flags |= __PRINTF_FLAG_SIGNED; |
793 | flags |= __PRINTF_FLAG_SIGNED; |
640 | case 'u': |
794 | case 'u': |
641 | break; |
795 | break; |
642 | case 'X': |
796 | case 'X': |
643 | flags |= __PRINTF_FLAG_BIGCHARS; |
797 | flags |= __PRINTF_FLAG_BIGCHARS; |
644 | case 'x': |
798 | case 'x': |
645 | base = 16; |
799 | base = 16; |
646 | break; |
800 | break; |
- | 801 | ||
647 | /* percentile itself */ |
802 | /* Percentile itself */ |
648 | case '%': |
803 | case '%': |
649 | j = i; |
804 | j = i; |
650 | goto next_char; |
805 | goto next_char; |
- | 806 | ||
651 | /* |
807 | /* |
652 | * Bad formatting. |
808 | * Bad formatting. |
653 | */ |
809 | */ |
654 | default: |
810 | default: |
655 | /* |
811 | /* |
656 | * Unknown format. Now, j is the index of '%' |
812 | * Unknown format. Now, j is the index of '%' |
657 | * so we will print whole bad format sequence. |
813 | * so we will print whole bad format sequence. |
658 | */ |
814 | */ |
659 | goto next_char; |
815 | goto next_char; |
660 | } |
816 | } |
661 | - | ||
662 | 817 | ||
663 | /* Print integers */ |
818 | /* Print integers */ |
- | 819 | size_t size; |
|
664 | /* print number */ |
820 | uint64_t number; |
665 | switch (qualifier) { |
821 | switch (qualifier) { |
666 | case PrintfQualifierByte: |
822 | case PrintfQualifierByte: |
667 | size = sizeof(unsigned char); |
823 | size = sizeof(unsigned char); |
668 | number = (uint64_t) va_arg(ap, unsigned int); |
824 | number = (uint64_t) va_arg(ap, unsigned int); |
669 | break; |
825 | break; |
Line 685... | Line 841... | ||
685 | break; |
841 | break; |
686 | case PrintfQualifierPointer: |
842 | case PrintfQualifierPointer: |
687 | size = sizeof(void *); |
843 | size = sizeof(void *); |
688 | number = (uint64_t) (unsigned long) va_arg(ap, void *); |
844 | number = (uint64_t) (unsigned long) va_arg(ap, void *); |
689 | break; |
845 | break; |
- | 846 | default: |
|
690 | default: /* Unknown qualifier */ |
847 | /* Unknown qualifier */ |
691 | counter = -counter; |
848 | counter = -counter; |
692 | goto out; |
849 | goto out; |
693 | } |
850 | } |
694 | 851 | ||
695 | if (flags & __PRINTF_FLAG_SIGNED) { |
852 | if (flags & __PRINTF_FLAG_SIGNED) { |
696 | if (number & (0x1 << (size * 8 - 1))) { |
853 | if (number & (0x1 << (size * 8 - 1))) { |
697 | flags |= __PRINTF_FLAG_NEGATIVE; |
854 | flags |= __PRINTF_FLAG_NEGATIVE; |
698 | 855 | ||
699 | if (size == sizeof(uint64_t)) { |
856 | if (size == sizeof(uint64_t)) { |
700 | number = -((int64_t) number); |
857 | number = -((int64_t) number); |
701 | } else { |
858 | } else { |
702 | number = ~number; |
859 | number = ~number; |
703 | number &= |
860 | number &= |
Line 705... | Line 862... | ||
705 | (size * 8)); |
862 | (size * 8)); |
706 | number++; |
863 | number++; |
707 | } |
864 | } |
708 | } |
865 | } |
709 | } |
866 | } |
710 | 867 | ||
711 | if ((retval = print_number(number, width, precision, |
868 | if ((retval = print_number(number, width, precision, |
712 | base, flags, ps)) < 0) { |
869 | base, flags, ps)) < 0) { |
713 | counter = -counter; |
870 | counter = -counter; |
714 | goto out; |
871 | goto out; |
715 | } |
872 | } |
716 | 873 | ||
717 | counter += retval; |
874 | counter += retval; |
718 | j = i + 1; |
875 | j = nxt; |
719 | } |
876 | } |
720 | next_char: |
877 | next_char: |
721 | 878 | ; |
|
722 | ++i; |
- | |
723 | } |
879 | } |
724 | 880 | ||
725 | if (i > j) { |
881 | if (i > j) { |
726 | if ((retval = printf_putnchars(&fmt[j], (unative_t) (i - j), |
882 | if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) { |
727 | ps)) < 0) { /* error */ |
883 | /* Error */ |
728 | counter = -counter; |
884 | counter = -counter; |
729 | goto out; |
885 | goto out; |
730 | - | ||
731 | } |
886 | } |
732 | counter += retval; |
887 | counter += retval; |
733 | } |
888 | } |
734 | 889 | ||
735 | out: |
890 | out: |
736 | return counter; |
891 | return ((int) counter); |
737 | } |
892 | } |
738 | 893 | ||
739 | /** @} |
894 | /** @} |
740 | */ |
895 | */ |