Subversion Repositories HelenOS-historic

Rev

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

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