1 /* Helper functions for parsing printf format strings.
2 Copyright (C) 1995-2023 Free Software Foundation, Inc.
3 This file is part of th GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
23 #include <sys/param.h>
27 #ifndef COMPILE_WPRINTF
29 # define UCHAR_T unsigned char
32 # define ISDIGIT(Ch) isdigit (Ch)
33 # define HANDLE_REGISTERED_MODIFIER __handle_registered_modifier_mb
35 # define CHAR_T wchar_t
36 # define UCHAR_T unsigned int
38 # define L_(Str) L##Str
39 # define ISDIGIT(Ch) iswdigit (Ch)
40 # define HANDLE_REGISTERED_MODIFIER __handle_registered_modifier_wc
43 #include "printf-parse.h"
50 /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
51 with the parsed details. POSN is the number of arguments already
52 consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
53 the number of args consumed by this spec; *MAX_REF_ARG is updated so it
54 remains the highest argument index used. */
57 #ifdef COMPILE_WPRINTF
58 __parse_one_specwc (const UCHAR_T
*format
, size_t posn
,
59 struct printf_spec
*spec
, size_t *max_ref_arg
,
62 __parse_one_specmb (const UCHAR_T
*format
, size_t posn
,
63 struct printf_spec
*spec
, size_t *max_ref_arg
,
74 /* Clear information structure. */
79 spec
->info
.showsign
= 0;
84 spec
->info
.wide
= sizeof (UCHAR_T
) > 1;
85 spec
->info
.is_binary128
= 0;
89 /* Test for positional argument. */
90 if (ISDIGIT (*format
))
92 const UCHAR_T
*begin
= format
;
94 n
= read_int (&format
);
96 if (n
!= 0 && *format
== L_('$'))
97 /* Is positional parameter. */
99 ++format
; /* Skip the '$'. */
102 spec
->data_arg
= n
- 1;
103 *max_ref_arg
= MAX (*max_ref_arg
, n
);
107 /* Oops; that was actually the width and/or 0 padding flag.
108 Step back and read it again. */
112 /* Check for spec modifiers. */
118 /* Output a space in place of a sign, when there is no sign. */
119 spec
->info
.space
= 1;
122 /* Always output + or - for numbers. */
123 spec
->info
.showsign
= 1;
126 /* Left-justify things. */
130 /* Use the "alternate form":
131 Hex has 0x or 0X, FP always has a decimal point. */
136 spec
->info
.pad
= '0';
139 /* Show grouping in numbers if the locale information
141 spec
->info
.group
= 1;
144 /* Use the internationalized form of the output. Currently
145 means to use the `outdigits' of the current locale. */
156 spec
->info
.pad
= ' ';
158 /* Get the field width. */
159 spec
->width_arg
= -1;
160 spec
->info
.width
= 0;
161 if (*format
== L_('*'))
163 /* The field width is given in an argument.
164 A negative field width indicates left justification. */
165 const UCHAR_T
*begin
= ++format
;
167 if (ISDIGIT (*format
))
169 /* The width argument might be found in a positional parameter. */
170 n
= read_int (&format
);
172 if (n
!= 0 && *format
== L_('$'))
176 spec
->width_arg
= n
- 1;
177 *max_ref_arg
= MAX (*max_ref_arg
, n
);
179 ++format
; /* Skip '$'. */
183 if (spec
->width_arg
< 0)
185 /* Not in a positional parameter. Consume one argument. */
186 spec
->width_arg
= posn
++;
188 format
= begin
; /* Step back and reread. */
191 else if (ISDIGIT (*format
))
193 int n
= read_int (&format
);
195 /* Constant width specification. */
197 spec
->info
.width
= n
;
199 /* Get the precision. */
201 /* -1 means none given; 0 means explicit 0. */
202 spec
->info
.prec
= -1;
203 if (*format
== L_('.'))
206 if (*format
== L_('*'))
208 /* The precision is given in an argument. */
209 const UCHAR_T
*begin
= ++format
;
211 if (ISDIGIT (*format
))
213 n
= read_int (&format
);
215 if (n
!= 0 && *format
== L_('$'))
219 spec
->prec_arg
= n
- 1;
220 *max_ref_arg
= MAX (*max_ref_arg
, n
);
226 if (spec
->prec_arg
< 0)
228 /* Not in a positional parameter. */
229 spec
->prec_arg
= posn
++;
234 else if (ISDIGIT (*format
))
236 int n
= read_int (&format
);
242 /* "%.?" is treated like "%.0?". */
246 /* Check for type modifiers. */
247 spec
->info
.is_long_double
= 0;
248 spec
->info
.is_short
= 0;
249 spec
->info
.is_long
= 0;
250 spec
->info
.is_char
= 0;
253 if (__builtin_expect (__printf_modifier_table
== NULL
, 1)
254 || __printf_modifier_table
[*format
] == NULL
255 || HANDLE_REGISTERED_MODIFIER (&format
, &spec
->info
) != 0)
259 /* ints are short ints or chars. */
260 if (*format
!= L_('h'))
261 spec
->info
.is_short
= 1;
265 spec
->info
.is_char
= 1;
269 /* ints are long ints. */
270 spec
->info
.is_long
= 1;
271 if (*format
!= L_('l'))
276 /* doubles are long doubles, and ints are long long ints. */
278 /* 4.4 uses this for long long. */
279 spec
->info
.is_long_double
= 1;
283 /* ints are size_ts. */
284 assert (sizeof (size_t) <= sizeof (unsigned long long int));
285 #if LONG_MAX != LONG_LONG_MAX
286 spec
->info
.is_long_double
= (sizeof (size_t)
287 > sizeof (unsigned long int));
289 spec
->info
.is_long
= sizeof (size_t) > sizeof (unsigned int);
292 assert (sizeof (ptrdiff_t) <= sizeof (long long int));
293 #if LONG_MAX != LONG_LONG_MAX
294 spec
->info
.is_long_double
= (sizeof (ptrdiff_t) > sizeof (long int));
296 spec
->info
.is_long
= sizeof (ptrdiff_t) > sizeof (int);
299 assert (sizeof (uintmax_t) <= sizeof (unsigned long long int));
300 #if LONG_MAX != LONG_LONG_MAX
301 spec
->info
.is_long_double
= (sizeof (uintmax_t)
302 > sizeof (unsigned long int));
304 spec
->info
.is_long
= sizeof (uintmax_t) > sizeof (unsigned int);
308 if (*format
== L_('f'))
314 if (ISDIGIT (*format
))
315 bitwidth
= read_int (&format
);
320 bitwidth
= INT_FAST8_WIDTH
;
323 bitwidth
= INT_FAST16_WIDTH
;
326 bitwidth
= INT_FAST32_WIDTH
;
329 bitwidth
= INT_FAST64_WIDTH
;
335 spec
->info
.is_char
= 1;
338 spec
->info
.is_short
= 1;
343 spec
->info
.is_long_double
= 1;
344 spec
->info
.is_long
= 1;
347 /* ISO C requires this error to be detected. */
348 __set_errno (EINVAL
);
354 /* Not a recognized modifier. Backup. */
359 /* Get the format specification. */
360 spec
->info
.spec
= (wchar_t) *format
++;
362 if (__builtin_expect (__printf_function_table
== NULL
, 1)
363 || spec
->info
.spec
> UCHAR_MAX
364 || __printf_arginfo_table
[spec
->info
.spec
] == NULL
365 /* We don't try to get the types for all arguments if the format
366 uses more than one. The normal case is covered though. If
367 the call returns -1 we continue with the normal specifiers. */
368 || (int) (spec
->ndata_args
= (*__printf_arginfo_table
[spec
->info
.spec
])
369 (&spec
->info
, 1, &spec
->data_arg_type
,
372 /* Find the data argument types of a built-in spec. */
373 spec
->ndata_args
= 1;
375 switch (spec
->info
.spec
)
385 #if LONG_MAX != LONG_LONG_MAX
386 if (spec
->info
.is_long_double
)
387 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG_LONG
;
390 if (spec
->info
.is_long
)
391 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG
;
392 else if (spec
->info
.is_short
)
393 spec
->data_arg_type
= PA_INT
|PA_FLAG_SHORT
;
394 else if (spec
->info
.is_char
)
395 spec
->data_arg_type
= PA_CHAR
;
397 spec
->data_arg_type
= PA_INT
;
407 if (spec
->info
.is_long_double
)
408 spec
->data_arg_type
= PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
;
410 spec
->data_arg_type
= PA_DOUBLE
;
413 spec
->data_arg_type
= PA_CHAR
;
416 spec
->data_arg_type
= PA_WCHAR
;
419 spec
->data_arg_type
= PA_STRING
;
422 spec
->data_arg_type
= PA_WSTRING
;
425 spec
->data_arg_type
= PA_POINTER
;
428 spec
->data_arg_type
= PA_INT
|PA_FLAG_PTR
;
433 /* An unknown spec will consume no args. */
434 spec
->ndata_args
= 0;
439 if (spec
->data_arg
== -1 && spec
->ndata_args
> 0)
441 /* There are args consumed, but no positional spec. Use the
442 next sequential arg position. */
443 spec
->data_arg
= posn
;
444 nargs
+= spec
->ndata_args
;
447 if (spec
->info
.spec
== L
'\0')
448 /* Format ended before this spec was complete. */
449 spec
->end_of_fmt
= spec
->next_fmt
= format
- 1;
452 /* Find the next format spec. */
453 spec
->end_of_fmt
= format
;
454 #ifdef COMPILE_WPRINTF
455 spec
->next_fmt
= __find_specwc (format
);
457 spec
->next_fmt
= __find_specmb (format
);