Tue Apr 2 21:27:01 1996 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
[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 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <dirent.h>
23 #include <error.h>
24 #include <getopt.h>
25 #include <langinfo.h>
26 #include <libintl.h>
27 #include <limits.h>
28 #include <locale.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <errno.h>
33
34 #include "localeinfo.h"
35
36
37 /* If set print the name of the category.  */
38 static int show_category_name;
39
40 /* If set print the name of the item.  */
41 static int show_keyword_name;
42
43 /* Long options.  */
44 static const struct option long_options[] =
45 {
46   { "all-locales", no_argument, NULL, 'a' },
47   { "category-name", no_argument, &show_category_name, 1 },
48   { "charmaps", no_argument, NULL, 'm' },
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 /* Definition of the data structure which represents a category and its
69    items.  */
70 struct category
71 {
72   int cat_id;
73   const char *name;
74   size_t number;
75   struct cat_item
76   {
77     int item_id;
78     const char *name;
79     enum { std, opt } status;
80     enum value_type value_type;
81     int min;
82     int max;
83   } *item_desc;
84 };
85
86 /* Simple helper macro.  */
87 #define NELEMS(arr) ((sizeof (arr)) / (sizeof (arr[0])))
88
89 /* For some tricky stuff.  */
90 #define NO_PAREN(Item, More...) Item, ## More
91
92 /* We have all categories defined in `categories.def'.  Now construct
93    the description and data structure used for all categories.  */
94 #define DEFINE_ELEMENT(Item, More...) { Item, ## More },
95 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out)      \
96     static struct cat_item category##_desc[] =                                \
97       {                                                                       \
98         NO_PAREN items                                                        \
99       };
100
101 #include "categories.def"
102 #undef DEFINE_CATEGORY
103
104 static struct category category[] =
105   {
106 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out)      \
107     { _NL_NUM_##category, name, NELEMS (category##_desc) - 1,                 \
108       category##_desc },
109 #include "categories.def"
110 #undef DEFINE_CATEGORY
111   };
112 #define NCATEGORIES NELEMS (category)
113
114
115 /* Prototypes for local functions.  */
116 static void usage (int status) __attribute__ ((noreturn));
117 static void write_locales (void);
118 static void write_charmaps (void);
119 static void show_locale_vars (void);
120 static void show_info (const char *name);
121
122
123 int
124 main (int argc, char *argv[])
125 {
126   int optchar;
127   int do_all = 0;
128   int do_help = 0;
129   int do_version = 0;
130   int do_charmaps = 0;
131
132   /* Set initial values for global varaibles.  */
133   show_category_name = 0;
134   show_keyword_name = 0;
135
136   /* Set locale.  Do not set LC_ALL because the other categories must
137      not be affected (acccording to POSIX.2).  */
138   setlocale (LC_CTYPE, "");
139   setlocale (LC_MESSAGES, "");
140
141   /* Initialize the message catalog.  */
142   textdomain (PACKAGE);
143
144   while ((optchar = getopt_long (argc, argv, "achkmv", long_options, NULL))
145          != EOF)
146     switch (optchar)
147       {
148       case '\0':
149         break;
150       case 'a':
151         do_all = 1;
152         break;
153       case 'c':
154         show_category_name = 1;
155         break;
156       case 'h':
157         do_help = 1;
158         break;
159       case 'k':
160         show_keyword_name = 1;
161         break;
162       case 'm':
163         do_charmaps = 1;
164         break;
165       case 'v':
166         do_version = 1;
167         break;
168       default:
169         error (1, 0, gettext ("illegal option \"%s\""), optarg);
170         break;
171       }
172
173   /* Version information is requested.  */
174   if (do_version)
175     {
176       fprintf (stderr, "GNU %s %s\n", PACKAGE, VERSION);
177       exit (EXIT_SUCCESS);
178     }
179
180   /* Help is requested.  */
181   if (do_help)
182     usage (EXIT_SUCCESS);
183
184   /* `-a' requests the names of all available locales.  */
185   if (do_all != 0)
186     {
187       write_locales ();
188       exit (EXIT_SUCCESS);
189     }
190
191   /* `m' requests the names of all available charmaps.  The names can be
192      used for the -f argument to localedef(3).  */
193   if (do_charmaps != 0)
194     {
195       write_charmaps ();
196       exit (EXIT_SUCCESS);
197     }
198
199   /* If no real argument is given we have to print the contents of the
200      current locale definition variables.  These are LANG and the LC_*.  */
201   if (optind == argc && show_keyword_name == 0 && show_category_name == 0)
202     {
203       show_locale_vars ();
204       exit (EXIT_SUCCESS);
205     }
206
207   /* Process all given names.  */
208   while (optind <  argc)
209     show_info (argv[optind++]);
210
211   exit (EXIT_SUCCESS);
212 }
213
214
215 /* Display usage information and exit.  */
216 static void
217 usage(int status)
218 {
219   if (status != EXIT_SUCCESS)
220     fprintf (stderr, gettext ("Try `%s --help' for more information.\n"),
221              program_invocation_name);
222   else
223     printf(gettext ("\
224 Usage: %s [OPTION]... name\n\
225 Mandatory arguments to long options are mandatory for short options too.\n\
226   -h, --help            display this help and exit\n\
227   -v, --version         output version information and exit\n\
228 \n\
229   -a, --all-locales     write names of available locales\n\
230   -m, --charmaps        write names of available charmaps\n\
231 \n\
232   -c, --category-name   write names of selected categories\n\
233   -k, --keyword-name    write names of selected keywords\n\
234 \n\
235 "), program_invocation_name);
236
237   exit (status);
238 }
239
240
241 /* Write the names of all available locales to stdout.  */
242 static void
243 write_locales (void)
244 {
245   DIR *dir;
246   struct dirent *dirent;
247
248   /* `POSIX' locale is always available (POSIX.2 4.34.3).  */
249   puts ("POSIX");
250
251   dir = opendir (LOCALE_PATH);
252   if (dir == NULL)
253     {
254       error (1, errno, gettext ("cannot read locale directory `%s'"),
255              LOCALE_PATH);
256       return;
257     }
258
259   /* Now we can look for all files in the directory.  */
260   while ((dirent = readdir (dir)) != NULL)
261     if (strcmp (dirent->d_name, ".") != 0
262         && strcmp (dirent->d_name, "..") != 0)
263       puts (dirent->d_name);
264
265   closedir (dir);
266 }
267
268
269 /* Write the names of all available character maps to stdout.  */
270 static void
271 write_charmaps (void)
272 {
273   DIR *dir;
274   struct dirent *dirent;
275
276   dir = opendir (CHARMAP_PATH);
277   if (dir == NULL)
278     {
279       error (1, errno, gettext ("cannot read character map directory `%s'"),
280              CHARMAP_PATH);
281       return;
282     }
283
284   /* Now we can look for all files in the directory.  */
285   while ((dirent = readdir (dir)) != NULL)
286     if (strcmp (dirent->d_name, ".") != 0
287         && strcmp (dirent->d_name, "..") != 0)
288       puts (dirent->d_name);
289
290   closedir (dir);
291 }
292
293
294 /* We have to show the contents of the environments determining the
295    locale.  */
296 static void
297 show_locale_vars (void)
298 {
299   size_t cat_no;
300   const char *lcall = getenv ("LC_ALL");
301   const char *lang = getenv ("LANG") ? : "POSIX";
302
303   void get_source (const char *name)
304     {
305       char *val = getenv (name);
306
307       if (lcall != NULL || val == NULL)
308         printf ("%s=\"%s\"\n", name, lcall ? : lang);
309       else
310         printf ("%s=%s\n", name, val);
311     }
312
313   /* LANG has to be the first value.  */
314   printf ("LANG=%s\n", lang);
315
316   /* Now all categories in an unspecified order.  */
317   for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
318     get_source (category[cat_no].name);
319
320   /* The last is the LC_ALL value.  */
321   printf ("LC_ALL=%s\n", lcall ? : "");
322 }
323
324
325 /* Show the information request for NAME.  */
326 static void
327 show_info (const char *name)
328 {
329   size_t cat_no;
330
331   void print_item (struct cat_item *item)
332     {
333       if (show_keyword_name != 0)
334         printf ("%s=", item->name);
335
336       switch (item->value_type)
337         {
338         case string:
339           printf ("%s%s%s", show_keyword_name ? "\"" : "",
340                   nl_langinfo (item->item_id) ? : "",
341                   show_keyword_name ? "\"" : "");
342           break;
343         case stringarray:
344           {
345             int cnt;
346             const char *val;
347
348             if (show_keyword_name)
349               putchar ('"');
350
351             for (cnt = 0; cnt < item->max - 1; ++cnt)
352               {
353                 val = nl_langinfo (item->item_id + cnt);
354                 printf ("%s;", val ? : "");
355               }
356
357             val = nl_langinfo (item->item_id + cnt);
358             printf ("%s", val ? : "");
359
360             if (show_keyword_name)
361               putchar ('"');
362           }
363           break;
364         case byte:
365           {
366             const char *val = nl_langinfo (item->item_id);
367
368             if (val != NULL)
369               printf ("%d", *val == CHAR_MAX ? -1 : *val);
370           }
371           break;
372         case bytearray:
373           {
374             const char *val = nl_langinfo (item->item_id);
375             int cnt = val ? strlen (val) : 0;
376
377             while (cnt > 1)
378               {
379                 printf ("%d;", *val == CHAR_MAX ? -1 : *val);
380                 --cnt;
381                 ++val;
382               }
383
384             printf ("%d", cnt == 0 || *val == CHAR_MAX ? -1 : *val);
385           }
386           break;
387         case word:
388           {
389             unsigned int val = (unsigned int) nl_langinfo (item->item_id);
390             printf ("%d", val);
391           }
392           break;
393         default:
394         }
395       putchar ('\n');
396     }
397
398   for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
399     {
400       size_t item_no;
401
402       if (strcmp (name, category[cat_no].name) == 0)
403         /* Print the whole category.  */
404         {
405           if (show_category_name != 0)
406             puts (category[cat_no].name);
407
408           for (item_no = 0; item_no < category[cat_no].number; ++item_no)
409             print_item (&category[cat_no].item_desc[item_no]);
410
411           return;
412         }
413
414       for (item_no = 0; item_no < category[cat_no].number; ++item_no)
415         if (strcmp (name, category[cat_no].item_desc[item_no].name) == 0)
416           {
417             if (show_category_name != 0)
418               puts (category[cat_no].name);
419
420             print_item (&category[cat_no].item_desc[item_no]);
421             return;
422           }
423     }
424 }