2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2002, 2003 Alexey Zelkin <phantom@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * XXX: implement missing era_* (LC_TIME) keywords (require libc &
31 * nl_langinfo(3) extensions)
33 * XXX: correctly handle reserved 'charmap' keyword and '-m' option (require
34 * localedef(1) implementation). Currently it's handled via
35 * nl_langinfo(CODESET).
38 #include <sys/param.h>
39 #include <sys/types.h>
50 #include <stringlist.h>
52 #include "setlocale.h"
54 /* Local prototypes */
55 char *format_grouping(char *);
56 void init_locales_list(void);
57 void list_charmaps(void);
58 void list_locales(void);
59 const char *lookup_localecat(int);
60 char *kwval_lconv(int);
61 int kwval_lookup(const char *, char **, int *, int *, int *);
62 int showdetails(const char *);
63 void showkeywordslist(char *substring);
64 void showlocale(void);
67 /* Global variables */
68 static StringList *locales = NULL;
70 static int all_locales = 0;
71 static int all_charmaps = 0;
72 static int prt_categories = 0;
73 static int prt_keywords = 0;
75 static const struct _lcinfo {
79 { "LC_CTYPE", LC_CTYPE },
80 { "LC_COLLATE", LC_COLLATE },
81 { "LC_TIME", LC_TIME },
82 { "LC_NUMERIC", LC_NUMERIC },
83 { "LC_MONETARY", LC_MONETARY },
84 { "LC_MESSAGES", LC_MESSAGES }
86 #define NLCINFO nitems(lcinfo)
88 /* ids for values not referenced by nl_langinfo() */
106 KW_INT_P_CS_PRECEDES,
107 KW_INT_P_SEP_BY_SPACE,
108 KW_INT_N_CS_PRECEDES,
109 KW_INT_N_SEP_BY_SPACE,
131 static const struct _kwinfo {
134 int catid; /* LC_* */
139 { "charmap", TYPE_STR, LC_CTYPE, SRC_LINFO,
140 CODESET, "" }, /* hack */
142 /* LC_MONETARY - POSIX */
143 { "int_curr_symbol", TYPE_STR, LC_MONETARY, SRC_LCONV,
144 KW_INT_CURR_SYMBOL, "" },
145 { "currency_symbol", TYPE_STR, LC_MONETARY, SRC_LCONV,
146 KW_CURRENCY_SYMBOL, "" },
147 { "mon_decimal_point", TYPE_STR, LC_MONETARY, SRC_LCONV,
148 KW_MON_DECIMAL_POINT, "" },
149 { "mon_thousands_sep", TYPE_STR, LC_MONETARY, SRC_LCONV,
150 KW_MON_THOUSANDS_SEP, "" },
151 { "mon_grouping", TYPE_UNQ, LC_MONETARY, SRC_LCONV,
152 KW_MON_GROUPING, "" },
153 { "positive_sign", TYPE_STR, LC_MONETARY, SRC_LCONV,
154 KW_POSITIVE_SIGN, "" },
155 { "negative_sign", TYPE_STR, LC_MONETARY, SRC_LCONV,
156 KW_NEGATIVE_SIGN, "" },
157 { "int_frac_digits", TYPE_NUM, LC_MONETARY, SRC_LCONV,
158 KW_INT_FRAC_DIGITS, "" },
159 { "frac_digits", TYPE_NUM, LC_MONETARY, SRC_LCONV,
160 KW_FRAC_DIGITS, "" },
161 { "p_cs_precedes", TYPE_NUM, LC_MONETARY, SRC_LCONV,
162 KW_P_CS_PRECEDES, "" },
163 { "p_sep_by_space", TYPE_NUM, LC_MONETARY, SRC_LCONV,
164 KW_P_SEP_BY_SPACE, "" },
165 { "n_cs_precedes", TYPE_NUM, LC_MONETARY, SRC_LCONV,
166 KW_N_CS_PRECEDES, "" },
167 { "n_sep_by_space", TYPE_NUM, LC_MONETARY, SRC_LCONV,
168 KW_N_SEP_BY_SPACE, "" },
169 { "p_sign_posn", TYPE_NUM, LC_MONETARY, SRC_LCONV,
170 KW_P_SIGN_POSN, "" },
171 { "n_sign_posn", TYPE_NUM, LC_MONETARY, SRC_LCONV,
172 KW_N_SIGN_POSN, "" },
173 { "int_p_cs_precedes", TYPE_NUM, LC_MONETARY, SRC_LCONV,
174 KW_INT_P_CS_PRECEDES, "" },
175 { "int_p_sep_by_space", TYPE_NUM, LC_MONETARY, SRC_LCONV,
176 KW_INT_P_SEP_BY_SPACE, "" },
177 { "int_n_cs_precedes", TYPE_NUM, LC_MONETARY, SRC_LCONV,
178 KW_INT_N_CS_PRECEDES, "" },
179 { "int_n_sep_by_space", TYPE_NUM, LC_MONETARY, SRC_LCONV,
180 KW_INT_N_SEP_BY_SPACE, "" },
181 { "int_p_sign_posn", TYPE_NUM, LC_MONETARY, SRC_LCONV,
182 KW_INT_P_SIGN_POSN, "" },
183 { "int_n_sign_posn", TYPE_NUM, LC_MONETARY, SRC_LCONV,
184 KW_INT_N_SIGN_POSN, "" },
186 /* LC_NUMERIC - POSIX */
187 { "decimal_point", TYPE_STR, LC_NUMERIC, SRC_LINFO,
189 { "thousands_sep", TYPE_STR, LC_NUMERIC, SRC_LINFO,
191 { "grouping", TYPE_UNQ, LC_NUMERIC, SRC_LCONV,
193 /* LC_NUMERIC - local additions */
194 { "radixchar", TYPE_STR, LC_NUMERIC, SRC_LINFO,
195 RADIXCHAR, "Same as decimal_point (FreeBSD only)" }, /* compat */
196 { "thousep", TYPE_STR, LC_NUMERIC, SRC_LINFO,
197 THOUSEP, "Same as thousands_sep (FreeBSD only)" }, /* compat */
199 /* LC_TIME - POSIX */
200 { "abday", TYPE_STR, LC_TIME, SRC_LTIME,
202 { "day", TYPE_STR, LC_TIME, SRC_LTIME,
204 { "abmon", TYPE_STR, LC_TIME, SRC_LTIME,
206 { "mon", TYPE_STR, LC_TIME, SRC_LTIME,
208 { "d_t_fmt", TYPE_STR, LC_TIME, SRC_LINFO,
210 { "d_fmt", TYPE_STR, LC_TIME, SRC_LINFO,
212 { "t_fmt", TYPE_STR, LC_TIME, SRC_LINFO,
214 { "am_pm", TYPE_STR, LC_TIME, SRC_LTIME,
216 { "t_fmt_ampm", TYPE_STR, LC_TIME, SRC_LINFO,
218 { "era", TYPE_UNQ, LC_TIME, SRC_LINFO,
219 ERA, "(unavailable)" },
220 { "era_d_fmt", TYPE_STR, LC_TIME, SRC_LINFO,
221 ERA_D_FMT, "(unavailable)" },
222 { "era_d_t_fmt", TYPE_STR, LC_TIME, SRC_LINFO,
223 ERA_D_T_FMT, "(unavailable)" },
224 { "era_t_fmt", TYPE_STR, LC_TIME, SRC_LINFO,
225 ERA_T_FMT, "(unavailable)" },
226 { "alt_digits", TYPE_UNQ, LC_TIME, SRC_LINFO,
228 /* LC_TIME - local additions */
229 { "abday_1", TYPE_STR, LC_TIME, SRC_LINFO,
230 ABDAY_1, "(FreeBSD only)" },
231 { "abday_2", TYPE_STR, LC_TIME, SRC_LINFO,
232 ABDAY_2, "(FreeBSD only)" },
233 { "abday_3", TYPE_STR, LC_TIME, SRC_LINFO,
234 ABDAY_3, "(FreeBSD only)" },
235 { "abday_4", TYPE_STR, LC_TIME, SRC_LINFO,
236 ABDAY_4, "(FreeBSD only)" },
237 { "abday_5", TYPE_STR, LC_TIME, SRC_LINFO,
238 ABDAY_5, "(FreeBSD only)" },
239 { "abday_6", TYPE_STR, LC_TIME, SRC_LINFO,
240 ABDAY_6, "(FreeBSD only)" },
241 { "abday_7", TYPE_STR, LC_TIME, SRC_LINFO,
242 ABDAY_7, "(FreeBSD only)" },
243 { "day_1", TYPE_STR, LC_TIME, SRC_LINFO,
244 DAY_1, "(FreeBSD only)" },
245 { "day_2", TYPE_STR, LC_TIME, SRC_LINFO,
246 DAY_2, "(FreeBSD only)" },
247 { "day_3", TYPE_STR, LC_TIME, SRC_LINFO,
248 DAY_3, "(FreeBSD only)" },
249 { "day_4", TYPE_STR, LC_TIME, SRC_LINFO,
250 DAY_4, "(FreeBSD only)" },
251 { "day_5", TYPE_STR, LC_TIME, SRC_LINFO,
252 DAY_5, "(FreeBSD only)" },
253 { "day_6", TYPE_STR, LC_TIME, SRC_LINFO,
254 DAY_6, "(FreeBSD only)" },
255 { "day_7", TYPE_STR, LC_TIME, SRC_LINFO,
256 DAY_7, "(FreeBSD only)" },
257 { "abmon_1", TYPE_STR, LC_TIME, SRC_LINFO,
258 ABMON_1, "(FreeBSD only)" },
259 { "abmon_2", TYPE_STR, LC_TIME, SRC_LINFO,
260 ABMON_2, "(FreeBSD only)" },
261 { "abmon_3", TYPE_STR, LC_TIME, SRC_LINFO,
262 ABMON_3, "(FreeBSD only)" },
263 { "abmon_4", TYPE_STR, LC_TIME, SRC_LINFO,
264 ABMON_4, "(FreeBSD only)" },
265 { "abmon_5", TYPE_STR, LC_TIME, SRC_LINFO,
266 ABMON_5, "(FreeBSD only)" },
267 { "abmon_6", TYPE_STR, LC_TIME, SRC_LINFO,
268 ABMON_6, "(FreeBSD only)" },
269 { "abmon_7", TYPE_STR, LC_TIME, SRC_LINFO,
270 ABMON_7, "(FreeBSD only)" },
271 { "abmon_8", TYPE_STR, LC_TIME, SRC_LINFO,
272 ABMON_8, "(FreeBSD only)" },
273 { "abmon_9", TYPE_STR, LC_TIME, SRC_LINFO,
274 ABMON_9, "(FreeBSD only)" },
275 { "abmon_10", TYPE_STR, LC_TIME, SRC_LINFO,
276 ABMON_10, "(FreeBSD only)" },
277 { "abmon_11", TYPE_STR, LC_TIME, SRC_LINFO,
278 ABMON_11, "(FreeBSD only)" },
279 { "abmon_12", TYPE_STR, LC_TIME, SRC_LINFO,
280 ABMON_12, "(FreeBSD only)" },
281 { "mon_1", TYPE_STR, LC_TIME, SRC_LINFO,
282 MON_1, "(FreeBSD only)" },
283 { "mon_2", TYPE_STR, LC_TIME, SRC_LINFO,
284 MON_2, "(FreeBSD only)" },
285 { "mon_3", TYPE_STR, LC_TIME, SRC_LINFO,
286 MON_3, "(FreeBSD only)" },
287 { "mon_4", TYPE_STR, LC_TIME, SRC_LINFO,
288 MON_4, "(FreeBSD only)" },
289 { "mon_5", TYPE_STR, LC_TIME, SRC_LINFO,
290 MON_5, "(FreeBSD only)" },
291 { "mon_6", TYPE_STR, LC_TIME, SRC_LINFO,
292 MON_6, "(FreeBSD only)" },
293 { "mon_7", TYPE_STR, LC_TIME, SRC_LINFO,
294 MON_7, "(FreeBSD only)" },
295 { "mon_8", TYPE_STR, LC_TIME, SRC_LINFO,
296 MON_8, "(FreeBSD only)" },
297 { "mon_9", TYPE_STR, LC_TIME, SRC_LINFO,
298 MON_9, "(FreeBSD only)" },
299 { "mon_10", TYPE_STR, LC_TIME, SRC_LINFO,
300 MON_10, "(FreeBSD only)" },
301 { "mon_11", TYPE_STR, LC_TIME, SRC_LINFO,
302 MON_11, "(FreeBSD only)" },
303 { "mon_12", TYPE_STR, LC_TIME, SRC_LINFO,
304 MON_12, "(FreeBSD only)" },
305 { "altmon_1", TYPE_STR, LC_TIME, SRC_LINFO,
306 ALTMON_1, "(FreeBSD only)" },
307 { "altmon_2", TYPE_STR, LC_TIME, SRC_LINFO,
308 ALTMON_2, "(FreeBSD only)" },
309 { "altmon_3", TYPE_STR, LC_TIME, SRC_LINFO,
310 ALTMON_3, "(FreeBSD only)" },
311 { "altmon_4", TYPE_STR, LC_TIME, SRC_LINFO,
312 ALTMON_4, "(FreeBSD only)" },
313 { "altmon_5", TYPE_STR, LC_TIME, SRC_LINFO,
314 ALTMON_5, "(FreeBSD only)" },
315 { "altmon_6", TYPE_STR, LC_TIME, SRC_LINFO,
316 ALTMON_6, "(FreeBSD only)" },
317 { "altmon_7", TYPE_STR, LC_TIME, SRC_LINFO,
318 ALTMON_7, "(FreeBSD only)" },
319 { "altmon_8", TYPE_STR, LC_TIME, SRC_LINFO,
320 ALTMON_8, "(FreeBSD only)" },
321 { "altmon_9", TYPE_STR, LC_TIME, SRC_LINFO,
322 ALTMON_9, "(FreeBSD only)" },
323 { "altmon_10", TYPE_STR, LC_TIME, SRC_LINFO,
324 ALTMON_10, "(FreeBSD only)" },
325 { "altmon_11", TYPE_STR, LC_TIME, SRC_LINFO,
326 ALTMON_11, "(FreeBSD only)" },
327 { "altmon_12", TYPE_STR, LC_TIME, SRC_LINFO,
328 ALTMON_12, "(FreeBSD only)" },
329 { "am_str", TYPE_STR, LC_TIME, SRC_LINFO,
330 AM_STR, "(FreeBSD only)" },
331 { "pm_str", TYPE_STR, LC_TIME, SRC_LINFO,
332 PM_STR, "(FreeBSD only)" },
333 { "d_md_order", TYPE_STR, LC_TIME, SRC_LINFO,
334 D_MD_ORDER, "(FreeBSD only)" }, /* local */
336 /* LC_MESSAGES - POSIX */
337 { "yesexpr", TYPE_STR, LC_MESSAGES, SRC_LINFO,
339 { "noexpr", TYPE_STR, LC_MESSAGES, SRC_LINFO,
341 /* LC_MESSAGES - local additions */
342 { "yesstr", TYPE_STR, LC_MESSAGES, SRC_LINFO,
343 YESSTR, "(POSIX legacy)" }, /* compat */
344 { "nostr", TYPE_STR, LC_MESSAGES, SRC_LINFO,
345 NOSTR, "(POSIX legacy)" } /* compat */
348 #define NKWINFO (nitems(kwinfo))
350 static const char *boguslocales[] = { "UTF-8" };
351 #define NBOGUS (nitems(boguslocales))
354 main(int argc, char *argv[])
359 while ((ch = getopt(argc, argv, "ackms:")) != -1) {
380 /* validate arguments */
381 if (all_locales && all_charmaps)
383 if ((all_locales || all_charmaps) && argc > 0)
385 if ((all_locales || all_charmaps) && (prt_categories || prt_keywords))
400 /* check for special case '-k list' */
402 if (prt_keywords && argc > 0)
404 if (strcasecmp(argv[tmp++], "list") == 0) {
405 showkeywordslist(argv[tmp]);
409 /* process '-c', '-k', or command line arguments. */
410 if (prt_categories || prt_keywords || argc > 0) {
411 if (prt_keywords || argc > 0)
412 setlocale(LC_ALL, "");
415 if (showdetails(*argv) != 0)
422 for (i = 0; i < nitems(kwinfo); i++)
423 showdetails(kwinfo[i].name);
428 /* no arguments, show current locale state */
437 printf("Usage: locale [ -a | -m ]\n"
438 " locale -k list [prefix]\n"
439 " locale [ -ck ] [keyword ...]\n");
444 * Output information about all available locales
446 * XXX actually output of this function does not guarantee that locale
447 * is really available to application, since it can be broken or
448 * inconsistent thus setlocale() will fail. Maybe add '-V' function to
449 * also validate these locales?
457 for (i = 0; i < locales->sl_cur; i++) {
458 printf("%s\n", locales->sl_str[i]);
463 * qsort() helper function
466 scmp(const void *s1, const void *s2)
468 return strcmp(*(const char * const *)s1, *(const char * const *)s2);
472 * Output information about all available charmaps
474 * XXX this function is doing a task in hackish way, i.e. by scaning
475 * list of locales, spliting their codeset part and building list of
483 StringList *charmaps;
485 /* initialize StringList */
486 charmaps = sl_init();
487 if (charmaps == NULL)
488 err(1, "could not allocate memory");
490 /* fetch locales list */
493 /* split codesets and build their list */
494 for (i = 0; i < locales->sl_cur; i++) {
495 s = locales->sl_str[i];
496 if ((cs = strchr(s, '.')) != NULL) {
498 if (sl_find(charmaps, cs) == NULL)
499 sl_add(charmaps, cs);
503 /* add US-ASCII, if not yet added */
504 if (sl_find(charmaps, "US-ASCII") == NULL)
505 sl_add(charmaps, strdup("US-ASCII"));
508 qsort(charmaps->sl_str, charmaps->sl_cur, sizeof(char *), scmp);
511 for (i = 0; i < charmaps->sl_cur; i++) {
512 printf("%s\n", charmaps->sl_str[i]);
517 * Retrieve sorted list of system locales (or user locales, if PATH_LOCALE
518 * environment variable is set)
521 init_locales_list(void)
528 /* why call this function twice ? */
532 /* initialize StringList */
535 err(1, "could not allocate memory");
537 /* get actual locales directory name */
538 if (__detect_path_locale() != 0)
539 err(1, "unable to find locales storage");
541 /* open locales directory */
542 dirp = opendir(_PathLocale);
544 err(1, "could not open directory '%s'", _PathLocale);
546 /* scan directory and store its contents except "." and ".." */
547 while ((dp = readdir(dirp)) != NULL) {
548 if (*(dp->d_name) == '.')
549 continue; /* exclude "." and ".." */
550 for (bogus = i = 0; i < NBOGUS; i++)
551 if (strncmp(dp->d_name, boguslocales[i],
552 strlen(boguslocales[i])) == 0)
555 sl_add(locales, strdup(dp->d_name));
559 /* make sure that 'POSIX' and 'C' locales are present in the list.
560 * POSIX 1003.1-2001 requires presence of 'POSIX' name only here, but
561 * we also list 'C' for constistency
563 if (sl_find(locales, "POSIX") == NULL)
564 sl_add(locales, strdup("POSIX"));
566 if (sl_find(locales, "C") == NULL)
567 sl_add(locales, strdup("C"));
569 /* make output nicer, sort the list */
570 qsort(locales->sl_str, locales->sl_cur, sizeof(char *), scmp);
574 * Show current locale status, depending on environment variables
580 const char *lang, *vval, *eval;
582 setlocale(LC_ALL, "");
584 lang = getenv("LANG");
588 printf("LANG=%s\n", lang);
589 /* XXX: if LANG is null, then set it to "C" to get implied values? */
591 for (i = 0; i < NLCINFO; i++) {
592 vval = setlocale(lcinfo[i].id, NULL);
593 eval = getenv(lcinfo[i].name);
594 if (eval != NULL && !strcmp(eval, vval)
595 && strcmp(lang, vval)) {
597 * Appropriate environment variable set, its value
598 * is valid and not overridden by LC_ALL
600 * XXX: possible side effect: if both LANG and
601 * overridden environment variable are set into same
602 * value, then it'll be assumed as 'implied'
604 printf("%s=%s\n", lcinfo[i].name, vval);
606 printf("%s=\"%s\"\n", lcinfo[i].name, vval);
610 vval = getenv("LC_ALL");
614 printf("LC_ALL=%s\n", vval);
618 format_grouping(char *binary)
620 static char rval[64];
626 * XXX This check will need to be modified if/when localeconv() is
629 if (*binary == CHAR_MAX)
634 for (cp = binary; *cp != '\0'; ++cp) {
637 break; /* garbage input */
639 len = snprintf(&rval[roff], sizeof(rval) - roff, "%u;", *cp);
640 if (len < 0 || (unsigned)len >= sizeof(rval) - roff)
641 break; /* insufficient space for output */
644 break; /* special termination */
647 /* Truncate at the last successfully snprintf()ed semicolon. */
649 rval[roff - 1] = '\0';
655 * keyword value lookup helper for values accessible via localeconv()
667 rval = format_grouping(lc->grouping);
669 case KW_INT_CURR_SYMBOL:
670 rval = lc->int_curr_symbol;
672 case KW_CURRENCY_SYMBOL:
673 rval = lc->currency_symbol;
675 case KW_MON_DECIMAL_POINT:
676 rval = lc->mon_decimal_point;
678 case KW_MON_THOUSANDS_SEP:
679 rval = lc->mon_thousands_sep;
681 case KW_MON_GROUPING:
682 rval = format_grouping(lc->mon_grouping);
684 case KW_POSITIVE_SIGN:
685 rval = lc->positive_sign;
687 case KW_NEGATIVE_SIGN:
688 rval = lc->negative_sign;
690 case KW_INT_FRAC_DIGITS:
691 rval = &(lc->int_frac_digits);
694 rval = &(lc->frac_digits);
696 case KW_P_CS_PRECEDES:
697 rval = &(lc->p_cs_precedes);
699 case KW_P_SEP_BY_SPACE:
700 rval = &(lc->p_sep_by_space);
702 case KW_N_CS_PRECEDES:
703 rval = &(lc->n_cs_precedes);
705 case KW_N_SEP_BY_SPACE:
706 rval = &(lc->n_sep_by_space);
709 rval = &(lc->p_sign_posn);
712 rval = &(lc->n_sign_posn);
714 case KW_INT_P_CS_PRECEDES:
715 rval = &(lc->int_p_cs_precedes);
717 case KW_INT_P_SEP_BY_SPACE:
718 rval = &(lc->int_p_sep_by_space);
720 case KW_INT_N_CS_PRECEDES:
721 rval = &(lc->int_n_cs_precedes);
723 case KW_INT_N_SEP_BY_SPACE:
724 rval = &(lc->int_n_sep_by_space);
726 case KW_INT_P_SIGN_POSN:
727 rval = &(lc->int_p_sign_posn);
729 case KW_INT_N_SIGN_POSN:
730 rval = &(lc->int_n_sign_posn);
739 * keyword value lookup helper for LC_TIME keywords not accessible
740 * via nl_langinfo() or localeconv()
747 nl_item i, s_item = 0, e_item = 0;
767 if (asprintf(&rval, "%s;%s",
769 nl_langinfo(PM_STR)) == -1)
774 kwsbuf = sbuf_new_auto();
777 for (i = s_item; i <= e_item; i++) {
778 (void) sbuf_cat(kwsbuf, nl_langinfo(i));
780 (void) sbuf_cat(kwsbuf, ";");
782 (void) sbuf_finish(kwsbuf);
783 rval = strdup(sbuf_data(kwsbuf));
791 * keyword value and properties lookup
794 kwval_lookup(const char *kwname, char **kwval, int *cat, int *type, int *alloc)
798 static char nastr[3] = "-1";
802 for (i = 0; i < NKWINFO; i++) {
803 if (strcasecmp(kwname, kwinfo[i].name) == 0) {
805 *cat = kwinfo[i].catid;
806 *type = kwinfo[i].type;
807 switch (kwinfo[i].source) {
809 *kwval = nl_langinfo(kwinfo[i].value_ref);
812 *kwval = kwval_lconv(kwinfo[i].value_ref);
814 * XXX This check will need to be modified
815 * if/when localeconv() is fixed (PR172215).
817 if (**kwval == CHAR_MAX) {
818 if (*type == TYPE_NUM)
824 *kwval = kwval_ltime(kwinfo[i].value_ref);
836 * Show details about requested keyword according to '-k' and/or '-c'
837 * command line options specified.
840 showdetails(const char *kw)
842 int type, cat, tmpval, alloc;
845 if (kwval_lookup(kw, &kwval, &cat, &type, &alloc) == 0) {
846 /* Invalid keyword specified */
847 fprintf(stderr, "Unknown keyword: `%s'\n", kw);
851 if (prt_categories) {
853 printf("%-20s ", lookup_localecat(cat));
855 printf("%-20s\t%s\n", kw, lookup_localecat(cat));
861 tmpval = (char)*kwval;
862 printf("%s=%d\n", kw, tmpval);
865 printf("%s=\"%s\"\n", kw, kwval);
868 printf("%s=%s\n", kw, kwval);
873 if (!prt_categories && !prt_keywords) {
876 tmpval = (char)*kwval;
877 printf("%d\n", tmpval);
881 printf("%s\n", kwval);
893 * Convert locale category id into string
896 lookup_localecat(int cat)
900 for (i = 0; i < NLCINFO; i++)
901 if (lcinfo[i].id == cat) {
902 return (lcinfo[i].name);
908 * Show list of keywords
911 showkeywordslist(char *substring)
915 #define FMT "%-20s %-12s %-7s %-20s\n"
917 if (substring == NULL)
918 printf("List of available keywords\n\n");
920 printf("List of available keywords starting with '%s'\n\n",
922 printf(FMT, "Keyword", "Category", "Type", "Comment");
923 printf("-------------------- ------------ ------- --------------------\n");
924 for (i = 0; i < NKWINFO; i++) {
925 if (substring != NULL) {
926 if (strncmp(kwinfo[i].name, substring,
927 strlen(substring)) != 0)
932 lookup_localecat(kwinfo[i].catid),
933 (kwinfo[i].type == TYPE_NUM) ? "number" : "string",