Subversion Repositories HelenOS

Rev

Rev 4377 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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