Rev 1444 | Rev 1617 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 974 | cejka | 1 | /* |
| 2 | * Copyright (C) 2001-2004 Jakub Jermar |
||
| 3 | * Copyright (C) 2006 Josef Cejka |
||
| 4 | * All rights reserved. |
||
| 5 | * |
||
| 6 | * Redistribution and use in source and binary forms, with or without |
||
| 7 | * modification, are permitted provided that the following conditions |
||
| 8 | * are met: |
||
| 9 | * |
||
| 10 | * - Redistributions of source code must retain the above copyright |
||
| 11 | * notice, this list of conditions and the following disclaimer. |
||
| 12 | * - Redistributions in binary form must reproduce the above copyright |
||
| 13 | * notice, this list of conditions and the following disclaimer in the |
||
| 14 | * documentation and/or other materials provided with the distribution. |
||
| 15 | * - The name of the author may not be used to endorse or promote products |
||
| 16 | * derived from this software without specific prior written permission. |
||
| 17 | * |
||
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
| 25 | * 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 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 28 | */ |
||
| 29 | |||
| 1272 | cejka | 30 | /** |
| 31 | * @file print.c |
||
| 32 | * @brief Printing functions. |
||
| 33 | */ |
||
| 34 | |||
| 1234 | cejka | 35 | #include <unistd.h> |
| 974 | cejka | 36 | #include <stdio.h> |
| 1234 | cejka | 37 | #include <io/printf_core.h> |
| 1173 | cejka | 38 | #include <ctype.h> |
| 39 | #include <string.h> |
||
| 1616 | palkovsky | 40 | /* For serialization */ |
| 41 | #include <async.h> |
||
| 974 | cejka | 42 | |
| 1272 | cejka | 43 | #define __PRINTF_FLAG_PREFIX 0x00000001 /**< show prefixes 0x or 0*/ |
| 44 | #define __PRINTF_FLAG_SIGNED 0x00000002 /**< signed / unsigned number */ |
||
| 45 | #define __PRINTF_FLAG_ZEROPADDED 0x00000004 /**< print leading zeroes */ |
||
| 46 | #define __PRINTF_FLAG_LEFTALIGNED 0x00000010 /**< align to left */ |
||
| 47 | #define __PRINTF_FLAG_SHOWPLUS 0x00000020 /**< always show + sign */ |
||
| 48 | #define __PRINTF_FLAG_SPACESIGN 0x00000040 /**< print space instead of plus */ |
||
| 49 | #define __PRINTF_FLAG_BIGCHARS 0x00000080 /**< show big characters */ |
||
| 50 | #define __PRINTF_FLAG_NEGATIVE 0x00000100 /**< number has - sign */ |
||
| 995 | cejka | 51 | |
| 1272 | cejka | 52 | #define PRINT_NUMBER_BUFFER_SIZE (64+5) /**< Buffer big enought for 64 bit number |
| 1073 | cejka | 53 | * printed in base 2, sign, prefix and |
| 54 | * 0 to terminate string.. (last one is only for better testing |
||
| 55 | * end of buffer by zero-filling subroutine) |
||
| 56 | */ |
||
| 1272 | cejka | 57 | /** Enumeration of possible arguments types. |
| 58 | */ |
||
| 1073 | cejka | 59 | typedef enum { |
| 60 | PrintfQualifierByte = 0, |
||
| 61 | PrintfQualifierShort, |
||
| 62 | PrintfQualifierInt, |
||
| 63 | PrintfQualifierLong, |
||
| 64 | PrintfQualifierLongLong, |
||
| 65 | PrintfQualifierSizeT, |
||
| 66 | PrintfQualifierPointer |
||
| 67 | } qualifier_t; |
||
| 974 | cejka | 68 | |
| 1272 | cejka | 69 | static char digits_small[] = "0123456789abcdef"; /**< Small hexadecimal characters */ |
| 70 | static char digits_big[] = "0123456789ABCDEF"; /**< Big hexadecimal characters */ |
||
| 995 | cejka | 71 | |
| 1234 | cejka | 72 | /** Print count chars from buffer without adding newline |
| 73 | * @param buf Buffer with size at least count bytes - NULL pointer NOT allowed! |
||
| 74 | * @param count |
||
| 75 | * @param ps output method and its data |
||
| 76 | * @return 0 on success, EOF on fail |
||
| 77 | */ |
||
| 78 | static int printf_putnchars(const char * buf, size_t count, struct printf_spec *ps) |
||
| 79 | { |
||
| 80 | if (ps->write((void *)buf, count, ps->data) == count) { |
||
| 81 | return 0; |
||
| 82 | } |
||
| 83 | |||
| 84 | return EOF; |
||
| 85 | } |
||
| 1173 | cejka | 86 | |
| 1234 | cejka | 87 | /** Print string without added newline |
| 88 | * @param str string to print |
||
| 89 | * @param ps write function specification and support data |
||
| 90 | * @return 0 on success or EOF on fail |
||
| 91 | */ |
||
| 92 | static int printf_putstr(const char * str, struct printf_spec *ps) |
||
| 93 | { |
||
| 94 | size_t count; |
||
| 95 | |||
| 96 | if (str == NULL) { |
||
| 97 | return printf_putnchars("(NULL)", 6, ps); |
||
| 98 | } |
||
| 99 | |||
| 100 | for (count = 0; str[count] != 0; count++); |
||
| 101 | |||
| 102 | if (ps->write((void *) str, count, ps->data) == count) { |
||
| 103 | return 0; |
||
| 104 | } |
||
| 105 | |||
| 106 | return EOF; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** Print one character to output |
||
| 110 | * @param c one character |
||
| 111 | * @param ps output method |
||
| 112 | * @return printed character or EOF |
||
| 113 | */ |
||
| 114 | static int printf_putchar(int c, struct printf_spec *ps) |
||
| 115 | { |
||
| 116 | unsigned char ch = c; |
||
| 117 | |||
| 118 | if (ps->write((void *) &ch, 1, ps->data) == 1) { |
||
| 119 | return c; |
||
| 120 | } |
||
| 121 | |||
| 122 | return EOF; |
||
| 123 | } |
||
| 124 | |||
| 1173 | cejka | 125 | /** Print one formatted character |
| 126 | * @param c character to print |
||
| 127 | * @param width |
||
| 128 | * @param flags |
||
| 129 | * @return number of printed characters or EOF |
||
| 130 | */ |
||
| 1234 | cejka | 131 | static int print_char(char c, int width, uint64_t flags, struct printf_spec *ps) |
| 1173 | cejka | 132 | { |
| 133 | int counter = 0; |
||
| 134 | |||
| 135 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
||
| 136 | while (--width > 0) { /* one space is consumed by character itself hence predecrement */ |
||
| 137 | /* FIXME: painful slow */ |
||
| 1234 | cejka | 138 | printf_putchar(' ', ps); |
| 1173 | cejka | 139 | ++counter; |
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 1234 | cejka | 143 | if (printf_putchar(c, ps) == EOF) { |
| 1173 | cejka | 144 | return EOF; |
| 145 | } |
||
| 146 | |||
| 147 | while (--width > 0) { /* one space is consumed by character itself hence predecrement */ |
||
| 1234 | cejka | 148 | printf_putchar(' ', ps); |
| 1173 | cejka | 149 | ++counter; |
| 150 | } |
||
| 151 | |||
| 152 | return ++counter; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** Print one string |
||
| 156 | * @param s string |
||
| 157 | * @param width |
||
| 158 | * @param precision |
||
| 159 | * @param flags |
||
| 160 | * @return number of printed characters or EOF |
||
| 161 | */ |
||
| 162 | |||
| 1234 | cejka | 163 | static int print_string(char *s, int width, int precision, uint64_t flags, struct printf_spec *ps) |
| 1173 | cejka | 164 | { |
| 165 | int counter = 0; |
||
| 166 | size_t size; |
||
| 167 | |||
| 168 | if (s == NULL) { |
||
| 1234 | cejka | 169 | return printf_putstr("(NULL)", ps); |
| 1173 | cejka | 170 | } |
| 171 | |||
| 172 | size = strlen(s); |
||
| 173 | |||
| 174 | /* print leading spaces */ |
||
| 175 | |||
| 176 | if (precision == 0) |
||
| 177 | precision = size; |
||
| 178 | |||
| 179 | width -= precision; |
||
| 180 | |||
| 181 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
||
| 182 | while (width-- > 0) { |
||
| 1234 | cejka | 183 | printf_putchar(' ', ps); |
| 1173 | cejka | 184 | counter++; |
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | while (precision > size) { |
||
| 189 | precision--; |
||
| 1234 | cejka | 190 | printf_putchar(' ', ps); |
| 1173 | cejka | 191 | ++counter; |
| 192 | } |
||
| 193 | |||
| 1234 | cejka | 194 | if (printf_putnchars(s, precision, ps) == EOF) { |
| 1173 | cejka | 195 | return EOF; |
| 196 | } |
||
| 197 | |||
| 198 | counter += precision; |
||
| 199 | |||
| 200 | while (width-- > 0) { |
||
| 1234 | cejka | 201 | printf_putchar(' ', ps); |
| 1173 | cejka | 202 | ++counter; |
| 203 | } |
||
| 204 | |||
| 205 | return ++counter; |
||
| 206 | } |
||
| 207 | |||
| 208 | |||
| 974 | cejka | 209 | /** Print number in given base |
| 210 | * |
||
| 211 | * Print significant digits of a number in given |
||
| 212 | * base. |
||
| 213 | * |
||
| 214 | * @param num Number to print. |
||
| 1173 | cejka | 215 | * @param width |
| 216 | * @param precision |
||
| 974 | cejka | 217 | * @param base Base to print the number in (should |
| 218 | * be in range 2 .. 16). |
||
| 1073 | cejka | 219 | * @param flags output modifiers |
| 220 | * @return number of written characters or EOF |
||
| 974 | cejka | 221 | * |
| 222 | */ |
||
| 1234 | cejka | 223 | static int print_number(uint64_t num, int width, int precision, int base , uint64_t flags, struct printf_spec *ps) |
| 974 | cejka | 224 | { |
| 1073 | cejka | 225 | char *digits = digits_small; |
| 226 | char d[PRINT_NUMBER_BUFFER_SIZE]; /* this is good enough even for base == 2, prefix and sign */ |
||
| 227 | char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1]; |
||
| 1234 | cejka | 228 | int size = 0; /* size of number with all prefixes and signs */ |
| 229 | int number_size; /* size of plain number */ |
||
| 1173 | cejka | 230 | int written = 0; |
| 231 | char sgn; |
||
| 974 | cejka | 232 | |
| 1073 | cejka | 233 | if (flags & __PRINTF_FLAG_BIGCHARS) |
| 234 | digits = digits_big; |
||
| 995 | cejka | 235 | |
| 1073 | cejka | 236 | *ptr-- = 0; /* Put zero at end of string */ |
| 237 | |||
| 238 | if (num == 0) { |
||
| 239 | *ptr-- = '0'; |
||
| 1173 | cejka | 240 | size++; |
| 1073 | cejka | 241 | } else { |
| 242 | do { |
||
| 243 | *ptr-- = digits[num % base]; |
||
| 1173 | cejka | 244 | size++; |
| 1073 | cejka | 245 | } while (num /= base); |
| 246 | } |
||
| 1234 | cejka | 247 | |
| 248 | number_size = size; |
||
| 1173 | cejka | 249 | |
| 250 | /* Collect sum of all prefixes/signs/... to calculate padding and leading zeroes */ |
||
| 251 | if (flags & __PRINTF_FLAG_PREFIX) { |
||
| 1073 | cejka | 252 | switch(base) { |
| 253 | case 2: /* Binary formating is not standard, but usefull */ |
||
| 1173 | cejka | 254 | size += 2; |
| 1073 | cejka | 255 | break; |
| 256 | case 8: |
||
| 1173 | cejka | 257 | size++; |
| 1073 | cejka | 258 | break; |
| 259 | case 16: |
||
| 1173 | cejka | 260 | size += 2; |
| 1073 | cejka | 261 | break; |
| 262 | } |
||
| 263 | } |
||
| 1173 | cejka | 264 | |
| 265 | sgn = 0; |
||
| 1073 | cejka | 266 | if (flags & __PRINTF_FLAG_SIGNED) { |
| 267 | if (flags & __PRINTF_FLAG_NEGATIVE) { |
||
| 1173 | cejka | 268 | sgn = '-'; |
| 269 | size++; |
||
| 1073 | cejka | 270 | } else if (flags & __PRINTF_FLAG_SHOWPLUS) { |
| 1173 | cejka | 271 | sgn = '+'; |
| 272 | size++; |
||
| 1073 | cejka | 273 | } else if (flags & __PRINTF_FLAG_SPACESIGN) { |
| 1173 | cejka | 274 | sgn = ' '; |
| 275 | size++; |
||
| 1073 | cejka | 276 | } |
| 277 | } |
||
| 974 | cejka | 278 | |
| 1073 | cejka | 279 | if (flags & __PRINTF_FLAG_LEFTALIGNED) { |
| 280 | flags &= ~__PRINTF_FLAG_ZEROPADDED; |
||
| 281 | } |
||
| 1173 | cejka | 282 | |
| 283 | /* if number is leftaligned or precision is specified then zeropadding is ignored */ |
||
| 1073 | cejka | 284 | if (flags & __PRINTF_FLAG_ZEROPADDED) { |
| 1173 | cejka | 285 | if ((precision == 0) && (width > size)) { |
| 1234 | cejka | 286 | precision = width - size + number_size; |
| 1073 | cejka | 287 | } |
| 288 | } |
||
| 1173 | cejka | 289 | |
| 290 | /* print leading spaces */ |
||
| 1234 | cejka | 291 | if (number_size > precision) /* We must print whole number not only a part */ |
| 292 | precision = number_size; |
||
| 1173 | cejka | 293 | |
| 1234 | cejka | 294 | width -= precision + size - number_size; |
| 1073 | cejka | 295 | |
| 1173 | cejka | 296 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
| 297 | while (width-- > 0) { |
||
| 1234 | cejka | 298 | printf_putchar(' ', ps); |
| 1173 | cejka | 299 | written++; |
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | /* print sign */ |
||
| 304 | if (sgn) { |
||
| 1234 | cejka | 305 | printf_putchar(sgn, ps); |
| 1173 | cejka | 306 | written++; |
| 307 | } |
||
| 308 | |||
| 309 | /* print prefix */ |
||
| 310 | |||
| 311 | if (flags & __PRINTF_FLAG_PREFIX) { |
||
| 312 | switch(base) { |
||
| 313 | case 2: /* Binary formating is not standard, but usefull */ |
||
| 1234 | cejka | 314 | printf_putchar('0', ps); |
| 1173 | cejka | 315 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
| 1234 | cejka | 316 | printf_putchar('B', ps); |
| 1173 | cejka | 317 | } else { |
| 1234 | cejka | 318 | printf_putchar('b', ps); |
| 1173 | cejka | 319 | } |
| 1197 | cejka | 320 | written += 2; |
| 1173 | cejka | 321 | break; |
| 322 | case 8: |
||
| 1234 | cejka | 323 | printf_putchar('o', ps); |
| 1173 | cejka | 324 | written++; |
| 325 | break; |
||
| 326 | case 16: |
||
| 1234 | cejka | 327 | printf_putchar('0', ps); |
| 1173 | cejka | 328 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
| 1234 | cejka | 329 | printf_putchar('X', ps); |
| 1173 | cejka | 330 | } else { |
| 1234 | cejka | 331 | printf_putchar('x', ps); |
| 1173 | cejka | 332 | } |
| 333 | written += 2; |
||
| 334 | break; |
||
| 335 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | /* print leading zeroes */ |
||
| 1234 | cejka | 339 | precision -= number_size; |
| 1173 | cejka | 340 | while (precision-- > 0) { |
| 1234 | cejka | 341 | printf_putchar('0', ps); |
| 1173 | cejka | 342 | written++; |
| 343 | } |
||
| 344 | |||
| 345 | |||
| 346 | /* print number itself */ |
||
| 347 | |||
| 1234 | cejka | 348 | written += printf_putstr(++ptr, ps); |
| 1173 | cejka | 349 | |
| 350 | /* print ending spaces */ |
||
| 351 | |||
| 352 | while (width-- > 0) { |
||
| 1234 | cejka | 353 | printf_putchar(' ', ps); |
| 1173 | cejka | 354 | written++; |
| 355 | } |
||
| 356 | |||
| 357 | return written; |
||
| 974 | cejka | 358 | } |
| 359 | |||
| 360 | |||
| 1272 | cejka | 361 | /** Print formatted string. |
| 974 | cejka | 362 | * |
| 1272 | cejka | 363 | * Print string formatted according to the fmt parameter |
| 364 | * and variadic arguments. Each formatting directive |
||
| 1199 | cejka | 365 | * must have the following form: |
| 1272 | cejka | 366 | * |
| 367 | * \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION |
||
| 974 | cejka | 368 | * |
| 1272 | cejka | 369 | * FLAGS:@n |
| 370 | * - "#" Force to print prefix. |
||
| 371 | * For conversion \%o the prefix is 0, for %x and \%X prefixes are 0x and 0X |
||
| 372 | * and for conversion \%b the prefix is 0b. |
||
| 373 | * |
||
| 374 | * - "-" Align to left. |
||
| 375 | * |
||
| 376 | * - "+" Print positive sign just as negative. |
||
| 377 | * |
||
| 378 | * - " " If the printed number is positive and "+" flag is not set, print space in |
||
| 379 | * place of sign. |
||
| 380 | * |
||
| 381 | * - "0" Print 0 as padding instead of spaces. Zeroes are placed between sign and the |
||
| 382 | * rest of the number. This flag is ignored if "-" flag is specified. |
||
| 1199 | cejka | 383 | * |
| 1272 | cejka | 384 | * WIDTH:@n |
| 385 | * - Specify minimal width of printed argument. If it is bigger, width is ignored. |
||
| 386 | * If width is specified with a "*" character instead of number, width is taken from |
||
| 387 | * parameter list. And integer parameter is expected before parameter for processed |
||
| 388 | * conversion specification. If this value is negative its absolute value is taken |
||
| 389 | * and the "-" flag is set. |
||
| 974 | cejka | 390 | * |
| 1272 | cejka | 391 | * PRECISION:@n |
| 392 | * - Value precision. For numbers it specifies minimum valid numbers. |
||
| 1199 | cejka | 393 | * Smaller numbers are printed with leading zeroes. Bigger numbers are not affected. |
| 1272 | cejka | 394 | * Strings with more than precision characters are cut off. |
| 395 | * Just as with width, an "*" can be used used instead of a number. |
||
| 396 | * An integer value is then expected in parameters. When both width and precision |
||
| 397 | * are specified using "*", the first parameter is used for width and the second one |
||
| 398 | * for precision. |
||
| 1199 | cejka | 399 | * |
| 1272 | cejka | 400 | * TYPE:@n |
| 401 | * - "hh" Signed or unsigned char.@n |
||
| 402 | * - "h" Signed or usigned short.@n |
||
| 403 | * - "" Signed or usigned int (default value).@n |
||
| 404 | * - "l" Signed or usigned long int.@n |
||
| 405 | * - "ll" Signed or usigned long long int.@n |
||
| 406 | * - "z" Type size_t.@n |
||
| 1199 | cejka | 407 | * |
| 408 | * |
||
| 1272 | cejka | 409 | * CONVERSION:@n |
| 410 | * - % Print percentile character itself. |
||
| 974 | cejka | 411 | * |
| 1272 | cejka | 412 | * - c Print single character. |
| 974 | cejka | 413 | * |
| 1272 | cejka | 414 | * - s Print zero terminated string. If a NULL value is passed as value, "(NULL)" is printed instead. |
| 1199 | cejka | 415 | * |
| 1272 | cejka | 416 | * - P, p Print value of a pointer. Void * value is expected and it is printed in hexadecimal notation with prefix |
| 417 | * (as with \%#X or \%#x for 32bit or \%#X / \%#x for 64bit long pointers). |
||
| 974 | cejka | 418 | * |
| 1272 | cejka | 419 | * - b Print value as unsigned binary number. Prefix is not printed by default. (Nonstandard extension.) |
| 1199 | cejka | 420 | * |
| 1272 | cejka | 421 | * - o Print value as unsigned octal number. Prefix is not printed by default. |
| 974 | cejka | 422 | * |
| 1272 | cejka | 423 | * - d,i Print signed decimal number. There is no difference between d and i conversion. |
| 974 | cejka | 424 | * |
| 1272 | cejka | 425 | * - u Print unsigned decimal number. |
| 974 | cejka | 426 | * |
| 1272 | cejka | 427 | * - X, x Print hexadecimal number with upper- or lower-case. Prefix is not printed by default. |
| 1199 | cejka | 428 | * |
| 974 | cejka | 429 | * All other characters from fmt except the formatting directives |
| 430 | * are printed in verbatim. |
||
| 431 | * |
||
| 432 | * @param fmt Formatting NULL terminated string. |
||
| 1272 | cejka | 433 | * @return Number of printed characters or negative value on failure. |
| 974 | cejka | 434 | */ |
| 1234 | cejka | 435 | int printf_core(const char *fmt, struct printf_spec *ps, va_list ap) |
| 974 | cejka | 436 | { |
| 1444 | cejka | 437 | int i = 0, j = 0; /* i is index of currently processed char from fmt, j is index to the first not printed nonformating character */ |
| 1073 | cejka | 438 | int end; |
| 1444 | cejka | 439 | int counter; /* counter of printed characters */ |
| 440 | int retval; /* used to store return values from called functions */ |
||
| 1073 | cejka | 441 | char c; |
| 1444 | cejka | 442 | qualifier_t qualifier; /* type of argument */ |
| 443 | int base; /* base in which will be parameter (numbers only) printed */ |
||
| 444 | uint64_t number; /* argument value */ |
||
| 445 | size_t size; /* byte size of integer parameter */ |
||
| 1173 | cejka | 446 | int width, precision; |
| 995 | cejka | 447 | uint64_t flags; |
| 974 | cejka | 448 | |
| 1616 | palkovsky | 449 | /* Don't let other threads interfere */ |
| 450 | async_serialize_start(); |
||
| 451 | |||
| 974 | cejka | 452 | counter = 0; |
| 1173 | cejka | 453 | |
| 974 | cejka | 454 | while ((c = fmt[i])) { |
| 992 | jermar | 455 | /* control character */ |
| 974 | cejka | 456 | if (c == '%' ) { |
| 992 | jermar | 457 | /* print common characters if any processed */ |
| 974 | cejka | 458 | if (i > j) { |
| 1234 | cejka | 459 | if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) == EOF) { /* error */ |
| 1616 | palkovsky | 460 | goto minus_out; |
| 974 | cejka | 461 | } |
| 462 | counter += retval; |
||
| 463 | } |
||
| 1073 | cejka | 464 | |
| 465 | j = i; |
||
| 995 | cejka | 466 | /* parse modifiers */ |
| 467 | flags = 0; |
||
| 1073 | cejka | 468 | end = 0; |
| 469 | |||
| 470 | do { |
||
| 471 | ++i; |
||
| 472 | switch (c = fmt[i]) { |
||
| 473 | case '#': flags |= __PRINTF_FLAG_PREFIX; break; |
||
| 474 | case '-': flags |= __PRINTF_FLAG_LEFTALIGNED; break; |
||
| 475 | case '+': flags |= __PRINTF_FLAG_SHOWPLUS; break; |
||
| 476 | case ' ': flags |= __PRINTF_FLAG_SPACESIGN; break; |
||
| 477 | case '0': flags |= __PRINTF_FLAG_ZEROPADDED; break; |
||
| 478 | default: end = 1; |
||
| 479 | }; |
||
| 480 | |||
| 481 | } while (end == 0); |
||
| 1173 | cejka | 482 | |
| 483 | /* width & '*' operator */ |
||
| 484 | width = 0; |
||
| 485 | if (isdigit(fmt[i])) { |
||
| 486 | while (isdigit(fmt[i])) { |
||
| 487 | width *= 10; |
||
| 488 | width += fmt[i++] - '0'; |
||
| 489 | } |
||
| 490 | } else if (fmt[i] == '*') { |
||
| 491 | /* get width value from argument list*/ |
||
| 492 | i++; |
||
| 493 | width = (int)va_arg(ap, int); |
||
| 494 | if (width < 0) { |
||
| 495 | /* negative width means to set '-' flag */ |
||
| 496 | width *= -1; |
||
| 497 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
||
| 498 | } |
||
| 499 | } |
||
| 500 | |||
| 501 | /* precision and '*' operator */ |
||
| 502 | precision = 0; |
||
| 503 | if (fmt[i] == '.') { |
||
| 504 | ++i; |
||
| 505 | if (isdigit(fmt[i])) { |
||
| 506 | while (isdigit(fmt[i])) { |
||
| 507 | precision *= 10; |
||
| 508 | precision += fmt[i++] - '0'; |
||
| 509 | } |
||
| 510 | } else if (fmt[i] == '*') { |
||
| 511 | /* get precision value from argument list*/ |
||
| 512 | i++; |
||
| 513 | precision = (int)va_arg(ap, int); |
||
| 514 | if (precision < 0) { |
||
| 515 | /* negative precision means to ignore it */ |
||
| 516 | precision = 0; |
||
| 517 | } |
||
| 518 | } |
||
| 519 | } |
||
| 1073 | cejka | 520 | |
| 521 | switch (fmt[i++]) { |
||
| 1173 | cejka | 522 | /** TODO: unimplemented qualifiers: |
| 523 | * t ptrdiff_t - ISO C 99 |
||
| 524 | */ |
||
| 1073 | cejka | 525 | case 'h': /* char or short */ |
| 526 | qualifier = PrintfQualifierShort; |
||
| 527 | if (fmt[i] == 'h') { |
||
| 528 | i++; |
||
| 529 | qualifier = PrintfQualifierByte; |
||
| 530 | } |
||
| 531 | break; |
||
| 532 | case 'l': /* long or long long*/ |
||
| 533 | qualifier = PrintfQualifierLong; |
||
| 534 | if (fmt[i] == 'l') { |
||
| 535 | i++; |
||
| 536 | qualifier = PrintfQualifierLongLong; |
||
| 537 | } |
||
| 538 | break; |
||
| 539 | case 'z': /* size_t */ |
||
| 540 | qualifier = PrintfQualifierSizeT; |
||
| 541 | break; |
||
| 542 | default: |
||
| 543 | qualifier = PrintfQualifierInt; /* default type */ |
||
| 544 | --i; |
||
| 995 | cejka | 545 | } |
| 1073 | cejka | 546 | |
| 547 | base = 10; |
||
| 548 | |||
| 974 | cejka | 549 | switch (c = fmt[i]) { |
| 550 | |||
| 551 | /* |
||
| 552 | * String and character conversions. |
||
| 553 | */ |
||
| 554 | case 's': |
||
| 1234 | cejka | 555 | if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) == EOF) { |
| 1616 | palkovsky | 556 | goto minus_out; |
| 974 | cejka | 557 | }; |
| 558 | |||
| 559 | counter += retval; |
||
| 1073 | cejka | 560 | j = i + 1; |
| 561 | goto next_char; |
||
| 974 | cejka | 562 | case 'c': |
| 1173 | cejka | 563 | c = va_arg(ap, unsigned int); |
| 1234 | cejka | 564 | if ((retval = print_char(c, width, flags, ps)) == EOF) { |
| 1616 | palkovsky | 565 | goto minus_out; |
| 974 | cejka | 566 | }; |
| 567 | |||
| 568 | counter += retval; |
||
| 1073 | cejka | 569 | j = i + 1; |
| 570 | goto next_char; |
||
| 974 | cejka | 571 | |
| 1073 | cejka | 572 | /* |
| 573 | * Integer values |
||
| 974 | cejka | 574 | */ |
| 1073 | cejka | 575 | case 'P': /* pointer */ |
| 576 | flags |= __PRINTF_FLAG_BIGCHARS; |
||
| 974 | cejka | 577 | case 'p': |
| 1073 | cejka | 578 | flags |= __PRINTF_FLAG_PREFIX; |
| 579 | base = 16; |
||
| 580 | qualifier = PrintfQualifierPointer; |
||
| 581 | break; |
||
| 582 | case 'b': |
||
| 583 | base = 2; |
||
| 974 | cejka | 584 | break; |
| 1073 | cejka | 585 | case 'o': |
| 586 | base = 8; |
||
| 974 | cejka | 587 | break; |
| 588 | case 'd': |
||
| 995 | cejka | 589 | case 'i': |
| 1073 | cejka | 590 | flags |= __PRINTF_FLAG_SIGNED; |
| 591 | case 'u': |
||
| 592 | break; |
||
| 974 | cejka | 593 | case 'X': |
| 1073 | cejka | 594 | flags |= __PRINTF_FLAG_BIGCHARS; |
| 974 | cejka | 595 | case 'x': |
| 1073 | cejka | 596 | base = 16; |
| 597 | break; |
||
| 598 | /* percentile itself */ |
||
| 599 | case '%': |
||
| 600 | j = i; |
||
| 601 | goto next_char; |
||
| 974 | cejka | 602 | /* |
| 603 | * Bad formatting. |
||
| 604 | */ |
||
| 605 | default: |
||
| 1073 | cejka | 606 | /* Unknown format |
| 607 | * now, the j is index of '%' so we will |
||
| 608 | * print whole bad format sequence |
||
| 609 | */ |
||
| 610 | goto next_char; |
||
| 611 | } |
||
| 612 | |||
| 613 | |||
| 614 | /* Print integers */ |
||
| 615 | /* print number */ |
||
| 616 | switch (qualifier) { |
||
| 617 | case PrintfQualifierByte: |
||
| 618 | size = sizeof(unsigned char); |
||
| 619 | number = (uint64_t)va_arg(ap, unsigned int); |
||
| 620 | break; |
||
| 621 | case PrintfQualifierShort: |
||
| 622 | size = sizeof(unsigned short); |
||
| 623 | number = (uint64_t)va_arg(ap, unsigned int); |
||
| 624 | break; |
||
| 625 | case PrintfQualifierInt: |
||
| 626 | size = sizeof(unsigned int); |
||
| 627 | number = (uint64_t)va_arg(ap, unsigned int); |
||
| 628 | break; |
||
| 629 | case PrintfQualifierLong: |
||
| 630 | size = sizeof(unsigned long); |
||
| 631 | number = (uint64_t)va_arg(ap, unsigned long); |
||
| 632 | break; |
||
| 633 | case PrintfQualifierLongLong: |
||
| 634 | size = sizeof(unsigned long long); |
||
| 635 | number = (uint64_t)va_arg(ap, unsigned long long); |
||
| 636 | break; |
||
| 637 | case PrintfQualifierPointer: |
||
| 638 | size = sizeof(void *); |
||
| 639 | number = (uint64_t)(unsigned long)va_arg(ap, void *); |
||
| 640 | break; |
||
| 641 | case PrintfQualifierSizeT: |
||
| 642 | size = sizeof(size_t); |
||
| 643 | number = (uint64_t)va_arg(ap, size_t); |
||
| 644 | break; |
||
| 645 | default: /* Unknown qualifier */ |
||
| 1616 | palkovsky | 646 | goto minus_out; |
| 1073 | cejka | 647 | |
| 974 | cejka | 648 | } |
| 1073 | cejka | 649 | |
| 650 | if (flags & __PRINTF_FLAG_SIGNED) { |
||
| 651 | if (number & (0x1 << (size*8 - 1))) { |
||
| 652 | flags |= __PRINTF_FLAG_NEGATIVE; |
||
| 653 | |||
| 654 | if (size == sizeof(uint64_t)) { |
||
| 655 | number = -((int64_t)number); |
||
| 656 | } else { |
||
| 657 | number = ~number; |
||
| 658 | number &= (~((0xFFFFFFFFFFFFFFFFll) << (size * 8))); |
||
| 659 | number++; |
||
| 660 | } |
||
| 661 | } |
||
| 662 | } |
||
| 663 | |||
| 1234 | cejka | 664 | if ((retval = print_number(number, width, precision, base, flags, ps)) == EOF ) { |
| 1616 | palkovsky | 665 | goto minus_out; |
| 1073 | cejka | 666 | }; |
| 667 | |||
| 668 | counter += retval; |
||
| 669 | j = i + 1; |
||
| 974 | cejka | 670 | } |
| 1073 | cejka | 671 | next_char: |
| 672 | |||
| 974 | cejka | 673 | ++i; |
| 674 | } |
||
| 675 | |||
| 975 | cejka | 676 | if (i > j) { |
| 1234 | cejka | 677 | if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) == EOF) { /* error */ |
| 1616 | palkovsky | 678 | goto minus_out; |
| 975 | cejka | 679 | } |
| 680 | counter += retval; |
||
| 681 | } |
||
| 682 | |||
| 1616 | palkovsky | 683 | async_serialize_end(); |
| 974 | cejka | 684 | return counter; |
| 1616 | palkovsky | 685 | minus_out: |
| 686 | async_serialize_end(); |
||
| 687 | return -counter; |
||
| 974 | cejka | 688 | } |
| 689 |