]>
Commit | Line | Data |
---|---|---|
6591c335 | 1 | /* Copyright (C) 1991,92,93,94,95,96,97,98 Free Software Foundation, Inc. |
2c6fe0bd | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
2c6fe0bd UD |
4 | The GNU C Library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public License as | |
6 | published by the Free Software Foundation; either version 2 of the | |
7 | License, or (at your option) any later version. | |
28f540f4 | 8 | |
2c6fe0bd UD |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | Library General Public License for more details. | |
28f540f4 | 13 | |
2c6fe0bd UD |
14 | You should have received a copy of the GNU Library General Public |
15 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
16 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
17 | Boston, MA 02111-1307, USA. */ | |
28f540f4 | 18 | |
28f540f4 | 19 | #include <ctype.h> |
28f540f4 | 20 | #include <limits.h> |
a04e7405 | 21 | #include <printf.h> |
28f540f4 RM |
22 | #include <stdarg.h> |
23 | #include <stdlib.h> | |
66aeca9c | 24 | #include <errno.h> |
299a95b9 | 25 | #include <wchar.h> |
5107cf1d | 26 | #include <bits/libc-lock.h> |
01c901a5 | 27 | #include <sys/param.h> |
28f540f4 | 28 | #include "_itoa.h" |
cc3fa755 | 29 | #include <locale/localeinfo.h> |
a04e7405 | 30 | |
299a95b9 RM |
31 | /* This code is shared between the standard stdio implementation found |
32 | in GNU C library and the libio implementation originally found in | |
33 | GNU libg++. | |
34 | ||
35 | Beside this it is also shared between the normal and wide character | |
36 | implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */ | |
37 | ||
38 | #ifndef COMPILE_WPRINTF | |
39 | # define CHAR_T char | |
40 | # define UCHAR_T unsigned char | |
41 | # define INT_T int | |
42 | # define L_(Str) Str | |
43 | # define ISDIGIT(Ch) isdigit (Ch) | |
44 | ||
45 | # ifdef USE_IN_LIBIO | |
84384f5b | 46 | # define PUT(F, S, N) _IO_sputn ((F), (S), (N)) |
299a95b9 RM |
47 | # define PAD(Padchar) \ |
48 | if (width > 0) \ | |
84384f5b | 49 | done += _IO_padn (s, (Padchar), width) |
299a95b9 RM |
50 | # else |
51 | # define PUTC(C, F) putc (C, F) | |
52 | ssize_t __printf_pad __P ((FILE *, char pad, size_t n)); | |
53 | # define PAD(Padchar) \ | |
54 | if (width > 0) \ | |
84384f5b UD |
55 | { ssize_t __res = __printf_pad (s, (Padchar), width); \ |
56 | if (__res == -1) return -1; \ | |
57 | done += __res; } | |
299a95b9 RM |
58 | # endif |
59 | #else | |
60 | # define vfprintf vfwprintf | |
61 | # define CHAR_T wchar_t | |
62 | # define UCHAR_T uwchar_t | |
63 | # define INT_T wint_t | |
64 | # define L_(Str) L##Str | |
65 | # define ISDIGIT(Ch) iswdigit (Ch) | |
66 | ||
67 | # ifdef USE_IN_LIBIO | |
84384f5b | 68 | # define PUT(F, S, N) _IO_sputn ((F), (S), (N)) |
7c713e28 | 69 | # define PAD(Padchar) \ |
299a95b9 | 70 | if (width > 0) \ |
84384f5b | 71 | done += _IO_wpadn (s, (Padchar), width) |
299a95b9 RM |
72 | # else |
73 | # define PUTC(C, F) wputc (C, F) | |
74 | ssize_t __wprintf_pad __P ((FILE *, wchar_t pad, size_t n)); | |
7c713e28 | 75 | # define PAD(Padchar) \ |
299a95b9 | 76 | if (width > 0) \ |
84384f5b UD |
77 | { ssize_t __res = __wprintf_pad (s, (Padchar), width); \ |
78 | if (__res == -1) return -1; \ | |
79 | done += __res; } | |
299a95b9 RM |
80 | # endif |
81 | #endif | |
82 | ||
a04e7405 RM |
83 | /* Include the shared code for parsing the format string. */ |
84 | #include "printf-parse.h" | |
85 | ||
28f540f4 | 86 | |
28f540f4 RM |
87 | #ifdef USE_IN_LIBIO |
88 | /* This code is for use in libio. */ | |
299a95b9 | 89 | # include <libioP.h> |
7c713e28 | 90 | # define PUTC(C, F) _IO_putc_unlocked (C, F) |
299a95b9 | 91 | # define vfprintf _IO_vfprintf |
299a95b9 | 92 | # define FILE _IO_FILE |
a68b0d31 | 93 | # undef va_list |
299a95b9 RM |
94 | # define va_list _IO_va_list |
95 | # undef BUFSIZ | |
96 | # define BUFSIZ _IO_BUFSIZ | |
97 | # define ARGCHECK(S, Format) \ | |
28f540f4 RM |
98 | do \ |
99 | { \ | |
100 | /* Check file argument for consistence. */ \ | |
299a95b9 | 101 | CHECK_FILE (S, -1); \ |
68dbb3a6 UD |
102 | if (S->_flags & _IO_NO_WRITES) \ |
103 | { \ | |
104 | __set_errno (EBADF); \ | |
105 | return -1; \ | |
106 | } \ | |
107 | if (Format == NULL) \ | |
28f540f4 RM |
108 | { \ |
109 | MAYBE_SET_EINVAL; \ | |
110 | return -1; \ | |
111 | } \ | |
112 | } while (0) | |
299a95b9 | 113 | # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED) |
28f540f4 RM |
114 | #else /* ! USE_IN_LIBIO */ |
115 | /* This code is for use in the GNU C library. */ | |
299a95b9 RM |
116 | # include <stdio.h> |
117 | # define PUT(F, S, N) fwrite (S, 1, N, F) | |
118 | # define ARGCHECK(S, Format) \ | |
28f540f4 RM |
119 | do \ |
120 | { \ | |
121 | /* Check file argument for consistence. */ \ | |
68dbb3a6 UD |
122 | if (!__validfp (S) || !S->__mode.__write) \ |
123 | { \ | |
124 | __set_errno (EBADF); \ | |
125 | return -1; \ | |
126 | } \ | |
127 | if (Format == NULL) \ | |
28f540f4 | 128 | { \ |
c4029823 | 129 | __set_errno (EINVAL); \ |
28f540f4 RM |
130 | return -1; \ |
131 | } \ | |
299a95b9 | 132 | if (!S->__seen) \ |
28f540f4 | 133 | { \ |
299a95b9 | 134 | if (__flshfp (S, EOF) == EOF) \ |
28f540f4 RM |
135 | return -1; \ |
136 | } \ | |
299a95b9 RM |
137 | } \ |
138 | while (0) | |
139 | # define UNBUFFERED_P(s) ((s)->__buffer == NULL) | |
46ec036d UD |
140 | |
141 | /* XXX These declarations should go as soon as the stdio header files | |
142 | have these prototypes. */ | |
143 | extern void __flockfile (FILE *); | |
144 | extern void __funlockfile (FILE *); | |
28f540f4 RM |
145 | #endif /* USE_IN_LIBIO */ |
146 | ||
147 | ||
299a95b9 | 148 | #define outchar(Ch) \ |
28f540f4 RM |
149 | do \ |
150 | { \ | |
299a95b9 | 151 | register const int outc = (Ch); \ |
77a58cad | 152 | if (PUTC (outc, s) == EOF) \ |
28f540f4 RM |
153 | return -1; \ |
154 | else \ | |
155 | ++done; \ | |
299a95b9 RM |
156 | } \ |
157 | while (0) | |
28f540f4 | 158 | |
299a95b9 | 159 | #define outstring(String, Len) \ |
28f540f4 RM |
160 | do \ |
161 | { \ | |
ba1ffaa1 | 162 | if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \ |
299a95b9 | 163 | return -1; \ |
ba1ffaa1 | 164 | done += (Len); \ |
299a95b9 RM |
165 | } \ |
166 | while (0) | |
28f540f4 | 167 | |
299a95b9 RM |
168 | /* For handling long_double and longlong we use the same flag. */ |
169 | #ifndef is_longlong | |
170 | # define is_longlong is_long_double | |
171 | #endif | |
172 | ||
fafaa44e | 173 | |
299a95b9 RM |
174 | /* Global variables. */ |
175 | static const char null[] = "(null)"; | |
28f540f4 | 176 | |
28f540f4 | 177 | |
299a95b9 | 178 | /* Helper function to provide temporary buffering for unbuffered streams. */ |
dfd2257a UD |
179 | static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list)) |
180 | internal_function; | |
299a95b9 RM |
181 | |
182 | /* Handle unknown format specifier. */ | |
183 | static int printf_unknown __P ((FILE *, const struct printf_info *, | |
184 | const void *const *)); | |
28f540f4 | 185 | |
299a95b9 | 186 | /* Group digits of number string. */ |
dfd2257a UD |
187 | static char *group_number __P ((CHAR_T *, CHAR_T *, const CHAR_T *, wchar_t)) |
188 | internal_function; | |
28f540f4 | 189 | |
a04e7405 | 190 | |
299a95b9 | 191 | /* The function itself. */ |
28f540f4 | 192 | int |
299a95b9 | 193 | vfprintf (FILE *s, const CHAR_T *format, va_list ap) |
28f540f4 RM |
194 | { |
195 | /* The character used as thousands separator. */ | |
196 | wchar_t thousands_sep; | |
197 | ||
198 | /* The string describing the size of groups of digits. */ | |
a04e7405 RM |
199 | const char *grouping; |
200 | ||
299a95b9 RM |
201 | /* Place to accumulate the result. */ |
202 | int done; | |
a04e7405 | 203 | |
299a95b9 RM |
204 | /* Current character in format string. */ |
205 | const UCHAR_T *f; | |
28f540f4 | 206 | |
a04e7405 | 207 | /* End of leading constant string. */ |
299a95b9 RM |
208 | const UCHAR_T *lead_str_end; |
209 | ||
210 | /* Points to next format specifier. */ | |
211 | const UCHAR_T *end_of_spec; | |
212 | ||
213 | /* Buffer intermediate results. */ | |
214 | char work_buffer[1000]; | |
215 | #define workend (&work_buffer[sizeof (work_buffer) - 1]) | |
216 | ||
217 | /* State for restartable multibyte character handling functions. */ | |
218 | mbstate_t mbstate; | |
219 | ||
220 | /* We have to save the original argument pointer. */ | |
221 | va_list ap_save; | |
28f540f4 | 222 | |
299a95b9 RM |
223 | /* Count number of specifiers we already processed. */ |
224 | int nspecs_done; | |
225 | ||
47707456 UD |
226 | /* For the %m format we may need the current `errno' value. */ |
227 | int save_errno = errno; | |
228 | ||
299a95b9 RM |
229 | |
230 | /* This table maps a character into a number representing a | |
231 | class. In each step there is a destination label for each | |
232 | class. */ | |
233 | static const int jump_table[] = | |
234 | { | |
235 | /* ' ' */ 1, 0, 0, /* '#' */ 4, | |
236 | 0, /* '%' */ 14, 0, /* '\''*/ 6, | |
237 | 0, 0, /* '*' */ 7, /* '+' */ 2, | |
238 | 0, /* '-' */ 3, /* '.' */ 9, 0, | |
239 | /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8, | |
240 | /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8, | |
241 | /* '8' */ 8, /* '9' */ 8, 0, 0, | |
242 | 0, 0, 0, 0, | |
2f6d1f1b | 243 | 0, /* 'A' */ 26, 0, /* 'C' */ 25, |
299a95b9 RM |
244 | 0, /* 'E' */ 19, 0, /* 'G' */ 19, |
245 | 0, 0, 0, 0, | |
246 | /* 'L' */ 12, 0, 0, 0, | |
2c6fe0bd | 247 | 0, 0, 0, /* 'S' */ 21, |
299a95b9 RM |
248 | 0, 0, 0, 0, |
249 | /* 'X' */ 18, 0, /* 'Z' */ 13, 0, | |
250 | 0, 0, 0, 0, | |
2f6d1f1b | 251 | 0, /* 'a' */ 26, 0, /* 'c' */ 20, |
299a95b9 RM |
252 | /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19, |
253 | /* 'h' */ 10, /* 'i' */ 15, 0, 0, | |
254 | /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17, | |
255 | /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21, | |
256 | 0, /* 'u' */ 16, 0, 0, | |
257 | /* 'x' */ 18 | |
258 | }; | |
259 | ||
260 | #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'x') | |
261 | #define CHAR_CLASS(Ch) (jump_table[(int) (Ch) - ' ']) | |
262 | #define JUMP(ChExpr, table) \ | |
263 | do \ | |
264 | { \ | |
265 | const void *ptr; \ | |
266 | spec = (ChExpr); \ | |
267 | ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \ | |
268 | : table[CHAR_CLASS (spec)]; \ | |
269 | goto *ptr; \ | |
270 | } \ | |
271 | while (0) | |
272 | ||
273 | #define STEP0_3_TABLE \ | |
274 | /* Step 0: at the beginning. */ \ | |
2f6d1f1b | 275 | static const void *step0_jumps[27] = \ |
299a95b9 RM |
276 | { \ |
277 | REF (form_unknown), \ | |
278 | REF (flag_space), /* for ' ' */ \ | |
279 | REF (flag_plus), /* for '+' */ \ | |
280 | REF (flag_minus), /* for '-' */ \ | |
281 | REF (flag_hash), /* for '<hash>' */ \ | |
282 | REF (flag_zero), /* for '0' */ \ | |
283 | REF (flag_quote), /* for '\'' */ \ | |
284 | REF (width_asterics), /* for '*' */ \ | |
285 | REF (width), /* for '1'...'9' */ \ | |
286 | REF (precision), /* for '.' */ \ | |
287 | REF (mod_half), /* for 'h' */ \ | |
288 | REF (mod_long), /* for 'l' */ \ | |
289 | REF (mod_longlong), /* for 'L', 'q' */ \ | |
290 | REF (mod_size_t), /* for 'Z' */ \ | |
291 | REF (form_percent), /* for '%' */ \ | |
292 | REF (form_integer), /* for 'd', 'i' */ \ | |
293 | REF (form_unsigned), /* for 'u' */ \ | |
294 | REF (form_octal), /* for 'o' */ \ | |
295 | REF (form_hexa), /* for 'X', 'x' */ \ | |
296 | REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \ | |
297 | REF (form_character), /* for 'c' */ \ | |
2c6fe0bd | 298 | REF (form_string), /* for 's', 'S' */ \ |
299a95b9 RM |
299 | REF (form_pointer), /* for 'p' */ \ |
300 | REF (form_number), /* for 'n' */ \ | |
2c6fe0bd | 301 | REF (form_strerror), /* for 'm' */ \ |
2f6d1f1b UD |
302 | REF (form_wcharacter), /* for 'C' */ \ |
303 | REF (form_floathex) /* for 'A', 'a' */ \ | |
299a95b9 RM |
304 | }; \ |
305 | /* Step 1: after processing width. */ \ | |
2f6d1f1b | 306 | static const void *step1_jumps[27] = \ |
299a95b9 RM |
307 | { \ |
308 | REF (form_unknown), \ | |
309 | REF (form_unknown), /* for ' ' */ \ | |
310 | REF (form_unknown), /* for '+' */ \ | |
311 | REF (form_unknown), /* for '-' */ \ | |
312 | REF (form_unknown), /* for '<hash>' */ \ | |
313 | REF (form_unknown), /* for '0' */ \ | |
314 | REF (form_unknown), /* for '\'' */ \ | |
315 | REF (form_unknown), /* for '*' */ \ | |
316 | REF (form_unknown), /* for '1'...'9' */ \ | |
317 | REF (precision), /* for '.' */ \ | |
318 | REF (mod_half), /* for 'h' */ \ | |
319 | REF (mod_long), /* for 'l' */ \ | |
320 | REF (mod_longlong), /* for 'L', 'q' */ \ | |
321 | REF (mod_size_t), /* for 'Z' */ \ | |
322 | REF (form_percent), /* for '%' */ \ | |
323 | REF (form_integer), /* for 'd', 'i' */ \ | |
324 | REF (form_unsigned), /* for 'u' */ \ | |
325 | REF (form_octal), /* for 'o' */ \ | |
326 | REF (form_hexa), /* for 'X', 'x' */ \ | |
327 | REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \ | |
328 | REF (form_character), /* for 'c' */ \ | |
2c6fe0bd | 329 | REF (form_string), /* for 's', 'S' */ \ |
299a95b9 RM |
330 | REF (form_pointer), /* for 'p' */ \ |
331 | REF (form_number), /* for 'n' */ \ | |
2c6fe0bd | 332 | REF (form_strerror), /* for 'm' */ \ |
2f6d1f1b UD |
333 | REF (form_wcharacter), /* for 'C' */ \ |
334 | REF (form_floathex) /* for 'A', 'a' */ \ | |
299a95b9 RM |
335 | }; \ |
336 | /* Step 2: after processing precision. */ \ | |
2f6d1f1b | 337 | static const void *step2_jumps[27] = \ |
299a95b9 RM |
338 | { \ |
339 | REF (form_unknown), \ | |
340 | REF (form_unknown), /* for ' ' */ \ | |
341 | REF (form_unknown), /* for '+' */ \ | |
342 | REF (form_unknown), /* for '-' */ \ | |
343 | REF (form_unknown), /* for '<hash>' */ \ | |
344 | REF (form_unknown), /* for '0' */ \ | |
345 | REF (form_unknown), /* for '\'' */ \ | |
346 | REF (form_unknown), /* for '*' */ \ | |
347 | REF (form_unknown), /* for '1'...'9' */ \ | |
348 | REF (form_unknown), /* for '.' */ \ | |
349 | REF (mod_half), /* for 'h' */ \ | |
350 | REF (mod_long), /* for 'l' */ \ | |
351 | REF (mod_longlong), /* for 'L', 'q' */ \ | |
352 | REF (mod_size_t), /* for 'Z' */ \ | |
353 | REF (form_percent), /* for '%' */ \ | |
354 | REF (form_integer), /* for 'd', 'i' */ \ | |
355 | REF (form_unsigned), /* for 'u' */ \ | |
356 | REF (form_octal), /* for 'o' */ \ | |
357 | REF (form_hexa), /* for 'X', 'x' */ \ | |
358 | REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \ | |
359 | REF (form_character), /* for 'c' */ \ | |
2c6fe0bd | 360 | REF (form_string), /* for 's', 'S' */ \ |
299a95b9 RM |
361 | REF (form_pointer), /* for 'p' */ \ |
362 | REF (form_number), /* for 'n' */ \ | |
2c6fe0bd | 363 | REF (form_strerror), /* for 'm' */ \ |
2f6d1f1b UD |
364 | REF (form_wcharacter), /* for 'C' */ \ |
365 | REF (form_floathex) /* for 'A', 'a' */ \ | |
299a95b9 | 366 | }; \ |
cc3fa755 UD |
367 | /* Step 3a: after processing first 'h' modifier. */ \ |
368 | static const void *step3a_jumps[27] = \ | |
369 | { \ | |
370 | REF (form_unknown), \ | |
371 | REF (form_unknown), /* for ' ' */ \ | |
372 | REF (form_unknown), /* for '+' */ \ | |
373 | REF (form_unknown), /* for '-' */ \ | |
374 | REF (form_unknown), /* for '<hash>' */ \ | |
375 | REF (form_unknown), /* for '0' */ \ | |
376 | REF (form_unknown), /* for '\'' */ \ | |
377 | REF (form_unknown), /* for '*' */ \ | |
378 | REF (form_unknown), /* for '1'...'9' */ \ | |
379 | REF (form_unknown), /* for '.' */ \ | |
380 | REF (mod_halfhalf), /* for 'h' */ \ | |
381 | REF (form_unknown), /* for 'l' */ \ | |
382 | REF (form_unknown), /* for 'L', 'q' */ \ | |
383 | REF (form_unknown), /* for 'Z' */ \ | |
384 | REF (form_percent), /* for '%' */ \ | |
385 | REF (form_integer), /* for 'd', 'i' */ \ | |
386 | REF (form_unsigned), /* for 'u' */ \ | |
387 | REF (form_octal), /* for 'o' */ \ | |
388 | REF (form_hexa), /* for 'X', 'x' */ \ | |
389 | REF (form_unknown), /* for 'E', 'e', 'f', 'G', 'g' */ \ | |
390 | REF (form_unknown), /* for 'c' */ \ | |
391 | REF (form_unknown), /* for 's', 'S' */ \ | |
392 | REF (form_unknown), /* for 'p' */ \ | |
393 | REF (form_number), /* for 'n' */ \ | |
394 | REF (form_unknown), /* for 'm' */ \ | |
395 | REF (form_unknown), /* for 'C' */ \ | |
396 | REF (form_unknown) /* for 'A', 'a' */ \ | |
397 | }; \ | |
398 | /* Step 3b: after processing first 'l' modifier. */ \ | |
399 | static const void *step3b_jumps[27] = \ | |
299a95b9 RM |
400 | { \ |
401 | REF (form_unknown), \ | |
402 | REF (form_unknown), /* for ' ' */ \ | |
403 | REF (form_unknown), /* for '+' */ \ | |
404 | REF (form_unknown), /* for '-' */ \ | |
405 | REF (form_unknown), /* for '<hash>' */ \ | |
406 | REF (form_unknown), /* for '0' */ \ | |
407 | REF (form_unknown), /* for '\'' */ \ | |
408 | REF (form_unknown), /* for '*' */ \ | |
409 | REF (form_unknown), /* for '1'...'9' */ \ | |
410 | REF (form_unknown), /* for '.' */ \ | |
411 | REF (form_unknown), /* for 'h' */ \ | |
412 | REF (mod_longlong), /* for 'l' */ \ | |
413 | REF (form_unknown), /* for 'L', 'q' */ \ | |
414 | REF (form_unknown), /* for 'Z' */ \ | |
415 | REF (form_percent), /* for '%' */ \ | |
416 | REF (form_integer), /* for 'd', 'i' */ \ | |
417 | REF (form_unsigned), /* for 'u' */ \ | |
418 | REF (form_octal), /* for 'o' */ \ | |
419 | REF (form_hexa), /* for 'X', 'x' */ \ | |
420 | REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \ | |
421 | REF (form_character), /* for 'c' */ \ | |
2c6fe0bd | 422 | REF (form_string), /* for 's', 'S' */ \ |
299a95b9 RM |
423 | REF (form_pointer), /* for 'p' */ \ |
424 | REF (form_number), /* for 'n' */ \ | |
2c6fe0bd | 425 | REF (form_strerror), /* for 'm' */ \ |
2f6d1f1b UD |
426 | REF (form_wcharacter), /* for 'C' */ \ |
427 | REF (form_floathex) /* for 'A', 'a' */ \ | |
299a95b9 | 428 | } |
28f540f4 | 429 | |
299a95b9 RM |
430 | #define STEP4_TABLE \ |
431 | /* Step 4: processing format specifier. */ \ | |
2f6d1f1b | 432 | static const void *step4_jumps[27] = \ |
299a95b9 RM |
433 | { \ |
434 | REF (form_unknown), \ | |
435 | REF (form_unknown), /* for ' ' */ \ | |
436 | REF (form_unknown), /* for '+' */ \ | |
437 | REF (form_unknown), /* for '-' */ \ | |
438 | REF (form_unknown), /* for '<hash>' */ \ | |
439 | REF (form_unknown), /* for '0' */ \ | |
440 | REF (form_unknown), /* for '\'' */ \ | |
441 | REF (form_unknown), /* for '*' */ \ | |
442 | REF (form_unknown), /* for '1'...'9' */ \ | |
443 | REF (form_unknown), /* for '.' */ \ | |
444 | REF (form_unknown), /* for 'h' */ \ | |
445 | REF (form_unknown), /* for 'l' */ \ | |
446 | REF (form_unknown), /* for 'L', 'q' */ \ | |
447 | REF (form_unknown), /* for 'Z' */ \ | |
448 | REF (form_percent), /* for '%' */ \ | |
449 | REF (form_integer), /* for 'd', 'i' */ \ | |
450 | REF (form_unsigned), /* for 'u' */ \ | |
451 | REF (form_octal), /* for 'o' */ \ | |
452 | REF (form_hexa), /* for 'X', 'x' */ \ | |
453 | REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \ | |
454 | REF (form_character), /* for 'c' */ \ | |
2c6fe0bd | 455 | REF (form_string), /* for 's', 'S' */ \ |
299a95b9 RM |
456 | REF (form_pointer), /* for 'p' */ \ |
457 | REF (form_number), /* for 'n' */ \ | |
2c6fe0bd | 458 | REF (form_strerror), /* for 'm' */ \ |
2f6d1f1b UD |
459 | REF (form_wcharacter), /* for 'C' */ \ |
460 | REF (form_floathex) /* for 'A', 'a' */ \ | |
299a95b9 | 461 | } |
a04e7405 | 462 | |
a04e7405 | 463 | |
299a95b9 | 464 | #define process_arg(fspec) \ |
edf5b2d7 | 465 | /* Start real work. We know about all flags and modifiers and \ |
299a95b9 RM |
466 | now process the wanted format specifier. */ \ |
467 | LABEL (form_percent): \ | |
468 | /* Write a literal "%". */ \ | |
469 | outchar ('%'); \ | |
470 | break; \ | |
471 | \ | |
472 | LABEL (form_integer): \ | |
473 | /* Signed decimal integer. */ \ | |
474 | base = 10; \ | |
475 | \ | |
476 | if (is_longlong) \ | |
477 | { \ | |
478 | long long int signed_number; \ | |
479 | \ | |
b8fe19fa RM |
480 | if (fspec == NULL) \ |
481 | signed_number = va_arg (ap, long long int); \ | |
482 | else \ | |
483 | signed_number = args_value[fspec->data_arg].pa_long_long_int; \ | |
299a95b9 RM |
484 | \ |
485 | is_negative = signed_number < 0; \ | |
486 | number.longlong = is_negative ? (- signed_number) : signed_number; \ | |
487 | \ | |
488 | goto LABEL (longlong_number); \ | |
489 | } \ | |
490 | else \ | |
491 | { \ | |
492 | long int signed_number; \ | |
493 | \ | |
b8fe19fa | 494 | if (fspec == NULL) \ |
6591c335 UD |
495 | { \ |
496 | if (is_long) \ | |
497 | signed_number = va_arg (ap, long int); \ | |
498 | else /* `char' and `short int' will be promoted to `int'. */ \ | |
499 | signed_number = va_arg (ap, int); \ | |
500 | } \ | |
b8fe19fa RM |
501 | else \ |
502 | if (is_long) \ | |
503 | signed_number = args_value[fspec->data_arg].pa_long_int; \ | |
504 | else \ | |
505 | signed_number = args_value[fspec->data_arg].pa_int; \ | |
299a95b9 RM |
506 | \ |
507 | is_negative = signed_number < 0; \ | |
508 | number.word = is_negative ? (- signed_number) : signed_number; \ | |
509 | \ | |
510 | goto LABEL (number); \ | |
511 | } \ | |
512 | /* NOTREACHED */ \ | |
513 | \ | |
514 | LABEL (form_unsigned): \ | |
515 | /* Unsigned decimal integer. */ \ | |
516 | base = 10; \ | |
517 | goto LABEL (unsigned_number); \ | |
518 | /* NOTREACHED */ \ | |
519 | \ | |
520 | LABEL (form_octal): \ | |
521 | /* Unsigned octal integer. */ \ | |
522 | base = 8; \ | |
523 | goto LABEL (unsigned_number); \ | |
524 | /* NOTREACHED */ \ | |
525 | \ | |
526 | LABEL (form_hexa): \ | |
527 | /* Unsigned hexadecimal integer. */ \ | |
528 | base = 16; \ | |
529 | \ | |
530 | LABEL (unsigned_number): /* Unsigned number of base BASE. */ \ | |
531 | \ | |
cc3fa755 | 532 | /* ISO specifies the `+' and ` ' flags only for signed \ |
299a95b9 RM |
533 | conversions. */ \ |
534 | is_negative = 0; \ | |
535 | showsign = 0; \ | |
536 | space = 0; \ | |
537 | \ | |
538 | if (is_longlong) \ | |
539 | { \ | |
b8fe19fa RM |
540 | if (fspec == NULL) \ |
541 | number.longlong = va_arg (ap, unsigned long long int); \ | |
542 | else \ | |
543 | number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \ | |
299a95b9 RM |
544 | \ |
545 | LABEL (longlong_number): \ | |
546 | if (prec < 0) \ | |
547 | /* Supply a default precision if none was given. */ \ | |
548 | prec = 1; \ | |
549 | else \ | |
550 | /* We have to take care for the '0' flag. If a precision \ | |
551 | is given it must be ignored. */ \ | |
552 | pad = ' '; \ | |
553 | \ | |
554 | /* If the precision is 0 and the number is 0 nothing has to \ | |
6591c335 UD |
555 | be written for the number, except for the 'o' format in \ |
556 | alternate form. */ \ | |
299a95b9 | 557 | if (prec == 0 && number.longlong == 0) \ |
6591c335 UD |
558 | { \ |
559 | string = workend; \ | |
560 | if (base == 8 && alt) \ | |
561 | *string-- = '0'; \ | |
562 | } \ | |
299a95b9 RM |
563 | else \ |
564 | { \ | |
565 | /* Put the number in WORK. */ \ | |
566 | string = _itoa (number.longlong, workend + 1, base, \ | |
567 | spec == 'X'); \ | |
568 | string -= 1; \ | |
569 | if (group && grouping) \ | |
570 | string = group_number (string, workend, grouping, \ | |
571 | thousands_sep); \ | |
572 | } \ | |
573 | /* Simply further test for num != 0. */ \ | |
574 | number.word = number.longlong != 0; \ | |
575 | } \ | |
576 | else \ | |
577 | { \ | |
b8fe19fa | 578 | if (fspec == NULL) \ |
6591c335 UD |
579 | { \ |
580 | if (is_long) \ | |
581 | number.word = va_arg (ap, unsigned long int); \ | |
582 | else if (!is_short) \ | |
583 | number.word = va_arg (ap, unsigned int); \ | |
584 | else \ | |
585 | number.word = (unsigned short int) va_arg (ap, unsigned int); \ | |
586 | } \ | |
299a95b9 | 587 | else \ |
b8fe19fa RM |
588 | if (is_long) \ |
589 | number.word = args_value[fspec->data_arg].pa_u_long_int; \ | |
cc3fa755 UD |
590 | else if (is_char) \ |
591 | number.word = (unsigned char) \ | |
592 | args_value[fspec->data_arg].pa_char; \ | |
b8fe19fa RM |
593 | else if (!is_short) \ |
594 | number.word = args_value[fspec->data_arg].pa_u_int; \ | |
595 | else \ | |
596 | number.word = (unsigned short int) \ | |
597 | args_value[fspec->data_arg].pa_u_short_int; \ | |
299a95b9 RM |
598 | \ |
599 | LABEL (number): \ | |
600 | if (prec < 0) \ | |
601 | /* Supply a default precision if none was given. */ \ | |
602 | prec = 1; \ | |
603 | else \ | |
604 | /* We have to take care for the '0' flag. If a precision \ | |
605 | is given it must be ignored. */ \ | |
606 | pad = ' '; \ | |
607 | \ | |
608 | /* If the precision is 0 and the number is 0 nothing has to \ | |
6591c335 UD |
609 | be written for the number, except for the 'o' format in \ |
610 | alternate form. */ \ | |
299a95b9 | 611 | if (prec == 0 && number.word == 0) \ |
6591c335 UD |
612 | { \ |
613 | string = workend; \ | |
614 | if (base == 8 && alt) \ | |
615 | *string-- = '0'; \ | |
616 | } \ | |
299a95b9 RM |
617 | else \ |
618 | { \ | |
619 | /* Put the number in WORK. */ \ | |
620 | string = _itoa_word (number.word, workend + 1, base, \ | |
621 | spec == 'X'); \ | |
622 | string -= 1; \ | |
623 | if (group && grouping) \ | |
624 | string = group_number (string, workend, grouping, \ | |
625 | thousands_sep); \ | |
626 | } \ | |
627 | } \ | |
628 | \ | |
629 | prec -= workend - string; \ | |
630 | \ | |
631 | if (prec > 0) \ | |
632 | /* Add zeros to the precision. */ \ | |
633 | while (prec-- > 0) \ | |
634 | *string-- = '0'; \ | |
635 | else if (number.word != 0 && alt && base == 8) \ | |
636 | /* Add octal marker. */ \ | |
637 | *string-- = '0'; \ | |
638 | \ | |
639 | if (!left) \ | |
640 | { \ | |
641 | width -= workend - string; \ | |
642 | \ | |
643 | if (number.word != 0 && alt && base == 16) \ | |
644 | /* Account for 0X hex marker. */ \ | |
645 | width -= 2; \ | |
646 | \ | |
647 | if (is_negative || showsign || space) \ | |
648 | --width; \ | |
649 | \ | |
650 | if (pad == '0') \ | |
651 | { \ | |
652 | while (width-- > 0) \ | |
653 | *string-- = '0'; \ | |
654 | \ | |
655 | if (number.word != 0 && alt && base == 16) \ | |
656 | { \ | |
657 | *string-- = spec; \ | |
658 | *string-- = '0'; \ | |
659 | } \ | |
660 | \ | |
661 | if (is_negative) \ | |
662 | *string-- = '-'; \ | |
663 | else if (showsign) \ | |
664 | *string-- = '+'; \ | |
665 | else if (space) \ | |
666 | *string-- = ' '; \ | |
667 | } \ | |
668 | else \ | |
669 | { \ | |
670 | if (number.word != 0 && alt && base == 16) \ | |
671 | { \ | |
672 | *string-- = spec; \ | |
673 | *string-- = '0'; \ | |
674 | } \ | |
675 | \ | |
676 | if (is_negative) \ | |
677 | *string-- = '-'; \ | |
678 | else if (showsign) \ | |
679 | *string-- = '+'; \ | |
680 | else if (space) \ | |
681 | *string-- = ' '; \ | |
682 | \ | |
683 | while (width-- > 0) \ | |
684 | *string-- = ' '; \ | |
685 | } \ | |
686 | \ | |
687 | outstring (string + 1, workend - string); \ | |
688 | \ | |
689 | break; \ | |
690 | } \ | |
691 | else \ | |
692 | { \ | |
693 | if (number.word != 0 && alt && base == 16) \ | |
694 | { \ | |
695 | *string-- = spec; \ | |
696 | *string-- = '0'; \ | |
697 | } \ | |
698 | \ | |
699 | if (is_negative) \ | |
700 | *string-- = '-'; \ | |
701 | else if (showsign) \ | |
702 | *string-- = '+'; \ | |
703 | else if (space) \ | |
704 | *string-- = ' '; \ | |
705 | \ | |
706 | width -= workend - string; \ | |
707 | outstring (string + 1, workend - string); \ | |
708 | \ | |
709 | PAD (' '); \ | |
710 | break; \ | |
711 | } \ | |
712 | \ | |
713 | LABEL (form_float): \ | |
714 | { \ | |
715 | /* Floating-point number. This is handled by printf_fp.c. */ \ | |
716 | extern int __printf_fp __P ((FILE *, const struct printf_info *, \ | |
717 | const void **const)); \ | |
718 | const void *ptr; \ | |
719 | int function_done; \ | |
720 | \ | |
299a95b9 RM |
721 | if (fspec == NULL) \ |
722 | { \ | |
723 | struct printf_info info = { prec: prec, \ | |
724 | width: width, \ | |
725 | spec: spec, \ | |
726 | is_long_double: is_long_double, \ | |
727 | is_short: is_short, \ | |
728 | is_long: is_long, \ | |
729 | alt: alt, \ | |
730 | space: space, \ | |
731 | left: left, \ | |
732 | showsign: showsign, \ | |
733 | group: group, \ | |
b8fe19fa RM |
734 | pad: pad, \ |
735 | extra: 0 }; \ | |
736 | \ | |
737 | if (is_long_double) \ | |
738 | the_arg.pa_long_double = va_arg (ap, long double); \ | |
739 | else \ | |
740 | the_arg.pa_double = va_arg (ap, double); \ | |
741 | ptr = (const void *) &the_arg; \ | |
299a95b9 RM |
742 | \ |
743 | function_done = __printf_fp (s, &info, &ptr); \ | |
744 | } \ | |
745 | else \ | |
b8fe19fa RM |
746 | { \ |
747 | ptr = (const void *) &args_value[fspec->data_arg]; \ | |
748 | \ | |
749 | function_done = __printf_fp (s, &fspec->info, &ptr); \ | |
750 | } \ | |
299a95b9 RM |
751 | \ |
752 | if (function_done < 0) \ | |
753 | /* Error in print handler. */ \ | |
754 | return -1; \ | |
755 | \ | |
756 | done += function_done; \ | |
757 | } \ | |
758 | break; \ | |
759 | \ | |
2f6d1f1b UD |
760 | LABEL (form_floathex): \ |
761 | { \ | |
762 | /* FLoating point number printed as hexadecimal number. */ \ | |
763 | extern int __printf_fphex __P ((FILE *, const struct printf_info *, \ | |
764 | const void **const)); \ | |
765 | const void *ptr; \ | |
766 | int function_done; \ | |
767 | \ | |
768 | if (fspec == NULL) \ | |
769 | { \ | |
770 | struct printf_info info = { prec: prec, \ | |
771 | width: width, \ | |
772 | spec: spec, \ | |
773 | is_long_double: is_long_double, \ | |
774 | is_short: is_short, \ | |
775 | is_long: is_long, \ | |
776 | alt: alt, \ | |
777 | space: space, \ | |
778 | left: left, \ | |
779 | showsign: showsign, \ | |
780 | group: group, \ | |
781 | pad: pad, \ | |
782 | extra: 0 }; \ | |
783 | \ | |
784 | if (is_long_double) \ | |
785 | the_arg.pa_long_double = va_arg (ap, long double); \ | |
786 | else \ | |
787 | the_arg.pa_double = va_arg (ap, double); \ | |
788 | ptr = (const void *) &the_arg; \ | |
789 | \ | |
790 | function_done = __printf_fphex (s, &info, &ptr); \ | |
791 | } \ | |
792 | else \ | |
793 | { \ | |
794 | ptr = (const void *) &args_value[fspec->data_arg]; \ | |
795 | \ | |
796 | function_done = __printf_fphex (s, &fspec->info, &ptr); \ | |
797 | } \ | |
798 | \ | |
799 | if (function_done < 0) \ | |
800 | /* Error in print handler. */ \ | |
801 | return -1; \ | |
802 | \ | |
803 | done += function_done; \ | |
804 | } \ | |
805 | break; \ | |
806 | \ | |
299a95b9 RM |
807 | LABEL (form_character): \ |
808 | /* Character. */ \ | |
2c6fe0bd UD |
809 | if (is_long) \ |
810 | goto LABEL (form_wcharacter); \ | |
299a95b9 RM |
811 | --width; /* Account for the character itself. */ \ |
812 | if (!left) \ | |
813 | PAD (' '); \ | |
b8fe19fa | 814 | if (fspec == NULL) \ |
2c6fe0bd | 815 | outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \ |
b8fe19fa RM |
816 | else \ |
817 | outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \ | |
299a95b9 RM |
818 | if (left) \ |
819 | PAD (' '); \ | |
820 | break; \ | |
821 | \ | |
2c6fe0bd UD |
822 | LABEL (form_wcharacter): \ |
823 | { \ | |
824 | /* Wide character. */ \ | |
825 | char buf[MB_CUR_MAX]; \ | |
826 | mbstate_t mbstate; \ | |
827 | size_t len; \ | |
828 | \ | |
3c720987 | 829 | memset (&mbstate, '\0', sizeof (mbstate_t)); \ |
2c6fe0bd UD |
830 | len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \ |
831 | : args_value[fspec->data_arg].pa_wchar), \ | |
832 | &mbstate); \ | |
833 | width -= len; \ | |
834 | if (!left) \ | |
835 | PAD (' '); \ | |
836 | outstring (buf, len); \ | |
837 | if (left) \ | |
838 | PAD (' '); \ | |
839 | } \ | |
840 | break; \ | |
841 | \ | |
299a95b9 RM |
842 | LABEL (form_string): \ |
843 | { \ | |
844 | size_t len; \ | |
845 | \ | |
846 | /* The string argument could in fact be `char *' or `wchar_t *'. \ | |
847 | But this should not make a difference here. */ \ | |
b8fe19fa RM |
848 | if (fspec == NULL) \ |
849 | string = (char *) va_arg (ap, const char *); \ | |
850 | else \ | |
851 | string = (char *) args_value[fspec->data_arg].pa_string; \ | |
299a95b9 RM |
852 | \ |
853 | /* Entry point for printing other strings. */ \ | |
854 | LABEL (print_string): \ | |
855 | \ | |
856 | if (string == NULL) \ | |
857 | { \ | |
858 | /* Write "(null)" if there's space. */ \ | |
859 | if (prec == -1 || prec >= (int) sizeof (null) - 1) \ | |
860 | { \ | |
861 | string = (char *) null; \ | |
862 | len = sizeof (null) - 1; \ | |
863 | } \ | |
864 | else \ | |
865 | { \ | |
866 | string = (char *) ""; \ | |
867 | len = 0; \ | |
868 | } \ | |
869 | } \ | |
2c6fe0bd | 870 | else if (!is_long && spec != L_('S')) \ |
299a95b9 RM |
871 | { \ |
872 | if (prec != -1) \ | |
2c6fe0bd UD |
873 | /* Search for the end of the string, but don't search past \ |
874 | the length specified by the precision. */ \ | |
875 | len = strnlen (string, prec); \ | |
299a95b9 RM |
876 | else \ |
877 | len = strlen (string); \ | |
878 | } \ | |
879 | else \ | |
880 | { \ | |
881 | const wchar_t *s2 = (const wchar_t *) string; \ | |
07a4742f | 882 | mbstate_t mbstate; \ |
299a95b9 | 883 | \ |
3c720987 | 884 | memset (&mbstate, '\0', sizeof (mbstate_t)); \ |
23396375 | 885 | len = __wcsrtombs (NULL, &s2, 0, &mbstate); \ |
299a95b9 RM |
886 | if (len == (size_t) -1) \ |
887 | /* Illegal wide-character string. */ \ | |
888 | return -1; \ | |
889 | \ | |
890 | s2 = (const wchar_t *) string; \ | |
299a95b9 | 891 | string = alloca (len + 1); \ |
3c720987 | 892 | memset (&mbstate, '\0', sizeof (mbstate_t)); \ |
23396375 UD |
893 | (void) __wcsrtombs (string, &s2, prec != -1 ? prec : UINT_MAX, \ |
894 | &mbstate); \ | |
299a95b9 RM |
895 | } \ |
896 | \ | |
897 | if ((width -= len) < 0) \ | |
898 | { \ | |
899 | outstring (string, len); \ | |
900 | break; \ | |
901 | } \ | |
902 | \ | |
903 | if (!left) \ | |
904 | PAD (' '); \ | |
905 | outstring (string, len); \ | |
906 | if (left) \ | |
907 | PAD (' '); \ | |
908 | } \ | |
909 | break; \ | |
910 | \ | |
911 | LABEL (form_pointer): \ | |
912 | /* Generic pointer. */ \ | |
913 | { \ | |
914 | const void *ptr; \ | |
b8fe19fa RM |
915 | if (fspec == NULL) \ |
916 | ptr = va_arg (ap, void *); \ | |
917 | else \ | |
918 | ptr = args_value[fspec->data_arg].pa_pointer; \ | |
299a95b9 RM |
919 | if (ptr != NULL) \ |
920 | { \ | |
921 | /* If the pointer is not NULL, write it as a %#x spec. */ \ | |
922 | base = 16; \ | |
923 | number.word = (unsigned long int) ptr; \ | |
924 | is_negative = 0; \ | |
925 | alt = 1; \ | |
926 | group = 0; \ | |
927 | spec = 'x'; \ | |
928 | goto LABEL (number); \ | |
929 | } \ | |
930 | else \ | |
931 | { \ | |
932 | /* Write "(nil)" for a nil pointer. */ \ | |
933 | string = (char *) "(nil)"; \ | |
934 | /* Make sure the full string "(nil)" is printed. */ \ | |
935 | if (prec < 5) \ | |
936 | prec = 5; \ | |
937 | is_long = 0; /* This is no wide-char string. */ \ | |
938 | goto LABEL (print_string); \ | |
939 | } \ | |
940 | } \ | |
941 | /* NOTREACHED */ \ | |
942 | \ | |
943 | LABEL (form_number): \ | |
944 | /* Answer the count of characters written. */ \ | |
b8fe19fa | 945 | if (fspec == NULL) \ |
6591c335 UD |
946 | { \ |
947 | if (is_longlong) \ | |
948 | *(long long int *) va_arg (ap, void *) = done; \ | |
949 | else if (is_long) \ | |
950 | *(long int *) va_arg (ap, void *) = done; \ | |
951 | else if (!is_short) \ | |
952 | *(int *) va_arg (ap, void *) = done; \ | |
953 | else \ | |
954 | *(short int *) va_arg (ap, void *) = done; \ | |
955 | } \ | |
299a95b9 | 956 | else \ |
b8fe19fa RM |
957 | if (is_longlong) \ |
958 | *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \ | |
959 | else if (is_long) \ | |
960 | *(long int *) args_value[fspec->data_arg].pa_pointer = done; \ | |
961 | else if (!is_short) \ | |
962 | *(int *) args_value[fspec->data_arg].pa_pointer = done; \ | |
963 | else \ | |
964 | *(short int *) args_value[fspec->data_arg].pa_pointer = done; \ | |
299a95b9 RM |
965 | break; \ |
966 | \ | |
967 | LABEL (form_strerror): \ | |
968 | /* Print description of error ERRNO. */ \ | |
310b3460 UD |
969 | string = \ |
970 | (char *) __strerror_r (save_errno, work_buffer, sizeof work_buffer); \ | |
299a95b9 RM |
971 | is_long = 0; /* This is no wide-char string. */ \ |
972 | goto LABEL (print_string) | |
973 | ||
974 | ||
975 | /* Sanity check of arguments. */ | |
28f540f4 RM |
976 | ARGCHECK (s, format); |
977 | ||
978 | if (UNBUFFERED_P (s)) | |
979 | /* Use a helper function which will allocate a local temporary buffer | |
980 | for the stream and then call us again. */ | |
a04e7405 | 981 | return buffered_vfprintf (s, format, ap); |
28f540f4 | 982 | |
299a95b9 RM |
983 | /* Initialize local variables. */ |
984 | done = 0; | |
985 | grouping = (const char *) -1; | |
7cc27f44 | 986 | #ifdef __va_copy |
2f6d1f1b | 987 | /* This macro will be available soon in gcc's <stdarg.h>. We need it |
7cc27f44 UD |
988 | since on some systems `va_list' is not an integral type. */ |
989 | __va_copy (ap_save, ap); | |
990 | #else | |
299a95b9 | 991 | ap_save = ap; |
7cc27f44 | 992 | #endif |
299a95b9 | 993 | nspecs_done = 0; |
28f540f4 | 994 | |
3c720987 UD |
995 | /* Put state for processing format string in initial state. */ |
996 | memset (&mbstate, '\0', sizeof (mbstate_t)); | |
997 | ||
299a95b9 RM |
998 | /* Find the first format specifier. */ |
999 | f = lead_str_end = find_spec (format, &mbstate); | |
28f540f4 | 1000 | |
7c713e28 | 1001 | /* Lock stream. */ |
46ec036d UD |
1002 | #ifdef USE_IN_LIBIO |
1003 | __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s); | |
1004 | _IO_flockfile (s); | |
1005 | #else | |
f0f1bf85 | 1006 | __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile, s); |
d41c6f61 | 1007 | __flockfile (s); |
46ec036d | 1008 | #endif |
aa1075ea | 1009 | |
299a95b9 RM |
1010 | /* Write the literal text before the first format. */ |
1011 | outstring ((const UCHAR_T *) format, | |
1012 | lead_str_end - (const UCHAR_T *) format); | |
a04e7405 | 1013 | |
299a95b9 RM |
1014 | /* If we only have to print a simple string, return now. */ |
1015 | if (*f == L_('\0')) | |
edf5b2d7 | 1016 | goto all_done; |
a04e7405 | 1017 | |
299a95b9 RM |
1018 | /* Process whole format string. */ |
1019 | do | |
28f540f4 | 1020 | { |
299a95b9 RM |
1021 | #define REF(Name) &&do_##Name |
1022 | #define LABEL(Name) do_##Name | |
1023 | STEP0_3_TABLE; | |
1024 | STEP4_TABLE; | |
1025 | ||
b8fe19fa | 1026 | union printf_arg *args_value; /* This is not used here but ... */ |
299a95b9 RM |
1027 | int is_negative; /* Flag for negative number. */ |
1028 | union | |
1029 | { | |
1030 | unsigned long long int longlong; | |
1031 | unsigned long int word; | |
1032 | } number; | |
1033 | int base; | |
1034 | union printf_arg the_arg; | |
1035 | char *string; /* Pointer to argument string. */ | |
1036 | int alt = 0; /* Alternate format. */ | |
1037 | int space = 0; /* Use space prefix if no sign is needed. */ | |
1038 | int left = 0; /* Left-justify output. */ | |
1039 | int showsign = 0; /* Always begin with plus or minus sign. */ | |
1040 | int group = 0; /* Print numbers according grouping rules. */ | |
1041 | int is_long_double = 0; /* Argument is long double/ long long int. */ | |
1042 | int is_short = 0; /* Argument is long int. */ | |
1043 | int is_long = 0; /* Argument is short int. */ | |
cc3fa755 | 1044 | int is_char = 0; /* Argument is promoted (unsigned) char. */ |
299a95b9 RM |
1045 | int width = 0; /* Width of output; 0 means none specified. */ |
1046 | int prec = -1; /* Precision of output; -1 means none specified. */ | |
1047 | char pad = ' '; /* Padding character. */ | |
1048 | CHAR_T spec; | |
1049 | ||
1050 | /* Get current character in format string. */ | |
1051 | JUMP (*++f, step0_jumps); | |
1052 | ||
1053 | /* ' ' flag. */ | |
1054 | LABEL (flag_space): | |
1055 | space = 1; | |
1056 | JUMP (*++f, step0_jumps); | |
1057 | ||
1058 | /* '+' flag. */ | |
1059 | LABEL (flag_plus): | |
1060 | showsign = 1; | |
1061 | JUMP (*++f, step0_jumps); | |
1062 | ||
1063 | /* The '-' flag. */ | |
1064 | LABEL (flag_minus): | |
1065 | left = 1; | |
1066 | pad = L_(' '); | |
1067 | JUMP (*++f, step0_jumps); | |
1068 | ||
1069 | /* The '#' flag. */ | |
1070 | LABEL (flag_hash): | |
1071 | alt = 1; | |
1072 | JUMP (*++f, step0_jumps); | |
1073 | ||
1074 | /* The '0' flag. */ | |
1075 | LABEL (flag_zero): | |
1076 | if (!left) | |
1077 | pad = L_('0'); | |
1078 | JUMP (*++f, step0_jumps); | |
1079 | ||
1080 | /* The '\'' flag. */ | |
1081 | LABEL (flag_quote): | |
1082 | group = 1; | |
1083 | ||
1084 | /* XXX Completely wrong. Use wctob. */ | |
1085 | if (grouping == (const char *) -1) | |
28f540f4 | 1086 | { |
299a95b9 RM |
1087 | /* Figure out the thousands separator character. */ |
1088 | if (mbtowc (&thousands_sep, | |
1089 | _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP), | |
1090 | strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0) | |
1091 | thousands_sep = (wchar_t) | |
1092 | *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP); | |
1093 | grouping = _NL_CURRENT (LC_NUMERIC, GROUPING); | |
1094 | if (*grouping == '\0' || *grouping == CHAR_MAX | |
1095 | || thousands_sep == L'\0') | |
1096 | grouping = NULL; | |
28f540f4 | 1097 | } |
299a95b9 | 1098 | JUMP (*++f, step0_jumps); |
28f540f4 | 1099 | |
299a95b9 RM |
1100 | /* Get width from argument. */ |
1101 | LABEL (width_asterics): | |
1102 | { | |
1103 | const UCHAR_T *tmp; /* Temporary value. */ | |
a04e7405 | 1104 | |
299a95b9 RM |
1105 | tmp = ++f; |
1106 | if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$')) | |
a641835a | 1107 | /* The width comes from a positional parameter. */ |
299a95b9 | 1108 | goto do_positional; |
a04e7405 | 1109 | |
299a95b9 | 1110 | width = va_arg (ap, int); |
a04e7405 | 1111 | |
299a95b9 RM |
1112 | /* Negative width means left justified. */ |
1113 | if (width < 0) | |
1114 | { | |
1115 | width = -width; | |
1116 | pad = L_(' '); | |
1117 | left = 1; | |
1118 | } | |
1119 | } | |
1120 | JUMP (*f, step1_jumps); | |
1121 | ||
1122 | /* Given width in format string. */ | |
1123 | LABEL (width): | |
1124 | width = read_int (&f); | |
1125 | if (*f == L_('$')) | |
a641835a | 1126 | /* Oh, oh. The argument comes from a positional parameter. */ |
299a95b9 RM |
1127 | goto do_positional; |
1128 | JUMP (*f, step1_jumps); | |
1129 | ||
1130 | LABEL (precision): | |
1131 | ++f; | |
1132 | if (*f == L_('*')) | |
1133 | { | |
1134 | const UCHAR_T *tmp; /* Temporary value. */ | |
a04e7405 | 1135 | |
299a95b9 RM |
1136 | tmp = ++f; |
1137 | if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$')) | |
a641835a | 1138 | /* The precision comes from a positional parameter. */ |
299a95b9 | 1139 | goto do_positional; |
28f540f4 | 1140 | |
299a95b9 | 1141 | prec = va_arg (ap, int); |
28f540f4 | 1142 | |
299a95b9 RM |
1143 | /* If the precision is negative the precision is omitted. */ |
1144 | if (prec < 0) | |
1145 | prec = -1; | |
1146 | } | |
1147 | else if (ISDIGIT (*f)) | |
1148 | prec = read_int (&f); | |
1149 | else | |
1150 | prec = 0; | |
1151 | JUMP (*f, step2_jumps); | |
1152 | ||
cc3fa755 | 1153 | /* Process 'h' modifier. There might another 'h' following. */ |
299a95b9 RM |
1154 | LABEL (mod_half): |
1155 | is_short = 1; | |
cc3fa755 UD |
1156 | JUMP (*++f, step3a_jumps); |
1157 | ||
1158 | /* Process 'hh' modifier. */ | |
1159 | LABEL (mod_halfhalf): | |
1160 | is_short = 0; | |
1161 | is_char = 1; | |
299a95b9 RM |
1162 | JUMP (*++f, step4_jumps); |
1163 | ||
cc3fa755 | 1164 | /* Process 'l' modifier. There might another 'l' following. */ |
299a95b9 RM |
1165 | LABEL (mod_long): |
1166 | is_long = 1; | |
cc3fa755 | 1167 | JUMP (*++f, step3b_jumps); |
299a95b9 RM |
1168 | |
1169 | /* Process 'L', 'q', or 'll' modifier. No other modifier is | |
1170 | allowed to follow. */ | |
1171 | LABEL (mod_longlong): | |
1172 | is_long_double = 1; | |
1173 | JUMP (*++f, step4_jumps); | |
1174 | ||
1175 | LABEL (mod_size_t): | |
1176 | is_longlong = sizeof (size_t) > sizeof (unsigned long int); | |
1177 | is_long = sizeof (size_t) > sizeof (unsigned int); | |
1178 | JUMP (*++f, step4_jumps); | |
1179 | ||
299a95b9 RM |
1180 | /* Process current format. */ |
1181 | while (1) | |
28f540f4 | 1182 | { |
299a95b9 RM |
1183 | process_arg (((struct printf_spec *) NULL)); |
1184 | ||
1185 | LABEL (form_unknown): | |
1186 | if (spec == L_('\0')) | |
7c713e28 RM |
1187 | { |
1188 | /* The format string ended before the specifier is complete. */ | |
edf5b2d7 UD |
1189 | done = -1; |
1190 | goto all_done; | |
7c713e28 | 1191 | } |
299a95b9 RM |
1192 | |
1193 | /* If we are in the fast loop force entering the complicated | |
1194 | one. */ | |
1195 | goto do_positional; | |
28f540f4 | 1196 | } |
299a95b9 | 1197 | |
3e5f5557 UD |
1198 | /* The format is correctly handled. */ |
1199 | ++nspecs_done; | |
1200 | ||
299a95b9 RM |
1201 | /* Look for next format specifier. */ |
1202 | f = find_spec ((end_of_spec = ++f), &mbstate); | |
1203 | ||
1204 | /* Write the following constant string. */ | |
1205 | outstring (end_of_spec, f - end_of_spec); | |
a04e7405 | 1206 | } |
299a95b9 | 1207 | while (*f != L_('\0')); |
28f540f4 | 1208 | |
edf5b2d7 UD |
1209 | /* Unlock stream and return. */ |
1210 | goto all_done; | |
299a95b9 RM |
1211 | |
1212 | /* Here starts the more complex loop to handle positional parameters. */ | |
1213 | do_positional: | |
1214 | { | |
1215 | /* Array with information about the needed arguments. This has to | |
6d52618b | 1216 | be dynamically extensible. */ |
299a95b9 RM |
1217 | size_t nspecs = 0; |
1218 | size_t nspecs_max = 32; /* A more or less arbitrary start value. */ | |
1219 | struct printf_spec *specs | |
1220 | = alloca (nspecs_max * sizeof (struct printf_spec)); | |
1221 | ||
1222 | /* The number of arguments the format string requests. This will | |
1223 | determine the size of the array needed to store the argument | |
1224 | attributes. */ | |
1225 | size_t nargs = 0; | |
1226 | int *args_type; | |
1227 | union printf_arg *args_value; | |
1228 | ||
1229 | /* Positional parameters refer to arguments directly. This could | |
1230 | also determine the maximum number of arguments. Track the | |
1231 | maximum number. */ | |
1232 | size_t max_ref_arg = 0; | |
1233 | ||
1234 | /* Just a counter. */ | |
ba1ffaa1 | 1235 | size_t cnt; |
299a95b9 RM |
1236 | |
1237 | ||
1238 | if (grouping == (const char *) -1) | |
a04e7405 | 1239 | { |
299a95b9 RM |
1240 | /* XXX Use wctob. But this is incompatible for now. */ |
1241 | /* Figure out the thousands separator character. */ | |
1242 | if (mbtowc (&thousands_sep, | |
1243 | _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP), | |
1244 | strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0) | |
1245 | thousands_sep = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP); | |
1246 | grouping = _NL_CURRENT (LC_NUMERIC, GROUPING); | |
1247 | if (*grouping == '\0' || *grouping == CHAR_MAX | |
1248 | || thousands_sep == L'\0') | |
1249 | grouping = NULL; | |
1250 | } | |
1251 | ||
1252 | for (f = lead_str_end; *f != '\0'; f = specs[nspecs++].next_fmt) | |
1253 | { | |
1254 | if (nspecs >= nspecs_max) | |
1255 | { | |
1256 | /* Extend the array of format specifiers. */ | |
1257 | struct printf_spec *old = specs; | |
1258 | ||
1259 | nspecs_max *= 2; | |
1260 | specs = alloca (nspecs_max * sizeof (struct printf_spec)); | |
1261 | ||
1262 | if (specs == &old[nspecs]) | |
1263 | /* Stack grows up, OLD was the last thing allocated; | |
1264 | extend it. */ | |
1265 | nspecs_max += nspecs_max / 2; | |
1266 | else | |
1267 | { | |
1268 | /* Copy the old array's elements to the new space. */ | |
1269 | memcpy (specs, old, nspecs * sizeof (struct printf_spec)); | |
1270 | if (old == &specs[nspecs]) | |
1271 | /* Stack grows down, OLD was just below the new | |
1272 | SPECS. We can use that space when the new space | |
1273 | runs out. */ | |
1274 | nspecs_max += nspecs_max / 2; | |
1275 | } | |
1276 | } | |
1277 | ||
1278 | /* Parse the format specifier. */ | |
ec42724d RM |
1279 | nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg, |
1280 | &mbstate); | |
299a95b9 RM |
1281 | } |
1282 | ||
1283 | /* Determine the number of arguments the format string consumes. */ | |
1284 | nargs = MAX (nargs, max_ref_arg); | |
1285 | ||
1286 | /* Allocate memory for the argument descriptions. */ | |
1287 | args_type = alloca (nargs * sizeof (int)); | |
1288 | memset (args_type, 0, nargs * sizeof (int)); | |
1289 | args_value = alloca (nargs * sizeof (union printf_arg)); | |
1290 | ||
1291 | /* XXX Could do sanity check here: If any element in ARGS_TYPE is | |
1292 | still zero after this loop, format is invalid. For now we | |
1293 | simply use 0 as the value. */ | |
1294 | ||
1295 | /* Fill in the types of all the arguments. */ | |
1296 | for (cnt = 0; cnt < nspecs; ++cnt) | |
1297 | { | |
1298 | /* If the width is determined by an argument this is an int. */ | |
1299 | if (specs[cnt].width_arg != -1) | |
1300 | args_type[specs[cnt].width_arg] = PA_INT; | |
1301 | ||
1302 | /* If the precision is determined by an argument this is an int. */ | |
1303 | if (specs[cnt].prec_arg != -1) | |
1304 | args_type[specs[cnt].prec_arg] = PA_INT; | |
1305 | ||
1306 | switch (specs[cnt].ndata_args) | |
1307 | { | |
1308 | case 0: /* No arguments. */ | |
1309 | break; | |
1310 | case 1: /* One argument; we already have the type. */ | |
1311 | args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type; | |
1312 | break; | |
1313 | default: | |
1314 | /* We have more than one argument for this format spec. | |
1315 | We must call the arginfo function again to determine | |
1316 | all the types. */ | |
1317 | (void) (*__printf_arginfo_table[specs[cnt].info.spec]) | |
1318 | (&specs[cnt].info, | |
1319 | specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]); | |
1320 | break; | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | /* Now we know all the types and the order. Fill in the argument | |
1325 | values. */ | |
7cc27f44 | 1326 | for (cnt = 0; cnt < nargs; ++cnt) |
299a95b9 RM |
1327 | switch (args_type[cnt]) |
1328 | { | |
a04e7405 | 1329 | #define T(tag, mem, type) \ |
299a95b9 | 1330 | case tag: \ |
7cc27f44 | 1331 | args_value[cnt].mem = va_arg (ap_save, type); \ |
299a95b9 | 1332 | break |
a04e7405 RM |
1333 | |
1334 | T (PA_CHAR, pa_char, int); /* Promoted. */ | |
2c6fe0bd | 1335 | T (PA_WCHAR, pa_wchar, wint_t); |
a04e7405 RM |
1336 | T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */ |
1337 | T (PA_INT, pa_int, int); | |
1338 | T (PA_INT|PA_FLAG_LONG, pa_long_int, long int); | |
1339 | T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int); | |
1340 | T (PA_FLOAT, pa_float, double); /* Promoted. */ | |
1341 | T (PA_DOUBLE, pa_double, double); | |
1342 | T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double); | |
1343 | T (PA_STRING, pa_string, const char *); | |
2c6fe0bd | 1344 | T (PA_WSTRING, pa_wstring, const wchar_t *); |
a04e7405 RM |
1345 | T (PA_POINTER, pa_pointer, void *); |
1346 | #undef T | |
299a95b9 RM |
1347 | default: |
1348 | if ((args_type[cnt] & PA_FLAG_PTR) != 0) | |
7cc27f44 | 1349 | args_value[cnt].pa_pointer = va_arg (ap_save, void *); |
299a95b9 RM |
1350 | else |
1351 | args_value[cnt].pa_long_double = 0.0; | |
1352 | break; | |
1353 | } | |
a04e7405 | 1354 | |
299a95b9 | 1355 | /* Now walk through all format specifiers and process them. */ |
ba1ffaa1 | 1356 | for (; (size_t) nspecs_done < nspecs; ++nspecs_done) |
299a95b9 RM |
1357 | { |
1358 | #undef REF | |
1359 | #define REF(Name) &&do2_##Name | |
1360 | #undef LABEL | |
1361 | #define LABEL(Name) do2_##Name | |
1362 | STEP4_TABLE; | |
1363 | ||
1364 | int is_negative; | |
1365 | union | |
28f540f4 | 1366 | { |
299a95b9 RM |
1367 | unsigned long long int longlong; |
1368 | unsigned long int word; | |
1369 | } number; | |
1370 | int base; | |
1371 | union printf_arg the_arg; | |
1372 | char *string; /* Pointer to argument string. */ | |
1373 | ||
1374 | /* Fill variables from values in struct. */ | |
1375 | int alt = specs[nspecs_done].info.alt; | |
1376 | int space = specs[nspecs_done].info.space; | |
1377 | int left = specs[nspecs_done].info.left; | |
1378 | int showsign = specs[nspecs_done].info.showsign; | |
1379 | int group = specs[nspecs_done].info.group; | |
1380 | int is_long_double = specs[nspecs_done].info.is_long_double; | |
6591c335 UD |
1381 | int is_short = specs[nspecs_done].info.is_short; |
1382 | int is_char = specs[nspecs_done].info.is_char; | |
299a95b9 RM |
1383 | int is_long = specs[nspecs_done].info.is_long; |
1384 | int width = specs[nspecs_done].info.width; | |
1385 | int prec = specs[nspecs_done].info.prec; | |
1386 | char pad = specs[nspecs_done].info.pad; | |
1387 | CHAR_T spec = specs[nspecs_done].info.spec; | |
1388 | ||
1389 | /* Fill in last information. */ | |
1390 | if (specs[nspecs_done].width_arg != -1) | |
1391 | { | |
1392 | /* Extract the field width from an argument. */ | |
1393 | specs[nspecs_done].info.width = | |
1394 | args_value[specs[nspecs_done].width_arg].pa_int; | |
1395 | ||
1396 | if (specs[nspecs_done].info.width < 0) | |
1397 | /* If the width value is negative left justification is | |
1398 | selected and the value is taken as being positive. */ | |
1399 | { | |
1400 | specs[nspecs_done].info.width *= -1; | |
1401 | left = specs[nspecs_done].info.left = 1; | |
1402 | } | |
1403 | width = specs[nspecs_done].info.width; | |
1404 | } | |
a04e7405 | 1405 | |
299a95b9 RM |
1406 | if (specs[nspecs_done].prec_arg != -1) |
1407 | { | |
1408 | /* Extract the precision from an argument. */ | |
1409 | specs[nspecs_done].info.prec = | |
1410 | args_value[specs[nspecs_done].prec_arg].pa_int; | |
28f540f4 | 1411 | |
299a95b9 RM |
1412 | if (specs[nspecs_done].info.prec < 0) |
1413 | /* If the precision is negative the precision is | |
1414 | omitted. */ | |
1415 | specs[nspecs_done].info.prec = -1; | |
a04e7405 | 1416 | |
299a95b9 RM |
1417 | prec = specs[nspecs_done].info.prec; |
1418 | } | |
28f540f4 | 1419 | |
299a95b9 RM |
1420 | /* Process format specifiers. */ |
1421 | while (1) | |
1422 | { | |
1423 | JUMP (spec, step4_jumps); | |
28f540f4 | 1424 | |
299a95b9 | 1425 | process_arg ((&specs[nspecs_done])); |
28f540f4 | 1426 | |
299a95b9 RM |
1427 | LABEL (form_unknown): |
1428 | { | |
1429 | extern printf_function **__printf_function_table; | |
1430 | int function_done; | |
1431 | printf_function *function; | |
1432 | unsigned int i; | |
1433 | const void **ptr; | |
28f540f4 | 1434 | |
299a95b9 RM |
1435 | function = |
1436 | (__printf_function_table == NULL ? NULL : | |
1437 | __printf_function_table[specs[nspecs_done].info.spec]); | |
28f540f4 | 1438 | |
299a95b9 RM |
1439 | if (function == NULL) |
1440 | function = &printf_unknown; | |
a04e7405 | 1441 | |
299a95b9 RM |
1442 | ptr = alloca (specs[nspecs_done].ndata_args |
1443 | * sizeof (const void *)); | |
1444 | ||
1445 | /* Fill in an array of pointers to the argument values. */ | |
1446 | for (i = 0; i < specs[nspecs_done].ndata_args; ++i) | |
1447 | ptr[i] = &args_value[specs[nspecs_done].data_arg + i]; | |
28f540f4 | 1448 | |
299a95b9 RM |
1449 | /* Call the function. */ |
1450 | function_done = (*function) (s, &specs[nspecs_done].info, ptr); | |
1451 | ||
6d52618b | 1452 | /* If an error occurred we don't have information about # |
299a95b9 RM |
1453 | of chars. */ |
1454 | if (function_done < 0) | |
7c713e28 | 1455 | { |
edf5b2d7 UD |
1456 | done = -1; |
1457 | goto all_done; | |
7c713e28 | 1458 | } |
299a95b9 RM |
1459 | |
1460 | done += function_done; | |
1461 | } | |
1462 | break; | |
28f540f4 | 1463 | } |
28f540f4 | 1464 | |
299a95b9 RM |
1465 | /* Write the following constant string. */ |
1466 | outstring (specs[nspecs_done].end_of_fmt, | |
1467 | specs[nspecs_done].next_fmt | |
1468 | - specs[nspecs_done].end_of_fmt); | |
1469 | } | |
1470 | } | |
28f540f4 | 1471 | |
edf5b2d7 | 1472 | all_done: |
7c713e28 | 1473 | /* Unlock the stream. */ |
edf5b2d7 | 1474 | __libc_cleanup_region_end (1); |
aa1075ea | 1475 | |
28f540f4 RM |
1476 | return done; |
1477 | } | |
1478 | ||
42d2676e | 1479 | #ifdef USE_IN_LIBIO |
77a58cad RM |
1480 | # undef vfprintf |
1481 | # ifdef strong_alias | |
1482 | /* This is for glibc. */ | |
299a95b9 | 1483 | strong_alias (_IO_vfprintf, vfprintf); |
77a58cad RM |
1484 | # else |
1485 | # if defined __ELF__ || defined __GNU_LIBRARY__ | |
1486 | # include <gnu-stabs.h> | |
1487 | # ifdef weak_alias | |
1488 | weak_alias (_IO_vfprintf, vfprintf); | |
1489 | # endif | |
1490 | # endif | |
1491 | # endif | |
42d2676e | 1492 | #endif |
299a95b9 | 1493 | \f |
a04e7405 RM |
1494 | /* Handle an unknown format specifier. This prints out a canonicalized |
1495 | representation of the format spec itself. */ | |
28f540f4 | 1496 | static int |
299a95b9 RM |
1497 | printf_unknown (FILE *s, const struct printf_info *info, |
1498 | const void *const *args) | |
1499 | ||
28f540f4 RM |
1500 | { |
1501 | int done = 0; | |
299a95b9 | 1502 | char work_buffer[BUFSIZ]; |
28f540f4 | 1503 | register char *w; |
28f540f4 | 1504 | |
a04e7405 | 1505 | outchar ('%'); |
28f540f4 RM |
1506 | |
1507 | if (info->alt) | |
1508 | outchar ('#'); | |
1509 | if (info->group) | |
1510 | outchar ('\''); | |
1511 | if (info->showsign) | |
1512 | outchar ('+'); | |
1513 | else if (info->space) | |
1514 | outchar (' '); | |
1515 | if (info->left) | |
1516 | outchar ('-'); | |
1517 | if (info->pad == '0') | |
1518 | outchar ('0'); | |
1519 | ||
a04e7405 | 1520 | if (info->width != 0) |
28f540f4 | 1521 | { |
299a95b9 | 1522 | w = _itoa_word (info->width, workend + 1, 10, 0); |
2f6d1f1b UD |
1523 | while (w <= workend) |
1524 | outchar (*w++); | |
28f540f4 | 1525 | } |
28f540f4 RM |
1526 | |
1527 | if (info->prec != -1) | |
1528 | { | |
a04e7405 | 1529 | outchar ('.'); |
299a95b9 | 1530 | w = _itoa_word (info->prec, workend + 1, 10, 0); |
2f6d1f1b UD |
1531 | while (w <= workend) |
1532 | outchar (*w++); | |
28f540f4 RM |
1533 | } |
1534 | ||
a04e7405 RM |
1535 | if (info->spec != '\0') |
1536 | outchar (info->spec); | |
28f540f4 RM |
1537 | |
1538 | return done; | |
1539 | } | |
1540 | \f | |
1541 | /* Group the digits according to the grouping rules of the current locale. | |
1542 | The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */ | |
28f540f4 | 1543 | static char * |
dfd2257a | 1544 | internal_function |
299a95b9 | 1545 | group_number (CHAR_T *w, CHAR_T *rear_ptr, const CHAR_T *grouping, |
28f540f4 RM |
1546 | wchar_t thousands_sep) |
1547 | { | |
1548 | int len; | |
1549 | char *src, *s; | |
1550 | ||
1551 | /* We treat all negative values like CHAR_MAX. */ | |
1552 | ||
feb3c934 | 1553 | if (*grouping == CHAR_MAX || *grouping <= 0) |
28f540f4 RM |
1554 | /* No grouping should be done. */ |
1555 | return w; | |
1556 | ||
1557 | len = *grouping; | |
1558 | ||
1559 | /* Copy existing string so that nothing gets overwritten. */ | |
299a95b9 | 1560 | src = (char *) alloca (rear_ptr - w); |
86187531 | 1561 | s = (char *) __mempcpy (src, w + 1, rear_ptr - w) - 1; |
299a95b9 | 1562 | w = rear_ptr; |
28f540f4 RM |
1563 | |
1564 | /* Process all characters in the string. */ | |
1565 | while (s >= src) | |
1566 | { | |
1567 | *w-- = *s--; | |
1568 | ||
1569 | if (--len == 0 && s >= src) | |
1570 | { | |
1571 | /* A new group begins. */ | |
1572 | *w-- = thousands_sep; | |
1573 | ||
1574 | len = *grouping++; | |
1575 | if (*grouping == '\0') | |
1576 | /* The previous grouping repeats ad infinitum. */ | |
1577 | --grouping; | |
60c96635 UD |
1578 | else if (*grouping == CHAR_MAX |
1579 | #if CHAR_MIN < 0 | |
1580 | || *grouping < 0 | |
1581 | #endif | |
1582 | ) | |
28f540f4 RM |
1583 | { |
1584 | /* No further grouping to be done. | |
1585 | Copy the rest of the number. */ | |
1586 | do | |
1587 | *w-- = *s--; | |
1588 | while (s >= src); | |
1589 | break; | |
1590 | } | |
1591 | } | |
1592 | } | |
28f540f4 RM |
1593 | return w; |
1594 | } | |
1595 | \f | |
1596 | #ifdef USE_IN_LIBIO | |
1597 | /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */ | |
1598 | struct helper_file | |
1599 | { | |
1600 | struct _IO_FILE_plus _f; | |
1601 | _IO_FILE *_put_stream; | |
c4029823 UD |
1602 | #ifdef _IO_MTSAFE_IO |
1603 | _IO_lock_t lock; | |
1604 | #endif | |
28f540f4 RM |
1605 | }; |
1606 | ||
1607 | static int | |
522548fb | 1608 | _IO_helper_overflow (_IO_FILE *s, int c) |
28f540f4 RM |
1609 | { |
1610 | _IO_FILE *target = ((struct helper_file*) s)->_put_stream; | |
1611 | int used = s->_IO_write_ptr - s->_IO_write_base; | |
1612 | if (used) | |
1613 | { | |
1614 | _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used); | |
1615 | s->_IO_write_ptr -= written; | |
1616 | } | |
7c713e28 | 1617 | return PUTC (c, s); |
28f540f4 RM |
1618 | } |
1619 | ||
1620 | static const struct _IO_jump_t _IO_helper_jumps = | |
299a95b9 RM |
1621 | { |
1622 | JUMP_INIT_DUMMY, | |
1623 | JUMP_INIT (finish, _IO_default_finish), | |
1624 | JUMP_INIT (overflow, _IO_helper_overflow), | |
1625 | JUMP_INIT (underflow, _IO_default_underflow), | |
1626 | JUMP_INIT (uflow, _IO_default_uflow), | |
1627 | JUMP_INIT (pbackfail, _IO_default_pbackfail), | |
1628 | JUMP_INIT (xsputn, _IO_default_xsputn), | |
1629 | JUMP_INIT (xsgetn, _IO_default_xsgetn), | |
1630 | JUMP_INIT (seekoff, _IO_default_seekoff), | |
1631 | JUMP_INIT (seekpos, _IO_default_seekpos), | |
1632 | JUMP_INIT (setbuf, _IO_default_setbuf), | |
1633 | JUMP_INIT (sync, _IO_default_sync), | |
1634 | JUMP_INIT (doallocate, _IO_default_doallocate), | |
1635 | JUMP_INIT (read, _IO_default_read), | |
1636 | JUMP_INIT (write, _IO_default_write), | |
1637 | JUMP_INIT (seek, _IO_default_seek), | |
1638 | JUMP_INIT (close, _IO_default_close), | |
1639 | JUMP_INIT (stat, _IO_default_stat) | |
1640 | }; | |
28f540f4 RM |
1641 | |
1642 | static int | |
dfd2257a | 1643 | internal_function |
299a95b9 RM |
1644 | buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format, |
1645 | _IO_va_list args) | |
28f540f4 RM |
1646 | { |
1647 | char buf[_IO_BUFSIZ]; | |
1648 | struct helper_file helper; | |
1649 | register _IO_FILE *hp = (_IO_FILE *) &helper; | |
1650 | int result, to_flush; | |
1651 | ||
1652 | /* Initialize helper. */ | |
1653 | helper._put_stream = s; | |
1654 | hp->_IO_write_base = buf; | |
1655 | hp->_IO_write_ptr = buf; | |
1656 | hp->_IO_write_end = buf + sizeof buf; | |
1657 | hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS; | |
bd355af0 UD |
1658 | #if _IO_JUMPS_OFFSET |
1659 | hp->_vtable_offset = 0; | |
1660 | #endif | |
c4029823 UD |
1661 | #ifdef _IO_MTSAFE_IO |
1662 | hp->_lock = &helper.lock; | |
84384f5b | 1663 | __libc_lock_init (*hp->_lock); |
c4029823 | 1664 | #endif |
96aa2d94 RM |
1665 | _IO_JUMPS (hp) = (struct _IO_jump_t *) &_IO_helper_jumps; |
1666 | ||
28f540f4 RM |
1667 | /* Now print to helper instead. */ |
1668 | result = _IO_vfprintf (hp, format, args); | |
1669 | ||
1670 | /* Now flush anything from the helper to the S. */ | |
1671 | if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0) | |
1672 | { | |
ba1ffaa1 | 1673 | if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush) |
28f540f4 RM |
1674 | return -1; |
1675 | } | |
1676 | ||
1677 | return result; | |
1678 | } | |
1679 | ||
1680 | #else /* !USE_IN_LIBIO */ | |
1681 | ||
1682 | static int | |
dfd2257a | 1683 | internal_function |
299a95b9 | 1684 | buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args) |
28f540f4 RM |
1685 | { |
1686 | char buf[BUFSIZ]; | |
1687 | int result; | |
1688 | ||
1689 | s->__bufp = s->__buffer = buf; | |
1690 | s->__bufsize = sizeof buf; | |
1691 | s->__put_limit = s->__buffer + s->__bufsize; | |
1692 | s->__get_limit = s->__buffer; | |
1693 | ||
1694 | /* Now use buffer to print. */ | |
1695 | result = vfprintf (s, format, args); | |
1696 | ||
1697 | if (fflush (s) == EOF) | |
c21ec326 | 1698 | result = -1; |
28f540f4 RM |
1699 | s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL; |
1700 | s->__bufsize = 0; | |
1701 | ||
1702 | return result; | |
1703 | } | |
299a95b9 | 1704 | \f |
28f540f4 RM |
1705 | /* Pads string with given number of a specified character. |
1706 | This code is taken from iopadn.c of the GNU I/O library. */ | |
1707 | #define PADSIZE 16 | |
299a95b9 RM |
1708 | static const CHAR_T blanks[PADSIZE] = |
1709 | { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), | |
1710 | L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') }; | |
1711 | static const CHAR_T zeroes[PADSIZE] = | |
1712 | { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), | |
1713 | L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') }; | |
28f540f4 RM |
1714 | |
1715 | ssize_t | |
299a95b9 RM |
1716 | #ifndef COMPILE_WPRINTF |
1717 | __printf_pad (FILE *s, char pad, size_t count) | |
1718 | #else | |
1719 | __wprintf_pad (FILE *s, wchar_t pad, size_t count) | |
1720 | #endif | |
28f540f4 | 1721 | { |
299a95b9 | 1722 | const CHAR_T *padptr; |
a04e7405 | 1723 | register size_t i; |
28f540f4 | 1724 | |
299a95b9 | 1725 | padptr = pad == L_(' ') ? blanks : zeroes; |
28f540f4 RM |
1726 | |
1727 | for (i = count; i >= PADSIZE; i -= PADSIZE) | |
a04e7405 RM |
1728 | if (PUT (s, padptr, PADSIZE) != PADSIZE) |
1729 | return -1; | |
28f540f4 | 1730 | if (i > 0) |
a04e7405 RM |
1731 | if (PUT (s, padptr, i) != i) |
1732 | return -1; | |
1733 | ||
1734 | return count; | |
28f540f4 RM |
1735 | } |
1736 | #undef PADSIZE | |
1737 | #endif /* USE_IN_LIBIO */ |