Update.
[glibc.git] / stdio-common / printf-parse.h
blob036b3b4dfe04106e36a0178df6cf643b51ce7364
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 <string.h>
23 #include <stddef.h>
25 #define NDEBUG 1
26 #include <assert.h>
29 struct printf_spec
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. */
45 size_t ndata_args;
49 /* The various kinds off arguments that can be passed to printf. */
50 union printf_arg
52 unsigned char pa_char;
53 wchar_t pa_wchar;
54 short int pa_short_int;
55 int pa_int;
56 long int pa_long_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;
62 float pa_float;
63 double pa_double;
64 long double pa_long_double;
65 const char *pa_string;
66 const wchar_t *pa_wstring;
67 void *pa_pointer;
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)))
80 retval *= 10;
81 retval += **pstr - L_('0');
84 return retval;
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 != '%')
96 int len;
98 /* Remove any hints of a wrong encoding. */
99 ps->count = 0;
100 if (isascii (*format) || (len = mbrlen (format, MB_CUR_MAX, ps)) <= 0)
101 ++format;
102 else
103 format += len;
105 return format;
109 /* These are defined in reg-printf.c. */
110 extern printf_arginfo_function *__printf_arginfo_table[];
111 extern printf_function **__printf_function_table;
114 /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
115 with the parsed details. POSN is the number of arguments already
116 consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
117 the number of args consumed by this spec; *MAX_REF_ARG is updated so it
118 remains the highest argument index used. */
119 static inline size_t
120 parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec,
121 size_t *max_ref_arg, mbstate_t *ps)
123 unsigned int n;
124 size_t nargs = 0;
126 /* Skip the '%'. */
127 ++format;
129 /* Clear information structure. */
130 spec->data_arg = -1;
131 spec->info.alt = 0;
132 spec->info.space = 0;
133 spec->info.left = 0;
134 spec->info.showsign = 0;
135 spec->info.group = 0;
136 spec->info.pad = ' ';
138 /* Test for positional argument. */
139 if (ISDIGIT (*format))
141 const UCHAR_T *begin = format;
143 n = read_int (&format);
145 if (n > 0 && *format == L_('$'))
146 /* Is positional parameter. */
148 ++format; /* Skip the '$'. */
149 spec->data_arg = n - 1;
150 *max_ref_arg = MAX (*max_ref_arg, n);
152 else
153 /* Oops; that was actually the width and/or 0 padding flag.
154 Step back and read it again. */
155 format = begin;
158 /* Check for spec modifiers. */
159 while (*format == L_(' ') || *format == L_('+') || *format == L_('-') ||
160 *format == L_('#') || *format == L_('0') || *format == L_('\''))
161 switch (*format++)
163 case L_(' '):
164 /* Output a space in place of a sign, when there is no sign. */
165 spec->info.space = 1;
166 break;
167 case L_('+'):
168 /* Always output + or - for numbers. */
169 spec->info.showsign = 1;
170 break;
171 case L_('-'):
172 /* Left-justify things. */
173 spec->info.left = 1;
174 break;
175 case L_('#'):
176 /* Use the "alternate form":
177 Hex has 0x or 0X, FP always has a decimal point. */
178 spec->info.alt = 1;
179 break;
180 case L_('0'):
181 /* Pad with 0s. */
182 spec->info.pad = '0';
183 break;
184 case L_('\''):
185 /* Show grouping in numbers if the locale information
186 indicates any. */
187 spec->info.group = 1;
188 break;
190 if (spec->info.left)
191 spec->info.pad = ' ';
193 /* Get the field width. */
194 spec->width_arg = -1;
195 spec->info.width = 0;
196 if (*format == L_('*'))
198 /* The field width is given in an argument.
199 A negative field width indicates left justification. */
200 const UCHAR_T *begin = ++format;
202 if (ISDIGIT (*format))
204 /* The width argument might be found in a positional parameter. */
205 n = read_int (&format);
207 if (n > 0 && *format == L_('$'))
209 spec->width_arg = n - 1;
210 *max_ref_arg = MAX (*max_ref_arg, n);
211 ++format; /* Skip '$'. */
215 if (spec->width_arg < 0)
217 /* Not in a positional parameter. Consume one argument. */
218 spec->width_arg = posn++;
219 ++nargs;
220 format = begin; /* Step back and reread. */
223 else if (ISDIGIT (*format))
224 /* Constant width specification. */
225 spec->info.width = read_int (&format);
227 /* Get the precision. */
228 spec->prec_arg = -1;
229 /* -1 means none given; 0 means explicit 0. */
230 spec->info.prec = -1;
231 if (*format == L_('.'))
233 ++format;
234 if (*format == L_('*'))
236 /* The precision is given in an argument. */
237 const UCHAR_T *begin = ++format;
239 if (ISDIGIT (*format))
241 n = read_int (&format);
243 if (n > 0 && *format == L_('$'))
245 spec->prec_arg = n - 1;
246 *max_ref_arg = MAX (*max_ref_arg, n);
247 ++format;
251 if (spec->prec_arg < 0)
253 /* Not in a positional parameter. */
254 spec->prec_arg = posn++;
255 ++nargs;
256 format = begin;
259 else if (ISDIGIT (*format))
260 spec->info.prec = read_int (&format);
261 else
262 /* "%.?" is treated like "%.0?". */
263 spec->info.prec = 0;
266 /* Check for type modifiers. */
267 #define is_longlong is_long_double
268 spec->info.is_long_double = 0;
269 spec->info.is_short = 0;
270 spec->info.is_long = 0;
271 spec->info.is_char = 0;
273 if (*format == L_('h') || *format == L_('l') || *format == L_('L') ||
274 *format == L_('Z') || *format == L_('q'))
275 switch (*format++)
277 case L_('h'):
278 /* int's are short int's. */
279 if (spec->info.is_short == 0)
280 spec->info.is_short = 1;
281 else
283 spec->info.is_short = 0;
284 spec->info.is_char = 1;
286 break;
287 case L_('l'):
288 /* int's are long int's. */
289 spec->info.is_long = 1;
290 if (*format != L_('l'))
291 break;
292 ++format;
293 /* FALLTHROUGH */
294 case L_('L'):
295 /* double's are long double's, and int's are long long int's. */
296 case L_('q'):
297 /* 4.4 uses this for long long. */
298 spec->info.is_long_double = 1;
299 break;
300 case L_('Z'):
301 /* int's are size_t's. */
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;
308 /* Get the format specification. */
309 spec->info.spec = (wchar_t) *format++;
310 if (__printf_function_table != NULL
311 && spec->info.spec <= UCHAR_MAX
312 && __printf_arginfo_table[spec->info.spec] != NULL)
313 /* We don't try to get the types for all arguments if the format
314 uses more than one. The normal case is covered though. */
315 spec->ndata_args = (*__printf_arginfo_table[spec->info.spec])
316 (&spec->info, 1, &spec->data_arg_type);
317 else
319 /* Find the data argument types of a built-in spec. */
320 spec->ndata_args = 1;
322 switch (spec->info.spec)
324 case L'i':
325 case L'd':
326 case L'u':
327 case L'o':
328 case L'X':
329 case L'x':
330 if (spec->info.is_longlong)
331 spec->data_arg_type = PA_INT|PA_FLAG_LONG_LONG;
332 else if (spec->info.is_long)
333 spec->data_arg_type = PA_INT|PA_FLAG_LONG;
334 else if (spec->info.is_short)
335 spec->data_arg_type = PA_INT|PA_FLAG_SHORT;
336 else if (spec->info.is_char)
337 spec->data_arg_type = PA_CHAR;
338 else
339 spec->data_arg_type = PA_INT;
340 break;
341 case L'e':
342 case L'E':
343 case L'f':
344 case L'g':
345 case L'G':
346 if (spec->info.is_long_double)
347 spec->data_arg_type = PA_DOUBLE|PA_FLAG_LONG_DOUBLE;
348 else
349 spec->data_arg_type = PA_DOUBLE;
350 break;
351 case L'c':
352 spec->data_arg_type = PA_CHAR;
353 break;
354 case L'C':
355 spec->data_arg_type = PA_WCHAR;
356 break;
357 case L's':
358 spec->data_arg_type = PA_STRING;
359 break;
360 case L'S':
361 spec->data_arg_type = PA_WSTRING;
362 break;
363 case L'p':
364 spec->data_arg_type = PA_POINTER;
365 break;
366 case L'n':
367 spec->data_arg_type = PA_INT|PA_FLAG_PTR;
368 break;
370 case L'm':
371 default:
372 /* An unknown spec will consume no args. */
373 spec->ndata_args = 0;
374 break;
378 if (spec->data_arg == -1 && spec->ndata_args > 0)
380 /* There are args consumed, but no positional spec. Use the
381 next sequential arg position. */
382 spec->data_arg = posn;
383 nargs += spec->ndata_args;
386 if (spec->info.spec == L'\0')
387 /* Format ended before this spec was complete. */
388 spec->end_of_fmt = spec->next_fmt = format - 1;
389 else
391 /* Find the next format spec. */
392 spec->end_of_fmt = format;
393 spec->next_fmt = find_spec (format, ps);
396 return nargs;