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