Subversion Repositories HelenOS

Rev

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