Back to index

glibc  2.9
printf-parsemb.c
Go to the documentation of this file.
00001 /* Helper functions for parsing printf format strings.
00002    Copyright (C) 1995-2000,2002,2003,2004,2006 Free Software Foundation, Inc.
00003    This file is part of th GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <ctype.h>
00021 #include <limits.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <sys/param.h>
00025 #include <wchar.h>
00026 #include <wctype.h>
00027 
00028 #ifndef COMPILE_WPRINTF
00029 # define CHAR_T             char
00030 # define UCHAR_T     unsigned char
00031 # define INT_T              int
00032 # define L_(Str)     Str
00033 # define ISDIGIT(Ch) isdigit (Ch)
00034 #else
00035 # define CHAR_T             wchar_t
00036 # define UCHAR_T     unsigned int
00037 # define INT_T              wint_t
00038 # define L_(Str)     L##Str
00039 # define ISDIGIT(Ch) iswdigit (Ch)
00040 #endif
00041 
00042 #include "printf-parse.h"
00043 
00044 #define NDEBUG 1
00045 #include <assert.h>
00046 
00047 
00048 
00049 /* FORMAT must point to a '%' at the beginning of a spec.  Fills in *SPEC
00050    with the parsed details.  POSN is the number of arguments already
00051    consumed.  At most MAXTYPES - POSN types are filled in TYPES.  Return
00052    the number of args consumed by this spec; *MAX_REF_ARG is updated so it
00053    remains the highest argument index used.  */
00054 size_t
00055 attribute_hidden
00056 #ifdef COMPILE_WPRINTF
00057 __parse_one_specwc (const UCHAR_T *format, size_t posn,
00058                   struct printf_spec *spec, size_t *max_ref_arg)
00059 #else
00060 __parse_one_specmb (const UCHAR_T *format, size_t posn,
00061                   struct printf_spec *spec, size_t *max_ref_arg)
00062 #endif
00063 {
00064   unsigned int n;
00065   size_t nargs = 0;
00066 
00067   /* Skip the '%'.  */
00068   ++format;
00069 
00070   /* Clear information structure.  */
00071   spec->data_arg = -1;
00072   spec->info.alt = 0;
00073   spec->info.space = 0;
00074   spec->info.left = 0;
00075   spec->info.showsign = 0;
00076   spec->info.group = 0;
00077   spec->info.i18n = 0;
00078   spec->info.extra = 0;
00079   spec->info.pad = ' ';
00080   spec->info.wide = sizeof (UCHAR_T) > 1;
00081 
00082   /* Test for positional argument.  */
00083   if (ISDIGIT (*format))
00084     {
00085       const UCHAR_T *begin = format;
00086 
00087       n = read_int (&format);
00088 
00089       if (n > 0 && *format == L_('$'))
00090        /* Is positional parameter.  */
00091        {
00092          ++format;          /* Skip the '$'.  */
00093          spec->data_arg = n - 1;
00094          *max_ref_arg = MAX (*max_ref_arg, n);
00095        }
00096       else
00097        /* Oops; that was actually the width and/or 0 padding flag.
00098           Step back and read it again.  */
00099        format = begin;
00100     }
00101 
00102   /* Check for spec modifiers.  */
00103   do
00104     {
00105       switch (*format)
00106        {
00107        case L_(' '):
00108          /* Output a space in place of a sign, when there is no sign.  */
00109          spec->info.space = 1;
00110          continue;
00111        case L_('+'):
00112          /* Always output + or - for numbers.  */
00113          spec->info.showsign = 1;
00114          continue;
00115        case L_('-'):
00116          /* Left-justify things.  */
00117          spec->info.left = 1;
00118          continue;
00119        case L_('#'):
00120          /* Use the "alternate form":
00121             Hex has 0x or 0X, FP always has a decimal point.  */
00122          spec->info.alt = 1;
00123          continue;
00124        case L_('0'):
00125          /* Pad with 0s.  */
00126          spec->info.pad = '0';
00127          continue;
00128        case L_('\''):
00129          /* Show grouping in numbers if the locale information
00130             indicates any.  */
00131          spec->info.group = 1;
00132          continue;
00133        case L_('I'):
00134          /* Use the internationalized form of the output.  Currently
00135             means to use the `outdigits' of the current locale.  */
00136          spec->info.i18n = 1;
00137          continue;
00138        default:
00139          break;
00140        }
00141       break;
00142     }
00143   while (*++format);
00144 
00145   if (spec->info.left)
00146     spec->info.pad = ' ';
00147 
00148   /* Get the field width.  */
00149   spec->width_arg = -1;
00150   spec->info.width = 0;
00151   if (*format == L_('*'))
00152     {
00153       /* The field width is given in an argument.
00154         A negative field width indicates left justification.  */
00155       const UCHAR_T *begin = ++format;
00156 
00157       if (ISDIGIT (*format))
00158        {
00159          /* The width argument might be found in a positional parameter.  */
00160          n = read_int (&format);
00161 
00162          if (n > 0 && *format == L_('$'))
00163            {
00164              spec->width_arg = n - 1;
00165              *max_ref_arg = MAX (*max_ref_arg, n);
00166              ++format;             /* Skip '$'.  */
00167            }
00168        }
00169 
00170       if (spec->width_arg < 0)
00171        {
00172          /* Not in a positional parameter.  Consume one argument.  */
00173          spec->width_arg = posn++;
00174          ++nargs;
00175          format = begin;    /* Step back and reread.  */
00176        }
00177     }
00178   else if (ISDIGIT (*format))
00179     /* Constant width specification.  */
00180     spec->info.width = read_int (&format);
00181 
00182   /* Get the precision.  */
00183   spec->prec_arg = -1;
00184   /* -1 means none given; 0 means explicit 0.  */
00185   spec->info.prec = -1;
00186   if (*format == L_('.'))
00187     {
00188       ++format;
00189       if (*format == L_('*'))
00190        {
00191          /* The precision is given in an argument.  */
00192          const UCHAR_T *begin = ++format;
00193 
00194          if (ISDIGIT (*format))
00195            {
00196              n = read_int (&format);
00197 
00198              if (n > 0 && *format == L_('$'))
00199               {
00200                 spec->prec_arg = n - 1;
00201                 *max_ref_arg = MAX (*max_ref_arg, n);
00202                 ++format;
00203               }
00204            }
00205 
00206          if (spec->prec_arg < 0)
00207            {
00208              /* Not in a positional parameter.  */
00209              spec->prec_arg = posn++;
00210              ++nargs;
00211              format = begin;
00212            }
00213        }
00214       else if (ISDIGIT (*format))
00215        spec->info.prec = read_int (&format);
00216       else
00217        /* "%.?" is treated like "%.0?".  */
00218        spec->info.prec = 0;
00219     }
00220 
00221   /* Check for type modifiers.  */
00222   spec->info.is_long_double = 0;
00223   spec->info.is_short = 0;
00224   spec->info.is_long = 0;
00225   spec->info.is_char = 0;
00226 
00227   switch (*format++)
00228     {
00229     case L_('h'):
00230       /* ints are short ints or chars.  */
00231       if (*format != L_('h'))
00232        spec->info.is_short = 1;
00233       else
00234        {
00235          ++format;
00236          spec->info.is_char = 1;
00237        }
00238       break;
00239     case L_('l'):
00240       /* ints are long ints.  */
00241       spec->info.is_long = 1;
00242       if (*format != L_('l'))
00243        break;
00244       ++format;
00245       /* FALLTHROUGH */
00246     case L_('L'):
00247       /* doubles are long doubles, and ints are long long ints.  */
00248     case L_('q'):
00249       /* 4.4 uses this for long long.  */
00250       spec->info.is_long_double = 1;
00251       break;
00252     case L_('z'):
00253     case L_('Z'):
00254       /* ints are size_ts.  */
00255       assert (sizeof (size_t) <= sizeof (unsigned long long int));
00256 #if LONG_MAX != LONG_LONG_MAX
00257       spec->info.is_long_double = sizeof (size_t) > sizeof (unsigned long int);
00258 #endif
00259       spec->info.is_long = sizeof (size_t) > sizeof (unsigned int);
00260       break;
00261     case L_('t'):
00262       assert (sizeof (ptrdiff_t) <= sizeof (long long int));
00263 #if LONG_MAX != LONG_LONG_MAX
00264       spec->info.is_long_double = (sizeof (ptrdiff_t) > sizeof (long int));
00265 #endif
00266       spec->info.is_long = sizeof (ptrdiff_t) > sizeof (int);
00267       break;
00268     case L_('j'):
00269       assert (sizeof (uintmax_t) <= sizeof (unsigned long long int));
00270 #if LONG_MAX != LONG_LONG_MAX
00271       spec->info.is_long_double = (sizeof (uintmax_t)
00272                                > sizeof (unsigned long int));
00273 #endif
00274       spec->info.is_long = sizeof (uintmax_t) > sizeof (unsigned int);
00275       break;
00276     default:
00277       /* Not a recognized modifier.  Backup.  */
00278       --format;
00279       break;
00280     }
00281 
00282   /* Get the format specification.  */
00283   spec->info.spec = (wchar_t) *format++;
00284   if (__builtin_expect (__printf_function_table != NULL, 0)
00285       && spec->info.spec <= UCHAR_MAX
00286       && __printf_arginfo_table[spec->info.spec] != NULL)
00287     /* We don't try to get the types for all arguments if the format
00288        uses more than one.  The normal case is covered though.  */
00289     spec->ndata_args = (*__printf_arginfo_table[spec->info.spec])
00290       (&spec->info, 1, &spec->data_arg_type);
00291   else
00292     {
00293       /* Find the data argument types of a built-in spec.  */
00294       spec->ndata_args = 1;
00295 
00296       switch (spec->info.spec)
00297        {
00298        case L'i':
00299        case L'd':
00300        case L'u':
00301        case L'o':
00302        case L'X':
00303        case L'x':
00304 #if LONG_MAX != LONG_LONG_MAX
00305          if (spec->info.is_long_double)
00306            spec->data_arg_type = PA_INT|PA_FLAG_LONG_LONG;
00307          else
00308 #endif
00309            if (spec->info.is_long)
00310              spec->data_arg_type = PA_INT|PA_FLAG_LONG;
00311            else if (spec->info.is_short)
00312              spec->data_arg_type = PA_INT|PA_FLAG_SHORT;
00313            else if (spec->info.is_char)
00314              spec->data_arg_type = PA_CHAR;
00315            else
00316              spec->data_arg_type = PA_INT;
00317          break;
00318        case L'e':
00319        case L'E':
00320        case L'f':
00321        case L'F':
00322        case L'g':
00323        case L'G':
00324        case L'a':
00325        case L'A':
00326          if (spec->info.is_long_double)
00327            spec->data_arg_type = PA_DOUBLE|PA_FLAG_LONG_DOUBLE;
00328          else
00329            spec->data_arg_type = PA_DOUBLE;
00330          break;
00331        case L'c':
00332          spec->data_arg_type = PA_CHAR;
00333          break;
00334        case L'C':
00335          spec->data_arg_type = PA_WCHAR;
00336          break;
00337        case L's':
00338          spec->data_arg_type = PA_STRING;
00339          break;
00340        case L'S':
00341          spec->data_arg_type = PA_WSTRING;
00342          break;
00343        case L'p':
00344          spec->data_arg_type = PA_POINTER;
00345          break;
00346        case L'n':
00347          spec->data_arg_type = PA_INT|PA_FLAG_PTR;
00348          break;
00349 
00350        case L'm':
00351        default:
00352          /* An unknown spec will consume no args.  */
00353          spec->ndata_args = 0;
00354          break;
00355        }
00356     }
00357 
00358   if (spec->data_arg == -1 && spec->ndata_args > 0)
00359     {
00360       /* There are args consumed, but no positional spec.  Use the
00361         next sequential arg position.  */
00362       spec->data_arg = posn;
00363       nargs += spec->ndata_args;
00364     }
00365 
00366   if (spec->info.spec == L'\0')
00367     /* Format ended before this spec was complete.  */
00368     spec->end_of_fmt = spec->next_fmt = format - 1;
00369   else
00370     {
00371       /* Find the next format spec.  */
00372       spec->end_of_fmt = format;
00373 #ifdef COMPILE_WPRINTF
00374       spec->next_fmt = __find_specwc (format);
00375 #else
00376       spec->next_fmt = __find_specmb (format);
00377 #endif
00378     }
00379 
00380   return nargs;
00381 }