1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
25 #include <sys/types.h>
32 #include "linereader.h"
33 #include "localedef.h"
34 #include "localeinfo.h"
38 /* The real definition of the struct for the LC_MESSAGES locale. */
39 struct locale_messages_t
49 messages_startup (struct linereader
*lr
, struct localedef_t
*locale
,
53 locale
->categories
[LC_MESSAGES
].messages
=
54 (struct locale_messages_t
*) xcalloc (1,
55 sizeof (struct locale_messages_t
));
59 lr
->translate_strings
= 1;
60 lr
->return_widestr
= 0;
66 messages_finish (struct localedef_t
*locale
, struct charmap_t
*charmap
)
68 struct locale_messages_t
*messages
69 = locale
->categories
[LC_MESSAGES
].messages
;
72 /* Now resolve copying and also handle completely missing definitions. */
75 /* First see whether we were supposed to copy. If yes, find the
77 if (locale
->copy_name
[LC_MESSAGES
] != NULL
)
79 /* Find the copying locale. This has to happen transitively since
80 the locale we are copying from might also copying another one. */
81 struct localedef_t
*from
= locale
;
84 from
= find_locale (LC_MESSAGES
, from
->copy_name
[LC_MESSAGES
],
85 from
->repertoire_name
, charmap
);
86 while (from
->categories
[LC_MESSAGES
].messages
== NULL
87 && from
->copy_name
[LC_MESSAGES
] != NULL
);
89 messages
= locale
->categories
[LC_MESSAGES
].messages
90 = from
->categories
[LC_MESSAGES
].messages
;
93 /* If there is still no definition issue an warning and create an
97 error (0, 0, _("No definition for %s category found"),
99 messages_startup (NULL
, locale
, 0);
100 messages
= locale
->categories
[LC_MESSAGES
].messages
;
105 /* The fields YESSTR and NOSTR are optional. */
106 if (messages
->yesstr
== NULL
)
107 messages
->yesstr
= "";
108 if (messages
->nostr
== NULL
)
109 messages
->nostr
= "";
111 if (messages
->yesexpr
== NULL
)
113 if (! be_quiet
&& ! nothing
)
114 error (0, 0, _("%s: field `%s' undefined"), "LC_MESSAGES", "yesexpr");
115 messages
->yesexpr
= "";
117 else if (messages
->yesexpr
[0] == '\0')
121 %s: value for field `%s' must not be an empty string"),
122 "LC_MESSAGES", "yesexpr");
129 /* Test whether it are correct regular expressions. */
130 result
= regcomp (&re
, messages
->yesexpr
, REG_EXTENDED
);
131 if (result
!= 0 && !be_quiet
)
135 (void) regerror (result
, &re
, errbuf
, BUFSIZ
);
137 %s: no correct regular expression for field `%s': %s"),
138 "LC_MESSAGES", "yesexpr", errbuf
);
140 else if (result
!= 0)
144 if (messages
->noexpr
== NULL
)
146 if (! be_quiet
&& ! nothing
)
147 error (0, 0, _("%s: field `%s' undefined"), "LC_MESSAGES", "noexpr");
148 messages
->noexpr
= "";
150 else if (messages
->noexpr
[0] == '\0')
154 %s: value for field `%s' must not be an empty string"),
155 "LC_MESSAGES", "noexpr");
162 /* Test whether it are correct regular expressions. */
163 result
= regcomp (&re
, messages
->noexpr
, REG_EXTENDED
);
164 if (result
!= 0 && !be_quiet
)
168 (void) regerror (result
, &re
, errbuf
, BUFSIZ
);
170 %s: no correct regular expression for field `%s': %s"),
171 "LC_MESSAGES", "noexpr", errbuf
);
173 else if (result
!= 0)
180 messages_output (struct localedef_t
*locale
, struct charmap_t
*charmap
,
181 const char *output_path
)
183 struct locale_messages_t
*messages
184 = locale
->categories
[LC_MESSAGES
].messages
;
185 struct iovec iov
[2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES
)];
186 struct locale_file data
;
187 uint32_t idx
[_NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES
)];
190 data
.magic
= LIMAGIC (LC_MESSAGES
);
191 data
.n
= _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES
);
192 iov
[cnt
].iov_base
= (void *) &data
;
193 iov
[cnt
].iov_len
= sizeof (data
);
196 iov
[cnt
].iov_base
= (void *) idx
;
197 iov
[cnt
].iov_len
= sizeof (idx
);
200 idx
[cnt
- 2] = iov
[0].iov_len
+ iov
[1].iov_len
;
201 iov
[cnt
].iov_base
= (char *) messages
->yesexpr
;
202 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
205 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
206 iov
[cnt
].iov_base
= (char *) messages
->noexpr
;
207 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
210 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
211 iov
[cnt
].iov_base
= (char *) messages
->yesstr
;
212 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
215 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
216 iov
[cnt
].iov_base
= (char *) messages
->nostr
;
217 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
219 assert (cnt
+ 1 == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES
));
221 write_locale_data (output_path
, "LC_MESSAGES",
222 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES
), iov
);
226 /* The parser for the LC_MESSAGES section of the locale definition. */
228 messages_read (struct linereader
*ldfile
, struct localedef_t
*result
,
229 struct charmap_t
*charmap
, const char *repertoire_name
,
232 struct repertoire_t
*repertoire
= NULL
;
233 struct locale_messages_t
*messages
;
237 /* Get the repertoire we have to use. */
238 if (repertoire_name
!= NULL
)
239 repertoire
= repertoire_read (repertoire_name
);
241 /* The rest of the line containing `LC_MESSAGES' must be free. */
242 lr_ignore_rest (ldfile
, 1);
247 now
= lr_token (ldfile
, charmap
, NULL
);
250 while (nowtok
== tok_eol
);
252 /* If we see `copy' now we are almost done. */
253 if (nowtok
== tok_copy
)
255 handle_copy (ldfile
, charmap
, repertoire
, result
, tok_lc_messages
,
256 LC_MESSAGES
, "LC_MESSAGES", ignore_content
);
260 /* Prepare the data structures. */
261 messages_startup (ldfile
, result
, ignore_content
);
262 messages
= result
->categories
[LC_MESSAGES
].messages
;
268 /* Of course we don't proceed beyond the end of file. */
269 if (nowtok
== tok_eof
)
272 /* Ignore empty lines. */
273 if (nowtok
== tok_eol
)
275 now
= lr_token (ldfile
, charmap
, NULL
);
282 #define STR_ELEM(cat) \
284 /* Ignore the rest of the line if we don't need the input of \
286 if (ignore_content) \
288 lr_ignore_rest (ldfile, 0); \
292 if (messages->cat != NULL) \
294 lr_error (ldfile, _("\
295 %s: field `%s' declared more than once"), "LC_MESSAGES", #cat); \
296 lr_ignore_rest (ldfile, 0); \
299 now = lr_token (ldfile, charmap, repertoire); \
300 if (now->tok != tok_string) \
302 else if (!ignore_content && now->val.str.startmb == NULL) \
304 lr_error (ldfile, _("\
305 %s: unknown character in field `%s'"), "LC_MESSAGES", #cat); \
306 messages->cat = ""; \
308 else if (!ignore_content) \
309 messages->cat = now->val.str.startmb; \
318 /* Next we assume `LC_MESSAGES'. */
319 arg
= lr_token (ldfile
, charmap
, NULL
);
320 if (arg
->tok
== tok_eof
)
322 if (arg
->tok
== tok_eol
)
323 lr_error (ldfile
, _("%s: incomplete `END' line"), "LC_MESSAGES");
324 else if (arg
->tok
!= tok_lc_messages
)
325 lr_error (ldfile
, _("\
326 %1$s: definition does not end with `END %1$s'"), "LC_MESSAGES");
327 lr_ignore_rest (ldfile
, arg
->tok
== tok_lc_messages
);
332 SYNTAX_ERROR (_("%s: syntax error"), "LC_MESSAGES");
335 /* Prepare for the next round. */
336 now
= lr_token (ldfile
, charmap
, NULL
);
340 /* When we come here we reached the end of the file. */
341 lr_error (ldfile
, _("%s: premature end of file"), "LC_MESSAGES");