2.5-18.1
[platform/upstream/glibc.git] / locale / programs / ld-address.c
1 /* Copyright (C) 1998-2002, 2005, 2006 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    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License version 2 as
7    published by the Free Software Foundation.
8
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.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <byteswap.h>
23 #include <error.h>
24 #include <langinfo.h>
25 #include <string.h>
26 #include <sys/uio.h>
27
28 #include <assert.h>
29
30 #include "localedef.h"
31 #include "localeinfo.h"
32 #include "locfile.h"
33
34
35 static struct
36 {
37   const char ab2[3];
38   const char ab3[4];
39   uint32_t num;
40 } iso3166[] =
41 {
42 #define DEFINE_COUNTRY_CODE(Name, Ab2, Ab3, Num) \
43   { #Ab2, #Ab3, Num },
44 #include "iso-3166.def"
45 };
46
47
48 static struct
49 {
50   const char ab[3];
51   const char term[4];
52   const char lib[4];
53 } iso639[] =
54 {
55 #define DEFINE_LANGUAGE_CODE(Name, Ab, Term, Lib) \
56   { #Ab, #Term, #Lib },
57 #define DEFINE_LANGUAGE_CODE3(Name, Term, Lib) \
58   { "", #Term, #Lib },
59 #include "iso-639.def"
60 };
61
62
63 /* The real definition of the struct for the LC_ADDRESS locale.  */
64 struct locale_address_t
65 {
66   const char *postal_fmt;
67   const char *country_name;
68   const char *country_post;
69   const char *country_ab2;
70   const char *country_ab3;
71   uint32_t country_num;
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   if (lr != NULL)
91     {
92       lr->translate_strings = 1;
93       lr->return_widestr = 0;
94     }
95 }
96
97
98 void
99 address_finish (struct localedef_t *locale, const struct charmap_t *charmap)
100 {
101   struct locale_address_t *address = locale->categories[LC_ADDRESS].address;
102   size_t cnt;
103   int helper;
104   int nothing = 0;
105
106   /* Now resolve copying and also handle completely missing definitions.  */
107   if (address == NULL)
108     {
109       /* First see whether we were supposed to copy.  If yes, find the
110          actual definition.  */
111       if (locale->copy_name[LC_ADDRESS] != NULL)
112         {
113           /* Find the copying locale.  This has to happen transitively since
114              the locale we are copying from might also copying another one.  */
115           struct localedef_t *from = locale;
116
117           do
118             from = find_locale (LC_ADDRESS, from->copy_name[LC_ADDRESS],
119                                 from->repertoire_name, charmap);
120           while (from->categories[LC_ADDRESS].address == NULL
121                  && from->copy_name[LC_ADDRESS] != NULL);
122
123           address = locale->categories[LC_ADDRESS].address
124             = from->categories[LC_ADDRESS].address;
125         }
126
127       /* If there is still no definition issue an warning and create an
128          empty one.  */
129       if (address == NULL)
130         {
131           if (! be_quiet)
132             WITH_CUR_LOCALE (error (0, 0, _("\
133 No definition for %s category found"), "LC_ADDRESS"));
134           address_startup (NULL, locale, 0);
135           address = locale->categories[LC_ADDRESS].address;
136           nothing = 1;
137         }
138     }
139
140   if (address->postal_fmt == NULL)
141     {
142       if (! nothing)
143         WITH_CUR_LOCALE (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";
147     }
148   else
149     {
150       /* We must check whether the format string contains only the
151          allowed escape sequences.  */
152       const char *cp = address->postal_fmt;
153
154       if (*cp == '\0')
155         WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
156                                 "LC_ADDRESS", "postal_fmt"));
157       else
158         while (*cp != '\0')
159           {
160             if (*cp == '%')
161               {
162                 if (*++cp == 'R')
163                   /* Romanize-flag.  */
164                   ++cp;
165                 if (strchr ("afdbshNtreCzTSc%", *cp) == NULL)
166                   {
167                     WITH_CUR_LOCALE (error (0, 0, _("\
168 %s: invalid escape `%%%c' sequence in field `%s'"),
169                                             "LC_ADDRESS", *cp, "postal_fmt"));
170                     break;
171                   }
172               }
173             ++cp;
174           }
175     }
176
177 #define TEST_ELEM(cat) \
178   if (address->cat == NULL)                                                   \
179     {                                                                         \
180       if (verbose && ! nothing)                                               \
181         WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),        \
182                                 "LC_ADDRESS", #cat));                         \
183       address->cat = "";                                                      \
184     }
185
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);
194
195   helper = 1;
196   if (address->lang_term == NULL)
197     {
198       if (verbose && ! nothing)
199         WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
200                                 "LC_ADDRESS", "lang_term"));
201       address->lang_term = "";
202       cnt = sizeof (iso639) / sizeof (iso639[0]);
203     }
204   else if (address->lang_term[0] == '\0')
205     {
206       if (verbose)
207         WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
208                                 "LC_ADDRESS", "lang_term"));
209       cnt = sizeof (iso639) / sizeof (iso639[0]);
210     }
211   else
212     {
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)
216           break;
217       if (cnt == sizeof (iso639) / sizeof (iso639[0]))
218         WITH_CUR_LOCALE (error (0, 0, _("\
219 %s: terminology language code `%s' not defined"),
220                                 "LC_ADDRESS", address->lang_term));
221     }
222
223   if (address->lang_ab == NULL)
224     {
225       if ((cnt == sizeof (iso639) / sizeof (iso639[0])
226            || iso639[cnt].ab[0] != '\0')
227           && verbose && ! nothing)
228         WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
229                                 "LC_ADDRESS", "lang_ab"));
230       address->lang_ab = "";
231     }
232   else if (address->lang_ab[0] == '\0')
233     {
234       if ((cnt == sizeof (iso639) / sizeof (iso639[0])
235            || iso639[cnt].ab[0] != '\0')
236           && verbose)
237         WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
238                                 "LC_ADDRESS", "lang_ab"));
239     }
240   else if (cnt < sizeof (iso639) / sizeof (iso639[0])
241            && iso639[cnt].ab[0] == '\0')
242     {
243       WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be defined"),
244                               "LC_ADDRESS", "lang_ab"));
245
246       address->lang_ab = "";
247     }
248   else
249     {
250       if (cnt == sizeof (iso639) / sizeof (iso639[0]))
251         {
252           helper = 2;
253           for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
254             if (strcmp (address->lang_ab, iso639[cnt].ab) == 0)
255               break;
256           if (cnt == sizeof (iso639) / sizeof (iso639[0]))
257             WITH_CUR_LOCALE (error (0, 0, _("\
258 %s: language abbreviation `%s' not defined"),
259                                     "LC_ADDRESS", address->lang_ab));
260         }
261       else
262         if (strcmp (iso639[cnt].ab, address->lang_ab) != 0
263             && iso639[cnt].ab[0] != '\0')
264           WITH_CUR_LOCALE (error (0, 0, _("\
265 %s: `%s' value does not match `%s' value"),
266                                   "LC_ADDRESS", "lang_ab", "lang_term"));
267     }
268
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')
273     {
274       if (verbose)
275         WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
276                                 "LC_ADDRESS", "lang_lib"));
277     }
278   else
279     {
280       if (cnt == sizeof (iso639) / sizeof (iso639[0]))
281         {
282           for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
283             if (strcmp (address->lang_lib, iso639[cnt].lib) == 0)
284               break;
285           if (cnt == sizeof (iso639) / sizeof (iso639[0]))
286             WITH_CUR_LOCALE (error (0, 0, _("\
287 %s: language abbreviation `%s' not defined"),
288                                     "LC_ADDRESS", address->lang_lib));
289         }
290       else
291         if (strcmp (iso639[cnt].ab, address->lang_ab) != 0)
292           WITH_CUR_LOCALE (error (0, 0, _("\
293 %s: `%s' value does not match `%s' value"), "LC_ADDRESS", "lang_lib",
294                                   helper == 1 ? "lang_term" : "lang_ab"));
295     }
296
297   if (address->country_num == 0)
298     {
299       if (verbose && ! nothing)
300         WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
301                                 "LC_ADDRESS", "country_num"));
302       cnt = sizeof (iso3166) / sizeof (iso3166[0]);
303     }
304   else
305     {
306       for (cnt = 0; cnt < sizeof (iso3166) / sizeof (iso3166[0]); ++cnt)
307         if (address->country_num == iso3166[cnt].num)
308           break;
309
310       if (cnt == sizeof (iso3166) / sizeof (iso3166[0]))
311         WITH_CUR_LOCALE (error (0, 0, _("\
312 %s: numeric country code `%d' not valid"),
313                                 "LC_ADDRESS", address->country_num));
314     }
315
316   if (address->country_ab2 == NULL)
317     {
318       if (verbose && ! nothing)
319         WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
320                                 "LC_ADDRESS", "country_ab2"));
321       address->country_ab2 = "  ";
322     }
323   else if (cnt != sizeof (iso3166) / sizeof (iso3166[0])
324            && strcmp (address->country_ab2, iso3166[cnt].ab2) != 0)
325     WITH_CUR_LOCALE (error (0, 0,
326                             _("%s: `%s' value does not match `%s' value"),
327                             "LC_ADDRESS", "country_ab2", "country_num"));
328
329   if (address->country_ab3 == NULL)
330     {
331       if (verbose && ! nothing)
332         WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
333                                 "LC_ADDRESS", "country_ab3"));
334       address->country_ab3 = "   ";
335     }
336   else if (cnt != sizeof (iso3166) / sizeof (iso3166[0])
337            && strcmp (address->country_ab3, iso3166[cnt].ab3) != 0)
338     WITH_CUR_LOCALE (error (0, 0, _("\
339 %s: `%s' value does not match `%s' value"),
340                             "LC_ADDRESS", "country_ab3", "country_num"));
341 }
342
343
344 void
345 address_output (struct localedef_t *locale, const struct charmap_t *charmap,
346                 const char *output_path)
347 {
348   struct locale_address_t *address = locale->categories[LC_ADDRESS].address;
349   struct iovec iov[3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS)];
350   struct locale_file data;
351   uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS)];
352   size_t cnt = 0;
353
354   data.magic = LIMAGIC (LC_ADDRESS);
355   data.n = _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS);
356   iov[cnt].iov_base = (void *) &data;
357   iov[cnt].iov_len = sizeof (data);
358   ++cnt;
359
360   iov[cnt].iov_base = (void *) idx;
361   iov[cnt].iov_len = sizeof (idx);
362   ++cnt;
363
364   idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
365   iov[cnt].iov_base = (void *) address->postal_fmt;
366   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
367   ++cnt;
368
369   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
370   iov[cnt].iov_base = (void *) address->country_name;
371   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
372   ++cnt;
373
374   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
375   iov[cnt].iov_base = (void *) address->country_post;
376   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
377   ++cnt;
378
379   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
380   iov[cnt].iov_base = (void *) address->country_ab2;
381   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
382   ++cnt;
383
384   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
385   iov[cnt].iov_base = (void *) address->country_ab3;
386   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
387   ++cnt;
388
389   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
390   iov[cnt].iov_base = (void *) address->country_car;
391   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
392   ++cnt;
393
394   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
395
396   /* Align following data */
397   iov[cnt].iov_base = (void *) "\0\0";
398   iov[cnt].iov_len = ((idx[cnt - 2] + 3) & ~3) - idx[cnt - 2];
399   idx[cnt - 2] = (idx[cnt - 2] + 3) & ~3;
400   ++cnt;
401
402   iov[cnt].iov_base = (void *) &address->country_num;
403   iov[cnt].iov_len = sizeof (uint32_t);
404   ++cnt;
405
406   idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
407   iov[cnt].iov_base = (void *) address->country_isbn;
408   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
409   ++cnt;
410
411   idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
412   iov[cnt].iov_base = (void *) address->lang_name;
413   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
414   ++cnt;
415
416   idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
417   iov[cnt].iov_base = (void *) address->lang_ab;
418   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
419   ++cnt;
420
421   idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
422   iov[cnt].iov_base = (void *) address->lang_term;
423   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
424   ++cnt;
425
426   idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
427   iov[cnt].iov_base = (void *) address->lang_lib;
428   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
429   ++cnt;
430
431   idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
432   iov[cnt].iov_base = (void *) charmap->code_set_name;
433   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
434   ++cnt;
435
436   assert (cnt == 3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS));
437
438   write_locale_data (output_path, LC_ADDRESS, "LC_ADDRESS",
439                      3 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS), iov);
440 }
441
442
443 /* The parser for the LC_ADDRESS section of the locale definition.  */
444 void
445 address_read (struct linereader *ldfile, struct localedef_t *result,
446               const struct charmap_t *charmap, const char *repertoire_name,
447               int ignore_content)
448 {
449   struct locale_address_t *address;
450   struct token *now;
451   struct token *arg;
452   enum token_t nowtok;
453
454   /* The rest of the line containing `LC_ADDRESS' must be free.  */
455   lr_ignore_rest (ldfile, 1);
456
457
458   do
459     {
460       now = lr_token (ldfile, charmap, result, NULL, verbose);
461       nowtok = now->tok;
462     }
463   while (nowtok == tok_eol);
464
465   /* If we see `copy' now we are almost done.  */
466   if (nowtok == tok_copy)
467     {
468       handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_address,
469                    LC_ADDRESS, "LC_ADDRESS", ignore_content);
470       return;
471     }
472
473   /* Prepare the data structures.  */
474   address_startup (ldfile, result, ignore_content);
475   address = result->categories[LC_ADDRESS].address;
476
477   while (1)
478     {
479       /* Of course we don't proceed beyond the end of file.  */
480       if (nowtok == tok_eof)
481         break;
482
483       /* Ignore empty lines.  */
484       if (nowtok == tok_eol)
485         {
486           now = lr_token (ldfile, charmap, result, NULL, verbose);
487           nowtok = now->tok;
488           continue;
489         }
490
491       switch (nowtok)
492         {
493 #define STR_ELEM(cat) \
494         case tok_##cat:                                                       \
495           /* Ignore the rest of the line if we don't need the input of        \
496              this line.  */                                                   \
497           if (ignore_content)                                                 \
498             {                                                                 \
499               lr_ignore_rest (ldfile, 0);                                     \
500               break;                                                          \
501             }                                                                 \
502                                                                               \
503           arg = lr_token (ldfile, charmap, result, NULL, verbose);            \
504           if (arg->tok != tok_string)                                         \
505             goto err_label;                                                   \
506           if (address->cat != NULL)                                           \
507             lr_error (ldfile, _("\
508 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat);                \
509           else if (!ignore_content && arg->val.str.startmb == NULL)           \
510             {                                                                 \
511               lr_error (ldfile, _("\
512 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat);                   \
513               address->cat = "";                                              \
514             }                                                                 \
515           else if (!ignore_content)                                           \
516             address->cat = arg->val.str.startmb;                              \
517           break
518
519           STR_ELEM (postal_fmt);
520           STR_ELEM (country_name);
521           STR_ELEM (country_post);
522           STR_ELEM (country_ab2);
523           STR_ELEM (country_ab3);
524           STR_ELEM (country_car);
525           STR_ELEM (lang_name);
526           STR_ELEM (lang_ab);
527           STR_ELEM (lang_term);
528           STR_ELEM (lang_lib);
529
530 #define INT_STR_ELEM(cat) \
531         case tok_##cat:                                                       \
532           /* Ignore the rest of the line if we don't need the input of        \
533              this line.  */                                                   \
534           if (ignore_content)                                                 \
535             {                                                                 \
536               lr_ignore_rest (ldfile, 0);                                     \
537               break;                                                          \
538             }                                                                 \
539                                                                               \
540           arg = lr_token (ldfile, charmap, result, NULL, verbose);            \
541           if (arg->tok != tok_string && arg->tok != tok_number)               \
542             goto err_label;                                                   \
543           if (address->cat != NULL)                                           \
544             lr_error (ldfile, _("\
545 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat);                \
546           else if (!ignore_content && arg->tok == tok_string                  \
547                    && arg->val.str.startmb == NULL)                           \
548             {                                                                 \
549               lr_error (ldfile, _("\
550 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat);                   \
551               address->cat = "";                                              \
552             }                                                                 \
553           else if (!ignore_content)                                           \
554             {                                                                 \
555               if (arg->tok == tok_string)                                     \
556                 address->cat = arg->val.str.startmb;                          \
557               else                                                            \
558                 {                                                             \
559                   char *numbuf = (char *) xmalloc (11);                       \
560                   snprintf (numbuf, 11, "%ld", arg->val.num);                 \
561                   address->cat = numbuf;                                      \
562                 }                                                             \
563             }                                                                 \
564           break
565
566           INT_STR_ELEM (country_isbn);
567
568 #define INT_ELEM(cat) \
569         case tok_##cat:                                                       \
570           /* Ignore the rest of the line if we don't need the input of        \
571              this line.  */                                                   \
572           if (ignore_content)                                                 \
573             {                                                                 \
574               lr_ignore_rest (ldfile, 0);                                     \
575               break;                                                          \
576             }                                                                 \
577                                                                               \
578           arg = lr_token (ldfile, charmap, result, NULL, verbose);            \
579           if (arg->tok != tok_number)                                         \
580             goto err_label;                                                   \
581           else if (address->cat != 0)                                         \
582             lr_error (ldfile, _("\
583 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat);                \
584           else if (!ignore_content)                                           \
585             address->cat = arg->val.num;                                      \
586           break
587
588           INT_ELEM (country_num);
589
590         case tok_end:
591           /* Next we assume `LC_ADDRESS'.  */
592           arg = lr_token (ldfile, charmap, result, NULL, verbose);
593           if (arg->tok == tok_eof)
594             break;
595           if (arg->tok == tok_eol)
596             lr_error (ldfile, _("%s: incomplete `END' line"),
597                       "LC_ADDRESS");
598           else if (arg->tok != tok_lc_address)
599             lr_error (ldfile, _("\
600 %1$s: definition does not end with `END %1$s'"), "LC_ADDRESS");
601           lr_ignore_rest (ldfile, arg->tok == tok_lc_address);
602           return;
603
604         default:
605         err_label:
606           SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
607         }
608
609       /* Prepare for the next round.  */
610       now = lr_token (ldfile, charmap, result, NULL, verbose);
611       nowtok = now->tok;
612     }
613
614   /* When we come here we reached the end of the file.  */
615   lr_error (ldfile, _("%s: premature end of file"), "LC_ADDRESS");
616 }