Subversion Repositories HelenOS-historic

Rev

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

Rev 1073 Rev 1173
Line 29... Line 29...
29
 
29
 
30
#include <stdio.h>
30
#include <stdio.h>
31
#include <unistd.h>
31
#include <unistd.h>
32
#include <io/io.h>
32
#include <io/io.h>
33
#include <stdarg.h>
33
#include <stdarg.h>
-
 
34
#include <ctype.h>
-
 
35
#include <string.h>
34
 
36
 
35
#define __PRINTF_FLAG_PREFIX        0x00000001  /* show prefixes 0x or 0*/
37
#define __PRINTF_FLAG_PREFIX        0x00000001  /* show prefixes 0x or 0*/
36
#define __PRINTF_FLAG_SIGNED        0x00000002  /* signed / unsigned number */
38
#define __PRINTF_FLAG_SIGNED        0x00000002  /* signed / unsigned number */
37
#define __PRINTF_FLAG_ZEROPADDED    0x00000004  /* print leading zeroes */
39
#define __PRINTF_FLAG_ZEROPADDED    0x00000004  /* print leading zeroes */
38
#define __PRINTF_FLAG_LEFTALIGNED   0x00000010  /* align to left */
40
#define __PRINTF_FLAG_LEFTALIGNED   0x00000010  /* align to left */
Line 57... Line 59...
57
} qualifier_t;
59
} qualifier_t;
58
 
60
 
59
static char digits_small[] = "0123456789abcdef";    /* Small hexadecimal characters */
61
static char digits_small[] = "0123456789abcdef";    /* Small hexadecimal characters */
60
static char digits_big[] = "0123456789ABCDEF";  /* Big hexadecimal characters */
62
static char digits_big[] = "0123456789ABCDEF";  /* Big hexadecimal characters */
61
 
63
 
-
 
64
 
-
 
65
/** Print one formatted character
-
 
66
 * @param c character to print
-
 
67
 * @param width
-
 
68
 * @param flags
-
 
69
 * @return number of printed characters or EOF
-
 
70
 */
-
 
71
                       
-
 
72
static int print_char(char c, int width, uint64_t flags)
-
 
73
{
-
 
74
    int counter = 0;
-
 
75
    char space = ' ';
-
 
76
   
-
 
77
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
-
 
78
        while (--width > 0) {   /* one space is consumed by character itself hence predecrement */
-
 
79
            /* FIXME: painful slow */
-
 
80
            putchar(' ');  
-
 
81
            ++counter;
-
 
82
        }
-
 
83
    }
-
 
84
   
-
 
85
    if (putchar(c) == EOF) {
-
 
86
        return EOF;
-
 
87
    }
-
 
88
 
-
 
89
    while (--width > 0) { /* one space is consumed by character itself hence predecrement */
-
 
90
        putchar(' ');
-
 
91
        ++counter;
-
 
92
    }
-
 
93
   
-
 
94
    return ++counter;
-
 
95
}
-
 
96
 
-
 
97
/** Print one string
-
 
98
 * @param s string
-
 
99
 * @param width
-
 
100
 * @param precision
-
 
101
 * @param flags
-
 
102
 * @return number of printed characters or EOF
-
 
103
 */
-
 
104
                       
-
 
105
static int print_string(char *s, int width, int precision, uint64_t flags)
-
 
106
{
-
 
107
    int counter = 0;
-
 
108
    size_t size;
-
 
109
 
-
 
110
    if (s == NULL) {
-
 
111
        return putstr("(NULL)");
-
 
112
    }
-
 
113
   
-
 
114
    size = strlen(s);
-
 
115
 
-
 
116
    /* print leading spaces */
-
 
117
 
-
 
118
    if (precision == 0)
-
 
119
        precision = size;
-
 
120
 
-
 
121
    width -= precision;
-
 
122
   
-
 
123
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
-
 
124
        while (width-- > 0) {  
-
 
125
            putchar(' ');  
-
 
126
            counter++;
-
 
127
        }
-
 
128
    }
-
 
129
 
-
 
130
    while (precision > size) {
-
 
131
        precision--;
-
 
132
        putchar(' ');  
-
 
133
        ++counter;
-
 
134
    }
-
 
135
   
-
 
136
    if (putnchars(s, precision) == EOF) {
-
 
137
        return EOF;
-
 
138
    }
-
 
139
 
-
 
140
    counter += precision;
-
 
141
 
-
 
142
    while (width-- > 0) {
-
 
143
        putchar(' ');  
-
 
144
        ++counter;
-
 
145
    }
-
 
146
   
-
 
147
    return ++counter;
