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