1 | /* Copyright (C) 1995-2017 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 | 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. |
9 | |
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. |
14 | |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, see <http://www.gnu.org/licenses/>. */ |
17 | |
18 | #ifdef HAVE_CONFIG_H |
19 | # include <config.h> |
20 | #endif |
21 | |
22 | #include <langinfo.h> |
23 | #include <sys/types.h> |
24 | #include <regex.h> |
25 | #include <string.h> |
26 | #include <stdint.h> |
27 | #include <sys/uio.h> |
28 | |
29 | #include <assert.h> |
30 | |
31 | #include "localedef.h" |
32 | #include "linereader.h" |
33 | #include "localeinfo.h" |
34 | #include "locfile.h" |
35 | |
36 | |
37 | /* The real definition of the struct for the LC_MESSAGES locale. */ |
38 | struct locale_messages_t |
39 | { |
40 | const char *yesexpr; |
41 | const char *noexpr; |
42 | const char *yesstr; |
43 | const char *nostr; |
44 | }; |
45 | |
46 | |
47 | static void |
48 | messages_startup (struct linereader *lr, struct localedef_t *locale, |
49 | int ignore_content) |
50 | { |
51 | if (!ignore_content) |
52 | locale->categories[LC_MESSAGES].messages = |
53 | (struct locale_messages_t *) xcalloc (1, |
54 | sizeof (struct locale_messages_t)); |
55 | |
56 | if (lr != NULL) |
57 | { |
58 | lr->translate_strings = 1; |
59 | lr->return_widestr = 0; |
60 | } |
61 | } |
62 | |
63 | |
64 | void |
65 | messages_finish (struct localedef_t *locale, const struct charmap_t *charmap) |
66 | { |
67 | struct locale_messages_t *messages |
68 | = locale->categories[LC_MESSAGES].messages; |
69 | int nothing = 0; |
70 | |
71 | /* Now resolve copying and also handle completely missing definitions. */ |
72 | if (messages == NULL) |
73 | { |
74 | /* First see whether we were supposed to copy. If yes, find the |
75 | actual definition. */ |
76 | if (locale->copy_name[LC_MESSAGES] != NULL) |
77 | { |
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; |
81 | |
82 | do |
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); |
87 | |
88 | messages = locale->categories[LC_MESSAGES].messages |
89 | = from->categories[LC_MESSAGES].messages; |
90 | } |
91 | |
92 | /* If there is still no definition issue an warning and create an |
93 | empty one. */ |
94 | if (messages == NULL) |
95 | { |
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; |
102 | } |
103 | } |
104 | |
105 | /* The fields YESSTR and NOSTR are optional. */ |
106 | if (messages->yesstr == NULL) |
107 | messages->yesstr = "" ; |
108 | if (messages->nostr == NULL) |
109 | messages->nostr = "" ; |
110 | |
111 | if (messages->yesexpr == NULL) |
112 | { |
113 | if (! be_quiet && ! nothing) |
114 | WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' undefined" ), |
115 | "LC_MESSAGES" , "yesexpr" )); |
116 | messages->yesexpr = "^[yY]" ; |
117 | } |
118 | else if (messages->yesexpr[0] == '\0') |
119 | { |
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" )); |
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 | WITH_CUR_LOCALE (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 | WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' undefined" ), |
149 | "LC_MESSAGES" , "noexpr" )); |
150 | messages->noexpr = "^[nN]" ; |
151 | } |
152 | else if (messages->noexpr[0] == '\0') |
153 | { |
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" )); |
158 | } |
159 | else |
160 | { |
161 | int result; |
162 | regex_t re; |
163 | |
164 | /* Test whether it are correct regular expressions. */ |
165 | result = regcomp (&re, messages->noexpr, REG_EXTENDED); |
166 | if (result != 0 && !be_quiet) |
167 | { |
168 | char errbuf[BUFSIZ]; |
169 | |
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)); |
174 | } |
175 | else if (result != 0) |
176 | regfree (&re); |
177 | } |
178 | } |
179 | |
180 | |
181 | void |
182 | messages_output (struct localedef_t *locale, const struct charmap_t *charmap, |
183 | const char *output_path) |
184 | { |
185 | struct locale_messages_t *messages |
186 | = locale->categories[LC_MESSAGES].messages; |
187 | struct locale_file file; |
188 | |
189 | init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)); |
190 | add_locale_string (&file, messages->yesexpr); |
191 | add_locale_string (&file, messages->noexpr); |
192 | add_locale_string (&file, messages->yesstr); |
193 | add_locale_string (&file, messages->nostr); |
194 | add_locale_string (&file, charmap->code_set_name); |
195 | write_locale_data (output_path, LC_MESSAGES, "LC_MESSAGES" , &file); |
196 | } |
197 | |
198 | |
199 | /* The parser for the LC_MESSAGES section of the locale definition. */ |
200 | void |
201 | messages_read (struct linereader *ldfile, struct localedef_t *result, |
202 | const struct charmap_t *charmap, const char *repertoire_name, |
203 | int ignore_content) |
204 | { |
205 | struct repertoire_t *repertoire = NULL; |
206 | struct locale_messages_t *messages; |
207 | struct token *now; |
208 | enum token_t nowtok; |
209 | |
210 | /* Get the repertoire we have to use. */ |
211 | if (repertoire_name != NULL) |
212 | repertoire = repertoire_read (repertoire_name); |
213 | |
214 | /* The rest of the line containing `LC_MESSAGES' must be free. */ |
215 | lr_ignore_rest (ldfile, 1); |
216 | |
217 | |
218 | do |
219 | { |
220 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
221 | nowtok = now->tok; |
222 | } |
223 | while (nowtok == tok_eol); |
224 | |
225 | /* If we see `copy' now we are almost done. */ |
226 | if (nowtok == tok_copy) |
227 | { |
228 | handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_messages, |
229 | LC_MESSAGES, "LC_MESSAGES" , ignore_content); |
230 | return; |
231 | } |
232 | |
233 | /* Prepare the data structures. */ |
234 | messages_startup (ldfile, result, ignore_content); |
235 | messages = result->categories[LC_MESSAGES].messages; |
236 | |
237 | while (1) |
238 | { |
239 | struct token *arg; |
240 | |
241 | /* Of course we don't proceed beyond the end of file. */ |
242 | if (nowtok == tok_eof) |
243 | break; |
244 | |
245 | /* Ignore empty lines. */ |
246 | if (nowtok == tok_eol) |
247 | { |
248 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
249 | nowtok = now->tok; |
250 | continue; |
251 | } |
252 | |
253 | switch (nowtok) |
254 | { |
255 | #define STR_ELEM(cat) \ |
256 | case tok_##cat: \ |
257 | /* Ignore the rest of the line if we don't need the input of \ |
258 | this line. */ \ |
259 | if (ignore_content) \ |
260 | { \ |
261 | lr_ignore_rest (ldfile, 0); \ |
262 | break; \ |
263 | } \ |
264 | \ |
265 | if (messages->cat != NULL) \ |
266 | { \ |
267 | lr_error (ldfile, _("\ |
268 | %s: field `%s' declared more than once"), "LC_MESSAGES", #cat); \ |
269 | lr_ignore_rest (ldfile, 0); \ |
270 | break; \ |
271 | } \ |
272 | now = lr_token (ldfile, charmap, result, repertoire, verbose); \ |
273 | if (now->tok != tok_string) \ |
274 | goto syntax_error; \ |
275 | else if (!ignore_content && now->val.str.startmb == NULL) \ |
276 | { \ |
277 | lr_error (ldfile, _("\ |
278 | %s: unknown character in field `%s'"), "LC_MESSAGES", #cat); \ |
279 | messages->cat = ""; \ |
280 | } \ |
281 | else if (!ignore_content) \ |
282 | messages->cat = now->val.str.startmb; \ |
283 | break |
284 | |
285 | STR_ELEM (yesexpr); |
286 | STR_ELEM (noexpr); |
287 | STR_ELEM (yesstr); |
288 | STR_ELEM (nostr); |
289 | |
290 | case tok_end: |
291 | /* Next we assume `LC_MESSAGES'. */ |
292 | arg = lr_token (ldfile, charmap, result, NULL, verbose); |
293 | if (arg->tok == tok_eof) |
294 | break; |
295 | if (arg->tok == tok_eol) |
296 | lr_error (ldfile, _("%s: incomplete `END' line" ), "LC_MESSAGES" ); |
297 | else if (arg->tok != tok_lc_messages) |
298 | lr_error (ldfile, _("\ |
299 | %1$s: definition does not end with `END %1$s'" ), "LC_MESSAGES" ); |
300 | lr_ignore_rest (ldfile, arg->tok == tok_lc_messages); |
301 | return; |
302 | |
303 | default: |
304 | syntax_error: |
305 | SYNTAX_ERROR (_("%s: syntax error" ), "LC_MESSAGES" ); |
306 | } |
307 | |
308 | /* Prepare for the next round. */ |
309 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
310 | nowtok = now->tok; |
311 | } |
312 | |
313 | /* When we come here we reached the end of the file. */ |
314 | lr_error (ldfile, _("%s: premature end of file" ), "LC_MESSAGES" ); |
315 | } |
316 | |