]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/ld-address.c
Update.
[thirdparty/glibc.git] / 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.
4
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.
9
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.
14
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. */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <byteswap.h>
25 #include <error.h>
26 #include <langinfo.h>
27 #include <string.h>
28 #include <sys/uio.h>
29
30 #include <assert.h>
31
32 #include "localeinfo.h"
33 #include "locfile.h"
34
35
36 static struct
37 {
38 const char ab2[2];
39 const char ab3[3];
40 uint32_t num;
41 } iso3166[] =
42 {
43 #define DEFINE_COUNTRY_CODE(Name, Ab2, Ab3, Num) \
44 { #Ab2, #Ab3, Num },
45 #include "iso-3166.def"
46 };
47
48
49 static struct
50 {
51 const char ab[2];
52 const char term[3];
53 const char lib[3];
54 } iso639[] =
55 {
56 #define DEFINE_LANGUAGE_CODE(Name, Ab, Term, Lib) \
57 { #Ab, #Term, #Lib },
58 #include "iso-639.def"
59 };
60
61
62 /* The real definition of the struct for the LC_ADDRESS locale. */
63 struct locale_address_t
64 {
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;
70 uint32_t country_num;
71 uint32_t country_num_ob;
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;
78 };
79
80
81 static void
82 address_startup (struct linereader *lr, struct localedef_t *locale,
83 int ignore_content)
84 {
85 if (!ignore_content)
86 locale->categories[LC_ADDRESS].address =
87 (struct locale_address_t *) xcalloc (1,
88 sizeof (struct locale_address_t));
89
90 lr->translate_strings = 1;
91 lr->return_widestr = 0;
92 }
93
94
95 void
96 address_finish (struct localedef_t *locale, struct charmap_t *charmap)
97 {
98 struct locale_address_t *address = locale->categories[LC_ADDRESS].address;
99 size_t cnt;
100 int helper;
101
102 if (address->postal_fmt == NULL)
103 {
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";
108 }
109 else
110 {
111 /* We must check whether the format string contains only the
112 allowed escape sequences. */
113 const char *cp = address->postal_fmt;
114
115 if (*cp == '\0')
116 error (0, 0, _("%s: field `%s' must not be empty"),
117 "LC_ADDRESS", "postal_fmt");
118 else
119 while (*cp != '\0')
120 {
121 if (*cp == '%')
122 {
123 if (*++cp == 'R')
124 /* Romanize-flag. */
125 ++cp;
126 if (strchr ("afdbshNtreCzTc%", *cp) == NULL)
127 {
128 error (0, 0, _("\
129 %s: invalid escape `%%%c' sequence in field `%s'"),
130 "LC_ADDRESS", *cp, "postal_fmt");
131 break;
132 }
133 }
134 ++cp;
135 }
136 }
137
138 #define TEST_ELEM(cat) \
139 if (address->cat == NULL) \
140 { \
141 if (verbose) \
142 error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS", #cat); \
143 address->cat = ""; \
144 }
145
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);
154
155 helper = 1;
156 if (address->lang_term == NULL)
157 {
158 if (verbose)
159 error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS",
160 "lang_term");
161 address->lang_term = "";
162 cnt = sizeof (iso639) / sizeof (iso639[0]);
163 }
164 else if (address->lang_term[0] == '\0')
165 {
166 if (verbose)
167 error (0, 0, _("%s: field `%s' must not be empty"),
168 "LC_ADDRESS", "lang_term");
169 cnt = sizeof (iso639) / sizeof (iso639[0]);
170 }
171 else
172 {
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)
176 break;
177 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
178 error (0, 0, _("\
179 %s: terminology language code `%s' not defined"),
180 "LC_ADDRESS", address->lang_term);
181 }
182
183 if (address->lang_ab == NULL)
184 {
185 if (verbose)
186 error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS", "lang_ab");
187 address->lang_ab = "";
188 }
189 else if (address->lang_ab[0] == '\0')
190 {
191 if (verbose)
192 error (0, 0, _("%s: field `%s' must not be empty"),
193 "LC_ADDRESS", "lang_ab");
194 }
195 else
196 {
197 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
198 {
199 helper = 2;
200 for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
201 if (strcmp (address->lang_ab, iso639[cnt].ab) == 0)
202 break;
203 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
204 error (0, 0, _("\
205 %s: language abbreviation `%s' not defined"),
206 "LC_ADDRESS", address->lang_ab);
207 }
208 else
209 if (strcmp (iso639[cnt].ab, address->lang_ab) != 0)
210 error (0, 0, _("\
211 %s: `%s' value does not match `%s' value"),
212 "LC_ADDRESS", "lang_ab", "lang_term");
213 }
214
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')
219 {
220 if (verbose)
221 error (0, 0, _("%s: field `%s' must not be empty"),
222 "LC_ADDRESS", "lang_lib");
223 }
224 else
225 {
226 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
227 {
228 for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
229 if (strcmp (address->lang_lib, iso639[cnt].lib) == 0)
230 break;
231 if (cnt == sizeof (iso639) / sizeof (iso639[0]))
232 error (0, 0, _("\
233 %s: language abbreviation `%s' not defined"),
234 "LC_ADDRESS", address->lang_lib);
235 }
236 else
237 if (strcmp (iso639[cnt].ab, address->lang_ab) != 0)
238 error (0, 0, _("\
239 %s: `%s' value does not match `%s' value"), "LC_ADDRESS", "lang_lib",
240 helper == 1 ? "lang_term" : "lang_ab");
241 }
242
243 if (address->country_num == 0)
244 {
245 if (verbose)
246 error (0, 0, _("%s: field `%s' not defined"),
247 "LC_ADDRESS", "country_num");
248 cnt = sizeof (iso3166) / sizeof (iso3166[0]);
249 }
250 else
251 {
252 for (cnt = 0; cnt < sizeof (iso3166) / sizeof (iso3166[0]); ++cnt)
253 if (address->country_num == iso3166[cnt].num)
254 break;
255
256 if (cnt == sizeof (iso3166) / sizeof (iso3166[0]))
257 error (0, 0, _("\
258 %s: numeric country code `%d' not valid"),
259 "LC_ADDRESS", address->country_num);
260 }
261 address->country_num_ob = bswap_32 (address->country_num);
262
263 if (address->country_ab2 == NULL)
264 {
265 if (verbose)
266 error (0, 0, _("%s: field `%s' not defined"),
267 "LC_ADDRESS", "country_ab2");
268 address->country_ab2 = " ";
269 }
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");
274
275 if (address->country_ab3 == NULL)
276 {
277 if (verbose)
278 error (0, 0, _("%s: field `%s' not defined"),
279 "LC_ADDRESS", "country_ab3");
280 address->country_ab3 = " ";
281 }
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");
286 }
287
288
289 void
290 address_output (struct localedef_t *locale, struct charmap_t *charmap,
291 const char *output_path)
292 {
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)];
297 size_t cnt = 0;
298
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);
303 ++cnt;
304
305 iov[cnt].iov_base = (void *) idx;
306 iov[cnt].iov_len = sizeof (idx);
307 ++cnt;
308
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;
312 ++cnt;
313
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;
317 ++cnt;
318
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;
322 ++cnt;
323
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;
327 ++cnt;
328
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;
332 ++cnt;
333
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;
337 ++cnt;
338
339 #if __BYTE_ORDER == __LITTLE_ENDIAN
340 # define country_num_eb country_num_ob
341 # define country_num_el country_num
342 #else
343 # define country_num_eb country_num
344 # define country_num_el country_num_ob
345 #endif
346
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);
350 ++cnt;
351
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);
355 ++cnt;
356
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;
360 ++cnt;
361
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;
365 ++cnt;
366
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;
370 ++cnt;
371
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;
375 ++cnt;
376
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;
380 ++cnt;
381
382 assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS));
383
384 write_locale_data (output_path, "LC_ADDRESS",
385 2 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS), iov);
386 }
387
388
389 /* The parser for the LC_ADDRESS section of the locale definition. */
390 void
391 address_read (struct linereader *ldfile, struct localedef_t *result,
392 struct charmap_t *charmap, const char *repertoire_name,
393 int ignore_content)
394 {
395 struct repertoire_t *repertoire = NULL;
396 struct locale_address_t *address;
397 struct token *now;
398 struct token *arg;
399 enum token_t nowtok;
400
401 /* Get the repertoire we have to use. */
402 if (repertoire_name != NULL)
403 repertoire = repertoire_read (repertoire_name);
404
405 /* The rest of the line containing `LC_ADDRESS' must be free. */
406 lr_ignore_rest (ldfile, 1);
407
408
409 do
410 {
411 now = lr_token (ldfile, charmap, NULL);
412 nowtok = now->tok;
413 }
414 while (nowtok == tok_eol);
415
416 /* If we see `copy' now we are almost done. */
417 if (nowtok == tok_copy)
418 {
419 handle_copy (ldfile, charmap, repertoire, tok_lc_address, LC_ADDRESS,
420 "LC_ADDRESS", ignore_content);
421 return;
422 }
423
424 /* Prepare the data structures. */
425 address_startup (ldfile, result, ignore_content);
426 address = result->categories[LC_ADDRESS].address;
427
428 while (1)
429 {
430 /* Of course we don't proceed beyond the end of file. */
431 if (nowtok == tok_eof)
432 break;
433
434 /* Ingore empty lines. */
435 if (nowtok == tok_eol)
436 {
437 now = lr_token (ldfile, charmap, NULL);
438 nowtok = now->tok;
439 continue;
440 }
441
442 switch (nowtok)
443 {
444 #define STR_ELEM(cat) \
445 case tok_##cat: \
446 arg = lr_token (ldfile, charmap, NULL); \
447 if (arg->tok != tok_string) \
448 goto err_label; \
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) \
453 { \
454 lr_error (ldfile, _("\
455 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat); \
456 address->cat = ""; \
457 } \
458 else if (!ignore_content) \
459 address->cat = arg->val.str.startmb; \
460 break
461
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);
470 STR_ELEM (lang_ab);
471 STR_ELEM (lang_term);
472 STR_ELEM (lang_lib);
473
474 #define INT_ELEM(cat) \
475 case tok_##cat: \
476 arg = lr_token (ldfile, charmap, NULL); \
477 if (arg->tok != tok_number) \
478 goto err_label; \
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; \
484 break
485
486 INT_ELEM (country_num);
487
488 case tok_end:
489 /* Next we assume `LC_ADDRESS'. */
490 arg = lr_token (ldfile, charmap, NULL);
491 if (arg->tok == tok_eof)
492 break;
493 if (arg->tok == tok_eol)
494 lr_error (ldfile, _("%s: incomplete `END' line"),
495 "LC_ADDRESS");
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);
500 return;
501
502 default:
503 err_label:
504 SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
505 }
506
507 /* Prepare for the next round. */
508 now = lr_token (ldfile, charmap, NULL);
509 nowtok = now->tok;
510 }
511
512 /* When we come here we reached the end of the file. */
513 lr_error (ldfile, _("%s: premature end of file"), "LC_ADDRESS");
514 }