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