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