(hsearch_r): Add back ensurance that hval is not zero.
[glibc/history.git] / locale / programs / ld-address.c
blobdf2e193142ce74cfb4abe0a817d67cd561cb11f8
1 /* Copyright (C) 1998-2002, 2005, 2006, 2008 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 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, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <byteswap.h>
24 #include <error.h>
25 #include <langinfo.h>
26 #include <string.h>
27 #include <sys/uio.h>
29 #include <assert.h>
31 #include "localedef.h"
32 #include "localeinfo.h"
33 #include "locfile.h"
36 static struct
38 const char ab2[3];
39 const char ab3[4];
40 uint32_t num;
41 } iso3166[] =
43 #define DEFINE_COUNTRY_CODE(Name, Ab2, Ab3, Num) \
44 { #Ab2, #Ab3, Num },
45 #include "iso-3166.def"
49 static struct
51 const char ab[3];
52 const char term[4];
53 const char lib[4];
54 } iso639[] =
56 #define DEFINE_LANGUAGE_CODE(Name, Ab, Term, Lib) \
57 { #Ab, #Term, #Lib },
58 #define DEFINE_LANGUAGE_CODE3(Name, Term, Lib) \
59 { "", #Term, #Lib },
60 #define DEFINE_LANGUAGE_CODE2(Name, Term) \
61 { "", #Term, "" },
62 #include "iso-639.def"
66 /* The real definition of the struct for the LC_ADDRESS locale. */
67 struct locale_address_t
69 const char *postal_fmt;
70 const char *country_name;
71 const char *country_post;
72 const char *country_ab2;
73 const char *country_ab3;
74 uint32_t country_num;
75 const char *country_car;
76 const char *country_isbn;
77 const char *lang_name;
78 const char *lang_ab;
79 const char *lang_term;
80 const char *lang_lib;
84 static void
85 address_startup (struct linereader *lr, struct localedef_t *locale,
86 int ignore_content)
88 if (!ignore_content)
89 locale->categories[LC_ADDRESS].address =
90 (struct locale_address_t *) xcalloc (1,
91 sizeof (struct locale_address_t));
93 if (lr != NULL)
95 lr->translate_strings = 1;
96 lr->return_widestr = 0;
101 void
102 address_finish (struct localedef_t *locale, const struct charmap_t *charmap)
104 struct locale_address_t *address = locale->categories[LC_ADDRESS].address;
105 size_t cnt;
106 int helper;
107 int nothing = 0;
109 /* Now resolve copying and also handle completely missing definitions. */
110 if (address == NULL)
112 /* First see whether we were supposed to copy. If yes, find the
113 actual definition. */
114 if (locale->copy_name[LC_ADDRESS] != NULL)
116 /* Find the copying locale. This has to happen transitively since
117 the locale we are copying from might also copying another one. */
118 struct localedef_t *from = locale;
121 from = find_locale (LC_ADDRESS, from->copy_name[LC_ADDRESS],
122 from->repertoire_name, charmap);
123 while (from->categories[LC_ADDRESS].address == NULL
124 && from->copy_name[LC_ADDRESS] != NULL);
126 address = locale->categories[LC_ADDRESS].address
127 = from->categories[LC_ADDRESS].address;
130 /* If there is still no definition issue an warning and create an
131 empty one. */
132 if (address == NULL)
134 if (! be_quiet)
135 WITH_CUR_LOCALE (error (0, 0, _("\
136 No definition for %s category found"), "LC_ADDRESS"));
137 address_startup (NULL, locale, 0);
138 address = locale->categories[LC_ADDRESS].address;
139 nothing = 1;
143 if (address->postal_fmt == NULL)
145 if (! nothing)
146 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
147 "LC_ADDRESS", "postal_fmt"));
148 /* Use as the default value the value of the i18n locale. */
149 address->postal_fmt = "%a%N%f%N%d%N%b%N%s %h %e %r%N%C-%z %T%N%c%N";
151 else
153 /* We must check whether the format string contains only the
154 allowed escape sequences. */
155 const char *cp = address->postal_fmt;
157 if (*cp == '\0')
158 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
159 "LC_ADDRESS", "postal_fmt"));
160 else
161 while (*cp != '\0')
163 if (*cp == '%')
165 if (*++cp == 'R')
166 /* Romanize-flag. */
167 ++cp;
168 if (strchr ("afdbshNtreCzTSc%", *cp) == NULL)
170 WITH_CUR_LOCALE (error (0, 0, _("\
171 %s: invalid escape `%%%c' sequence in field `%s'"),
172 "LC_ADDRESS", *cp, "postal_fmt"));
173 break;
176 ++cp;
180 #define TEST_ELEM(cat) \
181 if (address->cat == NULL) \
183 if (verbose && ! nothing) \
184 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
185 "LC_ADDRESS", #cat)); \
186 address->cat = ""; \
189 TEST_ELEM (country_name);
190 /* XXX Test against list of defined codes. */
191 TEST_ELEM (country_post);
192 /* XXX Test against list of defined codes. */
193 TEST_ELEM (country_car);
194 /* XXX Test against list of defined codes. */
195 TEST_ELEM (country_isbn);
196 TEST_ELEM (lang_name);
198 helper = 1;
199 if (address->lang_term == NULL)
201 if (verbose && ! nothing)
202 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
203 "LC_ADDRESS", "lang_term"));
204 address->lang_term = "";
205 cnt = sizeof (iso639) / sizeof (iso639[0]);
207 else if (address->lang_term[0] == '\0')
209 if (verbose)
210 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
211 "LC_ADDRESS", "lang_term"));
212 cnt = sizeof (iso639) / sizeof (iso639[0]);
214 else
216 /* Look for this language in the table. */
217 for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
218 if (strcmp (address->lang_term, iso639[cnt].term) == 0)
219 break;
220 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
221 WITH_CUR_LOCALE (error (0, 0, _("\
222 %s: terminology language code `%s' not defined"),
223 "LC_ADDRESS", address->lang_term));
226 if (address->lang_ab == NULL)
228 if ((cnt == sizeof (iso639) / sizeof (iso639[0])
229 || iso639[cnt].ab[0] != '\0')
230 && verbose && ! nothing)
231 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
232 "LC_ADDRESS", "lang_ab"));
233 address->lang_ab = "";
235 else if (address->lang_ab[0] == '\0')
237 if ((cnt == sizeof (iso639) / sizeof (iso639[0])
238 || iso639[cnt].ab[0] != '\0')
239 && verbose)
240 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
241 "LC_ADDRESS", "lang_ab"));
243 else if (cnt < sizeof (iso639) / sizeof (iso639[0])
244 && iso639[cnt].ab[0] == '\0')
246 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be defined"),
247 "LC_ADDRESS", "lang_ab"));
249 address->lang_ab = "";
251 else
253 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
255 helper = 2;
256 for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
257 if (strcmp (address->lang_ab, iso639[cnt].ab) == 0)
258 break;
259 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
260 WITH_CUR_LOCALE (error (0, 0, _("\
261 %s: language abbreviation `%s' not defined"),
262 "LC_ADDRESS", address->lang_ab));
264 else
265 if (strcmp (iso639[cnt].ab, address->lang_ab) != 0
266 && iso639[cnt].ab[0] != '\0')
267 WITH_CUR_LOCALE (error (0, 0, _("\
268 %s: `%s' value does not match `%s' value"),
269 "LC_ADDRESS", "lang_ab", "lang_term"));
272 if (address->lang_lib == NULL)
273 /* This is no error. */
274 address->lang_lib = address->lang_term;
275 else if (address->lang_lib[0] == '\0')
277 if (verbose)
278 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
279 "LC_ADDRESS", "lang_lib"));
281 else
283 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
285 for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
286 if (strcmp (address->lang_lib, iso639[cnt].lib) == 0)
287 break;
288 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
289 WITH_CUR_LOCALE (error (0, 0, _("\
290 %s: language abbreviation `%s' not defined"),
291 "LC_ADDRESS", address->lang_lib));
293 else
294 if (strcmp (iso639[cnt].ab, address->lang_ab) != 0)
295 WITH_CUR_LOCALE (error (0, 0, _("\
296 %s: `%s' value does not match `%s' value"), "LC_ADDRESS", "lang_lib",
297 helper == 1 ? "lang_term" : "lang_ab"));
300 if (address->country_num == 0)
302 if (verbose && ! nothing)
303 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
304 "LC_ADDRESS", "country_num"));
305 cnt = sizeof (iso3166) / sizeof (iso3166[0]);
307 else
309 for (cnt = 0; cnt < sizeof (iso3166) / sizeof (iso3166[0]); ++cnt)
310 if (address->country_num == iso3166[cnt].num)
311 break;
313 if (cnt == sizeof (iso3166) / sizeof (iso3166[0]))
314 WITH_CUR_LOCALE (error (0, 0, _("\
315 %s: numeric country code `%d' not valid"),
316 "LC_ADDRESS", address->country_num));
319 if (address->country_ab2 == NULL)
321 if (verbose && ! nothing)
322 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
323 "LC_ADDRESS", "country_ab2"));
324 address->country_ab2 = " ";
326 else if (cnt != sizeof (iso3166) / sizeof (iso3166[0])
327 && strcmp (address->country_ab2, iso3166[cnt].ab2) != 0)
328 WITH_CUR_LOCALE (error (0, 0,
329 _("%s: `%s' value does not match `%s' value"),
330 "LC_ADDRESS", "country_ab2", "country_num"));
332 if (address->country_ab3 == NULL)
334 if (verbose && ! nothing)
335 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
336 "LC_ADDRESS", "country_ab3"));
337 address->country_ab3 = " ";
339 else if (cnt != sizeof (iso3166) / sizeof (iso3166[0])
340 && strcmp (address->country_ab3, iso3166[cnt].ab3) != 0)
341 WITH_CUR_LOCALE (error (0, 0, _("\
342 %s: `%s' value does not match `%s' value"),
343 "LC_ADDRESS", "country_ab3", "country_num"));
347 void
348 address_output (struct localedef_t *locale, const struct charmap_t *charmap,
349 const char *output_path)
351 struct locale_address_t *address = locale->categories[LC_ADDRESS].address;
352 struct iovec iov[3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS)];
353 struct locale_file data;
354 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS)];
355 size_t cnt = 0;
357 data.magic = LIMAGIC (LC_ADDRESS);
358 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS);
359 iov[cnt].iov_base = (void *) &data;
360 iov[cnt].iov_len = sizeof (data);
361 ++cnt;
363 iov[cnt].iov_base = (void *) idx;
364 iov[cnt].iov_len = sizeof (idx);
365 ++cnt;
367 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
368 iov[cnt].iov_base = (void *) address->postal_fmt;
369 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
370 ++cnt;
372 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
373 iov[cnt].iov_base = (void *) address->country_name;
374 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
375 ++cnt;
377 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
378 iov[cnt].iov_base = (void *) address->country_post;
379 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
380 ++cnt;
382 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
383 iov[cnt].iov_base = (void *) address->country_ab2;
384 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
385 ++cnt;
387 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
388 iov[cnt].iov_base = (void *) address->country_ab3;
389 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
390 ++cnt;
392 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
393 iov[cnt].iov_base = (void *) address->country_car;
394 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
395 ++cnt;
397 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
399 /* Align following data */
400 iov[cnt].iov_base = (void *) "\0\0";
401 iov[cnt].iov_len = ((idx[cnt - 2] + 3) & ~3) - idx[cnt - 2];
402 idx[cnt - 2] = (idx[cnt - 2] + 3) & ~3;
403 ++cnt;
405 iov[cnt].iov_base = (void *) &address->country_num;
406 iov[cnt].iov_len = sizeof (uint32_t);
407 ++cnt;
409 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
410 iov[cnt].iov_base = (void *) address->country_isbn;
411 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
412 ++cnt;
414 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
415 iov[cnt].iov_base = (void *) address->lang_name;
416 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
417 ++cnt;
419 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
420 iov[cnt].iov_base = (void *) address->lang_ab;
421 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
422 ++cnt;
424 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
425 iov[cnt].iov_base = (void *) address->lang_term;
426 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
427 ++cnt;
429 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
430 iov[cnt].iov_base = (void *) address->lang_lib;
431 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
432 ++cnt;
434 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
435 iov[cnt].iov_base = (void *) charmap->code_set_name;
436 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
437 ++cnt;
439 assert (cnt == 3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS));
441 write_locale_data (output_path, LC_ADDRESS, "LC_ADDRESS",
442 3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS), iov);
446 /* The parser for the LC_ADDRESS section of the locale definition. */
447 void
448 address_read (struct linereader *ldfile, struct localedef_t *result,
449 const struct charmap_t *charmap, const char *repertoire_name,
450 int ignore_content)
452 struct locale_address_t *address;
453 struct token *now;
454 struct token *arg;
455 enum token_t nowtok;
457 /* The rest of the line containing `LC_ADDRESS' must be free. */
458 lr_ignore_rest (ldfile, 1);
463 now = lr_token (ldfile, charmap, result, NULL, verbose);
464 nowtok = now->tok;
466 while (nowtok == tok_eol);
468 /* If we see `copy' now we are almost done. */
469 if (nowtok == tok_copy)
471 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_address,
472 LC_ADDRESS, "LC_ADDRESS", ignore_content);
473 return;
476 /* Prepare the data structures. */
477 address_startup (ldfile, result, ignore_content);
478 address = result->categories[LC_ADDRESS].address;
480 while (1)
482 /* Of course we don't proceed beyond the end of file. */
483 if (nowtok == tok_eof)
484 break;
486 /* Ignore empty lines. */
487 if (nowtok == tok_eol)
489 now = lr_token (ldfile, charmap, result, NULL, verbose);
490 nowtok = now->tok;
491 continue;
494 switch (nowtok)
496 #define STR_ELEM(cat) \
497 case tok_##cat: \
498 /* Ignore the rest of the line if we don't need the input of \
499 this line. */ \
500 if (ignore_content) \
502 lr_ignore_rest (ldfile, 0); \
503 break; \
506 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
507 if (arg->tok != tok_string) \
508 goto err_label; \
509 if (address->cat != NULL) \
510 lr_error (ldfile, _("\
511 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
512 else if (!ignore_content && arg->val.str.startmb == NULL) \
514 lr_error (ldfile, _("\
515 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
516 address->cat = ""; \
518 else if (!ignore_content) \
519 address->cat = arg->val.str.startmb; \
520 break
522 STR_ELEM (postal_fmt);
523 STR_ELEM (country_name);
524 STR_ELEM (country_post);
525 STR_ELEM (country_ab2);
526 STR_ELEM (country_ab3);
527 STR_ELEM (country_car);
528 STR_ELEM (lang_name);
529 STR_ELEM (lang_ab);
530 STR_ELEM (lang_term);
531 STR_ELEM (lang_lib);
533 #define INT_STR_ELEM(cat) \
534 case tok_##cat: \
535 /* Ignore the rest of the line if we don't need the input of \
536 this line. */ \
537 if (ignore_content) \
539 lr_ignore_rest (ldfile, 0); \
540 break; \
543 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
544 if (arg->tok != tok_string && arg->tok != tok_number) \
545 goto err_label; \
546 if (address->cat != NULL) \
547 lr_error (ldfile, _("\
548 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
549 else if (!ignore_content && arg->tok == tok_string \
550 && arg->val.str.startmb == NULL) \
552 lr_error (ldfile, _("\
553 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
554 address->cat = ""; \
556 else if (!ignore_content) \
558 if (arg->tok == tok_string) \
559 address->cat = arg->val.str.startmb; \
560 else \
562 char *numbuf = (char *) xmalloc (11); \
563 snprintf (numbuf, 11, "%ld", arg->val.num); \
564 address->cat = numbuf; \
567 break
569 INT_STR_ELEM (country_isbn);
571 #define INT_ELEM(cat) \
572 case tok_##cat: \
573 /* Ignore the rest of the line if we don't need the input of \
574 this line. */ \
575 if (ignore_content) \
577 lr_ignore_rest (ldfile, 0); \
578 break; \
581 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
582 if (arg->tok != tok_number) \
583 goto err_label; \
584 else if (address->cat != 0) \
585 lr_error (ldfile, _("\
586 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
587 else if (!ignore_content) \
588 address->cat = arg->val.num; \
589 break
591 INT_ELEM (country_num);
593 case tok_end:
594 /* Next we assume `LC_ADDRESS'. */
595 arg = lr_token (ldfile, charmap, result, NULL, verbose);
596 if (arg->tok == tok_eof)
597 break;
598 if (arg->tok == tok_eol)
599 lr_error (ldfile, _("%s: incomplete `END' line"),
600 "LC_ADDRESS");
601 else if (arg->tok != tok_lc_address)
602 lr_error (ldfile, _("\
603 %1$s: definition does not end with `END %1$s'"), "LC_ADDRESS");
604 lr_ignore_rest (ldfile, arg->tok == tok_lc_address);
605 return;
607 default:
608 err_label:
609 SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
612 /* Prepare for the next round. */
613 now = lr_token (ldfile, charmap, result, NULL, verbose);
614 nowtok = now->tok;
617 /* When we come here we reached the end of the file. */
618 lr_error (ldfile, _("%s: premature end of file"), "LC_ADDRESS");