]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/programs/ld-messages.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / locale / programs / ld-messages.c
CommitLineData
dff8da6b 1/* Copyright (C) 1995-2024 Free Software Foundation, Inc.
c84142e8 2 This file is part of the GNU C Library.
19bc17a9 3
43bc8ac6 4 This program is free software; you can redistribute it and/or modify
2e2efe65
RM
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.
19bc17a9 8
43bc8ac6 9 This program is distributed in the hope that it will be useful,
c84142e8 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
43bc8ac6
UD
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
19bc17a9 13
43bc8ac6 14 You should have received a copy of the GNU General Public License
5a82c748 15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
19bc17a9
RM
16
17#ifdef HAVE_CONFIG_H
18# include <config.h>
19#endif
20
19bc17a9 21#include <langinfo.h>
4b10dd6c
UD
22#include <sys/types.h>
23#include <regex.h>
19bc17a9 24#include <string.h>
e054f494 25#include <stdint.h>
19bc17a9
RM
26#include <sys/uio.h>
27
19bc17a9
RM
28#include <assert.h>
29
4b10dd6c 30#include "localedef.h"
f2b98f97 31#include "linereader.h"
19bc17a9 32#include "localeinfo.h"
4b10dd6c 33#include "locfile.h"
19bc17a9
RM
34
35
36/* The real definition of the struct for the LC_MESSAGES locale. */
37struct locale_messages_t
38{
39 const char *yesexpr;
40 const char *noexpr;
41 const char *yesstr;
42 const char *nostr;
43};
44
45
4b10dd6c 46static void
19bc17a9 47messages_startup (struct linereader *lr, struct localedef_t *locale,
4b10dd6c 48 int ignore_content)
19bc17a9 49{
4b10dd6c
UD
50 if (!ignore_content)
51 locale->categories[LC_MESSAGES].messages =
52 (struct locale_messages_t *) xcalloc (1,
53 sizeof (struct locale_messages_t));
19bc17a9 54
b9eb05d6
UD
55 if (lr != NULL)
56 {
57 lr->translate_strings = 1;
58 lr->return_widestr = 0;
59 }
19bc17a9
RM
60}
61
62
63void
47e8b443 64messages_finish (struct localedef_t *locale, const struct charmap_t *charmap)
19bc17a9
RM
65{
66 struct locale_messages_t *messages
67 = locale->categories[LC_MESSAGES].messages;
b9eb05d6
UD
68 int nothing = 0;
69
70 /* Now resolve copying and also handle completely missing definitions. */
71 if (messages == NULL)
72 {
73 /* First see whether we were supposed to copy. If yes, find the
74 actual definition. */
75 if (locale->copy_name[LC_MESSAGES] != NULL)
76 {
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;
80
81 do
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);
86
87 messages = locale->categories[LC_MESSAGES].messages
88 = from->categories[LC_MESSAGES].messages;
89 }
90
91 /* If there is still no definition issue an warning and create an
92 empty one. */
93 if (messages == NULL)
94 {
f16491eb
CD
95 record_warning (_("\
96No definition for %s category found"), "LC_MESSAGES");
b9eb05d6
UD
97 messages_startup (NULL, locale, 0);
98 messages = locale->categories[LC_MESSAGES].messages;
99 nothing = 1;
100 }
101 }
19bc17a9
RM
102
103 /* The fields YESSTR and NOSTR are optional. */
4b10dd6c
UD
104 if (messages->yesstr == NULL)
105 messages->yesstr = "";
106 if (messages->nostr == NULL)
107 messages->nostr = "";
108
880f421f
UD
109 if (messages->yesexpr == NULL)
110 {
f16491eb
CD
111 if (! nothing)
112 record_error (0, 0, _("%s: field `%s' undefined"),
113 "LC_MESSAGES", "yesexpr");
ba5566a7 114 messages->yesexpr = "^[yY]";
4b10dd6c
UD
115 }
116 else if (messages->yesexpr[0] == '\0')
117 {
f16491eb 118 record_error (0, 0, _("\
4b10dd6c 119%s: value for field `%s' must not be an empty string"),
f16491eb 120 "LC_MESSAGES", "yesexpr");
880f421f 121 }
19bc17a9
RM
122 else
123 {
124 int result;
125 regex_t re;
126
4b10dd6c 127 /* Test whether it are correct regular expressions. */
19bc17a9 128 result = regcomp (&re, messages->yesexpr, REG_EXTENDED);
c84142e8 129 if (result != 0 && !be_quiet)
19bc17a9
RM
130 {
131 char errbuf[BUFSIZ];
132
133 (void) regerror (result, &re, errbuf, BUFSIZ);
f16491eb 134 record_error (0, 0, _("\
4b10dd6c 135%s: no correct regular expression for field `%s': %s"),
f16491eb 136 "LC_MESSAGES", "yesexpr", errbuf);
19bc17a9 137 }
4b10dd6c
UD
138 else if (result != 0)
139 regfree (&re);
19bc17a9
RM
140 }
141
880f421f
UD
142 if (messages->noexpr == NULL)
143 {
f16491eb
CD
144 if (! nothing)
145 record_error (0, 0, _("%s: field `%s' undefined"),
146 "LC_MESSAGES", "noexpr");
ba5566a7 147 messages->noexpr = "^[nN]";
4b10dd6c
UD
148 }
149 else if (messages->noexpr[0] == '\0')
150 {
f16491eb 151 record_error (0, 0, _("\
4b10dd6c 152%s: value for field `%s' must not be an empty string"),
f16491eb 153 "LC_MESSAGES", "noexpr");
880f421f 154 }
19bc17a9
RM
155 else
156 {
157 int result;
158 regex_t re;
159
4b10dd6c 160 /* Test whether it are correct regular expressions. */
19bc17a9 161 result = regcomp (&re, messages->noexpr, REG_EXTENDED);
c84142e8 162 if (result != 0 && !be_quiet)
19bc17a9
RM
163 {
164 char errbuf[BUFSIZ];
165
166 (void) regerror (result, &re, errbuf, BUFSIZ);
f16491eb 167 record_error (0, 0, _("\
4b10dd6c 168%s: no correct regular expression for field `%s': %s"),
f16491eb 169 "LC_MESSAGES", "noexpr", errbuf);
19bc17a9 170 }
4b10dd6c
UD
171 else if (result != 0)
172 regfree (&re);
19bc17a9
RM
173 }
174}
175
176
177void
47e8b443 178messages_output (struct localedef_t *locale, const struct charmap_t *charmap,
4b10dd6c 179 const char *output_path)
19bc17a9
RM
180{
181 struct locale_messages_t *messages
182 = locale->categories[LC_MESSAGES].messages;
1ecbb381
RS
183 struct locale_file file;
184
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);
19bc17a9
RM
192}
193
194
4b10dd6c 195/* The parser for the LC_MESSAGES section of the locale definition. */
19bc17a9 196void
4b10dd6c 197messages_read (struct linereader *ldfile, struct localedef_t *result,
47e8b443 198 const struct charmap_t *charmap, const char *repertoire_name,
4b10dd6c 199 int ignore_content)
19bc17a9 200{
4b10dd6c
UD
201 struct repertoire_t *repertoire = NULL;
202 struct locale_messages_t *messages;
203 struct token *now;
204 enum token_t nowtok;
205
206 /* Get the repertoire we have to use. */
207 if (repertoire_name != NULL)
208 repertoire = repertoire_read (repertoire_name);
19bc17a9 209
4b10dd6c
UD
210 /* The rest of the line containing `LC_MESSAGES' must be free. */
211 lr_ignore_rest (ldfile, 1);
212
213
214 do
19bc17a9 215 {
47e8b443 216 now = lr_token (ldfile, charmap, result, NULL, verbose);
4b10dd6c
UD
217 nowtok = now->tok;
218 }
219 while (nowtok == tok_eol);
19bc17a9 220
4b10dd6c
UD
221 /* If we see `copy' now we are almost done. */
222 if (nowtok == tok_copy)
223 {
01ff9d0b 224 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_messages,
b9eb05d6 225 LC_MESSAGES, "LC_MESSAGES", ignore_content);
4b10dd6c
UD
226 return;
227 }
19bc17a9 228
4b10dd6c
UD
229 /* Prepare the data structures. */
230 messages_startup (ldfile, result, ignore_content);
231 messages = result->categories[LC_MESSAGES].messages;
232
233 while (1)
234 {
235 struct token *arg;
236
237 /* Of course we don't proceed beyond the end of file. */
238 if (nowtok == tok_eof)
239 break;
240
42d7c593 241 /* Ignore empty lines. */
4b10dd6c 242 if (nowtok == tok_eol)
19bc17a9 243 {
47e8b443 244 now = lr_token (ldfile, charmap, result, NULL, verbose);
4b10dd6c
UD
245 nowtok = now->tok;
246 continue;
19bc17a9 247 }
19bc17a9 248
4b10dd6c 249 switch (nowtok)
19bc17a9 250 {
4b10dd6c
UD
251#define STR_ELEM(cat) \
252 case tok_##cat: \
b9eb05d6
UD
253 /* Ignore the rest of the line if we don't need the input of \
254 this line. */ \
255 if (ignore_content) \
256 { \
257 lr_ignore_rest (ldfile, 0); \
258 break; \
259 } \
260 \
4b10dd6c
UD
261 if (messages->cat != NULL) \
262 { \
263 lr_error (ldfile, _("\
264%s: field `%s' declared more than once"), "LC_MESSAGES", #cat); \
265 lr_ignore_rest (ldfile, 0); \
266 break; \
267 } \
47e8b443 268 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
4b10dd6c
UD
269 if (now->tok != tok_string) \
270 goto syntax_error; \
271 else if (!ignore_content && now->val.str.startmb == NULL) \
272 { \
273 lr_error (ldfile, _("\
274%s: unknown character in field `%s'"), "LC_MESSAGES", #cat); \
275 messages->cat = ""; \
276 } \
277 else if (!ignore_content) \
278 messages->cat = now->val.str.startmb; \
279 break
280
281 STR_ELEM (yesexpr);
282 STR_ELEM (noexpr);
283 STR_ELEM (yesstr);
284 STR_ELEM (nostr);
285
286 case tok_end:
287 /* Next we assume `LC_MESSAGES'. */
47e8b443 288 arg = lr_token (ldfile, charmap, result, NULL, verbose);
4b10dd6c
UD
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;
298
299 default:
300 syntax_error:
301 SYNTAX_ERROR (_("%s: syntax error"), "LC_MESSAGES");
19bc17a9 302 }
19bc17a9 303
4b10dd6c 304 /* Prepare for the next round. */
47e8b443 305 now = lr_token (ldfile, charmap, result, NULL, verbose);
4b10dd6c 306 nowtok = now->tok;
19bc17a9 307 }
4b10dd6c
UD
308
309 /* When we come here we reached the end of the file. */
310 lr_error (ldfile, _("%s: premature end of file"), "LC_MESSAGES");
19bc17a9 311}