Back to index

glibc  2.9
ld-numeric.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995-2002,2005,2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
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 "linereader.h"
00031 #include "localeinfo.h"
00032 #include "locfile.h"
00033 
00034 
00035 /* The real definition of the struct for the LC_NUMERIC locale.  */
00036 struct locale_numeric_t
00037 {
00038   const char *decimal_point;
00039   const char *thousands_sep;
00040   char *grouping;
00041   size_t grouping_len;
00042   uint32_t decimal_point_wc;
00043   uint32_t thousands_sep_wc;
00044 };
00045 
00046 
00047 static void
00048 numeric_startup (struct linereader *lr, struct localedef_t *locale,
00049                int ignore_content)
00050 {
00051   if (!ignore_content)
00052     {
00053       locale->categories[LC_NUMERIC].numeric =
00054        (struct locale_numeric_t *) xcalloc (1,
00055                                         sizeof (struct locale_numeric_t));
00056     }
00057 
00058   if (lr != NULL)
00059     {
00060       lr->translate_strings = 1;
00061       lr->return_widestr = 0;
00062     }
00063 }
00064 
00065 
00066 void
00067 numeric_finish (struct localedef_t *locale, const struct charmap_t *charmap)
00068 {
00069   struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
00070   int nothing = 0;
00071 
00072   /* Now resolve copying and also handle completely missing definitions.  */
00073   if (numeric == NULL)
00074     {
00075       /* First see whether we were supposed to copy.  If yes, find the
00076         actual definition.  */
00077       if (locale->copy_name[LC_NUMERIC] != NULL)
00078        {
00079          /* Find the copying locale.  This has to happen transitively since
00080             the locale we are copying from might also copying another one.  */
00081          struct localedef_t *from = locale;
00082 
00083          do
00084            from = find_locale (LC_NUMERIC, from->copy_name[LC_NUMERIC],
00085                             from->repertoire_name, charmap);
00086          while (from->categories[LC_NUMERIC].numeric == NULL
00087                && from->copy_name[LC_NUMERIC] != NULL);
00088 
00089          numeric = locale->categories[LC_NUMERIC].numeric
00090            = from->categories[LC_NUMERIC].numeric;
00091        }
00092 
00093       /* If there is still no definition issue an warning and create an
00094         empty one.  */
00095       if (numeric == NULL)
00096        {
00097          if (! be_quiet)
00098            WITH_CUR_LOCALE (error (0, 0, _("\
00099 No definition for %s category found"), "LC_NUMERIC"));
00100          numeric_startup (NULL, locale, 0);
00101          numeric = locale->categories[LC_NUMERIC].numeric;
00102          nothing = 1;
00103        }
00104     }
00105 
00106   /* The decimal point must not be empty.  This is not said explicitly
00107      in POSIX but ANSI C (ISO/IEC 9899) says in 4.4.2.1 it has to be
00108      != "".  */
00109   if (numeric->decimal_point == NULL)
00110     {
00111       if (! be_quiet && ! nothing)
00112        WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
00113                             "LC_NUMERIC", "decimal_point"));
00114       numeric->decimal_point = ".";
00115     }
00116   else if (numeric->decimal_point[0] == '\0' && ! be_quiet && ! nothing)
00117     {
00118       WITH_CUR_LOCALE (error (0, 0, _("\
00119 %s: value for field `%s' must not be an empty string"),
00120                            "LC_NUMERIC", "decimal_point"));
00121     }
00122   if (numeric->decimal_point_wc == L'\0')
00123     numeric->decimal_point_wc = L'.';
00124 
00125   if (numeric->grouping_len == 0 && ! be_quiet && ! nothing)
00126     WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
00127                          "LC_NUMERIC", "grouping"));
00128 }
00129 
00130 
00131 void
00132 numeric_output (struct localedef_t *locale, const struct charmap_t *charmap,
00133               const char *output_path)
00134 {
00135   struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
00136   struct iovec iov[3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)];
00137   struct locale_file data;
00138   uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)];
00139   size_t cnt = 0;
00140 
00141   data.magic = LIMAGIC (LC_NUMERIC);
00142   data.n = _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC);
00143   iov[cnt].iov_base = (void *) &data;
00144   iov[cnt].iov_len = sizeof (data);
00145   ++cnt;
00146 
00147   iov[cnt].iov_base = (void *) idx;
00148   iov[cnt].iov_len = sizeof (idx);
00149   ++cnt;
00150 
00151   idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
00152   iov[cnt].iov_base = (void *) (numeric->decimal_point ?: "");
00153   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00154   ++cnt;
00155 
00156   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
00157   iov[cnt].iov_base = (void *) (numeric->thousands_sep ?: "");
00158   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00159   ++cnt;
00160 
00161   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
00162   iov[cnt].iov_base = numeric->grouping;
00163   iov[cnt].iov_len = numeric->grouping_len;
00164   ++cnt;
00165 
00166   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
00167 
00168   /* Align following data */
00169   iov[cnt].iov_base = (void *) "\0\0";
00170   iov[cnt].iov_len = ((idx[cnt - 2] + 3) & ~3) - idx[cnt - 2];
00171   idx[cnt - 2] = (idx[cnt - 2] + 3) & ~3;
00172   ++cnt;
00173 
00174   iov[cnt].iov_base = (void *) &numeric->decimal_point_wc;
00175   iov[cnt].iov_len = sizeof (uint32_t);
00176   ++cnt;
00177 
00178   idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
00179   iov[cnt].iov_base = (void *) &numeric->thousands_sep_wc;
00180   iov[cnt].iov_len = sizeof (uint32_t);
00181   ++cnt;
00182 
00183   idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
00184   iov[cnt].iov_base = (void *) charmap->code_set_name;
00185   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00186 
00187   assert (cnt + 1 == 3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC));
00188 
00189   write_locale_data (output_path, LC_NUMERIC, "LC_NUMERIC",
00190                    3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC), iov);
00191 }
00192 
00193 
00194 /* The parser for the LC_NUMERIC section of the locale definition.  */
00195 void
00196 numeric_read (struct linereader *ldfile, struct localedef_t *result,
00197              const struct charmap_t *charmap, const char *repertoire_name,
00198              int ignore_content)
00199 {
00200   struct repertoire_t *repertoire = NULL;
00201   struct locale_numeric_t *numeric;
00202   struct token *now;
00203   enum token_t nowtok;
00204 
00205   /* Get the repertoire we have to use.  */
00206   if (repertoire_name != NULL)
00207     repertoire = repertoire_read (repertoire_name);
00208 
00209   /* The rest of the line containing `LC_NUMERIC' must be free.  */
00210   lr_ignore_rest (ldfile, 1);
00211 
00212 
00213   do
00214     {
00215       now = lr_token (ldfile, charmap, result, NULL, verbose);
00216       nowtok = now->tok;
00217     }
00218   while (nowtok == tok_eol);
00219 
00220   /* If we see `copy' now we are almost done.  */
00221   if (nowtok == tok_copy)
00222     {
00223       handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_numeric,
00224                  LC_NUMERIC, "LC_NUMERIC", ignore_content);
00225       return;
00226     }
00227 
00228   /* Prepare the data structures.  */
00229   numeric_startup (ldfile, result, ignore_content);
00230   numeric = result->categories[LC_NUMERIC].numeric;
00231 
00232   while (1)
00233     {
00234       /* Of course we don't proceed beyond the end of file.  */
00235       if (nowtok == tok_eof)
00236        break;
00237 
00238       /* Ingore empty lines.  */
00239       if (nowtok == tok_eol)
00240        {
00241          now = lr_token (ldfile, charmap, result, NULL, verbose);
00242          nowtok = now->tok;
00243          continue;
00244        }
00245 
00246       switch (nowtok)
00247        {
00248 #define STR_ELEM(cat) \
00249        case tok_##cat:                                                      \
00250          /* Ignore the rest of the line if we don't need the input of       \
00251             this line.  */                                           \
00252          if (ignore_content)                                                \
00253            {                                                         \
00254              lr_ignore_rest (ldfile, 0);                             \
00255              break;                                                  \
00256            }                                                         \
00257                                                                      \
00258          ldfile->return_widestr = 1;                                        \
00259          now = lr_token (ldfile, charmap, result, repertoire, verbose);            \
00260          if (now->tok != tok_string)                                        \
00261            goto err_label;                                           \
00262          if (numeric->cat != NULL)                                   \
00263            lr_error (ldfile, _("\
00264 %s: field `%s' declared more than once"), "LC_NUMERIC", #cat);              \
00265          else if (!ignore_content && now->val.str.startmb == NULL)          \
00266            {                                                         \
00267              lr_error (ldfile, _("\
00268 %s: unknown character in field `%s'"), "LC_NUMERIC", #cat);                 \
00269              numeric->cat = "";                                      \
00270              numeric->cat##_wc = L'\0';                              \
00271            }                                                         \
00272          else if (now->val.str.startwc != NULL && now->val.str.lenwc > 2)    \
00273            {                                                         \
00274              lr_error (ldfile, _("\
00275 %s: value for field `%s' must be a single character"), "LC_NUMERIC", #cat);   \
00276            }                                                         \
00277          else if (!ignore_content)                                   \
00278            {                                                         \
00279              numeric->cat = now->val.str.startmb;                           \
00280                                                                      \
00281              if (now->val.str.startwc != NULL)                              \
00282               numeric->cat##_wc = *now->val.str.startwc;                    \
00283            }                                                         \
00284          ldfile->return_widestr = 0;                                        \
00285          break
00286 
00287          STR_ELEM (decimal_point);
00288          STR_ELEM (thousands_sep);
00289 
00290        case tok_grouping:
00291          /* Ignore the rest of the line if we don't need the input of
00292             this line.  */
00293          if (ignore_content)
00294            {
00295              lr_ignore_rest (ldfile, 0);
00296              break;
00297            }
00298 
00299          now = lr_token (ldfile, charmap, result, NULL, verbose);
00300          if (now->tok != tok_minus1 && now->tok != tok_number)
00301            goto err_label;
00302          else
00303            {
00304              size_t act = 0;
00305              size_t max = 10;
00306              char *grouping = xmalloc (max);
00307 
00308              do
00309               {
00310                 if (act + 1 >= max)
00311                   {
00312                     max *= 2;
00313                     grouping = xrealloc (grouping, max);
00314                   }
00315 
00316                 if (act > 0 && grouping[act - 1] == '\177')
00317                   {
00318                     lr_error (ldfile, _("\
00319 %s: `-1' must be last entry in `%s' field"), "LC_NUMERIC", "grouping");
00320                     lr_ignore_rest (ldfile, 0);
00321                     break;
00322                   }
00323 
00324                 if (now->tok == tok_minus1)
00325                   grouping[act++] = '\177';
00326                 else if (now->val.num == 0)
00327                   {
00328                     /* A value of 0 disables grouping from here on but
00329                       we must not store a NUL character since this
00330                       terminates the string.  Use something different
00331                       which must not be used otherwise.  */
00332                     grouping[act++] = '\377';
00333                   }
00334                 else if (now->val.num > 126)
00335                   lr_error (ldfile, _("\
00336 %s: values for field `%s' must be smaller than 127"),
00337                            "LC_NUMERIC", "grouping");
00338                 else
00339                   grouping[act++] = now->val.num;
00340 
00341                 /* Next must be semicolon.  */
00342                 now = lr_token (ldfile, charmap, result, NULL, verbose);
00343                 if (now->tok != tok_semicolon)
00344                   break;
00345 
00346                 now = lr_token (ldfile, charmap, result, NULL, verbose);
00347               }
00348              while (now->tok == tok_minus1 || now->tok == tok_number);
00349 
00350              if (now->tok != tok_eol)
00351               goto err_label;
00352 
00353              grouping[act++] = '\0';
00354 
00355              numeric->grouping = xrealloc (grouping, act);
00356              numeric->grouping_len = act;
00357            }
00358          break;
00359 
00360        case tok_end:
00361          /* Next we assume `LC_NUMERIC'.  */
00362          now = lr_token (ldfile, charmap, result, NULL, verbose);
00363          if (now->tok == tok_eof)
00364            break;
00365          if (now->tok == tok_eol)
00366            lr_error (ldfile, _("%s: incomplete `END' line"), "LC_NUMERIC");
00367          else if (now->tok != tok_lc_numeric)
00368            lr_error (ldfile, _("\
00369 %1$s: definition does not end with `END %1$s'"), "LC_NUMERIC");
00370          lr_ignore_rest (ldfile, now->tok == tok_lc_numeric);
00371          return;
00372 
00373        default:
00374        err_label:
00375          SYNTAX_ERROR (_("%s: syntax error"), "LC_NUMERIC");
00376        }
00377 
00378       /* Prepare for the next round.  */
00379       now = lr_token (ldfile, charmap, result, NULL, verbose);
00380       nowtok = now->tok;
00381     }
00382 
00383   /* When we come here we reached the end of the file.  */
00384   lr_error (ldfile, _("%s: premature end of file"), "LC_NUMERIC");
00385 }