Rev 4221 | Rev 4537 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4221 | Rev 4296 | ||
---|---|---|---|
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 UTF-8 characters without adding newline. |
86 | /** Print one or more characters without adding newline. |
86 | * |
87 | * |
87 | * @param buf Buffer holding UTF-8 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 UTF-8 characters printed. |
93 | * @return Number of characters printed. |
93 | * |
94 | * |
94 | */ |
95 | */ |
95 | static int printf_putnchars_utf8(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->write_utf8((void *) buf, size, ps->data); |
99 | return ps->str_write((void *) buf, size, ps->data); |
99 | } |
100 | } |
100 | 101 | ||
101 | /** Print one or more UTF-32 characters without adding newline. |
102 | /** Print one or more wide characters without adding newline. |
102 | * |
103 | * |
103 | * @param buf Buffer holding UTF-32 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 UTF-32 characters printed. |
109 | * @return Number of wide characters printed. |
109 | * |
110 | * |
110 | */ |
111 | */ |
111 | static int printf_putnchars_utf32(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->write_utf32((void *) buf, size, ps->data); |
115 | return ps->wstr_write((void *) buf, size, ps->data); |
115 | } |
116 | } |
116 | 117 | ||
117 | /** Print UTF-8 string without adding a newline. |
118 | /** Print string without adding a newline. |
118 | * |
119 | * |
119 | * @param str UTF-8 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 UTF-8 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_utf8(nullstr, strlen(nullstr), ps); |
129 | return printf_putnchars(nullstr, str_size(nullstr), ps); |
129 | 130 | ||
130 | return ps->write_utf8((void *) str, strlen(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->write_utf8((void *) &invalch, 1, ps->data); |
145 | return ps->str_write((void *) &invalch, 1, ps->data); |
145 | 146 | ||
146 | return ps->write_utf8(&ch, 1, ps->data); |
147 | return ps->str_write(&ch, 1, ps->data); |
147 | } |
148 | } |
148 | 149 | ||
149 | /** Print one UTF-32 character. |
150 | /** Print one wide character. |
150 | * |
151 | * |
151 | * @param c UTF-32 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 (!unicode_check(ch)) |
160 | if (!chr_check(ch)) |
160 | return ps->write_utf8((void *) &invalch, 1, ps->data); |
161 | return ps->str_write((void *) &invalch, 1, ps->data); |
161 | 162 | ||
162 | return ps->write_utf32(&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 UTF-32 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 UTF-8 string. |
242 | /** Print string. |
242 | * |
243 | * |
243 | * @param str UTF-8 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 UTF-8 characters printed, negative value on failure. |
249 | * @return Number of characters printed, negative value on failure. |
249 | */ |
250 | */ |
250 | static int print_utf8(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 | size_t size = str_length(str); |
258 | count_t strw = str_length(str); |
258 | if (precision == 0) |
259 | if (precision == 0) |
259 | precision = size; |
260 | precision = strw; |
260 | 261 | ||
- | 262 | /* Left padding */ |
|
261 | count_t counter = 0; |
263 | count_t counter = 0; |
262 | width -= precision; |
264 | width -= precision; |
263 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
265 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
264 | while (width-- > 0) { |
266 | while (width-- > 0) { |
265 | if (printf_putchar(' ', ps) == 1) |
267 | if (printf_putchar(' ', ps) == 1) |
266 | counter++; |
268 | counter++; |
267 | } |
269 | } |
268 | } |
270 | } |
269 | 271 | ||
- | 272 | /* Part of @a str fitting into the alloted space. */ |
|
270 | int retval; |
273 | int retval; |
271 | size_t bytes = str_lsize(str, min(size, precision)); |
274 | size_t size = str_lsize(str, precision); |
272 | if ((retval = printf_putnchars_utf8(str, bytes, ps)) < 0) |
275 | if ((retval = printf_putnchars(str, size, ps)) < 0) |
273 | return -counter; |
276 | return -counter; |
274 | 277 | ||
275 | counter += retval; |
278 | counter += retval; |
276 | 279 | ||
- | 280 | /* Right padding */ |
|
277 | while (width-- > 0) { |
281 | while (width-- > 0) { |
278 | if (printf_putchar(' ', ps) == 1) |
282 | if (printf_putchar(' ', ps) == 1) |
279 | counter++; |
283 | counter++; |
280 | } |
284 | } |
281 | 285 | ||
282 | return ((int) counter); |
286 | return ((int) counter); |
- | 287 | ||
283 | } |
288 | } |
284 | 289 | ||
285 | /** Print UTF-32 string. |
290 | /** Print wide string. |
286 | * |
291 | * |
287 | * @param str UTF-32 string to be printed. |
292 | * @param str Wide string to be printed. |
288 | * @param width Width modifier. |
293 | * @param width Width modifier. |
289 | * @param precision Precision modifier. |
294 | * @param precision Precision modifier. |
290 | * @param flags Flags that modify the way the string is printed. |
295 | * @param flags Flags that modify the way the string is printed. |
291 | * |
296 | * |
292 | * @return Number of UTF-32 characters printed, negative value on failure. |
297 | * @return Number of wide characters printed, negative value on failure. |
293 | */ |
298 | */ |
294 | static int print_utf32(wchar_t *str, int width, unsigned int precision, |
299 | static int print_wstr(wchar_t *str, int width, unsigned int precision, |
295 | uint32_t flags, printf_spec_t *ps) |
300 | uint32_t flags, printf_spec_t *ps) |
296 | { |
301 | { |
297 | if (str == NULL) |
302 | if (str == NULL) |
298 | return printf_putstr(nullstr, ps); |
303 | return printf_putstr(nullstr, ps); |
299 | 304 | ||
- | 305 | if (*str == U_BOM) |
|
- | 306 | str++; |
|
- | 307 | ||
300 | /* Print leading spaces */ |
308 | /* Print leading spaces. */ |
301 | size_t size = wstr_length(str); |
309 | size_t strw = wstr_length(str); |
302 | if (precision == 0) |
310 | if (precision == 0) |
303 | precision = size; |
311 | precision = strw; |
304 | 312 | ||
- | 313 | /* Left padding */ |
|
305 | count_t counter = 0; |
314 | count_t counter = 0; |
306 | width -= precision; |
315 | width -= precision; |
307 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
316 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
308 | while (width-- > 0) { |
317 | while (width-- > 0) { |
309 | if (printf_putchar(' ', ps) == 1) |
318 | if (printf_putchar(' ', ps) == 1) |
310 | counter++; |
319 | counter++; |
311 | } |
320 | } |
312 | } |
321 | } |
313 | 322 | ||
- | 323 | /* Part of @a wstr fitting into the alloted space. */ |
|
314 | int retval; |
324 | int retval; |
315 | size_t bytes = min(size, precision) * sizeof(wchar_t); |
325 | size_t size = wstr_lsize(str, precision); |
316 | if ((retval = printf_putnchars_utf32(str, bytes, ps)) < 0) |
326 | if ((retval = printf_wputnchars(str, size, ps)) < 0) |
317 | return -counter; |
327 | return -counter; |
318 | 328 | ||
319 | counter += retval; |
329 | counter += retval; |
320 | 330 | ||
- | 331 | /* Right padding */ |
|
321 | while (width-- > 0) { |
332 | while (width-- > 0) { |
322 | if (printf_putchar(' ', ps) == 1) |
333 | if (printf_putchar(' ', ps) == 1) |
323 | counter++; |
334 | counter++; |
324 | } |
335 | } |
325 | 336 | ||
326 | return ((int) counter); |
337 | return ((int) counter); |
327 | } |
338 | } |
328 | 339 | ||
329 | /** Print a number in a given base. |
340 | /** Print a number in a given base. |
330 | * |
341 | * |
331 | * Print significant digits of a number in given base. |
342 | * Print significant digits of a number in given base. |
332 | * |
343 | * |
333 | * @param num Number to print. |
344 | * @param num Number to print. |
334 | * @param width Width modifier. |
345 | * @param width Width modifier. |
335 | * @param precision Precision modifier. |
346 | * @param precision Precision modifier. |
336 | * @param base Base to print the number in (must be between 2 and 16). |
347 | * @param base Base to print the number in (must be between 2 and 16). |
337 | * @param flags Flags that modify the way the number is printed. |
348 | * @param flags Flags that modify the way the number is printed. |
338 | * |
349 | * |
339 | * @return Number of characters printed. |
350 | * @return Number of characters printed. |
340 | * |
351 | * |
341 | */ |
352 | */ |
342 | static int print_number(uint64_t num, int width, int precision, int base, |
353 | static int print_number(uint64_t num, int width, int precision, int base, |
343 | uint32_t flags, printf_spec_t *ps) |
354 | uint32_t flags, printf_spec_t *ps) |
344 | { |
355 | { |
345 | char *digits; |
356 | char *digits; |
346 | if (flags & __PRINTF_FLAG_BIGCHARS) |
357 | if (flags & __PRINTF_FLAG_BIGCHARS) |
347 | digits = digits_big; |
358 | digits = digits_big; |
348 | else |
359 | else |
349 | digits = digits_small; |
360 | digits = digits_small; |
350 | 361 | ||
351 | char data[PRINT_NUMBER_BUFFER_SIZE]; |
362 | char data[PRINT_NUMBER_BUFFER_SIZE]; |
352 | char *ptr = &data[PRINT_NUMBER_BUFFER_SIZE - 1]; |
363 | char *ptr = &data[PRINT_NUMBER_BUFFER_SIZE - 1]; |
353 | 364 | ||
354 | /* Size of number with all prefixes and signs */ |
365 | /* Size of number with all prefixes and signs */ |
355 | int size = 0; |
366 | int size = 0; |
356 | 367 | ||
357 | /* Put zero at end of string */ |
368 | /* Put zero at end of string */ |
358 | *ptr-- = 0; |
369 | *ptr-- = 0; |
359 | 370 | ||
360 | if (num == 0) { |
371 | if (num == 0) { |
361 | *ptr-- = '0'; |
372 | *ptr-- = '0'; |
362 | size++; |
373 | size++; |
363 | } else { |
374 | } else { |
364 | do { |
375 | do { |
365 | *ptr-- = digits[num % base]; |
376 | *ptr-- = digits[num % base]; |
366 | size++; |
377 | size++; |
367 | } while (num /= base); |
378 | } while (num /= base); |
368 | } |
379 | } |
369 | 380 | ||
370 | /* Size of plain number */ |
381 | /* Size of plain number */ |
371 | int number_size = size; |
382 | int number_size = size; |
372 | 383 | ||
373 | /* |
384 | /* |
374 | * Collect the sum of all prefixes/signs/etc. to calculate padding and |
385 | * Collect the sum of all prefixes/signs/etc. to calculate padding and |
375 | * leading zeroes. |
386 | * leading zeroes. |
376 | */ |
387 | */ |
377 | if (flags & __PRINTF_FLAG_PREFIX) { |
388 | if (flags & __PRINTF_FLAG_PREFIX) { |
378 | switch(base) { |
389 | switch(base) { |
379 | case 2: |
390 | case 2: |
380 | /* Binary formating is not standard, but usefull */ |
391 | /* Binary formating is not standard, but usefull */ |
381 | size += 2; |
392 | size += 2; |
382 | break; |
393 | break; |
383 | case 8: |
394 | case 8: |
384 | size++; |
395 | size++; |
385 | break; |
396 | break; |
386 | case 16: |
397 | case 16: |
387 | size += 2; |
398 | size += 2; |
388 | break; |
399 | break; |
389 | } |
400 | } |
390 | } |
401 | } |
391 | 402 | ||
392 | char sgn = 0; |
403 | char sgn = 0; |
393 | if (flags & __PRINTF_FLAG_SIGNED) { |
404 | if (flags & __PRINTF_FLAG_SIGNED) { |
394 | if (flags & __PRINTF_FLAG_NEGATIVE) { |
405 | if (flags & __PRINTF_FLAG_NEGATIVE) { |
395 | sgn = '-'; |
406 | sgn = '-'; |
396 | size++; |
407 | size++; |
397 | } else if (flags & __PRINTF_FLAG_SHOWPLUS) { |
408 | } else if (flags & __PRINTF_FLAG_SHOWPLUS) { |
398 | sgn = '+'; |
409 | sgn = '+'; |
399 | size++; |
410 | size++; |
400 | } else if (flags & __PRINTF_FLAG_SPACESIGN) { |
411 | } else if (flags & __PRINTF_FLAG_SPACESIGN) { |
401 | sgn = ' '; |
412 | sgn = ' '; |
402 | size++; |
413 | size++; |
403 | } |
414 | } |
404 | } |
415 | } |
405 | 416 | ||
406 | if (flags & __PRINTF_FLAG_LEFTALIGNED) |
417 | if (flags & __PRINTF_FLAG_LEFTALIGNED) |
407 | flags &= ~__PRINTF_FLAG_ZEROPADDED; |
418 | flags &= ~__PRINTF_FLAG_ZEROPADDED; |
408 | 419 | ||
409 | /* |
420 | /* |
410 | * If the number is left-aligned or precision is specified then |
421 | * If the number is left-aligned or precision is specified then |
411 | * padding with zeros is ignored. |
422 | * padding with zeros is ignored. |
412 | */ |
423 | */ |
413 | if (flags & __PRINTF_FLAG_ZEROPADDED) { |
424 | if (flags & __PRINTF_FLAG_ZEROPADDED) { |
414 | if ((precision == 0) && (width > size)) |
425 | if ((precision == 0) && (width > size)) |
415 | precision = width - size + number_size; |
426 | precision = width - size + number_size; |
416 | } |
427 | } |
417 | 428 | ||
418 | /* Print leading spaces */ |
429 | /* Print leading spaces */ |
419 | if (number_size > precision) { |
430 | if (number_size > precision) { |
420 | /* Print the whole number, not only a part */ |
431 | /* Print the whole number, not only a part */ |
421 | precision = number_size; |
432 | precision = number_size; |
422 | } |
433 | } |
423 | 434 | ||
424 | width -= precision + size - number_size; |
435 | width -= precision + size - number_size; |
425 | count_t counter = 0; |
436 | count_t counter = 0; |
426 | 437 | ||
427 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
438 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
428 | while (width-- > 0) { |
439 | while (width-- > 0) { |
429 | if (printf_putchar(' ', ps) == 1) |
440 | if (printf_putchar(' ', ps) == 1) |
430 | counter++; |
441 | counter++; |
431 | } |
442 | } |
432 | } |
443 | } |
433 | 444 | ||
434 | /* Print sign */ |
445 | /* Print sign */ |
435 | if (sgn) { |
446 | if (sgn) { |
436 | if (printf_putchar(sgn, ps) == 1) |
447 | if (printf_putchar(sgn, ps) == 1) |
437 | counter++; |
448 | counter++; |
438 | } |
449 | } |
439 | 450 | ||
440 | /* Print prefix */ |
451 | /* Print prefix */ |
441 | if (flags & __PRINTF_FLAG_PREFIX) { |
452 | if (flags & __PRINTF_FLAG_PREFIX) { |
442 | switch(base) { |
453 | switch(base) { |
443 | case 2: |
454 | case 2: |
444 | /* Binary formating is not standard, but usefull */ |
455 | /* Binary formating is not standard, but usefull */ |
445 | if (printf_putchar('0', ps) == 1) |
456 | if (printf_putchar('0', ps) == 1) |
446 | counter++; |
457 | counter++; |
447 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
458 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
448 | if (printf_putchar('B', ps) == 1) |
459 | if (printf_putchar('B', ps) == 1) |
449 | counter++; |
460 | counter++; |
450 | } else { |
461 | } else { |
451 | if (printf_putchar('b', ps) == 1) |
462 | if (printf_putchar('b', ps) == 1) |
452 | counter++; |
463 | counter++; |
453 | } |
464 | } |
454 | break; |
465 | break; |
455 | case 8: |
466 | case 8: |
456 | if (printf_putchar('o', ps) == 1) |
467 | if (printf_putchar('o', ps) == 1) |
457 | counter++; |
468 | counter++; |
458 | break; |
469 | break; |
459 | case 16: |
470 | case 16: |
460 | if (printf_putchar('0', ps) == 1) |
471 | if (printf_putchar('0', ps) == 1) |
461 | counter++; |
472 | counter++; |
462 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
473 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
463 | if (printf_putchar('X', ps) == 1) |
474 | if (printf_putchar('X', ps) == 1) |
464 | counter++; |
475 | counter++; |
465 | } else { |
476 | } else { |
466 | if (printf_putchar('x', ps) == 1) |
477 | if (printf_putchar('x', ps) == 1) |
467 | counter++; |
478 | counter++; |
468 | } |
479 | } |
469 | break; |
480 | break; |
470 | } |
481 | } |
471 | } |
482 | } |
472 | 483 | ||
473 | /* Print leading zeroes */ |
484 | /* Print leading zeroes */ |
474 | precision -= number_size; |
485 | precision -= number_size; |
475 | while (precision-- > 0) { |
486 | while (precision-- > 0) { |
476 | if (printf_putchar('0', ps) == 1) |
487 | if (printf_putchar('0', ps) == 1) |
477 | counter++; |
488 | counter++; |
478 | } |
489 | } |
479 | 490 | ||
480 | /* Print the number itself */ |
491 | /* Print the number itself */ |
481 | int retval; |
492 | int retval; |
482 | if ((retval = printf_putstr(++ptr, ps)) > 0) |
493 | if ((retval = printf_putstr(++ptr, ps)) > 0) |
483 | counter += retval; |
494 | counter += retval; |
484 | 495 | ||
485 | /* Print tailing spaces */ |
496 | /* Print tailing spaces */ |
486 | 497 | ||
487 | while (width-- > 0) { |
498 | while (width-- > 0) { |
488 | if (printf_putchar(' ', ps) == 1) |
499 | if (printf_putchar(' ', ps) == 1) |
489 | counter++; |
500 | counter++; |
490 | } |
501 | } |
491 | 502 | ||
492 | return ((int) counter); |
503 | return ((int) counter); |
493 | } |
504 | } |
494 | 505 | ||
495 | /** Print formatted string. |
506 | /** Print formatted string. |
496 | * |
507 | * |
497 | * Print string formatted according to the fmt parameter and variadic arguments. |
508 | * Print string formatted according to the fmt parameter and variadic arguments. |
498 | * Each formatting directive must have the following form: |
509 | * Each formatting directive must have the following form: |
499 | * |
510 | * |
500 | * \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION |
511 | * \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION |
501 | * |
512 | * |
502 | * FLAGS:@n |
513 | * FLAGS:@n |
503 | * - "#" Force to print prefix. For \%o conversion, the prefix is 0, for |
514 | * - "#" Force to print prefix. For \%o conversion, the prefix is 0, for |
504 | * \%x and \%X prefixes are 0x and 0X and for conversion \%b the |
515 | * \%x and \%X prefixes are 0x and 0X and for conversion \%b the |
505 | * prefix is 0b. |
516 | * prefix is 0b. |
506 | * |
517 | * |
507 | * - "-" Align to left. |
518 | * - "-" Align to left. |
508 | * |
519 | * |
509 | * - "+" Print positive sign just as negative. |
520 | * - "+" Print positive sign just as negative. |
510 | * |
521 | * |
511 | * - " " If the printed number is positive and "+" flag is not set, |
522 | * - " " If the printed number is positive and "+" flag is not set, |
512 | * print space in place of sign. |
523 | * print space in place of sign. |
513 | * |
524 | * |
514 | * - "0" Print 0 as padding instead of spaces. Zeroes are placed between |
525 | * - "0" Print 0 as padding instead of spaces. Zeroes are placed between |
515 | * sign and the rest of the number. This flag is ignored if "-" |
526 | * sign and the rest of the number. This flag is ignored if "-" |
516 | * flag is specified. |
527 | * flag is specified. |
517 | * |
528 | * |
518 | * WIDTH:@n |
529 | * WIDTH:@n |
519 | * - Specify the minimal width of a printed argument. If it is bigger, |
530 | * - Specify the minimal width of a printed argument. If it is bigger, |
520 | * width is ignored. If width is specified with a "*" character instead of |
531 | * width is ignored. If width is specified with a "*" character instead of |
521 | * number, width is taken from parameter list. And integer parameter is |
532 | * number, width is taken from parameter list. And integer parameter is |
522 | * expected before parameter for processed conversion specification. If |
533 | * expected before parameter for processed conversion specification. If |
523 | * this value is negative its absolute value is taken and the "-" flag is |
534 | * this value is negative its absolute value is taken and the "-" flag is |
524 | * set. |
535 | * set. |
525 | * |
536 | * |
526 | * PRECISION:@n |
537 | * PRECISION:@n |
527 | * - Value precision. For numbers it specifies minimum valid numbers. |
538 | * - Value precision. For numbers it specifies minimum valid numbers. |
528 | * Smaller numbers are printed with leading zeroes. Bigger numbers are not |
539 | * Smaller numbers are printed with leading zeroes. Bigger numbers are not |
529 | * affected. Strings with more than precision characters are cut off. Just |
540 | * affected. Strings with more than precision characters are cut off. Just |
530 | * as with width, an "*" can be used used instead of a number. An integer |
541 | * as with width, an "*" can be used used instead of a number. An integer |
531 | * value is then expected in parameters. When both width and precision are |
542 | * value is then expected in parameters. When both width and precision are |
532 | * specified using "*", the first parameter is used for width and the |
543 | * specified using "*", the first parameter is used for width and the |
533 | * second one for precision. |
544 | * second one for precision. |
534 | * |
545 | * |
535 | * TYPE:@n |
546 | * TYPE:@n |
536 | * - "hh" Signed or unsigned char.@n |
547 | * - "hh" Signed or unsigned char.@n |
537 | * - "h" Signed or unsigned short.@n |
548 | * - "h" Signed or unsigned short.@n |
538 | * - "" Signed or unsigned int (default value).@n |
549 | * - "" Signed or unsigned int (default value).@n |
539 | * - "l" Signed or unsigned long int.@n |
550 | * - "l" Signed or unsigned long int.@n |
540 | * If conversion is "c", the character is wchar_t (UTF-32).@n |
551 | * If conversion is "c", the character is wchar_t (wide character).@n |
541 | * If conversion is "s", the string is wchar_t * (UTF-32).@n |
552 | * If conversion is "s", the string is wchar_t * (wide string).@n |
542 | * - "ll" Signed or unsigned long long int.@n |
553 | * - "ll" Signed or unsigned long long int.@n |
543 | * |
554 | * |
544 | * CONVERSION:@n |
555 | * CONVERSION:@n |
545 | * - % Print percentile character itself. |
556 | * - % Print percentile character itself. |
546 | * |
557 | * |
547 | * - c Print single character. The character is expected to be plain |
558 | * - c Print single character. The character is expected to be plain |
548 | * ASCII (e.g. only values 0 .. 127 are valid).@n |
559 | * ASCII (e.g. only values 0 .. 127 are valid).@n |
549 | * If type is "l", then the character is expected to be UTF-32 |
560 | * If type is "l", then the character is expected to be wide character |
550 | * (e.g. values 0 .. 0x10ffff are valid). |
561 | * (e.g. values 0 .. 0x10ffff are valid). |
551 | * |
562 | * |
552 | * - s Print zero terminated string. If a NULL value is passed as |
563 | * - s Print zero terminated string. If a NULL value is passed as |
553 | * value, "(NULL)" is printed instead.@n |
564 | * value, "(NULL)" is printed instead.@n |
554 | * The string is expected to be correctly encoded UTF-8 (or plain |
- | |
555 | * ASCII, which is a subset of UTF-8).@n |
- | |
556 | * If type is "l", then the string is expected to be correctly |
565 | * If type is "l", then the string is expected to be wide string. |
557 | * encoded UTF-32. |
- | |
558 | * |
566 | * |
559 | * - P, p Print value of a pointer. Void * value is expected and it is |
567 | * - P, p Print value of a pointer. Void * value is expected and it is |
560 | * printed in hexadecimal notation with prefix (as with \%#X / \%#x |
568 | * printed in hexadecimal notation with prefix (as with \%#X / \%#x |
561 | * for 32-bit or \%#X / \%#x for 64-bit long pointers). |
569 | * for 32-bit or \%#X / \%#x for 64-bit long pointers). |
562 | * |
570 | * |
563 | * - b Print value as unsigned binary number. Prefix is not printed by |
571 | * - b Print value as unsigned binary number. Prefix is not printed by |
564 | * default. (Nonstandard extension.) |
572 | * default. (Nonstandard extension.) |
565 | * |
573 | * |
566 | * - o Print value as unsigned octal number. Prefix is not printed by |
574 | * - o Print value as unsigned octal number. Prefix is not printed by |
567 | * default. |
575 | * default. |
568 | * |
576 | * |
569 | * - d, i Print signed decimal number. There is no difference between d |
577 | * - d, i Print signed decimal number. There is no difference between d |
570 | * and i conversion. |
578 | * and i conversion. |
571 | * |
579 | * |
572 | * - u Print unsigned decimal number. |
580 | * - u Print unsigned decimal number. |
573 | * |
581 | * |
574 | * - X, x Print hexadecimal number with upper- or lower-case. Prefix is |
582 | * - X, x Print hexadecimal number with upper- or lower-case. Prefix is |
575 | * not printed by default. |
583 | * not printed by default. |
576 | * |
584 | * |
577 | * All other characters from fmt except the formatting directives are printed in |
585 | * All other characters from fmt except the formatting directives are printed |
578 | * verbatim. |
586 | * verbatim. |
579 | * |
587 | * |
580 | * @param fmt Formatting NULL terminated string (UTF-8 or plain ASCII). |
588 | * @param fmt Format NULL-terminated string. |
581 | * |
589 | * |
582 | * @return Number of UTF-8 characters printed, negative value on failure. |
590 | * @return Number of characters printed, negative value on failure. |
583 | * |
591 | * |
584 | */ |
592 | */ |
585 | int printf_core(const char *fmt, printf_spec_t *ps, va_list ap) |
593 | int printf_core(const char *fmt, printf_spec_t *ps, va_list ap) |
586 | { |
594 | { |
587 | index_t i = 0; /* Index of the currently processed character from fmt */ |
595 | size_t i; /* Index of the currently processed character from fmt */ |
588 | index_t nxt = 0; |
596 | size_t nxt = 0; /* Index of the next character from fmt */ |
589 | index_t j = 0; /* Index to the first not printed nonformating character */ |
597 | size_t j = 0; /* Index to the first not printed nonformating character */ |
590 | 598 | ||
591 | wchar_t uc; /* Current UTF-32 character decoded from fmt */ |
- | |
592 | count_t counter = 0; /* Number of UTF-8 characters printed */ |
599 | count_t counter = 0; /* Number of characters printed */ |
593 | int retval; /* Return values from nested functions */ |
600 | int retval; /* Return values from nested functions */ |
594 | 601 | ||
595 | while (true) { |
602 | while (true) { |
596 | i = nxt; |
603 | i = nxt; |
597 | uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
604 | wchar_t uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
598 | 605 | ||
599 | if (uc == '\0') break; |
606 | if (uc == 0) |
- | 607 | break; |
|
600 | 608 | ||
601 | /* Control character */ |
609 | /* Control character */ |
602 | if (uc == '%') { |
610 | if (uc == '%') { |
603 | /* Print common characters if any processed */ |
611 | /* Print common characters if any processed */ |
604 | if (i > j) { |
612 | if (i > j) { |
605 | if ((retval = printf_putnchars_utf8(&fmt[j], i - j, ps)) < 0) { |
613 | if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) { |
606 | /* Error */ |
614 | /* Error */ |
607 | counter = -counter; |
615 | counter = -counter; |
608 | goto out; |
616 | goto out; |
609 | } |
617 | } |
610 | counter += retval; |
618 | counter += retval; |
611 | } |
619 | } |
612 | 620 | ||
613 | j = i; |
621 | j = i; |
614 | 622 | ||
615 | /* Parse modifiers */ |
623 | /* Parse modifiers */ |
616 | uint32_t flags = 0; |
624 | uint32_t flags = 0; |
617 | bool end = false; |
625 | bool end = false; |
618 | 626 | ||
619 | do { |
627 | do { |
620 | i = nxt; |
628 | i = nxt; |
621 | uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
629 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
622 | switch (uc) { |
630 | switch (uc) { |
623 | case '#': |
631 | case '#': |
624 | flags |= __PRINTF_FLAG_PREFIX; |
632 | flags |= __PRINTF_FLAG_PREFIX; |
625 | break; |
633 | break; |
626 | case '-': |
634 | case '-': |
627 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
635 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
628 | break; |
636 | break; |
629 | case '+': |
637 | case '+': |
630 | flags |= __PRINTF_FLAG_SHOWPLUS; |
638 | flags |= __PRINTF_FLAG_SHOWPLUS; |
631 | break; |
639 | break; |
632 | case ' ': |
640 | case ' ': |
633 | flags |= __PRINTF_FLAG_SPACESIGN; |
641 | flags |= __PRINTF_FLAG_SPACESIGN; |
634 | break; |
642 | break; |
635 | case '0': |
643 | case '0': |
636 | flags |= __PRINTF_FLAG_ZEROPADDED; |
644 | flags |= __PRINTF_FLAG_ZEROPADDED; |
637 | break; |
645 | break; |
638 | default: |
646 | default: |
639 | end = true; |
647 | end = true; |
640 | }; |
648 | }; |
641 | } while (!end); |
649 | } while (!end); |
642 | 650 | ||
643 | /* Width & '*' operator */ |
651 | /* Width & '*' operator */ |
644 | int width = 0; |
652 | int width = 0; |
645 | if (isdigit(uc)) { |
653 | if (isdigit(uc)) { |
646 | while (true) { |
654 | while (true) { |
647 | width *= 10; |
655 | width *= 10; |
648 | width += uc - '0'; |
656 | width += uc - '0'; |
649 | 657 | ||
650 | i = nxt; |
658 | i = nxt; |
651 | uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
659 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
652 | if (uc == '\0') |
660 | if (uc == 0) |
653 | break; |
661 | break; |
654 | if (!isdigit(uc)) |
662 | if (!isdigit(uc)) |
655 | break; |
663 | break; |
656 | } |
664 | } |
657 | } else if (uc == '*') { |
665 | } else if (uc == '*') { |
658 | /* Get width value from argument list */ |
666 | /* Get width value from argument list */ |
659 | i = nxt; |
667 | i = nxt; |
660 | uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
668 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
661 | width = (int) va_arg(ap, int); |
669 | width = (int) va_arg(ap, int); |
662 | if (width < 0) { |
670 | if (width < 0) { |
663 | /* Negative width sets '-' flag */ |
671 | /* Negative width sets '-' flag */ |
664 | width *= -1; |
672 | width *= -1; |
665 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
673 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
666 | } |
674 | } |
667 | } |
675 | } |
668 | 676 | ||
669 | /* Precision and '*' operator */ |
677 | /* Precision and '*' operator */ |
670 | int precision = 0; |
678 | int precision = 0; |
671 | if (uc == '.') { |
679 | if (uc == '.') { |
672 | i = nxt; |
680 | i = nxt; |
673 | uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
681 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
674 | if (isdigit(uc)) { |
682 | if (isdigit(uc)) { |
675 | while (true) { |
683 | while (true) { |
676 | precision *= 10; |
684 | precision *= 10; |
677 | precision += uc - '0'; |
685 | precision += uc - '0'; |
678 | 686 | ||
679 | i = nxt; |
687 | i = nxt; |
680 | uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
688 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
681 | if (uc == '\0') |
689 | if (uc == 0) |
682 | break; |
690 | break; |
683 | if (!isdigit(uc)) |
691 | if (!isdigit(uc)) |
684 | break; |
692 | break; |
685 | } |
693 | } |
686 | } else if (uc == '*') { |
694 | } else if (uc == '*') { |
687 | /* Get precision value from the argument list */ |
695 | /* Get precision value from the argument list */ |
688 | i = nxt; |
696 | i = nxt; |
689 | uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
697 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
690 | precision = (int) va_arg(ap, int); |
698 | precision = (int) va_arg(ap, int); |
691 | if (precision < 0) { |
699 | if (precision < 0) { |
692 | /* Ignore negative precision */ |
700 | /* Ignore negative precision */ |
693 | precision = 0; |
701 | precision = 0; |
694 | } |
702 | } |
695 | } |
703 | } |
696 | } |
704 | } |
697 | 705 | ||
698 | qualifier_t qualifier; |
706 | qualifier_t qualifier; |
699 | 707 | ||
700 | switch (uc) { |
708 | switch (uc) { |
701 | /** @todo Unimplemented qualifiers: |
709 | /** @todo Unimplemented qualifiers: |
702 | * t ptrdiff_t - ISO C 99 |
710 | * t ptrdiff_t - ISO C 99 |
703 | */ |
711 | */ |
704 | case 'h': |
712 | case 'h': |
705 | /* Char or short */ |
713 | /* Char or short */ |
706 | qualifier = PrintfQualifierShort; |
714 | qualifier = PrintfQualifierShort; |
707 | i = nxt; |
715 | i = nxt; |
708 | uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
716 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
709 | if (uc == 'h') { |
717 | if (uc == 'h') { |
710 | i = nxt; |
718 | i = nxt; |
711 | uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
719 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
712 | qualifier = PrintfQualifierByte; |
720 | qualifier = PrintfQualifierByte; |
713 | } |
721 | } |
714 | break; |
722 | break; |
715 | case 'l': |
723 | case 'l': |
716 | /* Long or long long */ |
724 | /* Long or long long */ |
717 | qualifier = PrintfQualifierLong; |
725 | qualifier = PrintfQualifierLong; |
718 | i = nxt; |
726 | i = nxt; |
719 | uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
727 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
720 | if (uc == 'l') { |
728 | if (uc == 'l') { |
721 | i = nxt; |
729 | i = nxt; |
722 | uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
730 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
723 | qualifier = PrintfQualifierLongLong; |
731 | qualifier = PrintfQualifierLongLong; |
724 | } |
732 | } |
725 | break; |
733 | break; |
726 | default: |
734 | default: |
727 | /* Default type */ |
735 | /* Default type */ |
728 | qualifier = PrintfQualifierInt; |
736 | qualifier = PrintfQualifierInt; |
729 | } |
737 | } |
730 | 738 | ||
731 | unsigned int base = 10; |
739 | unsigned int base = 10; |
732 | 740 | ||
733 | switch (uc) { |
741 | switch (uc) { |
734 | /* |
742 | /* |
735 | * String and character conversions. |
743 | * String and character conversions. |
736 | */ |
744 | */ |
737 | case 's': |
745 | case 's': |
738 | if (qualifier == PrintfQualifierLong) |
746 | if (qualifier == PrintfQualifierLong) |
739 | retval = print_utf32(va_arg(ap, wchar_t *), width, precision, flags, ps); |
747 | retval = print_wstr(va_arg(ap, wchar_t *), width, precision, flags, ps); |
740 | else |
748 | else |
741 | retval = print_utf8(va_arg(ap, char *), width, precision, flags, ps); |
749 | retval = print_str(va_arg(ap, char *), width, precision, flags, ps); |
742 | 750 | ||
743 | if (retval < 0) { |
751 | if (retval < 0) { |
744 | counter = -counter; |
752 | counter = -counter; |
745 | goto out; |
753 | goto out; |
746 | } |
754 | } |
747 | 755 | ||
748 | counter += retval; |
756 | counter += retval; |
749 | j = nxt; |
757 | j = nxt; |
750 | goto next_char; |
758 | goto next_char; |
751 | case 'c': |
759 | case 'c': |
752 | if (qualifier == PrintfQualifierLong) |
760 | if (qualifier == PrintfQualifierLong) |
753 | retval = print_wchar(va_arg(ap, wchar_t), width, flags, ps); |
761 | retval = print_wchar(va_arg(ap, wchar_t), width, flags, ps); |
754 | else |
762 | else |
755 | retval = print_char(va_arg(ap, unsigned int), width, flags, ps); |
763 | retval = print_char(va_arg(ap, unsigned int), width, flags, ps); |
756 | 764 | ||
757 | if (retval < 0) { |
765 | if (retval < 0) { |
758 | counter = -counter; |
766 | counter = -counter; |
759 | goto out; |
767 | goto out; |
760 | }; |
768 | }; |
761 | 769 | ||
762 | counter += retval; |
770 | counter += retval; |
763 | j = nxt; |
771 | j = nxt; |
764 | goto next_char; |
772 | goto next_char; |
765 | 773 | ||
766 | /* |
774 | /* |
767 | * Integer values |
775 | * Integer values |
768 | */ |
776 | */ |
769 | case 'P': |
777 | case 'P': |
770 | /* Pointer */ |
778 | /* Pointer */ |
771 | flags |= __PRINTF_FLAG_BIGCHARS; |
779 | flags |= __PRINTF_FLAG_BIGCHARS; |
772 | case 'p': |
780 | case 'p': |
773 | flags |= __PRINTF_FLAG_PREFIX; |
781 | flags |= __PRINTF_FLAG_PREFIX; |
774 | base = 16; |
782 | base = 16; |
775 | qualifier = PrintfQualifierPointer; |
783 | qualifier = PrintfQualifierPointer; |
776 | break; |
784 | break; |
777 | case 'b': |
785 | case 'b': |
778 | base = 2; |
786 | base = 2; |
779 | break; |
787 | break; |
780 | case 'o': |
788 | case 'o': |
781 | base = 8; |
789 | base = 8; |
782 | break; |
790 | break; |
783 | case 'd': |
791 | case 'd': |
784 | case 'i': |
792 | case 'i': |
785 | flags |= __PRINTF_FLAG_SIGNED; |
793 | flags |= __PRINTF_FLAG_SIGNED; |
786 | case 'u': |
794 | case 'u': |
787 | break; |
795 | break; |
788 | case 'X': |
796 | case 'X': |
789 | flags |= __PRINTF_FLAG_BIGCHARS; |
797 | flags |= __PRINTF_FLAG_BIGCHARS; |
790 | case 'x': |
798 | case 'x': |
791 | base = 16; |
799 | base = 16; |
792 | break; |
800 | break; |
793 | 801 | ||
794 | /* Percentile itself */ |
802 | /* Percentile itself */ |
795 | case '%': |
803 | case '%': |
796 | j = i; |
804 | j = i; |
797 | goto next_char; |
805 | goto next_char; |
798 | 806 | ||
799 | /* |
807 | /* |
800 | * Bad formatting. |
808 | * Bad formatting. |
801 | */ |
809 | */ |
802 | default: |
810 | default: |
803 | /* |
811 | /* |
804 | * Unknown format. Now, j is the index of '%' |
812 | * Unknown format. Now, j is the index of '%' |
805 | * so we will print whole bad format sequence. |
813 | * so we will print whole bad format sequence. |
806 | */ |
814 | */ |
807 | goto next_char; |
815 | goto next_char; |
808 | } |
816 | } |
809 | 817 | ||
810 | /* Print integers */ |
818 | /* Print integers */ |
811 | size_t size; |
819 | size_t size; |
812 | uint64_t number; |
820 | uint64_t number; |
813 | switch (qualifier) { |
821 | switch (qualifier) { |
814 | case PrintfQualifierByte: |
822 | case PrintfQualifierByte: |
815 | size = sizeof(unsigned char); |
823 | size = sizeof(unsigned char); |
816 | number = (uint64_t) va_arg(ap, unsigned int); |
824 | number = (uint64_t) va_arg(ap, unsigned int); |
817 | break; |
825 | break; |
818 | case PrintfQualifierShort: |
826 | case PrintfQualifierShort: |
819 | size = sizeof(unsigned short); |
827 | size = sizeof(unsigned short); |
820 | number = (uint64_t) va_arg(ap, unsigned int); |
828 | number = (uint64_t) va_arg(ap, unsigned int); |
821 | break; |
829 | break; |
822 | case PrintfQualifierInt: |
830 | case PrintfQualifierInt: |
823 | size = sizeof(unsigned int); |
831 | size = sizeof(unsigned int); |
824 | number = (uint64_t) va_arg(ap, unsigned int); |
832 | number = (uint64_t) va_arg(ap, unsigned int); |
825 | break; |
833 | break; |
826 | case PrintfQualifierLong: |
834 | case PrintfQualifierLong: |
827 | size = sizeof(unsigned long); |
835 | size = sizeof(unsigned long); |
828 | number = (uint64_t) va_arg(ap, unsigned long); |
836 | number = (uint64_t) va_arg(ap, unsigned long); |
829 | break; |
837 | break; |
830 | case PrintfQualifierLongLong: |
838 | case PrintfQualifierLongLong: |
831 | size = sizeof(unsigned long long); |
839 | size = sizeof(unsigned long long); |
832 | number = (uint64_t) va_arg(ap, unsigned long long); |
840 | number = (uint64_t) va_arg(ap, unsigned long long); |
833 | break; |
841 | break; |
834 | case PrintfQualifierPointer: |
842 | case PrintfQualifierPointer: |
835 | size = sizeof(void *); |
843 | size = sizeof(void *); |
836 | number = (uint64_t) (unsigned long) va_arg(ap, void *); |
844 | number = (uint64_t) (unsigned long) va_arg(ap, void *); |
837 | break; |
845 | break; |
838 | default: |
846 | default: |
839 | /* Unknown qualifier */ |
847 | /* Unknown qualifier */ |
840 | counter = -counter; |
848 | counter = -counter; |
841 | goto out; |
849 | goto out; |
842 | } |
850 | } |
843 | 851 | ||
844 | if (flags & __PRINTF_FLAG_SIGNED) { |
852 | if (flags & __PRINTF_FLAG_SIGNED) { |
845 | if (number & (0x1 << (size * 8 - 1))) { |
853 | if (number & (0x1 << (size * 8 - 1))) { |
846 | flags |= __PRINTF_FLAG_NEGATIVE; |
854 | flags |= __PRINTF_FLAG_NEGATIVE; |
847 | 855 | ||
848 | if (size == sizeof(uint64_t)) { |
856 | if (size == sizeof(uint64_t)) { |
849 | number = -((int64_t) number); |
857 | number = -((int64_t) number); |
850 | } else { |
858 | } else { |
851 | number = ~number; |
859 | number = ~number; |
852 | number &= |
860 | number &= |
853 | ~(0xFFFFFFFFFFFFFFFFll << |
861 | ~(0xFFFFFFFFFFFFFFFFll << |
854 | (size * 8)); |
862 | (size * 8)); |
855 | number++; |
863 | number++; |
856 | } |
864 | } |
857 | } |
865 | } |
858 | } |
866 | } |
859 | 867 | ||
860 | if ((retval = print_number(number, width, precision, |
868 | if ((retval = print_number(number, width, precision, |
861 | base, flags, ps)) < 0) { |
869 | base, flags, ps)) < 0) { |
862 | counter = -counter; |
870 | counter = -counter; |
863 | goto out; |
871 | goto out; |
864 | } |
872 | } |
865 | 873 | ||
866 | counter += retval; |
874 | counter += retval; |
867 | j = nxt; |
875 | j = nxt; |
868 | } |
876 | } |
869 | next_char: |
877 | next_char: |
870 | ; |
878 | ; |
871 | } |
879 | } |
872 | 880 | ||
873 | if (i > j) { |
881 | if (i > j) { |
874 | if ((retval = printf_putnchars_utf8(&fmt[j], i - j, ps)) < 0) { |
882 | if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) { |
875 | /* Error */ |
883 | /* Error */ |
876 | counter = -counter; |
884 | counter = -counter; |
877 | goto out; |
885 | goto out; |
878 | } |
886 | } |
879 | counter += retval; |
887 | counter += retval; |
880 | } |
888 | } |
881 | 889 | ||
882 | out: |
890 | out: |
883 | return ((int) counter); |
891 | return ((int) counter); |
884 | } |
892 | } |
885 | 893 | ||
886 | /** @} |
894 | /** @} |
887 | */ |
895 | */ |
888 | 896 |