summaryrefslogtreecommitdiffstats
path: root/usr.bin/locale/locale.c
blob: 4748c44affa5faf07cf21fe4f1a9ba780aacd32b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*-
 * Copyright (c) 2002, 2003 Alexey Zelkin <phantom@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

/*
 * XXX: implement missing era_* (LC_TIME) keywords (require libc &
 *	nl_langinfo(3) extensions)
 *
 * XXX: correctly handle reserved 'charmap' keyword and '-m' option (require
 *      localedef(1) implementation).  Currently it's handled via
 *	nl_langinfo(CODESET).
 */

#include <sys/types.h>
#include <dirent.h>
#include <err.h>
#include <locale.h>
#include <langinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stringlist.h>
#include <unistd.h>
#include "setlocale.h"

/* Local prototypes */
void	init_locales_list(void);
void	list_charmaps(void);
void	list_locales(void);
const char *lookup_localecat(int);
char	*kwval_lconv(int);
int	kwval_lookup(char *, char **, int *, int *);
void	showdetails(char *);
void	showkeywordslist(void);
void	showlocale(void);
void	usage(void);

/* Global variables */
static StringList *locales = NULL;

int	all_locales = 0;
int	all_charmaps = 0;
int	prt_categories = 0;
int	prt_keywords = 0;
int	more_params = 0;

struct _lcinfo {
	const char	*name;
	int		id;
} lcinfo [] = {
	{ "LC_CTYPE",		LC_CTYPE },
	{ "LC_COLLATE",		LC_COLLATE },
	{ "LC_TIME",		LC_TIME },
	{ "LC_NUMERIC",		LC_NUMERIC },
	{ "LC_MONETARY",	LC_MONETARY },
	{ "LC_MESSAGES",	LC_MESSAGES }
};
#define NLCINFO (sizeof(lcinfo)/sizeof(lcinfo[0]))

/* ids for values not referenced by nl_langinfo() */
#define	KW_ZERO			10000
#define	KW_GROUPING		(KW_ZERO+1)
#define KW_INT_CURR_SYMBOL 	(KW_ZERO+2)
#define KW_CURRENCY_SYMBOL 	(KW_ZERO+3)
#define KW_MON_DECIMAL_POINT 	(KW_ZERO+4)
#define KW_MON_THOUSANDS_SEP 	(KW_ZERO+5)
#define KW_MON_GROUPING 	(KW_ZERO+6)
#define KW_POSITIVE_SIGN 	(KW_ZERO+7)
#define KW_NEGATIVE_SIGN 	(KW_ZERO+8)
#define KW_INT_FRAC_DIGITS 	(KW_ZERO+9)
#define KW_FRAC_DIGITS 		(KW_ZERO+10)
#define KW_P_CS_PRECEDES 	(KW_ZERO+11)
#define KW_P_SEP_BY_SPACE 	(KW_ZERO+12)
#define KW_N_CS_PRECEDES 	(KW_ZERO+13)
#define KW_N_SEP_BY_SPACE 	(KW_ZERO+14)
#define KW_P_SIGN_POSN 		(KW_ZERO+15)
#define KW_N_SIGN_POSN 		(KW_ZERO+16)
#define KW_INT_P_CS_PRECEDES 	(KW_ZERO+17)
#define KW_INT_P_SEP_BY_SPACE 	(KW_ZERO+18)
#define KW_INT_N_CS_PRECEDES 	(KW_ZERO+19)
#define KW_INT_N_SEP_BY_SPACE 	(KW_ZERO+20)
#define KW_INT_P_SIGN_POSN 	(KW_ZERO+21)
#define KW_INT_N_SIGN_POSN 	(KW_ZERO+22)

