Subversion Repositories HelenOS

Rev

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

Rev 2927 Rev 4345
1
/*
1
/*
2
 * Copyright (c) 2006 Martin Decky
2
 * Copyright (c) 2006 Martin Decky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup generic
29
/** @addtogroup generic
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include "printf.h"
35
#include "printf.h"
36
#include "stdarg.h"
36
#include "stdarg.h"
37
#include <types.h>
37
#include <types.h>
38
 
38
 
39
typedef char *char_ptr;
39
typedef char *char_ptr;
40
 
40
 
41
static char digits[] = "0123456789abcdef";  /**< Hexadecimal characters */
41
static char digits[] = "0123456789abcdef";  /**< Hexadecimal characters */
42
 
42
 
43
void puts(const char *str)
43
void puts(const char *str)
44
{
44
{
45
    int len = 0;
45
    int len = 0;
46
   
46
   
47
    while (str[len] != 0)
47
    while (str[len] != 0)
48
        len++;
48
        len++;
49
   
49
   
50
    write(str, len);
50
    write(str, len);
51
}
51
}
52
 
52
 
53
 
53
 
54
/** Print hexadecimal digits
54
/** Print hexadecimal digits
55
 *
55
 *
56
 * Print fixed count of hexadecimal digits from
56
 * Print fixed count of hexadecimal digits from
57
 * the number num. The digits are printed in
57
 * the number num. The digits are printed in
58
 * natural left-to-right order starting with
58
 * natural left-to-right order starting with
59
 * the width-th digit.
59
 * the width-th digit.
60
 *
60
 *
61
 * @param num   Number containing digits.
61
 * @param num   Number containing digits.
62
 * @param width Count of digits to print.
62
 * @param width Count of digits to print.
63
 *
63
 *
64
 */
64
 */
65
static void print_fixed_hex(const uint64_t num, const int width)
65
static void print_fixed_hex(const uint64_t num, const int width)
66
{
66
{
67
    int i;
67
    int i;
68
   
68
   
69
    for (i = width * 8 - 4; i >= 0; i -= 4)
69
    for (i = width * 8 - 4; i >= 0; i -= 4)
70
        write(digits + ((num >> i) & 0xf), 1);
70
        write(digits + ((num >> i) & 0xf), 1);
71
}
71
}
72
 
72
 
73
 
73
 
74
/** Print number in given base
74
/** Print number in given base
75
 *
75
 *
76
 * Print significant digits of a number in given
76
 * Print significant digits of a number in given
77
 * base.
77
 * base.
78
 *
78
 *
79
 * @param num  Number to print.
79
 * @param num  Number to print.
80
 * @param base Base to print the number in (should
80
 * @param base Base to print the number in (should
81
 *             be in range 2 .. 16).
81
 *             be in range 2 .. 16).
82
 *
82
 *
83
 */
83
 */
84
static void print_number(const unative_t num, const unsigned int base)
84
static void print_number(const unative_t num, const unsigned int base)
85
{
85
{
86
    int val = num;
86
    int val = num; 
-
 
87
 
-
 
88
    /* This is enough even for base 2. */
87
    char d[sizeof(unative_t) * 8 + 1];      /* this is good enough even for base == 2 */
89
    char d[sizeof(unative_t) * 8 + 1];
88
    int i = sizeof(unative_t) * 8 - 1;
90
    int i = sizeof(unative_t) * 8 - 1;
89
   
91
 
90
    do {
92
    do {
91
        d[i--] = digits[val % base];
93
        d[i--] = digits[val % base];
92
    } while (val /= base);
94
    } while (val /= base);
93
   
95
 
94
    d[sizeof(unative_t) * 8] = 0;  
96
    d[sizeof(unative_t) * 8] = 0;  
95
    puts(&d[i + 1]);
97
    puts(&d[i + 1]);
96
}
98
}
97
 
99
 
98
 
100
 
