Subversion Repositories HelenOS

Rev

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

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