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