]>
Commit | Line | Data |
---|---|---|
a334319f | 1 | /* Copyright (C) 1995-1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
c84142e8 | 2 | This file is part of the GNU C Library. |
4b10dd6c | 3 | Contributed by Ulrich Drepper <drepper@gnu.org>, 1995. |
19bc17a9 | 4 | |
a334319f UD |
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. | |
19bc17a9 | 9 | |
a334319f | 10 | The GNU C Library is distributed in the hope that it will be useful, |
c84142e8 | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
a334319f UD |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. | |
19bc17a9 | 14 | |
a334319f UD |
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. */ | |
19bc17a9 RM |
19 | |
20 | #ifdef HAVE_CONFIG_H | |
21 | # include <config.h> | |
22 | #endif | |
23 | ||
19bc17a9 | 24 | #include <langinfo.h> |
4b10dd6c UD |
25 | #include <sys/types.h> |
26 | #include <regex.h> | |
19bc17a9 RM |
27 | #include <string.h> |
28 | #include <sys/uio.h> | |
29 | ||
19bc17a9 RM |
30 | #include <assert.h> |
31 | ||
4b10dd6c | 32 | #include "localedef.h" |
f2b98f97 | 33 | #include "linereader.h" |
19bc17a9 | 34 | #include "localeinfo.h" |
4b10dd6c | 35 | #include "locfile.h" |
19bc17a9 RM |
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 | ||
4b10dd6c | 48 | static void |
19bc17a9 | 49 | messages_startup (struct linereader *lr, struct localedef_t *locale, |
4b10dd6c | 50 | int ignore_content) |
19bc17a9 | 51 | { |
4b10dd6c UD |
52 | if (!ignore_content) |
53 | locale->categories[LC_MESSAGES].messages = | |
54 | (struct locale_messages_t *) xcalloc (1, | |
55 | sizeof (struct locale_messages_t)); | |
19bc17a9 | 56 | |
b9eb05d6 UD |
57 | if (lr != NULL) |
58 | { | |
59 | lr->translate_strings = 1; | |
60 | lr->return_widestr = 0; | |
61 | } | |
19bc17a9 RM |
62 | } |
63 | ||
64 | ||
65 | void | |
47e8b443 | 66 | messages_finish (struct localedef_t *locale, const struct charmap_t *charmap) |
19bc17a9 RM |
67 | { |
68 | struct locale_messages_t *messages | |
69 | = locale->categories[LC_MESSAGES].messages; | |
b9eb05d6 UD |
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 | { | |
f6ada7ad | 97 | if (! be_quiet) |
f2b98f97 UD |
98 | WITH_CUR_LOCALE (error (0, 0, _("\ |
99 | No definition for %s category found"), "LC_MESSAGES")); | |
b9eb05d6 UD |
100 | messages_startup (NULL, locale, 0); |
101 | messages = locale->categories[LC_MESSAGES].messages; | |
102 | nothing = 1; | |
103 | } | |
104 | } | |
19bc17a9 RM |
105 | |
106 | /* The fields YESSTR and NOSTR are optional. */ | |
4b10dd6c UD |
107 | if (messages->yesstr == NULL) |
108 | messages->yesstr = ""; | |
109 | if (messages->nostr == NULL) | |
110 | messages->nostr = ""; | |
111 | ||
880f421f UD |
112 | if (messages->yesexpr == NULL) |
113 | { | |
b9eb05d6 | 114 | if (! be_quiet && ! nothing) |
f2b98f97 UD |
115 | WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' undefined"), |
116 | "LC_MESSAGES", "yesexpr")); | |
ba5566a7 | 117 | messages->yesexpr = "^[yY]"; |
4b10dd6c UD |
118 | } |
119 | else if (messages->yesexpr[0] == '\0') | |
120 | { | |
121 | if (!be_quiet) | |
f2b98f97 | 122 | WITH_CUR_LOCALE (error (0, 0, _("\ |
4b10dd6c | 123 | %s: value for field `%s' must not be an empty string"), |
f2b98f97 | 124 | "LC_MESSAGES", "yesexpr")); |
880f421f | 125 | } |
19bc17a9 RM |
126 | else |
127 | { | |
128 | int result; | |
129 | regex_t re; | |
130 | ||
4b10dd6c | 131 | /* Test whether it are correct regular expressions. */ |
19bc17a9 | 132 | result = regcomp (&re, messages->yesexpr, REG_EXTENDED); |
c84142e8 | 133 | if (result != 0 && !be_quiet) |
19bc17a9 RM |
134 | { |
135 | char errbuf[BUFSIZ]; | |
136 | ||
137 | (void) regerror (result, &re, errbuf, BUFSIZ); | |
f2b98f97 | 138 | WITH_CUR_LOCALE (error (0, 0, _("\ |
4b10dd6c | 139 | %s: no correct regular expression for field `%s': %s"), |
f2b98f97 | 140 | "LC_MESSAGES", "yesexpr", errbuf)); |
19bc17a9 | 141 | } |
4b10dd6c UD |
142 | else if (result != 0) |
143 | regfree (&re); | |
19bc17a9 RM |
144 | } |
145 | ||
880f421f UD |
146 | if (messages->noexpr == NULL) |
147 | { | |
b9eb05d6 | 148 | if (! be_quiet && ! nothing) |
f2b98f97 UD |
149 | WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' undefined"), |
150 | "LC_MESSAGES", "noexpr")); | |
ba5566a7 | 151 | messages->noexpr = "^[nN]"; |
4b10dd6c UD |
152 | } |
153 | else if (messages->noexpr[0] == '\0') | |
154 | { | |
155 | if (!be_quiet) | |
f2b98f97 | 156 | WITH_CUR_LOCALE (error (0, 0, _("\ |
4b10dd6c | 157 | %s: value for field `%s' must not be an empty string"), |
f2b98f97 | 158 | "LC_MESSAGES", "noexpr")); |
880f421f | 159 | } |
19bc17a9 RM |
160 | else |
161 | { | |
162 | int result; | |
163 | regex_t re; | |
164 | ||
4b10dd6c | 165 | /* Test whether it are correct regular expressions. */ |
19bc17a9 | 166 | result = regcomp (&re, messages->noexpr, REG_EXTENDED); |
c84142e8 | 167 | if (result != 0 && !be_quiet) |
19bc17a9 RM |
168 | { |
169 | char errbuf[BUFSIZ]; | |
170 | ||
171 | (void) regerror (result, &re, errbuf, BUFSIZ); | |
f2b98f97 | 172 | WITH_CUR_LOCALE (error (0, 0, _("\ |
4b10dd6c | 173 | %s: no correct regular expression for field `%s': %s"), |
f2b98f97 | 174 | "LC_MESSAGES", "noexpr", errbuf)); |
19bc17a9 | 175 | } |
4b10dd6c UD |
176 | else if (result != 0) |
177 | regfree (&re); | |
19bc17a9 RM |
178 | } |
179 | } | |
180 | ||
181 | ||
182 | void | |
47e8b443 | 183 | messages_output (struct localedef_t *locale, const struct charmap_t *charmap, |
4b10dd6c | 184 | const char *output_path) |
19bc17a9 RM |
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; | |
4b10dd6c | 190 | uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)]; |
19bc17a9 RM |
191 | size_t cnt = 0; |
192 | ||
19bc17a9 RM |
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; | |
4b10dd6c | 204 | iov[cnt].iov_base = (char *) messages->yesexpr; |
19bc17a9 RM |
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; | |
4b10dd6c | 209 | iov[cnt].iov_base = (char *) messages->noexpr; |
19bc17a9 RM |
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; | |
4b10dd6c | 214 | iov[cnt].iov_base = (char *) messages->yesstr; |
19bc17a9 RM |
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; | |
4b10dd6c | 219 | iov[cnt].iov_base = (char *) messages->nostr; |
19bc17a9 | 220 | iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1; |
e7f21fa6 UD |
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; | |
19bc17a9 RM |
226 | |
227 | assert (cnt + 1 == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)); | |
228 | ||
a7b65cdc | 229 | write_locale_data (output_path, LC_MESSAGES, "LC_MESSAGES", |
19bc17a9 RM |
230 | 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES), iov); |
231 | } | |
232 | ||
233 | ||
4b10dd6c | 234 | /* The parser for the LC_MESSAGES section of the locale definition. */ |
19bc17a9 | 235 | void |
4b10dd6c | 236 | messages_read (struct linereader *ldfile, struct localedef_t *result, |
47e8b443 | 237 | const struct charmap_t *charmap, const char *repertoire_name, |
4b10dd6c | 238 | int ignore_content) |
19bc17a9 | 239 | { |
4b10dd6c UD |
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); | |
19bc17a9 | 248 | |
4b10dd6c UD |
249 | /* The rest of the line containing `LC_MESSAGES' must be free. */ |
250 | lr_ignore_rest (ldfile, 1); | |
251 | ||
252 | ||
253 | do | |
19bc17a9 | 254 | { |
47e8b443 | 255 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
4b10dd6c UD |
256 | nowtok = now->tok; |
257 | } | |
258 | while (nowtok == tok_eol); | |
19bc17a9 | 259 | |
4b10dd6c UD |
260 | /* If we see `copy' now we are almost done. */ |
261 | if (nowtok == tok_copy) | |
262 | { | |
01ff9d0b | 263 | handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_messages, |
b9eb05d6 | 264 | LC_MESSAGES, "LC_MESSAGES", ignore_content); |
4b10dd6c UD |
265 | return; |
266 | } | |
19bc17a9 | 267 | |
4b10dd6c UD |
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 | ||
42d7c593 | 280 | /* Ignore empty lines. */ |
4b10dd6c | 281 | if (nowtok == tok_eol) |
19bc17a9 | 282 | { |
47e8b443 | 283 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
4b10dd6c UD |
284 | nowtok = now->tok; |
285 | continue; | |
19bc17a9 | 286 | } |
19bc17a9 | 287 | |
4b10dd6c | 288 | switch (nowtok) |
19bc17a9 | 289 | { |
4b10dd6c UD |
290 | #define STR_ELEM(cat) \ |
291 | case tok_##cat: \ | |
b9eb05d6 UD |
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 | \ | |
4b10dd6c UD |
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 | } \ | |
47e8b443 | 307 | now = lr_token (ldfile, charmap, result, repertoire, verbose); \ |
4b10dd6c UD |
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'. */ | |
47e8b443 | 327 | arg = lr_token (ldfile, charmap, result, NULL, verbose); |
4b10dd6c UD |
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"); | |
19bc17a9 | 341 | } |
19bc17a9 | 342 | |
4b10dd6c | 343 | /* Prepare for the next round. */ |
47e8b443 | 344 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
4b10dd6c | 345 | nowtok = now->tok; |
19bc17a9 | 346 | } |
4b10dd6c UD |
347 | |
348 | /* When we come here we reached the end of the file. */ | |
349 | lr_error (ldfile, _("%s: premature end of file"), "LC_MESSAGES"); | |
19bc17a9 | 350 | } |