larn(6): Fix two "use of index before limits check" issues.
[dragonfly.git] / usr.bin / locale / locale.c
blobbb5b540bc57c0d82919f31ba7b1c261e0f18730a
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 $
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>
41 #include <assert.h>
42 #include <dirent.h>
43 #include <err.h>
44 #include <locale.h>
45 #include <langinfo.h>
46 #include <limits.h>
47 #include <paths.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <stringlist.h>
52 #include <unistd.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);
64 void usage(void);
66 /* Global variables */
67 static StringList *locales = NULL;
69 int all_locales = 0;
70 int all_charmaps = 0;
71 int prt_categories = 0;
72 int prt_keywords = 0;
73 int more_params = 0;
75 struct _lcinfo {
76 const char *name;
77 int id;
78 } 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 (sizeof(lcinfo)/sizeof(lcinfo[0]))
88 /* ids for values not referenced by nl_langinfo() */
89 #define KW_ZERO 10000
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)
113 struct _kwinfo {
114 const char *name;
115 int isstr; /* true - string, false - number */
116 int catid; /* LC_* */
117 int value_ref;
118 const char *comment;
119 } kwinfo [] = {
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[])
216 int ch;
217 int tmp;
219 while ((ch = getopt(argc, argv, "ackm")) != -1) {
220 switch (ch) {
221 case 'a':
222 all_locales = 1;
223 break;
224 case 'c':
225 prt_categories = 1;
226 break;
227 case 'k':
228 prt_keywords = 1;
229 break;
230 case 'm':
231 all_charmaps = 1;
232 break;
233 default:
234 usage();
237 argc -= optind;
238 argv += optind;
240 /* validate arguments */
241 if (all_locales && all_charmaps)
242 usage();
243 if ((all_locales || all_charmaps) && argc > 0)
244 usage();
245 if ((all_locales || all_charmaps) && (prt_categories || prt_keywords))
246 usage();
247 if ((prt_categories || prt_keywords) && argc <= 0)
248 usage();
250 /* process '-a' */
251 if (all_locales) {
252 list_locales();
253 exit(0);
256 /* process '-m' */
257 if (all_charmaps) {
258 list_charmaps();
259 exit(0);
262 /* check for special case '-k list' */
263 tmp = 0;
264 if (prt_keywords && argc > 0)
265 while (tmp < argc)
266 if (strcasecmp(argv[tmp++], "list") == 0) {
267 showkeywordslist();
268 exit(0);
271 /* process '-c' and/or '-k' */
272 if (prt_categories || prt_keywords || argc > 0) {
273 setlocale(LC_ALL, "");
274 while (argc > 0) {
275 showdetails(*argv);
276 argv++;
277 argc--;
279 exit(0);
282 /* no arguments, show current locale state */
283 showlocale();
285 return (0);
288 void
289 usage(void)
291 printf("usage: locale [ -a | -m ]\n"
292 " locale [ -ck ] keyword ...\n");
293 exit(1);
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?
304 void
305 list_locales(void)
307 size_t i;
309 init_locales_list();
310 for (i = 0; i < locales->sl_cur; i++) {
311 printf("%s\n", locales->sl_str[i]);
316 * qsort() helper function
318 static int
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
329 * them.
331 void
332 list_charmaps(void)
334 size_t i;
335 char *s, *cs;
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 */
344 init_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) {
350 cs++;
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");
360 /* sort the list */
361 qsort(charmaps->sl_str, charmaps->sl_cur, sizeof(char *), scmp);
363 /* print results */
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)
373 void
374 init_locales_list(void)
376 DIR *dirp;
377 struct dirent *dp;
378 char *s;
379 char *localedir;
381 /* why call this function twice ? */
382 if (locales != NULL)
383 return;
385 /* initialize StringList */
386 locales = sl_init();
387 if (locales == NULL)
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);
399 if (dirp == NULL)
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);
408 if (s == NULL)
409 err(1, "could not allocate memory");
410 sl_add(locales, s);
413 closedir(dirp);
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
432 void
433 showlocale(void)
435 size_t i;
436 const char *lang, *vval, *eval;
438 setlocale(LC_ALL, "");
440 lang = getenv("LANG");
441 if (lang == NULL) {
442 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);
461 } else {
462 printf("%s=\"%s\"\n", lcinfo[i].name, vval);
466 vval = getenv("LC_ALL");
467 if (vval == NULL) {
468 vval = "";
470 printf("LC_ALL=\"%s\"\n", vval);
474 * keyword value lookup helper (via localeconv())
476 char *
477 kwval_lconv(int id)
479 struct lconv *lc;
480 char *rval;
482 rval = NULL;
483 lc = localeconv();
484 switch (id) {
485 case KW_GROUPING:
486 rval = lc->grouping;
487 break;
488 case KW_INT_CURR_SYMBOL:
489 rval = lc->int_curr_symbol;
490 break;
491 case KW_CURRENCY_SYMBOL:
492 rval = lc->currency_symbol;
493 break;
494 case KW_MON_DECIMAL_POINT:
495 rval = lc->mon_decimal_point;
496 break;
497 case KW_MON_THOUSANDS_SEP:
498 rval = lc->mon_thousands_sep;
499 break;
500 case KW_MON_GROUPING:
501 rval = lc->mon_grouping;
502 break;
503 case KW_POSITIVE_SIGN:
504 rval = lc->positive_sign;
505 break;
506 case KW_NEGATIVE_SIGN:
507 rval = lc->negative_sign;
508 break;
509 case KW_INT_FRAC_DIGITS:
510 rval = &(lc->int_frac_digits);
511 break;
512 case KW_FRAC_DIGITS:
513 rval = &(lc->frac_digits);
514 break;
515 case KW_P_CS_PRECEDES:
516 rval = &(lc->p_cs_precedes);
517 break;
518 case KW_P_SEP_BY_SPACE:
519 rval = &(lc->p_sep_by_space);
520 break;
521 case KW_N_CS_PRECEDES:
522 rval = &(lc->n_cs_precedes);
523 break;
524 case KW_N_SEP_BY_SPACE:
525 rval = &(lc->n_sep_by_space);
526 break;
527 case KW_P_SIGN_POSN:
528 rval = &(lc->p_sign_posn);
529 break;
530 case KW_N_SIGN_POSN:
531 rval = &(lc->n_sign_posn);
532 break;
533 case KW_INT_P_CS_PRECEDES:
534 rval = &(lc->int_p_cs_precedes);
535 break;
536 case KW_INT_P_SEP_BY_SPACE:
537 rval = &(lc->int_p_sep_by_space);
538 break;
539 case KW_INT_N_CS_PRECEDES:
540 rval = &(lc->int_n_cs_precedes);
541 break;
542 case KW_INT_N_SEP_BY_SPACE:
543 rval = &(lc->int_n_sep_by_space);
544 break;
545 case KW_INT_P_SIGN_POSN:
546 rval = &(lc->int_p_sign_posn);
547 break;
548 case KW_INT_N_SIGN_POSN:
549 rval = &(lc->int_n_sign_posn);
550 break;
551 default:
552 break;
554 return (rval);
558 * keyword value and properties lookup
561 kwval_lookup(char *kwname, char **kwval, int *cat, int *isstr)
563 int rval;
564 size_t i;
566 rval = 0;
567 for (i = 0; i < NKWINFO; i++) {
568 if (strcasecmp(kwname, kwinfo[i].name) == 0) {
569 rval = 1;
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);
574 } else {
575 *kwval = kwval_lconv(kwinfo[i].value_ref);
577 break;
581 return (rval);
585 * Show details about requested keyword according to '-k' and/or '-c'
586 * command line options specified.
588 void
589 showdetails(char *kw)
591 int isstr, cat, tmpval;
592 char *kwval;
594 if (kwval_lookup(kw, &kwval, &cat, &isstr) == 0) {
596 * invalid keyword specified.
597 * XXX: any actions?
599 return;
602 if (prt_categories) {
603 printf("%s\n", lookup_localecat(cat));
606 if (prt_keywords) {
607 if (isstr) {
608 printf("%s=\"%s\"\n", kw, kwval);
609 } else {
610 tmpval = (char) *kwval;
611 printf("%s=%d\n", kw, tmpval);
615 if (!prt_categories && !prt_keywords) {
616 if (isstr) {
617 printf("%s\n", kwval);
618 } else {
619 tmpval = (char) *kwval;
620 printf("%d\n", tmpval);
626 * Convert locale category id into string
628 const char *
629 lookup_localecat(int cat)
631 size_t i;
633 for (i = 0; i < NLCINFO; i++)
634 if (lcinfo[i].id == cat) {
635 return (lcinfo[i].name);
637 return ("UNKNOWN");
641 * Show list of keywords
643 void
644 showkeywordslist(void)
646 size_t i;
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++) {
654 printf(FMT,
655 kwinfo[i].name,
656 lookup_localecat(kwinfo[i].catid),
657 (kwinfo[i].isstr == 0) ? "number" : "string",
658 kwinfo[i].comment);