Subversion Repositories HelenOS

Rev

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

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