* posix/glob.h (__glob_opendir_hook, __glob_readdir_hook,
[glibc.git] / locale / programs / locale.c
blob5196fa51fe5b050f0f586d81515e56b5de347345
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 variables. */
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 usage (EXIT_FAILURE);
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"),
238 program_invocation_name);
240 exit (status);
244 /* Write the names of all available locales to stdout. */
245 static void
246 write_locales (void)
248 DIR *dir;
249 struct dirent *dirent;
251 /* `POSIX' locale is always available (POSIX.2 4.34.3). */
252 puts ("POSIX");
254 dir = opendir (LOCALE_PATH);
255 if (dir == NULL)
257 error (1, errno, gettext ("cannot read locale directory `%s'"),
258 LOCALE_PATH);
259 return;
262 /* Now we can look for all files in the directory. */
263 while ((dirent = readdir (dir)) != NULL)
264 if (strcmp (dirent->d_name, ".") != 0
265 && strcmp (dirent->d_name, "..") != 0)
266 puts (dirent->d_name);
268 closedir (dir);
272 /* Write the names of all available character maps to stdout. */
273 static void
274 write_charmaps (void)
276 DIR *dir;
277 struct dirent *dirent;
279 dir = opendir (CHARMAP_PATH);
280 if (dir == NULL)
282 error (1, errno, gettext ("cannot read character map directory `%s'"),
283 CHARMAP_PATH);
284 return;
287 /* Now we can look for all files in the directory. */
288 while ((dirent = readdir (dir)) != NULL)
289 if (strcmp (dirent->d_name, ".") != 0
290 && strcmp (dirent->d_name, "..") != 0)
291 puts (dirent->d_name);
293 closedir (dir);
297 /* We have to show the contents of the environments determining the
298 locale. */
299 static void
300 show_locale_vars (void)
302 size_t cat_no;
303 const char *lcall = getenv ("LC_ALL");
304 const char *lang = getenv ("LANG") ? : "POSIX";
306 void get_source (const char *name)
308 char *val = getenv (name);
310 if (lcall != NULL || val == NULL)
311 printf ("%s=\"%s\"\n", name, lcall ? : lang);
312 else
313 printf ("%s=%s\n", name, val);
316 /* LANG has to be the first value. */
317 printf ("LANG=%s\n", lang);
319 /* Now all categories in an unspecified order. */
320 for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
321 get_source (category[cat_no].name);
323 /* The last is the LC_ALL value. */
324 printf ("LC_ALL=%s\n", lcall ? : "");
328 /* Show the information request for NAME. */
329 static void
330 show_info (const char *name)
332 size_t cat_no;
334 void print_item (struct cat_item *item)
336 if (show_keyword_name != 0)
337 printf ("%s=", item->name);
339 switch (item->value_type)
341 case string:
342 printf ("%s%s%s", show_keyword_name ? "\"" : "",
343 nl_langinfo (item->item_id) ? : "",
344 show_keyword_name ? "\"" : "");
345 break;
346 case stringarray:
348 int cnt;
349 const char *val;
351 if (show_keyword_name)
352 putchar ('"');
354 for (cnt = 0; cnt < item->max - 1; ++cnt)
356 val = nl_langinfo (item->item_id + cnt);
357 printf ("%s;", val ? : "");
360 val = nl_langinfo (item->item_id + cnt);
361 printf ("%s", val ? : "");
363 if (show_keyword_name)
364 putchar ('"');
366 break;
367 case byte:
369 const char *val = nl_langinfo (item->item_id);
371 if (val != NULL)
372 printf ("%d", *val == CHAR_MAX ? -1 : *val);
374 break;
375 case bytearray:
377 const char *val = nl_langinfo (item->item_id);
378 int cnt = val ? strlen (val) : 0;
380 while (cnt > 1)
382 printf ("%d;", *val == CHAR_MAX ? -1 : *val);
383 --cnt;
384 ++val;
387 printf ("%d", cnt == 0 || *val == CHAR_MAX ? -1 : *val);
389 break;
390 case word:
392 unsigned int val = (unsigned int) nl_langinfo (item->item_id);
393 printf ("%d", val);
395 break;
396 default:
398 putchar ('\n');
401 for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
403 size_t item_no;
405 if (strcmp (name, category[cat_no].name) == 0)
406 /* Print the whole category. */
408 if (show_category_name != 0)
409 puts (category[cat_no].name);
411 for (item_no = 0; item_no < category[cat_no].number; ++item_no)
412 print_item (&category[cat_no].item_desc[item_no]);
414 return;
417 for (item_no = 0; item_no < category[cat_no].number; ++item_no)
418 if (strcmp (name, category[cat_no].item_desc[item_no].name) == 0)
420 if (show_category_name != 0)
421 puts (category[cat_no].name);
423 print_item (&category[cat_no].item_desc[item_no]);
424 return;