Subversion Repositories HelenOS

Rev

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

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