Add __attribute__((__noreturn__)) to various function prototypes in usr.bin/.
[dragonfly.git] / usr.bin / locale / locale.c
blobc7b197c7246e3e37acc8fcb26c89078aed63d27b
1 /*-
2 * Copyright (c) 2002, 2003 Alexey Zelkin <phantom@FreeBSD.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
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 $
31 * XXX: implement missing era_* (LC_TIME) keywords (require libc &
32 * nl_langinfo(3) extensions)
34 * XXX: correctly handle reserved 'charmap' keyword and '-m' option (require
35 * localedef(1) implementation). Currently it's handled via
36 * nl_langinfo(CODESET).
39 #include <sys/types.h>
40 #include <assert.h>
41 #include <dirent.h>
42 #include <err.h>
43 #include <locale.h>
44 #include <langinfo.h>
45 #include <limits.h>
46 #include <paths.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <stringlist.h>
51 #include <unistd.h>
53 /* Local prototypes */
54 static void init_locales_list(void);
55 static void list_charmaps(void);
56 static void list_locales(void);
57 static const char *lookup_localecat(int);
58 static char *kwval_lconv(int);
59 static int kwval_lookup(char *, char **, int *, int *);
60 static void showdetails(char *);
61 static void showkeywordslist(void);
62 static void showlocale(void);
63 static void usage(void) __dead2;
65 /* Global variables */
66 static StringList *locales = NULL;
68 static int all_locales = 0;
69 static int all_charmaps = 0;
70 static int prt_categories = 0;
71 static int prt_keywords = 0;
73 static struct _lcinfo {
74 const char *name;
75 int id;
76 } lcinfo [] = {
77 { "LC_CTYPE", LC_CTYPE },
78 { "LC_COLLATE", LC_COLLATE },
79 { "LC_TIME", LC_TIME },
80 { "LC_NUMERIC", LC_NUMERIC },
81 { "LC_MONETARY", LC_MONETARY },
82 { "LC_MESSAGES", LC_MESSAGES }
84 #define NLCINFO (sizeof(lcinfo)/sizeof(lcinfo[0]))
86 /* ids for values not referenced by nl_langinfo() */
87 #define KW_ZERO 10000
88 #define KW_GROUPING (KW_ZERO+1)
89 #define KW_INT_CURR_SYMBOL (KW_ZERO+2)
90 #define KW_CURRENCY_SYMBOL (KW_ZERO+3)
91 #define KW_MON_DECIMAL_POINT (KW_ZERO+4)
92 #define KW_MON_THOUSANDS_SEP (KW_ZERO+5)
93 #define KW_MON_GROUPING (KW_ZERO+6)
94 #define KW_POSITIVE_SIGN (KW_ZERO+7)
95 #define KW_NEGATIVE_SIGN (KW_ZERO+8)
96 #define KW_INT_FRAC_DIGITS (KW_ZERO+9)
97 #define KW_FRAC_DIGITS (KW_ZERO+10)
98 #define KW_P_CS_PRECEDES (KW_ZERO+11)
99 #define KW_P_SEP_BY_SPACE (KW_ZERO+12)
100 #define KW_N_CS_PRECEDES (KW_ZERO+13)
101 #define KW_N_SEP_BY_SPACE (KW_ZERO+14)
102 #define KW_P_SIGN_POSN (KW_ZERO+15)
103 #define KW_N_SIGN_POSN (KW_ZERO+16)
104 #define KW_INT_P_CS_PRECEDES (KW_ZERO+17)
105 #define KW_INT_P_SEP_BY_SPACE (KW_ZERO+18)
106 #define KW_INT_N_CS_PRECEDES (KW_ZERO+19)
107 #define KW_INT_N_SEP_BY_SPACE (KW_ZERO+20)
108 #define KW_INT_P_SIGN_POSN (KW_ZERO+21)
109 #define KW_INT_N_SIGN_POSN (KW_ZERO+22)
111 static struct _kwinfo {
112 const char *name;
113 int isstr; /* true - string, false - number */
114 int catid; /* LC_* */
115 int value_ref;
116 const char *comment;
117 } kwinfo [] = {
118 { "charmap", 1, LC_CTYPE, CODESET, "" }, /* hack */
120 { "decimal_point", 1, LC_NUMERIC, RADIXCHAR, "" },
121 { "thousands_sep", 1, LC_NUMERIC, THOUSEP, "" },
122 { "grouping", 1, LC_NUMERIC, KW_GROUPING, "" },
123 { "radixchar", 1, LC_NUMERIC, RADIXCHAR,
124 "Same as decimal_point (BSD only)" }, /* compat */
125 { "thousep", 1, LC_NUMERIC, THOUSEP,
126 "Same as thousands_sep (BSD only)" }, /* compat */
128 { "int_curr_symbol", 1, LC_MONETARY, KW_INT_CURR_SYMBOL, "" },
129 { "currency_symbol", 1, LC_MONETARY, KW_CURRENCY_SYMBOL, "" },
130 { "mon_decimal_point", 1, LC_MONETARY, KW_MON_DECIMAL_POINT, "" },
131 { "mon_thousands_sep", 1, LC_MONETARY, KW_MON_THOUSANDS_SEP, "" },
132 { "mon_grouping", 1, LC_MONETARY, KW_MON_GROUPING, "" },
133 { "positive_sign", 1, LC_MONETARY, KW_POSITIVE_SIGN, "" },
134 { "negative_sign", 1, LC_MONETARY, KW_NEGATIVE_SIGN, "" },
136 { "int_frac_digits", 0, LC_MONETARY, KW_INT_FRAC_DIGITS, "" },
137 { "frac_digits", 0, LC_MONETARY, KW_FRAC_DIGITS, "" },
138 { "p_cs_precedes", 0, LC_MONETARY, KW_P_CS_PRECEDES, "" },
139 { "p_sep_by_space", 0, LC_MONETARY, KW_P_SEP_BY_SPACE, "" },
140 { "n_cs_precedes", 0, LC_MONETARY, KW_N_CS_PRECEDES, "" },
141 { "n_sep_by_space", 0, LC_MONETARY, KW_N_SEP_BY_SPACE, "" },
142 { "p_sign_posn", 0, LC_MONETARY, KW_P_SIGN_POSN, "" },
143 { "n_sign_posn", 0, LC_MONETARY, KW_N_SIGN_POSN, "" },
144 { "int_p_cs_precedes", 0, LC_MONETARY, KW_INT_P_CS_PRECEDES, "" },
145 { "int_p_sep_by_space", 0, LC_MONETARY, KW_INT_P_SEP_BY_SPACE, "" },
146 { "int_n_cs_precedes", 0, LC_MONETARY, KW_INT_N_CS_PRECEDES, "" },
147 { "int_n_sep_by_space", 0, LC_MONETARY, KW_INT_N_SEP_BY_SPACE, "" },
148 { "int_p_sign_posn", 0, LC_MONETARY, KW_INT_P_SIGN_POSN, "" },
149 { "int_n_sign_posn", 0, LC_MONETARY, KW_INT_N_SIGN_POSN, "" },
151 { "d_t_fmt", 1, LC_TIME, D_T_FMT, "" },
152 { "d_fmt", 1, LC_TIME, D_FMT, "" },
153 { "t_fmt", 1, LC_TIME, T_FMT, "" },
154 { "am_str", 1, LC_TIME, AM_STR, "" },
155 { "pm_str", 1, LC_TIME, PM_STR, "" },
156 { "t_fmt_ampm", 1, LC_TIME, T_FMT_AMPM, "" },
157 { "day_1", 1, LC_TIME, DAY_1, "" },
158 { "day_2", 1, LC_TIME, DAY_2, "" },
159 { "day_3", 1, LC_TIME, DAY_3, "" },
160 { "day_4", 1, LC_TIME, DAY_4, "" },
161 { "day_5", 1, LC_TIME, DAY_5, "" },
162 { "day_6", 1, LC_TIME, DAY_6, "" },
163 { "day_7", 1, LC_TIME, DAY_7, "" },
164 { "abday_1", 1, LC_TIME, ABDAY_1, "" },
165 { "abday_2", 1, LC_TIME, ABDAY_2, "" },
166 { "abday_3", 1, LC_TIME, ABDAY_3, "" },
167 { "abday_4", 1, LC_TIME, ABDAY_4, "" },
168 { "abday_5", 1, LC_TIME, ABDAY_5, "" },
169 { "abday_6", 1, LC_TIME, ABDAY_6, "" },
170 { "abday_7", 1, LC_TIME, ABDAY_7, "" },
171 { "mon_1", 1, LC_TIME, MON_1, "" },
172 { "mon_2", 1, LC_TIME, MON_2, "" },
173 { "mon_3", 1, LC_TIME, MON_3, "" },
174 { "mon_4", 1, LC_TIME, MON_4, "" },
175 { "mon_5", 1, LC_TIME, MON_5, "" },
176 { "mon_6", 1, LC_TIME, MON_6, "" },
177 { "mon_7", 1, LC_TIME, MON_7, "" },
178 { "mon_8", 1, LC_TIME, MON_8, "" },
179 { "mon_9", 1, LC_TIME, MON_9, "" },
180 { "mon_10", 1, LC_TIME, MON_10, "" },
181 { "mon_11", 1, LC_TIME, MON_11, "" },
182 { "mon_12", 1, LC_TIME, MON_12, "" },
183 { "abmon_1", 1, LC_TIME, ABMON_1, "" },
184 { "abmon_2", 1, LC_TIME, ABMON_2, "" },
185 { "abmon_3", 1, LC_TIME, ABMON_3, "" },
186 { "abmon_4", 1, LC_TIME, ABMON_4, "" },
187 { "abmon_5", 1, LC_TIME, ABMON_5, "" },
188 { "abmon_6", 1, LC_TIME, ABMON_6, "" },
189 { "abmon_7", 1, LC_TIME, ABMON_7, "" },
190 { "abmon_8", 1, LC_TIME, ABMON_8, "" },
191 { "abmon_9", 1, LC_TIME, ABMON_9, "" },
192 { "abmon_10", 1, LC_TIME, ABMON_10, "" },
193 { "abmon_11", 1, LC_TIME, ABMON_11, "" },
194 { "abmon_12", 1, LC_TIME, ABMON_12, "" },
195 { "era", 1, LC_TIME, ERA, "(unavailable)" },
196 { "era_d_fmt", 1, LC_TIME, ERA_D_FMT, "(unavailable)" },
197 { "era_d_t_fmt", 1, LC_TIME, ERA_D_T_FMT, "(unavailable)" },
198 { "era_t_fmt", 1, LC_TIME, ERA_T_FMT, "(unavailable)" },
199 { "alt_digits", 1, LC_TIME, ALT_DIGITS, "" },
201 { "yesexpr", 1, LC_MESSAGES, YESEXPR, "" },
202 { "noexpr", 1, LC_MESSAGES, NOEXPR, "" },
203 { "yesstr", 1, LC_MESSAGES, YESSTR,
204 "(POSIX legacy)" }, /* compat */
205 { "nostr", 1, LC_MESSAGES, NOSTR,
206 "(POSIX legacy)" } /* compat */
209 #define NKWINFO (sizeof(kwinfo)/sizeof(kwinfo[0]))
212 main(int argc, char *argv[])
214 int ch;
215 int tmp;
217 while ((ch = getopt(argc, argv, "ackm")) != -1) {
218 switch (ch) {
219 case 'a':
220 all_locales = 1;
221 break;
222 case 'c':
223 prt_categories = 1;
224 break;
225 case 'k':
226 prt_keywords = 1;
227 break;
228 case 'm':
229 all_charmaps = 1;
230 break;
231 default:
232 usage();
235 argc -= optind;
236 argv += optind;
238 /* validate arguments */
239 if (all_locales && all_charmaps)
240 usage();
241 if ((all_locales || all_charmaps) && argc > 0)
242 usage();
243 if ((all_locales || all_charmaps) && (prt_categories || prt_keywords))
244 usage();
245 if ((prt_categories || prt_keywords) && argc <= 0)
246 usage();
248 /* process '-a' */
249 if (all_locales) {
250 list_locales();
251 exit(0);
254 /* process '-m' */
255 if (all_charmaps) {
256 list_charmaps();
257 exit(0);
260 /* check for special case '-k list' */
261 tmp = 0;
262 if (prt_keywords && argc > 0)
263 while (tmp < argc)
264 if (strcasecmp(argv[tmp++], "list") == 0) {
265 showkeywordslist();
266 exit(0);
269 /* process '-c' and/or '-k' */
270 if (prt_categories || prt_keywords || argc > 0) {
271 setlocale(LC_ALL, "");
272 while (argc > 0) {
273 showdetails(*argv);
274 argv++;
275 argc--;
277 exit(0);
280 /* no arguments, show current locale state */
281 showlocale();
283 return (0);
286 static void
287 usage(void)
289 printf("usage: locale [ -a | -m ]\n"
290 " locale [ -ck ] keyword ...\n");
291 exit(1);
295 * Output information about all available locales
297 * XXX actually output of this function does not guarantee that locale
298 * is really available to application, since it can be broken or
299 * inconsistent thus setlocale() will fail. Maybe add '-V' function to
300 * also validate these locales?
302 static void
303 list_locales(void)
305 size_t i;
307 init_locales_list();
308 for (i = 0; i < locales->sl_cur; i++) {
309 printf("%s\n", locales->sl_str[i]);
314 * qsort() helper function
316 static int
317 scmp(const void *s1, const void *s2)
319 return strcmp((const char *)s1, (const char *)s2);
323 * Output information about all available charmaps
325 * XXX this function is doing a task in hackish way, i.e. by scaning
326 * list of locales, spliting their codeset part and building list of
327 * them.
329 static void
330 list_charmaps(void)
332 size_t i;
333 char *s, *cs;
334 StringList *charmaps;
336 /* initialize StringList */
337 charmaps = sl_init();
338 if (charmaps == NULL)
339 err(1, "could not allocate memory");
341 /* fetch locales list */
342 init_locales_list();
344 /* split codesets and build their list */
345 for (i = 0; i < locales->sl_cur; i++) {
346 s = locales->sl_str[i];
347 if ((cs = strchr(s, '.')) != NULL) {
348 cs++;
349 if (sl_find(charmaps, cs) == NULL)
350 sl_add(charmaps, cs);
354 /* add US-ASCII, if not yet added */
355 if (sl_find(charmaps, "US-ASCII") == NULL)
356 sl_add(charmaps, "US-ASCII");
358 /* sort the list */
359 qsort(charmaps->sl_str, charmaps->sl_cur, sizeof(char *), scmp);
361 /* print results */
362 for (i = 0; i < charmaps->sl_cur; i++) {
363 printf("%s\n", charmaps->sl_str[i]);
368 * Retrieve sorted list of system locales (or user locales, if PATH_LOCALE
369 * environment variable is set)
371 static void
372 init_locales_list(void)
374 DIR *dirp;
375 struct dirent *dp;
376 char *s;
377 char *localedir;
379 /* why call this function twice ? */
380 if (locales != NULL)
381 return;
383 /* initialize StringList */
384 locales = sl_init();
385 if (locales == NULL)
386 err(1, "could not allocate memory");
388 /* get actual locales directory name */
389 setlocale(LC_CTYPE, "C");
391 localedir = getenv("PATH_LOCALE");
392 if (localedir == NULL)
393 localedir = _PATH_LOCALE;
395 /* open locales directory */
396 dirp = opendir(localedir);
397 if (dirp == NULL)
398 err(1, "could not open directory '%s'", localedir);
400 /* scan directory and store its contents except "." and ".." */
401 while ((dp = readdir(dirp)) != NULL) {
402 /* exclude "." and ".." */
403 if ((dp->d_name[0] != '.' || (dp->d_name[1] != '\0' &&
404 (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))) {
405 s = strdup(dp->d_name);
406 if (s == NULL)
407 err(1, "could not allocate memory");
408 sl_add(locales, s);
411 closedir(dirp);
413 /* make sure that 'POSIX' and 'C' locales are present in the list.
414 * POSIX 1003.1-2001 requires presence of 'POSIX' name only here, but
415 * we also list 'C' for constistency
417 if (sl_find(locales, "POSIX") == NULL)
418 sl_add(locales, "POSIX");
420 if (sl_find(locales, "C") == NULL)
421 sl_add(locales, "C");
423 /* make output nicer, sort the list */
424 qsort(locales->sl_str, locales->sl_cur, sizeof(char *), scmp);
428 * Show current locale status, depending on environment variables
430 static void
431 showlocale(void)
433 size_t i;
434 const char *lang, *vval, *eval;
436 setlocale(LC_ALL, "");
438 lang = getenv("LANG");
439 if (lang == NULL) {
440 lang = "";
442 printf("LANG=\"%s\"\n", lang);
443 /* XXX: if LANG is null, then set it to "C" to get implied values? */
445 for (i = 0; i < NLCINFO; i++) {
446 vval = setlocale(lcinfo[i].id, NULL);
447 eval = getenv(lcinfo[i].name);
448 if (eval != NULL && !strcmp(eval, vval)
449 && strcmp(lang, vval)) {
451 * Appropriate environment variable set, its value
452 * is valid and not overriden by LC_ALL
454 * XXX: possible side effect: if both LANG and
455 * overriden environment variable are set into same
456 * value, then it'll be assumed as 'implied'
458 printf("%s=\"%s\"\n", lcinfo[i].name, vval);
459 } else {
460 printf("%s=\"%s\"\n", lcinfo[i].name, vval);
464 vval = getenv("LC_ALL");
465 if (vval == NULL) {
466 vval = "";
468 printf("LC_ALL=\"%s\"\n", vval);
472 * keyword value lookup helper (via localeconv())
474 static char *
475 kwval_lconv(int id)
477 struct lconv *lc;
478 char *rval;
480 rval = NULL;
481 lc = localeconv();
482 switch (id) {
483 case KW_GROUPING:
484 rval = lc->grouping;
485 break;
486 case KW_INT_CURR_SYMBOL:
487 rval = lc->int_curr_symbol;
488 break;
489 case KW_CURRENCY_SYMBOL:
490 rval = lc->currency_symbol;
491 break;
492 case KW_MON_DECIMAL_POINT:
493 rval = lc->mon_decimal_point;
494 break;
495 case KW_MON_THOUSANDS_SEP:
496 rval = lc->mon_thousands_sep;
497 break;
498 case KW_MON_GROUPING:
499 rval = lc->mon_grouping;
500 break;
501 case KW_POSITIVE_SIGN:
502 rval = lc->positive_sign;
503 break;
504 case KW_NEGATIVE_SIGN:
505 rval = lc->negative_sign;
506 break;
507 case KW_INT_FRAC_DIGITS:
508 rval = &(lc->int_frac_digits);
509 break;
510 case KW_FRAC_DIGITS:
511 rval = &(lc->frac_digits);
512 break;
513 case KW_P_CS_PRECEDES:
514 rval = &(lc->p_cs_precedes);
515 break;
516 case KW_P_SEP_BY_SPACE:
517 rval = &(lc->p_sep_by_space);
518 break;
519 case KW_N_CS_PRECEDES:
520 rval = &(lc->n_cs_precedes);
521 break;
522 case KW_N_SEP_BY_SPACE:
523 rval = &(lc->n_sep_by_space);
524 break;
525 case KW_P_SIGN_POSN:
526 rval = &(lc->p_sign_posn);
527 break;
528 case KW_N_SIGN_POSN:
529 rval = &(lc->n_sign_posn);
530 break;
531 case KW_INT_P_CS_PRECEDES:
532 rval = &(lc->int_p_cs_precedes);
533 break;
534 case KW_INT_P_SEP_BY_SPACE:
535 rval = &(lc->int_p_sep_by_space);
536 break;
537 case KW_INT_N_CS_PRECEDES:
538 rval = &(lc->int_n_cs_precedes);
539 break;
540 case KW_INT_N_SEP_BY_SPACE:
541 rval = &(lc->int_n_sep_by_space);
542 break;
543 case KW_INT_P_SIGN_POSN:
544 rval = &(lc->int_p_sign_posn);
545 break;
546 case KW_INT_N_SIGN_POSN:
547 rval = &(lc->int_n_sign_posn);
548 break;
549 default:
550 break;
552 return (rval);
556 * keyword value and properties lookup
558 static int
559 kwval_lookup(char *kwname, char **kwval, int *cat, int *isstr)
561 int rval;
562 size_t i;
564 rval = 0;
565 for (i = 0; i < NKWINFO; i++) {
566 if (strcasecmp(kwname, kwinfo[i].name) == 0) {
567 rval = 1;
568 *cat = kwinfo[i].catid;
569 *isstr = kwinfo[i].isstr;
570 if (kwinfo[i].value_ref < KW_ZERO) {
571 *kwval = nl_langinfo(kwinfo[i].value_ref);
572 } else {
573 *kwval = kwval_lconv(kwinfo[i].value_ref);
575 break;
579 return (rval);
583 * Show details about requested keyword according to '-k' and/or '-c'
584 * command line options specified.
586 static void
587 showdetails(char *kw)
589 int isstr, cat, tmpval;
590 char *kwval;
592 if (kwval_lookup(kw, &kwval, &cat, &isstr) == 0) {
594 * invalid keyword specified.
595 * XXX: any actions?
597 return;
600 if (prt_categories) {
601 printf("%s\n", lookup_localecat(cat));
604 if (prt_keywords) {
605 if (isstr) {
606 printf("%s=\"%s\"\n", kw, kwval);
607 } else {
608 tmpval = (char) *kwval;
609 printf("%s=%d\n", kw, tmpval);
613 if (!prt_categories && !prt_keywords) {
614 if (isstr) {
615 printf("%s\n", kwval);
616 } else {
617 tmpval = (char) *kwval;
618 printf("%d\n", tmpval);
624 * Convert locale category id into string
626 static const char *
627 lookup_localecat(int cat)
629 size_t i;
631 for (i = 0; i < NLCINFO; i++)
632 if (lcinfo[i].id == cat) {
633 return (lcinfo[i].name);
635 return ("UNKNOWN");
639 * Show list of keywords
641 static void
642 showkeywordslist(void)
644 size_t i;
646 #define FMT "%-20s %-12s %-7s %-20s\n"
648 printf("List of available keywords\n\n");
649 printf(FMT, "Keyword", "Category", "Type", "Comment");
650 printf("-------------------- ------------ ------- --------------------\n");
651 for (i = 0; i < NKWINFO; i++) {
652 printf(FMT,
653 kwinfo[i].name,
654 lookup_localecat(kwinfo[i].catid),
655 (kwinfo[i].isstr == 0) ? "number" : "string",
656 kwinfo[i].comment);