-
 
148
}
-
 
149
 
-
 
150
 
62
/** Print number in given base
151
/** Print number in given base
63
 *
152
 *
64
 * Print significant digits of a number in given
153
 * Print significant digits of a number in given
65
 * base.
154
 * base.
66
 *
155
 *
67
 * @param num  Number to print.
156
 * @param num  Number to print.
-
 
157
 * @param width
68
 * @param size not used, in future releases will be replaced with precision and width params
158
 * @param precision
69
 * @param base Base to print the number in (should
159
 * @param base Base to print the number in (should
70
 *             be in range 2 .. 16).
160
 *             be in range 2 .. 16).
71
 * @param flags output modifiers
161
 * @param flags output modifiers
72
 * @return number of written characters or EOF
162
 * @return number of written characters or EOF
73
 *
163
 *
74
 */
164
 */
75
static int print_number(uint64_t num, size_t size, int base , uint64_t flags)
165
static int print_number(uint64_t num, int width, int precision, int base , uint64_t flags)
76
{
166
{
77
    /* FIXME: This is only first version.
-
 
78
     * Printf does not have support for specification of size
-
 
79
     * and precision, so this function writes with parameters defined by
-
 
80
     * their type size.
-
 
81
     */
-
 
82
    char *digits = digits_small;
167
    char *digits = digits_small;
83
    char d[PRINT_NUMBER_BUFFER_SIZE];   /* this is good enough even for base == 2, prefix and sign */
168
    char d[PRINT_NUMBER_BUFFER_SIZE];   /* this is good enough even for base == 2, prefix and sign */
84
    char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1];
169
    char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1];
-
 
170
    int size = 0;
-
 
171
    int written = 0;
-
 
172
    char sgn;
85
   
173
   
86
    if (flags & __PRINTF_FLAG_BIGCHARS)
174
    if (flags & __PRINTF_FLAG_BIGCHARS)
87
        digits = digits_big;   
175
        digits = digits_big;   
88
   
176
   
89
    *ptr-- = 0; /* Put zero at end of string */
177
    *ptr-- = 0; /* Put zero at end of string */
90
 
178
 
91
    if (num == 0) {
179
    if (num == 0) {
92
        *ptr-- = '0';
180
        *ptr-- = '0';
-
 
181
        size++;
93
    } else {
182
    } else {
94
        do {
183
        do {
95
            *ptr-- = digits[num % base];
184
            *ptr-- = digits[num % base];
-
 
185
            size++;
96
        } while (num /= base);
186
        } while (num /= base);
97
    }
187
    }
-
 
188
 
-
 
189
    /* Collect sum of all prefixes/signs/... to calculate padding and leading zeroes */
98
    if (flags & __PRINTF_FLAG_PREFIX) { /*FIXME*/
190
    if (flags & __PRINTF_FLAG_PREFIX) {
99
        switch(base) {
191
        switch(base) {
100
            case 2: /* Binary formating is not standard, but usefull */
192
            case 2: /* Binary formating is not standard, but usefull */
101
                *ptr = 'b';
193
                size += 2;
102
                if (flags & __PRINTF_FLAG_BIGCHARS) *ptr = 'B';
-
 
103
                ptr--;
-
 
104
                *ptr-- = '0';
-
 
105
                break;
194
                break;
106
            case 8:
195
            case 8:
107
                *ptr-- = 'o';
196
                size++;
108
                break;
197
                break;
109
            case 16:
198
            case 16:
110
                *ptr = 'x';
199
                size += 2;
111
                if (flags & __PRINTF_FLAG_BIGCHARS) *ptr = 'X';
-
 
112
                ptr--;
-
 
113
                *ptr-- = '0';
-
 
114
                break;
200
                break;
115
        }
201
        }
116
    }
202
    }
117
   
203
 
-
 
204
    sgn = 0;
118
    if (flags & __PRINTF_FLAG_SIGNED) {
205
    if (flags & __PRINTF_FLAG_SIGNED) {
119
        if (flags & __PRINTF_FLAG_NEGATIVE) {
206
        if (flags & __PRINTF_FLAG_NEGATIVE) {
120
            *ptr-- = '-';
207
            sgn = '-';
-
 
208
            size++;
121
        } else if (flags & __PRINTF_FLAG_SHOWPLUS) {
209
        } else if (flags & __PRINTF_FLAG_SHOWPLUS) {
122
                *ptr-- = '+';
210
                sgn = '+';
-
 
211
                size++;
123
            } else if (flags & __PRINTF_FLAG_SPACESIGN) {
212
            } else if (flags & __PRINTF_FLAG_SPACESIGN) {
124
                    *ptr-- = ' ';
213
                    sgn = ' ';
-
 
214
                    size++;
125
                }
215
                }
126
    }
216
    }
