Thu Jun 13 17:25:11 1996 David Mosberger-Tang <davidm@azstarnet.com>
[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 #include <string.h>
34
35 #include "localeinfo.h"
36
37
38 /* If set print the name of the category.  */
39 static int show_category_name;
40
41 /* If set print the name of the item.  */
42 static int show_keyword_name;
43
44 /* Long options.  */
45 static const struct option long_options[] =
46 {
47   { "all-locales", no_argument, NULL, 'a' },
48   { "category-name", no_argument, &show_category_name, 1 },
49   { "charmaps", no_argument, NULL, 'm' },
50   { "help", no_argument, NULL, 'h' },
51   { "keyword-name", no_argument, &show_keyword_name, 1 },
52   { "version", no_argument, NULL, 'v' },
53   { NULL, 0, NULL, 0 }
54 };
55
56
57 /* We don't have these constants defined because we don't use them.  Give
58    default values.  */
59 #define CTYPE_MB_CUR_MIN 0
60 #define CTYPE_MB_CUR_MAX 0
61 #define CTYPE_HASH_SIZE 0
62 #define CTYPE_HASH_LAYERS 0
63 #define CTYPE_CLASS 0
64 #define CTYPE_TOUPPER_EB 0
65 #define CTYPE_TOLOWER_EB 0
66 #define CTYPE_TOUPPER_EL 0
67 #define CTYPE_TOLOWER_EL 0
68
69 /* Definition of the data structure which represents a category and its
70    items.  */
71 struct category
72 {
73   int cat_id;
74   const char *name;
75   size_t number;
76   struct cat_item
77   {
78     int item_id;
79     const char *name;
80     enum { std, opt } status;
81     enum value_type value_type;
82     int min;
83     int max;
84   } *item_desc;
85 };
86
87 /* Simple helper macro.  */
88 #define NELEMS(arr) ((sizeof (arr)) / (sizeof (arr[0])))
89
90 /* For some tricky stuff.  */
91 #define NO_PAREN(Item, More...) Item, ## More
92
93 /* We have all categories defined in `categories.def'.  Now construct
94    the description and data structure used for all categories.  */
95 #define DEFINE_ELEMENT(Item, More...) { Item, ## More },
96 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out)      \
97     static struct cat_item category##_desc[] =                                \
98       {                                                                       \
99         NO_PAREN items                                                        \
100       };
101
102 #include "categories.def"
103 #undef DEFINE_CATEGORY
104
105 static struct category category[] =
106   {
107 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out)      \
108     { _NL_NUM_##category, name, NELEMS (category##_desc) - 1,                 \
109       category##_desc },
110 #include "categories.def"
111 #undef DEFINE_CATEGORY
112   };
113 #define NCATEGORIES NELEMS (category)
114
115
116 /* Prototypes for local functions.  */
117 static void usage (int status) __attribute__ ((noreturn));
118 static void write_locales (void);
119 static void write_charmaps (void);
120 static void show_locale_vars (void);
121 static void show_info (const char *name);
122
123
124 int
125 main (int argc, char *argv[])
126 {
127   int optchar;
128   int do_all = 0;
129   int do_help = 0;
130   int do_version = 0;
131   int do_charmaps = 0;
132
133   /* Set initial values for global varaibles.  */
134   show_category_name = 0;
135   show_keyword_name = 0;
136
137   /* Set locale.  Do not set LC_ALL because the other categories must
138      not be affected (acccording to POSIX.2).  */
139   setlocale (LC_CTYPE, "");
140   setlocale (LC_MESSAGES, "");
141
142   /* Initialize the message catalog.  */
143   textdomain (PACKAGE);
144
145   while ((optchar = getopt_long (argc, argv, "achkmv", long_options, NULL))
146          != EOF)
147     switch (optchar)
148       {
149       case '\0':
150         break;
151       case 'a':
152         do_all = 1;
153         break;
154       case 'c':
155         show_category_name = 1;
156         break;
157       case 'h':
158         do_help = 1;
159         break;
160       case 'k':
161         show_keyword_name = 1;
162         break;
163       case 'm':
164         do_charmaps = 1;
165         break;
166       case 'v':
167         do_version = 1;
168         break;
169       default:
170         error (1, 0, gettext ("illegal option \"%s\""), optarg);
171         break;
172       }
173
174   /* Version information is requested.  */
175   if (do_version)
176     {
177       fprintf (stderr, "GNU %s %s\n", PACKAGE, VERSION);
178       exit (EXIT_SUCCESS);
179     }
180
181   /* Help is requested.  */
182   if (do_help)
183     usage (EXIT_SUCCESS);
184
185   /* `-a' requests the names of all available locales.  */
186   if (do_all != 0)
187     {
188       write_locales ();
189       exit (EXIT_SUCCESS);
190     }
191
192   /* `m' requests the names of all available charmaps.  The names can be
193      used for the -f argument to localedef(3).  */
194   if (do_charmaps != 0)
195     {
196       write_charmaps ();
197       exit (EXIT_SUCCESS);
198     }
199
200   /* Specific information about the current locale are requested.
201      Change to this locale now.  */
202   setlocale (LC_ALL, "");
203
204   /* If no real argument is given we have to print the contents of the
205      current locale definition variables.  These are LANG and the LC_*.  */
206   if (optind == argc && show_keyword_name == 0 && show_category_name == 0)
207     {
208       show_locale_vars ();
209       exit (EXIT_SUCCESS);
210     }
211
212   /* Process all given names.  */
213   while (optind <  argc)
214     show_info (argv[optind++]);
215
216   exit (EXIT_SUCCESS);
217 }
218
219
220 /* Display usage information and exit.  */
221 static void
222 usage(int status)
223 {
224   if (status != EXIT_SUCCESS)
225     fprintf (stderr, gettext ("Try `%s --help' for more information.\n"),
226              program_invocation_name);
227   else
228     printf(gettext ("\
229 Usage: %s [OPTION]... name\n\
230 Mandatory arguments to long options are mandatory for short options too.\n\
231   -h, --help            display this help and exit\n\
232   -v, --version         output version information and exit\n\
233 \n\
234   -a, --all-locales     write names of available locales\n\
235   -m, --charmaps        write names of available charmaps\n\
236 \n\
237   -c, --category-name   write names of selected categories\n\
238   -k, --keyword-name    write names of selected keywords\n\
239 \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         case word:
393           {
394             unsigned int val = (unsigned int) nl_langinfo (item->item_id);
395             printf ("%d", val);
396           }
397           break;
398         default:
399         }
400       putchar ('\n');
401     }
402
403   for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
404     {
405       size_t item_no;
406
407       if (strcmp (name, category[cat_no].name) == 0)
408         /* Print the whole category.  */
409         {
410           if (show_category_name != 0)
411             puts (category[cat_no].name);
412
413           for (item_no = 0; item_no < category[cat_no].number; ++item_no)
414             print_item (&category[cat_no].item_desc[item_no]);
415
416           return;
417         }
418
419       for (item_no = 0; item_no < category[cat_no].number; ++item_no)
420         if (strcmp (name, category[cat_no].item_desc[item_no].name) == 0)
421           {
422             if (show_category_name != 0)
423               puts (category[cat_no].name);
424
425             print_item (&category[cat_no].item_desc[item_no]);
426             return;
427           }
428     }
429 }