PowerPC: remove branch prediction from rint implementation
[glibc.git] / locale / programs / ld-messages.c
blob87f3a1d027a863b3504a1e50576876ae689fe1dd
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/>. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <langinfo.h>
23 #include <sys/types.h>
24 #include <regex.h>
25 #include <string.h>
26 #include <sys/uio.h>
28 #include <assert.h>
30 #include "localedef.h"
31 #include "linereader.h"
32 #include "localeinfo.h"
33 #include "locfile.h"
36 /* The real definition of the struct for the LC_MESSAGES locale. */
37 struct locale_messages_t
39 const char *yesexpr;
40 const char *noexpr;
41 const char *yesstr;
42 const char *nostr;
46 static void
47 messages_startup (struct linereader *lr, struct localedef_t *locale,
48 int ignore_content)
50 if (!ignore_content)
51 locale->categories[LC_MESSAGES].messages =
52 (struct locale_messages_t *) xcalloc (1,
53 sizeof (struct locale_messages_t));
55 if (lr != NULL)
57 lr->translate_strings = 1;
58 lr->return_widestr = 0;
63 void
64 messages_finish (struct localedef_t *locale, const struct charmap_t *charmap)
66 struct locale_messages_t *messages
67 = locale->categories[LC_MESSAGES].messages;
68 int nothing = 0;
70 /* Now resolve copying and also handle completely missing definitions. */
71 if (messages == NULL)
73 /* First see whether we were supposed to copy. If yes, find the
74 actual definition. */
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
92 empty one. */
93 if (messages == NULL)
95 if (! be_quiet)
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;
100 nothing = 1;
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')
119 if (!be_quiet)
120 WITH_CUR_LOCALE (error (0, 0, _("\
121 %s: value for field `%s' must not be an empty string"),
122 "LC_MESSAGES", "yesexpr"));
124 else
126 int result;
127 regex_t re;
129 /* Test whether it are correct regular expressions. */
130 result = regcomp (&re, messages->yesexpr, REG_EXTENDED);
131 if (result != 0 && !be_quiet)
133 char errbuf[BUFSIZ];
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)
141 regfree (&re);
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')
153 if (!be_quiet)
154 WITH_CUR_LOCALE (error (0, 0, _("\
155 %s: value for field `%s' must not be an empty string"),
156 "LC_MESSAGES", "noexpr"));
158 else
160 int result;
161 regex_t re;
163 /* Test whether it are correct regular expressions. */
164 result = regcomp (&re, messages->noexpr, REG_EXTENDED);
165 if (result != 0 && !be_quiet)
167 char errbuf[BUFSIZ];
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)
175 regfree (&re);
180 void
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)];
189 size_t cnt = 0;
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;
197 iov[cnt].iov_base = (void *) idx;
198 iov[cnt].iov_len = sizeof (idx);
199 ++cnt;
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;
204 ++cnt;
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;
209 ++cnt;
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;
214 ++cnt;
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;
219 ++cnt;
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. */
233 void
234 messages_read (struct linereader *ldfile, struct localedef_t *result,
235 const struct charmap_t *charmap, const char *repertoire_name,
236 int ignore_content)
238 struct repertoire_t *repertoire = NULL;
239 struct locale_messages_t *messages;
240 struct token *now;
241 enum token_t nowtok;
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);
254 nowtok = now->tok;
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);
263 return;
266 /* Prepare the data structures. */
267 messages_startup (ldfile, result, ignore_content);
268 messages = result->categories[LC_MESSAGES].messages;
270 while (1)
272 struct token *arg;
274 /* Of course we don't proceed beyond the end of file. */
275 if (nowtok == tok_eof)
276 break;
278 /* Ignore empty lines. */
279 if (nowtok == tok_eol)
281 now = lr_token (ldfile, charmap, result, NULL, verbose);
282 nowtok = now->tok;
283 continue;
286 switch (nowtok)
288 #define STR_ELEM(cat) \
289 case tok_##cat: \
290 /* Ignore the rest of the line if we don't need the input of \
291 this line. */ \
292 if (ignore_content) \
294 lr_ignore_rest (ldfile, 0); \
295 break; \
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); \
303 break; \
305 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
306 if (now->tok != tok_string) \
307 goto syntax_error; \
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; \
316 break
318 STR_ELEM (yesexpr);
319 STR_ELEM (noexpr);
320 STR_ELEM (yesstr);
321 STR_ELEM (nostr);
323 case tok_end:
324 /* Next we assume `LC_MESSAGES'. */
325 arg = lr_token (ldfile, charmap, result, NULL, verbose);
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;
336 default:
337 syntax_error:
338 SYNTAX_ERROR (_("%s: syntax error"), "LC_MESSAGES");
341 /* Prepare for the next round. */
342 now = lr_token (ldfile, charmap, result, NULL, verbose);
343 nowtok = now->tok;
346 /* When we come here we reached the end of the file. */
347 lr_error (ldfile, _("%s: premature end of file"), "LC_MESSAGES");