]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdio-common/vfprintf-internal.c
sparc (64bit): Regenerate ulps
[thirdparty/glibc.git] / stdio-common / vfprintf-internal.c
1 /* Copyright (C) 1991-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18 #include <array_length.h>
19 #include <assert.h>
20 #include <ctype.h>
21 #include <limits.h>
22 #include <printf.h>
23 #include <stdarg.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <wchar.h>
29 #include <libc-lock.h>
30 #include <sys/param.h>
31 #include <_itoa.h>
32 #include <locale/localeinfo.h>
33 #include <grouping_iterator.h>
34 #include <stdio.h>
35 #include <scratch_buffer.h>
36 #include <intprops.h>
37 #include <printf_buffer.h>
38 #include <printf_buffer_to_file.h>
39
40 /* This code is shared between the standard stdio implementation found
41 in GNU C library and the libio implementation originally found in
42 GNU libg++.
43
44 Beside this it is also shared between the normal and wide character
45 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
46
47 #include <libioP.h>
48
49 #ifdef COMPILE_WPRINTF
50 #include <wctype.h>
51 #endif
52
53 #define ARGCHECK(S, Format) \
54 do \
55 { \
56 /* Check file argument for consistence. */ \
57 CHECK_FILE (S, -1); \
58 if (S->_flags & _IO_NO_WRITES) \
59 { \
60 S->_flags |= _IO_ERR_SEEN; \
61 __set_errno (EBADF); \
62 return -1; \
63 } \
64 if (Format == NULL) \
65 { \
66 __set_errno (EINVAL); \
67 return -1; \
68 } \
69 } while (0)
70 #define UNBUFFERED_P(S) ((S)->_flags & _IO_UNBUFFERED)
71
72 #if __HAVE_FLOAT128_UNLIKE_LDBL
73 # define PARSE_FLOAT_VA_ARG_EXTENDED(INFO) \
74 do \
75 { \
76 if (is_long_double \
77 && (mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0) \
78 { \
79 INFO.is_binary128 = 1; \
80 the_arg.pa_float128 = va_arg (ap, _Float128); \
81 } \
82 else \
83 { \
84 PARSE_FLOAT_VA_ARG (INFO); \
85 } \
86 } \
87 while (0)
88 #else
89 # define PARSE_FLOAT_VA_ARG_EXTENDED(INFO) \
90 PARSE_FLOAT_VA_ARG (INFO);
91 #endif
92
93 #define PARSE_FLOAT_VA_ARG(INFO) \
94 do \
95 { \
96 INFO.is_binary128 = 0; \
97 if (is_long_double) \
98 the_arg.pa_long_double = va_arg (ap, long double); \
99 else \
100 the_arg.pa_double = va_arg (ap, double); \
101 } \
102 while (0)
103
104 #if __HAVE_FLOAT128_UNLIKE_LDBL
105 # define SETUP_FLOAT128_INFO(INFO) \
106 do \
107 { \
108 if ((mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0) \
109 INFO.is_binary128 = is_long_double; \
110 else \
111 INFO.is_binary128 = 0; \
112 } \
113 while (0)
114 #else
115 # define SETUP_FLOAT128_INFO(INFO) \
116 do \
117 { \
118 INFO.is_binary128 = 0; \
119 } \
120 while (0)
121 #endif
122
123 #ifndef COMPILE_WPRINTF
124 # include "printf_buffer-char.h"
125 # define vfprintf __vfprintf_internal
126 # define OTHER_CHAR_T wchar_t
127 # define UCHAR_T unsigned char
128 # define INT_T int
129 typedef const char *THOUSANDS_SEP_T;
130 # define L_(Str) Str
131 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
132 # define STR_LEN(Str) strlen (Str)
133
134 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
135 return -1
136 # define CONVERT_FROM_OTHER_STRING __wcsrtombs
137 #else
138 # include "printf_buffer-wchar_t.h"
139 # define vfprintf __vfwprintf_internal
140 # define OTHER_CHAR_T char
141 /* This is a hack!!! There should be a type uwchar_t. */
142 # define UCHAR_T unsigned int /* uwchar_t */
143 # define INT_T wint_t
144 typedef wchar_t THOUSANDS_SEP_T;
145 # define L_(Str) L##Str
146 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
147 # define STR_LEN(Str) __wcslen (Str)
148
149 # include <_itowa.h>
150
151 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
152 # define CONVERT_FROM_OTHER_STRING __mbsrtowcs
153
154 # undef _itoa
155 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
156 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
157 # undef EOF
158 # define EOF WEOF
159 #endif
160
161 /* Include the shared code for parsing the format string. */
162 #include "printf-parse.h"
163
164
165 /* Write the string SRC to S. If PREC is non-negative, write at most
166 PREC bytes. If LEFT is true, perform left justification. */
167 static void
168 outstring_converted_wide_string (struct Xprintf_buffer *target,
169 const OTHER_CHAR_T *src, int prec,
170 int width, bool left)
171 {
172 /* Use a small buffer to combine processing of multiple characters.
173 CONVERT_FROM_OTHER_STRING expects the buffer size in (wide)
174 characters, and buf_length counts that. */
175 enum { buf_length = 256 / sizeof (CHAR_T) };
176 CHAR_T buf[buf_length];
177 _Static_assert (sizeof (buf) > MB_LEN_MAX,
178 "buffer is large enough for a single multi-byte character");
179
180 /* Add the initial padding if needed. */
181 if (width > 0 && !left)
182 {
183 /* Make a first pass to find the output width, so that we can
184 add the required padding. */
185 mbstate_t mbstate = { 0 };
186 const OTHER_CHAR_T *src_copy = src;
187 size_t total_written;
188 if (prec < 0)
189 total_written = CONVERT_FROM_OTHER_STRING
190 (NULL, &src_copy, 0, &mbstate);
191 else
192 {
193 /* The source might not be null-terminated. Enforce the
194 limit manually, based on the output length. */
195 total_written = 0;
196 size_t limit = prec;
197 while (limit > 0 && src_copy != NULL)
198 {
199 size_t write_limit = buf_length;
200 if (write_limit > limit)
201 write_limit = limit;
202 size_t written = CONVERT_FROM_OTHER_STRING
203 (buf, &src_copy, write_limit, &mbstate);
204 if (written == (size_t) -1)
205 {
206 Xprintf_buffer_mark_failed (target);
207 return;
208 }
209 if (written == 0)
210 break;
211 total_written += written;
212 limit -= written;
213 }
214 }
215
216 /* Output initial padding. */
217 Xprintf_buffer_pad (target, L_(' '), width - total_written);
218 if (Xprintf_buffer_has_failed (target))
219 return;
220 }
221
222 /* Convert the input string, piece by piece. */
223 size_t total_written = 0;
224 {
225 mbstate_t mbstate = { 0 };
226 /* If prec is negative, remaining is not decremented, otherwise,
227 it serves as the write limit. */
228 size_t remaining = -1;
229 if (prec >= 0)
230 remaining = prec;
231 while (remaining > 0 && src != NULL)
232 {
233 size_t write_limit = buf_length;
234 if (remaining < write_limit)
235 write_limit = remaining;
236 size_t written = CONVERT_FROM_OTHER_STRING
237 (buf, &src, write_limit, &mbstate);
238 if (written == (size_t) -1)
239 {
240 Xprintf_buffer_mark_failed (target);
241 return;
242 }
243 if (written == 0)
244 break;
245 Xprintf_buffer_write (target, buf, written);
246 total_written += written;
247 if (prec >= 0)
248 remaining -= written;
249 }
250 }
251
252 /* Add final padding. */
253 if (width > 0 && left)
254 Xprintf_buffer_pad (target, L_(' '), width - total_written);
255 }
256
257 /* Calls __printf_fp or __printf_fphex based on the value of the
258 format specifier INFO->spec. */
259 static inline void
260 __printf_fp_spec (struct Xprintf_buffer *target,
261 const struct printf_info *info, const void *const *args)
262 {
263 if (info->spec == 'a' || info->spec == 'A')
264 Xprintf (fphex_l_buffer) (target, _NL_CURRENT_LOCALE, info, args);
265 else
266 Xprintf (fp_l_buffer) (target, _NL_CURRENT_LOCALE, info, args);
267 }
268
269 /* For handling long_double and longlong we use the same flag. If
270 `long' and `long long' are effectively the same type define it to
271 zero. */
272 #if LONG_MAX == LONG_LONG_MAX
273 # define is_longlong 0
274 #else
275 # define is_longlong is_long_double
276 #endif
277
278 /* If `long' and `int' is effectively the same type we don't have to
279 handle `long separately. */
280 #if INT_MAX == LONG_MAX
281 # define is_long_num 0
282 #else
283 # define is_long_num is_long
284 #endif
285
286
287 /* Global constants. */
288 static const CHAR_T null[] = L_("(null)");
289
290 /* Size of the work_buffer variable (in characters, not bytes. */
291 enum { WORK_BUFFER_SIZE = 1000 / sizeof (CHAR_T) };
292
293 /* This table maps a character into a number representing a class. In
294 each step there is a destination label for each class. */
295 static const uint8_t jump_table[] =
296 {
297 /* ' ' */ 1, 0, 0, /* '#' */ 4,
298 0, /* '%' */ 14, 0, /* '\''*/ 6,
299 0, 0, /* '*' */ 7, /* '+' */ 2,
300 0, /* '-' */ 3, /* '.' */ 9, 0,
301 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
302 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
303 /* '8' */ 8, /* '9' */ 8, 0, 0,
304 0, 0, 0, 0,
305 0, /* 'A' */ 26, /* 'B' */ 30, /* 'C' */ 25,
306 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
307 0, /* 'I' */ 29, 0, 0,
308 /* 'L' */ 12, 0, 0, 0,
309 0, 0, 0, /* 'S' */ 21,
310 0, 0, 0, 0,
311 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
312 0, 0, 0, 0,
313 0, /* 'a' */ 26, /* 'b' */ 30, /* 'c' */ 20,
314 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
315 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
316 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
317 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
318 /* 't' */ 27, /* 'u' */ 16, 0, 0,
319 /* 'x' */ 18, 0, /* 'z' */ 13
320 };
321
322 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
323 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
324 #define LABEL(Name) do_##Name
325 #ifdef SHARED
326 /* 'int' is enough and it saves some space on 64 bit systems. */
327 # define JUMP_TABLE_TYPE const int
328 # define JUMP_TABLE_BASE_LABEL do_form_unknown
329 # define REF(Name) &&do_##Name - &&JUMP_TABLE_BASE_LABEL
330 # define JUMP(ChExpr, table) \
331 do \
332 { \
333 int offset; \
334 void *ptr; \
335 spec = (ChExpr); \
336 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
337 : table[CHAR_CLASS (spec)]; \
338 ptr = &&JUMP_TABLE_BASE_LABEL + offset; \
339 goto *ptr; \
340 } \
341 while (0)
342 #else
343 # define JUMP_TABLE_TYPE const void *const
344 # define REF(Name) &&do_##Name
345 # define JUMP(ChExpr, table) \
346 do \
347 { \
348 const void *ptr; \
349 spec = (ChExpr); \
350 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
351 : table[CHAR_CLASS (spec)]; \
352 goto *ptr; \
353 } \
354 while (0)
355 #endif
356
357 #define STEP0_3_TABLE \
358 /* Step 0: at the beginning. */ \
359 static JUMP_TABLE_TYPE step0_jumps[31] = \
360 { \
361 REF (form_unknown), \
362 REF (flag_space), /* for ' ' */ \
363 REF (flag_plus), /* for '+' */ \
364 REF (flag_minus), /* for '-' */ \
365 REF (flag_hash), /* for '<hash>' */ \
366 REF (flag_zero), /* for '0' */ \
367 REF (flag_quote), /* for '\'' */ \
368 REF (width_asterics), /* for '*' */ \
369 REF (width), /* for '1'...'9' */ \
370 REF (precision), /* for '.' */ \
371 REF (mod_half), /* for 'h' */ \
372 REF (mod_long), /* for 'l' */ \
373 REF (mod_longlong), /* for 'L', 'q' */ \
374 REF (mod_size_t), /* for 'z', 'Z' */ \
375 REF (form_percent), /* for '%' */ \
376 REF (form_integer), /* for 'd', 'i' */ \
377 REF (form_unsigned), /* for 'u' */ \
378 REF (form_octal), /* for 'o' */ \
379 REF (form_hexa), /* for 'X', 'x' */ \
380 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
381 REF (form_character), /* for 'c' */ \
382 REF (form_string), /* for 's', 'S' */ \
383 REF (form_pointer), /* for 'p' */ \
384 REF (form_number), /* for 'n' */ \
385 REF (form_strerror), /* for 'm' */ \
386 REF (form_wcharacter), /* for 'C' */ \
387 REF (form_floathex), /* for 'A', 'a' */ \
388 REF (mod_ptrdiff_t), /* for 't' */ \
389 REF (mod_intmax_t), /* for 'j' */ \
390 REF (flag_i18n), /* for 'I' */ \
391 REF (form_binary), /* for 'B', 'b' */ \
392 }; \
393 /* Step 1: after processing width. */ \
394 static JUMP_TABLE_TYPE step1_jumps[31] = \
395 { \
396 REF (form_unknown), \
397 REF (form_unknown), /* for ' ' */ \
398 REF (form_unknown), /* for '+' */ \
399 REF (form_unknown), /* for '-' */ \
400 REF (form_unknown), /* for '<hash>' */ \
401 REF (form_unknown), /* for '0' */ \
402 REF (form_unknown), /* for '\'' */ \
403 REF (form_unknown), /* for '*' */ \
404 REF (form_unknown), /* for '1'...'9' */ \
405 REF (precision), /* for '.' */ \
406 REF (mod_half), /* for 'h' */ \
407 REF (mod_long), /* for 'l' */ \
408 REF (mod_longlong), /* for 'L', 'q' */ \
409 REF (mod_size_t), /* for 'z', 'Z' */ \
410 REF (form_percent), /* for '%' */ \
411 REF (form_integer), /* for 'd', 'i' */ \
412 REF (form_unsigned), /* for 'u' */ \
413 REF (form_octal), /* for 'o' */ \
414 REF (form_hexa), /* for 'X', 'x' */ \
415 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
416 REF (form_character), /* for 'c' */ \
417 REF (form_string), /* for 's', 'S' */ \
418 REF (form_pointer), /* for 'p' */ \
419 REF (form_number), /* for 'n' */ \
420 REF (form_strerror), /* for 'm' */ \
421 REF (form_wcharacter), /* for 'C' */ \
422 REF (form_floathex), /* for 'A', 'a' */ \
423 REF (mod_ptrdiff_t), /* for 't' */ \
424 REF (mod_intmax_t), /* for 'j' */ \
425 REF (form_unknown), /* for 'I' */ \
426 REF (form_binary), /* for 'B', 'b' */ \
427 }; \
428 /* Step 2: after processing precision. */ \
429 static JUMP_TABLE_TYPE step2_jumps[31] = \
430 { \
431 REF (form_unknown), \
432 REF (form_unknown), /* for ' ' */ \
433 REF (form_unknown), /* for '+' */ \
434 REF (form_unknown), /* for '-' */ \
435 REF (form_unknown), /* for '<hash>' */ \
436 REF (form_unknown), /* for '0' */ \
437 REF (form_unknown), /* for '\'' */ \
438 REF (form_unknown), /* for '*' */ \
439 REF (form_unknown), /* for '1'...'9' */ \
440 REF (form_unknown), /* for '.' */ \
441 REF (mod_half), /* for 'h' */ \
442 REF (mod_long), /* for 'l' */ \
443 REF (mod_longlong), /* for 'L', 'q' */ \
444 REF (mod_size_t), /* for 'z', 'Z' */ \
445 REF (form_percent), /* for '%' */ \
446 REF (form_integer), /* for 'd', 'i' */ \
447 REF (form_unsigned), /* for 'u' */ \
448 REF (form_octal), /* for 'o' */ \
449 REF (form_hexa), /* for 'X', 'x' */ \
450 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
451 REF (form_character), /* for 'c' */ \
452 REF (form_string), /* for 's', 'S' */ \
453 REF (form_pointer), /* for 'p' */ \
454 REF (form_number), /* for 'n' */ \
455 REF (form_strerror), /* for 'm' */ \
456 REF (form_wcharacter), /* for 'C' */ \
457 REF (form_floathex), /* for 'A', 'a' */ \
458 REF (mod_ptrdiff_t), /* for 't' */ \
459 REF (mod_intmax_t), /* for 'j' */ \
460 REF (form_unknown), /* for 'I' */ \
461 REF (form_binary), /* for 'B', 'b' */ \
462 }; \
463 /* Step 3a: after processing first 'h' modifier. */ \
464 static JUMP_TABLE_TYPE step3a_jumps[31] = \
465 { \
466 REF (form_unknown), \
467 REF (form_unknown), /* for ' ' */ \
468 REF (form_unknown), /* for '+' */ \
469 REF (form_unknown), /* for '-' */ \
470 REF (form_unknown), /* for '<hash>' */ \
471 REF (form_unknown), /* for '0' */ \
472 REF (form_unknown), /* for '\'' */ \
473 REF (form_unknown), /* for '*' */ \
474 REF (form_unknown), /* for '1'...'9' */ \
475 REF (form_unknown), /* for '.' */ \
476 REF (mod_halfhalf), /* for 'h' */ \
477 REF (form_unknown), /* for 'l' */ \
478 REF (form_unknown), /* for 'L', 'q' */ \
479 REF (form_unknown), /* for 'z', 'Z' */ \
480 REF (form_percent), /* for '%' */ \
481 REF (form_integer), /* for 'd', 'i' */ \
482 REF (form_unsigned), /* for 'u' */ \
483 REF (form_octal), /* for 'o' */ \
484 REF (form_hexa), /* for 'X', 'x' */ \
485 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
486 REF (form_unknown), /* for 'c' */ \
487 REF (form_unknown), /* for 's', 'S' */ \
488 REF (form_unknown), /* for 'p' */ \
489 REF (form_number), /* for 'n' */ \
490 REF (form_unknown), /* for 'm' */ \
491 REF (form_unknown), /* for 'C' */ \
492 REF (form_unknown), /* for 'A', 'a' */ \
493 REF (form_unknown), /* for 't' */ \
494 REF (form_unknown), /* for 'j' */ \
495 REF (form_unknown), /* for 'I' */ \
496 REF (form_binary), /* for 'B', 'b' */ \
497 }; \
498 /* Step 3b: after processing first 'l' modifier. */ \
499 static JUMP_TABLE_TYPE step3b_jumps[31] = \
500 { \
501 REF (form_unknown), \
502 REF (form_unknown), /* for ' ' */ \
503 REF (form_unknown), /* for '+' */ \
504 REF (form_unknown), /* for '-' */ \
505 REF (form_unknown), /* for '<hash>' */ \
506 REF (form_unknown), /* for '0' */ \
507 REF (form_unknown), /* for '\'' */ \
508 REF (form_unknown), /* for '*' */ \
509 REF (form_unknown), /* for '1'...'9' */ \
510 REF (form_unknown), /* for '.' */ \
511 REF (form_unknown), /* for 'h' */ \
512 REF (mod_longlong), /* for 'l' */ \
513 REF (form_unknown), /* for 'L', 'q' */ \
514 REF (form_unknown), /* for 'z', 'Z' */ \
515 REF (form_percent), /* for '%' */ \
516 REF (form_integer), /* for 'd', 'i' */ \
517 REF (form_unsigned), /* for 'u' */ \
518 REF (form_octal), /* for 'o' */ \
519 REF (form_hexa), /* for 'X', 'x' */ \
520 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
521 REF (form_character), /* for 'c' */ \
522 REF (form_string), /* for 's', 'S' */ \
523 REF (form_pointer), /* for 'p' */ \
524 REF (form_number), /* for 'n' */ \
525 REF (form_strerror), /* for 'm' */ \
526 REF (form_wcharacter), /* for 'C' */ \
527 REF (form_floathex), /* for 'A', 'a' */ \
528 REF (form_unknown), /* for 't' */ \
529 REF (form_unknown), /* for 'j' */ \
530 REF (form_unknown), /* for 'I' */ \
531 REF (form_binary), /* for 'B', 'b' */ \
532 }
533
534 #define STEP4_TABLE \
535 /* Step 4: processing format specifier. */ \
536 static JUMP_TABLE_TYPE step4_jumps[31] = \
537 { \
538 REF (form_unknown), \
539 REF (form_unknown), /* for ' ' */ \
540 REF (form_unknown), /* for '+' */ \
541 REF (form_unknown), /* for '-' */ \
542 REF (form_unknown), /* for '<hash>' */ \
543 REF (form_unknown), /* for '0' */ \
544 REF (form_unknown), /* for '\'' */ \
545 REF (form_unknown), /* for '*' */ \
546 REF (form_unknown), /* for '1'...'9' */ \
547 REF (form_unknown), /* for '.' */ \
548 REF (form_unknown), /* for 'h' */ \
549 REF (form_unknown), /* for 'l' */ \
550 REF (form_unknown), /* for 'L', 'q' */ \
551 REF (form_unknown), /* for 'z', 'Z' */ \
552 REF (form_percent), /* for '%' */ \
553 REF (form_integer), /* for 'd', 'i' */ \
554 REF (form_unsigned), /* for 'u' */ \
555 REF (form_octal), /* for 'o' */ \
556 REF (form_hexa), /* for 'X', 'x' */ \
557 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
558 REF (form_character), /* for 'c' */ \
559 REF (form_string), /* for 's', 'S' */ \
560 REF (form_pointer), /* for 'p' */ \
561 REF (form_number), /* for 'n' */ \
562 REF (form_strerror), /* for 'm' */ \
563 REF (form_wcharacter), /* for 'C' */ \
564 REF (form_floathex), /* for 'A', 'a' */ \
565 REF (form_unknown), /* for 't' */ \
566 REF (form_unknown), /* for 'j' */ \
567 REF (form_unknown), /* for 'I' */ \
568 REF (form_binary), /* for 'B', 'b' */ \
569 }
570
571 /* Handle positional format specifiers. */
572 static void printf_positional (struct Xprintf_buffer *buf,
573 const CHAR_T *format, int readonly_format,
574 va_list ap, va_list *ap_savep,
575 int nspecs_done, const UCHAR_T *lead_str_end,
576 CHAR_T *work_buffer, int save_errno,
577 const char *grouping,
578 THOUSANDS_SEP_T thousands_sep,
579 unsigned int mode_flags);
580
581 /* Handle unknown format specifier. */
582 static void printf_unknown (struct Xprintf_buffer *,
583 const struct printf_info *) __THROW;
584
585 static void group_number (struct Xprintf_buffer *buf,
586 struct grouping_iterator *iter,
587 CHAR_T *from, CHAR_T *to,
588 THOUSANDS_SEP_T thousands_sep, bool i18n);
589
590 /* The buffer-based function itself. */
591 void
592 Xprintf_buffer (struct Xprintf_buffer *buf, const CHAR_T *format,
593 va_list ap, unsigned int mode_flags)
594 {
595 /* The character used as thousands separator. */
596 THOUSANDS_SEP_T thousands_sep = 0;
597
598 /* The string describing the size of groups of digits. */
599 const char *grouping;
600
601 /* Current character in format string. */
602 const UCHAR_T *f;
603
604 /* End of leading constant string. */
605 const UCHAR_T *lead_str_end;
606
607 /* Points to next format specifier. */
608 const UCHAR_T *end_of_spec;
609
610 /* Buffer intermediate results. */
611 CHAR_T work_buffer[WORK_BUFFER_SIZE];
612 CHAR_T *workend;
613
614 /* We have to save the original argument pointer. */
615 va_list ap_save;
616
617 /* Count number of specifiers we already processed. */
618 int nspecs_done;
619
620 /* For the %m format we may need the current `errno' value. */
621 int save_errno = errno;
622
623 /* 1 if format is in read-only memory, -1 if it is in writable memory,
624 0 if unknown. */
625 int readonly_format = 0;
626
627 /* Initialize local variables. */
628 grouping = (const char *) -1;
629 #ifdef __va_copy
630 /* This macro will be available soon in gcc's <stdarg.h>. We need it
631 since on some systems `va_list' is not an integral type. */
632 __va_copy (ap_save, ap);
633 #else
634 ap_save = ap;
635 #endif
636 nspecs_done = 0;
637
638 #ifdef COMPILE_WPRINTF
639 /* Find the first format specifier. */
640 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
641 #else
642 /* Find the first format specifier. */
643 f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
644 #endif
645
646 /* Write the literal text before the first format. */
647 Xprintf_buffer_write (buf, format,
648 lead_str_end - (const UCHAR_T *) format);
649 if (Xprintf_buffer_has_failed (buf))
650 return;
651
652 /* If we only have to print a simple string, return now. */
653 if (*f == L_('\0'))
654 return;
655
656 /* Use the slow path in case any printf handler is registered. */
657 if (__glibc_unlikely (__printf_function_table != NULL
658 || __printf_modifier_table != NULL
659 || __printf_va_arg_table != NULL))
660 goto do_positional;
661
662 /* Process whole format string. */
663 do
664 {
665 STEP0_3_TABLE;
666 STEP4_TABLE;
667
668 int is_negative; /* Flag for negative number. */
669 union
670 {
671 unsigned long long int longlong;
672 unsigned long int word;
673 } number;
674 int base;
675 union printf_arg the_arg;
676 CHAR_T *string; /* Pointer to argument string. */
677 int alt = 0; /* Alternate format. */
678 int space = 0; /* Use space prefix if no sign is needed. */
679 int left = 0; /* Left-justify output. */
680 int showsign = 0; /* Always begin with plus or minus sign. */
681 int group = 0; /* Print numbers according grouping rules. */
682 /* Argument is long double/long long int. Only used if
683 double/long double or long int/long long int are distinct. */
684 int is_long_double __attribute__ ((unused)) = 0;
685 int is_short = 0; /* Argument is short int. */
686 int is_long = 0; /* Argument is long int. */
687 int is_char = 0; /* Argument is promoted (unsigned) char. */
688 int width = 0; /* Width of output; 0 means none specified. */
689 int prec = -1; /* Precision of output; -1 means none specified. */
690 /* This flag is set by the 'I' modifier and selects the use of the
691 `outdigits' as determined by the current locale. */
692 int use_outdigits = 0;
693 UCHAR_T pad = L_(' ');/* Padding character. */
694 CHAR_T spec;
695
696 workend = work_buffer + WORK_BUFFER_SIZE;
697
698 /* Get current character in format string. */
699 JUMP (*++f, step0_jumps);
700
701 /* ' ' flag. */
702 LABEL (flag_space):
703 space = 1;
704 JUMP (*++f, step0_jumps);
705
706 /* '+' flag. */
707 LABEL (flag_plus):
708 showsign = 1;
709 JUMP (*++f, step0_jumps);
710
711 /* The '-' flag. */
712 LABEL (flag_minus):
713 left = 1;
714 pad = L_(' ');
715 JUMP (*++f, step0_jumps);
716
717 /* The '#' flag. */
718 LABEL (flag_hash):
719 alt = 1;
720 JUMP (*++f, step0_jumps);
721
722 /* The '0' flag. */
723 LABEL (flag_zero):
724 if (!left)
725 pad = L_('0');
726 JUMP (*++f, step0_jumps);
727
728 /* The '\'' flag. */
729 LABEL (flag_quote):
730 group = 1;
731
732 if (grouping == (const char *) -1)
733 {
734 #ifdef COMPILE_WPRINTF
735 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
736 _NL_NUMERIC_THOUSANDS_SEP_WC);
737 #else
738 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
739 #endif
740
741 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
742 if (*grouping == '\0' || *grouping == CHAR_MAX
743 #ifdef COMPILE_WPRINTF
744 || thousands_sep == L'\0'
745 #else
746 || *thousands_sep == '\0'
747 #endif
748 )
749 grouping = NULL;
750 }
751 JUMP (*++f, step0_jumps);
752
753 LABEL (flag_i18n):
754 use_outdigits = 1;
755 JUMP (*++f, step0_jumps);
756
757 /* Get width from argument. */
758 LABEL (width_asterics):
759 {
760 const UCHAR_T *tmp; /* Temporary value. */
761
762 tmp = ++f;
763 if (ISDIGIT (*tmp))
764 {
765 int pos = read_int (&tmp);
766
767 if (pos == -1)
768 {
769 __set_errno (EOVERFLOW);
770 Xprintf_buffer_mark_failed (buf);
771 goto all_done;
772 }
773
774 if (pos && *tmp == L_('$'))
775 /* The width comes from a positional parameter. */
776 goto do_positional;
777 }
778 width = va_arg (ap, int);
779
780 /* Negative width means left justified. */
781 if (width < 0)
782 {
783 width = -width;
784 pad = L_(' ');
785 left = 1;
786 }
787 }
788 JUMP (*f, step1_jumps);
789
790 /* Given width in format string. */
791 LABEL (width):
792 width = read_int (&f);
793
794 if (__glibc_unlikely (width == -1))
795 {
796 __set_errno (EOVERFLOW);
797 Xprintf_buffer_mark_failed (buf);
798 goto all_done;
799 }
800
801 if (*f == L_('$'))
802 /* Oh, oh. The argument comes from a positional parameter. */
803 goto do_positional;
804 JUMP (*f, step1_jumps);
805
806 LABEL (precision):
807 ++f;
808 if (*f == L_('*'))
809 {
810 const UCHAR_T *tmp; /* Temporary value. */
811
812 tmp = ++f;
813 if (ISDIGIT (*tmp))
814 {
815 int pos = read_int (&tmp);
816
817 if (pos == -1)
818 {
819 __set_errno (EOVERFLOW);
820 Xprintf_buffer_mark_failed (buf);
821 goto all_done;
822 }
823
824 if (pos && *tmp == L_('$'))
825 /* The precision comes from a positional parameter. */
826 goto do_positional;
827 }
828 prec = va_arg (ap, int);
829
830 /* If the precision is negative the precision is omitted. */
831 if (prec < 0)
832 prec = -1;
833 }
834 else if (ISDIGIT (*f))
835 {
836 prec = read_int (&f);
837
838 /* The precision was specified in this case as an extremely
839 large positive value. */
840 if (prec == -1)
841 {
842 __set_errno (EOVERFLOW);
843 Xprintf_buffer_mark_failed (buf);
844 goto all_done;
845 }
846 }
847 else
848 prec = 0;
849 JUMP (*f, step2_jumps);
850
851 /* Process 'h' modifier. There might another 'h' following. */
852 LABEL (mod_half):
853 is_short = 1;
854 JUMP (*++f, step3a_jumps);
855
856 /* Process 'hh' modifier. */
857 LABEL (mod_halfhalf):
858 is_short = 0;
859 is_char = 1;
860 JUMP (*++f, step4_jumps);
861
862 /* Process 'l' modifier. There might another 'l' following. */
863 LABEL (mod_long):
864 is_long = 1;
865 JUMP (*++f, step3b_jumps);
866
867 /* Process 'L', 'q', or 'll' modifier. No other modifier is
868 allowed to follow. */
869 LABEL (mod_longlong):
870 is_long_double = 1;
871 is_long = 1;
872 JUMP (*++f, step4_jumps);
873
874 LABEL (mod_size_t):
875 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
876 is_long = sizeof (size_t) > sizeof (unsigned int);
877 JUMP (*++f, step4_jumps);
878
879 LABEL (mod_ptrdiff_t):
880 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
881 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
882 JUMP (*++f, step4_jumps);
883
884 LABEL (mod_intmax_t):
885 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
886 is_long = sizeof (intmax_t) > sizeof (unsigned int);
887 JUMP (*++f, step4_jumps);
888
889 /* Process current format. */
890 while (1)
891 {
892 #define process_arg_int() va_arg (ap, int)
893 #define process_arg_long_int() va_arg (ap, long int)
894 #define process_arg_long_long_int() va_arg (ap, long long int)
895 #define process_arg_pointer() va_arg (ap, void *)
896 #define process_arg_string() va_arg (ap, const char *)
897 #define process_arg_unsigned_int() va_arg (ap, unsigned int)
898 #define process_arg_unsigned_long_int() va_arg (ap, unsigned long int)
899 #define process_arg_unsigned_long_long_int() va_arg (ap, unsigned long long int)
900 #define process_arg_wchar_t() va_arg (ap, wchar_t)
901 #define process_arg_wstring() va_arg (ap, const wchar_t *)
902 #include "vfprintf-process-arg.c"
903 #undef process_arg_int
904 #undef process_arg_long_int
905 #undef process_arg_long_long_int
906 #undef process_arg_pointer
907 #undef process_arg_string
908 #undef process_arg_unsigned_int
909 #undef process_arg_unsigned_long_int
910 #undef process_arg_unsigned_long_long_int
911 #undef process_arg_wchar_t
912 #undef process_arg_wstring
913
914 LABEL (form_float):
915 LABEL (form_floathex):
916 {
917 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0))
918 is_long_double = 0;
919
920 struct printf_info info =
921 {
922 .prec = prec,
923 .width = width,
924 .spec = spec,
925 .is_long_double = is_long_double,
926 .is_short = is_short,
927 .is_long = is_long,
928 .alt = alt,
929 .space = space,
930 .left = left,
931 .showsign = showsign,
932 .group = group,
933 .pad = pad,
934 .extra = 0,
935 .i18n = use_outdigits,
936 .wide = sizeof (CHAR_T) != 1,
937 .is_binary128 = 0
938 };
939
940 PARSE_FLOAT_VA_ARG_EXTENDED (info);
941 const void *ptr = &the_arg;
942
943 __printf_fp_spec (buf, &info, &ptr);
944 }
945 break;
946
947 LABEL (form_unknown):
948 if (spec == L_('\0'))
949 {
950 /* The format string ended before the specifier is complete. */
951 __set_errno (EINVAL);
952 Xprintf_buffer_mark_failed (buf);
953 goto all_done;
954 }
955
956 /* If we are in the fast loop force entering the complicated
957 one. */
958 goto do_positional;
959 }
960
961 /* The format is correctly handled. */
962 ++nspecs_done;
963
964 /* Look for next format specifier. */
965 #ifdef COMPILE_WPRINTF
966 f = __find_specwc ((end_of_spec = ++f));
967 #else
968 f = __find_specmb ((end_of_spec = ++f));
969 #endif
970
971 /* Write the following constant string. */
972 Xprintf_buffer_write (buf, (const CHAR_T *) end_of_spec,
973 f - end_of_spec);
974 }
975 while (*f != L_('\0') && !Xprintf_buffer_has_failed (buf));
976
977 all_done:
978 /* printf_positional performs cleanup under its all_done label, so
979 vfprintf-process-arg.c uses it for this function and
980 printf_positional below. */
981 return;
982
983 /* Hand off processing for positional parameters. */
984 do_positional:
985 printf_positional (buf, format, readonly_format, ap, &ap_save,
986 nspecs_done, lead_str_end, work_buffer,
987 save_errno, grouping, thousands_sep, mode_flags);
988 }
989 \f
990 static void
991 printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format,
992 int readonly_format,
993 va_list ap, va_list *ap_savep, int nspecs_done,
994 const UCHAR_T *lead_str_end,
995 CHAR_T *work_buffer, int save_errno,
996 const char *grouping, THOUSANDS_SEP_T thousands_sep,
997 unsigned int mode_flags)
998 {
999 /* For positional argument handling. */
1000 struct scratch_buffer specsbuf;
1001 scratch_buffer_init (&specsbuf);
1002 struct printf_spec *specs = specsbuf.data;
1003 size_t specs_limit = specsbuf.length / sizeof (specs[0]);
1004
1005 /* Used as a backing store for args_value, args_size, args_type
1006 below. */
1007 struct scratch_buffer argsbuf;
1008 scratch_buffer_init (&argsbuf);
1009
1010 /* Array with information about the needed arguments. This has to
1011 be dynamically extensible. */
1012 size_t nspecs = 0;
1013
1014 /* The number of arguments the format string requests. This will
1015 determine the size of the array needed to store the argument
1016 attributes. */
1017 size_t nargs = 0;
1018
1019 /* Positional parameters refer to arguments directly. This could
1020 also determine the maximum number of arguments. Track the
1021 maximum number. */
1022 size_t max_ref_arg = 0;
1023
1024 /* Just a counter. */
1025 size_t cnt;
1026
1027 if (grouping == (const char *) -1)
1028 {
1029 #ifdef COMPILE_WPRINTF
1030 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1031 _NL_NUMERIC_THOUSANDS_SEP_WC);
1032 #else
1033 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1034 #endif
1035
1036 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1037 if (*grouping == '\0' || *grouping == CHAR_MAX)
1038 grouping = NULL;
1039 }
1040
1041 for (const UCHAR_T *f = lead_str_end; *f != L_('\0');
1042 f = specs[nspecs++].next_fmt)
1043 {
1044 if (nspecs == specs_limit)
1045 {
1046 if (!scratch_buffer_grow_preserve (&specsbuf))
1047 {
1048 Xprintf_buffer_mark_failed (buf);
1049 goto all_done;
1050 }
1051 specs = specsbuf.data;
1052 specs_limit = specsbuf.length / sizeof (specs[0]);
1053 }
1054
1055 /* Parse the format specifier. */
1056 #ifdef COMPILE_WPRINTF
1057 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1058 #else
1059 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
1060 #endif
1061 }
1062
1063 /* Determine the number of arguments the format string consumes. */
1064 nargs = MAX (nargs, max_ref_arg);
1065
1066 union printf_arg *args_value;
1067 int *args_size;
1068 int *args_type;
1069 {
1070 /* Calculate total size needed to represent a single argument
1071 across all three argument-related arrays. */
1072 size_t bytes_per_arg
1073 = sizeof (*args_value) + sizeof (*args_size) + sizeof (*args_type);
1074 if (!scratch_buffer_set_array_size (&argsbuf, nargs, bytes_per_arg))
1075 {
1076 Xprintf_buffer_mark_failed (buf);
1077 goto all_done;
1078 }
1079 args_value = argsbuf.data;
1080 /* Set up the remaining two arrays to each point past the end of
1081 the prior array, since space for all three has been allocated
1082 now. */
1083 args_size = &args_value[nargs].pa_int;
1084 args_type = &args_size[nargs];
1085 memset (args_type, (mode_flags & PRINTF_FORTIFY) != 0 ? '\xff' : '\0',
1086 nargs * sizeof (*args_type));
1087 }
1088
1089 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1090 still zero after this loop, format is invalid. For now we
1091 simply use 0 as the value. */
1092
1093 /* Fill in the types of all the arguments. */
1094 for (cnt = 0; cnt < nspecs; ++cnt)
1095 {
1096 /* If the width is determined by an argument this is an int. */
1097 if (specs[cnt].width_arg != -1)
1098 args_type[specs[cnt].width_arg] = PA_INT;
1099
1100 /* If the precision is determined by an argument this is an int. */
1101 if (specs[cnt].prec_arg != -1)
1102 args_type[specs[cnt].prec_arg] = PA_INT;
1103
1104 switch (specs[cnt].ndata_args)
1105 {
1106 case 0: /* No arguments. */
1107 break;
1108 case 1: /* One argument; we already have the
1109 type and size. */
1110 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1111 args_size[specs[cnt].data_arg] = specs[cnt].size;
1112 break;
1113 default:
1114 /* We have more than one argument for this format spec.
1115 We must call the arginfo function again to determine
1116 all the types. */
1117 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1118 (&specs[cnt].info,
1119 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1120 &args_size[specs[cnt].data_arg]);
1121 break;
1122 }
1123 }
1124
1125 /* Now we know all the types and the order. Fill in the argument
1126 values. */
1127 for (cnt = 0; cnt < nargs; ++cnt)
1128 switch (args_type[cnt])
1129 {
1130 #define T(tag, mem, type) \
1131 case tag: \
1132 args_value[cnt].mem = va_arg (*ap_savep, type); \
1133 break
1134
1135 T (PA_WCHAR, pa_wchar, wint_t);
1136 case PA_CHAR: /* Promoted. */
1137 case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1138 #if LONG_MAX == INT_MAX
1139 case PA_INT|PA_FLAG_LONG:
1140 #endif
1141 T (PA_INT, pa_int, int);
1142 #if LONG_MAX == LONG_LONG_MAX
1143 case PA_INT|PA_FLAG_LONG:
1144 #endif
1145 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1146 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1147 # error "he?"
1148 #endif
1149 case PA_FLOAT: /* Promoted. */
1150 T (PA_DOUBLE, pa_double, double);
1151 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1152 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0))
1153 {
1154 args_value[cnt].pa_double = va_arg (*ap_savep, double);
1155 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1156 }
1157 #if __HAVE_FLOAT128_UNLIKE_LDBL
1158 else if ((mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0)
1159 args_value[cnt].pa_float128 = va_arg (*ap_savep, _Float128);
1160 #endif
1161 else
1162 args_value[cnt].pa_long_double = va_arg (*ap_savep, long double);
1163 break;
1164 case PA_STRING: /* All pointers are the same */
1165 case PA_WSTRING: /* All pointers are the same */
1166 T (PA_POINTER, pa_pointer, void *);
1167 #undef T
1168 default:
1169 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1170 args_value[cnt].pa_pointer = va_arg (*ap_savep, void *);
1171 else if (__glibc_unlikely (__printf_va_arg_table != NULL)
1172 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1173 {
1174 args_value[cnt].pa_user = alloca (args_size[cnt]);
1175 (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1176 (args_value[cnt].pa_user, ap_savep);
1177 }
1178 else
1179 memset (&args_value[cnt], 0, sizeof (args_value[cnt]));
1180 break;
1181 case -1:
1182 /* Error case. Not all parameters appear in N$ format
1183 strings. We have no way to determine their type. */
1184 assert ((mode_flags & PRINTF_FORTIFY) != 0);
1185 __libc_fatal ("*** invalid %N$ use detected ***\n");
1186 }
1187
1188 /* Now walk through all format specifiers and process them. */
1189 for (; (size_t) nspecs_done < nspecs && !Xprintf_buffer_has_failed (buf);
1190 ++nspecs_done)
1191 {
1192 STEP4_TABLE;
1193
1194 int is_negative;
1195 union
1196 {
1197 unsigned long long int longlong;
1198 unsigned long int word;
1199 } number;
1200 int base;
1201 CHAR_T *string; /* Pointer to argument string. */
1202
1203 /* Fill variables from values in struct. */
1204 int alt = specs[nspecs_done].info.alt;
1205 int space = specs[nspecs_done].info.space;
1206 int left = specs[nspecs_done].info.left;
1207 int showsign = specs[nspecs_done].info.showsign;
1208 int group = specs[nspecs_done].info.group;
1209 int is_long_double __attribute__ ((unused))
1210 = specs[nspecs_done].info.is_long_double;
1211 int is_short = specs[nspecs_done].info.is_short;
1212 int is_char = specs[nspecs_done].info.is_char;
1213 int is_long = specs[nspecs_done].info.is_long;
1214 int width = specs[nspecs_done].info.width;
1215 int prec = specs[nspecs_done].info.prec;
1216 int use_outdigits = specs[nspecs_done].info.i18n;
1217 char pad = specs[nspecs_done].info.pad;
1218 CHAR_T spec = specs[nspecs_done].info.spec;
1219
1220 CHAR_T *workend = work_buffer + WORK_BUFFER_SIZE;
1221
1222 /* Fill in last information. */
1223 if (specs[nspecs_done].width_arg != -1)
1224 {
1225 /* Extract the field width from an argument. */
1226 specs[nspecs_done].info.width =
1227 args_value[specs[nspecs_done].width_arg].pa_int;
1228
1229 if (specs[nspecs_done].info.width < 0)
1230 /* If the width value is negative left justification is
1231 selected and the value is taken as being positive. */
1232 {
1233 specs[nspecs_done].info.width *= -1;
1234 left = specs[nspecs_done].info.left = 1;
1235 }
1236 width = specs[nspecs_done].info.width;
1237 }
1238
1239 if (specs[nspecs_done].prec_arg != -1)
1240 {
1241 /* Extract the precision from an argument. */
1242 specs[nspecs_done].info.prec =
1243 args_value[specs[nspecs_done].prec_arg].pa_int;
1244
1245 if (specs[nspecs_done].info.prec < 0)
1246 /* If the precision is negative the precision is
1247 omitted. */
1248 specs[nspecs_done].info.prec = -1;
1249
1250 prec = specs[nspecs_done].info.prec;
1251 }
1252
1253 /* Process format specifiers. */
1254 do
1255 {
1256 if (spec <= UCHAR_MAX
1257 && __printf_function_table != NULL
1258 && __printf_function_table[(size_t) spec] != NULL)
1259 {
1260 int function_done
1261 = Xprintf (function_invoke) (buf,
1262 __printf_function_table[(size_t) spec],
1263 &args_value[specs[nspecs_done]
1264 .data_arg],
1265 specs[nspecs_done].ndata_args,
1266 &specs[nspecs_done].info);
1267 if (function_done != -2)
1268 {
1269 /* If an error occurred we don't have information
1270 about # of chars. */
1271 if (function_done < 0)
1272 {
1273 /* Function has set errno. */
1274 Xprintf_buffer_mark_failed (buf);
1275 goto all_done;
1276 }
1277 break;
1278 }
1279 }
1280
1281 JUMP (spec, step4_jumps);
1282
1283 #define process_arg_data args_value[specs[nspecs_done].data_arg]
1284 #define process_arg_int() process_arg_data.pa_int
1285 #define process_arg_long_int() process_arg_data.pa_long_int
1286 #define process_arg_long_long_int() process_arg_data.pa_long_long_int
1287 #define process_arg_pointer() process_arg_data.pa_pointer
1288 #define process_arg_string() process_arg_data.pa_string
1289 #define process_arg_unsigned_int() process_arg_data.pa_u_int
1290 #define process_arg_unsigned_long_int() process_arg_data.pa_u_long_int
1291 #define process_arg_unsigned_long_long_int() process_arg_data.pa_u_long_long_int
1292 #define process_arg_wchar_t() process_arg_data.pa_wchar
1293 #define process_arg_wstring() process_arg_data.pa_wstring
1294 #include "vfprintf-process-arg.c"
1295 #undef process_arg_data
1296 #undef process_arg_int
1297 #undef process_arg_long_int
1298 #undef process_arg_long_long_int
1299 #undef process_arg_pointer
1300 #undef process_arg_string
1301 #undef process_arg_unsigned_int
1302 #undef process_arg_unsigned_long_int
1303 #undef process_arg_unsigned_long_long_int
1304 #undef process_arg_wchar_t
1305 #undef process_arg_wstring
1306
1307 LABEL (form_float):
1308 LABEL (form_floathex):
1309 {
1310 const void *ptr
1311 = (const void *) &args_value[specs[nspecs_done].data_arg];
1312 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0))
1313 {
1314 specs[nspecs_done].data_arg_type = PA_DOUBLE;
1315 specs[nspecs_done].info.is_long_double = 0;
1316 }
1317 SETUP_FLOAT128_INFO (specs[nspecs_done].info);
1318
1319 __printf_fp_spec (buf, &specs[nspecs_done].info, &ptr);
1320 }
1321 break;
1322
1323 LABEL (form_unknown):
1324 {
1325 printf_unknown (buf, &specs[nspecs_done].info);
1326 }
1327 break;
1328 }
1329 while (Xprintf_buffer_has_failed (buf));
1330
1331 /* Write the following constant string. */
1332 Xprintf_buffer_write (buf,
1333 (const CHAR_T *) specs[nspecs_done].end_of_fmt,
1334 (specs[nspecs_done].next_fmt
1335 - specs[nspecs_done].end_of_fmt));
1336 }
1337 all_done:
1338 scratch_buffer_free (&argsbuf);
1339 scratch_buffer_free (&specsbuf);
1340 }
1341 \f
1342 /* Handle an unknown format specifier. This prints out a canonicalized
1343 representation of the format spec itself. */
1344 static void
1345 printf_unknown (struct Xprintf_buffer *buf, const struct printf_info *info)
1346 {
1347 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
1348 CHAR_T *const workend
1349 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1350 CHAR_T *w;
1351
1352 Xprintf_buffer_putc (buf, L_('%'));
1353
1354 if (info->alt)
1355 Xprintf_buffer_putc (buf, L_('#'));
1356 if (info->group)
1357 Xprintf_buffer_putc (buf, L_('\''));
1358 if (info->showsign)
1359 Xprintf_buffer_putc (buf, L_('+'));
1360 else if (info->space)
1361 Xprintf_buffer_putc (buf, L_(' '));
1362 if (info->left)
1363 Xprintf_buffer_putc (buf, L_('-'));
1364 if (info->pad == L_('0'))
1365 Xprintf_buffer_putc (buf, L_('0'));
1366 if (info->i18n)
1367 Xprintf_buffer_putc (buf, L_('I'));
1368
1369 if (info->width != 0)
1370 {
1371 w = _itoa_word (info->width, workend, 10, 0);
1372 while (w < workend)
1373 Xprintf_buffer_putc (buf, *w++);
1374 }
1375
1376 if (info->prec != -1)
1377 {
1378 Xprintf_buffer_putc (buf, L_('.'));
1379 w = _itoa_word (info->prec, workend, 10, 0);
1380 while (w < workend)
1381 Xprintf_buffer_putc (buf, *w++);
1382 }
1383
1384 if (info->spec != L_('\0'))
1385 Xprintf_buffer_putc (buf, info->spec);
1386 }
1387
1388 static void
1389 group_number (struct Xprintf_buffer *buf,
1390 struct grouping_iterator *iter,
1391 CHAR_T *from, CHAR_T *to, THOUSANDS_SEP_T thousands_sep,
1392 bool i18n)
1393 {
1394 if (!i18n)
1395 for (CHAR_T *cp = from; cp != to; ++cp)
1396 {
1397 if (__grouping_iterator_next (iter))
1398 {
1399 #ifdef COMPILE_WPRINTF
1400 __wprintf_buffer_putc (buf, thousands_sep);
1401 #else
1402 __printf_buffer_puts (buf, thousands_sep);
1403 #endif
1404 }
1405 Xprintf_buffer_putc (buf, *cp);
1406 }
1407 else
1408 {
1409 /* Apply digit translation and grouping. */
1410 for (CHAR_T *cp = from; cp != to; ++cp)
1411 {
1412 if (__grouping_iterator_next (iter))
1413 {
1414 #ifdef COMPILE_WPRINTF
1415 __wprintf_buffer_putc (buf, thousands_sep);
1416 #else
1417 __printf_buffer_puts (buf, thousands_sep);
1418 #endif
1419 }
1420 int digit = *cp - '0';
1421 #ifdef COMPILE_WPRINTF
1422 __wprintf_buffer_putc
1423 (buf, _NL_CURRENT_WORD (LC_CTYPE,
1424 _NL_CTYPE_OUTDIGIT0_WC + digit));
1425 #else
1426 __printf_buffer_puts
1427 (buf, _NL_CURRENT (LC_CTYPE, _NL_CTYPE_OUTDIGIT0_MB + digit));
1428 #endif
1429 }
1430 }
1431 }
1432
1433
1434 /* The FILE-based function. */
1435 int
1436 vfprintf (FILE *s, const CHAR_T *format, va_list ap, unsigned int mode_flags)
1437 {
1438 /* Orient the stream. */
1439 #ifdef ORIENT
1440 ORIENT;
1441 #endif
1442
1443 /* Sanity check of arguments. */
1444 ARGCHECK (s, format);
1445
1446 #ifdef ORIENT
1447 /* Check for correct orientation. */
1448 if (_IO_vtable_offset (s) == 0
1449 && _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1450 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1451 /* The stream is already oriented otherwise. */
1452 return EOF;
1453 #endif
1454
1455 if (!_IO_need_lock (s))
1456 {
1457 struct Xprintf (buffer_to_file) wrap;
1458 Xprintf (buffer_to_file_init) (&wrap, s);
1459 Xprintf_buffer (&wrap.base, format, ap, mode_flags);
1460 return Xprintf (buffer_to_file_done) (&wrap);
1461 }
1462
1463 int done;
1464
1465 /* Lock stream. */
1466 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1467 _IO_flockfile (s);
1468
1469 /* Set up the wrapping buffer. */
1470 struct Xprintf (buffer_to_file) wrap;
1471 Xprintf (buffer_to_file_init) (&wrap, s);
1472
1473 /* Perform the printing operation on the buffer. */
1474 Xprintf_buffer (&wrap.base, format, ap, mode_flags);
1475 done = Xprintf (buffer_to_file_done) (&wrap);
1476
1477 /* Unlock the stream. */
1478 _IO_funlockfile (s);
1479 _IO_cleanup_region_end (0);
1480
1481 return done;
1482 }