Subversion Repositories HelenOS

Rev

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