Update.
[platform/upstream/glibc.git] / locale / programs / ld-address.c
1 /* Copyright (C) 1998, 1999, 2000, 2001 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[3];
39   const char ab3[4];
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[3];
52   const char term[4];
53   const char lib[4];
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   const char *country_car;
72   const char *country_isbn;
73   const char *lang_name;
74   const char *lang_ab;
75   const char *lang_term;
76   const char *lang_lib;
77 };
78
79
80 static void
81 address_startup (struct linereader *lr, struct localedef_t *locale,
82                  int ignore_content)
83 {
84   if (!ignore_content)
85     locale->categories[LC_ADDRESS].address =
86       (struct locale_address_t *) xcalloc (1,
87                                            sizeof (struct locale_address_t));
88
89   if (lr != NULL)
90     {
91       lr->translate_strings = 1;
92       lr->return_widestr = 0;
93     }
94 }
95
96
97 void
98 address_finish (struct localedef_t *locale, struct charmap_t *charmap)
99 {
100   struct locale_address_t *address = locale->categories[LC_ADDRESS].address;
101   size_t cnt;
102   int helper;
103   int nothing = 0;
104
105   /* Now resolve copying and also handle completely missing definitions.  */
106   if (address == NULL)
107     {
108       /* First see whether we were supposed to copy.  If yes, find the
109          actual definition.  */
110       if (locale->copy_name[LC_ADDRESS] != NULL)
111         {
112           /* Find the copying locale.  This has to happen transitively since
113              the locale we are copying from might also copying another one.  */
114           struct localedef_t *from = locale;
115
116           do
117             from = find_locale (LC_ADDRESS, from->copy_name[LC_ADDRESS],
118                                 from->repertoire_name, charmap);
119           while (from->categories[LC_ADDRESS].address == NULL
120                  && from->copy_name[LC_ADDRESS] != NULL);
121
122           address = locale->categories[LC_ADDRESS].address
123             = from->categories[LC_ADDRESS].address;
124         }
125
126       /* If there is still no definition issue an warning and create an
127          empty one.  */
128       if (address == NULL)
129         {
130           if (! be_quiet)
131             error (0, 0, _("No definition for %s category found"),
132                    "LC_ADDRESS");
133           address_startup (NULL, locale, 0);
134           address = locale->categories[LC_ADDRESS].address;
135           nothing = 1;
136         }
137     }
138
139   if (address->postal_fmt == NULL)
140     {
141       if (! nothing)
142         error (0, 0, _("%s: field `%s' not defined"),
143                "LC_ADDRESS", "postal_fmt");
144       /* Use as the default value the value of the i18n locale.  */
145       address->postal_fmt = "%a%N%f%N%d%N%b%N%s %h %e %r%N%C-%z %T%N%c%N";
146     }
147   else
148     {
149       /* We must check whether the format string contains only the
150          allowed escape sequences.  */
151       const char *cp = address->postal_fmt;
152
153       if (*cp == '\0')
154         error (0, 0, _("%s: field `%s' must not be empty"),
155                "LC_ADDRESS", "postal_fmt");
156       else
157         while (*cp != '\0')
158           {
159             if (*cp == '%')
160               {
161                 if (*++cp == 'R')
162                   /* Romanize-flag.  */
163                   ++cp;
164                 if (strchr ("afdbshNtreCzTSc%", *cp) == NULL)
165                   {
166                     error (0, 0, _("\
167 %s: invalid escape `%%%c' sequence in field `%s'"),
168                            "LC_ADDRESS", *cp, "postal_fmt");
169                     break;
170                   }
171               }
172             ++cp;
173           }
174     }
175
176 #define TEST_ELEM(cat) \
177   if (address->cat == NULL)                                                   \
178     {                                                                         \
179       if (verbose && ! nothing)                                               \
180         error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS", #cat);    \
181       address->cat = "";                                                      \
182     }
183
184   TEST_ELEM (country_name);
185   /* XXX Test against list of defined codes.  */
186   TEST_ELEM (country_post);
187   /* XXX Test against list of defined codes.  */
188   TEST_ELEM (country_car);
189   /* XXX Test against list of defined codes.  */
190   TEST_ELEM (country_isbn);
191   TEST_ELEM (lang_name);
192
193   helper = 1;
194   if (address->lang_term == NULL)
195     {
196       if (verbose && ! nothing)
197         error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS",
198                "lang_term");
199       address->lang_term = "";
200       cnt = sizeof (iso639) / sizeof (iso639[0]);
201     }
202   else if (address->lang_term[0] == '\0')
203     {
204       if (verbose)
205         error (0, 0, _("%s: field `%s' must not be empty"),
206                "LC_ADDRESS", "lang_term");
207       cnt = sizeof (iso639) / sizeof (iso639[0]);
208     }
209   else
210     {
211       /* Look for this language in the table.  */
212       for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
213         if (strcmp (address->lang_term, iso639[cnt].term) == 0)
214           break;
215       if (cnt == sizeof (iso639) / sizeof (iso639[0]))
216         error (0, 0, _("\
217 %s: terminology language code `%s' not defined"),
218                "LC_ADDRESS", address->lang_term);
219     }
220
221   if (address->lang_ab == NULL)
222     {
223       if (verbose && ! nothing)
224         error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS", "lang_ab");
225       address->lang_ab = "";
226     }
227   else if (address->lang_ab[0] == '\0')
228     {
229       if (verbose)
230         error (0, 0, _("%s: field `%s' must not be empty"),
231                "LC_ADDRESS", "lang_ab");
232     }
233   else
234     {
235       if (cnt == sizeof (iso639) / sizeof (iso639[0]))
236         {
237           helper = 2;
238           for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
239             if (strcmp (address->lang_ab, iso639[cnt].ab) == 0)
240               break;
241           if (cnt == sizeof (iso639) / sizeof (iso639[0]))
242             error (0, 0, _("\
243 %s: language abbreviation `%s' not defined"),
244                    "LC_ADDRESS", address->lang_ab);
245         }
246       else
247         if (strcmp (iso639[cnt].ab, address->lang_ab) != 0)
248           error (0, 0, _("\
249 %s: `%s' value does not match `%s' value"),
250                  "LC_ADDRESS", "lang_ab", "lang_term");
251     }
252
253   if (address->lang_lib == NULL)
254     /* This is no error.  */
255     address->lang_lib = address->lang_term;
256   else if (address->lang_lib[0] == '\0')
257     {
258       if (verbose)
259         error (0, 0, _("%s: field `%s' must not be empty"),
260                "LC_ADDRESS", "lang_lib");
261     }
262   else
263     {
264       if (cnt == sizeof (iso639) / sizeof (iso639[0]))
265         {
266           for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
267             if (strcmp (address->lang_lib, iso639[cnt].lib) == 0)
268               break;
269           if (cnt == sizeof (iso639) / sizeof (iso639[0]))
270             error (0, 0, _("\
271 %s: language abbreviation `%s' not defined"),
272                    "LC_ADDRESS", address->lang_lib);
273         }
274       else
275         if (strcmp (iso639[cnt].ab, address->lang_ab) != 0)
276           error (0, 0, _("\
277 %s: `%s' value does not match `%s' value"), "LC_ADDRESS", "lang_lib",
278                  helper == 1 ? "lang_term" : "lang_ab");
279     }
280
281   if (address->country_num == 0)
282     {
283       if (verbose && ! nothing)
284         error (0, 0, _("%s: field `%s' not defined"),
285                "LC_ADDRESS", "country_num");
286       cnt = sizeof (iso3166) / sizeof (iso3166[0]);
287     }
288   else
289     {
290       for (cnt = 0; cnt < sizeof (iso3166) / sizeof (iso3166[0]); ++cnt)
291         if (address->country_num == iso3166[cnt].num)
292           break;
293
294       if (cnt == sizeof (iso3166) / sizeof (iso3166[0]))
295         error (0, 0, _("\
296 %s: numeric country code `%d' not valid"),
297                "LC_ADDRESS", address->country_num);
298     }
299
300   if (address->country_ab2 == NULL)
301     {
302       if (verbose && ! nothing)
303         error (0, 0, _("%s: field `%s' not defined"),
304                "LC_ADDRESS", "country_ab2");
305       address->country_ab2 = "  ";
306     }
307   else if (cnt != sizeof (iso3166) / sizeof (iso3166[0])
308            && strcmp (address->country_ab2, iso3166[cnt].ab2) != 0)
309     error (0, 0, _("%s: `%s' value does not match `%s' value"),
310            "LC_ADDRESS", "country_ab2", "country_num");
311
312   if (address->country_ab3 == NULL)
313     {
314       if (verbose && ! nothing)
315         error (0, 0, _("%s: field `%s' not defined"),
316                "LC_ADDRESS", "country_ab3");
317       address->country_ab3 = "   ";
318     }
319   else if (cnt != sizeof (iso3166) / sizeof (iso3166[0])
320            && strcmp (address->country_ab3, iso3166[cnt].ab3) != 0)
321     error (0, 0, _("%s: `%s' value does not match `%s' value"),
322            "LC_ADDRESS", "country_ab3", "country_num");
323 }
324
325
326 void
327 address_output (struct localedef_t *locale, struct charmap_t *charmap,
328                 const char *output_path)
329 {
330   struct locale_address_t *address = locale->categories[LC_ADDRESS].address;
331   struct iovec iov[3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS)];
332   struct locale_file data;
333   uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS)];
334   size_t cnt = 0;
335
336   data.magic = LIMAGIC (LC_ADDRESS);
337   data.n = _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS);
338   iov[cnt].iov_base = (void *) &data;
339   iov[cnt].iov_len = sizeof (data);
340   ++cnt;
341
342   iov[cnt].iov_base = (void *) idx;
343   iov[cnt].iov_len = sizeof (idx);
344   ++cnt;
345
346   idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
347   iov[cnt].iov_base = (void *) address->postal_fmt;
348   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
349   ++cnt;
350
351   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
352   iov[cnt].iov_base = (void *) address->country_name;
353   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
354   ++cnt;
355
356   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
357   iov[cnt].iov_base = (void *) address->country_post;
358   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
359   ++cnt;
360
361   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
362   iov[cnt].iov_base = (void *) address->country_ab2;
363   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
364   ++cnt;
365
366   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
367   iov[cnt].iov_base = (void *) address->country_ab3;
368   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
369   ++cnt;
370
371   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
372   iov[cnt].iov_base = (void *) address->country_car;
373   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
374   ++cnt;
375
376   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
377
378   /* Align following data */
379   iov[cnt].iov_base = (void *) "\0\0";
380   iov[cnt].iov_len = ((idx[cnt - 2] + 3) & ~3) - idx[cnt - 2];
381   idx[cnt - 2] = (idx[cnt - 2] + 3) & ~3;
382   ++cnt;
383
384   iov[cnt].iov_base = (void *) &address->country_num;
385   iov[cnt].iov_len = sizeof (uint32_t);
386   ++cnt;
387
388   idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
389   iov[cnt].iov_base = (void *) address->country_isbn;
390   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
391   ++cnt;
392
393   idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
394   iov[cnt].iov_base = (void *) address->lang_name;
395   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
396   ++cnt;
397
398   idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
399   iov[cnt].iov_base = (void *) address->lang_ab;
400   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
401   ++cnt;
402
403   idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
404   iov[cnt].iov_base = (void *) address->lang_term;
405   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
406   ++cnt;
407
408   idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
409   iov[cnt].iov_base = (void *) address->lang_lib;
410   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
411   ++cnt;
412
413   idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
414   iov[cnt].iov_base = (void *) charmap->code_set_name;
415   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
416   ++cnt;
417
418   assert (cnt == 3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS));
419
420   write_locale_data (output_path, "LC_ADDRESS",
421                      3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS), iov);
422 }
423
424
425 /* The parser for the LC_ADDRESS section of the locale definition.  */
426 void
427 address_read (struct linereader *ldfile, struct localedef_t *result,
428               struct charmap_t *charmap, const char *repertoire_name,
429               int ignore_content)
430 {
431   struct locale_address_t *address;
432   struct token *now;
433   struct token *arg;
434   enum token_t nowtok;
435
436   /* The rest of the line containing `LC_ADDRESS' must be free.  */
437   lr_ignore_rest (ldfile, 1);
438
439
440   do
441     {
442       now = lr_token (ldfile, charmap, NULL, verbose);
443       nowtok = now->tok;
444     }
445   while (nowtok == tok_eol);
446
447   /* If we see `copy' now we are almost done.  */
448   if (nowtok == tok_copy)
449     {
450       handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_address,
451                    LC_ADDRESS, "LC_ADDRESS", ignore_content);
452       return;
453     }
454
455   /* Prepare the data structures.  */
456   address_startup (ldfile, result, ignore_content);
457   address = result->categories[LC_ADDRESS].address;
458
459   while (1)
460     {
461       /* Of course we don't proceed beyond the end of file.  */
462       if (nowtok == tok_eof)
463         break;
464
465       /* Ignore empty lines.  */
466       if (nowtok == tok_eol)
467         {
468           now = lr_token (ldfile, charmap, NULL, verbose);
469           nowtok = now->tok;
470           continue;
471         }
472
473       switch (nowtok)
474         {
475 #define STR_ELEM(cat) \
476         case tok_##cat:                                                       \
477           /* Ignore the rest of the line if we don't need the input of        \
478              this line.  */                                                   \
479           if (ignore_content)                                                 \
480             {                                                                 \
481               lr_ignore_rest (ldfile, 0);                                     \
482               break;                                                          \
483             }                                                                 \
484                                                                               \
485           arg = lr_token (ldfile, charmap, NULL, verbose);                    \
486           if (arg->tok != tok_string)                                         \
487             goto err_label;                                                   \
488           if (address->cat != NULL)                                           \
489             lr_error (ldfile, _("\
490 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat);                \
491           else if (!ignore_content && arg->val.str.startmb == NULL)           \
492             {                                                                 \
493               lr_error (ldfile, _("\
494 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat);                   \
495               address->cat = "";                                              \
496             }                                                                 \
497           else if (!ignore_content)                                           \
498             address->cat = arg->val.str.startmb;                              \
499           break
500
501           STR_ELEM (postal_fmt);
502           STR_ELEM (country_name);
503           STR_ELEM (country_post);
504           STR_ELEM (country_ab2);
505           STR_ELEM (country_ab3);
506           STR_ELEM (country_car);
507           STR_ELEM (lang_name);
508           STR_ELEM (lang_ab);
509           STR_ELEM (lang_term);
510           STR_ELEM (lang_lib);
511
512 #define INT_STR_ELEM(cat) \
513         case tok_##cat:                                                       \
514           /* Ignore the rest of the line if we don't need the input of        \
515              this line.  */                                                   \
516           if (ignore_content)                                                 \
517             {                                                                 \
518               lr_ignore_rest (ldfile, 0);                                     \
519               break;                                                          \
520             }                                                                 \
521                                                                               \
522           arg = lr_token (ldfile, charmap, NULL, verbose);                    \
523           if (arg->tok != tok_string && arg->tok != tok_number)               \
524             goto err_label;                                                   \
525           if (address->cat != NULL)                                           \
526             lr_error (ldfile, _("\
527 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat);                \
528           else if (!ignore_content && arg->tok == tok_string                  \
529                    && arg->val.str.startmb == NULL)                           \
530             {                                                                 \
531               lr_error (ldfile, _("\
532 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat);                   \
533               address->cat = "";                                              \
534             }                                                                 \
535           else if (!ignore_content)                                           \
536             {                                                                 \
537               if (arg->tok == tok_string)                                     \
538                 address->cat = arg->val.str.startmb;                          \
539               else                                                            \
540                 {                                                             \
541                   char *numbuf = (char *) xmalloc (11);                       \
542                   snprintf (numbuf, 11, "%ld", arg->val.num);                 \
543                   address->cat = numbuf;                                      \
544                 }                                                             \
545             }                                                                 \
546           break
547
548           INT_STR_ELEM (country_isbn);
549
550 #define INT_ELEM(cat) \
551         case tok_##cat:                                                       \
552           /* Ignore the rest of the line if we don't need the input of        \
553              this line.  */                                                   \
554           if (ignore_content)                                                 \
555             {                                                                 \
556               lr_ignore_rest (ldfile, 0);                                     \
557               break;                                                          \
558             }                                                                 \
559                                                                               \
560           arg = lr_token (ldfile, charmap, NULL, verbose);                    \
561           if (arg->tok != tok_number)                                         \
562             goto err_label;                                                   \
563           else if (address->cat != 0)                                         \
564             lr_error (ldfile, _("\
565 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat);                \
566           else if (!ignore_content)                                           \
567             address->cat = arg->val.num;                                      \
568           break
569
570           INT_ELEM (country_num);
571
572         case tok_end:
573           /* Next we assume `LC_ADDRESS'.  */
574           arg = lr_token (ldfile, charmap, NULL, verbose);
575           if (arg->tok == tok_eof)
576             break;
577           if (arg->tok == tok_eol)
578             lr_error (ldfile, _("%s: incomplete `END' line"),
579                       "LC_ADDRESS");
580           else if (arg->tok != tok_lc_address)
581             lr_error (ldfile, _("\
582 %1$s: definition does not end with `END %1$s'"), "LC_ADDRESS");
583           lr_ignore_rest (ldfile, arg->tok == tok_lc_address);
584           return;
585
586         default:
587         err_label:
588           SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
589         }
590
591       /* Prepare for the next round.  */
592       now = lr_token (ldfile, charmap, NULL, verbose);
593       nowtok = now->tok;
594     }
595
596   /* When we come here we reached the end of the file.  */
597   lr_error (ldfile, _("%s: premature end of file"), "LC_ADDRESS");
598 }