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 /* Find the next spec in FORMAT, or the end of the string. Returns
50 a pointer into FORMAT, to a '%' or a '\0'. */
52 #ifdef COMPILE_WPRINTF
53 __find_specwc (const UCHAR_T
*format
)
55 __find_specmb (const UCHAR_T
*format
, mbstate_t *ps
)
58 #ifdef COMPILE_WPRINTF
59 return (const UCHAR_T
*) __wcschrnul ((const CHAR_T
*) format
, L
'%');
61 while (*format
!= L_('\0') && *format
!= L_('%'))
65 /* Remove any hints of a wrong encoding. */
67 if (! isascii (*format
)
68 && (len
= __mbrlen ((const CHAR_T
*) format
, MB_CUR_MAX
, ps
)) > 0)
78 /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
79 with the parsed details. POSN is the number of arguments already
80 consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
81 the number of args consumed by this spec; *MAX_REF_ARG is updated so it
82 remains the highest argument index used. */
85 #ifdef COMPILE_WPRINTF
86 __parse_one_specwc (const UCHAR_T
*format
, size_t posn
,
87 struct printf_spec
*spec
, size_t *max_ref_arg
)
89 __parse_one_specmb (const UCHAR_T
*format
, size_t posn
,
90 struct printf_spec
*spec
, size_t *max_ref_arg
,
100 /* Clear information structure. */
103 spec
->info
.space
= 0;
105 spec
->info
.showsign
= 0;
106 spec
->info
.group
= 0;
108 spec
->info
.extra
= 0;
109 spec
->info
.pad
= ' ';
110 spec
->info
.wide
= sizeof (UCHAR_T
) > 1;
112 /* Test for positional argument. */
113 if (ISDIGIT (*format
))
115 const UCHAR_T
*begin
= format
;
117 n
= read_int (&format
);
119 if (n
> 0 && *format
== L_('$'))
120 /* Is positional parameter. */
122 ++format
; /* Skip the '$'. */
123 spec
->data_arg
= n
- 1;
124 *max_ref_arg
= MAX (*max_ref_arg
, n
);
127 /* Oops; that was actually the width and/or 0 padding flag.
128 Step back and read it again. */
132 /* Check for spec modifiers. */
138 /* Output a space in place of a sign, when there is no sign. */
139 spec
->info
.space
= 1;
142 /* Always output + or - for numbers. */
143 spec
->info
.showsign
= 1;
146 /* Left-justify things. */
150 /* Use the "alternate form":
151 Hex has 0x or 0X, FP always has a decimal point. */
156 spec
->info
.pad
= '0';
159 /* Show grouping in numbers if the locale information
161 spec
->info
.group
= 1;
164 /* Use the internationalized form of the output. Currently
165 means to use the `outdigits' of the current locale. */
176 spec
->info
.pad
= ' ';
178 /* Get the field width. */
179 spec
->width_arg
= -1;
180 spec
->info
.width
= 0;
181 if (*format
== L_('*'))
183 /* The field width is given in an argument.
184 A negative field width indicates left justification. */
185 const UCHAR_T
*begin
= ++format
;
187 if (ISDIGIT (*format
))
189 /* The width argument might be found in a positional parameter. */
190 n
= read_int (&format
);
192 if (n
> 0 && *format
== L_('$'))
194 spec
->width_arg
= n
- 1;
195 *max_ref_arg
= MAX (*max_ref_arg
, n
);
196 ++format
; /* Skip '$'. */
200 if (spec
->width_arg
< 0)
202 /* Not in a positional parameter. Consume one argument. */
203 spec
->width_arg
= posn
++;
205 format
= begin
; /* Step back and reread. */
208 else if (ISDIGIT (*format
))
209 /* Constant width specification. */
210 spec
->info
.width
= read_int (&format
);
212 /* Get the precision. */
214 /* -1 means none given; 0 means explicit 0. */
215 spec
->info
.prec
= -1;
216 if (*format
== L_('.'))
219 if (*format
== L_('*'))
221 /* The precision is given in an argument. */
222 const UCHAR_T
*begin
= ++format
;
224 if (ISDIGIT (*format
))
226 n
= read_int (&format
);
228 if (n
> 0 && *format
== L_('$'))
230 spec
->prec_arg
= n
- 1;
231 *max_ref_arg
= MAX (*max_ref_arg
, n
);
236 if (spec
->prec_arg
< 0)
238 /* Not in a positional parameter. */
239 spec
->prec_arg
= posn
++;
244 else if (ISDIGIT (*format
))
245 spec
->info
.prec
= read_int (&format
);
247 /* "%.?" is treated like "%.0?". */
251 /* Check for type modifiers. */
252 spec
->info
.is_long_double
= 0;
253 spec
->info
.is_short
= 0;
254 spec
->info
.is_long
= 0;
255 spec
->info
.is_char
= 0;
260 /* ints are short ints or chars. */
261 if (*format
!= L_('h'))
262 spec
->info
.is_short
= 1;
266 spec
->info
.is_char
= 1;
270 /* ints are long ints. */
271 spec
->info
.is_long
= 1;
272 if (*format
!= L_('l'))
277 /* doubles are long doubles, and ints are long long ints. */
279 /* 4.4 uses this for long long. */
280 spec
->info
.is_long_double
= 1;
284 /* ints are size_ts. */
285 assert (sizeof (size_t) <= sizeof (unsigned long long int));
286 #if LONG_MAX != LONG_LONG_MAX
287 spec
->info
.is_long_double
= sizeof (size_t) > 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);
307 /* Not a recognized modifier. Backup. */
312 /* Get the format specification. */
313 spec
->info
.spec
= (wchar_t) *format
++;
314 if (__builtin_expect (__printf_function_table
!= NULL
, 0)
315 && spec
->info
.spec
<= UCHAR_MAX
316 && __printf_arginfo_table
[spec
->info
.spec
] != NULL
)
317 /* We don't try to get the types for all arguments if the format
318 uses more than one. The normal case is covered though. */
319 spec
->ndata_args
= (*__printf_arginfo_table
[spec
->info
.spec
])
320 (&spec
->info
, 1, &spec
->data_arg_type
);
323 /* Find the data argument types of a built-in spec. */
324 spec
->ndata_args
= 1;
326 switch (spec
->info
.spec
)
334 #if LONG_MAX != LONG_LONG_MAX
335 if (spec
->info
.is_long_double
)
336 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG_LONG
;
339 if (spec
->info
.is_long
)
340 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG
;
341 else if (spec
->info
.is_short
)
342 spec
->data_arg_type
= PA_INT
|PA_FLAG_SHORT
;
343 else if (spec
->info
.is_char
)
344 spec
->data_arg_type
= PA_CHAR
;
346 spec
->data_arg_type
= PA_INT
;
356 if (spec
->info
.is_long_double
)
357 spec
->data_arg_type
= PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
;
359 spec
->data_arg_type
= PA_DOUBLE
;
362 spec
->data_arg_type
= PA_CHAR
;
365 spec
->data_arg_type
= PA_WCHAR
;
368 spec
->data_arg_type
= PA_STRING
;
371 spec
->data_arg_type
= PA_WSTRING
;
374 spec
->data_arg_type
= PA_POINTER
;
377 spec
->data_arg_type
= PA_INT
|PA_FLAG_PTR
;
382 /* An unknown spec will consume no args. */
383 spec
->ndata_args
= 0;
388 if (spec
->data_arg
== -1 && spec
->ndata_args
> 0)
390 /* There are args consumed, but no positional spec. Use the
391 next sequential arg position. */
392 spec
->data_arg
= posn
;
393 nargs
+= spec
->ndata_args
;
396 if (spec
->info
.spec
== L
'\0')
397 /* Format ended before this spec was complete. */
398 spec
->end_of_fmt
= spec
->next_fmt
= format
- 1;
401 /* Find the next format spec. */
402 spec
->end_of_fmt
= format
;
403 #ifdef COMPILE_WPRINTF
404 spec
->next_fmt
= __find_specwc (format
);
406 spec
->next_fmt
= __find_specmb (format
, ps
);