1 /* Copyright (C) 1995-1999,2000,2001,2002,2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
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.
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.
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/>. */
23 #include <sys/types.h>
30 #include "localedef.h"
31 #include "linereader.h"
32 #include "localeinfo.h"
36 /* The real definition of the struct for the LC_MESSAGES locale. */
37 struct locale_messages_t
47 messages_startup (struct linereader
*lr
, struct localedef_t
*locale
,
51 locale
->categories
[LC_MESSAGES
].messages
=
52 (struct locale_messages_t
*) xcalloc (1,
53 sizeof (struct locale_messages_t
));
57 lr
->translate_strings
= 1;
58 lr
->return_widestr
= 0;
64 messages_finish (struct localedef_t
*locale
, const struct charmap_t
*charmap
)
66 struct locale_messages_t
*messages
67 = locale
->categories
[LC_MESSAGES
].messages
;
70 /* Now resolve copying and also handle completely missing definitions. */
73 /* First see whether we were supposed to copy. If yes, find the
75 if (locale
->copy_name
[LC_MESSAGES
] != NULL
)
77 /* Find the copying locale. This has to happen transitively since
78 the locale we are copying from might also copying another one. */
79 struct localedef_t
*from
= locale
;
82 from
= find_locale (LC_MESSAGES
, from
->copy_name
[LC_MESSAGES
],
83 from
->repertoire_name
, charmap
);
84 while (from
->categories
[LC_MESSAGES
].messages
== NULL
85 && from
->copy_name
[LC_MESSAGES
] != NULL
);
87 messages
= locale
->categories
[LC_MESSAGES
].messages
88 = from
->categories
[LC_MESSAGES
].messages
;
91 /* If there is still no definition issue an warning and create an
96 WITH_CUR_LOCALE (error (0, 0, _("\
97 No definition for %s category found"), "LC_MESSAGES"));
98 messages_startup (NULL
, locale
, 0);
99 messages
= locale
->categories
[LC_MESSAGES
].messages
;
104 /* The fields YESSTR and NOSTR are optional. */
105 if (messages
->yesstr
== NULL
)
106 messages
->yesstr
= "";
107 if (messages
->nostr
== NULL
)
108 messages
->nostr
= "";
110 if (messages
->yesexpr
== NULL
)
112 if (! be_quiet
&& ! nothing
)
113 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' undefined"),
114 "LC_MESSAGES", "yesexpr"));
115 messages
->yesexpr
= "^[yY]";
117 else if (messages
->yesexpr
[0] == '\0')
120 WITH_CUR_LOCALE (error (0, 0, _("\
121 %s: value for field `%s' must not be an empty string"),
122 "LC_MESSAGES", "yesexpr"));
129 /* Test whether it are correct regular expressions. */
130 result
= regcomp (&re
, messages
->yesexpr
, REG_EXTENDED
);
131 if (result
!= 0 && !be_quiet
)
135 (void) regerror (result
, &re
, errbuf
, BUFSIZ
);
136 WITH_CUR_LOCALE (error (0, 0, _("\
137 %s: no correct regular expression for field `%s': %s"),
138 "LC_MESSAGES", "yesexpr", errbuf
));
140 else if (result
!= 0)
144 if (messages
->noexpr
== NULL
)
146 if (! be_quiet
&& ! nothing
)
147 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' undefined"),
148 "LC_MESSAGES", "noexpr"));
149 messages
->noexpr
= "^[nN]";
151 else if (messages
->noexpr
[0] == '\0')
154 WITH_CUR_LOCALE (error (0, 0, _("\
155 %s: value for field `%s' must not be an empty string"),
156 "LC_MESSAGES", "noexpr"));
163 /* Test whether it are correct regular expressions. */
164 result
= regcomp (&re
, messages
->noexpr
, REG_EXTENDED
);
165 if (result
!= 0 && !be_quiet
)
169 (void) regerror (result
, &re
, errbuf
, BUFSIZ
);
170 WITH_CUR_LOCALE (error (0, 0, _("\
171 %s: no correct regular expression for field `%s': %s"),
172 "LC_MESSAGES", "noexpr", errbuf
));
174 else if (result
!= 0)
181 messages_output (struct localedef_t
*locale
, const struct charmap_t
*charmap
,
182 const char *output_path
)
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
;
188 uint32_t idx
[_NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES
)];
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
);
197 iov
[cnt
].iov_base
= (void *) idx
;
198 iov
[cnt
].iov_len
= sizeof (idx
);
201 idx
[cnt
- 2] = iov
[0].iov_len
+ iov
[1].iov_len
;
202 iov
[cnt
].iov_base
= (char *) messages
->yesexpr
;
203 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
206 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
207 iov
[cnt
].iov_base
= (char *) messages
->noexpr
;
208 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
211 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
212 iov
[cnt
].iov_base
= (char *) messages
->yesstr
;
213 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
216 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
217 iov
[cnt
].iov_base
= (char *) messages
->nostr
;
218 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
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;
225 assert (cnt
+ 1 == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES
));
227 write_locale_data (output_path
, LC_MESSAGES
, "LC_MESSAGES",
228 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES
), iov
);
232 /* The parser for the LC_MESSAGES section of the locale definition. */
234 messages_read (struct linereader
*ldfile
, struct localedef_t
*result
,
235 const struct charmap_t
*charmap
, const char *repertoire_name
,
238 struct repertoire_t
*repertoire
= NULL
;
239 struct locale_messages_t
*messages
;
243 /* Get the repertoire we have to use. */
244 if (repertoire_name
!= NULL
)
245 repertoire
= repertoire_read (repertoire_name
);
247 /* The rest of the line containing `LC_MESSAGES' must be free. */
248 lr_ignore_rest (ldfile
, 1);
253 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
256 while (nowtok
== tok_eol
);
258 /* If we see `copy' now we are almost done. */
259 if (nowtok
== tok_copy
)
261 handle_copy (ldfile
, charmap
, repertoire_name
, result
, tok_lc_messages
,
262 LC_MESSAGES
, "LC_MESSAGES", ignore_content
);
266 /* Prepare the data structures. */
267 messages_startup (ldfile
, result
, ignore_content
);
268 messages
= result
->categories
[LC_MESSAGES
].messages
;
274 /* Of course we don't proceed beyond the end of file. */
275 if (nowtok
== tok_eof
)
278 /* Ignore empty lines. */
279 if (nowtok
== tok_eol
)
281 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
288 #define STR_ELEM(cat) \
290 /* Ignore the rest of the line if we don't need the input of \
292 if (ignore_content) \
294 lr_ignore_rest (ldfile, 0); \
298 if (messages->cat != NULL) \
300 lr_error (ldfile, _("\
301 %s: field `%s' declared more than once"), "LC_MESSAGES", #cat); \
302 lr_ignore_rest (ldfile, 0); \
305 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
306 if (now->tok != tok_string) \
308 else if (!ignore_content && now->val.str.startmb == NULL) \
310 lr_error (ldfile, _("\
311 %s: unknown character in field `%s'"), "LC_MESSAGES", #cat); \
312 messages->cat = ""; \
314 else if (!ignore_content) \
315 messages->cat = now->val.str.startmb; \
324 /* Next we assume `LC_MESSAGES'. */
325 arg
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
326 if (arg
->tok
== tok_eof
)
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
);
338 SYNTAX_ERROR (_("%s: syntax error"), "LC_MESSAGES");
341 /* Prepare for the next round. */
342 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
346 /* When we come here we reached the end of the file. */
347 lr_error (ldfile
, _("%s: premature end of file"), "LC_MESSAGES");