1 /* Copyright (C) 1998-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; version 2 of the License, or
7 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
29 #include "localedef.h"
30 #include "localeinfo.h"
41 #define DEFINE_COUNTRY_CODE(Name, Ab2, Ab3, Num) \
43 #include "iso-3166.def"
54 #define DEFINE_LANGUAGE_CODE(Name, Ab, Term, Lib) \
56 #define DEFINE_LANGUAGE_CODE3(Name, Term, Lib) \
58 #define DEFINE_LANGUAGE_CODE2(Name, Term) \
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 No definition for %s category found"), "LC_ADDRESS");
134 address_startup (NULL
, locale
, 0);
135 address
= locale
->categories
[LC_ADDRESS
].address
;
140 if (address
->postal_fmt
== NULL
)
143 record_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";
150 /* We must check whether the format string contains only the allowed
151 escape sequences. Last checked against ISO 30112 WD10 [2014]. */
152 const char *cp
= address
->postal_fmt
;
155 record_error (0, 0, _("%s: field `%s' must not be empty"),
156 "LC_ADDRESS", "postal_fmt");
165 if (strchr ("nafdbshNtreClzTSc%", *cp
) == NULL
)
167 record_error (0, 0, _("\
168 %s: invalid escape `%%%c' sequence in field `%s'"),
169 "LC_ADDRESS", *cp
, "postal_fmt");
177 #define TEST_ELEM(cat) \
178 if (address->cat == NULL) \
180 if (verbose && ! nothing) \
181 record_warning (_("%s: field `%s' not defined"), "LC_ADDRESS", #cat); \
185 TEST_ELEM (country_name
);
186 /* XXX Test against list of defined codes. */
187 TEST_ELEM (country_post
);
188 /* XXX Test against list of defined codes. */
189 TEST_ELEM (country_car
);
190 /* XXX Test against list of defined codes. */
191 TEST_ELEM (country_isbn
);
192 TEST_ELEM (lang_name
);
195 if (address
->lang_term
== NULL
)
197 if (verbose
&& ! nothing
)
198 record_warning (_("%s: field `%s' not defined"), "LC_ADDRESS",
200 address
->lang_term
= "";
201 cnt
= sizeof (iso639
) / sizeof (iso639
[0]);
203 else if (address
->lang_term
[0] == '\0')
206 record_warning (_("%s: field `%s' must not be empty"), "LC_ADDRESS",
208 cnt
= sizeof (iso639
) / sizeof (iso639
[0]);
212 /* Look for this language in the table. */
213 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
214 if (strcmp (address
->lang_term
, iso639
[cnt
].term
) == 0)
216 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
217 record_error (0, 0, _("\
218 %s: terminology language code `%s' not defined"),
219 "LC_ADDRESS", address
->lang_term
);
222 if (address
->lang_ab
== NULL
)
224 if ((cnt
== sizeof (iso639
) / sizeof (iso639
[0])
225 || iso639
[cnt
].ab
[0] != '\0')
226 && verbose
&& ! nothing
)
227 record_warning (_("%s: field `%s' not defined"), "LC_ADDRESS",
229 address
->lang_ab
= "";
231 else if (address
->lang_ab
[0] == '\0')
233 if ((cnt
== sizeof (iso639
) / sizeof (iso639
[0])
234 || iso639
[cnt
].ab
[0] != '\0')
236 record_warning (_("%s: field `%s' must not be empty"),
237 "LC_ADDRESS", "lang_ab");
239 else if (cnt
< sizeof (iso639
) / sizeof (iso639
[0])
240 && iso639
[cnt
].ab
[0] == '\0')
242 record_error (0, 0, _("%s: field `%s' must not be defined"),
243 "LC_ADDRESS", "lang_ab");
245 address
->lang_ab
= "";
249 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
252 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
253 if (strcmp (address
->lang_ab
, iso639
[cnt
].ab
) == 0)
255 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
256 record_error (0, 0, _("\
257 %s: language abbreviation `%s' not defined"),
258 "LC_ADDRESS", address
->lang_ab
);
261 if (strcmp (iso639
[cnt
].ab
, address
->lang_ab
) != 0
262 && iso639
[cnt
].ab
[0] != '\0')
263 record_error (0, 0, _("\
264 %s: `%s' value does not match `%s' value"),
265 "LC_ADDRESS", "lang_ab", "lang_term");
268 if (address
->lang_lib
== NULL
)
269 /* This is no error. */
270 address
->lang_lib
= address
->lang_term
;
271 else if (address
->lang_lib
[0] == '\0')
274 record_warning (_("%s: field `%s' must not be empty"),
275 "LC_ADDRESS", "lang_lib");
279 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
281 for (cnt
= 0; cnt
< sizeof (iso639
) / sizeof (iso639
[0]); ++cnt
)
282 if (strcmp (address
->lang_lib
, iso639
[cnt
].lib
) == 0)
284 if (cnt
== sizeof (iso639
) / sizeof (iso639
[0]))
285 record_error (0, 0, _("\
286 %s: language abbreviation `%s' not defined"),
287 "LC_ADDRESS", address
->lang_lib
);
290 if (strcmp (iso639
[cnt
].ab
, address
->lang_ab
) != 0)
291 record_error (0, 0, _("\
292 %s: `%s' value does not match `%s' value"), "LC_ADDRESS", "lang_lib",
293 helper
== 1 ? "lang_term" : "lang_ab");
296 if (address
->country_num
== 0)
298 if (verbose
&& ! nothing
)
299 record_warning (_("%s: field `%s' not defined"), "LC_ADDRESS",
301 cnt
= sizeof (iso3166
) / sizeof (iso3166
[0]);
305 for (cnt
= 0; cnt
< sizeof (iso3166
) / sizeof (iso3166
[0]); ++cnt
)
306 if (address
->country_num
== iso3166
[cnt
].num
)
309 if (cnt
== sizeof (iso3166
) / sizeof (iso3166
[0]))
310 record_error (0, 0, _("\
311 %s: numeric country code `%d' not valid"),
312 "LC_ADDRESS", address
->country_num
);
315 if (address
->country_ab2
== NULL
)
317 if (verbose
&& ! nothing
)
318 record_warning (_("%s: field `%s' not defined"), "LC_ADDRESS",
320 address
->country_ab2
= " ";
322 else if (cnt
!= sizeof (iso3166
) / sizeof (iso3166
[0])
323 && strcmp (address
->country_ab2
, iso3166
[cnt
].ab2
) != 0)
324 record_error (0, 0, _("%s: `%s' value does not match `%s' value"),
325 "LC_ADDRESS", "country_ab2", "country_num");
327 if (address
->country_ab3
== NULL
)
329 if (verbose
&& ! nothing
)
330 record_warning (_("%s: field `%s' not defined"), "LC_ADDRESS",
332 address
->country_ab3
= " ";
334 else if (cnt
!= sizeof (iso3166
) / sizeof (iso3166
[0])
335 && strcmp (address
->country_ab3
, iso3166
[cnt
].ab3
) != 0)
336 record_error (0, 0, _("\
337 %s: `%s' value does not match `%s' value"),
338 "LC_ADDRESS", "country_ab3", "country_num");
343 address_output (struct localedef_t
*locale
, const struct charmap_t
*charmap
,
344 const char *output_path
)
346 struct locale_address_t
*address
= locale
->categories
[LC_ADDRESS
].address
;
347 struct locale_file file
;
349 init_locale_data (&file
, _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS
));
350 add_locale_string (&file
, address
->postal_fmt
);
351 add_locale_string (&file
, address
->country_name
);
352 add_locale_string (&file
, address
->country_post
);
353 add_locale_string (&file
, address
->country_ab2
);
354 add_locale_string (&file
, address
->country_ab3
);
355 add_locale_string (&file
, address
->country_car
);
356 add_locale_uint32 (&file
, address
->country_num
);
357 add_locale_string (&file
, address
->country_isbn
);
358 add_locale_string (&file
, address
->lang_name
);
359 add_locale_string (&file
, address
->lang_ab
);
360 add_locale_string (&file
, address
->lang_term
);
361 add_locale_string (&file
, address
->lang_lib
);
362 add_locale_string (&file
, charmap
->code_set_name
);
363 write_locale_data (output_path
, LC_ADDRESS
, "LC_ADDRESS", &file
);
367 /* The parser for the LC_ADDRESS section of the locale definition. */
369 address_read (struct linereader
*ldfile
, struct localedef_t
*result
,
370 const struct charmap_t
*charmap
, const char *repertoire_name
,
373 struct locale_address_t
*address
;
378 /* The rest of the line containing `LC_ADDRESS' must be free. */
379 lr_ignore_rest (ldfile
, 1);
384 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
387 while (nowtok
== tok_eol
);
389 /* If we see `copy' now we are almost done. */
390 if (nowtok
== tok_copy
)
392 handle_copy (ldfile
, charmap
, repertoire_name
, result
, tok_lc_address
,
393 LC_ADDRESS
, "LC_ADDRESS", ignore_content
);
397 /* Prepare the data structures. */
398 address_startup (ldfile
, result
, ignore_content
);
399 address
= result
->categories
[LC_ADDRESS
].address
;
403 /* Of course we don't proceed beyond the end of file. */
404 if (nowtok
== tok_eof
)
407 /* Ignore empty lines. */
408 if (nowtok
== tok_eol
)
410 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
417 #define STR_ELEM(cat) \
419 /* Ignore the rest of the line if we don't need the input of \
421 if (ignore_content) \
423 lr_ignore_rest (ldfile, 0); \
427 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
428 if (arg->tok != tok_string) \
430 if (address->cat != NULL) \
431 lr_error (ldfile, _("\
432 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
433 else if (!ignore_content && arg->val.str.startmb == NULL) \
435 lr_error (ldfile, _("\
436 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
439 else if (!ignore_content) \
440 address->cat = arg->val.str.startmb; \
443 STR_ELEM (postal_fmt
);
444 STR_ELEM (country_name
);
445 STR_ELEM (country_post
);
446 STR_ELEM (country_ab2
);
447 STR_ELEM (country_ab3
);
448 STR_ELEM (country_car
);
449 STR_ELEM (lang_name
);
451 STR_ELEM (lang_term
);
454 #define INT_STR_ELEM(cat) \
456 /* Ignore the rest of the line if we don't need the input of \
458 if (ignore_content) \
460 lr_ignore_rest (ldfile, 0); \
464 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
465 if (arg->tok != tok_string && arg->tok != tok_number) \
467 if (address->cat != NULL) \
468 lr_error (ldfile, _("\
469 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
470 else if (!ignore_content && arg->tok == tok_string \
471 && arg->val.str.startmb == NULL) \
473 lr_error (ldfile, _("\
474 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
477 else if (!ignore_content) \
479 if (arg->tok == tok_string) \
480 address->cat = arg->val.str.startmb; \
483 char *numbuf = (char *) xmalloc (21); \
484 snprintf (numbuf, 21, "%ld", arg->val.num); \
485 address->cat = numbuf; \
490 INT_STR_ELEM (country_isbn
);
492 #define INT_ELEM(cat) \
494 /* Ignore the rest of the line if we don't need the input of \
496 if (ignore_content) \
498 lr_ignore_rest (ldfile, 0); \
502 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
503 if (arg->tok != tok_number) \
505 else if (address->cat != 0) \
506 lr_error (ldfile, _("\
507 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat); \
508 else if (!ignore_content) \
509 address->cat = arg->val.num; \
512 INT_ELEM (country_num
);
515 /* Next we assume `LC_ADDRESS'. */
516 arg
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
517 if (arg
->tok
== tok_eof
)
519 if (arg
->tok
== tok_eol
)
520 lr_error (ldfile
, _("%s: incomplete `END' line"),
522 else if (arg
->tok
!= tok_lc_address
)
523 lr_error (ldfile
, _("\
524 %1$s: definition does not end with `END %1$s'"), "LC_ADDRESS");
525 lr_ignore_rest (ldfile
, arg
->tok
== tok_lc_address
);
530 SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
533 /* Prepare for the next round. */
534 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
538 /* When we come here we reached the end of the file. */
539 lr_error (ldfile
, _("%s: premature end of file"), "LC_ADDRESS");