1 // Copyright (C) 1997-2021 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library.  This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 
14 // Under Section 7 of GPL version 3, you are granted additional
15 // permissions described in the GCC Runtime Library Exception, version
16 // 3.1, as published by the Free Software Foundation.
17 
18 // You should have received a copy of the GNU General Public License and
19 // a copy of the GCC Runtime Library Exception along with this program;
20 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
21 // <http://www.gnu.org/licenses/>.
22 
23 #define _GLIBCXX_USE_CXX11_ABI 1
24 #include <clocale>
25 #include <cstring>
26 #include <cstdlib>
27 #include <locale>
28 
29 namespace std _GLIBCXX_VISIBILITY(default)
30 {
31 _GLIBCXX_BEGIN_NAMESPACE_VERSION
32 
33   using namespace __gnu_cxx;
34 
locale(const char * __s)35   locale::locale(const char* __s) : _M_impl(0)
36   {
37     if (__s)
38       {
39 	_S_initialize();
40 	if (std::strcmp(__s, "C") == 0 || std::strcmp(__s, "POSIX") == 0)
41 	  (_M_impl = _S_classic)->_M_add_reference();
42 	else if (std::strcmp(__s, "") != 0)
43 	  _M_impl = new _Impl(__s, 1);
44 	else
45 	  {
46 	    // Get it from the environment.
47 	    char* __env = std::getenv("LC_ALL");
48 	    // If LC_ALL is set we are done.
49 	    if (__env && std::strcmp(__env, "") != 0)
50 	      {
51 		if (std::strcmp(__env, "C") == 0
52 		    || std::strcmp(__env, "POSIX") == 0)
53 		  (_M_impl = _S_classic)->_M_add_reference();
54 		else
55 		  _M_impl = new _Impl(__env, 1);
56 	      }
57 	    else
58 	      {
59 		// LANG may set a default different from "C".
60 		string __lang;
61 		__env = std::getenv("LANG");
62 		if (!__env || std::strcmp(__env, "") == 0
63 		    || std::strcmp(__env, "C") == 0
64 		    || std::strcmp(__env, "POSIX") == 0)
65 		  __lang = "C";
66 		else
67 		  __lang = __env;
68 
69 		// Scan the categories looking for the first one
70 		// different from LANG.
71 		size_t __i = 0;
72 		if (__lang == "C")
73 		  for (; __i < _S_categories_size; ++__i)
74 		    {
75 		      __env = std::getenv(_S_categories[__i]);
76 		      if (__env && std::strcmp(__env, "") != 0
77 			  && std::strcmp(__env, "C") != 0
78 			  && std::strcmp(__env, "POSIX") != 0)
79 			break;
80 		    }
81 		else
82 		  for (; __i < _S_categories_size; ++__i)
83 		    {
84 		      __env = std::getenv(_S_categories[__i]);
85 		      if (__env && std::strcmp(__env, "") != 0
86 			  && __lang != __env)
87 			break;
88 		    }
89 
90 		// If one is found, build the complete string of
91 		// the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on...
92 		if (__i < _S_categories_size)
93 		  {
94 		    string __str;
95 		    __str.reserve(128);
96 		    for (size_t __j = 0; __j < __i; ++__j)
97 		      {
98 			__str += _S_categories[__j];
99 			__str += '=';
100 			__str += __lang;
101 			__str += ';';
102 		      }
103 		    __str += _S_categories[__i];
104 		    __str += '=';
105 		    __str += __env;
106 		    __str += ';';
107 		    ++__i;
108 		    for (; __i < _S_categories_size; ++__i)
109 		      {
110 			__env = std::getenv(_S_categories[__i]);
111 			__str += _S_categories[__i];
112 			if (!__env || std::strcmp(__env, "") == 0)
113 			  {
114 			    __str += '=';
115 			    __str += __lang;
116 			    __str += ';';
117 			  }
118 			else if (std::strcmp(__env, "C") == 0
119 				 || std::strcmp(__env, "POSIX") == 0)
120 			  __str += "=C;";
121 			else
122 			  {
123 			    __str += '=';
124 			    __str += __env;
125 			    __str += ';';
126 			  }
127 		      }
128 		    __str.erase(__str.end() - 1);
129 		    _M_impl = new _Impl(__str.c_str(), 1);
130 		  }
131 		// ... otherwise either an additional instance of
132 		// the "C" locale or LANG.
133 		else if (__lang == "C")
134 		  (_M_impl = _S_classic)->_M_add_reference();
135 		else
136 		  _M_impl = new _Impl(__lang.c_str(), 1);
137 	      }
138 	  }
139       }
140     else
141       __throw_runtime_error(__N("locale::locale null not valid"));
142   }
143 
locale(const locale & __base,const char * __s,category __cat)144   locale::locale(const locale& __base, const char* __s, category __cat)
145   : _M_impl(0)
146   {
147     // NB: There are complicated, yet more efficient ways to do
148     // this. Building up locales on a per-category way is tedious, so
149     // let's do it this way until people complain.
150     locale __add(__s);
151     _M_coalesce(__base, __add, __cat);
152   }
153 
locale(const locale & __base,const locale & __add,category __cat)154   locale::locale(const locale& __base, const locale& __add, category __cat)
155   : _M_impl(0)
156   { _M_coalesce(__base, __add, __cat); }
157 
158   void
_M_coalesce(const locale & __base,const locale & __add,category __cat)159   locale::_M_coalesce(const locale& __base, const locale& __add,
160 		      category __cat)
161   {
162     __cat = _S_normalize_category(__cat);
163     _M_impl = new _Impl(*__base._M_impl, 1);
164 
165     __try
166       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
167     __catch(...)
168       {
169 	_M_impl->_M_remove_reference();
170 	__throw_exception_again;
171       }
172   }
173 
174 const int num_facets = (
175     _GLIBCXX_NUM_FACETS + _GLIBCXX_NUM_CXX11_FACETS
176 #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
177     + _GLIBCXX_NUM_LBDL_ALT128_FACETS
178 #endif
179     )
180 #ifdef _GLIBCXX_USE_WCHAR_T
181   * 2
182 #endif
183   + _GLIBCXX_NUM_UNICODE_FACETS;
184 
185   // Construct named _Impl.
186   locale::_Impl::
_Impl(const char * __s,size_t __refs)187   _Impl(const char* __s, size_t __refs)
188   : _M_refcount(__refs), _M_facets(0), _M_facets_size(num_facets),
189     _M_caches(0), _M_names(0)
190   {
191     // Initialize the underlying locale model, which also checks to
192     // see if the given name is valid.
193     __c_locale __cloc;
194     locale::facet::_S_create_c_locale(__cloc, __s);
195     __c_locale __clocm = __cloc;
196 
197     __try
198       {
199 	_M_facets = new const facet*[_M_facets_size]();
200 	_M_caches = new const facet*[_M_facets_size]();
201 	_M_names = new char*[_S_categories_size]();
202 
203 	// Name the categories.
204 	const char* __smon = __s;
205 	const size_t __len = std::strlen(__s);
206 	if (!std::memchr(__s, ';', __len))
207 	  {
208 	    _M_names[0] = new char[__len + 1];
209 	    std::memcpy(_M_names[0], __s, __len + 1);
210 	  }
211 	else
212 	  {
213 	    const char* __end = __s;
214 	    bool __found_ctype = false;
215 	    bool __found_monetary = false;
216 	    size_t __ci = 0, __mi = 0;
217 	    for (size_t __i = 0; __i < _S_categories_size; ++__i)
218 	      {
219 		const char* __beg = std::strchr(__end + 1, '=') + 1;
220 		__end = std::strchr(__beg, ';');
221 		if (!__end)
222 		  __end = __s + __len;
223 		_M_names[__i] = new char[__end - __beg + 1];
224 		std::memcpy(_M_names[__i], __beg, __end - __beg);
225 		_M_names[__i][__end - __beg] = '\0';
226 		if (!__found_ctype
227 		    && *(__beg - 2) == 'E' && *(__beg - 3) == 'P')
228 		  {
229 		    __found_ctype = true;
230 		    __ci = __i;
231 		  }
232 		else if (!__found_monetary && *(__beg - 2) == 'Y')
233 		  {
234 		    __found_monetary = true;
235 		    __mi = __i;
236 		  }
237 	      }
238 
239 	    if (std::strcmp(_M_names[__ci], _M_names[__mi]))
240 	      {
241 		__smon = _M_names[__mi];
242 		__clocm = locale::facet::_S_lc_ctype_c_locale(__cloc,
243 							      __smon);
244 	      }
245 	  }
246 
247 	// Construct all standard facets and add them to _M_facets.
248 	_M_init_facet(new std::ctype<char>(__cloc, 0, false));
249 	_M_init_facet(new codecvt<char, char, mbstate_t>(__cloc));
250 	_M_init_facet(new numpunct<char>(__cloc));
251 	_M_init_facet(new num_get<char>);
252 	_M_init_facet(new num_put<char>);
253 	_M_init_facet(new std::collate<char>(__cloc));
254 	_M_init_facet(new moneypunct<char, false>(__cloc, 0));
255 	_M_init_facet(new moneypunct<char, true>(__cloc, 0));
256 	_M_init_facet(new money_get<char>);
257 	_M_init_facet(new money_put<char>);
258 	_M_init_facet(new __timepunct<char>(__cloc, __s));
259 	_M_init_facet(new time_get<char>);
260 	_M_init_facet(new time_put<char>);
261 	_M_init_facet(new std::messages<char>(__cloc, __s));
262 
263 #ifdef  _GLIBCXX_USE_WCHAR_T
264 	_M_init_facet(new std::ctype<wchar_t>(__cloc));
265 	_M_init_facet(new codecvt<wchar_t, char, mbstate_t>(__cloc));
266 	_M_init_facet(new numpunct<wchar_t>(__cloc));
267 	_M_init_facet(new num_get<wchar_t>);
268 	_M_init_facet(new num_put<wchar_t>);
269 	_M_init_facet(new std::collate<wchar_t>(__cloc));
270 	_M_init_facet(new moneypunct<wchar_t, false>(__clocm, __smon));
271 	_M_init_facet(new moneypunct<wchar_t, true>(__clocm, __smon));
272 	_M_init_facet(new money_get<wchar_t>);
273 	_M_init_facet(new money_put<wchar_t>);
274 	_M_init_facet(new __timepunct<wchar_t>(__cloc, __s));
275 	_M_init_facet(new time_get<wchar_t>);
276 	_M_init_facet(new time_put<wchar_t>);
277 	_M_init_facet(new std::messages<wchar_t>(__cloc, __s));
278 #endif
279 
280 #if _GLIBCXX_NUM_UNICODE_FACETS != 0
281         _M_init_facet(new codecvt<char16_t, char, mbstate_t>);
282         _M_init_facet(new codecvt<char32_t, char, mbstate_t>);
283 
284 #ifdef _GLIBCXX_USE_CHAR8_T
285         _M_init_facet(new codecvt<char16_t, char8_t, mbstate_t>);
286         _M_init_facet(new codecvt<char32_t, char8_t, mbstate_t>);
287 #endif
288 
289 #endif
290 
291 #if _GLIBCXX_USE_DUAL_ABI
292         _M_init_extra(&__cloc, &__clocm, __s, __smon);
293 #endif
294 
295 #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
296 	_M_init_extra_ldbl128(false);
297 #endif
298 
299 	locale::facet::_S_destroy_c_locale(__cloc);
300 	if (__clocm != __cloc)
301 	  locale::facet::_S_destroy_c_locale(__clocm);
302       }
303     __catch(...)
304       {
305 	locale::facet::_S_destroy_c_locale(__cloc);
306 	if (__clocm != __cloc)
307 	  locale::facet::_S_destroy_c_locale(__clocm);
308 	this->~_Impl();
309 	__throw_exception_again;
310       }
311   }
312 
313   void
314   locale::_Impl::
_M_replace_categories(const _Impl * __imp,category __cat)315   _M_replace_categories(const _Impl* __imp, category __cat)
316   {
317     category __mask = 1;
318     if (!_M_names[0] || !__imp->_M_names[0])
319       {
320 	if (_M_names[0])
321 	  {
322 	    delete [] _M_names[0];
323 	    _M_names[0] = 0;   // Unnamed.
324 	  }
325 
326 	for (size_t __ix = 0; __ix < _S_categories_size; ++__ix, __mask <<= 1)
327 	  {
328 	    if (__mask & __cat)
329 	      // Need to replace entry in _M_facets with other locale's info.
330 	      _M_replace_category(__imp, _S_facet_categories[__ix]);
331 	  }
332       }
333     else
334       {
335 	if (!_M_names[1])
336 	  {
337 	    // A full set of _M_names must be prepared, all identical
338 	    // to _M_names[0] to begin with. Then, below, a few will
339 	    // be replaced by the corresponding __imp->_M_names. I.e.,
340 	    // not a "simple" locale anymore (see locale::operator==).
341 	    const size_t __len = std::strlen(_M_names[0]) + 1;
342 	    for (size_t __i = 1; __i < _S_categories_size; ++__i)
343 	      {
344 		_M_names[__i] = new char[__len];
345 		std::memcpy(_M_names[__i], _M_names[0], __len);
346 	      }
347 	  }
348 
349 	for (size_t __ix = 0; __ix < _S_categories_size; ++__ix, __mask <<= 1)
350 	  {
351 	    if (__mask & __cat)
352 	      {
353 		// Need to replace entry in _M_facets with other locale's info.
354 		_M_replace_category(__imp, _S_facet_categories[__ix]);
355 
356 		// FIXME: Hack for libstdc++/29217: the numerical encodings
357 		// of the time and collate categories are swapped vs the
358 		// order of the names in locale::_S_categories.  We'd like to
359 		// adjust the former (the latter is dictated by compatibility
360 		// with glibc) but we can't for binary compatibility.
361 		size_t __ix_name = __ix;
362 		if (__ix == 2 || __ix == 3)
363 		  __ix_name = 5 - __ix;
364 
365 		char* __src = __imp->_M_names[__ix_name] ?
366 		              __imp->_M_names[__ix_name] : __imp->_M_names[0];
367 		const size_t __len = std::strlen(__src) + 1;
368 		char* __new = new char[__len];
369 		std::memcpy(__new, __src, __len);
370 		delete [] _M_names[__ix_name];
371 		_M_names[__ix_name] = __new;
372 	      }
373 	  }
374       }
375   }
376 
377 _GLIBCXX_END_NAMESPACE_VERSION
378 } // namespace
379