Subversion Repositories HelenOS

Rev

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

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