Update.
[glibc.git] / locale / programs / ld-messages.c
blobe52f5413e2d819aa96ab8887e97d2bfa37c0d47d
1 /* Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 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. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <alloca.h>
25 #include <langinfo.h>
26 #include <string.h>
27 #include <sys/uio.h>
29 #ifdef HAVE_REGEX
30 # include <regex.h>
31 #else
32 # include <rx.h>
33 #endif
35 /* Undefine following line in production version. */
36 /* #define NDEBUG 1 */
37 #include <assert.h>
39 #include "locales.h"
40 #include "stringtrans.h"
41 #include "localeinfo.h"
44 extern void *xmalloc (size_t __n);
47 /* The real definition of the struct for the LC_MESSAGES locale. */
48 struct locale_messages_t
50 const char *yesexpr;
51 const char *noexpr;
52 const char *yesstr;
53 const char *nostr;
57 void
58 messages_startup (struct linereader *lr, struct localedef_t *locale,
59 struct charset_t *charset)
61 struct locale_messages_t *messages;
63 /* We have a definition for LC_MESSAGES. */
64 copy_posix.mask &= ~(1 << LC_MESSAGES);
66 /* It is important that we always use UCS1 encoding for strings now. */
67 encoding_method = ENC_UCS1;
69 locale->categories[LC_MESSAGES].messages = messages =
70 (struct locale_messages_t *) xmalloc (sizeof (struct locale_messages_t));
72 memset (messages, '\0', sizeof (struct locale_messages_t));
76 void
77 messages_finish (struct localedef_t *locale)
79 struct locale_messages_t *messages
80 = locale->categories[LC_MESSAGES].messages;
82 /* The fields YESSTR and NOSTR are optional. */
83 if (messages->yesexpr == NULL)
85 if (!be_quiet)
86 error (0, 0, _("field `%s' in category `%s' undefined"),
87 "yesexpr", "LC_MESSAGES");
89 else
91 int result;
92 regex_t re;
94 /* Test whether it are correct regular expressions. */
95 result = regcomp (&re, messages->yesexpr, REG_EXTENDED);
96 if (result != 0 && !be_quiet)
98 char errbuf[BUFSIZ];
100 (void) regerror (result, &re, errbuf, BUFSIZ);
101 error (0, 0, _("\
102 no correct regular expression for field `%s' in category `%s': %s"),
103 "yesexpr", "LC_MESSAGES", errbuf);
107 if (messages->noexpr == NULL)
109 if (!be_quiet)
110 error (0, 0, _("field `%s' in category `%s' undefined"),
111 "noexpr", "LC_MESSAGES");
113 else
115 int result;
116 regex_t re;
118 /* Test whether it are correct regular expressions. */
119 result = regcomp (&re, messages->noexpr, REG_EXTENDED);
120 if (result != 0 && !be_quiet)
122 char errbuf[BUFSIZ];
124 (void) regerror (result, &re, errbuf, BUFSIZ);
125 error (0, 0, _("\
126 no correct regular expression for field `%s' in category `%s': %s"),
127 "noexpr", "LC_MESSAGES", errbuf);
133 void
134 messages_output (struct localedef_t *locale, const char *output_path)
136 struct locale_messages_t *messages
137 = locale->categories[LC_MESSAGES].messages;
138 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)];
139 struct locale_file data;
140 u_int32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES)];
141 size_t cnt = 0;
143 if ((locale->binary & (1 << LC_MESSAGES)) != 0)
145 iov[0].iov_base = messages;
146 iov[0].iov_len = locale->len[LC_MESSAGES];
148 write_locale_data (output_path, "LC_MESSAGES", 1, iov);
150 return;
153 data.magic = LIMAGIC (LC_MESSAGES);
154 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES);
155 iov[cnt].iov_base = (void *) &data;
156 iov[cnt].iov_len = sizeof (data);
157 ++cnt;
159 iov[cnt].iov_base = (void *) idx;
160 iov[cnt].iov_len = sizeof (idx);
161 ++cnt;
163 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
164 iov[cnt].iov_base = (void *) (messages->yesexpr ?: "");
165 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
166 ++cnt;
168 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
169 iov[cnt].iov_base = (void *) (messages->noexpr ?: "");
170 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
171 ++cnt;
173 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
174 iov[cnt].iov_base = (void *) (messages->yesstr ?: "");
175 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
176 ++cnt;
178 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
179 iov[cnt].iov_base = (void *) (messages->nostr ?: "");
180 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
182 assert (cnt + 1 == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES));
184 write_locale_data (output_path, "LC_MESSAGES",
185 2 + _NL_ITEM_INDEX (_NL_NUM_LC_MESSAGES), iov);
189 void
190 messages_add (struct linereader *lr, struct localedef_t *locale,
191 enum token_t tok, struct token *code,
192 struct charset_t *charset)
194 struct locale_messages_t *messages
195 = locale->categories[LC_MESSAGES].messages;
197 switch (tok)
199 case tok_yesexpr:
200 if (code->val.str.start == NULL)
202 lr_error (lr, _("unknown character in field `%s' of category `%s'"),
203 "yesexpr", "LC_MESSAGES");
204 messages->yesexpr = "";
206 else
207 messages->yesexpr = code->val.str.start;
208 break;
210 case tok_noexpr:
211 if (code->val.str.start == NULL)
213 lr_error (lr, _("unknown character in field `%s' of category `%s'"),
214 "noexpr", "LC_MESSAGES");
215 messages->noexpr = "";
217 else
218 messages->noexpr = code->val.str.start;
219 break;
221 case tok_yesstr:
222 if (code->val.str.start == NULL)
224 lr_error (lr, _("unknown character in field `%s' of category `%s'"),
225 "yesstr", "LC_MESSAGES");
226 messages->yesstr = "";
228 else
229 messages->yesstr = code->val.str.start;
230 break;
232 case tok_nostr:
233 if (code->val.str.start == NULL)
235 lr_error (lr, _("unknown character in field `%s' of category `%s'"),
236 "nostr", "LC_MESSAGES");
237 messages->nostr = "";
239 else
240 messages->nostr = code->val.str.start;
241 break;
243 default:
244 assert (! "unknown token in category `LC_MESSAGES': should not happen");