Subversion Repositories HelenOS-historic

Rev

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