xref: /freebsd/lib/libc/locale/xlocale.c (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
1 /*-
2  * Copyright (c) 2011 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by David Chisnall under sponsorship from
6  * the FreeBSD Foundation.
7  *
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.
16  *
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.
28  *
29  * $FreeBSD$
30  */
31 
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"
38 
39 /**
40  * Each locale loader declares a global component.  This is used by setlocale()
41  * and also by xlocale with LC_GLOBAL_LOCALE..
42  */
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;
49 /*
50  * And another version for the statically-allocated C locale.  We only have
51  * components for the parts that are expected to be sensible.
52  */
53 extern struct xlocale_component __xlocale_C_collate;
54 extern struct xlocale_component __xlocale_C_ctype;
55 
56 #ifndef __NO_TLS
57 /*
58  * The locale for this thread.
59  */
60 _Thread_local locale_t __thread_locale;
61 #endif
62 /*
63  * Flag indicating that one or more per-thread locales exist.
64  */
65 int __has_thread_locale;
66 /*
67  * Private functions in setlocale.c.
68  */
69 const char *
70 __get_locale_env(int category);
71 int
72 __detect_path_locale(void);
73 
74 struct _xlocale __xlocale_global_locale = {
75 	{0},
76 	{
77 		&__xlocale_global_collate,
78 		&__xlocale_global_ctype,
79 		&__xlocale_global_monetary,
80 		&__xlocale_global_numeric,
81 		&__xlocale_global_time,
82 		&__xlocale_global_messages
83 	},
84 	1,
85 	0,
86 	1,
87 	0
88 };
89 
90 struct _xlocale __xlocale_C_locale = {
91 	{0},
92 	{
93 		&__xlocale_C_collate,
94 		&__xlocale_C_ctype,
95 		0, 0, 0, 0
96 	},
97 	1,
98 	0,
99 	1,
100 	0
101 };
102 
103 static void*(*constructors[])(const char*, locale_t) =
104 {
105 	__collate_load,
106 	__ctype_load,
107 	__monetary_load,
108 	__numeric_load,
109 	__time_load,
110 	__messages_load
111 };
112 
113 static pthread_key_t locale_info_key;
114 static int fake_tls;
115 static locale_t thread_local_locale;
116 
117 static void init_key(void)
118 {
119 
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;
126 	}
127 	/* At least one per-thread locale has now been set. */
128 	__has_thread_locale = 1;
129 	__detect_path_locale();
130 }
131 
132 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
133 
134 static locale_t
135 get_thread_locale(void)
136 {
137 
138 	_once(&once_control, init_key);
139 
140 	return (fake_tls ? thread_local_locale :
141 		pthread_getspecific(locale_info_key));
142 }
143 
144 #ifdef __NO_TLS
145 locale_t
146 __get_locale(void)
147 {
148 	locale_t l = get_thread_locale();
149 	return (l ? l : &__xlocale_global_locale);
150 
151 }
152 #endif
153 
154 static void
155 set_thread_locale(locale_t loc)
156 {
157 	locale_t l = (loc == LC_GLOBAL_LOCALE) ? 0 : loc;
158 
159 	_once(&once_control, init_key);
160 
161 	if (NULL != l) {
162 		xlocale_retain((struct xlocale_refcounted*)l);
163 	}
164 	locale_t old = pthread_getspecific(locale_info_key);
165 	if ((NULL != old) && (l != old)) {
166 		xlocale_release((struct xlocale_refcounted*)old);
167 	}
168 	if (fake_tls) {
169 		thread_local_locale = l;
170 	} else {
171 		pthread_setspecific(locale_info_key, l);
172 	}
173 #ifndef __NO_TLS
174 	__thread_locale = l;
175 	__set_thread_rune_locale(loc);
176 #endif
177 }
178 
179 /**
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.
182  */
183 static void
184 destruct_locale(void *l)
185 {
186 	locale_t loc = l;
187 
188 	for (int type=0 ; type<XLC_LAST ; type++) {
189 		if (loc->components[type]) {
190 			xlocale_release(loc->components[type]);
191 		}
192 	}
193 	if (loc->csym) {
194 		free(loc->csym);
195 	}
196 	free(l);
197 }
198 
199 /**
200  * Allocates a new, uninitialised, locale.
201  */
202 static locale_t
203 alloc_locale(void)
204 {
205 	locale_t new = calloc(sizeof(struct _xlocale), 1);
206 
207 	new->header.destructor = destruct_locale;
208 	new->monetary_locale_changed = 1;
209 	new->numeric_locale_changed = 1;
210 	return (new);
211 }
212 static void
213 copyflags(locale_t new, locale_t old)
214 {
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;
219 }
220 
221 static int dupcomponent(int type, locale_t base, locale_t new)
222 {
223 	/* Always copy from the global locale, since it has mutable components.
224 	 */
225 	struct xlocale_component *src = base->components[type];
226 
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);
232 		}
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;
241 	}
242 	return (0 != new->components[type]);
243 }
244 
245 /*
246  * Public interfaces.  These are the five public functions described by the
247  * xlocale interface.
248  */
249 
250 locale_t newlocale(int mask, const char *locale, locale_t base)
251 {
252 	int type;
253 	const char *realLocale = locale;
254 	int useenv = 0;
255 	int success = 1;
256 
257 	_once(&once_control, init_key);
258 
259 	locale_t new = alloc_locale();
260 	if (NULL == new) {
261 		return (NULL);
262 	}
263 
264 	FIX_LOCALE(base);
265 	copyflags(new, base);
266 
267 	if (NULL == locale) {
268 		realLocale = "C";
269 	} else if ('\0' == locale[0]) {
270 		useenv = 1;
271 	}
272 
273 	for (type=0 ; type<XLC_LAST ; type++) {
274 		if (mask & 1) {
275 			if (useenv) {
276 				realLocale = __get_locale_env(type);
277 			}
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;
286 			}
287 		} else {
288 			if (!dupcomponent(type, base, new)) {
289 				success = 0;
290 				break;
291 			}
292 		}
293 		mask >>= 1;
294 	}
295 	if (0 == success) {
296 		xlocale_release(new);
297 		new = NULL;
298 	}
299 
300 	return (new);
301 }
302 
303 locale_t duplocale(locale_t base)
304 {
305 	locale_t new = alloc_locale();
306 	int type;
307 
308 	_once(&once_control, init_key);
309 
310 	if (NULL == new) {
311 		return (NULL);
312 	}
313 
314 	FIX_LOCALE(base);
315 	copyflags(new, base);
316 
317 	for (type=0 ; type<XLC_LAST ; type++) {
318 		dupcomponent(type, base, new);
319 	}
320 
321 	return (new);
322 }
323 
324 /*
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.
327  */
328 void
329 freelocale(locale_t loc)
330 {
331 
332 	/*
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.
336 	 */
337 	if (loc != NULL && loc != LC_GLOBAL_LOCALE &&
338 	    loc != &__xlocale_global_locale)
339 		xlocale_release(loc);
340 }
341 
342 /*
343  * Returns the name of the locale for a particular component of a locale_t.
344  */
345 const char *querylocale(int mask, locale_t loc)
346 {
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");
354 }
355 
356 /*
357  * Installs the specified locale_t as this thread's locale.
358  */
359 locale_t uselocale(locale_t loc)
360 {
361 	locale_t old = get_thread_locale();
362 	if (NULL != loc) {
363 		set_thread_locale(loc);
364 	}
365 	return (old ? old : LC_GLOBAL_LOCALE);
366 }
367 
368