Thu Jun 13 17:25:11 1996 David Mosberger-Tang <davidm@azstarnet.com>
[glibc.git] / locale / programs / locale.c
blobef5d9ae82550225f971fe1c23f07cf4c964df496
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>
33 #include <string.h>
35 #include "localeinfo.h"
38 /* If set print the name of the category. */
39 static int show_category_name;
41 /* If set print the name of the item. */
42 static int show_keyword_name;
44 /* Long options. */
45 static const struct option long_options[] =
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 }
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
69 /* Definition of the data structure which represents a category and its
70 items. */
71 struct category
73 int cat_id;
74 const char *name;
75 size_t number;
76 struct cat_item
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;
87 /* Simple helper macro. */
88 #define NELEMS(arr) ((sizeof (arr)) / (sizeof (arr[0])))
90 /* For some tricky stuff. */
91 #define NO_PAREN(Item, More...) Item, ## More
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 \
102 #include "categories.def"
103 #undef DEFINE_CATEGORY
105 static struct category category[] =
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
113 #define NCATEGORIES NELEMS (category)
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);
125 main (int argc, char *argv[])
127 int optchar;
128 int do_all = 0;
129 int do_help = 0;
130 int do_version = 0;
131 int do_charmaps = 0;
133 /* Set initial values for global varaibles. */
134 show_category_name = 0;
135 show_keyword_name = 0;
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, "");
142 /* Initialize the message catalog. */
143 textdomain (PACKAGE);
145 while ((optchar = getopt_long (argc, argv, "achkmv", long_options, NULL))
146 != EOF)
147 switch (optchar)
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;
174 /* Version information is requested. */
175 if (do_version)
177 fprintf (stderr, "GNU %s %s\n", PACKAGE, VERSION);
178 exit (EXIT_SUCCESS);
181 /* Help is requested. */
182 if (do_help)
183 usage (EXIT_SUCCESS);
185 /* `-a' requests the names of all available locales. */
186 if (do_all != 0)
188 write_locales ();
189 exit (EXIT_SUCCESS);
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)
196 write_charmaps ();
197 exit (EXIT_SUCCESS);
200 /* Specific information about the current locale are requested.
201 Change to this locale now. */
202 setlocale (LC_ALL, "");
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)
208 show_locale_vars ();
209 exit (EXIT_SUCCESS);
212 /* Process all given names. */
213 while (optind < argc)
214 show_info (argv[optind++]);
216 exit (EXIT_SUCCESS);
220 /* Display usage information and exit. */
221 static void
222 usage(int status)
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\
234 -a, --all-locales write names of available locales\n\
235 -m, --charmaps write names of available charmaps\n\
237 -c, --category-name write names of selected categories\n\
238 -k, --keyword-name write names of selected keywords\n\
240 "), program_invocation_name);
242 exit (status);
246 /* Write the names of all available locales to stdout. */
247 static void
248 write_locales (void)
250 DIR *dir;
251 struct dirent *dirent;
253 /* `POSIX' locale is always available (POSIX.2 4.34.3). */
254 puts ("POSIX");
256 dir = opendir (LOCALE_PATH);
257 if (dir == NULL)
259 error (1, errno, gettext ("cannot read locale directory `%s'"),
260 LOCALE_PATH);
261 return;
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);
270 closedir (dir);
274 /* Write the names of all available character maps to stdout. */
275 static void
276 write_charmaps (void)
278 DIR *dir;
279 struct dirent *dirent;
281 dir = opendir (CHARMAP_PATH);
282 if (dir == NULL)
284 error (1, errno, gettext ("cannot read character map directory `%s'"),
285 CHARMAP_PATH);
286 return;
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);
295 closedir (dir);
299 /* We have to show the contents of the environments determining the
300 locale. */
301 static void
302 show_locale_vars (void)
304 size_t cat_no;
305 const char *lcall = getenv ("LC_ALL");
306 const char *lang = getenv ("LANG") ? : "POSIX";
308 void get_source (const char *name)
310 char *val = getenv (name);
312 if (lcall != NULL || val == NULL)
313 printf ("%s=\"%s\"\n", name, lcall ? : lang);
314 else
315 printf ("%s=%s\n", name, val);
318 /* LANG has to be the first value. */
319 printf ("LANG=%s\n", lang);
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);
325 /* The last is the LC_ALL value. */
326 printf ("LC_ALL=%s\n", lcall ? : "");
330 /* Show the information request for NAME. */
331 static void
332 show_info (const char *name)
334 size_t cat_no;
336 void print_item (struct cat_item *item)
338 if (show_keyword_name != 0)
339 printf ("%s=", item->name);
341 switch (item->value_type)
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:
350 int cnt;
351 const char *val;
353 if (show_keyword_name)
354 putchar ('"');
356 for (cnt = 0; cnt < item->max - 1; ++cnt)
358 val = nl_langinfo (item->item_id + cnt);
359 printf ("%s;", val ? : "");
362 val = nl_langinfo (item->item_id + cnt);
363 printf ("%s", val ? : "");
365 if (show_keyword_name)
366 putchar ('"');
368 break;
369 case byte:
371 const char *val = nl_langinfo (item->item_id);
373 if (val != NULL)
374 printf ("%d", *val == CHAR_MAX ? -1 : *val);
376 break;
377 case bytearray:
379 const char *val = nl_langinfo (item->item_id);
380 int cnt = val ? strlen (val) : 0;
382 while (cnt > 1)
384 printf ("%d;", *val == CHAR_MAX ? -1 : *val);
385 --cnt;
386 ++val;
389 printf ("%d", cnt == 0 || *val == CHAR_MAX ? -1 : *val);
391 break;
392 case word:
394 unsigned int val = (unsigned int) nl_langinfo (item->item_id);
395 printf ("%d", val);
397 break;
398 default:
400 putchar ('\n');
403 for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
405 size_t item_no;
407 if (strcmp (name, category[cat_no].name) == 0)
408 /* Print the whole category. */
410 if (show_category_name != 0)
411 puts (category[cat_no].name);
413 for (item_no = 0; item_no < category[cat_no].number; ++item_no)
414 print_item (&category[cat_no].item_desc[item_no]);
416 return;
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)
422 if (show_category_name != 0)
423 puts (category[cat_no].name);
425 print_item (&category[cat_no].item_desc[item_no]);
426 return;