]>
git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/ld-address.c
1 /* Copyright (C) 1998-2017 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, see <http://www.gnu.org/licenses/>. */
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 #define DEFINE_LANGUAGE_CODE2(Name, Term) \
61 #include "iso-639.def"
65 /* The real definition of the struct for the LC_ADDRESS locale. */
66 struct locale_address_t
68 const char *postal_fmt
;
69 const char *country_name
;
70 const char *country_post
;
71 const char *country_ab2
;
72 const char *country_ab3
;
74 const char *country_car
;
75 const char *country_isbn
;
76 const char *lang_name
;
78 const char *lang_term
;
84 address_startup (struct linereader
*lr
, struct localedef_t
*locale
,
88 locale
->categories
[LC_ADDRESS
].address
=
89 (struct locale_address_t
*) xcalloc (1,
90 sizeof (struct locale_address_t
));
94 lr
->translate_strings
= 1;
95 lr
->return_widestr
= 0;
101 address_finish (struct localedef_t
*locale
, const struct charmap_t
*charmap
)
103 struct locale_address_t
*address
= locale
->categories
[LC_ADDRESS
].address
;
108 /* Now resolve copying and also handle completely missing definitions. */
111 /* First see whether we were supposed to copy. If yes, find the
112 actual definition. */
113 if (locale
->copy_name
[LC_ADDRESS
] != NULL
)
115 /* Find the copying locale. This has to happen transitively since
116 the locale we are copying from might also copying another one. */
117 struct localedef_t
*from
= locale
;
120 from
= find_locale (LC_ADDRESS
, from
->copy_name
[LC_ADDRESS
],
121 from
->repertoire_name
, charmap
);
122 while (from
->categories
[LC_ADDRESS
].address
== NULL
123 && from
->copy_name
[LC_ADDRESS
] != NULL
);
125 address
= locale
->categories
[LC_ADDRESS
].address
126 = from
->categories
[LC_ADDRESS
].address
;
129 /* If there is still no definition issue an warning and create an
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 record_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 allowed
152 escape sequences. Last checked against ISO 30112 WD10 [2014]. */
153 const char *cp
= address
->postal_fmt
;
156 record_error (0, 0, _("%s: field `%s' must not be empty"),
157 "LC_ADDRESS", "postal_fmt");
166 if (strchr ("nafdbshNtreClzTSc%", *cp
) == NULL
)
168 record_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 record_warning (_("%s: field `%s' not defined"), "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 record_warning (_("%s: field `%s' not defined"), "LC_ADDRESS",
201 address
->lang_term
= "";
202 cnt
= sizeof (iso639
) / sizeof (iso639
[0]);
204 else if (address
->lang_term
[0] == '\0')
207 record_warning (_("%s: field `%s' must not be empty"), "LC_ADDRESS",
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 record_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 ((cnt
== sizeof (iso639
) / sizeof (iso639
[0])
226 || iso639
[cnt
].ab
[0] != '\0')
227 && verbose
&& ! nothing
)
228 record_warning (_("%s: field `%s' not defined"), "LC_ADDRESS",
230 address
->lang_ab
= "";
232 else if (address
->lang_ab
[0] == '\0')
234 if ((cnt
== sizeof (iso639
) / sizeof (iso639
[0])
235 || iso639
[cnt
].ab
[0] != '\0')
237 record_warning (_("%s: field `%s' must not be empty"),
238 "LC_ADDRESS", "lang_ab");
240 else if (cnt
< sizeof (iso639
) / sizeof (iso639
[0])
241 && iso639
[cnt
].ab
[0] == '\0')
243 record_error (0, 0, _("%s: field `%s' must not be defined"),
244 "LC_ADDRESS", "lang_ab");
246 address
->lang_ab
= "";
250 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
253 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
254 if (strcmp (address
->lang_ab
, iso639
[cnt
].ab
) == 0)
256 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
257 record_error (0, 0, _("\
258 %s: language abbreviation `%s' not defined"),
259 "LC_ADDRESS", address
->lang_ab
);
262 if (strcmp (iso639
[cnt
].ab
, address
->lang_ab
) != 0
263 && iso639
[cnt
].ab
[0] != '\0')
264 record_error (0, 0, _("\
265 %s: `%s' value does not match `%s' value"),
266 "LC_ADDRESS", "lang_ab", "lang_term");
269 if (address
->lang_lib
== NULL
)
270 /* This is no error. */
271 address
->lang_lib
= address
->lang_term
;
272 else if (address
->lang_lib
[0] == '\0')
275 record_warning (_("%s: field `%s' must not be empty"),
276 "LC_ADDRESS", "lang_lib");
280 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
282 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
283 if (strcmp (address
->lang_lib
, iso639
[cnt
].lib
) == 0)
285 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
286 record_error (0, 0, _("\
287 %s: language abbreviation `%s' not defined"),
288 "LC_ADDRESS", address
->lang_lib
);
291 if (strcmp (iso639
[cnt
].ab
, address
->lang_ab
) != 0)
292 record_error (0, 0, _("\
293 %s: `%s' value does not match `%s' value"), "LC_ADDRESS", "lang_lib",
294 helper
== 1 ? "lang_term" : "lang_ab");
297 if (address
->country_num
== 0)
299 if (verbose
&& ! nothing
)
300 record_warning (_("%s: field `%s' not defined"), "LC_ADDRESS",
302 cnt
= sizeof (iso3166
) / sizeof (iso3166
[0]);
306 for (cnt
= 0; cnt
< sizeof (iso3166
) / sizeof (iso3166
[0]); ++cnt
)
307 if (address
->country_num
== iso3166
[cnt
].num
)
310 if (cnt
== sizeof (iso3166
) / sizeof (iso3166
[0]))
311 record_error (0, 0, _("\
312 %s: numeric country code `%d' not valid"),
313 "LC_ADDRESS", address
->country_num
);
316 if (address
->country_ab2
== NULL
)
318 if (verbose
&& ! nothing
)
319 record_warning (_("%s: field `%s' not defined"), "LC_ADDRESS",
321 address
->country_ab2
= " ";
323 else if (cnt
!= sizeof (iso3166
) / sizeof (iso3166
[0])
324 && strcmp (address
->country_ab2
, iso3166
[cnt
].ab2
) != 0)
325 record_error (0, 0, _("%s: `%s' value does not match `%s' value"),
326 "LC_ADDRESS", "country_ab2", "country_num");
328 if (address
->country_ab3
== NULL
)
330 if (verbose
&& ! nothing
)
331 record_warning (_("%s: field `%s' not defined"), "LC_ADDRESS",
333 address
->country_ab3
= " ";
335 else if (cnt
!= sizeof (iso3166
) / sizeof (iso3166
[0])
336 && strcmp (address
->country_ab3
, iso3166
[cnt
].ab3
) != 0)
337 record_error (0, 0, _("\
338 %s: `%s' value does not match `%s' value"),
339 "LC_ADDRESS", "country_ab3", "country_num");
344 address_output (struct localedef_t
*locale
, const struct charmap_t
*charmap
,
345 const char *output_path
)
347 struct locale_address_t
*address
= locale
->categories
[LC_ADDRESS
].address
;
348 struct locale_file file
;
350 init_locale_data (&file
, _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
));
351 add_locale_string (&file
, address
->postal_fmt
);
352 add_locale_string (&file
, address
->country_name
);
353 add_locale_string (&file
, address
->country_post
);
354 add_locale_string (&file
, address
->country_ab2
);
355 add_locale_string (&file
, address
->country_ab3
);
356 add_locale_string (&file
, address
->country_car
);
357 add_locale_uint32 (&file
, address
->country_num
);
358 add_locale_string (&file
, address
->country_isbn
);
359 add_locale_string (&file
, address
->lang_name
);
360 add_locale_string (&file
, address
->lang_ab
);
361 add_locale_string (&file
, address
->lang_term
);
362 add_locale_string (&file
, address
->lang_lib
);
363 add_locale_string (&file
, charmap
->code_set_name
);
364 write_locale_data (output_path
, LC_ADDRESS
, "LC_ADDRESS", &file
);
368 /* The parser for the LC_ADDRESS section of the locale definition. */
370 address_read (struct linereader
*ldfile
, struct localedef_t
*result
,
371 const struct charmap_t
*charmap
, const char *repertoire_name
,
374 struct locale_address_t
*address
;
379 /* The rest of the line containing `LC_ADDRESS' must be free. */
380 lr_ignore_rest (ldfile
, 1);
385 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
388 while (nowtok
== tok_eol
);
390 /* If we see `copy' now we are almost done. */
391 if (nowtok
== tok_copy
)
393 handle_copy (ldfile
, charmap
, repertoire_name
, result
, tok_lc_address
,
394 LC_ADDRESS
, "LC_ADDRESS", ignore_content
);
398 /* Prepare the data structures. */
399 address_startup (ldfile
, result
, ignore_content
);
400 address
= result
->categories
[LC_ADDRESS
].address
;
404 /* Of course we don't proceed beyond the end of file. */
405 if (nowtok
== tok_eof
)
408 /* Ignore empty lines. */
409 if (nowtok
== tok_eol
)
411 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
418 #define STR_ELEM(cat) \
420 /* Ignore the rest of the line if we don't need the input of \
422 if (ignore_content) \
424 lr_ignore_rest (ldfile, 0); \
428 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
429 if (arg->tok != tok_string) \
431 if (address->cat != NULL) \
432 lr_error (ldfile, _("\
433 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
434 else if (!ignore_content && arg->val.str.startmb == NULL) \
436 lr_error (ldfile, _("\
437 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
440 else if (!ignore_content) \
441 address->cat = arg->val.str.startmb; \
444 STR_ELEM (postal_fmt
);
445 STR_ELEM (country_name
);
446 STR_ELEM (country_post
);
447 STR_ELEM (country_ab2
);
448 STR_ELEM (country_ab3
);
449 STR_ELEM (country_car
);
450 STR_ELEM (lang_name
);
452 STR_ELEM (lang_term
);
455 #define INT_STR_ELEM(cat) \
457 /* Ignore the rest of the line if we don't need the input of \
459 if (ignore_content) \
461 lr_ignore_rest (ldfile, 0); \
465 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
466 if (arg->tok != tok_string && arg->tok != tok_number) \
468 if (address->cat != NULL) \
469 lr_error (ldfile, _("\
470 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
471 else if (!ignore_content && arg->tok == tok_string \
472 && arg->val.str.startmb == NULL) \
474 lr_error (ldfile, _("\
475 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
478 else if (!ignore_content) \
480 if (arg->tok == tok_string) \
481 address->cat = arg->val.str.startmb; \
484 char *numbuf = (char *) xmalloc (21); \
485 snprintf (numbuf, 21, "%ld", arg->val.num); \
486 address->cat = numbuf; \
491 INT_STR_ELEM (country_isbn
);
493 #define INT_ELEM(cat) \
495 /* Ignore the rest of the line if we don't need the input of \
497 if (ignore_content) \
499 lr_ignore_rest (ldfile, 0); \
503 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
504 if (arg->tok != tok_number) \
506 else if (address->cat != 0) \
507 lr_error (ldfile, _("\
508 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
509 else if (!ignore_content) \
510 address->cat = arg->val.num; \
513 INT_ELEM (country_num
);
516 /* Next we assume `LC_ADDRESS'. */
517 arg
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
518 if (arg
->tok
== tok_eof
)
520 if (arg
->tok
== tok_eol
)
521 lr_error (ldfile
, _("%s: incomplete `END' line"),
523 else if (arg
->tok
!= tok_lc_address
)
524 lr_error (ldfile
, _("\
525 %1$s: definition does not end with `END %1$s'"), "LC_ADDRESS");
526 lr_ignore_rest (ldfile
, arg
->tok
== tok_lc_address
);
531 SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
534 /* Prepare for the next round. */
535 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
539 /* When we come here we reached the end of the file. */
540 lr_error (ldfile
, _("%s: premature end of file"), "LC_ADDRESS");