127
 
217
 
128
    /* Print leading zeroes */
-
 
129
 
-
 
130
    if (flags & __PRINTF_FLAG_LEFTALIGNED) {
218
    if (flags & __PRINTF_FLAG_LEFTALIGNED) {
131
        flags &= ~__PRINTF_FLAG_ZEROPADDED;
219
        flags &= ~__PRINTF_FLAG_ZEROPADDED;
132
    }
220
    }
-
 
221
 
-
 
222
    /* if number is leftaligned or precision is specified then zeropadding is ignored */
133
    if (flags & __PRINTF_FLAG_ZEROPADDED) {
223
    if (flags & __PRINTF_FLAG_ZEROPADDED) {
134
            while (ptr != d ) {
224
        if ((precision == 0) && (width > size)) {
135
            *ptr-- = '0';
225
            precision = width - size;
136
        }
226
        }
137
    }
227
    }
-
 
228
 
-
 
229
    /* print leading spaces */
-
 
230
    if (size > precision) /* We must print whole number not only a part */
-
 
231
        precision = size;
-
 
232
 
-
 
233
    width -= precision;
-
 
234
   
-
 
235
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
-
 
236
        while (width-- > 0) {  
-
 
237
            putchar(' ');  
-
 
238
            written++;
-
 
239
        }
-
 
240
    }
-
 
241
   
-
 
242
    /* print sign */
-
 
243
    if (sgn) {
-
 
244
        putchar(sgn);
-
 
245
        written++;
-
 
246
    }
-
 
247
   
-
 
248
    /* print prefix */
-
 
249
   
-
 
250
    if (flags & __PRINTF_FLAG_PREFIX) {
-
 
251
        switch(base) {
-
 
252
            case 2: /* Binary formating is not standard, but usefull */
-
 
253
                putchar('0');
-
 
254
                if (flags & __PRINTF_FLAG_BIGCHARS) {
-
 
255
                    putchar('B');
-
 
256
                } else {
-
 
257
                    putchar('b');
-
 
258
                }
-
 
259
                written == 2;
-
 
260
                break;
-
 
261
            case 8:
-
 
262
                putchar('o');
-
 
263
                written++;
-
 
264
                break;
-
 
265
            case 16:
-
 
266
                putchar('0');
-
 
267
                if (flags & __PRINTF_FLAG_BIGCHARS) {
-
 
268
                    putchar('X');
-
 
269
                } else {
-
 
270
                    putchar('x');
-
 
271
                }
-
 
272
                written += 2;
-
 
273
                break;
-
 
274
        }
-
 
275
    }
-
 
276
 
-
 
277
    /* print leading zeroes */
-
 
278
    precision -= size;
-
 
279
    while (precision-- > 0) {  
-
 
280
        putchar('0');  
-
 
281
        written++;
-
 
282
    }
-
 
283
 
-
 
284
   
-
 
285
    /* print number itself */
-
 
286
 
-
 
287
    written += putstr(++ptr);
138
   
288
   
-
 
289
    /* print ending spaces */
-
 
290
   
-
 
291
    while (width-- > 0) {  
-
 
292
        putchar(' ');  
-
 
293
        written++;
-
 
294
    }
-
 
295
 
139
    return putstr(++ptr);
296
    return written;
140
}
297
}
141
 
298
 
142
 
299
 
143
 
300
 
144
/** General formatted text print
301
/** General formatted text print
Line 206... Line 363...
206
    char c;
363
    char c;
207
    qualifier_t qualifier;  /* type of argument */
364
    qualifier_t qualifier;  /* type of argument */
208
    int base;   /* base in which will be parameter (numbers only) printed */
365
    int base;   /* base in which will be parameter (numbers only) printed */
209
    uint64_t number; /* argument value */
366
    uint64_t number; /* argument value */
210
    size_t  size; /* byte size of integer parameter */
367
    size_t  size; /* byte size of integer parameter */
-
 
368
    int width, precision;
211
    uint64_t flags;
369
    uint64_t flags;
212
   
370
   
213
    counter = 0;
371
    counter = 0;
214
    va_start(ap, fmt);
372
    va_start(ap, fmt);
215
 
373
   
