Subversion Repositories HelenOS

Rev

Rev 4055 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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