Subversion Repositories HelenOS

Rev

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

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