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. */
29 /*#include "localedef.h"*/
30 #include "localeinfo.h"
33 /* If set dump C code describing the current locale. */
36 /* If set print the name of the category. */
37 static int show_category_name;
39 /* If set print the name of the item. */
40 static int show_keyword_name;
43 static const struct option long_options[] =
45 { "all-locales", no_argument, NULL, 'a' },
46 { "category-name", no_argument, &show_category_name, 1 },
47 { "charmaps", no_argument, NULL, 'm' },
48 { "dump", no_argument, &do_dump, 1 },
49 { "help", no_argument, NULL, 'h' },
50 { "keyword-name", no_argument, &show_keyword_name, 1 },
51 { "version", no_argument, NULL, 'v' },
56 /* We don't have these constants defined because we don't use them. Give
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
63 #define CTYPE_TOUPPER_EB 0
64 #define CTYPE_TOLOWER_EB 0
65 #define CTYPE_TOUPPER_EL 0
66 #define CTYPE_TOLOWER_EL 0
73 enum { std, opt } status;
74 enum value_type value_type;
80 /* We have all categories defined in `categories.def'. Now construct
81 the description and data structure used for all categories. */
82 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out) \
83 static struct cat_item category##_desc[] = \
88 #include "locale/aux/categories.def"
89 #undef DEFINE_CATEGORY
91 static struct category category[] =
93 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out) \
94 { _NL_NUM_##category, name, NELEMS (category##_desc) - 1, \
95 category##_desc, NULL, NULL, NULL, out },
96 #include "locale/aux/categories.def"
97 #undef DEFINE_CATEGORY
99 #define NCATEGORIES NELEMS (category)
102 /* Prototypes for local functions. */
103 static void usage (int status) __attribute__ ((noreturn));
104 static void write_locales (void);
105 static void write_charmaps (void);
106 static void show_locale_vars (void);
107 static void show_info (const char *name);
108 static void dump_category (const char *name);
112 main (int argc, char *argv[])
120 /* Set initial values for global varaibles. */
122 show_category_name = 0;
123 show_keyword_name = 0;
125 /* Set locale. Do not set LC_ALL because the other categories must
126 not be affected (acccording to POSIX.2). */
127 setlocale (LC_CTYPE, "");
128 setlocale (LC_MESSAGES, "");
130 /* Initialize the message catalog. */
131 textdomain (PACKAGE);
133 while ((optchar = getopt_long (argc, argv, "achkmv", long_options, NULL))
143 show_category_name = 1;
149 show_keyword_name = 1;
158 error (1, 0, gettext ("illegal option \"%s\""), optarg);
162 /* Version information is requested. */
165 fprintf (stderr, "GNU %s %s\n", PACKAGE, VERSION);
169 /* Help is requested. */
171 usage (EXIT_SUCCESS);
177 /* Generated by GNU %s %s. */\n\
179 #include \"localeinfo.h\"\n", program_invocation_name, VERSION);
181 while (optind < argc)
182 dump_category (argv[optind++]);
187 /* `-a' requests the names of all available locales. */
194 /* `m' requests the names of all available charmaps. The names can be
195 used for the -f argument to localedef(3). */
196 if (do_charmaps != 0)
202 /* If no real argument is given we have to print the contents of the
203 current locale definition variables. These are LANG and the LC_*. */
204 if (optind == argc && show_keyword_name == 0 && show_category_name == 0)
210 /* Process all given names. */
211 while (optind < argc)
212 show_info (argv[optind++]);
218 /* Display usage information and exit. */
222 if (status != EXIT_SUCCESS)
223 fprintf (stderr, gettext ("Try `%s --help' for more information.\n"),
224 program_invocation_name);
227 Usage: %s [OPTION]... name\n\
228 Mandatory arguments to long options are mandatory for short options too.\n\
229 -h, --help display this help and exit\n\
230 -v, --version output version information and exit\n\
232 -a, --all-locales write names of available locales\n\
233 -m, --charmaps write names of available charmaps\n\
235 -c, --category-name write names of selected categories\n\
236 -k, --keyword-name write names of selected keywords\n\
238 --dump dump C code describing the current locale\n\
239 (this code can be used in the C library)\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);
397 for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
401 if (category[cat_no].outfct != NULL)
402 /* Categories which need special handling of the output are
403 not written. This is especially for LC_CTYPE and LC_COLLATE.
404 It does not make sense to have this large number of cryptic
405 characters displayed. */
408 if (strcmp (name, category[cat_no].name) == 0)
409 /* Print the whole category. */
411 if (show_category_name != 0)
412 puts (category[cat_no].name);
414 for (item_no = 0; item_no < category[cat_no].number; ++item_no)
415 print_item (&category[cat_no].item_desc[item_no]);
420 for (item_no = 0; item_no < category[cat_no].number; ++item_no)
421 if (strcmp (name, category[cat_no].item_desc[item_no].name) == 0)
423 if (show_category_name != 0)
424 puts (category[cat_no].name);
426 print_item (&category[cat_no].item_desc[item_no]);
434 dump_category (const char *name)
437 size_t cat_no, item_no, nstrings;
439 for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
440 if (strcmp (name, category[cat_no].name) == 0)
443 if (cat_no >= NCATEGORIES)
446 /* The NAME specifies a correct locale category. */
447 if (category[cat_no].outfct != NULL)
449 category[cat_no].outfct ();
453 locname = (getenv ("LC_ALL") ?: getenv (name) ?:
454 getenv ("LANG") ?: (char *) "POSIX");
456 /* Determine the number of strings in advance. */
458 for (item_no = 0; item_no < category[cat_no].number; ++item_no)
459 switch (category[cat_no].item_desc[item_no].value_type)
467 nstrings += category[cat_no].item_desc[item_no].max;
471 printf ("\nconst struct locale_data _nl_%s_%s =\n{\n"
472 " NULL, 0, /* no file mapped */\n %Zu,\n {\n",
473 locname, name, nstrings);
475 for (item_no = 0; item_no < category[cat_no].number; ++item_no)
476 switch (category[cat_no].item_desc[item_no].value_type)
480 const char *val = nl_langinfo (
481 category[cat_no].item_desc[item_no].item_id);
484 printf (" \"%s\",\n", val);
494 for (cnt = 0; cnt < category[cat_no].item_desc[item_no].max; ++cnt)
497 category[cat_no].item_desc[item_no].item_id + cnt);
500 printf (" \"%s\",\n", val);
508 const char *val = nl_langinfo (
509 category[cat_no].item_desc[item_no].item_id);
512 printf (" \"\\%o\",\n",
513 *(unsigned char *) val ? : UCHAR_MAX);
520 const char *bytes = nl_langinfo (
521 category[cat_no].item_desc[item_no].item_id);
525 fputs (" \"", stdout);
528 printf ("\\%o", *(unsigned char *) bytes++);
529 while (*bytes != '\0');
531 printf ("\\%o", UCHAR_MAX);