Updated to fedora-glibc-20090427T1419
[glibc/history.git] / locale / programs / ld-messages.c
blob7e41a19e3975ad365bdc1a04023e4c34f3a84c91
1 /* Copyright (C) 1995-1999,2000,2001,2002,2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <langinfo.h>
24 #include <sys/types.h>
25 #include <regex.h>
26 #include <string.h>
27 #include <sys/uio.h>
29 #include <assert.h>
31 #include "localedef.h"
32 #include "linereader.h"
33 #include "localeinfo.h"
34 #include "locfile.h"
37 /* The real definition of the struct for the LC_MESSAGES locale. */
38 struct locale_messages_t
40 const char *yesexpr;
41 const char *noexpr;
42 const char *yesstr;
43 const char *nostr;
47 static void
48 messages_startup (struct linereader *lr, struct localedef_t *locale,
49 int ignore_content)
51 if (!ignore_content)
52 locale->categories[LC_MESSAGES].messages =
53 (struct locale_messages_t *) xcalloc (1,
54 sizeof (struct locale_messages_t));
56 if (lr != NULL)
58 lr->translate_strings = 1;
59 lr->return_widestr = 0;
64 void
65 messages_finish (struct localedef_t *locale, const struct charmap_t *charmap)
67 struct locale_messages_t *messages
68 = locale->categories[LC_MESSAGES].messages;
69 int nothing = 0;
71 /* Now resolve copying and also handle completely missing definitions. */
72 if (messages == NULL)
74 /* First see whether we were supposed to copy. If yes, find the
75 actual definition. */
76 if (locale->copy_name[LC_MESSAGES] != NULL)
78 /* Find the copying locale. This has to happen transitively since
79 the locale we are copying from might also copying another one. */
80 struct localedef_t *from = locale;
83 from = find_locale (LC_MESSAGES, from->copy_name[LC_MESSAGES],
84 from->repertoire_name, charmap);
85 while (from->categories[LC_MESSAGES].messages == NULL
86 && from->copy_name[LC_MESSAGES] != NULL);
88 messages = locale->categories[LC_MESSAGES].messages
89 = from->categories[LC_MESSAGES].messages;
92 /* If there is still no definition issue an warning and create an
93 empty one. */
94 if (messages == NULL)
96 if (! be_quiet)
97 WITH_CUR_LOCALE (error (0, 0, _("\
98 No definition for %s category found"), "LC_MESSAGES"));
99 messages_startup (NULL, locale, 0);
100 messages = locale->categories[LC_MESSAGES].messages;
101 nothing = 1;
105 /* The fields YESSTR and NOSTR are optional. */
106 if (messages->yesstr == NULL)
107 messages->yesstr = "";
108 if (messages->nostr == NULL)
109 messages->nostr = "";
111 if (messages->yesexpr == NULL)
113 if (! be_quiet && ! nothing)
114 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' undefined"),
115 "LC_MESSAGES", "yesexpr"));
116 messages->yesexpr = "^[yY]";
118 else if (messages->yesexpr[0] == '\0')
120 if (!be_quiet)
121 WITH_CUR_LOCALE (error (0, 0, _("\
122 %s: value for field `%s' must not be an empty string"),
123 "LC_MESSAGES", "yesexpr"));
125 else
127 int result;
128 regex_t re;
130 /* Test whether it are correct regular expressions. */
131 result = regcomp (&re, messages->yesexpr, REG_EXTENDED);
132 if (result != 0 && !be_quiet)
134 char errbuf[BUFSIZ];
136 (void) regerror (result, &re, errbuf, BUFSIZ);
137 WITH_CUR_LOCALE (error (0, 0, _("\
138 %s: no correct regular expression for field `%s': %s"),
139 "LC_MESSAGES", "yesexpr", errbuf));
141 else if (result != 0)
142 regfree (&re);
145 if (messages->noexpr == NULL)
147 if (! be_quiet && ! nothing)
148 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' undefined"),
149 "LC_MESSAGES", "noexpr"));
150 messages->noexpr = "^[nN]";
152 else if (messages->noexpr[0] == '\0')
154 if (!be_quiet)
155 WITH_CUR_LOCALE (error (0, 0, _("\
156 %s: value for field `%s' must not be an empty string"),
157 "LC_MESSAGES", "noexpr"));
159 else
161 int result;
162 regex_t re;
164 /* Test whether it are correct regular expressions. */
165 result = regcomp (&re, messages->noexpr, REG_EXTENDED);
166 if (result != 0 && !be_quiet)
168 char errbuf[BUFSIZ];
170 (void) regerror (result, &re, errbuf, BUFSIZ);
171 WITH_CUR_LOCALE (error (0, 0, _("\
172 %s: no correct regular expression for field `%s': %s"),
173 "LC_MESSAGES", "noexpr", errbuf));
175 else if (result != 0)
176 regfree (&re);
181 void
182 messages_output (struct localedef_t *locale, const struct charmap_t *charmap,
183 const char *output_path)
185 struct locale_messages_t *messages
186 = locale->categories[LC_MESSAGES].messages;
187 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)];
188 struct locale_file data;
189 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)];
190 size_t cnt = 0;
192 data.magic = LIMAGIC (LC_MESSAGES);
193 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES);
194 iov[cnt].iov_base = (void *) &data;
195 iov[cnt].iov_len = sizeof (data);
196 ++cnt;
198 iov[cnt].iov_base = (void *) idx;
199 iov[cnt].iov_len = sizeof (idx);
200 ++cnt;
202 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
203 iov[cnt].iov_base = (char *) messages->yesexpr;
204 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
205 ++cnt;
207 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
208 iov[cnt].iov_base = (char *) messages->noexpr;
209 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
210 ++cnt;
212 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
213 iov[cnt].iov_base = (char *) messages->yesstr;
214 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
215 ++cnt;
217 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
218 iov[cnt].iov_base = (char *) messages->nostr;
219 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
220 ++cnt;
222 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
223 iov[cnt].iov_base = (char *) charmap->code_set_name;
224 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
226 assert (cnt + 1 == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES));
228 write_locale_data (output_path, LC_MESSAGES, "LC_MESSAGES",
229 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES), iov);
233 /* The parser for the LC_MESSAGES section of the locale definition. */
234 void
235 messages_read (struct linereader *ldfile, struct localedef_t *result,
236 const struct charmap_t *charmap, const char *repertoire_name,
237 int ignore_content)
239 struct repertoire_t *repertoire = NULL;
240 struct locale_messages_t *messages;
241 struct token *now;
242 enum token_t nowtok;
244 /* Get the repertoire we have to use. */
245 if (repertoire_name != NULL)
246 repertoire = repertoire_read (repertoire_name);
248 /* The rest of the line containing `LC_MESSAGES' must be free. */
249 lr_ignore_rest (ldfile, 1);
254 now = lr_token (ldfile, charmap, result, NULL, verbose);
255 nowtok = now->tok;
257 while (nowtok == tok_eol);
259 /* If we see `copy' now we are almost done. */
260 if (nowtok == tok_copy)
262 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_messages,
263 LC_MESSAGES, "LC_MESSAGES", ignore_content);
264 return;
267 /* Prepare the data structures. */
268 messages_startup (ldfile, result, ignore_content);
269 messages = result->categories[LC_MESSAGES].messages;
271 while (1)
273 struct token *arg;
275 /* Of course we don't proceed beyond the end of file. */
276 if (nowtok == tok_eof)
277 break;
279 /* Ignore empty lines. */
280 if (nowtok == tok_eol)
282 now = lr_token (ldfile, charmap, result, NULL, verbose);
283 nowtok = now->tok;
284 continue;
287 switch (nowtok)
289 #define STR_ELEM(cat) \
290 case tok_##cat: \
291 /* Ignore the rest of the line if we don't need the input of \
292 this line. */ \
293 if (ignore_content) \
295 lr_ignore_rest (ldfile, 0); \
296 break; \
299 if (messages->cat != NULL) \
301 lr_error (ldfile, _("\
302 %s: field `%s' declared more than once"), "LC_MESSAGES", #cat); \
303 lr_ignore_rest (ldfile, 0); \
304 break; \
306 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
307 if (now->tok != tok_string) \
308 goto syntax_error; \
309 else if (!ignore_content && now->val.str.startmb == NULL) \
311 lr_error (ldfile, _("\
312 %s: unknown character in field `%s'"), "LC_MESSAGES", #cat); \
313 messages->cat = ""; \
315 else if (!ignore_content) \
316 messages->cat = now->val.str.startmb; \
317 break
319 STR_ELEM (yesexpr);
320 STR_ELEM (noexpr);
321 STR_ELEM (yesstr);
322 STR_ELEM (nostr);
324 case tok_end:
325 /* Next we assume `LC_MESSAGES'. */
326 arg = lr_token (ldfile, charmap, result, NULL, verbose);
327 if (arg->tok == tok_eof)
328 break;
329 if (arg->tok == tok_eol)
330 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_MESSAGES");
331 else if (arg->tok != tok_lc_messages)
332 lr_error (ldfile, _("\
333 %1$s: definition does not end with `END %1$s'"), "LC_MESSAGES");
334 lr_ignore_rest (ldfile, arg->tok == tok_lc_messages);
335 return;
337 default:
338 syntax_error:
339 SYNTAX_ERROR (_("%s: syntax error"), "LC_MESSAGES");
342 /* Prepare for the next round. */
343 now = lr_token (ldfile, charmap, result, NULL, verbose);
344 nowtok = now->tok;
347 /* When we come here we reached the end of the file. */
348 lr_error (ldfile, _("%s: premature end of file"), "LC_MESSAGES");