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. */
30 #include "localedef.h"
31 #include "localeinfo.h"
42 #define DEFINE_COUNTRY_CODE(Name, Ab2, Ab3, Num) \
44 #include "iso-3166.def"
55 #define DEFINE_LANGUAGE_CODE(Name, Ab, Term, Lib) \
57 #define DEFINE_LANGUAGE_CODE3(Name, 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
;
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
, const 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
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
;
140 if (address
->postal_fmt
== NULL
)
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";
150 /* We must check whether the format string contains only the
151 allowed escape sequences. */
152 const char *cp
= address
->postal_fmt
;
155 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
156 "LC_ADDRESS", "postal_fmt"));
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"));
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)); \
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
);
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')
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]);
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)
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')
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
= "";
250 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
253 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
254 if (strcmp (address
->lang_ab
, iso639
[cnt
].ab
) == 0)
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
));
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')
275 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
276 "LC_ADDRESS", "lang_lib"));
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)
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
));
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]);
306 for (cnt
= 0; cnt
< sizeof (iso3166
) / sizeof (iso3166
[0]); ++cnt
)
307 if (address
->country_num
== iso3166
[cnt
].num
)
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"));
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
)];
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
);
360 iov
[cnt
].iov_base
= (void *) idx
;
361 iov
[cnt
].iov_len
= sizeof (idx
);
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;
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;
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;
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;
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;
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;
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;
402 iov
[cnt
].iov_base
= (void *) &address
->country_num
;
403 iov
[cnt
].iov_len
= sizeof (uint32_t);
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;
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;
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;
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;
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;
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;
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. */
445 address_read (struct linereader
*ldfile
, struct localedef_t
*result
,
446 const struct charmap_t
*charmap
, const char *repertoire_name
,
449 struct locale_address_t
*address
;
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
);
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
);
473 /* Prepare the data structures. */
474 address_startup (ldfile
, result
, ignore_content
);
475 address
= result
->categories
[LC_ADDRESS
].address
;
479 /* Of course we don't proceed beyond the end of file. */
480 if (nowtok
== tok_eof
)
483 /* Ignore empty lines. */
484 if (nowtok
== tok_eol
)
486 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
493 #define STR_ELEM(cat) \
495 /* Ignore the rest of the line if we don't need the input of \
497 if (ignore_content) \
499 lr_ignore_rest (ldfile, 0); \
503 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
504 if (arg->tok != tok_string) \
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); \
515 else if (!ignore_content) \
516 address->cat = arg->val.str.startmb; \
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
);
527 STR_ELEM (lang_term
);
530 #define INT_STR_ELEM(cat) \
532 /* Ignore the rest of the line if we don't need the input of \
534 if (ignore_content) \
536 lr_ignore_rest (ldfile, 0); \
540 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
541 if (arg->tok != tok_string && arg->tok != tok_number) \
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); \
553 else if (!ignore_content) \
555 if (arg->tok == tok_string) \
556 address->cat = arg->val.str.startmb; \
559 char *numbuf = (char *) xmalloc (11); \
560 snprintf (numbuf, 11, "%ld", arg->val.num); \
561 address->cat = numbuf; \
566 INT_STR_ELEM (country_isbn
);
568 #define INT_ELEM(cat) \
570 /* Ignore the rest of the line if we don't need the input of \
572 if (ignore_content) \
574 lr_ignore_rest (ldfile, 0); \
578 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
579 if (arg->tok != tok_number) \
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; \
588 INT_ELEM (country_num
);
591 /* Next we assume `LC_ADDRESS'. */
592 arg
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
593 if (arg
->tok
== tok_eof
)
595 if (arg
->tok
== tok_eol
)
596 lr_error (ldfile
, _("%s: incomplete `END' line"),
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
);
606 SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
609 /* Prepare for the next round. */
610 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
614 /* When we come here we reached the end of the file. */
615 lr_error (ldfile
, _("%s: premature end of file"), "LC_ADDRESS");