1 /* $NetBSD: locale.c,v 1.6 2009/01/02 00:20:23 tnozaki Exp $ */
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
28 * FreeBSD: src/usr.bin/locale/locale.c,v 1.10 2003/06/26 11:05:56 phantom Exp
31 #include <sys/cdefs.h>
32 #if defined(LIBC_SCCS) && !defined(lint)
33 __RCSID("$NetBSD: locale.c,v 1.6 2009/01/02 00:20:23 tnozaki Exp $");
34 #endif /* LIBC_SCCS and not lint */
37 * XXX: implement missing era_* (LC_TIME) keywords (require libc &
38 * nl_langinfo(3) extensions)
40 * XXX: correctly handle reserved 'charmap' keyword and '-m' option (require
41 * localedef(1) implementation). Currently it's handled via
42 * nl_langinfo(CODESET).
45 #include <sys/types.h>
56 #include <stringlist.h>
60 #include "citrus_namespace.h"
61 #include "citrus_region.h"
62 #include "citrus_lookup.h"
64 #include "setlocale_local.h"
66 /* Local prototypes */
67 void init_locales_list(void);
68 void init_locales_list_alias(void);
69 void list_charmaps(void);
70 void list_locales(void);
71 const char *lookup_localecat(int);
72 char *kwval_lconv(int);
73 int kwval_lookup(char *, char **, int *, int *);
74 void showdetails(char *);
75 void showkeywordslist(void);
76 void showlocale(void);
79 /* Global variables */
80 static StringList
*locales
= NULL
;
84 int prt_categories
= 0;
92 { "LC_CTYPE", LC_CTYPE
},
93 { "LC_COLLATE", LC_COLLATE
},
94 { "LC_TIME", LC_TIME
},
95 { "LC_NUMERIC", LC_NUMERIC
},
96 { "LC_MONETARY", LC_MONETARY
},
97 { "LC_MESSAGES", LC_MESSAGES
}
99 #define NLCINFO (sizeof(lcinfo)/sizeof(lcinfo[0]))
101 /* ids for values not referenced by nl_langinfo() */
102 #define KW_ZERO 10000
103 #define KW_GROUPING (KW_ZERO+1)
104 #define KW_INT_CURR_SYMBOL (KW_ZERO+2)
105 #define KW_CURRENCY_SYMBOL (KW_ZERO+3)
106 #define KW_MON_DECIMAL_POINT (KW_ZERO+4)
107 #define KW_MON_THOUSANDS_SEP (KW_ZERO+5)
108 #define KW_MON_GROUPING (KW_ZERO+6)
109 #define KW_POSITIVE_SIGN (KW_ZERO+7)
110 #define KW_NEGATIVE_SIGN (KW_ZERO+8)
111 #define KW_INT_FRAC_DIGITS (KW_ZERO+9)
112 #define KW_FRAC_DIGITS (KW_ZERO+10)
113 #define KW_P_CS_PRECEDES (KW_ZERO+11)
114 #define KW_P_SEP_BY_SPACE (KW_ZERO+12)
115 #define KW_N_CS_PRECEDES (KW_ZERO+13)
116 #define KW_N_SEP_BY_SPACE (KW_ZERO+14)
117 #define KW_P_SIGN_POSN (KW_ZERO+15)
118 #define KW_N_SIGN_POSN (KW_ZERO+16)
119 #define KW_INT_P_CS_PRECEDES (KW_ZERO+17)
120 #define KW_INT_P_SEP_BY_SPACE (KW_ZERO+18)
121 #define KW_INT_N_CS_PRECEDES (KW_ZERO+19)
122 #define KW_INT_N_SEP_BY_SPACE (KW_ZERO+20)
123 #define KW_INT_P_SIGN_POSN (KW_ZERO+21)
124 #define KW_INT_N_SIGN_POSN (KW_ZERO+22)
128 int isstr
; /* true - string, false - number */
129 int catid
; /* LC_* */
133 { "charmap", 1, LC_CTYPE
, CODESET
, "" }, /* hack */
135 { "decimal_point", 1, LC_NUMERIC
, RADIXCHAR
, "" },
136 { "thousands_sep", 1, LC_NUMERIC
, THOUSEP
, "" },
137 { "grouping", 1, LC_NUMERIC
, KW_GROUPING
, "" },
138 { "radixchar", 1, LC_NUMERIC
, RADIXCHAR
,
139 "Same as decimal_point (BSD only)" }, /* compat */
140 { "thousep", 1, LC_NUMERIC
, THOUSEP
,
141 "Same as thousands_sep (BSD only)" }, /* compat */
143 { "int_curr_symbol", 1, LC_MONETARY
, KW_INT_CURR_SYMBOL
, "" },
144 { "currency_symbol", 1, LC_MONETARY
, KW_CURRENCY_SYMBOL
, "" },
145 { "mon_decimal_point", 1, LC_MONETARY
, KW_MON_DECIMAL_POINT
, "" },
146 { "mon_thousands_sep", 1, LC_MONETARY
, KW_MON_THOUSANDS_SEP
, "" },
147 { "mon_grouping", 1, LC_MONETARY
, KW_MON_GROUPING
, "" },
148 { "positive_sign", 1, LC_MONETARY
, KW_POSITIVE_SIGN
, "" },
149 { "negative_sign", 1, LC_MONETARY
, KW_NEGATIVE_SIGN
, "" },
151 { "int_frac_digits", 0, LC_MONETARY
, KW_INT_FRAC_DIGITS
, "" },
152 { "frac_digits", 0, LC_MONETARY
, KW_FRAC_DIGITS
, "" },
153 { "p_cs_precedes", 0, LC_MONETARY
, KW_P_CS_PRECEDES
, "" },
154 { "p_sep_by_space", 0, LC_MONETARY
, KW_P_SEP_BY_SPACE
, "" },
155 { "n_cs_precedes", 0, LC_MONETARY
, KW_N_CS_PRECEDES
, "" },
156 { "n_sep_by_space", 0, LC_MONETARY
, KW_N_SEP_BY_SPACE
, "" },
157 { "p_sign_posn", 0, LC_MONETARY
, KW_P_SIGN_POSN
, "" },
158 { "n_sign_posn", 0, LC_MONETARY
, KW_N_SIGN_POSN
, "" },
159 { "int_p_cs_precedes", 0, LC_MONETARY
, KW_INT_P_CS_PRECEDES
, "" },
160 { "int_p_sep_by_space", 0, LC_MONETARY
, KW_INT_P_SEP_BY_SPACE
, "" },
161 { "int_n_cs_precedes", 0, LC_MONETARY
, KW_INT_N_CS_PRECEDES
, "" },
162 { "int_n_sep_by_space", 0, LC_MONETARY
, KW_INT_N_SEP_BY_SPACE
, "" },
163 { "int_p_sign_posn", 0, LC_MONETARY
, KW_INT_P_SIGN_POSN
, "" },
164 { "int_n_sign_posn", 0, LC_MONETARY
, KW_INT_N_SIGN_POSN
, "" },
166 { "d_t_fmt", 1, LC_TIME
, D_T_FMT
, "" },
167 { "d_fmt", 1, LC_TIME
, D_FMT
, "" },
168 { "t_fmt", 1, LC_TIME
, T_FMT
, "" },
169 { "am_str", 1, LC_TIME
, AM_STR
, "" },
170 { "pm_str", 1, LC_TIME
, PM_STR
, "" },
171 { "t_fmt_ampm", 1, LC_TIME
, T_FMT_AMPM
, "" },
172 { "day_1", 1, LC_TIME
, DAY_1
, "" },
173 { "day_2", 1, LC_TIME
, DAY_2
, "" },
174 { "day_3", 1, LC_TIME
, DAY_3
, "" },
175 { "day_4", 1, LC_TIME
, DAY_4
, "" },
176 { "day_5", 1, LC_TIME
, DAY_5
, "" },
177 { "day_6", 1, LC_TIME
, DAY_6
, "" },
178 { "day_7", 1, LC_TIME
, DAY_7
, "" },
179 { "abday_1", 1, LC_TIME
, ABDAY_1
, "" },
180 { "abday_2", 1, LC_TIME
, ABDAY_2
, "" },
181 { "abday_3", 1, LC_TIME
, ABDAY_3
, "" },
182 { "abday_4", 1, LC_TIME
, ABDAY_4
, "" },
183 { "abday_5", 1, LC_TIME
, ABDAY_5
, "" },
184 { "abday_6", 1, LC_TIME
, ABDAY_6
, "" },
185 { "abday_7", 1, LC_TIME
, ABDAY_7
, "" },
186 { "mon_1", 1, LC_TIME
, MON_1
, "" },
187 { "mon_2", 1, LC_TIME
, MON_2
, "" },
188 { "mon_3", 1, LC_TIME
, MON_3
, "" },
189 { "mon_4", 1, LC_TIME
, MON_4
, "" },
190 { "mon_5", 1, LC_TIME
, MON_5
, "" },
191 { "mon_6", 1, LC_TIME
, MON_6
, "" },
192 { "mon_7", 1, LC_TIME
, MON_7
, "" },
193 { "mon_8", 1, LC_TIME
, MON_8
, "" },
194 { "mon_9", 1, LC_TIME
, MON_9
, "" },
195 { "mon_10", 1, LC_TIME
, MON_10
, "" },
196 { "mon_11", 1, LC_TIME
, MON_11
, "" },
197 { "mon_12", 1, LC_TIME
, MON_12
, "" },
198 { "abmon_1", 1, LC_TIME
, ABMON_1
, "" },
199 { "abmon_2", 1, LC_TIME
, ABMON_2
, "" },
200 { "abmon_3", 1, LC_TIME
, ABMON_3
, "" },
201 { "abmon_4", 1, LC_TIME
, ABMON_4
, "" },
202 { "abmon_5", 1, LC_TIME
, ABMON_5
, "" },
203 { "abmon_6", 1, LC_TIME
, ABMON_6
, "" },
204 { "abmon_7", 1, LC_TIME
, ABMON_7
, "" },
205 { "abmon_8", 1, LC_TIME
, ABMON_8
, "" },
206 { "abmon_9", 1, LC_TIME
, ABMON_9
, "" },
207 { "abmon_10", 1, LC_TIME
, ABMON_10
, "" },
208 { "abmon_11", 1, LC_TIME
, ABMON_11
, "" },
209 { "abmon_12", 1, LC_TIME
, ABMON_12
, "" },
210 { "era", 1, LC_TIME
, ERA
, "(unavailable)" },
211 { "era_d_fmt", 1, LC_TIME
, ERA_D_FMT
, "(unavailable)" },
212 { "era_d_t_fmt", 1, LC_TIME
, ERA_D_T_FMT
, "(unavailable)" },
213 { "era_t_fmt", 1, LC_TIME
, ERA_T_FMT
, "(unavailable)" },
214 { "alt_digits", 1, LC_TIME
, ALT_DIGITS
, "" },
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 */
224 #define NKWINFO (sizeof(kwinfo)/sizeof(kwinfo[0]))
227 main(int argc
, char *argv
[])
232 while ((ch
= getopt(argc
, argv
, "ackm")) != -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
))
260 if ((prt_categories
|| prt_keywords
) && argc
<= 0)
275 /* check for special case '-k list' */
277 if (prt_keywords
&& argc
> 0)
279 if (strcasecmp(argv
[tmp
++], "list") == 0) {
284 /* process '-c' and/or '-k' */
285 if (prt_categories
|| prt_keywords
|| argc
> 0) {
286 setlocale(LC_ALL
, "");
295 /* no arguments, show current locale state */
304 printf("usage: locale [ -a | -m ]\n"
305 " locale [ -ck ] name ...\n");
310 * Output information about all available locales
312 * XXX actually output of this function does not guarantee that locale
313 * is really available to application, since it can be broken or
314 * inconsistent thus setlocale() will fail. Maybe add '-V' function to
315 * also validate these locales?
323 for (i
= 0; i
< locales
->sl_cur
; i
++) {
324 printf("%s\n", locales
->sl_str
[i
]);
329 * qsort() helper function
332 scmp(const void *s1
, const void *s2
)
334 return strcmp(*(const char **)s1
, *(const char **)s2
);
338 * Output information about all available charmaps
340 * XXX this function is doing a task in hackish way, i.e. by scaning
341 * list of locales, spliting their codeset part and building list of
349 StringList
*charmaps
;
351 /* initialize StringList */
352 charmaps
= sl_init();
353 if (charmaps
== NULL
)
354 err(1, "could not allocate memory");
356 /* fetch locales list */
359 /* split codesets and build their list */
360 for (i
= 0; i
< locales
->sl_cur
; i
++) {
361 s
= locales
->sl_str
[i
];
362 if ((cs
= strchr(s
, '.')) != NULL
) {
364 if (sl_find(charmaps
, cs
) == NULL
)
365 sl_add(charmaps
, cs
);
369 /* add US-ASCII, if not yet added */
370 if (sl_find(charmaps
, "US-ASCII") == NULL
)
371 sl_add(charmaps
, "US-ASCII");
374 qsort(charmaps
->sl_str
, charmaps
->sl_cur
, sizeof(char *), scmp
);
377 for (i
= 0; i
< charmaps
->sl_cur
; i
++) {
378 printf("%s\n", charmaps
->sl_str
[i
]);
383 * Retrieve sorted list of system locales (or user locales, if PATH_LOCALE
384 * environment variable is set)
387 init_locales_list(void)
393 /* why call this function twice ? */
397 /* initialize StringList */
400 err(1, "could not allocate memory");
402 /* get actual locales directory name */
403 setlocale(LC_CTYPE
, "C");
404 if (_PathLocale
== NULL
)
405 errx(1, "unable to find locales storage");
407 /* open locales directory */
408 dirp
= opendir(_PathLocale
);
410 err(1, "could not open directory '%s'", _PathLocale
);
412 /* scan directory and store its contents except "." and ".." */
413 while ((dp
= readdir(dirp
)) != NULL
) {
414 /* exclude "." and "..", _LOCALE_ALIAS_NAME */
415 if ((dp
->d_name
[0] != '.' || (dp
->d_name
[1] != '\0' &&
416 (dp
->d_name
[1] != '.' || dp
->d_name
[2] != '\0'))) &&
417 strcmp(_LOCALE_ALIAS_NAME
, dp
->d_name
) != 0) {
418 s
= strdup(dp
->d_name
);
420 err(1, "could not allocate memory");
426 /* make sure that 'POSIX' and 'C' locales are present in the list.
427 * POSIX 1003.1-2001 requires presence of 'POSIX' name only here, but
428 * we also list 'C' for constistency
430 if (sl_find(locales
, "POSIX") == NULL
)
431 sl_add(locales
, "POSIX");
433 if (sl_find(locales
, "C") == NULL
)
434 sl_add(locales
, "C");
436 init_locales_list_alias();
438 /* make output nicer, sort the list */
439 qsort(locales
->sl_str
, locales
->sl_cur
, sizeof(char *), scmp
);
443 init_locales_list_alias(void)
445 char aliaspath
[PATH_MAX
];
447 struct _lookup
*hlookup
;
448 struct _region key
, dat
;
455 _DIAGASSERT(locales
!= NULL
);
456 _DIAGASSERT(_PathLocale
!= NULL
);
458 (void)snprintf(aliaspath
, sizeof(aliaspath
),
459 "%s/" _LOCALE_ALIAS_NAME
, _PathLocale
);
462 if (_lookup_seq_open(&hlookup
, aliaspath
,
463 _LOOKUP_CASE_SENSITIVE
) == 0) {
464 while (_lookup_seq_next(hlookup
, &key
, &dat
) == 0) {
465 n
= _region_size((const struct _region
*)&key
);
466 s
= _region_head((const struct _region
*)&key
);
467 for (t
= s
; n
> 0 && *s
!= '/'; --n
, ++s
);
469 fp
= fopen(aliaspath
, "r");
471 while ((s
= fgetln(fp
, &n
)) != NULL
) {
473 if (*s
== '#' || *s
== '\n')
475 for (t
= s
; n
> 0 && strchr("/ \t\n", *s
) == NULL
;
481 err(1, "could not allocate memory");
484 if (sl_find(locales
, s
) == NULL
)
490 _lookup_seq_close(hlookup
);
498 * Show current locale status, depending on environment variables
504 const char *lang
, *vval
, *eval
;
506 setlocale(LC_ALL
, "");
508 lang
= getenv("LANG");
512 printf("LANG=\"%s\"\n", lang
);
513 /* XXX: if LANG is null, then set it to "C" to get implied values? */
515 for (i
= 0; i
< NLCINFO
; i
++) {
516 vval
= setlocale(lcinfo
[i
].id
, NULL
);
517 eval
= getenv(lcinfo
[i
].name
);
518 if (eval
!= NULL
&& !strcmp(eval
, vval
)
519 && strcmp(lang
, vval
)) {
521 * Appropriate environment variable set, its value
522 * is valid and not overriden by LC_ALL
524 * XXX: possible side effect: if both LANG and
525 * overriden environment variable are set into same
526 * value, then it'll be assumed as 'implied'
528 printf("%s=\"%s\"\n", lcinfo
[i
].name
, vval
);
530 printf("%s=\"%s\"\n", lcinfo
[i
].name
, vval
);
534 vval
= getenv("LC_ALL");
538 printf("LC_ALL=\"%s\"\n", vval
);
542 * keyword value lookup helper (via localeconv())
556 case KW_INT_CURR_SYMBOL
:
557 rval
= lc
->int_curr_symbol
;
559 case KW_CURRENCY_SYMBOL
:
560 rval
= lc
->currency_symbol
;
562 case KW_MON_DECIMAL_POINT
:
563 rval
= lc
->mon_decimal_point
;
565 case KW_MON_THOUSANDS_SEP
:
566 rval
= lc
->mon_thousands_sep
;
568 case KW_MON_GROUPING
:
569 rval
= lc
->mon_grouping
;
571 case KW_POSITIVE_SIGN
:
572 rval
= lc
->positive_sign
;
574 case KW_NEGATIVE_SIGN
:
575 rval
= lc
->negative_sign
;
577 case KW_INT_FRAC_DIGITS
:
578 rval
= &(lc
->int_frac_digits
);
581 rval
= &(lc
->frac_digits
);
583 case KW_P_CS_PRECEDES
:
584 rval
= &(lc
->p_cs_precedes
);
586 case KW_P_SEP_BY_SPACE
:
587 rval
= &(lc
->p_sep_by_space
);
589 case KW_N_CS_PRECEDES
:
590 rval
= &(lc
->n_cs_precedes
);
592 case KW_N_SEP_BY_SPACE
:
593 rval
= &(lc
->n_sep_by_space
);
596 rval
= &(lc
->p_sign_posn
);
599 rval
= &(lc
->n_sign_posn
);
601 case KW_INT_P_CS_PRECEDES
:
602 rval
= &(lc
->int_p_cs_precedes
);
604 case KW_INT_P_SEP_BY_SPACE
:
605 rval
= &(lc
->int_p_sep_by_space
);
607 case KW_INT_N_CS_PRECEDES
:
608 rval
= &(lc
->int_n_cs_precedes
);
610 case KW_INT_N_SEP_BY_SPACE
:
611 rval
= &(lc
->int_n_sep_by_space
);
613 case KW_INT_P_SIGN_POSN
:
614 rval
= &(lc
->int_p_sign_posn
);
616 case KW_INT_N_SIGN_POSN
:
617 rval
= &(lc
->int_n_sign_posn
);
626 * keyword value and properties lookup
629 kwval_lookup(char *kwname
, char **kwval
, int *cat
, int *isstr
)
635 for (i
= 0; i
< NKWINFO
; i
++) {
636 if (strcasecmp(kwname
, kwinfo
[i
].name
) == 0) {
638 *cat
= kwinfo
[i
].catid
;
639 *isstr
= kwinfo
[i
].isstr
;
640 if (kwinfo
[i
].value_ref
< KW_ZERO
) {
641 *kwval
= nl_langinfo(kwinfo
[i
].value_ref
);
643 *kwval
= kwval_lconv(kwinfo
[i
].value_ref
);
653 * Show details about requested keyword according to '-k' and/or '-c'
654 * command line options specified.
657 showdetails(char *kw
)
659 int isstr
, cat
, tmpval
;
662 if (kwval_lookup(kw
, &kwval
, &cat
, &isstr
) == 0) {
664 * invalid keyword specified.
670 if (prt_categories
) {
671 printf("%s\n", lookup_localecat(cat
));
676 printf("%s=\"%s\"\n", kw
, kwval
);
678 tmpval
= (char) *kwval
;
679 printf("%s=%d\n", kw
, tmpval
);
683 if (!prt_categories
&& !prt_keywords
) {
685 printf("%s\n", kwval
);
687 tmpval
= (char) *kwval
;
688 printf("%d\n", tmpval
);
694 * Convert locale category id into string
697 lookup_localecat(int cat
)
701 for (i
= 0; i
< NLCINFO
; i
++)
702 if (lcinfo
[i
].id
== cat
) {
703 return (lcinfo
[i
].name
);
709 * Show list of keywords
712 showkeywordslist(void)
716 #define FMT "%-20s %-12s %-7s %-20s\n"
718 printf("List of available keywords\n\n");
719 printf(FMT
, "Keyword", "Category", "Type", "Comment");
720 printf("-------------------- ------------ ------- --------------------\n");
721 for (i
= 0; i
< NKWINFO
; i
++) {
724 lookup_localecat(kwinfo
[i
].catid
),
725 (kwinfo
[i
].isstr
== 0) ? "number" : "string",