tool/kq_sendcli: Add option to set write size.
[dragonfly.git] / lib / libc / locale / xlocale.c
blob45e44701a4c62a45cffb80da7bc7abd9e5950211
1 /*-
2 * Copyright (c) 2011 The FreeBSD Foundation
3 * All rights reserved.
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
10 * are met:
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
27 * SUCH DAMAGE.
29 * $FreeBSD: head/lib/libc/locale/xlocale.c 303495 2016-07-29 17:18:47Z ed $
32 #include <pthread.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <runetype.h>
36 #include "libc_private.h"
37 #include "xlocale_private.h"
39 /**
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;
56 #ifndef __NO_TLS
58 * The locale for this thread.
60 __thread locale_t __thread_locale;
61 #endif
63 * Flag indicating that one or more per-thread locales exist.
65 int __has_thread_locale;
67 * Private functions in setlocale.c.
69 const char *
70 __get_locale_env(int category);
71 int
72 __detect_path_locale(void);
74 struct _xlocale __xlocale_global_locale = {
75 {0},
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 = {
91 {0},
93 &__xlocale_C_collate,
94 &__xlocale_C_ctype,
95 0, 0, 0, 0
103 static void*(*constructors[])(const char*, locale_t) =
105 __collate_load,
106 __ctype_load,
107 __monetary_load,
108 __numeric_load,
109 __time_load,
110 __messages_load
113 static pthread_key_t locale_info_key;
114 static int fake_tls;
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);
124 } else {
125 fake_tls = 1;
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;
134 static locale_t
135 get_thread_locale(void)
138 _once(&once_control, init_key);
140 return (fake_tls ? thread_local_locale :
141 pthread_getspecific(locale_info_key));
144 #ifdef __NO_TLS
145 locale_t
146 __get_locale(void)
148 locale_t l = get_thread_locale();
149 return (l ? l : &__xlocale_global_locale);
152 #endif
154 static void
155 set_thread_locale(locale_t loc)
157 locale_t l = (loc == LC_GLOBAL_LOCALE) ? 0 : loc;
159 _once(&once_control, init_key);
161 if (NULL != l) {
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);
168 if (fake_tls) {
169 thread_local_locale = l;
170 } else {
171 pthread_setspecific(locale_info_key, l);
173 #ifndef __NO_TLS
174 __thread_locale = l;
175 __set_thread_rune_locale(loc);
176 #endif
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.
183 static void
184 destruct_locale(void *l)
186 locale_t loc = l;
188 for (int type=0 ; type<XLC_LAST ; type++) {
189 if (loc->components[type]) {
190 xlocale_release(loc->components[type]);
193 if (loc->csym) {
194 free(loc->csym);
196 free(l);
200 * Allocates a new, uninitialised, locale.
202 static locale_t
203 alloc_locale(void)
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;
210 return (new);
212 static void
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,
231 ENCODING_LEN);
233 } else if (base->components[type]) {
234 new->components[type] = xlocale_retain(base->components[type]);
235 } else {
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. */
240 return 1;
242 return (0 != new->components[type]);
246 * Public interfaces. These are the five public functions described by the
247 * xlocale interface.
250 locale_t newlocale(int mask, const char *locale, locale_t base)
252 int type;
253 const char *realLocale = locale;
254 int useenv = 0;
255 int success = 1;
257 _once(&once_control, init_key);
259 locale_t new = alloc_locale();
260 if (NULL == new) {
261 return (NULL);
264 FIX_LOCALE(base);
265 copyflags(new, base);
267 if (NULL == locale) {
268 realLocale = "C";
269 } else if ('\0' == locale[0]) {
270 useenv = 1;
273 for (type=0 ; type<XLC_LAST ; type++) {
274 if (mask & 1) {
275 if (useenv) {
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);
283 } else {
284 success = 0;
285 break;
287 } else {
288 if (!dupcomponent(type, base, new)) {
289 success = 0;
290 break;
293 mask >>= 1;
295 if (0 == success) {
296 xlocale_release(new);
297 new = NULL;
300 return (new);
303 locale_t duplocale(locale_t base)
305 locale_t new = alloc_locale();
306 int type;
308 _once(&once_control, init_key);
310 if (NULL == new) {
311 return (NULL);
314 FIX_LOCALE(base);
315 copyflags(new, base);
317 for (type=0 ; type<XLC_LAST ; type++) {
318 dupcomponent(type, base, new);
321 return (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.
328 void
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;
348 FIX_LOCALE(loc);
349 if (type >= XLC_LAST)
350 return (NULL);
351 if (loc->components[type])
352 return (loc->components[type]->locale);
353 return ("C");
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();
362 if (NULL != loc) {
363 set_thread_locale(loc);
365 return (old ? old : LC_GLOBAL_LOCALE);