2 * Copyright (c) 2011 The FreeBSD Foundation
5 * This software was developed by David Chisnall under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: head/lib/libc/locale/xlocale.c 303495 2016-07-29 17:18:47Z ed $
32 #include "namespace.h"
38 #include "libc_private.h"
39 #include "xlocale_private.h"
40 #include "un-namespace.h"
43 * Each locale loader declares a global component. This is used by setlocale()
44 * and also by xlocale with LC_GLOBAL_LOCALE..
46 extern struct xlocale_component __xlocale_global_collate;
47 extern struct xlocale_component __xlocale_global_ctype;
48 extern struct xlocale_component __xlocale_global_monetary;
49 extern struct xlocale_component __xlocale_global_numeric;
50 extern struct xlocale_component __xlocale_global_time;
51 extern struct xlocale_component __xlocale_global_messages;
53 * And another version for the statically-allocated C locale. We only have
54 * components for the parts that are expected to be sensible.
56 extern struct xlocale_component __xlocale_C_collate;
57 extern struct xlocale_component __xlocale_C_ctype;
61 * The locale for this thread.
63 __thread locale_t __thread_locale;
66 * Flag indicating that one or more per-thread locales exist.
68 int __has_thread_locale;
70 * Private functions in setlocale.c.
72 const char * __get_locale_env(int category);
73 int __get_locale_str(int category, const char *str, char * const res);
74 int __detect_path_locale(void);
76 struct _xlocale __xlocale_global_locale = {
79 &__xlocale_global_collate,
80 &__xlocale_global_ctype,
81 &__xlocale_global_monetary,
82 &__xlocale_global_numeric,
83 &__xlocale_global_time,
84 &__xlocale_global_messages
86 .monetary_locale_changed = 1,
87 .using_monetary_locale = 0,
88 .numeric_locale_changed = 1,
89 .using_numeric_locale = 0
92 struct _xlocale __xlocale_C_locale = {
99 .monetary_locale_changed = 1,
100 .using_monetary_locale = 0,
101 .numeric_locale_changed = 1,
102 .using_numeric_locale = 0
105 static void*(*constructors[])(const char*, locale_t) =
115 static pthread_key_t locale_info_key;
117 static locale_t thread_local_locale;
119 static void init_key(void)
122 _pthread_key_create(&locale_info_key, xlocale_release);
123 _pthread_setspecific(locale_info_key, (void*)42);
124 if (_pthread_getspecific(locale_info_key) == (void*)42) {
125 _pthread_setspecific(locale_info_key, 0);
129 /* At least one per-thread locale has now been set. */
130 __has_thread_locale = 1;
131 __detect_path_locale();
134 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
137 get_thread_locale(void)
140 _once(&once_control, init_key);
142 return (fake_tls ? thread_local_locale :
143 _pthread_getspecific(locale_info_key));
150 locale_t l = get_thread_locale();
151 return (l ? l : &__xlocale_global_locale);
157 set_thread_locale(locale_t loc)
159 locale_t l = (loc == LC_GLOBAL_LOCALE) ? 0 : loc;
161 _once(&once_control, init_key);
164 xlocale_retain((struct xlocale_refcounted*)l);
166 locale_t old = _pthread_getspecific(locale_info_key);
167 if ((NULL != old) && (l != old)) {
168 xlocale_release((struct xlocale_refcounted*)old);
171 thread_local_locale = l;
173 _pthread_setspecific(locale_info_key, l);
177 __set_thread_rune_locale(loc);
182 * Clean up a locale, once its reference count reaches zero. This function is
183 * called by xlocale_release(), it should not be called directly.
186 destruct_locale(void *l)
190 for (int type=0 ; type<XLC_LAST ; type++) {
191 if (loc->components[type]) {
192 xlocale_release(loc->components[type]);
202 * Allocates a new, uninitialised, locale.
207 locale_t new = calloc(sizeof(struct _xlocale), 1);
209 new->header.destructor = destruct_locale;
210 new->monetary_locale_changed = 1;
211 new->numeric_locale_changed = 1;
215 copyflags(locale_t new, locale_t old)
217 new->using_monetary_locale = old->using_monetary_locale;
218 new->using_numeric_locale = old->using_numeric_locale;
219 new->using_time_locale = old->using_time_locale;
220 new->using_messages_locale = old->using_messages_locale;
223 static int dupcomponent(int type, locale_t base, locale_t new)
225 /* Always copy from the global locale, since it has mutable components.
227 struct xlocale_component *src = base->components[type];
229 if (&__xlocale_global_locale == base) {
230 new->components[type] = constructors[type](src->locale, new);
231 if (new->components[type]) {
232 strncpy(new->components[type]->locale, src->locale,
235 } else if (base->components[type]) {
236 new->components[type] = xlocale_retain(base->components[type]);
238 /* If the component was NULL, return success - if base is a
239 * valid locale then the flag indicating that this isn't
240 * present should be set. If it isn't a valid locale, then
241 * we're stuck anyway. */
244 return (0 != new->components[type]);
248 * Public interfaces. These are the five public functions described by the
252 locale_t newlocale(int mask, const char *locale, locale_t base)
255 const char *realLocale = locale;
257 char lres[ENCODING_LEN + 1] = "";
264 _once(&once_control, init_key);
266 locale_t new = alloc_locale();
272 copyflags(new, base);
274 if (NULL == locale) {
276 } else if ('\0' == locale[0]) {
278 } else if (strchr(locale, '/') != NULL) {
280 * Handle system native locale string
281 * e.g. "C/en_US.UTF-8/C/C/lt_LT/C"
285 } else if ('L' == locale[0] && strchr(locale, ';') != NULL) {
287 * We are called from c++ runtime lib with LC_*; string??
292 for (type=0 ; type<XLC_LAST ; type++) {
294 cp = strchr(np, '/');
295 if (cp == NULL && type == XLC_LAST - 1) {
296 cp = locale + strlen(locale);
297 } else if (cp == NULL || type == XLC_LAST - 1) {
303 if (len > ENCODING_LEN || len <= 0) {
308 strncpy(lres, np, len);
315 realLocale = __get_locale_env(type + 1);
319 __get_locale_str(type + 1, locale, lres);
322 new->components[type] =
323 constructors[type](realLocale, new);
324 if (new->components[type]) {
325 strncpy(new->components[type]->locale,
326 realLocale, ENCODING_LEN);
332 if (!dupcomponent(type, base, new)) {
340 xlocale_release(new);
347 locale_t duplocale(locale_t base)
349 locale_t new = alloc_locale();
352 _once(&once_control, init_key);
359 copyflags(new, base);
361 for (type=0 ; type<XLC_LAST ; type++) {
362 dupcomponent(type, base, new);
369 * Free a locale_t. This is quite a poorly named function. It actually
370 * disclaims a reference to a locale_t, rather than freeing it.
373 freelocale(locale_t loc)
377 * Fail if we're passed something that isn't a locale. If we're
378 * passed the global locale, pretend that we freed it but don't
379 * actually do anything.
381 if (loc != NULL && loc != LC_GLOBAL_LOCALE &&
382 loc != &__xlocale_global_locale)
383 xlocale_release(loc);
387 * Returns the name of the locale for a particular component of a locale_t.
389 const char *querylocale(int mask, locale_t loc)
391 int type = ffs(mask) - 1;
393 if (type >= XLC_LAST)
395 if (loc->components[type])
396 return (loc->components[type]->locale);
401 * Installs the specified locale_t as this thread's locale.
403 locale_t uselocale(locale_t loc)
405 locale_t old = get_thread_locale();
407 set_thread_locale(loc);
409 return (old ? old : LC_GLOBAL_LOCALE);