xref: /dragonfly/lib/libc/locale/xlocale.c (revision 7b1120e5)
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: head/lib/libc/locale/xlocale.c 303495 2016-07-29 17:18:47Z ed $
30  */
31 
32 #include <errno.h>
33 #include <pthread.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <runetype.h>
37 #include "libc_private.h"
38 #include "xlocale_private.h"
39 
40 /**
41  * Each locale loader declares a global component.  This is used by setlocale()
42  * and also by xlocale with LC_GLOBAL_LOCALE..
43  */
44 extern struct xlocale_component __xlocale_global_collate;
45 extern struct xlocale_component __xlocale_global_ctype;
46 extern struct xlocale_component __xlocale_global_monetary;
47 extern struct xlocale_component __xlocale_global_numeric;
48 extern struct xlocale_component __xlocale_global_time;
49 extern struct xlocale_component __xlocale_global_messages;
50 /*
51  * And another version for the statically-allocated C locale.  We only have
52  * components for the parts that are expected to be sensible.
53  */
54 extern struct xlocale_component __xlocale_C_collate;
55 extern struct xlocale_component __xlocale_C_ctype;
56 
57 #ifndef __NO_TLS
58 /*
59  * The locale for this thread.
60  */
61 __thread locale_t __thread_locale;
62 #endif
63 /*
64  * Flag indicating that one or more per-thread locales exist.
65  */
66 int __has_thread_locale;
67 /*
68  * Private functions in setlocale.c.
69  */
70 const char * __get_locale_env(int category);
71 int __get_locale_str(int category, const char *str, char * const res);
72 int __detect_path_locale(void);
73 
74 struct _xlocale __xlocale_global_locale = {
75 	.header = {0},
76 	.components = {
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 	.monetary_locale_changed = 1,
85 	.using_monetary_locale = 0,
86 	.numeric_locale_changed = 1,
87 	.using_numeric_locale = 0
88 };
89 
90 struct _xlocale __xlocale_C_locale = {
91 	.header = {0},
92 	.components = {
93 		&__xlocale_C_collate,
94 		&__xlocale_C_ctype,
95 		0, 0, 0, 0
96 	},
97 	.monetary_locale_changed = 1,
98 	.using_monetary_locale = 0,
99 	.numeric_locale_changed = 1,
100 	.using_numeric_locale = 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 	const char *np, *cp;
255 	char lres[ENCODING_LEN + 1] = "";
256 	int len;
257 	int useenv = 0;
258 	int useslh = 0;
259 	int usestr = 0;
260 	int success = 1;
261 
262 	_once(&once_control, init_key);
263 
264 	locale_t new = alloc_locale();
265 	if (NULL == new) {
266 		return (NULL);
267 	}
268 
269 	FIX_LOCALE(base);
270 	copyflags(new, base);
271 
272 	if (NULL == locale) {
273 		realLocale = "C";
274 	} else if ('\0' == locale[0]) {
275 		useenv = 1;
276 	} else if (strchr(locale, '/') != NULL) {
277 		/*
278 		 * Handle system native locale string
279 		 * e.g. "C/en_US.UTF-8/C/C/lt_LT/C"
280 		 */
281 		useslh = 1;
282 		np = locale;
283 	} else if ('L' == locale[0] && strchr(locale, ';') != NULL) {
284 		/*
285 		 * We are called from c++ runtime lib with LC_*; string??
286 		 */
287 		usestr = 1;
288 	}
289 
290 	for (type=0 ; type<XLC_LAST ; type++) {
291 		if (useslh) {
292 			cp = strchr(np, '/');
293 			if (cp == NULL && type == XLC_LAST - 1) {
294 				cp = locale + strlen(locale);
295 			} else if (cp == NULL || type == XLC_LAST - 1) {
296 				errno = EINVAL;
297 				success = 0;
298 				break;
299 			}
300 			len = cp - np;
301 			if (len > ENCODING_LEN || len <= 0) {
302 				errno = EINVAL;
303 				success = 0;
304 				break;
305 			}
306 			strncpy(lres, np, len);
307 			lres[len] = '\0';
308 			np = cp + 1;
309 		}
310 
311 		if (mask & 1) {
312 			if (useenv) {
313 				realLocale = __get_locale_env(type + 1);
314 			} else if (useslh) {
315 				realLocale = lres;
316 			} else if (usestr) {
317 				__get_locale_str(type + 1, locale, lres);
318 				realLocale = lres;
319 			}
320 			new->components[type] =
321 			     constructors[type](realLocale, new);
322 			if (new->components[type]) {
323 				strncpy(new->components[type]->locale,
324 				     realLocale, ENCODING_LEN);
325 			} else {
326 				success = 0;
327 				break;
328 			}
329 		} else {
330 			if (!dupcomponent(type, base, new)) {
331 				success = 0;
332 				break;
333 			}
334 		}
335 		mask >>= 1;
336 	}
337 	if (0 == success) {
338 		xlocale_release(new);
339 		new = NULL;
340 	}
341 
342 	return (new);
343 }
344 
345 locale_t duplocale(locale_t base)
346 {
347 	locale_t new = alloc_locale();
348 	int type;
349 
350 	_once(&once_control, init_key);
351 
352 	if (NULL == new) {
353 		return (NULL);
354 	}
355 
356 	FIX_LOCALE(base);
357 	copyflags(new, base);
358 
359 	for (type=0 ; type<XLC_LAST ; type++) {
360 		dupcomponent(type, base, new);
361 	}
362 
363 	return (new);
364 }
365 
366 /*
367  * Free a locale_t.  This is quite a poorly named function.  It actually
368  * disclaims a reference to a locale_t, rather than freeing it.
369  */
370 void
371 freelocale(locale_t loc)
372 {
373 
374 	/*
375 	 * Fail if we're passed something that isn't a locale. If we're
376 	 * passed the global locale, pretend that we freed it but don't
377 	 * actually do anything.
378 	 */
379 	if (loc != NULL && loc != LC_GLOBAL_LOCALE &&
380 	    loc != &__xlocale_global_locale)
381 		xlocale_release(loc);
382 }
383 
384 /*
385  * Returns the name of the locale for a particular component of a locale_t.
386  */
387 const char *querylocale(int mask, locale_t loc)
388 {
389 	int type = ffs(mask) - 1;
390 	FIX_LOCALE(loc);
391 	if (type >= XLC_LAST)
392 		return (NULL);
393 	if (loc->components[type])
394 		return (loc->components[type]->locale);
395 	return ("C");
396 }
397 
398 /*
399  * Installs the specified locale_t as this thread's locale.
400  */
401 locale_t uselocale(locale_t loc)
402 {
403 	locale_t old = get_thread_locale();
404 	if (NULL != loc) {
405 		set_thread_locale(loc);
406 	}
407 	return (old ? old : LC_GLOBAL_LOCALE);
408 }
409 
410