* malloc/malloc.c [MALLOC_DEBUG]: Keep track of current maximum
[glibc.git] / locale / programs / ld-address.c
blobdac85510713f8651afe4644772f36c499d85a3de
1 /* Copyright (C) 1998-2002, 2005, 2006 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 <byteswap.h>
23 #include <error.h>
24 #include <langinfo.h>
25 #include <string.h>
26 #include <sys/uio.h>
28 #include <assert.h>
30 #include "localedef.h"
31 #include "localeinfo.h"
32 #include "locfile.h"
35 static struct
37 const char ab2[3];
38 const char ab3[4];
39 uint32_t num;
40 } iso3166[] =
42 #define DEFINE_COUNTRY_CODE(Name, Ab2, Ab3, Num) \
43 { #Ab2, #Ab3, Num },
44 #include "iso-3166.def"
48 static struct
50 const char ab[3];
51 const char term[4];
52 const char lib[4];
53 } iso639[] =
55 #define DEFINE_LANGUAGE_CODE(Name, Ab, Term, Lib) \
56 { #Ab, #Term, #Lib },
57 #define DEFINE_LANGUAGE_CODE3(Name, Term, Lib) \
58 { "", #Term, #Lib },
59 #include "iso-639.def"
63 /* The real definition of the struct for the LC_ADDRESS locale. */
64 struct locale_address_t
66 const char *postal_fmt;
67 const char *country_name;
68 const char *country_post;
69 const char *country_ab2;
70 const char *country_ab3;
71 uint32_t country_num;
72 const char *country_car;
73 const char *country_isbn;
74 const char *lang_name;
75 const char *lang_ab;
76 const char *lang_term;
77 const char *lang_lib;
81 static void
82 address_startup (struct linereader *lr, struct localedef_t *locale,
83 int ignore_content)
85 if (!ignore_content)
86 locale->categories[LC_ADDRESS].address =
87 (struct locale_address_t *) xcalloc (1,
88 sizeof (struct locale_address_t));
90 if (lr != NULL)
92 lr->translate_strings = 1;
93 lr->return_widestr = 0;
98 void
99 address_finish (struct localedef_t *locale, const struct charmap_t *charmap)
101 struct locale_address_t *address = locale->categories[LC_ADDRESS].address;
102 size_t cnt;
103 int helper;
104 int nothing = 0;
106 /* Now resolve copying and also handle completely missing definitions. */
107 if (address == NULL)
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
128 empty one. */
129 if (address == NULL)
131 if (! be_quiet)
132 WITH_CUR_LOCALE (error (0, 0, _("\
133 No definition for %s category found"), "LC_ADDRESS"));
134 address_startup (NULL, locale, 0);
135 address = locale->categories[LC_ADDRESS].address;
136 nothing = 1;
140 if (address->postal_fmt == NULL)
142 if (! nothing)
143 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
144 "LC_ADDRESS", "postal_fmt"));
145 /* Use as the default value the value of the i18n locale. */
146 address->postal_fmt = "%a%N%f%N%d%N%b%N%s %h %e %r%N%C-%z %T%N%c%N";
148 else
150 /* We must check whether the format string contains only the
151 allowed escape sequences. */
152 const char *cp = address->postal_fmt;
154 if (*cp == '\0')
155 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
156 "LC_ADDRESS", "postal_fmt"));
157 else
158 while (*cp != '\0')
160 if (*cp == '%')
162 if (*++cp == 'R')
163 /* Romanize-flag. */
164 ++cp;
165 if (strchr ("afdbshNtreCzTSc%", *cp) == NULL)
167 WITH_CUR_LOCALE (error (0, 0, _("\
168 %s: invalid escape `%%%c' sequence in field `%s'"),
169 "LC_ADDRESS", *cp, "postal_fmt"));
170 break;
173 ++cp;
177 #define TEST_ELEM(cat) \
178 if (address->cat == NULL) \
180 if (verbose && ! nothing) \
181 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
182 "LC_ADDRESS", #cat)); \
183 address->cat = ""; \
186 TEST_ELEM (country_name);
187 /* XXX Test against list of defined codes. */
188 TEST_ELEM (country_post);
189 /* XXX Test against list of defined codes. */
190 TEST_ELEM (country_car);
191 /* XXX Test against list of defined codes. */
192 TEST_ELEM (country_isbn);
193 TEST_ELEM (lang_name);
195 helper = 1;
196 if (address->lang_term == NULL)
198 if (verbose && ! nothing)
199 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
200 "LC_ADDRESS", "lang_term"));
201 address->lang_term = "";
202 cnt = sizeof (iso639) / sizeof (iso639[0]);
204 else if (address->lang_term[0] == '\0')
206 if (verbose)
207 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
208 "LC_ADDRESS", "lang_term"));
209 cnt = sizeof (iso639) / sizeof (iso639[0]);
211 else
213 /* Look for this language in the table. */
214 for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
215 if (strcmp (address->lang_term, iso639[cnt].term) == 0)
216 break;
217 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
218 WITH_CUR_LOCALE (error (0, 0, _("\
219 %s: terminology language code `%s' not defined"),
220 "LC_ADDRESS", address->lang_term));
223 if (address->lang_ab == NULL)
225 if ((cnt == sizeof (iso639) / sizeof (iso639[0])
226 || iso639[cnt].ab[0] != '\0')
227 && verbose && ! nothing)
228 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
229 "LC_ADDRESS", "lang_ab"));
230 address->lang_ab = "";
232 else if (address->lang_ab[0] == '\0')
234 if ((cnt == sizeof (iso639) / sizeof (iso639[0])
235 || iso639[cnt].ab[0] != '\0')
236 && verbose)
237 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
238 "LC_ADDRESS", "lang_ab"));
240 else if (cnt < sizeof (iso639) / sizeof (iso639[0])
241 && iso639[cnt].ab[0] == '\0')
243 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be defined"),
244 "LC_ADDRESS", "lang_ab"));
246 address->lang_ab = "";
248 else
250 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
252 helper = 2;
253 for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
254 if (strcmp (address->lang_ab, iso639[cnt].ab) == 0)
255 break;
256 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
257 WITH_CUR_LOCALE (error (0, 0, _("\
258 %s: language abbreviation `%s' not defined"),
259 "LC_ADDRESS", address->lang_ab));
261 else
262 if (strcmp (iso639[cnt].ab, address->lang_ab) != 0
263 && iso639[cnt].ab[0] != '\0')
264 WITH_CUR_LOCALE (error (0, 0, _("\
265 %s: `%s' value does not match `%s' value"),
266 "LC_ADDRESS", "lang_ab", "lang_term"));
269 if (address->lang_lib == NULL)
270 /* This is no error. */
271 address->lang_lib = address->lang_term;
272 else if (address->lang_lib[0] == '\0')
274 if (verbose)
275 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
276 "LC_ADDRESS", "lang_lib"));
278 else
280 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
282 for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
283 if (strcmp (address->lang_lib, iso639[cnt].lib) == 0)
284 break;
285 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
286 WITH_CUR_LOCALE (error (0, 0, _("\
287 %s: language abbreviation `%s' not defined"),
288 "LC_ADDRESS", address->lang_lib));
290 else
291 if (strcmp (iso639[cnt].ab, address->lang_ab) != 0)
292 WITH_CUR_LOCALE (error (0, 0, _("\
293 %s: `%s' value does not match `%s' value"), "LC_ADDRESS", "lang_lib",
294 helper == 1 ? "lang_term" : "lang_ab"));
297 if (address->country_num == 0)
299 if (verbose && ! nothing)
300 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
301 "LC_ADDRESS", "country_num"));
302 cnt = sizeof (iso3166) / sizeof (iso3166[0]);
304 else
306 for (cnt = 0; cnt < sizeof (iso3166) / sizeof (iso3166[0]); ++cnt)
307 if (address->country_num == iso3166[cnt].num)
308 break;
310 if (cnt == sizeof (iso3166) / sizeof (iso3166[0]))
311 WITH_CUR_LOCALE (error (0, 0, _("\
312 %s: numeric country code `%d' not valid"),
313 "LC_ADDRESS", address->country_num));
316 if (address->country_ab2 == NULL)
318 if (verbose && ! nothing)
319 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
320 "LC_ADDRESS", "country_ab2"));
321 address->country_ab2 = " ";
323 else if (cnt != sizeof (iso3166) / sizeof (iso3166[0])
324 && strcmp (address->country_ab2, iso3166[cnt].ab2) != 0)
325 WITH_CUR_LOCALE (error (0, 0,
326 _("%s: `%s' value does not match `%s' value"),
327 "LC_ADDRESS", "country_ab2", "country_num"));
329 if (address->country_ab3 == NULL)
331 if (verbose && ! nothing)
332 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
333 "LC_ADDRESS", "country_ab3"));
334 address->country_ab3 = " ";
336 else if (cnt != sizeof (iso3166) / sizeof (iso3166[0])
337 && strcmp (address->country_ab3, iso3166[cnt].ab3) != 0)
338 WITH_CUR_LOCALE (error (0, 0, _("\
339 %s: `%s' value does not match `%s' value"),
340 "LC_ADDRESS", "country_ab3", "country_num"));
344 void
345 address_output (struct localedef_t *locale, const struct charmap_t *charmap,
346 const char *output_path)
348 struct locale_address_t *address = locale->categories[LC_ADDRESS].address;
349 struct iovec iov[3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS)];
350 struct locale_file data;
351 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS)];
352 size_t cnt = 0;
354 data.magic = LIMAGIC (LC_ADDRESS);
355 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS);
356 iov[cnt].iov_base = (void *) &data;
357 iov[cnt].iov_len = sizeof (data);
358 ++cnt;
360 iov[cnt].iov_base = (void *) idx;
361 iov[cnt].iov_len = sizeof (idx);
362 ++cnt;
364 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
365 iov[cnt].iov_base = (void *) address->postal_fmt;
366 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
367 ++cnt;
369 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
370 iov[cnt].iov_base = (void *) address->country_name;
371 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
372 ++cnt;
374 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
375 iov[cnt].iov_base = (void *) address->country_post;
376 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
377 ++cnt;
379 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
380 iov[cnt].iov_base = (void *) address->country_ab2;
381 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
382 ++cnt;
384 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
385 iov[cnt].iov_base = (void *) address->country_ab3;
386 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
387 ++cnt;
389 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
390 iov[cnt].iov_base = (void *) address->country_car;
391 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
392 ++cnt;
394 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
396 /* Align following data */
397 iov[cnt].iov_base = (void *) "\0\0";
398 iov[cnt].iov_len = ((idx[cnt - 2] + 3) & ~3) - idx[cnt - 2];
399 idx[cnt - 2] = (idx[cnt - 2] + 3) & ~3;
400 ++cnt;
402 iov[cnt].iov_base = (void *) &address->country_num;
403 iov[cnt].iov_len = sizeof (uint32_t);
404 ++cnt;
406 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
407 iov[cnt].iov_base = (void *) address->country_isbn;
408 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
409 ++cnt;
411 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
412 iov[cnt].iov_base = (void *) address->lang_name;
413 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
414 ++cnt;
416 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
417 iov[cnt].iov_base = (void *) address->lang_ab;
418 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
419 ++cnt;
421 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
422 iov[cnt].iov_base = (void *) address->lang_term;
423 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
424 ++cnt;
426 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
427 iov[cnt].iov_base = (void *) address->lang_lib;
428 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
429 ++cnt;
431 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
432 iov[cnt].iov_base = (void *) charmap->code_set_name;
433 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
434 ++cnt;
436 assert (cnt == 3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS));
438 write_locale_data (output_path, LC_ADDRESS, "LC_ADDRESS",
439 3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS), iov);
443 /* The parser for the LC_ADDRESS section of the locale definition. */
444 void
445 address_read (struct linereader *ldfile, struct localedef_t *result,
446 const struct charmap_t *charmap, const char *repertoire_name,
447 int ignore_content)
449 struct locale_address_t *address;
450 struct token *now;
451 struct token *arg;
452 enum token_t nowtok;
454 /* The rest of the line containing `LC_ADDRESS' must be free. */
455 lr_ignore_rest (ldfile, 1);
460 now = lr_token (ldfile, charmap, result, NULL, verbose);
461 nowtok = now->tok;
463 while (nowtok == tok_eol);
465 /* If we see `copy' now we are almost done. */
466 if (nowtok == tok_copy)
468 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_address,
469 LC_ADDRESS, "LC_ADDRESS", ignore_content);
470 return;
473 /* Prepare the data structures. */
474 address_startup (ldfile, result, ignore_content);
475 address = result->categories[LC_ADDRESS].address;
477 while (1)
479 /* Of course we don't proceed beyond the end of file. */
480 if (nowtok == tok_eof)
481 break;
483 /* Ignore empty lines. */
484 if (nowtok == tok_eol)
486 now = lr_token (ldfile, charmap, result, NULL, verbose);
487 nowtok = now->tok;
488 continue;
491 switch (nowtok)
493 #define STR_ELEM(cat) \
494 case tok_##cat: \
495 /* Ignore the rest of the line if we don't need the input of \
496 this line. */ \
497 if (ignore_content) \
499 lr_ignore_rest (ldfile, 0); \
500 break; \
503 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
504 if (arg->tok != tok_string) \
505 goto err_label; \
506 if (address->cat != NULL) \
507 lr_error (ldfile, _("\
508 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
509 else if (!ignore_content && arg->val.str.startmb == NULL) \
511 lr_error (ldfile, _("\
512 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
513 address->cat = ""; \
515 else if (!ignore_content) \
516 address->cat = arg->val.str.startmb; \
517 break
519 STR_ELEM (postal_fmt);
520 STR_ELEM (country_name);
521 STR_ELEM (country_post);
522 STR_ELEM (country_ab2);
523 STR_ELEM (country_ab3);
524 STR_ELEM (country_car);
525 STR_ELEM (lang_name);
526 STR_ELEM (lang_ab);
527 STR_ELEM (lang_term);
528 STR_ELEM (lang_lib);
530 #define INT_STR_ELEM(cat) \
531 case tok_##cat: \
532 /* Ignore the rest of the line if we don't need the input of \
533 this line. */ \
534 if (ignore_content) \
536 lr_ignore_rest (ldfile, 0); \
537 break; \
540 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
541 if (arg->tok != tok_string && arg->tok != tok_number) \
542 goto err_label; \
543 if (address->cat != NULL) \
544 lr_error (ldfile, _("\
545 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
546 else if (!ignore_content && arg->tok == tok_string \
547 && arg->val.str.startmb == NULL) \
549 lr_error (ldfile, _("\
550 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
551 address->cat = ""; \
553 else if (!ignore_content) \
555 if (arg->tok == tok_string) \
556 address->cat = arg->val.str.startmb; \
557 else \
559 char *numbuf = (char *) xmalloc (11); \
560 snprintf (numbuf, 11, "%ld", arg->val.num); \
561 address->cat = numbuf; \
564 break
566 INT_STR_ELEM (country_isbn);
568 #define INT_ELEM(cat) \
569 case tok_##cat: \
570 /* Ignore the rest of the line if we don't need the input of \
571 this line. */ \
572 if (ignore_content) \
574 lr_ignore_rest (ldfile, 0); \
575 break; \
578 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
579 if (arg->tok != tok_number) \
580 goto err_label; \
581 else if (address->cat != 0) \
582 lr_error (ldfile, _("\
583 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
584 else if (!ignore_content) \
585 address->cat = arg->val.num; \
586 break
588 INT_ELEM (country_num);
590 case tok_end:
591 /* Next we assume `LC_ADDRESS'. */
592 arg = lr_token (ldfile, charmap, result, NULL, verbose);
593 if (arg->tok == tok_eof)
594 break;
595 if (arg->tok == tok_eol)
596 lr_error (ldfile, _("%s: incomplete `END' line"),
597 "LC_ADDRESS");
598 else if (arg->tok != tok_lc_address)
599 lr_error (ldfile, _("\
600 %1$s: definition does not end with `END %1$s'"), "LC_ADDRESS");
601 lr_ignore_rest (ldfile, arg->tok == tok_lc_address);
602 return;
604 default:
605 err_label:
606 SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
609 /* Prepare for the next round. */
610 now = lr_token (ldfile, charmap, result, NULL, verbose);
611 nowtok = now->tok;
614 /* When we come here we reached the end of the file. */
615 lr_error (ldfile, _("%s: premature end of file"), "LC_ADDRESS");