update from main archive 960912
[platform/upstream/glibc.git] / locale / programs / locale.c
1 /* locale - Implementation of the locale program according to POSIX 1003.2
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB.  If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <dirent.h>
26 #include <error.h>
27 #include <getopt.h>
28 #include <langinfo.h>
29 #include <libintl.h>
30 #include <limits.h>
31 #include <locale.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <string.h>
37
38 #include "localeinfo.h"
39
40
41 /* If set print the name of the category.  */
42 static int show_category_name;
43
44 /* If set print the name of the item.  */
45 static int show_keyword_name;
46
47 /* Long options.  */
48 static const struct option long_options[] =
49 {
50   { "all-locales", no_argument, NULL, 'a' },
51   { "category-name", no_argument, &show_category_name, 1 },
52   { "charmaps", no_argument, NULL, 'm' },
53   { "help", no_argument, NULL, 'h' },
54   { "keyword-name", no_argument, &show_keyword_name, 1 },
55   { "version", no_argument, NULL, 'V' },
56   { NULL, 0, NULL, 0 }
57 };
58
59
60 /* We don't have these constants defined because we don't use them.  Give
61    default values.  */
62 #define CTYPE_MB_CUR_MIN 0
63 #define CTYPE_MB_CUR_MAX 0
64 #define CTYPE_HASH_SIZE 0
65 #define CTYPE_HASH_LAYERS 0
66 #define CTYPE_CLASS 0
67 #define CTYPE_TOUPPER_EB 0
68 #define CTYPE_TOLOWER_EB 0
69 #define CTYPE_TOUPPER_EL 0
70 #define CTYPE_TOLOWER_EL 0
71
72 /* Definition of the data structure which represents a category and its
73    items.  */
74 struct category
75 {
76   int cat_id;
77   const char *name;
78   size_t number;
79   struct cat_item
80   {
81     int item_id;
82     const char *name;
83     enum { std, opt } status;
84     enum value_type value_type;
85     int min;
86     int max;
87   } *item_desc;
88 };
89
90 /* Simple helper macro.  */
91 #define NELEMS(arr) ((sizeof (arr)) / (sizeof (arr[0])))
92
93 /* For some tricky stuff.  */
94 #define NO_PAREN(Item, More...) Item, ## More
95
96 /* We have all categories defined in `categories.def'.  Now construct
97    the description and data structure used for all categories.  */
98 #define DEFINE_ELEMENT(Item, More...) { Item, ## More },
99 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out)      \
100     static struct cat_item category##_desc[] =                                \
101       {                                                                       \
102         NO_PAREN items                                                        \
103       };
104
105 #include "categories.def"
106 #undef DEFINE_CATEGORY
107
108 static struct category category[] =
109   {
110 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out)      \
111     [category] = { _NL_NUM_##category, name, NELEMS (category##_desc),        \
112                    category##_desc },
113 #include "categories.def"
114 #undef DEFINE_CATEGORY
115   };
116 #define NCATEGORIES NELEMS (category)
117
118
119 /* Automatically set variable.  */
120 extern const char *__progname;
121
122 /* helper function for extended name handling.  */
123 extern void locale_special (const char *name, int show_category_name,
124                             int show_keyword_name);
125
126 /* Prototypes for local functions.  */
127 static void usage (int status) __attribute__ ((noreturn));
128 static void write_locales (void);
129 static void write_charmaps (void);
130 static void show_locale_vars (void);
131 static void show_info (const char *name);
132
133
134 int
135 main (int argc, char *argv[])
136 {
137   int optchar;
138   int do_all = 0;
139   int do_help = 0;
140   int do_version = 0;
141   int do_charmaps = 0;
142
143   /* Set initial values for global variables.  */
144   show_category_name = 0;
145   show_keyword_name = 0;
146
147   /* Set locale.  Do not set LC_ALL because the other categories must
148      not be affected (acccording to POSIX.2).  */
149   setlocale (LC_CTYPE, "");
150   setlocale (LC_MESSAGES, "");
151
152   /* Initialize the message catalog.  */
153   textdomain (PACKAGE);
154
155   while ((optchar = getopt_long (argc, argv, "achkmV", long_options, NULL))
156          != EOF)
157     switch (optchar)
158       {
159       case '\0':                /* Long option.  */
160         break;
161       case 'a':
162         do_all = 1;
163         break;
164       case 'c':
165         show_category_name = 1;
166         break;
167       case 'h':
168         do_help = 1;
169         break;
170       case 'k':
171         show_keyword_name = 1;
172         break;
173       case 'm':
174         do_charmaps = 1;
175         break;
176       case 'V':
177         do_version = 1;
178         break;
179       default:
180         usage (EXIT_FAILURE);
181       }
182
183   /* Version information is requested.  */
184   if (do_version)
185     {
186       fprintf (stderr, "%s - GNU %s %s\n", __progname, PACKAGE, VERSION);
187       exit (EXIT_SUCCESS);
188     }
189
190   /* Help is requested.  */
191   if (do_help)
192     usage (EXIT_SUCCESS);
193
194   /* `-a' requests the names of all available locales.  */
195   if (do_all != 0)
196     {
197       write_locales ();
198       exit (EXIT_SUCCESS);
199     }
200
201   /* `m' requests the names of all available charmaps.  The names can be
202      used for the -f argument to localedef(3).  */
203   if (do_charmaps != 0)
204     {
205       write_charmaps ();
206       exit (EXIT_SUCCESS);
207     }
208
209   /* Specific information about the current locale are requested.
210      Change to this locale now.  */
211   setlocale (LC_ALL, "");
212
213   /* If no real argument is given we have to print the contents of the
214      current locale definition variables.  These are LANG and the LC_*.  */
215   if (optind == argc && show_keyword_name == 0 && show_category_name == 0)
216     {
217       show_locale_vars ();
218       exit (EXIT_SUCCESS);
219     }
220
221   /* Process all given names.  */
222   while (optind <  argc)
223     show_info (argv[optind++]);
224
225   exit (EXIT_SUCCESS);
226 }
227
228
229 /* Display usage information and exit.  */
230 static void
231 usage (int status)
232 {
233   if (status != EXIT_SUCCESS)
234     fprintf (stderr, gettext ("Try `%s --help' for more information.\n"),
235              __progname);
236   else
237     {
238       printf (gettext ("\
239 Usage: %s [OPTION]... name\n\
240 Mandatory arguments to long options are mandatory for short options too.\n\
241   -h, --help            display this help and exit\n\
242   -V, --version         output version information and exit\n\
243 \n\
244   -a, --all-locales     write names of available locales\n\
245   -m, --charmaps        write names of available charmaps\n\
246 \n\
247   -c, --category-name   write names of selected categories\n\
248   -k, --keyword-name    write names of selected keywords\n"),
249               __progname);
250       printf (gettext ("Report bugs to <bug-glibc@prep.ai.mit.edu>.\n"));
251     }
252
253   exit (status);
254 }
255
256
257 /* Write the names of all available locales to stdout.  */
258 static void
259 write_locales (void)
260 {
261   DIR *dir;
262   struct dirent *dirent;
263
264   /* `POSIX' locale is always available (POSIX.2 4.34.3).  */
265   puts ("POSIX");
266
267   dir = opendir (LOCALE_PATH);
268   if (dir == NULL)
269     {
270       error (1, errno, gettext ("cannot read locale directory `%s'"),
271              LOCALE_PATH);
272       return;
273     }
274
275   /* Now we can look for all files in the directory.  */
276   while ((dirent = readdir (dir)) != NULL)
277     if (strcmp (dirent->d_name, ".") != 0
278         && strcmp (dirent->d_name, "..") != 0)
279       puts (dirent->d_name);
280
281   closedir (dir);
282 }
283
284
285 /* Write the names of all available character maps to stdout.  */
286 static void
287 write_charmaps (void)
288 {
289   DIR *dir;
290   struct dirent *dirent;
291
292   dir = opendir (CHARMAP_PATH);
293   if (dir == NULL)
294     {
295       error (1, errno, gettext ("cannot read character map directory `%s'"),
296              CHARMAP_PATH);
297       return;
298     }
299
300   /* Now we can look for all files in the directory.  */
301   while ((dirent = readdir (dir)) != NULL)
302     if (strcmp (dirent->d_name, ".") != 0
303         && strcmp (dirent->d_name, "..") != 0)
304       puts (dirent->d_name);
305
306   closedir (dir);
307 }
308
309
310 /* We have to show the contents of the environments determining the
311    locale.  */
312 static void
313 show_locale_vars (void)
314 {
315   size_t cat_no;
316   const char *lcall = getenv ("LC_ALL");
317   const char *lang = getenv ("LANG") ? : "POSIX";
318
319   void get_source (const char *name)
320     {
321       char *val = getenv (name);
322
323       if (lcall != NULL || val == NULL)
324         printf ("%s=\"%s\"\n", name, lcall ? : lang);
325       else
326         printf ("%s=%s\n", name, val);
327     }
328
329   /* LANG has to be the first value.  */
330   printf ("LANG=%s\n", lang);
331
332   /* Now all categories in an unspecified order.  */
333   for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
334     get_source (category[cat_no].name);
335
336   /* The last is the LC_ALL value.  */
337   printf ("LC_ALL=%s\n", lcall ? : "");
338 }
339
340
341 /* Show the information request for NAME.  */
342 static void
343 show_info (const char *name)
344 {
345   size_t cat_no;
346
347   void print_item (struct cat_item *item)
348     {
349       if (show_keyword_name != 0)
350         printf ("%s=", item->name);
351
352       switch (item->value_type)
353         {
354         case string:
355           printf ("%s%s%s", show_keyword_name ? "\"" : "",
356                   nl_langinfo (item->item_id) ? : "",
357                   show_keyword_name ? "\"" : "");
358           break;
359         case stringarray:
360           {
361             int cnt;
362             const char *val;
363
364             if (show_keyword_name)
365               putchar ('"');
366
367             for (cnt = 0; cnt < item->max - 1; ++cnt)
368               {
369                 val = nl_langinfo (item->item_id + cnt);
370                 printf ("%s;", val ? : "");
371               }
372
373             val = nl_langinfo (item->item_id + cnt);
374             printf ("%s", val ? : "");
375
376             if (show_keyword_name)
377               putchar ('"');
378           }
379           break;
380         case byte:
381           {
382             const char *val = nl_langinfo (item->item_id);
383
384             if (val != NULL)
385               printf ("%d", *val == CHAR_MAX ? -1 : *val);
386           }
387           break;
388         case bytearray:
389           {
390             const char *val = nl_langinfo (item->item_id);
391             int cnt = val ? strlen (val) : 0;
392
393             while (cnt > 1)
394               {
395                 printf ("%d;", *val == CHAR_MAX ? -1 : *val);
396                 --cnt;
397                 ++val;
398               }
399
400             printf ("%d", cnt == 0 || *val == CHAR_MAX ? -1 : *val);
401           }
402           break;
403         case word:
404           {
405             unsigned int val = (unsigned int) nl_langinfo (item->item_id);
406             printf ("%d", val);
407           }
408           break;
409         default:
410         }
411       putchar ('\n');
412     }
413
414   for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
415     {
416       size_t item_no;
417
418       if (strcmp (name, category[cat_no].name) == 0)
419         /* Print the whole category.  */
420         {
421           if (show_category_name != 0)
422             puts (category[cat_no].name);
423
424           for (item_no = 0; item_no < category[cat_no].number; ++item_no)
425             print_item (&category[cat_no].item_desc[item_no]);
426
427           return;
428         }
429
430       for (item_no = 0; item_no < category[cat_no].number; ++item_no)
431         if (strcmp (name, category[cat_no].item_desc[item_no].name) == 0)
432           {
433             if (show_category_name != 0)
434               puts (category[cat_no].name);
435
436             print_item (&category[cat_no].item_desc[item_no]);
437             return;
438           }
439     }
440
441   /* When we get to here the name is not standard ones.  For testing
442      and perhpas advanced use we allow some more symbols.  */
443   locale_special (name, show_category_name, show_keyword_name);
444 }