]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/ld-messages.c
Update.
[thirdparty/glibc.git] / locale / programs / ld-messages.c
1 /* Copyright (C) 1995-1999, 2000, 2001, 2002 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 "localedef.h"
33 #include "linereader.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, const 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 WITH_CUR_LOCALE (error (0, 0, _("\
99 No definition for %s category found"), "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 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' undefined"),
116 "LC_MESSAGES", "yesexpr"));
117 messages->yesexpr = "^[yY]";
118 }
119 else if (messages->yesexpr[0] == '\0')
120 {
121 if (!be_quiet)
122 WITH_CUR_LOCALE (error (0, 0, _("\
123 %s: value for field `%s' must not be an empty string"),
124 "LC_MESSAGES", "yesexpr"));
125 }
126 else
127 {
128 int result;
129 regex_t re;
130
131 /* Test whether it are correct regular expressions. */
132 result = regcomp (&re, messages->yesexpr, REG_EXTENDED);
133 if (result != 0 && !be_quiet)
134 {
135 char errbuf[BUFSIZ];
136
137 (void) regerror (result, &re, errbuf, BUFSIZ);
138 WITH_CUR_LOCALE (error (0, 0, _("\
139 %s: no correct regular expression for field `%s': %s"),
140 "LC_MESSAGES", "yesexpr", errbuf));
141 }
142 else if (result != 0)
143 regfree (&re);
144 }
145
146 if (messages->noexpr == NULL)
147 {
148 if (! be_quiet && ! nothing)
149 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' undefined"),
150 "LC_MESSAGES", "noexpr"));
151 messages->noexpr = "^[nN]";
152 }
153 else if (messages->noexpr[0] == '\0')
154 {
155 if (!be_quiet)
156 WITH_CUR_LOCALE (error (0, 0, _("\
157 %s: value for field `%s' must not be an empty string"),
158 "LC_MESSAGES", "noexpr"));
159 }
160 else
161 {
162 int result;
163 regex_t re;
164
165 /* Test whether it are correct regular expressions. */
166 result = regcomp (&re, messages->noexpr, REG_EXTENDED);
167 if (result != 0 && !be_quiet)
168 {
169 char errbuf[BUFSIZ];
170
171 (void) regerror (result, &re, errbuf, BUFSIZ);
172 WITH_CUR_LOCALE (error (0, 0, _("\
173 %s: no correct regular expression for field `%s': %s"),
174 "LC_MESSAGES", "noexpr", errbuf));
175 }
176 else if (result != 0)
177 regfree (&re);
178 }
179 }
180
181
182 void
183 messages_output (struct localedef_t *locale, const struct charmap_t *charmap,
184 const char *output_path)
185 {
186 struct locale_messages_t *messages
187 = locale->categories[LC_MESSAGES].messages;
188 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)];
189 struct locale_file data;
190 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)];
191 size_t cnt = 0;
192
193 data.magic = LIMAGIC (LC_MESSAGES);
194 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES);
195 iov[cnt].iov_base = (void *) &data;
196 iov[cnt].iov_len = sizeof (data);
197 ++cnt;
198
199 iov[cnt].iov_base = (void *) idx;
200 iov[cnt].iov_len = sizeof (idx);
201 ++cnt;
202
203 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
204 iov[cnt].iov_base = (char *) messages->yesexpr;
205 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
206 ++cnt;
207
208 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
209 iov[cnt].iov_base = (char *) messages->noexpr;
210 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
211 ++cnt;
212
213 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
214 iov[cnt].iov_base = (char *) messages->yesstr;
215 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
216 ++cnt;
217
218 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
219 iov[cnt].iov_base = (char *) messages->nostr;
220 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
221 ++cnt;
222
223 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
224 iov[cnt].iov_base = (char *) charmap->code_set_name;
225 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
226
227 assert (cnt + 1 == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES));
228
229 write_locale_data (output_path, LC_MESSAGES, "LC_MESSAGES",
230 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES), iov);
231 }
232
233
234 /* The parser for the LC_MESSAGES section of the locale definition. */
235 void
236 messages_read (struct linereader *ldfile, struct localedef_t *result,
237 const struct charmap_t *charmap, const char *repertoire_name,
238 int ignore_content)
239 {
240 struct repertoire_t *repertoire = NULL;
241 struct locale_messages_t *messages;
242 struct token *now;
243 enum token_t nowtok;
244
245 /* Get the repertoire we have to use. */
246 if (repertoire_name != NULL)
247 repertoire = repertoire_read (repertoire_name);
248
249 /* The rest of the line containing `LC_MESSAGES' must be free. */
250 lr_ignore_rest (ldfile, 1);
251
252
253 do
254 {
255 now = lr_token (ldfile, charmap, result, NULL, verbose);
256 nowtok = now->tok;
257 }
258 while (nowtok == tok_eol);
259
260 /* If we see `copy' now we are almost done. */
261 if (nowtok == tok_copy)
262 {
263 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_messages,
264 LC_MESSAGES, "LC_MESSAGES", ignore_content);
265 return;
266 }
267
268 /* Prepare the data structures. */
269 messages_startup (ldfile, result, ignore_content);
270 messages = result->categories[LC_MESSAGES].messages;
271
272 while (1)
273 {
274 struct token *arg;
275
276 /* Of course we don't proceed beyond the end of file. */
277 if (nowtok == tok_eof)
278 break;
279
280 /* Ignore empty lines. */
281 if (nowtok == tok_eol)
282 {
283 now = lr_token (ldfile, charmap, result, NULL, verbose);
284 nowtok = now->tok;
285 continue;
286 }
287
288 switch (nowtok)
289 {
290 #define STR_ELEM(cat) \
291 case tok_##cat: \
292 /* Ignore the rest of the line if we don't need the input of \
293 this line. */ \
294 if (ignore_content) \
295 { \
296 lr_ignore_rest (ldfile, 0); \
297 break; \
298 } \
299 \
300 if (messages->cat != NULL) \
301 { \
302 lr_error (ldfile, _("\
303 %s: field `%s' declared more than once"), "LC_MESSAGES", #cat); \
304 lr_ignore_rest (ldfile, 0); \
305 break; \
306 } \
307 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
308 if (now->tok != tok_string) \
309 goto syntax_error; \
310 else if (!ignore_content && now->val.str.startmb == NULL) \
311 { \
312 lr_error (ldfile, _("\
313 %s: unknown character in field `%s'"), "LC_MESSAGES", #cat); \
314 messages->cat = ""; \
315 } \
316 else if (!ignore_content) \
317 messages->cat = now->val.str.startmb; \
318 break
319
320 STR_ELEM (yesexpr);
321 STR_ELEM (noexpr);
322 STR_ELEM (yesstr);
323 STR_ELEM (nostr);
324
325 case tok_end:
326 /* Next we assume `LC_MESSAGES'. */
327 arg = lr_token (ldfile, charmap, result, NULL, verbose);
328 if (arg->tok == tok_eof)
329 break;
330 if (arg->tok == tok_eol)
331 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_MESSAGES");
332 else if (arg->tok != tok_lc_messages)
333 lr_error (ldfile, _("\
334 %1$s: definition does not end with `END %1$s'"), "LC_MESSAGES");
335 lr_ignore_rest (ldfile, arg->tok == tok_lc_messages);
336 return;
337
338 default:
339 syntax_error:
340 SYNTAX_ERROR (_("%s: syntax error"), "LC_MESSAGES");
341 }
342
343 /* Prepare for the next round. */
344 now = lr_token (ldfile, charmap, result, NULL, verbose);
345 nowtok = now->tok;
346 }
347
348 /* When we come here we reached the end of the file. */
349 lr_error (ldfile, _("%s: premature end of file"), "LC_MESSAGES");
350 }