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
36 #include "libc_private.h"
37 #include "xlocale_private.h"
40 * Each locale loader declares a global component. This is used by setlocale()
41 * and also by xlocale with LC_GLOBAL_LOCALE..
43 extern struct xlocale_component __xlocale_global_collate;
44 extern struct xlocale_component __xlocale_global_ctype;
45 extern struct xlocale_component __xlocale_global_monetary;
46 extern struct xlocale_component __xlocale_global_numeric;
47 extern struct xlocale_component __xlocale_global_time;
48 extern struct xlocale_component __xlocale_global_messages;
50 * And another version for the statically-allocated C locale. We only have
51 * components for the parts that are expected to be sensible.
53 extern struct xlocale_component __xlocale_C_collate;
54 extern struct xlocale_component __xlocale_C_ctype;
58 * The locale for this thread.
60 _Thread_local locale_t __thread_locale;
63 * Flag indicating that one or more per-thread locales exist.
65 int __has_thread_locale;
67 * Private functions in setlocale.c.
70 __get_locale_env(int category);
72 __detect_path_locale(void);
74 struct _xlocale __xlocale_global_locale = {
77 &__xlocale_global_collate,
78 &__xlocale_global_ctype,
79 &__xlocale_global_monetary,
80 &__xlocale_global_numeric,
81 &__xlocale_global_time,
82 &__xlocale_global_messages
90 struct _xlocale __xlocale_C_locale = {
103 static void*(*constructors[])(const char*, locale_t) =
113 static pthread_key_t locale_info_key;
115 static locale_t thread_local_locale;
117 static void init_key(void)
120 pthread_key_create(&locale_info_key, xlocale_release);
121 pthread_setspecific(locale_info_key, (void*)42);
122 if (pthread_getspecific(locale_info_key) == (void*)42) {
123 pthread_setspecific(locale_info_key, 0);
127 /* At least one per-thread locale has now been set. */
128 __has_thread_locale = 1;
129 __detect_path_locale();
132 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
135 get_thread_locale(void)
138 _once(&once_control, init_key);
140 return (fake_tls ? thread_local_locale :
141 pthread_getspecific(locale_info_key));
148 locale_t l = get_thread_locale();
149 return (l ? l : &__xlocale_global_locale);
155 set_thread_locale(locale_t loc)
157 locale_t l = (loc == LC_GLOBAL_LOCALE) ? 0 : loc;
159 _once(&once_control, init_key);
162 xlocale_retain((struct xlocale_refcounted*)l);
164 locale_t old = pthread_getspecific(locale_info_key);
165 if ((NULL != old) && (l != old)) {
166 xlocale_release((struct xlocale_refcounted*)old);
169 thread_local_locale = l;
171 pthread_setspecific(locale_info_key, l);
175 __set_thread_rune_locale(loc);
180 * Clean up a locale, once its reference count reaches zero. This function is
181 * called by xlocale_release(), it should not be called directly.
184 destruct_locale(void *l)
188 for (int type=0 ; type<XLC_LAST ; type++) {
189 if (loc->components[type]) {
190 xlocale_release(loc->components[type]);
200 * Allocates a new, uninitialised, locale.
205 locale_t new = calloc(sizeof(struct _xlocale), 1);
207 new->header.destructor = destruct_locale;
208 new->monetary_locale_changed = 1;
209 new->numeric_locale_changed = 1;
213 copyflags(locale_t new, locale_t old)
215 new->using_monetary_locale = old->using_monetary_locale;
216 new->using_numeric_locale = old->using_numeric_locale;
217 new->using_time_locale = old->using_time_locale;
218 new->using_messages_locale = old->using_messages_locale;
221 static int dupcomponent(int type, locale_t base, locale_t new)
223 /* Always copy from the global locale, since it has mutable components.
225 struct xlocale_component *src = base->components[type];
227 if (&__xlocale_global_locale == base) {
228 new->components[type] = constructors[type](src->locale, new);
229 if (new->components[type]) {
230 strncpy(new->components[type]->locale, src->locale,
233 } else if (base->components[type]) {
234 new->components[type] = xlocale_retain(base->components[type]);
236 /* If the component was NULL, return success - if base is a
237 * valid locale then the flag indicating that this isn't
238 * present should be set. If it isn't a valid locale, then
239 * we're stuck anyway. */
242 return (0 != new->components[type]);
246 * Public interfaces. These are the five public functions described by the
250 locale_t newlocale(int mask, const char *locale, locale_t base)
253 const char *realLocale = locale;
257 _once(&once_control, init_key);
259 locale_t new = alloc_locale();
265 copyflags(new, base);
267 if (NULL == locale) {
269 } else if ('\0' == locale[0]) {
273 for (type=0 ; type<XLC_LAST ; type++) {
276 realLocale = __get_locale_env(type);
278 new->components[type] =
279 constructors[type](realLocale, new);
280 if (new->components[type]) {
281 strncpy(new->components[type]->locale,
282 realLocale, ENCODING_LEN);
288 if (!dupcomponent(type, base, new)) {
296 xlocale_release(new);
303 locale_t duplocale(locale_t base)
305 locale_t new = alloc_locale();
308 _once(&once_control, init_key);
315 copyflags(new, base);
317 for (type=0 ; type<XLC_LAST ; type++) {
318 dupcomponent(type, base, new);
325 * Free a locale_t. This is quite a poorly named function. It actually
326 * disclaims a reference to a locale_t, rather than freeing it.
329 freelocale(locale_t loc)
333 * Fail if we're passed something that isn't a locale. If we're
334 * passed the global locale, pretend that we freed it but don't
335 * actually do anything.
337 if (loc != NULL && loc != LC_GLOBAL_LOCALE &&
338 loc != &__xlocale_global_locale)
339 xlocale_release(loc);
343 * Returns the name of the locale for a particular component of a locale_t.
345 const char *querylocale(int mask, locale_t loc)
347 int type = ffs(mask) - 1;
349 if (type >= XLC_LAST)
351 if (loc->components[type])
352 return (loc->components[type]->locale);
357 * Installs the specified locale_t as this thread's locale.
359 locale_t uselocale(locale_t loc)
361 locale_t old = get_thread_locale();
363 set_thread_locale(loc);
365 return (old ? old : LC_GLOBAL_LOCALE);