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