Subversion Repositories HelenOS-historic

Rev

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

Rev 1018 Rev 1763
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
#include "printf.h"
29
#include "printf.h"
30
#include "msim.h"
30
#include "stdarg.h"
-
 
31
#include <types.h>
31
 
32
 
32
static char digits[] = "0123456789abcdef";  /**< Hexadecimal characters */
33
typedef char *char_ptr;
33
 
34
 
-
 
35
static char digits[] = "0123456789abcdef";  /**< Hexadecimal characters */
34
 
36
 
35
void puts(const char *str)
37
void puts(const char *str)
36
{
38
{
37
    int len = 0;
39
    int len = 0;
38
   
40
   
39
    while (str[len] != 0)
41
    while (str[len] != 0)
40
        len++;
42
        len++;
41
   
43
   
42
    write(str, len);
44
    write(str, len);
43
}
45
}
44
 
46
 
45
 
47
 
46
/** Print hexadecimal digits
48
/** Print hexadecimal digits
47
 *
49
 *
48
 * Print fixed count of hexadecimal digits from
50
 * Print fixed count of hexadecimal digits from
49
 * the number num. The digits are printed in
51
 * the number num. The digits are printed in
50
 * natural left-to-right order starting with
52
 * natural left-to-right order starting with
51
 * the width-th digit.
53
 * the width-th digit.
52
 *
54
 *
53
 * @param num   Number containing digits.
55
 * @param num   Number containing digits.
54
 * @param width Count of digits to print.
56
 * @param width Count of digits to print.
55
 *
57
 *
56
 */
58
 */
57
static void print_fixed_hex(const __u64 num, const int width)
59
static void print_fixed_hex(const __u64 num, const int width)
58
{
60
{
59
    int i;
61
    int i;
60
   
62
   
61
    for (i = width * 8 - 4; i >= 0; i -= 4)
63
    for (i = width * 8 - 4; i >= 0; i -= 4)
62
        write(digits + ((num >> i) & 0xf), 1);
64
        write(digits + ((num >> i) & 0xf), 1);
63
}
65
}
64
 
66
 
65
 
67
 
66
/** Print number in given base
68
/** Print number in given base
67
 *
69
 *
68
 * Print significant digits of a number in given
70
 * Print significant digits of a number in given
69
 * base.
71
 * base.
70
 *
72
 *
71
 * @param num  Number to print.
73
 * @param num  Number to print.
72
 * @param base Base to print the number in (should
74
 * @param base Base to print the number in (should
73
 *             be in range 2 .. 16).
75
 *             be in range 2 .. 16).
74
 *
76
 *
75
 */
77
 */
76
static void print_number(const __native num, const unsigned int base)
78
static void print_number(const __native num, const unsigned int base)
77
{
79
{
78
    int val = num;
80
    int val = num;
79
    char d[sizeof(__native) * 8 + 1];       /* this is good enough even for base == 2 */
81
    char d[sizeof(__native) * 8 + 1];       /* this is good enough even for base == 2 */
80
    int i = sizeof(__native) * 8 - 1;
82
    int i = sizeof(__native) * 8 - 1;
81
   
83
   
82
    do {
84
    do {
83
        d[i--] = digits[val % base];
85
        d[i--] = digits[val % base];
84
    } while (val /= base);
86
    } while (val /= base);
85
   
87
   
86
    d[sizeof(__native) * 8] = 0;   
88
    d[sizeof(__native) * 8] = 0;   
87
    puts(&d[i + 1]);
89
    puts(&d[i + 1]);
88
}
90
}
89
 
91
 
90
 
92
 
