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. */
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 #define DEFINE_LANGUAGE_CODE2(Name, 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
;
75 const char *country_car
;
76 const char *country_isbn
;
77 const char *lang_name
;
79 const char *lang_term
;
85 address_startup (struct linereader
*lr
, struct localedef_t
*locale
,
89 locale
->categories
[LC_ADDRESS
].address
=
90 (struct locale_address_t
*) xcalloc (1,
91 sizeof (struct locale_address_t
));
95 lr
->translate_strings
= 1;
96 lr
->return_widestr
= 0;
102 address_finish (struct localedef_t
*locale
, const struct charmap_t
*charmap
)
104 struct locale_address_t
*address
= locale
->categories
[LC_ADDRESS
].address
;
109 /* Now resolve copying and also handle completely missing definitions. */
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
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
;
143 if (address
->postal_fmt
== NULL
)
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";
153 /* We must check whether the format string contains only the
154 allowed escape sequences. */
155 const char *cp
= address
->postal_fmt
;
158 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
159 "LC_ADDRESS", "postal_fmt"));
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"));
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)); \
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
);
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')
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]);
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)
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')
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
= "";
253 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
256 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
257 if (strcmp (address
->lang_ab
, iso639
[cnt
].ab
) == 0)
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
));
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')
278 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
279 "LC_ADDRESS", "lang_lib"));
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)
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
));
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]);
309 for (cnt
= 0; cnt
< sizeof (iso3166
) / sizeof (iso3166
[0]); ++cnt
)
310 if (address
->country_num
== iso3166
[cnt
].num
)
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"));
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
)];
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
);
363 iov
[cnt
].iov_base
= (void *) idx
;
364 iov
[cnt
].iov_len
= sizeof (idx
);
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;
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;
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;
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;
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;
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;
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;
405 iov
[cnt
].iov_base
= (void *) &address
->country_num
;
406 iov
[cnt
].iov_len
= sizeof (uint32_t);
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;
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;
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;
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;
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;
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;
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. */
448 address_read (struct linereader
*ldfile
, struct localedef_t
*result
,
449 const struct charmap_t
*charmap
, const char *repertoire_name
,
452 struct locale_address_t
*address
;
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
);
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
);
476 /* Prepare the data structures. */
477 address_startup (ldfile
, result
, ignore_content
);
478 address
= result
->categories
[LC_ADDRESS
].address
;
482 /* Of course we don't proceed beyond the end of file. */
483 if (nowtok
== tok_eof
)
486 /* Ignore empty lines. */
487 if (nowtok
== tok_eol
)
489 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
496 #define STR_ELEM(cat) \
498 /* Ignore the rest of the line if we don't need the input of \
500 if (ignore_content) \
502 lr_ignore_rest (ldfile, 0); \
506 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
507 if (arg->tok != tok_string) \
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); \
518 else if (!ignore_content) \
519 address->cat = arg->val.str.startmb; \
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
);
530 STR_ELEM (lang_term
);
533 #define INT_STR_ELEM(cat) \
535 /* Ignore the rest of the line if we don't need the input of \
537 if (ignore_content) \
539 lr_ignore_rest (ldfile, 0); \
543 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
544 if (arg->tok != tok_string && arg->tok != tok_number) \
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); \
556 else if (!ignore_content) \
558 if (arg->tok == tok_string) \
559 address->cat = arg->val.str.startmb; \
562 char *numbuf = (char *) xmalloc (11); \
563 snprintf (numbuf, 11, "%ld", arg->val.num); \
564 address->cat = numbuf; \
569 INT_STR_ELEM (country_isbn
);
571 #define INT_ELEM(cat) \
573 /* Ignore the rest of the line if we don't need the input of \
575 if (ignore_content) \
577 lr_ignore_rest (ldfile, 0); \
581 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
582 if (arg->tok != tok_number) \
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; \
591 INT_ELEM (country_num
);
594 /* Next we assume `LC_ADDRESS'. */
595 arg
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
596 if (arg
->tok
== tok_eof
)
598 if (arg
->tok
== tok_eol
)
599 lr_error (ldfile
, _("%s: incomplete `END' line"),
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
);
609 SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
612 /* Prepare for the next round. */
613 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
617 /* When we come here we reached the end of the file. */
618 lr_error (ldfile
, _("%s: premature end of file"), "LC_ADDRESS");