Subversion Repositories HelenOS

Rev

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