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