]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/programs/ld-messages.c
Update.
[thirdparty/glibc.git] / locale / programs / ld-messages.c
CommitLineData
4b10dd6c 1/* Copyright (C) 1995, 1996, 1997, 1998, 1999 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
UD
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.
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
13 Library General Public License for more details.
19bc17a9 14
c84142e8
UD
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. */
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
4b10dd6c 66messages_finish (struct localedef_t *locale, 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
UD
115 error (0, 0, _("%s: field `%s' undefined"), "LC_MESSAGES", "yesexpr");
116 messages->yesexpr = "";
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
UD
148 error (0, 0, _("%s: field `%s' undefined"), "LC_MESSAGES", "noexpr");
149 messages->noexpr = "";
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
4b10dd6c
UD
181messages_output (struct localedef_t *locale, struct charmap_t *charmap,
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
RM
218 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
219
220 assert (cnt + 1 == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES));
221
222 write_locale_data (output_path, "LC_MESSAGES",
223 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES), iov);
224}
225
226
4b10dd6c 227/* The parser for the LC_MESSAGES section of the locale definition. */
19bc17a9 228void
4b10dd6c
UD
229messages_read (struct linereader *ldfile, struct localedef_t *result,
230 struct charmap_t *charmap, const char *repertoire_name,
231 int ignore_content)
19bc17a9 232{
4b10dd6c
UD
233 struct repertoire_t *repertoire = NULL;
234 struct locale_messages_t *messages;
235 struct token *now;
236 enum token_t nowtok;
237
238 /* Get the repertoire we have to use. */
239 if (repertoire_name != NULL)
240 repertoire = repertoire_read (repertoire_name);
19bc17a9 241
4b10dd6c
UD
242 /* The rest of the line containing `LC_MESSAGES' must be free. */
243 lr_ignore_rest (ldfile, 1);
244
245
246 do
19bc17a9 247 {
4b10dd6c
UD
248 now = lr_token (ldfile, charmap, NULL);
249 nowtok = now->tok;
250 }
251 while (nowtok == tok_eol);
19bc17a9 252
4b10dd6c
UD
253 /* If we see `copy' now we are almost done. */
254 if (nowtok == tok_copy)
255 {
b9eb05d6
UD
256 handle_copy (ldfile, charmap, repertoire, result, tok_lc_messages,
257 LC_MESSAGES, "LC_MESSAGES", ignore_content);
4b10dd6c
UD
258 return;
259 }
19bc17a9 260
4b10dd6c
UD
261 /* Prepare the data structures. */
262 messages_startup (ldfile, result, ignore_content);
263 messages = result->categories[LC_MESSAGES].messages;
264
265 while (1)
266 {
267 struct token *arg;
268
269 /* Of course we don't proceed beyond the end of file. */
270 if (nowtok == tok_eof)
271 break;
272
42d7c593 273 /* Ignore empty lines. */
4b10dd6c 274 if (nowtok == tok_eol)
19bc17a9 275 {
4b10dd6c
UD
276 now = lr_token (ldfile, charmap, NULL);
277 nowtok = now->tok;
278 continue;
19bc17a9 279 }
19bc17a9 280
4b10dd6c 281 switch (nowtok)
19bc17a9 282 {
4b10dd6c
UD
283#define STR_ELEM(cat) \
284 case tok_##cat: \
b9eb05d6
UD
285 /* Ignore the rest of the line if we don't need the input of \
286 this line. */ \
287 if (ignore_content) \
288 { \
289 lr_ignore_rest (ldfile, 0); \
290 break; \
291 } \
292 \
4b10dd6c
UD
293 if (messages->cat != NULL) \
294 { \
295 lr_error (ldfile, _("\
296%s: field `%s' declared more than once"), "LC_MESSAGES", #cat); \
297 lr_ignore_rest (ldfile, 0); \
298 break; \
299 } \
300 now = lr_token (ldfile, charmap, repertoire); \
301 if (now->tok != tok_string) \
302 goto syntax_error; \
303 else if (!ignore_content && now->val.str.startmb == NULL) \
304 { \
305 lr_error (ldfile, _("\
306%s: unknown character in field `%s'"), "LC_MESSAGES", #cat); \
307 messages->cat = ""; \
308 } \
309 else if (!ignore_content) \
310 messages->cat = now->val.str.startmb; \
311 break
312
313 STR_ELEM (yesexpr);
314 STR_ELEM (noexpr);
315 STR_ELEM (yesstr);
316 STR_ELEM (nostr);
317
318 case tok_end:
319 /* Next we assume `LC_MESSAGES'. */
320 arg = lr_token (ldfile, charmap, NULL);
321 if (arg->tok == tok_eof)
322 break;
323 if (arg->tok == tok_eol)
324 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_MESSAGES");
325 else if (arg->tok != tok_lc_messages)
326 lr_error (ldfile, _("\
327%1$s: definition does not end with `END %1$s'"), "LC_MESSAGES");
328 lr_ignore_rest (ldfile, arg->tok == tok_lc_messages);
329 return;
330
331 default:
332 syntax_error:
333 SYNTAX_ERROR (_("%s: syntax error"), "LC_MESSAGES");
19bc17a9 334 }
19bc17a9 335
4b10dd6c
UD
336 /* Prepare for the next round. */
337 now = lr_token (ldfile, charmap, NULL);
338 nowtok = now->tok;
19bc17a9 339 }
4b10dd6c
UD
340
341 /* When we come here we reached the end of the file. */
342 lr_error (ldfile, _("%s: premature end of file"), "LC_MESSAGES");
19bc17a9 343}