Updated to fedora-glibc-20071010T2047
[glibc.git] / stdio-common / printf-parsemb.c
blobda6fd3edb0bdde5e38747e753dc5b40b5c4f3d39
1 /* Helper functions for parsing printf format strings.
2 Copyright (C) 1995-2000,2002,2003,2004,2006 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <ctype.h>
21 #include <limits.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/param.h>
25 #include <wchar.h>
26 #include <wctype.h>
28 #ifndef COMPILE_WPRINTF
29 # define CHAR_T char
30 # define UCHAR_T unsigned char
31 # define INT_T int
32 # define L_(Str) Str
33 # define ISDIGIT(Ch) isdigit (Ch)
34 #else
35 # define CHAR_T wchar_t
36 # define UCHAR_T unsigned int
37 # define INT_T wint_t
38 # define L_(Str) L##Str
39 # define ISDIGIT(Ch) iswdigit (Ch)
40 #endif
42 #include "printf-parse.h"
44 #define NDEBUG 1
45 #include <assert.h>
49 /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
50 with the parsed details. POSN is the number of arguments already
51 consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
52 the number of args consumed by this spec; *MAX_REF_ARG is updated so it
53 remains the highest argument index used. */
54 size_t
55 attribute_hidden
56 #ifdef COMPILE_WPRINTF
57 __parse_one_specwc (const UCHAR_T *format, size_t posn,
58 struct printf_spec *spec, size_t *max_ref_arg)
59 #else
60 __parse_one_specmb (const UCHAR_T *format, size_t posn,
61 struct printf_spec *spec, size_t *max_ref_arg)
62 #endif
64 unsigned int n;
65 size_t nargs = 0;
67 /* Skip the '%'. */
68 ++format;
70 /* Clear information structure. */
71 spec->data_arg = -1;
72 spec->info.alt = 0;
73 spec->info.space = 0;
74 spec->info.left = 0;
75 spec->info.showsign = 0;
76 spec->info.group = 0;
77 spec->info.i18n = 0;
78 spec->info.extra = 0;
79 spec->info.pad = ' ';
80 spec->info.wide = sizeof (UCHAR_T) > 1;
82 /* Test for positional argument. */
83 if (ISDIGIT (*format))
85 const UCHAR_T *begin = format;
87 n = read_int (&format);
89 if (n > 0 && *format == L_('$'))
90 /* Is positional parameter. */
92 ++format; /* Skip the '$'. */
93 spec->data_arg = n - 1;
94 *max_ref_arg = MAX (*max_ref_arg, n);
96 else
97 /* Oops; that was actually the width and/or 0 padding flag.
98 Step back and read it again. */
99 format = begin;
102 /* Check for spec modifiers. */
105 switch (*format)
107 case L_(' '):
108 /* Output a space in place of a sign, when there is no sign. */
109 spec->info.space = 1;
110 continue;
111 case L_('+'):
112 /* Always output + or - for numbers. */
113 spec->info.showsign = 1;
114 continue;
115 case L_('-'):
116 /* Left-justify things. */
117 spec->info.left = 1;
118 continue;
119 case L_('#'):
120 /* Use the "alternate form":
121 Hex has 0x or 0X, FP always has a decimal point. */
122 spec->info.alt = 1;
123 continue;
124 case L_('0'):
125 /* Pad with 0s. */
126 spec->info.pad = '0';
127 continue;
128 case L_('\''):
129 /* Show grouping in numbers if the locale information
130 indicates any. */
131 spec->info.group = 1;
132 continue;
133 case L_('I'):
134 /* Use the internationalized form of the output. Currently
135 means to use the `outdigits' of the current locale. */
136 spec->info.i18n = 1;
137 continue;
138 default:
139 break;
141 break;
143 while (*++format);
145 if (spec->info.left)
146 spec->info.pad = ' ';
148 /* Get the field width. */
149 spec->width_arg = -1;
150 spec->info.width = 0;
151 if (*format == L_('*'))
153 /* The field width is given in an argument.
154 A negative field width indicates left justification. */
155 const UCHAR_T *begin = ++format;
157 if (ISDIGIT (*format))
159 /* The width argument might be found in a positional parameter. */
160 n = read_int (&format);
162 if (n > 0 && *format == L_('$'))
164 spec->width_arg = n - 1;
165 *max_ref_arg = MAX (*max_ref_arg, n);
166 ++format; /* Skip '$'. */
170 if (spec->width_arg < 0)
172 /* Not in a positional parameter. Consume one argument. */
173 spec->width_arg = posn++;
174 ++nargs;
175 format = begin; /* Step back and reread. */
178 else if (ISDIGIT (*format))
179 /* Constant width specification. */
180 spec->info.width = read_int (&format);
182 /* Get the precision. */
183 spec->prec_arg = -1;
184 /* -1 means none given; 0 means explicit 0. */
185 spec->info.prec = -1;
186 if (*format == L_('.'))
188 ++format;
189 if (*format == L_('*'))
191 /* The precision is given in an argument. */
192 const UCHAR_T *begin = ++format;
194 if (ISDIGIT (*format))
196 n = read_int (&format);
198 if (n > 0 && *format == L_('$'))
200 spec->prec_arg = n - 1;
201 *max_ref_arg = MAX (*max_ref_arg, n);
202 ++format;
206 if (spec->prec_arg < 0)
208 /* Not in a positional parameter. */
209 spec->prec_arg = posn++;
210 ++nargs;
211 format = begin;
214 else if (ISDIGIT (*format))
215 spec->info.prec = read_int (&format);
216 else
217 /* "%.?" is treated like "%.0?". */
218 spec->info.prec = 0;
221 /* Check for type modifiers. */
222 spec->info.is_long_double = 0;
223 spec->info.is_short = 0;
224 spec->info.is_long = 0;
225 spec->info.is_char = 0;
227 switch (*format++)
229 case L_('h'):
230 /* ints are short ints or chars. */
231 if (*format != L_('h'))
232 spec->info.is_short = 1;
233 else
235 ++format;
236 spec->info.is_char = 1;
238 break;
239 case L_('l'):
240 /* ints are long ints. */
241 spec->info.is_long = 1;
242 if (*format != L_('l'))
243 break;
244 ++format;
245 /* FALLTHROUGH */
246 case L_('L'):
247 /* doubles are long doubles, and ints are long long ints. */
248 case L_('q'):
249 /* 4.4 uses this for long long. */
250 spec->info.is_long_double = 1;
251 break;
252 case L_('z'):
253 case L_('Z'):
254 /* ints are size_ts. */
255 assert (sizeof (size_t) <= sizeof (unsigned long long int));
256 #if LONG_MAX != LONG_LONG_MAX
257 spec->info.is_long_double = sizeof (size_t) > sizeof (unsigned long int);
258 #endif
259 spec->info.is_long = sizeof (size_t) > sizeof (unsigned int);
260 break;
261 case L_('t'):
262 assert (sizeof (ptrdiff_t) <= sizeof (long long int));
263 #if LONG_MAX != LONG_LONG_MAX
264 spec->info.is_long_double = (sizeof (ptrdiff_t) > sizeof (long int));
265 #endif
266 spec->info.is_long = sizeof (ptrdiff_t) > sizeof (int);
267 break;
268 case L_('j'):
269 assert (sizeof (uintmax_t) <= sizeof (unsigned long long int));
270 #if LONG_MAX != LONG_LONG_MAX
271 spec->info.is_long_double = (sizeof (uintmax_t)
272 > sizeof (unsigned long int));
273 #endif
274 spec->info.is_long = sizeof (uintmax_t) > sizeof (unsigned int);
275 break;
276 default:
277 /* Not a recognized modifier. Backup. */
278 --format;
279 break;
282 /* Get the format specification. */
283 spec->info.spec = (wchar_t) *format++;
284 if (__builtin_expect (__printf_function_table != NULL, 0)
285 && spec->info.spec <= UCHAR_MAX
286 && __printf_arginfo_table[spec->info.spec] != NULL)
287 /* We don't try to get the types for all arguments if the format
288 uses more than one. The normal case is covered though. */
289 spec->ndata_args = (*__printf_arginfo_table[spec->info.spec])
290 (&spec->info, 1, &spec->data_arg_type);
291 else
293 /* Find the data argument types of a built-in spec. */
294 spec->ndata_args = 1;
296 switch (spec->info.spec)
298 case L'i':
299 case L'd':
300 case L'u':
301 case L'o':
302 case L'X':
303 case L'x':
304 #if LONG_MAX != LONG_LONG_MAX
305 if (spec->info.is_long_double)
306 spec->data_arg_type = PA_INT|PA_FLAG_LONG_LONG;
307 else
308 #endif
309 if (spec->info.is_long)
310 spec->data_arg_type = PA_INT|PA_FLAG_LONG;
311 else if (spec->info.is_short)
312 spec->data_arg_type = PA_INT|PA_FLAG_SHORT;
313 else if (spec->info.is_char)
314 spec->data_arg_type = PA_CHAR;
315 else
316 spec->data_arg_type = PA_INT;
317 break;
318 case L'e':
319 case L'E':
320 case L'f':
321 case L'F':
322 case L'g':
323 case L'G':
324 case L'a':
325 case L'A':
326 if (spec->info.is_long_double)
327 spec->data_arg_type = PA_DOUBLE|PA_FLAG_LONG_DOUBLE;
328 else
329 spec->data_arg_type = PA_DOUBLE;
330 break;
331 case L'c':
332 spec->data_arg_type = PA_CHAR;
333 break;
334 case L'C':
335 spec->data_arg_type = PA_WCHAR;
336 break;
337 case L's':
338 spec->data_arg_type = PA_STRING;
339 break;
340 case L'S':
341 spec->data_arg_type = PA_WSTRING;
342 break;
343 case L'p':
344 spec->data_arg_type = PA_POINTER;
345 break;
346 case L'n':
347 spec->data_arg_type = PA_INT|PA_FLAG_PTR;
348 break;
350 case L'm':
351 default:
352 /* An unknown spec will consume no args. */
353 spec->ndata_args = 0;
354 break;
358 if (spec->data_arg == -1 && spec->ndata_args > 0)
360 /* There are args consumed, but no positional spec. Use the
361 next sequential arg position. */
362 spec->data_arg = posn;
363 nargs += spec->ndata_args;
366 if (spec->info.spec == L'\0')
367 /* Format ended before this spec was complete. */
368 spec->end_of_fmt = spec->next_fmt = format - 1;
369 else
371 /* Find the next format spec. */
372 spec->end_of_fmt = format;
373 #ifdef COMPILE_WPRINTF
374 spec->next_fmt = __find_specwc (format);
375 #else
376 spec->next_fmt = __find_specmb (format);
377 #endif
380 return nargs;