xref: /freebsd/lib/libc/locale/xlocale.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software was developed by David Chisnall under sponsorship from
8  * the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33 
34 #include <pthread.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <runetype.h>
38 #include "libc_private.h"
39 #include "xlocale_private.h"
40 
41 /**
42  * Each locale loader declares a global component.  This is used by setlocale()
43  * and also by xlocale with LC_GLOBAL_LOCALE..
44  */
45 extern struct xlocale_component __xlocale_global_collate;
46 extern struct xlocale_component __xlocale_global_ctype;
47 extern struct xlocale_component __xlocale_global_monetary;
48 extern struct xlocale_component __xlocale_global_numeric;
49 extern struct xlocale_component __xlocale_global_time;
50 extern struct xlocale_component __xlocale_global_messages;
51 /*
52  * And another version for the statically-allocated C locale.  We only have
53  * components for the parts that are expected to be sensible.
54  */
55 extern struct xlocale_component __xlocale_C_collate;
56 extern struct xlocale_component __xlocale_C_ctype;
57 
58 /*
59  * The locale for this thread.
60  */
61 _Thread_local locale_t __thread_locale;
62 
63 /*
64  * Flag indicating that one or more per-thread locales exist.
65  */
66 int __has_thread_locale;
67 
68 /*
69  * Private functions in setlocale.c.
70  */
71 const char *__get_locale_env(int category);
72 int __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
118 init_key(void)
119 {
120 	int error;
121 
122 	error = pthread_key_create(&locale_info_key, xlocale_release);
123 	if (error == 0) {
124 		pthread_setspecific(locale_info_key, (void*)42);
125 		if (pthread_getspecific(locale_info_key) == (void*)42) {
126 			pthread_setspecific(locale_info_key, 0);
127 		} else {
128 			fake_tls = 1;
129 		}
130 	} else {
131 		fake_tls = 1;
132 	}
133 	/* At least one per-thread locale has now been set. */
134 	__has_thread_locale = 1;
135 	__detect_path_locale();
136 }
137 
138 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
139 
140 static locale_t
141 get_thread_locale(void)
142 {
143 
144 	_once(&once_control, init_key);
145 
146 	return (fake_tls ? thread_local_locale :
147 	    pthread_getspecific(locale_info_key));
148 }
149 
150 static void
151 set_thread_locale(locale_t loc)
152 {
153 	locale_t l = (loc == LC_GLOBAL_LOCALE) ? 0 : loc;
154 
155 	_once(&once_control, init_key);
156 
157 	if (NULL != l) {
158 		xlocale_retain((struct xlocale_refcounted*)l);
159 	}
160 	locale_t old = get_thread_locale();
161 	if ((NULL != old) && (l != old)) {
162 		xlocale_release((struct xlocale_refcounted*)old);
163 	}
164 	if (fake_tls) {
165 		thread_local_locale = l;
166 	} else {
167 		pthread_setspecific(locale_info_key, l);
168 	}
169 	__thread_locale = l;
170 	__set_thread_rune_locale(loc);
171 }
172 
173 /**
174  * Clean up a locale, once its reference count reaches zero.  This function is
175  * called by xlocale_release(), it should not be called directly.
176  */
177 static void
178 destruct_locale(void *l)
179 {
180 	locale_t loc = l;
181 
182 	for (int type=0 ; type<XLC_LAST ; type++) {
183 		if (loc->components[type]) {
184 			xlocale_release(loc->components[type]);
185 		}
186 	}
187 	if (loc->csym) {
188 		free(loc->csym);
189 	}
190 	free(l);
191 }
192 
193 /**
194  * Allocates a new, uninitialised, locale.
195  */
196 static locale_t
197 alloc_locale(void)
198 {
199 	locale_t new = calloc(sizeof(struct _xlocale), 1);
200 
201 	if (new == NULL)
202 		return (NULL);
203 
204 	new->header.destructor = destruct_locale;
205 	new->monetary_locale_changed = 1;
206 	new->numeric_locale_changed = 1;
207 	return (new);
208 }
209 
210 static void
211 copyflags(locale_t new, locale_t old)
212 {
213 	new->using_monetary_locale = old->using_monetary_locale;
214 	new->using_numeric_locale = old->using_numeric_locale;
215 	new->using_time_locale = old->using_time_locale;
216 	new->using_messages_locale = old->using_messages_locale;
217 }
218 
219 static int
220 dupcomponent(int type, locale_t base, locale_t new)
221 {
222 	/* Always copy from the global locale, since it has mutable components.
223 	 */
224 	struct xlocale_component *src = base->components[type];
225 
226 	if (&__xlocale_global_locale == base) {
227 		new->components[type] = constructors[type](src->locale, new);
228 		if (new->components[type]) {
229 			strncpy(new->components[type]->locale, src->locale,
230 			    ENCODING_LEN);
231 			strncpy(new->components[type]->version, src->version,
232 			    XLOCALE_DEF_VERSION_LEN);
233 		}
234 	} else if (base->components[type]) {
235 		new->components[type] = xlocale_retain(base->components[type]);
236 	} else {
237 		/* If the component was NULL, return success - if base is a
238 		 * valid locale then the flag indicating that this isn't
239 		 * present should be set.  If it isn't a valid locale, then
240 		 * we're stuck anyway. */
241 		return 1;
242 	}
243 	return (0 != new->components[type]);
244 }
245 
246 /*
247  * Public interfaces.  These are the five public functions described by the
248  * xlocale interface.
249  */
250 
251 locale_t
252 newlocale(int mask, const char *locale, locale_t base)
253 {
254 	locale_t orig_base;
255 	int type;
256 	const char *realLocale = locale;
257 	int useenv = 0;
258 	int success = 1;
259 
260 	locale_t new = alloc_locale();
261 	if (NULL == new) {
262 		return (NULL);
263 	}
264 
265 	_once(&once_control, init_key);
266 
267 	orig_base = base;
268 	FIX_LOCALE(base);
269 	copyflags(new, base);
270 
271 	if (NULL == locale) {
272 		realLocale = "C";
273 	} else if ('\0' == locale[0]) {
274 		useenv = 1;
275 	}
276 
277 	for (type=0 ; type<XLC_LAST ; type++) {
278 		if (mask & 1) {
279 			if (useenv) {
280 				realLocale = __get_locale_env(type + 1);
281 			}
282 			new->components[type] =
283 			     constructors[type](realLocale, new);
284 			if (new->components[type]) {
285 				strncpy(new->components[type]->locale,
286 				     realLocale, ENCODING_LEN);
287 			} else {
288 				success = 0;
289 				break;
290 			}
291 		} else {
292 			if (!dupcomponent(type, base, new)) {
293 				success = 0;
294 				break;
295 			}
296 		}
297 		mask >>= 1;
298 	}
299 	if (0 == success) {
300 		xlocale_release(new);
301 		new = NULL;
302 	} else if (base == orig_base) {
303 		xlocale_release(base);
304 	}
305 
306 	return (new);
307 }
308 
309 locale_t
310 duplocale(locale_t base)
311 {
312 	locale_t new = alloc_locale();
313 	int type;
314 
315 	if (NULL == new) {
316 		return (NULL);
317 	}
318 
319 	_once(&once_control, init_key);
320 
321 	FIX_LOCALE(base);
322 	copyflags(new, base);
323 
324 	for (type=0 ; type<XLC_LAST ; type++) {
325 		dupcomponent(type, base, new);
326 	}
327 
328 	return (new);
329 }
330 
331 /*
332  * Free a locale_t.  This is quite a poorly named function.  It actually
333  * disclaims a reference to a locale_t, rather than freeing it.
334  */
335 void
336 freelocale(locale_t loc)
337 {
338 
339 	/*
340 	 * Fail if we're passed something that isn't a locale. If we're
341 	 * passed the global locale, pretend that we freed it but don't
342 	 * actually do anything.
343 	 */
344 	if (loc != NULL && loc != LC_GLOBAL_LOCALE &&
345 	    loc != &__xlocale_global_locale)
346 		xlocale_release(loc);
347 }
348 
349 /*
350  * Returns the name or version of the locale for a particular component of a
351  * locale_t.
352  */
353 const char *
354 querylocale(int mask, locale_t loc)
355 {
356 	int type = ffs(mask & ~LC_VERSION_MASK) - 1;
357 	FIX_LOCALE(loc);
358 	if (type >= XLC_LAST)
359 		return (NULL);
360 	if (mask & LC_VERSION_MASK) {
361 		if (loc->components[type])
362 			return (loc->components[type]->version);
363 		return ("");
364 	} else {
365 		if (loc->components[type])
366 			return (loc->components[type]->locale);
367 		return ("C");
368 	}
369 }
370 
371 /*
372  * Installs the specified locale_t as this thread's locale.
373  */
374 locale_t
375 uselocale(locale_t loc)
376 {
377 	locale_t old = get_thread_locale();
378 	if (NULL != loc) {
379 		set_thread_locale(loc);
380 	}
381 	return (old ? old : LC_GLOBAL_LOCALE);
382 }
383 
384