1 /* Copyright (C) 1995-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
5 This program is free software; you can redistribute it and/or modify
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.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
23 #include <sys/types.h>
31 #include "localedef.h"
32 #include "linereader.h"
33 #include "localeinfo.h"
37 /* The real definition of the struct for the LC_MESSAGES locale. */
38 struct locale_messages_t
48 messages_startup (struct linereader
*lr
, struct localedef_t
*locale
,
52 locale
->categories
[LC_MESSAGES
].messages
=
53 (struct locale_messages_t
*) xcalloc (1,
54 sizeof (struct locale_messages_t
));
58 lr
->translate_strings
= 1;
59 lr
->return_widestr
= 0;
65 messages_finish (struct localedef_t
*locale
, const struct charmap_t
*charmap
)
67 struct locale_messages_t
*messages
68 = locale
->categories
[LC_MESSAGES
].messages
;
71 /* Now resolve copying and also handle completely missing definitions. */
74 /* First see whether we were supposed to copy. If yes, find the
76 if (locale
->copy_name
[LC_MESSAGES
] != NULL
)
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
;
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
);
88 messages
= locale
->categories
[LC_MESSAGES
].messages
89 = from
->categories
[LC_MESSAGES
].messages
;
92 /* If there is still no definition issue an warning and create an
97 No definition for %s category found"), "LC_MESSAGES");
98 messages_startup (NULL
, locale
, 0);
99 messages
= locale
->categories
[LC_MESSAGES
].messages
;
104 /* The fields YESSTR and NOSTR are optional. */
105 if (messages
->yesstr
== NULL
)
106 messages
->yesstr
= "";
107 if (messages
->nostr
== NULL
)
108 messages
->nostr
= "";
110 if (messages
->yesexpr
== NULL
)
113 record_error (0, 0, _("%s: field `%s' undefined"),
114 "LC_MESSAGES", "yesexpr");
115 messages
->yesexpr
= "^[yY]";
117 else if (messages
->yesexpr
[0] == '\0')
119 record_error (0, 0, _("\
120 %s: value for field `%s' must not be an empty string"),
121 "LC_MESSAGES", "yesexpr");
128 /* Test whether it are correct regular expressions. */
129 result
= regcomp (&re
, messages
->yesexpr
, REG_EXTENDED
);
130 if (result
!= 0 && !be_quiet
)
134 (void) regerror (result
, &re
, errbuf
, BUFSIZ
);
135 record_error (0, 0, _("\
136 %s: no correct regular expression for field `%s': %s"),
137 "LC_MESSAGES", "yesexpr", errbuf
);
139 else if (result
!= 0)
143 if (messages
->noexpr
== NULL
)
146 record_error (0, 0, _("%s: field `%s' undefined"),
147 "LC_MESSAGES", "noexpr");
148 messages
->noexpr
= "^[nN]";
150 else if (messages
->noexpr
[0] == '\0')
152 record_error (0, 0, _("\
153 %s: value for field `%s' must not be an empty string"),
154 "LC_MESSAGES", "noexpr");
161 /* Test whether it are correct regular expressions. */
162 result
= regcomp (&re
, messages
->noexpr
, REG_EXTENDED
);
163 if (result
!= 0 && !be_quiet
)
167 (void) regerror (result
, &re
, errbuf
, BUFSIZ
);
168 record_error (0, 0, _("\
169 %s: no correct regular expression for field `%s': %s"),
170 "LC_MESSAGES", "noexpr", errbuf
);
172 else if (result
!= 0)
179 messages_output (struct localedef_t
*locale
, const struct charmap_t
*charmap
,
180 const char *output_path
)
182 struct locale_messages_t
*messages
183 = locale
->categories
[LC_MESSAGES
].messages
;
184 struct locale_file file
;
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
);
196 /* The parser for the LC_MESSAGES section of the locale definition. */
198 messages_read (struct linereader
*ldfile
, struct localedef_t
*result
,
199 const struct charmap_t
*charmap
, const char *repertoire_name
,
202 struct repertoire_t
*repertoire
= NULL
;
203 struct locale_messages_t
*messages
;
207 /* Get the repertoire we have to use. */
208 if (repertoire_name
!= NULL
)
209 repertoire
= repertoire_read (repertoire_name
);
211 /* The rest of the line containing `LC_MESSAGES' must be free. */
212 lr_ignore_rest (ldfile
, 1);
217 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
220 while (nowtok
== tok_eol
);
222 /* If we see `copy' now we are almost done. */
223 if (nowtok
== tok_copy
)
225 handle_copy (ldfile
, charmap
, repertoire_name
, result
, tok_lc_messages
,
226 LC_MESSAGES
, "LC_MESSAGES", ignore_content
);
230 /* Prepare the data structures. */
231 messages_startup (ldfile
, result
, ignore_content
);
232 messages
= result
->categories
[LC_MESSAGES
].messages
;
238 /* Of course we don't proceed beyond the end of file. */
239 if (nowtok
== tok_eof
)
242 /* Ignore empty lines. */
243 if (nowtok
== tok_eol
)
245 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
252 #define STR_ELEM(cat) \
254 /* Ignore the rest of the line if we don't need the input of \
256 if (ignore_content) \
258 lr_ignore_rest (ldfile, 0); \
262 if (messages->cat != NULL) \
264 lr_error (ldfile, _("\
265 %s: field `%s' declared more than once"), "LC_MESSAGES", #cat); \
266 lr_ignore_rest (ldfile, 0); \
269 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
270 if (now->tok != tok_string) \
272 else if (!ignore_content && now->val.str.startmb == NULL) \
274 lr_error (ldfile, _("\
275 %s: unknown character in field `%s'"), "LC_MESSAGES", #cat); \
276 messages->cat = ""; \
278 else if (!ignore_content) \
279 messages->cat = now->val.str.startmb; \
288 /* Next we assume `LC_MESSAGES'. */
289 arg
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
290 if (arg
->tok
== tok_eof
)
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
);
302 SYNTAX_ERROR (_("%s: syntax error"), "LC_MESSAGES");
305 /* Prepare for the next round. */
306 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
310 /* When we come here we reached the end of the file. */
311 lr_error (ldfile
, _("%s: premature end of file"), "LC_MESSAGES");