Back to index

glibc  2.9
ld-messages.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995-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@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 <sys/types.h>
00025 #include <regex.h>
00026 #include <string.h>
00027 #include <sys/uio.h>
00028 
00029 #include <assert.h>
00030 
00031 #include "localedef.h"
00032 #include "linereader.h"
00033 #include "localeinfo.h"
00034 #include "locfile.h"
00035 
00036 
00037 /* The real definition of the struct for the LC_MESSAGES locale.  */
00038 struct locale_messages_t
00039 {
00040   const char *yesexpr;
00041   const char *noexpr;
00042   const char *yesstr;
00043   const char *nostr;
00044 };
00045 
00046 
00047 static void
00048 messages_startup (struct linereader *lr, struct localedef_t *locale,
00049                 int ignore_content)
00050 {
00051   if (!ignore_content)
00052     locale->categories[LC_MESSAGES].messages =
00053       (struct locale_messages_t *) xcalloc (1,
00054                                        sizeof (struct locale_messages_t));
00055 
00056   if (lr != NULL)
00057     {
00058       lr->translate_strings = 1;
00059       lr->return_widestr = 0;
00060     }
00061 }
00062 
00063 
00064 void
00065 messages_finish (struct localedef_t *locale, const struct charmap_t *charmap)
00066 {
00067   struct locale_messages_t *messages
00068     = locale->categories[LC_MESSAGES].messages;
00069   int nothing = 0;
00070 
00071   /* Now resolve copying and also handle completely missing definitions.  */
00072   if (messages == NULL)
00073     {
00074       /* First see whether we were supposed to copy.  If yes, find the
00075         actual definition.  */
00076       if (locale->copy_name[LC_MESSAGES] != NULL)
00077        {
00078          /* Find the copying locale.  This has to happen transitively since
00079             the locale we are copying from might also copying another one.  */
00080          struct localedef_t *from = locale;
00081 
00082          do
00083            from = find_locale (LC_MESSAGES, from->copy_name[LC_MESSAGES],
00084                             from->repertoire_name, charmap);
00085          while (from->categories[LC_MESSAGES].messages == NULL
00086                && from->copy_name[LC_MESSAGES] != NULL);
00087 
00088          messages = locale->categories[LC_MESSAGES].messages
00089            = from->categories[LC_MESSAGES].messages;
00090        }
00091 
00092       /* If there is still no definition issue an warning and create an
00093         empty one.  */
00094       if (messages == NULL)
00095        {
00096          if (! be_quiet)
00097            WITH_CUR_LOCALE (error (0, 0, _("\
00098 No definition for %s category found"), "LC_MESSAGES"));
00099          messages_startup (NULL, locale, 0);
00100          messages = locale->categories[LC_MESSAGES].messages;
00101          nothing = 1;
00102        }
00103     }
00104 
00105   /* The fields YESSTR and NOSTR are optional.  */
00106   if (messages->yesstr == NULL)
00107     messages->yesstr = "";
00108   if (messages->nostr == NULL)
00109     messages->nostr = "";
00110 
00111   if (messages->yesexpr == NULL)
00112     {
00113       if (! be_quiet && ! nothing)
00114        WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' undefined"),
00115                             "LC_MESSAGES", "yesexpr"));
00116       messages->yesexpr = "^[yY]";
00117     }
00118   else if (messages->yesexpr[0] == '\0')
00119     {
00120       if (!be_quiet)
00121        WITH_CUR_LOCALE (error (0, 0, _("\
00122 %s: value for field `%s' must not be an empty string"),
00123                             "LC_MESSAGES", "yesexpr"));
00124     }
00125   else
00126     {
00127       int result;
00128       regex_t re;
00129 
00130       /* Test whether it are correct regular expressions.  */
00131       result = regcomp (&re, messages->yesexpr, REG_EXTENDED);
00132       if (result != 0 && !be_quiet)
00133        {
00134          char errbuf[BUFSIZ];
00135 
00136          (void) regerror (result, &re, errbuf, BUFSIZ);
00137          WITH_CUR_LOCALE (error (0, 0, _("\
00138 %s: no correct regular expression for field `%s': %s"),
00139                               "LC_MESSAGES", "yesexpr", errbuf));
00140        }
00141       else if (result != 0)
00142        regfree (&re);
00143     }
00144 
00145   if (messages->noexpr == NULL)
00146     {
00147       if (! be_quiet && ! nothing)
00148        WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' undefined"),
00149                             "LC_MESSAGES", "noexpr"));
00150       messages->noexpr = "^[nN]";
00151     }
00152   else if (messages->noexpr[0] == '\0')
00153     {
00154       if (!be_quiet)
00155        WITH_CUR_LOCALE (error (0, 0, _("\
00156 %s: value for field `%s' must not be an empty string"),
00157                             "LC_MESSAGES", "noexpr"));
00158     }
00159   else
00160     {
00161       int result;
00162       regex_t re;
00163 
00164       /* Test whether it are correct regular expressions.  */
00165       result = regcomp (&re, messages->noexpr, REG_EXTENDED);
00166       if (result != 0 && !be_quiet)
00167        {
00168          char errbuf[BUFSIZ];
00169 
00170          (void) regerror (result, &re, errbuf, BUFSIZ);
00171          WITH_CUR_LOCALE (error (0, 0, _("\
00172 %s: no correct regular expression for field `%s': %s"),
00173                               "LC_MESSAGES", "noexpr", errbuf));
00174        }
00175       else if (result != 0)
00176        regfree (&re);
00177     }
00178 }
00179 
00180 
00181 void
00182 messages_output (struct localedef_t *locale, const struct charmap_t *charmap,
00183                const char *output_path)
00184 {
00185   struct locale_messages_t *messages
00186     = locale->categories[LC_MESSAGES].messages;
00187   struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)];
00188   struct locale_file data;
00189   uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)];
00190   size_t cnt = 0;
00191 
00192   data.magic = LIMAGIC (LC_MESSAGES);
00193   data.n = _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES);
00194   iov[cnt].iov_base = (void *) &data;
00195   iov[cnt].iov_len = sizeof (data);
00196   ++cnt;
00197 
00198   iov[cnt].iov_base = (void *) idx;
00199   iov[cnt].iov_len = sizeof (idx);
00200   ++cnt;
00201 
00202   idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
00203   iov[cnt].iov_base = (char *) messages->yesexpr;
00204   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00205   ++cnt;
00206 
00207   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
00208   iov[cnt].iov_base = (char *) messages->noexpr;
00209   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00210   ++cnt;
00211 
00212   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
00213   iov[cnt].iov_base = (char *) messages->yesstr;
00214   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00215   ++cnt;
00216 
00217   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
00218   iov[cnt].iov_base = (char *) messages->nostr;
00219   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00220   ++cnt;
00221 
00222   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
00223   iov[cnt].iov_base = (char *) charmap->code_set_name;
00224   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
00225 
00226   assert (cnt + 1 == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES));
00227 
00228   write_locale_data (output_path, LC_MESSAGES, "LC_MESSAGES",
00229                    2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES), iov);
00230 }
00231 
00232 
00233 /* The parser for the LC_MESSAGES section of the locale definition.  */
00234 void
00235 messages_read (struct linereader *ldfile, struct localedef_t *result,
00236               const struct charmap_t *charmap, const char *repertoire_name,
00237               int ignore_content)
00238 {
00239   struct repertoire_t *repertoire = NULL;
00240   struct locale_messages_t *messages;
00241   struct token *now;
00242   enum token_t nowtok;
00243 
00244   /* Get the repertoire we have to use.  */
00245   if (repertoire_name != NULL)
00246     repertoire = repertoire_read (repertoire_name);
00247 
00248   /* The rest of the line containing `LC_MESSAGES' must be free.  */
00249   lr_ignore_rest (ldfile, 1);
00250 
00251 
00252   do
00253     {
00254       now = lr_token (ldfile, charmap, result, NULL, verbose);
00255       nowtok = now->tok;
00256     }
00257   while (nowtok == tok_eol);
00258 
00259   /* If we see `copy' now we are almost done.  */
00260   if (nowtok == tok_copy)
00261     {
00262       handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_messages,
00263                  LC_MESSAGES, "LC_MESSAGES", ignore_content);
00264       return;
00265     }
00266 
00267   /* Prepare the data structures.  */
00268   messages_startup (ldfile, result, ignore_content);
00269   messages = result->categories[LC_MESSAGES].messages;
00270 
00271   while (1)
00272     {
00273       struct token *arg;
00274 
00275       /* Of course we don't proceed beyond the end of file.  */
00276       if (nowtok == tok_eof)
00277        break;
00278 
00279       /* Ignore empty lines.  */
00280       if (nowtok == tok_eol)
00281        {
00282          now = lr_token (ldfile, charmap, result, NULL, verbose);
00283          nowtok = now->tok;
00284          continue;
00285        }
00286 
00287       switch (nowtok)
00288        {
00289 #define STR_ELEM(cat) \
00290        case tok_##cat:                                                      \
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          if (messages->cat != NULL)                                         \
00300            {                                                         \
00301              lr_error (ldfile, _("\
00302 %s: field `%s' declared more than once"), "LC_MESSAGES", #cat);                    \
00303              lr_ignore_rest (ldfile, 0);                             \
00304              break;                                                  \
00305            }                                                         \
00306          now = lr_token (ldfile, charmap, result, repertoire, verbose);      \
00307          if (now->tok != tok_string)                                        \
00308            goto syntax_error;                                               \
00309          else if (!ignore_content && now->val.str.startmb == NULL)          \
00310            {                                                         \
00311              lr_error (ldfile, _("\
00312 %s: unknown character in field `%s'"), "LC_MESSAGES", #cat);                \
00313              messages->cat = "";                                     \
00314            }                                                         \
00315          else if (!ignore_content)                                   \
00316            messages->cat = now->val.str.startmb;                     \
00317          break
00318 
00319          STR_ELEM (yesexpr);
00320          STR_ELEM (noexpr);
00321          STR_ELEM (yesstr);
00322          STR_ELEM (nostr);
00323 
00324        case tok_end:
00325          /* Next we assume `LC_MESSAGES'.  */
00326          arg = lr_token (ldfile, charmap, result, NULL, verbose);
00327          if (arg->tok == tok_eof)
00328            break;
00329          if (arg->tok == tok_eol)
00330            lr_error (ldfile, _("%s: incomplete `END' line"), "LC_MESSAGES");
00331          else if (arg->tok != tok_lc_messages)
00332            lr_error (ldfile, _("\
00333 %1$s: definition does not end with `END %1$s'"), "LC_MESSAGES");
00334          lr_ignore_rest (ldfile, arg->tok == tok_lc_messages);
00335          return;
00336 
00337        default:
00338        syntax_error:
00339          SYNTAX_ERROR (_("%s: syntax error"), "LC_MESSAGES");
00340        }
00341 
00342       /* Prepare for the next round.  */
00343       now = lr_token (ldfile, charmap, result, NULL, verbose);
00344       nowtok = now->tok;
00345     }
00346 
00347   /* When we come here we reached the end of the file.  */
00348   lr_error (ldfile, _("%s: premature end of file"), "LC_MESSAGES");
00349 }