Subversion Repositories HelenOS

Rev

Rev 1226 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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