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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
28 #define MAX(a,b) ({typeof(a) _a = (a); typeof(b) _b = (b); \
30 #define MIN(a,b) ({typeof(a) _a = (a); typeof(b) _b = (b); \
35 /* Information parsed from the format spec. */
36 struct printf_info info
;
38 /* Pointers into the format string for the end of this format
39 spec and the next (or to the end of the string if no more). */
40 const char *end_of_fmt
, *next_fmt
;
42 /* Position of arguments for precision and width, or -1 if `info' has
43 the constant value. */
44 int prec_arg
, width_arg
;
46 int data_arg
; /* Position of data argument. */
47 int data_arg_type
; /* Type of first argument. */
48 /* Number of arguments consumed by this format specifier. */
53 /* The various kinds off arguments that can be passed to printf. */
56 unsigned char pa_char
;
57 short int pa_short_int
;
60 long long int pa_long_long_int
;
61 unsigned short int pa_u_short_int
;
62 unsigned int pa_u_int
;
63 unsigned long int pa_u_long_int
;
64 unsigned long long int pa_u_long_long_int
;
67 long double pa_long_double
;
68 const char *pa_string
;
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 char *
94 find_spec (const char *format
, mbstate_t *ps
)
96 while (*format
!= '\0' && *format
!= '%')
100 /* Remove any hints of a wrong encoding. */
102 if (isascii (*format
) || (len
= mbrlen (format
, MB_CUR_MAX
, ps
)) <= 0)
111 /* This is defined in reg-printf.c. */
112 extern printf_arginfo_function
**__printf_arginfo_table
;
115 /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
116 with the parsed details. POSN is the number of arguments already
117 consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
118 the number of args consumed by this spec; *MAX_REF_ARG is updated so it
119 remains the highest argument index used. */
121 parse_one_spec (const UCHAR_T
*format
, size_t posn
, struct printf_spec
*spec
,
122 size_t *max_ref_arg
, mbstate_t *ps
)
130 /* Clear information structure. */
133 spec
->info
.space
= 0;
135 spec
->info
.showsign
= 0;
136 spec
->info
.group
= 0;
137 spec
->info
.pad
= ' ';
139 /* Test for positional argument. */
140 if (ISDIGIT (*format
))
142 const UCHAR_T
*begin
= format
;
144 n
= read_int (&format
);
146 if (n
> 0 && *format
== L_('$'))
147 /* Is positional parameter. */
149 ++format
; /* Skip the '$'. */
150 spec
->data_arg
= n
- 1;
151 *max_ref_arg
= MAX (*max_ref_arg
, n
);
154 /* Oops; that was actually the width and/or 0 padding flag.
155 Step back and read it again. */
159 /* Check for spec modifiers. */
160 while (*format
== L_(' ') || *format
== L_('+') || *format
== L_('-') ||
161 *format
== L_('#') || *format
== L_('0') || *format
== L_('\''))
165 /* Output a space in place of a sign, when there is no sign. */
166 spec
->info
.space
= 1;
169 /* Always output + or - for numbers. */
170 spec
->info
.showsign
= 1;
173 /* Left-justify things. */
177 /* Use the "alternate form":
178 Hex has 0x or 0X, FP always has a decimal point. */
183 spec
->info
.pad
= '0';
186 /* Show grouping in numbers if the locale information
188 spec
->info
.group
= 1;
192 spec
->info
.pad
= ' ';
194 /* Get the field width. */
195 spec
->width_arg
= -1;
196 spec
->info
.width
= 0;
197 if (*format
== L_('*'))
199 /* The field width is given in an argument.
200 A negative field width indicates left justification. */
201 const UCHAR_T
*begin
= ++format
;
203 if (ISDIGIT (*format
))
205 /* The width argument might be found in a positional parameter. */
206 n
= read_int (&format
);
208 if (n
> 0 && *format
== L_('$'))
210 spec
->width_arg
= n
- 1;
211 *max_ref_arg
= MAX (*max_ref_arg
, n
);
212 ++format
; /* Skip '$'. */
216 if (spec
->width_arg
< 0)
218 /* Not in a positional parameter. Consume one argument. */
219 spec
->width_arg
= posn
++;
221 format
= begin
; /* Step back and reread. */
224 else if (ISDIGIT (*format
))
225 /* Constant width specification. */
226 spec
->info
.width
= read_int (&format
);
228 /* Get the precision. */
230 /* -1 means none given; 0 means explicit 0. */
231 spec
->info
.prec
= -1;
232 if (*format
== L_('.'))
235 if (*format
== L_('*'))
237 /* The precision is given in an argument. */
238 const UCHAR_T
*begin
= ++format
;
240 if (ISDIGIT (*format
))
242 n
= read_int (&format
);
244 if (n
> 0 && *format
== L_('$'))
246 spec
->prec_arg
= n
- 1;
247 *max_ref_arg
= MAX (*max_ref_arg
, n
);
252 if (spec
->prec_arg
< 0)
254 /* Not in a positional parameter. */
255 spec
->prec_arg
= posn
++;
260 else if (ISDIGIT (*format
))
261 spec
->info
.prec
= read_int (&format
);
263 /* "%.?" is treated like "%.0?". */
267 /* Check for type modifiers. */
268 #define is_longlong is_long_double
269 spec
->info
.is_long_double
= 0;
270 spec
->info
.is_short
= 0;
271 spec
->info
.is_long
= 0;
273 if (*format
== L_('h') || *format
== L_('l') || *format
== L_('L') ||
274 *format
== L_('Z') || *format
== L_('q'))
278 /* int's are short int's. */
279 spec
->info
.is_short
= 1;
282 /* int's are long int's. */
283 spec
->info
.is_long
= 1;
284 if (*format
!= L_('l'))
289 /* double's are long double's, and int's are long long int's. */
291 /* 4.4 uses this for long long. */
292 spec
->info
.is_long_double
= 1;
295 /* int's are size_t's. */
296 assert (sizeof(size_t) <= sizeof(unsigned long long int));
297 spec
->info
.is_longlong
= sizeof(size_t) > sizeof(unsigned long int);
298 spec
->info
.is_long
= sizeof(size_t) > sizeof(unsigned int);
302 /* Get the format specification. */
303 #ifdef THIS_IS_INCOMPATIBLE_WITH_LINUX_LIBC
304 spec
->info
.spec
= (wchar_t) *format
++;
306 spec
->info
.spec
= (char) *format
++;
308 if (__printf_arginfo_table
!= NULL
&&
309 __printf_arginfo_table
[spec
->info
.spec
] != NULL
)
310 /* We don't try to get the types for all arguments if the format
311 uses more than one. The normal case is covered though. */
312 spec
->ndata_args
= (*__printf_arginfo_table
[spec
->info
.spec
])
313 (&spec
->info
, 1, &spec
->data_arg_type
);
316 /* Find the data argument types of a built-in spec. */
317 spec
->ndata_args
= 1;
319 switch (spec
->info
.spec
)
327 if (spec
->info
.is_longlong
)
328 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG_LONG
;
329 else if (spec
->info
.is_long
)
330 spec
->data_arg_type
= PA_INT
|PA_FLAG_LONG
;
331 else if (spec
->info
.is_short
)
332 spec
->data_arg_type
= PA_INT
|PA_FLAG_SHORT
;
334 spec
->data_arg_type
= PA_INT
;
341 if (spec
->info
.is_long_double
)
342 spec
->data_arg_type
= PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
;
344 spec
->data_arg_type
= PA_DOUBLE
;
347 spec
->data_arg_type
= PA_CHAR
;
350 spec
->data_arg_type
= PA_STRING
;
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
);