Subversion Repositories HelenOS-historic

Rev

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

Rev 40 Rev 42
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";
35
static char digits[] = "0123456789abcdef";
36
static spinlock_t printflock;
36
static spinlock_t printflock;
37
 
37
 
38
void print_str(char *str)
38
void print_str(char *str)
39
{
39
{
40
        int i = 0;
40
        int i = 0;
41
    char c;
41
    char c;
42
   
42
   
43
    while (c = str[i++])
43
    while (c = str[i++])
44
        putchar(c);
44
        putchar(c);
45
}
45
}
46
 
46
 
47
 
47
 
48
/*
48
/*
49
 * This is a universal function for printing hexadecimal numbers of fixed
49
 * This is a universal function for printing hexadecimal numbers of fixed
50
 * width.
50
 * width.
51
 */
51
 */
52
void print_fixed_hex(__u32 num, int width)
52
void print_fixed_hex(__native num, int width)
53
{
53
{
54
    int i;
54
    int i;
55
   
55
   
56
    for (i = width*8 - 4; i >= 0; i -= 4)
56
    for (i = width*8 - 4; i >= 0; i -= 4)
57
        putchar(digits[(num>>i) & 0xf]);
57
        putchar(digits[(num>>i) & 0xf]);
58
}
58
}
59
 
59
 
60
/*
60
/*
61
 * This is a universal function for printing decimal and hexadecimal numbers.
61
 * This is a universal function for printing decimal and hexadecimal numbers.
62
 * It prints only significant digits.
62
 * It prints only significant digits.
63
 */
63
 */
64
void print_number(__u32 num, int base)
64
void print_number(__native num, int base)
65
{
65
{
66
    char d[32+1];       /* this is good enough even for base == 2 */
66
    char d[sizeof(__native)*8+1];       /* this is good enough even for base == 2 */
67
        int i = 31;
67
        int i = sizeof(__native)*8-1;
68
   
68
   
69
    do {
69
    do {
70
        d[i--] = digits[num % base];
70
        d[i--] = digits[num % base];
71
    } while (num /= base);
71
    } while (num /= base);
72
   
72
   
73
    d[32] = 0; 
73
    d[sizeof(__native)*8] = 0; 
74
    print_str(&d[i + 1]);
74
    print_str(&d[i + 1]);
75
}
75
}
76
 
76
 
77
/*
77
/*
78
 * This is our function for printing formatted text.
78
 * This is our function for printing formatted text.
79
 * It's much simpler than the user-space one.
79
 * It's much simpler than the user-space one.
80
 * We are greateful for this function.
80
 * We are greateful for this function.
81
 */
81
 */
82
void printf(char *fmt, ...)
82
void printf(char *fmt, ...)
83
{
83
{
84
    int irqpri, i = 0;
84
    int irqpri, i = 0;
85
    va_list ap;
85
    va_list ap;
86
    char c;
86
    char c;
87
 
87
 
88
    va_start(ap, fmt);
88
    va_start(ap, fmt);
89
 
89
 
90
    irqpri = cpu_priority_high();
90
    irqpri = cpu_priority_high();
91
    spinlock_lock(&printflock);
91
    spinlock_lock(&printflock);
92
 
92
 
93
    while (c = fmt[i++]) {
93
    while (c = fmt[i++]) {
94
        switch (c) {
94
        switch (c) {
95
 
95
 
96
            /* control character */
96
            /* control character */
97
            case '%':
97
            case '%':
98
                switch (c = fmt[i++]) {
98
                switch (c = fmt[i++]) {
99
 
99
 
100
                /* percentile itself */
100
                /* percentile itself */
101
                case '%':
101
                case '%':
102
                    break;
102
                    break;
103
 
103
 
104
                /*
104
                /*
105
                 * String and character conversions.
105
                 * String and character conversions.
106
                 */
106
                 */
107
                case 's':
107
                case 's':
108
                    print_str(va_arg(ap, char_ptr));
108
                    print_str(va_arg(ap, char_ptr));
109
                    goto loop;
109
                    goto loop;
110
 
110
 
111
                case 'c':
111
                case 'c':
112
                    c = va_arg(ap, char);
112
                    c = (char) va_arg(ap, int);
113
                    break;
113
                    break;
114
 
114
 
115
                /*
115
                /*
116
                         * Hexadecimal conversions with fixed width.
116
                         * Hexadecimal conversions with fixed width.
117
                         */
117
                         */
118
                case 'L':
118
                case 'L':
119
                    print_str("0x");
119
                    print_str("0x");
120
                case 'l':
120
                case 'l':
121
                        print_fixed_hex(va_arg(ap, __native), INT32);
121
                        print_fixed_hex(va_arg(ap, __native), INT32);
122
                    goto loop;
122
                    goto loop;
123
 
123
 
124
                case 'W':
124
                case 'W':
125
                    print_str("0x");
125
                    print_str("0x");
126
                case 'w':
126
                case 'w':
127
                        print_fixed_hex(va_arg(ap, __native), INT16);
127
                        print_fixed_hex(va_arg(ap, __native), INT16);
128
                    goto loop;
128
                    goto loop;
129
 
129
 
130
                case 'B':
130
                case 'B':
131
                    print_str("0x");
131
                    print_str("0x");
132
                case 'b':
132
                case 'b':
133
                        print_fixed_hex(va_arg(ap, __native), INT8);
133
                        print_fixed_hex(va_arg(ap, __native), INT8);
134
                    goto loop;
134
                    goto loop;
135
 
135
 
136
                /*
136
                /*
137
                         * Decimal and hexadecimal conversions.
137
                         * Decimal and hexadecimal conversions.
138
                         */
138
                         */
139
                case 'd':
139
                case 'd':
140
                        print_number(va_arg(ap, __native), 10);
140
                        print_number(va_arg(ap, __native), 10);
141
                    goto loop;
141
                    goto loop;
142
 
142
 
143
                case 'X':
143
                case 'X':
144
                            print_str("0x");
144
                            print_str("0x");
145
                case 'x':
145
                case 'x':
146
                        print_number(va_arg(ap, __native), 16);
146
                        print_number(va_arg(ap, __native), 16);
147
                    goto loop;
147
                    goto loop;
148
       
148
       
149
                /*
149
                /*
150
                 * Bad formatting.
150
                 * Bad formatting.
151
                 */
151
                 */
152
                default:
152
                default:
153
                    goto out;
153
                    goto out;
154
                }
154
                }
155
 
155
 
156
            default: putchar(c);
156
            default: putchar(c);
157
        }
157
        }
158
   
158
   
159
loop:
159
loop:
160
        ;
160
        ;
161
    }
161
    }
162
 
162
 
163
out:
163
out:
164
    spinlock_unlock(&printflock);
164
    spinlock_unlock(&printflock);
165
    cpu_priority_restore(irqpri);
165
    cpu_priority_restore(irqpri);
166
   
166
   
167
    va_end(ap);
167
    va_end(ap);
168
}
168
}
169
 
169