Subversion Repositories HelenOS-historic

Rev

Rev 115 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
#include <putchar.h>
30
#include <print.h>
31
#include <synch/spinlock.h>
40 jermar 32
#include <arch/arg.h>
115 jermar 33
#include <arch/asm.h>
1 jermar 34
 
40 jermar 35
 
61 decky 36
static char digits[] = "0123456789abcdef"; /**< Hexadecimal characters */
37
static spinlock_t printflock;              /**< printf spinlock */
1 jermar 38
 
61 decky 39
 
40
/** Print NULL terminated string
41
 *
42
 * Print characters from str using putchar() until
43
 * \x00 character is reached.
44
 *
45
 * @param str Characters to print.
46
 *
47
 */
63 decky 48
void print_str(const char *str)
1 jermar 49
{
68 decky 50
    int i = 0;
1 jermar 51
    char c;
68 decky 52
 
1 jermar 53
    while (c = str[i++])
68 decky 54
        putchar(c);
1 jermar 55
}
56
 
57
 
61 decky 58
/** Print hexadecimal digits
59
 *
60
 * Print fixed count of hexadecimal digits from
61
 * the number num. The digits are printed in
62
 * natural left-to-right order starting with
63
 * the width-th digit.
64
 *
65
 * @param num   Number containing digits.
66
 * @param width Count of digits to print.
67
 *
1 jermar 68
 */
181 cejka 69
void print_fixed_hex(const __u64 num, const int width)
1 jermar 70
{
71
    int i;
72
 
73
    for (i = width*8 - 4; i >= 0; i -= 4)
74
        putchar(digits[(num>>i) & 0xf]);
75
}
76
 
61 decky 77
 
78
/** Print number in given base
79
 *
80
 * Print significant digits of a number in given
81
 * base.
82
 *
83
 * @param num  Number to print.
84
 * @param base Base to print the number in (should
85
 *             be in range 2 .. 16).
86
 *
1 jermar 87
 */
68 decky 88
void print_number(const __native num, const unsigned int base)
63 decky 89
{
90
    int val = num;
42 jermar 91
    char d[sizeof(__native)*8+1];       /* this is good enough even for base == 2 */
68 decky 92
    int i = sizeof(__native)*8-1;
93
 
1 jermar 94
    do {
63 decky 95
        d[i--] = digits[val % base];
96
    } while (val /= base);
1 jermar 97
 
42 jermar 98
    d[sizeof(__native)*8] = 0; 
1 jermar 99
    print_str(&d[i + 1]);
100
}
101
 
61 decky 102
 
103
/** General formatted text print
104
 *
105
 * Print text formatted according the fmt parameter
106
 * and variant arguments. Each formatting directive
107
 * begins with % (percentage) character and one of the
108
 * following character:
109
 *
110
 * %    Prints the percentage character.
66 jermar 111
 * s    The next variant argument is treated as char*
61 decky 112
 *      and printed as a NULL terminated string.
66 jermar 113
 * c    The next variant argument is treated as a single char.
77 jermar 114
 * p    The next variant argument is treated as a maximum
115
 *      bit-width integer with respect to architecture
116
 *      and printed in full hexadecimal width.
117
 * P    As with 'p', but '0x' is prefixed.
118
 * q    The next variant argument is treated as a 64b integer
119
 *      and printed in full hexadecimal width.
120
 * Q    As with 'q', but '0x' is prefixed.
66 jermar 121
 * l    The next variant argument is treated as a 32b integer
61 decky 122
 *      and printed in full hexadecimal width.
123
 * L    As with 'l', but '0x' is prefixed.
66 jermar 124
 * w    The next variant argument is treated as a 16b integer
61 decky 125
 *      and printed in full hexadecimal width.
126
 * W    As with 'w', but '0x' is prefixed.
66 jermar 127
 * b    The next variant argument is treated as a 8b integer
61 decky 128
 *      and printed in full hexadecimal width.
129
 * N    As with 'b', but '0x' is prefixed.
66 jermar 130
 * d    The next variant argument is treated as integer
61 decky 131
 *      and printed in standard decimal format (only significant
132
 *      digits).
66 jermar 133
 * x    The next variant argument is treated as integer
61 decky 134
 *      and printed in standard hexadecimal format (only significant
135
 *      digits).
136
 * X    As with 'x', but '0x' is prefixed.
137
 *
138
 * All other characters from fmt except the formatting directives
139
 * are printed in verbatim.
140
 *
141
 * @param fmt Formatting NULL terminated string.
142
 *
1 jermar 143
 */
63 decky 144
void printf(const char *fmt, ...)
1 jermar 145
{
40 jermar 146
    int irqpri, i = 0;
147
    va_list ap;
148
    char c;
1 jermar 149
 
40 jermar 150
    va_start(ap, fmt);
151
 
1 jermar 152
    irqpri = cpu_priority_high();
153
    spinlock_lock(&printflock);
40 jermar 154
 
1 jermar 155
    while (c = fmt[i++]) {
156
        switch (c) {
157
 
158
            /* control character */
159
            case '%':
160
                switch (c = fmt[i++]) {
161
 
162
                /* percentile itself */
163
                case '%':
164
                    break;
165
 
166
                /*
167
                 * String and character conversions.
168
                 */
169
                case 's':
40 jermar 170
                    print_str(va_arg(ap, char_ptr));
1 jermar 171
                    goto loop;
172
 
173
                case 'c':
42 jermar 174
                    c = (char) va_arg(ap, int);
1 jermar 175
                    break;
176
 
177
                /*
178
                         * Hexadecimal conversions with fixed width.
179
                         */
77 jermar 180
                case 'P':
181
                    print_str("0x");
182
                case 'p':
183
                        print_fixed_hex(va_arg(ap, __native), sizeof(__native));
184
                    goto loop;
185
 
186
                case 'Q':
187
                    print_str("0x");
188
                case 'q':
181 cejka 189
                        print_fixed_hex(va_arg(ap, __u64), INT64);
77 jermar 190
                    goto loop;
191
 
1 jermar 192
                case 'L':
193
                    print_str("0x");
194
                case 'l':
40 jermar 195
                        print_fixed_hex(va_arg(ap, __native), INT32);
1 jermar 196
                    goto loop;
197
 
198
                case 'W':
199
                    print_str("0x");
200
                case 'w':
40 jermar 201
                        print_fixed_hex(va_arg(ap, __native), INT16);
1 jermar 202
                    goto loop;
203
 
204
                case 'B':
205
                    print_str("0x");
206
                case 'b':
40 jermar 207
                        print_fixed_hex(va_arg(ap, __native), INT8);
1 jermar 208
                    goto loop;
209
 
210
                /*
211
                         * Decimal and hexadecimal conversions.
212
                         */
213
                case 'd':
40 jermar 214
                        print_number(va_arg(ap, __native), 10);
1 jermar 215
                    goto loop;
216
 
217
                case 'X':
218
                            print_str("0x");
219
                case 'x':
40 jermar 220
                        print_number(va_arg(ap, __native), 16);
1 jermar 221
                    goto loop;
222
 
223
                /*
224
                 * Bad formatting.
225
                 */
226
                default:
227
                    goto out;
228
                }
229
 
230
            default: putchar(c);
231
        }
232
 
233
loop:
234
        ;
235
    }
236
 
237
out:
238
    spinlock_unlock(&printflock);
239
    cpu_priority_restore(irqpri);
40 jermar 240
 
241
    va_end(ap);
1 jermar 242
}