| 1 | /* Implementation of the locale program according to POSIX 9945-2. | 
|---|
| 2 | Copyright (C) 1995-2020 Free Software Foundation, Inc. | 
|---|
| 3 | This file is part of the GNU C Library. | 
|---|
| 4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1995. | 
|---|
| 5 |  | 
|---|
| 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. | 
|---|
| 10 |  | 
|---|
| 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. | 
|---|
| 15 |  | 
|---|
| 16 | You should have received a copy of the GNU General Public License | 
|---|
| 17 | along with this program; if not, see <https://www.gnu.org/licenses/>.  */ | 
|---|
| 18 |  | 
|---|
| 19 | #ifdef HAVE_CONFIG_H | 
|---|
| 20 | # include <config.h> | 
|---|
| 21 | #endif | 
|---|
| 22 |  | 
|---|
| 23 | #include <argp.h> | 
|---|
| 24 | #include <argz.h> | 
|---|
| 25 | #include <dirent.h> | 
|---|
| 26 | #include <errno.h> | 
|---|
| 27 | #include <error.h> | 
|---|
| 28 | #include <fcntl.h> | 
|---|
| 29 | #include <langinfo.h> | 
|---|
| 30 | #include <libintl.h> | 
|---|
| 31 | #include <limits.h> | 
|---|
| 32 | #include <locale.h> | 
|---|
| 33 | #include <search.h> | 
|---|
| 34 | #include <stdio.h> | 
|---|
| 35 | #include <stdio_ext.h> | 
|---|
| 36 | #include <stdlib.h> | 
|---|
| 37 | #include <string.h> | 
|---|
| 38 | #include <unistd.h> | 
|---|
| 39 | #include <stdint.h> | 
|---|
| 40 | #include <sys/mman.h> | 
|---|
| 41 | #include <sys/stat.h> | 
|---|
| 42 |  | 
|---|
| 43 | #include "record-status.h" | 
|---|
| 44 | #include "localeinfo.h" | 
|---|
| 45 | #include "charmap-dir.h" | 
|---|
| 46 | #include "../locarchive.h" | 
|---|
| 47 | #include <programs/xmalloc.h> | 
|---|
| 48 |  | 
|---|
| 49 | #define ARCHIVE_NAME COMPLOCALEDIR "/locale-archive" | 
|---|
| 50 |  | 
|---|
| 51 | /* If set print the name of the category.  */ | 
|---|
| 52 | static int show_category_name; | 
|---|
| 53 |  | 
|---|
| 54 | /* If set print the name of the item.  */ | 
|---|
| 55 | static int show_keyword_name; | 
|---|
| 56 |  | 
|---|
| 57 | /* Print names of all available locales.  */ | 
|---|
| 58 | static int do_all; | 
|---|
| 59 |  | 
|---|
| 60 | /* Print names of all available character maps.  */ | 
|---|
| 61 | static int do_charmaps = 0; | 
|---|
| 62 |  | 
|---|
| 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; | 
|---|
| 66 |  | 
|---|
| 67 | /* Definitions of arguments for argp functions.  */ | 
|---|
| 68 | static const struct argp_option options[] = | 
|---|
| 69 | { | 
|---|
| 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 } | 
|---|
| 80 | }; | 
|---|
| 81 |  | 
|---|
| 82 | /* Short description of program.  */ | 
|---|
| 83 | static const char doc[] = N_( "Get locale-specific information."); | 
|---|
| 84 |  | 
|---|
| 85 | /* Strings for arguments in help texts.  */ | 
|---|
| 86 | static const char args_doc[] = N_( "NAME\n[-a|-m]"); | 
|---|
| 87 |  | 
|---|
| 88 | /* Prototype for option handler.  */ | 
|---|
| 89 | static error_t parse_opt (int key, char *arg, struct argp_state *state); | 
|---|
| 90 |  | 
|---|
| 91 | /* Function to print some extra text in the help message.  */ | 
|---|
| 92 | static char *more_help (int key, const char *text, void *input); | 
|---|
| 93 |  | 
|---|
| 94 | /* Data structure to communicate with argp functions.  */ | 
|---|
| 95 | static struct argp argp = | 
|---|
| 96 | { | 
|---|
| 97 | options, parse_opt, args_doc, doc, NULL, more_help | 
|---|
| 98 | }; | 
|---|
| 99 |  | 
|---|
| 100 |  | 
|---|
| 101 | /* We don't have these constants defined because we don't use them.  Give | 
|---|
| 102 | default values.  */ | 
|---|
| 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 | 
|---|
| 112 |  | 
|---|
| 113 | /* Definition of the data structure which represents a category and its | 
|---|
| 114 | items.  */ | 
|---|
| 115 | struct category | 
|---|
| 116 | { | 
|---|
| 117 | int cat_id; | 
|---|
| 118 | const char *name; | 
|---|
| 119 | size_t number; | 
|---|
| 120 | struct cat_item | 
|---|
| 121 | { | 
|---|
| 122 | int item_id; | 
|---|
| 123 | const char *name; | 
|---|
| 124 | enum { std, opt } status; | 
|---|
| 125 | enum value_type value_type; | 
|---|
| 126 | int min; | 
|---|
| 127 | int max; | 
|---|
| 128 | } *item_desc; | 
|---|
| 129 | }; | 
|---|
| 130 |  | 
|---|
| 131 | /* Simple helper macro.  */ | 
|---|
| 132 | #define NELEMS(arr) ((sizeof (arr)) / (sizeof (arr[0]))) | 
|---|
| 133 |  | 
|---|
| 134 | /* For some tricky stuff.  */ | 
|---|
| 135 | #define NO_PAREN(Item, More...) Item, ## More | 
|---|
| 136 |  | 
|---|
| 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[] =				      \ | 
|---|
| 142 | {									      \ | 
|---|
| 143 | NO_PAREN items							      \ | 
|---|
| 144 | }; | 
|---|
| 145 |  | 
|---|
| 146 | #include "categories.def" | 
|---|
| 147 | #undef DEFINE_CATEGORY | 
|---|
| 148 |  | 
|---|
| 149 | static struct category category[] = | 
|---|
| 150 | { | 
|---|
| 151 | #define DEFINE_CATEGORY(category, name, items, postload) \ | 
|---|
| 152 | [category] = { _NL_NUM_##category, name, NELEMS (category##_desc),	      \ | 
|---|
| 153 | category##_desc }, | 
|---|
| 154 | #include "categories.def" | 
|---|
| 155 | #undef DEFINE_CATEGORY | 
|---|
| 156 | }; | 
|---|
| 157 | #define NCATEGORIES NELEMS (category) | 
|---|
| 158 |  | 
|---|
| 159 |  | 
|---|
| 160 | /* Automatically set variable.  */ | 
|---|
| 161 | extern const char *__progname; | 
|---|
| 162 |  | 
|---|
| 163 | /* helper function for extended name handling.  */ | 
|---|
| 164 | extern void locale_special (const char *name, int show_category_name, | 
|---|
| 165 | int show_keyword_name); | 
|---|
| 166 |  | 
|---|
| 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); | 
|---|
| 176 | static void try_setlocale (int category, const char *category_name); | 
|---|
| 177 | static char *quote_string (const char *input); | 
|---|
| 178 | static void setlocale_diagnostics (void); | 
|---|
| 179 |  | 
|---|
| 180 |  | 
|---|
| 181 | int | 
|---|
| 182 | main (int argc, char *argv[]) | 
|---|
| 183 | { | 
|---|
| 184 | int remaining; | 
|---|
| 185 |  | 
|---|
| 186 | /* Set initial values for global variables.  */ | 
|---|
| 187 | show_category_name = 0; | 
|---|
| 188 | show_keyword_name = 0; | 
|---|
| 189 |  | 
|---|
| 190 | /* Set locale.  Do not set LC_ALL because the other categories must | 
|---|
| 191 | not be affected (according to POSIX.2).  */ | 
|---|
| 192 | try_setlocale (LC_CTYPE, "LC_CTYPE"); | 
|---|
| 193 | try_setlocale (LC_MESSAGES, "LC_MESSAGES"); | 
|---|
| 194 |  | 
|---|
| 195 | /* Initialize the message catalog.  */ | 
|---|
| 196 | textdomain (PACKAGE); | 
|---|
| 197 |  | 
|---|
| 198 | /* Parse and process arguments.  */ | 
|---|
| 199 | argp_parse (&argp, argc, argv, 0, &remaining, NULL); | 
|---|
| 200 |  | 
|---|
| 201 | /* `-a' requests the names of all available locales.  */ | 
|---|
| 202 | if (do_all != 0) | 
|---|
| 203 | { | 
|---|
| 204 | setlocale_diagnostics (); | 
|---|
| 205 | try_setlocale (LC_COLLATE, "LC_COLLATE"); | 
|---|
| 206 | write_locales (); | 
|---|
| 207 | exit (EXIT_SUCCESS); | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 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) | 
|---|
| 213 | { | 
|---|
| 214 | setlocale_diagnostics (); | 
|---|
| 215 | write_charmaps (); | 
|---|
| 216 | exit (EXIT_SUCCESS); | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | /* Specific information about the current locale are requested. | 
|---|
| 220 | Change to this locale now.  */ | 
|---|
| 221 | try_setlocale (LC_ALL, "LC_ALL"); | 
|---|
| 222 | setlocale_diagnostics (); | 
|---|
| 223 |  | 
|---|
| 224 | /* If no real argument is given we have to print the contents of the | 
|---|
| 225 | current locale definition variables.  These are LANG and the LC_*.  */ | 
|---|
| 226 | if (remaining == argc && show_keyword_name == 0 && show_category_name == 0) | 
|---|
| 227 | { | 
|---|
| 228 | show_locale_vars (); | 
|---|
| 229 | exit (EXIT_SUCCESS); | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 | /* Process all given names.  */ | 
|---|
| 233 | while (remaining <  argc) | 
|---|
| 234 | show_info (argv[remaining++]); | 
|---|
| 235 |  | 
|---|
| 236 | exit (EXIT_SUCCESS); | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 |  | 
|---|
| 240 | /* Handle program arguments.  */ | 
|---|
| 241 | static error_t | 
|---|
| 242 | parse_opt (int key, char *arg, struct argp_state *state) | 
|---|
| 243 | { | 
|---|
| 244 | switch (key) | 
|---|
| 245 | { | 
|---|
| 246 | case 'a': | 
|---|
| 247 | do_all = 1; | 
|---|
| 248 | break; | 
|---|
| 249 | case 'c': | 
|---|
| 250 | show_category_name = 1; | 
|---|
| 251 | break; | 
|---|
| 252 | case 'm': | 
|---|
| 253 | do_charmaps = 1; | 
|---|
| 254 | break; | 
|---|
| 255 | case 'k': | 
|---|
| 256 | show_keyword_name = 1; | 
|---|
| 257 | break; | 
|---|
| 258 | case 'v': | 
|---|
| 259 | verbose = 1; | 
|---|
| 260 | break; | 
|---|
| 261 | default: | 
|---|
| 262 | return ARGP_ERR_UNKNOWN; | 
|---|
| 263 | } | 
|---|
| 264 | return 0; | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 |  | 
|---|
| 268 | static char * | 
|---|
| 269 | more_help (int key, const char *text, void *input) | 
|---|
| 270 | { | 
|---|
| 271 | char *tp = NULL; | 
|---|
| 272 | switch (key) | 
|---|
| 273 | { | 
|---|
| 274 | case ARGP_KEY_HELP_EXTRA: | 
|---|
| 275 | /* We print some extra information.  */ | 
|---|
| 276 | if (asprintf (&tp, gettext ( "\ | 
|---|
| 277 | For bug reporting instructions, please see:\n\ | 
|---|
| 278 | %s.\n"), REPORT_BUGS_TO) < 0) | 
|---|
| 279 | return NULL; | 
|---|
| 280 | return tp; | 
|---|
| 281 | default: | 
|---|
| 282 | break; | 
|---|
| 283 | } | 
|---|
| 284 | return (char *) text; | 
|---|
| 285 | } | 
|---|
| 286 |  | 
|---|
| 287 |  | 
|---|
| 288 | /* Print the version information.  */ | 
|---|
| 289 | static void | 
|---|
| 290 | print_version (FILE *stream, struct argp_state *state) | 
|---|
| 291 | { | 
|---|
| 292 | fprintf (stream, "locale %s%s\n", PKGVERSION, VERSION); | 
|---|
| 293 | fprintf (stream, gettext ( "\ | 
|---|
| 294 | Copyright (C) %s Free Software Foundation, Inc.\n\ | 
|---|
| 295 | This is free software; see the source for copying conditions.  There is NO\n\ | 
|---|
| 296 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ | 
|---|
| 297 | "), "2020"); | 
|---|
| 298 | fprintf (stream, gettext ( "Written by %s.\n"), "Ulrich Drepper"); | 
|---|
| 299 | } | 
|---|
| 300 |  | 
|---|
| 301 |  | 
|---|
| 302 | /* Simple action function which prints arguments as strings.  */ | 
|---|
| 303 | static void | 
|---|
| 304 | print_names (const void *nodep, VISIT value, int level) | 
|---|
| 305 | { | 
|---|
| 306 | if (value == postorder || value == leaf) | 
|---|
| 307 | puts (*(char **) nodep); | 
|---|
| 308 | } | 
|---|
| 309 |  | 
|---|
| 310 |  | 
|---|
| 311 | static int | 
|---|
| 312 | select_dirs (const struct dirent *dirent) | 
|---|
| 313 | { | 
|---|
| 314 | int result = 0; | 
|---|
| 315 |  | 
|---|
| 316 | if (strcmp (dirent->d_name, ".") != 0 && strcmp (dirent->d_name, "..") != 0) | 
|---|
| 317 | { | 
|---|
| 318 | mode_t mode = 0; | 
|---|
| 319 |  | 
|---|
| 320 | if (dirent->d_type != DT_UNKNOWN && dirent->d_type != DT_LNK) | 
|---|
| 321 | mode = DTTOIF (dirent->d_type); | 
|---|
| 322 | else | 
|---|
| 323 | { | 
|---|
| 324 | struct stat64 st; | 
|---|
| 325 | char buf[sizeof (COMPLOCALEDIR) | 
|---|
| 326 | + strlen (dirent->d_name) + 1]; | 
|---|
| 327 |  | 
|---|
| 328 | stpcpy (stpcpy (stpcpy (buf, COMPLOCALEDIR), "/"), | 
|---|
| 329 | dirent->d_name); | 
|---|
| 330 |  | 
|---|
| 331 | if (stat64 (buf, &st) == 0) | 
|---|
| 332 | mode = st.st_mode; | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | result = S_ISDIR (mode); | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | return result; | 
|---|
| 339 | } | 
|---|
| 340 |  | 
|---|
| 341 |  | 
|---|
| 342 | static void | 
|---|
| 343 | print_LC_IDENTIFICATION (void *mapped, size_t size) | 
|---|
| 344 | { | 
|---|
| 345 | /* Read the information from the file.  */ | 
|---|
| 346 | struct | 
|---|
| 347 | { | 
|---|
| 348 | unsigned int magic; | 
|---|
| 349 | unsigned int nstrings; | 
|---|
| 350 | unsigned int strindex[0]; | 
|---|
| 351 | } *filedata = mapped; | 
|---|
| 352 |  | 
|---|
| 353 | if (filedata->magic == LIMAGIC (LC_IDENTIFICATION) | 
|---|
| 354 | && (sizeof *filedata | 
|---|
| 355 | + (filedata->nstrings | 
|---|
| 356 | * sizeof (unsigned int)) | 
|---|
| 357 | <= size)) | 
|---|
| 358 | { | 
|---|
| 359 | const char *str; | 
|---|
| 360 |  | 
|---|
| 361 | #define HANDLE(idx, name) \ | 
|---|
| 362 | str = ((char *) mapped						      \ | 
|---|
| 363 | + filedata->strindex[_NL_ITEM_INDEX (_NL_IDENTIFICATION_##idx)]);    \ | 
|---|
| 364 | if (*str != '\0')							      \ | 
|---|
| 365 | printf ("%9s | %s\n", name, str) | 
|---|
| 366 | HANDLE (TITLE, "title"); | 
|---|
| 367 | HANDLE (SOURCE, "source"); | 
|---|
| 368 | HANDLE (ADDRESS, "address"); | 
|---|
| 369 | HANDLE (CONTACT, "contact"); | 
|---|
| 370 | HANDLE (EMAIL, "email"); | 
|---|
| 371 | HANDLE (TEL, "telephone"); | 
|---|
| 372 | HANDLE (FAX, "fax"); | 
|---|
| 373 | HANDLE (LANGUAGE, "language"); | 
|---|
| 374 | HANDLE (TERRITORY, "territory"); | 
|---|
| 375 | HANDLE (AUDIENCE, "audience"); | 
|---|
| 376 | HANDLE (APPLICATION, "application"); | 
|---|
| 377 | HANDLE (ABBREVIATION, "abbreviation"); | 
|---|
| 378 | HANDLE (REVISION, "revision"); | 
|---|
| 379 | HANDLE (DATE, "date"); | 
|---|
| 380 | } | 
|---|
| 381 | } | 
|---|
| 382 |  | 
|---|
| 383 |  | 
|---|
| 384 | static void | 
|---|
| 385 | print_LC_CTYPE (void *mapped, size_t size) | 
|---|
| 386 | { | 
|---|
| 387 | struct | 
|---|
| 388 | { | 
|---|
| 389 | unsigned int magic; | 
|---|
| 390 | unsigned int nstrings; | 
|---|
| 391 | unsigned int strindex[0]; | 
|---|
| 392 | } *filedata = mapped; | 
|---|
| 393 |  | 
|---|
| 394 | if (filedata->magic == LIMAGIC (LC_CTYPE) | 
|---|
| 395 | && (sizeof *filedata | 
|---|
| 396 | + (filedata->nstrings | 
|---|
| 397 | * sizeof (unsigned int)) | 
|---|
| 398 | <= size)) | 
|---|
| 399 | { | 
|---|
| 400 | const char *str; | 
|---|
| 401 |  | 
|---|
| 402 | str = ((char *) mapped | 
|---|
| 403 | + filedata->strindex[_NL_ITEM_INDEX (_NL_CTYPE_CODESET_NAME)]); | 
|---|
| 404 | if (*str != '\0') | 
|---|
| 405 | printf ( "  codeset | %s\n", str); | 
|---|
| 406 | } | 
|---|
| 407 | } | 
|---|
| 408 |  | 
|---|
| 409 |  | 
|---|
| 410 | /* Write the names of all available locales to stdout.  We have some | 
|---|
| 411 | sources of the information: the contents of the locale directory | 
|---|
| 412 | and the locale.alias file.  To avoid duplicates and print the | 
|---|
| 413 | result is a reasonable order we put all entries is a search tree | 
|---|
| 414 | and print them afterwards.  */ | 
|---|
| 415 | static void | 
|---|
| 416 | write_locales (void) | 
|---|
| 417 | { | 
|---|
| 418 | char linebuf[80]; | 
|---|
| 419 | void *all_data = NULL; | 
|---|
| 420 | struct dirent **dirents; | 
|---|
| 421 | int ndirents; | 
|---|
| 422 | int cnt; | 
|---|
| 423 | char *alias_path; | 
|---|
| 424 | size_t alias_path_len; | 
|---|
| 425 | char *entry; | 
|---|
| 426 | int first_locale = 1; | 
|---|
| 427 |  | 
|---|
| 428 | #define PUT(name) tsearch (name, &all_data, \ | 
|---|
| 429 | (int (*) (const void *, const void *)) strcoll) | 
|---|
| 430 | #define GET(name) tfind (name, &all_data, \ | 
|---|
| 431 | (int (*) (const void *, const void *)) strcoll) | 
|---|
| 432 |  | 
|---|
| 433 | /* `POSIX' locale is always available (POSIX.2 4.34.3).  */ | 
|---|
| 434 | PUT ( "POSIX"); | 
|---|
| 435 | /* And so is the "C" locale.  */ | 
|---|
| 436 | PUT ( "C"); | 
|---|
| 437 |  | 
|---|
| 438 | memset (linebuf, '-', sizeof (linebuf) - 1); | 
|---|
| 439 | linebuf[sizeof (linebuf) - 1] = '\0'; | 
|---|
| 440 |  | 
|---|
| 441 | /* First scan the locale archive.  */ | 
|---|
| 442 | if (write_archive_locales (&all_data, linebuf)) | 
|---|
| 443 | first_locale = 0; | 
|---|
| 444 |  | 
|---|
| 445 | /* Now we can look for all files in the directory.  */ | 
|---|
| 446 | ndirents = scandir (COMPLOCALEDIR, &dirents, select_dirs, | 
|---|
| 447 | alphasort); | 
|---|
| 448 | for (cnt = 0; cnt < ndirents; ++cnt) | 
|---|
| 449 | { | 
|---|
| 450 | /* Test whether at least the LC_CTYPE data is there.  Some | 
|---|
| 451 | directories only contain translations.  */ | 
|---|
| 452 | char buf[sizeof (COMPLOCALEDIR) | 
|---|
| 453 | + strlen (dirents[cnt]->d_name) | 
|---|
| 454 | + sizeof "/LC_IDENTIFICATION"]; | 
|---|
| 455 | char *enddir; | 
|---|
| 456 | struct stat64 st; | 
|---|
| 457 |  | 
|---|
| 458 | stpcpy (enddir = stpcpy (stpcpy (stpcpy (buf, | 
|---|
| 459 | COMPLOCALEDIR), | 
|---|
| 460 | "/"), | 
|---|
| 461 | dirents[cnt]->d_name), | 
|---|
| 462 | "/LC_IDENTIFICATION"); | 
|---|
| 463 |  | 
|---|
| 464 | if (stat64 (buf, &st) == 0 && S_ISREG (st.st_mode)) | 
|---|
| 465 | { | 
|---|
| 466 | if (verbose && GET (dirents[cnt]->d_name) == NULL) | 
|---|
| 467 | { | 
|---|
| 468 | /* Provide some nice output of all kinds of | 
|---|
| 469 | information.  */ | 
|---|
| 470 | int fd; | 
|---|
| 471 |  | 
|---|
| 472 | if (! first_locale) | 
|---|
| 473 | putchar_unlocked ('\n'); | 
|---|
| 474 | first_locale = 0; | 
|---|
| 475 |  | 
|---|
| 476 | printf ( "locale: %-15.15s directory: %.*s\n%s\n", | 
|---|
| 477 | dirents[cnt]->d_name, (int) (enddir - buf), buf, | 
|---|
| 478 | linebuf); | 
|---|
| 479 |  | 
|---|
| 480 | fd = open64 (buf, O_RDONLY); | 
|---|
| 481 | if (fd != -1) | 
|---|
| 482 | { | 
|---|
| 483 | void *mapped = mmap64 (NULL, st.st_size, PROT_READ, | 
|---|
| 484 | MAP_SHARED, fd, 0); | 
|---|
| 485 | if (mapped != MAP_FAILED) | 
|---|
| 486 | { | 
|---|
| 487 | print_LC_IDENTIFICATION (mapped, st.st_size); | 
|---|
| 488 |  | 
|---|
| 489 | munmap (mapped, st.st_size); | 
|---|
| 490 | } | 
|---|
| 491 |  | 
|---|
| 492 | close (fd); | 
|---|
| 493 |  | 
|---|
| 494 | /* Now try to get the charset information.  */ | 
|---|
| 495 | strcpy (enddir, "/LC_CTYPE"); | 
|---|
| 496 | fd = open64 (buf, O_RDONLY); | 
|---|
| 497 | if (fd != -1 && fstat64 (fd, &st) >= 0 | 
|---|
| 498 | && ((mapped = mmap64 (NULL, st.st_size, PROT_READ, | 
|---|
| 499 | MAP_SHARED, fd, 0)) | 
|---|
| 500 | != MAP_FAILED)) | 
|---|
| 501 | { | 
|---|
| 502 | print_LC_CTYPE (mapped, st.st_size); | 
|---|
| 503 |  | 
|---|
| 504 | munmap (mapped, st.st_size); | 
|---|
| 505 | } | 
|---|
| 506 |  | 
|---|
| 507 | if (fd != -1) | 
|---|
| 508 | close (fd); | 
|---|
| 509 | } | 
|---|
| 510 | } | 
|---|
| 511 |  | 
|---|
| 512 | /* If the verbose format is not selected we simply | 
|---|
| 513 | collect the names.  */ | 
|---|
| 514 | PUT (xstrdup (dirents[cnt]->d_name)); | 
|---|
| 515 | } | 
|---|
| 516 | } | 
|---|
| 517 | if (ndirents > 0) | 
|---|
| 518 | free (dirents); | 
|---|
| 519 |  | 
|---|
| 520 | /* Now read the locale.alias files.  */ | 
|---|
| 521 | if (argz_create_sep (LOCALE_ALIAS_PATH, ':', &alias_path, &alias_path_len)) | 
|---|
| 522 | error (1, errno, gettext ( "while preparing output")); | 
|---|
| 523 |  | 
|---|
| 524 | entry = NULL; | 
|---|
| 525 | while ((entry = argz_next (alias_path, alias_path_len, entry))) | 
|---|
| 526 | { | 
|---|
| 527 | static const char aliasfile[] = "/locale.alias"; | 
|---|
| 528 | FILE *fp; | 
|---|
| 529 | char full_name[strlen (entry) + sizeof aliasfile]; | 
|---|
| 530 |  | 
|---|
| 531 | stpcpy (stpcpy (full_name, entry), aliasfile); | 
|---|
| 532 | fp = fopen (full_name, "rm"); | 
|---|
| 533 | if (fp == NULL) | 
|---|
| 534 | /* Ignore non-existing files.  */ | 
|---|
| 535 | continue; | 
|---|
| 536 |  | 
|---|
| 537 | /* No threads present.  */ | 
|---|
| 538 | __fsetlocking (fp, FSETLOCKING_BYCALLER); | 
|---|
| 539 |  | 
|---|
| 540 | while (! feof_unlocked (fp)) | 
|---|
| 541 | { | 
|---|
| 542 | /* It is a reasonable approach to use a fix buffer here | 
|---|
| 543 | because | 
|---|
| 544 | a) we are only interested in the first two fields | 
|---|
| 545 | b) these fields must be usable as file names and so must | 
|---|
| 546 | not be that long  */ | 
|---|
| 547 | char buf[BUFSIZ]; | 
|---|
| 548 | char *alias; | 
|---|
| 549 | char *value; | 
|---|
| 550 | char *cp; | 
|---|
| 551 |  | 
|---|
| 552 | if (fgets_unlocked (buf, BUFSIZ, fp) == NULL) | 
|---|
| 553 | /* EOF reached.  */ | 
|---|
| 554 | break; | 
|---|
| 555 |  | 
|---|
| 556 | cp = buf; | 
|---|
| 557 | /* Ignore leading white space.  */ | 
|---|
| 558 | while (isspace (cp[0]) && cp[0] != '\n') | 
|---|
| 559 | ++cp; | 
|---|
| 560 |  | 
|---|
| 561 | /* A leading '#' signals a comment line.  */ | 
|---|
| 562 | if (cp[0] != '\0' && cp[0] != '#' && cp[0] != '\n') | 
|---|
| 563 | { | 
|---|
| 564 | alias = cp++; | 
|---|
| 565 | while (cp[0] != '\0' && !isspace (cp[0])) | 
|---|
| 566 | ++cp; | 
|---|
| 567 | /* Terminate alias name.  */ | 
|---|
| 568 | if (cp[0] != '\0') | 
|---|
| 569 | *cp++ = '\0'; | 
|---|
| 570 |  | 
|---|
| 571 | /* Now look for the beginning of the value.  */ | 
|---|
| 572 | while (isspace (cp[0])) | 
|---|
| 573 | ++cp; | 
|---|
| 574 |  | 
|---|
| 575 | if (cp[0] != '\0') | 
|---|
| 576 | { | 
|---|
| 577 | value = cp++; | 
|---|
| 578 | while (cp[0] != '\0' && !isspace (cp[0])) | 
|---|
| 579 | ++cp; | 
|---|
| 580 | /* Terminate value.  */ | 
|---|
| 581 | if (cp[0] == '\n') | 
|---|
| 582 | { | 
|---|
| 583 | /* This has to be done to make the following | 
|---|
| 584 | test for the end of line possible.  We are | 
|---|
| 585 | looking for the terminating '\n' which do not | 
|---|
| 586 | overwrite here.  */ | 
|---|
| 587 | *cp++ = '\0'; | 
|---|
| 588 | *cp = '\n'; | 
|---|
| 589 | } | 
|---|
| 590 | else if (cp[0] != '\0') | 
|---|
| 591 | *cp++ = '\0'; | 
|---|
| 592 |  | 
|---|
| 593 | /* Add the alias.  */ | 
|---|
| 594 | if (! verbose && GET (value) != NULL) | 
|---|
| 595 | PUT (xstrdup (alias)); | 
|---|
| 596 | } | 
|---|
| 597 | } | 
|---|
| 598 |  | 
|---|
| 599 | /* Possibly not the whole line fits into the buffer. | 
|---|
| 600 | Ignore the rest of the line.  */ | 
|---|
| 601 | while (strchr (cp, '\n') == NULL) | 
|---|
| 602 | { | 
|---|
| 603 | cp = buf; | 
|---|
| 604 | if (fgets_unlocked (buf, BUFSIZ, fp) == NULL) | 
|---|
| 605 | /* Make sure the inner loop will be left.  The outer | 
|---|
| 606 | loop will exit at the `feof' test.  */ | 
|---|
| 607 | *cp = '\n'; | 
|---|
| 608 | } | 
|---|
| 609 | } | 
|---|
| 610 |  | 
|---|
| 611 | fclose (fp); | 
|---|
| 612 | } | 
|---|
| 613 |  | 
|---|
| 614 | if (! verbose) | 
|---|
| 615 | { | 
|---|
| 616 | twalk (all_data, print_names); | 
|---|
| 617 | } | 
|---|
| 618 | } | 
|---|
| 619 |  | 
|---|
| 620 |  | 
|---|
| 621 | struct nameent | 
|---|
| 622 | { | 
|---|
| 623 | char *name; | 
|---|
| 624 | uint32_t locrec_offset; | 
|---|
| 625 | }; | 
|---|
| 626 |  | 
|---|
| 627 |  | 
|---|
| 628 | static int | 
|---|
| 629 | nameentcmp (const void *a, const void *b) | 
|---|
| 630 | { | 
|---|
| 631 | return strcoll (((const struct nameent *) a)->name, | 
|---|
| 632 | ((const struct nameent *) b)->name); | 
|---|
| 633 | } | 
|---|
| 634 |  | 
|---|
| 635 |  | 
|---|
| 636 | static int | 
|---|
| 637 | write_archive_locales (void **all_datap, char *linebuf) | 
|---|
| 638 | { | 
|---|
| 639 | struct stat64 st; | 
|---|
| 640 | void *all_data = *all_datap; | 
|---|
| 641 | size_t len = 0; | 
|---|
| 642 | struct locarhead *head; | 
|---|
| 643 | struct namehashent *namehashtab; | 
|---|
| 644 | char *addr = MAP_FAILED; | 
|---|
| 645 | int fd, ret = 0; | 
|---|
| 646 | uint32_t cnt; | 
|---|
| 647 |  | 
|---|
| 648 | fd = open64 (ARCHIVE_NAME, O_RDONLY); | 
|---|
| 649 | if (fd < 0) | 
|---|
| 650 | return 0; | 
|---|
| 651 |  | 
|---|
| 652 | if (fstat64 (fd, &st) < 0 || st.st_size < sizeof (*head)) | 
|---|
| 653 | goto error_out; | 
|---|
| 654 |  | 
|---|
| 655 | len = st.st_size; | 
|---|
| 656 | addr = mmap64 (NULL, len, PROT_READ, MAP_SHARED, fd, 0); | 
|---|
| 657 | if (addr == MAP_FAILED) | 
|---|
| 658 | goto error_out; | 
|---|
| 659 |  | 
|---|
| 660 | head = (struct locarhead *) addr; | 
|---|
| 661 | if (head->namehash_offset + head->namehash_size > len | 
|---|
| 662 | || head->string_offset + head->string_size > len | 
|---|
| 663 | || head->locrectab_offset + head->locrectab_size > len | 
|---|
| 664 | || head->sumhash_offset + head->sumhash_size > len) | 
|---|
| 665 | goto error_out; | 
|---|
| 666 |  | 
|---|
| 667 | namehashtab = (struct namehashent *) (addr + head->namehash_offset); | 
|---|
| 668 | if (! verbose) | 
|---|
| 669 | { | 
|---|
| 670 | for (cnt = 0; cnt < head->namehash_size; ++cnt) | 
|---|
| 671 | if (namehashtab[cnt].locrec_offset != 0) | 
|---|
| 672 | { | 
|---|
| 673 | PUT (xstrdup (addr + namehashtab[cnt].name_offset)); | 
|---|
| 674 | ++ret; | 
|---|
| 675 | } | 
|---|
| 676 | } | 
|---|
| 677 | else | 
|---|
| 678 | { | 
|---|
| 679 | struct nameent *names; | 
|---|
| 680 | uint32_t used; | 
|---|
| 681 |  | 
|---|
| 682 | names = (struct nameent *) xmalloc (head->namehash_used | 
|---|
| 683 | * sizeof (struct nameent)); | 
|---|
| 684 | for (cnt = used = 0; cnt < head->namehash_size; ++cnt) | 
|---|
| 685 | if (namehashtab[cnt].locrec_offset != 0) | 
|---|
| 686 | { | 
|---|
| 687 | names[used].name = addr + namehashtab[cnt].name_offset; | 
|---|
| 688 | names[used++].locrec_offset = namehashtab[cnt].locrec_offset; | 
|---|
| 689 | } | 
|---|
| 690 |  | 
|---|
| 691 | /* Sort the names.  */ | 
|---|
| 692 | qsort (names, used, sizeof (struct nameent), nameentcmp); | 
|---|
| 693 |  | 
|---|
| 694 | for (cnt = 0; cnt < used; ++cnt) | 
|---|
| 695 | { | 
|---|
| 696 | struct locrecent *locrec; | 
|---|
| 697 |  | 
|---|
| 698 | PUT (xstrdup (names[cnt].name)); | 
|---|
| 699 |  | 
|---|
| 700 | if (cnt) | 
|---|
| 701 | putchar_unlocked ('\n'); | 
|---|
| 702 |  | 
|---|
| 703 | printf ( "locale: %-15.15s archive: "ARCHIVE_NAME "\n%s\n", | 
|---|
| 704 | names[cnt].name, linebuf); | 
|---|
| 705 |  | 
|---|
| 706 | locrec = (struct locrecent *) (addr + names[cnt].locrec_offset); | 
|---|
| 707 |  | 
|---|
| 708 | print_LC_IDENTIFICATION (addr | 
|---|
| 709 | + locrec->record[LC_IDENTIFICATION].offset, | 
|---|
| 710 | locrec->record[LC_IDENTIFICATION].len); | 
|---|
| 711 |  | 
|---|
| 712 | print_LC_CTYPE (addr + locrec->record[LC_CTYPE].offset, | 
|---|
| 713 | locrec->record[LC_CTYPE].len); | 
|---|
| 714 | } | 
|---|
| 715 |  | 
|---|
| 716 | ret = used; | 
|---|
| 717 | } | 
|---|
| 718 |  | 
|---|
| 719 | error_out: | 
|---|
| 720 | if (addr != MAP_FAILED) | 
|---|
| 721 | munmap (addr, len); | 
|---|
| 722 | close (fd); | 
|---|
| 723 | *all_datap = all_data; | 
|---|
| 724 | return ret; | 
|---|
| 725 | } | 
|---|
| 726 |  | 
|---|
| 727 |  | 
|---|
| 728 | /* Write the names of all available character maps to stdout.  */ | 
|---|
| 729 | static void | 
|---|
| 730 | write_charmaps (void) | 
|---|
| 731 | { | 
|---|
| 732 | void *all_data = NULL; | 
|---|
| 733 | CHARMAP_DIR *dir; | 
|---|
| 734 | const char *dirent; | 
|---|
| 735 |  | 
|---|
| 736 | /* Look for all files in the charmap directory.  */ | 
|---|
| 737 | dir = charmap_opendir (CHARMAP_PATH); | 
|---|
| 738 | if (dir == NULL) | 
|---|
| 739 | return; | 
|---|
| 740 |  | 
|---|
| 741 | while ((dirent = charmap_readdir (dir)) != NULL) | 
|---|
| 742 | { | 
|---|
| 743 | char **aliases; | 
|---|
| 744 | char **p; | 
|---|
| 745 |  | 
|---|
| 746 | PUT (xstrdup (dirent)); | 
|---|
| 747 |  | 
|---|
| 748 | aliases = charmap_aliases (CHARMAP_PATH, dirent); | 
|---|
| 749 |  | 
|---|
| 750 | #if 0 | 
|---|
| 751 | /* Add the code_set_name and the aliases.  */ | 
|---|
| 752 | for (p = aliases; *p; p++) | 
|---|
| 753 | PUT (xstrdup (*p)); | 
|---|
| 754 | #else | 
|---|
| 755 | /* Add the code_set_name only.  Most aliases are obsolete.  */ | 
|---|
| 756 | p = aliases; | 
|---|
| 757 | if (*p) | 
|---|
| 758 | PUT (xstrdup (*p)); | 
|---|
| 759 | #endif | 
|---|
| 760 |  | 
|---|
| 761 | charmap_free_aliases (aliases); | 
|---|
| 762 | } | 
|---|
| 763 |  | 
|---|
| 764 | charmap_closedir (dir); | 
|---|
| 765 |  | 
|---|
| 766 | twalk (all_data, print_names); | 
|---|
| 767 | } | 
|---|
| 768 |  | 
|---|
| 769 | /* Print a properly quoted assignment of NAME with VAL, using double | 
|---|
| 770 | quotes iff DQUOTE is true.  */ | 
|---|
| 771 | static void | 
|---|
| 772 | print_assignment (const char *name, const char *val, bool dquote) | 
|---|
| 773 | { | 
|---|
| 774 | printf ( "%s=", name); | 
|---|
| 775 | if (dquote) | 
|---|
| 776 | putchar ('"'); | 
|---|
| 777 | while (*val != '\0') | 
|---|
| 778 | { | 
|---|
| 779 | size_t segment | 
|---|
| 780 | = strcspn (val, dquote ? "$`\"\\": "~|&;<>()$`\\\"' \t\n"); | 
|---|
| 781 | printf ( "%.*s", (int) segment, val); | 
|---|
| 782 | val += segment; | 
|---|
| 783 | if (*val == '\0') | 
|---|
| 784 | break; | 
|---|
| 785 | putchar ('\\'); | 
|---|
| 786 | putchar (*val++); | 
|---|
| 787 | } | 
|---|
| 788 | if (dquote) | 
|---|
| 789 | putchar ('"'); | 
|---|
| 790 | putchar ('\n'); | 
|---|
| 791 | } | 
|---|
| 792 |  | 
|---|
| 793 | /* We have to show the contents of the environments determining the | 
|---|
| 794 | locale.  */ | 
|---|
| 795 | static void | 
|---|
| 796 | show_locale_vars (void) | 
|---|
| 797 | { | 
|---|
| 798 | const char *lcall = getenv ( "LC_ALL") ?: ""; | 
|---|
| 799 | const char *lang = getenv ( "LANG") ?: ""; | 
|---|
| 800 |  | 
|---|
| 801 | /* LANG has to be the first value.  */ | 
|---|
| 802 | print_assignment ( "LANG", lang, false); | 
|---|
| 803 |  | 
|---|
| 804 | /* Now all categories in an unspecified order.  */ | 
|---|
| 805 | for (size_t cat_no = 0; cat_no < NCATEGORIES; ++cat_no) | 
|---|
| 806 | if (cat_no != LC_ALL) | 
|---|
| 807 | { | 
|---|
| 808 | const char *name = category[cat_no].name; | 
|---|
| 809 | const char *val = getenv (name); | 
|---|
| 810 |  | 
|---|
| 811 | if (lcall[0] != '\0' || val == NULL) | 
|---|
| 812 | print_assignment (name, | 
|---|
| 813 | lcall[0] != '\0' ? lcall | 
|---|
| 814 | : lang[0] != '\0' ? lang | 
|---|
| 815 | : "POSIX", | 
|---|
| 816 | true); | 
|---|
| 817 | else | 
|---|
| 818 | print_assignment (name, val, false); | 
|---|
| 819 | } | 
|---|
| 820 |  | 
|---|
| 821 | /* The last is the LC_ALL value.  */ | 
|---|
| 822 | print_assignment ( "LC_ALL", lcall, false); | 
|---|
| 823 | } | 
|---|
| 824 |  | 
|---|
| 825 |  | 
|---|
| 826 | /* Subroutine of show_info, below.  */ | 
|---|
| 827 | static void | 
|---|
| 828 | print_item (struct cat_item *item) | 
|---|
| 829 | { | 
|---|
| 830 | switch (item->value_type) | 
|---|
| 831 | { | 
|---|
| 832 | case string: | 
|---|
| 833 | if (show_keyword_name) | 
|---|
| 834 | printf ( "%s=\"", item->name); | 
|---|
| 835 | fputs (nl_langinfo (item->item_id) ? : "", stdout); | 
|---|
| 836 | if (show_keyword_name) | 
|---|
| 837 | putchar ('"'); | 
|---|
| 838 | putchar ('\n'); | 
|---|
| 839 | break; | 
|---|
| 840 | case stringarray: | 
|---|
| 841 | { | 
|---|
| 842 | const char *val; | 
|---|
| 843 | int cnt; | 
|---|
| 844 |  | 
|---|
| 845 | if (show_keyword_name) | 
|---|
| 846 | printf ( "%s=\"", item->name); | 
|---|
| 847 |  | 
|---|
| 848 | for (cnt = 0; cnt < item->max - 1; ++cnt) | 
|---|
| 849 | { | 
|---|
| 850 | val = nl_langinfo (item->item_id + cnt); | 
|---|
| 851 | if (val != NULL) | 
|---|
| 852 | fputs (val, stdout); | 
|---|
| 853 | putchar (';'); | 
|---|
| 854 | } | 
|---|
| 855 |  | 
|---|
| 856 | val = nl_langinfo (item->item_id + cnt); | 
|---|
| 857 | if (val != NULL) | 
|---|
| 858 | fputs (val, stdout); | 
|---|
| 859 |  | 
|---|
| 860 | if (show_keyword_name) | 
|---|
| 861 | putchar ('"'); | 
|---|
| 862 | putchar ('\n'); | 
|---|
| 863 | } | 
|---|
| 864 | break; | 
|---|
| 865 | case stringlist: | 
|---|
| 866 | { | 
|---|
| 867 | int first = 1; | 
|---|
| 868 | const char *val = nl_langinfo (item->item_id) ? : ""; | 
|---|
| 869 |  | 
|---|
| 870 | if (show_keyword_name) | 
|---|
| 871 | printf ( "%s=", item->name); | 
|---|
| 872 |  | 
|---|
| 873 | for (int cnt = 0; cnt < item->max && *val != '\0'; ++cnt) | 
|---|
| 874 | { | 
|---|
| 875 | printf ( "%s%s%s%s", first ? "": ";", | 
|---|
| 876 | show_keyword_name ? "\"": "", val, | 
|---|
| 877 | show_keyword_name ? "\"": ""); | 
|---|
| 878 | val = strchr (val, '\0') + 1; | 
|---|
| 879 | first = 0; | 
|---|
| 880 | } | 
|---|
| 881 | putchar ('\n'); | 
|---|
| 882 | } | 
|---|
| 883 | break; | 
|---|
| 884 | case byte: | 
|---|
| 885 | { | 
|---|
| 886 | const char *val = nl_langinfo (item->item_id); | 
|---|
| 887 |  | 
|---|
| 888 | if (show_keyword_name) | 
|---|
| 889 | printf ( "%s=", item->name); | 
|---|
| 890 |  | 
|---|
| 891 | if (val != NULL) | 
|---|
| 892 | printf ( "%d", *val == '\377' ? -1 : *val); | 
|---|
| 893 | putchar ('\n'); | 
|---|
| 894 | } | 
|---|
| 895 | break; | 
|---|
| 896 | case bytearray: | 
|---|
| 897 | { | 
|---|
| 898 | const char *val = nl_langinfo (item->item_id); | 
|---|
| 899 | int cnt = val ? strlen (val) : 0; | 
|---|
| 900 |  | 
|---|
| 901 | if (show_keyword_name) | 
|---|
| 902 | printf ( "%s=", item->name); | 
|---|
| 903 |  | 
|---|
| 904 | while (cnt > 1) | 
|---|
| 905 | { | 
|---|
| 906 | printf ( "%d;", *val == '\177' ? -1 : *val); | 
|---|
| 907 | --cnt; | 
|---|
| 908 | ++val; | 
|---|
| 909 | } | 
|---|
| 910 |  | 
|---|
| 911 | printf ( "%d\n", cnt == 0 || *val == '\177' ? -1 : *val); | 
|---|
| 912 | } | 
|---|
| 913 | break; | 
|---|
| 914 | case word: | 
|---|
| 915 | { | 
|---|
| 916 | union { unsigned int word; char *string; } val; | 
|---|
| 917 | val.string = nl_langinfo (item->item_id); | 
|---|
| 918 | if (show_keyword_name) | 
|---|
| 919 | printf ( "%s=", item->name); | 
|---|
| 920 |  | 
|---|
| 921 | printf ( "%d\n", val.word); | 
|---|
| 922 | } | 
|---|
| 923 | break; | 
|---|
| 924 | case wordarray: | 
|---|
| 925 | { | 
|---|
| 926 | int first = 1; | 
|---|
| 927 | union { unsigned int *wordarray; char *string; } val; | 
|---|
| 928 |  | 
|---|
| 929 | val.string = nl_langinfo (item->item_id); | 
|---|
| 930 | if (show_keyword_name) | 
|---|
| 931 | printf ( "%s=", item->name); | 
|---|
| 932 |  | 
|---|
| 933 | for (int cnt = 0; cnt < item->max; ++cnt) | 
|---|
| 934 | { | 
|---|
| 935 | printf ( "%s%d", first ? "": ";", val.wordarray[cnt]); | 
|---|
| 936 | first = 0; | 
|---|
| 937 | } | 
|---|
| 938 | putchar ('\n'); | 
|---|
| 939 | } | 
|---|
| 940 | break; | 
|---|
| 941 | case wstring: | 
|---|
| 942 | case wstringarray: | 
|---|
| 943 | case wstringlist: | 
|---|
| 944 | /* We don't print wide character information since the same | 
|---|
| 945 | information is available in a multibyte string.  */ | 
|---|
| 946 | default: | 
|---|
| 947 | break; | 
|---|
| 948 | } | 
|---|
| 949 | } | 
|---|
| 950 |  | 
|---|
| 951 | /* Show the information request for NAME.  */ | 
|---|
| 952 | static void | 
|---|
| 953 | show_info (const char *name) | 
|---|
| 954 | { | 
|---|
| 955 | for (size_t cat_no = 0; cat_no < NCATEGORIES; ++cat_no) | 
|---|
| 956 | if (cat_no != LC_ALL) | 
|---|
| 957 | { | 
|---|
| 958 | if (strcmp (name, category[cat_no].name) == 0) | 
|---|
| 959 | /* Print the whole category.  */ | 
|---|
| 960 | { | 
|---|
| 961 | if (show_category_name != 0) | 
|---|
| 962 | puts (category[cat_no].name); | 
|---|
| 963 |  | 
|---|
| 964 | for (size_t item_no = 0; | 
|---|
| 965 | item_no < category[cat_no].number; | 
|---|
| 966 | ++item_no) | 
|---|
| 967 | print_item (&category[cat_no].item_desc[item_no]); | 
|---|
| 968 |  | 
|---|
| 969 | return; | 
|---|
| 970 | } | 
|---|
| 971 |  | 
|---|
| 972 | for (size_t item_no = 0; item_no < category[cat_no].number; ++item_no) | 
|---|
| 973 | if (strcmp (name, category[cat_no].item_desc[item_no].name) == 0) | 
|---|
| 974 | { | 
|---|
| 975 | if (show_category_name != 0) | 
|---|
| 976 | puts (category[cat_no].name); | 
|---|
| 977 |  | 
|---|
| 978 | print_item (&category[cat_no].item_desc[item_no]); | 
|---|
| 979 | return; | 
|---|
| 980 | } | 
|---|
| 981 | } | 
|---|
| 982 |  | 
|---|
| 983 | /* The name is not a standard one. | 
|---|
| 984 | For testing and perhaps advanced use allow some more symbols.  */ | 
|---|
| 985 | locale_special (name, show_category_name, show_keyword_name); | 
|---|
| 986 | } | 
|---|
| 987 |  | 
|---|
| 988 | /* Set to true by try_setlocale if setlocale fails.  Used by | 
|---|
| 989 | setlocale_diagnostics.  */ | 
|---|
| 990 | static bool setlocale_failed; | 
|---|
| 991 |  | 
|---|
| 992 | /* Call setlocale, with non-fatal error reporting.  */ | 
|---|
| 993 | static void | 
|---|
| 994 | try_setlocale (int category, const char *category_name) | 
|---|
| 995 | { | 
|---|
| 996 | if (setlocale (category, "") == NULL) | 
|---|
| 997 | { | 
|---|
| 998 | error (0, errno, gettext ( "Cannot set %s to default locale"), | 
|---|
| 999 | category_name); | 
|---|
| 1000 | setlocale_failed = true; | 
|---|
| 1001 | } | 
|---|
| 1002 | } | 
|---|
| 1003 |  | 
|---|
| 1004 | /* Return a quoted version of the passed string, or NULL on error.  */ | 
|---|
| 1005 | static char * | 
|---|
| 1006 | quote_string (const char *input) | 
|---|
| 1007 | { | 
|---|
| 1008 | char *buffer; | 
|---|
| 1009 | size_t length; | 
|---|
| 1010 | FILE *stream = open_memstream (&buffer, &length); | 
|---|
| 1011 | if (stream == NULL) | 
|---|
| 1012 | return NULL; | 
|---|
| 1013 |  | 
|---|
| 1014 | while (true) | 
|---|
| 1015 | { | 
|---|
| 1016 | unsigned char ch = *input++; | 
|---|
| 1017 | if (ch == '\0') | 
|---|
| 1018 | break; | 
|---|
| 1019 |  | 
|---|
| 1020 | /* Use C backslash escapes for those control characters for | 
|---|
| 1021 | which they are defined.  */ | 
|---|
| 1022 | switch (ch) | 
|---|
| 1023 | { | 
|---|
| 1024 | case '\a': | 
|---|
| 1025 | putc_unlocked ('\\', stream); | 
|---|
| 1026 | putc_unlocked ('a', stream); | 
|---|
| 1027 | break; | 
|---|
| 1028 | case '\b': | 
|---|
| 1029 | putc_unlocked ('\\', stream); | 
|---|
| 1030 | putc_unlocked ('b', stream); | 
|---|
| 1031 | break; | 
|---|
| 1032 | case '\f': | 
|---|
| 1033 | putc_unlocked ('\\', stream); | 
|---|
| 1034 | putc_unlocked ('f', stream); | 
|---|
| 1035 | break; | 
|---|
| 1036 | case '\n': | 
|---|
| 1037 | putc_unlocked ('\\', stream); | 
|---|
| 1038 | putc_unlocked ('n', stream); | 
|---|
| 1039 | break; | 
|---|
| 1040 | case '\r': | 
|---|
| 1041 | putc_unlocked ('\\', stream); | 
|---|
| 1042 | putc_unlocked ('r', stream); | 
|---|
| 1043 | break; | 
|---|
| 1044 | case '\t': | 
|---|
| 1045 | putc_unlocked ('\\', stream); | 
|---|
| 1046 | putc_unlocked ('t', stream); | 
|---|
| 1047 | break; | 
|---|
| 1048 | case '\v': | 
|---|
| 1049 | putc_unlocked ('\\', stream); | 
|---|
| 1050 | putc_unlocked ('v', stream); | 
|---|
| 1051 | break; | 
|---|
| 1052 | case '\\': | 
|---|
| 1053 | case '\'': | 
|---|
| 1054 | case '\"': | 
|---|
| 1055 | putc_unlocked ('\\', stream); | 
|---|
| 1056 | putc_unlocked (ch, stream); | 
|---|
| 1057 | break; | 
|---|
| 1058 | default: | 
|---|
| 1059 | if (ch < ' ' || ch > '~') | 
|---|
| 1060 | /* Use octal sequences because they are fixed width, | 
|---|
| 1061 | unlike hexadecimal sequences.  */ | 
|---|
| 1062 | fprintf (stream, "\\%03o", ch); | 
|---|
| 1063 | else | 
|---|
| 1064 | putc_unlocked (ch, stream); | 
|---|
| 1065 | } | 
|---|
| 1066 | } | 
|---|
| 1067 |  | 
|---|
| 1068 | if (ferror (stream)) | 
|---|
| 1069 | { | 
|---|
| 1070 | fclose (stream); | 
|---|
| 1071 | free (buffer); | 
|---|
| 1072 | return NULL; | 
|---|
| 1073 | } | 
|---|
| 1074 | if (fclose (stream) != 0) | 
|---|
| 1075 | { | 
|---|
| 1076 | free (buffer); | 
|---|
| 1077 | return NULL; | 
|---|
| 1078 | } | 
|---|
| 1079 |  | 
|---|
| 1080 | return buffer; | 
|---|
| 1081 | } | 
|---|
| 1082 |  | 
|---|
| 1083 | /* Print additional information if there was a setlocale error (during | 
|---|
| 1084 | try_setlocale).  */ | 
|---|
| 1085 | static void | 
|---|
| 1086 | setlocale_diagnostics (void) | 
|---|
| 1087 | { | 
|---|
| 1088 | if (setlocale_failed) | 
|---|
| 1089 | { | 
|---|
| 1090 | const char *locpath = getenv ( "LOCPATH"); | 
|---|
| 1091 | if (locpath != NULL) | 
|---|
| 1092 | { | 
|---|
| 1093 | char *quoted = quote_string (locpath); | 
|---|
| 1094 | if (quoted != NULL) | 
|---|
| 1095 | fprintf (stderr, | 
|---|
| 1096 | gettext ( "\ | 
|---|
| 1097 | warning: The LOCPATH variable is set to \"%s\"\n"), | 
|---|
| 1098 | quoted); | 
|---|
| 1099 | else | 
|---|
| 1100 | fputs ( "warning: The LOCPATH variable is set\n", stderr); | 
|---|
| 1101 | free (quoted); | 
|---|
| 1102 | } | 
|---|
| 1103 | } | 
|---|
| 1104 | } | 
|---|
| 1105 |  | 
|---|