1 /* Internal header for parsing printf format strings.
2 Copyright (C) 1995, 1996 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. */
31 /* Information parsed from the format spec. */
32 struct printf_info info
;
34 /* Pointers into the format string for the end of this format
35 spec and the next (or to the end of the string if no more). */
36 const char *end_of_fmt
, *next_fmt
;
38 /* Position of arguments for precision and width, or -1 if `info' has
39 the constant value. */
40 int prec_arg
, width_arg
;
42 int data_arg
; /* Position of data argument. */
43 int data_arg_type
; /* Type of first argument. */
44 /* Number of arguments consumed by this format specifier. */
49 /* The various kinds off arguments that can be passed to printf. */
52 unsigned char pa_char
;
54 short int pa_short_int
;
57 long long int pa_long_long_int
;
58 unsigned short int pa_u_short_int
;
59 unsigned int pa_u_int
;
60 unsigned long int pa_u_long_int
;
61 unsigned long long int pa_u_long_long_int
;
64 long double pa_long_double
;
65 const char *pa_string
;
66 const wchar_t *pa_wstring
;
71 /* Read a simple integer from a string and update the string pointer.
72 It is assumed that the first character is a digit. */
73 static inline unsigned int
74 read_int (const UCHAR_T
* *pstr
)
76 unsigned int retval
= **pstr
- L_('0');
78 while (ISDIGIT (*++(*pstr
)))
81 retval
+= **pstr
- L_('0');
89 /* Find the next spec in FORMAT, or the end of the string. Returns
90 a pointer into FORMAT, to a '%' or a '\0'. */
91 static inline const char *
92 find_spec (const char *format
, mbstate_t *ps
)
94 while (*format
!= '\0' && *format
!= '%')
98 /* Remove any hints of a wrong encoding. */
100 if (isascii (*format
) || (len
= mbrlen (format
, MB_CUR_MAX
, ps
)) <= 0)
109 /* This is defined in reg-printf.c. */
110 extern printf_arginfo_function
**__printf_arginfo_table
;
113 /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
114 with the parsed details. POSN is the number of arguments already
115 consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
116 the number of args consumed by this spec; *MAX_REF_ARG is updated so it
117 remains the highest argument index used. */
119 parse_one_spec (const UCHAR_T
*format
, size_t posn
, struct printf_spec
*spec
,
120 size_t *max_ref_arg
, mbstate_t *ps
)
128 /* Clear information structure. */
131 spec
->info
.space
= 0;
133 spec
->info
.showsign
= 0;
134 spec
->info
.group
= 0;
135 spec
->info
.pad
= ' ';
137 /* Test for positional argument. */
138 if (ISDIGIT (*format
))
140 const UCHAR_T
*begin
= format
;
142 n
= read_int (&format
);
144 if (n
> 0 && *format
== L_('$'))
145 /* Is positional parameter. */
147 ++format
; /* Skip the '$'. */
148 spec
->data_arg
= n
- 1;
149 *max_ref_arg
= MAX (*max_ref_arg
, n
);
152 /* Oops; that was actually the width and/or 0 padding flag.
153 Step back and read it again. */
157 /* Check for spec modifiers. */
158 while (*format
== L_(' ') || *format
== L_('+') || *format
== L_('-') ||
159 *format
== L_('#') || *format
== L_('0') || *format
== L_('\''))
163 /* Output a space in place of a sign, when there is no sign. */
164 spec
->info
.space
= 1;
167 /* Always output + or - for numbers. */
168 spec
->info
.showsign
= 1;
171 /* Left-justify things. */
175 /* Use the "alternate form":
176 Hex has 0x or 0X, FP always has a decimal point. */
181 spec
->info
.pad
= '0';
184 /* Show grouping in numbers if the locale information
186 spec
->info
.group
= 1;
190 spec
->info
.pad
= ' ';
192 /* Get the field width. */
193 spec
->width_arg
= -1;
194 spec
->info
.width
= 0;
195 if (*format
== L_('*'))
197 /* The field width is given in an argument.
198 A negative field width indicates left justification. */
199 const UCHAR_T
*begin
= ++format
;
201 if (ISDIGIT (*format
))
203 /* The width argument might be found in a positional parameter. */
204 n
= read_int (&format
);
206 if (n
> 0 && *format
== L_('$'))
208 spec
->width_arg
= n
- 1;
209 *max_ref_arg
= MAX (*max_ref_arg
, n
);
210 ++format
; /* Skip '$'. */
214 if (spec
->width_arg
< 0)
216 /* Not in a positional parameter. Consume one argument. */
217 spec
->width_arg
= posn
++;
219 format
= begin
; /* Step back and reread. */
222 else if (ISDIGIT (*format
))
223 /* Constant width specification. */
224 spec
->info
.width
= read_int (&format
);
226 /* Get the precision. */
228 /* -1 means none given; 0 means explicit 0. */
229 spec
->info
.prec
= -1;
230 if (*format
== L_('.'))
233 if (*format
== L_('*'))
235 /* The precision is given in an argument. */
236 const UCHAR_T
*begin
= ++format
;
238 if (ISDIGIT (*format
))
240 n
= read_int (&format
);
242 if (n
> 0 && *format
== L_('$'))
244 spec
->prec_arg
= n
- 1;
245 *max_ref_arg
= MAX (*max_ref_arg
, n
);
250 if (spec
->prec_arg
< 0)
252 /* Not in a positional parameter. */
253 spec
->prec_arg
= posn
++;
258 else if (ISDIGIT (*format
))
259 spec
->info
.prec
= read_int (&format
);
261 /* "%.?" is treated like "%.0?". */
265 /* Check for type modifiers. */
266 #define is_longlong is_long_double
267 spec
->info
.is_long_double
= 0;
268 spec
->info
.is_short
= 0;
269 spec
->info
.is_long
= 0;
271 if (*format
== L_('h') || *format
== L_('l') || *format
== L_('L') ||
272 *format
== L_('Z') || *format
== L_('q'))
276 /* int's are short int's. */
277 spec
->info
.is_short
= 1;
280 /* int's are long int's. */
281 spec
->info
.is_long
= 1;
282 if (*format
!= L_('l'))
287 /* double's are long double's, and int's are long long int's. */
289 /* 4.4 uses this for long long. */
290 spec
->info
.is_long_double
= 1;
293 /* int's are size_t's. */
294 assert (sizeof(size_t) <= sizeof(unsigned long long int));
295 spec
->info
.is_longlong
= sizeof(size_t) > sizeof(unsigned long int);
296 spec
->info
.is_long
= sizeof(size_t) > sizeof(unsigned int);
300 /* Get the format specification. */
301 spec
->info
.spec
= (wchar_t) *format
++;
302 if (__printf_arginfo_table
!= NULL
&&
303 __printf_arginfo_table
[spec
->info
.spec
] != NULL
)
304 /* We don't try to get the types for all arguments if the format
305 uses more than one. The normal case is covered though. */
306 spec
->ndata_args
= (*__printf_arginfo_table
[spec
->info
.spec
])
307 (&spec
->info
, 1, &spec
->data_arg_type
);
310 /* Find the data argument types of a built-in spec. */
311 spec
->ndata_args
= 1;
313 switch (spec
->info
.spec
)
321 if (spec
->info
.is_longlong
)
322 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG_LONG
;
323 else if (spec
->info
.is_long
)
324 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG
;
325 else if (spec
->info
.is_short
)
326 spec
->data_arg_type
= PA_INT
|PA_FLAG_SHORT
;
328 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;
366 if (spec
->data_arg
== -1 && spec
->ndata_args
> 0)
368 /* There are args consumed, but no positional spec.
369 Use the next sequential arg position. */
370 spec
->data_arg
= posn
;
371 posn
+= spec
->ndata_args
;
372 nargs
+= spec
->ndata_args
;
376 if (spec
->info
.spec
== L
'\0')
377 /* Format ended before this spec was complete. */
378 spec
->end_of_fmt
= spec
->next_fmt
= format
- 1;
381 /* Find the next format spec. */
382 spec
->end_of_fmt
= format
;
383 spec
->next_fmt
= find_spec (format
, ps
);