Subversion Repositories HelenOS-historic

Rev

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

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