99
/** General formatted text print
101
/** General formatted text print
100
 *
102
 *
101
 * Print text formatted according the fmt parameter
103
 * Print text formatted according the fmt parameter
102
 * and variant arguments. Each formatting directive
104
 * and variant arguments. Each formatting directive
103
 * begins with \% (percentage) character and one of the
105
 * begins with \% (percentage) character and one of the
104
 * following character:
106
 * following character:
105
 *
107
 *
106
 * \%    Prints the percentage character.
108
 * \%    Prints the percentage character.
107
 *
109
 *
108
 * s    The next variant argument is treated as char*
110
 * s    The next variant argument is treated as char*
109
 *      and printed as a NULL terminated string.
111
 *      and printed as a NULL terminated string.
110
 *
112
 *
111
 * c    The next variant argument is treated as a single char.
113
 * c    The next variant argument is treated as a single char.
112
 *
114
 *
113
 * p    The next variant argument is treated as a maximum
115
 * p    The next variant argument is treated as a maximum
114
 *      bit-width integer with respect to architecture
116
 *      bit-width integer with respect to architecture
115
 *      and printed in full hexadecimal width.
117
 *      and printed in full hexadecimal width.
116
 *
118
 *
117
 * P    As with 'p', but '0x' is prefixed.
119
 * P    As with 'p', but '0x' is prefixed.
118
 *
120
 *
119
 * q    The next variant argument is treated as a 64b integer
121
 * q    The next variant argument is treated as a 64b integer
120
 *      and printed in full hexadecimal width.
122
 *      and printed in full hexadecimal width.
121
 *
123
 *
122
 * Q    As with 'q', but '0x' is prefixed.
124
 * Q    As with 'q', but '0x' is prefixed.
123
 *
125
 *
124
 * l    The next variant argument is treated as a 32b integer
126
 * l    The next variant argument is treated as a 32b integer
125
 *      and printed in full hexadecimal width.
127
 *      and printed in full hexadecimal width.
126
 *
128
 *
127
 * L    As with 'l', but '0x' is prefixed.
129
 * L    As with 'l', but '0x' is prefixed.
128
 *
130
 *
129
 * w    The next variant argument is treated as a 16b integer
131
 * w    The next variant argument is treated as a 16b integer
130
 *      and printed in full hexadecimal width.
132
 *      and printed in full hexadecimal width.
131
 *
133
 *
132
 * W    As with 'w', but '0x' is prefixed.
134
 * W    As with 'w', but '0x' is prefixed.
133
 *
135
 *
134
 * b    The next variant argument is treated as a 8b integer
136
 * b    The next variant argument is treated as a 8b integer
135
 *      and printed in full hexadecimal width.
137
 *      and printed in full hexadecimal width.
136
 *
138
 *
137
 * B    As with 'b', but '0x' is prefixed.
139
 * B    As with 'b', but '0x' is prefixed.
138
 *
140
 *
139
 * d    The next variant argument is treated as integer
141
 * d    The next variant argument is treated as integer
140
 *      and printed in standard decimal format (only significant
142
 *      and printed in standard decimal format (only significant
141
 *      digits).
143
 *      digits).
142
 *
144
 *
143
 * x    The next variant argument is treated as integer
145
 * x    The next variant argument is treated as integer
144
 *      and printed in standard hexadecimal format (only significant
146
 *      and printed in standard hexadecimal format (only significant
145
 *      digits).
147
 *      digits).
146
 *
148
 *
147
 * X    As with 'x', but '0x' is prefixed.
149
 * X    As with 'x', but '0x' is prefixed.
148
 *
150
 *
149
 * All other characters from fmt except the formatting directives
151
 * All other characters from fmt except the formatting directives
150
 * are printed in verbatim.
152
 * are printed in verbatim.
151
 *
153
 *
152
 * @param fmt Formatting NULL terminated string.
154
 * @param fmt Formatting NULL terminated string.
153
 */
155
 */
