Subversion Repositories HelenOS-historic

Rev

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

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