216
    while ((c = fmt[i])) {
374
    while ((c = fmt[i])) {
217
        /* control character */
375
        /* control character */
218
        if (c == '%' ) {
376
        if (c == '%' ) {
219
            /* print common characters if any processed */ 
377
            /* print common characters if any processed */ 
220
            if (i > j) {
378
            if (i > j) {
Line 239... Line 397...
239
                    case '0': flags |= __PRINTF_FLAG_ZEROPADDED; break;
397
                    case '0': flags |= __PRINTF_FLAG_ZEROPADDED; break;
240
                    default: end = 1;
398
                    default: end = 1;
241
                }; 
399
                }; 
242
               
400
               
243
            } while (end == 0);
401
            } while (end == 0);
-
 
402
           
244
            /* TODO: width & '*' operator */
403
            /* width & '*' operator */
-
 
404
            width = 0;
-
 
405
            if (isdigit(fmt[i])) {
-
 
406
                while (isdigit(fmt[i])) {
-
 
407
                    width *= 10;
-
 
408
                    width += fmt[i++] - '0';
-
 
409
                }
-
 
410
            } else if (fmt[i] == '*') {
-
 
411
                /* get width value from argument list*/
-
 
412
                i++;
-
 
413
                width = (int)va_arg(ap, int);
-
 
414
                if (width < 0) {
-
 
415
                    /* negative width means to set '-' flag */
-
 
416
                    width *= -1;
-
 
417
                    flags |= __PRINTF_FLAG_LEFTALIGNED;
-
 
418
                }
-
 
419
            }
-
 
420
           
245
            /* TODO: precision and '*' operator */ 
421
            /* precision and '*' operator */   
-
 
422
            precision = 0;
-
 
423
            if (fmt[i] == '.') {
-
 
424
                ++i;
-
 
425
                if (isdigit(fmt[i])) {
-
 
426
                    while (isdigit(fmt[i])) {
-
 
427
                        precision *= 10;
-
 
428
                        precision += fmt[i++] - '0';
-
 
429
                    }
-
 
430
                } else if (fmt[i] == '*') {
-
 
431
                    /* get precision value from argument list*/
-
 
432
                    i++;
-
 
433
                    precision = (int)va_arg(ap, int);
-
 
434
                    if (precision < 0) {
-
 
435
                        /* negative precision means to ignore it */
-
 
436
                        precision = 0;
-
 
437
                    }
-
 
438
                }
-
 
439
            }
246
 
440
 
247
            switch (fmt[i++]) {
441
            switch (fmt[i++]) {
-
 
442
                /** TODO: unimplemented qualifiers:
-
 
443
                 * t ptrdiff_t - ISO C 99
-
 
444
                 */
248
                case 'h':   /* char or short */
445
                case 'h':   /* char or short */
249
                    qualifier = PrintfQualifierShort;
446
                    qualifier = PrintfQualifierShort;
250
                    if (fmt[i] == 'h') {
447
                    if (fmt[i] == 'h') {
251
                        i++;
448
                        i++;
252
                        qualifier = PrintfQualifierByte;
449
                        qualifier = PrintfQualifierByte;
Line 273... Line 470...
273
 
470
 
274
                /*
471
                /*
275
                * String and character conversions.
472
                * String and character conversions.
276
                */
473
                */
277
                case 's':
474
                case 's':
278
                    if ((retval = putstr(va_arg(ap, char*))) == EOF) {
475
                    if ((retval = print_string(va_arg(ap, char*), width, precision, flags)) == EOF) {
279
                        return -counter;
476
                        return -counter;
280
                    };
477
                    };
281
                   
478
                   
282
                    counter += retval;
479
                    counter += retval;
283
                    j = i + 1;
480
                    j = i + 1;
284
                    goto next_char;
481
                    goto next_char;
285
                case 'c':
482
                case 'c':
286
                    c = va_arg(ap, unsigned long);
483
                    c = va_arg(ap, unsigned int);
287
                    if ((retval = putnchars(&c, sizeof(char))) == EOF) {
484
                    if ((retval = print_char(c, width, flags )) == EOF) {
288
                        return -counter;
485
                        return -counter;
289
                    };
486
                    };
290
                   
487
                   
291
                    counter += retval;
488
                    counter += retval;
292
                    j = i + 1;
489
                    j = i + 1;
Line 382... Line 579...
382
                        number++;
579
                        number++;
383
                    }
580
                    }
384
                }
581
                }
385
            }
582
            }
386
 
583
 
387
            if ((retval = print_number(number, size, base, flags)) == EOF ) {
584
            if ((retval = print_number(number, width, precision, base, flags)) == EOF ) {
388
                return -counter;
585
                return -counter;
389
            };
586
            };
390
 
587
 
391
            counter += retval;
588
            counter += retval;
392
            j = i + 1;
589
            j = i + 1;