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 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. */
31 #include "localedef.h"
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 #define DEFINE_LANGUAGE_CODE3(Name, Term, Lib) \
60 #include "iso-639.def"
64 /* The real definition of the struct for the LC_ADDRESS locale. */
65 struct locale_address_t
67 const char *postal_fmt
;
68 const char *country_name
;
69 const char *country_post
;
70 const char *country_ab2
;
71 const char *country_ab3
;
73 const char *country_car
;
74 const char *country_isbn
;
75 const char *lang_name
;
77 const char *lang_term
;
83 address_startup (struct linereader
*lr
, struct localedef_t
*locale
,
87 locale
->categories
[LC_ADDRESS
].address
=
88 (struct locale_address_t
*) xcalloc (1,
89 sizeof (struct locale_address_t
));
93 lr
->translate_strings
= 1;
94 lr
->return_widestr
= 0;
100 address_finish (struct localedef_t
*locale
, const struct charmap_t
*charmap
)
102 struct locale_address_t
*address
= locale
->categories
[LC_ADDRESS
].address
;
107 /* Now resolve copying and also handle completely missing definitions. */
110 /* First see whether we were supposed to copy. If yes, find the
111 actual definition. */
112 if (locale
->copy_name
[LC_ADDRESS
] != NULL
)
114 /* Find the copying locale. This has to happen transitively since
115 the locale we are copying from might also copying another one. */
116 struct localedef_t
*from
= locale
;
119 from
= find_locale (LC_ADDRESS
, from
->copy_name
[LC_ADDRESS
],
120 from
->repertoire_name
, charmap
);
121 while (from
->categories
[LC_ADDRESS
].address
== NULL
122 && from
->copy_name
[LC_ADDRESS
] != NULL
);
124 address
= locale
->categories
[LC_ADDRESS
].address
125 = from
->categories
[LC_ADDRESS
].address
;
128 /* If there is still no definition issue an warning and create an
133 WITH_CUR_LOCALE (error (0, 0, _("\
134 No definition for %s category found"), "LC_ADDRESS"));
135 address_startup (NULL
, locale
, 0);
136 address
= locale
->categories
[LC_ADDRESS
].address
;
141 if (address
->postal_fmt
== NULL
)
144 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
145 "LC_ADDRESS", "postal_fmt"));
146 /* Use as the default value the value of the i18n locale. */
147 address
->postal_fmt
= "%a%N%f%N%d%N%b%N%s %h %e %r%N%C-%z %T%N%c%N";
151 /* We must check whether the format string contains only the
152 allowed escape sequences. */
153 const char *cp
= address
->postal_fmt
;
156 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
157 "LC_ADDRESS", "postal_fmt"));
166 if (strchr ("afdbshNtreCzTSc%", *cp
) == NULL
)
168 WITH_CUR_LOCALE (error (0, 0, _("\
169 %s: invalid escape `%%%c' sequence in field `%s'"),
170 "LC_ADDRESS", *cp
, "postal_fmt"));
178 #define TEST_ELEM(cat) \
179 if (address->cat == NULL) \
181 if (verbose && ! nothing) \
182 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
183 "LC_ADDRESS", #cat)); \
187 TEST_ELEM (country_name
);
188 /* XXX Test against list of defined codes. */
189 TEST_ELEM (country_post
);
190 /* XXX Test against list of defined codes. */
191 TEST_ELEM (country_car
);
192 /* XXX Test against list of defined codes. */
193 TEST_ELEM (country_isbn
);
194 TEST_ELEM (lang_name
);
197 if (address
->lang_term
== NULL
)
199 if (verbose
&& ! nothing
)
200 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
201 "LC_ADDRESS", "lang_term"));
202 address
->lang_term
= "";
203 cnt
= sizeof (iso639
) / sizeof (iso639
[0]);
205 else if (address
->lang_term
[0] == '\0')
208 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
209 "LC_ADDRESS", "lang_term"));
210 cnt
= sizeof (iso639
) / sizeof (iso639
[0]);
214 /* Look for this language in the table. */
215 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
216 if (strcmp (address
->lang_term
, iso639
[cnt
].term
) == 0)
218 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
219 WITH_CUR_LOCALE (error (0, 0, _("\
220 %s: terminology language code `%s' not defined"),
221 "LC_ADDRESS", address
->lang_term
));
224 if (address
->lang_ab
== NULL
)
226 if ((cnt
== sizeof (iso639
) / sizeof (iso639
[0])
227 || iso639
[cnt
].ab
[0] != '\0')
228 && verbose
&& ! nothing
)
229 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
230 "LC_ADDRESS", "lang_ab"));
231 address
->lang_ab
= "";
233 else if (address
->lang_ab
[0] == '\0')
235 if ((cnt
== sizeof (iso639
) / sizeof (iso639
[0])
236 || iso639
[cnt
].ab
[0] != '\0')
238 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
239 "LC_ADDRESS", "lang_ab"));
241 else if (cnt
< sizeof (iso639
) / sizeof (iso639
[0])
242 && iso639
[cnt
].ab
[0] == '\0')
244 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be defined"),
245 "LC_ADDRESS", "lang_ab"));
247 address
->lang_ab
= "";
251 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
254 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
255 if (strcmp (address
->lang_ab
, iso639
[cnt
].ab
) == 0)
257 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
258 WITH_CUR_LOCALE (error (0, 0, _("\
259 %s: language abbreviation `%s' not defined"),
260 "LC_ADDRESS", address
->lang_ab
));
263 if (strcmp (iso639
[cnt
].ab
, address
->lang_ab
) != 0
264 && iso639
[cnt
].ab
[0] != '\0')
265 WITH_CUR_LOCALE (error (0, 0, _("\
266 %s: `%s' value does not match `%s' value"),
267 "LC_ADDRESS", "lang_ab", "lang_term"));
270 if (address
->lang_lib
== NULL
)
271 /* This is no error. */
272 address
->lang_lib
= address
->lang_term
;
273 else if (address
->lang_lib
[0] == '\0')
276 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
277 "LC_ADDRESS", "lang_lib"));
281 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
283 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
284 if (strcmp (address
->lang_lib
, iso639
[cnt
].lib
) == 0)
286 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
287 WITH_CUR_LOCALE (error (0, 0, _("\
288 %s: language abbreviation `%s' not defined"),
289 "LC_ADDRESS", address
->lang_lib
));
292 if (strcmp (iso639
[cnt
].ab
, address
->lang_ab
) != 0)
293 WITH_CUR_LOCALE (error (0, 0, _("\
294 %s: `%s' value does not match `%s' value"), "LC_ADDRESS", "lang_lib",
295 helper
== 1 ? "lang_term" : "lang_ab"));
298 if (address
->country_num
== 0)
300 if (verbose
&& ! nothing
)
301 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
302 "LC_ADDRESS", "country_num"));
303 cnt
= sizeof (iso3166
) / sizeof (iso3166
[0]);
307 for (cnt
= 0; cnt
< sizeof (iso3166
) / sizeof (iso3166
[0]); ++cnt
)
308 if (address
->country_num
== iso3166
[cnt
].num
)
311 if (cnt
== sizeof (iso3166
) / sizeof (iso3166
[0]))
312 WITH_CUR_LOCALE (error (0, 0, _("\
313 %s: numeric country code `%d' not valid"),
314 "LC_ADDRESS", address
->country_num
));
317 if (address
->country_ab2
== NULL
)
319 if (verbose
&& ! nothing
)
320 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
321 "LC_ADDRESS", "country_ab2"));
322 address
->country_ab2
= " ";
324 else if (cnt
!= sizeof (iso3166
) / sizeof (iso3166
[0])
325 && strcmp (address
->country_ab2
, iso3166
[cnt
].ab2
) != 0)
326 WITH_CUR_LOCALE (error (0, 0,
327 _("%s: `%s' value does not match `%s' value"),
328 "LC_ADDRESS", "country_ab2", "country_num"));
330 if (address
->country_ab3
== NULL
)
332 if (verbose
&& ! nothing
)
333 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
334 "LC_ADDRESS", "country_ab3"));
335 address
->country_ab3
= " ";
337 else if (cnt
!= sizeof (iso3166
) / sizeof (iso3166
[0])
338 && strcmp (address
->country_ab3
, iso3166
[cnt
].ab3
) != 0)
339 WITH_CUR_LOCALE (error (0, 0, _("\
340 %s: `%s' value does not match `%s' value"),
341 "LC_ADDRESS", "country_ab3", "country_num"));
346 address_output (struct localedef_t
*locale
, const struct charmap_t
*charmap
,
347 const char *output_path
)
349 struct locale_address_t
*address
= locale
->categories
[LC_ADDRESS
].address
;
350 struct iovec iov
[3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
)];
351 struct locale_file data
;
352 uint32_t idx
[_NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
)];
355 data
.magic
= LIMAGIC (LC_ADDRESS
);
356 data
.n
= _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
);
357 iov
[cnt
].iov_base
= (void *) &data
;
358 iov
[cnt
].iov_len
= sizeof (data
);
361 iov
[cnt
].iov_base
= (void *) idx
;
362 iov
[cnt
].iov_len
= sizeof (idx
);
365 idx
[cnt
- 2] = iov
[0].iov_len
+ iov
[1].iov_len
;
366 iov
[cnt
].iov_base
= (void *) address
->postal_fmt
;
367 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
370 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
371 iov
[cnt
].iov_base
= (void *) address
->country_name
;
372 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
375 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
376 iov
[cnt
].iov_base
= (void *) address
->country_post
;
377 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
380 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
381 iov
[cnt
].iov_base
= (void *) address
->country_ab2
;
382 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
385 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
386 iov
[cnt
].iov_base
= (void *) address
->country_ab3
;
387 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
390 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
391 iov
[cnt
].iov_base
= (void *) address
->country_car
;
392 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
395 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
397 /* Align following data */
398 iov
[cnt
].iov_base
= (void *) "\0\0";
399 iov
[cnt
].iov_len
= ((idx
[cnt
- 2] + 3) & ~3) - idx
[cnt
- 2];
400 idx
[cnt
- 2] = (idx
[cnt
- 2] + 3) & ~3;
403 iov
[cnt
].iov_base
= (void *) &address
->country_num
;
404 iov
[cnt
].iov_len
= sizeof (uint32_t);
407 idx
[cnt
- 3] = idx
[cnt
- 4] + iov
[cnt
- 1].iov_len
;
408 iov
[cnt
].iov_base
= (void *) address
->country_isbn
;
409 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
412 idx
[cnt
- 3] = idx
[cnt
- 4] + iov
[cnt
- 1].iov_len
;
413 iov
[cnt
].iov_base
= (void *) address
->lang_name
;
414 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
417 idx
[cnt
- 3] = idx
[cnt
- 4] + iov
[cnt
- 1].iov_len
;
418 iov
[cnt
].iov_base
= (void *) address
->lang_ab
;
419 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
422 idx
[cnt
- 3] = idx
[cnt
- 4] + iov
[cnt
- 1].iov_len
;
423 iov
[cnt
].iov_base
= (void *) address
->lang_term
;
424 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
427 idx
[cnt
- 3] = idx
[cnt
- 4] + iov
[cnt
- 1].iov_len
;
428 iov
[cnt
].iov_base
= (void *) address
->lang_lib
;
429 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
432 idx
[cnt
- 3] = idx
[cnt
- 4] + iov
[cnt
- 1].iov_len
;
433 iov
[cnt
].iov_base
= (void *) charmap
->code_set_name
;
434 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
437 assert (cnt
== 3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
));
439 write_locale_data (output_path
, LC_ADDRESS
, "LC_ADDRESS",
440 3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
), iov
);
444 /* The parser for the LC_ADDRESS section of the locale definition. */
446 address_read (struct linereader
*ldfile
, struct localedef_t
*result
,
447 const struct charmap_t
*charmap
, const char *repertoire_name
,
450 struct locale_address_t
*address
;
455 /* The rest of the line containing `LC_ADDRESS' must be free. */
456 lr_ignore_rest (ldfile
, 1);
461 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
464 while (nowtok
== tok_eol
);
466 /* If we see `copy' now we are almost done. */
467 if (nowtok
== tok_copy
)
469 handle_copy (ldfile
, charmap
, repertoire_name
, result
, tok_lc_address
,
470 LC_ADDRESS
, "LC_ADDRESS", ignore_content
);
474 /* Prepare the data structures. */
475 address_startup (ldfile
, result
, ignore_content
);
476 address
= result
->categories
[LC_ADDRESS
].address
;
480 /* Of course we don't proceed beyond the end of file. */
481 if (nowtok
== tok_eof
)
484 /* Ignore empty lines. */
485 if (nowtok
== tok_eol
)
487 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
494 #define STR_ELEM(cat) \
496 /* Ignore the rest of the line if we don't need the input of \
498 if (ignore_content) \
500 lr_ignore_rest (ldfile, 0); \
504 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
505 if (arg->tok != tok_string) \
507 if (address->cat != NULL) \
508 lr_error (ldfile, _("\
509 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
510 else if (!ignore_content && arg->val.str.startmb == NULL) \
512 lr_error (ldfile, _("\
513 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
516 else if (!ignore_content) \
517 address->cat = arg->val.str.startmb; \
520 STR_ELEM (postal_fmt
);
521 STR_ELEM (country_name
);
522 STR_ELEM (country_post
);
523 STR_ELEM (country_ab2
);
524 STR_ELEM (country_ab3
);
525 STR_ELEM (country_car
);
526 STR_ELEM (lang_name
);
528 STR_ELEM (lang_term
);
531 #define INT_STR_ELEM(cat) \
533 /* Ignore the rest of the line if we don't need the input of \
535 if (ignore_content) \
537 lr_ignore_rest (ldfile, 0); \
541 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
542 if (arg->tok != tok_string && arg->tok != tok_number) \
544 if (address->cat != NULL) \
545 lr_error (ldfile, _("\
546 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
547 else if (!ignore_content && arg->tok == tok_string \
548 && arg->val.str.startmb == NULL) \
550 lr_error (ldfile, _("\
551 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
554 else if (!ignore_content) \
556 if (arg->tok == tok_string) \
557 address->cat = arg->val.str.startmb; \
560 char *numbuf = (char *) xmalloc (11); \
561 snprintf (numbuf, 11, "%ld", arg->val.num); \
562 address->cat = numbuf; \
567 INT_STR_ELEM (country_isbn
);
569 #define INT_ELEM(cat) \
571 /* Ignore the rest of the line if we don't need the input of \
573 if (ignore_content) \
575 lr_ignore_rest (ldfile, 0); \
579 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
580 if (arg->tok != tok_number) \
582 else if (address->cat != 0) \
583 lr_error (ldfile, _("\
584 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
585 else if (!ignore_content) \
586 address->cat = arg->val.num; \
589 INT_ELEM (country_num
);
592 /* Next we assume `LC_ADDRESS'. */
593 arg
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
594 if (arg
->tok
== tok_eof
)
596 if (arg
->tok
== tok_eol
)
597 lr_error (ldfile
, _("%s: incomplete `END' line"),
599 else if (arg
->tok
!= tok_lc_address
)
600 lr_error (ldfile
, _("\
601 %1$s: definition does not end with `END %1$s'"), "LC_ADDRESS");
602 lr_ignore_rest (ldfile
, arg
->tok
== tok_lc_address
);
607 SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
610 /* Prepare for the next round. */
611 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
615 /* When we come here we reached the end of the file. */
616 lr_error (ldfile
, _("%s: premature end of file"), "LC_ADDRESS");