Subversion Repositories HelenOS

Rev

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

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