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