154
void printf(const char *fmt, ...)
156
void printf(const char *fmt, ...)
155
{
157
{
156
    int i = 0;
158
    int i = 0;
157
    va_list ap;
159
    va_list ap;
158
    char c;
160
    char c;
159
   
161
 
160
    va_start(ap, fmt);
162
    va_start(ap, fmt);
161
   
163
 
162
    while ((c = fmt[i++])) {
164
    while ((c = fmt[i++])) {
163
        switch (c) {
165
        switch (c) {
164
           
166
 
165
            /* control character */
167
        /* control character */
-
 
168
        case '%':
-
 
169
 
-
 
170
            switch (c = fmt[i++]) {
-
 
171
 
-
 
172
            /* percentile itself */
166
            case '%':
173
            case '%':
-
 
174
                break;
-
 
175
 
-
 
176
            /*
-
 
177
             * String and character conversions.
-
 
178
             */
-
 
179
            case 's':
-
 
180
                puts(va_arg(ap, char_ptr));
-
 
181
                goto loop;
-
 
182
 
-
 
183
            case 'c':
-
 
184
                c = (char) va_arg(ap, int);
-
 
185
                break;
-
 
186
 
-
 
187
            /*
-
 
188
             * Hexadecimal conversions with fixed width.
-
 
189
             */
-
 
190
            case 'P':
-
 
191
                puts("0x");
-
 
192
            case 'p':
-
 
193
                print_fixed_hex(va_arg(ap, unative_t),
-
 
194
                    sizeof(unative_t));
-
 
195
                goto loop;
-
 
196
 
-
 
197
            case 'Q':
-
 
198
                puts("0x");
-
 
199
            case 'q':
-
 
200
                print_fixed_hex(va_arg(ap, uint64_t), INT64);
-
 
201
                goto loop;
-
 
202
 
-
 
203
            case 'L':
-
 
204
                puts("0x");
-
 
205
            case 'l':
-
 
206
                print_fixed_hex(va_arg(ap, unative_t), INT32);
-
 
207
                goto loop;
-
 
208
 
-
 
209
            case 'W':
-
 
210
                puts("0x");
-
 
211
            case 'w':
-
 
212
                print_fixed_hex(va_arg(ap, unative_t), INT16);
-
 
213
                goto loop;
-
 
214
 
-
 
215
            case 'B':
-
 
216
                puts("0x");
-
 
217
            case 'b':
-
 
218
                print_fixed_hex(va_arg(ap, unative_t), INT8);
-
 
219
                goto loop;
-
 
220
 
-
 
221
            /*
-
 
222
             * Decimal and hexadecimal conversions.
-
 
223
             */
-
 
224
            case 'd':
-
 
225
                print_number(va_arg(ap, unative_t), 10);
-
 
226
                goto loop;
167
               
227
               
168
                switch (c = fmt[i++]) {
-
 
169
                   
-
 
170
                    /* percentile itself */
-
 
171
                    case '%':
-
 
172
                        break;
-
 
173
                   
-
 
174
                    /*
-
 
175
                     * String and character conversions.
-
 
176
                     */
-
 
177
                    case 's':
-
 
178
                        puts(va_arg(ap, char_ptr));
-
 
179
                        goto loop;
-
 
180
                   
-
 
181
                    case 'c':
-
 
182
                        c = (char) va_arg(ap, int);
-
 
183
                        break;
-
 
184
                   
-
 
185
                    /*
-
 
186
                     * Hexadecimal conversions with fixed width.
-
 
187
                     */
-
 
188
                    case 'P':
-
 
189
                        puts("0x");
-
 
190
                    case 'p':
-
 
191
                        print_fixed_hex(va_arg(ap, unative_t), sizeof(unative_t));
-
 
192
                        goto loop;
-
 
193
                   
-
 
194
                    case 'Q':
-
 
195
                        puts("0x");
-
 
196
                    case 'q':
-
 
197
                        print_fixed_hex(va_arg(ap, uint64_t), INT64);
-
 
198
                        goto loop;
-
 
199
                   
-
 
200
                    case 'L':
-
 
201
                        puts("0x");
-
 
202
                    case 'l':
-
 
203
                        print_fixed_hex(va_arg(ap, unative_t), INT32);
-
 
204
                        goto loop;
-
 
205
                   
-
 
206
                    case 'W':
-
 
207
                        puts("0x");
-
 
208
                    case 'w':
-
 
209
                        print_fixed_hex(va_arg(ap, unative_t), INT16);
-
 
210
                        goto loop;
-
 
211
                   
-
 
212
                    case 'B':
-
 
213
                        puts("0x");
-
 
214
                    case 'b':
-
 
215
                        print_fixed_hex(va_arg(ap, unative_t), INT8);
-
 
216
                        goto loop;
-
 
217
                   
-
 
218
                    /*
-
 
219
                     * Decimal and hexadecimal conversions.
-
 
220
                     */
-
 
221
                    case 'd':
-
 
222
                        print_number(va_arg(ap, unative_t), 10);
-
 
223
                        goto loop;
-
 
224
                   
-
 
225
                    case 'X':
228
            case 'X':
226
                        puts("0x");
229
                puts("0x");
227
                    case 'x':
230
            case 'x':
228
                        print_number(va_arg(ap, unative_t), 16);
231
                print_number(va_arg(ap, unative_t), 16);
229
                        goto loop;
232
                goto loop;
230
                   
233
               
231
                    /*
234
            /*
232
                     * Bad formatting.
235
             * Bad formatting.
233
                     */
236
             */
234
                    default:
-
 
235
                        goto out;
-
 
236
            }
-
 
237
           
-
 
238
            default:
237
            default:
-
 
238
                goto out;
-
 
239
            }
-
 
240
 
-
 
241
        default:
239
                write(&c, 1);
242
            write(&c, 1);
240
        }
243
        }
241
   
-
 
242
loop:
244
loop:
243
        ;
245
        ;
244
    }
246
    }
245
   
247
   
246
out:
248
out:
247
   
249
   
248
    va_end(ap);
250
    va_end(ap);
249
}
251
}
250
 
252
 
251
/** @}
253
/** @}
252
 */
254
 */
253
 
255