Subversion Repositories HelenOS

Rev

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

Rev 3343 Rev 3397
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
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
/** @addtogroup libc
30
/** @addtogroup libc
31
 * @{
31
 * @{
32
 */
32
 */
33
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief   Printing functions.
35
 * @brief   Printing functions.
36
 */
36
 */
37
 
37
 
38
#include <unistd.h>
38
#include <unistd.h>
39
#include <stdio.h>
39
#include <stdio.h>
40
#include <io/printf_core.h>
40
#include <io/printf_core.h>
41
#include <ctype.h>
41
#include <ctype.h>
42
#include <string.h>
42
#include <string.h>
43
 
43
 
44
#define __PRINTF_FLAG_PREFIX        0x00000001  /**< show prefixes 0x or 0*/
44
#define __PRINTF_FLAG_PREFIX        0x00000001  /**< show prefixes 0x or 0*/
45
#define __PRINTF_FLAG_SIGNED        0x00000002  /**< signed / unsigned number */
45
#define __PRINTF_FLAG_SIGNED        0x00000002  /**< signed / unsigned number */
46
#define __PRINTF_FLAG_ZEROPADDED    0x00000004  /**< print leading zeroes */
46
#define __PRINTF_FLAG_ZEROPADDED    0x00000004  /**< print leading zeroes */
47
#define __PRINTF_FLAG_LEFTALIGNED   0x00000010  /**< align to left */
47
#define __PRINTF_FLAG_LEFTALIGNED   0x00000010  /**< align to left */
48
#define __PRINTF_FLAG_SHOWPLUS      0x00000020  /**< always show + sign */
48
#define __PRINTF_FLAG_SHOWPLUS      0x00000020  /**< always show + sign */
49
#define __PRINTF_FLAG_SPACESIGN     0x00000040  /**< print space instead of plus */
49
#define __PRINTF_FLAG_SPACESIGN     0x00000040  /**< print space instead of plus */
50
#define __PRINTF_FLAG_BIGCHARS      0x00000080  /**< show big characters */
50
#define __PRINTF_FLAG_BIGCHARS      0x00000080  /**< show big characters */
51
#define __PRINTF_FLAG_NEGATIVE      0x00000100  /**< number has - sign */
51
#define __PRINTF_FLAG_NEGATIVE      0x00000100  /**< number has - sign */
52
 
52
 
53
#define PRINT_NUMBER_BUFFER_SIZE    (64+5)      /**< Buffer big enought for 64 bit number
53
#define PRINT_NUMBER_BUFFER_SIZE    (64+5)      /**< Buffer big enought for 64 bit number
54
                             * printed in base 2, sign, prefix and
54
                             * printed in base 2, sign, prefix and
55
                             * 0 to terminate string.. (last one is only for better testing
55
                             * 0 to terminate string.. (last one is only for better testing
56
                             * end of buffer by zero-filling subroutine)
56
                             * end of buffer by zero-filling subroutine)
57
                             */
57
                             */
58
/** Enumeration of possible arguments types.
58
/** Enumeration of possible arguments types.
59
 */
59
 */
60
typedef enum {
60
typedef enum {
61
    PrintfQualifierByte = 0,
61
    PrintfQualifierByte = 0,
62
    PrintfQualifierShort,
62
    PrintfQualifierShort,
63
    PrintfQualifierInt,
63
    PrintfQualifierInt,
64
    PrintfQualifierLong,
64
    PrintfQualifierLong,
65
    PrintfQualifierLongLong,
65
    PrintfQualifierLongLong,
66
    PrintfQualifierSizeT,
66
    PrintfQualifierSizeT,
67
    PrintfQualifierPointer
67
    PrintfQualifierPointer
68
} qualifier_t;
68
} qualifier_t;
69
 
69
 
70
static char digits_small[] = "0123456789abcdef";    /**< Small hexadecimal characters */
70
static char digits_small[] = "0123456789abcdef";    /**< Small hexadecimal characters */
71
static char digits_big[] = "0123456789ABCDEF";      /**< Big hexadecimal characters */
71
static char digits_big[] = "0123456789ABCDEF";      /**< Big hexadecimal characters */
72
 
72
 
73
/** Print count chars from buffer without adding newline
73
/** Print count chars from buffer without adding newline
74
 * @param buf Buffer with size at least count bytes - NULL pointer NOT allowed!
74
 * @param buf Buffer with size at least count bytes - NULL pointer NOT allowed!
75
 * @param count
75
 * @param count
76
 * @param ps output method and its data
76
 * @param ps output method and its data
77
 * @return number of printed characters
77
 * @return number of printed characters
78
 */
78
 */
79
static int printf_putnchars(const char * buf, size_t count,
79
static int printf_putnchars(const char * buf, size_t count,
80
    struct printf_spec *ps)
80
    struct printf_spec *ps)
81
{
81
{
82
    return ps->write((void *)buf, count, ps->data);
82
    return ps->write((void *)buf, count, ps->data);
83
}
83
}
84
 
84
 
85
/** Print string without added newline
85
/** Print string without added newline
86
 * @param str string to print
86
 * @param str string to print
87
 * @param ps write function specification and support data
87
 * @param ps write function specification and support data
88
 * @return number of printed characters
88
 * @return number of printed characters
89
 */
89
 */
