Subversion Repositories HelenOS-historic

Rev

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

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