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