91
/** General formatted text print
93
/** General formatted text print
92
 *
94
 *
93
 * Print text formatted according the fmt parameter
95
 * Print text formatted according the fmt parameter
94
 * and variant arguments. Each formatting directive
96
 * and variant arguments. Each formatting directive
95
 * begins with \% (percentage) character and one of the
97
 * begins with \% (percentage) character and one of the
96
 * following character:
98
 * following character:
97
 *
99
 *
98
 * \%    Prints the percentage character.
100
 * \%    Prints the percentage character.
99
 *
101
 *
100
 * s    The next variant argument is treated as char*
102
 * s    The next variant argument is treated as char*
101
 *      and printed as a NULL terminated string.
103
 *      and printed as a NULL terminated string.
102
 *
104
 *
103
 * c    The next variant argument is treated as a single char.
105
 * c    The next variant argument is treated as a single char.
104
 *
106
 *
105
 * p    The next variant argument is treated as a maximum
107
 * p    The next variant argument is treated as a maximum
106
 *      bit-width integer with respect to architecture
108
 *      bit-width integer with respect to architecture
107
 *      and printed in full hexadecimal width.
109
 *      and printed in full hexadecimal width.
108
 *
110
 *
109
 * P    As with 'p', but '0x' is prefixed.
111
 * P    As with 'p', but '0x' is prefixed.
110
 *
112
 *
111
 * q    The next variant argument is treated as a 64b integer
113
 * q    The next variant argument is treated as a 64b integer
112
 *      and printed in full hexadecimal width.
114
 *      and printed in full hexadecimal width.
113
 *
115
 *
114
 * Q    As with 'q', but '0x' is prefixed.
116
 * Q    As with 'q', but '0x' is prefixed.
115
 *
117
 *
116
 * l    The next variant argument is treated as a 32b integer
118
 * l    The next variant argument is treated as a 32b integer
117
 *      and printed in full hexadecimal width.
119
 *      and printed in full hexadecimal width.
118
 *
120
 *
119
 * L    As with 'l', but '0x' is prefixed.
121
 * L    As with 'l', but '0x' is prefixed.
120
 *
122
 *
121
 * w    The next variant argument is treated as a 16b integer
123
 * w    The next variant argument is treated as a 16b integer
122
 *      and printed in full hexadecimal width.
124
 *      and printed in full hexadecimal width.
123
 *
125
 *
124
 * W    As with 'w', but '0x' is prefixed.
126
 * W    As with 'w', but '0x' is prefixed.
125
 *
127
 *
126
 * b    The next variant argument is treated as a 8b integer
128
 * b    The next variant argument is treated as a 8b integer
127
 *      and printed in full hexadecimal width.
129
 *      and printed in full hexadecimal width.
128
 *
130
 *
129
 * B    As with 'b', but '0x' is prefixed.
131
 * B    As with 'b', but '0x' is prefixed.
130
 *
132
 *
131
 * d    The next variant argument is treated as integer
133
 * d    The next variant argument is treated as integer
132
 *      and printed in standard decimal format (only significant
134
 *      and printed in standard decimal format (only significant
133
 *      digits).
135
 *      digits).
134
 *
136
 *
135
 * x    The next variant argument is treated as integer
137
 * x    The next variant argument is treated as integer
136
 *      and printed in standard hexadecimal format (only significant
138
 *      and printed in standard hexadecimal format (only significant
137
 *      digits).
139
 *      digits).
138
 *
140
 *
139
 * X    As with 'x', but '0x' is prefixed.
141
 * X    As with 'x', but '0x' is prefixed.
140
 *
142
 *
141
 * All other characters from fmt except the formatting directives
143
 * All other characters from fmt except the formatting directives
142
 * are printed in verbatim.
144
 * are printed in verbatim.
143
 *
145
 *
144
 * @param fmt Formatting NULL terminated string.
146
 * @param fmt Formatting NULL terminated string.
145
 */
147
 */