90
static int printf_putstr(const char * str, struct printf_spec *ps)
90
static int printf_putstr(const char * str, struct printf_spec *ps)
91
{
91
{
92
    size_t count;
92
    size_t count;
93
   
93
   
94
    if (str == NULL)
94
    if (str == NULL)
95
        return printf_putnchars("(NULL)", 6, ps);
95
        return printf_putnchars("(NULL)", 6, ps);
96
 
96
 
97
    for (count = 0; str[count] != 0; count++);
97
    count = strlen(str);
98
 
98
 
99
    if (ps->write((void *) str, count, ps->data) == count)
99
    return ps->write((void *) str, count, ps->data);
100
        return 0;
-
 
101
   
-
 
102
    return EOF;
-
 
103
}
100
}
104
 
101
 
105
/** Print one character to output
102
/** Print one character to output
106
 * @param c one character
103
 * @param c one character
107
 * @param ps output method
104
 * @param ps output method
108
 * @return number of printed characters
105
 * @return number of printed characters
109
 */
106
 */
110
static int printf_putchar(int c, struct printf_spec *ps)
107
static int printf_putchar(int c, struct printf_spec *ps)
111
{
108
{
112
    unsigned char ch = c;
109
    unsigned char ch = c;
113
   
110
   
114
    return ps->write((void *) &ch, 1, ps->data);
111
    return ps->write((void *) &ch, 1, ps->data);
115
}
112
}
116
 
113
 
117
/** Print one formatted character
114
/** Print one formatted character
118
 * @param c character to print
115
 * @param c character to print
119
 * @param width
116
 * @param width
120
 * @param flags
117
 * @param flags
121
 * @return number of printed characters
118
 * @return number of printed characters
122
 */
119
 */
123
static int print_char(char c, int width, uint64_t flags, struct printf_spec *ps)
120
static int print_char(char c, int width, uint64_t flags, struct printf_spec *ps)
124
{
121
{
125
    int counter = 0;
122
    int counter = 0;
126
   
123
   
127
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
124
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
128
        /*
125
        /*
129
         * One space is consumed by the character itself, hence the
126
         * One space is consumed by the character itself, hence the
130
         * predecrement.
127
         * predecrement.
131
         */
128
         */
132
        while (--width > 0) {
129
        while (--width > 0) {
133
            if (printf_putchar(' ', ps) > 0)   
130
            if (printf_putchar(' ', ps) > 0)   
134
                ++counter;
131
                ++counter;
135
        }
132
        }
136
    }
133
    }
137
   
134
   
138
    if (printf_putchar(c, ps) > 0)
135
    if (printf_putchar(c, ps) > 0)
139
        counter++;
136
        counter++;
140
    /*
137
    /*
141
     * One space is consumed by the character itself, hence the
138
     * One space is consumed by the character itself, hence the
142
     * predecrement.
139
     * predecrement.
143
     */
140
     */
144
    while (--width > 0) {
141
    while (--width > 0) {
145
        if (printf_putchar(' ', ps) > 0)
142
        if (printf_putchar(' ', ps) > 0)
146
            ++counter;
143
            ++counter;
147
    }
144
    }
148
   
145
   
149
    return ++counter;
146
    return ++counter;
150
}
147
}
151
 
148
 
152
/** Print one string
149
/** Print one string
153
 * @param s string
150
 * @param s string
154
 * @param width
151
 * @param width
155
 * @param precision
152
 * @param precision
156
 * @param flags
153
 * @param flags
157
 * @return number of printed characters
154
 * @return number of printed characters
158
 */
155
 */
159
static int print_string(char *s, int width, int precision, uint64_t flags,
156
static int print_string(char *s, int width, int precision, uint64_t flags,
160
    struct printf_spec *ps)
157
    struct printf_spec *ps)
161
{
158
{
162
    int counter = 0;
159
    int counter = 0;
163
    size_t size;
160
    size_t size;
164
    int retval;
161
    int retval;
165
 
162
 
166
    if (s == NULL) {
163
    if (s == NULL) {
167
        return printf_putstr("(NULL)", ps);
164
        return printf_putstr("(NULL)", ps);
168
    }
165
    }
169
   
166
   
170
    size = strlen(s);
167
    size = strlen(s);
171
 
168
 
172
    /* print leading spaces */
169
    /* print leading spaces */
173
 
170
 
174
    if (precision == 0)
171
    if (precision == 0)
175
        precision = size;
172
        precision = size;
176
 
173
 
177
    width -= precision;
174
    width -= precision;
178
   
175
   
179
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
176
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
180
        while (width-- > 0) {  
177
        while (width-- > 0) {  
181
            if (printf_putchar(' ', ps) == 1)  
178
            if (printf_putchar(' ', ps) == 1)  
182
                counter++;
179
                counter++;
183
        }
180
        }
184
    }
181
    }
185
 
182
 
186
    if ((retval = printf_putnchars(s, size < precision ? size : precision,
183
    if ((retval = printf_putnchars(s, size < precision ? size : precision,
187
        ps)) < 0) {
184
        ps)) < 0) {
188
        return -counter;
185
        return -counter;
189
    }
186
    }
190
 
187
 
191
    counter += retval; 
188
    counter += retval; 
192
 
189
 
193
    while (width-- > 0) {
190
    while (width-- > 0) {
194
        if (printf_putchar(' ', ps) == 1)  
191
        if (printf_putchar(' ', ps) == 1)  
195
            ++counter;
192
            ++counter;
196
    }
193
    }
197
   
194
   
198
    return counter;
195
    return counter;
199
}
196
}
200
 
197
 
201
 
198
 
