Update.
[glibc.git] / locale / programs / ld-messages.c
bloba29cfeaaecb7fc716a843c82e14d09319e779c2a
1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <langinfo.h>
25 #include <sys/types.h>
26 #include <regex.h>
27 #include <string.h>
28 #include <sys/uio.h>
30 #include <assert.h>
32 #include "linereader.h"
33 #include "localedef.h"
34 #include "localeinfo.h"
35 #include "locfile.h"
38 /* The real definition of the struct for the LC_MESSAGES locale. */
39 struct locale_messages_t
41 const char *yesexpr;
42 const char *noexpr;
43 const char *yesstr;
44 const char *nostr;
48 static void
49 messages_startup (struct linereader *lr, struct localedef_t *locale,
50 int ignore_content)
52 if (!ignore_content)
53 locale->categories[LC_MESSAGES].messages =
54 (struct locale_messages_t *) xcalloc (1,
55 sizeof (struct locale_messages_t));
57 if (lr != NULL)
59 lr->translate_strings = 1;
60 lr->return_widestr = 0;
65 void
66 messages_finish (struct localedef_t *locale, struct charmap_t *charmap)
68 struct locale_messages_t *messages
69 = locale->categories[LC_MESSAGES].messages;
70 int nothing = 0;
72 /* Now resolve copying and also handle completely missing definitions. */
73 if (messages == NULL)
75 /* First see whether we were supposed to copy. If yes, find the
76 actual definition. */
77 if (locale->copy_name[LC_MESSAGES] != NULL)
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;
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);
89 messages = locale->categories[LC_MESSAGES].messages
90 = from->categories[LC_MESSAGES].messages;
93 /* If there is still no definition issue an warning and create an
94 empty one. */
95 if (messages == NULL)
97 error (0, 0, _("No definition for %s category found"),
98 "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 error (0, 0, _("%s: field `%s' undefined"), "LC_MESSAGES", "yesexpr");
115 messages->yesexpr = "";
117 else if (messages->yesexpr[0] == '\0')
119 if (!be_quiet)
120 error (0, 0, _("\
121 %s: value for field `%s' must not be an empty string"),
122 "LC_MESSAGES", "yesexpr");
124 else
126 int result;
127 regex_t re;
129 /* Test whether it are correct regular expressions. */
130 result = regcomp (&re, messages->yesexpr, REG_EXTENDED);
131 if (result != 0 && !be_quiet)
133 char errbuf[BUFSIZ];
135 (void) regerror (result, &re, errbuf, BUFSIZ);
136 error (0, 0, _("\
137 %s: no correct regular expression for field `%s': %s"),
138 "LC_MESSAGES", "yesexpr", errbuf);
140 else if (result != 0)
141 regfree (&re);
144 if (messages->noexpr == NULL)
146 if (! be_quiet && ! nothing)
147 error (0, 0, _("%s: field `%s' undefined"), "LC_MESSAGES", "noexpr");
148 messages->noexpr = "";
150 else if (messages->noexpr[0] == '\0')
152 if (!be_quiet)
153 error (0, 0, _("\
154 %s: value for field `%s' must not be an empty string"),
155 "LC_MESSAGES", "noexpr");
157 else
159 int result;
160 regex_t re;
162 /* Test whether it are correct regular expressions. */
163 result = regcomp (&re, messages->noexpr, REG_EXTENDED);
164 if (result != 0 && !be_quiet)
166 char errbuf[BUFSIZ];
168 (void) regerror (result, &re, errbuf, BUFSIZ);
169 error (0, 0, _("\
170 %s: no correct regular expression for field `%s': %s"),
171 "LC_MESSAGES", "noexpr", errbuf);
173 else if (result != 0)
174 regfree (&re);
179 void
180 messages_output (struct localedef_t *locale, struct charmap_t *charmap,
181 const char *output_path)
183 struct locale_messages_t *messages
184 = locale->categories[LC_MESSAGES].messages;
185 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)];
186 struct locale_file data;
187 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)];
188 size_t cnt = 0;
190 data.magic = LIMAGIC (LC_MESSAGES);
191 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES);
192 iov[cnt].iov_base = (void *) &data;
193 iov[cnt].iov_len = sizeof (data);
194 ++cnt;
196 iov[cnt].iov_base = (void *) idx;
197 iov[cnt].iov_len = sizeof (idx);
198 ++cnt;
200 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
201 iov[cnt].iov_base = (char *) messages->yesexpr;
202 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
203 ++cnt;
205 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
206 iov[cnt].iov_base = (char *) messages->noexpr;
207 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
208 ++cnt;
210 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
211 iov[cnt].iov_base = (char *) messages->yesstr;
212 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
213 ++cnt;
215 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
216 iov[cnt].iov_base = (char *) messages->nostr;
217 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
219 assert (cnt + 1 == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES));
221 write_locale_data (output_path, "LC_MESSAGES",
222 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES), iov);
226 /* The parser for the LC_MESSAGES section of the locale definition. */
227 void
228 messages_read (struct linereader *ldfile, struct localedef_t *result,
229 struct charmap_t *charmap, const char *repertoire_name,
230 int ignore_content)
232 struct repertoire_t *repertoire = NULL;
233 struct locale_messages_t *messages;
234 struct token *now;
235 enum token_t nowtok;
237 /* Get the repertoire we have to use. */
238 if (repertoire_name != NULL)
239 repertoire = repertoire_read (repertoire_name);
241 /* The rest of the line containing `LC_MESSAGES' must be free. */
242 lr_ignore_rest (ldfile, 1);
247 now = lr_token (ldfile, charmap, NULL);
248 nowtok = now->tok;
250 while (nowtok == tok_eol);
252 /* If we see `copy' now we are almost done. */
253 if (nowtok == tok_copy)
255 handle_copy (ldfile, charmap, repertoire, result, tok_lc_messages,
256 LC_MESSAGES, "LC_MESSAGES", ignore_content);
257 return;
260 /* Prepare the data structures. */
261 messages_startup (ldfile, result, ignore_content);
262 messages = result->categories[LC_MESSAGES].messages;
264 while (1)
266 struct token *arg;
268 /* Of course we don't proceed beyond the end of file. */
269 if (nowtok == tok_eof)
270 break;
272 /* Ignore empty lines. */
273 if (nowtok == tok_eol)
275 now = lr_token (ldfile, charmap, NULL);
276 nowtok = now->tok;
277 continue;
280 switch (nowtok)
282 #define STR_ELEM(cat) \
283 case tok_##cat: \
284 /* Ignore the rest of the line if we don't need the input of \
285 this line. */ \
286 if (ignore_content) \
288 lr_ignore_rest (ldfile, 0); \
289 break; \
292 if (messages->cat != NULL) \
294 lr_error (ldfile, _("\
295 %s: field `%s' declared more than once"), "LC_MESSAGES", #cat); \
296 lr_ignore_rest (ldfile, 0); \
297 break; \
299 now = lr_token (ldfile, charmap, repertoire); \
300 if (now->tok != tok_string) \
301 goto syntax_error; \
302 else if (!ignore_content && now->val.str.startmb == NULL) \
304 lr_error (ldfile, _("\
305 %s: unknown character in field `%s'"), "LC_MESSAGES", #cat); \
306 messages->cat = ""; \
308 else if (!ignore_content) \
309 messages->cat = now->val.str.startmb; \
310 break
312 STR_ELEM (yesexpr);
313 STR_ELEM (noexpr);
314 STR_ELEM (yesstr);
315 STR_ELEM (nostr);
317 case tok_end:
318 /* Next we assume `LC_MESSAGES'. */
319 arg = lr_token (ldfile, charmap, NULL);
320 if (arg->tok == tok_eof)
321 break;
322 if (arg->tok == tok_eol)
323 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_MESSAGES");
324 else if (arg->tok != tok_lc_messages)
325 lr_error (ldfile, _("\
326 %1$s: definition does not end with `END %1$s'"), "LC_MESSAGES");
327 lr_ignore_rest (ldfile, arg->tok == tok_lc_messages);
328 return;
330 default:
331 syntax_error:
332 SYNTAX_ERROR (_("%s: syntax error"), "LC_MESSAGES");
335 /* Prepare for the next round. */
336 now = lr_token (ldfile, charmap, NULL);
337 nowtok = now->tok;
340 /* When we come here we reached the end of the file. */
341 lr_error (ldfile, _("%s: premature end of file"), "LC_MESSAGES");