1 /* Copyright (C) 1998-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. */
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 (iso639
[cnt
].ab
[0] != '\0' && verbose
&& ! nothing
)
226 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
227 "LC_ADDRESS", "lang_ab"));
228 address
->lang_ab
= "";
230 else if (address
->lang_ab
[0] == '\0')
232 if (iso639
[cnt
].ab
[0] != '\0' && verbose
)
233 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
234 "LC_ADDRESS", "lang_ab"));
236 else if (iso639
[cnt
].ab
[0] == '\0')
238 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be defined"),
239 "LC_ADDRESS", "lang_ab"));
241 address
->lang_ab
= "";
245 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
248 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
249 if (strcmp (address
->lang_ab
, iso639
[cnt
].ab
) == 0)
251 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
252 WITH_CUR_LOCALE (error (0, 0, _("\
253 %s: language abbreviation `%s' not defined"),
254 "LC_ADDRESS", address
->lang_ab
));
257 if (strcmp (iso639
[cnt
].ab
, address
->lang_ab
) != 0
258 && iso639
[cnt
].ab
[0] != '\0')
259 WITH_CUR_LOCALE (error (0, 0, _("\
260 %s: `%s' value does not match `%s' value"),
261 "LC_ADDRESS", "lang_ab", "lang_term"));
264 if (address
->lang_lib
== NULL
)
265 /* This is no error. */
266 address
->lang_lib
= address
->lang_term
;
267 else if (address
->lang_lib
[0] == '\0')
270 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
271 "LC_ADDRESS", "lang_lib"));
275 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
277 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
278 if (strcmp (address
->lang_lib
, iso639
[cnt
].lib
) == 0)
280 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
281 WITH_CUR_LOCALE (error (0, 0, _("\
282 %s: language abbreviation `%s' not defined"),
283 "LC_ADDRESS", address
->lang_lib
));
286 if (strcmp (iso639
[cnt
].ab
, address
->lang_ab
) != 0)
287 WITH_CUR_LOCALE (error (0, 0, _("\
288 %s: `%s' value does not match `%s' value"), "LC_ADDRESS", "lang_lib",
289 helper
== 1 ? "lang_term" : "lang_ab"));
292 if (address
->country_num
== 0)
294 if (verbose
&& ! nothing
)
295 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
296 "LC_ADDRESS", "country_num"));
297 cnt
= sizeof (iso3166
) / sizeof (iso3166
[0]);
301 for (cnt
= 0; cnt
< sizeof (iso3166
) / sizeof (iso3166
[0]); ++cnt
)
302 if (address
->country_num
== iso3166
[cnt
].num
)
305 if (cnt
== sizeof (iso3166
) / sizeof (iso3166
[0]))
306 WITH_CUR_LOCALE (error (0, 0, _("\
307 %s: numeric country code `%d' not valid"),
308 "LC_ADDRESS", address
->country_num
));
311 if (address
->country_ab2
== NULL
)
313 if (verbose
&& ! nothing
)
314 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
315 "LC_ADDRESS", "country_ab2"));
316 address
->country_ab2
= " ";
318 else if (cnt
!= sizeof (iso3166
) / sizeof (iso3166
[0])
319 && strcmp (address
->country_ab2
, iso3166
[cnt
].ab2
) != 0)
320 WITH_CUR_LOCALE (error (0, 0,
321 _("%s: `%s' value does not match `%s' value"),
322 "LC_ADDRESS", "country_ab2", "country_num"));
324 if (address
->country_ab3
== NULL
)
326 if (verbose
&& ! nothing
)
327 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
328 "LC_ADDRESS", "country_ab3"));
329 address
->country_ab3
= " ";
331 else if (cnt
!= sizeof (iso3166
) / sizeof (iso3166
[0])
332 && strcmp (address
->country_ab3
, iso3166
[cnt
].ab3
) != 0)
333 WITH_CUR_LOCALE (error (0, 0, _("\
334 %s: `%s' value does not match `%s' value"),
335 "LC_ADDRESS", "country_ab3", "country_num"));
340 address_output (struct localedef_t
*locale
, const struct charmap_t
*charmap
,
341 const char *output_path
)
343 struct locale_address_t
*address
= locale
->categories
[LC_ADDRESS
].address
;
344 struct iovec iov
[3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
)];
345 struct locale_file data
;
346 uint32_t idx
[_NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
)];
349 data
.magic
= LIMAGIC (LC_ADDRESS
);
350 data
.n
= _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
);
351 iov
[cnt
].iov_base
= (void *) &data
;
352 iov
[cnt
].iov_len
= sizeof (data
);
355 iov
[cnt
].iov_base
= (void *) idx
;
356 iov
[cnt
].iov_len
= sizeof (idx
);
359 idx
[cnt
- 2] = iov
[0].iov_len
+ iov
[1].iov_len
;
360 iov
[cnt
].iov_base
= (void *) address
->postal_fmt
;
361 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
364 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
365 iov
[cnt
].iov_base
= (void *) address
->country_name
;
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_post
;
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_ab2
;
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_ab3
;
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_car
;
386 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
389 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
391 /* Align following data */
392 iov
[cnt
].iov_base
= (void *) "\0\0";
393 iov
[cnt
].iov_len
= ((idx
[cnt
- 2] + 3) & ~3) - idx
[cnt
- 2];
394 idx
[cnt
- 2] = (idx
[cnt
- 2] + 3) & ~3;
397 iov
[cnt
].iov_base
= (void *) &address
->country_num
;
398 iov
[cnt
].iov_len
= sizeof (uint32_t);
401 idx
[cnt
- 3] = idx
[cnt
- 4] + iov
[cnt
- 1].iov_len
;
402 iov
[cnt
].iov_base
= (void *) address
->country_isbn
;
403 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
406 idx
[cnt
- 3] = idx
[cnt
- 4] + iov
[cnt
- 1].iov_len
;
407 iov
[cnt
].iov_base
= (void *) address
->lang_name
;
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_ab
;
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_term
;
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_lib
;
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 *) charmap
->code_set_name
;
428 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
431 assert (cnt
== 3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
));
433 write_locale_data (output_path
, LC_ADDRESS
, "LC_ADDRESS",
434 3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
), iov
);
438 /* The parser for the LC_ADDRESS section of the locale definition. */
440 address_read (struct linereader
*ldfile
, struct localedef_t
*result
,
441 const struct charmap_t
*charmap
, const char *repertoire_name
,
444 struct locale_address_t
*address
;
449 /* The rest of the line containing `LC_ADDRESS' must be free. */
450 lr_ignore_rest (ldfile
, 1);
455 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
458 while (nowtok
== tok_eol
);
460 /* If we see `copy' now we are almost done. */
461 if (nowtok
== tok_copy
)
463 handle_copy (ldfile
, charmap
, repertoire_name
, result
, tok_lc_address
,
464 LC_ADDRESS
, "LC_ADDRESS", ignore_content
);
468 /* Prepare the data structures. */
469 address_startup (ldfile
, result
, ignore_content
);
470 address
= result
->categories
[LC_ADDRESS
].address
;
474 /* Of course we don't proceed beyond the end of file. */
475 if (nowtok
== tok_eof
)
478 /* Ignore empty lines. */
479 if (nowtok
== tok_eol
)
481 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
488 #define STR_ELEM(cat) \
490 /* Ignore the rest of the line if we don't need the input of \
492 if (ignore_content) \
494 lr_ignore_rest (ldfile, 0); \
498 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
499 if (arg->tok != tok_string) \
501 if (address->cat != NULL) \
502 lr_error (ldfile, _("\
503 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
504 else if (!ignore_content && arg->val.str.startmb == NULL) \
506 lr_error (ldfile, _("\
507 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
510 else if (!ignore_content) \
511 address->cat = arg->val.str.startmb; \
514 STR_ELEM (postal_fmt
);
515 STR_ELEM (country_name
);
516 STR_ELEM (country_post
);
517 STR_ELEM (country_ab2
);
518 STR_ELEM (country_ab3
);
519 STR_ELEM (country_car
);
520 STR_ELEM (lang_name
);
522 STR_ELEM (lang_term
);
525 #define INT_STR_ELEM(cat) \
527 /* Ignore the rest of the line if we don't need the input of \
529 if (ignore_content) \
531 lr_ignore_rest (ldfile, 0); \
535 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
536 if (arg->tok != tok_string && arg->tok != tok_number) \
538 if (address->cat != NULL) \
539 lr_error (ldfile, _("\
540 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
541 else if (!ignore_content && arg->tok == tok_string \
542 && arg->val.str.startmb == NULL) \
544 lr_error (ldfile, _("\
545 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
548 else if (!ignore_content) \
550 if (arg->tok == tok_string) \
551 address->cat = arg->val.str.startmb; \
554 char *numbuf = (char *) xmalloc (11); \
555 snprintf (numbuf, 11, "%ld", arg->val.num); \
556 address->cat = numbuf; \
561 INT_STR_ELEM (country_isbn
);
563 #define INT_ELEM(cat) \
565 /* Ignore the rest of the line if we don't need the input of \
567 if (ignore_content) \
569 lr_ignore_rest (ldfile, 0); \
573 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
574 if (arg->tok != tok_number) \
576 else if (address->cat != 0) \
577 lr_error (ldfile, _("\
578 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
579 else if (!ignore_content) \
580 address->cat = arg->val.num; \
583 INT_ELEM (country_num
);
586 /* Next we assume `LC_ADDRESS'. */
587 arg
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
588 if (arg
->tok
== tok_eof
)
590 if (arg
->tok
== tok_eol
)
591 lr_error (ldfile
, _("%s: incomplete `END' line"),
593 else if (arg
->tok
!= tok_lc_address
)
594 lr_error (ldfile
, _("\
595 %1$s: definition does not end with `END %1$s'"), "LC_ADDRESS");
596 lr_ignore_rest (ldfile
, arg
->tok
== tok_lc_address
);
601 SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
604 /* Prepare for the next round. */
605 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
609 /* When we come here we reached the end of the file. */
610 lr_error (ldfile
, _("%s: premature end of file"), "LC_ADDRESS");