1 /* Internal header for parsing printf format strings.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
33 /* Information parsed from the format spec. */
34 struct printf_info info
;
36 /* Pointers into the format string for the end of this format
37 spec and the next (or to the end of the string if no more). */
38 const UCHAR_T
*end_of_fmt
, *next_fmt
;
40 /* Position of arguments for precision and width, or -1 if `info' has
41 the constant value. */
42 int prec_arg
, width_arg
;
44 int data_arg
; /* Position of data argument. */
45 int data_arg_type
; /* Type of first argument. */
46 /* Number of arguments consumed by this format specifier. */
51 /* The various kinds off arguments that can be passed to printf. */
54 unsigned char pa_char
;
56 short int pa_short_int
;
59 long long int pa_long_long_int
;
60 unsigned short int pa_u_short_int
;
61 unsigned int pa_u_int
;
62 unsigned long int pa_u_long_int
;
63 unsigned long long int pa_u_long_long_int
;
66 long double pa_long_double
;
67 const char *pa_string
;
68 const wchar_t *pa_wstring
;
73 /* Read a simple integer from a string and update the string pointer.
74 It is assumed that the first character is a digit. */
75 static inline unsigned int
76 read_int (const UCHAR_T
* *pstr
)
78 unsigned int retval
= **pstr
- L_('0');
80 while (ISDIGIT (*++(*pstr
)))
83 retval
+= **pstr
- L_('0');
91 /* Find the next spec in FORMAT, or the end of the string. Returns
92 a pointer into FORMAT, to a '%' or a '\0'. */
93 static inline const UCHAR_T
*
94 #ifdef COMPILE_WPRINTF
95 find_spec (const UCHAR_T
*format
)
97 find_spec (const UCHAR_T
*format
, mbstate_t *ps
)
100 #ifdef COMPILE_WPRINTF
101 return (const UCHAR_T
*) __wcschrnul ((const CHAR_T
*) format
, L
'%');
103 while (*format
!= L_('\0') && *format
!= L_('%'))
107 /* Remove any hints of a wrong encoding. */
109 if (! isascii (*format
) && (len
= mbrlen (format
, MB_CUR_MAX
, ps
)) > 0)
119 /* These are defined in reg-printf.c. */
120 extern printf_arginfo_function
*__printf_arginfo_table
[];
121 extern printf_function
**__printf_function_table
;
124 /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
125 with the parsed details. POSN is the number of arguments already
126 consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
127 the number of args consumed by this spec; *MAX_REF_ARG is updated so it
128 remains the highest argument index used. */
130 #ifdef COMPILE_WPRINTF
131 parse_one_spec (const UCHAR_T
*format
, size_t posn
, struct printf_spec
*spec
,
134 parse_one_spec (const UCHAR_T
*format
, size_t posn
, struct printf_spec
*spec
,
135 size_t *max_ref_arg
, mbstate_t *ps
)
144 /* Clear information structure. */
147 spec
->info
.space
= 0;
149 spec
->info
.showsign
= 0;
150 spec
->info
.group
= 0;
151 spec
->info
.pad
= ' ';
153 /* Test for positional argument. */
154 if (ISDIGIT (*format
))
156 const UCHAR_T
*begin
= format
;
158 n
= read_int (&format
);
160 if (n
> 0 && *format
== L_('$'))
161 /* Is positional parameter. */
163 ++format
; /* Skip the '$'. */
164 spec
->data_arg
= n
- 1;
165 *max_ref_arg
= MAX (*max_ref_arg
, n
);
168 /* Oops; that was actually the width and/or 0 padding flag.
169 Step back and read it again. */
173 /* Check for spec modifiers. */
174 while (*format
== L_(' ') || *format
== L_('+') || *format
== L_('-') ||
175 *format
== L_('#') || *format
== L_('0') || *format
== L_('\''))
179 /* Output a space in place of a sign, when there is no sign. */
180 spec
->info
.space
= 1;
183 /* Always output + or - for numbers. */
184 spec
->info
.showsign
= 1;
187 /* Left-justify things. */
191 /* Use the "alternate form":
192 Hex has 0x or 0X, FP always has a decimal point. */
197 spec
->info
.pad
= '0';
200 /* Show grouping in numbers if the locale information
202 spec
->info
.group
= 1;
206 spec
->info
.pad
= ' ';
208 /* Get the field width. */
209 spec
->width_arg
= -1;
210 spec
->info
.width
= 0;
211 if (*format
== L_('*'))
213 /* The field width is given in an argument.
214 A negative field width indicates left justification. */
215 const UCHAR_T
*begin
= ++format
;
217 if (ISDIGIT (*format
))
219 /* The width argument might be found in a positional parameter. */
220 n
= read_int (&format
);
222 if (n
> 0 && *format
== L_('$'))
224 spec
->width_arg
= n
- 1;
225 *max_ref_arg
= MAX (*max_ref_arg
, n
);
226 ++format
; /* Skip '$'. */
230 if (spec
->width_arg
< 0)
232 /* Not in a positional parameter. Consume one argument. */
233 spec
->width_arg
= posn
++;
235 format
= begin
; /* Step back and reread. */
238 else if (ISDIGIT (*format
))
239 /* Constant width specification. */
240 spec
->info
.width
= read_int (&format
);
242 /* Get the precision. */
244 /* -1 means none given; 0 means explicit 0. */
245 spec
->info
.prec
= -1;
246 if (*format
== L_('.'))
249 if (*format
== L_('*'))
251 /* The precision is given in an argument. */
252 const UCHAR_T
*begin
= ++format
;
254 if (ISDIGIT (*format
))
256 n
= read_int (&format
);
258 if (n
> 0 && *format
== L_('$'))
260 spec
->prec_arg
= n
- 1;
261 *max_ref_arg
= MAX (*max_ref_arg
, n
);
266 if (spec
->prec_arg
< 0)
268 /* Not in a positional parameter. */
269 spec
->prec_arg
= posn
++;
274 else if (ISDIGIT (*format
))
275 spec
->info
.prec
= read_int (&format
);
277 /* "%.?" is treated like "%.0?". */
281 /* Check for type modifiers. */
282 spec
->info
.is_long_double
= 0;
283 spec
->info
.is_short
= 0;
284 spec
->info
.is_long
= 0;
285 spec
->info
.is_char
= 0;
290 /* ints are short ints or chars. */
291 if (*format
!= L_('h'))
292 spec
->info
.is_short
= 1;
296 spec
->info
.is_char
= 1;
300 /* ints are long ints. */
301 spec
->info
.is_long
= 1;
302 if (*format
!= L_('l'))
307 /* doubles are long doubles, and ints are long long ints. */
309 /* 4.4 uses this for long long. */
310 spec
->info
.is_long_double
= 1;
314 /* ints are size_ts. */
315 assert (sizeof (size_t) <= sizeof (unsigned long long int));
316 #if LONG_MAX != LONG_LONG_MAX
317 spec
->info
.is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
319 spec
->info
.is_long
= sizeof (size_t) > sizeof (unsigned int);
322 assert (sizeof (ptrdiff_t) <= sizeof (long long int));
323 #if LONG_MAX != LONG_LONG_MAX
324 spec
->info
.is_long_double
= (sizeof (ptrdiff_t) > sizeof (long int));
326 spec
->info
.is_long
= sizeof (ptrdiff_t) > sizeof (int);
329 assert (sizeof (uintmax_t) <= sizeof (unsigned long long int));
330 #if LONG_MAX != LONG_LONG_MAX
331 spec
->info
.is_long_double
= (sizeof (uintmax_t)
332 > sizeof (unsigned long int));
334 spec
->info
.is_long
= sizeof (uintmax_t) > sizeof (unsigned int);
337 /* Not a recognized modifier. Backup. */
342 /* Get the format specification. */
343 spec
->info
.spec
= (wchar_t) *format
++;
344 if (__printf_function_table
!= NULL
345 && spec
->info
.spec
<= UCHAR_MAX
346 && __printf_arginfo_table
[spec
->info
.spec
] != NULL
)
347 /* We don't try to get the types for all arguments if the format
348 uses more than one. The normal case is covered though. */
349 spec
->ndata_args
= (*__printf_arginfo_table
[spec
->info
.spec
])
350 (&spec
->info
, 1, &spec
->data_arg_type
);
353 /* Find the data argument types of a built-in spec. */
354 spec
->ndata_args
= 1;
356 switch (spec
->info
.spec
)
364 #if LONG_MAX != LONG_LONG_MAX
365 if (spec
->info
.is_long_double
)
366 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG_LONG
;
369 if (spec
->info
.is_long
)
370 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG
;
371 else if (spec
->info
.is_short
)
372 spec
->data_arg_type
= PA_INT
|PA_FLAG_SHORT
;
373 else if (spec
->info
.is_char
)
374 spec
->data_arg_type
= PA_CHAR
;
376 spec
->data_arg_type
= PA_INT
;
385 if (spec
->info
.is_long_double
)
386 spec
->data_arg_type
= PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
;
388 spec
->data_arg_type
= PA_DOUBLE
;
391 spec
->data_arg_type
= PA_CHAR
;
394 spec
->data_arg_type
= PA_WCHAR
;
397 spec
->data_arg_type
= PA_STRING
;
400 spec
->data_arg_type
= PA_WSTRING
;
403 spec
->data_arg_type
= PA_POINTER
;
406 spec
->data_arg_type
= PA_INT
|PA_FLAG_PTR
;
411 /* An unknown spec will consume no args. */
412 spec
->ndata_args
= 0;
417 if (spec
->data_arg
== -1 && spec
->ndata_args
> 0)
419 /* There are args consumed, but no positional spec. Use the
420 next sequential arg position. */
421 spec
->data_arg
= posn
;
422 nargs
+= spec
->ndata_args
;
425 if (spec
->info
.spec
== L
'\0')
426 /* Format ended before this spec was complete. */
427 spec
->end_of_fmt
= spec
->next_fmt
= format
- 1;
430 /* Find the next format spec. */
431 spec
->end_of_fmt
= format
;
432 #ifdef COMPILE_WPRINTF
433 spec
->next_fmt
= find_spec (format
);
435 spec
->next_fmt
= find_spec (format
, ps
);