xref: /dragonfly/lib/libc/locale/xlocale.c (revision 655933d6)
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 "namespace.h"
33 #include <errno.h>
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 #include "un-namespace.h"
41 
42 /**
43  * Each locale loader declares a global component.  This is used by setlocale()
44  * and also by xlocale with LC_GLOBAL_LOCALE..
45  */
46 extern struct xlocale_component __xlocale_global_collate;
47 extern struct xlocale_component __xlocale_global_ctype;
48 extern struct xlocale_component __xlocale_global_monetary;
49 extern struct xlocale_component __xlocale_global_numeric;
50 extern struct xlocale_component __xlocale_global_time;
51 extern struct xlocale_component __xlocale_global_messages;
52 /*
53  * And another version for the statically-allocated C locale.  We only have
54  * components for the parts that are expected to be sensible.
55  */
56 extern struct xlocale_component __xlocale_C_collate;
57 extern struct xlocale_component __xlocale_C_ctype;
58 
59 /*
60  * The locale for this thread.
61  */
62 __thread locale_t __thread_locale;
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 static void
145 set_thread_locale(locale_t loc)
146 {
147 	locale_t l = (loc == LC_GLOBAL_LOCALE) ? 0 : loc;
148 
149 	_once(&once_control, init_key);
150 
151 	if (NULL != l) {
152 		xlocale_retain((struct xlocale_refcounted*)l);
153 	}
154 	locale_t old = _pthread_getspecific(locale_info_key);
155 	if ((NULL != old) && (l != old)) {
156 		xlocale_release((struct xlocale_refcounted*)old);
157 	}
158 	if (fake_tls) {
159 		thread_local_locale = l;
160 	} else {
161 		_pthread_setspecific(locale_info_key, l);
162 	}
163 	__thread_locale = l;
164 	__set_thread_rune_locale(loc);
165 }
166 
167 /**
168  * Clean up a locale, once its reference count reaches zero.  This function is
169  * called by xlocale_release(), it should not be called directly.
170  */
171 static void
172 destruct_locale(void *l)
173 {
174 	locale_t loc = l;
175 
176 	for (int type=0 ; type<XLC_LAST ; type++) {
177 		if (loc->components[type]) {
178 			xlocale_release(loc->components[type]);
179 		}
180 	}
181 	if (loc->csym) {
182 		free(loc->csym);
183 	}
184 	free(l);
185 }
186 
187 /**
188  * Allocates a new, uninitialised, locale.
189  */
190 static locale_t
191 alloc_locale(void)
192 {
193 	locale_t new = calloc(sizeof(struct _xlocale), 1);
194 
195 	new->header.destructor = destruct_locale;
196 	new->monetary_locale_changed = 1;
197 	new->numeric_locale_changed = 1;
198 	return (new);
199 }
200 static void
201 copyflags(locale_t new, locale_t old)
202 {
203 	new->using_monetary_locale = old->using_monetary_locale;
204 	new->using_numeric_locale = old->using_numeric_locale;
205 	new->using_time_locale = old->using_time_locale;
206 	new->using_messages_locale = old->using_messages_locale;
207 }
208 
209 static int dupcomponent(int type, locale_t base, locale_t new)
210 {
211 	/* Always copy from the global locale, since it has mutable components.
212 	 */
213 	struct xlocale_component *src = base->components[type];
214 
215 	if (&__xlocale_global_locale == base) {
216 		new->components[type] = constructors[type](src->locale, new);
217 		if (new->components[type]) {
218 			strncpy(new->components[type]->locale, src->locale,
219 			    ENCODING_LEN);
220 		}
221 	} else if (base->components[type]) {
222 		new->components[type] = xlocale_retain(base->components[type]);
223 	} else {
224 		/* If the component was NULL, return success - if base is a
225 		 * valid locale then the flag indicating that this isn't
226 		 * present should be set.  If it isn't a valid locale, then
227 		 * we're stuck anyway. */
228 		return 1;
229 	}
230 	return (0 != new->components[type]);
231 }
232 
233 /*
234  * Public interfaces.  These are the five public functions described by the
235  * xlocale interface.
236  */
237 
238 locale_t newlocale(int mask, const char *locale, locale_t base)
239 {
240 	int type;
241 	const char *realLocale = locale;
242 	const char *np, *cp;
243 	char lres[ENCODING_LEN + 1] = "";
244 	int len;
245 	int useenv = 0;
246 	int useslh = 0;
247 	int usestr = 0;
248 	int success = 1;
249 
250 	_once(&once_control, init_key);
251 
252 	locale_t new = alloc_locale();
253 	if (NULL == new) {
254 		return (NULL);
255 	}
256 
257 	FIX_LOCALE(base);
258 	copyflags(new, base);
259 
260 	if (NULL == locale) {
261 		realLocale = "C";
262 	} else if ('\0' == locale[0]) {
263 		useenv = 1;
264 	} else if (strchr(locale, '/') != NULL) {
265 		/*
266 		 * Handle system native locale string
267 		 * e.g. "C/en_US.UTF-8/C/C/lt_LT/C"
268 		 */
269 		useslh = 1;
270 		np = locale;
271 	} else if ('L' == locale[0] && strchr(locale, ';') != NULL) {
272 		/*
273 		 * We are called from c++ runtime lib with LC_*; string??
274 		 */
275 		usestr = 1;
276 	}
277 
278 	for (type=0 ; type<XLC_LAST ; type++) {
279 		if (useslh) {
280 			cp = strchr(np, '/');
281 			if (cp == NULL && type == XLC_LAST - 1) {
282 				cp = locale + strlen(locale);
283 			} else if (cp == NULL || type == XLC_LAST - 1) {
284 				errno = EINVAL;
285 				success = 0;
286 				break;
287 			}
288 			len = cp - np;
289 			if (len > ENCODING_LEN || len <= 0) {
290 				errno = EINVAL;
291 				success = 0;
292 				break;
293 			}
294 			strncpy(lres, np, len);
295 			lres[len] = '\0';
296 			np = cp + 1;
297 		}
298 
299 		if (mask & 1) {
300 			if (useenv) {
301 				realLocale = __get_locale_env(type + 1);
302 			} else if (useslh) {
303 				realLocale = lres;
304 			} else if (usestr) {
305 				__get_locale_str(type + 1, locale, lres);
306 				realLocale = lres;
307 			}
308 			new->components[type] =
309 			     constructors[type](realLocale, new);
310 			if (new->components[type]) {
311 				strncpy(new->components[type]->locale,
312 				     realLocale, ENCODING_LEN);
313 			} else {
314 				success = 0;
315 				break;
316 			}
317 		} else {
318 			if (!dupcomponent(type, base, new)) {
319 				success = 0;
320 				break;
321 			}
322 		}
323 		mask >>= 1;
324 	}
325 	if (0 == success) {
326 		xlocale_release(new);
327 		new = NULL;
328 	}
329 
330 	return (new);
331 }
332 
333 locale_t duplocale(locale_t base)
334 {
335 	locale_t new = alloc_locale();
336 	int type;
337 
338 	_once(&once_control, init_key);
339 
340 	if (NULL == new) {
341 		return (NULL);
342 	}
343 
344 	FIX_LOCALE(base);
345 	copyflags(new, base);
346 
347 	for (type=0 ; type<XLC_LAST ; type++) {
348 		dupcomponent(type, base, new);
349 	}
350 
351 	return (new);
352 }
353 
354 /*
355  * Free a locale_t.  This is quite a poorly named function.  It actually
356  * disclaims a reference to a locale_t, rather than freeing it.
357  */
358 void
359 freelocale(locale_t loc)
360 {
361 
362 	/*
363 	 * Fail if we're passed something that isn't a locale. If we're
364 	 * passed the global locale, pretend that we freed it but don't
365 	 * actually do anything.
366 	 */
367 	if (loc != NULL && loc != LC_GLOBAL_LOCALE &&
368 	    loc != &__xlocale_global_locale)
369 		xlocale_release(loc);
370 }
371 
372 /*
373  * Returns the name of the locale for a particular component of a locale_t.
374  */
375 const char *querylocale(int mask, locale_t loc)
376 {
377 	int type = ffs(mask) - 1;
378 	FIX_LOCALE(loc);
379 	if (type >= XLC_LAST)
380 		return (NULL);
381 	if (loc->components[type])
382 		return (loc->components[type]->locale);
383 	return ("C");
384 }
385 
386 /*
387  * Installs the specified locale_t as this thread's locale.
388  */
389 locale_t uselocale(locale_t loc)
390 {
391 	locale_t old = get_thread_locale();
392 	if (NULL != loc) {
393 		set_thread_locale(loc);
394 	}
395 	return (old ? old : LC_GLOBAL_LOCALE);
396 }
397 
398