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