1 /* Implementation of the locale program according to POSIX 9945-2.
2 Copyright (C) 1995-1997,1999,2000,2001,2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1995.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
37 #include <stdio_ext.h>
44 #include "localeinfo.h"
45 #include "charmap-dir.h"
47 extern void *xmalloc (size_t __n);
48 extern char *xstrdup (const char *__str);
51 /* If set print the name of the category. */
52 static int show_category_name;
54 /* If set print the name of the item. */
55 static int show_keyword_name;
57 /* Print names of all available locales. */
60 /* Print names of all available character maps. */
61 static int do_charmaps = 0;
63 /* Nonzero if verbose output is wanted. */
66 /* Name and version of program. */
67 static void print_version (FILE *stream, struct argp_state *state);
68 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
70 /* Definitions of arguments for argp functions. */
71 static const struct argp_option options[] =
73 { NULL, 0, NULL, 0, N_("System information:") },
74 { "all-locales", 'a', NULL, OPTION_NO_USAGE,
75 N_("Write names of available locales") },
76 { "charmaps", 'm', NULL, OPTION_NO_USAGE,
77 N_("Write names of available charmaps") },
78 { NULL, 0, NULL, 0, N_("Modify output format:") },
79 { "category-name", 'c', NULL, 0, N_("Write names of selected categories") },
80 { "keyword-name", 'k', NULL, 0, N_("Write names of selected keywords") },
81 { "verbose", 'v', NULL, 0, N_("Print more information") },
82 { NULL, 0, NULL, 0, NULL }
85 /* Short description of program. */
86 static const char doc[] = N_("Get locale-specific information.");
88 /* Strings for arguments in help texts. */
89 static const char args_doc[] = N_("NAME\n[-a|-m]");
91 /* Prototype for option handler. */
92 static error_t parse_opt (int key, char *arg, struct argp_state *state);
94 /* Function to print some extra text in the help message. */
95 static char *more_help (int key, const char *text, void *input);
97 /* Data structure to communicate with argp functions. */
98 static struct argp argp =
100 options, parse_opt, args_doc, doc, NULL, more_help
104 /* We don't have these constants defined because we don't use them. Give
106 #define CTYPE_MB_CUR_MIN 0
107 #define CTYPE_MB_CUR_MAX 0
108 #define CTYPE_HASH_SIZE 0
109 #define CTYPE_HASH_LAYERS 0
110 #define CTYPE_CLASS 0
111 #define CTYPE_TOUPPER_EB 0
112 #define CTYPE_TOLOWER_EB 0
113 #define CTYPE_TOUPPER_EL 0
114 #define CTYPE_TOLOWER_EL 0
116 /* Definition of the data structure which represents a category and its
127 enum { std, opt } status;
128 enum value_type value_type;
134 /* Simple helper macro. */
135 #define NELEMS(arr) ((sizeof (arr)) / (sizeof (arr[0])))
137 /* For some tricky stuff. */
138 #define NO_PAREN(Item, More...) Item, ## More
140 /* We have all categories defined in `categories.def'. Now construct
141 the description and data structure used for all categories. */
142 #define DEFINE_ELEMENT(Item, More...) { Item, ## More },
143 #define DEFINE_CATEGORY(category, name, items, postload) \
144 static struct cat_item category##_desc[] = \
149 #include "categories.def"
150 #undef DEFINE_CATEGORY
152 static struct category category[] =
154 #define DEFINE_CATEGORY(category, name, items, postload) \
155 [category] = { _NL_NUM_##category, name, NELEMS (category##_desc), \
157 #include "categories.def"
158 #undef DEFINE_CATEGORY
160 #define NCATEGORIES NELEMS (category)
163 /* Automatically set variable. */
164 extern const char *__progname;
166 /* helper function for extended name handling. */
167 extern void locale_special (const char *name, int show_category_name,
168 int show_keyword_name);
170 /* Prototypes for local functions. */
171 static void write_locales (void);
172 static void write_charmaps (void);
173 static void show_locale_vars (void);
174 static void show_info (const char *name);
178 main (int argc, char *argv[])
182 /* Set initial values for global variables. */
183 show_category_name = 0;
184 show_keyword_name = 0;
186 /* Set locale. Do not set LC_ALL because the other categories must
187 not be affected (according to POSIX.2). */
188 setlocale (LC_CTYPE, "");
189 setlocale (LC_MESSAGES, "");
191 /* Initialize the message catalog. */
192 textdomain (PACKAGE);
194 /* Parse and process arguments. */
195 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
197 /* `-a' requests the names of all available locales. */
200 setlocale (LC_COLLATE, "");
205 /* `m' requests the names of all available charmaps. The names can be
206 used for the -f argument to localedef(1). */
207 if (do_charmaps != 0)
213 /* Specific information about the current locale are requested.
214 Change to this locale now. */
215 setlocale (LC_ALL, "");
217 /* If no real argument is given we have to print the contents of the
218 current locale definition variables. These are LANG and the LC_*. */
219 if (remaining == argc && show_keyword_name == 0 && show_category_name == 0)
225 /* Process all given names. */
226 while (remaining < argc)
227 show_info (argv[remaining++]);
233 /* Handle program arguments. */
235 parse_opt (int key, char *arg, struct argp_state *state)
243 show_category_name = 1;
249 show_keyword_name = 1;
255 return ARGP_ERR_UNKNOWN;
262 more_help (int key, const char *text, void *input)
266 case ARGP_KEY_HELP_EXTRA:
267 /* We print some extra information. */
268 return xstrdup (gettext ("\
269 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
273 return (char *) text;
276 /* Print the version information. */
278 print_version (FILE *stream, struct argp_state *state)
280 fprintf (stream, "locale (GNU %s) %s\n", PACKAGE, VERSION);
281 fprintf (stream, gettext ("\
282 Copyright (C) %s Free Software Foundation, Inc.\n\
283 This is free software; see the source for copying conditions. There is NO\n\
284 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
286 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
290 /* Simple action function which prints arguments as strings. */
292 print_names (const void *nodep, VISIT value, int level)
294 if (value == postorder || value == leaf)
295 puts (*(char **) nodep);
300 select_dirs (const struct dirent *dirent)
304 if (strcmp (dirent->d_name, ".") != 0 && strcmp (dirent->d_name, "..") != 0)
308 #ifdef _DIRENT_HAVE_D_TYPE
309 if (dirent->d_type != DT_UNKNOWN && dirent->d_type != DT_LNK)
310 mode = DTTOIF (dirent->d_type);
315 char buf[sizeof (LOCALEDIR) + strlen (dirent->d_name) + 1];
317 stpcpy (stpcpy (stpcpy (buf, LOCALEDIR), "/"), dirent->d_name);
319 if (stat64 (buf, &st) == 0)
323 result = S_ISDIR (mode);
330 /* Write the names of all available locales to stdout. We have some
331 sources of the information: the contents of the locale directory
332 and the locale.alias file. To avoid duplicates and print the
333 result is a reasonable order we put all entries is a search tree
334 and print them afterwards. */
339 void *all_data = NULL;
340 struct dirent **dirents;
344 size_t alias_path_len;
346 int first_locale = 1;
348 #define PUT(name) tsearch (name, &all_data, \
349 (int (*) (const void *, const void *)) strcoll)
351 /* Now read the locale.alias files. */
352 if (argz_create_sep (LOCALE_ALIAS_PATH, ':', &alias_path, &alias_path_len))
353 error (1, errno, gettext ("while preparing output"));
356 while ((entry = argz_next (alias_path, alias_path_len, entry)))
358 static const char aliasfile[] = "/locale.alias";
360 char full_name[strlen (entry) + sizeof aliasfile];
362 stpcpy (stpcpy (full_name, entry), aliasfile);
363 fp = fopen (full_name, "r");
365 /* Ignore non-existing files. */
368 /* No threads present. */
369 __fsetlocking (fp, FSETLOCKING_BYCALLER);
371 while (! feof_unlocked (fp))
373 /* It is a reasonable approach to use a fix buffer here
375 a) we are only interested in the first two fields
376 b) these fields must be usable as file names and so must
383 if (fgets_unlocked (buf, BUFSIZ, fp) == NULL)
388 /* Ignore leading white space. */
389 while (isspace (cp[0]) && cp[0] != '\n')
392 /* A leading '#' signals a comment line. */
393 if (cp[0] != '\0' && cp[0] != '#' && cp[0] != '\n')
396 while (cp[0] != '\0' && !isspace (cp[0]))
398 /* Terminate alias name. */
402 /* Now look for the beginning of the value. */
403 while (isspace (cp[0]))
409 while (cp[0] != '\0' && !isspace (cp[0]))
411 /* Terminate value. */
414 /* This has to be done to make the following
415 test for the end of line possible. We are
416 looking for the terminating '\n' which do not
421 else if (cp[0] != '\0')
426 PUT (xstrdup (alias));
430 /* Possibly not the whole line fits into the buffer.
431 Ignore the rest of the line. */
432 while (strchr (cp, '\n') == NULL)
435 if (fgets_unlocked (buf, BUFSIZ, fp) == NULL)
436 /* Make sure the inner loop will be left. The outer
437 loop will exit at the `feof' test. */
445 memset (linebuf, '-', sizeof (linebuf) - 1);
446 linebuf[sizeof (linebuf) - 1] = '\0';
448 /* Now we can look for all files in the directory. */
449 ndirents = scandir (LOCALEDIR, &dirents, select_dirs, alphasort);
450 for (cnt = 0; cnt < ndirents; ++cnt)
452 /* Test whether at least the LC_CTYPE data is there. Some
453 directories only contain translations. */
454 char buf[sizeof (LOCALEDIR) + strlen (dirents[cnt]->d_name)
455 + sizeof "/LC_IDENTIFICATION"];
459 stpcpy (enddir = stpcpy (stpcpy (stpcpy (buf, LOCALEDIR), "/"),
460 dirents[cnt]->d_name),
461 "/LC_IDENTIFICATION");
463 if (stat64 (buf, &st) == 0 && S_ISREG (st.st_mode))
467 /* Provide some nice output of all kinds of
472 putchar_unlocked ('\n');
475 printf ("locale: %-15.15s directory: %.*s\n%s\n",
476 dirents[cnt]->d_name, (int) (enddir - buf), buf,
479 fd = open64 (buf, O_RDONLY);
482 void *mapped = mmap64 (NULL, st.st_size, PROT_READ,
484 if (mapped != MAP_FAILED)
486 /* Read the information from the file. */
490 unsigned int nstrings;
491 unsigned int strindex[0];
492 } *filedata = mapped;
494 if (filedata->magic == LIMAGIC (LC_IDENTIFICATION)
496 + (filedata->nstrings
497 * sizeof (unsigned int))
498 <= (size_t) st.st_size))
502 #define HANDLE(idx, name) \
503 str = ((char *) mapped \
504 + filedata->strindex[_NL_ITEM_INDEX (_NL_IDENTIFICATION_##idx)]); \
506 printf ("%9s | %s\n", name, str)
507 HANDLE (TITLE, "title");
508 HANDLE (SOURCE, "source");
509 HANDLE (ADDRESS, "address");
510 HANDLE (CONTACT, "contact");
511 HANDLE (EMAIL, "email");
512 HANDLE (TEL, "telephone");
514 HANDLE (LANGUAGE, "language");
515 HANDLE (TERRITORY, "territory");
516 HANDLE (AUDIENCE, "audience");
517 HANDLE (APPLICATION, "application");
518 HANDLE (ABBREVIATION, "abbreviation");
519 HANDLE (REVISION, "revision");
520 HANDLE (DATE, "date");
523 munmap (mapped, st.st_size);
528 /* Now try to get the charset information. */
529 strcpy (enddir, "/LC_CTYPE");
530 fd = open64 (buf, O_RDONLY);
531 if (fd != -1 && fstat64 (fd, &st) >= 0
532 && ((mapped = mmap64 (NULL, st.st_size, PROT_READ,
539 unsigned int nstrings;
540 unsigned int strindex[0];
541 } *filedata = mapped;
543 if (filedata->magic == LIMAGIC (LC_CTYPE)
545 + (filedata->nstrings
546 * sizeof (unsigned int))
547 <= (size_t) st.st_size))
551 str = ((char *) mapped
552 + filedata->strindex[_NL_ITEM_INDEX (_NL_CTYPE_CODESET_NAME)]);
554 printf (" codeset | %s\n", str);
557 munmap (mapped, st.st_size);
565 /* If the verbose format is not selected we simply
566 collect the names. */
567 PUT (xstrdup (dirents[cnt]->d_name));
575 /* `POSIX' locale is always available (POSIX.2 4.34.3). */
577 /* And so is the "C" locale. */
580 twalk (all_data, print_names);
585 /* Write the names of all available character maps to stdout. */
587 write_charmaps (void)
589 void *all_data = NULL;
593 /* Look for all files in the charmap directory. */
594 dir = charmap_opendir (CHARMAP_PATH);
598 while ((dirent = charmap_readdir (dir)) != NULL)
603 PUT (xstrdup (dirent));
605 aliases = charmap_aliases (CHARMAP_PATH, dirent);
608 /* Add the code_set_name and the aliases. */
609 for (p = aliases; *p; p++)
612 /* Add the code_set_name only. Most aliases are obsolete. */
618 charmap_free_aliases (aliases);
621 charmap_closedir (dir);
623 twalk (all_data, print_names);
627 /* We have to show the contents of the environments determining the
630 show_locale_vars (void)
633 const char *lcall = getenv ("LC_ALL");
634 const char *lang = getenv ("LANG") ? : "POSIX";
636 auto void get_source (const char *name);
638 void get_source (const char *name)
640 char *val = getenv (name);
642 if ((lcall ?: "")[0] != '\0' || val == NULL)
643 printf ("%s=\"%s\"\n", name, (lcall ?: "")[0] ? lcall : lang);
645 printf ("%s=%s\n", name, val);
648 /* LANG has to be the first value. */
649 printf ("LANG=%s\n", lang);
651 /* Now all categories in an unspecified order. */
652 for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
653 if (cat_no != LC_ALL)
654 get_source (category[cat_no].name);
656 /* The last is the LC_ALL value. */
657 printf ("LC_ALL=%s\n", lcall ? : "");
661 /* Show the information request for NAME. */
663 show_info (const char *name)
667 auto void print_item (struct cat_item *item);
669 void print_item (struct cat_item *item)
671 switch (item->value_type)
674 if (show_keyword_name)
675 printf ("%s=\"", item->name);
676 fputs (nl_langinfo (item->item_id) ? : "", stdout);
677 if (show_keyword_name)
686 if (show_keyword_name)
687 printf ("%s=\"", item->name);
689 for (cnt = 0; cnt < item->max - 1; ++cnt)
691 val = nl_langinfo (item->item_id + cnt);
697 val = nl_langinfo (item->item_id + cnt);
701 if (show_keyword_name)
709 const char *val = nl_langinfo (item->item_id) ? : "";
712 if (show_keyword_name)
713 printf ("%s=", item->name);
715 for (cnt = 0; cnt < item->max && *val != '\0'; ++cnt)
717 printf ("%s%s%s%s", first ? "" : ";",
718 show_keyword_name ? "\"" : "", val,
719 show_keyword_name ? "\"" : "");
720 val = strchr (val, '\0') + 1;
728 const char *val = nl_langinfo (item->item_id);
730 if (show_keyword_name)
731 printf ("%s=", item->name);
734 printf ("%d", *val == CHAR_MAX ? -1 : *val);
740 const char *val = nl_langinfo (item->item_id);
741 int cnt = val ? strlen (val) : 0;
743 if (show_keyword_name)
744 printf ("%s=", item->name);
748 printf ("%d;", *val == CHAR_MAX ? -1 : *val);
753 printf ("%d\n", cnt == 0 || *val == CHAR_MAX ? -1 : *val);
759 (unsigned int) (unsigned long int) nl_langinfo (item->item_id);
760 if (show_keyword_name)
761 printf ("%s=", item->name);
763 printf ("%d\n", val);
769 /* We don't print wide character information since the same
770 information is available in a multibyte string. */
777 for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no)
778 if (cat_no != LC_ALL)
782 if (strcmp (name, category[cat_no].name) == 0)
783 /* Print the whole category. */
785 if (show_category_name != 0)
786 puts (category[cat_no].name);
788 for (item_no = 0; item_no < category[cat_no].number; ++item_no)
789 print_item (&category[cat_no].item_desc[item_no]);
794 for (item_no = 0; item_no < category[cat_no].number; ++item_no)
795 if (strcmp (name, category[cat_no].item_desc[item_no].name) == 0)
797 if (show_category_name != 0)
798 puts (category[cat_no].name);
800 print_item (&category[cat_no].item_desc[item_no]);
805 /* The name is not a standard one.
806 For testing and perhaps advanced use allow some more symbols. */
807 locale_special (name, show_category_name, show_keyword_name);