xref: /freebsd/usr.bin/locale/locale.c (revision 1fb62fb074788ca4713551be09d6569966a3abee)
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/param.h>
39 #include <sys/types.h>
40 
41 #include <dirent.h>
42 #include <err.h>
43 #include <locale.h>
44 #include <langinfo.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <stringlist.h>
49 #include <unistd.h>
50 #include "setlocale.h"
51 
52 /* Local prototypes */
53 char	*format_grouping(const char *);
54 void	init_locales_list(void);
55 void	list_charmaps(void);
56 void	list_locales(void);
57 const char *lookup_localecat(int);
58 char	*kwval_lconv(int);
59 int	kwval_lookup(char *, char **, int *, int *);
60 void	showdetails(char *);
61 void	showkeywordslist(char *substring);
62 void	showlocale(void);
63 void	usage(void);
64 
65 /* Global variables */
66 static StringList *locales = NULL;
67 
68 int	all_locales = 0;
69 int	all_charmaps = 0;
70 int	prt_categories = 0;
71 int	prt_keywords = 0;
72 int	more_params = 0;
73 
74 struct _lcinfo {
75 	const char	*name;
76 	int		id;
77 } lcinfo [] = {
78 	{ "LC_CTYPE",		LC_CTYPE },
79 	{ "LC_COLLATE",		LC_COLLATE },
80 	{ "LC_TIME",		LC_TIME },
81 	{ "LC_NUMERIC",		LC_NUMERIC },
82 	{ "LC_MONETARY",	LC_MONETARY },
83 	{ "LC_MESSAGES",	LC_MESSAGES }
84 };
85 #define	NLCINFO nitems(lcinfo)
86 
87 /* ids for values not referenced by nl_langinfo() */
88 #define	KW_ZERO			10000
89 #define	KW_GROUPING		(KW_ZERO+1)
90 #define	KW_INT_CURR_SYMBOL	(KW_ZERO+2)
91 #define	KW_CURRENCY_SYMBOL	(KW_ZERO+3)
92 #define	KW_MON_DECIMAL_POINT	(KW_ZERO+4)
93 #define	KW_MON_THOUSANDS_SEP	(KW_ZERO+5)
94 #define	KW_MON_GROUPING		(KW_ZERO+6)
95 #define	KW_POSITIVE_SIGN	(KW_ZERO+7)
96 #define	KW_NEGATIVE_SIGN	(KW_ZERO+8)
97 #define	KW_INT_FRAC_DIGITS	(KW_ZERO+9)
98 #define	KW_FRAC_DIGITS		(KW_ZERO+10)
99 #define	KW_P_CS_PRECEDES	(KW_ZERO+11)
100 #define	KW_P_SEP_BY_SPACE	(KW_ZERO+12)
101 #define	KW_N_CS_PRECEDES	(KW_ZERO+13)
102 #define	KW_N_SEP_BY_SPACE	(KW_ZERO+14)
103 #define	KW_P_SIGN_POSN		(KW_ZERO+15)
104 #define	KW_N_SIGN_POSN		(KW_ZERO+16)
105 #define	KW_INT_P_CS_PRECEDES	(KW_ZERO+17)
106 #define	KW_INT_P_SEP_BY_SPACE	(KW_ZERO+18)
107 #define	KW_INT_N_CS_PRECEDES	(KW_ZERO+19)
108 #define	KW_INT_N_SEP_BY_SPACE	(KW_ZERO+20)
109 #define	KW_INT_P_SIGN_POSN	(KW_ZERO+21)
110 #define	KW_INT_N_SIGN_POSN	(KW_ZERO+22)
111 
112 struct _kwinfo {
113 	const char	*name;
114 	int		isstr;		/* true - string, false - number */
115 	int		catid;		/* LC_* */
116 	int		value_ref;
117 	const char	*comment;
118 } kwinfo [] = {
119 	{ "charmap",		1, LC_CTYPE,	CODESET, "" },	/* hack */
120 
121 	{ "decimal_point",	1, LC_NUMERIC,	RADIXCHAR, "" },
122 	{ "thousands_sep",	1, LC_NUMERIC,	THOUSEP, "" },
123 	{ "grouping",		1, LC_NUMERIC,	KW_GROUPING, "" },
124 	{ "radixchar",		1, LC_NUMERIC,	RADIXCHAR,
125 	  "Same as decimal_point (FreeBSD only)" },		/* compat */
126 	{ "thousep",		1, LC_NUMERIC,	THOUSEP,
127 	  "Same as thousands_sep (FreeBSD only)" },		/* compat */
128 
129 	{ "int_curr_symbol",	1, LC_MONETARY,	KW_INT_CURR_SYMBOL, "" },
130 	{ "currency_symbol",	1, LC_MONETARY,	KW_CURRENCY_SYMBOL, "" },
131 	{ "mon_decimal_point",	1, LC_MONETARY,	KW_MON_DECIMAL_POINT, "" },
132 	{ "mon_thousands_sep",	1, LC_MONETARY,	KW_MON_THOUSANDS_SEP, "" },
133 	{ "mon_grouping",	1, LC_MONETARY,	KW_MON_GROUPING, "" },
134 	{ "positive_sign",	1, LC_MONETARY,	KW_POSITIVE_SIGN, "" },
135 	{ "negative_sign",	1, LC_MONETARY,	KW_NEGATIVE_SIGN, "" },
136 
137 	{ "int_frac_digits",	0, LC_MONETARY,	KW_INT_FRAC_DIGITS, "" },
138 	{ "frac_digits",	0, LC_MONETARY,	KW_FRAC_DIGITS, "" },
139 	{ "p_cs_precedes",	0, LC_MONETARY,	KW_P_CS_PRECEDES, "" },
140 	{ "p_sep_by_space",	0, LC_MONETARY,	KW_P_SEP_BY_SPACE, "" },
141 	{ "n_cs_precedes",	0, LC_MONETARY,	KW_N_CS_PRECEDES, "" },
142 	{ "n_sep_by_space",	0, LC_MONETARY,	KW_N_SEP_BY_SPACE, "" },
143 	{ "p_sign_posn",	0, LC_MONETARY,	KW_P_SIGN_POSN, "" },
144 	{ "n_sign_posn",	0, LC_MONETARY,	KW_N_SIGN_POSN, "" },
145 	{ "int_p_cs_precedes",	0, LC_MONETARY,	KW_INT_P_CS_PRECEDES, "" },
146 	{ "int_p_sep_by_space",	0, LC_MONETARY,	KW_INT_P_SEP_BY_SPACE, "" },
147 	{ "int_n_cs_precedes",	0, LC_MONETARY,	KW_INT_N_CS_PRECEDES, "" },
148 	{ "int_n_sep_by_space",	0, LC_MONETARY,	KW_INT_N_SEP_BY_SPACE, "" },
149 	{ "int_p_sign_posn",	0, LC_MONETARY,	KW_INT_P_SIGN_POSN, "" },
150 	{ "int_n_sign_posn",	0, LC_MONETARY,	KW_INT_N_SIGN_POSN, "" },
151 
152 	{ "d_t_fmt",		1, LC_TIME,	D_T_FMT, "" },
153 	{ "d_fmt",		1, LC_TIME,	D_FMT, "" },
154 	{ "t_fmt",		1, LC_TIME,	T_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, "(FreeBSD only)" },
197 	{ "altmon_2",		1, LC_TIME,	ALTMON_2, "(FreeBSD only)" },
198 	{ "altmon_3",		1, LC_TIME,	ALTMON_3, "(FreeBSD only)" },
199 	{ "altmon_4",		1, LC_TIME,	ALTMON_4, "(FreeBSD only)" },
200 	{ "altmon_5",		1, LC_TIME,	ALTMON_5, "(FreeBSD only)" },
201 	{ "altmon_6",		1, LC_TIME,	ALTMON_6, "(FreeBSD only)" },
202 	{ "altmon_7",		1, LC_TIME,	ALTMON_7, "(FreeBSD only)" },
203 	{ "altmon_8",		1, LC_TIME,	ALTMON_8, "(FreeBSD only)" },
204 	{ "altmon_9",		1, LC_TIME,	ALTMON_9, "(FreeBSD only)" },
205 	{ "altmon_10",		1, LC_TIME,	ALTMON_10, "(FreeBSD only)" },
206 	{ "altmon_11",		1, LC_TIME,	ALTMON_11, "(FreeBSD only)" },
207 	{ "altmon_12",		1, LC_TIME,	ALTMON_12, "(FreeBSD only)" },
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 (nitems(kwinfo))
225 
226 const char *boguslocales[] = { "UTF-8" };
227 #define	NBOGUS	(nitems(boguslocales))
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 
264 	/* process '-a' */
265 	if (all_locales) {
266 		list_locales();
267 		exit(0);
268 	}
269 
270 	/* process '-m' */
271 	if (all_charmaps) {
272 		list_charmaps();
273 		exit(0);
274 	}
275 
276 	/* check for special case '-k list' */
277 	tmp = 0;
278 	if (prt_keywords && argc > 0)
279 		while (tmp < argc)
280 			if (strcasecmp(argv[tmp++], "list") == 0) {
281 				showkeywordslist(argv[tmp]);
282 				exit(0);
283 			}
284 
285 	/* process '-c', '-k', or command line arguments. */
286 	if (prt_categories || prt_keywords || argc > 0) {
287 		if (prt_keywords || argc > 0)
288 			setlocale(LC_ALL, "");
289 		if (argc > 0) {
290 			while (argc > 0) {
291 				showdetails(*argv);
292 				argv++;
293 				argc--;
294 			}
295 		} else {
296 			uint i;
297 			for (i = 0; i < nitems(kwinfo); i++)
298 				showdetails ((char *)kwinfo [i].name);
299 		}
300 		exit(0);
301 	}
302 
303 	/* no arguments, show current locale state */
304 	showlocale();
305 
306 	return (0);
307 }
308 
309 void
310 usage(void)
311 {
312 	printf("Usage: locale [ -a | -m ]\n"
313 	       "       locale -k list [prefix]\n"
314 	       "       locale [ -ck ] [keyword ...]\n");
315 	exit(1);
316 }
317 
318 /*
319  * Output information about all available locales
320  *
321  * XXX actually output of this function does not guarantee that locale
322  *     is really available to application, since it can be broken or
323  *     inconsistent thus setlocale() will fail.  Maybe add '-V' function to
324  *     also validate these locales?
325  */
326 void
327 list_locales(void)
328 {
329 	size_t i;
330 
331 	init_locales_list();
332 	for (i = 0; i < locales->sl_cur; i++) {
333 		printf("%s\n", locales->sl_str[i]);
334 	}
335 }
336 
337 /*
338  * qsort() helper function
339  */
340 static int
341 scmp(const void *s1, const void *s2)
342 {
343 	return strcmp(*(const char **)s1, *(const char **)s2);
344 }
345 
346 /*
347  * Output information about all available charmaps
348  *
349  * XXX this function is doing a task in hackish way, i.e. by scaning
350  *     list of locales, spliting their codeset part and building list of
351  *     them.
352  */
353 void
354 list_charmaps(void)
355 {
356 	size_t i;
357 	char *s, *cs;
358 	StringList *charmaps;
359 
360 	/* initialize StringList */
361 	charmaps = sl_init();
362 	if (charmaps == NULL)
363 		err(1, "could not allocate memory");
364 
365 	/* fetch locales list */
366 	init_locales_list();
367 
368 	/* split codesets and build their list */
369 	for (i = 0; i < locales->sl_cur; i++) {
370 		s = locales->sl_str[i];
371 		if ((cs = strchr(s, '.')) != NULL) {
372 			cs++;
373 			if (sl_find(charmaps, cs) == NULL)
374 				sl_add(charmaps, cs);
375 		}
376 	}
377 
378 	/* add US-ASCII, if not yet added */
379 	if (sl_find(charmaps, "US-ASCII") == NULL)
380 		sl_add(charmaps, "US-ASCII");
381 
382 	/* sort the list */
383 	qsort(charmaps->sl_str, charmaps->sl_cur, sizeof(char *), scmp);
384 
385 	/* print results */
386 	for (i = 0; i < charmaps->sl_cur; i++) {
387 		printf("%s\n", charmaps->sl_str[i]);
388 	}
389 }
390 
391 /*
392  * Retrieve sorted list of system locales (or user locales, if PATH_LOCALE
393  * environment variable is set)
394  */
395 void
396 init_locales_list(void)
397 {
398 	DIR *dirp;
399 	struct dirent *dp;
400 	size_t i;
401 	int bogus;
402 
403 	/* why call this function twice ? */
404 	if (locales != NULL)
405 		return;
406 
407 	/* initialize StringList */
408 	locales = sl_init();
409 	if (locales == NULL)
410 		err(1, "could not allocate memory");
411 
412 	/* get actual locales directory name */
413 	if (__detect_path_locale() != 0)
414 		err(1, "unable to find locales storage");
415 
416 	/* open locales directory */
417 	dirp = opendir(_PathLocale);
418 	if (dirp == NULL)
419 		err(1, "could not open directory '%s'", _PathLocale);
420 
421 	/* scan directory and store its contents except "." and ".." */
422 	while ((dp = readdir(dirp)) != NULL) {
423 		if (*(dp->d_name) == '.')
424 			continue;		/* exclude "." and ".." */
425 		for (bogus = i = 0; i < NBOGUS; i++)
426 			if (strncmp(dp->d_name, boguslocales[i],
427 			    strlen(boguslocales[i])) == 0)
428 				bogus = 1;
429 		if (!bogus)
430 			sl_add(locales, strdup(dp->d_name));
431 	}
432 	closedir(dirp);
433 
434 	/* make sure that 'POSIX' and 'C' locales are present in the list.
435 	 * POSIX 1003.1-2001 requires presence of 'POSIX' name only here, but
436 	 * we also list 'C' for constistency
437 	 */
438 	if (sl_find(locales, "POSIX") == NULL)
439 		sl_add(locales, "POSIX");
440 
441 	if (sl_find(locales, "C") == NULL)
442 		sl_add(locales, "C");
443 
444 	/* make output nicer, sort the list */
445 	qsort(locales->sl_str, locales->sl_cur, sizeof(char *), scmp);
446 }
447 
448 /*
449  * Show current locale status, depending on environment variables
450  */
451 void
452 showlocale(void)
453 {
454 	size_t	i;
455 	const char *lang, *vval, *eval;
456 
457 	setlocale(LC_ALL, "");
458 
459 	lang = getenv("LANG");
460 	if (lang == NULL) {
461 		lang = "";
462 	}
463 	printf("LANG=%s\n", lang);
464 	/* XXX: if LANG is null, then set it to "C" to get implied values? */
465 
466 	for (i = 0; i < NLCINFO; i++) {
467 		vval = setlocale(lcinfo[i].id, NULL);
468 		eval = getenv(lcinfo[i].name);
469 		if (eval != NULL && !strcmp(eval, vval)
470 				&& strcmp(lang, vval)) {
471 			/*
472 			 * Appropriate environment variable set, its value
473 			 * is valid and not overridden by LC_ALL
474 			 *
475 			 * XXX: possible side effect: if both LANG and
476 			 * overridden environment variable are set into same
477 			 * value, then it'll be assumed as 'implied'
478 			 */
479 			printf("%s=%s\n", lcinfo[i].name, vval);
480 		} else {
481 			printf("%s=\"%s\"\n", lcinfo[i].name, vval);
482 		}
483 	}
484 
485 	vval = getenv("LC_ALL");
486 	if (vval == NULL) {
487 		vval = "";
488 	}
489 	printf("LC_ALL=%s\n", vval);
490 }
491 
492 char *
493 format_grouping(const char *binary)
494 {
495 	static char rval[64];
496 	const char *cp;
497 	size_t len;
498 
499 	rval[0] = '\0';
500 	for (cp = binary; *cp != '\0'; ++cp) {
501 		char group[sizeof("127;")];
502 		snprintf(group, sizeof(group), "%hhd;", *cp);
503 		len = strlcat(rval, group, sizeof(rval));
504 		if (len >= sizeof(rval)) {
505 			len = sizeof(rval) - 1;
506 			break;
507 		}
508 		if (*cp == CHAR_MAX) {
509 			break;
510 		}
511 	}
512 
513 	/* Remove the trailing ';'. */
514 	rval[len - 1] = '\0';
515 
516 	return (rval);
517 }
518 
519 
520 /*
521  * keyword value lookup helper (via localeconv())
522  */
523 char *
524 kwval_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  */
607 int
608 kwval_lookup(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  */
635 void
636 showdetails(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  */
679 const char *
680 lookup_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  */
694 void
695 showkeywordslist(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