patches from bug-gnu-utils to support more architectures
[glibc.git] / stdio-common / printf-parse.h
blob86a9821b1fd98d8f0a1d782d0ecbdfd812c4b0e0
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. */
20 #include <ctype.h>
21 #include <printf.h>
22 #include <string.h>
23 #include <stddef.h>
25 #define NDEBUG 1
26 #include <assert.h>
28 #define MAX(a,b) ({typeof(a) _a = (a); typeof(b) _b = (b); \
29 _a > _b ? _a : _b; })
30 #define MIN(a,b) ({typeof(a) _a = (a); typeof(b) _b = (b); \
31 _a < _b ? _a : _b; })
33 struct printf_spec
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. */
49 size_t ndata_args;
53 /* The various kinds off arguments that can be passed to printf. */
54 union printf_arg
56 unsigned char pa_char;
57 wchar_t pa_wchar;
58 short int pa_short_int;
59 int pa_int;
60 long int pa_long_int;
61 long long int pa_long_long_int;
62 unsigned short int pa_u_short_int;
63 unsigned int pa_u_int;
64 unsigned long int pa_u_long_int;
65 unsigned long long int pa_u_long_long_int;
66 float pa_float;
67 double pa_double;
68 long double pa_long_double;
69 const char *pa_string;
70 const wchar_t *pa_wstring;
71 void *pa_pointer;
75 /* Read a simple integer from a string and update the string pointer.
76 It is assumed that the first character is a digit. */
77 static inline unsigned int
78 read_int (const UCHAR_T * *pstr)
80 unsigned int retval = **pstr - L_('0');
82 while (ISDIGIT (*++(*pstr)))
84 retval *= 10;
85 retval += **pstr - L_('0');
88 return retval;
93 /* Find the next spec in FORMAT, or the end of the string. Returns
94 a pointer into FORMAT, to a '%' or a '\0'. */
95 static inline const char *
96 find_spec (const char *format, mbstate_t *ps)
98 while (*format != '\0' && *format != '%')
100 int len;
102 /* Remove any hints of a wrong encoding. */
103 ps->count = 0;
104 if (isascii (*format) || (len = mbrlen (format, MB_CUR_MAX, ps)) <= 0)
105 ++format;
106 else
107 format += len;
109 return format;
113 /* This is defined in reg-printf.c. */
114 extern printf_arginfo_function **__printf_arginfo_table;
117 /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
118 with the parsed details. POSN is the number of arguments already
119 consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
120 the number of args consumed by this spec; *MAX_REF_ARG is updated so it
121 remains the highest argument index used. */
122 static inline size_t
123 parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec,
124 size_t *max_ref_arg, mbstate_t *ps)
126 unsigned int n;
127 size_t nargs = 0;
129 /* Skip the '%'. */
130 ++format;
132 /* Clear information structure. */
133 spec->data_arg = -1;
134 spec->info.alt = 0;
135 spec->info.space = 0;
136 spec->info.left = 0;
137 spec->info.showsign = 0;
138 spec->info.group = 0;
139 spec->info.pad = ' ';
141 /* Test for positional argument. */
142 if (ISDIGIT (*format))
144 const UCHAR_T *begin = format;
146 n = read_int (&format);
148 if (n > 0 && *format == L_('$'))
149 /* Is positional parameter. */
151 ++format; /* Skip the '$'. */
152 spec->data_arg = n - 1;
153 *max_ref_arg = MAX (*max_ref_arg, n);
155 else
156 /* Oops; that was actually the width and/or 0 padding flag.
157 Step back and read it again. */
158 format = begin;
161 /* Check for spec modifiers. */
162 while (*format == L_(' ') || *format == L_('+') || *format == L_('-') ||
163 *format == L_('#') || *format == L_('0') || *format == L_('\''))
164 switch (*format++)
166 case L_(' '):
167 /* Output a space in place of a sign, when there is no sign. */
168 spec->info.space = 1;
169 break;
170 case L_('+'):
171 /* Always output + or - for numbers. */
172 spec->info.showsign = 1;
173 break;
174 case L_('-'):
175 /* Left-justify things. */
176 spec->info.left = 1;
177 break;
178 case L_('#'):
179 /* Use the "alternate form":
180 Hex has 0x or 0X, FP always has a decimal point. */
181 spec->info.alt = 1;
182 break;
183 case L_('0'):
184 /* Pad with 0s. */
185 spec->info.pad = '0';
186 break;
187 case L_('\''):
188 /* Show grouping in numbers if the locale information
189 indicates any. */
190 spec->info.group = 1;
191 break;
193 if (spec->info.left)
194 spec->info.pad = ' ';
196 /* Get the field width. */
197 spec->width_arg = -1;
198 spec->info.width = 0;
199 if (*format == L_('*'))
201 /* The field width is given in an argument.
202 A negative field width indicates left justification. */
203 const UCHAR_T *begin = ++format;
205 if (ISDIGIT (*format))
207 /* The width argument might be found in a positional parameter. */
208 n = read_int (&format);
210 if (n > 0 && *format == L_('$'))
212 spec->width_arg = n - 1;
213 *max_ref_arg = MAX (*max_ref_arg, n);
214 ++format; /* Skip '$'. */
218 if (spec->width_arg < 0)
220 /* Not in a positional parameter. Consume one argument. */
221 spec->width_arg = posn++;
222 ++nargs;
223 format = begin; /* Step back and reread. */
226 else if (ISDIGIT (*format))
227 /* Constant width specification. */
228 spec->info.width = read_int (&format);
230 /* Get the precision. */
231 spec->prec_arg = -1;
232 /* -1 means none given; 0 means explicit 0. */
233 spec->info.prec = -1;
234 if (*format == L_('.'))
236 ++format;
237 if (*format == L_('*'))
239 /* The precision is given in an argument. */
240 const UCHAR_T *begin = ++format;
242 if (ISDIGIT (*format))
244 n = read_int (&format);
246 if (n > 0 && *format == L_('$'))
248 spec->prec_arg = n - 1;
249 *max_ref_arg = MAX (*max_ref_arg, n);
250 ++format;
254 if (spec->prec_arg < 0)
256 /* Not in a positional parameter. */
257 spec->prec_arg = posn++;
258 ++nargs;
259 format = begin;
262 else if (ISDIGIT (*format))
263 spec->info.prec = read_int (&format);
264 else
265 /* "%.?" is treated like "%.0?". */
266 spec->info.prec = 0;
269 /* Check for type modifiers. */
270 #define is_longlong is_long_double
271 spec->info.is_long_double = 0;
272 spec->info.is_short = 0;
273 spec->info.is_long = 0;
275 if (*format == L_('h') || *format == L_('l') || *format == L_('L') ||
276 *format == L_('Z') || *format == L_('q'))
277 switch (*format++)
279 case L_('h'):
280 /* int's are short int's. */
281 spec->info.is_short = 1;
282 break;
283 case L_('l'):
284 /* int's are long int's. */
285 spec->info.is_long = 1;
286 if (*format != L_('l'))
287 break;
288 ++format;
289 /* FALLTHROUGH */
290 case L_('L'):
291 /* double's are long double's, and int's are long long int's. */
292 case L_('q'):
293 /* 4.4 uses this for long long. */
294 spec->info.is_long_double = 1;
295 break;
296 case L_('Z'):
297 /* int's are size_t's. */
298 assert (sizeof(size_t) <= sizeof(unsigned long long int));
299 spec->info.is_longlong = sizeof(size_t) > sizeof(unsigned long int);
300 spec->info.is_long = sizeof(size_t) > sizeof(unsigned int);
301 break;
304 /* Get the format specification. */
305 spec->info.spec = (wchar_t) *format++;
306 if (__printf_arginfo_table != NULL &&
307 __printf_arginfo_table[spec->info.spec] != NULL)
308 /* We don't try to get the types for all arguments if the format
309 uses more than one. The normal case is covered though. */
310 spec->ndata_args = (*__printf_arginfo_table[spec->info.spec])
311 (&spec->info, 1, &spec->data_arg_type);
312 else
314 /* Find the data argument types of a built-in spec. */
315 spec->ndata_args = 1;
317 switch (spec->info.spec)
319 case L'i':
320 case L'd':
321 case L'u':
322 case L'o':
323 case L'X':
324 case L'x':
325 if (spec->info.is_longlong)
326 spec->data_arg_type = PA_INT|PA_FLAG_LONG_LONG;
327 else if (spec->info.is_long)
328 spec->data_arg_type = PA_INT|PA_FLAG_LONG;
329 else if (spec->info.is_short)
330 spec->data_arg_type = PA_INT|PA_FLAG_SHORT;
331 else
332 spec->data_arg_type = PA_INT;
333 break;
334 case L'e':
335 case L'E':
336 case L'f':
337 case L'g':
338 case L'G':
339 if (spec->info.is_long_double)
340 spec->data_arg_type = PA_DOUBLE|PA_FLAG_LONG_DOUBLE;
341 else
342 spec->data_arg_type = PA_DOUBLE;
343 break;
344 case L'c':
345 spec->data_arg_type = PA_CHAR;
346 break;
347 case L'C':
348 spec->data_arg_type = PA_WCHAR;
349 break;
350 case L's':
351 spec->data_arg_type = PA_STRING;
352 break;
353 case L'S':
354 spec->data_arg_type = PA_WSTRING;
355 break;
356 case L'p':
357 spec->data_arg_type = PA_POINTER;
358 break;
359 case L'n':
360 spec->data_arg_type = PA_INT|PA_FLAG_PTR;
361 break;
363 case L'm':
364 default:
365 /* An unknown spec will consume no args. */
366 spec->ndata_args = 0;
367 break;
370 if (spec->data_arg == -1 && spec->ndata_args > 0)
372 /* There are args consumed, but no positional spec.
373 Use the next sequential arg position. */
374 spec->data_arg = posn;
375 posn += spec->ndata_args;
376 nargs += spec->ndata_args;
380 if (spec->info.spec == L'\0')
381 /* Format ended before this spec was complete. */
382 spec->end_of_fmt = spec->next_fmt = format - 1;
383 else
385 /* Find the next format spec. */
386 spec->end_of_fmt = format;
387 spec->next_fmt = find_spec (format, ps);
390 return nargs;