1 /* Copyright (C) 1995 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"
32 /* If set dump C code describing the current locale. */
35 /* If set print the name of the category. */
36 static int show_category_name
;
38 /* If set print the name of the item. */
39 static int show_keyword_name
;
42 static const struct option long_options
[] =
44 { "all-locales", no_argument
, NULL
, 'a' },
45 { "category-name", no_argument
, &show_category_name
, 1 },
46 { "charmaps", no_argument
, NULL
, 'm' },
47 { "dump", no_argument
, &do_dump
, 1 },
48 { "help", no_argument
, NULL
, 'h' },
49 { "keyword-name", no_argument
, &show_keyword_name
, 1 },
50 { "version", no_argument
, NULL
, 'v' },
55 /* We don't have these constants defined because we don't use them. Give
57 #define CTYPE_MB_CUR_MIN 0
58 #define CTYPE_MB_CUR_MAX 0
59 #define CTYPE_HASH_SIZE 0
60 #define CTYPE_HASH_LAYERS 0
62 #define CTYPE_TOUPPER_EB 0
63 #define CTYPE_TOLOWER_EB 0
64 #define CTYPE_TOUPPER_EL 0
65 #define CTYPE_TOLOWER_EL 0
68 /* We have all categories defined in `categories.def'. Now construct
69 the description and data structure used for all categories. */
70 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out) \
71 static struct cat_item category##_desc[] = \
76 #include "categories.def"
77 #undef DEFINE_CATEGORY
79 static struct category category
[] =
81 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out) \
82 { _NL_NUM_##category, name, NELEMS (category##_desc) - 1, \
83 category##_desc, NULL, NULL, NULL, out },
84 #include "categories.def"
85 #undef DEFINE_CATEGORY
87 #define NCATEGORIES NELEMS (category)
90 /* Prototypes for local functions. */
91 static void usage (int status
) __attribute__ ((noreturn
));
92 static void write_locales (void);
93 static void write_charmaps (void);
94 static void show_locale_vars (void);
95 static void show_info (const char *name
);
96 static void dump_category (const char *name
);
100 main (int argc
, char *argv
[])
108 /* Set initial values for global varaibles. */
110 show_category_name
= 0;
111 show_keyword_name
= 0;
113 /* Set locale. Do not set LC_ALL because the other categories must
114 not be affected (acccording to POSIX.2). */
115 setlocale (LC_CTYPE
, "");
116 setlocale (LC_MESSAGES
, "");
118 /* Initialize the message catalog. */
119 textdomain (PACKAGE
);
121 while ((optchar
= getopt_long (argc
, argv
, "achkmv", long_options
, NULL
))
131 show_category_name
= 1;
137 show_keyword_name
= 1;
146 error (1, 0, gettext ("illegal option \"%s\""), optarg
);
150 /* Version information is requested. */
153 fprintf (stderr
, "GNU %s %s\n", PACKAGE
, VERSION
);
157 /* Help is requested. */
159 usage (EXIT_SUCCESS
);
165 /* Generated by GNU %s %s. */\n\
167 #include \"localeinfo.h\"\n", program_invocation_name
, VERSION
);
169 while (optind
< argc
)
170 dump_category (argv
[optind
++]);
175 /* `-a' requests the names of all available locales. */
182 /* `m' requests the names of all available charmaps. The names can be
183 used for the -f argument to localedef(3). */
184 if (do_charmaps
!= 0)
190 /* If no real argument is given we have to print the contents of the
191 current locale definition variables. These are LANG and the LC_*. */
192 if (optind
== argc
&& show_keyword_name
== 0 && show_category_name
== 0)
198 /* Process all given names. */
199 while (optind
< argc
)
200 show_info (argv
[optind
++]);
206 /* Display usage information and exit. */
210 if (status
!= EXIT_SUCCESS
)
211 fprintf (stderr
, gettext ("Try `%s --help' for more information.\n"),
212 program_invocation_name
);
215 Usage: %s [OPTION]... name\n\
216 Mandatory arguments to long options are mandatory for short options too.\n\
217 -h, --help display this help and exit\n\
218 -v, --version output version information and exit\n\
220 -a, --all-locales write names of available locales\n\
221 -m, --charmaps write names of available charmaps\n\
223 -c, --category-name write names of selected categories\n\
224 -k, --keyword-name write names of selected keywords\n\
226 --dump dump C code describing the current locale\n\
227 (this code can be used in the C library)\n\
228 "), program_invocation_name
);
234 /* Write the names of all available locales to stdout. */
239 struct dirent
*dirent
;
241 /* `POSIX' locale is always available (POSIX.2 4.34.3). */
244 dir
= opendir (LOCALE_PATH
);
247 error (1, errno
, gettext ("cannot read locale directory `%s'"),
252 /* Now we can look for all files in the directory. */
253 while ((dirent
= readdir (dir
)) != NULL
)
254 if (strcmp (dirent
->d_name
, ".") != 0
255 && strcmp (dirent
->d_name
, "..") != 0)
256 puts (dirent
->d_name
);
262 /* Write the names of all available character maps to stdout. */
264 write_charmaps (void)
267 struct dirent
*dirent
;
269 dir
= opendir (CHARMAP_PATH
);
272 error (1, errno
, gettext ("cannot read character map directory `%s'"),
277 /* Now we can look for all files in the directory. */
278 while ((dirent
= readdir (dir
)) != NULL
)
279 if (strcmp (dirent
->d_name
, ".") != 0
280 && strcmp (dirent
->d_name
, "..") != 0)
281 puts (dirent
->d_name
);
287 /* We have to show the contents of the environments determining the
290 show_locale_vars (void)
293 const char *lcall
= getenv ("LC_ALL");
294 const char *lang
= getenv ("LANG") ? : "POSIX";
296 void get_source (const char *name
)
298 char *val
= getenv (name
);
300 if (lcall
!= NULL
|| val
== NULL
)
301 printf ("%s=\"%s\"\n", name
, lcall
? : lang
);
303 printf ("%s=%s\n", name
, val
);
306 /* LANG has to be the first value. */
307 printf ("LANG=%s\n", lang
);
309 /* Now all categories in an unspecified order. */
310 for (cat_no
= 0; cat_no
< NCATEGORIES
; ++cat_no
)
311 get_source (category
[cat_no
].name
);
313 /* The last is the LC_ALL value. */
314 printf ("LC_ALL=%s\n", lcall
? : "");
318 /* Show the information request for NAME. */
320 show_info (const char *name
)
324 void print_item (struct cat_item
*item
)
326 if (show_keyword_name
!= 0)
327 printf ("%s=", item
->name
);
329 switch (item
->value_type
)
332 printf ("%s%s%s", show_keyword_name
? "\"" : "",
333 nl_langinfo (item
->item_id
) ? : "",
334 show_keyword_name
? "\"" : "");
341 if (show_keyword_name
)
344 for (cnt
= 0; cnt
< item
->max
- 1; ++cnt
)
346 val
= nl_langinfo (item
->item_id
+ cnt
);
347 printf ("%s;", val
? : "");
350 val
= nl_langinfo (item
->item_id
+ cnt
);
351 printf ("%s", val
? : "");
353 if (show_keyword_name
)
359 const char *val
= nl_langinfo (item
->item_id
);
362 printf ("%d", *val
== CHAR_MAX
? -1 : *val
);
367 const char *val
= nl_langinfo (item
->item_id
);
368 int cnt
= val
? strlen (val
) : 0;
372 printf ("%d;", *val
== CHAR_MAX
? -1 : *val
);
377 printf ("%d", cnt
== 0 || *val
== CHAR_MAX
? -1 : *val
);
385 for (cat_no
= 0; cat_no
< NCATEGORIES
; ++cat_no
)
389 if (category
[cat_no
].outfct
!= NULL
)
390 /* Categories which need special handling of the output are
391 not written. This is especially for LC_CTYPE and LC_COLLATE.
392 It does not make sense to have this large number of cryptic
393 characters displayed. */
396 if (strcmp (name
, category
[cat_no
].name
) == 0)
397 /* Print the whole category. */
399 if (show_category_name
!= 0)
400 puts (category
[cat_no
].name
);
402 for (item_no
= 0; item_no
< category
[cat_no
].number
; ++item_no
)
403 print_item (&category
[cat_no
].item_desc
[item_no
]);
408 for (item_no
= 0; item_no
< category
[cat_no
].number
; ++item_no
)
409 if (strcmp (name
, category
[cat_no
].item_desc
[item_no
].name
) == 0)
411 if (show_category_name
!= 0)
412 puts (category
[cat_no
].name
);
414 print_item (&category
[cat_no
].item_desc
[item_no
]);
422 dump_category (const char *name
)
425 size_t cat_no
, item_no
, nstrings
;
427 for (cat_no
= 0; cat_no
< NCATEGORIES
; ++cat_no
)
428 if (strcmp (name
, category
[cat_no
].name
) == 0)
431 if (cat_no
>= NCATEGORIES
)
434 /* The NAME specifies a correct locale category. */
435 if (category
[cat_no
].outfct
!= NULL
)
437 category
[cat_no
].outfct ();
441 locname
= (getenv ("LC_ALL") ?: getenv (name
) ?:
442 getenv ("LANG") ?: (char *) "POSIX");
444 /* Determine the number of strings in advance. */
446 for (item_no
= 0; item_no
< category
[cat_no
].number
; ++item_no
)
447 switch (category
[cat_no
].item_desc
[item_no
].value_type
)
455 nstrings
+= category
[cat_no
].item_desc
[item_no
].max
;
459 printf ("\nconst struct locale_data _nl_%s_%s =\n{\n"
460 " NULL, 0, /* no file mapped */\n %Zu,\n {\n",
461 locname
, name
, nstrings
);
463 for (item_no
= 0; item_no
< category
[cat_no
].number
; ++item_no
)
464 switch (category
[cat_no
].item_desc
[item_no
].value_type
)
468 const char *val
= nl_langinfo (
469 category
[cat_no
].item_desc
[item_no
].item_id
);
472 printf (" \"%s\",\n", val
);
482 for (cnt
= 0; cnt
< category
[cat_no
].item_desc
[item_no
].max
; ++cnt
)
485 category
[cat_no
].item_desc
[item_no
].item_id
+ cnt
);
488 printf (" \"%s\",\n", val
);
496 const char *val
= nl_langinfo (
497 category
[cat_no
].item_desc
[item_no
].item_id
);
500 printf (" \"\\%o\",\n",
501 *(unsigned char *) val
? : UCHAR_MAX
);
508 const char *bytes
= nl_langinfo (
509 category
[cat_no
].item_desc
[item_no
].item_id
);
513 fputs (" \"", stdout
);
516 printf ("\\%o", *(unsigned char *) bytes
++);
517 while (*bytes
!= '\0');
519 printf ("\\%o", UCHAR_MAX
);