1 /* Helper functions for parsing printf format strings.
2 Copyright (C) 1995-2017 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 <http://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
)
61 __parse_one_specmb (const UCHAR_T
*format
, size_t posn
,
62 struct printf_spec
*spec
, size_t *max_ref_arg
)
71 /* Clear information structure. */
76 spec
->info
.showsign
= 0;
81 spec
->info
.wide
= sizeof (UCHAR_T
) > 1;
83 /* Test for positional argument. */
84 if (ISDIGIT (*format
))
86 const UCHAR_T
*begin
= format
;
88 n
= read_int (&format
);
90 if (n
!= 0 && *format
== L_('$'))
91 /* Is positional parameter. */
93 ++format
; /* Skip the '$'. */
96 spec
->data_arg
= n
- 1;
97 *max_ref_arg
= MAX (*max_ref_arg
, n
);
101 /* Oops; that was actually the width and/or 0 padding flag.
102 Step back and read it again. */
106 /* Check for spec modifiers. */
112 /* Output a space in place of a sign, when there is no sign. */
113 spec
->info
.space
= 1;
116 /* Always output + or - for numbers. */
117 spec
->info
.showsign
= 1;
120 /* Left-justify things. */
124 /* Use the "alternate form":
125 Hex has 0x or 0X, FP always has a decimal point. */
130 spec
->info
.pad
= '0';
133 /* Show grouping in numbers if the locale information
135 spec
->info
.group
= 1;
138 /* Use the internationalized form of the output. Currently
139 means to use the `outdigits' of the current locale. */
150 spec
->info
.pad
= ' ';
152 /* Get the field width. */
153 spec
->width_arg
= -1;
154 spec
->info
.width
= 0;
155 if (*format
== L_('*'))
157 /* The field width is given in an argument.
158 A negative field width indicates left justification. */
159 const UCHAR_T
*begin
= ++format
;
161 if (ISDIGIT (*format
))
163 /* The width argument might be found in a positional parameter. */
164 n
= read_int (&format
);
166 if (n
!= 0 && *format
== L_('$'))
170 spec
->width_arg
= n
- 1;
171 *max_ref_arg
= MAX (*max_ref_arg
, n
);
173 ++format
; /* Skip '$'. */
177 if (spec
->width_arg
< 0)
179 /* Not in a positional parameter. Consume one argument. */
180 spec
->width_arg
= posn
++;
182 format
= begin
; /* Step back and reread. */
185 else if (ISDIGIT (*format
))
187 int n
= read_int (&format
);
189 /* Constant width specification. */
191 spec
->info
.width
= n
;
193 /* Get the precision. */
195 /* -1 means none given; 0 means explicit 0. */
196 spec
->info
.prec
= -1;
197 if (*format
== L_('.'))
200 if (*format
== L_('*'))
202 /* The precision is given in an argument. */
203 const UCHAR_T
*begin
= ++format
;
205 if (ISDIGIT (*format
))
207 n
= read_int (&format
);
209 if (n
!= 0 && *format
== L_('$'))
213 spec
->prec_arg
= n
- 1;
214 *max_ref_arg
= MAX (*max_ref_arg
, n
);
220 if (spec
->prec_arg
< 0)
222 /* Not in a positional parameter. */
223 spec
->prec_arg
= posn
++;
228 else if (ISDIGIT (*format
))
230 int n
= read_int (&format
);
236 /* "%.?" is treated like "%.0?". */
240 /* Check for type modifiers. */
241 spec
->info
.is_long_double
= 0;
242 spec
->info
.is_short
= 0;
243 spec
->info
.is_long
= 0;
244 spec
->info
.is_char
= 0;
247 if (__builtin_expect (__printf_modifier_table
== NULL
, 1)
248 || __printf_modifier_table
[*format
] == NULL
249 || HANDLE_REGISTERED_MODIFIER (&format
, &spec
->info
) != 0)
253 /* ints are short ints or chars. */
254 if (*format
!= L_('h'))
255 spec
->info
.is_short
= 1;
259 spec
->info
.is_char
= 1;
263 /* ints are long ints. */
264 spec
->info
.is_long
= 1;
265 if (*format
!= L_('l'))
270 /* doubles are long doubles, and ints are long long ints. */
272 /* 4.4 uses this for long long. */
273 spec
->info
.is_long_double
= 1;
277 /* ints are size_ts. */
278 assert (sizeof (size_t) <= sizeof (unsigned long long int));
279 #if LONG_MAX != LONG_LONG_MAX
280 spec
->info
.is_long_double
= (sizeof (size_t)
281 > sizeof (unsigned long int));
283 spec
->info
.is_long
= sizeof (size_t) > sizeof (unsigned int);
286 assert (sizeof (ptrdiff_t) <= sizeof (long long int));
287 #if LONG_MAX != LONG_LONG_MAX
288 spec
->info
.is_long_double
= (sizeof (ptrdiff_t) > sizeof (long int));
290 spec
->info
.is_long
= sizeof (ptrdiff_t) > sizeof (int);
293 assert (sizeof (uintmax_t) <= sizeof (unsigned long long int));
294 #if LONG_MAX != LONG_LONG_MAX
295 spec
->info
.is_long_double
= (sizeof (uintmax_t)
296 > sizeof (unsigned long int));
298 spec
->info
.is_long
= sizeof (uintmax_t) > sizeof (unsigned int);
301 /* Not a recognized modifier. Backup. */
306 /* Get the format specification. */
307 spec
->info
.spec
= (wchar_t) *format
++;
309 if (__builtin_expect (__printf_function_table
== NULL
, 1)
310 || spec
->info
.spec
> UCHAR_MAX
311 || __printf_arginfo_table
[spec
->info
.spec
] == NULL
312 /* We don't try to get the types for all arguments if the format
313 uses more than one. The normal case is covered though. If
314 the call returns -1 we continue with the normal specifiers. */
315 || (int) (spec
->ndata_args
= (*__printf_arginfo_table
[spec
->info
.spec
])
316 (&spec
->info
, 1, &spec
->data_arg_type
,
319 /* Find the data argument types of a built-in spec. */
320 spec
->ndata_args
= 1;
322 switch (spec
->info
.spec
)
330 #if LONG_MAX != LONG_LONG_MAX
331 if (spec
->info
.is_long_double
)
332 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG_LONG
;
335 if (spec
->info
.is_long
)
336 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG
;
337 else if (spec
->info
.is_short
)
338 spec
->data_arg_type
= PA_INT
|PA_FLAG_SHORT
;
339 else if (spec
->info
.is_char
)
340 spec
->data_arg_type
= PA_CHAR
;
342 spec
->data_arg_type
= PA_INT
;
352 if (spec
->info
.is_long_double
)
353 spec
->data_arg_type
= PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
;
355 spec
->data_arg_type
= PA_DOUBLE
;
358 spec
->data_arg_type
= PA_CHAR
;
361 spec
->data_arg_type
= PA_WCHAR
;
364 spec
->data_arg_type
= PA_STRING
;
367 spec
->data_arg_type
= PA_WSTRING
;
370 spec
->data_arg_type
= PA_POINTER
;
373 spec
->data_arg_type
= PA_INT
|PA_FLAG_PTR
;
378 /* An unknown spec will consume no args. */
379 spec
->ndata_args
= 0;
384 if (spec
->data_arg
== -1 && spec
->ndata_args
> 0)
386 /* There are args consumed, but no positional spec. Use the
387 next sequential arg position. */
388 spec
->data_arg
= posn
;
389 nargs
+= spec
->ndata_args
;
392 if (spec
->info
.spec
== L
'\0')
393 /* Format ended before this spec was complete. */
394 spec
->end_of_fmt
= spec
->next_fmt
= format
- 1;
397 /* Find the next format spec. */
398 spec
->end_of_fmt
= format
;
399 #ifdef COMPILE_WPRINTF
400 spec
->next_fmt
= __find_specwc (format
);
402 spec
->next_fmt
= __find_specmb (format
);