1/* Copyright (C) 1995-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
16
17#ifdef HAVE_CONFIG_H
18# include <config.h>
19#endif
20
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>
27
28#include <assert.h>
29
30#include "localedef.h"
31#include "linereader.h"
32#include "localeinfo.h"
33#include "locfile.h"
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
46static void
47messages_startup (struct linereader *lr, struct localedef_t *locale,
48 int ignore_content)
49{
50 if (!ignore_content)
51 locale->categories[LC_MESSAGES].messages =
52 (struct locale_messages_t *) xcalloc (n: 1,
53 s: sizeof (struct locale_messages_t));
54
55 if (lr != NULL)
56 {
57 lr->translate_strings = 1;
58 lr->return_widestr = 0;
59 }
60}
61
62
63void
64messages_finish (struct localedef_t *locale, const struct charmap_t *charmap)
65{
66 struct locale_messages_t *messages
67 = locale->categories[LC_MESSAGES].messages;
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, name: from->copy_name[LC_MESSAGES],
83 repertoire_name: 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 {
95 record_warning (_("\
96No definition for %s category found"), "LC_MESSAGES");
97 messages_startup (NULL, locale, ignore_content: 0);
98 messages = locale->categories[LC_MESSAGES].messages;
99 nothing = 1;
100 }
101 }
102
103 /* The fields YESSTR and NOSTR are optional. */
104 if (messages->yesstr == NULL)
105 messages->yesstr = "";
106 if (messages->nostr == NULL)
107 messages->nostr = "";
108
109 if (messages->yesexpr == NULL)
110 {
111 if (! nothing)
112 record_error (status: 0, errnum: 0, _("%s: field `%s' undefined"),
113 "LC_MESSAGES", "yesexpr");
114 messages->yesexpr = "^[yY]";
115 }
116 else if (messages->yesexpr[0] == '\0')
117 {
118 record_error (status: 0, errnum: 0, _("\
119%s: value for field `%s' must not be an empty string"),
120 "LC_MESSAGES", "yesexpr");
121 }
122 else
123 {
124 int result;
125 regex_t re;
126
127 /* Test whether it are correct regular expressions. */
128 result = regcomp (preg: &re, pattern: messages->yesexpr, REG_EXTENDED);
129 if (result != 0 && !be_quiet)
130 {
131 char errbuf[BUFSIZ];
132
133 (void) regerror (errcode: result, preg: &re, errbuf: errbuf, BUFSIZ);
134 record_error (status: 0, errnum: 0, _("\
135%s: no correct regular expression for field `%s': %s"),
136 "LC_MESSAGES", "yesexpr", errbuf);
137 }
138 else if (result != 0)
139 regfree (preg: &re);
140 }
141
142 if (messages->noexpr == NULL)
143 {
144 if (! nothing)
145 record_error (status: 0, errnum: 0, _("%s: field `%s' undefined"),
146 "LC_MESSAGES", "noexpr");
147 messages->noexpr = "^[nN]";
148 }
149 else if (messages->noexpr[0] == '\0')
150 {
151 record_error (status: 0, errnum: 0, _("\
152%s: value for field `%s' must not be an empty string"),
153 "LC_MESSAGES", "noexpr");
154 }
155 else
156 {
157 int result;
158 regex_t re;
159
160 /* Test whether it are correct regular expressions. */
161 result = regcomp (preg: &re, pattern: messages->noexpr, REG_EXTENDED);
162 if (result != 0 && !be_quiet)
163 {
164 char errbuf[BUFSIZ];
165
166 (void) regerror (errcode: result, preg: &re, errbuf: errbuf, BUFSIZ);
167 record_error (status: 0, errnum: 0, _("\
168%s: no correct regular expression for field `%s': %s"),
169 "LC_MESSAGES", "noexpr", errbuf);
170 }
171 else if (result != 0)
172 regfree (preg: &re);
173 }
174}
175
176
177void
178messages_output (struct localedef_t *locale, const struct charmap_t *charmap,
179 const char *output_path)
180{
181 struct locale_messages_t *messages
182 = locale->categories[LC_MESSAGES].messages;
183 struct locale_file file;
184
185 init_locale_data (file: &file, _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES));
186 add_locale_string (file: &file, string: messages->yesexpr);
187 add_locale_string (file: &file, string: messages->noexpr);
188 add_locale_string (file: &file, string: messages->yesstr);
189 add_locale_string (file: &file, string: messages->nostr);
190 add_locale_string (file: &file, string: charmap->code_set_name);
191 write_locale_data (output_path, LC_MESSAGES, category: "LC_MESSAGES", file: &file);
192}
193
194
195/* The parser for the LC_MESSAGES section of the locale definition. */
196void
197messages_read (struct linereader *ldfile, struct localedef_t *result,
198 const struct charmap_t *charmap, const char *repertoire_name,
199 int ignore_content)
200{
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 (filename: repertoire_name);
209
210 /* The rest of the line containing `LC_MESSAGES' must be free. */
211 lr_ignore_rest (lr: ldfile, verbose: 1);
212
213
214 do
215 {
216 now = lr_token (lr: ldfile, charmap, locale: result, NULL, verbose);
217 nowtok = now->tok;
218 }
219 while (nowtok == tok_eol);
220
221 /* If we see `copy' now we are almost done. */
222 if (nowtok == tok_copy)
223 {
224 handle_copy (ldfile, charmap, repertoire_name, result, token: tok_lc_messages,
225 LC_MESSAGES, locale_name: "LC_MESSAGES", ignore_content);
226 return;
227 }
228
229 /* Prepare the data structures. */
230 messages_startup (lr: ldfile, locale: 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
241 /* Ignore empty lines. */
242 if (nowtok == tok_eol)
243 {
244 now = lr_token (lr: ldfile, charmap, locale: result, NULL, verbose);
245 nowtok = now->tok;
246 continue;
247 }
248
249 switch (nowtok)
250 {
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) \
256 { \
257 lr_ignore_rest (ldfile, 0); \
258 break; \
259 } \
260 \
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 } \
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) \
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'. */
288 arg = lr_token (lr: ldfile, charmap, locale: result, NULL, verbose);
289 if (arg->tok == tok_eof)
290 break;
291 if (arg->tok == tok_eol)
292 lr_error (lr: ldfile, _("%s: incomplete `END' line"), "LC_MESSAGES");
293 else if (arg->tok != tok_lc_messages)
294 lr_error (lr: ldfile, _("\
295%1$s: definition does not end with `END %1$s'"), "LC_MESSAGES");
296 lr_ignore_rest (lr: ldfile, verbose: arg->tok == tok_lc_messages);
297 return;
298
299 default:
300 syntax_error:
301 SYNTAX_ERROR (_("%s: syntax error"), "LC_MESSAGES");
302 }
303
304 /* Prepare for the next round. */
305 now = lr_token (lr: ldfile, charmap, locale: result, NULL, verbose);
306 nowtok = now->tok;
307 }
308
309 /* When we come here we reached the end of the file. */
310 lr_error (lr: ldfile, _("%s: premature end of file"), "LC_MESSAGES");
311}
312

source code of glibc/locale/programs/ld-messages.c