.
[glibc/history.git] / locale / programs / ld-name.c
blobc1153fd51d0403c2013ea42e6844b38a80b9f1f1
1 /* Copyright (C) 1998,1999,2000,2001,2002,2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <langinfo.h>
23 #include <string.h>
24 #include <sys/uio.h>
26 #include <assert.h>
28 #include "localedef.h"
29 #include "localeinfo.h"
30 #include "locfile.h"
33 /* The real definition of the struct for the LC_NAME locale. */
34 struct locale_name_t
36 const char *name_fmt;
37 const char *name_gen;
38 const char *name_mr;
39 const char *name_mrs;
40 const char *name_miss;
41 const char *name_ms;
45 static void
46 name_startup (struct linereader *lr, struct localedef_t *locale,
47 int ignore_content)
49 if (!ignore_content)
50 locale->categories[LC_NAME].name =
51 (struct locale_name_t *) xcalloc (1, sizeof (struct locale_name_t));
53 if (lr != NULL)
55 lr->translate_strings = 1;
56 lr->return_widestr = 0;
61 void
62 name_finish (struct localedef_t *locale, const struct charmap_t *charmap)
64 struct locale_name_t *name = locale->categories[LC_NAME].name;
65 int nothing = 0;
67 /* Now resolve copying and also handle completely missing definitions. */
68 if (name == NULL)
70 /* First see whether we were supposed to copy. If yes, find the
71 actual definition. */
72 if (locale->copy_name[LC_NAME] != NULL)
74 /* Find the copying locale. This has to happen transitively since
75 the locale we are copying from might also copying another one. */
76 struct localedef_t *from = locale;
79 from = find_locale (LC_NAME, from->copy_name[LC_NAME],
80 from->repertoire_name, charmap);
81 while (from->categories[LC_NAME].name == NULL
82 && from->copy_name[LC_NAME] != NULL);
84 name = locale->categories[LC_NAME].name
85 = from->categories[LC_NAME].name;
88 /* If there is still no definition issue an warning and create an
89 empty one. */
90 if (name == NULL)
92 if (! be_quiet)
93 WITH_CUR_LOCALE (error (0, 0, _("\
94 No definition for %s category found"), "LC_NAME"));
95 name_startup (NULL, locale, 0);
96 name = locale->categories[LC_NAME].name;
97 nothing = 1;
101 if (name->name_fmt == NULL)
103 if (! nothing)
104 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
105 "LC_NAME", "name_fmt"));
106 /* Use as the default value the value of the i18n locale. */
107 name->name_fmt = "%p%t%g%t%m%t%f";
109 else
111 /* We must check whether the format string contains only the
112 allowed escape sequences. */
113 const char *cp = name->name_fmt;
115 if (*cp == '\0')
116 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
117 "LC_NAME", "name_fmt"));
118 else
119 while (*cp != '\0')
121 if (*cp == '%')
123 if (*++cp == 'R')
124 /* Romanize-flag. */
125 ++cp;
126 if (strchr ("dfFgGlomMpsSt", *cp) == NULL)
128 WITH_CUR_LOCALE (error (0, 0, _("\
129 %s: invalid escape sequence in field `%s'"), "LC_NAME", "name_fmt"));
130 break;
133 ++cp;
137 #define TEST_ELEM(cat) \
138 if (name->cat == NULL) \
140 if (verbose && ! nothing) \
141 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
142 "LC_NAME", #cat)); \
143 name->cat = ""; \
146 TEST_ELEM (name_gen);
147 TEST_ELEM (name_mr);
148 TEST_ELEM (name_mrs);
149 TEST_ELEM (name_miss);
150 TEST_ELEM (name_ms);
154 void
155 name_output (struct localedef_t *locale, const struct charmap_t *charmap,
156 const char *output_path)
158 struct locale_name_t *name = locale->categories[LC_NAME].name;
159 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_NAME)];
160 struct locale_file data;
161 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_NAME)];
162 size_t cnt = 0;
164 data.magic = LIMAGIC (LC_NAME);
165 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_NAME);
166 iov[cnt].iov_base = (void *) &data;
167 iov[cnt].iov_len = sizeof (data);
168 ++cnt;
170 iov[cnt].iov_base = (void *) idx;
171 iov[cnt].iov_len = sizeof (idx);
172 ++cnt;
174 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
175 iov[cnt].iov_base = (void *) name->name_fmt;
176 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
177 ++cnt;
179 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
180 iov[cnt].iov_base = (void *) name->name_gen;
181 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
182 ++cnt;
184 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
185 iov[cnt].iov_base = (void *) name->name_mr;
186 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
187 ++cnt;
189 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
190 iov[cnt].iov_base = (void *) name->name_mrs;
191 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
192 ++cnt;
194 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
195 iov[cnt].iov_base = (void *) name->name_miss;
196 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
197 ++cnt;
199 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
200 iov[cnt].iov_base = (void *) name->name_ms;
201 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
202 ++cnt;
204 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
205 iov[cnt].iov_base = (void *) charmap->code_set_name;
206 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
207 ++cnt;
209 assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_NAME));
211 write_locale_data (output_path, LC_NAME, "LC_NAME",
212 2 + _NL_ITEM_INDEX (_NL_NUM_LC_NAME), iov);
216 /* The parser for the LC_NAME section of the locale definition. */
217 void
218 name_read (struct linereader *ldfile, struct localedef_t *result,
219 const struct charmap_t *charmap, const char *repertoire_name,
220 int ignore_content)
222 struct locale_name_t *name;
223 struct token *now;
224 struct token *arg;
225 enum token_t nowtok;
227 /* The rest of the line containing `LC_NAME' must be empty. */
228 lr_ignore_rest (ldfile, 1);
232 now = lr_token (ldfile, charmap, result, NULL, verbose);
233 nowtok = now->tok;
235 while (nowtok == tok_eol);
237 /* If we see `copy' now we are almost done. */
238 if (nowtok == tok_copy)
240 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_name,
241 LC_NAME, "LC_NAME", ignore_content);
242 return;
245 /* Prepare the data structures. */
246 name_startup (ldfile, result, ignore_content);
247 name = result->categories[LC_NAME].name;
249 while (1)
251 /* Of course we don't proceed beyond the end of file. */
252 if (nowtok == tok_eof)
253 break;
255 /* Ignore empty lines. */
256 if (nowtok == tok_eol)
258 now = lr_token (ldfile, charmap, result, NULL, verbose);
259 nowtok = now->tok;
260 continue;
263 switch (nowtok)
265 #define STR_ELEM(cat) \
266 case tok_##cat: \
267 /* Ignore the rest of the line if we don't need the input of \
268 this line. */ \
269 if (ignore_content) \
271 lr_ignore_rest (ldfile, 0); \
272 break; \
275 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
276 if (arg->tok != tok_string) \
277 goto err_label; \
278 if (name->cat != NULL) \
279 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
280 "LC_NAME", #cat); \
281 else if (!ignore_content && arg->val.str.startmb == NULL) \
283 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
284 "LC_NAME", #cat); \
285 name->cat = ""; \
287 else if (!ignore_content) \
288 name->cat = arg->val.str.startmb; \
289 break
291 STR_ELEM (name_fmt);
292 STR_ELEM (name_gen);
293 STR_ELEM (name_mr);
294 STR_ELEM (name_mrs);
295 STR_ELEM (name_miss);
296 STR_ELEM (name_ms);
298 case tok_end:
299 /* Next we assume `LC_NAME'. */
300 arg = lr_token (ldfile, charmap, result, NULL, verbose);
301 if (arg->tok == tok_eof)
302 break;
303 if (arg->tok == tok_eol)
304 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_NAME");
305 else if (arg->tok != tok_lc_name)
306 lr_error (ldfile, _("\
307 %1$s: definition does not end with `END %1$s'"), "LC_NAME");
308 lr_ignore_rest (ldfile, arg->tok == tok_lc_name);
309 return;
311 default:
312 err_label:
313 SYNTAX_ERROR (_("%s: syntax error"), "LC_NAME");
316 /* Prepare for the next round. */
317 now = lr_token (ldfile, charmap, result, NULL, verbose);
318 nowtok = now->tok;
321 /* When we come here we reached the end of the file. */
322 lr_error (ldfile, _("%s: premature end of file"), "LC_NAME");