Subversion Repositories HelenOS-historic

Rev

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

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