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