* locale/programs/localedef.c (add_to_readlist): Rename local
[glibc.git] / stdio-common / printf-parsemb.c
blob2f21fc6365cc4c07765de0878d69a18c0188e6a6
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 /* Find the next spec in FORMAT, or the end of the string. Returns
50 a pointer into FORMAT, to a '%' or a '\0'. */
51 const UCHAR_T *
52 #ifdef COMPILE_WPRINTF
53 __find_specwc (const UCHAR_T *format)
54 #else
55 __find_specmb (const UCHAR_T *format, mbstate_t *ps)
56 #endif
58 #ifdef COMPILE_WPRINTF
59 return (const UCHAR_T *) __wcschrnul ((const CHAR_T *) format, L'%');
60 #else
61 while (*format != L_('\0') && *format != L_('%'))
63 int len;
65 /* Remove any hints of a wrong encoding. */
66 ps->__count = 0;
67 if (! isascii (*format)
68 && (len = __mbrlen ((const CHAR_T *) format, MB_CUR_MAX, ps)) > 0)
69 format += len;
70 else
71 ++format;
73 return format;
74 #endif
78 /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
79 with the parsed details. POSN is the number of arguments already
80 consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
81 the number of args consumed by this spec; *MAX_REF_ARG is updated so it
82 remains the highest argument index used. */
83 size_t
84 attribute_hidden
85 #ifdef COMPILE_WPRINTF
86 __parse_one_specwc (const UCHAR_T *format, size_t posn,
87 struct printf_spec *spec, size_t *max_ref_arg)
88 #else
89 __parse_one_specmb (const UCHAR_T *format, size_t posn,
90 struct printf_spec *spec, size_t *max_ref_arg,
91 mbstate_t *ps)
92 #endif
94 unsigned int n;
95 size_t nargs = 0;
97 /* Skip the '%'. */
98 ++format;
100 /* Clear information structure. */
101 spec->data_arg = -1;
102 spec->info.alt = 0;
103 spec->info.space = 0;
104 spec->info.left = 0;
105 spec->info.showsign = 0;
106 spec->info.group = 0;
107 spec->info.i18n = 0;
108 spec->info.extra = 0;
109 spec->info.pad = ' ';
110 spec->info.wide = sizeof (UCHAR_T) > 1;
112 /* Test for positional argument. */
113 if (ISDIGIT (*format))
115 const UCHAR_T *begin = format;
117 n = read_int (&format);
119 if (n > 0 && *format == L_('$'))
120 /* Is positional parameter. */
122 ++format; /* Skip the '$'. */
123 spec->data_arg = n - 1;
124 *max_ref_arg = MAX (*max_ref_arg, n);
126 else
127 /* Oops; that was actually the width and/or 0 padding flag.
128 Step back and read it again. */
129 format = begin;
132 /* Check for spec modifiers. */
135 switch (*format)
137 case L_(' '):
138 /* Output a space in place of a sign, when there is no sign. */
139 spec->info.space = 1;
140 continue;
141 case L_('+'):
142 /* Always output + or - for numbers. */
143 spec->info.showsign = 1;
144 continue;
145 case L_('-'):
146 /* Left-justify things. */
147 spec->info.left = 1;
148 continue;
149 case L_('#'):
150 /* Use the "alternate form":
151 Hex has 0x or 0X, FP always has a decimal point. */
152 spec->info.alt = 1;
153 continue;
154 case L_('0'):
155 /* Pad with 0s. */
156 spec->info.pad = '0';
157 continue;
158 case L_('\''):
159 /* Show grouping in numbers if the locale information
160 indicates any. */
161 spec->info.group = 1;
162 continue;
163 case L_('I'):
164 /* Use the internationalized form of the output. Currently
165 means to use the `outdigits' of the current locale. */
166 spec->info.i18n = 1;
167 continue;
168 default:
169 break;
171 break;
173 while (*++format);
175 if (spec->info.left)
176 spec->info.pad = ' ';
178 /* Get the field width. */
179 spec->width_arg = -1;
180 spec->info.width = 0;
181 if (*format == L_('*'))
183 /* The field width is given in an argument.
184 A negative field width indicates left justification. */
185 const UCHAR_T *begin = ++format;
187 if (ISDIGIT (*format))
189 /* The width argument might be found in a positional parameter. */
190 n = read_int (&format);
192 if (n > 0 && *format == L_('$'))
194 spec->width_arg = n - 1;
195 *max_ref_arg = MAX (*max_ref_arg, n);
196 ++format; /* Skip '$'. */
200 if (spec->width_arg < 0)
202 /* Not in a positional parameter. Consume one argument. */
203 spec->width_arg = posn++;
204 ++nargs;
205 format = begin; /* Step back and reread. */
208 else if (ISDIGIT (*format))
209 /* Constant width specification. */
210 spec->info.width = read_int (&format);
212 /* Get the precision. */
213 spec->prec_arg = -1;
214 /* -1 means none given; 0 means explicit 0. */
215 spec->info.prec = -1;
216 if (*format == L_('.'))
218 ++format;
219 if (*format == L_('*'))
221 /* The precision is given in an argument. */
222 const UCHAR_T *begin = ++format;
224 if (ISDIGIT (*format))
226 n = read_int (&format);
228 if (n > 0 && *format == L_('$'))
230 spec->prec_arg = n - 1;
231 *max_ref_arg = MAX (*max_ref_arg, n);
232 ++format;
236 if (spec->prec_arg < 0)
238 /* Not in a positional parameter. */
239 spec->prec_arg = posn++;
240 ++nargs;
241 format = begin;
244 else if (ISDIGIT (*format))
245 spec->info.prec = read_int (&format);
246 else
247 /* "%.?" is treated like "%.0?". */
248 spec->info.prec = 0;
251 /* Check for type modifiers. */
252 spec->info.is_long_double = 0;
253 spec->info.is_short = 0;
254 spec->info.is_long = 0;
255 spec->info.is_char = 0;
257 switch (*format++)
259 case L_('h'):
260 /* ints are short ints or chars. */
261 if (*format != L_('h'))
262 spec->info.is_short = 1;
263 else
265 ++format;
266 spec->info.is_char = 1;
268 break;
269 case L_('l'):
270 /* ints are long ints. */
271 spec->info.is_long = 1;
272 if (*format != L_('l'))
273 break;
274 ++format;
275 /* FALLTHROUGH */
276 case L_('L'):
277 /* doubles are long doubles, and ints are long long ints. */
278 case L_('q'):
279 /* 4.4 uses this for long long. */
280 spec->info.is_long_double = 1;
281 break;
282 case L_('z'):
283 case L_('Z'):
284 /* ints are size_ts. */
285 assert (sizeof (size_t) <= sizeof (unsigned long long int));
286 #if LONG_MAX != LONG_LONG_MAX
287 spec->info.is_long_double = sizeof (size_t) > sizeof (unsigned long int);
288 #endif
289 spec->info.is_long = sizeof (size_t) > sizeof (unsigned int);
290 break;
291 case L_('t'):
292 assert (sizeof (ptrdiff_t) <= sizeof (long long int));
293 #if LONG_MAX != LONG_LONG_MAX
294 spec->info.is_long_double = (sizeof (ptrdiff_t) > sizeof (long int));
295 #endif
296 spec->info.is_long = sizeof (ptrdiff_t) > sizeof (int);
297 break;
298 case L_('j'):
299 assert (sizeof (uintmax_t) <= sizeof (unsigned long long int));
300 #if LONG_MAX != LONG_LONG_MAX
301 spec->info.is_long_double = (sizeof (uintmax_t)
302 > sizeof (unsigned long int));
303 #endif
304 spec->info.is_long = sizeof (uintmax_t) > sizeof (unsigned int);
305 break;
306 default:
307 /* Not a recognized modifier. Backup. */
308 --format;
309 break;
312 /* Get the format specification. */
313 spec->info.spec = (wchar_t) *format++;
314 if (__builtin_expect (__printf_function_table != NULL, 0)
315 && spec->info.spec <= UCHAR_MAX
316 && __printf_arginfo_table[spec->info.spec] != NULL)
317 /* We don't try to get the types for all arguments if the format
318 uses more than one. The normal case is covered though. */
319 spec->ndata_args = (*__printf_arginfo_table[spec->info.spec])
320 (&spec->info, 1, &spec->data_arg_type);
321 else
323 /* Find the data argument types of a built-in spec. */
324 spec->ndata_args = 1;
326 switch (spec->info.spec)
328 case L'i':
329 case L'd':
330 case L'u':
331 case L'o':
332 case L'X':
333 case L'x':
334 #if LONG_MAX != LONG_LONG_MAX
335 if (spec->info.is_long_double)
336 spec->data_arg_type = PA_INT|PA_FLAG_LONG_LONG;
337 else
338 #endif
339 if (spec->info.is_long)
340 spec->data_arg_type = PA_INT|PA_FLAG_LONG;
341 else if (spec->info.is_short)
342 spec->data_arg_type = PA_INT|PA_FLAG_SHORT;
343 else if (spec->info.is_char)
344 spec->data_arg_type = PA_CHAR;
345 else
346 spec->data_arg_type = PA_INT;
347 break;
348 case L'e':
349 case L'E':
350 case L'f':
351 case L'F':
352 case L'g':
353 case L'G':
354 case L'a':
355 case L'A':
356 if (spec->info.is_long_double)
357 spec->data_arg_type = PA_DOUBLE|PA_FLAG_LONG_DOUBLE;
358 else
359 spec->data_arg_type = PA_DOUBLE;
360 break;
361 case L'c':
362 spec->data_arg_type = PA_CHAR;
363 break;
364 case L'C':
365 spec->data_arg_type = PA_WCHAR;
366 break;
367 case L's':
368 spec->data_arg_type = PA_STRING;
369 break;
370 case L'S':
371 spec->data_arg_type = PA_WSTRING;
372 break;
373 case L'p':
374 spec->data_arg_type = PA_POINTER;
375 break;
376 case L'n':
377 spec->data_arg_type = PA_INT|PA_FLAG_PTR;
378 break;
380 case L'm':
381 default:
382 /* An unknown spec will consume no args. */
383 spec->ndata_args = 0;
384 break;
388 if (spec->data_arg == -1 && spec->ndata_args > 0)
390 /* There are args consumed, but no positional spec. Use the
391 next sequential arg position. */
392 spec->data_arg = posn;
393 nargs += spec->ndata_args;
396 if (spec->info.spec == L'\0')
397 /* Format ended before this spec was complete. */
398 spec->end_of_fmt = spec->next_fmt = format - 1;
399 else
401 /* Find the next format spec. */
402 spec->end_of_fmt = format;
403 #ifdef COMPILE_WPRINTF
404 spec->next_fmt = __find_specwc (format);
405 #else
406 spec->next_fmt = __find_specmb (format, ps);
407 #endif
410 return nargs;