Thu Nov 2 19:24:37 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / stdio-common / printf-parse.h
blob0f6e9e287b6cbcfd71c420b8f85f09e2a58d9525
1 /* Internal header for parsing printf format strings.
2 Copyright (C) 1995 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. */
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 short int pa_short_int;
58 int pa_int;
59 long int pa_long_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;
65 float pa_float;
66 double pa_double;
67 long double pa_long_double;
68 const char *pa_string;
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 char * *pstr)
78 unsigned int retval = **pstr - '0';
80 while (isdigit (*++(*pstr)))
82 retval *= 10;
83 retval += **pstr - '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 char *
94 find_spec (const char *format)
96 while (*format != '\0' && *format != '%')
98 int len;
100 if (isascii (*format) || (len = mblen (format, MB_CUR_MAX)) <= 0)
101 ++format;
102 else
103 format += len;
105 return format;
109 /* This is defined in reg-printf.c. */
110 extern printf_arginfo_function **__printf_arginfo_table;
113 /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
114 with the parsed details. POSN is the number of arguments already
115 consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
116 the number of args consumed by this spec; *MAX_REF_ARG is updated so it
117 remains the highest argument index used. */
118 static inline size_t
119 parse_one_spec (const char *format, size_t posn, struct printf_spec *spec,
120 size_t *max_ref_arg)
122 unsigned int n;
123 size_t nargs = 0;
125 /* Skip the '%'. */
126 ++format;
128 /* Clear information structure. */
129 spec->data_arg = -1;
130 spec->info.alt = 0;
131 spec->info.space = 0;
132 spec->info.left = 0;
133 spec->info.showsign = 0;
134 spec->info.group = 0;
135 spec->info.pad = ' ';
137 /* Test for positional argument. */
138 if (isdigit (*format))
140 const char *begin = format;
142 n = read_int (&format);
144 if (n > 0 && *format == '$')
145 /* Is positional parameter. */
147 ++format; /* Skip the '$'. */
148 spec->data_arg = n - 1;
149 *max_ref_arg = MAX (*max_ref_arg, n);
151 else
152 /* Oops; that was actually the width and/or 0 padding flag.
153 Step back and read it again. */
154 format = begin;
157 /* Check for spec modifiers. */
158 while (*format == ' ' || *format == '+' || *format == '-' ||
159 *format == '#' || *format == '0' || *format == '\'')
160 switch (*format++)
162 case ' ':
163 /* Output a space in place of a sign, when there is no sign. */
164 spec->info.space = 1;
165 break;
166 case '+':
167 /* Always output + or - for numbers. */
168 spec->info.showsign = 1;
169 break;
170 case '-':
171 /* Left-justify things. */
172 spec->info.left = 1;
173 break;
174 case '#':
175 /* Use the "alternate form":
176 Hex has 0x or 0X, FP always has a decimal point. */
177 spec->info.alt = 1;
178 break;
179 case '0':
180 /* Pad with 0s. */
181 spec->info.pad = '0';
182 break;
183 case '\'':
184 /* Show grouping in numbers if the locale information
185 indicates any. */
186 spec->info.group = 1;
187 break;
189 if (spec->info.left)
190 spec->info.pad = ' ';
192 /* Get the field width. */
193 spec->width_arg = -1;
194 spec->info.width = 0;
195 if (*format == '*')
197 /* The field width is given in an argument.
198 A negative field width indicates left justification. */
199 const char *begin = ++format;
201 if (isdigit (*format))
203 /* The width argument might be found in a positional parameter. */
204 n = read_int (&format);
206 if (n > 0 && *format == '$')
208 spec->width_arg = n - 1;
209 *max_ref_arg = MAX (*max_ref_arg, n);
210 ++format; /* Skip '$'. */
214 if (spec->width_arg < 0)
216 /* Not in a positional parameter. Consume one argument. */
217 spec->width_arg = posn++;
218 ++nargs;
219 format = begin; /* Step back and reread. */
222 else if (isdigit (*format))
223 /* Constant width specification. */
224 spec->info.width = read_int (&format);
226 /* Get the precision. */
227 spec->prec_arg = -1;
228 /* -1 means none given; 0 means explicit 0. */
229 spec->info.prec = -1;
230 if (*format == '.')
232 ++format;
233 if (*format == '*')
235 /* The precision is given in an argument. */
236 const char *begin = ++format;
238 if (isdigit (*format))
240 n = read_int (&format);
242 if (n > 0 && *format == '$')
244 spec->prec_arg = n - 1;
245 *max_ref_arg = MAX (*max_ref_arg, n);
246 ++format;
250 if (spec->prec_arg < 0)
252 /* Not in a positional parameter. */
253 spec->prec_arg = posn++;
254 ++nargs;
255 format = begin;
258 else if (isdigit (*format))
259 spec->info.prec = read_int (&format);
260 else
261 /* "%.?" is treated like "%.0?". */
262 spec->info.prec = 0;
264 /* If there was a precision specified, ignore the 0 flag and always
265 pad with spaces. */
266 spec->info.pad = ' ';
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 while (*format == 'h' || *format == 'l' || *format == 'L' ||
276 *format == 'Z' || *format == 'q')
277 switch (*format++)
279 case 'h':
280 /* int's are short int's. */
281 spec->info.is_short = 1;
282 break;
283 case 'l':
284 if (spec->info.is_long)
285 /* A double `l' is equivalent to an `L'. */
286 spec->info.is_longlong = 1;
287 else
288 /* int's are long int's. */
289 spec->info.is_long = 1;
290 break;
291 case 'L':
292 /* double's are long double's, and int's are long long int's. */
293 spec->info.is_long_double = 1;
294 break;
295 case 'Z':
296 /* int's are size_t's. */
297 assert (sizeof(size_t) <= sizeof(unsigned long long int));
298 spec->info.is_longlong = sizeof(size_t) > sizeof(unsigned long int);
299 spec->info.is_long = sizeof(size_t) > sizeof(unsigned int);
300 break;
301 case 'q':
302 /* 4.4 uses this for long long. */
303 spec->info.is_longlong = 1;
304 break;
307 /* Get the format specification. */
308 spec->info.spec = *format++;
309 if (__printf_arginfo_table != NULL &&
310 __printf_arginfo_table[spec->info.spec] != NULL)
311 /* We don't try to get the types for all arguments if the format
312 uses more than one. The normal case is covered though. */
313 spec->ndata_args = (*__printf_arginfo_table[spec->info.spec])
314 (&spec->info, 1, &spec->data_arg_type);
315 else
317 /* Find the data argument types of a built-in spec. */
318 spec->ndata_args = 1;
320 switch (spec->info.spec)
322 case 'i':
323 case 'd':
324 case 'u':
325 case 'o':
326 case 'X':
327 case 'x':
328 if (spec->info.is_longlong)
329 spec->data_arg_type = PA_INT|PA_FLAG_LONG_LONG;
330 else if (spec->info.is_long)
331 spec->data_arg_type = PA_INT|PA_FLAG_LONG;
332 else if (spec->info.is_short)
333 spec->data_arg_type = PA_INT|PA_FLAG_SHORT;
334 else
335 spec->data_arg_type = PA_INT;
336 break;
337 case 'e':
338 case 'E':
339 case 'f':
340 case 'g':
341 case 'G':
342 if (spec->info.is_long_double)
343 spec->data_arg_type = PA_DOUBLE|PA_FLAG_LONG_DOUBLE;
344 else
345 spec->data_arg_type = PA_DOUBLE;
346 break;
347 case 'c':
348 spec->data_arg_type = PA_CHAR;
349 break;
350 case 's':
351 spec->data_arg_type = PA_STRING;
352 break;
353 case 'p':
354 spec->data_arg_type = PA_POINTER;
355 break;
356 case 'n':
357 spec->data_arg_type = PA_INT|PA_FLAG_PTR;
358 break;
360 case 'm':
361 default:
362 /* An unknown spec will consume no args. */
363 spec->ndata_args = 0;
364 break;
367 if (spec->data_arg == -1 && spec->ndata_args > 0)
369 /* There are args consumed, but no positional spec.
370 Use the next sequential arg position. */
371 spec->data_arg = posn;
372 posn += spec->ndata_args;
373 nargs += spec->ndata_args;
377 if (spec->info.spec == '\0')
378 /* Format ended before this spec was complete. */
379 spec->end_of_fmt = spec->next_fmt = format - 1;
380 else
382 /* Find the next format spec. */
383 spec->end_of_fmt = format;
384 spec->next_fmt = find_spec (format);
387 return nargs;