sparc: Fix la_symbind for bind-now (BZ 23734)
[glibc.git] / locale / programs / ld-messages.c
blobde728f6a495b47cdc55a8d4be38c9e3fadd14621
1 /* Copyright (C) 1995-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include <langinfo.h>
22 #include <sys/types.h>
23 #include <regex.h>
24 #include <string.h>
25 #include <stdint.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 record_warning (_("\
96 No definition for %s category found"), "LC_MESSAGES");
97 messages_startup (NULL, locale, 0);
98 messages = locale->categories[LC_MESSAGES].messages;
99 nothing = 1;
103 /* The fields YESSTR and NOSTR are optional. */
104 if (messages->yesstr == NULL)
105 messages->yesstr = "";
106 if (messages->nostr == NULL)
107 messages->nostr = "";
109 if (messages->yesexpr == NULL)
111 if (! nothing)
112 record_error (0, 0, _("%s: field `%s' undefined"),
113 "LC_MESSAGES", "yesexpr");
114 messages->yesexpr = "^[yY]";
116 else if (messages->yesexpr[0] == '\0')
118 record_error (0, 0, _("\
119 %s: value for field `%s' must not be an empty string"),
120 "LC_MESSAGES", "yesexpr");
122 else
124 int result;
125 regex_t re;
127 /* Test whether it are correct regular expressions. */
128 result = regcomp (&re, messages->yesexpr, REG_EXTENDED);
129 if (result != 0 && !be_quiet)
131 char errbuf[BUFSIZ];
133 (void) regerror (result, &re, errbuf, BUFSIZ);
134 record_error (0, 0, _("\
135 %s: no correct regular expression for field `%s': %s"),
136 "LC_MESSAGES", "yesexpr", errbuf);
138 else if (result != 0)
139 regfree (&re);
142 if (messages->noexpr == NULL)
144 if (! nothing)
145 record_error (0, 0, _("%s: field `%s' undefined"),
146 "LC_MESSAGES", "noexpr");
147 messages->noexpr = "^[nN]";
149 else if (messages->noexpr[0] == '\0')
151 record_error (0, 0, _("\
152 %s: value for field `%s' must not be an empty string"),
153 "LC_MESSAGES", "noexpr");
155 else
157 int result;
158 regex_t re;
160 /* Test whether it are correct regular expressions. */
161 result = regcomp (&re, messages->noexpr, REG_EXTENDED);
162 if (result != 0 && !be_quiet)
164 char errbuf[BUFSIZ];
166 (void) regerror (result, &re, errbuf, BUFSIZ);
167 record_error (0, 0, _("\
168 %s: no correct regular expression for field `%s': %s"),
169 "LC_MESSAGES", "noexpr", errbuf);
171 else if (result != 0)
172 regfree (&re);
177 void
178 messages_output (struct localedef_t *locale, const struct charmap_t *charmap,
179 const char *output_path)
181 struct locale_messages_t *messages
182 = locale->categories[LC_MESSAGES].messages;
183 struct locale_file file;
185 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES));
186 add_locale_string (&file, messages->yesexpr);
187 add_locale_string (&file, messages->noexpr);
188 add_locale_string (&file, messages->yesstr);
189 add_locale_string (&file, messages->nostr);
190 add_locale_string (&file, charmap->code_set_name);
191 write_locale_data (output_path, LC_MESSAGES, "LC_MESSAGES", &file);
195 /* The parser for the LC_MESSAGES section of the locale definition. */
196 void
197 messages_read (struct linereader *ldfile, struct localedef_t *result,
198 const struct charmap_t *charmap, const char *repertoire_name,
199 int ignore_content)
201 struct repertoire_t *repertoire = NULL;
202 struct locale_messages_t *messages;
203 struct token *now;
204 enum token_t nowtok;
206 /* Get the repertoire we have to use. */
207 if (repertoire_name != NULL)
208 repertoire = repertoire_read (repertoire_name);
210 /* The rest of the line containing `LC_MESSAGES' must be free. */
211 lr_ignore_rest (ldfile, 1);
216 now = lr_token (ldfile, charmap, result, NULL, verbose);
217 nowtok = now->tok;
219 while (nowtok == tok_eol);
221 /* If we see `copy' now we are almost done. */
222 if (nowtok == tok_copy)
224 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_messages,
225 LC_MESSAGES, "LC_MESSAGES", ignore_content);
226 return;
229 /* Prepare the data structures. */
230 messages_startup (ldfile, result, ignore_content);
231 messages = result->categories[LC_MESSAGES].messages;
233 while (1)
235 struct token *arg;
237 /* Of course we don't proceed beyond the end of file. */
238 if (nowtok == tok_eof)
239 break;
241 /* Ignore empty lines. */
242 if (nowtok == tok_eol)
244 now = lr_token (ldfile, charmap, result, NULL, verbose);
245 nowtok = now->tok;
246 continue;
249 switch (nowtok)
251 #define STR_ELEM(cat) \
252 case tok_##cat: \
253 /* Ignore the rest of the line if we don't need the input of \
254 this line. */ \
255 if (ignore_content) \
257 lr_ignore_rest (ldfile, 0); \
258 break; \
261 if (messages->cat != NULL) \
263 lr_error (ldfile, _("\
264 %s: field `%s' declared more than once"), "LC_MESSAGES", #cat); \
265 lr_ignore_rest (ldfile, 0); \
266 break; \
268 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
269 if (now->tok != tok_string) \
270 goto syntax_error; \
271 else if (!ignore_content && now->val.str.startmb == NULL) \
273 lr_error (ldfile, _("\
274 %s: unknown character in field `%s'"), "LC_MESSAGES", #cat); \
275 messages->cat = ""; \
277 else if (!ignore_content) \
278 messages->cat = now->val.str.startmb; \
279 break
281 STR_ELEM (yesexpr);
282 STR_ELEM (noexpr);
283 STR_ELEM (yesstr);
284 STR_ELEM (nostr);
286 case tok_end:
287 /* Next we assume `LC_MESSAGES'. */
288 arg = lr_token (ldfile, charmap, result, NULL, verbose);
289 if (arg->tok == tok_eof)
290 break;
291 if (arg->tok == tok_eol)
292 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_MESSAGES");
293 else if (arg->tok != tok_lc_messages)
294 lr_error (ldfile, _("\
295 %1$s: definition does not end with `END %1$s'"), "LC_MESSAGES");
296 lr_ignore_rest (ldfile, arg->tok == tok_lc_messages);
297 return;
299 default:
300 syntax_error:
301 SYNTAX_ERROR (_("%s: syntax error"), "LC_MESSAGES");
304 /* Prepare for the next round. */
305 now = lr_token (ldfile, charmap, result, NULL, verbose);
306 nowtok = now->tok;
309 /* When we come here we reached the end of the file. */
310 lr_error (ldfile, _("%s: premature end of file"), "LC_MESSAGES");