10d5acd74SJohn Marino /*-
20d5acd74SJohn Marino * Copyright (c) 2011 The FreeBSD Foundation
30d5acd74SJohn Marino * All rights reserved.
40d5acd74SJohn Marino *
50d5acd74SJohn Marino * This software was developed by David Chisnall under sponsorship from
60d5acd74SJohn Marino * the FreeBSD Foundation.
70d5acd74SJohn Marino *
80d5acd74SJohn Marino * Redistribution and use in source and binary forms, with or without
90d5acd74SJohn Marino * modification, are permitted provided that the following conditions
100d5acd74SJohn Marino * are met:
110d5acd74SJohn Marino * 1. Redistributions of source code must retain the above copyright
120d5acd74SJohn Marino * notice, this list of conditions and the following disclaimer.
130d5acd74SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
140d5acd74SJohn Marino * notice, this list of conditions and the following disclaimer in the
150d5acd74SJohn Marino * documentation and/or other materials provided with the distribution.
160d5acd74SJohn Marino *
170d5acd74SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
180d5acd74SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
190d5acd74SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200d5acd74SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
210d5acd74SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
220d5acd74SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
230d5acd74SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
240d5acd74SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
250d5acd74SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
260d5acd74SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
270d5acd74SJohn Marino * SUCH DAMAGE.
280d5acd74SJohn Marino *
291c87de76Szrj * $FreeBSD: head/lib/libc/locale/xlocale.c 303495 2016-07-29 17:18:47Z ed $
300d5acd74SJohn Marino */
310d5acd74SJohn Marino
32*806a5ed7SSascha Wildner #include "namespace.h"
332c92c360Szrj #include <errno.h>
340d5acd74SJohn Marino #include <pthread.h>
350d5acd74SJohn Marino #include <stdio.h>
360d5acd74SJohn Marino #include <string.h>
370d5acd74SJohn Marino #include <runetype.h>
380d5acd74SJohn Marino #include "libc_private.h"
390d5acd74SJohn Marino #include "xlocale_private.h"
40*806a5ed7SSascha Wildner #include "un-namespace.h"
410d5acd74SJohn Marino
420d5acd74SJohn Marino /**
430d5acd74SJohn Marino * Each locale loader declares a global component. This is used by setlocale()
440d5acd74SJohn Marino * and also by xlocale with LC_GLOBAL_LOCALE..
450d5acd74SJohn Marino */
460d5acd74SJohn Marino extern struct xlocale_component __xlocale_global_collate;
470d5acd74SJohn Marino extern struct xlocale_component __xlocale_global_ctype;
480d5acd74SJohn Marino extern struct xlocale_component __xlocale_global_monetary;
490d5acd74SJohn Marino extern struct xlocale_component __xlocale_global_numeric;
500d5acd74SJohn Marino extern struct xlocale_component __xlocale_global_time;
510d5acd74SJohn Marino extern struct xlocale_component __xlocale_global_messages;
520d5acd74SJohn Marino /*
530d5acd74SJohn Marino * And another version for the statically-allocated C locale. We only have
540d5acd74SJohn Marino * components for the parts that are expected to be sensible.
550d5acd74SJohn Marino */
560d5acd74SJohn Marino extern struct xlocale_component __xlocale_C_collate;
570d5acd74SJohn Marino extern struct xlocale_component __xlocale_C_ctype;
580d5acd74SJohn Marino
590d5acd74SJohn Marino /*
600d5acd74SJohn Marino * The locale for this thread.
610d5acd74SJohn Marino */
620d5acd74SJohn Marino __thread locale_t __thread_locale;
630d5acd74SJohn Marino /*
640d5acd74SJohn Marino * Flag indicating that one or more per-thread locales exist.
650d5acd74SJohn Marino */
660d5acd74SJohn Marino int __has_thread_locale;
670d5acd74SJohn Marino /*
680d5acd74SJohn Marino * Private functions in setlocale.c.
690d5acd74SJohn Marino */
7044783347Szrj const char * __get_locale_env(int category);
71a0507d3dSzrj int __get_locale_str(int category, const char *str, char * const res);
7244783347Szrj int __detect_path_locale(void);
730d5acd74SJohn Marino
740d5acd74SJohn Marino struct _xlocale __xlocale_global_locale = {
756173c3dfSSascha Wildner .header = {0},
766173c3dfSSascha Wildner .components = {
770d5acd74SJohn Marino &__xlocale_global_collate,
780d5acd74SJohn Marino &__xlocale_global_ctype,
790d5acd74SJohn Marino &__xlocale_global_monetary,
800d5acd74SJohn Marino &__xlocale_global_numeric,
810d5acd74SJohn Marino &__xlocale_global_time,
820d5acd74SJohn Marino &__xlocale_global_messages
830d5acd74SJohn Marino },
846173c3dfSSascha Wildner .monetary_locale_changed = 1,
856173c3dfSSascha Wildner .using_monetary_locale = 0,
866173c3dfSSascha Wildner .numeric_locale_changed = 1,
876173c3dfSSascha Wildner .using_numeric_locale = 0
880d5acd74SJohn Marino };
890d5acd74SJohn Marino
900d5acd74SJohn Marino struct _xlocale __xlocale_C_locale = {
916173c3dfSSascha Wildner .header = {0},
926173c3dfSSascha Wildner .components = {
930d5acd74SJohn Marino &__xlocale_C_collate,
940d5acd74SJohn Marino &__xlocale_C_ctype,
950d5acd74SJohn Marino 0, 0, 0, 0
960d5acd74SJohn Marino },
976173c3dfSSascha Wildner .monetary_locale_changed = 1,
986173c3dfSSascha Wildner .using_monetary_locale = 0,
996173c3dfSSascha Wildner .numeric_locale_changed = 1,
1006173c3dfSSascha Wildner .using_numeric_locale = 0
1010d5acd74SJohn Marino };
1020d5acd74SJohn Marino
1030d5acd74SJohn Marino static void*(*constructors[])(const char*, locale_t) =
1040d5acd74SJohn Marino {
1050d5acd74SJohn Marino __collate_load,
1060d5acd74SJohn Marino __ctype_load,
1070d5acd74SJohn Marino __monetary_load,
1080d5acd74SJohn Marino __numeric_load,
1090d5acd74SJohn Marino __time_load,
1100d5acd74SJohn Marino __messages_load
1110d5acd74SJohn Marino };
1120d5acd74SJohn Marino
1130d5acd74SJohn Marino static pthread_key_t locale_info_key;
1140d5acd74SJohn Marino static int fake_tls;
1150d5acd74SJohn Marino static locale_t thread_local_locale;
1160d5acd74SJohn Marino
init_key(void)1170d5acd74SJohn Marino static void init_key(void)
1180d5acd74SJohn Marino {
1190d5acd74SJohn Marino
120*806a5ed7SSascha Wildner _pthread_key_create(&locale_info_key, xlocale_release);
121*806a5ed7SSascha Wildner _pthread_setspecific(locale_info_key, (void*)42);
122*806a5ed7SSascha Wildner if (_pthread_getspecific(locale_info_key) == (void*)42) {
123*806a5ed7SSascha Wildner _pthread_setspecific(locale_info_key, 0);
1240d5acd74SJohn Marino } else {
1250d5acd74SJohn Marino fake_tls = 1;
1260d5acd74SJohn Marino }
1270d5acd74SJohn Marino /* At least one per-thread locale has now been set. */
1280d5acd74SJohn Marino __has_thread_locale = 1;
1290d5acd74SJohn Marino __detect_path_locale();
1300d5acd74SJohn Marino }
1310d5acd74SJohn Marino
1320d5acd74SJohn Marino static pthread_once_t once_control = PTHREAD_ONCE_INIT;
1330d5acd74SJohn Marino
1340d5acd74SJohn Marino static locale_t
get_thread_locale(void)1350d5acd74SJohn Marino get_thread_locale(void)
1360d5acd74SJohn Marino {
1370d5acd74SJohn Marino
1380d5acd74SJohn Marino _once(&once_control, init_key);
1390d5acd74SJohn Marino
1400d5acd74SJohn Marino return (fake_tls ? thread_local_locale :
141*806a5ed7SSascha Wildner _pthread_getspecific(locale_info_key));
1420d5acd74SJohn Marino }
1430d5acd74SJohn Marino
1440d5acd74SJohn Marino static void
set_thread_locale(locale_t loc)1450d5acd74SJohn Marino set_thread_locale(locale_t loc)
1460d5acd74SJohn Marino {
147bf0d950aSJohn Marino locale_t l = (loc == LC_GLOBAL_LOCALE) ? 0 : loc;
1480d5acd74SJohn Marino
1490d5acd74SJohn Marino _once(&once_control, init_key);
1500d5acd74SJohn Marino
151bf0d950aSJohn Marino if (NULL != l) {
152bf0d950aSJohn Marino xlocale_retain((struct xlocale_refcounted*)l);
1530d5acd74SJohn Marino }
154*806a5ed7SSascha Wildner locale_t old = _pthread_getspecific(locale_info_key);
155bf0d950aSJohn Marino if ((NULL != old) && (l != old)) {
1560d5acd74SJohn Marino xlocale_release((struct xlocale_refcounted*)old);
1570d5acd74SJohn Marino }
1580d5acd74SJohn Marino if (fake_tls) {
159bf0d950aSJohn Marino thread_local_locale = l;
1600d5acd74SJohn Marino } else {
161*806a5ed7SSascha Wildner _pthread_setspecific(locale_info_key, l);
1620d5acd74SJohn Marino }
163bf0d950aSJohn Marino __thread_locale = l;
1640d5acd74SJohn Marino __set_thread_rune_locale(loc);
1650d5acd74SJohn Marino }
1660d5acd74SJohn Marino
1670d5acd74SJohn Marino /**
1680d5acd74SJohn Marino * Clean up a locale, once its reference count reaches zero. This function is
1690d5acd74SJohn Marino * called by xlocale_release(), it should not be called directly.
1700d5acd74SJohn Marino */
1710d5acd74SJohn Marino static void
destruct_locale(void * l)1720d5acd74SJohn Marino destruct_locale(void *l)
1730d5acd74SJohn Marino {
1740d5acd74SJohn Marino locale_t loc = l;
1750d5acd74SJohn Marino
1760d5acd74SJohn Marino for (int type=0 ; type<XLC_LAST ; type++) {
1770d5acd74SJohn Marino if (loc->components[type]) {
1780d5acd74SJohn Marino xlocale_release(loc->components[type]);
1790d5acd74SJohn Marino }
1800d5acd74SJohn Marino }
1810d5acd74SJohn Marino if (loc->csym) {
1820d5acd74SJohn Marino free(loc->csym);
1830d5acd74SJohn Marino }
1840d5acd74SJohn Marino free(l);
1850d5acd74SJohn Marino }
1860d5acd74SJohn Marino
1870d5acd74SJohn Marino /**
1880d5acd74SJohn Marino * Allocates a new, uninitialised, locale.
1890d5acd74SJohn Marino */
1900d5acd74SJohn Marino static locale_t
alloc_locale(void)1910d5acd74SJohn Marino alloc_locale(void)
1920d5acd74SJohn Marino {
1930d5acd74SJohn Marino locale_t new = calloc(sizeof(struct _xlocale), 1);
1940d5acd74SJohn Marino
1950d5acd74SJohn Marino new->header.destructor = destruct_locale;
1960d5acd74SJohn Marino new->monetary_locale_changed = 1;
1970d5acd74SJohn Marino new->numeric_locale_changed = 1;
1980d5acd74SJohn Marino return (new);
1990d5acd74SJohn Marino }
2000d5acd74SJohn Marino static void
copyflags(locale_t new,locale_t old)2010d5acd74SJohn Marino copyflags(locale_t new, locale_t old)
2020d5acd74SJohn Marino {
2030d5acd74SJohn Marino new->using_monetary_locale = old->using_monetary_locale;
2040d5acd74SJohn Marino new->using_numeric_locale = old->using_numeric_locale;
2050d5acd74SJohn Marino new->using_time_locale = old->using_time_locale;
2060d5acd74SJohn Marino new->using_messages_locale = old->using_messages_locale;
2070d5acd74SJohn Marino }
2080d5acd74SJohn Marino
dupcomponent(int type,locale_t base,locale_t new)2090d5acd74SJohn Marino static int dupcomponent(int type, locale_t base, locale_t new)
2100d5acd74SJohn Marino {
2110d5acd74SJohn Marino /* Always copy from the global locale, since it has mutable components.
2120d5acd74SJohn Marino */
2130d5acd74SJohn Marino struct xlocale_component *src = base->components[type];
2140d5acd74SJohn Marino
2150d5acd74SJohn Marino if (&__xlocale_global_locale == base) {
2160d5acd74SJohn Marino new->components[type] = constructors[type](src->locale, new);
2170d5acd74SJohn Marino if (new->components[type]) {
2180d5acd74SJohn Marino strncpy(new->components[type]->locale, src->locale,
2190d5acd74SJohn Marino ENCODING_LEN);
2200d5acd74SJohn Marino }
2210d5acd74SJohn Marino } else if (base->components[type]) {
2220d5acd74SJohn Marino new->components[type] = xlocale_retain(base->components[type]);
2230d5acd74SJohn Marino } else {
2240d5acd74SJohn Marino /* If the component was NULL, return success - if base is a
2250d5acd74SJohn Marino * valid locale then the flag indicating that this isn't
2260d5acd74SJohn Marino * present should be set. If it isn't a valid locale, then
2270d5acd74SJohn Marino * we're stuck anyway. */
2280d5acd74SJohn Marino return 1;
2290d5acd74SJohn Marino }
2300d5acd74SJohn Marino return (0 != new->components[type]);
2310d5acd74SJohn Marino }
2320d5acd74SJohn Marino
2330d5acd74SJohn Marino /*
2340d5acd74SJohn Marino * Public interfaces. These are the five public functions described by the
2350d5acd74SJohn Marino * xlocale interface.
2360d5acd74SJohn Marino */
2370d5acd74SJohn Marino
newlocale(int mask,const char * locale,locale_t base)2380d5acd74SJohn Marino locale_t newlocale(int mask, const char *locale, locale_t base)
2390d5acd74SJohn Marino {
2400d5acd74SJohn Marino int type;
2410d5acd74SJohn Marino const char *realLocale = locale;
2422c92c360Szrj const char *np, *cp;
2432c92c360Szrj char lres[ENCODING_LEN + 1] = "";
2442c92c360Szrj int len;
2450d5acd74SJohn Marino int useenv = 0;
2462c92c360Szrj int useslh = 0;
247a0507d3dSzrj int usestr = 0;
2480d5acd74SJohn Marino int success = 1;
2490d5acd74SJohn Marino
2500d5acd74SJohn Marino _once(&once_control, init_key);
2510d5acd74SJohn Marino
2520d5acd74SJohn Marino locale_t new = alloc_locale();
2530d5acd74SJohn Marino if (NULL == new) {
2540d5acd74SJohn Marino return (NULL);
2550d5acd74SJohn Marino }
2560d5acd74SJohn Marino
2570d5acd74SJohn Marino FIX_LOCALE(base);
2580d5acd74SJohn Marino copyflags(new, base);
2590d5acd74SJohn Marino
2600d5acd74SJohn Marino if (NULL == locale) {
2610d5acd74SJohn Marino realLocale = "C";
2620d5acd74SJohn Marino } else if ('\0' == locale[0]) {
2630d5acd74SJohn Marino useenv = 1;
2642c92c360Szrj } else if (strchr(locale, '/') != NULL) {
2652c92c360Szrj /*
2662c92c360Szrj * Handle system native locale string
2672c92c360Szrj * e.g. "C/en_US.UTF-8/C/C/lt_LT/C"
2682c92c360Szrj */
2692c92c360Szrj useslh = 1;
2702c92c360Szrj np = locale;
271a0507d3dSzrj } else if ('L' == locale[0] && strchr(locale, ';') != NULL) {
272a0507d3dSzrj /*
273a0507d3dSzrj * We are called from c++ runtime lib with LC_*; string??
274a0507d3dSzrj */
275a0507d3dSzrj usestr = 1;
2760d5acd74SJohn Marino }
2770d5acd74SJohn Marino
2780d5acd74SJohn Marino for (type=0 ; type<XLC_LAST ; type++) {
2792c92c360Szrj if (useslh) {
2802c92c360Szrj cp = strchr(np, '/');
2812c92c360Szrj if (cp == NULL && type == XLC_LAST - 1) {
2822c92c360Szrj cp = locale + strlen(locale);
2832c92c360Szrj } else if (cp == NULL || type == XLC_LAST - 1) {
2842c92c360Szrj errno = EINVAL;
2852c92c360Szrj success = 0;
2862c92c360Szrj break;
2872c92c360Szrj }
2882c92c360Szrj len = cp - np;
2892c92c360Szrj if (len > ENCODING_LEN || len <= 0) {
2902c92c360Szrj errno = EINVAL;
2912c92c360Szrj success = 0;
2922c92c360Szrj break;
2932c92c360Szrj }
2942c92c360Szrj strncpy(lres, np, len);
2952c92c360Szrj lres[len] = '\0';
2962c92c360Szrj np = cp + 1;
2972c92c360Szrj }
2982c92c360Szrj
2990d5acd74SJohn Marino if (mask & 1) {
3000d5acd74SJohn Marino if (useenv) {
301a0507d3dSzrj realLocale = __get_locale_env(type + 1);
3022c92c360Szrj } else if (useslh) {
3032c92c360Szrj realLocale = lres;
3042c92c360Szrj } else if (usestr) {
305a0507d3dSzrj __get_locale_str(type + 1, locale, lres);
306a0507d3dSzrj realLocale = lres;
3070d5acd74SJohn Marino }
3080d5acd74SJohn Marino new->components[type] =
3090d5acd74SJohn Marino constructors[type](realLocale, new);
3100d5acd74SJohn Marino if (new->components[type]) {
3110d5acd74SJohn Marino strncpy(new->components[type]->locale,
3120d5acd74SJohn Marino realLocale, ENCODING_LEN);
3130d5acd74SJohn Marino } else {
3140d5acd74SJohn Marino success = 0;
3150d5acd74SJohn Marino break;
3160d5acd74SJohn Marino }
3170d5acd74SJohn Marino } else {
3180d5acd74SJohn Marino if (!dupcomponent(type, base, new)) {
3190d5acd74SJohn Marino success = 0;
3200d5acd74SJohn Marino break;
3210d5acd74SJohn Marino }
3220d5acd74SJohn Marino }
3230d5acd74SJohn Marino mask >>= 1;
3240d5acd74SJohn Marino }
3250d5acd74SJohn Marino if (0 == success) {
3260d5acd74SJohn Marino xlocale_release(new);
3270d5acd74SJohn Marino new = NULL;
3280d5acd74SJohn Marino }
3290d5acd74SJohn Marino
3300d5acd74SJohn Marino return (new);
3310d5acd74SJohn Marino }
3320d5acd74SJohn Marino
duplocale(locale_t base)3330d5acd74SJohn Marino locale_t duplocale(locale_t base)
3340d5acd74SJohn Marino {
3350d5acd74SJohn Marino locale_t new = alloc_locale();
3360d5acd74SJohn Marino int type;
3370d5acd74SJohn Marino
3380d5acd74SJohn Marino _once(&once_control, init_key);
3390d5acd74SJohn Marino
3400d5acd74SJohn Marino if (NULL == new) {
3410d5acd74SJohn Marino return (NULL);
3420d5acd74SJohn Marino }
3430d5acd74SJohn Marino
3440d5acd74SJohn Marino FIX_LOCALE(base);
3450d5acd74SJohn Marino copyflags(new, base);
3460d5acd74SJohn Marino
3470d5acd74SJohn Marino for (type=0 ; type<XLC_LAST ; type++) {
3480d5acd74SJohn Marino dupcomponent(type, base, new);
3490d5acd74SJohn Marino }
3500d5acd74SJohn Marino
3510d5acd74SJohn Marino return (new);
3520d5acd74SJohn Marino }
3530d5acd74SJohn Marino
3540d5acd74SJohn Marino /*
3550d5acd74SJohn Marino * Free a locale_t. This is quite a poorly named function. It actually
3560d5acd74SJohn Marino * disclaims a reference to a locale_t, rather than freeing it.
3570d5acd74SJohn Marino */
3581c87de76Szrj void
freelocale(locale_t loc)3590d5acd74SJohn Marino freelocale(locale_t loc)
3600d5acd74SJohn Marino {
3611c87de76Szrj
3621c87de76Szrj /*
3631c87de76Szrj * Fail if we're passed something that isn't a locale. If we're
3641c87de76Szrj * passed the global locale, pretend that we freed it but don't
3651c87de76Szrj * actually do anything.
3661c87de76Szrj */
3671c87de76Szrj if (loc != NULL && loc != LC_GLOBAL_LOCALE &&
3681c87de76Szrj loc != &__xlocale_global_locale)
3690d5acd74SJohn Marino xlocale_release(loc);
3700d5acd74SJohn Marino }
3710d5acd74SJohn Marino
3720d5acd74SJohn Marino /*
3730d5acd74SJohn Marino * Returns the name of the locale for a particular component of a locale_t.
3740d5acd74SJohn Marino */
querylocale(int mask,locale_t loc)3750d5acd74SJohn Marino const char *querylocale(int mask, locale_t loc)
3760d5acd74SJohn Marino {
3770d5acd74SJohn Marino int type = ffs(mask) - 1;
3780d5acd74SJohn Marino FIX_LOCALE(loc);
3790d5acd74SJohn Marino if (type >= XLC_LAST)
3800d5acd74SJohn Marino return (NULL);
3810d5acd74SJohn Marino if (loc->components[type])
3820d5acd74SJohn Marino return (loc->components[type]->locale);
3830d5acd74SJohn Marino return ("C");
3840d5acd74SJohn Marino }
3850d5acd74SJohn Marino
3860d5acd74SJohn Marino /*
3870d5acd74SJohn Marino * Installs the specified locale_t as this thread's locale.
3880d5acd74SJohn Marino */
uselocale(locale_t loc)3890d5acd74SJohn Marino locale_t uselocale(locale_t loc)
3900d5acd74SJohn Marino {
3910d5acd74SJohn Marino locale_t old = get_thread_locale();
3920d5acd74SJohn Marino if (NULL != loc) {
3930d5acd74SJohn Marino set_thread_locale(loc);
3940d5acd74SJohn Marino }
3950d5acd74SJohn Marino return (old ? old : LC_GLOBAL_LOCALE);
3960d5acd74SJohn Marino }
3970d5acd74SJohn Marino
398