]>
git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/ld-address.c
1 /* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
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 #include "iso-639.def"
62 /* The real definition of the struct for the LC_ADDRESS locale. */
63 struct locale_address_t
65 const char *postal_fmt
;
66 const char *country_name
;
67 const char *country_post
;
68 const char *country_ab2
;
69 const char *country_ab3
;
71 uint32_t country_num_ob
;
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
));
90 lr
->translate_strings
= 1;
91 lr
->return_widestr
= 0;
96 address_finish (struct localedef_t
*locale
, struct charmap_t
*charmap
)
98 struct locale_address_t
*address
= locale
->categories
[LC_ADDRESS
].address
;
102 if (address
->postal_fmt
== NULL
)
104 error (0, 0, _("%s: field `%s' not defined"),
105 "LC_ADDRESS", "postal_fmt");
106 /* Use as the default value the value of the i18n locale. */
107 address
->postal_fmt
= "%a%N%f%N%d%N%b%N%s %h %e %r%N%C-%z %T%N%c%N";
111 /* We must check whether the format string contains only the
112 allowed escape sequences. */
113 const char *cp
= address
->postal_fmt
;
116 error (0, 0, _("%s: field `%s' must not be empty"),
117 "LC_ADDRESS", "postal_fmt");
126 if (strchr ("afdbshNtreCzTc%", *cp
) == NULL
)
129 %s: invalid escape `%%%c' sequence in field `%s'"),
130 "LC_ADDRESS", *cp
, "postal_fmt");
138 #define TEST_ELEM(cat) \
139 if (address->cat == NULL) \
142 error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS", #cat); \
146 TEST_ELEM (country_name
);
147 /* XXX Test against list of defined codes. */
148 TEST_ELEM (country_post
);
149 /* XXX Test against list of defined codes. */
150 TEST_ELEM (country_car
);
151 /* XXX Test against list of defined codes. */
152 TEST_ELEM (country_isbn
);
153 TEST_ELEM (lang_name
);
156 if (address
->lang_term
== NULL
)
159 error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS",
161 address
->lang_term
= "";
162 cnt
= sizeof (iso639
) / sizeof (iso639
[0]);
164 else if (address
->lang_term
[0] == '\0')
167 error (0, 0, _("%s: field `%s' must not be empty"),
168 "LC_ADDRESS", "lang_term");
169 cnt
= sizeof (iso639
) / sizeof (iso639
[0]);
173 /* Look for this language in the table. */
174 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
175 if (strcmp (address
->lang_term
, iso639
[cnt
].term
) == 0)
177 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
179 %s: terminology language code `%s' not defined"),
180 "LC_ADDRESS", address
->lang_term
);
183 if (address
->lang_ab
== NULL
)
186 error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS", "lang_ab");
187 address
->lang_ab
= "";
189 else if (address
->lang_ab
[0] == '\0')
192 error (0, 0, _("%s: field `%s' must not be empty"),
193 "LC_ADDRESS", "lang_ab");
197 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
200 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
201 if (strcmp (address
->lang_ab
, iso639
[cnt
].ab
) == 0)
203 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
205 %s: language abbreviation `%s' not defined"),
206 "LC_ADDRESS", address
->lang_ab
);
209 if (strcmp (iso639
[cnt
].ab
, address
->lang_ab
) != 0)
211 %s: `%s' value does not match `%s' value"),
212 "LC_ADDRESS", "lang_ab", "lang_term");
215 if (address
->lang_lib
== NULL
)
216 /* This is no error. */
217 address
->lang_lib
= address
->lang_term
;
218 else if (address
->lang_lib
[0] == '\0')
221 error (0, 0, _("%s: field `%s' must not be empty"),
222 "LC_ADDRESS", "lang_lib");
226 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
228 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
229 if (strcmp (address
->lang_lib
, iso639
[cnt
].lib
) == 0)
231 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
233 %s: language abbreviation `%s' not defined"),
234 "LC_ADDRESS", address
->lang_lib
);
237 if (strcmp (iso639
[cnt
].ab
, address
->lang_ab
) != 0)
239 %s: `%s' value does not match `%s' value"), "LC_ADDRESS", "lang_lib",
240 helper
== 1 ? "lang_term" : "lang_ab");
243 if (address
->country_num
== 0)
246 error (0, 0, _("%s: field `%s' not defined"),
247 "LC_ADDRESS", "country_num");
248 cnt
= sizeof (iso3166
) / sizeof (iso3166
[0]);
252 for (cnt
= 0; cnt
< sizeof (iso3166
) / sizeof (iso3166
[0]); ++cnt
)
253 if (address
->country_num
== iso3166
[cnt
].num
)
256 if (cnt
== sizeof (iso3166
) / sizeof (iso3166
[0]))
258 %s: numeric country code `%d' not valid"),
259 "LC_ADDRESS", address
->country_num
);
261 address
->country_num_ob
= bswap_32 (address
->country_num
);
263 if (address
->country_ab2
== NULL
)
266 error (0, 0, _("%s: field `%s' not defined"),
267 "LC_ADDRESS", "country_ab2");
268 address
->country_ab2
= " ";
270 else if (cnt
!= sizeof (iso3166
) / sizeof (iso3166
[0])
271 && strcmp (address
->country_ab2
, iso3166
[cnt
].ab2
) != 0)
272 error (0, 0, _("%s: `%s' value does not match `%s' value"),
273 "LC_ADDRESS", "country_ab2", "country_num");
275 if (address
->country_ab3
== NULL
)
278 error (0, 0, _("%s: field `%s' not defined"),
279 "LC_ADDRESS", "country_ab3");
280 address
->country_ab3
= " ";
282 else if (cnt
!= sizeof (iso3166
) / sizeof (iso3166
[0])
283 && strcmp (address
->country_ab3
, iso3166
[cnt
].ab3
) != 0)
284 error (0, 0, _("%s: `%s' value does not match `%s' value"),
285 "LC_ADDRESS", "country_ab3", "country_num");
290 address_output (struct localedef_t
*locale
, struct charmap_t
*charmap
,
291 const char *output_path
)
293 struct locale_address_t
*address
= locale
->categories
[LC_ADDRESS
].address
;
294 struct iovec iov
[2 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
)];
295 struct locale_file data
;
296 uint32_t idx
[_NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
)];
299 data
.magic
= LIMAGIC (LC_ADDRESS
);
300 data
.n
= _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
);
301 iov
[cnt
].iov_base
= (void *) &data
;
302 iov
[cnt
].iov_len
= sizeof (data
);
305 iov
[cnt
].iov_base
= (void *) idx
;
306 iov
[cnt
].iov_len
= sizeof (idx
);
309 idx
[cnt
- 2] = iov
[0].iov_len
+ iov
[1].iov_len
;
310 iov
[cnt
].iov_base
= (void *) address
->postal_fmt
;
311 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
314 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
315 iov
[cnt
].iov_base
= (void *) address
->country_name
;
316 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
319 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
320 iov
[cnt
].iov_base
= (void *) address
->country_post
;
321 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
324 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
325 iov
[cnt
].iov_base
= (void *) address
->country_ab2
;
326 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
329 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
330 iov
[cnt
].iov_base
= (void *) address
->country_ab3
;
331 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
334 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
335 iov
[cnt
].iov_base
= (void *) address
->country_car
;
336 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
339 #if __BYTE_ORDER == __LITTLE_ENDIAN
340 # define country_num_eb country_num_ob
341 # define country_num_el country_num
343 # define country_num_eb country_num
344 # define country_num_el country_num_ob
347 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
348 iov
[cnt
].iov_base
= (void *) address
->country_num_eb
;
349 iov
[cnt
].iov_len
= sizeof (uint32_t);
352 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
353 iov
[cnt
].iov_base
= (void *) address
->country_num_el
;
354 iov
[cnt
].iov_len
= sizeof (uint32_t);
357 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
358 iov
[cnt
].iov_base
= (void *) address
->country_isbn
;
359 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
362 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
363 iov
[cnt
].iov_base
= (void *) address
->lang_name
;
364 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
367 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
368 iov
[cnt
].iov_base
= (void *) address
->lang_ab
;
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
->lang_term
;
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
->lang_lib
;
379 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
382 assert (cnt
== 2 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
));
384 write_locale_data (output_path
, "LC_ADDRESS",
385 2 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
), iov
);
389 /* The parser for the LC_ADDRESS section of the locale definition. */
391 address_read (struct linereader
*ldfile
, struct localedef_t
*result
,
392 struct charmap_t
*charmap
, const char *repertoire_name
,
395 struct repertoire_t
*repertoire
= NULL
;
396 struct locale_address_t
*address
;
401 /* Get the repertoire we have to use. */
402 if (repertoire_name
!= NULL
)
403 repertoire
= repertoire_read (repertoire_name
);
405 /* The rest of the line containing `LC_ADDRESS' must be free. */
406 lr_ignore_rest (ldfile
, 1);
411 now
= lr_token (ldfile
, charmap
, NULL
);
414 while (nowtok
== tok_eol
);
416 /* If we see `copy' now we are almost done. */
417 if (nowtok
== tok_copy
)
419 handle_copy (ldfile
, charmap
, repertoire
, tok_lc_address
, LC_ADDRESS
,
420 "LC_ADDRESS", ignore_content
);
424 /* Prepare the data structures. */
425 address_startup (ldfile
, result
, ignore_content
);
426 address
= result
->categories
[LC_ADDRESS
].address
;
430 /* Of course we don't proceed beyond the end of file. */
431 if (nowtok
== tok_eof
)
434 /* Ingore empty lines. */
435 if (nowtok
== tok_eol
)
437 now
= lr_token (ldfile
, charmap
, NULL
);
444 #define STR_ELEM(cat) \
446 arg = lr_token (ldfile, charmap, NULL); \
447 if (arg->tok != tok_string) \
449 if (address->cat != NULL) \
450 lr_error (ldfile, _("\
451 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
452 else if (!ignore_content && arg->val.str.startmb == NULL) \
454 lr_error (ldfile, _("\
455 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
458 else if (!ignore_content) \
459 address->cat = arg->val.str.startmb; \
462 STR_ELEM (postal_fmt
);
463 STR_ELEM (country_name
);
464 STR_ELEM (country_post
);
465 STR_ELEM (country_ab2
);
466 STR_ELEM (country_ab3
);
467 STR_ELEM (country_car
);
468 STR_ELEM (country_isbn
);
469 STR_ELEM (lang_name
);
471 STR_ELEM (lang_term
);
474 #define INT_ELEM(cat) \
476 arg = lr_token (ldfile, charmap, NULL); \
477 if (arg->tok != tok_number) \
479 else if (address->cat != 0) \
480 lr_error (ldfile, _("\
481 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
482 else if (!ignore_content) \
483 address->cat = arg->val.num; \
486 INT_ELEM (country_num
);
489 /* Next we assume `LC_ADDRESS'. */
490 arg
= lr_token (ldfile
, charmap
, NULL
);
491 if (arg
->tok
== tok_eof
)
493 if (arg
->tok
== tok_eol
)
494 lr_error (ldfile
, _("%s: incomplete `END' line"),
496 else if (arg
->tok
!= tok_lc_address
)
497 lr_error (ldfile
, _("\
498 %1$s: definition does not end with `END %1$s'"), "LC_ADDRESS");
499 lr_ignore_rest (ldfile
, arg
->tok
== tok_lc_address
);
504 SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
507 /* Prepare for the next round. */
508 now
= lr_token (ldfile
, charmap
, NULL
);
512 /* When we come here we reached the end of the file. */
513 lr_error (ldfile
, _("%s: premature end of file"), "LC_ADDRESS");