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