xref: /netbsd-src/lib/libc/locale/global_locale.c (revision 975a152cfcdb39ae6e496af647af0c7275ca0b61)
1 /* $NetBSD: global_locale.c,v 1.20 2013/08/20 19:58:30 joerg Exp $ */
2 
3 /*-
4  * Copyright (c)2008 Citrus Project,
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: global_locale.c,v 1.20 2013/08/20 19:58:30 joerg Exp $");
32 #endif /* LIBC_SCCS and not lint */
33 
34 #include <sys/types.h>
35 #include <sys/ctype_bits.h>
36 #include <sys/localedef.h>
37 #include <langinfo.h>
38 #include <limits.h>
39 #define __SETLOCALE_SOURCE__
40 #include <locale.h>
41 #include <stdlib.h>
42 
43 #include "runetype_local.h"
44 #include "setlocale_local.h"
45 
46 #ifndef NBCHAR_MAX
47 #define NBCHAR_MAX (char)CHAR_MAX
48 #endif
49 
50 static const _MessagesLocale _DefaultMessagesLocale = {
51 	"^[Yy]",
52 	"^[Nn]",
53 	"yes",
54 	"no"
55 };
56 
57 static const _MonetaryLocale _DefaultMonetaryLocale = {
58 	"",
59 	"",
60 	"",
61 	"",
62 	"",
63 	"",
64 	"",
65 	(char)CHAR_MAX,
66 	(char)CHAR_MAX,
67 	(char)CHAR_MAX,
68 	(char)CHAR_MAX,
69 	(char)CHAR_MAX,
70 	(char)CHAR_MAX,
71 	(char)CHAR_MAX,
72 	(char)CHAR_MAX,
73 	(char)CHAR_MAX,
74 	(char)CHAR_MAX,
75 	(char)CHAR_MAX,
76 	(char)CHAR_MAX,
77 	(char)CHAR_MAX,
78 	(char)CHAR_MAX
79 };
80 
81 static const _NumericLocale _DefaultNumericLocale = {
82 	".",
83 	"",
84 	""
85 };
86 
87 static const _TimeLocale _DefaultTimeLocale =
88 {
89 	{
90 		"Sun","Mon","Tue","Wed","Thu","Fri","Sat",
91 	},
92 	{
93 		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
94 		"Friday", "Saturday"
95 	},
96 	{
97 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
98 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
99 	},
100 	{
101 		"January", "February", "March", "April", "May", "June", "July",
102 		"August", "September", "October", "November", "December"
103 	},
104 	{
105 		"AM", "PM"
106 	},
107 	"%a %b %e %H:%M:%S %Y",
108 	"%m/%d/%y",
109 	"%H:%M:%S",
110 	"%I:%M:%S %p"
111 };
112 
113 static const struct lconv _C_ldata = {
114 	.decimal_point		= __UNCONST("."),
115 	.thousands_sep		= __UNCONST(""),
116 	.grouping		= __UNCONST(""),
117 	.int_curr_symbol	= __UNCONST(""),
118 	.currency_symbol	= __UNCONST(""),
119 	.mon_decimal_point	= __UNCONST(""),
120 	.mon_thousands_sep	= __UNCONST(""),
121 	.mon_grouping		= __UNCONST(""),
122 	.positive_sign		= __UNCONST(""),
123 	.negative_sign		= __UNCONST(""),
124 	.int_frac_digits	= NBCHAR_MAX,
125 	.frac_digits		= NBCHAR_MAX,
126 	.p_cs_precedes		= NBCHAR_MAX,
127 	.p_sep_by_space		= NBCHAR_MAX,
128 	.n_cs_precedes		= NBCHAR_MAX,
129 	.n_sep_by_space		= NBCHAR_MAX,
130 	.p_sign_posn		= NBCHAR_MAX,
131 	.n_sign_posn		= NBCHAR_MAX,
132 	.int_p_cs_precedes	= NBCHAR_MAX,
133 	.int_n_cs_precedes	= NBCHAR_MAX,
134 	.int_p_sep_by_space	= NBCHAR_MAX,
135 	.int_n_sep_by_space	= NBCHAR_MAX,
136 	.int_p_sign_posn	= NBCHAR_MAX,
137 	.int_n_sign_posn	= NBCHAR_MAX,
138 };
139 
140 static struct _locale_cache_t _global_cache = {
141     .ldata = __UNCONST(&_C_ldata),
142 };
143 
144 __dso_protected struct _locale _lc_global_locale = {
145     .cache = &_global_cache,
146     .query = { _C_LOCALE },
147     .part_name = {
148 	[(size_t)LC_ALL     ] = _C_LOCALE,
149 	[(size_t)LC_COLLATE ] = _C_LOCALE,
150 	[(size_t)LC_CTYPE   ] = _C_LOCALE,
151 	[(size_t)LC_MONETARY] = _C_LOCALE,
152 	[(size_t)LC_NUMERIC ] = _C_LOCALE,
153 	[(size_t)LC_TIME    ] = _C_LOCALE,
154 	[(size_t)LC_MESSAGES] = _C_LOCALE,
155     },
156     .part_impl = {
157 	[(size_t)LC_ALL     ] = (_locale_part_t)NULL,
158 	[(size_t)LC_COLLATE ] = (_locale_part_t)NULL,
159 	[(size_t)LC_CTYPE   ] = (_locale_part_t)
160 	    __UNCONST(&_DefaultRuneLocale),
161 	[(size_t)LC_MONETARY] = (_locale_part_t)
162 	    __UNCONST(&_DefaultMonetaryLocale),
163 	[(size_t)LC_NUMERIC ] = (_locale_part_t)
164 	    __UNCONST(&_DefaultNumericLocale),
165 	[(size_t)LC_MESSAGES] = (_locale_part_t)
166 	    __UNCONST(&_DefaultMessagesLocale),
167 	[(size_t)LC_TIME] = (_locale_part_t)
168 	    __UNCONST(&_DefaultTimeLocale),
169     },
170 };
171 
172 static const struct _locale_cache_t _C_cache = {
173     .ldata = __UNCONST(&_C_ldata),
174 };
175 
176 __dso_protected const struct _locale _lc_C_locale = {
177     .cache = __UNCONST(&_C_cache),
178     .query = { _C_LOCALE },
179     .part_name = {
180 	[(size_t)LC_ALL     ] = _C_LOCALE,
181 	[(size_t)LC_COLLATE ] = _C_LOCALE,
182 	[(size_t)LC_CTYPE   ] = _C_LOCALE,
183 	[(size_t)LC_MONETARY] = _C_LOCALE,
184 	[(size_t)LC_NUMERIC ] = _C_LOCALE,
185 	[(size_t)LC_TIME    ] = _C_LOCALE,
186 	[(size_t)LC_MESSAGES] = _C_LOCALE,
187     },
188     .part_impl = {
189 	[(size_t)LC_ALL     ] = (_locale_part_t)NULL,
190 	[(size_t)LC_COLLATE ] = (_locale_part_t)NULL,
191 	[(size_t)LC_CTYPE   ] = (_locale_part_t)
192 	    __UNCONST(&_DefaultRuneLocale),
193 	[(size_t)LC_MONETARY] = (_locale_part_t)
194 	    __UNCONST(&_DefaultMonetaryLocale),
195 	[(size_t)LC_NUMERIC ] = (_locale_part_t)
196 	    __UNCONST(&_DefaultNumericLocale),
197 	[(size_t)LC_MESSAGES] = (_locale_part_t)
198 	    __UNCONST(&_DefaultMessagesLocale),
199 	[(size_t)LC_TIME] = (_locale_part_t)
200 	    __UNCONST(&_DefaultTimeLocale),
201     },
202 };
203