Subversion Repositories HelenOS-historic

Rev

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

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