Subversion Repositories HelenOS-historic

Rev

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

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