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