202
/** Print number in given base
199
/** Print number in given base
203
 *
200
 *
204
 * Print significant digits of a number in given
201
 * Print significant digits of a number in given
205
 * base.
202
 * base.
206
 *
203
 *
207
 * @param num  Number to print.
204
 * @param num  Number to print.
208
 * @param width
205
 * @param width
209
 * @param precision
206
 * @param precision
210
 * @param base Base to print the number in (should
207
 * @param base Base to print the number in (should
211
 *             be in range 2 .. 16).
208
 *             be in range 2 .. 16).
212
 * @param flags output modifiers
209
 * @param flags output modifiers
213
 * @return number of printed characters
210
 * @return number of printed characters
214
 *
211
 *
215
 */
212
 */
216
static int print_number(uint64_t num, int width, int precision, int base,
213
static int print_number(uint64_t num, int width, int precision, int base,
217
    uint64_t flags, struct printf_spec *ps)
214
    uint64_t flags, struct printf_spec *ps)
218
{
215
{
219
    char *digits = digits_small;
216
    char *digits = digits_small;
220
    char d[PRINT_NUMBER_BUFFER_SIZE];   /* this is good enough even for
217
    char d[PRINT_NUMBER_BUFFER_SIZE];   /* this is good enough even for
221
                         * base == 2, prefix and sign */
218
                         * base == 2, prefix and sign */
222
    char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1];
219
    char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1];
223
    int size = 0; /* size of number with all prefixes and signs */
220
    int size = 0; /* size of number with all prefixes and signs */
224
    int number_size; /* size of plain number */
221
    int number_size; /* size of plain number */
225
    char sgn;
222
    char sgn;
226
    int retval;
223
    int retval;
227
    int counter = 0;
224
    int counter = 0;
228
   
225
   
229
    if (flags & __PRINTF_FLAG_BIGCHARS)
226
    if (flags & __PRINTF_FLAG_BIGCHARS)
230
        digits = digits_big;   
227
        digits = digits_big;   
231
   
228
   
232
    *ptr-- = 0; /* Put zero at end of string */
229
    *ptr-- = 0; /* Put zero at end of string */
233
 
230
 
234
    if (num == 0) {
231
    if (num == 0) {
235
        *ptr-- = '0';
232
        *ptr-- = '0';
236
        size++;
233
        size++;
237
    } else {
234
    } else {
238
        do {
235
        do {
239
            *ptr-- = digits[num % base];
236
            *ptr-- = digits[num % base];
240
            size++;
237
            size++;
241
        } while (num /= base);
238
        } while (num /= base);
242
    }
239
    }
243
   
240
   
244
    number_size = size;
241
    number_size = size;
245
 
242
 
246
    /*
243
    /*
247
     * Collect sum of all prefixes/signs/... to calculate padding and
244
     * Collect sum of all prefixes/signs/... to calculate padding and
248
     * leading zeroes
245
     * leading zeroes
249
     */
246
     */
250
    if (flags & __PRINTF_FLAG_PREFIX) {
247
    if (flags & __PRINTF_FLAG_PREFIX) {
251
        switch(base) {
248
        switch(base) {
252
        case 2: /* Binary formating is not standard, but usefull */
249
        case 2: /* Binary formating is not standard, but usefull */
253
            size += 2;
250
            size += 2;
254
            break;
251
            break;
255
        case 8:
252
        case 8:
256
            size++;
253
            size++;
257
            break;
254
            break;
258
        case 16:
255
        case 16:
259
            size += 2;
256
            size += 2;
260
            break;
257
            break;
261
        }
258
        }
262
    }
259
    }
263
 
260
 
264
    sgn = 0;
261
    sgn = 0;
265
    if (flags & __PRINTF_FLAG_SIGNED) {
262
    if (flags & __PRINTF_FLAG_SIGNED) {
266
        if (flags & __PRINTF_FLAG_NEGATIVE) {
263
        if (flags & __PRINTF_FLAG_NEGATIVE) {
267
            sgn = '-';
264
            sgn = '-';
268
            size++;
265
            size++;
269
        } else if (flags & __PRINTF_FLAG_SHOWPLUS) {
266
        } else if (flags & __PRINTF_FLAG_SHOWPLUS) {
270
            sgn = '+';
267
            sgn = '+';
271
            size++;
268
            size++;
272
        } else if (flags & __PRINTF_FLAG_SPACESIGN) {
269
        } else if (flags & __PRINTF_FLAG_SPACESIGN) {
273
            sgn = ' ';
270
            sgn = ' ';
274
            size++;
271
            size++;
275
        }
272
        }
276
    }
273
    }
277
 
274
 
278
    if (flags & __PRINTF_FLAG_LEFTALIGNED) {
275
    if (flags & __PRINTF_FLAG_LEFTALIGNED) {
279
        flags &= ~__PRINTF_FLAG_ZEROPADDED;
276
        flags &= ~__PRINTF_FLAG_ZEROPADDED;
280
    }
277
    }
281
 
278
 
282
    /*
279
    /*
283
     * If number is leftaligned or precision is specified then zeropadding
280
     * If number is leftaligned or precision is specified then zeropadding
284
     * is ignored.
281
     * is ignored.
285
     */
282
     */
286
    if (flags & __PRINTF_FLAG_ZEROPADDED) {
283
    if (flags & __PRINTF_FLAG_ZEROPADDED) {
287
        if ((precision == 0) && (width > size)) {
284
        if ((precision == 0) && (width > size)) {
288
            precision = width - size + number_size;
285
            precision = width - size + number_size;
289
        }
286
        }
290
    }
287
    }
291
 
288
 
292
    /* print leading spaces */
289
    /* print leading spaces */
293
 
290
 
294
    /* We must print whole number not only a part. */
291
    /* We must print whole number not only a part. */
295
    if (number_size > precision)
292
    if (number_size > precision)
