locale.c revision 310041
1/*- 2 * Copyright (c) 2002, 2003 Alexey Zelkin <phantom@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: stable/10/usr.bin/locale/locale.c 310041 2016-12-13 23:10:35Z vangyzen $ 27 */ 28 29/* 30 * XXX: implement missing era_* (LC_TIME) keywords (require libc & 31 * nl_langinfo(3) extensions) 32 * 33 * XXX: correctly handle reserved 'charmap' keyword and '-m' option (require 34 * localedef(1) implementation). Currently it's handled via 35 * nl_langinfo(CODESET). 36 */ 37 38#include <sys/types.h> 39#include <dirent.h> 40#include <err.h> 41#include <limits.h> 42#include <locale.h> 43#include <langinfo.h> 44#include <stdio.h> 45#include <stdlib.h> 46#include <string.h> 47#include <stringlist.h> 48#include <unistd.h> 49#include "setlocale.h" 50 51/* Local prototypes */ 52char *format_grouping(const char *); 53void init_locales_list(void); 54void list_charmaps(void); 55void list_locales(void); 56const char *lookup_localecat(int); 57char *kwval_lconv(int); 58int kwval_lookup(const char *, char **, int *, int *); 59void showdetails(const char *); 60void showkeywordslist(char *substring); 61void showlocale(void); 62void usage(void); 63 64/* Global variables */ 65static StringList *locales = NULL; 66 67static int all_locales = 0; 68static int all_charmaps = 0; 69static int prt_categories = 0; 70static int prt_keywords = 0; 71 72static const struct _lcinfo { 73 const char *name; 74 int id; 75} lcinfo [] = { 76 { "LC_CTYPE", LC_CTYPE }, 77 { "LC_COLLATE", LC_COLLATE }, 78 { "LC_TIME", LC_TIME }, 79 { "LC_NUMERIC", LC_NUMERIC }, 80 { "LC_MONETARY", LC_MONETARY }, 81 { "LC_MESSAGES", LC_MESSAGES } 82}; 83#define NLCINFO (sizeof(lcinfo)/sizeof(lcinfo[0])) 84 85/* ids for values not referenced by nl_langinfo() */ 86#define KW_ZERO 10000 87#define KW_GROUPING (KW_ZERO+1) 88#define KW_INT_CURR_SYMBOL (KW_ZERO+2) 89#define KW_CURRENCY_SYMBOL (KW_ZERO+3) 90#define KW_MON_DECIMAL_POINT (KW_ZERO+4) 91#define KW_MON_THOUSANDS_SEP (KW_ZERO+5) 92#define KW_MON_GROUPING (KW_ZERO+6) 93#define KW_POSITIVE_SIGN (KW_ZERO+7) 94#define KW_NEGATIVE_SIGN (KW_ZERO+8) 95#define KW_INT_FRAC_DIGITS (KW_ZERO+9) 96#define KW_FRAC_DIGITS (KW_ZERO+10) 97#define KW_P_CS_PRECEDES (KW_ZERO+11) 98#define KW_P_SEP_BY_SPACE (KW_ZERO+12) 99#define KW_N_CS_PRECEDES (KW_ZERO+13) 100#define KW_N_SEP_BY_SPACE (KW_ZERO+14) 101#define KW_P_SIGN_POSN (KW_ZERO+15) 102#define KW_N_SIGN_POSN (KW_ZERO+16) 103#define KW_INT_P_CS_PRECEDES (KW_ZERO+17) 104#define KW_INT_P_SEP_BY_SPACE (KW_ZERO+18) 105#define KW_INT_N_CS_PRECEDES (KW_ZERO+19) 106#define KW_INT_N_SEP_BY_SPACE (KW_ZERO+20) 107#define KW_INT_P_SIGN_POSN (KW_ZERO+21) 108#define KW_INT_N_SIGN_POSN (KW_ZERO+22) 109 110static const struct _kwinfo { 111 const char *name; 112 int isstr; /* true - string, false - number */ 113 int catid; /* LC_* */ 114 int value_ref; 115 const char *comment; 116} kwinfo [] = { 117 { "charmap", 1, LC_CTYPE, CODESET, "" }, /* hack */ 118 119 { "decimal_point", 1, LC_NUMERIC, RADIXCHAR, "" }, 120 { "thousands_sep", 1, LC_NUMERIC, THOUSEP, "" }, 121 { "grouping", 1, LC_NUMERIC, KW_GROUPING, "" }, 122 { "radixchar", 1, LC_NUMERIC, RADIXCHAR, 123 "Same as decimal_point (FreeBSD only)" }, /* compat */ 124 { "thousep", 1, LC_NUMERIC, THOUSEP, 125 "Same as thousands_sep (FreeBSD only)" }, /* compat */ 126 127 { "int_curr_symbol", 1, LC_MONETARY, KW_INT_CURR_SYMBOL, "" }, 128 { "currency_symbol", 1, LC_MONETARY, KW_CURRENCY_SYMBOL, "" }, 129 { "mon_decimal_point", 1, LC_MONETARY, KW_MON_DECIMAL_POINT, "" }, 130 { "mon_thousands_sep", 1, LC_MONETARY, KW_MON_THOUSANDS_SEP, "" }, 131 { "mon_grouping", 1, LC_MONETARY, KW_MON_GROUPING, "" }, 132 { "positive_sign", 1, LC_MONETARY, KW_POSITIVE_SIGN, "" }, 133 { "negative_sign", 1, LC_MONETARY, KW_NEGATIVE_SIGN, "" }, 134 135 { "int_frac_digits", 0, LC_MONETARY, KW_INT_FRAC_DIGITS, "" }, 136 { "frac_digits", 0, LC_MONETARY, KW_FRAC_DIGITS, "" }, 137 { "p_cs_precedes", 0, LC_MONETARY, KW_P_CS_PRECEDES, "" }, 138 { "p_sep_by_space", 0, LC_MONETARY, KW_P_SEP_BY_SPACE, "" }, 139 { "n_cs_precedes", 0, LC_MONETARY, KW_N_CS_PRECEDES, "" }, 140 { "n_sep_by_space", 0, LC_MONETARY, KW_N_SEP_BY_SPACE, "" }, 141 { "p_sign_posn", 0, LC_MONETARY, KW_P_SIGN_POSN, "" }, 142 { "n_sign_posn", 0, LC_MONETARY, KW_N_SIGN_POSN, "" }, 143 { "int_p_cs_precedes", 0, LC_MONETARY, KW_INT_P_CS_PRECEDES, "" }, 144 { "int_p_sep_by_space", 0, LC_MONETARY, KW_INT_P_SEP_BY_SPACE, "" }, 145 { "int_n_cs_precedes", 0, LC_MONETARY, KW_INT_N_CS_PRECEDES, "" }, 146 { "int_n_sep_by_space", 0, LC_MONETARY, KW_INT_N_SEP_BY_SPACE, "" }, 147 { "int_p_sign_posn", 0, LC_MONETARY, KW_INT_P_SIGN_POSN, "" }, 148 { "int_n_sign_posn", 0, LC_MONETARY, KW_INT_N_SIGN_POSN, "" }, 149 150 { "d_t_fmt", 1, LC_TIME, D_T_FMT, "" }, 151 { "d_fmt", 1, LC_TIME, D_FMT, "" }, 152 { "t_fmt", 1, LC_TIME, T_FMT, "" }, 153 { "am_str", 1, LC_TIME, AM_STR, "" }, 154 { "pm_str", 1, LC_TIME, PM_STR, "" }, 155 { "t_fmt_ampm", 1, LC_TIME, T_FMT_AMPM, "" }, 156 { "day_1", 1, LC_TIME, DAY_1, "" }, 157 { "day_2", 1, LC_TIME, DAY_2, "" }, 158 { "day_3", 1, LC_TIME, DAY_3, "" }, 159 { "day_4", 1, LC_TIME, DAY_4, "" }, 160 { "day_5", 1, LC_TIME, DAY_5, "" }, 161 { "day_6", 1, LC_TIME, DAY_6, "" }, 162 { "day_7", 1, LC_TIME, DAY_7, "" }, 163 { "abday_1", 1, LC_TIME, ABDAY_1, "" }, 164 { "abday_2", 1, LC_TIME, ABDAY_2, "" }, 165 { "abday_3", 1, LC_TIME, ABDAY_3, "" }, 166 { "abday_4", 1, LC_TIME, ABDAY_4, "" }, 167 { "abday_5", 1, LC_TIME, ABDAY_5, "" }, 168 { "abday_6", 1, LC_TIME, ABDAY_6, "" }, 169 { "abday_7", 1, LC_TIME, ABDAY_7, "" }, 170 { "mon_1", 1, LC_TIME, MON_1, "" }, 171 { "mon_2", 1, LC_TIME, MON_2, "" }, 172 { "mon_3", 1, LC_TIME, MON_3, "" }, 173 { "mon_4", 1, LC_TIME, MON_4, "" }, 174 { "mon_5", 1, LC_TIME, MON_5, "" }, 175 { "mon_6", 1, LC_TIME, MON_6, "" }, 176 { "mon_7", 1, LC_TIME, MON_7, "" }, 177 { "mon_8", 1, LC_TIME, MON_8, "" }, 178 { "mon_9", 1, LC_TIME, MON_9, "" }, 179 { "mon_10", 1, LC_TIME, MON_10, "" }, 180 { "mon_11", 1, LC_TIME, MON_11, "" }, 181 { "mon_12", 1, LC_TIME, MON_12, "" }, 182 { "abmon_1", 1, LC_TIME, ABMON_1, "" }, 183 { "abmon_2", 1, LC_TIME, ABMON_2, "" }, 184 { "abmon_3", 1, LC_TIME, ABMON_3, "" }, 185 { "abmon_4", 1, LC_TIME, ABMON_4, "" }, 186 { "abmon_5", 1, LC_TIME, ABMON_5, "" }, 187 { "abmon_6", 1, LC_TIME, ABMON_6, "" }, 188 { "abmon_7", 1, LC_TIME, ABMON_7, "" }, 189 { "abmon_8", 1, LC_TIME, ABMON_8, "" }, 190 { "abmon_9", 1, LC_TIME, ABMON_9, "" }, 191 { "abmon_10", 1, LC_TIME, ABMON_10, "" }, 192 { "abmon_11", 1, LC_TIME, ABMON_11, "" }, 193 { "abmon_12", 1, LC_TIME, ABMON_12, "" }, 194 { "altmon_1", 1, LC_TIME, ALTMON_1, "(FreeBSD only)" }, 195 { "altmon_2", 1, LC_TIME, ALTMON_2, "(FreeBSD only)" }, 196 { "altmon_3", 1, LC_TIME, ALTMON_3, "(FreeBSD only)" }, 197 { "altmon_4", 1, LC_TIME, ALTMON_4, "(FreeBSD only)" }, 198 { "altmon_5", 1, LC_TIME, ALTMON_5, "(FreeBSD only)" }, 199 { "altmon_6", 1, LC_TIME, ALTMON_6, "(FreeBSD only)" }, 200 { "altmon_7", 1, LC_TIME, ALTMON_7, "(FreeBSD only)" }, 201 { "altmon_8", 1, LC_TIME, ALTMON_8, "(FreeBSD only)" }, 202 { "altmon_9", 1, LC_TIME, ALTMON_9, "(FreeBSD only)" }, 203 { "altmon_10", 1, LC_TIME, ALTMON_10, "(FreeBSD only)" }, 204 { "altmon_11", 1, LC_TIME, ALTMON_11, "(FreeBSD only)" }, 205 { "altmon_12", 1, LC_TIME, ALTMON_12, "(FreeBSD only)" }, 206 { "era", 1, LC_TIME, ERA, "(unavailable)" }, 207 { "era_d_fmt", 1, LC_TIME, ERA_D_FMT, "(unavailable)" }, 208 { "era_d_t_fmt", 1, LC_TIME, ERA_D_T_FMT, "(unavailable)" }, 209 { "era_t_fmt", 1, LC_TIME, ERA_T_FMT, "(unavailable)" }, 210 { "alt_digits", 1, LC_TIME, ALT_DIGITS, "" }, 211 { "d_md_order", 1, LC_TIME, D_MD_ORDER, 212 "(FreeBSD only)" }, /* local */ 213 214 { "yesexpr", 1, LC_MESSAGES, YESEXPR, "" }, 215 { "noexpr", 1, LC_MESSAGES, NOEXPR, "" }, 216 { "yesstr", 1, LC_MESSAGES, YESSTR, 217 "(POSIX legacy)" }, /* compat */ 218 { "nostr", 1, LC_MESSAGES, NOSTR, 219 "(POSIX legacy)" } /* compat */ 220 221}; 222#define NKWINFO (sizeof(kwinfo)/sizeof(kwinfo[0])) 223 224static const char *boguslocales[] = { "UTF-8" }; 225#define NBOGUS (sizeof(boguslocales)/sizeof(boguslocales[0])) 226 227int 228main(int argc, char *argv[]) 229{ 230 int ch; 231 int tmp; 232 233 while ((ch = getopt(argc, argv, "ackms:")) != -1) { 234 switch (ch) { 235 case 'a': 236 all_locales = 1; 237 break; 238 case 'c': 239 prt_categories = 1; 240 break; 241 case 'k': 242 prt_keywords = 1; 243 break; 244 case 'm': 245 all_charmaps = 1; 246 break; 247 default: 248 usage(); 249 } 250 } 251 argc -= optind; 252 argv += optind; 253 254 /* validate arguments */ 255 if (all_locales && all_charmaps) 256 usage(); 257 if ((all_locales || all_charmaps) && argc > 0) 258 usage(); 259 if ((all_locales || all_charmaps) && (prt_categories || prt_keywords)) 260 usage(); 261 262 /* process '-a' */ 263 if (all_locales) { 264 list_locales(); 265 exit(0); 266 } 267 268 /* process '-m' */ 269 if (all_charmaps) { 270 list_charmaps(); 271 exit(0); 272 } 273 274 /* check for special case '-k list' */ 275 tmp = 0; 276 if (prt_keywords && argc > 0) 277 while (tmp < argc) 278 if (strcasecmp(argv[tmp++], "list") == 0) { 279 showkeywordslist(argv[tmp]); 280 exit(0); 281 } 282 283 /* process '-c', '-k', or command line arguments. */ 284 if (prt_categories || prt_keywords || argc > 0) { 285 if (prt_keywords || argc > 0) 286 setlocale(LC_ALL, ""); 287 if (argc > 0) { 288 while (argc > 0) { 289 showdetails(*argv); 290 argv++; 291 argc--; 292 } 293 } else { 294 uint i; 295 for (i = 0; i < sizeof (kwinfo) / sizeof (struct _kwinfo); i++) 296 showdetails(kwinfo[i].name); 297 } 298 exit(0); 299 } 300 301 /* no arguments, show current locale state */ 302 showlocale(); 303 304 return (0); 305} 306 307void 308usage(void) 309{ 310 printf("Usage: locale [ -a | -m ]\n" 311 " locale -k list [prefix]\n" 312 " locale [ -ck ] [keyword ...]\n"); 313 exit(1); 314} 315 316/* 317 * Output information about all available locales 318 * 319 * XXX actually output of this function does not guarantee that locale 320 * is really available to application, since it can be broken or 321 * inconsistent thus setlocale() will fail. Maybe add '-V' function to 322 * also validate these locales? 323 */ 324void 325list_locales(void) 326{ 327 size_t i; 328 329 init_locales_list(); 330 for (i = 0; i < locales->sl_cur; i++) { 331 printf("%s\n", locales->sl_str[i]); 332 } 333} 334 335/* 336 * qsort() helper function 337 */ 338static int 339scmp(const void *s1, const void *s2) 340{ 341 return strcmp(*(const char * const *)s1, *(const char * const *)s2); 342} 343 344/* 345 * Output information about all available charmaps 346 * 347 * XXX this function is doing a task in hackish way, i.e. by scaning 348 * list of locales, spliting their codeset part and building list of 349 * them. 350 */ 351void 352list_charmaps(void) 353{ 354 size_t i; 355 char *s, *cs; 356 StringList *charmaps; 357 358 /* initialize StringList */ 359 charmaps = sl_init(); 360 if (charmaps == NULL) 361 err(1, "could not allocate memory"); 362 363 /* fetch locales list */ 364 init_locales_list(); 365 366 /* split codesets and build their list */ 367 for (i = 0; i < locales->sl_cur; i++) { 368 s = locales->sl_str[i]; 369 if ((cs = strchr(s, '.')) != NULL) { 370 cs++; 371 if (sl_find(charmaps, cs) == NULL) 372 sl_add(charmaps, cs); 373 } 374 } 375 376 /* add US-ASCII, if not yet added */ 377 if (sl_find(charmaps, "US-ASCII") == NULL) 378 sl_add(charmaps, strdup("US-ASCII")); 379 380 /* sort the list */ 381 qsort(charmaps->sl_str, charmaps->sl_cur, sizeof(char *), scmp); 382 383 /* print results */ 384 for (i = 0; i < charmaps->sl_cur; i++) { 385 printf("%s\n", charmaps->sl_str[i]); 386 } 387} 388 389/* 390 * Retrieve sorted list of system locales (or user locales, if PATH_LOCALE 391 * environment variable is set) 392 */ 393void 394init_locales_list(void) 395{ 396 DIR *dirp; 397 struct dirent *dp; 398 size_t i; 399 int bogus; 400 401 /* why call this function twice ? */ 402 if (locales != NULL) 403 return; 404 405 /* initialize StringList */ 406 locales = sl_init(); 407 if (locales == NULL) 408 err(1, "could not allocate memory"); 409 410 /* get actual locales directory name */ 411 if (__detect_path_locale() != 0) 412 err(1, "unable to find locales storage"); 413 414 /* open locales directory */ 415 dirp = opendir(_PathLocale); 416 if (dirp == NULL) 417 err(1, "could not open directory '%s'", _PathLocale); 418 419 /* scan directory and store its contents except "." and ".." */ 420 while ((dp = readdir(dirp)) != NULL) { 421 if (*(dp->d_name) == '.') 422 continue; /* exclude "." and ".." */ 423 for (bogus = i = 0; i < NBOGUS; i++) 424 if (strncmp(dp->d_name, boguslocales[i], 425 strlen(boguslocales[i])) == 0) 426 bogus = 1; 427 if (!bogus) 428 sl_add(locales, strdup(dp->d_name)); 429 } 430 closedir(dirp); 431 432 /* make sure that 'POSIX' and 'C' locales are present in the list. 433 * POSIX 1003.1-2001 requires presence of 'POSIX' name only here, but 434 * we also list 'C' for constistency 435 */ 436 if (sl_find(locales, "POSIX") == NULL) 437 sl_add(locales, strdup("POSIX")); 438 439 if (sl_find(locales, "C") == NULL) 440 sl_add(locales, strdup("C")); 441 442 /* make output nicer, sort the list */ 443 qsort(locales->sl_str, locales->sl_cur, sizeof(char *), scmp); 444} 445 446/* 447 * Show current locale status, depending on environment variables 448 */ 449void 450showlocale(void) 451{ 452 size_t i; 453 const char *lang, *vval, *eval; 454 455 setlocale(LC_ALL, ""); 456 457 lang = getenv("LANG"); 458 if (lang == NULL) { 459 lang = ""; 460 } 461 printf("LANG=%s\n", lang); 462 /* XXX: if LANG is null, then set it to "C" to get implied values? */ 463 464 for (i = 0; i < NLCINFO; i++) { 465 vval = setlocale(lcinfo[i].id, NULL); 466 eval = getenv(lcinfo[i].name); 467 if (eval != NULL && !strcmp(eval, vval) 468 && strcmp(lang, vval)) { 469 /* 470 * Appropriate environment variable set, its value 471 * is valid and not overridden by LC_ALL 472 * 473 * XXX: possible side effect: if both LANG and 474 * overridden environment variable are set into same 475 * value, then it'll be assumed as 'implied' 476 */ 477 printf("%s=%s\n", lcinfo[i].name, vval); 478 } else { 479 printf("%s=\"%s\"\n", lcinfo[i].name, vval); 480 } 481 } 482 483 vval = getenv("LC_ALL"); 484 if (vval == NULL) { 485 vval = ""; 486 } 487 printf("LC_ALL=%s\n", vval); 488} 489 490char * 491format_grouping(const char *binary) 492{ 493 static char rval[64]; 494 const char *cp; 495 size_t roff; 496 int len; 497 498 rval[0] = '\0'; 499 roff = 0; 500 for (cp = binary; *cp != '\0'; ++cp) { 501#if CHAR_MIN != 0 502 if (*cp < 0) 503 break; /* garbage input */ 504#endif 505 len = snprintf(&rval[roff], sizeof(rval) - roff, "%u;", *cp); 506 if (len < 0 || (unsigned)len >= sizeof(rval) - roff) 507 break; /* insufficient space for output */ 508 roff += len; 509 if (*cp == CHAR_MAX) 510 break; /* special termination */ 511 } 512 513 /* Truncate at the last successfully snprintf()ed semicolon. */ 514 if (roff != 0) 515 rval[roff - 1] = '\0'; 516 517 return (&rval[0]); 518} 519 520/* 521 * keyword value lookup helper (via localeconv()) 522 */ 523char * 524kwval_lconv(int id) 525{ 526 struct lconv *lc; 527 char *rval; 528 529 rval = NULL; 530 lc = localeconv(); 531 switch (id) { 532 case KW_GROUPING: 533 rval = format_grouping(lc->grouping); 534 break; 535 case KW_INT_CURR_SYMBOL: 536 rval = lc->int_curr_symbol; 537 break; 538 case KW_CURRENCY_SYMBOL: 539 rval = lc->currency_symbol; 540 break; 541 case KW_MON_DECIMAL_POINT: 542 rval = lc->mon_decimal_point; 543 break; 544 case KW_MON_THOUSANDS_SEP: 545 rval = lc->mon_thousands_sep; 546 break; 547 case KW_MON_GROUPING: 548 rval = format_grouping(lc->mon_grouping); 549 break; 550 case KW_POSITIVE_SIGN: 551 rval = lc->positive_sign; 552 break; 553 case KW_NEGATIVE_SIGN: 554 rval = lc->negative_sign; 555 break; 556 case KW_INT_FRAC_DIGITS: 557 rval = &(lc->int_frac_digits); 558 break; 559 case KW_FRAC_DIGITS: 560 rval = &(lc->frac_digits); 561 break; 562 case KW_P_CS_PRECEDES: 563 rval = &(lc->p_cs_precedes); 564 break; 565 case KW_P_SEP_BY_SPACE: 566 rval = &(lc->p_sep_by_space); 567 break; 568 case KW_N_CS_PRECEDES: 569 rval = &(lc->n_cs_precedes); 570 break; 571 case KW_N_SEP_BY_SPACE: 572 rval = &(lc->n_sep_by_space); 573 break; 574 case KW_P_SIGN_POSN: 575 rval = &(lc->p_sign_posn); 576 break; 577 case KW_N_SIGN_POSN: 578 rval = &(lc->n_sign_posn); 579 break; 580 case KW_INT_P_CS_PRECEDES: 581 rval = &(lc->int_p_cs_precedes); 582 break; 583 case KW_INT_P_SEP_BY_SPACE: 584 rval = &(lc->int_p_sep_by_space); 585 break; 586 case KW_INT_N_CS_PRECEDES: 587 rval = &(lc->int_n_cs_precedes); 588 break; 589 case KW_INT_N_SEP_BY_SPACE: 590 rval = &(lc->int_n_sep_by_space); 591 break; 592 case KW_INT_P_SIGN_POSN: 593 rval = &(lc->int_p_sign_posn); 594 break; 595 case KW_INT_N_SIGN_POSN: 596 rval = &(lc->int_n_sign_posn); 597 break; 598 default: 599 break; 600 } 601 return (rval); 602} 603 604/* 605 * keyword value and properties lookup 606 */ 607int 608kwval_lookup(const char *kwname, char **kwval, int *cat, int *isstr) 609{ 610 int rval; 611 size_t i; 612 613 rval = 0; 614 for (i = 0; i < NKWINFO; i++) { 615 if (strcasecmp(kwname, kwinfo[i].name) == 0) { 616 rval = 1; 617 *cat = kwinfo[i].catid; 618 *isstr = kwinfo[i].isstr; 619 if (kwinfo[i].value_ref < KW_ZERO) { 620 *kwval = nl_langinfo(kwinfo[i].value_ref); 621 } else { 622 *kwval = kwval_lconv(kwinfo[i].value_ref); 623 } 624 break; 625 } 626 } 627 628 return (rval); 629} 630 631/* 632 * Show details about requested keyword according to '-k' and/or '-c' 633 * command line options specified. 634 */ 635void 636showdetails(const char *kw) 637{ 638 int isstr, cat, tmpval; 639 char *kwval; 640 641 if (kwval_lookup(kw, &kwval, &cat, &isstr) == 0) { 642 /* 643 * invalid keyword specified. 644 * XXX: any actions? 645 */ 646 fprintf(stderr, "Unknown keyword: `%s'\n", kw); 647 return; 648 } 649 650 if (prt_categories) { 651 if (prt_keywords) 652 printf("%-20s ", lookup_localecat(cat)); 653 else 654 printf("%-20s\t%s\n", kw, lookup_localecat(cat)); 655 } 656 657 if (prt_keywords) { 658 if (isstr) { 659 printf("%s=\"%s\"\n", kw, kwval); 660 } else { 661 tmpval = (char) *kwval; 662 printf("%s=%d\n", kw, tmpval); 663 } 664 } 665 666 if (!prt_categories && !prt_keywords) { 667 if (isstr) { 668 printf("%s\n", kwval); 669 } else { 670 tmpval = (char) *kwval; 671 printf("%d\n", tmpval); 672 } 673 } 674} 675 676/* 677 * Convert locale category id into string 678 */ 679const char * 680lookup_localecat(int cat) 681{ 682 size_t i; 683 684 for (i = 0; i < NLCINFO; i++) 685 if (lcinfo[i].id == cat) { 686 return (lcinfo[i].name); 687 } 688 return ("UNKNOWN"); 689} 690 691/* 692 * Show list of keywords 693 */ 694void 695showkeywordslist(char *substring) 696{ 697 size_t i; 698 699#define FMT "%-20s %-12s %-7s %-20s\n" 700 701 if (substring == NULL) 702 printf("List of available keywords\n\n"); 703 else 704 printf("List of available keywords starting with '%s'\n\n", 705 substring); 706 printf(FMT, "Keyword", "Category", "Type", "Comment"); 707 printf("-------------------- ------------ ------- --------------------\n"); 708 for (i = 0; i < NKWINFO; i++) { 709 if (substring != NULL) { 710 if (strncmp(kwinfo[i].name, substring, 711 strlen(substring)) != 0) 712 continue; 713 } 714 printf(FMT, 715 kwinfo[i].name, 716 lookup_localecat(kwinfo[i].catid), 717 (kwinfo[i].isstr == 0) ? "number" : "string", 718 kwinfo[i].comment); 719 } 720} 721