math: Fix exp10 undefined left shift
[glibc.git] / stdio-common / vfprintf-internal.c
blob771beca9bf71f4c817800fb44c45c19ec1e3a9d3
1 /* Copyright (C) 1991-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
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>
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++.
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. */
47 #include <libioP.h>
49 #ifdef COMPILE_WPRINTF
50 #include <wctype.h>
51 #endif
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)
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
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); \
102 while (0)
104 #if __HAVE_FLOAT128_UNLIKE_LDBL
105 # define SETUP_FLOAT128_INFO(INFO) \
106 do \
108 if ((mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0) \
109 INFO.is_binary128 = is_long_double; \
110 else \
111 INFO.is_binary128 = 0; \
113 while (0)
114 #else
115 # define SETUP_FLOAT128_INFO(INFO) \
116 do \
118 INFO.is_binary128 = 0; \
120 while (0)
121 #endif
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)
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)
149 # include <_itowa.h>
151 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
152 # define CONVERT_FROM_OTHER_STRING __mbsrtowcs
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
161 /* Include the shared code for parsing the format string. */
162 #include "printf-parse.h"
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)
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");
180 /* Add the initial padding if needed. */
181 if (width > 0 && !left)
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
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)
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)
206 Xprintf_buffer_mark_failed (target);
207 return;
209 if (written == 0)
210 break;
211 total_written += written;
212 limit -= written;
216 /* Output initial padding. */
217 Xprintf_buffer_pad (target, L_(' '), width - total_written);
218 if (Xprintf_buffer_has_failed (target))
219 return;
222 /* Convert the input string, piece by piece. */
223 size_t total_written = 0;
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)
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)
240 Xprintf_buffer_mark_failed (target);
241 return;
243 if (written == 0)
244 break;
245 Xprintf_buffer_write (target, buf, written);
246 total_written += written;
247 if (prec >= 0)
248 remaining -= written;
252 /* Add final padding. */
253 if (width > 0 && left)
254 Xprintf_buffer_pad (target, L_(' '), width - total_written);
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)
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);
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
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
287 /* Global constants. */
288 static const CHAR_T null[] = L_("(null)");
290 /* Size of the work_buffer variable (in characters, not bytes. */
291 enum { WORK_BUFFER_SIZE = 1000 / sizeof (CHAR_T) };
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[] =
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, /* 'w' */ 31,
319 /* 'x' */ 18, 0, /* 'z' */ 13
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 \
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; \
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 \
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; \
354 while (0)
355 #endif
357 #define STEP0_3_TABLE \
358 /* Step 0: at the beginning. */ \
359 static JUMP_TABLE_TYPE step0_jumps[32] = \
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 REF (mod_bitwidth), /* for 'w' */ \
393 }; \
394 /* Step 1: after processing width. */ \
395 static JUMP_TABLE_TYPE step1_jumps[32] = \
397 REF (form_unknown), \
398 REF (form_unknown), /* for ' ' */ \
399 REF (form_unknown), /* for '+' */ \
400 REF (form_unknown), /* for '-' */ \
401 REF (form_unknown), /* for '<hash>' */ \
402 REF (form_unknown), /* for '0' */ \
403 REF (form_unknown), /* for '\'' */ \
404 REF (form_unknown), /* for '*' */ \
405 REF (form_unknown), /* for '1'...'9' */ \
406 REF (precision), /* for '.' */ \
407 REF (mod_half), /* for 'h' */ \
408 REF (mod_long), /* for 'l' */ \
409 REF (mod_longlong), /* for 'L', 'q' */ \
410 REF (mod_size_t), /* for 'z', 'Z' */ \
411 REF (form_percent), /* for '%' */ \
412 REF (form_integer), /* for 'd', 'i' */ \
413 REF (form_unsigned), /* for 'u' */ \
414 REF (form_octal), /* for 'o' */ \
415 REF (form_hexa), /* for 'X', 'x' */ \
416 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
417 REF (form_character), /* for 'c' */ \
418 REF (form_string), /* for 's', 'S' */ \
419 REF (form_pointer), /* for 'p' */ \
420 REF (form_number), /* for 'n' */ \
421 REF (form_strerror), /* for 'm' */ \
422 REF (form_wcharacter), /* for 'C' */ \
423 REF (form_floathex), /* for 'A', 'a' */ \
424 REF (mod_ptrdiff_t), /* for 't' */ \
425 REF (mod_intmax_t), /* for 'j' */ \
426 REF (form_unknown), /* for 'I' */ \
427 REF (form_binary), /* for 'B', 'b' */ \
428 REF (mod_bitwidth), /* for 'w' */ \
429 }; \
430 /* Step 2: after processing precision. */ \
431 static JUMP_TABLE_TYPE step2_jumps[32] = \
433 REF (form_unknown), \
434 REF (form_unknown), /* for ' ' */ \
435 REF (form_unknown), /* for '+' */ \
436 REF (form_unknown), /* for '-' */ \
437 REF (form_unknown), /* for '<hash>' */ \
438 REF (form_unknown), /* for '0' */ \
439 REF (form_unknown), /* for '\'' */ \
440 REF (form_unknown), /* for '*' */ \
441 REF (form_unknown), /* for '1'...'9' */ \
442 REF (form_unknown), /* for '.' */ \
443 REF (mod_half), /* for 'h' */ \
444 REF (mod_long), /* for 'l' */ \
445 REF (mod_longlong), /* for 'L', 'q' */ \
446 REF (mod_size_t), /* for 'z', 'Z' */ \
447 REF (form_percent), /* for '%' */ \
448 REF (form_integer), /* for 'd', 'i' */ \
449 REF (form_unsigned), /* for 'u' */ \
450 REF (form_octal), /* for 'o' */ \
451 REF (form_hexa), /* for 'X', 'x' */ \
452 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
453 REF (form_character), /* for 'c' */ \
454 REF (form_string), /* for 's', 'S' */ \
455 REF (form_pointer), /* for 'p' */ \
456 REF (form_number), /* for 'n' */ \
457 REF (form_strerror), /* for 'm' */ \
458 REF (form_wcharacter), /* for 'C' */ \
459 REF (form_floathex), /* for 'A', 'a' */ \
460 REF (mod_ptrdiff_t), /* for 't' */ \
461 REF (mod_intmax_t), /* for 'j' */ \
462 REF (form_unknown), /* for 'I' */ \
463 REF (form_binary), /* for 'B', 'b' */ \
464 REF (mod_bitwidth), /* for 'w' */ \
465 }; \
466 /* Step 3a: after processing first 'h' modifier. */ \
467 static JUMP_TABLE_TYPE step3a_jumps[32] = \
469 REF (form_unknown), \
470 REF (form_unknown), /* for ' ' */ \
471 REF (form_unknown), /* for '+' */ \
472 REF (form_unknown), /* for '-' */ \
473 REF (form_unknown), /* for '<hash>' */ \
474 REF (form_unknown), /* for '0' */ \
475 REF (form_unknown), /* for '\'' */ \
476 REF (form_unknown), /* for '*' */ \
477 REF (form_unknown), /* for '1'...'9' */ \
478 REF (form_unknown), /* for '.' */ \
479 REF (mod_halfhalf), /* for 'h' */ \
480 REF (form_unknown), /* for 'l' */ \
481 REF (form_unknown), /* for 'L', 'q' */ \
482 REF (form_unknown), /* for 'z', 'Z' */ \
483 REF (form_percent), /* for '%' */ \
484 REF (form_integer), /* for 'd', 'i' */ \
485 REF (form_unsigned), /* for 'u' */ \
486 REF (form_octal), /* for 'o' */ \
487 REF (form_hexa), /* for 'X', 'x' */ \
488 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
489 REF (form_unknown), /* for 'c' */ \
490 REF (form_unknown), /* for 's', 'S' */ \
491 REF (form_unknown), /* for 'p' */ \
492 REF (form_number), /* for 'n' */ \
493 REF (form_unknown), /* for 'm' */ \
494 REF (form_unknown), /* for 'C' */ \
495 REF (form_unknown), /* for 'A', 'a' */ \
496 REF (form_unknown), /* for 't' */ \
497 REF (form_unknown), /* for 'j' */ \
498 REF (form_unknown), /* for 'I' */ \
499 REF (form_binary), /* for 'B', 'b' */ \
500 REF (form_unknown), /* for 'w' */ \
501 }; \
502 /* Step 3b: after processing first 'l' modifier. */ \
503 static JUMP_TABLE_TYPE step3b_jumps[32] = \
505 REF (form_unknown), \
506 REF (form_unknown), /* for ' ' */ \
507 REF (form_unknown), /* for '+' */ \
508 REF (form_unknown), /* for '-' */ \
509 REF (form_unknown), /* for '<hash>' */ \
510 REF (form_unknown), /* for '0' */ \
511 REF (form_unknown), /* for '\'' */ \
512 REF (form_unknown), /* for '*' */ \
513 REF (form_unknown), /* for '1'...'9' */ \
514 REF (form_unknown), /* for '.' */ \
515 REF (form_unknown), /* for 'h' */ \
516 REF (mod_longlong), /* for 'l' */ \
517 REF (form_unknown), /* for 'L', 'q' */ \
518 REF (form_unknown), /* for 'z', 'Z' */ \
519 REF (form_percent), /* for '%' */ \
520 REF (form_integer), /* for 'd', 'i' */ \
521 REF (form_unsigned), /* for 'u' */ \
522 REF (form_octal), /* for 'o' */ \
523 REF (form_hexa), /* for 'X', 'x' */ \
524 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
525 REF (form_character), /* for 'c' */ \
526 REF (form_string), /* for 's', 'S' */ \
527 REF (form_pointer), /* for 'p' */ \
528 REF (form_number), /* for 'n' */ \
529 REF (form_strerror), /* for 'm' */ \
530 REF (form_wcharacter), /* for 'C' */ \
531 REF (form_floathex), /* for 'A', 'a' */ \
532 REF (form_unknown), /* for 't' */ \
533 REF (form_unknown), /* for 'j' */ \
534 REF (form_unknown), /* for 'I' */ \
535 REF (form_binary), /* for 'B', 'b' */ \
536 REF (form_unknown), /* for 'w' */ \
539 #define STEP4_TABLE \
540 /* Step 4: processing format specifier. */ \
541 static JUMP_TABLE_TYPE step4_jumps[32] = \
543 REF (form_unknown), \
544 REF (form_unknown), /* for ' ' */ \
545 REF (form_unknown), /* for '+' */ \
546 REF (form_unknown), /* for '-' */ \
547 REF (form_unknown), /* for '<hash>' */ \
548 REF (form_unknown), /* for '0' */ \
549 REF (form_unknown), /* for '\'' */ \
550 REF (form_unknown), /* for '*' */ \
551 REF (form_unknown), /* for '1'...'9' */ \
552 REF (form_unknown), /* for '.' */ \
553 REF (form_unknown), /* for 'h' */ \
554 REF (form_unknown), /* for 'l' */ \
555 REF (form_unknown), /* for 'L', 'q' */ \
556 REF (form_unknown), /* for 'z', 'Z' */ \
557 REF (form_percent), /* for '%' */ \
558 REF (form_integer), /* for 'd', 'i' */ \
559 REF (form_unsigned), /* for 'u' */ \
560 REF (form_octal), /* for 'o' */ \
561 REF (form_hexa), /* for 'X', 'x' */ \
562 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
563 REF (form_character), /* for 'c' */ \
564 REF (form_string), /* for 's', 'S' */ \
565 REF (form_pointer), /* for 'p' */ \
566 REF (form_number), /* for 'n' */ \
567 REF (form_strerror), /* for 'm' */ \
568 REF (form_wcharacter), /* for 'C' */ \
569 REF (form_floathex), /* for 'A', 'a' */ \
570 REF (form_unknown), /* for 't' */ \
571 REF (form_unknown), /* for 'j' */ \
572 REF (form_unknown), /* for 'I' */ \
573 REF (form_binary), /* for 'B', 'b' */ \
574 REF (form_unknown), /* for 'w' */ \
577 /* Handle positional format specifiers. */
578 static void printf_positional (struct Xprintf_buffer *buf,
579 const CHAR_T *format, int readonly_format,
580 va_list ap, va_list *ap_savep,
581 int nspecs_done, const UCHAR_T *lead_str_end,
582 CHAR_T *work_buffer, int save_errno,
583 const char *grouping,
584 THOUSANDS_SEP_T thousands_sep,
585 unsigned int mode_flags);
587 /* Handle unknown format specifier. */
588 static void printf_unknown (struct Xprintf_buffer *,
589 const struct printf_info *) __THROW;
591 static void group_number (struct Xprintf_buffer *buf,
592 struct grouping_iterator *iter,
593 CHAR_T *from, CHAR_T *to,
594 THOUSANDS_SEP_T thousands_sep, bool i18n);
596 /* The buffer-based function itself. */
597 void
598 Xprintf_buffer (struct Xprintf_buffer *buf, const CHAR_T *format,
599 va_list ap, unsigned int mode_flags)
601 /* The character used as thousands separator. */
602 THOUSANDS_SEP_T thousands_sep = 0;
604 /* The string describing the size of groups of digits. */
605 const char *grouping;
607 /* Current character in format string. */
608 const UCHAR_T *f;
610 /* End of leading constant string. */
611 const UCHAR_T *lead_str_end;
613 /* Points to next format specifier. */
614 const UCHAR_T *end_of_spec;
616 /* Buffer intermediate results. */
617 CHAR_T work_buffer[WORK_BUFFER_SIZE];
618 CHAR_T *workend;
620 /* We have to save the original argument pointer. */
621 va_list ap_save;
623 /* Count number of specifiers we already processed. */
624 int nspecs_done;
626 /* For the %m format we may need the current `errno' value. */
627 int save_errno = errno;
629 /* 1 if format is in read-only memory, -1 if it is in writable memory,
630 0 if unknown. */
631 int readonly_format = 0;
633 /* Initialize local variables. */
634 grouping = (const char *) -1;
635 #ifdef __va_copy
636 /* This macro will be available soon in gcc's <stdarg.h>. We need it
637 since on some systems `va_list' is not an integral type. */
638 __va_copy (ap_save, ap);
639 #else
640 ap_save = ap;
641 #endif
642 nspecs_done = 0;
644 #ifdef COMPILE_WPRINTF
645 /* Find the first format specifier. */
646 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
647 #else
648 /* Find the first format specifier. */
649 f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
650 #endif
652 /* Write the literal text before the first format. */
653 Xprintf_buffer_write (buf, format,
654 lead_str_end - (const UCHAR_T *) format);
655 if (Xprintf_buffer_has_failed (buf))
656 return;
658 /* If we only have to print a simple string, return now. */
659 if (*f == L_('\0'))
660 return;
662 /* Use the slow path in case any printf handler is registered. */
663 if (__glibc_unlikely (__printf_function_table != NULL
664 || __printf_modifier_table != NULL
665 || __printf_va_arg_table != NULL))
666 goto do_positional;
668 /* Process whole format string. */
671 STEP0_3_TABLE;
672 STEP4_TABLE;
674 int is_negative; /* Flag for negative number. */
675 union
677 unsigned long long int longlong;
678 unsigned long int word;
679 } number;
680 int base;
681 union printf_arg the_arg;
682 CHAR_T *string; /* Pointer to argument string. */
683 int alt = 0; /* Alternate format. */
684 int space = 0; /* Use space prefix if no sign is needed. */
685 int left = 0; /* Left-justify output. */
686 int showsign = 0; /* Always begin with plus or minus sign. */
687 int group = 0; /* Print numbers according grouping rules. */
688 /* Argument is long double/long long int. Only used if
689 double/long double or long int/long long int are distinct. */
690 int is_long_double __attribute__ ((unused)) = 0;
691 int is_short = 0; /* Argument is short int. */
692 int is_long = 0; /* Argument is long int. */
693 int is_char = 0; /* Argument is promoted (unsigned) char. */
694 int width = 0; /* Width of output; 0 means none specified. */
695 int prec = -1; /* Precision of output; -1 means none specified. */
696 /* This flag is set by the 'I' modifier and selects the use of the
697 `outdigits' as determined by the current locale. */
698 int use_outdigits = 0;
699 UCHAR_T pad = L_(' ');/* Padding character. */
700 CHAR_T spec;
702 workend = work_buffer + WORK_BUFFER_SIZE;
704 /* Get current character in format string. */
705 JUMP (*++f, step0_jumps);
707 /* ' ' flag. */
708 LABEL (flag_space):
709 space = 1;
710 JUMP (*++f, step0_jumps);
712 /* '+' flag. */
713 LABEL (flag_plus):
714 showsign = 1;
715 JUMP (*++f, step0_jumps);
717 /* The '-' flag. */
718 LABEL (flag_minus):
719 left = 1;
720 pad = L_(' ');
721 JUMP (*++f, step0_jumps);
723 /* The '#' flag. */
724 LABEL (flag_hash):
725 alt = 1;
726 JUMP (*++f, step0_jumps);
728 /* The '0' flag. */
729 LABEL (flag_zero):
730 if (!left)
731 pad = L_('0');
732 JUMP (*++f, step0_jumps);
734 /* The '\'' flag. */
735 LABEL (flag_quote):
736 group = 1;
738 if (grouping == (const char *) -1)
740 #ifdef COMPILE_WPRINTF
741 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
742 _NL_NUMERIC_THOUSANDS_SEP_WC);
743 #else
744 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
745 #endif
747 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
748 if (*grouping == '\0' || *grouping == CHAR_MAX
749 #ifdef COMPILE_WPRINTF
750 || thousands_sep == L'\0'
751 #else
752 || *thousands_sep == '\0'
753 #endif
755 grouping = NULL;
757 JUMP (*++f, step0_jumps);
759 LABEL (flag_i18n):
760 use_outdigits = 1;
761 JUMP (*++f, step0_jumps);
763 /* Get width from argument. */
764 LABEL (width_asterics):
766 const UCHAR_T *tmp; /* Temporary value. */
768 tmp = ++f;
769 if (ISDIGIT (*tmp))
771 int pos = read_int (&tmp);
773 if (pos == -1)
775 __set_errno (EOVERFLOW);
776 Xprintf_buffer_mark_failed (buf);
777 goto all_done;
780 if (pos && *tmp == L_('$'))
781 /* The width comes from a positional parameter. */
782 goto do_positional;
784 width = va_arg (ap, int);
786 /* Negative width means left justified. */
787 if (width < 0)
789 width = -width;
790 pad = L_(' ');
791 left = 1;
794 JUMP (*f, step1_jumps);
796 /* Given width in format string. */
797 LABEL (width):
798 width = read_int (&f);
800 if (__glibc_unlikely (width == -1))
802 __set_errno (EOVERFLOW);
803 Xprintf_buffer_mark_failed (buf);
804 goto all_done;
807 if (*f == L_('$'))
808 /* Oh, oh. The argument comes from a positional parameter. */
809 goto do_positional;
810 JUMP (*f, step1_jumps);
812 LABEL (precision):
813 ++f;
814 if (*f == L_('*'))
816 const UCHAR_T *tmp; /* Temporary value. */
818 tmp = ++f;
819 if (ISDIGIT (*tmp))
821 int pos = read_int (&tmp);
823 if (pos == -1)
825 __set_errno (EOVERFLOW);
826 Xprintf_buffer_mark_failed (buf);
827 goto all_done;
830 if (pos && *tmp == L_('$'))
831 /* The precision comes from a positional parameter. */
832 goto do_positional;
834 prec = va_arg (ap, int);
836 /* If the precision is negative the precision is omitted. */
837 if (prec < 0)
838 prec = -1;
840 else if (ISDIGIT (*f))
842 prec = read_int (&f);
844 /* The precision was specified in this case as an extremely
845 large positive value. */
846 if (prec == -1)
848 __set_errno (EOVERFLOW);
849 Xprintf_buffer_mark_failed (buf);
850 goto all_done;
853 else
854 prec = 0;
855 JUMP (*f, step2_jumps);
857 /* Process 'h' modifier. There might another 'h' following. */
858 LABEL (mod_half):
859 is_short = 1;
860 JUMP (*++f, step3a_jumps);
862 /* Process 'hh' modifier. */
863 LABEL (mod_halfhalf):
864 is_short = 0;
865 is_char = 1;
866 JUMP (*++f, step4_jumps);
868 /* Process 'l' modifier. There might another 'l' following. */
869 LABEL (mod_long):
870 is_long = 1;
871 JUMP (*++f, step3b_jumps);
873 /* Process 'L', 'q', or 'll' modifier. No other modifier is
874 allowed to follow. */
875 LABEL (mod_longlong):
876 is_long_double = 1;
877 is_long = 1;
878 JUMP (*++f, step4_jumps);
880 LABEL (mod_size_t):
881 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
882 is_long = sizeof (size_t) > sizeof (unsigned int);
883 JUMP (*++f, step4_jumps);
885 LABEL (mod_ptrdiff_t):
886 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
887 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
888 JUMP (*++f, step4_jumps);
890 LABEL (mod_intmax_t):
891 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
892 is_long = sizeof (intmax_t) > sizeof (unsigned int);
893 JUMP (*++f, step4_jumps);
895 /* Process 'wN' or 'wfN' modifier. */
896 LABEL (mod_bitwidth):
897 ++f;
898 bool is_fast = false;
899 if (*f == L_('f'))
901 ++f;
902 is_fast = true;
904 int bitwidth = 0;
905 if (ISDIGIT (*f))
906 bitwidth = read_int (&f);
907 if (is_fast)
908 switch (bitwidth)
910 case 8:
911 bitwidth = INT_FAST8_WIDTH;
912 break;
913 case 16:
914 bitwidth = INT_FAST16_WIDTH;
915 break;
916 case 32:
917 bitwidth = INT_FAST32_WIDTH;
918 break;
919 case 64:
920 bitwidth = INT_FAST64_WIDTH;
921 break;
923 switch (bitwidth)
925 case 8:
926 is_char = 1;
927 break;
928 case 16:
929 is_short = 1;
930 break;
931 case 32:
932 break;
933 case 64:
934 is_long_double = 1;
935 is_long = 1;
936 break;
937 default:
938 /* ISO C requires this error to be detected. */
939 __set_errno (EINVAL);
940 Xprintf_buffer_mark_failed (buf);
941 goto all_done;
943 JUMP (*f, step4_jumps);
945 /* Process current format. */
946 while (1)
948 #define process_arg_int() va_arg (ap, int)
949 #define process_arg_long_int() va_arg (ap, long int)
950 #define process_arg_long_long_int() va_arg (ap, long long int)
951 #define process_arg_pointer() va_arg (ap, void *)
952 #define process_arg_string() va_arg (ap, const char *)
953 #define process_arg_unsigned_int() va_arg (ap, unsigned int)
954 #define process_arg_unsigned_long_int() va_arg (ap, unsigned long int)
955 #define process_arg_unsigned_long_long_int() va_arg (ap, unsigned long long int)
956 #define process_arg_wchar_t() va_arg (ap, wchar_t)
957 #define process_arg_wstring() va_arg (ap, const wchar_t *)
958 #include "vfprintf-process-arg.c"
959 #undef process_arg_int
960 #undef process_arg_long_int
961 #undef process_arg_long_long_int
962 #undef process_arg_pointer
963 #undef process_arg_string
964 #undef process_arg_unsigned_int
965 #undef process_arg_unsigned_long_int
966 #undef process_arg_unsigned_long_long_int
967 #undef process_arg_wchar_t
968 #undef process_arg_wstring
970 LABEL (form_float):
971 LABEL (form_floathex):
973 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0))
974 is_long_double = 0;
976 struct printf_info info =
978 .prec = prec,
979 .width = width,
980 .spec = spec,
981 .is_long_double = is_long_double,
982 .is_short = is_short,
983 .is_long = is_long,
984 .alt = alt,
985 .space = space,
986 .left = left,
987 .showsign = showsign,
988 .group = group,
989 .pad = pad,
990 .extra = 0,
991 .i18n = use_outdigits,
992 .wide = sizeof (CHAR_T) != 1,
993 .is_binary128 = 0
996 PARSE_FLOAT_VA_ARG_EXTENDED (info);
997 const void *ptr = &the_arg;
999 __printf_fp_spec (buf, &info, &ptr);
1001 break;
1003 LABEL (form_unknown):
1004 if (spec == L_('\0'))
1006 /* The format string ended before the specifier is complete. */
1007 __set_errno (EINVAL);
1008 Xprintf_buffer_mark_failed (buf);
1009 goto all_done;
1012 /* If we are in the fast loop force entering the complicated
1013 one. */
1014 goto do_positional;
1017 /* The format is correctly handled. */
1018 ++nspecs_done;
1020 /* Look for next format specifier. */
1021 #ifdef COMPILE_WPRINTF
1022 f = __find_specwc ((end_of_spec = ++f));
1023 #else
1024 f = __find_specmb ((end_of_spec = ++f));
1025 #endif
1027 /* Write the following constant string. */
1028 Xprintf_buffer_write (buf, (const CHAR_T *) end_of_spec,
1029 f - end_of_spec);
1031 while (*f != L_('\0') && !Xprintf_buffer_has_failed (buf));
1033 all_done:
1034 /* printf_positional performs cleanup under its all_done label, so
1035 vfprintf-process-arg.c uses it for this function and
1036 printf_positional below. */
1037 return;
1039 /* Hand off processing for positional parameters. */
1040 do_positional:
1041 printf_positional (buf, format, readonly_format, ap, &ap_save,
1042 nspecs_done, lead_str_end, work_buffer,
1043 save_errno, grouping, thousands_sep, mode_flags);
1046 static void
1047 printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format,
1048 int readonly_format,
1049 va_list ap, va_list *ap_savep, int nspecs_done,
1050 const UCHAR_T *lead_str_end,
1051 CHAR_T *work_buffer, int save_errno,
1052 const char *grouping, THOUSANDS_SEP_T thousands_sep,
1053 unsigned int mode_flags)
1055 /* For positional argument handling. */
1056 struct scratch_buffer specsbuf;
1057 scratch_buffer_init (&specsbuf);
1058 struct printf_spec *specs = specsbuf.data;
1059 size_t specs_limit = specsbuf.length / sizeof (specs[0]);
1061 /* Used as a backing store for args_value, args_size, args_type
1062 below. */
1063 struct scratch_buffer argsbuf;
1064 scratch_buffer_init (&argsbuf);
1066 /* Array with information about the needed arguments. This has to
1067 be dynamically extensible. */
1068 size_t nspecs = 0;
1070 /* The number of arguments the format string requests. This will
1071 determine the size of the array needed to store the argument
1072 attributes. */
1073 size_t nargs = 0;
1075 /* Positional parameters refer to arguments directly. This could
1076 also determine the maximum number of arguments. Track the
1077 maximum number. */
1078 size_t max_ref_arg = 0;
1080 /* Just a counter. */
1081 size_t cnt;
1083 if (grouping == (const char *) -1)
1085 #ifdef COMPILE_WPRINTF
1086 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1087 _NL_NUMERIC_THOUSANDS_SEP_WC);
1088 #else
1089 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1090 #endif
1092 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1093 if (*grouping == '\0' || *grouping == CHAR_MAX)
1094 grouping = NULL;
1097 for (const UCHAR_T *f = lead_str_end; *f != L_('\0');
1098 f = specs[nspecs++].next_fmt)
1100 if (nspecs == specs_limit)
1102 if (!scratch_buffer_grow_preserve (&specsbuf))
1104 Xprintf_buffer_mark_failed (buf);
1105 goto all_done;
1107 specs = specsbuf.data;
1108 specs_limit = specsbuf.length / sizeof (specs[0]);
1111 /* Parse the format specifier. */
1112 bool failed;
1113 #ifdef COMPILE_WPRINTF
1114 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg,
1115 &failed);
1116 #else
1117 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg,
1118 &failed);
1119 #endif
1120 if (failed)
1122 Xprintf_buffer_mark_failed (buf);
1123 goto all_done;
1127 /* Determine the number of arguments the format string consumes. */
1128 nargs = MAX (nargs, max_ref_arg);
1130 union printf_arg *args_value;
1131 int *args_size;
1132 int *args_type;
1133 void *args_pa_user;
1134 size_t args_pa_user_offset;
1136 /* Calculate total size needed to represent a single argument
1137 across all three argument-related arrays. */
1138 size_t bytes_per_arg
1139 = sizeof (*args_value) + sizeof (*args_size) + sizeof (*args_type);
1140 if (!scratch_buffer_set_array_size (&argsbuf, nargs, bytes_per_arg))
1142 Xprintf_buffer_mark_failed (buf);
1143 goto all_done;
1145 args_value = argsbuf.data;
1146 /* Set up the remaining two arrays to each point past the end of
1147 the prior array, since space for all three has been allocated
1148 now. */
1149 args_size = &args_value[nargs].pa_int;
1150 args_type = &args_size[nargs];
1151 args_pa_user = &args_type[nargs];
1152 memset (args_type, (mode_flags & PRINTF_FORTIFY) != 0 ? '\xff' : '\0',
1153 nargs * sizeof (*args_type));
1156 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1157 still zero after this loop, format is invalid. For now we
1158 simply use 0 as the value. */
1160 /* Fill in the types of all the arguments. */
1161 for (cnt = 0; cnt < nspecs; ++cnt)
1163 /* If the width is determined by an argument this is an int. */
1164 if (specs[cnt].width_arg != -1)
1165 args_type[specs[cnt].width_arg] = PA_INT;
1167 /* If the precision is determined by an argument this is an int. */
1168 if (specs[cnt].prec_arg != -1)
1169 args_type[specs[cnt].prec_arg] = PA_INT;
1171 switch (specs[cnt].ndata_args)
1173 case 0: /* No arguments. */
1174 break;
1175 case 1: /* One argument; we already have the
1176 type and size. */
1177 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1178 args_size[specs[cnt].data_arg] = specs[cnt].size;
1179 break;
1180 default:
1181 /* We have more than one argument for this format spec.
1182 We must call the arginfo function again to determine
1183 all the types. */
1184 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1185 (&specs[cnt].info,
1186 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1187 &args_size[specs[cnt].data_arg]);
1188 break;
1192 /* Now we know all the types and the order. Fill in the argument
1193 values. */
1194 for (cnt = 0; cnt < nargs; ++cnt)
1195 switch (args_type[cnt])
1197 #define T(tag, mem, type) \
1198 case tag: \
1199 args_value[cnt].mem = va_arg (*ap_savep, type); \
1200 break
1202 T (PA_WCHAR, pa_wchar, wint_t);
1203 case PA_CHAR: /* Promoted. */
1204 case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1205 #if LONG_MAX == INT_MAX
1206 case PA_INT|PA_FLAG_LONG:
1207 #endif
1208 T (PA_INT, pa_int, int);
1209 #if LONG_MAX == LONG_LONG_MAX
1210 case PA_INT|PA_FLAG_LONG:
1211 #endif
1212 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1213 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1214 # error "he?"
1215 #endif
1216 case PA_FLOAT: /* Promoted. */
1217 T (PA_DOUBLE, pa_double, double);
1218 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1219 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0))
1221 args_value[cnt].pa_double = va_arg (*ap_savep, double);
1222 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1224 #if __HAVE_FLOAT128_UNLIKE_LDBL
1225 else if ((mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0)
1226 args_value[cnt].pa_float128 = va_arg (*ap_savep, _Float128);
1227 #endif
1228 else
1229 args_value[cnt].pa_long_double = va_arg (*ap_savep, long double);
1230 break;
1231 case PA_STRING: /* All pointers are the same */
1232 case PA_WSTRING: /* All pointers are the same */
1233 T (PA_POINTER, pa_pointer, void *);
1234 #undef T
1235 default:
1236 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1237 args_value[cnt].pa_pointer = va_arg (*ap_savep, void *);
1238 else if (__glibc_unlikely (__printf_va_arg_table != NULL)
1239 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1241 while (args_pa_user + args_size[cnt] >
1242 argsbuf.data + argsbuf.length)
1244 args_pa_user_offset = args_pa_user - (void *) &args_type[nargs];
1245 if (!scratch_buffer_grow_preserve (&argsbuf))
1247 Xprintf_buffer_mark_failed (buf);
1248 goto all_done;
1250 args_value = argsbuf.data;
1251 /* Set up the remaining two arrays to each point past the end of
1252 the prior array, since space for all three has been allocated
1253 now. */
1254 args_size = &args_value[nargs].pa_int;
1255 args_type = &args_size[nargs];
1256 args_pa_user = (void *) &args_type[nargs] + args_pa_user_offset;
1258 args_value[cnt].pa_user = args_pa_user;
1259 args_pa_user += args_size[cnt];
1260 (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1261 (args_value[cnt].pa_user, ap_savep);
1263 else
1264 memset (&args_value[cnt], 0, sizeof (args_value[cnt]));
1265 break;
1266 case -1:
1267 /* Error case. Not all parameters appear in N$ format
1268 strings. We have no way to determine their type. */
1269 assert ((mode_flags & PRINTF_FORTIFY) != 0);
1270 __libc_fatal ("*** invalid %N$ use detected ***\n");
1273 /* Now walk through all format specifiers and process them. */
1274 for (; (size_t) nspecs_done < nspecs && !Xprintf_buffer_has_failed (buf);
1275 ++nspecs_done)
1277 STEP4_TABLE;
1279 int is_negative;
1280 union
1282 unsigned long long int longlong;
1283 unsigned long int word;
1284 } number;
1285 int base;
1286 CHAR_T *string; /* Pointer to argument string. */
1288 /* Fill variables from values in struct. */
1289 int alt = specs[nspecs_done].info.alt;
1290 int space = specs[nspecs_done].info.space;
1291 int left = specs[nspecs_done].info.left;
1292 int showsign = specs[nspecs_done].info.showsign;
1293 int group = specs[nspecs_done].info.group;
1294 int is_long_double __attribute__ ((unused))
1295 = specs[nspecs_done].info.is_long_double;
1296 int is_short = specs[nspecs_done].info.is_short;
1297 int is_char = specs[nspecs_done].info.is_char;
1298 int is_long = specs[nspecs_done].info.is_long;
1299 int width = specs[nspecs_done].info.width;
1300 int prec = specs[nspecs_done].info.prec;
1301 int use_outdigits = specs[nspecs_done].info.i18n;
1302 char pad = specs[nspecs_done].info.pad;
1303 CHAR_T spec = specs[nspecs_done].info.spec;
1305 CHAR_T *workend = work_buffer + WORK_BUFFER_SIZE;
1307 /* Fill in last information. */
1308 if (specs[nspecs_done].width_arg != -1)
1310 /* Extract the field width from an argument. */
1311 specs[nspecs_done].info.width =
1312 args_value[specs[nspecs_done].width_arg].pa_int;
1314 if (specs[nspecs_done].info.width < 0)
1315 /* If the width value is negative left justification is
1316 selected and the value is taken as being positive. */
1318 specs[nspecs_done].info.width *= -1;
1319 left = specs[nspecs_done].info.left = 1;
1321 width = specs[nspecs_done].info.width;
1324 if (specs[nspecs_done].prec_arg != -1)
1326 /* Extract the precision from an argument. */
1327 specs[nspecs_done].info.prec =
1328 args_value[specs[nspecs_done].prec_arg].pa_int;
1330 if (specs[nspecs_done].info.prec < 0)
1331 /* If the precision is negative the precision is
1332 omitted. */
1333 specs[nspecs_done].info.prec = -1;
1335 prec = specs[nspecs_done].info.prec;
1338 /* Process format specifiers. */
1341 if (spec <= UCHAR_MAX
1342 && __printf_function_table != NULL
1343 && __printf_function_table[(size_t) spec] != NULL)
1345 int function_done
1346 = Xprintf (function_invoke) (buf,
1347 __printf_function_table[(size_t) spec],
1348 &args_value[specs[nspecs_done]
1349 .data_arg],
1350 specs[nspecs_done].ndata_args,
1351 &specs[nspecs_done].info);
1352 if (function_done != -2)
1354 /* If an error occurred we don't have information
1355 about # of chars. */
1356 if (function_done < 0)
1358 /* Function has set errno. */
1359 Xprintf_buffer_mark_failed (buf);
1360 goto all_done;
1362 break;
1366 JUMP (spec, step4_jumps);
1368 #define process_arg_data args_value[specs[nspecs_done].data_arg]
1369 #define process_arg_int() process_arg_data.pa_int
1370 #define process_arg_long_int() process_arg_data.pa_long_int
1371 #define process_arg_long_long_int() process_arg_data.pa_long_long_int
1372 #define process_arg_pointer() process_arg_data.pa_pointer
1373 #define process_arg_string() process_arg_data.pa_string
1374 #define process_arg_unsigned_int() process_arg_data.pa_u_int
1375 #define process_arg_unsigned_long_int() process_arg_data.pa_u_long_int
1376 #define process_arg_unsigned_long_long_int() process_arg_data.pa_u_long_long_int
1377 #define process_arg_wchar_t() process_arg_data.pa_wchar
1378 #define process_arg_wstring() process_arg_data.pa_wstring
1379 #include "vfprintf-process-arg.c"
1380 #undef process_arg_data
1381 #undef process_arg_int
1382 #undef process_arg_long_int
1383 #undef process_arg_long_long_int
1384 #undef process_arg_pointer
1385 #undef process_arg_string
1386 #undef process_arg_unsigned_int
1387 #undef process_arg_unsigned_long_int
1388 #undef process_arg_unsigned_long_long_int
1389 #undef process_arg_wchar_t
1390 #undef process_arg_wstring
1392 LABEL (form_float):
1393 LABEL (form_floathex):
1395 const void *ptr
1396 = (const void *) &args_value[specs[nspecs_done].data_arg];
1397 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0))
1399 specs[nspecs_done].data_arg_type = PA_DOUBLE;
1400 specs[nspecs_done].info.is_long_double = 0;
1402 SETUP_FLOAT128_INFO (specs[nspecs_done].info);
1404 __printf_fp_spec (buf, &specs[nspecs_done].info, &ptr);
1406 break;
1408 LABEL (form_unknown):
1410 printf_unknown (buf, &specs[nspecs_done].info);
1412 break;
1414 while (Xprintf_buffer_has_failed (buf));
1416 /* Write the following constant string. */
1417 Xprintf_buffer_write (buf,
1418 (const CHAR_T *) specs[nspecs_done].end_of_fmt,
1419 (specs[nspecs_done].next_fmt
1420 - specs[nspecs_done].end_of_fmt));
1422 all_done:
1423 scratch_buffer_free (&argsbuf);
1424 scratch_buffer_free (&specsbuf);
1427 /* Handle an unknown format specifier. This prints out a canonicalized
1428 representation of the format spec itself. */
1429 static void
1430 printf_unknown (struct Xprintf_buffer *buf, const struct printf_info *info)
1432 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
1433 CHAR_T *const workend
1434 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1435 CHAR_T *w;
1437 Xprintf_buffer_putc (buf, L_('%'));
1439 if (info->alt)
1440 Xprintf_buffer_putc (buf, L_('#'));
1441 if (info->group)
1442 Xprintf_buffer_putc (buf, L_('\''));
1443 if (info->showsign)
1444 Xprintf_buffer_putc (buf, L_('+'));
1445 else if (info->space)
1446 Xprintf_buffer_putc (buf, L_(' '));
1447 if (info->left)
1448 Xprintf_buffer_putc (buf, L_('-'));
1449 if (info->pad == L_('0'))
1450 Xprintf_buffer_putc (buf, L_('0'));
1451 if (info->i18n)
1452 Xprintf_buffer_putc (buf, L_('I'));
1454 if (info->width != 0)
1456 w = _itoa_word (info->width, workend, 10, 0);
1457 while (w < workend)
1458 Xprintf_buffer_putc (buf, *w++);
1461 if (info->prec != -1)
1463 Xprintf_buffer_putc (buf, L_('.'));
1464 w = _itoa_word (info->prec, workend, 10, 0);
1465 while (w < workend)
1466 Xprintf_buffer_putc (buf, *w++);
1469 if (info->spec != L_('\0'))
1470 Xprintf_buffer_putc (buf, info->spec);
1473 static void
1474 group_number (struct Xprintf_buffer *buf,
1475 struct grouping_iterator *iter,
1476 CHAR_T *from, CHAR_T *to, THOUSANDS_SEP_T thousands_sep,
1477 bool i18n)
1479 if (!i18n)
1480 for (CHAR_T *cp = from; cp != to; ++cp)
1482 if (__grouping_iterator_next (iter))
1484 #ifdef COMPILE_WPRINTF
1485 __wprintf_buffer_putc (buf, thousands_sep);
1486 #else
1487 __printf_buffer_puts (buf, thousands_sep);
1488 #endif
1490 Xprintf_buffer_putc (buf, *cp);
1492 else
1494 /* Apply digit translation and grouping. */
1495 for (CHAR_T *cp = from; cp != to; ++cp)
1497 if (__grouping_iterator_next (iter))
1499 #ifdef COMPILE_WPRINTF
1500 __wprintf_buffer_putc (buf, thousands_sep);
1501 #else
1502 __printf_buffer_puts (buf, thousands_sep);
1503 #endif
1505 int digit = *cp - '0';
1506 #ifdef COMPILE_WPRINTF
1507 __wprintf_buffer_putc
1508 (buf, _NL_CURRENT_WORD (LC_CTYPE,
1509 _NL_CTYPE_OUTDIGIT0_WC + digit));
1510 #else
1511 __printf_buffer_puts
1512 (buf, _NL_CURRENT (LC_CTYPE, _NL_CTYPE_OUTDIGIT0_MB + digit));
1513 #endif
1519 /* The FILE-based function. */
1521 vfprintf (FILE *s, const CHAR_T *format, va_list ap, unsigned int mode_flags)
1523 /* Orient the stream. */
1524 #ifdef ORIENT
1525 ORIENT;
1526 #endif
1528 /* Sanity check of arguments. */
1529 ARGCHECK (s, format);
1531 #ifdef ORIENT
1532 /* Check for correct orientation. */
1533 if (_IO_vtable_offset (s) == 0
1534 && _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1535 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1536 /* The stream is already oriented otherwise. */
1537 return EOF;
1538 #endif
1540 if (!_IO_need_lock (s))
1542 struct Xprintf (buffer_to_file) wrap;
1543 Xprintf (buffer_to_file_init) (&wrap, s);
1544 Xprintf_buffer (&wrap.base, format, ap, mode_flags);
1545 return Xprintf (buffer_to_file_done) (&wrap);
1548 int done;
1550 /* Lock stream. */
1551 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1552 _IO_flockfile (s);
1554 /* Set up the wrapping buffer. */
1555 struct Xprintf (buffer_to_file) wrap;
1556 Xprintf (buffer_to_file_init) (&wrap, s);
1558 /* Perform the printing operation on the buffer. */
1559 Xprintf_buffer (&wrap.base, format, ap, mode_flags);
1560 done = Xprintf (buffer_to_file_done) (&wrap);
1562 /* Unlock the stream. */
1563 _IO_funlockfile (s);
1564 _IO_cleanup_region_end (0);
1566 return done;