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