Blame stdio-common/printf-parsemb.c

Packit 6c4009
/* Helper functions for parsing printf format strings.
Packit 6c4009
   Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of th GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
#include <wctype.h>
Packit 6c4009
Packit 6c4009
#ifndef COMPILE_WPRINTF
Packit 6c4009
# define CHAR_T		char
Packit 6c4009
# define UCHAR_T	unsigned char
Packit 6c4009
# define INT_T		int
Packit 6c4009
# define L_(Str)	Str
Packit 6c4009
# define ISDIGIT(Ch)	isdigit (Ch)
Packit 6c4009
# define HANDLE_REGISTERED_MODIFIER __handle_registered_modifier_mb
Packit 6c4009
#else
Packit 6c4009
# define CHAR_T		wchar_t
Packit 6c4009
# define UCHAR_T	unsigned int
Packit 6c4009
# define INT_T		wint_t
Packit 6c4009
# define L_(Str)	L##Str
Packit 6c4009
# define ISDIGIT(Ch)	iswdigit (Ch)
Packit 6c4009
# define HANDLE_REGISTERED_MODIFIER __handle_registered_modifier_wc
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include "printf-parse.h"
Packit 6c4009
Packit 6c4009
#define NDEBUG 1
Packit 6c4009
#include <assert.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* FORMAT must point to a '%' at the beginning of a spec.  Fills in *SPEC
Packit 6c4009
   with the parsed details.  POSN is the number of arguments already
Packit 6c4009
   consumed.  At most MAXTYPES - POSN types are filled in TYPES.  Return
Packit 6c4009
   the number of args consumed by this spec; *MAX_REF_ARG is updated so it
Packit 6c4009
   remains the highest argument index used.  */
Packit 6c4009
size_t
Packit 6c4009
attribute_hidden
Packit 6c4009
#ifdef COMPILE_WPRINTF
Packit 6c4009
__parse_one_specwc (const UCHAR_T *format, size_t posn,
Packit 6c4009
		    struct printf_spec *spec, size_t *max_ref_arg)
Packit 6c4009
#else
Packit 6c4009
__parse_one_specmb (const UCHAR_T *format, size_t posn,
Packit 6c4009
		    struct printf_spec *spec, size_t *max_ref_arg)
