1 /* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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. */
32 #include "localeinfo.h"
43 #define DEFINE_COUNTRY_CODE(Name, Ab2, Ab3, Num) \
45 #include "iso-3166.def"
56 #define DEFINE_LANGUAGE_CODE(Name, Ab, Term, Lib) \
58 #include "iso-639.def"
62 /* The real definition of the struct for the LC_ADDRESS locale. */
63 struct locale_address_t
65 const char *postal_fmt;
66 const char *country_name;
67 const char *country_post;
68 const char *country_ab2;
69 const char *country_ab3;
71 uint32_t country_num_ob;
72 const char *country_car;
73 const char *country_isbn;
74 const char *lang_name;
76 const char *lang_term;
82 address_startup (struct linereader *lr, struct localedef_t *locale,
86 locale->categories[LC_ADDRESS].address =
87 (struct locale_address_t *) xcalloc (1,
88 sizeof (struct locale_address_t));
92 lr->translate_strings = 1;
93 lr->return_widestr = 0;
99 address_finish (struct localedef_t *locale, struct charmap_t *charmap)
101 struct locale_address_t *address = locale->categories[LC_ADDRESS].address;
106 /* Now resolve copying and also handle completely missing definitions. */
109 /* First see whether we were supposed to copy. If yes, find the
110 actual definition. */
111 if (locale->copy_name[LC_ADDRESS] != NULL)
113 /* Find the copying locale. This has to happen transitively since
114 the locale we are copying from might also copying another one. */
115 struct localedef_t *from = locale;
118 from = find_locale (LC_ADDRESS, from->copy_name[LC_ADDRESS],
119 from->repertoire_name, charmap);
120 while (from->categories[LC_ADDRESS].address == NULL
121 && from->copy_name[LC_ADDRESS] != NULL);
123 address = locale->categories[LC_ADDRESS].address
124 = from->categories[LC_ADDRESS].address;
127 /* If there is still no definition issue an warning and create an
131 error (0, 0, _("No definition for %s category found"), "LC_ADDRESS");
132 address_startup (NULL, locale, 0);
133 address = locale->categories[LC_ADDRESS].address;
138 if (address->postal_fmt == NULL)
141 error (0, 0, _("%s: field `%s' not defined"),
142 "LC_ADDRESS", "postal_fmt");
143 /* Use as the default value the value of the i18n locale. */
144 address->postal_fmt = "%a%N%f%N%d%N%b%N%s %h %e %r%N%C-%z %T%N%c%N";
148 /* We must check whether the format string contains only the
149 allowed escape sequences. */
150 const char *cp = address->postal_fmt;
153 error (0, 0, _("%s: field `%s' must not be empty"),
154 "LC_ADDRESS", "postal_fmt");
163 if (strchr ("afdbshNtreCzTc%", *cp) == NULL)
166 %s: invalid escape `%%%c' sequence in field `%s'"),
167 "LC_ADDRESS", *cp, "postal_fmt");
175 #define TEST_ELEM(cat) \
176 if (address->cat == NULL) \
178 if (verbose && ! nothing) \
179 error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS", #cat); \
183 TEST_ELEM (country_name);
184 /* XXX Test against list of defined codes. */
185 TEST_ELEM (country_post);
186 /* XXX Test against list of defined codes. */
187 TEST_ELEM (country_car);
188 /* XXX Test against list of defined codes. */
189 TEST_ELEM (country_isbn);
190 TEST_ELEM (lang_name);
193 if (address->lang_term == NULL)
195 if (verbose && ! nothing)
196 error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS",
198 address->lang_term = "";
199 cnt = sizeof (iso639) / sizeof (iso639[0]);
201 else if (address->lang_term[0] == '\0')
204 error (0, 0, _("%s: field `%s' must not be empty"),
205 "LC_ADDRESS", "lang_term");
206 cnt = sizeof (iso639) / sizeof (iso639[0]);
210 /* Look for this language in the table. */
211 for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
212 if (strcmp (address->lang_term, iso639[cnt].term) == 0)
214 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
216 %s: terminology language code `%s' not defined"),
217 "LC_ADDRESS", address->lang_term);
220 if (address->lang_ab == NULL)
222 if (verbose && ! nothing)
223 error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS", "lang_ab");
224 address->lang_ab = "";
226 else if (address->lang_ab[0] == '\0')
229 error (0, 0, _("%s: field `%s' must not be empty"),
230 "LC_ADDRESS", "lang_ab");
234 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
237 for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
238 if (strcmp (address->lang_ab, iso639[cnt].ab) == 0)
240 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
242 %s: language abbreviation `%s' not defined"),
243 "LC_ADDRESS", address->lang_ab);
246 if (strcmp (iso639[cnt].ab, address->lang_ab) != 0)
248 %s: `%s' value does not match `%s' value"),
249 "LC_ADDRESS", "lang_ab", "lang_term");
252 if (address->lang_lib == NULL)
253 /* This is no error. */
254 address->lang_lib = address->lang_term;
255 else if (address->lang_lib[0] == '\0')
258 error (0, 0, _("%s: field `%s' must not be empty"),
259 "LC_ADDRESS", "lang_lib");
263 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
265 for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
266 if (strcmp (address->lang_lib, iso639[cnt].lib) == 0)
268 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
270 %s: language abbreviation `%s' not defined"),
271 "LC_ADDRESS", address->lang_lib);
274 if (strcmp (iso639[cnt].ab, address->lang_ab) != 0)
276 %s: `%s' value does not match `%s' value"), "LC_ADDRESS", "lang_lib",
277 helper == 1 ? "lang_term" : "lang_ab");
280 if (address->country_num == 0)
282 if (verbose && ! nothing)
283 error (0, 0, _("%s: field `%s' not defined"),
284 "LC_ADDRESS", "country_num");
285 cnt = sizeof (iso3166) / sizeof (iso3166[0]);
289 for (cnt = 0; cnt < sizeof (iso3166) / sizeof (iso3166[0]); ++cnt)
290 if (address->country_num == iso3166[cnt].num)
293 if (cnt == sizeof (iso3166) / sizeof (iso3166[0]))
295 %s: numeric country code `%d' not valid"),
296 "LC_ADDRESS", address->country_num);
298 address->country_num_ob = bswap_32 (address->country_num);
300 if (address->country_ab2 == NULL)
302 if (verbose && ! nothing)
303 error (0, 0, _("%s: field `%s' not defined"),
304 "LC_ADDRESS", "country_ab2");
305 address->country_ab2 = " ";
307 else if (cnt != sizeof (iso3166) / sizeof (iso3166[0])
308 && strcmp (address->country_ab2, iso3166[cnt].ab2) != 0)
309 error (0, 0, _("%s: `%s' value does not match `%s' value"),
310 "LC_ADDRESS", "country_ab2", "country_num");
312 if (address->country_ab3 == NULL)
314 if (verbose && ! nothing)
315 error (0, 0, _("%s: field `%s' not defined"),
316 "LC_ADDRESS", "country_ab3");
317 address->country_ab3 = " ";
319 else if (cnt != sizeof (iso3166) / sizeof (iso3166[0])
320 && strcmp (address->country_ab3, iso3166[cnt].ab3) != 0)
321 error (0, 0, _("%s: `%s' value does not match `%s' value"),
322 "LC_ADDRESS", "country_ab3", "country_num");
327 address_output (struct localedef_t *locale, struct charmap_t *charmap,
328 const char *output_path)
330 struct locale_address_t *address = locale->categories[LC_ADDRESS].address;
331 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS)];
332 struct locale_file data;
333 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS)];
336 data.magic = LIMAGIC (LC_ADDRESS);
337 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS);
338 iov[cnt].iov_base = (void *) &data;
339 iov[cnt].iov_len = sizeof (data);
342 iov[cnt].iov_base = (void *) idx;
343 iov[cnt].iov_len = sizeof (idx);
346 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
347 iov[cnt].iov_base = (void *) address->postal_fmt;
348 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
351 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
352 iov[cnt].iov_base = (void *) address->country_name;
353 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
356 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
357 iov[cnt].iov_base = (void *) address->country_post;
358 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
361 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
362 iov[cnt].iov_base = (void *) address->country_ab2;
363 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
366 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
367 iov[cnt].iov_base = (void *) address->country_ab3;
368 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
371 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
372 iov[cnt].iov_base = (void *) address->country_car;
373 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
376 #if __BYTE_ORDER == __LITTLE_ENDIAN
377 # define country_num_eb country_num_ob
378 # define country_num_el country_num
380 # define country_num_eb country_num
381 # define country_num_el country_num_ob
384 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
385 iov[cnt].iov_base = (void *) address->country_num_eb;
386 iov[cnt].iov_len = sizeof (uint32_t);
389 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
390 iov[cnt].iov_base = (void *) address->country_num_el;
391 iov[cnt].iov_len = sizeof (uint32_t);
394 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
395 iov[cnt].iov_base = (void *) address->country_isbn;
396 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
399 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
400 iov[cnt].iov_base = (void *) address->lang_name;
401 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
404 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
405 iov[cnt].iov_base = (void *) address->lang_ab;
406 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
409 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
410 iov[cnt].iov_base = (void *) address->lang_term;
411 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
414 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
415 iov[cnt].iov_base = (void *) address->lang_lib;
416 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
419 assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS));
421 write_locale_data (output_path, "LC_ADDRESS",
422 2 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS), iov);
426 /* The parser for the LC_ADDRESS section of the locale definition. */
428 address_read (struct linereader *ldfile, struct localedef_t *result,
429 struct charmap_t *charmap, const char *repertoire_name,
432 struct repertoire_t *repertoire = NULL;
433 struct locale_address_t *address;
438 /* Get the repertoire we have to use. */
439 if (repertoire_name != NULL)
440 repertoire = repertoire_read (repertoire_name);
442 /* The rest of the line containing `LC_ADDRESS' must be free. */
443 lr_ignore_rest (ldfile, 1);
448 now = lr_token (ldfile, charmap, NULL);
451 while (nowtok == tok_eol);
453 /* If we see `copy' now we are almost done. */
454 if (nowtok == tok_copy)
456 handle_copy (ldfile, charmap, repertoire, result, tok_lc_address,
457 LC_ADDRESS, "LC_ADDRESS", ignore_content);
461 /* Prepare the data structures. */
462 address_startup (ldfile, result, ignore_content);
463 address = result->categories[LC_ADDRESS].address;
467 /* Of course we don't proceed beyond the end of file. */
468 if (nowtok == tok_eof)
471 /* Ingore empty lines. */
472 if (nowtok == tok_eol)
474 now = lr_token (ldfile, charmap, NULL);
481 #define STR_ELEM(cat) \
483 /* Ignore the rest of the line if we don't need the input of \
485 if (ignore_content) \
487 lr_ignore_rest (ldfile, 0); \
491 arg = lr_token (ldfile, charmap, NULL); \
492 if (arg->tok != tok_string) \
494 if (address->cat != NULL) \
495 lr_error (ldfile, _("\
496 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
497 else if (!ignore_content && arg->val.str.startmb == NULL) \
499 lr_error (ldfile, _("\
500 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
503 else if (!ignore_content) \
504 address->cat = arg->val.str.startmb; \
507 STR_ELEM (postal_fmt);
508 STR_ELEM (country_name);
509 STR_ELEM (country_post);
510 STR_ELEM (country_ab2);
511 STR_ELEM (country_ab3);
512 STR_ELEM (country_car);
513 STR_ELEM (country_isbn);
514 STR_ELEM (lang_name);
516 STR_ELEM (lang_term);
519 #define INT_ELEM(cat) \
521 /* Ignore the rest of the line if we don't need the input of \
523 if (ignore_content) \
525 lr_ignore_rest (ldfile, 0); \
529 arg = lr_token (ldfile, charmap, NULL); \
530 if (arg->tok != tok_number) \
532 else if (address->cat != 0) \
533 lr_error (ldfile, _("\
534 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
535 else if (!ignore_content) \
536 address->cat = arg->val.num; \
539 INT_ELEM (country_num);
542 /* Next we assume `LC_ADDRESS'. */
543 arg = lr_token (ldfile, charmap, NULL);
544 if (arg->tok == tok_eof)
546 if (arg->tok == tok_eol)
547 lr_error (ldfile, _("%s: incomplete `END' line"),
549 else if (arg->tok != tok_lc_address)
550 lr_error (ldfile, _("\
551 %1$s: definition does not end with `END %1$s'"), "LC_ADDRESS");
552 lr_ignore_rest (ldfile, arg->tok == tok_lc_address);
557 SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
560 /* Prepare for the next round. */
561 now = lr_token (ldfile, charmap, NULL);
565 /* When we come here we reached the end of the file. */
566 lr_error (ldfile, _("%s: premature end of file"), "LC_ADDRESS");