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/>. */
22 #include <sys/types.h>
30 #include "localedef.h"
31 #include "linereader.h"
32 #include "localeinfo.h"
36 /* The real definition of the struct for the LC_MESSAGES locale. */
37 struct locale_messages_t
47 messages_startup (struct linereader
*lr
, struct localedef_t
*locale
,
51 locale
->categories
[LC_MESSAGES
].messages
=
52 (struct locale_messages_t
*) xcalloc (1,
53 sizeof (struct locale_messages_t
));
57 lr
->translate_strings
= 1;
58 lr
->return_widestr
= 0;
64 messages_finish (struct localedef_t
*locale
, const struct charmap_t
*charmap
)
66 struct locale_messages_t
*messages
67 = locale
->categories
[LC_MESSAGES
].messages
;
70 /* Now resolve copying and also handle completely missing definitions. */
73 /* First see whether we were supposed to copy. If yes, find the
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
96 No definition for %s category found"), "LC_MESSAGES");
97 messages_startup (NULL
, locale
, 0);
98 messages
= locale
->categories
[LC_MESSAGES
].messages
;
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
)
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");
127 /* Test whether it are correct regular expressions. */
128 result
= regcomp (&re
, messages
->yesexpr
, REG_EXTENDED
);
129 if (result
!= 0 && !be_quiet
)
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)
142 if (messages
->noexpr
== NULL
)
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");
160 /* Test whether it are correct regular expressions. */
161 result
= regcomp (&re
, messages
->noexpr
, REG_EXTENDED
);
162 if (result
!= 0 && !be_quiet
)
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)
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. */
197 messages_read (struct linereader
*ldfile
, struct localedef_t
*result
,
198 const struct charmap_t
*charmap
, const char *repertoire_name
,
201 struct repertoire_t
*repertoire
= NULL
;
202 struct locale_messages_t
*messages
;
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
);
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
);
229 /* Prepare the data structures. */
230 messages_startup (ldfile
, result
, ignore_content
);
231 messages
= result
->categories
[LC_MESSAGES
].messages
;
237 /* Of course we don't proceed beyond the end of file. */
238 if (nowtok
== tok_eof
)
241 /* Ignore empty lines. */
242 if (nowtok
== tok_eol
)
244 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
251 #define STR_ELEM(cat) \
253 /* Ignore the rest of the line if we don't need the input of \
255 if (ignore_content) \
257 lr_ignore_rest (ldfile, 0); \
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); \
268 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
269 if (now->tok != tok_string) \
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; \
287 /* Next we assume `LC_MESSAGES'. */
288 arg
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
289 if (arg
->tok
== tok_eof
)
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
);
301 SYNTAX_ERROR (_("%s: syntax error"), "LC_MESSAGES");
304 /* Prepare for the next round. */
305 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
309 /* When we come here we reached the end of the file. */
310 lr_error (ldfile
, _("%s: premature end of file"), "LC_MESSAGES");