Rev 4213 | Rev 4244 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4213 | Rev 4223 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
3 | * Copyright (c) 2006 Josef Cejka |
3 | * Copyright (c) 2006 Josef Cejka |
4 | * Copyright (c) 2009 Martin Decky |
4 | * Copyright (c) 2009 Martin Decky |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions |
8 | * modification, are permitted provided that the following conditions |
9 | * are met: |
9 | * are met: |
10 | * |
10 | * |
11 | * - Redistributions of source code must retain the above copyright |
11 | * - Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
12 | * notice, this list of conditions and the following disclaimer. |
13 | * - Redistributions in binary form must reproduce the above copyright |
13 | * - Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
15 | * documentation and/or other materials provided with the distribution. |
16 | * - The name of the author may not be used to endorse or promote products |
16 | * - The name of the author may not be used to endorse or promote products |
17 | * derived from this software without specific prior written permission. |
17 | * derived from this software without specific prior written permission. |
18 | * |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
29 | */ |
30 | 30 | ||
31 | /** @addtogroup generic |
31 | /** @addtogroup generic |
32 | * @{ |
32 | * @{ |
33 | */ |
33 | */ |
34 | /** |
34 | /** |
35 | * @file |
35 | * @file |
36 | * @brief Printing functions. |
36 | * @brief Printing functions. |
37 | */ |
37 | */ |
38 | 38 | ||
39 | #include <printf/printf_core.h> |
39 | #include <printf/printf_core.h> |
40 | #include <print.h> |
40 | #include <print.h> |
41 | #include <arch/arg.h> |
41 | #include <arch/arg.h> |
42 | #include <macros.h> |
42 | #include <macros.h> |
43 | #include <string.h> |
43 | #include <string.h> |
44 | #include <arch.h> |
44 | #include <arch.h> |
45 | 45 | ||
46 | /** show prefixes 0x or 0 */ |
46 | /** show prefixes 0x or 0 */ |
47 | #define __PRINTF_FLAG_PREFIX 0x00000001 |
47 | #define __PRINTF_FLAG_PREFIX 0x00000001 |
48 | /** signed / unsigned number */ |
48 | /** signed / unsigned number */ |
49 | #define __PRINTF_FLAG_SIGNED 0x00000002 |
49 | #define __PRINTF_FLAG_SIGNED 0x00000002 |
50 | /** print leading zeroes */ |
50 | /** print leading zeroes */ |
51 | #define __PRINTF_FLAG_ZEROPADDED 0x00000004 |
51 | #define __PRINTF_FLAG_ZEROPADDED 0x00000004 |
52 | /** align to left */ |
52 | /** align to left */ |
53 | #define __PRINTF_FLAG_LEFTALIGNED 0x00000010 |
53 | #define __PRINTF_FLAG_LEFTALIGNED 0x00000010 |
54 | /** always show + sign */ |
54 | /** always show + sign */ |
55 | #define __PRINTF_FLAG_SHOWPLUS 0x00000020 |
55 | #define __PRINTF_FLAG_SHOWPLUS 0x00000020 |
56 | /** print space instead of plus */ |
56 | /** print space instead of plus */ |
57 | #define __PRINTF_FLAG_SPACESIGN 0x00000040 |
57 | #define __PRINTF_FLAG_SPACESIGN 0x00000040 |
58 | /** show big characters */ |
58 | /** show big characters */ |
59 | #define __PRINTF_FLAG_BIGCHARS 0x00000080 |
59 | #define __PRINTF_FLAG_BIGCHARS 0x00000080 |
60 | /** number has - sign */ |
60 | /** number has - sign */ |
61 | #define __PRINTF_FLAG_NEGATIVE 0x00000100 |
61 | #define __PRINTF_FLAG_NEGATIVE 0x00000100 |
62 | 62 | ||
63 | /** |
63 | /** |
64 | * Buffer big enough for 64-bit number printed in base 2, sign, prefix and 0 |
64 | * Buffer big enough for 64-bit number printed in base 2, sign, prefix and 0 |
65 | * to terminate string... (last one is only for better testing end of buffer by |
65 | * to terminate string... (last one is only for better testing end of buffer by |
66 | * zero-filling subroutine) |
66 | * zero-filling subroutine) |
67 | */ |
67 | */ |
68 | #define PRINT_NUMBER_BUFFER_SIZE (64 + 5) |
68 | #define PRINT_NUMBER_BUFFER_SIZE (64 + 5) |
69 | 69 | ||
70 | /** Enumeration of possible arguments types. |
70 | /** Enumeration of possible arguments types. |
71 | */ |
71 | */ |
72 | typedef enum { |
72 | typedef enum { |
73 | PrintfQualifierByte = 0, |
73 | PrintfQualifierByte = 0, |
74 | PrintfQualifierShort, |
74 | PrintfQualifierShort, |
75 | PrintfQualifierInt, |
75 | PrintfQualifierInt, |
76 | PrintfQualifierLong, |
76 | PrintfQualifierLong, |
77 | PrintfQualifierLongLong, |
77 | PrintfQualifierLongLong, |
78 | PrintfQualifierPointer |
78 | PrintfQualifierPointer |
79 | } qualifier_t; |
79 | } qualifier_t; |
80 | 80 | ||
81 | static char nullstr[] = "(NULL)"; |
81 | static char nullstr[] = "(NULL)"; |
82 | static char digits_small[] = "0123456789abcdef"; |
82 | static char digits_small[] = "0123456789abcdef"; |
83 | static char digits_big[] = "0123456789ABCDEF"; |
83 | static char digits_big[] = "0123456789ABCDEF"; |
- | 84 | static char invalch = U_SPECIAL; |
|
84 | 85 | ||
85 | /** Print one or more characters without adding newline. |
86 | /** Print one or more characters without adding newline. |
86 | * |
87 | * |
87 | * @param buf Buffer holding characters with size of |
88 | * @param buf Buffer holding characters with size of |
88 | * at least size bytes. NULL is not allowed! |
89 | * at least size bytes. NULL is not allowed! |
89 | * @param size Size of the buffer in bytes. |
90 | * @param size Size of the buffer in bytes. |
90 | * @param ps Output method and its data. |
91 | * @param ps Output method and its data. |
91 | * |
92 | * |
92 | * @return Number of characters printed. |
93 | * @return Number of characters printed. |
93 | * |
94 | * |
94 | */ |
95 | */ |
95 | static int printf_putnchars(const char *buf, size_t size, |
96 | static int printf_putnchars(const char *buf, size_t size, |
96 | printf_spec_t *ps) |
97 | printf_spec_t *ps) |
97 | { |
98 | { |
98 | return ps->str_write((void *) buf, size, ps->data); |
99 | return ps->str_write((void *) buf, size, ps->data); |
99 | } |
100 | } |
100 | 101 | ||
101 | /** Print one or more wide characters without adding newline. |
102 | /** Print one or more wide characters without adding newline. |
102 | * |
103 | * |
103 | * @param buf Buffer holding wide characters with size of |
104 | * @param buf Buffer holding wide characters with size of |
104 | * at least size bytes. NULL is not allowed! |
105 | * at least size bytes. NULL is not allowed! |
105 | * @param size Size of the buffer in bytes. |
106 | * @param size Size of the buffer in bytes. |
106 | * @param ps Output method and its data. |
107 | * @param ps Output method and its data. |
107 | * |
108 | * |
108 | * @return Number of wide characters printed. |
109 | * @return Number of wide characters printed. |
109 | * |
110 | * |
110 | */ |
111 | */ |
111 | static int printf_wputnchars(const wchar_t *buf, size_t size, |
112 | static int printf_wputnchars(const wchar_t *buf, size_t size, |
112 | printf_spec_t *ps) |
113 | printf_spec_t *ps) |
113 | { |
114 | { |
114 | return ps->wstr_write((void *) buf, size, ps->data); |
115 | return ps->wstr_write((void *) buf, size, ps->data); |
115 | } |
116 | } |
116 | 117 | ||
117 | /** Print string without adding a newline. |
118 | /** Print string without adding a newline. |
118 | * |
119 | * |
119 | * @param str String to print. |
120 | * @param str String to print. |
120 | * @param ps Write function specification and support data. |
121 | * @param ps Write function specification and support data. |
121 | * |
122 | * |
122 | * @return Number of characters printed. |
123 | * @return Number of characters printed. |
123 | * |
124 | * |
124 | */ |
125 | */ |
125 | static int printf_putstr(const char *str, printf_spec_t *ps) |
126 | static int printf_putstr(const char *str, printf_spec_t *ps) |
126 | { |
127 | { |
127 | if (str == NULL) |
128 | if (str == NULL) |
128 | return printf_putnchars(nullstr, str_size(nullstr), ps); |
129 | return printf_putnchars(nullstr, str_size(nullstr), ps); |
129 | 130 | ||
130 | return ps->str_write((void *) str, str_size(str), ps->data); |
131 | return ps->str_write((void *) str, str_size(str), ps->data); |
131 | } |
132 | } |
132 | 133 | ||
133 | /** Print one ASCII character. |
134 | /** Print one ASCII character. |
134 | * |
135 | * |
135 | * @param c ASCII character to be printed. |
136 | * @param c ASCII character to be printed. |
136 | * @param ps Output method. |
137 | * @param ps Output method. |
137 | * |
138 | * |
138 | * @return Number of characters printed. |
139 | * @return Number of characters printed. |
139 | * |
140 | * |
140 | */ |
141 | */ |
141 | static int printf_putchar(const char ch, printf_spec_t *ps) |
142 | static int printf_putchar(const char ch, printf_spec_t *ps) |
142 | { |
143 | { |
143 | if (!ascii_check(ch)) |
144 | if (!ascii_check(ch)) |
144 | return ps->str_write((void *) &invalch, 1, ps->data); |
145 | return ps->str_write((void *) &invalch, 1, ps->data); |
145 | 146 | ||
146 | return ps->str_write(&ch, 1, ps->data); |
147 | return ps->str_write(&ch, 1, ps->data); |
147 | } |
148 | } |
148 | 149 | ||
149 | /** Print one wide character. |
150 | /** Print one wide character. |
150 | * |
151 | * |
151 | * @param c Wide character to be printed. |
152 | * @param c Wide character to be printed. |
152 | * @param ps Output method. |
153 | * @param ps Output method. |
153 | * |
154 | * |
154 | * @return Number of characters printed. |
155 | * @return Number of characters printed. |
155 | * |
156 | * |
156 | */ |
157 | */ |
157 | static int printf_putwchar(const wchar_t ch, printf_spec_t *ps) |
158 | static int printf_putwchar(const wchar_t ch, printf_spec_t *ps) |
158 | { |
159 | { |
159 | if (!chr_check(ch)) |
160 | if (!chr_check(ch)) |
160 | return ps->str_write((void *) &invalch, 1, ps->data); |
161 | return ps->str_write((void *) &invalch, 1, ps->data); |
161 | 162 | ||
162 | return ps->wstr_write(&ch, sizeof(wchar_t), ps->data); |
163 | return ps->wstr_write(&ch, sizeof(wchar_t), ps->data); |
163 | } |
164 | } |
164 | 165 | ||
165 | /** Print one formatted ASCII character. |
166 | /** Print one formatted ASCII character. |
166 | * |
167 | * |
167 | * @param ch Character to print. |
168 | * @param ch Character to print. |
168 | * @param width Width modifier. |
169 | * @param width Width modifier. |
169 | * @param flags Flags that change the way the character is printed. |
170 | * @param flags Flags that change the way the character is printed. |
170 | * |
171 | * |
171 | * @return Number of characters printed, negative value on failure. |
172 | * @return Number of characters printed, negative value on failure. |
172 | * |
173 | * |
173 | */ |
174 | */ |
174 | static int print_char(const char ch, int width, uint32_t flags, printf_spec_t *ps) |
175 | static int print_char(const char ch, int width, uint32_t flags, printf_spec_t *ps) |
175 | { |
176 | { |
176 | count_t counter = 0; |
177 | count_t counter = 0; |
177 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
178 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
178 | while (--width > 0) { |
179 | while (--width > 0) { |
179 | /* |
180 | /* |
180 | * One space is consumed by the character itself, hence |
181 | * One space is consumed by the character itself, hence |
181 | * the predecrement. |
182 | * the predecrement. |
182 | */ |
183 | */ |
183 | if (printf_putchar(' ', ps) > 0) |
184 | if (printf_putchar(' ', ps) > 0) |
184 | counter++; |
185 | counter++; |
185 | } |
186 | } |
186 | } |
187 | } |
187 | 188 | ||
188 | if (printf_putchar(ch, ps) > 0) |
189 | if (printf_putchar(ch, ps) > 0) |
189 | counter++; |
190 | counter++; |
190 | 191 | ||
191 | while (--width > 0) { |
192 | while (--width > 0) { |
192 | /* |
193 | /* |
193 | * One space is consumed by the character itself, hence |
194 | * One space is consumed by the character itself, hence |
194 | * the predecrement. |
195 | * the predecrement. |
195 | */ |
196 | */ |
196 | if (printf_putchar(' ', ps) > 0) |
197 | if (printf_putchar(' ', ps) > 0) |
197 | counter++; |
198 | counter++; |
198 | } |
199 | } |
199 | 200 | ||
200 | return (int) (counter + 1); |
201 | return (int) (counter + 1); |
201 | } |
202 | } |
202 | 203 | ||
203 | /** Print one formatted wide character. |
204 | /** Print one formatted wide character. |
204 | * |
205 | * |
205 | * @param ch Character to print. |
206 | * @param ch Character to print. |
206 | * @param width Width modifier. |
207 | * @param width Width modifier. |
207 | * @param flags Flags that change the way the character is printed. |
208 | * @param flags Flags that change the way the character is printed. |
208 | * |
209 | * |
209 | * @return Number of characters printed, negative value on failure. |
210 | * @return Number of characters printed, negative value on failure. |
210 | * |
211 | * |
211 | */ |
212 | */ |
212 | static int print_wchar(const wchar_t ch, int width, uint32_t flags, printf_spec_t *ps) |
213 | static int print_wchar(const wchar_t ch, int width, uint32_t flags, printf_spec_t *ps) |
213 | { |
214 | { |
214 | count_t counter = 0; |
215 | count_t counter = 0; |
215 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
216 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
216 | while (--width > 0) { |
217 | while (--width > 0) { |
217 | /* |
218 | /* |
218 | * One space is consumed by the character itself, hence |
219 | * One space is consumed by the character itself, hence |
219 | * the predecrement. |
220 | * the predecrement. |
220 | */ |
221 | */ |
221 | if (printf_putchar(' ', ps) > 0) |
222 | if (printf_putchar(' ', ps) > 0) |
222 | counter++; |
223 | counter++; |
223 | } |
224 | } |
224 | } |
225 | } |
225 | 226 | ||
226 | if (printf_putwchar(ch, ps) > 0) |
227 | if (printf_putwchar(ch, ps) > 0) |
227 | counter++; |
228 | counter++; |
228 | 229 | ||
229 | while (--width > 0) { |
230 | while (--width > 0) { |
230 | /* |
231 | /* |
231 | * One space is consumed by the character itself, hence |
232 | * One space is consumed by the character itself, hence |
232 | * the predecrement. |
233 | * the predecrement. |
233 | */ |
234 | */ |
234 | if (printf_putchar(' ', ps) > 0) |
235 | if (printf_putchar(' ', ps) > 0) |
235 | counter++; |
236 | counter++; |
236 | } |
237 | } |
237 | 238 | ||
238 | return (int) (counter + 1); |
239 | return (int) (counter + 1); |
239 | } |
240 | } |
240 | 241 | ||
241 | /** Print string. |
242 | /** Print string. |
242 | * |
243 | * |
243 | * @param str String to be printed. |
244 | * @param str String to be printed. |
244 | * @param width Width modifier. |
245 | * @param width Width modifier. |
245 | * @param precision Precision modifier. |
246 | * @param precision Precision modifier. |
246 | * @param flags Flags that modify the way the string is printed. |
247 | * @param flags Flags that modify the way the string is printed. |
247 | * |
248 | * |
248 | * @return Number of characters printed, negative value on failure. |
249 | * @return Number of characters printed, negative value on failure. |
249 | */ |
250 | */ |
250 | static int print_str(char *str, int width, unsigned int precision, |
251 | static int print_str(char *str, int width, unsigned int precision, |
251 | uint32_t flags, printf_spec_t *ps) |
252 | uint32_t flags, printf_spec_t *ps) |
252 | { |
253 | { |
253 | if (str == NULL) |
254 | if (str == NULL) |
254 | return printf_putstr(nullstr, ps); |
255 | return printf_putstr(nullstr, ps); |
255 | 256 | ||
256 | /* Print leading spaces. */ |
257 | /* Print leading spaces. */ |
257 | count_t strw = str_length(str); |
258 | count_t strw = str_length(str); |
258 | if (precision == 0) |
259 | if (precision == 0) |
259 | precision = strw; |
260 | precision = strw; |
260 | 261 | ||
261 | /* Left padding */ |
262 | /* Left padding */ |
262 | count_t counter = 0; |
263 | count_t counter = 0; |
263 | width -= precision; |
264 | width -= precision; |
264 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
265 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
265 | while (width-- > 0) { |
266 | while (width-- > 0) { |
266 | if (printf_putchar(' ', ps) == 1) |
267 | if (printf_putchar(' ', ps) == 1) |
267 | counter++; |
268 | counter++; |
268 | } |
269 | } |
269 | } |
270 | } |
270 | 271 | ||
271 | /* Part of @a str fitting into the alloted space. */ |
272 | /* Part of @a str fitting into the alloted space. */ |
272 | int retval; |
273 | int retval; |
273 | size_t size = str_lsize(str, precision); |
274 | size_t size = str_lsize(str, precision); |
274 | if ((retval = printf_putnchars(str, size, ps)) < 0) |
275 | if ((retval = printf_putnchars(str, size, ps)) < 0) |
275 | return -counter; |
276 | return -counter; |
276 | 277 | ||
277 | counter += retval; |
278 | counter += retval; |
278 | 279 | ||
279 | /* Right padding */ |
280 | /* Right padding */ |
280 | while (width-- > 0) { |
281 | while (width-- > 0) { |
281 | if (printf_putchar(' ', ps) == 1) |
282 | if (printf_putchar(' ', ps) == 1) |
282 | counter++; |
283 | counter++; |
283 | } |
284 | } |
284 | 285 | ||
285 | return ((int) counter); |
286 | return ((int) counter); |
286 | 287 | ||
287 | } |
288 | } |
288 | 289 | ||
289 | /** Print wide string. |
290 | /** Print wide string. |
290 | * |
291 | * |
291 | * @param str Wide string to be printed. |
292 | * @param str Wide string to be printed. |
292 | * @param width Width modifier. |
293 | * @param width Width modifier. |
293 | * @param precision Precision modifier. |
294 | * @param precision Precision modifier. |
294 | * @param flags Flags that modify the way the string is printed. |
295 | * @param flags Flags that modify the way the string is printed. |
295 | * |
296 | * |
296 | * @return Number of wide characters printed, negative value on failure. |
297 | * @return Number of wide characters printed, negative value on failure. |
297 | */ |
298 | */ |
298 | static int print_wstr(wchar_t *str, int width, unsigned int precision, |
299 | static int print_wstr(wchar_t *str, int width, unsigned int precision, |
299 | uint32_t flags, printf_spec_t *ps) |
300 | uint32_t flags, printf_spec_t *ps) |
300 | { |
301 | { |
301 | if (str == NULL) |
302 | if (str == NULL) |
302 | return printf_putstr(nullstr, ps); |
303 | return printf_putstr(nullstr, ps); |
303 | 304 | ||
304 | /* Print leading spaces. */ |
305 | /* Print leading spaces. */ |
305 | size_t strw = wstr_length(str); |
306 | size_t strw = wstr_length(str); |
306 | if (precision == 0) |
307 | if (precision == 0) |
307 | precision = strw; |
308 | precision = strw; |
308 | 309 | ||
309 | /* Left padding */ |
310 | /* Left padding */ |
310 | count_t counter = 0; |
311 | count_t counter = 0; |
311 | width -= precision; |
312 | width -= precision; |
312 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
313 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
313 | while (width-- > 0) { |
314 | while (width-- > 0) { |
314 | if (printf_putchar(' ', ps) == 1) |
315 | if (printf_putchar(' ', ps) == 1) |
315 | counter++; |
316 | counter++; |
316 | } |
317 | } |
317 | } |
318 | } |
318 | 319 | ||
319 | /* Part of @a wstr fitting into the alloted space. */ |
320 | /* Part of @a wstr fitting into the alloted space. */ |
320 | int retval; |
321 | int retval; |
321 | size_t size = wstr_lsize(str, precision); |
322 | size_t size = wstr_lsize(str, precision); |
322 | if ((retval = printf_wputnchars(str, size, ps)) < 0) |
323 | if ((retval = printf_wputnchars(str, size, ps)) < 0) |
323 | return -counter; |
324 | return -counter; |
324 | 325 | ||
325 | counter += retval; |
326 | counter += retval; |
326 | 327 | ||
327 | /* Right padding */ |
328 | /* Right padding */ |
328 | while (width-- > 0) { |
329 | while (width-- > 0) { |
329 | if (printf_putchar(' ', ps) == 1) |
330 | if (printf_putchar(' ', ps) == 1) |
330 | counter++; |
331 | counter++; |
331 | } |
332 | } |
332 | 333 | ||
333 | return ((int) counter); |
334 | return ((int) counter); |
334 | } |
335 | } |
335 | 336 | ||
336 | /** Print a number in a given base. |
337 | /** Print a number in a given base. |
337 | * |
338 | * |
338 | * Print significant digits of a number in given base. |
339 | * Print significant digits of a number in given base. |
339 | * |
340 | * |
340 | * @param num Number to print. |
341 | * @param num Number to print. |
341 | * @param width Width modifier. |
342 | * @param width Width modifier. |
342 | * @param precision Precision modifier. |
343 | * @param precision Precision modifier. |
343 | * @param base Base to print the number in (must be between 2 and 16). |
344 | * @param base Base to print the number in (must be between 2 and 16). |
344 | * @param flags Flags that modify the way the number is printed. |
345 | * @param flags Flags that modify the way the number is printed. |
345 | * |
346 | * |
346 | * @return Number of characters printed. |
347 | * @return Number of characters printed. |
347 | * |
348 | * |
348 | */ |
349 | */ |
349 | static int print_number(uint64_t num, int width, int precision, int base, |
350 | static int print_number(uint64_t num, int width, int precision, int base, |
350 | uint32_t flags, printf_spec_t *ps) |
351 | uint32_t flags, printf_spec_t *ps) |
351 | { |
352 | { |
352 | char *digits; |
353 | char *digits; |
353 | if (flags & __PRINTF_FLAG_BIGCHARS) |
354 | if (flags & __PRINTF_FLAG_BIGCHARS) |
354 | digits = digits_big; |
355 | digits = digits_big; |
355 | else |
356 | else |
356 | digits = digits_small; |
357 | digits = digits_small; |
357 | 358 | ||
358 | char data[PRINT_NUMBER_BUFFER_SIZE]; |
359 | char data[PRINT_NUMBER_BUFFER_SIZE]; |
359 | char *ptr = &data[PRINT_NUMBER_BUFFER_SIZE - 1]; |
360 | char *ptr = &data[PRINT_NUMBER_BUFFER_SIZE - 1]; |
360 | 361 | ||
361 | /* Size of number with all prefixes and signs */ |
362 | /* Size of number with all prefixes and signs */ |
362 | int size = 0; |
363 | int size = 0; |
363 | 364 | ||
364 | /* Put zero at end of string */ |
365 | /* Put zero at end of string */ |
365 | *ptr-- = 0; |
366 | *ptr-- = 0; |
366 | 367 | ||
367 | if (num == 0) { |
368 | if (num == 0) { |
368 | *ptr-- = '0'; |
369 | *ptr-- = '0'; |
369 | size++; |
370 | size++; |
370 | } else { |
371 | } else { |
371 | do { |
372 | do { |
372 | *ptr-- = digits[num % base]; |
373 | *ptr-- = digits[num % base]; |
373 | size++; |
374 | size++; |
374 | } while (num /= base); |
375 | } while (num /= base); |
375 | } |
376 | } |
376 | 377 | ||
377 | /* Size of plain number */ |
378 | /* Size of plain number */ |
378 | int number_size = size; |
379 | int number_size = size; |
379 | 380 | ||
380 | /* |
381 | /* |
381 | * Collect the sum of all prefixes/signs/etc. to calculate padding and |
382 | * Collect the sum of all prefixes/signs/etc. to calculate padding and |
382 | * leading zeroes. |
383 | * leading zeroes. |
383 | */ |
384 | */ |
384 | if (flags & __PRINTF_FLAG_PREFIX) { |
385 | if (flags & __PRINTF_FLAG_PREFIX) { |
385 | switch(base) { |
386 | switch(base) { |
386 | case 2: |
387 | case 2: |
387 | /* Binary formating is not standard, but usefull */ |
388 | /* Binary formating is not standard, but usefull */ |
388 | size += 2; |
389 | size += 2; |
389 | break; |
390 | break; |
390 | case 8: |
391 | case 8: |
391 | size++; |
392 | size++; |
392 | break; |
393 | break; |
393 | case 16: |
394 | case 16: |
394 | size += 2; |
395 | size += 2; |
395 | break; |
396 | break; |
396 | } |
397 | } |
397 | } |
398 | } |
398 | 399 | ||
399 | char sgn = 0; |
400 | char sgn = 0; |
400 | if (flags & __PRINTF_FLAG_SIGNED) { |
401 | if (flags & __PRINTF_FLAG_SIGNED) { |
401 | if (flags & __PRINTF_FLAG_NEGATIVE) { |
402 | if (flags & __PRINTF_FLAG_NEGATIVE) { |
402 | sgn = '-'; |
403 | sgn = '-'; |
403 | size++; |
404 | size++; |
404 | } else if (flags & __PRINTF_FLAG_SHOWPLUS) { |
405 | } else if (flags & __PRINTF_FLAG_SHOWPLUS) { |
405 | sgn = '+'; |
406 | sgn = '+'; |
406 | size++; |
407 | size++; |
407 | } else if (flags & __PRINTF_FLAG_SPACESIGN) { |
408 | } else if (flags & __PRINTF_FLAG_SPACESIGN) { |
408 | sgn = ' '; |
409 | sgn = ' '; |
409 | size++; |
410 | size++; |
410 | } |
411 | } |
411 | } |
412 | } |
412 | 413 | ||
413 | if (flags & __PRINTF_FLAG_LEFTALIGNED) |
414 | if (flags & __PRINTF_FLAG_LEFTALIGNED) |
414 | flags &= ~__PRINTF_FLAG_ZEROPADDED; |
415 | flags &= ~__PRINTF_FLAG_ZEROPADDED; |
415 | 416 | ||
416 | /* |
417 | /* |
417 | * If the number is left-aligned or precision is specified then |
418 | * If the number is left-aligned or precision is specified then |
418 | * padding with zeros is ignored. |
419 | * padding with zeros is ignored. |
419 | */ |
420 | */ |
420 | if (flags & __PRINTF_FLAG_ZEROPADDED) { |
421 | if (flags & __PRINTF_FLAG_ZEROPADDED) { |
421 | if ((precision == 0) && (width > size)) |
422 | if ((precision == 0) && (width > size)) |
422 | precision = width - size + number_size; |
423 | precision = width - size + number_size; |
423 | } |
424 | } |
424 | 425 | ||
425 | /* Print leading spaces */ |
426 | /* Print leading spaces */ |
426 | if (number_size > precision) { |
427 | if (number_size > precision) { |
427 | /* Print the whole number, not only a part */ |
428 | /* Print the whole number, not only a part */ |
428 | precision = number_size; |
429 | precision = number_size; |
429 | } |
430 | } |
430 | 431 | ||
431 | width -= precision + size - number_size; |
432 | width -= precision + size - number_size; |
432 | count_t counter = 0; |
433 | count_t counter = 0; |
433 | 434 | ||
434 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
435 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
435 | while (width-- > 0) { |
436 | while (width-- > 0) { |
436 | if (printf_putchar(' ', ps) == 1) |
437 | if (printf_putchar(' ', ps) == 1) |
437 | counter++; |
438 | counter++; |
438 | } |
439 | } |
439 | } |
440 | } |
440 | 441 | ||
441 | /* Print sign */ |
442 | /* Print sign */ |
442 | if (sgn) { |
443 | if (sgn) { |
443 | if (printf_putchar(sgn, ps) == 1) |
444 | if (printf_putchar(sgn, ps) == 1) |
444 | counter++; |
445 | counter++; |
445 | } |
446 | } |
446 | 447 | ||
447 | /* Print prefix */ |
448 | /* Print prefix */ |
448 | if (flags & __PRINTF_FLAG_PREFIX) { |
449 | if (flags & __PRINTF_FLAG_PREFIX) { |
449 | switch(base) { |
450 | switch(base) { |
450 | case 2: |
451 | case 2: |
451 | /* Binary formating is not standard, but usefull */ |
452 | /* Binary formating is not standard, but usefull */ |
452 | if (printf_putchar('0', ps) == 1) |
453 | if (printf_putchar('0', ps) == 1) |
453 | counter++; |
454 | counter++; |
454 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
455 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
455 | if (printf_putchar('B', ps) == 1) |
456 | if (printf_putchar('B', ps) == 1) |
456 | counter++; |
457 | counter++; |
457 | } else { |
458 | } else { |
458 | if (printf_putchar('b', ps) == 1) |
459 | if (printf_putchar('b', ps) == 1) |
459 | counter++; |
460 | counter++; |
460 | } |
461 | } |
461 | break; |
462 | break; |
462 | case 8: |
463 | case 8: |
463 | if (printf_putchar('o', ps) == 1) |
464 | if (printf_putchar('o', ps) == 1) |
464 | counter++; |
465 | counter++; |
465 | break; |
466 | break; |
466 | case 16: |
467 | case 16: |
467 | if (printf_putchar('0', ps) == 1) |
468 | if (printf_putchar('0', ps) == 1) |
468 | counter++; |
469 | counter++; |
469 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
470 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
470 | if (printf_putchar('X', ps) == 1) |
471 | if (printf_putchar('X', ps) == 1) |
471 | counter++; |
472 | counter++; |
472 | } else { |
473 | } else { |
473 | if (printf_putchar('x', ps) == 1) |
474 | if (printf_putchar('x', ps) == 1) |
474 | counter++; |
475 | counter++; |
475 | } |
476 | } |
476 | break; |
477 | break; |
477 | } |
478 | } |
478 | } |
479 | } |
479 | 480 | ||
480 | /* Print leading zeroes */ |
481 | /* Print leading zeroes */ |
481 | precision -= number_size; |
482 | precision -= number_size; |
482 | while (precision-- > 0) { |
483 | while (precision-- > 0) { |
483 | if (printf_putchar('0', ps) == 1) |
484 | if (printf_putchar('0', ps) == 1) |
484 | counter++; |
485 | counter++; |
485 | } |
486 | } |
486 | 487 | ||
487 | /* Print the number itself */ |
488 | /* Print the number itself */ |
488 | int retval; |
489 | int retval; |
489 | if ((retval = printf_putstr(++ptr, ps)) > 0) |
490 | if ((retval = printf_putstr(++ptr, ps)) > 0) |
490 | counter += retval; |
491 | counter += retval; |
491 | 492 | ||
492 | /* Print tailing spaces */ |
493 | /* Print tailing spaces */ |
493 | 494 | ||
494 | while (width-- > 0) { |
495 | while (width-- > 0) { |
495 | if (printf_putchar(' ', ps) == 1) |
496 | if (printf_putchar(' ', ps) == 1) |
496 | counter++; |
497 | counter++; |
497 | } |
498 | } |
498 | 499 | ||
499 | return ((int) counter); |
500 | return ((int) counter); |
500 | } |
501 | } |
501 | 502 | ||
502 | /** Print formatted string. |
503 | /** Print formatted string. |
503 | * |
504 | * |
504 | * Print string formatted according to the fmt parameter and variadic arguments. |
505 | * Print string formatted according to the fmt parameter and variadic arguments. |
505 | * Each formatting directive must have the following form: |
506 | * Each formatting directive must have the following form: |
506 | * |
507 | * |
507 | * \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION |
508 | * \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION |
508 | * |
509 | * |
509 | * FLAGS:@n |
510 | * FLAGS:@n |
510 | * - "#" Force to print prefix. For \%o conversion, the prefix is 0, for |
511 | * - "#" Force to print prefix. For \%o conversion, the prefix is 0, for |
511 | * \%x and \%X prefixes are 0x and 0X and for conversion \%b the |
512 | * \%x and \%X prefixes are 0x and 0X and for conversion \%b the |
512 | * prefix is 0b. |
513 | * prefix is 0b. |
513 | * |
514 | * |
514 | * - "-" Align to left. |
515 | * - "-" Align to left. |
515 | * |
516 | * |
516 | * - "+" Print positive sign just as negative. |
517 | * - "+" Print positive sign just as negative. |
517 | * |
518 | * |
518 | * - " " If the printed number is positive and "+" flag is not set, |
519 | * - " " If the printed number is positive and "+" flag is not set, |
519 | * print space in place of sign. |
520 | * print space in place of sign. |
520 | * |
521 | * |
521 | * - "0" Print 0 as padding instead of spaces. Zeroes are placed between |
522 | * - "0" Print 0 as padding instead of spaces. Zeroes are placed between |
522 | * sign and the rest of the number. This flag is ignored if "-" |
523 | * sign and the rest of the number. This flag is ignored if "-" |
523 | * flag is specified. |
524 | * flag is specified. |
524 | * |
525 | * |
525 | * WIDTH:@n |
526 | * WIDTH:@n |
526 | * - Specify the minimal width of a printed argument. If it is bigger, |
527 | * - Specify the minimal width of a printed argument. If it is bigger, |
527 | * width is ignored. If width is specified with a "*" character instead of |
528 | * width is ignored. If width is specified with a "*" character instead of |
528 | * number, width is taken from parameter list. And integer parameter is |
529 | * number, width is taken from parameter list. And integer parameter is |
529 | * expected before parameter for processed conversion specification. If |
530 | * expected before parameter for processed conversion specification. If |
530 | * this value is negative its absolute value is taken and the "-" flag is |
531 | * this value is negative its absolute value is taken and the "-" flag is |
531 | * set. |
532 | * set. |
532 | * |
533 | * |
533 | * PRECISION:@n |
534 | * PRECISION:@n |
534 | * - Value precision. For numbers it specifies minimum valid numbers. |
535 | * - Value precision. For numbers it specifies minimum valid numbers. |
535 | * Smaller numbers are printed with leading zeroes. Bigger numbers are not |
536 | * Smaller numbers are printed with leading zeroes. Bigger numbers are not |
536 | * affected. Strings with more than precision characters are cut off. Just |
537 | * affected. Strings with more than precision characters are cut off. Just |
537 | * as with width, an "*" can be used used instead of a number. An integer |
538 | * as with width, an "*" can be used used instead of a number. An integer |
538 | * value is then expected in parameters. When both width and precision are |
539 | * value is then expected in parameters. When both width and precision are |
539 | * specified using "*", the first parameter is used for width and the |
540 | * specified using "*", the first parameter is used for width and the |
540 | * second one for precision. |
541 | * second one for precision. |
541 | * |
542 | * |
542 | * TYPE:@n |
543 | * TYPE:@n |
543 | * - "hh" Signed or unsigned char.@n |
544 | * - "hh" Signed or unsigned char.@n |
544 | * - "h" Signed or unsigned short.@n |
545 | * - "h" Signed or unsigned short.@n |
545 | * - "" Signed or unsigned int (default value).@n |
546 | * - "" Signed or unsigned int (default value).@n |
546 | * - "l" Signed or unsigned long int.@n |
547 | * - "l" Signed or unsigned long int.@n |
547 | * If conversion is "c", the character is wchar_t (wide character).@n |
548 | * If conversion is "c", the character is wchar_t (wide character).@n |
548 | * If conversion is "s", the string is wchar_t * (wide string).@n |
549 | * If conversion is "s", the string is wchar_t * (wide string).@n |
549 | * - "ll" Signed or unsigned long long int.@n |
550 | * - "ll" Signed or unsigned long long int.@n |
550 | * |
551 | * |
551 | * CONVERSION:@n |
552 | * CONVERSION:@n |
552 | * - % Print percentile character itself. |
553 | * - % Print percentile character itself. |
553 | * |
554 | * |
554 | * - c Print single character. The character is expected to be plain |
555 | * - c Print single character. The character is expected to be plain |
555 | * ASCII (e.g. only values 0 .. 127 are valid).@n |
556 | * ASCII (e.g. only values 0 .. 127 are valid).@n |
556 | * If type is "l", then the character is expected to be wide character |
557 | * If type is "l", then the character is expected to be wide character |
557 | * (e.g. values 0 .. 0x10ffff are valid). |
558 | * (e.g. values 0 .. 0x10ffff are valid). |
558 | * |
559 | * |
559 | * - s Print zero terminated string. If a NULL value is passed as |
560 | * - s Print zero terminated string. If a NULL value is passed as |
560 | * value, "(NULL)" is printed instead.@n |
561 | * value, "(NULL)" is printed instead.@n |
561 | * If type is "l", then the string is expected to be wide string. |
562 | * If type is "l", then the string is expected to be wide string. |
562 | * |
563 | * |
563 | * - P, p Print value of a pointer. Void * value is expected and it is |
564 | * - P, p Print value of a pointer. Void * value is expected and it is |
564 | * printed in hexadecimal notation with prefix (as with \%#X / \%#x |
565 | * printed in hexadecimal notation with prefix (as with \%#X / \%#x |
565 | * for 32-bit or \%#X / \%#x for 64-bit long pointers). |
566 | * for 32-bit or \%#X / \%#x for 64-bit long pointers). |
566 | * |
567 | * |
567 | * - b Print value as unsigned binary number. Prefix is not printed by |
568 | * - b Print value as unsigned binary number. Prefix is not printed by |
568 | * default. (Nonstandard extension.) |
569 | * default. (Nonstandard extension.) |
569 | * |
570 | * |
570 | * - o Print value as unsigned octal number. Prefix is not printed by |
571 | * - o Print value as unsigned octal number. Prefix is not printed by |
571 | * default. |
572 | * default. |
572 | * |
573 | * |
573 | * - d, i Print signed decimal number. There is no difference between d |
574 | * - d, i Print signed decimal number. There is no difference between d |
574 | * and i conversion. |
575 | * and i conversion. |
575 | * |
576 | * |
576 | * - u Print unsigned decimal number. |
577 | * - u Print unsigned decimal number. |
577 | * |
578 | * |
578 | * - X, x Print hexadecimal number with upper- or lower-case. Prefix is |
579 | * - X, x Print hexadecimal number with upper- or lower-case. Prefix is |
579 | * not printed by default. |
580 | * not printed by default. |
580 | * |
581 | * |
581 | * All other characters from fmt except the formatting directives are printed |
582 | * All other characters from fmt except the formatting directives are printed |
582 | * verbatim. |
583 | * verbatim. |
583 | * |
584 | * |
584 | * @param fmt Format NULL-terminated string. |
585 | * @param fmt Format NULL-terminated string. |
585 | * |
586 | * |
586 | * @return Number of characters printed, negative value on failure. |
587 | * @return Number of characters printed, negative value on failure. |
587 | * |
588 | * |
588 | */ |
589 | */ |
589 | int printf_core(const char *fmt, printf_spec_t *ps, va_list ap) |
590 | int printf_core(const char *fmt, printf_spec_t *ps, va_list ap) |
590 | { |
591 | { |
591 | size_t i; /* Index of the currently processed character from fmt */ |
592 | size_t i; /* Index of the currently processed character from fmt */ |
592 | size_t nxt = 0; /* Index of the next character from fmt */ |
593 | size_t nxt = 0; /* Index of the next character from fmt */ |
593 | size_t j = 0; /* Index to the first not printed nonformating character */ |
594 | size_t j = 0; /* Index to the first not printed nonformating character */ |
594 | 595 | ||
595 | count_t counter = 0; /* Number of characters printed */ |
596 | count_t counter = 0; /* Number of characters printed */ |
596 | int retval; /* Return values from nested functions */ |
597 | int retval; /* Return values from nested functions */ |
597 | 598 | ||
598 | while (true) { |
599 | while (true) { |
599 | i = nxt; |
600 | i = nxt; |
600 | wchar_t uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
601 | wchar_t uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
601 | 602 | ||
602 | if (uc == 0) |
603 | if (uc == 0) |
603 | break; |
604 | break; |
604 | 605 | ||
605 | /* Control character */ |
606 | /* Control character */ |
606 | if (uc == '%') { |
607 | if (uc == '%') { |
607 | /* Print common characters if any processed */ |
608 | /* Print common characters if any processed */ |
608 | if (i > j) { |
609 | if (i > j) { |
609 | if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) { |
610 | if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) { |
610 | /* Error */ |
611 | /* Error */ |
611 | counter = -counter; |
612 | counter = -counter; |
612 | goto out; |
613 | goto out; |
613 | } |
614 | } |
614 | counter += retval; |
615 | counter += retval; |
615 | } |
616 | } |
616 | 617 | ||
617 | j = i; |
618 | j = i; |
618 | 619 | ||
619 | /* Parse modifiers */ |
620 | /* Parse modifiers */ |
620 | uint32_t flags = 0; |
621 | uint32_t flags = 0; |
621 | bool end = false; |
622 | bool end = false; |
622 | 623 | ||
623 | do { |
624 | do { |
624 | i = nxt; |
625 | i = nxt; |
625 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
626 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
626 | switch (uc) { |
627 | switch (uc) { |
627 | case '#': |
628 | case '#': |
628 | flags |= __PRINTF_FLAG_PREFIX; |
629 | flags |= __PRINTF_FLAG_PREFIX; |
629 | break; |
630 | break; |
630 | case '-': |
631 | case '-': |
631 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
632 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
632 | break; |
633 | break; |
633 | case '+': |
634 | case '+': |
634 | flags |= __PRINTF_FLAG_SHOWPLUS; |
635 | flags |= __PRINTF_FLAG_SHOWPLUS; |
635 | break; |
636 | break; |
636 | case ' ': |
637 | case ' ': |
637 | flags |= __PRINTF_FLAG_SPACESIGN; |
638 | flags |= __PRINTF_FLAG_SPACESIGN; |
638 | break; |
639 | break; |
639 | case '0': |
640 | case '0': |
640 | flags |= __PRINTF_FLAG_ZEROPADDED; |
641 | flags |= __PRINTF_FLAG_ZEROPADDED; |
641 | break; |
642 | break; |
642 | default: |
643 | default: |
643 | end = true; |
644 | end = true; |
644 | }; |
645 | }; |
645 | } while (!end); |
646 | } while (!end); |
646 | 647 | ||
647 | /* Width & '*' operator */ |
648 | /* Width & '*' operator */ |
648 | int width = 0; |
649 | int width = 0; |
649 | if (isdigit(uc)) { |
650 | if (isdigit(uc)) { |
650 | while (true) { |
651 | while (true) { |
651 | width *= 10; |
652 | width *= 10; |
652 | width += uc - '0'; |
653 | width += uc - '0'; |
653 | 654 | ||
654 | i = nxt; |
655 | i = nxt; |
655 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
656 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
656 | if (uc == 0) |
657 | if (uc == 0) |
657 | break; |
658 | break; |
658 | if (!isdigit(uc)) |
659 | if (!isdigit(uc)) |
659 | break; |
660 | break; |
660 | } |
661 | } |
661 | } else if (uc == '*') { |
662 | } else if (uc == '*') { |
662 | /* Get width value from argument list */ |
663 | /* Get width value from argument list */ |
663 | i = nxt; |
664 | i = nxt; |
664 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
665 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
665 | width = (int) va_arg(ap, int); |
666 | width = (int) va_arg(ap, int); |
666 | if (width < 0) { |
667 | if (width < 0) { |
667 | /* Negative width sets '-' flag */ |
668 | /* Negative width sets '-' flag */ |
668 | width *= -1; |
669 | width *= -1; |
669 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
670 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
670 | } |
671 | } |
671 | } |
672 | } |
672 | 673 | ||
673 | /* Precision and '*' operator */ |
674 | /* Precision and '*' operator */ |
674 | int precision = 0; |
675 | int precision = 0; |
675 | if (uc == '.') { |
676 | if (uc == '.') { |
676 | i = nxt; |
677 | i = nxt; |
677 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
678 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
678 | if (isdigit(uc)) { |
679 | if (isdigit(uc)) { |
679 | while (true) { |
680 | while (true) { |
680 | precision *= 10; |
681 | precision *= 10; |
681 | precision += uc - '0'; |
682 | precision += uc - '0'; |
682 | 683 | ||
683 | i = nxt; |
684 | i = nxt; |
684 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
685 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
685 | if (uc == 0) |
686 | if (uc == 0) |
686 | break; |
687 | break; |
687 | if (!isdigit(uc)) |
688 | if (!isdigit(uc)) |
688 | break; |
689 | break; |
689 | } |
690 | } |
690 | } else if (uc == '*') { |
691 | } else if (uc == '*') { |
691 | /* Get precision value from the argument list */ |
692 | /* Get precision value from the argument list */ |
692 | i = nxt; |
693 | i = nxt; |
693 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
694 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
694 | precision = (int) va_arg(ap, int); |
695 | precision = (int) va_arg(ap, int); |
695 | if (precision < 0) { |
696 | if (precision < 0) { |
696 | /* Ignore negative precision */ |
697 | /* Ignore negative precision */ |
697 | precision = 0; |
698 | precision = 0; |
698 | } |
699 | } |
699 | } |
700 | } |
700 | } |
701 | } |
701 | 702 | ||
702 | qualifier_t qualifier; |
703 | qualifier_t qualifier; |
703 | 704 | ||
704 | switch (uc) { |
705 | switch (uc) { |
705 | /** @todo Unimplemented qualifiers: |
706 | /** @todo Unimplemented qualifiers: |
706 | * t ptrdiff_t - ISO C 99 |
707 | * t ptrdiff_t - ISO C 99 |
707 | */ |
708 | */ |
708 | case 'h': |
709 | case 'h': |
709 | /* Char or short */ |
710 | /* Char or short */ |
710 | qualifier = PrintfQualifierShort; |
711 | qualifier = PrintfQualifierShort; |
711 | i = nxt; |
712 | i = nxt; |
712 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
713 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
713 | if (uc == 'h') { |
714 | if (uc == 'h') { |
714 | i = nxt; |
715 | i = nxt; |
715 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
716 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
716 | qualifier = PrintfQualifierByte; |
717 | qualifier = PrintfQualifierByte; |
717 | } |
718 | } |
718 | break; |
719 | break; |
719 | case 'l': |
720 | case 'l': |
720 | /* Long or long long */ |
721 | /* Long or long long */ |
721 | qualifier = PrintfQualifierLong; |
722 | qualifier = PrintfQualifierLong; |
722 | i = nxt; |
723 | i = nxt; |
723 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
724 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
724 | if (uc == 'l') { |
725 | if (uc == 'l') { |
725 | i = nxt; |
726 | i = nxt; |
726 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
727 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
727 | qualifier = PrintfQualifierLongLong; |
728 | qualifier = PrintfQualifierLongLong; |
728 | } |
729 | } |
729 | break; |
730 | break; |
730 | default: |
731 | default: |
731 | /* Default type */ |
732 | /* Default type */ |
732 | qualifier = PrintfQualifierInt; |
733 | qualifier = PrintfQualifierInt; |
733 | } |
734 | } |
734 | 735 | ||
735 | unsigned int base = 10; |
736 | unsigned int base = 10; |
736 | 737 | ||
737 | switch (uc) { |
738 | switch (uc) { |
738 | /* |
739 | /* |
739 | * String and character conversions. |
740 | * String and character conversions. |
740 | */ |
741 | */ |
741 | case 's': |
742 | case 's': |
742 | if (qualifier == PrintfQualifierLong) |
743 | if (qualifier == PrintfQualifierLong) |
743 | retval = print_wstr(va_arg(ap, wchar_t *), width, precision, flags, ps); |
744 | retval = print_wstr(va_arg(ap, wchar_t *), width, precision, flags, ps); |
744 | else |
745 | else |
745 | retval = print_str(va_arg(ap, char *), width, precision, flags, ps); |
746 | retval = print_str(va_arg(ap, char *), width, precision, flags, ps); |
746 | 747 | ||
747 | if (retval < 0) { |
748 | if (retval < 0) { |
748 | counter = -counter; |
749 | counter = -counter; |
749 | goto out; |
750 | goto out; |
750 | } |
751 | } |
751 | 752 | ||
752 | counter += retval; |
753 | counter += retval; |
753 | j = nxt; |
754 | j = nxt; |
754 | goto next_char; |
755 | goto next_char; |
755 | case 'c': |
756 | case 'c': |
756 | if (qualifier == PrintfQualifierLong) |
757 | if (qualifier == PrintfQualifierLong) |
757 | retval = print_wchar(va_arg(ap, wchar_t), width, flags, ps); |
758 | retval = print_wchar(va_arg(ap, wchar_t), width, flags, ps); |
758 | else |
759 | else |
759 | retval = print_char(va_arg(ap, unsigned int), width, flags, ps); |
760 | retval = print_char(va_arg(ap, unsigned int), width, flags, ps); |
760 | 761 | ||
761 | if (retval < 0) { |
762 | if (retval < 0) { |
762 | counter = -counter; |
763 | counter = -counter; |
763 | goto out; |
764 | goto out; |
764 | }; |
765 | }; |
765 | 766 | ||
766 | counter += retval; |
767 | counter += retval; |
767 | j = nxt; |
768 | j = nxt; |
768 | goto next_char; |
769 | goto next_char; |
769 | 770 | ||
770 | /* |
771 | /* |
771 | * Integer values |
772 | * Integer values |
772 | */ |
773 | */ |
773 | case 'P': |
774 | case 'P': |
774 | /* Pointer */ |
775 | /* Pointer */ |
775 | flags |= __PRINTF_FLAG_BIGCHARS; |
776 | flags |= __PRINTF_FLAG_BIGCHARS; |
776 | case 'p': |
777 | case 'p': |
777 | flags |= __PRINTF_FLAG_PREFIX; |
778 | flags |= __PRINTF_FLAG_PREFIX; |
778 | base = 16; |
779 | base = 16; |
779 | qualifier = PrintfQualifierPointer; |
780 | qualifier = PrintfQualifierPointer; |
780 | break; |
781 | break; |
781 | case 'b': |
782 | case 'b': |
782 | base = 2; |
783 | base = 2; |
783 | break; |
784 | break; |
784 | case 'o': |
785 | case 'o': |
785 | base = 8; |
786 | base = 8; |
786 | break; |
787 | break; |
787 | case 'd': |
788 | case 'd': |
788 | case 'i': |
789 | case 'i': |
789 | flags |= __PRINTF_FLAG_SIGNED; |
790 | flags |= __PRINTF_FLAG_SIGNED; |
790 | case 'u': |
791 | case 'u': |
791 | break; |
792 | break; |
792 | case 'X': |
793 | case 'X': |
793 | flags |= __PRINTF_FLAG_BIGCHARS; |
794 | flags |= __PRINTF_FLAG_BIGCHARS; |
794 | case 'x': |
795 | case 'x': |
795 | base = 16; |
796 | base = 16; |
796 | break; |
797 | break; |
797 | 798 | ||
798 | /* Percentile itself */ |
799 | /* Percentile itself */ |
799 | case '%': |
800 | case '%': |
800 | j = i; |
801 | j = i; |
801 | goto next_char; |
802 | goto next_char; |
802 | 803 | ||
803 | /* |
804 | /* |
804 | * Bad formatting. |
805 | * Bad formatting. |
805 | */ |
806 | */ |
806 | default: |
807 | default: |
807 | /* |
808 | /* |
808 | * Unknown format. Now, j is the index of '%' |
809 | * Unknown format. Now, j is the index of '%' |
809 | * so we will print whole bad format sequence. |
810 | * so we will print whole bad format sequence. |
810 | */ |
811 | */ |
811 | goto next_char; |
812 | goto next_char; |
812 | } |
813 | } |
813 | 814 | ||
814 | /* Print integers */ |
815 | /* Print integers */ |
815 | size_t size; |
816 | size_t size; |
816 | uint64_t number; |
817 | uint64_t number; |
817 | switch (qualifier) { |
818 | switch (qualifier) { |
818 | case PrintfQualifierByte: |
819 | case PrintfQualifierByte: |
819 | size = sizeof(unsigned char); |
820 | size = sizeof(unsigned char); |
820 | number = (uint64_t) va_arg(ap, unsigned int); |
821 | number = (uint64_t) va_arg(ap, unsigned int); |
821 | break; |
822 | break; |
822 | case PrintfQualifierShort: |
823 | case PrintfQualifierShort: |
823 | size = sizeof(unsigned short); |
824 | size = sizeof(unsigned short); |
824 | number = (uint64_t) va_arg(ap, unsigned int); |
825 | number = (uint64_t) va_arg(ap, unsigned int); |
825 | break; |
826 | break; |
826 | case PrintfQualifierInt: |
827 | case PrintfQualifierInt: |
827 | size = sizeof(unsigned int); |
828 | size = sizeof(unsigned int); |
828 | number = (uint64_t) va_arg(ap, unsigned int); |
829 | number = (uint64_t) va_arg(ap, unsigned int); |
829 | break; |
830 | break; |
830 | case PrintfQualifierLong: |
831 | case PrintfQualifierLong: |
831 | size = sizeof(unsigned long); |
832 | size = sizeof(unsigned long); |
832 | number = (uint64_t) va_arg(ap, unsigned long); |
833 | number = (uint64_t) va_arg(ap, unsigned long); |
833 | break; |
834 | break; |
834 | case PrintfQualifierLongLong: |
835 | case PrintfQualifierLongLong: |
835 | size = sizeof(unsigned long long); |
836 | size = sizeof(unsigned long long); |
836 | number = (uint64_t) va_arg(ap, unsigned long long); |
837 | number = (uint64_t) va_arg(ap, unsigned long long); |
837 | break; |
838 | break; |
838 | case PrintfQualifierPointer: |
839 | case PrintfQualifierPointer: |
839 | size = sizeof(void *); |
840 | size = sizeof(void *); |
840 | number = (uint64_t) (unsigned long) va_arg(ap, void *); |
841 | number = (uint64_t) (unsigned long) va_arg(ap, void *); |
841 | break; |
842 | break; |
842 | default: |
843 | default: |
843 | /* Unknown qualifier */ |
844 | /* Unknown qualifier */ |
844 | counter = -counter; |
845 | counter = -counter; |
845 | goto out; |
846 | goto out; |
846 | } |
847 | } |
847 | 848 | ||
848 | if (flags & __PRINTF_FLAG_SIGNED) { |
849 | if (flags & __PRINTF_FLAG_SIGNED) { |
849 | if (number & (0x1 << (size * 8 - 1))) { |
850 | if (number & (0x1 << (size * 8 - 1))) { |
850 | flags |= __PRINTF_FLAG_NEGATIVE; |
851 | flags |= __PRINTF_FLAG_NEGATIVE; |
851 | 852 | ||
852 | if (size == sizeof(uint64_t)) { |
853 | if (size == sizeof(uint64_t)) { |
853 | number = -((int64_t) number); |
854 | number = -((int64_t) number); |
854 | } else { |
855 | } else { |
855 | number = ~number; |
856 | number = ~number; |
856 | number &= |
857 | number &= |
857 | ~(0xFFFFFFFFFFFFFFFFll << |
858 | ~(0xFFFFFFFFFFFFFFFFll << |
858 | (size * 8)); |
859 | (size * 8)); |
859 | number++; |
860 | number++; |
860 | } |
861 | } |
861 | } |
862 | } |
862 | } |
863 | } |
863 | 864 | ||
864 | if ((retval = print_number(number, width, precision, |
865 | if ((retval = print_number(number, width, precision, |
865 | base, flags, ps)) < 0) { |
866 | base, flags, ps)) < 0) { |
866 | counter = -counter; |
867 | counter = -counter; |
867 | goto out; |
868 | goto out; |
868 | } |
869 | } |
869 | 870 | ||
870 | counter += retval; |
871 | counter += retval; |
871 | j = nxt; |
872 | j = nxt; |
872 | } |
873 | } |
873 | next_char: |
874 | next_char: |
874 | ; |
875 | ; |
875 | } |
876 | } |
876 | 877 | ||
877 | if (i > j) { |
878 | if (i > j) { |
878 | if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) { |
879 | if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) { |
879 | /* Error */ |
880 | /* Error */ |
880 | counter = -counter; |
881 | counter = -counter; |
881 | goto out; |
882 | goto out; |
882 | } |
883 | } |
883 | counter += retval; |
884 | counter += retval; |
884 | } |
885 | } |
885 | 886 | ||
886 | out: |
887 | out: |
887 | return ((int) counter); |
888 | return ((int) counter); |
888 | } |
889 | } |
889 | 890 | ||
890 | /** @} |
891 | /** @} |
891 | */ |
892 | */ |
892 | 893 |