Update.
[glibc.git] / stdio-common / printf-parse.h
blob4f217abfd35cc723eda758d50bf3a5f63027ceab
1 /* Internal header for parsing printf format strings.
2 Copyright (C) 1995, 1996, 1997, 1998 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. */
20 #include <ctype.h>
21 #include <printf.h>
22 #include <stdint.h>
23 #include <stddef.h>
24 #include <string.h>
26 #define NDEBUG 1
27 #include <assert.h>
30 struct printf_spec
32 /* Information parsed from the format spec. */
33 struct printf_info info;
35 /* Pointers into the format string for the end of this format
36 spec and the next (or to the end of the string if no more). */
37 const char *end_of_fmt, *next_fmt;
39 /* Position of arguments for precision and width, or -1 if `info' has
40 the constant value. */
41 int prec_arg, width_arg;
43 int data_arg; /* Position of data argument. */
44 int data_arg_type; /* Type of first argument. */
45 /* Number of arguments consumed by this format specifier. */
46 size_t ndata_args;
50 /* The various kinds off arguments that can be passed to printf. */
51 union printf_arg
53 unsigned char pa_char;
54 wchar_t pa_wchar;
55 short int pa_short_int;
56 int pa_int;
57 long int pa_long_int;
58 long long int pa_long_long_int;
59 unsigned short int pa_u_short_int;
60 unsigned int pa_u_int;
61 unsigned long int pa_u_long_int;
62 unsigned long long int pa_u_long_long_int;
63 float pa_float;
64 double pa_double;
65 long double pa_long_double;
66 const char *pa_string;
67 const wchar_t *pa_wstring;
68 void *pa_pointer;
72 /* Read a simple integer from a string and update the string pointer.
73 It is assumed that the first character is a digit. */
74 static inline unsigned int
75 read_int (const UCHAR_T * *pstr)
77 unsigned int retval = **pstr - L_('0');
79 while (ISDIGIT (*++(*pstr)))
81 retval *= 10;
82 retval += **pstr - L_('0');
85 return retval;
90 /* Find the next spec in FORMAT, or the end of the string. Returns
91 a pointer into FORMAT, to a '%' or a '\0'. */
92 static inline const char *
93 find_spec (const char *format, mbstate_t *ps)
95 while (*format != '\0' && *format != '%')
97 int len;
99 /* Remove any hints of a wrong encoding. */
100 ps->count = 0;
101 if (isascii (*format) || (len = mbrlen (format, MB_CUR_MAX, ps)) <= 0)
102 ++format;
103 else
104 format += len;
106 return format;
110 /* These are defined in reg-printf.c. */
111 extern printf_arginfo_function *__printf_arginfo_table[];
112 extern printf_function **__printf_function_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. */
120 static inline size_t
121 parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec,
122 size_t *max_ref_arg, mbstate_t *ps)
124 unsigned int n;
125 size_t nargs = 0;
127 /* Skip the '%'. */
128 ++format;
130 /* Clear information structure. */
131 spec->data_arg = -1;
132 spec->info.alt = 0;
133 spec->info.space = 0;
134 spec->info.left = 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);
153 else
154 /* Oops; that was actually the width and/or 0 padding flag.
155 Step back and read it again. */
156 format = begin;
159 /* Check for spec modifiers. */
160 while (*format == L_(' ') || *format == L_('+') || *format == L_('-') ||
161 *format == L_('#') || *format == L_('0') || *format == L_('\''))
162 switch (*format++)
164 case L_(' '):
165 /* Output a space in place of a sign, when there is no sign. */
166 spec->info.space = 1;
167 break;
168 case L_('+'):
169 /* Always output + or - for numbers. */
170 spec->info.showsign = 1;
171 break;
172 case L_('-'):
173 /* Left-justify things. */
174 spec->info.left = 1;
175 break;
176 case L_('#'):
177 /* Use the "alternate form":
178 Hex has 0x or 0X, FP always has a decimal point. */
179 spec->info.alt = 1;
180 break;
181 case L_('0'):
182 /* Pad with 0s. */
183 spec->info.pad = '0';
184 break;
185 case L_('\''):
186 /* Show grouping in numbers if the locale information
187 indicates any. */
188 spec->info.group = 1;
189 break;
191 if (spec->info.left)
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++;
220 ++nargs;
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. */
229 spec->prec_arg = -1;
230 /* -1 means none given; 0 means explicit 0. */
231 spec->info.prec = -1;
232 if (*format == L_('.'))
234 ++format;
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);
248 ++format;
252 if (spec->prec_arg < 0)
254 /* Not in a positional parameter. */
255 spec->prec_arg = posn++;
256 ++nargs;
257 format = begin;
260 else if (ISDIGIT (*format))
261 spec->info.prec = read_int (&format);
262 else
263 /* "%.?" is treated like "%.0?". */
264 spec->info.prec = 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;
272 spec->info.is_char = 0;
274 switch (*format++)
276 case L_('h'):
277 /* ints are short ints or chars. */
278 if (*format != L_('h'))
279 spec->info.is_short = 1;
280 else
282 ++format;
283 spec->info.is_char = 1;
285 break;
286 case L_('l'):
287 /* ints are long ints. */
288 spec->info.is_long = 1;
289 if (*format != L_('l'))
290 break;
291 ++format;
292 /* FALLTHROUGH */
293 case L_('L'):
294 /* doubles are long doubles, and ints are long long ints. */
295 case L_('q'):
296 /* 4.4 uses this for long long. */
297 spec->info.is_long_double = 1;
298 break;
299 case L_('z'):
300 case L_('Z'):
301 /* ints are size_ts. */
302 assert (sizeof (size_t) <= sizeof (unsigned long long int));
303 spec->info.is_longlong = sizeof (size_t) > sizeof (unsigned long int);
304 spec->info.is_long = sizeof (size_t) > sizeof (unsigned int);
305 break;
306 case L_('t'):
307 assert (sizeof (ptrdiff_t) <= sizeof (long long int));
308 spec->info.is_longlong = (sizeof (ptrdiff_t) > sizeof (long int));
309 spec->info.is_long = sizeof (ptrdiff_t) > sizeof (int);
310 break;
311 case L_('j'):
312 assert (sizeof (uintmax_t) <= sizeof (unsigned long long int));
313 spec->info.is_longlong = (sizeof (uintmax_t)
314 > sizeof (unsigned long int));
315 spec->info.is_long = sizeof (uintmax_t) > sizeof (unsigned int);
316 break;
317 default:
318 /* Not a recognized modifier. Backup. */
319 --format;
320 break;
323 /* Get the format specification. */
324 spec->info.spec = (wchar_t) *format++;
325 if (__printf_function_table != NULL
326 && spec->info.spec <= UCHAR_MAX
327 && __printf_arginfo_table[spec->info.spec] != NULL)
328 /* We don't try to get the types for all arguments if the format
329 uses more than one. The normal case is covered though. */
330 spec->ndata_args = (*__printf_arginfo_table[spec->info.spec])
331 (&spec->info, 1, &spec->data_arg_type);
332 else
334 /* Find the data argument types of a built-in spec. */
335 spec->ndata_args = 1;
337 switch (spec->info.spec)
339 case L'i':
340 case L'd':
341 case L'u':
342 case L'o':
343 case L'X':
344 case L'x':
345 if (spec->info.is_longlong)
346 spec->data_arg_type = PA_INT|PA_FLAG_LONG_LONG;
347 else if (spec->info.is_long)
348 spec->data_arg_type = PA_INT|PA_FLAG_LONG;
349 else if (spec->info.is_short)
350 spec->data_arg_type = PA_INT|PA_FLAG_SHORT;
351 else if (spec->info.is_char)
352 spec->data_arg_type = PA_CHAR;
353 else
354 spec->data_arg_type = PA_INT;
355 break;
356 case L'e':
357 case L'E':
358 case L'f':
359 case L'g':
360 case L'G':
361 if (spec->info.is_long_double)
362 spec->data_arg_type = PA_DOUBLE|PA_FLAG_LONG_DOUBLE;
363 else
364 spec->data_arg_type = PA_DOUBLE;
365 break;
366 case L'c':
367 spec->data_arg_type = PA_CHAR;
368 break;
369 case L'C':
370 spec->data_arg_type = PA_WCHAR;
371 break;
372 case L's':
373 spec->data_arg_type = PA_STRING;
374 break;
375 case L'S':
376 spec->data_arg_type = PA_WSTRING;
377 break;
378 case L'p':
379 spec->data_arg_type = PA_POINTER;
380 break;
381 case L'n':
382 spec->data_arg_type = PA_INT|PA_FLAG_PTR;
383 break;
385 case L'm':
386 default:
387 /* An unknown spec will consume no args. */
388 spec->ndata_args = 0;
389 break;
393 if (spec->data_arg == -1 && spec->ndata_args > 0)
395 /* There are args consumed, but no positional spec. Use the
396 next sequential arg position. */
397 spec->data_arg = posn;
398 nargs += spec->ndata_args;
401 if (spec->info.spec == L'\0')
402 /* Format ended before this spec was complete. */
403 spec->end_of_fmt = spec->next_fmt = format - 1;
404 else
406 /* Find the next format spec. */
407 spec->end_of_fmt = format;
408 spec->next_fmt = find_spec (format, ps);
411 return nargs;