296
        precision = number_size;
293
        precision = number_size;
297
 
294
 
298
    width -= precision + size - number_size;
295
    width -= precision + size - number_size;
299
   
296
   
300
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
297
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
301
        while (width-- > 0) {  
298
        while (width-- > 0) {  
302
            if (printf_putchar(' ', ps) == 1)  
299
            if (printf_putchar(' ', ps) == 1)  
303
                counter++;
300
                counter++;
304
        }
301
        }
305
    }
302
    }
306
   
303
   
307
    /* print sign */
304
    /* print sign */
308
    if (sgn) {
305
    if (sgn) {
309
        if (printf_putchar(sgn, ps) == 1)
306
        if (printf_putchar(sgn, ps) == 1)
310
            counter++;
307
            counter++;
311
    }
308
    }
312
   
309
   
313
    /* print prefix */
310
    /* print prefix */
314
   
311
   
315
    if (flags & __PRINTF_FLAG_PREFIX) {
312
    if (flags & __PRINTF_FLAG_PREFIX) {
316
        switch(base) {
313
        switch(base) {
317
        case 2: /* Binary formating is not standard, but usefull */
314
        case 2: /* Binary formating is not standard, but usefull */
318
            if (printf_putchar('0', ps) == 1)
315
            if (printf_putchar('0', ps) == 1)
319
                counter++;
316
                counter++;
320
            if (flags & __PRINTF_FLAG_BIGCHARS) {
317
            if (flags & __PRINTF_FLAG_BIGCHARS) {
321
                if (printf_putchar('B', ps) == 1)
318
                if (printf_putchar('B', ps) == 1)
322
                    counter++;
319
                    counter++;
323
            } else {
320
            } else {
324
                if (printf_putchar('b', ps) == 1)
321
                if (printf_putchar('b', ps) == 1)
325
                    counter++;
322
                    counter++;
326
            }
323
            }
327
            break;
324
            break;
328
        case 8:
325
        case 8:
329
            if (printf_putchar('o', ps) == 1)
326
            if (printf_putchar('o', ps) == 1)
330
                counter++;
327
                counter++;
331
            break;
328
            break;
332
        case 16:
329
        case 16:
333
            if (printf_putchar('0', ps) == 1)
330
            if (printf_putchar('0', ps) == 1)
334
                counter++;
331
                counter++;
335
            if (flags & __PRINTF_FLAG_BIGCHARS) {
332
            if (flags & __PRINTF_FLAG_BIGCHARS) {
336
                if (printf_putchar('X', ps) == 1)
333
                if (printf_putchar('X', ps) == 1)
337
                    counter++;
334
                    counter++;
338
            } else {
335
            } else {
339
                if (printf_putchar('x', ps) == 1)
336
                if (printf_putchar('x', ps) == 1)
340
                    counter++;
337
                    counter++;
341
            }
338
            }
342
            break;
339
            break;
343
        }
340
        }
344
    }
341
    }
345
 
342
 
346
    /* print leading zeroes */
343
    /* print leading zeroes */
347
    precision -= number_size;
344
    precision -= number_size;
348
    while (precision-- > 0) {  
345
    while (precision-- > 0) {  
349
        if (printf_putchar('0', ps) == 1)
346
        if (printf_putchar('0', ps) == 1)
350
            counter++;
347
            counter++;
351
    }
348
    }
352
 
349
 
353
   
350
   
354
    /* print number itself */
351
    /* print number itself */
355
 
352
 
356
    if ((retval = printf_putstr(++ptr, ps)) > 0) {
353
    if ((retval = printf_putstr(++ptr, ps)) > 0) {
357
        counter += retval;
354
        counter += retval;
358
    }
355
    }
359
   
356
   
360
    /* print ending spaces */
357
    /* print ending spaces */
361
   
358
   
362
    while (width-- > 0) {  
359
    while (width-- > 0) {  
363
        if (printf_putchar(' ', ps) == 1)  
360
        if (printf_putchar(' ', ps) == 1)  
364
            counter++;
361
            counter++;
365
    }
362
    }
366
 
363
 
367
    return counter;
364
    return counter;
368
}
365
}
369
 
366
 
370
 
367
 