struct _kwinfo {
	const char	*name;
	int		isstr;		/* true - string, false - number */
	int		catid;		/* LC_* */
	int		value_ref;
	const char	*comment;
} kwinfo [] = {
	{ "charmap",		1, LC_CTYPE,	CODESET, "" },	/* hack */

	{ "decimal_point",	1, LC_NUMERIC,	RADIXCHAR, "" },
	{ "thousands_sep",	1, LC_NUMERIC,	THOUSEP, "" },
	{ "grouping",		1, LC_NUMERIC,	KW_GROUPING, "" },
	{ "radixchar",		1, LC_NUMERIC,	RADIXCHAR,
	  "Same as decimal_point (FreeBSD only)" },		/* compat */
	{ "thousep",		1, LC_NUMERIC,	THOUSEP,
	  "Same as thousands_sep (FreeBSD only)" },		/* compat */

	{ "int_curr_symbol",	1, LC_MONETARY,	KW_INT_CURR_SYMBOL, "" },
	{ "currency_symbol",	1, LC_MONETARY,	KW_CURRENCY_SYMBOL, "" },
	{ "mon_decimal_point",	1, LC_MONETARY,	KW_MON_DECIMAL_POINT, "" },
	{ "mon_thousands_sep",	1, LC_MONETARY,	KW_MON_THOUSANDS_SEP, "" },
	{ "mon_grouping",	1, LC_MONETARY,	KW_MON_GROUPING, "" },
	{ "positive_sign",	1, LC_MONETARY,	KW_POSITIVE_SIGN, "" },
	{ "negative_sign",	1, LC_MONETARY,	KW_NEGATIVE_SIGN, "" },

	{ "int_frac_digits",	0, LC_MONETARY,	KW_INT_FRAC_DIGITS, "" },
	{ "frac_digits",	0, LC_MONETARY,	KW_FRAC_DIGITS, "" },
	{ "p_cs_precedes",	0, LC_MONETARY,	KW_P_CS_PRECEDES, "" },
	{ "p_sep_by_space",	0, LC_MONETARY,	KW_P_SEP_BY_SPACE, "" },
	{ "n_cs_precedes",	0, LC_MONETARY,	KW_N_CS_PRECEDES, "" },
	{ "n_sep_by_space",	0, LC_MONETARY,	KW_N_SEP_BY_SPACE, "" },
	{ "p_sign_posn",	0, LC_MONETARY,	KW_P_SIGN_POSN, "" },
	{ "n_sign_posn",	0, LC_MONETARY,	KW_N_SIGN_POSN, "" },
	{ "int_p_cs_precedes",	0, LC_MONETARY,	KW_INT_P_CS_PRECEDES, "" },
	{ "int_p_sep_by_space",	0, LC_MONETARY,	KW_INT_P_SEP_BY_SPACE, "" },
	{ "int_n_cs_precedes",	0, LC_MONETARY,	KW_INT_N_CS_PRECEDES, "" },
	{ "int_n_sep_by_space",	0, LC_MONETARY,	KW_INT_N_SEP_BY_SPACE, "" },
	{ "int_p_sign_posn",	0, LC_MONETARY,	KW_INT_P_SIGN_POSN, "" },
	{ "int_n_sign_posn",	0, LC_MONETARY,	KW_INT_N_SIGN_POSN, "" },

	{ "d_t_fmt",		1, LC_TIME,	D_T_FMT, "" },
	{ "d_fmt",		1, LC_TIME,	D_FMT, "" },
	{ "t_fmt",		1, LC_TIME,	T_FMT, "" },
	{ "am_str",		1, LC_TIME,	AM_STR, "" },
	{ "pm_str",		1, LC_TIME,	PM_STR, "" },
	{ "t_fmt_ampm",		1, LC_TIME,	T_FMT_AMPM, "" },
	{ "day_1",		1, LC_TIME,	DAY_1, "" },
	{ "day_2",		1, LC_TIME,	DAY_2, "" },
	{ "day_3",		1, LC_TIME,	DAY_3, "" },
	{ "day_4",		1, LC_TIME,	DAY_4, "" },
	{ "day_5",		1, LC_TIME,	DAY_5, "" },
	{ "day_6",		1, LC_TIME,	DAY_6, "" },
	{ "day_7",		1, LC_TIME,	DAY_7, "" },
	{ "abday_1",		1, LC_TIME,	ABDAY_1, "" },
	{ "abday_2",		1, LC_TIME,	ABDAY_2, "" },
	{ "abday_3",		1, LC_TIME,	ABDAY_3, "" },
	{ "abday_4",		1, LC_TIME,	ABDAY_4, "" },
	{ "abday_5",		1, LC_TIME,	ABDAY_5, "" },
	{ "abday_6",		1, LC_TIME,	ABDAY_6, "" },
	{ "abday_7",		1, LC_TIME,	ABDAY_7, "" },
	{ "mon_1",		1, LC_TIME,	MON_1, "" },
	{ "mon_2",		1, LC_TIME,	MON_2, "" },
	{ "mon_3",		1, LC_TIME,	MON_3, "" },
	{ "mon_4",		1, LC_TIME,	MON_4, "" },
	{ "mon_5",		1, LC_TIME,	MON_5, "" },
	{ "mon_6",		1, LC_TIME,	MON_6, "" },
	{ "mon_7",		1, LC_TIME,	MON_7, "" },
	{ "mon_8",		1, LC_TIME,	MON_8, "" },
	{ "mon_9",		1, LC_TIME,	MON_9, "" },
	{ "mon_10",		1, LC_TIME,	MON_10, "" },
	{ "mon_11",		1, LC_TIME,	MON_11, "" },
	{ "mon_12",		1, LC_TIME,	MON_12, "" },
	{ "abmon_1",		1, LC_TIME,	ABMON_1, "" },
	{ "abmon_2",		1, LC_TIME,	ABMON_2, "" },
	{ "abmon_3",		1, LC_TIME,	ABMON_3, "" },
	{ "abmon_4",		1, LC_TIME,	ABMON_4, "" },
	{ "abmon_5",		1, LC_TIME,	ABMON_5, "" },
	{ "abmon_6",		1, LC_TIME,	ABMON_6, "" },
	{ "abmon_7",		1, LC_TIME,	ABMON_7, "" },
	{ "abmon_8",		1, LC_TIME,	ABMON_8, "" },
	{ "abmon_9",		1, LC_TIME,	ABMON_9, "" },
	{ "abmon_10",		1, LC_TIME,	ABMON_10, "" },
	{ "abmon_11",		1, LC_TIME,	ABMON_11, "" },
	{ "abmon_12",		1, LC_TIME,	ABMON_12, "" },
	{ "era",		1, LC_TIME,	ERA, "(unavailable)" },
	{ "era_d_fmt",		1, LC_TIME,	ERA_D_FMT, "(unavailable)" },
	{ "era_d_t_fmt",	1, LC_TIME,	ERA_D_T_FMT, "(unavailable)" },
	{ "era_t_fmt",		1, LC_TIME,	ERA_T_FMT, "(unavailable)" },
	{ "alt_digits",		1, LC_TIME,	ALT_DIGITS, "" },
	{ "d_md_order",		1, LC_TIME,	D_MD_ORDER,
	  "(FreeBSD only)"				},	/* local */

	{ "yesexpr",		1, LC_MESSAGES, YESEXPR, "" },
	{ "noexpr",		1, LC_MESSAGES, NOEXPR, "" },
	{ "yesstr",		1, LC_MESSAGES, YESSTR,
	  "(POSIX legacy)" },					/* compat */
	{ "nostr",		1, LC_MESSAGES, NOSTR,
	  "(POSIX legacy)" }					/* compat */

};
#define NKWINFO (sizeof(kwinfo)/sizeof(kwinfo[0]))

