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