Subversion Repositories HelenOS-historic

Rev

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

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