1 /* Implementation of the locale program according to POSIX 9945-2.
2 Copyright (C) 1995-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1995.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>. */
35 #include <stdio_ext.h>
43 #include "record-status.h"
44 #include "localeinfo.h"
45 #include "charmap-dir.h"
46 #include "../locarchive.h"
47 #include <programs/xmalloc.h>
49 #define ARCHIVE_NAME COMPLOCALEDIR "/locale-archive"
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 /* Name and version of program. */
64 static void print_version (FILE *stream
, struct argp_state
*state
);
65 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
67 /* Definitions of arguments for argp functions. */
68 static const struct argp_option options
[] =
70 { NULL
, 0, NULL
, 0, N_("System information:") },
71 { "all-locales", 'a', NULL
, OPTION_NO_USAGE
,
72 N_("Write names of available locales") },
73 { "charmaps", 'm', NULL
, OPTION_NO_USAGE
,
74 N_("Write names of available charmaps") },
75 { NULL
, 0, NULL
, 0, N_("Modify output format:") },
76 { "category-name", 'c', NULL
, 0, N_("Write names of selected categories") },
77 { "keyword-name", 'k', NULL
, 0, N_("Write names of selected keywords") },
78 { "verbose", 'v', NULL
, 0, N_("Print more information") },
79 { NULL
, 0, NULL
, 0, NULL
}
82 /* Short description of program. */
83 static const char doc
[] = N_("Get locale-specific information.");
85 /* Strings for arguments in help texts. */
86 static const char args_doc
[] = N_("NAME\n[-a|-m]");
88 /* Prototype for option handler. */
89 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
91 /* Function to print some extra text in the help message. */
92 static char *more_help (int key
, const char *text
, void *input
);
94 /* Data structure to communicate with argp functions. */
95 static struct argp argp
=
97 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
101 /* We don't have these constants defined because we don't use them. Give
103 #define CTYPE_MB_CUR_MIN 0
104 #define CTYPE_MB_CUR_MAX 0
105 #define CTYPE_HASH_SIZE 0
106 #define CTYPE_HASH_LAYERS 0
107 #define CTYPE_CLASS 0
108 #define CTYPE_TOUPPER_EB 0
109 #define CTYPE_TOLOWER_EB 0
110 #define CTYPE_TOUPPER_EL 0
111 #define CTYPE_TOLOWER_EL 0
113 /* Definition of the data structure which represents a category and its
124 enum { std
, opt
} status
;
125 enum value_type value_type
;
131 /* Simple helper macro. */
132 #define NELEMS(arr) ((sizeof (arr)) / (sizeof (arr[0])))
134 /* For some tricky stuff. */
135 #define NO_PAREN(Item, More...) Item, ## More
137 /* We have all categories defined in `categories.def'. Now construct
138 the description and data structure used for all categories. */
139 #define DEFINE_ELEMENT(Item, More...) { Item, ## More },
140 #define DEFINE_CATEGORY(category, name, items, postload) \
141 static struct cat_item category##_desc[] = \
146 #include "categories.def"
147 #undef DEFINE_CATEGORY
149 static struct category category
[] =
151 #define DEFINE_CATEGORY(category, name, items, postload) \
152 [category] = { _NL_NUM_##category, name, NELEMS (category##_desc), \
154 #include "categories.def"
155 #undef DEFINE_CATEGORY
157 #define NCATEGORIES NELEMS (category)
160 /* Automatically set variable. */
161 extern const char *__progname
;
163 /* helper function for extended name handling. */
164 extern void locale_special (const char *name
, int show_category_name
,
165 int show_keyword_name
);
167 /* Prototypes for local functions. */
168 static void print_LC_IDENTIFICATION (void *mapped
, size_t size
);
169 static void print_LC_CTYPE (void *mapped
, size_t size
);
170 static void write_locales (void);
171 static int nameentcmp (const void *a
, const void *b
);
172 static int write_archive_locales (void **all_datap
, char *linebuf
);
173 static void write_charmaps (void);
174 static void show_locale_vars (void);
175 static void show_info (const char *name
);
179 main (int argc
, char *argv
[])
183 /* Set initial values for global variables. */
184 show_category_name
= 0;
185 show_keyword_name
= 0;
187 /* Set locale. Do not set LC_ALL because the other categories must
188 not be affected (according to POSIX.2). */
189 if (setlocale (LC_CTYPE
, "") == NULL
)
190 error (0, errno
, gettext ("Cannot set LC_CTYPE to default locale"));
191 if (setlocale (LC_MESSAGES
, "") == NULL
)
192 error (0, errno
, gettext ("Cannot set LC_MESSAGES to default locale"));
194 /* Initialize the message catalog. */
195 textdomain (PACKAGE
);
197 /* Parse and process arguments. */
198 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
200 /* `-a' requests the names of all available locales. */
203 if (setlocale (LC_COLLATE
, "") == NULL
)
205 gettext ("Cannot set LC_COLLATE to default locale"));
210 /* `m' requests the names of all available charmaps. The names can be
211 used for the -f argument to localedef(1). */
212 if (do_charmaps
!= 0)
218 /* Specific information about the current locale are requested.
219 Change to this locale now. */
220 if (setlocale (LC_ALL
, "") == NULL
)
221 error (0, errno
, gettext ("Cannot set LC_ALL to default locale"));
223 /* If no real argument is given we have to print the contents of the
224 current locale definition variables. These are LANG and the LC_*. */
225 if (remaining
== argc
&& show_keyword_name
== 0 && show_category_name
== 0)
231 /* Process all given names. */
232 while (remaining
< argc
)
233 show_info (argv
[remaining
++]);
239 /* Handle program arguments. */
241 parse_opt (int key
, char *arg
, struct argp_state
*state
)
249 show_category_name
= 1;
255 show_keyword_name
= 1;
261 return ARGP_ERR_UNKNOWN
;
268 more_help (int key
, const char *text
, void *input
)
273 case ARGP_KEY_HELP_EXTRA
:
274 /* We print some extra information. */
275 if (asprintf (&tp
, gettext ("\
276 For bug reporting instructions, please see:\n\
277 %s.\n"), REPORT_BUGS_TO
) < 0)
283 return (char *) text
;
287 /* Print the version information. */
289 print_version (FILE *stream
, struct argp_state
*state
)
291 fprintf (stream
, "locale %s%s\n", PKGVERSION
, VERSION
);
292 fprintf (stream
, gettext ("\
293 Copyright (C) %s Free Software Foundation, Inc.\n\
294 This is free software; see the source for copying conditions. There is NO\n\
295 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
297 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
301 /* Simple action function which prints arguments as strings. */
303 print_names (const void *nodep
, VISIT value
, int level
)
305 if (value
== postorder
|| value
== leaf
)
306 puts (*(char **) nodep
);
311 select_dirs (const struct dirent
*dirent
)
315 if (strcmp (dirent
->d_name
, ".") != 0 && strcmp (dirent
->d_name
, "..") != 0)
319 if (dirent
->d_type
!= DT_UNKNOWN
&& dirent
->d_type
!= DT_LNK
)
320 mode
= DTTOIF (dirent
->d_type
);
324 char buf
[sizeof (COMPLOCALEDIR
)
325 + strlen (dirent
->d_name
) + 1];
327 stpcpy (stpcpy (stpcpy (buf
, COMPLOCALEDIR
), "/"),
330 if (stat64 (buf
, &st
) == 0)
334 result
= S_ISDIR (mode
);
342 print_LC_IDENTIFICATION (void *mapped
, size_t size
)
344 /* Read the information from the file. */
348 unsigned int nstrings
;
349 unsigned int strindex
[0];
350 } *filedata
= mapped
;
352 if (filedata
->magic
== LIMAGIC (LC_IDENTIFICATION
)
354 + (filedata
->nstrings
355 * sizeof (unsigned int))
360 #define HANDLE(idx, name) \
361 str = ((char *) mapped \
362 + filedata->strindex[_NL_ITEM_INDEX (_NL_IDENTIFICATION_##idx)]); \
364 printf ("%9s | %s\n", name, str)
365 HANDLE (TITLE
, "title");
366 HANDLE (SOURCE
, "source");
367 HANDLE (ADDRESS
, "address");
368 HANDLE (CONTACT
, "contact");
369 HANDLE (EMAIL
, "email");
370 HANDLE (TEL
, "telephone");
372 HANDLE (LANGUAGE
, "language");
373 HANDLE (TERRITORY
, "territory");
374 HANDLE (AUDIENCE
, "audience");
375 HANDLE (APPLICATION
, "application");
376 HANDLE (ABBREVIATION
, "abbreviation");
377 HANDLE (REVISION
, "revision");
378 HANDLE (DATE
, "date");
384 print_LC_CTYPE (void *mapped
, size_t size
)
389 unsigned int nstrings
;
390 unsigned int strindex
[0];
391 } *filedata
= mapped
;
393 if (filedata
->magic
== LIMAGIC (LC_CTYPE
)
395 + (filedata
->nstrings
396 * sizeof (unsigned int))
401 str
= ((char *) mapped
402 + filedata
->strindex
[_NL_ITEM_INDEX (_NL_CTYPE_CODESET_NAME
)]);
404 printf (" codeset | %s\n", str
);
409 /* Write the names of all available locales to stdout. We have some
410 sources of the information: the contents of the locale directory
411 and the locale.alias file. To avoid duplicates and print the
412 result is a reasonable order we put all entries is a search tree
413 and print them afterwards. */
418 void *all_data
= NULL
;
419 struct dirent
**dirents
;
423 size_t alias_path_len
;
425 int first_locale
= 1;
427 #define PUT(name) tsearch (name, &all_data, \
428 (int (*) (const void *, const void *)) strcoll)
429 #define GET(name) tfind (name, &all_data, \
430 (int (*) (const void *, const void *)) strcoll)
432 /* `POSIX' locale is always available (POSIX.2 4.34.3). */
434 /* And so is the "C" locale. */
437 memset (linebuf
, '-', sizeof (linebuf
) - 1);
438 linebuf
[sizeof (linebuf
) - 1] = '\0';
440 /* First scan the locale archive. */
441 if (write_archive_locales (&all_data
, linebuf
))
444 /* Now we can look for all files in the directory. */
445 ndirents
= scandir (COMPLOCALEDIR
, &dirents
, select_dirs
,
447 for (cnt
= 0; cnt
< ndirents
; ++cnt
)
449 /* Test whether at least the LC_CTYPE data is there. Some
450 directories only contain translations. */
451 char buf
[sizeof (COMPLOCALEDIR
)
452 + strlen (dirents
[cnt
]->d_name
)
453 + sizeof "/LC_IDENTIFICATION"];
457 stpcpy (enddir
= stpcpy (stpcpy (stpcpy (buf
,
460 dirents
[cnt
]->d_name
),
461 "/LC_IDENTIFICATION");
463 if (stat64 (buf
, &st
) == 0 && S_ISREG (st
.st_mode
))
465 if (verbose
&& GET (dirents
[cnt
]->d_name
) == NULL
)
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 print_LC_IDENTIFICATION (mapped
, st
.st_size
);
488 munmap (mapped
, st
.st_size
);
493 /* Now try to get the charset information. */
494 strcpy (enddir
, "/LC_CTYPE");
495 fd
= open64 (buf
, O_RDONLY
);
496 if (fd
!= -1 && fstat64 (fd
, &st
) >= 0
497 && ((mapped
= mmap64 (NULL
, st
.st_size
, PROT_READ
,
501 print_LC_CTYPE (mapped
, st
.st_size
);
503 munmap (mapped
, st
.st_size
);
511 /* If the verbose format is not selected we simply
512 collect the names. */
513 PUT (xstrdup (dirents
[cnt
]->d_name
));
519 /* Now read the locale.alias files. */
520 if (argz_create_sep (LOCALE_ALIAS_PATH
, ':', &alias_path
, &alias_path_len
))
521 error (1, errno
, gettext ("while preparing output"));
524 while ((entry
= argz_next (alias_path
, alias_path_len
, entry
)))
526 static const char aliasfile
[] = "/locale.alias";
528 char full_name
[strlen (entry
) + sizeof aliasfile
];
530 stpcpy (stpcpy (full_name
, entry
), aliasfile
);
531 fp
= fopen (full_name
, "rm");
533 /* Ignore non-existing files. */
536 /* No threads present. */
537 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
539 while (! feof_unlocked (fp
))
541 /* It is a reasonable approach to use a fix buffer here
543 a) we are only interested in the first two fields
544 b) these fields must be usable as file names and so must
551 if (fgets_unlocked (buf
, BUFSIZ
, fp
) == NULL
)
556 /* Ignore leading white space. */
557 while (isspace (cp
[0]) && cp
[0] != '\n')
560 /* A leading '#' signals a comment line. */
561 if (cp
[0] != '\0' && cp
[0] != '#' && cp
[0] != '\n')
564 while (cp
[0] != '\0' && !isspace (cp
[0]))
566 /* Terminate alias name. */
570 /* Now look for the beginning of the value. */
571 while (isspace (cp
[0]))
577 while (cp
[0] != '\0' && !isspace (cp
[0]))
579 /* Terminate value. */
582 /* This has to be done to make the following
583 test for the end of line possible. We are
584 looking for the terminating '\n' which do not
589 else if (cp
[0] != '\0')
593 if (! verbose
&& GET (value
) != NULL
)
594 PUT (xstrdup (alias
));
598 /* Possibly not the whole line fits into the buffer.
599 Ignore the rest of the line. */
600 while (strchr (cp
, '\n') == NULL
)
603 if (fgets_unlocked (buf
, BUFSIZ
, fp
) == NULL
)
604 /* Make sure the inner loop will be left. The outer
605 loop will exit at the `feof' test. */
615 twalk (all_data
, print_names
);
623 uint32_t locrec_offset
;
628 nameentcmp (const void *a
, const void *b
)
630 return strcoll (((const struct nameent
*) a
)->name
,
631 ((const struct nameent
*) b
)->name
);
636 write_archive_locales (void **all_datap
, char *linebuf
)
639 void *all_data
= *all_datap
;
641 struct locarhead
*head
;
642 struct namehashent
*namehashtab
;
643 char *addr
= MAP_FAILED
;
647 fd
= open64 (ARCHIVE_NAME
, O_RDONLY
);
651 if (fstat64 (fd
, &st
) < 0 || st
.st_size
< sizeof (*head
))
655 addr
= mmap64 (NULL
, len
, PROT_READ
, MAP_SHARED
, fd
, 0);
656 if (addr
== MAP_FAILED
)
659 head
= (struct locarhead
*) addr
;
660 if (head
->namehash_offset
+ head
->namehash_size
> len
661 || head
->string_offset
+ head
->string_size
> len
662 || head
->locrectab_offset
+ head
->locrectab_size
> len
663 || head
->sumhash_offset
+ head
->sumhash_size
> len
)
666 namehashtab
= (struct namehashent
*) (addr
+ head
->namehash_offset
);
669 for (cnt
= 0; cnt
< head
->namehash_size
; ++cnt
)
670 if (namehashtab
[cnt
].locrec_offset
!= 0)
672 PUT (xstrdup (addr
+ namehashtab
[cnt
].name_offset
));
678 struct nameent
*names
;
681 names
= (struct nameent
*) xmalloc (head
->namehash_used
682 * sizeof (struct nameent
));
683 for (cnt
= used
= 0; cnt
< head
->namehash_size
; ++cnt
)
684 if (namehashtab
[cnt
].locrec_offset
!= 0)
686 names
[used
].name
= addr
+ namehashtab
[cnt
].name_offset
;
687 names
[used
++].locrec_offset
= namehashtab
[cnt
].locrec_offset
;
690 /* Sort the names. */
691 qsort (names
, used
, sizeof (struct nameent
), nameentcmp
);
693 for (cnt
= 0; cnt
< used
; ++cnt
)
695 struct locrecent
*locrec
;
697 PUT (xstrdup (names
[cnt
].name
));
700 putchar_unlocked ('\n');
702 printf ("locale: %-15.15s archive: " ARCHIVE_NAME
"\n%s\n",
703 names
[cnt
].name
, linebuf
);
705 locrec
= (struct locrecent
*) (addr
+ names
[cnt
].locrec_offset
);
707 print_LC_IDENTIFICATION (addr
708 + locrec
->record
[LC_IDENTIFICATION
].offset
,
709 locrec
->record
[LC_IDENTIFICATION
].len
);
711 print_LC_CTYPE (addr
+ locrec
->record
[LC_CTYPE
].offset
,
712 locrec
->record
[LC_CTYPE
].len
);
719 if (addr
!= MAP_FAILED
)
722 *all_datap
= all_data
;
727 /* Write the names of all available character maps to stdout. */
729 write_charmaps (void)
731 void *all_data
= NULL
;
735 /* Look for all files in the charmap directory. */
736 dir
= charmap_opendir (CHARMAP_PATH
);
740 while ((dirent
= charmap_readdir (dir
)) != NULL
)
745 PUT (xstrdup (dirent
));
747 aliases
= charmap_aliases (CHARMAP_PATH
, dirent
);
750 /* Add the code_set_name and the aliases. */
751 for (p
= aliases
; *p
; p
++)
754 /* Add the code_set_name only. Most aliases are obsolete. */
760 charmap_free_aliases (aliases
);
763 charmap_closedir (dir
);
765 twalk (all_data
, print_names
);
768 /* Print a properly quoted assignment of NAME with VAL, using double
769 quotes iff DQUOTE is true. */
771 print_assignment (const char *name
, const char *val
, bool dquote
)
773 printf ("%s=", name
);
779 = strcspn (val
, dquote
? "$`\"\\" : "~|&;<>()$`\\\"' \t\n");
780 printf ("%.*s", (int) segment
, val
);
792 /* We have to show the contents of the environments determining the
795 show_locale_vars (void)
797 const char *lcall
= getenv ("LC_ALL") ?: "";
798 const char *lang
= getenv ("LANG") ?: "";
800 /* LANG has to be the first value. */
801 print_assignment ("LANG", lang
, false);
803 /* Now all categories in an unspecified order. */
804 for (size_t cat_no
= 0; cat_no
< NCATEGORIES
; ++cat_no
)
805 if (cat_no
!= LC_ALL
)
807 const char *name
= category
[cat_no
].name
;
808 const char *val
= getenv (name
);
810 if (lcall
[0] != '\0' || val
== NULL
)
811 print_assignment (name
,
812 lcall
[0] != '\0' ? lcall
813 : lang
[0] != '\0' ? lang
817 print_assignment (name
, val
, false);
820 /* The last is the LC_ALL value. */
821 print_assignment ("LC_ALL", lcall
, false);
825 /* Subroutine of show_info, below. */
827 print_item (struct cat_item
*item
)
829 switch (item
->value_type
)
832 if (show_keyword_name
)
833 printf ("%s=\"", item
->name
);
834 fputs (nl_langinfo (item
->item_id
) ? : "", stdout
);
835 if (show_keyword_name
)
844 if (show_keyword_name
)
845 printf ("%s=\"", item
->name
);
847 for (cnt
= 0; cnt
< item
->max
- 1; ++cnt
)
849 val
= nl_langinfo (item
->item_id
+ cnt
);
855 val
= nl_langinfo (item
->item_id
+ cnt
);
859 if (show_keyword_name
)
867 const char *val
= nl_langinfo (item
->item_id
) ? : "";
869 if (show_keyword_name
)
870 printf ("%s=", item
->name
);
872 for (int cnt
= 0; cnt
< item
->max
&& *val
!= '\0'; ++cnt
)
874 printf ("%s%s%s%s", first
? "" : ";",
875 show_keyword_name
? "\"" : "", val
,
876 show_keyword_name
? "\"" : "");
877 val
= strchr (val
, '\0') + 1;
885 const char *val
= nl_langinfo (item
->item_id
);
887 if (show_keyword_name
)
888 printf ("%s=", item
->name
);
891 printf ("%d", *val
== '\377' ? -1 : *val
);
897 const char *val
= nl_langinfo (item
->item_id
);
898 int cnt
= val
? strlen (val
) : 0;
900 if (show_keyword_name
)
901 printf ("%s=", item
->name
);
905 printf ("%d;", *val
== '\177' ? -1 : *val
);
910 printf ("%d\n", cnt
== 0 || *val
== '\177' ? -1 : *val
);
915 union { unsigned int word
; char *string
; } val
;
916 val
.string
= nl_langinfo (item
->item_id
);
917 if (show_keyword_name
)
918 printf ("%s=", item
->name
);
920 printf ("%d\n", val
.word
);
926 union { unsigned int *wordarray
; char *string
; } val
;
928 val
.string
= nl_langinfo (item
->item_id
);
929 if (show_keyword_name
)
930 printf ("%s=", item
->name
);
932 for (int cnt
= 0; cnt
< item
->max
; ++cnt
)
934 printf ("%s%d", first
? "" : ";", val
.wordarray
[cnt
]);
943 /* We don't print wide character information since the same
944 information is available in a multibyte string. */
950 /* Show the information request for NAME. */
952 show_info (const char *name
)
954 for (size_t cat_no
= 0; cat_no
< NCATEGORIES
; ++cat_no
)
955 if (cat_no
!= LC_ALL
)
957 if (strcmp (name
, category
[cat_no
].name
) == 0)
958 /* Print the whole category. */
960 if (show_category_name
!= 0)
961 puts (category
[cat_no
].name
);
963 for (size_t item_no
= 0;
964 item_no
< category
[cat_no
].number
;
966 print_item (&category
[cat_no
].item_desc
[item_no
]);
971 for (size_t item_no
= 0; item_no
< category
[cat_no
].number
; ++item_no
)
972 if (strcmp (name
, category
[cat_no
].item_desc
[item_no
].name
) == 0)
974 if (show_category_name
!= 0)
975 puts (category
[cat_no
].name
);
977 print_item (&category
[cat_no
].item_desc
[item_no
]);
982 /* The name is not a standard one.
983 For testing and perhaps advanced use allow some more symbols. */
984 locale_special (name
, show_category_name
, show_keyword_name
);