2 * Copyright (c) 2002, 2003 Alexey Zelkin <phantom@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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/types.h>
46 #include <stringlist.h>
48 #include "setlocale.h"
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);
62 /* Global variables */
63 static StringList *locales = NULL;
67 int prt_categories = 0;
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 }
82 #define NLCINFO (sizeof(lcinfo)/sizeof(lcinfo[0]))
84 /* ids for values not referenced by nl_langinfo() */
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)
111 int isstr; /* true - string, false - number */
112 int catid; /* LC_* */
116 { "charmap", 1, LC_CTYPE, CODESET, "" }, /* hack */
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 */
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, "" },
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, "" },
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 */
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 */
221 #define NKWINFO (sizeof(kwinfo)/sizeof(kwinfo[0]))
223 const char *boguslocales[] = { "UTF-8" };
224 #define NBOGUS (sizeof(boguslocales)/sizeof(boguslocales[0]))
227 main(int argc, char *argv[])
232 while ((ch = getopt(argc, argv, "ackms:")) != -1) {
253 /* validate arguments */
254 if (all_locales && all_charmaps)
256 if ((all_locales || all_charmaps) && argc > 0)
258 if ((all_locales || all_charmaps) && (prt_categories || prt_keywords))
273 /* check for special case '-k list' */
275 if (prt_keywords && argc > 0)
277 if (strcasecmp(argv[tmp++], "list") == 0) {
278 showkeywordslist(argv[tmp]);
282 /* process '-c', '-k', or command line arguments. */
283 if (prt_categories || prt_keywords || argc > 0) {
285 setlocale(LC_ALL, "");
293 for (i = 0; i < sizeof (kwinfo) / sizeof (struct _kwinfo); i++)
294 showdetails ((char *)kwinfo [i].name);
299 /* no arguments, show current locale state */
308 printf("Usage: locale [ -a | -m ]\n"
309 " locale -k list [prefix]\n"
310 " locale [ -ck ] [keyword ...]\n");
315 * Output information about all available locales
317 * XXX actually output of this function does not guarantee that locale
318 * is really available to application, since it can be broken or
319 * inconsistent thus setlocale() will fail. Maybe add '-V' function to
320 * also validate these locales?
328 for (i = 0; i < locales->sl_cur; i++) {
329 printf("%s\n", locales->sl_str[i]);
334 * qsort() helper function
337 scmp(const void *s1, const void *s2)
339 return strcmp(*(const char **)s1, *(const char **)s2);
343 * Output information about all available charmaps
345 * XXX this function is doing a task in hackish way, i.e. by scaning
346 * list of locales, spliting their codeset part and building list of
354 StringList *charmaps;
356 /* initialize StringList */
357 charmaps = sl_init();
358 if (charmaps == NULL)
359 err(1, "could not allocate memory");
361 /* fetch locales list */
364 /* split codesets and build their list */
365 for (i = 0; i < locales->sl_cur; i++) {
366 s = locales->sl_str[i];
367 if ((cs = strchr(s, '.')) != NULL) {
369 if (sl_find(charmaps, cs) == NULL)
370 sl_add(charmaps, cs);
374 /* add US-ASCII, if not yet added */
375 if (sl_find(charmaps, "US-ASCII") == NULL)
376 sl_add(charmaps, "US-ASCII");
379 qsort(charmaps->sl_str, charmaps->sl_cur, sizeof(char *), scmp);
382 for (i = 0; i < charmaps->sl_cur; i++) {
383 printf("%s\n", charmaps->sl_str[i]);
388 * Retrieve sorted list of system locales (or user locales, if PATH_LOCALE
389 * environment variable is set)
392 init_locales_list(void)
399 /* why call this function twice ? */
403 /* initialize StringList */
406 err(1, "could not allocate memory");
408 /* get actual locales directory name */
409 if (__detect_path_locale() != 0)
410 err(1, "unable to find locales storage");
412 /* open locales directory */
413 dirp = opendir(_PathLocale);
415 err(1, "could not open directory '%s'", _PathLocale);
417 /* scan directory and store its contents except "." and ".." */
418 while ((dp = readdir(dirp)) != NULL) {
419 if (*(dp->d_name) == '.')
420 continue; /* exclude "." and ".." */
421 for (bogus = i = 0; i < NBOGUS; i++)
422 if (strncmp(dp->d_name, boguslocales[i],
423 strlen(boguslocales[i])) == 0)
426 sl_add(locales, strdup(dp->d_name));
430 /* make sure that 'POSIX' and 'C' locales are present in the list.
431 * POSIX 1003.1-2001 requires presence of 'POSIX' name only here, but
432 * we also list 'C' for constistency
434 if (sl_find(locales, "POSIX") == NULL)
435 sl_add(locales, "POSIX");
437 if (sl_find(locales, "C") == NULL)
438 sl_add(locales, "C");
440 /* make output nicer, sort the list */
441 qsort(locales->sl_str, locales->sl_cur, sizeof(char *), scmp);
445 * Show current locale status, depending on environment variables
451 const char *lang, *vval, *eval;
453 setlocale(LC_ALL, "");
455 lang = getenv("LANG");
459 printf("LANG=%s\n", lang);
460 /* XXX: if LANG is null, then set it to "C" to get implied values? */
462 for (i = 0; i < NLCINFO; i++) {
463 vval = setlocale(lcinfo[i].id, NULL);
464 eval = getenv(lcinfo[i].name);
465 if (eval != NULL && !strcmp(eval, vval)
466 && strcmp(lang, vval)) {
468 * Appropriate environment variable set, its value
469 * is valid and not overriden by LC_ALL
471 * XXX: possible side effect: if both LANG and
472 * overriden environment variable are set into same
473 * value, then it'll be assumed as 'implied'
475 printf("%s=%s\n", lcinfo[i].name, vval);
477 printf("%s=\"%s\"\n", lcinfo[i].name, vval);
481 vval = getenv("LC_ALL");
485 printf("LC_ALL=%s\n", vval);
489 * keyword value lookup helper (via localeconv())
503 case KW_INT_CURR_SYMBOL:
504 rval = lc->int_curr_symbol;
506 case KW_CURRENCY_SYMBOL:
507 rval = lc->currency_symbol;
509 case KW_MON_DECIMAL_POINT:
510 rval = lc->mon_decimal_point;
512 case KW_MON_THOUSANDS_SEP:
513 rval = lc->mon_thousands_sep;
515 case KW_MON_GROUPING:
516 rval = lc->mon_grouping;
518 case KW_POSITIVE_SIGN:
519 rval = lc->positive_sign;
521 case KW_NEGATIVE_SIGN:
522 rval = lc->negative_sign;
524 case KW_INT_FRAC_DIGITS:
525 rval = &(lc->int_frac_digits);
528 rval = &(lc->frac_digits);
530 case KW_P_CS_PRECEDES:
531 rval = &(lc->p_cs_precedes);
533 case KW_P_SEP_BY_SPACE:
534 rval = &(lc->p_sep_by_space);
536 case KW_N_CS_PRECEDES:
537 rval = &(lc->n_cs_precedes);
539 case KW_N_SEP_BY_SPACE:
540 rval = &(lc->n_sep_by_space);
543 rval = &(lc->p_sign_posn);
546 rval = &(lc->n_sign_posn);
548 case KW_INT_P_CS_PRECEDES:
549 rval = &(lc->int_p_cs_precedes);
551 case KW_INT_P_SEP_BY_SPACE:
552 rval = &(lc->int_p_sep_by_space);
554 case KW_INT_N_CS_PRECEDES:
555 rval = &(lc->int_n_cs_precedes);
557 case KW_INT_N_SEP_BY_SPACE:
558 rval = &(lc->int_n_sep_by_space);
560 case KW_INT_P_SIGN_POSN:
561 rval = &(lc->int_p_sign_posn);
563 case KW_INT_N_SIGN_POSN:
564 rval = &(lc->int_n_sign_posn);
573 * keyword value and properties lookup
576 kwval_lookup(char *kwname, char **kwval, int *cat, int *isstr)
582 for (i = 0; i < NKWINFO; i++) {
583 if (strcasecmp(kwname, kwinfo[i].name) == 0) {
585 *cat = kwinfo[i].catid;
586 *isstr = kwinfo[i].isstr;
587 if (kwinfo[i].value_ref < KW_ZERO) {
588 *kwval = nl_langinfo(kwinfo[i].value_ref);
590 *kwval = kwval_lconv(kwinfo[i].value_ref);
600 * Show details about requested keyword according to '-k' and/or '-c'
601 * command line options specified.
604 showdetails(char *kw)
606 int isstr, cat, tmpval;
609 if (kwval_lookup(kw, &kwval, &cat, &isstr) == 0) {
611 * invalid keyword specified.
614 fprintf(stderr, "Unknown keyword: `%s'\n", kw);
618 if (prt_categories) {
620 printf("%-20s ", lookup_localecat(cat));
622 printf("%-20s\t%s\n", kw, lookup_localecat(cat));
627 printf("%s=\"%s\"\n", kw, kwval);
629 tmpval = (char) *kwval;
630 printf("%s=%d\n", kw, tmpval);
634 if (!prt_categories && !prt_keywords) {
636 printf("%s\n", kwval);
638 tmpval = (char) *kwval;
639 printf("%d\n", tmpval);
645 * Convert locale category id into string
648 lookup_localecat(int cat)
652 for (i = 0; i < NLCINFO; i++)
653 if (lcinfo[i].id == cat) {
654 return (lcinfo[i].name);
660 * Show list of keywords
663 showkeywordslist(char *substring)
667 #define FMT "%-20s %-12s %-7s %-20s\n"
669 if (substring == NULL)
670 printf("List of available keywords\n\n");
672 printf("List of available keywords starting with '%s'\n\n",
674 printf(FMT, "Keyword", "Category", "Type", "Comment");
675 printf("-------------------- ------------ ------- --------------------\n");
676 for (i = 0; i < NKWINFO; i++) {
677 if (substring != NULL) {
678 if (strncmp(kwinfo[i].name, substring,
679 strlen(substring)) != 0)
684 lookup_localecat(kwinfo[i].catid),
685 (kwinfo[i].isstr == 0) ? "number" : "string",