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. */
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;
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' },
57 /* We don't have these constants defined because we don't use them. Give
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
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
80 enum { std, opt } status;
81 enum value_type value_type;
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[] = \
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, \
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[])
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))
155 show_category_name = 1;
161 show_keyword_name = 1;
170 error (1, 0, gettext ("illegal option \"%s\""), optarg);
174 /* Version information is requested. */
177 fprintf (stderr, "GNU %s %s\n", PACKAGE, VERSION);
181 /* Help is requested. */
183 usage (EXIT_SUCCESS);
185 /* `-a' requests the names of all available locales. */
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)
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)
212 /* Process all given names. */
213 while (optind < argc)
214 show_info (argv[optind++]);
220 /* Display usage information and exit. */
224 if (status != EXIT_SUCCESS)
225 fprintf (stderr, gettext ("Try `%s --help' for more information.\n"),
226 program_invocation_name);
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);
246 /* Write the names of all available locales to stdout. */
251 struct dirent *dirent;
253 /* `POSIX' locale is always available (POSIX.2 4.34.3). */
256 dir = opendir (LOCALE_PATH);
259 error (1, errno, gettext ("cannot read locale directory `%s'"),
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);
274 /* Write the names of all available character maps to stdout. */
276 write_charmaps (void)
279 struct dirent *dirent;
281 dir = opendir (CHARMAP_PATH);
284 error (1, errno, gettext ("cannot read character map directory `%s'"),
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);
299 /* We have to show the contents of the environments determining the
302 show_locale_vars (void)
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);
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. */
332 show_info (const char *name)
336 void print_item (struct cat_item *item)
338 if (show_keyword_name != 0)
339 printf ("%s=", item->name);
341 switch (item->value_type)
344 printf ("%s%s%s", show_keyword_name ? "\"" : "",
345 nl_langinfo (item->item_id) ? : "",
346 show_keyword_name ? "\"" : "");
353 if (show_keyword_name)
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)
371 const char *val = nl_langinfo (item->item_id);
374 printf ("%d", *val == CHAR_MAX ? -1 : *val);
379 const char *val = nl_langinfo (item->item_id);
380 int cnt = val ? strlen (val) : 0;
384 printf ("%d;", *val == CHAR_MAX ? -1 : *val);
389 printf ("%d", cnt == 0 || *val == CHAR_MAX ? -1 : *val);
394 unsigned int val = (unsigned int) nl_langinfo (item->item_id);
403 for (cat_no = 0; cat_no < NCATEGORIES; ++cat_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]);
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]);