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
26 * FreeBSD: src/usr.bin/locale/locale.c,v 1.10 2003/06/26 11:05:56 phantom Exp
27 * $NetBSD: locale.c,v 1.5 2006/02/16 19:19:49 tnozaki Exp $
28 * $DragonFly: src/usr.bin/locale/locale.c,v 1.2 2007/09/20 07:40:51 hasso Exp $
32 * XXX: implement missing era_* (LC_TIME) keywords (require libc &
33 * nl_langinfo(3) extensions)
35 * XXX: correctly handle reserved 'charmap' keyword and '-m' option (require
36 * localedef(1) implementation). Currently it's handled via
37 * nl_langinfo(CODESET).
40 #include <sys/types.h>
51 #include <stringlist.h>
54 /* Local prototypes */
55 void init_locales_list(void);
56 void list_charmaps(void);
57 void list_locales(void);
58 const char *lookup_localecat(int);
59 char *kwval_lconv(int);
60 int kwval_lookup(char *, char **, int *, int *);
61 void showdetails(char *);
62 void showkeywordslist(void);
63 void showlocale(void);
66 /* Global variables */
67 static StringList
*locales
= NULL
;
71 int prt_categories
= 0;
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 (sizeof(lcinfo)/sizeof(lcinfo[0]))
88 /* ids for values not referenced by nl_langinfo() */
90 #define KW_GROUPING (KW_ZERO+1)
91 #define KW_INT_CURR_SYMBOL (KW_ZERO+2)
92 #define KW_CURRENCY_SYMBOL (KW_ZERO+3)
93 #define KW_MON_DECIMAL_POINT (KW_ZERO+4)
94 #define KW_MON_THOUSANDS_SEP (KW_ZERO+5)
95 #define KW_MON_GROUPING (KW_ZERO+6)
96 #define KW_POSITIVE_SIGN (KW_ZERO+7)
97 #define KW_NEGATIVE_SIGN (KW_ZERO+8)
98 #define KW_INT_FRAC_DIGITS (KW_ZERO+9)
99 #define KW_FRAC_DIGITS (KW_ZERO+10)
100 #define KW_P_CS_PRECEDES (KW_ZERO+11)
101 #define KW_P_SEP_BY_SPACE (KW_ZERO+12)
102 #define KW_N_CS_PRECEDES (KW_ZERO+13)
103 #define KW_N_SEP_BY_SPACE (KW_ZERO+14)
104 #define KW_P_SIGN_POSN (KW_ZERO+15)
105 #define KW_N_SIGN_POSN (KW_ZERO+16)
106 #define KW_INT_P_CS_PRECEDES (KW_ZERO+17)
107 #define KW_INT_P_SEP_BY_SPACE (KW_ZERO+18)
108 #define KW_INT_N_CS_PRECEDES (KW_ZERO+19)
109 #define KW_INT_N_SEP_BY_SPACE (KW_ZERO+20)
110 #define KW_INT_P_SIGN_POSN (KW_ZERO+21)
111 #define KW_INT_N_SIGN_POSN (KW_ZERO+22)
115 int isstr
; /* true - string, false - number */
116 int catid
; /* LC_* */
120 { "charmap", 1, LC_CTYPE
, CODESET
, "" }, /* hack */
122 { "decimal_point", 1, LC_NUMERIC
, RADIXCHAR
, "" },
123 { "thousands_sep", 1, LC_NUMERIC
, THOUSEP
, "" },
124 { "grouping", 1, LC_NUMERIC
, KW_GROUPING
, "" },
125 { "radixchar", 1, LC_NUMERIC
, RADIXCHAR
,
126 "Same as decimal_point (BSD only)" }, /* compat */
127 { "thousep", 1, LC_NUMERIC
, THOUSEP
,
128 "Same as thousands_sep (BSD only)" }, /* compat */
130 { "int_curr_symbol", 1, LC_MONETARY
, KW_INT_CURR_SYMBOL
, "" },
131 { "currency_symbol", 1, LC_MONETARY
, KW_CURRENCY_SYMBOL
, "" },
132 { "mon_decimal_point", 1, LC_MONETARY
, KW_MON_DECIMAL_POINT
, "" },
133 { "mon_thousands_sep", 1, LC_MONETARY
, KW_MON_THOUSANDS_SEP
, "" },
134 { "mon_grouping", 1, LC_MONETARY
, KW_MON_GROUPING
, "" },
135 { "positive_sign", 1, LC_MONETARY
, KW_POSITIVE_SIGN
, "" },
136 { "negative_sign", 1, LC_MONETARY
, KW_NEGATIVE_SIGN
, "" },
138 { "int_frac_digits", 0, LC_MONETARY
, KW_INT_FRAC_DIGITS
, "" },
139 { "frac_digits", 0, LC_MONETARY
, KW_FRAC_DIGITS
, "" },
140 { "p_cs_precedes", 0, LC_MONETARY
, KW_P_CS_PRECEDES
, "" },
141 { "p_sep_by_space", 0, LC_MONETARY
, KW_P_SEP_BY_SPACE
, "" },
142 { "n_cs_precedes", 0, LC_MONETARY
, KW_N_CS_PRECEDES
, "" },
143 { "n_sep_by_space", 0, LC_MONETARY
, KW_N_SEP_BY_SPACE
, "" },
144 { "p_sign_posn", 0, LC_MONETARY
, KW_P_SIGN_POSN
, "" },
145 { "n_sign_posn", 0, LC_MONETARY
, KW_N_SIGN_POSN
, "" },
146 { "int_p_cs_precedes", 0, LC_MONETARY
, KW_INT_P_CS_PRECEDES
, "" },
147 { "int_p_sep_by_space", 0, LC_MONETARY
, KW_INT_P_SEP_BY_SPACE
, "" },
148 { "int_n_cs_precedes", 0, LC_MONETARY
, KW_INT_N_CS_PRECEDES
, "" },
149 { "int_n_sep_by_space", 0, LC_MONETARY
, KW_INT_N_SEP_BY_SPACE
, "" },
150 { "int_p_sign_posn", 0, LC_MONETARY
, KW_INT_P_SIGN_POSN
, "" },
151 { "int_n_sign_posn", 0, LC_MONETARY
, KW_INT_N_SIGN_POSN
, "" },
153 { "d_t_fmt", 1, LC_TIME
, D_T_FMT
, "" },
154 { "d_fmt", 1, LC_TIME
, D_FMT
, "" },
155 { "t_fmt", 1, LC_TIME
, T_FMT
, "" },
156 { "am_str", 1, LC_TIME
, AM_STR
, "" },
157 { "pm_str", 1, LC_TIME
, PM_STR
, "" },
158 { "t_fmt_ampm", 1, LC_TIME
, T_FMT_AMPM
, "" },
159 { "day_1", 1, LC_TIME
, DAY_1
, "" },
160 { "day_2", 1, LC_TIME
, DAY_2
, "" },
161 { "day_3", 1, LC_TIME
, DAY_3
, "" },
162 { "day_4", 1, LC_TIME
, DAY_4
, "" },
163 { "day_5", 1, LC_TIME
, DAY_5
, "" },
164 { "day_6", 1, LC_TIME
, DAY_6
, "" },
165 { "day_7", 1, LC_TIME
, DAY_7
, "" },
166 { "abday_1", 1, LC_TIME
, ABDAY_1
, "" },
167 { "abday_2", 1, LC_TIME
, ABDAY_2
, "" },
168 { "abday_3", 1, LC_TIME
, ABDAY_3
, "" },
169 { "abday_4", 1, LC_TIME
, ABDAY_4
, "" },
170 { "abday_5", 1, LC_TIME
, ABDAY_5
, "" },
171 { "abday_6", 1, LC_TIME
, ABDAY_6
, "" },
172 { "abday_7", 1, LC_TIME
, ABDAY_7
, "" },
173 { "mon_1", 1, LC_TIME
, MON_1
, "" },
174 { "mon_2", 1, LC_TIME
, MON_2
, "" },
175 { "mon_3", 1, LC_TIME
, MON_3
, "" },
176 { "mon_4", 1, LC_TIME
, MON_4
, "" },
177 { "mon_5", 1, LC_TIME
, MON_5
, "" },
178 { "mon_6", 1, LC_TIME
, MON_6
, "" },
179 { "mon_7", 1, LC_TIME
, MON_7
, "" },
180 { "mon_8", 1, LC_TIME
, MON_8
, "" },
181 { "mon_9", 1, LC_TIME
, MON_9
, "" },
182 { "mon_10", 1, LC_TIME
, MON_10
, "" },
183 { "mon_11", 1, LC_TIME
, MON_11
, "" },
184 { "mon_12", 1, LC_TIME
, MON_12
, "" },
185 { "abmon_1", 1, LC_TIME
, ABMON_1
, "" },
186 { "abmon_2", 1, LC_TIME
, ABMON_2
, "" },
187 { "abmon_3", 1, LC_TIME
, ABMON_3
, "" },
188 { "abmon_4", 1, LC_TIME
, ABMON_4
, "" },
189 { "abmon_5", 1, LC_TIME
, ABMON_5
, "" },
190 { "abmon_6", 1, LC_TIME
, ABMON_6
, "" },
191 { "abmon_7", 1, LC_TIME
, ABMON_7
, "" },
192 { "abmon_8", 1, LC_TIME
, ABMON_8
, "" },
193 { "abmon_9", 1, LC_TIME
, ABMON_9
, "" },
194 { "abmon_10", 1, LC_TIME
, ABMON_10
, "" },
195 { "abmon_11", 1, LC_TIME
, ABMON_11
, "" },
196 { "abmon_12", 1, LC_TIME
, ABMON_12
, "" },
197 { "era", 1, LC_TIME
, ERA
, "(unavailable)" },
198 { "era_d_fmt", 1, LC_TIME
, ERA_D_FMT
, "(unavailable)" },
199 { "era_d_t_fmt", 1, LC_TIME
, ERA_D_T_FMT
, "(unavailable)" },
200 { "era_t_fmt", 1, LC_TIME
, ERA_T_FMT
, "(unavailable)" },
201 { "alt_digits", 1, LC_TIME
, ALT_DIGITS
, "" },
203 { "yesexpr", 1, LC_MESSAGES
, YESEXPR
, "" },
204 { "noexpr", 1, LC_MESSAGES
, NOEXPR
, "" },
205 { "yesstr", 1, LC_MESSAGES
, YESSTR
,
206 "(POSIX legacy)" }, /* compat */
207 { "nostr", 1, LC_MESSAGES
, NOSTR
,
208 "(POSIX legacy)" } /* compat */
211 #define NKWINFO (sizeof(kwinfo)/sizeof(kwinfo[0]))
214 main(int argc
, char *argv
[])
219 while ((ch
= getopt(argc
, argv
, "ackm")) != -1) {
240 /* validate arguments */
241 if (all_locales
&& all_charmaps
)
243 if ((all_locales
|| all_charmaps
) && argc
> 0)
245 if ((all_locales
|| all_charmaps
) && (prt_categories
|| prt_keywords
))
247 if ((prt_categories
|| prt_keywords
) && argc
<= 0)
262 /* check for special case '-k list' */
264 if (prt_keywords
&& argc
> 0)
266 if (strcasecmp(argv
[tmp
++], "list") == 0) {
271 /* process '-c' and/or '-k' */
272 if (prt_categories
|| prt_keywords
|| argc
> 0) {
273 setlocale(LC_ALL
, "");
282 /* no arguments, show current locale state */
291 printf("usage: locale [ -a | -m ]\n"
292 " locale [ -ck ] keyword ...\n");
297 * Output information about all available locales
299 * XXX actually output of this function does not guarantee that locale
300 * is really available to application, since it can be broken or
301 * inconsistent thus setlocale() will fail. Maybe add '-V' function to
302 * also validate these locales?
310 for (i
= 0; i
< locales
->sl_cur
; i
++) {
311 printf("%s\n", locales
->sl_str
[i
]);
316 * qsort() helper function
319 scmp(const void *s1
, const void *s2
)
321 return strcmp((const char *)s1
, (const char *)s2
);
325 * Output information about all available charmaps
327 * XXX this function is doing a task in hackish way, i.e. by scaning
328 * list of locales, spliting their codeset part and building list of
336 StringList
*charmaps
;
338 /* initialize StringList */
339 charmaps
= sl_init();
340 if (charmaps
== NULL
)
341 err(1, "could not allocate memory");
343 /* fetch locales list */
346 /* split codesets and build their list */
347 for (i
= 0; i
< locales
->sl_cur
; i
++) {
348 s
= locales
->sl_str
[i
];
349 if ((cs
= strchr(s
, '.')) != NULL
) {
351 if (sl_find(charmaps
, cs
) == NULL
)
352 sl_add(charmaps
, cs
);
356 /* add US-ASCII, if not yet added */
357 if (sl_find(charmaps
, "US-ASCII") == NULL
)
358 sl_add(charmaps
, "US-ASCII");
361 qsort(charmaps
->sl_str
, charmaps
->sl_cur
, sizeof(char *), scmp
);
364 for (i
= 0; i
< charmaps
->sl_cur
; i
++) {
365 printf("%s\n", charmaps
->sl_str
[i
]);
370 * Retrieve sorted list of system locales (or user locales, if PATH_LOCALE
371 * environment variable is set)
374 init_locales_list(void)
381 /* why call this function twice ? */
385 /* initialize StringList */
388 err(1, "could not allocate memory");
390 /* get actual locales directory name */
391 setlocale(LC_CTYPE
, "C");
393 localedir
= getenv("PATH_LOCALE");
394 if (localedir
== NULL
)
395 localedir
= _PATH_LOCALE
;
397 /* open locales directory */
398 dirp
= opendir(localedir
);
400 err(1, "could not open directory '%s'", localedir
);
402 /* scan directory and store its contents except "." and ".." */
403 while ((dp
= readdir(dirp
)) != NULL
) {
404 /* exclude "." and ".." */
405 if ((dp
->d_name
[0] != '.' || (dp
->d_name
[1] != '\0' &&
406 (dp
->d_name
[1] != '.' || dp
->d_name
[2] != '\0')))) {
407 s
= strdup(dp
->d_name
);
409 err(1, "could not allocate memory");
415 /* make sure that 'POSIX' and 'C' locales are present in the list.
416 * POSIX 1003.1-2001 requires presence of 'POSIX' name only here, but
417 * we also list 'C' for constistency
419 if (sl_find(locales
, "POSIX") == NULL
)
420 sl_add(locales
, "POSIX");
422 if (sl_find(locales
, "C") == NULL
)
423 sl_add(locales
, "C");
425 /* make output nicer, sort the list */
426 qsort(locales
->sl_str
, locales
->sl_cur
, sizeof(char *), scmp
);
430 * Show current locale status, depending on environment variables
436 const char *lang
, *vval
, *eval
;
438 setlocale(LC_ALL
, "");
440 lang
= getenv("LANG");
444 printf("LANG=\"%s\"\n", lang
);
445 /* XXX: if LANG is null, then set it to "C" to get implied values? */
447 for (i
= 0; i
< NLCINFO
; i
++) {
448 vval
= setlocale(lcinfo
[i
].id
, NULL
);
449 eval
= getenv(lcinfo
[i
].name
);
450 if (eval
!= NULL
&& !strcmp(eval
, vval
)
451 && strcmp(lang
, vval
)) {
453 * Appropriate environment variable set, its value
454 * is valid and not overriden by LC_ALL
456 * XXX: possible side effect: if both LANG and
457 * overriden environment variable are set into same
458 * value, then it'll be assumed as 'implied'
460 printf("%s=\"%s\"\n", lcinfo
[i
].name
, vval
);
462 printf("%s=\"%s\"\n", lcinfo
[i
].name
, vval
);
466 vval
= getenv("LC_ALL");
470 printf("LC_ALL=\"%s\"\n", vval
);
474 * keyword value lookup helper (via localeconv())
488 case KW_INT_CURR_SYMBOL
:
489 rval
= lc
->int_curr_symbol
;
491 case KW_CURRENCY_SYMBOL
:
492 rval
= lc
->currency_symbol
;
494 case KW_MON_DECIMAL_POINT
:
495 rval
= lc
->mon_decimal_point
;
497 case KW_MON_THOUSANDS_SEP
:
498 rval
= lc
->mon_thousands_sep
;
500 case KW_MON_GROUPING
:
501 rval
= lc
->mon_grouping
;
503 case KW_POSITIVE_SIGN
:
504 rval
= lc
->positive_sign
;
506 case KW_NEGATIVE_SIGN
:
507 rval
= lc
->negative_sign
;
509 case KW_INT_FRAC_DIGITS
:
510 rval
= &(lc
->int_frac_digits
);
513 rval
= &(lc
->frac_digits
);
515 case KW_P_CS_PRECEDES
:
516 rval
= &(lc
->p_cs_precedes
);
518 case KW_P_SEP_BY_SPACE
:
519 rval
= &(lc
->p_sep_by_space
);
521 case KW_N_CS_PRECEDES
:
522 rval
= &(lc
->n_cs_precedes
);
524 case KW_N_SEP_BY_SPACE
:
525 rval
= &(lc
->n_sep_by_space
);
528 rval
= &(lc
->p_sign_posn
);
531 rval
= &(lc
->n_sign_posn
);
533 case KW_INT_P_CS_PRECEDES
:
534 rval
= &(lc
->int_p_cs_precedes
);
536 case KW_INT_P_SEP_BY_SPACE
:
537 rval
= &(lc
->int_p_sep_by_space
);
539 case KW_INT_N_CS_PRECEDES
:
540 rval
= &(lc
->int_n_cs_precedes
);
542 case KW_INT_N_SEP_BY_SPACE
:
543 rval
= &(lc
->int_n_sep_by_space
);
545 case KW_INT_P_SIGN_POSN
:
546 rval
= &(lc
->int_p_sign_posn
);
548 case KW_INT_N_SIGN_POSN
:
549 rval
= &(lc
->int_n_sign_posn
);
558 * keyword value and properties lookup
561 kwval_lookup(char *kwname
, char **kwval
, int *cat
, int *isstr
)
567 for (i
= 0; i
< NKWINFO
; i
++) {
568 if (strcasecmp(kwname
, kwinfo
[i
].name
) == 0) {
570 *cat
= kwinfo
[i
].catid
;
571 *isstr
= kwinfo
[i
].isstr
;
572 if (kwinfo
[i
].value_ref
< KW_ZERO
) {
573 *kwval
= nl_langinfo(kwinfo
[i
].value_ref
);
575 *kwval
= kwval_lconv(kwinfo
[i
].value_ref
);
585 * Show details about requested keyword according to '-k' and/or '-c'
586 * command line options specified.
589 showdetails(char *kw
)
591 int isstr
, cat
, tmpval
;
594 if (kwval_lookup(kw
, &kwval
, &cat
, &isstr
) == 0) {
596 * invalid keyword specified.
602 if (prt_categories
) {
603 printf("%s\n", lookup_localecat(cat
));
608 printf("%s=\"%s\"\n", kw
, kwval
);
610 tmpval
= (char) *kwval
;
611 printf("%s=%d\n", kw
, tmpval
);
615 if (!prt_categories
&& !prt_keywords
) {
617 printf("%s\n", kwval
);
619 tmpval
= (char) *kwval
;
620 printf("%d\n", tmpval
);
626 * Convert locale category id into string
629 lookup_localecat(int cat
)
633 for (i
= 0; i
< NLCINFO
; i
++)
634 if (lcinfo
[i
].id
== cat
) {
635 return (lcinfo
[i
].name
);
641 * Show list of keywords
644 showkeywordslist(void)
648 #define FMT "%-20s %-12s %-7s %-20s\n"
650 printf("List of available keywords\n\n");
651 printf(FMT
, "Keyword", "Category", "Type", "Comment");
652 printf("-------------------- ------------ ------- --------------------\n");
653 for (i
= 0; i
< NKWINFO
; i
++) {
656 lookup_localecat(kwinfo
[i
].catid
),
657 (kwinfo
[i
].isstr
== 0) ? "number" : "string",