Back to index

glibc  2.9
ld-measurement.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 <error.h>
00024 #include <langinfo.h>
00025 #include <string.h>
00026 #include <sys/uio.h>
00027 
00028 #include <assert.h>
00029 
00030 #include "localedef.h"
00031 #include "localeinfo.h"
00032 #include "locfile.h"
00033 
00034 
00035 /* The real definition of the struct for the LC_MEASUREMENT locale.  */
00036 struct locale_measurement_t
00037 {
00038   unsigned char measurement;
00039 };
00040 
00041 
00042 static void
00043 measurement_startup (struct linereader *lr, struct localedef_t *locale,
00044                    int ignore_content)
00045 {
00046   if (!ignore_content)
00047     locale->categories[LC_MEASUREMENT].measurement =
00048       (struct locale_measurement_t *)
00049       xcalloc (1, sizeof (struct locale_measurement_t));
00050 
00051   if (lr != NULL)
00052     {
00053       lr->translate_strings = 1;
00054       lr->return_widestr = 0;
00055     }
00056 }
00057 
00058 
00059 void
00060 measurement_finish (struct localedef_t *locale,
00061                   const struct charmap_t *charmap)
00062 {
00063   struct locale_measurement_t *measurement =
00064     locale->categories[LC_MEASUREMENT].measurement;
00065   int nothing = 0;
00066 
00067   /* Now resolve copying and also handle completely missing definitions.  */
00068   if (measurement == NULL)
00069     {
00070       /* First see whether we were supposed to copy.  If yes, find the
00071         actual definition.  */
00072       if (locale->copy_name[LC_MEASUREMENT] != NULL)
00073        {
00074          /* Find the copying locale.  This has to happen transitively since
00075             the locale we are copying from might also copying another one.  */
00076          struct localedef_t *from = locale;
00077 
00078          do
00079            from = find_locale (LC_MEASUREMENT,
00080                             from->copy_name[LC_MEASUREMENT],
00081                             from->repertoire_name, charmap);
00082          while (from->categories[LC_MEASUREMENT].measurement == NULL
00083                && from->copy_name[LC_MEASUREMENT] != NULL);
00084 
00085          measurement = locale->categories[LC_MEASUREMENT].measurement
00086            = from->categories[LC_MEASUREMENT].measurement;
00087        }
00088 
00089       /* If there is still no definition issue an warning and create an
00090         empty one.  */
00091       if (measurement == NULL)
00092        {
00093          if (! be_quiet)
00094            WITH_CUR_LOCALE (error (0, 0, _("\
00095 No definition for %s category found"), "LC_MEASUREMENT"));
00096          measurement_startup (NULL, locale, 0);
00097          measurement = locale->categories[LC_MEASUREMENT].measurement;
00098          nothing = 1;
00099        }
00100     }
00101 
00102   if (measurement->measurement == 0)
00103     {
00104       if (! nothing)
00105        WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
00106                             "LC_MEASUREMENT", "measurement"));
00107       /* Use as the default value the value of the i18n locale.  */
00108       measurement->measurement = 1;
00109     }
00110   else
00111     {
00112       if (measurement->measurement > 3)
00113        WITH_CUR_LOCALE (error (0, 0, _("%s: invalid value for field `%s'"),
00114                             "LC_MEASUREMENT", "measurement"));
00115     }
00116 }
00117 
00118 
00119 void
00120 measurement_output (struct localedef_t *locale,
00121                   const struct charmap_t *charmap, const char *output_path)
00122 {
00123   struct locale_measurement_t *measurement =
00124     locale->categories[LC_MEASUREMENT].measurement;
00125   struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT)];
00126   struct locale_file data;
00127   uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT)];
00128   size_t cnt = 0;
00129 
00130   data.magic = LIMAGIC (LC_MEASUREMENT);
00131   data.n = _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT);
00132   iov[cnt].iov_base = (void *) &data;
00133   iov[cnt].iov_len = sizeof (data);
00134   ++cnt;
00135 
00136   iov[cnt].iov_base = (void *) idx;
00137   iov[cnt].iov_len = sizeof (idx);
00138   ++cnt;
00139 
00140   idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
00141   iov[cnt].iov_base = &measurement->measurement;
00142   iov[cnt].iov_len = 1;
00143   ++cnt;
00144 
00145   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
00146   iov[cnt].iov_base = (void *) charmap->code_set_name;
00147   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00148   ++cnt;
00149 
00150   assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT));
00151 
00152   write_locale_data (output_path, LC_MEASUREMENT, "LC_MEASUREMENT",
00153                    2 + _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT), iov);
00154 }
00155 
00156 
00157 /* The parser for the LC_MEASUREMENT section of the locale definition.  */
00158 void
00159 measurement_read (struct linereader *ldfile, struct localedef_t *result,
00160                 const struct charmap_t *charmap, const char *repertoire_name,
00161                 int ignore_content)
00162 {
00163   struct locale_measurement_t *measurement;
00164   struct token *now;
00165   struct token *arg;
00166   enum token_t nowtok;
00167 
00168   /* The rest of the line containing `LC_MEASUREMENT' must be free.  */
00169   lr_ignore_rest (ldfile, 1);
00170 
00171   do
00172     {
00173       now = lr_token (ldfile, charmap, result, NULL, verbose);
00174       nowtok = now->tok;
00175     }
00176   while (nowtok == tok_eol);
00177 
00178   /* If we see `copy' now we are almost done.  */
00179   if (nowtok == tok_copy)
00180     {
00181       handle_copy (ldfile, charmap, repertoire_name, result,
00182                  tok_lc_measurement, LC_MEASUREMENT, "LC_MEASUREMENT",
00183                  ignore_content);
00184       return;
00185     }
00186 
00187   /* Prepare the data structures.  */
00188   measurement_startup (ldfile, result, ignore_content);
00189   measurement = result->categories[LC_MEASUREMENT].measurement;
00190 
00191   while (1)
00192     {
00193       /* Of course we don't proceed beyond the end of file.  */
00194       if (nowtok == tok_eof)
00195        break;
00196 
00197       /* Ingore empty lines.  */
00198       if (nowtok == tok_eol)
00199        {
00200          now = lr_token (ldfile, charmap, result, NULL, verbose);
00201          nowtok = now->tok;
00202          continue;
00203        }
00204 
00205       switch (nowtok)
00206        {
00207 #define INT_ELEM(cat) \
00208        case tok_##cat:                                                      \
00209          /* Ignore the rest of the line if we don't need the input of       \
00210             this line.  */                                           \
00211          if (ignore_content)                                                \
00212            {                                                         \
00213              lr_ignore_rest (ldfile, 0);                             \
00214              break;                                                  \
00215            }                                                         \
00216                                                                      \
00217          arg = lr_token (ldfile, charmap, result, NULL, verbose);           \
00218          if (arg->tok != tok_number)                                        \
00219            goto err_label;                                           \
00220          else if (measurement->cat != 0)                             \
00221            lr_error (ldfile, _("%s: field `%s' declared more than once"),    \
00222                     "LC_MEASUREMENT", #cat);                                \
00223          else if (!ignore_content)                                   \
00224            measurement->cat = arg->val.num;                                 \
00225          break
00226 
00227          INT_ELEM (measurement);
00228 
00229        case tok_end:
00230          /* Next we assume `LC_MEASUREMENT'.  */
00231          arg = lr_token (ldfile, charmap, result, NULL, verbose);
00232          if (arg->tok == tok_eof)
00233            break;
00234          if (arg->tok == tok_eol)
00235            lr_error (ldfile, _("%s: incomplete `END' line"),
00236                     "LC_MEASUREMENT");
00237          else if (arg->tok != tok_lc_measurement)
00238            lr_error (ldfile, _("\
00239 %1$s: definition does not end with `END %1$s'"), "LC_MEASUREMENT");
00240          lr_ignore_rest (ldfile, arg->tok == tok_lc_measurement);
00241          return;
00242 
00243        default:
00244        err_label:
00245          SYNTAX_ERROR (_("%s: syntax error"), "LC_MEASUREMENT");
00246        }
00247 
00248       /* Prepare for the next round.  */
00249       now = lr_token (ldfile, charmap, result, NULL, verbose);
00250       nowtok = now->tok;
00251     }
00252 
00253   /* When we come here we reached the end of the file.  */
00254   lr_error (ldfile, _("%s: premature end of file"),
00255            "LC_MEASUREMENT");
00256 }