Subversion Repositories HelenOS-historic

Rev

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

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