Updated to fedora-glibc-20050504T0728
[glibc.git] / stdio-common / printf-parsemb.c
blobc55b97060cea0df486d2344e83bbc3dba19c59bb
1 /* Helper functions for parsing printf format strings.
2 Copyright (C) 1995-2000, 2002, 2003, 2004 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) && (len = __mbrlen (format, MB_CUR_MAX, ps)) > 0)
68 format += len;
69 else
70 ++format;
72 return format;
73 #endif
77 /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
78 with the parsed details. POSN is the number of arguments already
79 consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
80 the number of args consumed by this spec; *MAX_REF_ARG is updated so it
81 remains the highest argument index used. */
82 size_t
83 attribute_hidden
84 #ifdef COMPILE_WPRINTF
85 __parse_one_specwc (const UCHAR_T *format, size_t posn,
86 struct printf_spec *spec, size_t *max_ref_arg)
87 #else
88 __parse_one_specmb (const UCHAR_T *format, size_t posn,
89 struct printf_spec *spec, size_t *max_ref_arg,
90 mbstate_t *ps)
91 #endif
93 unsigned int n;
94 size_t nargs = 0;
96 /* Skip the '%'. */
97 ++format;
99 /* Clear information structure. */
100 spec->data_arg = -1;
101 spec->info.alt = 0;
102 spec->info.space = 0;
103 spec->info.left = 0;
104 spec->info.showsign = 0;
105 spec->info.group = 0;
106 spec->info.i18n = 0;
107 spec->info.extra = 0;
108 spec->info.pad = ' ';
109 spec->info.wide = sizeof (UCHAR_T) > 1;
111 /* Test for positional argument. */
112 if (ISDIGIT (*format))
114 const UCHAR_T *begin = format;
116 n = read_int (&format);
118 if (n > 0 && *format == L_('$'))
119 /* Is positional parameter. */
121 ++format; /* Skip the '$'. */
122 spec->data_arg = n - 1;
123 *max_ref_arg = MAX (*max_ref_arg, n);
125 else
126 /* Oops; that was actually the width and/or 0 padding flag.
127 Step back and read it again. */
128 format = begin;
131 /* Check for spec modifiers. */
134 switch (*format)
136 case L_(' '):
137 /* Output a space in place of a sign, when there is no sign. */
138 spec->info.space = 1;
139 continue;
140 case L_('+'):
141 /* Always output + or - for numbers. */
142 spec->info.showsign = 1;
143 continue;
144 case L_('-'):
145 /* Left-justify things. */
146 spec->info.left = 1;
147 continue;
148 case L_('#'):
149 /* Use the "alternate form":
150 Hex has 0x or 0X, FP always has a decimal point. */
151 spec->info.alt = 1;
152 continue;
153 case L_('0'):
154 /* Pad with 0s. */
155 spec->info.pad = '0';
156 continue;
157 case L_('\''):
158 /* Show grouping in numbers if the locale information
159 indicates any. */
160 spec->info.group = 1;
161 continue;
162 case L_('I'):
163 /* Use the internationalized form of the output. Currently
164 means to use the `outdigits' of the current locale. */
165 spec->info.i18n = 1;
166 continue;
167 default:
168 break;
170 break;
172 while (*++format);
174 if (spec->info.left)
175 spec->info.pad = ' ';
177 /* Get the field width. */
178 spec->width_arg = -1;
179 spec->info.width = 0;
180 if (*format == L_('*'))
182 /* The field width is given in an argument.
183 A negative field width indicates left justification. */
184 const UCHAR_T *begin = ++format;
186 if (ISDIGIT (*format))
188 /* The width argument might be found in a positional parameter. */
189 n = read_int (&format);
191 if (n > 0 && *format == L_('$'))
193 spec->width_arg = n - 1;
194 *max_ref_arg = MAX (*max_ref_arg, n);
195 ++format; /* Skip '$'. */
199 if (spec->width_arg < 0)
201 /* Not in a positional parameter. Consume one argument. */
202 spec->width_arg = posn++;
203 ++nargs;
204 format = begin; /* Step back and reread. */
207 else if (ISDIGIT (*format))
208 /* Constant width specification. */
209 spec->info.width = read_int (&format);
211 /* Get the precision. */
212 spec->prec_arg = -1;
213 /* -1 means none given; 0 means explicit 0. */
214 spec->info.prec = -1;
215 if (*format == L_('.'))
217 ++format;
218 if (*format == L_('*'))
220 /* The precision is given in an argument. */
221 const UCHAR_T *begin = ++format;
223 if (ISDIGIT (*format))
225 n = read_int (&format);
227 if (n > 0 && *format == L_('$'))
229 spec->prec_arg = n - 1;
230 *max_ref_arg = MAX (*max_ref_arg, n);
231 ++format;
235 if (spec->prec_arg < 0)
237 /* Not in a positional parameter. */
238 spec->prec_arg = posn++;
239 ++nargs;
240 format = begin;
243 else if (ISDIGIT (*format))
244 spec->info.prec = read_int (&format);
245 else
246 /* "%.?" is treated like "%.0?". */
247 spec->info.prec = 0;
250 /* Check for type modifiers. */
251 spec->info.is_long_double = 0;
252 spec->info.is_short = 0;
253 spec->info.is_long = 0;
254 spec->info.is_char = 0;
256 switch (*format++)
258 case L_('h'):
259 /* ints are short ints or chars. */
260 if (*format != L_('h'))
261 spec->info.is_short = 1;
262 else
264 ++format;
265 spec->info.is_char = 1;
267 break;
268 case L_('l'):
269 /* ints are long ints. */
270 spec->info.is_long = 1;
271 if (*format != L_('l'))
272 break;
273 ++format;
274 /* FALLTHROUGH */
275 case L_('L'):
276 /* doubles are long doubles, and ints are long long ints. */
277 case L_('q'):
278 /* 4.4 uses this for long long. */
279 spec->info.is_long_double = 1;
280 break;
281 case L_('z'):
282 case L_('Z'):
283 /* ints are size_ts. */
284 assert (sizeof (size_t) <= sizeof (unsigned long long int));
285 #if LONG_MAX != LONG_LONG_MAX
286 spec->info.is_long_double = sizeof (size_t) > sizeof (unsigned long int);
287 #endif
288 spec->info.is_long = sizeof (size_t) > sizeof (unsigned int);
289 break;
290 case L_('t'):
291 assert (sizeof (ptrdiff_t) <= sizeof (long long int));
292 #if LONG_MAX != LONG_LONG_MAX
293 spec->info.is_long_double = (sizeof (ptrdiff_t) > sizeof (long int));
294 #endif
295 spec->info.is_long = sizeof (ptrdiff_t) > sizeof (int);
296 break;
297 case L_('j'):
298 assert (sizeof (uintmax_t) <= sizeof (unsigned long long int));
299 #if LONG_MAX != LONG_LONG_MAX
300 spec->info.is_long_double = (sizeof (uintmax_t)
301 > sizeof (unsigned long int));
302 #endif
303 spec->info.is_long = sizeof (uintmax_t) > sizeof (unsigned int);
304 break;
305 default:
306 /* Not a recognized modifier. Backup. */
307 --format;
308 break;
311 /* Get the format specification. */
312 spec->info.spec = (wchar_t) *format++;
313 if (__builtin_expect (__printf_function_table != NULL, 0)
314 && spec->info.spec <= UCHAR_MAX
315 && __printf_arginfo_table[spec->info.spec] != NULL)
316 /* We don't try to get the types for all arguments if the format
317 uses more than one. The normal case is covered though. */
318 spec->ndata_args = (*__printf_arginfo_table[spec->info.spec])
319 (&spec->info, 1, &spec->data_arg_type);
320 else
322 /* Find the data argument types of a built-in spec. */
323 spec->ndata_args = 1;
325 switch (spec->info.spec)
327 case L'i':
328 case L'd':
329 case L'u':
330 case L'o':
331 case L'X':
332 case L'x':
333 #if LONG_MAX != LONG_LONG_MAX
334 if (spec->info.is_long_double)
335 spec->data_arg_type = PA_INT|PA_FLAG_LONG_LONG;
336 else
337 #endif
338 if (spec->info.is_long)
339 spec->data_arg_type = PA_INT|PA_FLAG_LONG;
340 else if (spec->info.is_short)
341 spec->data_arg_type = PA_INT|PA_FLAG_SHORT;
342 else if (spec->info.is_char)
343 spec->data_arg_type = PA_CHAR;
344 else
345 spec->data_arg_type = PA_INT;
346 break;
347 case L'e':
348 case L'E':
349 case L'f':
350 case L'F':
351 case L'g':
352 case L'G':
353 case L'a':
354 case L'A':
355 if (spec->info.is_long_double)
356 spec->data_arg_type = PA_DOUBLE|PA_FLAG_LONG_DOUBLE;
357 else
358 spec->data_arg_type = PA_DOUBLE;
359 break;
360 case L'c':
361 spec->data_arg_type = PA_CHAR;
362 break;
363 case L'C':
364 spec->data_arg_type = PA_WCHAR;
365 break;
366 case L's':
367 spec->data_arg_type = PA_STRING;
368 break;
369 case L'S':
370 spec->data_arg_type = PA_WSTRING;
371 break;
372 case L'p':
373 spec->data_arg_type = PA_POINTER;
374 break;
375 case L'n':
376 spec->data_arg_type = PA_INT|PA_FLAG_PTR;
377 break;
379 case L'm':
380 default:
381 /* An unknown spec will consume no args. */
382 spec->ndata_args = 0;
383 break;
387 if (spec->data_arg == -1 && spec->ndata_args > 0)
389 /* There are args consumed, but no positional spec. Use the
390 next sequential arg position. */
391 spec->data_arg = posn;
392 nargs += spec->ndata_args;
395 if (spec->info.spec == L'\0')
396 /* Format ended before this spec was complete. */
397 spec->end_of_fmt = spec->next_fmt = format - 1;
398 else
400 /* Find the next format spec. */
401 spec->end_of_fmt = format;
402 #ifdef COMPILE_WPRINTF
403 spec->next_fmt = __find_specwc (format);
404 #else
405 spec->next_fmt = __find_specmb (format, ps);
406 #endif
409 return nargs;