146
void printf(const char *fmt, ...)
148
void printf(const char *fmt, ...)
147
{
149
{
148
    int i = 0;
150
    int i = 0;
149
    va_list ap;
151
    va_list ap;
150
    char c;
152
    char c;
151
   
153
   
152
    va_start(ap, fmt);
154
    va_start(ap, fmt);
153
   
155
   
154
    while ((c = fmt[i++])) {
156
    while ((c = fmt[i++])) {
155
        switch (c) {
157
        switch (c) {
156
           
158
           
157
            /* control character */
159
            /* control character */
158
            case '%':
160
            case '%':
159
               
161
               
160
                switch (c = fmt[i++]) {
162
                switch (c = fmt[i++]) {
161
                   
163
                   
162
                    /* percentile itself */
164
                    /* percentile itself */
163
                    case '%':
165
                    case '%':
164
                        break;
166
                        break;
165
                   
167
                   
166
                    /*
168
                    /*
167
                     * String and character conversions.
169
                     * String and character conversions.
168
                     */
170
                     */
169
                    case 's':
171
                    case 's':
170
                        puts(va_arg(ap, char_ptr));
172
                        puts(va_arg(ap, char_ptr));
171
                        goto loop;
173
                        goto loop;
172
                   
174
                   
173
                    case 'c':
175
                    case 'c':
174
                        c = (char) va_arg(ap, int);
176
                        c = (char) va_arg(ap, int);
175
                        break;
177
                        break;
176
                   
178
                   
177
                    /*
179
                    /*
178
                     * Hexadecimal conversions with fixed width.
180
                     * Hexadecimal conversions with fixed width.
179
                     */
181
                     */
180
                    case 'P':
182
                    case 'P':
181
                        puts("0x");
183
                        puts("0x");
182
                    case 'p':
184
                    case 'p':
183
                        print_fixed_hex(va_arg(ap, __native), sizeof(__native));
185
                        print_fixed_hex(va_arg(ap, __native), sizeof(__native));
184
                        goto loop;
186
                        goto loop;
185
                   
187
                   
186
                    case 'Q':
188
                    case 'Q':
187
                        puts("0x");
189
                        puts("0x");
188
                    case 'q':
190
                    case 'q':
189
                        print_fixed_hex(va_arg(ap, __u64), INT64);
191
                        print_fixed_hex(va_arg(ap, __u64), INT64);
190
                        goto loop;
192
                        goto loop;
191
                   
193
                   
192
                    case 'L':
194
                    case 'L':
193
                        puts("0x");
195
                        puts("0x");
194
                    case 'l':
196
                    case 'l':
195
                        print_fixed_hex(va_arg(ap, __native), INT32);
197
                        print_fixed_hex(va_arg(ap, __native), INT32);
196
                        goto loop;
198
                        goto loop;
197
                   
199
                   
198
                    case 'W':
200
                    case 'W':
199
                        puts("0x");
201
                        puts("0x");
200
                    case 'w':
202
                    case 'w':
201
                        print_fixed_hex(va_arg(ap, __native), INT16);
203
                        print_fixed_hex(va_arg(ap, __native), INT16);
202
                        goto loop;
204
                        goto loop;
203
                   
205
                   
204
                    case 'B':
206
                    case 'B':
205
                        puts("0x");
207
                        puts("0x");
206
                    case 'b':
208
                    case 'b':
207
                        print_fixed_hex(va_arg(ap, __native), INT8);
209
                        print_fixed_hex(va_arg(ap, __native), INT8);
208
                        goto loop;
210
                        goto loop;
209
                   
211
                   
210
                    /*
212
                    /*
211
                     * Decimal and hexadecimal conversions.
213
                     * Decimal and hexadecimal conversions.
212
                     */
214
                     */
213
                    case 'd':
215
                    case 'd':
214
                        print_number(va_arg(ap, __native), 10);
216
                        print_number(va_arg(ap, __native), 10);
215
                        goto loop;
217
                        goto loop;
216
                   
218
                   
217
                    case 'X':
219
                    case 'X':
218
                        puts("0x");
220
                        puts("0x");
219
                    case 'x':
221
                    case 'x':
220
                        print_number(va_arg(ap, __native), 16);
222
                        print_number(va_arg(ap, __native), 16);
221
                        goto loop;
223
                        goto loop;
222
                   
224
                   
223
                    /*
225
                    /*
224
                     * Bad formatting.
226
                     * Bad formatting.
225
                     */
227
                     */
226
                    default:
228
                    default:
227
                        goto out;
229
                        goto out;
228
            }
230
            }
229
           
231
           
230
            default:
232
            default:
231
                write(&c, 1);
233
                write(&c, 1);
232
        }
234
        }
233
   
235
   
234
loop:
236
loop:
235
        ;
237
        ;
236
    }
238
    }
237
   
239
   
238
out:
240
out:
239
   
241
   
240
    va_end(ap);
242
    va_end(ap);
241
}
243
}
242
 
244