Back to index

glibc  2.9
ld-name.c
Go to the documentation of this file.
00001 /* Copyright (C) 1998,1999,2000,2001,2002,2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published
00007    by the Free Software Foundation; version 2 of the License, or
00008    (at your option) any later version.
00009 
00010    This program 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
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software Foundation,
00017    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 # include <config.h>
00021 #endif
00022 
00023 #include <langinfo.h>
00024 #include <string.h>
00025 #include <sys/uio.h>
00026 
00027 #include <assert.h>
00028 
00029 #include "localedef.h"
00030 #include "localeinfo.h"
00031 #include "locfile.h"
00032 
00033 
00034 /* The real definition of the struct for the LC_NAME locale.  */
00035 struct locale_name_t
00036 {
00037   const char *name_fmt;
00038   const char *name_gen;
00039   const char *name_mr;
00040   const char *name_mrs;
00041   const char *name_miss;
00042   const char *name_ms;
00043 };
00044 
00045 
00046 static void
00047 name_startup (struct linereader *lr, struct localedef_t *locale,
00048               int ignore_content)
00049 {
00050   if (!ignore_content)
00051     locale->categories[LC_NAME].name =
00052       (struct locale_name_t *) xcalloc (1, sizeof (struct locale_name_t));
00053 
00054   if (lr != NULL)
00055     {
00056       lr->translate_strings = 1;
00057       lr->return_widestr = 0;
00058     }
00059 }
00060 
00061 
00062 void
00063 name_finish (struct localedef_t *locale, const struct charmap_t *charmap)
00064 {
00065   struct locale_name_t *name = locale->categories[LC_NAME].name;
00066   int nothing = 0;
00067 
00068   /* Now resolve copying and also handle completely missing definitions.  */
00069   if (name == NULL)
00070     {
00071       /* First see whether we were supposed to copy.  If yes, find the
00072         actual definition.  */
00073       if (locale->copy_name[LC_NAME] != NULL)
00074        {
00075          /* Find the copying locale.  This has to happen transitively since
00076             the locale we are copying from might also copying another one.  */
00077          struct localedef_t *from = locale;
00078 
00079          do
00080            from = find_locale (LC_NAME, from->copy_name[LC_NAME],
00081                             from->repertoire_name, charmap);
00082          while (from->categories[LC_NAME].name == NULL
00083                && from->copy_name[LC_NAME] != NULL);
00084 
00085          name = locale->categories[LC_NAME].name
00086            = from->categories[LC_NAME].name;
00087        }
00088 
00089       /* If there is still no definition issue an warning and create an
00090         empty one.  */
00091       if (name == NULL)
00092        {
00093          if (! be_quiet)
00094            WITH_CUR_LOCALE (error (0, 0, _("\
00095 No definition for %s category found"), "LC_NAME"));
00096          name_startup (NULL, locale, 0);
00097          name = locale->categories[LC_NAME].name;
00098          nothing = 1;
00099        }
00100     }
00101 
00102   if (name->name_fmt == NULL)
00103     {
00104       if (! nothing)
00105        WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
00106                             "LC_NAME", "name_fmt"));
00107       /* Use as the default value the value of the i18n locale.  */
00108       name->name_fmt = "%p%t%g%t%m%t%f";
00109     }
00110   else
00111     {
00112       /* We must check whether the format string contains only the
00113         allowed escape sequences.  */
00114       const char *cp = name->name_fmt;
00115 
00116       if (*cp == '\0')
00117        WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
00118                             "LC_NAME", "name_fmt"));
00119       else
00120        while (*cp != '\0')
00121          {
00122            if (*cp == '%')
00123              {
00124               if (*++cp == 'R')
00125                 /* Romanize-flag.  */
00126                 ++cp;
00127               if (strchr ("dfFgGlomMpsSt", *cp) == NULL)
00128                 {
00129                   WITH_CUR_LOCALE (error (0, 0, _("\
00130 %s: invalid escape sequence in field `%s'"), "LC_NAME", "name_fmt"));
00131                   break;
00132                 }
00133              }
00134            ++cp;
00135          }
00136     }
00137 
00138 #define TEST_ELEM(cat) \
00139   if (name->cat == NULL)                                             \
00140     {                                                                \
00141       if (verbose && ! nothing)                                             \
00142        WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),       \
00143                             "LC_NAME", #cat));                              \
00144       name->cat = "";                                                       \
00145     }
00146 
00147   TEST_ELEM (name_gen);
00148   TEST_ELEM (name_mr);
00149   TEST_ELEM (name_mrs);
00150   TEST_ELEM (name_miss);
00151   TEST_ELEM (name_ms);
00152 }
00153 
00154 
00155 void
00156 name_output (struct localedef_t *locale, const struct charmap_t *charmap,
00157             const char *output_path)
00158 {
00159   struct locale_name_t *name = locale->categories[LC_NAME].name;
00160   struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_NAME)];
00161   struct locale_file data;
00162   uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_NAME)];
00163   size_t cnt = 0;
00164 
00165   data.magic = LIMAGIC (LC_NAME);
00166   data.n = _NL_ITEM_INDEX (_NL_NUM_LC_NAME);
00167   iov[cnt].iov_base = (void *) &data;
00168   iov[cnt].iov_len = sizeof (data);
00169   ++cnt;
00170 
00171   iov[cnt].iov_base = (void *) idx;
00172   iov[cnt].iov_len = sizeof (idx);
00173   ++cnt;
00174 
00175   idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
00176   iov[cnt].iov_base = (void *) name->name_fmt;
00177   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00178   ++cnt;
00179 
00180   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
00181   iov[cnt].iov_base = (void *) name->name_gen;
00182   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00183   ++cnt;
00184 
00185   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
00186   iov[cnt].iov_base = (void *) name->name_mr;
00187   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00188   ++cnt;
00189 
00190   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
00191   iov[cnt].iov_base = (void *) name->name_mrs;
00192   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00193   ++cnt;
00194 
00195   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
00196   iov[cnt].iov_base = (void *) name->name_miss;
00197   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00198   ++cnt;
00199 
00200   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
00201   iov[cnt].iov_base = (void *) name->name_ms;
00202   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00203   ++cnt;
00204 
00205   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
00206   iov[cnt].iov_base = (void *) charmap->code_set_name;
00207   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00208   ++cnt;
00209 
00210   assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_NAME));
00211 
00212   write_locale_data (output_path, LC_NAME, "LC_NAME",
00213                    2 + _NL_ITEM_INDEX (_NL_NUM_LC_NAME), iov);
00214 }
00215 
00216 
00217 /* The parser for the LC_NAME section of the locale definition.  */
00218 void
00219 name_read (struct linereader *ldfile, struct localedef_t *result,
00220           const struct charmap_t *charmap, const char *repertoire_name,
00221           int ignore_content)
00222 {
00223   struct locale_name_t *name;
00224   struct token *now;
00225   struct token *arg;
00226   enum token_t nowtok;
00227 
00228   /* The rest of the line containing `LC_NAME' must be empty.  */
00229   lr_ignore_rest (ldfile, 1);
00230 
00231   do
00232     {
00233       now = lr_token (ldfile, charmap, result, NULL, verbose);
00234       nowtok = now->tok;
00235     }
00236   while (nowtok == tok_eol);
00237 
00238   /* If we see `copy' now we are almost done.  */
00239   if (nowtok == tok_copy)
00240     {
00241       handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_name,
00242                  LC_NAME, "LC_NAME", ignore_content);
00243       return;
00244     }
00245 
00246   /* Prepare the data structures.  */
00247   name_startup (ldfile, result, ignore_content);
00248   name = result->categories[LC_NAME].name;
00249 
00250   while (1)
00251     {
00252       /* Of course we don't proceed beyond the end of file.  */
00253       if (nowtok == tok_eof)
00254        break;
00255 
00256       /* Ignore empty lines.  */
00257       if (nowtok == tok_eol)
00258        {
00259          now = lr_token (ldfile, charmap, result, NULL, verbose);
00260          nowtok = now->tok;
00261          continue;
00262        }
00263 
00264       switch (nowtok)
00265        {
00266 #define STR_ELEM(cat) \
00267        case tok_##cat:                                                      \
00268          /* Ignore the rest of the line if we don't need the input of       \
00269             this line.  */                                           \
00270          if (ignore_content)                                                \
00271            {                                                         \
00272              lr_ignore_rest (ldfile, 0);                             \
00273              break;                                                  \
00274            }                                                         \
00275                                                                      \
00276          arg = lr_token (ldfile, charmap, result, NULL, verbose);           \
00277          if (arg->tok != tok_string)                                        \
00278            goto err_label;                                           \
00279          if (name->cat != NULL)                                      \
00280            lr_error (ldfile, _("%s: field `%s' declared more than once"),    \
00281                     "LC_NAME", #cat);                                       \
00282          else if (!ignore_content && arg->val.str.startmb == NULL)          \
00283            {                                                         \
00284              lr_error (ldfile, _("%s: unknown character in field `%s'"),     \
00285                      "LC_NAME", #cat);                               \
00286              name->cat = "";                                                \
00287            }                                                         \
00288          else if (!ignore_content)                                   \
00289            name->cat = arg->val.str.startmb;                                \
00290          break
00291 
00292          STR_ELEM (name_fmt);
00293          STR_ELEM (name_gen);
00294          STR_ELEM (name_mr);
00295          STR_ELEM (name_mrs);
00296          STR_ELEM (name_miss);
00297          STR_ELEM (name_ms);
00298 
00299        case tok_end:
00300          /* Next we assume `LC_NAME'.  */
00301          arg = lr_token (ldfile, charmap, result, NULL, verbose);
00302          if (arg->tok == tok_eof)
00303            break;
00304          if (arg->tok == tok_eol)
00305            lr_error (ldfile, _("%s: incomplete `END' line"), "LC_NAME");
00306          else if (arg->tok != tok_lc_name)
00307            lr_error (ldfile, _("\
00308 %1$s: definition does not end with `END %1$s'"), "LC_NAME");
00309          lr_ignore_rest (ldfile, arg->tok == tok_lc_name);
00310          return;
00311 
00312        default:
00313        err_label:
00314          SYNTAX_ERROR (_("%s: syntax error"), "LC_NAME");
00315        }
00316 
00317       /* Prepare for the next round.  */
00318       now = lr_token (ldfile, charmap, result, NULL, verbose);
00319       nowtok = now->tok;
00320     }
00321 
00322   /* When we come here we reached the end of the file.  */
00323   lr_error (ldfile, _("%s: premature end of file"), "LC_NAME");
00324 }