Subversion Repositories HelenOS

Rev

Rev 2571 | Rev 2712 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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