Subversion Repositories HelenOS

Rev

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