int
main(int argc, char *argv[])
{
	int	ch;
	int	tmp;

	while ((ch = getopt(argc, argv, "ackm")) != -1) {
		switch (ch) {
		case 'a':
			all_locales = 1;
			break;
		case 'c':
			prt_categories = 1;
			break;
		case 'k':
			prt_keywords = 1;
			break;
		case 'm':
			all_charmaps = 1;
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	/* validate arguments */
	if (all_locales && all_charmaps)
		usage();
	if ((all_locales || all_charmaps) && argc > 0) 
		usage();
	if ((all_locales || all_charmaps) && (prt_categories || prt_keywords))
		usage();
	if ((prt_categories || prt_keywords) && argc <= 0)
		usage();

	/* process '-a' */
	if (all_locales) {
		list_locales();
		exit(0);
	}

	/* process '-m' */
	if (all_charmaps) {
		list_charmaps();
		exit(0);
	}

	/* check for special case '-k list' */
	tmp = 0;
	if (prt_keywords && argc > 0)
		while (tmp < argc)
			if (strcasecmp(argv[tmp++], "list") == 0) {
				showkeywordslist();
				exit(0);
			}

	/* process '-c' and/or '-k' */
	if (prt_categories || prt_keywords || argc > 0) {
		setlocale(LC_ALL, "");
		while (argc > 0) {
			showdetails(*argv);
			argv++;
			argc--;
		}
		exit(0);
	}

	/* no arguments, show current locale state */
	showlocale();

	return (0);
}

void
usage(void)
{
	printf("Usage: locale [ -a | -m ]\n"
               "       locale [ -ck ] name ...\n");
	exit(1);
}

/*
 * Output information about all available locales
 *
 * XXX actually output of this function does not guarantee that locale
 *     is really available to application, since it can be broken or
 *     inconsistent thus setlocale() will fail.  Maybe add '-V' function to
 *     also validate these locales?
 */
void
list_locales(void)
{
	size_t i;

	init_locales_list();
	for (i = 0; i < locales->sl_cur; i++) {
		printf("%s\n", locales->sl_str[i]);
	}
}

/*
 * qsort() helper function
 */
static int
scmp(const void *s1, const void *s2)
{
	return strcmp(*(const char **)s1, *(const char **)s2);
}

/*
 * Output information about all available charmaps
 *
 * XXX this function is doing a task in hackish way, i.e. by scaning
 *     list of locales, spliting their codeset part and building list of
 *     them.
 */
void
list_charmaps(void)
{
	size_t i;
	char *s, *cs;
	StringList *charmaps;

	/* initialize StringList */
	charmaps = sl_init();
	if (charmaps == NULL)
		err(1, "could not allocate memory");

	/* fetch locales list */
	init_locales_list();

	/* split codesets and build their list */
	for (i = 0; i < locales->sl_cur; i++) {
		s = locales->sl_str[i];
		if ((cs = strchr(s, '.')) != NULL) {
			cs++;
			if (sl_find(charmaps, cs) == NULL)
				sl_add(charmaps, cs);
		}
	}

	/* add US-ASCII, if not yet added */
	if (sl_find(charmaps, "US-ASCII") == NULL)
		sl_add(charmaps, "US-ASCII");

	/* sort the list */
	qsort(charmaps->sl_str, charmaps->sl_cur, sizeof(char *), scmp);

	/* print results */
	for (i = 0; i < charmaps->sl_cur; i++) {
		printf("%s\n", charmaps->sl_str[i]);
	}
}

/*
 * Retrieve sorted list of system locales (or user locales, if PATH_LOCALE
 * environment variable is set)
 */
void
init_locales_list(void)
{
	DIR *dirp;
	struct dirent *dp;

	/* why call this function twice ? */
	if (locales != NULL)
		return;

	/* initialize StringList */
	locales = sl_init();
	if (locales == NULL)
		err(1, "could not allocate memory");

	/* get actual locales directory name */
	if (__detect_path_locale() != 0)
		err(1, "unable to find locales storage");

	/* open locales directory */
	dirp = opendir(_PathLocale);
	if (dirp == NULL)
		err(1, "could not open directory '%s'", _PathLocale);

	/* scan directory and store its contents except "." and ".." */
	while ((dp = readdir(dirp)) != NULL) {
		if (*(dp->d_name) == '.')
			continue;		/* exclude "." and ".." */
		sl_add(locales, strdup(dp->d_name));
	}
	closedir(dirp);

        /* make sure that 'POSIX' and 'C' locales are present in the list.
	 * POSIX 1003.1-2001 requires presence of 'POSIX' name only here, but
         * we also list 'C' for constistency
         */
	if (sl_find(locales, "POSIX") == NULL)
		sl_add(locales, "POSIX");

	if (sl_find(locales, "C") == NULL)
		sl_add(locales, "C");

	/* make output nicer, sort the list */
	qsort(locales->sl_str, locales->sl_cur, sizeof(char *), scmp);
}

/*
 * Show current locale status, depending on environment variables
 */
void
showlocale(void)
{
	size_t	i;
	const char *lang, *vval, *eval, *lcall;

	(void)setlocale(LC_ALL, "");

	if ((lang = getenv("LANG")) == NULL)
		lang = "";
	/* XXX: if LANG is null, then set it to "C" to get implied values? */
	if ((lcall = getenv("LC_ALL")) == NULL)
		lcall = "";
	if (!*lcall || !strcmp(lang, lcall))
		printf("LANG=%s\n", lang);
	else
		printf("LANG=\"%s\"\n", lcall);

	for (i = 0; i < NLCINFO; i++) {
		vval = setlocale(lcinfo[i].id, NULL);
		eval = getenv(lcinfo[i].name);
		if (eval != NULL && !strcmp(eval, vval)
				&& strcmp(lang, vval)) {
			/*
			 * Appropriate environment variable set, its value
			 * is valid and not overriden by LC_ALL
			 *
			 * XXX: possible side effect: if both LANG and
			 * overriden environment variable are set into same
			 * value, then it'll be assumed as 'implied'
			 */
			printf("%s=%s\n", lcinfo[i].name, vval);
		} else {
			printf("%s=\"%s\"\n", lcinfo[i].name, vval);
		}
	}

	printf("LC_ALL=%s\n", lcall);
}

/*
 * keyword value lookup helper (via localeconv())
 */
char *
kwval_lconv(int id)
{
	struct lconv *lc;
	char *rval;

	rval = NULL;
	lc = localeconv();
	switch (id) {
		case KW_GROUPING:
			rval = lc->grouping;
			break;
		case KW_INT_CURR_SYMBOL:
			rval = lc->int_curr_symbol;
			break;
		case KW_CURRENCY_SYMBOL:
			rval = lc->currency_symbol;
			break;
		case KW_MON_DECIMAL_POINT:
			rval = lc->mon_decimal_point;
			break;
		case KW_MON_THOUSANDS_SEP:
			rval = lc->mon_thousands_sep;
			break;
		case KW_MON_GROUPING:
			rval = lc->mon_grouping;
			break;
		case KW_POSITIVE_SIGN:
			rval = lc->positive_sign;
			break;
		case KW_NEGATIVE_SIGN:
			rval = lc->negative_sign;
			break;
		case KW_INT_FRAC_DIGITS:
			rval = &(lc->int_frac_digits);
			break;
		case KW_FRAC_DIGITS:
			rval = &(lc->frac_digits);
			break;
		case KW_P_CS_PRECEDES:
			rval = &(lc->p_cs_precedes);
			break;
		case KW_P_SEP_BY_SPACE:
			rval = &(lc->p_sep_by_space);
			break;
		case KW_N_CS_PRECEDES:
			rval = &(lc->n_cs_precedes);
			break;
		case KW_N_SEP_BY_SPACE:
			rval = &(lc->n_sep_by_space);
			break;
		case KW_P_SIGN_POSN:
			rval = &(lc->p_sign_posn);
			break;
		case KW_N_SIGN_POSN:
			rval = &(lc->n_sign_posn);
			break;
		case KW_INT_P_CS_PRECEDES:
			rval = &(lc->int_p_cs_precedes);
			break;
		case KW_INT_P_SEP_BY_SPACE:
			rval = &(lc->int_p_sep_by_space);
			break;
		case KW_INT_N_CS_PRECEDES:
			rval = &(lc->int_n_cs_precedes);
			break;
		case KW_INT_N_SEP_BY_SPACE:
			rval = &(lc->int_n_sep_by_space);
			break;
		case KW_INT_P_SIGN_POSN:
			rval = &(lc->int_p_sign_posn);
			break;
		case KW_INT_N_SIGN_POSN:
			rval = &(lc->int_n_sign_posn);
			break;
		default:
			break;
	}
	return (rval);
}

/*
 * keyword value and properties lookup
 */
int
kwval_lookup(char *kwname, char **kwval, int *cat, int *isstr)
{
	int	rval;
	size_t	i;

	rval = 0;
	for (i = 0; i < NKWINFO; i++) {
		if (strcasecmp(kwname, kwinfo[i].name) == 0) {
			rval = 1;
			*cat = kwinfo[i].catid;
			*isstr = kwinfo[i].isstr;
			if (kwinfo[i].value_ref < KW_ZERO) {
				*kwval = nl_langinfo(kwinfo[i].value_ref);
			} else {
				*kwval = kwval_lconv(kwinfo[i].value_ref);
			}
			break;
		}
	}

	return (rval);
}

/*
 * Show details about requested keyword according to '-k' and/or '-c'
 * command line options specified.
 */
void
showdetails(char *kw)
{
	int	isstr, cat, tmpval;
	char	*kwval;

	if (kwval_lookup(kw, &kwval, &cat, &isstr) == 0) {
		/*
		 * invalid keyword specified.
		 * XXX: any actions?
		 */
		return;
	}

	if (prt_categories) {
		printf("%s\n", lookup_localecat(cat));
	}

	if (prt_keywords) {
		if (isstr) {
			printf("%s=\"%s\"\n", kw, kwval);
		} else {
			tmpval = (char) *kwval;
			printf("%s=%d\n", kw, tmpval);
		}
	}

	if (!prt_categories && !prt_keywords) {
		if (isstr) {
			printf("%s\n", kwval);
		} else {
			tmpval = (char) *kwval;
			printf("%d\n", tmpval);
		}
	}
}

/*
 * Convert locale category id into string
 */
const char *
lookup_localecat(int cat)
{
	size_t	i;

	for (i = 0; i < NLCINFO; i++)
		if (lcinfo[i].id == cat) {
			return (lcinfo[i].name);
		}
	return ("UNKNOWN");
}

/*
 * Show list of keywords
 */
void
showkeywordslist(void)
{
	size_t	i;

#define FMT "%-20s %-12s %-7s %-20s\n"

	printf("List of available keywords\n\n");
	printf(FMT, "Keyword", "Category", "Type", "Comment");
	printf("-------------------- ------------ ------- --------------------\n");
	for (i = 0; i < NKWINFO; i++) {
		printf(FMT,
			kwinfo[i].name,
			lookup_localecat(kwinfo[i].catid),
			(kwinfo[i].isstr == 0) ? "number" : "string",
			kwinfo[i].comment);
	}
}
OpenPOWER on IntegriCloud