1 /* Helper functions for parsing printf format strings.
2 Copyright (C) 1995-2000,2002-2004,2006,2009 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include <sys/param.h>
28 #ifndef COMPILE_WPRINTF
30 # define UCHAR_T unsigned char
33 # define ISDIGIT(Ch) isdigit (Ch)
34 # define HANDLE_REGISTERED_MODIFIER __handle_registered_modifier_mb
36 # define CHAR_T wchar_t
37 # define UCHAR_T unsigned int
39 # define L_(Str) L##Str
40 # define ISDIGIT(Ch) iswdigit (Ch)
41 # define HANDLE_REGISTERED_MODIFIER __handle_registered_modifier_wc
44 #include "printf-parse.h"
51 /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
52 with the parsed details. POSN is the number of arguments already
53 consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
54 the number of args consumed by this spec; *MAX_REF_ARG is updated so it
55 remains the highest argument index used. */
58 #ifdef COMPILE_WPRINTF
59 __parse_one_specwc (const UCHAR_T
*format
, size_t posn
,
60 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
)
72 /* Clear information structure. */
77 spec
->info
.showsign
= 0;
82 spec
->info
.wide
= sizeof (UCHAR_T
) > 1;
84 /* Test for positional argument. */
85 if (ISDIGIT (*format
))
87 const UCHAR_T
*begin
= format
;
89 n
= read_int (&format
);
91 if (n
> 0 && *format
== L_('$'))
92 /* Is positional parameter. */
94 ++format
; /* Skip the '$'. */
95 spec
->data_arg
= n
- 1;
96 *max_ref_arg
= MAX (*max_ref_arg
, n
);
99 /* Oops; that was actually the width and/or 0 padding flag.
100 Step back and read it again. */
104 /* Check for spec modifiers. */
110 /* Output a space in place of a sign, when there is no sign. */
111 spec
->info
.space
= 1;
114 /* Always output + or - for numbers. */
115 spec
->info
.showsign
= 1;
118 /* Left-justify things. */
122 /* Use the "alternate form":
123 Hex has 0x or 0X, FP always has a decimal point. */
128 spec
->info
.pad
= '0';
131 /* Show grouping in numbers if the locale information
133 spec
->info
.group
= 1;
136 /* Use the internationalized form of the output. Currently
137 means to use the `outdigits' of the current locale. */
148 spec
->info
.pad
= ' ';
150 /* Get the field width. */
151 spec
->width_arg
= -1;
152 spec
->info
.width
= 0;
153 if (*format
== L_('*'))
155 /* The field width is given in an argument.
156 A negative field width indicates left justification. */
157 const UCHAR_T
*begin
= ++format
;
159 if (ISDIGIT (*format
))
161 /* The width argument might be found in a positional parameter. */
162 n
= read_int (&format
);
164 if (n
> 0 && *format
== L_('$'))
166 spec
->width_arg
= n
- 1;
167 *max_ref_arg
= MAX (*max_ref_arg
, n
);
168 ++format
; /* Skip '$'. */
172 if (spec
->width_arg
< 0)
174 /* Not in a positional parameter. Consume one argument. */
175 spec
->width_arg
= posn
++;
177 format
= begin
; /* Step back and reread. */
180 else if (ISDIGIT (*format
))
181 /* Constant width specification. */
182 spec
->info
.width
= read_int (&format
);
184 /* Get the precision. */
186 /* -1 means none given; 0 means explicit 0. */
187 spec
->info
.prec
= -1;
188 if (*format
== L_('.'))
191 if (*format
== L_('*'))
193 /* The precision is given in an argument. */
194 const UCHAR_T
*begin
= ++format
;
196 if (ISDIGIT (*format
))
198 n
= read_int (&format
);
200 if (n
> 0 && *format
== L_('$'))
202 spec
->prec_arg
= n
- 1;
203 *max_ref_arg
= MAX (*max_ref_arg
, n
);
208 if (spec
->prec_arg
< 0)
210 /* Not in a positional parameter. */
211 spec
->prec_arg
= posn
++;
216 else if (ISDIGIT (*format
))
217 spec
->info
.prec
= read_int (&format
);
219 /* "%.?" is treated like "%.0?". */
223 /* Check for type modifiers. */
224 spec
->info
.is_long_double
= 0;
225 spec
->info
.is_short
= 0;
226 spec
->info
.is_long
= 0;
227 spec
->info
.is_char
= 0;
230 if (__builtin_expect (__printf_modifier_table
== NULL
, 1)
231 || __printf_modifier_table
[*format
] == NULL
232 || HANDLE_REGISTERED_MODIFIER (&format
, &spec
->info
) != 0)
236 /* ints are short ints or chars. */
237 if (*format
!= L_('h'))
238 spec
->info
.is_short
= 1;
242 spec
->info
.is_char
= 1;
246 /* ints are long ints. */
247 spec
->info
.is_long
= 1;
248 if (*format
!= L_('l'))
253 /* doubles are long doubles, and ints are long long ints. */
255 /* 4.4 uses this for long long. */
256 spec
->info
.is_long_double
= 1;
260 /* ints are size_ts. */
261 assert (sizeof (size_t) <= sizeof (unsigned long long int));
262 #if LONG_MAX != LONG_LONG_MAX
263 spec
->info
.is_long_double
= (sizeof (size_t)
264 > sizeof (unsigned long int));
266 spec
->info
.is_long
= sizeof (size_t) > sizeof (unsigned int);
269 assert (sizeof (ptrdiff_t) <= sizeof (long long int));
270 #if LONG_MAX != LONG_LONG_MAX
271 spec
->info
.is_long_double
= (sizeof (ptrdiff_t) > sizeof (long int));
273 spec
->info
.is_long
= sizeof (ptrdiff_t) > sizeof (int);
276 assert (sizeof (uintmax_t) <= sizeof (unsigned long long int));
277 #if LONG_MAX != LONG_LONG_MAX
278 spec
->info
.is_long_double
= (sizeof (uintmax_t)
279 > sizeof (unsigned long int));
281 spec
->info
.is_long
= sizeof (uintmax_t) > sizeof (unsigned int);
284 /* Not a recognized modifier. Backup. */
289 /* Get the format specification. */
290 spec
->info
.spec
= (wchar_t) *format
++;
292 if (__builtin_expect (__printf_function_table
== NULL
, 1)
293 || spec
->info
.spec
> UCHAR_MAX
294 || __printf_arginfo_table
[spec
->info
.spec
] == NULL
295 /* We don't try to get the types for all arguments if the format
296 uses more than one. The normal case is covered though. If
297 the call returns -1 we continue with the normal specifiers. */
298 || (int) (spec
->ndata_args
= (*__printf_arginfo_table
[spec
->info
.spec
])
299 (&spec
->info
, 1, &spec
->data_arg_type
,
302 /* Find the data argument types of a built-in spec. */
303 spec
->ndata_args
= 1;
305 switch (spec
->info
.spec
)
313 #if LONG_MAX != LONG_LONG_MAX
314 if (spec
->info
.is_long_double
)
315 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG_LONG
;
318 if (spec
->info
.is_long
)
319 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG
;
320 else if (spec
->info
.is_short
)
321 spec
->data_arg_type
= PA_INT
|PA_FLAG_SHORT
;
322 else if (spec
->info
.is_char
)
323 spec
->data_arg_type
= PA_CHAR
;
325 spec
->data_arg_type
= PA_INT
;
335 if (spec
->info
.is_long_double
)
336 spec
->data_arg_type
= PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
;
338 spec
->data_arg_type
= PA_DOUBLE
;
341 spec
->data_arg_type
= PA_CHAR
;
344 spec
->data_arg_type
= PA_WCHAR
;
347 spec
->data_arg_type
= PA_STRING
;
350 spec
->data_arg_type
= PA_WSTRING
;
353 spec
->data_arg_type
= PA_POINTER
;
356 spec
->data_arg_type
= PA_INT
|PA_FLAG_PTR
;
361 /* An unknown spec will consume no args. */
362 spec
->ndata_args
= 0;
367 if (spec
->data_arg
== -1 && spec
->ndata_args
> 0)
369 /* There are args consumed, but no positional spec. Use the
370 next sequential arg position. */
371 spec
->data_arg
= posn
;
372 nargs
+= spec
->ndata_args
;
375 if (spec
->info
.spec
== L
'\0')
376 /* Format ended before this spec was complete. */
377 spec
->end_of_fmt
= spec
->next_fmt
= format
- 1;
380 /* Find the next format spec. */
381 spec
->end_of_fmt
= format
;
382 #ifdef COMPILE_WPRINTF
383 spec
->next_fmt
= __find_specwc (format
);
385 spec
->next_fmt
= __find_specmb (format
);