Thu May 30 11:24:05 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / locale / programs / locale.c
blob41c7b1d46e49ee4dea4ac81f4ee868c36fc12a66
1 /* Copyright (C) 1995, 1996 Free Software Foundation, Inc.
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.
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.
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. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
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>
34 #include "localeinfo.h"
37 /* If set print the name of the category. */
38 static int show_category_name;
40 /* If set print the name of the item. */
41 static int show_keyword_name;
43 /* Long options. */
44 static const struct option long_options[] =
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 }
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
68 /* Definition of the data structure which represents a category and its
69 items. */
70 struct category
72 int cat_id;
73 const char *name;
74 size_t number;
75 struct cat_item
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;
86 /* Simple helper macro. */
87 #define NELEMS(arr) ((sizeof (arr)) / (sizeof (arr[0])))
89 /* For some tricky stuff. */
90 #define NO_PAREN(Item, More...) Item, ## More
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 \
101 #include "categories.def"
102 #undef DEFINE_CATEGORY
104 static struct category category[] =
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
112 #define NCATEGORIES NELEMS (category)
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);
124 main (int argc, char *argv[])
126 int optchar;
127 int do_all = 0;
128 int do_help = 0;
129 int do_version = 0;
130 int do_charmaps = 0;
132 /* Set initial values for global varaibles. */
133 show_category_name = 0;
134 show_keyword_name = 0;
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, "");
141 /* Initialize the message catalog. */
142 textdomain (PACKAGE);
144 while ((optchar = getopt_long (argc, argv, "achkmv", long_options, NULL))
145 != EOF)
146 switch (optchar)
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;
173 /* Version information is requested. */
174 if (do_version)
176 fprintf (stderr, "GNU %s %s\n", PACKAGE, VERSION);
177 exit (EXIT_SUCCESS);
180 /* Help is requested. */
181 if (do_help)
182 usage (EXIT_SUCCESS);
184 /* `-a' requests the names of all available locales. */
185 if (do_all != 0)
187 write_locales ();
188 exit (EXIT_SUCCESS);
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)
195 write_charmaps ();
196 exit (EXIT_SUCCESS);
199 /* Specific information about the current locale are requested.
200 Change to this locale now. */
201 setlocale (LC_ALL, "");
203 /* If no real argument is given we have to print the contents of the
204 current locale definition variables. These are LANG and the LC_*. */
205 if (optind == argc && show_keyword_name == 0 && show_category_name == 0)
207 show_locale_vars ();
208 exit (EXIT_SUCCESS);
211 /* Process all given names. */
212 while (optind < argc)
213 show_info (argv[optind++]);
215 exit (EXIT_SUCCESS);
219 /* Display usage information and exit. */
220 static void
221 usage(int status)
223 if (status != EXIT_SUCCESS)
224 fprintf (stderr, gettext ("Try `%s --help' for more information.\n"),
225 program_invocation_name);
226 else
227 printf(gettext ("\
228 Usage: %s [OPTION]... name\n\
229 Mandatory arguments to long options are mandatory for short options too.\n\
230 -h, --help display this help and exit\n\
231 -v, --version output version information and exit\n\
233 -a, --all-locales write names of available locales\n\
234 -m, --charmaps write names of available charmaps\n\
236 -c, --category-name write names of selected categories\n\
237 -k, --keyword-name write names of selected keywords\n\
239 "), program_invocation_name);
241 exit (status);
245 /* Write the names of all available locales to stdout. */
246 static void
247 write_locales (void)
249 DIR *dir;
250 struct dirent *dirent;
252 /* `POSIX' locale is always available (POSIX.2 4.34.3). */
253 puts ("POSIX");
255 dir = opendir (LOCALE_PATH);
256 if (dir == NULL)
258 error (1, errno, gettext ("cannot read locale directory `%s'"),
259 LOCALE_PATH);
260 return;
263 /* Now we can look for all files in the directory. */
264 while ((dirent = readdir (dir)) != NULL)
265 if (strcmp (dirent->d_name, ".") != 0
266 && strcmp (dirent->d_name, "..") != 0)
267 puts (dirent->d_name);
269 closedir (dir);
273 /* Write the names of all available character maps to stdout. */
274 static void
275 write_charmaps (void)
277 DIR *dir;
278 struct dirent *dirent;
280 dir = opendir (CHARMAP_PATH);
281 if (dir == NULL)
283 error (1, errno, gettext ("cannot read character map directory `%s'"),
284 CHARMAP_PATH);
285 return;
288 /* Now we can look for all files in the directory. */
289 while ((dirent = readdir (dir)) != NULL)
290 if (strcmp (dirent->d_name, ".") != 0
291 && strcmp (dirent->d_name, "..") != 0)
292 puts (dirent->d_name);
294 closedir (dir);
298 /* We have to show the contents of the environments determining the
299 locale. */
300 static void
301 show_locale_vars (void)
303 size_t cat_no;
304 const char *lcall = getenv ("LC_ALL");
305 const char *lang = getenv ("LANG") ? : "POSIX";
307 void get_source (const char *name)
309 char *val = getenv (name);
311 if (lcall != NULL || val == NULL)
312 printf ("%s=\"%s\"\n", name, lcall ? : lang);
313 else
314 printf ("%s=%s\n", name, val);
317 /* LANG has to be the first value. */
318 printf ("LANG=%s\n", lang);
320 /* Now all categories in an unspecified order. */
321 for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
322 get_source (category[cat_no].name);
324 /* The last is the LC_ALL value. */
325 printf ("LC_ALL=%s\n", lcall ? : "");
329 /* Show the information request for NAME. */
330 static void
331 show_info (const char *name)
333 size_t cat_no;
335 void print_item (struct cat_item *item)
337 if (show_keyword_name != 0)
338 printf ("%s=", item->name);
340 switch (item->value_type)
342 case string:
343 printf ("%s%s%s", show_keyword_name ? "\"" : "",
344 nl_langinfo (item->item_id) ? : "",
345 show_keyword_name ? "\"" : "");
346 break;
347 case stringarray:
349 int cnt;
350 const char *val;
352 if (show_keyword_name)
353 putchar ('"');
355 for (cnt = 0; cnt < item->max - 1; ++cnt)
357 val = nl_langinfo (item->item_id + cnt);
358 printf ("%s;", val ? : "");
361 val = nl_langinfo (item->item_id + cnt);
362 printf ("%s", val ? : "");
364 if (show_keyword_name)
365 putchar ('"');
367 break;
368 case byte:
370 const char *val = nl_langinfo (item->item_id);
372 if (val != NULL)
373 printf ("%d", *val == CHAR_MAX ? -1 : *val);
375 break;
376 case bytearray:
378 const char *val = nl_langinfo (item->item_id);
379 int cnt = val ? strlen (val) : 0;
381 while (cnt > 1)
383 printf ("%d;", *val == CHAR_MAX ? -1 : *val);
384 --cnt;
385 ++val;
388 printf ("%d", cnt == 0 || *val == CHAR_MAX ? -1 : *val);
390 break;
391 case word:
393 unsigned int val = (unsigned int) nl_langinfo (item->item_id);
394 printf ("%d", val);
396 break;
397 default:
399 putchar ('\n');
402 for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
404 size_t item_no;
406 if (strcmp (name, category[cat_no].name) == 0)
407 /* Print the whole category. */
409 if (show_category_name != 0)
410 puts (category[cat_no].name);
412 for (item_no = 0; item_no < category[cat_no].number; ++item_no)
413 print_item (&category[cat_no].item_desc[item_no]);
415 return;
418 for (item_no = 0; item_no < category[cat_no].number; ++item_no)
419 if (strcmp (name, category[cat_no].item_desc[item_no].name) == 0)
421 if (show_category_name != 0)
422 puts (category[cat_no].name);
424 print_item (&category[cat_no].item_desc[item_no]);
425 return;