1 /* Helper functions for parsing printf format strings.
2 Copyright (C) 1995-2000,2002,2003,2004,2006 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)
35 # define CHAR_T wchar_t
36 # define UCHAR_T unsigned int
38 # define L_(Str) L##Str
39 # define ISDIGIT(Ch) iswdigit (Ch)
42 #include "printf-parse.h"
49 /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
50 with the parsed details. POSN is the number of arguments already
51 consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
52 the number of args consumed by this spec; *MAX_REF_ARG is updated so it
53 remains the highest argument index used. */
56 #ifdef COMPILE_WPRINTF
57 __parse_one_specwc (const UCHAR_T
*format
, size_t posn
,
58 struct printf_spec
*spec
, size_t *max_ref_arg
)
60 __parse_one_specmb (const UCHAR_T
*format
, size_t posn
,
61 struct printf_spec
*spec
, size_t *max_ref_arg
)
70 /* Clear information structure. */
75 spec
->info
.showsign
= 0;
80 spec
->info
.wide
= sizeof (UCHAR_T
) > 1;
82 /* Test for positional argument. */
83 if (ISDIGIT (*format
))
85 const UCHAR_T
*begin
= format
;
87 n
= read_int (&format
);
89 if (n
> 0 && *format
== L_('$'))
90 /* Is positional parameter. */
92 ++format
; /* Skip the '$'. */
93 spec
->data_arg
= n
- 1;
94 *max_ref_arg
= MAX (*max_ref_arg
, n
);
97 /* Oops; that was actually the width and/or 0 padding flag.
98 Step back and read it again. */
102 /* Check for spec modifiers. */
108 /* Output a space in place of a sign, when there is no sign. */
109 spec
->info
.space
= 1;
112 /* Always output + or - for numbers. */
113 spec
->info
.showsign
= 1;
116 /* Left-justify things. */
120 /* Use the "alternate form":
121 Hex has 0x or 0X, FP always has a decimal point. */
126 spec
->info
.pad
= '0';
129 /* Show grouping in numbers if the locale information
131 spec
->info
.group
= 1;
134 /* Use the internationalized form of the output. Currently
135 means to use the `outdigits' of the current locale. */
146 spec
->info
.pad
= ' ';
148 /* Get the field width. */
149 spec
->width_arg
= -1;
150 spec
->info
.width
= 0;
151 if (*format
== L_('*'))
153 /* The field width is given in an argument.
154 A negative field width indicates left justification. */
155 const UCHAR_T
*begin
= ++format
;
157 if (ISDIGIT (*format
))
159 /* The width argument might be found in a positional parameter. */
160 n
= read_int (&format
);
162 if (n
> 0 && *format
== L_('$'))
164 spec
->width_arg
= n
- 1;
165 *max_ref_arg
= MAX (*max_ref_arg
, n
);
166 ++format
; /* Skip '$'. */
170 if (spec
->width_arg
< 0)
172 /* Not in a positional parameter. Consume one argument. */
173 spec
->width_arg
= posn
++;
175 format
= begin
; /* Step back and reread. */
178 else if (ISDIGIT (*format
))
179 /* Constant width specification. */
180 spec
->info
.width
= read_int (&format
);
182 /* Get the precision. */
184 /* -1 means none given; 0 means explicit 0. */
185 spec
->info
.prec
= -1;
186 if (*format
== L_('.'))
189 if (*format
== L_('*'))
191 /* The precision is given in an argument. */
192 const UCHAR_T
*begin
= ++format
;
194 if (ISDIGIT (*format
))
196 n
= read_int (&format
);
198 if (n
> 0 && *format
== L_('$'))
200 spec
->prec_arg
= n
- 1;
201 *max_ref_arg
= MAX (*max_ref_arg
, n
);
206 if (spec
->prec_arg
< 0)
208 /* Not in a positional parameter. */
209 spec
->prec_arg
= posn
++;
214 else if (ISDIGIT (*format
))
215 spec
->info
.prec
= read_int (&format
);
217 /* "%.?" is treated like "%.0?". */
221 /* Check for type modifiers. */
222 spec
->info
.is_long_double
= 0;
223 spec
->info
.is_short
= 0;
224 spec
->info
.is_long
= 0;
225 spec
->info
.is_char
= 0;
230 /* ints are short ints or chars. */
231 if (*format
!= L_('h'))
232 spec
->info
.is_short
= 1;
236 spec
->info
.is_char
= 1;
240 /* ints are long ints. */
241 spec
->info
.is_long
= 1;
242 if (*format
!= L_('l'))
247 /* doubles are long doubles, and ints are long long ints. */
249 /* 4.4 uses this for long long. */
250 spec
->info
.is_long_double
= 1;
254 /* ints are size_ts. */
255 assert (sizeof (size_t) <= sizeof (unsigned long long int));
256 #if LONG_MAX != LONG_LONG_MAX
257 spec
->info
.is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
259 spec
->info
.is_long
= sizeof (size_t) > sizeof (unsigned int);
262 assert (sizeof (ptrdiff_t) <= sizeof (long long int));
263 #if LONG_MAX != LONG_LONG_MAX
264 spec
->info
.is_long_double
= (sizeof (ptrdiff_t) > sizeof (long int));
266 spec
->info
.is_long
= sizeof (ptrdiff_t) > sizeof (int);
269 assert (sizeof (uintmax_t) <= sizeof (unsigned long long int));
270 #if LONG_MAX != LONG_LONG_MAX
271 spec
->info
.is_long_double
= (sizeof (uintmax_t)
272 > sizeof (unsigned long int));
274 spec
->info
.is_long
= sizeof (uintmax_t) > sizeof (unsigned int);
277 /* Not a recognized modifier. Backup. */
282 /* Get the format specification. */
283 spec
->info
.spec
= (wchar_t) *format
++;
284 if (__builtin_expect (__printf_function_table
!= NULL
, 0)
285 && spec
->info
.spec
<= UCHAR_MAX
286 && __printf_arginfo_table
[spec
->info
.spec
] != NULL
)
287 /* We don't try to get the types for all arguments if the format
288 uses more than one. The normal case is covered though. */
289 spec
->ndata_args
= (*__printf_arginfo_table
[spec
->info
.spec
])
290 (&spec
->info
, 1, &spec
->data_arg_type
);
293 /* Find the data argument types of a built-in spec. */
294 spec
->ndata_args
= 1;
296 switch (spec
->info
.spec
)
304 #if LONG_MAX != LONG_LONG_MAX
305 if (spec
->info
.is_long_double
)
306 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG_LONG
;
309 if (spec
->info
.is_long
)
310 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG
;
311 else if (spec
->info
.is_short
)
312 spec
->data_arg_type
= PA_INT
|PA_FLAG_SHORT
;
313 else if (spec
->info
.is_char
)
314 spec
->data_arg_type
= PA_CHAR
;
316 spec
->data_arg_type
= PA_INT
;
326 if (spec
->info
.is_long_double
)
327 spec
->data_arg_type
= PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
;
329 spec
->data_arg_type
= PA_DOUBLE
;
332 spec
->data_arg_type
= PA_CHAR
;
335 spec
->data_arg_type
= PA_WCHAR
;
338 spec
->data_arg_type
= PA_STRING
;
341 spec
->data_arg_type
= PA_WSTRING
;
344 spec
->data_arg_type
= PA_POINTER
;
347 spec
->data_arg_type
= PA_INT
|PA_FLAG_PTR
;
352 /* An unknown spec will consume no args. */
353 spec
->ndata_args
= 0;
358 if (spec
->data_arg
== -1 && spec
->ndata_args
> 0)
360 /* There are args consumed, but no positional spec. Use the
361 next sequential arg position. */
362 spec
->data_arg
= posn
;
363 nargs
+= spec
->ndata_args
;
366 if (spec
->info
.spec
== L
'\0')
367 /* Format ended before this spec was complete. */
368 spec
->end_of_fmt
= spec
->next_fmt
= format
- 1;
371 /* Find the next format spec. */
372 spec
->end_of_fmt
= format
;
373 #ifdef COMPILE_WPRINTF
374 spec
->next_fmt
= __find_specwc (format
);
376 spec
->next_fmt
= __find_specmb (format
);