* io/fcntl.h [__USE_ATFILE] (AT_EACCESS): New macro.
[glibc.git] / locale / programs / ld-address.c
blob4d649b0456346b177c95ee84e264ecdb075c8db3
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. */
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 (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 = "";
243 else
245 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
247 helper = 2;
248 for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
249 if (strcmp (address->lang_ab, iso639[cnt].ab) == 0)
250 break;
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));
256 else
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')
269 if (verbose)
270 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
271 "LC_ADDRESS", "lang_lib"));
273 else
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)
279 break;
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));
285 else
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]);
299 else
301 for (cnt = 0; cnt < sizeof (iso3166) / sizeof (iso3166[0]); ++cnt)
302 if (address->country_num == iso3166[cnt].num)
303 break;
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"));
339 void
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)];
347 size_t cnt = 0;
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);
353 ++cnt;
355 iov[cnt].iov_base = (void *) idx;
356 iov[cnt].iov_len = sizeof (idx);
357 ++cnt;
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;
362 ++cnt;
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;
367 ++cnt;
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;
372 ++cnt;
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;
377 ++cnt;
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;
382 ++cnt;
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;
387 ++cnt;
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;
395 ++cnt;
397 iov[cnt].iov_base = (void *) &address->country_num;
398 iov[cnt].iov_len = sizeof (uint32_t);
399 ++cnt;
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;
404 ++cnt;
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;
409 ++cnt;
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;
414 ++cnt;
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;
419 ++cnt;
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;
424 ++cnt;
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;
429 ++cnt;
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. */
439 void
440 address_read (struct linereader *ldfile, struct localedef_t *result,
441 const struct charmap_t *charmap, const char *repertoire_name,
442 int ignore_content)
444 struct locale_address_t *address;
445 struct token *now;
446 struct token *arg;
447 enum token_t nowtok;
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);
456 nowtok = now->tok;
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);
465 return;
468 /* Prepare the data structures. */
469 address_startup (ldfile, result, ignore_content);
470 address = result->categories[LC_ADDRESS].address;
472 while (1)
474 /* Of course we don't proceed beyond the end of file. */
475 if (nowtok == tok_eof)
476 break;
478 /* Ignore empty lines. */
479 if (nowtok == tok_eol)
481 now = lr_token (ldfile, charmap, result, NULL, verbose);
482 nowtok = now->tok;
483 continue;
486 switch (nowtok)
488 #define STR_ELEM(cat) \
489 case tok_##cat: \
490 /* Ignore the rest of the line if we don't need the input of \
491 this line. */ \
492 if (ignore_content) \
494 lr_ignore_rest (ldfile, 0); \
495 break; \
498 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
499 if (arg->tok != tok_string) \
500 goto err_label; \
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); \
508 address->cat = ""; \
510 else if (!ignore_content) \
511 address->cat = arg->val.str.startmb; \
512 break
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);
521 STR_ELEM (lang_ab);
522 STR_ELEM (lang_term);
523 STR_ELEM (lang_lib);
525 #define INT_STR_ELEM(cat) \
526 case tok_##cat: \
527 /* Ignore the rest of the line if we don't need the input of \
528 this line. */ \
529 if (ignore_content) \
531 lr_ignore_rest (ldfile, 0); \
532 break; \
535 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
536 if (arg->tok != tok_string && arg->tok != tok_number) \
537 goto err_label; \
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); \
546 address->cat = ""; \
548 else if (!ignore_content) \
550 if (arg->tok == tok_string) \
551 address->cat = arg->val.str.startmb; \
552 else \
554 char *numbuf = (char *) xmalloc (11); \
555 snprintf (numbuf, 11, "%ld", arg->val.num); \
556 address->cat = numbuf; \
559 break
561 INT_STR_ELEM (country_isbn);
563 #define INT_ELEM(cat) \
564 case tok_##cat: \
565 /* Ignore the rest of the line if we don't need the input of \
566 this line. */ \
567 if (ignore_content) \
569 lr_ignore_rest (ldfile, 0); \
570 break; \
573 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
574 if (arg->tok != tok_number) \
575 goto err_label; \
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; \
581 break
583 INT_ELEM (country_num);
585 case tok_end:
586 /* Next we assume `LC_ADDRESS'. */
587 arg = lr_token (ldfile, charmap, result, NULL, verbose);
588 if (arg->tok == tok_eof)
589 break;
590 if (arg->tok == tok_eol)
591 lr_error (ldfile, _("%s: incomplete `END' line"),
592 "LC_ADDRESS");
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);
597 return;
599 default:
600 err_label:
601 SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
604 /* Prepare for the next round. */
605 now = lr_token (ldfile, charmap, result, NULL, verbose);
606 nowtok = now->tok;
609 /* When we come here we reached the end of the file. */
610 lr_error (ldfile, _("%s: premature end of file"), "LC_ADDRESS");