Subversion Repositories HelenOS-historic

Rev

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

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