Subversion Repositories HelenOS-historic

Rev

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

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