Update.
[platform/upstream/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   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, 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           error (0, 0, _("No definition for %s category found"), "LC_ADDRESS");
132           address_startup (NULL, locale, 0);
133           address = locale->categories[LC_ADDRESS].address;
134           nothing = 1;
135         }
136     }
137
138   if (address->postal_fmt == NULL)
139     {
140       if (! nothing)
141         error (0, 0, _("%s: field `%s' not defined"),
142                "LC_ADDRESS", "postal_fmt");
143       /* Use as the default value the value of the i18n locale.  */
144       address->postal_fmt = "%a%N%f%N%d%N%b%N%s %h %e %r%N%C-%z %T%N%c%N";
145     }
146   else
147     {
148       /* We must check whether the format string contains only the
149          allowed escape sequences.  */
150       const char *cp = address->postal_fmt;
151
152       if (*cp == '\0')
153         error (0, 0, _("%s: field `%s' must not be empty"),
154                "LC_ADDRESS", "postal_fmt");
155       else
156         while (*cp != '\0')
157           {
158             if (*cp == '%')
159               {
160                 if (*++cp == 'R')
161                   /* Romanize-flag.  */
162                   ++cp;
163                 if (strchr ("afdbshNtreCzTc%", *cp) == NULL)
164                   {
165                     error (0, 0, _("\
166 %s: invalid escape `%%%c' sequence in field `%s'"),
167                            "LC_ADDRESS", *cp, "postal_fmt");
168                     break;
169                   }
170               }
171             ++cp;
172           }
173     }
174
175 #define TEST_ELEM(cat) \
176   if (address->cat == NULL)                                                   \
177     {                                                                         \
178       if (verbose && ! nothing)                                               \
179         error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS", #cat);    \
180       address->cat = "";                                                      \
181     }
182
183   TEST_ELEM (country_name);
184   /* XXX Test against list of defined codes.  */
185   TEST_ELEM (country_post);
186   /* XXX Test against list of defined codes.  */
187   TEST_ELEM (country_car);
188   /* XXX Test against list of defined codes.  */
189   TEST_ELEM (country_isbn);
190   TEST_ELEM (lang_name);
191
192   helper = 1;
193   if (address->lang_term == NULL)
194     {
195       if (verbose && ! nothing)
196         error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS",
197                "lang_term");
198       address->lang_term = "";
199       cnt = sizeof (iso639) / sizeof (iso639[0]);
200     }
201   else if (address->lang_term[0] == '\0')
202     {
203       if (verbose)
204         error (0, 0, _("%s: field `%s' must not be empty"),
205                "LC_ADDRESS", "lang_term");
206       cnt = sizeof (iso639) / sizeof (iso639[0]);
207     }
208   else
209     {
210       /* Look for this language in the table.  */
211       for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
212         if (strcmp (address->lang_term, iso639[cnt].term) == 0)
213           break;
214       if (cnt == sizeof (iso639) / sizeof (iso639[0]))
215         error (0, 0, _("\
216 %s: terminology language code `%s' not defined"),
217                "LC_ADDRESS", address->lang_term);
218     }
219
220   if (address->lang_ab == NULL)
221     {
222       if (verbose && ! nothing)
223         error (0, 0, _("%s: field `%s' not defined"), "LC_ADDRESS", "lang_ab");
224       address->lang_ab = "";
225     }
226   else if (address->lang_ab[0] == '\0')
227     {
228       if (verbose)
229         error (0, 0, _("%s: field `%s' must not be empty"),
230                "LC_ADDRESS", "lang_ab");
231     }
232   else
233     {
234       if (cnt == sizeof (iso639) / sizeof (iso639[0]))
235         {
236           helper = 2;
237           for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
238             if (strcmp (address->lang_ab, iso639[cnt].ab) == 0)
239               break;
240           if (cnt == sizeof (iso639) / sizeof (iso639[0]))
241             error (0, 0, _("\
242 %s: language abbreviation `%s' not defined"),
243                    "LC_ADDRESS", address->lang_ab);
244         }
245       else
246         if (strcmp (iso639[cnt].ab, address->lang_ab) != 0)
247           error (0, 0, _("\
248 %s: `%s' value does not match `%s' value"),
249                  "LC_ADDRESS", "lang_ab", "lang_term");
250     }
251
252   if (address->lang_lib == NULL)
253     /* This is no error.  */
254     address->lang_lib = address->lang_term;
255   else if (address->lang_lib[0] == '\0')
256     {
257       if (verbose)
258         error (0, 0, _("%s: field `%s' must not be empty"),
259                "LC_ADDRESS", "lang_lib");
260     }
261   else
262     {
263       if (cnt == sizeof (iso639) / sizeof (iso639[0]))
264         {
265           for (cnt = 0; cnt < sizeof (iso639) / sizeof (iso639[0]); ++cnt)
266             if (strcmp (address->lang_lib, iso639[cnt].lib) == 0)
267               break;
268           if (cnt == sizeof (iso639) / sizeof (iso639[0]))
269             error (0, 0, _("\
270 %s: language abbreviation `%s' not defined"),
271                    "LC_ADDRESS", address->lang_lib);
272         }
273       else
274         if (strcmp (iso639[cnt].ab, address->lang_ab) != 0)
275           error (0, 0, _("\
276 %s: `%s' value does not match `%s' value"), "LC_ADDRESS", "lang_lib",
277                  helper == 1 ? "lang_term" : "lang_ab");
278     }
279
280   if (address->country_num == 0)
281     {
282       if (verbose && ! nothing)
283         error (0, 0, _("%s: field `%s' not defined"),
284                "LC_ADDRESS", "country_num");
285       cnt = sizeof (iso3166) / sizeof (iso3166[0]);
286     }
287   else
288     {
289       for (cnt = 0; cnt < sizeof (iso3166) / sizeof (iso3166[0]); ++cnt)
290         if (address->country_num == iso3166[cnt].num)
291           break;
292
293       if (cnt == sizeof (iso3166) / sizeof (iso3166[0]))
294         error (0, 0, _("\
295 %s: numeric country code `%d' not valid"),
296                "LC_ADDRESS", address->country_num);
297     }
298   address->country_num_ob = bswap_32 (address->country_num);
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[2 + _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 #if __BYTE_ORDER == __LITTLE_ENDIAN
377 # define country_num_eb country_num_ob
378 # define country_num_el country_num
379 #else
380 # define country_num_eb country_num
381 # define country_num_el country_num_ob
382 #endif
383
384   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
385   iov[cnt].iov_base = (void *) address->country_num_eb;
386   iov[cnt].iov_len = sizeof (uint32_t);
387   ++cnt;
388
389   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
390   iov[cnt].iov_base = (void *) address->country_num_el;
391   iov[cnt].iov_len = sizeof (uint32_t);
392   ++cnt;
393
394   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
395   iov[cnt].iov_base = (void *) address->country_isbn;
396   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
397   ++cnt;
398
399   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
400   iov[cnt].iov_base = (void *) address->lang_name;
401   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
402   ++cnt;
403
404   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
405   iov[cnt].iov_base = (void *) address->lang_ab;
406   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
407   ++cnt;
408
409   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
410   iov[cnt].iov_base = (void *) address->lang_term;
411   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
412   ++cnt;
413
414   idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
415   iov[cnt].iov_base = (void *) address->lang_lib;
416   iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
417   ++cnt;
418
419   assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS));
420
421   write_locale_data (output_path, "LC_ADDRESS",
422                      2 + _NL_ITEM_INDEX (_NL_NUM_LC_ADDRESS), iov);
423 }
424
425
426 /* The parser for the LC_ADDRESS section of the locale definition.  */
427 void
428 address_read (struct linereader *ldfile, struct localedef_t *result,
429               struct charmap_t *charmap, const char *repertoire_name,
430               int ignore_content)
431 {
432   struct repertoire_t *repertoire = NULL;
433   struct locale_address_t *address;
434   struct token *now;
435   struct token *arg;
436   enum token_t nowtok;
437
438   /* Get the repertoire we have to use.  */
439   if (repertoire_name != NULL)
440     repertoire = repertoire_read (repertoire_name);
441
442   /* The rest of the line containing `LC_ADDRESS' must be free.  */
443   lr_ignore_rest (ldfile, 1);
444
445
446   do
447     {
448       now = lr_token (ldfile, charmap, NULL);
449       nowtok = now->tok;
450     }
451   while (nowtok == tok_eol);
452
453   /* If we see `copy' now we are almost done.  */
454   if (nowtok == tok_copy)
455     {
456       handle_copy (ldfile, charmap, repertoire, result, tok_lc_address,
457                    LC_ADDRESS, "LC_ADDRESS", ignore_content);
458       return;
459     }
460
461   /* Prepare the data structures.  */
462   address_startup (ldfile, result, ignore_content);
463   address = result->categories[LC_ADDRESS].address;
464
465   while (1)
466     {
467       /* Of course we don't proceed beyond the end of file.  */
468       if (nowtok == tok_eof)
469         break;
470
471       /* Ingore empty lines.  */
472       if (nowtok == tok_eol)
473         {
474           now = lr_token (ldfile, charmap, NULL);
475           nowtok = now->tok;
476           continue;
477         }
478
479       switch (nowtok)
480         {
481 #define STR_ELEM(cat) \
482         case tok_##cat:                                                       \
483           /* Ignore the rest of the line if we don't need the input of        \
484              this line.  */                                                   \
485           if (ignore_content)                                                 \
486             {                                                                 \
487               lr_ignore_rest (ldfile, 0);                                     \
488               break;                                                          \
489             }                                                                 \
490                                                                               \
491           arg = lr_token (ldfile, charmap, NULL);                             \
492           if (arg->tok != tok_string)                                         \
493             goto err_label;                                                   \
494           if (address->cat != NULL)                                           \
495             lr_error (ldfile, _("\
496 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat);                \
497           else if (!ignore_content && arg->val.str.startmb == NULL)           \
498             {                                                                 \
499               lr_error (ldfile, _("\
500 %s: unknown character in field `%s'"), "LC_ADDRESS", #cat);                   \
501               address->cat = "";                                              \
502             }                                                                 \
503           else if (!ignore_content)                                           \
504             address->cat = arg->val.str.startmb;                              \
505           break
506
507           STR_ELEM (postal_fmt);
508           STR_ELEM (country_name);
509           STR_ELEM (country_post);
510           STR_ELEM (country_ab2);
511           STR_ELEM (country_ab3);
512           STR_ELEM (country_car);
513           STR_ELEM (country_isbn);
514           STR_ELEM (lang_name);
515           STR_ELEM (lang_ab);
516           STR_ELEM (lang_term);
517           STR_ELEM (lang_lib);
518
519 #define INT_ELEM(cat) \
520         case tok_##cat:                                                       \
521           /* Ignore the rest of the line if we don't need the input of        \
522              this line.  */                                                   \
523           if (ignore_content)                                                 \
524             {                                                                 \
525               lr_ignore_rest (ldfile, 0);                                     \
526               break;                                                          \
527             }                                                                 \
528                                                                               \
529           arg = lr_token (ldfile, charmap, NULL);                             \
530           if (arg->tok != tok_number)                                         \
531             goto err_label;                                                   \
532           else if (address->cat != 0)                                         \
533             lr_error (ldfile, _("\
534 %s: field `%s' declared more than once"), "LC_ADDRESS", #cat);                \
535           else if (!ignore_content)                                           \
536             address->cat = arg->val.num;                                      \
537           break
538
539           INT_ELEM (country_num);
540
541         case tok_end:
542           /* Next we assume `LC_ADDRESS'.  */
543           arg = lr_token (ldfile, charmap, NULL);
544           if (arg->tok == tok_eof)
545             break;
546           if (arg->tok == tok_eol)
547             lr_error (ldfile, _("%s: incomplete `END' line"),
548                       "LC_ADDRESS");
549           else if (arg->tok != tok_lc_address)
550             lr_error (ldfile, _("\
551 %1$s: definition does not end with `END %1$s'"), "LC_ADDRESS");
552           lr_ignore_rest (ldfile, arg->tok == tok_lc_address);
553           return;
554
555         default:
556         err_label:
557           SYNTAX_ERROR (_("%s: syntax error"), "LC_ADDRESS");
558         }
559
560       /* Prepare for the next round.  */
561       now = lr_token (ldfile, charmap, NULL);
562       nowtok = now->tok;
563     }
564
565   /* When we come here we reached the end of the file.  */
566   lr_error (ldfile, _("%s: premature end of file"), "LC_ADDRESS");
567 }