xref: /freebsd/lib/libc/locale/nl_langinfo.c (revision 1db64f89363c97858961c4df0b7d02f3223723cf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001, 2003 Alexey Zelkin <phantom@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Copyright (c) 2011 The FreeBSD Foundation
8  *
9  * Portions of this software were developed by David Chisnall
10  * under sponsorship from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <langinfo.h>
35 #include <limits.h>
36 #include <locale.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <runetype.h>
40 #include <wchar.h>
41 
42 #include "mblocal.h"
43 #include "lnumeric.h"
44 #include "lmessages.h"
45 #include "lmonetary.h"
46 #include "../stdtime/timelocal.h"
47 
48 #define _REL(BASE) ((int)item-BASE)
49 
50 char *
51 nl_langinfo_l(nl_item item, locale_t loc)
52 {
53    char *ret, *cs;
54    const char *s;
55    FIX_LOCALE(loc);
56 
57    switch (item) {
58 	case CODESET:
59 		s = XLOCALE_CTYPE(loc)->runes->__encoding;
60 		if (strcmp(s, "EUC-CN") == 0)
61 			ret = "eucCN";
62 		else if (strcmp(s, "EUC-JP") == 0)
63 			ret = "eucJP";
64 		else if (strcmp(s, "EUC-KR") == 0)
65 			ret = "eucKR";
66 		else if (strcmp(s, "EUC-TW") == 0)
67 			ret = "eucTW";
68 		else if (strcmp(s, "BIG5") == 0)
69 			ret = "Big5";
70 		else if (strcmp(s, "MSKanji") == 0)
71 			ret = "SJIS";
72 		else if (strcmp(s, "NONE") == 0)
73 			ret = "US-ASCII";
74 		else if (strncmp(s, "NONE:", 5) == 0)
75 			ret = (char *)(s + 5);
76 		else
77 			ret = (char *)s;
78 		break;
79 	case D_T_FMT:
80 		ret = (char *) __get_current_time_locale(loc)->c_fmt;
81 		break;
82 	case D_FMT:
83 		ret = (char *) __get_current_time_locale(loc)->x_fmt;
84 		break;
85 	case T_FMT:
86 		ret = (char *) __get_current_time_locale(loc)->X_fmt;
87 		break;
88 	case T_FMT_AMPM:
89 		ret = (char *) __get_current_time_locale(loc)->ampm_fmt;
90 		break;
91 	case AM_STR:
92 		ret = (char *) __get_current_time_locale(loc)->am;
93 		break;
94 	case PM_STR:
95 		ret = (char *) __get_current_time_locale(loc)->pm;
96 		break;
97 	case DAY_1: case DAY_2: case DAY_3:
98 	case DAY_4: case DAY_5: case DAY_6: case DAY_7:
99 		ret = (char*) __get_current_time_locale(loc)->weekday[_REL(DAY_1)];
100 		break;
101 	case ABDAY_1: case ABDAY_2: case ABDAY_3:
102 	case ABDAY_4: case ABDAY_5: case ABDAY_6: case ABDAY_7:
103 		ret = (char*) __get_current_time_locale(loc)->wday[_REL(ABDAY_1)];
104 		break;
105 	case MON_1: case MON_2: case MON_3: case MON_4:
106 	case MON_5: case MON_6: case MON_7: case MON_8:
107 	case MON_9: case MON_10: case MON_11: case MON_12:
108 		ret = (char*) __get_current_time_locale(loc)->month[_REL(MON_1)];
109 		break;
110 	case ABMON_1: case ABMON_2: case ABMON_3: case ABMON_4:
111 	case ABMON_5: case ABMON_6: case ABMON_7: case ABMON_8:
112 	case ABMON_9: case ABMON_10: case ABMON_11: case ABMON_12:
113 		ret = (char*) __get_current_time_locale(loc)->mon[_REL(ABMON_1)];
114 		break;
115 	case ALTMON_1: case ALTMON_2: case ALTMON_3: case ALTMON_4:
116 	case ALTMON_5: case ALTMON_6: case ALTMON_7: case ALTMON_8:
117 	case ALTMON_9: case ALTMON_10: case ALTMON_11: case ALTMON_12:
118 		ret = (char*)
119 		    __get_current_time_locale(loc)->alt_month[_REL(ALTMON_1)];
120 		break;
121 	case ERA:
122 		/* XXX: need to be implemented  */
123 		ret = "";
124 		break;
125 	case ERA_D_FMT:
126 		/* XXX: need to be implemented  */
127 		ret = "";
128 		break;
129 	case ERA_D_T_FMT:
130 		/* XXX: need to be implemented  */
131 		ret = "";
132 		break;
133 	case ERA_T_FMT:
134 		/* XXX: need to be implemented  */
135 		ret = "";
136 		break;
137 	case ALT_DIGITS:
138 		/* XXX: need to be implemented  */
139 		ret = "";
140 		break;
141 	case RADIXCHAR:
142 		ret = (char*) __get_current_numeric_locale(loc)->decimal_point;
143 		break;
144 	case THOUSEP:
145 		ret = (char*) __get_current_numeric_locale(loc)->thousands_sep;
146 		break;
147 	case YESEXPR:
148 		ret = (char*) __get_current_messages_locale(loc)->yesexpr;
149 		break;
150 	case NOEXPR:
151 		ret = (char*) __get_current_messages_locale(loc)->noexpr;
152 		break;
153 	/*
154 	 * YESSTR and NOSTR items marked with LEGACY are available, but not
155 	 * recommended by SUSv2 to be used in portable applications since
156 	 * they're subject to remove in future specification editions.
157 	 */
158 	case YESSTR:            /* LEGACY  */
159 		ret = (char*) __get_current_messages_locale(loc)->yesstr;
160 		break;
161 	case NOSTR:             /* LEGACY  */
162 		ret = (char*) __get_current_messages_locale(loc)->nostr;
163 		break;
164 	/*
165 	 * SUSv2 special formatted currency string
166 	 */
167 	case CRNCYSTR:
168 		ret = "";
169 		cs = (char*) __get_current_monetary_locale(loc)->currency_symbol;
170 		if (*cs != '\0') {
171 			char pos = localeconv_l(loc)->p_cs_precedes;
172 
173 			if (pos == localeconv_l(loc)->n_cs_precedes) {
174 				char psn = '\0';
175 
176 				if (pos == CHAR_MAX) {
177 					if (strcmp(cs, __get_current_monetary_locale(loc)->mon_decimal_point) == 0)
178 						psn = '.';
179 				} else
180 					psn = pos ? '-' : '+';
181 				if (psn != '\0') {
182 					int clen = strlen(cs);
183 
184 					if ((loc->csym = reallocf(loc->csym, clen + 2)) != NULL) {
185 						*loc->csym = psn;
186 						strcpy(loc->csym + 1, cs);
187 						ret = loc->csym;
188 					}
189 				}
190 			}
191 		}
192 		break;
193 	case D_MD_ORDER:        /* FreeBSD local extension */
194 		ret = (char *) __get_current_time_locale(loc)->md_order;
195 		break;
196 	default:
197 		ret = "";
198    }
199    return (ret);
200 }
201 
202 char *
203 nl_langinfo(nl_item item)
204 {
205 	return nl_langinfo_l(item, __get_locale());
206 }
207