371
/** Print formatted string.
368
/** Print formatted string.
372
 *
369
 *
373
 * Print string formatted according to the fmt parameter and variadic arguments.
370
 * Print string formatted according to the fmt parameter and variadic arguments.
374
 * Each formatting directive must have the following form:
371
 * Each formatting directive must have the following form:
375
 *
372
 *
376
 *  \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION
373
 *  \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION
377
 *
374
 *
378
 * FLAGS:@n
375
 * FLAGS:@n
379
 *  - "#" Force to print prefix.
376
 *  - "#" Force to print prefix.
380
 *  For conversion \%o the prefix is 0, for %x and \%X prefixes are 0x and
377
 *  For conversion \%o the prefix is 0, for %x and \%X prefixes are 0x and
381
 *  0X and for conversion \%b the prefix is 0b.
378
 *  0X and for conversion \%b the prefix is 0b.
382
 *
379
 *
383
 *  - "-"   Align to left.
380
 *  - "-"   Align to left.
384
 *
381
 *
385
 *  - "+"   Print positive sign just as negative.
382
 *  - "+"   Print positive sign just as negative.
386
 *
383
 *
387
 *  - " "   If the printed number is positive and "+" flag is not set,
384
 *  - " "   If the printed number is positive and "+" flag is not set,
388
 *      print space in place of sign.
385
 *      print space in place of sign.
389
 *
386
 *
390
 *  - "0"   Print 0 as padding instead of spaces. Zeroes are placed between
387
 *  - "0"   Print 0 as padding instead of spaces. Zeroes are placed between
391
 *      sign and the rest of the number. This flag is ignored if "-"
388
 *      sign and the rest of the number. This flag is ignored if "-"
392
 *      flag is specified.
389
 *      flag is specified.
393
 *
390
 *
394
 * WIDTH:@n
391
 * WIDTH:@n
395
 *  - Specify minimal width of printed argument. If it is bigger, width is
392
 *  - Specify minimal width of printed argument. If it is bigger, width is
396
 *    ignored. If width is specified with a "*" character instead of number,
393
 *    ignored. If width is specified with a "*" character instead of number,
397
 *    width is taken from parameter list. And integer parameter is expected
394
 *    width is taken from parameter list. And integer parameter is expected
398
 *    before parameter for processed conversion specification. If this value
395
 *    before parameter for processed conversion specification. If this value
399
 *    is negative its absolute value is taken and the "-" flag is set.
396
 *    is negative its absolute value is taken and the "-" flag is set.
400
 *
397
 *
401
 * PRECISION:@n
398
 * PRECISION:@n
402
 *  - Value precision. For numbers it specifies minimum valid numbers.
399
 *  - Value precision. For numbers it specifies minimum valid numbers.
403
 *    Smaller numbers are printed with leading zeroes. Bigger numbers are
400
 *    Smaller numbers are printed with leading zeroes. Bigger numbers are
404
 *    not affected. Strings with more than precision characters are cut off.
401
 *    not affected. Strings with more than precision characters are cut off.
405
 *    Just as with width, an "*" can be used used instead of a number. An
402
 *    Just as with width, an "*" can be used used instead of a number. An
406
 *    integer value is then expected in parameters. When both width and
403
 *    integer value is then expected in parameters. When both width and
407
 *    precision are specified using "*", the first parameter is used for
404
 *    precision are specified using "*", the first parameter is used for
408
 *    width and the second one for precision.
405
 *    width and the second one for precision.
409
 *
406
 *
410
 * TYPE:@n
407
 * TYPE:@n
411
 *  - "hh"  Signed or unsigned char.@n
408
 *  - "hh"  Signed or unsigned char.@n
412
 *  - "h"   Signed or usigned short.@n
409
 *  - "h"   Signed or usigned short.@n
413
 *  - ""    Signed or usigned int (default value).@n
410
 *  - ""    Signed or usigned int (default value).@n
414
 *  - "l"   Signed or usigned long int.@n
411
 *  - "l"   Signed or usigned long int.@n
415
 *  - "ll"  Signed or usigned long long int.@n
412
 *  - "ll"  Signed or usigned long long int.@n
416
 *  - "z"   Type size_t.@n
413
 *  - "z"   Type size_t.@n
417
 *
414
 *
418
 *
415
 *
419
 * CONVERSION:@n
416
 * CONVERSION:@n
420
 *  - % Print percentile character itself.
417
 *  - % Print percentile character itself.
421
 *
418
 *
422
 *  - c Print single character.
419
 *  - c Print single character.
423
 *
420
 *
424
 *  - s Print zero terminated string. If a NULL value is passed as
421
 *  - s Print zero terminated string. If a NULL value is passed as
425
 *      value, "(NULL)" is printed instead.
422
 *      value, "(NULL)" is printed instead.
426
 *
423
 *
427
 *  - P, p  Print value of a pointer. Void * value is expected and it is
424
 *  - P, p  Print value of a pointer. Void * value is expected and it is
428
 *      printed in hexadecimal notation with prefix (as with '\%#X' or
425
 *      printed in hexadecimal notation with prefix (as with '\%#X' or
429
 *      '\%#x' for 32bit or '\%#X' or '\%#x' for 64bit long pointers).
426
 *      '\%#x' for 32bit or '\%#X' or '\%#x' for 64bit long pointers).
430
 *
427
 *
431
 *  - b Print value as unsigned binary number. Prefix is not printed by
428
 *  - b Print value as unsigned binary number. Prefix is not printed by
432
 *      default. (Nonstandard extension.)
429
 *      default. (Nonstandard extension.)
433
 *
430
 *
434
 *  - o Print value as unsigned octal number. Prefix is not printed by
431
 *  - o Print value as unsigned octal number. Prefix is not printed by
435
 *      default.
432
 *      default.
436
 *
433
 *
437
 *  - d, i  Print signed decimal number. There is no difference between d
434
 *  - d, i  Print signed decimal number. There is no difference between d
438
 *      and i conversion.
435
 *      and i conversion.
439
 *
436
 *
440
 *  - u Print unsigned decimal number.
437
 *  - u Print unsigned decimal number.
441
 *
438
 *
442
 *  - X, x  Print hexadecimal number with upper- or lower-case. Prefix is
439
 *  - X, x  Print hexadecimal number with upper- or lower-case. Prefix is
443
 *      not printed by default.
440
 *      not printed by default.
444
 *
441
 *
445
 * All other characters from fmt except the formatting directives are printed in
442
 * All other characters from fmt except the formatting directives are printed in
446
 * verbatim.
443
 * verbatim.
447
 *
444
 *
448
 * @param fmt Formatting NULL terminated string.
445
 * @param fmt Formatting NULL terminated string.
449
 * @return Number of printed characters or negative value on failure.
446
 * @return Number of printed characters or negative value on failure.
450
 */
447
 */