Packit 6c4009
#endif
Packit 6c4009
{
Packit 6c4009
  unsigned int n;
Packit 6c4009
  size_t nargs = 0;
Packit 6c4009
Packit 6c4009
  /* Skip the '%'.  */
Packit 6c4009
  ++format;
Packit 6c4009
Packit 6c4009
  /* Clear information structure.  */
Packit 6c4009
  spec->data_arg = -1;
Packit 6c4009
  spec->info.alt = 0;
Packit 6c4009
  spec->info.space = 0;
Packit 6c4009
  spec->info.left = 0;
Packit 6c4009
  spec->info.showsign = 0;
Packit 6c4009
  spec->info.group = 0;
Packit 6c4009
  spec->info.i18n = 0;
Packit 6c4009
  spec->info.extra = 0;
Packit 6c4009
  spec->info.pad = ' ';
Packit 6c4009
  spec->info.wide = sizeof (UCHAR_T) > 1;
Packit 6c4009
  spec->info.is_binary128 = 0;
Packit 6c4009
Packit 6c4009
  /* Test for positional argument.  */
Packit 6c4009
  if (ISDIGIT (*format))
Packit 6c4009
    {
Packit 6c4009
      const UCHAR_T *begin = format;
Packit 6c4009
Packit 6c4009
      n = read_int (&format);
Packit 6c4009
Packit 6c4009
      if (n != 0 && *format == L_('$'))
Packit 6c4009
	/* Is positional parameter.  */
Packit 6c4009
	{
Packit 6c4009
	  ++format;		/* Skip the '$'.  */
Packit 6c4009
	  if (n != -1)
Packit 6c4009
	    {
Packit 6c4009
	      spec->data_arg = n - 1;
Packit 6c4009
	      *max_ref_arg = MAX (*max_ref_arg, n);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	/* Oops; that was actually the width and/or 0 padding flag.
Packit 6c4009
	   Step back and read it again.  */
Packit 6c4009
	format = begin;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Check for spec modifiers.  */
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      switch (*format)
Packit 6c4009
	{
Packit 6c4009
	case L_(' '):
Packit 6c4009
	  /* Output a space in place of a sign, when there is no sign.  */
Packit 6c4009
	  spec->info.space = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	case L_('+'):
Packit 6c4009
	  /* Always output + or - for numbers.  */
Packit 6c4009
	  spec->info.showsign = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	case L_('-'):
Packit 6c4009
	  /* Left-justify things.  */
Packit 6c4009
	  spec->info.left = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	case L_('#'):
Packit 6c4009
	  /* Use the "alternate form":
Packit 6c4009
	     Hex has 0x or 0X, FP always has a decimal point.  */
Packit 6c4009
	  spec->info.alt = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	case L_('0'):
Packit 6c4009
	  /* Pad with 0s.  */
Packit 6c4009
	  spec->info.pad = '0';
Packit 6c4009
	  continue;
Packit 6c4009
	case L_('\''):
Packit 6c4009
	  /* Show grouping in numbers if the locale information
Packit 6c4009
	     indicates any.  */
Packit 6c4009
	  spec->info.group = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	case L_('I'):
Packit 6c4009
	  /* Use the internationalized form of the output.  Currently
Packit 6c4009
	     means to use the `outdigits' of the current locale.  */
Packit 6c4009
	  spec->info.i18n = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	default:
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
      break;
Packit 6c4009
    }
Packit 6c4009
  while (*++format);
Packit 6c4009
Packit 6c4009
  if (spec->info.left)
Packit 6c4009
    spec->info.pad = ' ';
Packit 6c4009
Packit 6c4009
  /* Get the field width.  */
Packit 6c4009
  spec->width_arg = -1;
Packit 6c4009
  spec->info.width = 0;
Packit 6c4009
  if (*format == L_('*'))
Packit 6c4009
    {
Packit 6c4009
      /* The field width is given in an argument.
Packit 6c4009
	 A negative field width indicates left justification.  */
Packit 6c4009
      const UCHAR_T *begin = ++format;
Packit 6c4009
Packit 6c4009
      if (ISDIGIT (*format))
Packit 6c4009
	{
Packit 6c4009
	  /* The width argument might be found in a positional parameter.  */
Packit 6c4009
	  n = read_int (&format);
Packit 6c4009
Packit 6c4009
	  if (n != 0 && *format == L_('$'))
Packit 6c4009
	    {
Packit 6c4009
	      if (n != -1)
Packit 6c4009
		{
Packit 6c4009
		  spec->width_arg = n - 1;
Packit 6c4009
		  *max_ref_arg = MAX (*max_ref_arg, n);
Packit 6c4009
		}
Packit 6c4009
	      ++format;		/* Skip '$'.  */
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (spec->width_arg < 0)
Packit 6c4009
	{
Packit 6c4009
	  /* Not in a positional parameter.  Consume one argument.  */
Packit 6c4009
	  spec->width_arg = posn++;
Packit 6c4009
	  ++nargs;
Packit 6c4009
	  format = begin;	/* Step back and reread.  */
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else if (ISDIGIT (*format))
Packit 6c4009
    {
Packit 6c4009
      int n = read_int (&format);
Packit 6c4009
Packit 6c4009
      /* Constant width specification.  */
Packit 6c4009
      if (n != -1)
Packit 6c4009
	spec->info.width = n;
Packit 6c4009
    }
Packit 6c4009
  /* Get the precision.  */
Packit 6c4009
  spec->prec_arg = -1;
Packit 6c4009
  /* -1 means none given; 0 means explicit 0.  */
Packit 6c4009
  spec->info.prec = -1;
Packit 6c4009
  if (*format == L_('.'))
Packit 6c4009
    {
Packit 6c4009
      ++format;
Packit 6c4009
      if (*format == L_('*'))
Packit 6c4009
	{
Packit 6c4009
	  /* The precision is given in an argument.  */
Packit 6c4009
	  const UCHAR_T *begin = ++format;
Packit 6c4009
Packit 6c4009
	  if (ISDIGIT (*format))
Packit 6c4009
	    {
Packit 6c4009
	      n = read_int (&format);
Packit 6c4009
Packit 6c4009
	      if (n != 0 && *format == L_('$'))
Packit 6c4009
		{
Packit 6c4009
		  if (n != -1)
Packit 6c4009
		    {
Packit 6c4009
		      spec->prec_arg = n - 1;
Packit 6c4009
		      *max_ref_arg = MAX (*max_ref_arg, n);
Packit 6c4009
		    }
Packit 6c4009
		  ++format;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if (spec->prec_arg < 0)
Packit 6c4009
	    {
Packit 6c4009
	      /* Not in a positional parameter.  */
Packit 6c4009
	      spec->prec_arg = posn++;
Packit 6c4009
	      ++nargs;
Packit 6c4009
	      format = begin;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else if (ISDIGIT (*format))
Packit 6c4009
	{
Packit 6c4009
	  int n = read_int (&format);
Packit 6c4009
Packit 6c4009
	  if (n != -1)
Packit 6c4009
	    spec->info.prec = n;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	/* "%.?" is treated like "%.0?".  */
Packit 6c4009
	spec->info.prec = 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Check for type modifiers.  */
Packit 6c4009
  spec->info.is_long_double = 0;
Packit 6c4009
  spec->info.is_short = 0;
Packit 6c4009
  spec->info.is_long = 0;
Packit 6c4009
  spec->info.is_char = 0;
Packit 6c4009
  spec->info.user = 0;
Packit 6c4009
Packit 6c4009
  if (__builtin_expect (__printf_modifier_table == NULL, 1)
Packit 6c4009
      || __printf_modifier_table[*format] == NULL
Packit 6c4009
      || HANDLE_REGISTERED_MODIFIER (&format, &spec->info) != 0)
Packit 6c4009
    switch (*format++)
Packit 6c4009
      {
Packit 6c4009
      case L_('h'):
Packit 6c4009
	/* ints are short ints or chars.  */
Packit 6c4009
	if (*format != L_('h'))
Packit 6c4009
	  spec->info.is_short = 1;
Packit 6c4009
	else
Packit 6c4009
	  {
Packit 6c4009
	    ++format;
Packit 6c4009
	    spec->info.is_char = 1;
Packit 6c4009
	  }
Packit 6c4009
	break;
Packit 6c4009
      case L_('l'):
Packit 6c4009
	/* ints are long ints.  */
Packit 6c4009
	spec->info.is_long = 1;
Packit 6c4009
	if (*format != L_('l'))
Packit 6c4009
	  break;
Packit 6c4009
	++format;
Packit 6c4009
	/* FALLTHROUGH */
Packit 6c4009
      case L_('L'):
Packit 6c4009
	/* doubles are long doubles, and ints are long long ints.  */
Packit 6c4009
      case L_('q'):
Packit 6c4009
	/* 4.4 uses this for long long.  */
Packit 6c4009
	spec->info.is_long_double = 1;
Packit 6c4009
	break;
Packit 6c4009
      case L_('z'):
Packit 6c4009
      case L_('Z'):
Packit 6c4009
	/* ints are size_ts.  */
Packit 6c4009
	assert (sizeof (size_t) <= sizeof (unsigned long long int));
Packit 6c4009
#if LONG_MAX != LONG_LONG_MAX
Packit 6c4009
	spec->info.is_long_double = (sizeof (size_t)
Packit 6c4009
				     > sizeof (unsigned long int));
Packit 6c4009
#endif
Packit 6c4009
	spec->info.is_long = sizeof (size_t) > sizeof (unsigned int);
Packit 6c4009
	break;
Packit 6c4009
      case L_('t'):
Packit 6c4009
	assert (sizeof (ptrdiff_t) <= sizeof (long long int));
Packit 6c4009
#if LONG_MAX != LONG_LONG_MAX
Packit 6c4009
	spec->info.is_long_double = (sizeof (ptrdiff_t) > sizeof (long int));
Packit 6c4009
#endif
Packit 6c4009
	spec->info.is_long = sizeof (ptrdiff_t) > sizeof (int);
Packit 6c4009
	break;
Packit 6c4009
      case L_('j'):
Packit 6c4009
	assert (sizeof (uintmax_t) <= sizeof (unsigned long long int));
Packit 6c4009
#if LONG_MAX != LONG_LONG_MAX
Packit 6c4009
	spec->info.is_long_double = (sizeof (uintmax_t)
Packit 6c4009
				     > sizeof (unsigned long int));
Packit 6c4009
#endif
Packit 6c4009
	spec->info.is_long = sizeof (uintmax_t) > sizeof (unsigned int);
Packit 6c4009
	break;
Packit 6c4009
      default:
Packit 6c4009
	/* Not a recognized modifier.  Backup.  */
Packit 6c4009
	--format;
Packit 6c4009
	break;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  /* Get the format specification.  */
Packit 6c4009
  spec->info.spec = (wchar_t) *format++;
Packit 6c4009
  spec->size = -1;
Packit 6c4009
  if (__builtin_expect (__printf_function_table == NULL, 1)
Packit 6c4009
      || spec->info.spec > UCHAR_MAX
Packit 6c4009
      || __printf_arginfo_table[spec->info.spec] == NULL
Packit 6c4009
      /* We don't try to get the types for all arguments if the format
Packit 6c4009
	 uses more than one.  The normal case is covered though.  If
Packit 6c4009
	 the call returns -1 we continue with the normal specifiers.  */
Packit 6c4009
      || (int) (spec->ndata_args = (*__printf_arginfo_table[spec->info.spec])
Packit 6c4009
				   (&spec->info, 1, &spec->data_arg_type,
Packit 6c4009
				    &spec->size)) < 0)
Packit 6c4009
    {
Packit 6c4009
      /* Find the data argument types of a built-in spec.  */
Packit 6c4009
      spec->ndata_args = 1;
Packit 6c4009
Packit 6c4009
      switch (spec->info.spec)
Packit 6c4009
	{
Packit 6c4009
	case L'i':
Packit 6c4009
	case L'd':
Packit 6c4009
	case L'u':
Packit 6c4009
	case L'o':
Packit 6c4009
	case L'X':
Packit 6c4009
	case L'x':
Packit 6c4009
#if LONG_MAX != LONG_LONG_MAX
Packit 6c4009
	  if (spec->info.is_long_double)
Packit 6c4009
	    spec->data_arg_type = PA_INT|PA_FLAG_LONG_LONG;
Packit 6c4009
	  else
Packit 6c4009
#endif
Packit 6c4009
	    if (spec->info.is_long)
Packit 6c4009
	      spec->data_arg_type = PA_INT|PA_FLAG_LONG;
Packit 6c4009
	    else if (spec->info.is_short)
Packit 6c4009
	      spec->data_arg_type = PA_INT|PA_FLAG_SHORT;
Packit 6c4009
	    else if (spec->info.is_char)
Packit 6c4009
	      spec->data_arg_type = PA_CHAR;
Packit 6c4009
	    else
Packit 6c4009
	      spec->data_arg_type = PA_INT;
Packit 6c4009
	  break;
Packit 6c4009
	case L'e':
Packit 6c4009
	case L'E':
Packit 6c4009
	case L'f':
Packit 6c4009
	case L'F':
Packit 6c4009
	case L'g':
Packit 6c4009
	case L'G':
Packit 6c4009
	case L'a':
Packit 6c4009
	case L'A':
Packit 6c4009
	  if (spec->info.is_long_double)
Packit 6c4009
	    spec->data_arg_type = PA_DOUBLE|PA_FLAG_LONG_DOUBLE;
Packit 6c4009
	  else
Packit 6c4009
	    spec->data_arg_type = PA_DOUBLE;
Packit 6c4009
	  break;
Packit 6c4009
	case L'c':
Packit 6c4009
	  spec->data_arg_type = PA_CHAR;
Packit 6c4009
	  break;
Packit 6c4009
	case L'C':
Packit 6c4009
	  spec->data_arg_type = PA_WCHAR;
Packit 6c4009
	  break;
Packit 6c4009
	case L's':
Packit 6c4009
	  spec->data_arg_type = PA_STRING;
Packit 6c4009
	  break;
Packit 6c4009
	case L'S':
Packit 6c4009
	  spec->data_arg_type = PA_WSTRING;
Packit 6c4009
	  break;
Packit 6c4009
	case L'p':
Packit 6c4009
	  spec->data_arg_type = PA_POINTER;
Packit 6c4009
	  break;
Packit 6c4009
	case L'n':
Packit 6c4009
	  spec->data_arg_type = PA_INT|PA_FLAG_PTR;
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case L'm':
Packit 6c4009
	default:
Packit 6c4009
	  /* An unknown spec will consume no args.  */
Packit 6c4009
	  spec->ndata_args = 0;
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (spec->data_arg == -1 && spec->ndata_args > 0)
Packit 6c4009
    {
Packit 6c4009
      /* There are args consumed, but no positional spec.  Use the
Packit 6c4009
	 next sequential arg position.  */
Packit 6c4009
      spec->data_arg = posn;
Packit 6c4009
      nargs += spec->ndata_args;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (spec->info.spec == L'\0')
Packit 6c4009
    /* Format ended before this spec was complete.  */
Packit 6c4009
    spec->end_of_fmt = spec->next_fmt = format - 1;
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Find the next format spec.  */
Packit 6c4009
      spec->end_of_fmt = format;
Packit 6c4009
#ifdef COMPILE_WPRINTF
Packit 6c4009
      spec->next_fmt = __find_specwc (format);
Packit 6c4009
#else
Packit 6c4009
      spec->next_fmt = __find_specmb (format);
Packit 6c4009
#endif
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return nargs;
Packit 6c4009
}