Thu Mar 28 03:25:10 1996 Roland McGrath <roland@charlie-brown.gnu.ai.mit.edu>
[platform/upstream/glibc.git] / locale / programs / locale.c
1 /* Copyright (C) 1995, 1996 Free Software Foundation, Inc.
2
3 The GNU C Library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
7
8 The GNU C Library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public
14 License along with the GNU C Library; see the file COPYING.LIB.  If
15 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16 Cambridge, MA 02139, USA.  */
17
18 #include <dirent.h>
19 #include <getopt.h>
20 #include <langinfo.h>
21 #include <libintl.h>
22 #include <limits.h>
23 #include <locale.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <errno.h>
28
29 /*#include "localedef.h"*/
30 #include "localeinfo.h"
31
32
33 /* If set dump C code describing the current locale.  */
34 static int do_dump;
35
36 /* If set print the name of the category.  */
37 static int show_category_name;
38
39 /* If set print the name of the item.  */
40 static int show_keyword_name;
41
42 /* Long options.  */
43 static const struct option long_options[] =
44 {
45   { "all-locales", no_argument, NULL, 'a' },
46   { "category-name", no_argument, &show_category_name, 1 },
47   { "charmaps", no_argument, NULL, 'm' },
48   { "dump", no_argument, &do_dump, 1 },
49   { "help", no_argument, NULL, 'h' },
50   { "keyword-name", no_argument, &show_keyword_name, 1 },
51   { "version", no_argument, NULL, 'v' },
52   { NULL, 0, NULL, 0 }
53 };
54
55
56 /* We don't have these constants defined because we don't use them.  Give
57    default values.  */
58 #define CTYPE_MB_CUR_MIN 0
59 #define CTYPE_MB_CUR_MAX 0
60 #define CTYPE_HASH_SIZE 0
61 #define CTYPE_HASH_LAYERS 0
62 #define CTYPE_CLASS 0
63 #define CTYPE_TOUPPER_EB 0
64 #define CTYPE_TOLOWER_EB 0
65 #define CTYPE_TOUPPER_EL 0
66 #define CTYPE_TOLOWER_EL 0
67
68 /* XXX Hack */
69 struct cat_item
70 {
71   int item_id;
72   const char *name;
73   enum { std, opt } status;
74   enum value_type value_type;
75   int min;
76   int max;
77 };
78
79
80 /* We have all categories defined in `categories.def'.  Now construct
81    the description and data structure used for all categories.  */
82 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out)      \
83     static struct cat_item category##_desc[] =                                \
84       {                                                                       \
85         NO_PAREN items                                                        \
86       };
87
88 #include "locale/aux/categories.def"
89 #undef DEFINE_CATEGORY
90
91 static struct category category[] =
92   {
93 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out)      \
94     { _NL_NUM_##category, name, NELEMS (category##_desc) - 1,                 \
95       category##_desc, NULL, NULL, NULL, out },
96 #include "locale/aux/categories.def"
97 #undef DEFINE_CATEGORY
98   };
99 #define NCATEGORIES NELEMS (category)
100
101
102 /* Prototypes for local functions.  */
103 static void usage (int status) __attribute__ ((noreturn));
104 static void write_locales (void);
105 static void write_charmaps (void);
106 static void show_locale_vars (void);
107 static void show_info (const char *name);
108 static void dump_category (const char *name);
109
110
111 int
112 main (int argc, char *argv[])
113 {
114   int optchar;
115   int do_all = 0;
116   int do_help = 0;
117   int do_version = 0;
118   int do_charmaps = 0;
119
120   /* Set initial values for global varaibles.  */
121   do_dump = 0;
122   show_category_name = 0;
123   show_keyword_name = 0;
124
125   /* Set locale.  Do not set LC_ALL because the other categories must
126      not be affected (acccording to POSIX.2).  */
127   setlocale (LC_CTYPE, "");
128   setlocale (LC_MESSAGES, "");
129
130   /* Initialize the message catalog.  */
131   textdomain (PACKAGE);
132
133   while ((optchar = getopt_long (argc, argv, "achkmv", long_options, NULL))
134          != EOF)
135     switch (optchar)
136       {
137       case '\0':
138         break;
139       case 'a':
140         do_all = 1;
141         break;
142       case 'c':
143         show_category_name = 1;
144         break;
145       case 'h':
146         do_help = 1;
147         break;
148       case 'k':
149         show_keyword_name = 1;
150         break;
151       case 'm':
152         do_charmaps = 1;
153         break;
154       case 'v':
155         do_version = 1;
156         break;
157       default:
158         error (1, 0, gettext ("illegal option \"%s\""), optarg);
159         break;
160       }
161
162   /* Version information is requested.  */
163   if (do_version)
164     {
165       fprintf (stderr, "GNU %s %s\n", PACKAGE, VERSION);
166       exit (EXIT_SUCCESS);
167     }
168
169   /* Help is requested.  */
170   if (do_help)
171     usage (EXIT_SUCCESS);
172
173   /* Dump C code.  */
174   if (do_dump)
175     {
176       printf ("\
177 /* Generated by GNU %s %s.  */\n\
178 \n\
179 #include \"localeinfo.h\"\n", program_invocation_name, VERSION);
180
181       while (optind < argc)
182         dump_category (argv[optind++]);
183
184       exit (EXIT_SUCCESS);
185     }
186
187   /* `-a' requests the names of all available locales.  */
188   if (do_all != 0)
189     {
190       write_locales ();
191       exit (EXIT_SUCCESS);
192     }
193
194   /* `m' requests the names of all available charmaps.  The names can be
195      used for the -f argument to localedef(3).  */
196   if (do_charmaps != 0)
197     {
198       write_charmaps ();
199       exit (EXIT_SUCCESS);
200     }
201
202   /* If no real argument is given we have to print the contents of the
203      current locale definition variables.  These are LANG and the LC_*.  */
204   if (optind == argc && show_keyword_name == 0 && show_category_name == 0)
205     {
206       show_locale_vars ();
207       exit (EXIT_SUCCESS);
208     }
209
210   /* Process all given names.  */
211   while (optind <  argc)
212     show_info (argv[optind++]);
213
214   exit (EXIT_SUCCESS);
215 }
216
217
218 /* Display usage information and exit.  */
219 static void
220 usage(int status)
221 {
222   if (status != EXIT_SUCCESS)
223     fprintf (stderr, gettext ("Try `%s --help' for more information.\n"),
224              program_invocation_name);
225   else
226     printf(gettext ("\
227 Usage: %s [OPTION]... name\n\
228 Mandatory arguments to long options are mandatory for short options too.\n\
229   -h, --help            display this help and exit\n\
230   -v, --version         output version information and exit\n\
231 \n\
232   -a, --all-locales     write names of available locales\n\
233   -m, --charmaps        write names of available charmaps\n\
234 \n\
235   -c, --category-name   write names of selected categories\n\
236   -k, --keyword-name    write names of selected keywords\n\
237 \n\
238       --dump            dump C code describing the current locale\n\
239                         (this code can be used in the C library)\n\
240 "), program_invocation_name);
241
242   exit (status);
243 }
244
245
246 /* Write the names of all available locales to stdout.  */
247 static void
248 write_locales (void)
249 {
250   DIR *dir;
251   struct dirent *dirent;
252
253   /* `POSIX' locale is always available (POSIX.2 4.34.3).  */
254   puts ("POSIX");
255
256   dir = opendir (LOCALE_PATH);
257   if (dir == NULL)
258     {
259       error (1, errno, gettext ("cannot read locale directory `%s'"),
260              LOCALE_PATH);
261       return;
262     }
263
264   /* Now we can look for all files in the directory.  */
265   while ((dirent = readdir (dir)) != NULL)
266     if (strcmp (dirent->d_name, ".") != 0
267         && strcmp (dirent->d_name, "..") != 0)
268       puts (dirent->d_name);
269
270   closedir (dir);
271 }
272
273
274 /* Write the names of all available character maps to stdout.  */
275 static void
276 write_charmaps (void)
277 {
278   DIR *dir;
279   struct dirent *dirent;
280
281   dir = opendir (CHARMAP_PATH);
282   if (dir == NULL)
283     {
284       error (1, errno, gettext ("cannot read character map directory `%s'"),
285              CHARMAP_PATH);
286       return;
287     }
288
289   /* Now we can look for all files in the directory.  */
290   while ((dirent = readdir (dir)) != NULL)
291     if (strcmp (dirent->d_name, ".") != 0
292         && strcmp (dirent->d_name, "..") != 0)
293       puts (dirent->d_name);
294
295   closedir (dir);
296 }
297
298
299 /* We have to show the contents of the environments determining the
300    locale.  */
301 static void
302 show_locale_vars (void)
303 {
304   size_t cat_no;
305   const char *lcall = getenv ("LC_ALL");
306   const char *lang = getenv ("LANG") ? : "POSIX";
307
308   void get_source (const char *name)
309     {
310       char *val = getenv (name);
311
312       if (lcall != NULL || val == NULL)
313         printf ("%s=\"%s\"\n", name, lcall ? : lang);
314       else
315         printf ("%s=%s\n", name, val);
316     }
317
318   /* LANG has to be the first value.  */
319   printf ("LANG=%s\n", lang);
320
321   /* Now all categories in an unspecified order.  */
322   for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
323     get_source (category[cat_no].name);
324
325   /* The last is the LC_ALL value.  */
326   printf ("LC_ALL=%s\n", lcall ? : "");
327 }
328
329
330 /* Show the information request for NAME.  */
331 static void
332 show_info (const char *name)
333 {
334   size_t cat_no;
335
336   void print_item (struct cat_item *item)
337     {
338       if (show_keyword_name != 0)
339         printf ("%s=", item->name);
340
341       switch (item->value_type)
342         {
343         case string:
344           printf ("%s%s%s", show_keyword_name ? "\"" : "",
345                   nl_langinfo (item->item_id) ? : "",
346                   show_keyword_name ? "\"" : "");
347           break;
348         case stringarray:
349           {
350             int cnt;
351             const char *val;
352
353             if (show_keyword_name)
354               putchar ('"');
355
356             for (cnt = 0; cnt < item->max - 1; ++cnt)
357               {
358                 val = nl_langinfo (item->item_id + cnt);
359                 printf ("%s;", val ? : "");
360               }
361
362             val = nl_langinfo (item->item_id + cnt);
363             printf ("%s", val ? : "");
364
365             if (show_keyword_name)
366               putchar ('"');
367           }
368           break;
369         case byte:
370           {
371             const char *val = nl_langinfo (item->item_id);
372
373             if (val != NULL)
374               printf ("%d", *val == CHAR_MAX ? -1 : *val);
375           }
376           break;
377         case bytearray:
378           {
379             const char *val = nl_langinfo (item->item_id);
380             int cnt = val ? strlen (val) : 0;
381
382             while (cnt > 1)
383               {
384                 printf ("%d;", *val == CHAR_MAX ? -1 : *val);
385                 --cnt;
386                 ++val;
387               }
388
389             printf ("%d", cnt == 0 || *val == CHAR_MAX ? -1 : *val);
390           }
391           break;
392         default:
393         }
394       putchar ('\n');
395     }
396
397   for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
398     {
399       size_t item_no;
400
401       if (category[cat_no].outfct != NULL)
402         /* Categories which need special handling of the output are
403            not written.  This is especially for LC_CTYPE and LC_COLLATE.
404            It does not make sense to have this large number of cryptic
405            characters displayed.  */
406         continue;
407
408       if (strcmp (name, category[cat_no].name) == 0)
409         /* Print the whole category.  */
410         {
411           if (show_category_name != 0)
412             puts (category[cat_no].name);
413
414           for (item_no = 0; item_no < category[cat_no].number; ++item_no)
415             print_item (&category[cat_no].item_desc[item_no]);
416
417           return;
418         }
419
420       for (item_no = 0; item_no < category[cat_no].number; ++item_no)
421         if (strcmp (name, category[cat_no].item_desc[item_no].name) == 0)
422           {
423             if (show_category_name != 0)
424               puts (category[cat_no].name);
425
426             print_item (&category[cat_no].item_desc[item_no]);
427             return;
428           }
429     }
430 }
431
432
433 static void
434 dump_category (const char *name)
435 {
436   char *locname;
437   size_t cat_no, item_no, nstrings;
438
439   for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
440     if (strcmp (name, category[cat_no].name) == 0)
441       break;
442
443   if (cat_no >= NCATEGORIES)
444     return;
445
446   /* The NAME specifies a correct locale category.  */
447   if (category[cat_no].outfct != NULL)
448     {
449       category[cat_no].outfct ();
450       return;
451     }
452
453   locname = (getenv ("LC_ALL") ?: getenv (name) ?:
454              getenv ("LANG") ?: (char *) "POSIX");
455
456   /* Determine the number of strings in advance.  */
457   nstrings = 0;
458   for (item_no = 0; item_no < category[cat_no].number; ++item_no)
459     switch (category[cat_no].item_desc[item_no].value_type)
460       {
461       case string:
462       case byte:
463       case bytearray:
464         ++nstrings;
465         break;
466       case stringarray:
467         nstrings += category[cat_no].item_desc[item_no].max;
468       default:
469       }
470
471   printf ("\nconst struct locale_data _nl_%s_%s =\n{\n"
472           "  NULL, 0, /* no file mapped */\n  %Zu,\n  {\n",
473           locname, name, nstrings);
474
475   for (item_no = 0; item_no < category[cat_no].number; ++item_no)
476     switch (category[cat_no].item_desc[item_no].value_type)
477       {
478       case string:
479         {
480           const char *val = nl_langinfo (
481             category[cat_no].item_desc[item_no].item_id);
482
483           if (val != NULL)
484             printf ("    \"%s\",\n", val);
485           else
486             puts ("    NULL,");
487         }
488         break;
489       case stringarray:
490         {
491           const char *val;
492           int cnt;
493
494           for (cnt = 0; cnt < category[cat_no].item_desc[item_no].max; ++cnt)
495             {
496               val = nl_langinfo (
497                 category[cat_no].item_desc[item_no].item_id + cnt);
498
499               if (val != NULL)
500                 printf ("    \"%s\",\n", val);
501               else
502                 puts ("    NULL,");
503             }
504         }
505         break;
506       case byte:
507         {
508           const char *val = nl_langinfo (
509             category[cat_no].item_desc[item_no].item_id);
510
511           if (val != NULL)
512             printf ("    \"\\%o\",\n",
513                     *(unsigned char *) val ? : UCHAR_MAX);
514           else
515             puts ("    NULL,");
516         }
517         break;
518       case bytearray:
519         {
520           const char *bytes = nl_langinfo (
521             category[cat_no].item_desc[item_no].item_id);
522
523           if (bytes != NULL)
524             {
525               fputs ("    \"", stdout);
526               if (*bytes != '\0')
527                 do
528                   printf ("\\%o", *(unsigned char *) bytes++);
529                 while (*bytes != '\0');
530               else
531                 printf ("\\%o", UCHAR_MAX);
532
533               puts ("\",");
534             }
535           else
536             puts ("    NULL,");
537         }
538         break;
539       default:
540         break;
541       }
542
543   puts ("  }\n};");
544 }