Subversion Repositories HelenOS

Rev

Rev 4209 | Rev 4213 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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