Rev 4345 | Rev 4348 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4345 | Rev 4347 | ||
---|---|---|---|
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. |
- | 134 | * |
|
- | 135 | * @param c ASCII character to be printed. |
|
- | 136 | * @param ps Output method. |
|
119 | * |
137 | * |
120 | * @param c Character to be printed. |
138 | * @return Number of characters printed. |
121 | * @param ps Output method. |
- | |
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. |
133 | * |
150 | * |
134 | * @param c Character to print. |
151 | * @param c UTF-32 character to be printed. |
135 | * @param width Width modifier. |
152 | * @param ps Output method. |
- | 153 | * |
|
136 | * @param flags Flags that change the way the character is printed. |
154 | * @return Number of characters 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 | { |
- | 253 | if (str == NULL) |
|
- | 254 | return printf_putstr(nullstr, ps); |
|
- | 255 | ||
- | 256 | /* Print leading spaces */ |
|
- | 257 | size_t size = strlen_utf8(str); |
|
- | 258 | if (precision == 0) |
|
- | 259 | precision = size; |
|
- | 260 | ||
182 | int counter = 0; |
261 | count_t counter = 0; |
183 | size_t size; |
262 | width -= precision; |
- | 263 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
|
- | 264 | while (width-- > 0) { |
|
- | 265 | if (printf_putchar(' ', ps) == 1) |
|
- | 266 | counter++; |
|
- | 267 | } |
|
- | 268 | } |
|
- | 269 | ||
184 | int retval; |
270 | int retval; |
- | 271 | size_t bytes = utf8_count_bytes(str, min(size, precision)); |
|
- | 272 | if ((retval = printf_putnchars_utf8(str, bytes, ps)) < 0) |
|
- | 273 | return -counter; |
|
185 | 274 | ||
- | 275 | counter += retval; |
|
- | 276 | ||
186 | if (s == NULL) { |
277 | while (width-- > 0) { |
187 | return printf_putstr("(NULL)", ps); |
278 | if (printf_putchar(' ', ps) == 1) |
- | 279 | counter++; |
|
188 | } |
280 | } |
189 | 281 | ||
190 | size = strlen(s); |
282 | return ((int) counter); |
191 | 283 | } |
|
192 | /* print leading spaces */ |
- | |
193 | 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); |
|
194 | if (precision == 0) |
302 | if (precision == 0) |
195 | precision = size; |
303 | precision = size; |
196 | - | ||
197 | width -= precision; |
- | |
198 | 304 | ||
- | 305 | count_t counter = 0; |
|
- | 306 | width -= precision; |
|
199 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
307 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
200 | while (width-- > 0) { |
308 | while (width-- > 0) { |
201 | if (printf_putchar(' ', ps) == 1) |
309 | if (printf_putchar(' ', ps) == 1) |
202 | counter++; |
310 | counter++; |
203 | } |
311 | } |
204 | } |
312 | } |
205 | 313 | ||
- | 314 | int retval; |
|
- | 315 | size_t bytes = min(size, precision) * sizeof(wchar_t); |
|
206 | if ((retval = printf_putnchars(s, min(size, precision), ps)) < 0) { |
316 | if ((retval = printf_putnchars_utf32(str, bytes, ps)) < 0) |
207 | return -counter; |
317 | return -counter; |
208 | } |
318 | |
209 | counter += retval; |
319 | counter += retval; |
210 | 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). |
|
- | 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 | * |
|
- | 572 | * - u Print unsigned decimal number. |
|
438 | * |
573 | * |
- | 574 | * - X, x Print hexadecimal number with upper- or lower-case. Prefix is |
|
439 | * - c Print single character. |
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 */ |
471 | int j = 0; /* index to the first not printed nonformating character */ |
588 | index_t j = 0; /* Index to the first not printed nonformating character */ |
472 | int end; |
- | |
473 | int counter; /* counter of printed characters */ |
- | |
474 | int retval; /* used to store return values from called functions */ |
- | |
475 | char c; |
- | |
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 */ |
- | |
479 | size_t size; /* byte size of integer parameter */ |
- | |
480 | int width, precision; |
- | |
481 | uint64_t flags; |
- | |
482 | 589 | ||
- | 590 | wchar_t uc; /* Current UTF-32 character decoded from fmt */ |
|
483 | counter = 0; |
591 | count_t counter = 0; /* Number of UTF-8 characters printed */ |
- | 592 | int retval; /* Return values from nested functions */ |
|
484 | 593 | ||
485 | while ((c = fmt[i])) { |
594 | while ((uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT)) != 0) { |
486 | /* control character */ |
595 | /* Control character */ |
487 | if (c == '%') { |
596 | if (uc == '%') { |
488 | /* print common characters if any processed */ |
597 | /* Print common characters if any processed */ |
489 | if (i > j) { |
598 | if (i > j) { |
490 | if ((retval = printf_putnchars(&fmt[j], |
599 | if ((retval = printf_putnchars_utf8(&fmt[j], i - j, ps)) < 0) { |
491 | (size_t)(i - j), ps)) < 0) { /* error */ |
600 | /* Error */ |
492 | counter = -counter; |
601 | counter = -counter; |
493 | goto out; |
602 | goto out; |
494 | } |
603 | } |
495 | counter += retval; |
604 | counter += retval; |
496 | } |
605 | } |
497 | 606 | ||
498 | j = i; |
607 | j = i; |
- | 608 | ||
499 | /* parse modifiers */ |
609 | /* Parse modifiers */ |
500 | flags = 0; |
610 | uint32_t flags = 0; |
501 | end = 0; |
611 | bool end = false; |
502 | 612 | ||
503 | do { |
613 | do { |
504 | ++i; |
614 | i++; |
- | 615 | uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT); |
|
505 | switch (c = fmt[i]) { |
616 | switch (uc) { |
506 | case '#': |
617 | case '#': |
507 | flags |= __PRINTF_FLAG_PREFIX; |
618 | flags |= __PRINTF_FLAG_PREFIX; |
508 | break; |
619 | break; |
509 | case '-': |
620 | case '-': |
510 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
621 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
Line 517... | Line 628... | ||
517 | break; |
628 | break; |
518 | case '0': |
629 | case '0': |
519 | flags |= __PRINTF_FLAG_ZEROPADDED; |
630 | flags |= __PRINTF_FLAG_ZEROPADDED; |
520 | break; |
631 | break; |
521 | default: |
632 | default: |
522 | end = 1; |
633 | end = true; |
523 | }; |
634 | }; |
524 | - | ||
525 | } while (end == 0); |
635 | } while (!end); |
526 | 636 | ||
527 | /* width & '*' operator */ |
637 | /* Width & '*' operator */ |
528 | width = 0; |
638 | int width = 0; |
529 | if (isdigit(fmt[i])) { |
639 | if (isdigit(uc)) { |
- | 640 | while ((uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT)) != 0) { |
|
530 | while (isdigit(fmt[i])) { |
641 | if (!isdigit(uc)) |
- | 642 | break; |
|
- | 643 | ||
531 | width *= 10; |
644 | width *= 10; |
532 | width += fmt[i++] - '0'; |
645 | width += uc - '0'; |
- | 646 | i++; |
|
533 | } |
647 | } |
534 | } else if (fmt[i] == '*') { |
648 | } else if (uc == '*') { |
535 | /* get width value from argument list */ |
649 | /* Get width value from argument list */ |
536 | i++; |
650 | i++; |
- | 651 | uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT); |
|
537 | width = (int) va_arg(ap, int); |
652 | width = (int) va_arg(ap, int); |
538 | if (width < 0) { |
653 | if (width < 0) { |
539 | /* negative width sets '-' flag */ |
654 | /* Negative width sets '-' flag */ |
540 | width *= -1; |
655 | width *= -1; |
541 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
656 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
542 | } |
657 | } |
543 | } |
658 | } |
544 | 659 | ||
545 | /* precision and '*' operator */ |
660 | /* Precision and '*' operator */ |
546 | precision = 0; |
661 | int precision = 0; |
547 | if (fmt[i] == '.') { |
662 | if (uc == '.') { |
548 | ++i; |
663 | i++; |
- | 664 | uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT); |
|
549 | if (isdigit(fmt[i])) { |
665 | if (isdigit(uc)) { |
- | 666 | while ((uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT)) != 0) { |
|
550 | while (isdigit(fmt[i])) { |
667 | if (!isdigit(uc)) |
- | 668 | break; |
|
- | 669 | ||
551 | precision *= 10; |
670 | precision *= 10; |
552 | precision += fmt[i++] - '0'; |
671 | precision += uc - '0'; |
- | 672 | i++; |
|
553 | } |
673 | } |
554 | } else if (fmt[i] == '*') { |
674 | } else if (fmt[i] == '*') { |
555 | /* |
- | |
556 | * Get precision value from the argument |
675 | /* Get precision value from the argument list */ |
557 | * list. |
- | |
558 | */ |
- | |
559 | i++; |
676 | i++; |
- | 677 | uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT); |
|
560 | precision = (int) va_arg(ap, int); |
678 | precision = (int) va_arg(ap, int); |
561 | if (precision < 0) { |
679 | if (precision < 0) { |
562 | /* ignore negative precision */ |
680 | /* Ignore negative precision */ |
563 | precision = 0; |
681 | precision = 0; |
564 | } |
682 | } |
565 | } |
683 | } |
566 | } |
684 | } |
- | 685 | ||
- | 686 | qualifier_t qualifier; |
|
567 | 687 | ||
568 | switch (fmt[i++]) { |
688 | switch (uc) { |
569 | /** @todo unimplemented qualifiers: |
689 | /** @todo Unimplemented qualifiers: |
570 | * t ptrdiff_t - ISO C 99 |
690 | * t ptrdiff_t - ISO C 99 |
571 | */ |
691 | */ |
- | 692 | case 'h': |
|
572 | case 'h': /* char or short */ |
693 | /* Char or short */ |
573 | qualifier = PrintfQualifierShort; |
694 | qualifier = PrintfQualifierShort; |
- | 695 | i++; |
|
- | 696 | uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT); |
|
574 | if (fmt[i] == 'h') { |
697 | if (uc == 'h') { |
575 | i++; |
698 | i++; |
- | 699 | uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT); |
|
576 | qualifier = PrintfQualifierByte; |
700 | qualifier = PrintfQualifierByte; |
577 | } |
701 | } |
578 | break; |
702 | break; |
- | 703 | case 'l': |
|
579 | case 'l': /* long or long long*/ |
704 | /* Long or long long */ |
580 | qualifier = PrintfQualifierLong; |
705 | qualifier = PrintfQualifierLong; |
- | 706 | i++; |
|
- | 707 | uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT); |
|
581 | if (fmt[i] == 'l') { |
708 | if (uc == 'l') { |
582 | i++; |
709 | i++; |
- | 710 | uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT); |
|
583 | qualifier = PrintfQualifierLongLong; |
711 | qualifier = PrintfQualifierLongLong; |
584 | } |
712 | } |
585 | break; |
713 | break; |
586 | default: |
714 | default: |
587 | /* default type */ |
715 | /* Default type */ |
588 | qualifier = PrintfQualifierInt; |
716 | qualifier = PrintfQualifierInt; |
589 | --i; |
- | |
590 | } |
717 | } |
591 | 718 | ||
592 | base = 10; |
719 | unsigned int base = 10; |
593 | 720 | ||
594 | switch (c = fmt[i]) { |
721 | switch (uc) { |
595 | - | ||
596 | /* |
722 | /* |
597 | * String and character conversions. |
723 | * String and character conversions. |
598 | */ |
724 | */ |
599 | case 's': |
725 | case 's': |
- | 726 | if (qualifier == PrintfQualifierLong) |
|
600 | if ((retval = print_string(va_arg(ap, char *), |
727 | retval = print_utf32(va_arg(ap, wchar_t *), width, precision, flags, ps); |
- | 728 | else |
|
601 | width, precision, flags, ps)) < 0) { |
729 | retval = print_utf8(va_arg(ap, char *), width, precision, flags, ps); |
- | 730 | ||
- | 731 | if (retval < 0) { |
|
602 | counter = -counter; |
732 | counter = -counter; |
603 | goto out; |
733 | goto out; |
604 | }; |
734 | } |
605 | 735 | ||
606 | counter += retval; |
736 | counter += retval; |
607 | j = i + 1; |
737 | j = i + 1; |
608 | goto next_char; |
738 | goto next_char; |
609 | case 'c': |
739 | case 'c': |
610 | c = va_arg(ap, unsigned int); |
740 | if (qualifier == PrintfQualifierLong) |
611 | retval = print_char(c, width, flags, ps); |
741 | retval = print_wchar(va_arg(ap, wchar_t), width, flags, ps); |
- | 742 | else |
|
- | 743 | retval = print_char(va_arg(ap, unsigned int), width, flags, ps); |
|
- | 744 | ||
612 | if (retval < 0) { |
745 | if (retval < 0) { |
613 | counter = -counter; |
746 | counter = -counter; |
614 | goto out; |
747 | goto out; |
615 | }; |
748 | }; |
616 | 749 | ||
617 | counter += retval; |
750 | counter += retval; |
618 | j = i + 1; |
751 | j = i + 1; |
619 | goto next_char; |
752 | goto next_char; |
620 | 753 | ||
621 | /* |
754 | /* |
622 | * Integer values |
755 | * Integer values |
623 | */ |
756 | */ |
- | 757 | case 'P': |
|
624 | case 'P': /* pointer */ |
758 | /* Pointer */ |
625 | flags |= __PRINTF_FLAG_BIGCHARS; |
759 | flags |= __PRINTF_FLAG_BIGCHARS; |
626 | case 'p': |
760 | case 'p': |
627 | flags |= __PRINTF_FLAG_PREFIX; |
761 | flags |= __PRINTF_FLAG_PREFIX; |
628 | base = 16; |
762 | base = 16; |
629 | qualifier = PrintfQualifierPointer; |
763 | qualifier = PrintfQualifierPointer; |
630 | break; |
764 | break; |
631 | case 'b': |
765 | case 'b': |
632 | base = 2; |
766 | base = 2; |
633 | break; |
767 | break; |
634 | case 'o': |
768 | case 'o': |
635 | base = 8; |
769 | base = 8; |
636 | break; |
770 | break; |
637 | case 'd': |
771 | case 'd': |
638 | case 'i': |
772 | case 'i': |
639 | flags |= __PRINTF_FLAG_SIGNED; |
773 | flags |= __PRINTF_FLAG_SIGNED; |
640 | case 'u': |
774 | case 'u': |
641 | break; |
775 | break; |
642 | case 'X': |
776 | case 'X': |
643 | flags |= __PRINTF_FLAG_BIGCHARS; |
777 | flags |= __PRINTF_FLAG_BIGCHARS; |
644 | case 'x': |
778 | case 'x': |
645 | base = 16; |
779 | base = 16; |
646 | break; |
780 | break; |
- | 781 | ||
647 | /* percentile itself */ |
782 | /* Percentile itself */ |
648 | case '%': |
783 | case '%': |
649 | j = i; |
784 | j = i; |
650 | goto next_char; |
785 | goto next_char; |
- | 786 | ||
651 | /* |
787 | /* |
652 | * Bad formatting. |
788 | * Bad formatting. |
653 | */ |
789 | */ |
654 | default: |
790 | default: |
655 | /* |
791 | /* |
656 | * Unknown format. Now, j is the index of '%' |
792 | * Unknown format. Now, j is the index of '%' |
657 | * so we will print whole bad format sequence. |
793 | * so we will print whole bad format sequence. |
658 | */ |
794 | */ |
659 | goto next_char; |
795 | goto next_char; |
660 | } |
796 | } |
661 | - | ||
662 | 797 | ||
663 | /* Print integers */ |
798 | /* Print integers */ |
- | 799 | size_t size; |
|
664 | /* print number */ |
800 | uint64_t number; |
665 | switch (qualifier) { |
801 | switch (qualifier) { |
666 | case PrintfQualifierByte: |
802 | case PrintfQualifierByte: |
667 | size = sizeof(unsigned char); |
803 | size = sizeof(unsigned char); |
668 | number = (uint64_t) va_arg(ap, unsigned int); |
804 | number = (uint64_t) va_arg(ap, unsigned int); |
669 | break; |
805 | break; |
Line 685... | Line 821... | ||
685 | break; |
821 | break; |
686 | case PrintfQualifierPointer: |
822 | case PrintfQualifierPointer: |
687 | size = sizeof(void *); |
823 | size = sizeof(void *); |
688 | number = (uint64_t) (unsigned long) va_arg(ap, void *); |
824 | number = (uint64_t) (unsigned long) va_arg(ap, void *); |
689 | break; |
825 | break; |
- | 826 | default: |
|
690 | default: /* Unknown qualifier */ |
827 | /* Unknown qualifier */ |
691 | counter = -counter; |
828 | counter = -counter; |
692 | goto out; |
829 | goto out; |
693 | } |
830 | } |
694 | 831 | ||
695 | if (flags & __PRINTF_FLAG_SIGNED) { |
832 | if (flags & __PRINTF_FLAG_SIGNED) { |
696 | if (number & (0x1 << (size * 8 - 1))) { |
833 | if (number & (0x1 << (size * 8 - 1))) { |
697 | flags |= __PRINTF_FLAG_NEGATIVE; |
834 | flags |= __PRINTF_FLAG_NEGATIVE; |
698 | 835 | ||
699 | if (size == sizeof(uint64_t)) { |
836 | if (size == sizeof(uint64_t)) { |
700 | number = -((int64_t) number); |
837 | number = -((int64_t) number); |
701 | } else { |
838 | } else { |
702 | number = ~number; |
839 | number = ~number; |
703 | number &= |
840 | number &= |
Line 705... | Line 842... | ||
705 | (size * 8)); |
842 | (size * 8)); |
706 | number++; |
843 | number++; |
707 | } |
844 | } |
708 | } |
845 | } |
709 | } |
846 | } |
710 | 847 | ||
711 | if ((retval = print_number(number, width, precision, |
848 | if ((retval = print_number(number, width, precision, |
712 | base, flags, ps)) < 0) { |
849 | base, flags, ps)) < 0) { |
713 | counter = -counter; |
850 | counter = -counter; |
714 | goto out; |
851 | goto out; |
715 | } |
852 | } |
716 | 853 | ||
717 | counter += retval; |
854 | counter += retval; |
718 | j = i + 1; |
855 | j = i + 1; |
719 | } |
856 | } |
720 | next_char: |
857 | next_char: |
721 | 858 | ||
722 | ++i; |
859 | i++; |
723 | } |
860 | } |
724 | 861 | ||
725 | if (i > j) { |
862 | if (i > j) { |
726 | if ((retval = printf_putnchars(&fmt[j], (unative_t) (i - j), |
863 | if ((retval = printf_putnchars_utf8(&fmt[j], i - j, ps)) < 0) { |
727 | ps)) < 0) { /* error */ |
864 | /* Error */ |
728 | counter = -counter; |
865 | counter = -counter; |
729 | goto out; |
866 | goto out; |
730 | - | ||
731 | } |
867 | } |
732 | counter += retval; |
868 | counter += retval; |
733 | } |
869 | } |
734 | 870 | ||
735 | out: |
871 | out: |
736 | return counter; |
872 | return ((int) counter); |
737 | } |
873 | } |
738 | 874 | ||
739 | /** @} |
875 | /** @} |
740 | */ |
876 | */ |