nl_langinfo.c revision 78758
1/*-
2 * Copyright (c) 2001 Alexey Zelkin
3 * All rights reserved.
4 *
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.
13 *
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.
25 *
26 * $FreeBSD: head/lib/libc/locale/nl_langinfo.c 78758 2001-06-25 09:03:10Z ache $
27 */
28
29#include <locale.h>
30#include <langinfo.h>
31#include <limits.h>
32#include <stdlib.h>
33#include <string.h>
34#include "../stdtime/timelocal.h"
35#include "lnumeric.h"
36#include "lmonetary.h"
37#include "lmessages.h"
38
39#define TRANSITION_PERIOD_HACK
40
41#define _REL(BASE) ((int)item-BASE)
42
43char *
44nl_langinfo(nl_item item) {
45
46   char *ret, *s, *cs;
47   static char *csym = NULL;
48#ifdef TRANSITION_PERIOD_HACK
49   static char *cset = NULL;
50#endif /* TRANSITION_PERIOD_HACK */
51
52   switch (item) {
53	case CODESET:
54		ret = "";
55		if ((s = setlocale(LC_CTYPE, NULL)) != NULL) {
56			if ((cs = strchr(s, '.')) != NULL) {
57				ret = cs + 1;
58#ifdef TRANSITION_PERIOD_HACK
59				if (strncmp(ret, "ISO_", 4) == 0) {
60					int slen = strlen(ret);
61
62					if ((cset = reallocf(cset, slen)) != NULL) {
63						strcpy(cset, "ISO");
64						strcat(cset, ret + 4);
65						ret = cset;
66					}
67				} else if (strcmp(ret, "EUC") == 0) {
68					if (strncmp(s, "ja_JP", 5) == 0)
69						ret = "eucJP";
70					else if (strncmp(s, "ko_KR", 5) == 0)
71						ret = "eucKR";
72					else if (strncmp(s, "zh_CN", 5) == 0)
73						ret = "eucCN";
74				} else if (strcmp(ret, "ASCII") == 0)
75					ret = "US-ASCII";
76#endif /* TRANSITION_PERIOD_HACK */
77			} else if (strcmp(s, "C") == 0 ||
78				   strcmp(s, "POSIX") == 0
79#ifdef TRANSITION_PERIOD_HACK
80				   || strstr(s, "ASCII") != NULL
81#endif /* TRANSITION_PERIOD_HACK */
82				  )
83				ret = "US-ASCII";
84		}
85		break;
86	case D_T_FMT:
87		ret = (char *) __get_current_time_locale()->c_fmt;
88		break;
89	case D_FMT:
90		ret = (char *) __get_current_time_locale()->x_fmt;
91		break;
92	case T_FMT:
93		ret = (char *) __get_current_time_locale()->X_fmt;
94		break;
95	case T_FMT_AMPM:
96		ret = (char *) __get_current_time_locale()->ampm_fmt;
97		break;
98	case AM_STR:
99		ret = (char *) __get_current_time_locale()->am;
100		break;
101	case PM_STR:
102		ret = (char *) __get_current_time_locale()->pm;
103		break;
104	case DAY_1: case DAY_2: case DAY_3:
105	case DAY_4: case DAY_5: case DAY_6: case DAY_7:
106		ret = (char*) __get_current_time_locale()->weekday[_REL(DAY_1)];
107		break;
108	case ABDAY_1: case ABDAY_2: case ABDAY_3:
109	case ABDAY_4: case ABDAY_5: case ABDAY_6: case ABDAY_7:
110		ret = (char*) __get_current_time_locale()->wday[_REL(ABDAY_1)];
111		break;
112	case MON_1: case MON_2: case MON_3: case MON_4:
113	case MON_5: case MON_6: case MON_7: case MON_8:
114	case MON_9: case MON_10: case MON_11: case MON_12:
115		ret = (char*) __get_current_time_locale()->month[_REL(MON_1)];
116		break;
117	case ABMON_1: case ABMON_2: case ABMON_3: case ABMON_4:
118	case ABMON_5: case ABMON_6: case ABMON_7: case ABMON_8:
119	case ABMON_9: case ABMON_10: case ABMON_11: case ABMON_12:
120		ret = (char*) __get_current_time_locale()->mon[_REL(ABMON_1)];
121		break;
122	case ERA:
123		/* XXX: need to be implemented  */
124		ret = "";
125		break;
126	case ERA_D_FMT:
127		/* XXX: need to be implemented  */
128		ret = "";
129		break;
130	case ERA_D_T_FMT:
131		/* XXX: need to be implemented  */
132		ret = "";
133		break;
134	case ERA_T_FMT:
135		/* XXX: need to be implemented  */
136		ret = "";
137		break;
138	case ALT_DIGITS:
139		/* XXX: need to be implemented  */
140		ret = "";
141		break;
142	case RADIXCHAR:
143		ret = (char*) __get_current_numeric_locale()->decimal_point;
144		break;
145	case THOUSEP:
146		ret = (char*) __get_current_numeric_locale()->thousands_sep;
147		break;
148	case YESEXPR:
149		ret = (char*) __get_current_messages_locale()->yesexpr;
150		break;
151	case NOEXPR:
152		ret = (char*) __get_current_messages_locale()->noexpr;
153		break;
154	/* All items marked with LEGACY are available, but not recomended
155	 * by SUSv2 to be used in portable applications since they're subject
156	 * to remove in future specification editions
157	 */
158	case YESSTR:            /* LEGACY  */
159		ret = (char*) __get_current_messages_locale()->yesstr;
160		break;
161	case NOSTR:             /* LEGACY  */
162		ret = (char*) __get_current_messages_locale()->nostr;
163		break;
164	case CRNCYSTR:
165		ret = "";
166		cs = (char*) __get_current_monetary_locale()->currency_symbol;
167		if (*cs != '\0') {
168			char pos = localeconv()->p_cs_precedes;
169
170			if (pos == localeconv()->n_cs_precedes) {
171				char psn = '\0';
172
173				if (pos == CHAR_MAX) {
174					if (strcmp(cs, __get_current_monetary_locale()->mon_decimal_point) == 0)
175						psn = '.';
176				} else
177					psn = pos ? '-' : '+';
178				if (psn != '\0') {
179					int clen = strlen(cs);
180
181					if ((csym = reallocf(csym, clen + 2)) != NULL) {
182						*csym = psn;
183						strcpy(csym + 1, cs);
184						ret = csym;
185					}
186				}
187			}
188		}
189		break;
190	case D_MD_ORDER:        /* local extension */
191		ret = (char *) __get_current_time_locale()->md_order;
192		break;
193	default:
194		ret = "";
195   }
196   return (ret);
197}
198