Update
[glibc.git] / stdio-common / printf-parse.h
blobe9772ef6666dc49a59750fb399d6ba2e24d953ad
1 /* Internal header for parsing printf format strings.
2 Copyright (C) 1995-1999, 2000 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 <limits.h>
22 #include <printf.h>
23 #include <stdint.h>
24 #include <stddef.h>
25 #include <string.h>
27 #define NDEBUG 1
28 #include <assert.h>
31 struct printf_spec
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. */
47 size_t ndata_args;
51 /* The various kinds off arguments that can be passed to printf. */
52 union printf_arg
54 unsigned char pa_char;
55 wchar_t pa_wchar;
56 short int pa_short_int;
57 int pa_int;
58 long int pa_long_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;
64 float pa_float;
65 double pa_double;
66 long double pa_long_double;
67 const char *pa_string;
68 const wchar_t *pa_wstring;
69 void *pa_pointer;
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)))
82 retval *= 10;
83 retval += **pstr - L_('0');
86 return retval;
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)
96 #else
97 find_spec (const UCHAR_T *format, mbstate_t *ps)
98 #endif
100 #ifdef COMPILE_WPRINTF
101 return (const UCHAR_T *) __wcschrnul ((const CHAR_T *) format, L'%');
102 #else
103 while (*format != L_('\0') && *format != L_('%'))
105 int len;
107 /* Remove any hints of a wrong encoding. */
108 ps->__count = 0;
109 if (! isascii (*format) && (len = mbrlen (format, MB_CUR_MAX, ps)) > 0)
110 format += len;
111 else
112 ++format;
114 return format;
115 #endif
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. */
129 static inline size_t
130 #ifdef COMPILE_WPRINTF
131 parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec,
132 size_t *max_ref_arg)
133 #else
134 parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec,
135 size_t *max_ref_arg, mbstate_t *ps)
136 #endif
138 unsigned int n;
139 size_t nargs = 0;
141 /* Skip the '%'. */
142 ++format;
144 /* Clear information structure. */
145 spec->data_arg = -1;
146 spec->info.alt = 0;
147 spec->info.space = 0;
148 spec->info.left = 0;
149 spec->info.showsign = 0;
150 spec->info.group = 0;
151 spec->info.pad = ' ';
152 spec->info.wide = sizeof (UCHAR_T) > 1;
154 /* Test for positional argument. */
155 if (ISDIGIT (*format))
157 const UCHAR_T *begin = format;
159 n = read_int (&format);
161 if (n > 0 && *format == L_('$'))
162 /* Is positional parameter. */
164 ++format; /* Skip the '$'. */
165 spec->data_arg = n - 1;
166 *max_ref_arg = MAX (*max_ref_arg, n);
168 else
169 /* Oops; that was actually the width and/or 0 padding flag.
170 Step back and read it again. */
171 format = begin;
174 /* Check for spec modifiers. */
175 while (*format == L_(' ') || *format == L_('+') || *format == L_('-') ||
176 *format == L_('#') || *format == L_('0') || *format == L_('\''))
177 switch (*format++)
179 case L_(' '):
180 /* Output a space in place of a sign, when there is no sign. */
181 spec->info.space = 1;
182 break;
183 case L_('+'):
184 /* Always output + or - for numbers. */
185 spec->info.showsign = 1;
186 break;
187 case L_('-'):
188 /* Left-justify things. */
189 spec->info.left = 1;
190 break;
191 case L_('#'):
192 /* Use the "alternate form":
193 Hex has 0x or 0X, FP always has a decimal point. */
194 spec->info.alt = 1;
195 break;
196 case L_('0'):
197 /* Pad with 0s. */
198 spec->info.pad = '0';
199 break;
200 case L_('\''):
201 /* Show grouping in numbers if the locale information
202 indicates any. */
203 spec->info.group = 1;
204 break;
205 case L_('I'):
206 /* Use the internationalized form of the output. Currently
207 means to use the `outdigits' of the current locale. */
208 spec->info.i18n = 1;
209 break;
211 if (spec->info.left)
212 spec->info.pad = ' ';
214 /* Get the field width. */
215 spec->width_arg = -1;
216 spec->info.width = 0;
217 if (*format == L_('*'))
219 /* The field width is given in an argument.
220 A negative field width indicates left justification. */
221 const UCHAR_T *begin = ++format;
223 if (ISDIGIT (*format))
225 /* The width argument might be found in a positional parameter. */
226 n = read_int (&format);
228 if (n > 0 && *format == L_('$'))
230 spec->width_arg = n - 1;
231 *max_ref_arg = MAX (*max_ref_arg, n);
232 ++format; /* Skip '$'. */
236 if (spec->width_arg < 0)
238 /* Not in a positional parameter. Consume one argument. */
239 spec->width_arg = posn++;
240 ++nargs;
241 format = begin; /* Step back and reread. */
244 else if (ISDIGIT (*format))
245 /* Constant width specification. */
246 spec->info.width = read_int (&format);
248 /* Get the precision. */
249 spec->prec_arg = -1;
250 /* -1 means none given; 0 means explicit 0. */
251 spec->info.prec = -1;
252 if (*format == L_('.'))
254 ++format;
255 if (*format == L_('*'))
257 /* The precision is given in an argument. */
258 const UCHAR_T *begin = ++format;
260 if (ISDIGIT (*format))
262 n = read_int (&format);
264 if (n > 0 && *format == L_('$'))
266 spec->prec_arg = n - 1;
267 *max_ref_arg = MAX (*max_ref_arg, n);
268 ++format;
272 if (spec->prec_arg < 0)
274 /* Not in a positional parameter. */
275 spec->prec_arg = posn++;
276 ++nargs;
277 format = begin;
280 else if (ISDIGIT (*format))
281 spec->info.prec = read_int (&format);
282 else
283 /* "%.?" is treated like "%.0?". */
284 spec->info.prec = 0;
287 /* Check for type modifiers. */
288 spec->info.is_long_double = 0;
289 spec->info.is_short = 0;
290 spec->info.is_long = 0;
291 spec->info.is_char = 0;
293 switch (*format++)
295 case L_('h'):
296 /* ints are short ints or chars. */
297 if (*format != L_('h'))
298 spec->info.is_short = 1;
299 else
301 ++format;
302 spec->info.is_char = 1;
304 break;
305 case L_('l'):
306 /* ints are long ints. */
307 spec->info.is_long = 1;
308 if (*format != L_('l'))
309 break;
310 ++format;
311 /* FALLTHROUGH */
312 case L_('L'):
313 /* doubles are long doubles, and ints are long long ints. */
314 case L_('q'):
315 /* 4.4 uses this for long long. */
316 spec->info.is_long_double = 1;
317 break;
318 case L_('z'):
319 case L_('Z'):
320 /* ints are size_ts. */
321 assert (sizeof (size_t) <= sizeof (unsigned long long int));
322 #if LONG_MAX != LONG_LONG_MAX
323 spec->info.is_long_double = sizeof (size_t) > sizeof (unsigned long int);
324 #endif
325 spec->info.is_long = sizeof (size_t) > sizeof (unsigned int);
326 break;
327 case L_('t'):
328 assert (sizeof (ptrdiff_t) <= sizeof (long long int));
329 #if LONG_MAX != LONG_LONG_MAX
330 spec->info.is_long_double = (sizeof (ptrdiff_t) > sizeof (long int));
331 #endif
332 spec->info.is_long = sizeof (ptrdiff_t) > sizeof (int);
333 break;
334 case L_('j'):
335 assert (sizeof (uintmax_t) <= sizeof (unsigned long long int));
336 #if LONG_MAX != LONG_LONG_MAX
337 spec->info.is_long_double = (sizeof (uintmax_t)
338 > sizeof (unsigned long int));
339 #endif
340 spec->info.is_long = sizeof (uintmax_t) > sizeof (unsigned int);
341 break;
342 default:
343 /* Not a recognized modifier. Backup. */
344 --format;
345 break;
348 /* Get the format specification. */
349 spec->info.spec = (wchar_t) *format++;
350 if (__printf_function_table != NULL
351 && spec->info.spec <= UCHAR_MAX
352 && __printf_arginfo_table[spec->info.spec] != NULL)
353 /* We don't try to get the types for all arguments if the format
354 uses more than one. The normal case is covered though. */
355 spec->ndata_args = (*__printf_arginfo_table[spec->info.spec])
356 (&spec->info, 1, &spec->data_arg_type);
357 else
359 /* Find the data argument types of a built-in spec. */
360 spec->ndata_args = 1;
362 switch (spec->info.spec)
364 case L'i':
365 case L'd':
366 case L'u':
367 case L'o':
368 case L'X':
369 case L'x':
370 #if LONG_MAX != LONG_LONG_MAX
371 if (spec->info.is_long_double)
372 spec->data_arg_type = PA_INT|PA_FLAG_LONG_LONG;
373 else
374 #endif
375 if (spec->info.is_long)
376 spec->data_arg_type = PA_INT|PA_FLAG_LONG;
377 else if (spec->info.is_short)
378 spec->data_arg_type = PA_INT|PA_FLAG_SHORT;
379 else if (spec->info.is_char)
380 spec->data_arg_type = PA_CHAR;
381 else
382 spec->data_arg_type = PA_INT;
383 break;
384 case L'e':
385 case L'E':
386 case L'f':
387 case L'g':
388 case L'G':
389 case L'a':
390 case L'A':
391 if (spec->info.is_long_double)
392 spec->data_arg_type = PA_DOUBLE|PA_FLAG_LONG_DOUBLE;
393 else
394 spec->data_arg_type = PA_DOUBLE;
395 break;
396 case L'c':
397 spec->data_arg_type = PA_CHAR;
398 break;
399 case L'C':
400 spec->data_arg_type = PA_WCHAR;
401 break;
402 case L's':
403 spec->data_arg_type = PA_STRING;
404 break;
405 case L'S':
406 spec->data_arg_type = PA_WSTRING;
407 break;
408 case L'p':
409 spec->data_arg_type = PA_POINTER;
410 break;
411 case L'n':
412 spec->data_arg_type = PA_INT|PA_FLAG_PTR;
413 break;
415 case L'm':
416 default:
417 /* An unknown spec will consume no args. */
418 spec->ndata_args = 0;
419 break;
423 if (spec->data_arg == -1 && spec->ndata_args > 0)
425 /* There are args consumed, but no positional spec. Use the
426 next sequential arg position. */
427 spec->data_arg = posn;
428 nargs += spec->ndata_args;
431 if (spec->info.spec == L'\0')
432 /* Format ended before this spec was complete. */
433 spec->end_of_fmt = spec->next_fmt = format - 1;
434 else
436 /* Find the next format spec. */
437 spec->end_of_fmt = format;
438 #ifdef COMPILE_WPRINTF
439 spec->next_fmt = find_spec (format);
440 #else
441 spec->next_fmt = find_spec (format, ps);
442 #endif
445 return nargs;