451
int printf_core(const char *fmt, struct printf_spec *ps, va_list ap)
448
int printf_core(const char *fmt, struct printf_spec *ps, va_list ap)
452
{
449
{
453
    /* i is the index of the currently processed char from fmt */
450
    /* i is the index of the currently processed char from fmt */
454
    int i = 0;
451
    int i = 0;
455
    /* j is the index to the first not printed nonformating character */
452
    /* j is the index to the first not printed nonformating character */
456
    int j = 0;
453
    int j = 0;
457
 
454
 
458
    int end;
455
    int end;
459
    int counter;    /* counter of printed characters */
456
    int counter;    /* counter of printed characters */
460
    int retval; /* used to store return values from called functions */
457
    int retval; /* used to store return values from called functions */
461
    char c;
458
    char c;
462
    qualifier_t qualifier;  /* type of argument */
459
    qualifier_t qualifier;  /* type of argument */
463
    int base;   /* base in which will be a numeric parameter printed */
460
    int base;   /* base in which will be a numeric parameter printed */
464
    uint64_t number; /* argument value */
461
    uint64_t number; /* argument value */
465
    size_t  size;   /* byte size of integer parameter */
462
    size_t  size;   /* byte size of integer parameter */
466
    int width, precision;
463
    int width, precision;
467
    uint64_t flags;
464
    uint64_t flags;
468
   
465
   
469
    counter = 0;
466
    counter = 0;
470
   
467
   
471
    while ((c = fmt[i])) {
468
    while ((c = fmt[i])) {
472
        /* control character */
469
        /* control character */
473
        if (c == '%' ) {
470
        if (c == '%' ) {
474
            /* print common characters if any processed */ 
471
            /* print common characters if any processed */ 
475
            if (i > j) {
472
            if (i > j) {
476
                if ((retval = printf_putnchars(&fmt[j],
473
                if ((retval = printf_putnchars(&fmt[j],
477
                    (size_t)(i - j), ps)) < 0) { /* error */
474
                    (size_t)(i - j), ps)) < 0) { /* error */
478
                    goto minus_out;
475
                    goto minus_out;
479
                }
476
                }
480
                counter += retval;
477
                counter += retval;
481
            }
478
            }
482
       
479
       
483
            j = i;
480
            j = i;
484
            /* parse modifiers */
481
            /* parse modifiers */
485
            flags = 0;
482
            flags = 0;
486
            end = 0;
483
            end = 0;
487
           
484
           
488
            do {
485
            do {
489
                ++i;
486
                ++i;
490
                switch (c = fmt[i]) {
487
                switch (c = fmt[i]) {
491
                case '#':
488
                case '#':
492
                    flags |= __PRINTF_FLAG_PREFIX;
489
                    flags |= __PRINTF_FLAG_PREFIX;
493
                    break;
490
                    break;
494
                case '-':
491
                case '-':
495
                    flags |= __PRINTF_FLAG_LEFTALIGNED;
492
                    flags |= __PRINTF_FLAG_LEFTALIGNED;
496
                    break;
493
                    break;
497
                case '+':
494
                case '+':
498
                    flags |= __PRINTF_FLAG_SHOWPLUS;
495
                    flags |= __PRINTF_FLAG_SHOWPLUS;
499
                    break;
496
                    break;
500
                case ' ':
497
                case ' ':
501
                    flags |= __PRINTF_FLAG_SPACESIGN;
498
                    flags |= __PRINTF_FLAG_SPACESIGN;
502
                    break;
499
                    break;
503
                case '0':
500
                case '0':
504
                    flags |= __PRINTF_FLAG_ZEROPADDED;
501
                    flags |= __PRINTF_FLAG_ZEROPADDED;
505
                    break;
502
                    break;
506
                default:
503
                default:
507
                    end = 1;
504
                    end = 1;
508
                }; 
505
                }; 
509
               
506
               
510
            } while (end == 0);
507
            } while (end == 0);
511
           
508
           
512
            /* width & '*' operator */
509
            /* width & '*' operator */
513
            width = 0;
510
            width = 0;
514
            if (isdigit(fmt[i])) {
511
            if (isdigit(fmt[i])) {
515
                while (isdigit(fmt[i])) {
512
                while (isdigit(fmt[i])) {
516
                    width *= 10;
513
                    width *= 10;
517
                    width += fmt[i++] - '0';
514
                    width += fmt[i++] - '0';
518
                }
515
                }
519
            } else if (fmt[i] == '*') {
516
            } else if (fmt[i] == '*') {
520
                /* get width value from argument list*/
517
                /* get width value from argument list*/
521
                i++;
518
                i++;
522
                width = (int)va_arg(ap, int);
519
                width = (int)va_arg(ap, int);
523
                if (width < 0) {
520
                if (width < 0) {
524
                    /* negative width sets '-' flag */
521
                    /* negative width sets '-' flag */
525
                    width *= -1;
522
                    width *= -1;
526
                    flags |= __PRINTF_FLAG_LEFTALIGNED;
523
                    flags |= __PRINTF_FLAG_LEFTALIGNED;
527
                }
524
                }
528
            }
525
            }
529
           
526
           
530
            /* precision and '*' operator */   
527
            /* precision and '*' operator */   
531
            precision = 0;
528
            precision = 0;
532
            if (fmt[i] == '.') {
529
            if (fmt[i] == '.') {
533
                ++i;
530
                ++i;
534
                if (isdigit(fmt[i])) {
531
                if (isdigit(fmt[i])) {
535
                    while (isdigit(fmt[i])) {
532
                    while (isdigit(fmt[i])) {
536
                        precision *= 10;
533
                        precision *= 10;
537
                        precision += fmt[i++] - '0';
534
                        precision += fmt[i++] - '0';
538
                    }
535
                    }
539
                } else if (fmt[i] == '*') {
536
                } else if (fmt[i] == '*') {
540
                    /* get precision value from argument */
537
                    /* get precision value from argument */
541
                    i++;
538
                    i++;
542
                    precision = (int)va_arg(ap, int);
539
                    precision = (int)va_arg(ap, int);
543
                    if (precision < 0) {
540
                    if (precision < 0) {
544
                        /* negative precision ignored */
541
                        /* negative precision ignored */
545
                        precision = 0;
542
                        precision = 0;
546
                    }
543
                    }
547
                }
544
                }
548
            }
545
            }
549
 
546
 
550
            switch (fmt[i++]) {
547
            switch (fmt[i++]) {
551
            /** @todo unimplemented qualifiers:
548
            /** @todo unimplemented qualifiers:
552
             * t ptrdiff_t - ISO C 99
549
             * t ptrdiff_t - ISO C 99
553
             */
550
             */
554
            case 'h':   /* char or short */
551
            case 'h':   /* char or short */
555
                qualifier = PrintfQualifierShort;
552
                qualifier = PrintfQualifierShort;
556
                if (fmt[i] == 'h') {
553
                if (fmt[i] == 'h') {
557
                    i++;
554
                    i++;
558
                    qualifier = PrintfQualifierByte;
555
                    qualifier = PrintfQualifierByte;
559
                }
556
                }
560
                break;
557
                break;
561
            case 'l':   /* long or long long*/
558
            case 'l':   /* long or long long*/
562
                qualifier = PrintfQualifierLong;
559
                qualifier = PrintfQualifierLong;
563
                if (fmt[i] == 'l') {
560
                if (fmt[i] == 'l') {
564
                    i++;
561
                    i++;
565
                    qualifier = PrintfQualifierLongLong;
562
                    qualifier = PrintfQualifierLongLong;
566
                }
563
                }
567
                break;
564
                break;
568
            case 'z':   /* size_t */
565
            case 'z':   /* size_t */
569
                qualifier = PrintfQualifierSizeT;
566
                qualifier = PrintfQualifierSizeT;
570
                break;
567
                break;
571
            default:
568
            default:
572
                /* set default type */
569
                /* set default type */
573
                qualifier = PrintfQualifierInt;
570
                qualifier = PrintfQualifierInt;
574
                --i;
571
                --i;
575
            }  
572
            }  
576
           
573
           
577
            base = 10;
574
            base = 10;
578
 
575
 
579
            switch (c = fmt[i]) {
576
            switch (c = fmt[i]) {
580
 
577
 
581
            /*
578
            /*
582
             * String and character conversions.
579
             * String and character conversions.
583
             */
580
             */
584
            case 's':
581
            case 's':
585
                if ((retval = print_string(va_arg(ap, char*),
582
                if ((retval = print_string(va_arg(ap, char*),
586
                    width, precision, flags, ps)) < 0) {
583
                    width, precision, flags, ps)) < 0) {
587
                    goto minus_out;
584
                    goto minus_out;
588
                }
585
                }
589
                   
586
                   
590
                counter += retval;
587
                counter += retval;
591
                j = i + 1;
588
                j = i + 1;
592
                goto next_char;
589
                goto next_char;
593
            case 'c':
590
            case 'c':
594
                c = va_arg(ap, unsigned int);
591
                c = va_arg(ap, unsigned int);
595
                retval = print_char(c, width, flags, ps);
592
                retval = print_char(c, width, flags, ps);
596
                if (retval < 0) {
593
                if (retval < 0) {
597
                    goto minus_out;
594
                    goto minus_out;
598
                }
595
                }
599
 
596
 
600
                counter += retval;
597
                counter += retval;
601
                j = i + 1;
598
                j = i + 1;
602
                goto next_char;
599
                goto next_char;
603
 
600
 
604
            /*
601
            /*
605
             * Integer values
602
             * Integer values
606
             */
603
             */
607
            case 'P': /* pointer */
604
            case 'P': /* pointer */
608
                    flags |= __PRINTF_FLAG_BIGCHARS;
605
                    flags |= __PRINTF_FLAG_BIGCHARS;
609
            case 'p':
606
            case 'p':
610
                flags |= __PRINTF_FLAG_PREFIX;
607
                flags |= __PRINTF_FLAG_PREFIX;
611
                base = 16;
608
                base = 16;
612
                qualifier = PrintfQualifierPointer;
609
                qualifier = PrintfQualifierPointer;
613
                break; 
610
                break; 
614
            case 'b':
611
            case 'b':
615
                base = 2;
612
                base = 2;
616
                break;
613
                break;
617
            case 'o':
614
            case 'o':
618
                base = 8;
615
                base = 8;
619
                break;
616
                break;
620
            case 'd':
617
            case 'd':
621
            case 'i':
618
            case 'i':
622
                flags |= __PRINTF_FLAG_SIGNED;  
619
                flags |= __PRINTF_FLAG_SIGNED;  
623
            case 'u':
620
            case 'u':
624
                break;
621
                break;
625
            case 'X':
622
            case 'X':
626
                flags |= __PRINTF_FLAG_BIGCHARS;
623
                flags |= __PRINTF_FLAG_BIGCHARS;
627
            case 'x':
624
            case 'x':
628
                base = 16;
625
                base = 16;
629
                break;
626
                break;
630
            /* percentile itself */
627
            /* percentile itself */
631
            case '%':
628
            case '%':
632
                j = i;
629
                j = i;
633
                goto next_char;
630
                goto next_char;
634
            /*
631
            /*
635
             * Bad formatting.
632
             * Bad formatting.
636
             */
633
             */
637
            default:
634
            default:
638
                /*
635
                /*
639
                 * Unknown format. Now, j is the index of '%',
636
                 * Unknown format. Now, j is the index of '%',
640
                 * so we will print the whole bad format
637
                 * so we will print the whole bad format
641
                 * sequence.
638
                 * sequence.
642
                 */
639
                 */
643
                goto next_char;    
640
                goto next_char;    
644
            }
641
            }
645
       
642
       
646
       
643
       
647
            /* Print integers */
644
            /* Print integers */
648
            switch (qualifier) {
645
            switch (qualifier) {
649
            case PrintfQualifierByte:
646
            case PrintfQualifierByte:
650
                size = sizeof(unsigned char);
647
                size = sizeof(unsigned char);
651
                number = (uint64_t)va_arg(ap, unsigned int);
648
                number = (uint64_t)va_arg(ap, unsigned int);
652
                break;
649
                break;
653
            case PrintfQualifierShort:
650
            case PrintfQualifierShort:
654
                size = sizeof(unsigned short);
651
                size = sizeof(unsigned short);
655
                number = (uint64_t)va_arg(ap, unsigned int);
652
                number = (uint64_t)va_arg(ap, unsigned int);
656
                break;
653
                break;
657
            case PrintfQualifierInt:
654
            case PrintfQualifierInt:
658
                size = sizeof(unsigned int);
655
                size = sizeof(unsigned int);
659
                number = (uint64_t)va_arg(ap, unsigned int);
656
                number = (uint64_t)va_arg(ap, unsigned int);
660
                break;
657
                break;
661
            case PrintfQualifierLong:
658
            case PrintfQualifierLong:
662
                size = sizeof(unsigned long);
659
                size = sizeof(unsigned long);
663
                number = (uint64_t)va_arg(ap, unsigned long);
660
                number = (uint64_t)va_arg(ap, unsigned long);
664
                break;
661
                break;
665
            case PrintfQualifierLongLong:
662
            case PrintfQualifierLongLong:
666
                size = sizeof(unsigned long long);
663
                size = sizeof(unsigned long long);
667
                number = (uint64_t)va_arg(ap,
664
                number = (uint64_t)va_arg(ap,
668
                    unsigned long long);
665
                    unsigned long long);
669
                break;
666
                break;
670
            case PrintfQualifierPointer:
667
            case PrintfQualifierPointer:
671
                size = sizeof(void *);
668
                size = sizeof(void *);
672
                number = (uint64_t)(unsigned long)va_arg(ap,
669
                number = (uint64_t)(unsigned long)va_arg(ap,
673
                    void *);
670
                    void *);
674
                break;
671
                break;
675
            case PrintfQualifierSizeT:
672
            case PrintfQualifierSizeT:
676
                size = sizeof(size_t);
673
                size = sizeof(size_t);
677
                number = (uint64_t)va_arg(ap, size_t);
674
                number = (uint64_t)va_arg(ap, size_t);
678
                break;
675
                break;
679
            default: /* Unknown qualifier */
676
            default: /* Unknown qualifier */
680
                goto minus_out;
677
                goto minus_out;
681
                   
678
                   
682
            }
679
            }
683
           
680
           
684
            if (flags & __PRINTF_FLAG_SIGNED) {
681
            if (flags & __PRINTF_FLAG_SIGNED) {
685
                if (number & (0x1 << (size * 8 - 1))) {
682
                if (number & (0x1 << (size * 8 - 1))) {
686
                    flags |= __PRINTF_FLAG_NEGATIVE;
683
                    flags |= __PRINTF_FLAG_NEGATIVE;
687
               
684
               
688
                    if (size == sizeof(uint64_t)) {
685
                    if (size == sizeof(uint64_t)) {
689
                        number = -((int64_t)number);
686
                        number = -((int64_t)number);
690
                    } else {
687
                    } else {
691
                        number = ~number;
688
                        number = ~number;
692
                        number &=
689
                        number &=
693
                            ~(0xFFFFFFFFFFFFFFFFll <<
690
                            ~(0xFFFFFFFFFFFFFFFFll <<
694
                            (size * 8));
691
                            (size * 8));
695
                        number++;
692
                        number++;
696
                    }
693
                    }
697
                }
694
                }
698
            }
695
            }
699
 
696
 
700
            if ((retval = print_number(number, width, precision,
697
            if ((retval = print_number(number, width, precision,
701
                base, flags, ps)) < 0 ) {
698
                base, flags, ps)) < 0 ) {
702
                goto minus_out;
699
                goto minus_out;
703
            }
700
            }
704
 
701
 
705
            counter += retval;
702
            counter += retval;
706
            j = i + 1;
703
            j = i + 1;
707
        }  
704
        }  
708
next_char:
705
next_char:
709
           
706
           
710
        ++i;
707
        ++i;
711
    }
708
    }
712
   
709
   
713
    if (i > j) {
710
    if (i > j) {
714
        retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps);
711
        retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps);
715
        if (retval < 0) { /* error */
712
        if (retval < 0) { /* error */
716
            goto minus_out;
713
            goto minus_out;
717
        }
714
        }
718
        counter += retval;
715
        counter += retval;
719
    }
716
    }
720
   
717
   
721
    return counter;
718
    return counter;
722
minus_out:
719
minus_out:
723
    return -counter;
720
    return -counter;
724
}
721
}
725
 
722
 
726
/** @}
723
/** @}
727
 */
724
 */
728
 
725