Update to LGPL v2.1.
[jlayton/glibc.git] / locale / programs / ld-messages.c
1 /* Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <langinfo.h>
25 #include <sys/types.h>
26 #include <regex.h>
27 #include <string.h>
28 #include <sys/uio.h>
29
30 #include <assert.h>
31
32 #include "linereader.h"
33 #include "localedef.h"
34 #include "localeinfo.h"
35 #include "locfile.h"
36
37
38 /* The real definition of the struct for the LC_MESSAGES locale.  */
39 struct locale_messages_t
40 {
41   const char *yesexpr;
42   const char *noexpr;
43   const char *yesstr;
44   const char *nostr;
45 };
46
47
48 static void
49 messages_startup (struct linereader *lr, struct localedef_t *locale,
50                   int ignore_content)
51 {
52   if (!ignore_content)
53     locale->categories[LC_MESSAGES].messages =
54       (struct locale_messages_t *) xcalloc (1,
55                                             sizeof (struct locale_messages_t));
56
57   if (lr != NULL)
58     {
59       lr->translate_strings = 1;
60       lr->return_widestr = 0;
61     }
62 }
63
64
65 void
66 messages_finish (struct localedef_t *locale, struct charmap_t *charmap)
67 {
68   struct locale_messages_t *messages
69     = locale->categories[LC_MESSAGES].messages;
70   int nothing = 0;
71
72   /* Now resolve copying and also handle completely missing definitions.  */
73   if (messages == NULL)
74     {
75       /* First see whether we were supposed to copy.  If yes, find the
76          actual definition.  */
77       if (locale->copy_name[LC_MESSAGES] != NULL)
78         {
79           /* Find the copying locale.  This has to happen transitively since
80              the locale we are copying from might also copying another one.  */
81           struct localedef_t *from = locale;
82
83           do
84             from = find_locale (LC_MESSAGES, from->copy_name[LC_MESSAGES],
85                                 from->repertoire_name, charmap);
86           while (from->categories[LC_MESSAGES].messages == NULL
87                  && from->copy_name[LC_MESSAGES] != NULL);
88
89           messages = locale->categories[LC_MESSAGES].messages
90             = from->categories[LC_MESSAGES].messages;
91         }
92
93       /* If there is still no definition issue an warning and create an
94          empty one.  */
95       if (messages == NULL)
96         {
97           if (! be_quiet)
98             error (0, 0, _("No definition for %s category found"),
99                    "LC_MESSAGES");
100           messages_startup (NULL, locale, 0);
101           messages = locale->categories[LC_MESSAGES].messages;
102           nothing = 1;
103         }
104     }
105
106   /* The fields YESSTR and NOSTR are optional.  */
107   if (messages->yesstr == NULL)
108     messages->yesstr = "";
109   if (messages->nostr == NULL)
110     messages->nostr = "";
111
112   if (messages->yesexpr == NULL)
113     {
114       if (! be_quiet && ! nothing)
115         error (0, 0, _("%s: field `%s' undefined"), "LC_MESSAGES", "yesexpr");
116       messages->yesexpr = "";
117     }
118   else if (messages->yesexpr[0] == '\0')
119     {
120       if (!be_quiet)
121         error (0, 0, _("\
122 %s: value for field `%s' must not be an empty string"),
123                "LC_MESSAGES", "yesexpr");
124     }
125   else
126     {
127       int result;
128       regex_t re;
129
130       /* Test whether it are correct regular expressions.  */
131       result = regcomp (&re, messages->yesexpr, REG_EXTENDED);
132       if (result != 0 && !be_quiet)
133         {
134           char errbuf[BUFSIZ];
135
136           (void) regerror (result, &re, errbuf, BUFSIZ);
137           error (0, 0, _("\
138 %s: no correct regular expression for field `%s': %s"),
139                  "LC_MESSAGES", "yesexpr", errbuf);
140         }
141       else if (result != 0)
142         regfree (&re);
143     }
144
145   if (messages->noexpr == NULL)
146     {
147       if (! be_quiet && ! nothing)
148         error (0, 0, _("%s: field `%s' undefined"), "LC_MESSAGES", "noexpr");
149       messages->noexpr = "";
150     }
151   else if (messages->noexpr[0] == '\0')
152     {
153       if (!be_quiet)
154         error (0, 0, _("\
155 %s: value for field `%s' must not be an empty string"),
156                "LC_MESSAGES", "noexpr");
157     }
158   else
159     {
160       int result;
161       regex_t re;
162
163       /* Test whether it are correct regular expressions.  */
164       result = regcomp (&re, messages->noexpr, REG_EXTENDED);
165       if (result != 0 && !be_quiet)
166         {
167           char errbuf[BUFSIZ];
168
169           (void) regerror (result, &re, errbuf, BUFSIZ);
170           error (0, 0, _("\
171 %s: no correct regular expression for field `%s': %s"),
172                  "LC_MESSAGES", "noexpr", errbuf);
173         }
174       else if (result != 0)
175         regfree (&re);
176     }
177 }
178
179
180 void
181 messages_output (struct localedef_t *locale, struct charmap_t *charmap,
182                  const char *output_path)
183 {
184   struct locale_messages_t *messages
185     = locale->categories[LC_MESSAGES].messages;
186   struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)];
187   struct locale_file data;
188   uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)];
189   size_t cnt = 0;
190
191   data.magic = LIMAGIC (LC_MESSAGES);
192   data.n = _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES);
193   iov[cnt].iov_base = (void *) &data;
194   iov[cnt].iov_len = sizeof (data);
195   ++cnt;
196
197   iov[cnt].iov_base = (void *) idx;
198   iov[cnt].iov_len = sizeof (idx);
199   ++cnt;
200
201   idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
202   iov[cnt].iov_base = (char *) messages->yesexpr;
203   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
204   ++cnt;
205
206   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
207   iov[cnt].iov_base = (char *) messages->noexpr;
208   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
209   ++cnt;
210
211   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
212   iov[cnt].iov_base = (char *) messages->yesstr;
213   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
214   ++cnt;
215
216   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
217   iov[cnt].iov_base = (char *) messages->nostr;
218   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
219   ++cnt;
220
221   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
222   iov[cnt].iov_base = (char *) charmap->code_set_name;
223   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
224
225   assert (cnt + 1 == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES));
226
227   write_locale_data (output_path, "LC_MESSAGES",
228                      2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES), iov);
229 }
230
231
232 /* The parser for the LC_MESSAGES section of the locale definition.  */
233 void
234 messages_read (struct linereader *ldfile, struct localedef_t *result,
235                struct charmap_t *charmap, const char *repertoire_name,
236                int ignore_content)
237 {
238   struct repertoire_t *repertoire = NULL;
239   struct locale_messages_t *messages;
240   struct token *now;
241   enum token_t nowtok;
242
243   /* Get the repertoire we have to use.  */
244   if (repertoire_name != NULL)
245     repertoire = repertoire_read (repertoire_name);
246
247   /* The rest of the line containing `LC_MESSAGES' must be free.  */
248   lr_ignore_rest (ldfile, 1);
249
250
251   do
252     {
253       now = lr_token (ldfile, charmap, NULL, verbose);
254       nowtok = now->tok;
255     }
256   while (nowtok == tok_eol);
257
258   /* If we see `copy' now we are almost done.  */
259   if (nowtok == tok_copy)
260     {
261       handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_messages,
262                    LC_MESSAGES, "LC_MESSAGES", ignore_content);
263       return;
264     }
265
266   /* Prepare the data structures.  */
267   messages_startup (ldfile, result, ignore_content);
268   messages = result->categories[LC_MESSAGES].messages;
269
270   while (1)
271     {
272       struct token *arg;
273
274       /* Of course we don't proceed beyond the end of file.  */
275       if (nowtok == tok_eof)
276         break;
277
278       /* Ignore empty lines.  */
279       if (nowtok == tok_eol)
280         {
281           now = lr_token (ldfile, charmap, NULL, verbose);
282           nowtok = now->tok;
283           continue;
284         }
285
286       switch (nowtok)
287         {
288 #define STR_ELEM(cat) \
289         case tok_##cat:                                                       \
290           /* Ignore the rest of the line if we don't need the input of        \
291              this line.  */                                                   \
292           if (ignore_content)                                                 \
293             {                                                                 \
294               lr_ignore_rest (ldfile, 0);                                     \
295               break;                                                          \
296             }                                                                 \
297                                                                               \
298           if (messages->cat != NULL)                                          \
299             {                                                                 \
300               lr_error (ldfile, _("\
301 %s: field `%s' declared more than once"), "LC_MESSAGES", #cat);               \
302               lr_ignore_rest (ldfile, 0);                                     \
303               break;                                                          \
304             }                                                                 \
305           now = lr_token (ldfile, charmap, repertoire, verbose);              \
306           if (now->tok != tok_string)                                         \
307             goto syntax_error;                                                \
308           else if (!ignore_content && now->val.str.startmb == NULL)           \
309             {                                                                 \
310               lr_error (ldfile, _("\
311 %s: unknown character in field `%s'"), "LC_MESSAGES", #cat);                  \
312               messages->cat = "";                                             \
313             }                                                                 \
314           else if (!ignore_content)                                           \
315             messages->cat = now->val.str.startmb;                             \
316           break
317
318           STR_ELEM (yesexpr);
319           STR_ELEM (noexpr);
320           STR_ELEM (yesstr);
321           STR_ELEM (nostr);
322
323         case tok_end:
324           /* Next we assume `LC_MESSAGES'.  */
325           arg = lr_token (ldfile, charmap, NULL, verbose);
326           if (arg->tok == tok_eof)
327             break;
328           if (arg->tok == tok_eol)
329             lr_error (ldfile, _("%s: incomplete `END' line"), "LC_MESSAGES");
330           else if (arg->tok != tok_lc_messages)
331             lr_error (ldfile, _("\
332 %1$s: definition does not end with `END %1$s'"), "LC_MESSAGES");
333           lr_ignore_rest (ldfile, arg->tok == tok_lc_messages);
334           return;
335
336         default:
337         syntax_error:
338           SYNTAX_ERROR (_("%s: syntax error"), "LC_MESSAGES");
339         }
340
341       /* Prepare for the next round.  */
342       now = lr_token (ldfile, charmap, NULL, verbose);
343       nowtok = now->tok;
344     }
345
346   /* When we come here we reached the end of the file.  */
347   lr_error (ldfile, _("%s: premature end of file"), "LC_MESSAGES");
348 }