Blame locale/programs/ld-identification.c

Packit 6c4009
/* Copyright (C) 1998-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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 <stdlib.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 "localeinfo.h"
Packit 6c4009
#include "locfile.h"
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* The real definition of the struct for the LC_IDENTIFICATION locale.  */
Packit 6c4009
struct locale_identification_t
Packit 6c4009
{
Packit 6c4009
  const char *title;
Packit 6c4009
  const char *source;
Packit 6c4009
  const char *address;
Packit 6c4009
  const char *contact;
Packit 6c4009
  const char *email;
Packit 6c4009
  const char *tel;
Packit 6c4009
  const char *fax;
Packit 6c4009
  const char *language;
Packit 6c4009
  const char *territory;
Packit 6c4009
  const char *audience;
Packit 6c4009
  const char *application;
Packit 6c4009
  const char *abbreviation;
Packit 6c4009
  const char *revision;
Packit 6c4009
  const char *date;
Packit 6c4009
  const char *category[__LC_LAST];
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
static const char *category_name[__LC_LAST] =
Packit 6c4009
{
Packit 6c4009
  [LC_CTYPE] = "LC_CTYPE",
Packit 6c4009
  [LC_NUMERIC] = "LC_NUMERIC",
Packit 6c4009
  [LC_TIME] = "LC_TIME",
Packit 6c4009
  [LC_COLLATE] = "LC_COLLATE",
Packit 6c4009
  [LC_MONETARY] = "LC_MONETARY",
Packit 6c4009
  [LC_MESSAGES] = "LC_MESSAGES",
Packit 6c4009
  [LC_ALL] = "LC_ALL",
Packit 6c4009
  [LC_PAPER] = "LC_PAPER",
Packit 6c4009
  [LC_NAME] = "LC_NAME",
Packit 6c4009
  [LC_ADDRESS] = "LC_ADDRESS",
Packit 6c4009
  [LC_TELEPHONE] = "LC_TELEPHONE",
Packit 6c4009
  [LC_MEASUREMENT] = "LC_MEASUREMENT",
Packit 6c4009
  [LC_IDENTIFICATION] = "LC_IDENTIFICATION"
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
identification_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_IDENTIFICATION].identification =
Packit 6c4009
	(struct locale_identification_t *)
Packit 6c4009
	xcalloc (1, sizeof (struct locale_identification_t));
Packit 6c4009
Packit 6c4009
      locale->categories[LC_IDENTIFICATION].identification->category[LC_ALL] =
Packit 6c4009
	"";
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
identification_finish (struct localedef_t *locale,
Packit 6c4009
		       const struct charmap_t *charmap)
Packit 6c4009
{
Packit 6c4009
  struct locale_identification_t *identification
Packit 6c4009
    = locale->categories[LC_IDENTIFICATION].identification;
Packit 6c4009
  int nothing = 0;
Packit 6c4009
  size_t num;
Packit 6c4009
Packit 6c4009
  /* Now resolve copying and also handle completely missing definitions.  */
Packit 6c4009
  if (identification == 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_IDENTIFICATION] != 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_IDENTIFICATION,
Packit 6c4009
				from->copy_name[LC_IDENTIFICATION],
Packit 6c4009
				from->repertoire_name, charmap);
Packit 6c4009
	  while (from->categories[LC_IDENTIFICATION].identification == NULL
Packit 6c4009
		 && from->copy_name[LC_IDENTIFICATION] != NULL);
Packit 6c4009
Packit 6c4009
	  identification = locale->categories[LC_IDENTIFICATION].identification
Packit 6c4009
	    = from->categories[LC_IDENTIFICATION].identification;
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 (identification == NULL)
Packit 6c4009
	{
Packit 6c4009
	  record_warning (_("\
Packit 6c4009
No definition for %s category found"), "LC_IDENTIFICATION");
Packit 6c4009
	  identification_startup (NULL, locale, 0);
Packit 6c4009
	  identification
Packit 6c4009
	    = locale->categories[LC_IDENTIFICATION].identification;
Packit 6c4009
	  nothing = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#define TEST_ELEM(cat) \
Packit 6c4009
  if (identification->cat == NULL)					      \
Packit 6c4009
    {									      \
Packit 6c4009
      if (verbose && ! nothing)						      \
Packit 6c4009
	record_warning (_("%s: field `%s' not defined"), "LC_IDENTIFICATION", \
Packit 6c4009
			#cat);						      \
Packit 6c4009
      identification->cat = "";						      \
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  TEST_ELEM (title);
Packit 6c4009
  TEST_ELEM (source);
Packit 6c4009
  TEST_ELEM (address);
Packit 6c4009
  TEST_ELEM (contact);
Packit 6c4009
  TEST_ELEM (email);
Packit 6c4009
  TEST_ELEM (tel);
Packit 6c4009
  TEST_ELEM (fax);
Packit 6c4009
  TEST_ELEM (language);
Packit 6c4009
  TEST_ELEM (territory);
Packit 6c4009
  TEST_ELEM (audience);
Packit 6c4009
  TEST_ELEM (application);
Packit 6c4009
  TEST_ELEM (abbreviation);
Packit 6c4009
  TEST_ELEM (revision);
Packit 6c4009
  TEST_ELEM (date);
Packit 6c4009
Packit 6c4009
  for (num = 0; num < __LC_LAST; ++num)
Packit 6c4009
    {
Packit 6c4009
      /* We don't accept/parse this category, so skip it early.  */
Packit 6c4009
      if (num == LC_ALL)
Packit 6c4009
	continue;
Packit 6c4009
Packit 6c4009
      if (identification->category[num] == NULL)
Packit 6c4009
	{
Packit 6c4009
	  if (verbose && ! nothing)
Packit 6c4009
	    record_warning (_("\
Packit 6c4009
%s: no identification for category `%s'"), "LC_IDENTIFICATION",
Packit 6c4009
			    category_name[num]);
Packit 6c4009
	  identification->category[num] = "";
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Only list the standards we care about.  This is based on the
Packit 6c4009
	     ISO 30112 WD10 [2014] standard which supersedes all previous
Packit 6c4009
	     revisions of the ISO 14652 standard.  */
Packit 6c4009
	  static const char * const standards[] =
Packit 6c4009
	    {
Packit 6c4009
	      "posix:1993",
Packit 6c4009
	      "i18n:2004",
Packit 6c4009
	      "i18n:2012",
Packit 6c4009
	    };
Packit 6c4009
	  size_t i;
Packit 6c4009
	  bool matched = false;
Packit 6c4009
Packit 6c4009
	  for (i = 0; i < sizeof (standards) / sizeof (standards[0]); ++i)
Packit 6c4009
	    if (strcmp (identification->category[num], standards[i]) == 0)
Packit 6c4009
	      matched = true;
Packit 6c4009
Packit 6c4009
	  if (matched != true)
Packit 6c4009
	    record_error (0, 0, _("\
Packit 6c4009
%s: unknown standard `%s' for category `%s'"),
Packit 6c4009
			  "LC_IDENTIFICATION",
Packit 6c4009
			  identification->category[num],
Packit 6c4009
			  category_name[num]);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
identification_output (struct localedef_t *locale,
Packit 6c4009
		       const struct charmap_t *charmap,
Packit 6c4009
		       const char *output_path)
Packit 6c4009
{
Packit 6c4009
  struct locale_identification_t *identification
Packit 6c4009
    = locale->categories[LC_IDENTIFICATION].identification;
Packit 6c4009
  struct locale_file file;
Packit 6c4009
  size_t num;
Packit 6c4009
Packit 6c4009
  init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION));
Packit 6c4009
  add_locale_string (&file, identification->title);
Packit 6c4009
  add_locale_string (&file, identification->source);
Packit 6c4009
  add_locale_string (&file, identification->address);
Packit 6c4009
  add_locale_string (&file, identification->contact);
Packit 6c4009
  add_locale_string (&file, identification->email);
Packit 6c4009
  add_locale_string (&file, identification->tel);
Packit 6c4009
  add_locale_string (&file, identification->fax);
Packit 6c4009
  add_locale_string (&file, identification->language);
Packit 6c4009
  add_locale_string (&file, identification->territory);
Packit 6c4009
  add_locale_string (&file, identification->audience);
Packit 6c4009
  add_locale_string (&file, identification->application);
Packit 6c4009
  add_locale_string (&file, identification->abbreviation);
Packit 6c4009
  add_locale_string (&file, identification->revision);
Packit 6c4009
  add_locale_string (&file, identification->date);
Packit 6c4009
  start_locale_structure (&file;;
Packit 6c4009
  for (num = 0; num < __LC_LAST; ++num)
Packit 6c4009
    if (num != LC_ALL)
Packit 6c4009
      add_locale_string (&file, identification->category[num]);
Packit 6c4009
  end_locale_structure (&file;;
Packit 6c4009
  add_locale_string (&file, charmap->code_set_name);
Packit 6c4009
  write_locale_data (output_path, LC_IDENTIFICATION, "LC_IDENTIFICATION",
Packit 6c4009
		     &file;;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* The parser for the LC_IDENTIFICATION section of the locale definition.  */
Packit 6c4009
void
Packit 6c4009
identification_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 locale_identification_t *identification;
Packit 6c4009
  struct token *now;
Packit 6c4009
  struct token *arg;
Packit 6c4009
  struct token *cattok;
Packit 6c4009
  int category;
Packit 6c4009
  enum token_t nowtok;
Packit 6c4009
Packit 6c4009
  /* The rest of the line containing `LC_IDENTIFICATION' must be free.  */
Packit 6c4009
  lr_ignore_rest (ldfile, 1);
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,
Packit 6c4009
		   tok_lc_identification, LC_IDENTIFICATION,
Packit 6c4009
		   "LC_IDENTIFICATION", ignore_content);
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Prepare the data structures.  */
Packit 6c4009
  identification_startup (ldfile, result, ignore_content);
Packit 6c4009
  identification = result->categories[LC_IDENTIFICATION].identification;
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
      /* Ignore 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
	  arg = lr_token (ldfile, charmap, result, NULL, verbose);	      \
Packit 6c4009
	  if (arg->tok != tok_string)					      \
Packit 6c4009
	    goto err_label;						      \
Packit 6c4009
	  if (identification->cat != NULL)				      \
Packit 6c4009
	    lr_error (ldfile, _("\
Packit 6c4009
%s: field `%s' declared more than once"), "LC_IDENTIFICATION", #cat);	      \
Packit 6c4009
	  else if (!ignore_content && arg->val.str.startmb == NULL)	      \
Packit 6c4009
	    {								      \
Packit 6c4009
	      lr_error (ldfile, _("\
Packit 6c4009
%s: unknown character in field `%s'"), "LC_IDENTIFICATION", #cat);	      \
Packit 6c4009
	      identification->cat = "";					      \
Packit 6c4009
	    }								      \
Packit 6c4009
	  else if (!ignore_content)					      \
Packit 6c4009
	    identification->cat = arg->val.str.startmb;			      \
Packit 6c4009
	  break
Packit 6c4009
Packit 6c4009
	  STR_ELEM (title);
Packit 6c4009
	  STR_ELEM (source);
Packit 6c4009
	  STR_ELEM (address);
Packit 6c4009
	  STR_ELEM (contact);
Packit 6c4009
	  STR_ELEM (email);
Packit 6c4009
	  STR_ELEM (tel);
Packit 6c4009
	  STR_ELEM (fax);
Packit 6c4009
	  STR_ELEM (language);
Packit 6c4009
	  STR_ELEM (territory);
Packit 6c4009
	  STR_ELEM (audience);
Packit 6c4009
	  STR_ELEM (application);
Packit 6c4009
	  STR_ELEM (abbreviation);
Packit 6c4009
	  STR_ELEM (revision);
Packit 6c4009
	  STR_ELEM (date);
Packit 6c4009
Packit 6c4009
	case tok_category:
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
	  /* We expect two operands.  */
Packit 6c4009
	  arg = lr_token (ldfile, charmap, result, NULL, verbose);
Packit 6c4009
	  if (arg->tok != tok_string && arg->tok != tok_ident)
Packit 6c4009
	    goto err_label;
Packit 6c4009
	  /* Next is a semicolon.  */
Packit 6c4009
	  cattok = lr_token (ldfile, charmap, result, NULL, verbose);
Packit 6c4009
	  if (cattok->tok != tok_semicolon)
Packit 6c4009
	    goto err_label;
Packit 6c4009
	  /* Now a LC_xxx identifier.  */
Packit 6c4009
	  cattok = lr_token (ldfile, charmap, result, NULL, verbose);
Packit 6c4009
	  switch (cattok->tok)
Packit 6c4009
	    {
Packit 6c4009
#define CATEGORY(lname, uname) \
Packit 6c4009
	    case tok_lc_##lname:					      \
Packit 6c4009
	      category = LC_##uname;					      \
Packit 6c4009
	      break
Packit 6c4009
Packit 6c4009
	      CATEGORY (identification, IDENTIFICATION);
Packit 6c4009
	      CATEGORY (ctype, CTYPE);
Packit 6c4009
	      CATEGORY (collate, COLLATE);
Packit 6c4009
	      CATEGORY (time, TIME);
Packit 6c4009
	      CATEGORY (numeric, NUMERIC);
Packit 6c4009
	      CATEGORY (monetary, MONETARY);
Packit 6c4009
	      CATEGORY (messages, MESSAGES);
Packit 6c4009
	      CATEGORY (paper, PAPER);
Packit 6c4009
	      CATEGORY (name, NAME);
Packit 6c4009
	      CATEGORY (address, ADDRESS);
Packit 6c4009
	      CATEGORY (telephone, TELEPHONE);
Packit 6c4009
	      CATEGORY (measurement, MEASUREMENT);
Packit 6c4009
Packit 6c4009
	    default:
Packit 6c4009
	      goto err_label;
Packit 6c4009
	    }
Packit 6c4009
	  if (identification->category[category] != NULL)
Packit 6c4009
	    {
Packit 6c4009
	      lr_error (ldfile, _("\
Packit 6c4009
%s: duplicate category version definition"), "LC_IDENTIFICATION");
Packit 6c4009
	      free (arg->val.str.startmb);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    identification->category[category] = arg->val.str.startmb;
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case tok_end:
Packit 6c4009
	  /* Next we assume `LC_IDENTIFICATION'.  */
Packit 6c4009
	  arg = lr_token (ldfile, charmap, result, NULL, verbose);
Packit 6c4009
	  if (arg->tok == tok_eof)
Packit 6c4009
	    break;
Packit 6c4009
	  if (arg->tok == tok_eol)
Packit 6c4009
	    lr_error (ldfile, _("%s: incomplete `END' line"),
Packit 6c4009
		      "LC_IDENTIFICATION");
Packit 6c4009
	  else if (arg->tok != tok_lc_identification)
Packit 6c4009
	    lr_error (ldfile, _("\
Packit 6c4009
%1$s: definition does not end with `END %1$s'"), "LC_IDENTIFICATION");
Packit 6c4009
	  lr_ignore_rest (ldfile, arg->tok == tok_lc_identification);
Packit 6c4009
	  return;
Packit 6c4009
Packit 6c4009
	default:
Packit 6c4009
	err_label:
Packit 6c4009
	  SYNTAX_ERROR (_("%s: syntax error"), "LC_IDENTIFICATION");
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_IDENTIFICATION");
Packit 6c4009
}