Blame locale/programs/ld-numeric.c

Packit 6c4009
/* Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
Packit 6c4009
Packit 6c4009
   This program is free software; you can redistribute it and/or modify
Packit 6c4009
   it under the terms of the GNU General Public License as published
Packit 6c4009
   by the Free Software Foundation; version 2 of the License, or
Packit 6c4009
   (at your option) any later version.
Packit 6c4009
Packit 6c4009
   This program 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
Packit 6c4009
   GNU General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU General Public License
Packit 6c4009
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifdef HAVE_CONFIG_H
Packit 6c4009
# include <config.h>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include <langinfo.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <sys/uio.h>
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
Packit 6c4009
#include "localedef.h"
Packit 6c4009
#include "linereader.h"
Packit 6c4009
#include "localeinfo.h"
Packit 6c4009
#include "locfile.h"
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* The real definition of the struct for the LC_NUMERIC locale.  */
Packit 6c4009
struct locale_numeric_t
Packit 6c4009
{
Packit 6c4009
  const char *decimal_point;
Packit 6c4009
  const char *thousands_sep;
Packit 6c4009
  char *grouping;
Packit 6c4009
  size_t grouping_len;
Packit 6c4009
  uint32_t decimal_point_wc;
Packit 6c4009
  uint32_t thousands_sep_wc;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
numeric_startup (struct linereader *lr, struct localedef_t *locale,
Packit 6c4009
		 int ignore_content)
Packit 6c4009
{
Packit 6c4009
  if (!ignore_content)
Packit 6c4009
    {
Packit 6c4009
      locale->categories[LC_NUMERIC].numeric =
Packit 6c4009
	(struct locale_numeric_t *) xcalloc (1,
Packit 6c4009
					     sizeof (struct locale_numeric_t));
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (lr != NULL)
Packit 6c4009
    {
Packit 6c4009
      lr->translate_strings = 1;
Packit 6c4009
      lr->return_widestr = 0;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
numeric_finish (struct localedef_t *locale, const struct charmap_t *charmap)
Packit 6c4009
{
Packit 6c4009
  struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
Packit 6c4009
  int nothing = 0;
Packit 6c4009
Packit 6c4009
  /* Now resolve copying and also handle completely missing definitions.  */
Packit 6c4009
  if (numeric == NULL)
Packit 6c4009
    {
Packit 6c4009
      /* First see whether we were supposed to copy.  If yes, find the
Packit 6c4009
	 actual definition.  */
Packit 6c4009
      if (locale->copy_name[LC_NUMERIC] != NULL)
Packit 6c4009
	{
Packit 6c4009
	  /* Find the copying locale.  This has to happen transitively since
Packit 6c4009
	     the locale we are copying from might also copying another one.  */
Packit 6c4009
	  struct localedef_t *from = locale;
Packit 6c4009
Packit 6c4009
	  do
Packit 6c4009
	    from = find_locale (LC_NUMERIC, from->copy_name[LC_NUMERIC],
Packit 6c4009
				from->repertoire_name, charmap);
Packit 6c4009
	  while (from->categories[LC_NUMERIC].numeric == NULL
Packit 6c4009
		 && from->copy_name[LC_NUMERIC] != NULL);
Packit 6c4009
Packit 6c4009
	  numeric = locale->categories[LC_NUMERIC].numeric
Packit 6c4009
	    = from->categories[LC_NUMERIC].numeric;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* If there is still no definition issue an warning and create an
Packit 6c4009
	 empty one.  */
Packit 6c4009
      if (numeric == NULL)
Packit 6c4009
	{
Packit 6c4009
	  record_warning (_("\
Packit 6c4009
No definition for %s category found"), "LC_NUMERIC");
Packit 6c4009
	  numeric_startup (NULL, locale, 0);
Packit 6c4009
	  numeric = locale->categories[LC_NUMERIC].numeric;
Packit 6c4009
	  nothing = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* The decimal point must not be empty.  This is not said explicitly
Packit 6c4009
     in POSIX but ANSI C (ISO/IEC 9899) says in 4.4.2.1 it has to be
Packit 6c4009
     != "".  */
Packit 6c4009
  if (numeric->decimal_point == NULL)
Packit 6c4009
    {
Packit 6c4009
      if (! nothing)
Packit 6c4009
	record_error (0, 0, _("%s: field `%s' not defined"),
Packit 6c4009
		      "LC_NUMERIC", "decimal_point");
Packit 6c4009
      numeric->decimal_point = ".";
Packit 6c4009
    }
Packit 6c4009
  else if (numeric->decimal_point[0] == '\0' && ! nothing)
Packit 6c4009
    {
Packit 6c4009
      record_error (0, 0, _("\
Packit 6c4009
%s: value for field `%s' must not be an empty string"),
Packit 6c4009
		    "LC_NUMERIC", "decimal_point");
Packit 6c4009
    }
Packit 6c4009
  if (numeric->decimal_point_wc == L'\0')
Packit 6c4009
    numeric->decimal_point_wc = L'.';
Packit 6c4009
Packit 6c4009
  if (numeric->grouping_len == 0 && ! nothing)
Packit 6c4009
    record_error (0, 0, _("%s: field `%s' not defined"),
Packit 6c4009
		  "LC_NUMERIC", "grouping");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
numeric_output (struct localedef_t *locale, const struct charmap_t *charmap,
Packit 6c4009
		const char *output_path)
Packit 6c4009
{
Packit 6c4009
  struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
Packit 6c4009
  struct locale_file file;
Packit 6c4009
Packit 6c4009
  init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC));
Packit 6c4009
  add_locale_string (&file, numeric->decimal_point ?: "");
Packit 6c4009
  add_locale_string (&file, numeric->thousands_sep ?: "");
Packit 6c4009
  add_locale_raw_data (&file, numeric->grouping, numeric->grouping_len);
Packit 6c4009
  add_locale_uint32 (&file, numeric->decimal_point_wc);
Packit 6c4009
  add_locale_uint32 (&file, numeric->thousands_sep_wc);
Packit 6c4009
  add_locale_string (&file, charmap->code_set_name);
Packit 6c4009
  write_locale_data (output_path, LC_NUMERIC, "LC_NUMERIC", &file;;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* The parser for the LC_NUMERIC section of the locale definition.  */
Packit 6c4009
void
Packit 6c4009
numeric_read (struct linereader *ldfile, struct localedef_t *result,
Packit 6c4009
	      const struct charmap_t *charmap, const char *repertoire_name,
Packit 6c4009
	      int ignore_content)
Packit 6c4009
{
Packit 6c4009
  struct repertoire_t *repertoire = NULL;
Packit 6c4009
  struct locale_numeric_t *numeric;
Packit 6c4009
  struct token *now;
Packit 6c4009
  enum token_t nowtok;
Packit 6c4009
Packit 6c4009
  /* Get the repertoire we have to use.  */
Packit 6c4009
  if (repertoire_name != NULL)
Packit 6c4009
    repertoire = repertoire_read (repertoire_name);
Packit 6c4009
Packit 6c4009
  /* The rest of the line containing `LC_NUMERIC' must be free.  */
Packit 6c4009
  lr_ignore_rest (ldfile, 1);
Packit 6c4009
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      now = lr_token (ldfile, charmap, result, NULL, verbose);
Packit 6c4009
      nowtok = now->tok;
Packit 6c4009
    }
Packit 6c4009
  while (nowtok == tok_eol);
Packit 6c4009
Packit 6c4009
  /* If we see `copy' now we are almost done.  */
Packit 6c4009
  if (nowtok == tok_copy)
Packit 6c4009
    {
Packit 6c4009
      handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_numeric,
Packit 6c4009
		   LC_NUMERIC, "LC_NUMERIC", ignore_content);
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Prepare the data structures.  */
Packit 6c4009
  numeric_startup (ldfile, result, ignore_content);
Packit 6c4009
  numeric = result->categories[LC_NUMERIC].numeric;
Packit 6c4009
Packit 6c4009
  while (1)
Packit 6c4009
    {
Packit 6c4009
      /* Of course we don't proceed beyond the end of file.  */
Packit 6c4009
      if (nowtok == tok_eof)
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      /* Ingore empty lines.  */
Packit 6c4009
      if (nowtok == tok_eol)
Packit 6c4009
	{
Packit 6c4009
	  now = lr_token (ldfile, charmap, result, NULL, verbose);
Packit 6c4009
	  nowtok = now->tok;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      switch (nowtok)
Packit 6c4009
	{
Packit 6c4009
#define STR_ELEM(cat) \
Packit 6c4009
	case tok_##cat:							      \
Packit 6c4009
	  /* Ignore the rest of the line if we don't need the input of	      \
Packit 6c4009
	     this line.  */						      \
Packit 6c4009
	  if (ignore_content)						      \
Packit 6c4009
	    {								      \
Packit 6c4009
	      lr_ignore_rest (ldfile, 0);				      \
Packit 6c4009
	      break;							      \
Packit 6c4009
	    }								      \
Packit 6c4009
									      \
Packit 6c4009
	  ldfile->return_widestr = 1;					      \
Packit 6c4009
	  now = lr_token (ldfile, charmap, result, repertoire, verbose);	      \
Packit 6c4009
	  if (now->tok != tok_string)					      \
Packit 6c4009
	    goto err_label;						      \
Packit 6c4009
	  if (numeric->cat != NULL)					      \
Packit 6c4009
	    lr_error (ldfile, _("\
Packit 6c4009
%s: field `%s' declared more than once"), "LC_NUMERIC", #cat);		      \
Packit 6c4009
	  else if (!ignore_content && now->val.str.startmb == NULL)	      \
Packit 6c4009
	    {								      \
Packit 6c4009
	      lr_error (ldfile, _("\
Packit 6c4009
%s: unknown character in field `%s'"), "LC_NUMERIC", #cat);		      \
Packit 6c4009
	      numeric->cat = "";					      \
Packit 6c4009
	      numeric->cat##_wc = L'\0';				      \
Packit 6c4009
	    }								      \
Packit 6c4009
	  else if (now->val.str.startwc != NULL && now->val.str.lenwc > 2)    \
Packit 6c4009
	    {								      \
Packit 6c4009
	      lr_error (ldfile, _("\
Packit 6c4009
%s: value for field `%s' must be a single character"), "LC_NUMERIC", #cat);   \
Packit 6c4009
	    }								      \
Packit 6c4009
	  else if (!ignore_content)					      \
Packit 6c4009
	    {								      \
Packit 6c4009
	      numeric->cat = now->val.str.startmb;			      \
Packit 6c4009
									      \
Packit 6c4009
	      if (now->val.str.startwc != NULL)				      \
Packit 6c4009
		numeric->cat##_wc = *now->val.str.startwc;		      \
Packit 6c4009
	    }								      \
Packit 6c4009
	  ldfile->return_widestr = 0;					      \
Packit 6c4009
	  break
Packit 6c4009
Packit 6c4009
	  STR_ELEM (decimal_point);
Packit 6c4009
	  STR_ELEM (thousands_sep);
Packit 6c4009
Packit 6c4009
	case tok_grouping:
Packit 6c4009
	  /* Ignore the rest of the line if we don't need the input of
Packit 6c4009
	     this line.  */
Packit 6c4009
	  if (ignore_content)
Packit 6c4009
	    {
Packit 6c4009
	      lr_ignore_rest (ldfile, 0);
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  now = lr_token (ldfile, charmap, result, NULL, verbose);
Packit 6c4009
	  if (now->tok != tok_minus1 && now->tok != tok_number)
Packit 6c4009
	    goto err_label;
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      size_t act = 0;
Packit 6c4009
	      size_t max = 10;
Packit 6c4009
	      char *grouping = xmalloc (max);
Packit 6c4009
Packit 6c4009
	      do
Packit 6c4009
		{
Packit 6c4009
		  if (act + 1 >= max)
Packit 6c4009
		    {
Packit 6c4009
		      max *= 2;
Packit 6c4009
		      grouping = xrealloc (grouping, max);
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
		  if (act > 0 && grouping[act - 1] == '\177')
Packit 6c4009
		    {
Packit 6c4009
		      lr_error (ldfile, _("\
Packit 6c4009
%s: `-1' must be last entry in `%s' field"), "LC_NUMERIC", "grouping");
Packit 6c4009
		      lr_ignore_rest (ldfile, 0);
Packit 6c4009
		      break;
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
		  if (now->tok == tok_minus1)
Packit 6c4009
		    grouping[act++] = '\177';
Packit 6c4009
		  else if (now->val.num == 0)
Packit 6c4009
		    {
Packit 6c4009
		      /* A value of 0 disables grouping from here on but
Packit 6c4009
			 we must not store a NUL character since this
Packit 6c4009
			 terminates the string.  Use something different
Packit 6c4009
			 which must not be used otherwise.  */
Packit 6c4009
		      grouping[act++] = '\377';
Packit 6c4009
		    }
Packit 6c4009
		  else if (now->val.num > 126)
Packit 6c4009
		    lr_error (ldfile, _("\
Packit 6c4009
%s: values for field `%s' must be smaller than 127"),
Packit 6c4009
			      "LC_NUMERIC", "grouping");
Packit 6c4009
		  else
Packit 6c4009
		    grouping[act++] = now->val.num;
Packit 6c4009
Packit 6c4009
		  /* Next must be semicolon.  */
Packit 6c4009
		  now = lr_token (ldfile, charmap, result, NULL, verbose);
Packit 6c4009
		  if (now->tok != tok_semicolon)
Packit 6c4009
		    break;
Packit 6c4009
Packit 6c4009
		  now = lr_token (ldfile, charmap, result, NULL, verbose);
Packit 6c4009
		}
Packit 6c4009
	      while (now->tok == tok_minus1 || now->tok == tok_number);
Packit 6c4009
Packit 6c4009
	      if (now->tok != tok_eol)
Packit 6c4009
		goto err_label;
Packit 6c4009
Packit 6c4009
	      /* A single -1 means no grouping.  */
Packit 6c4009
	      if (act == 1 && grouping[0] == '\177')
Packit 6c4009
		act--;
Packit 6c4009
	      grouping[act++] = '\0';
Packit 6c4009
Packit 6c4009
	      numeric->grouping = xrealloc (grouping, act);
Packit 6c4009
	      numeric->grouping_len = act;
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case tok_end:
Packit 6c4009
	  /* Next we assume `LC_NUMERIC'.  */
Packit 6c4009
	  now = lr_token (ldfile, charmap, result, NULL, verbose);
Packit 6c4009
	  if (now->tok == tok_eof)
Packit 6c4009
	    break;
Packit 6c4009
	  if (now->tok == tok_eol)
Packit 6c4009
	    lr_error (ldfile, _("%s: incomplete `END' line"), "LC_NUMERIC");
Packit 6c4009
	  else if (now->tok != tok_lc_numeric)
Packit 6c4009
	    lr_error (ldfile, _("\
Packit 6c4009
%1$s: definition does not end with `END %1$s'"), "LC_NUMERIC");
Packit 6c4009
	  lr_ignore_rest (ldfile, now->tok == tok_lc_numeric);
Packit 6c4009
	  return;
Packit 6c4009
Packit 6c4009
	default:
Packit 6c4009
	err_label:
Packit 6c4009
	  SYNTAX_ERROR (_("%s: syntax error"), "LC_NUMERIC");
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Prepare for the next round.  */
Packit 6c4009
      now = lr_token (ldfile, charmap, result, NULL, verbose);
Packit 6c4009
      nowtok = now->tok;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* When we come here we reached the end of the file.  */
Packit 6c4009
  lr_error (ldfile, _("%s: premature end of file"), "LC_NUMERIC");
Packit 6c4009
}