/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
*/
/*
* Copyright 2014 Garrett D'Amore <garrett@damore.org>
*/
/*
* This file implements the 2008 newlocale and friends handling.
*/
#ifndef _LCONV_C99
#define _LCONV_C99
#endif
#include "lint.h"
#include <atomic.h>
#include <locale.h>
#include <errno.h>
#include <string.h>
#include "libc.h"
#include "mtlib.h"
#include "tsd.h"
#include "localeimpl.h"
#include "lctype.h"
/*
* Big Theory of Locales:
*
* (It is recommended that readers familiarize themselves with the POSIX
* 2008 (XPG Issue 7) specifications for locales, first.)
*
* Historically, we had a bunch of global variables that stored locale
* data. While this worked well, it limited applications to a single locale
* at a time. This doesn't work well in certain server applications.
*
* versions of functions that can take this object as a parameter, along
* with functions to clone and manipulate these locale objects. The new
* functions are named with a _l() suffix.
*
* Additionally uselocale() is introduced which can change the locale of
* of a single thread. However, setlocale() can still be used to change
* the global locale.
*
* In our implementation, we use libc's TSD to store the locale data that
* was previously global. We still have global data because some applications
* have had those global objects compiled into them. (Such applications will
* be unable to benefit from uselocale(), btw.) The legacy routines are
* reimplemented as wrappers that use the appropriate locale object by
* calling uselocale(). uselocale() when passed a NULL pointer returns the
* thread-specific locale object if one is present, or the global locale
* object otherwise. Note that once the TSD data is set, the only way
* to revert to the global locale is to pass the global locale LC_GLOBAL_LOCALE
* to uselocale().
*
* We are careful to minimize performance impact of multiple calls to
* uselocale() or setlocale() by using a cache of locale data whenever possible.
* As a consequence of this, applications that iterate over all possible
* locales will burn through a lot of virtual memory, but we find such
* applications rare. (locale -a might be an exception, but it is short lived.)
*
* Category data is never released (although enclosing locale objects might be),
* in order to guarantee thread-safety. Calling freelocale() on an object
* while it is in use by another thread is a programmer error (use-after-free)
* and we don't bother to note it further.
*
* Locale objects (global locales) established by setlocale() are also
* never freed (for MT safety), but we will save previous locale objects
* and reuse them when we can.
*/
};
extern struct lc_monetary lc_monetary_posix;
extern struct lc_numeric lc_numeric_posix;
extern struct lc_messages lc_messages_posix;
extern struct lc_time lc_time_posix;
extern struct lc_ctype lc_ctype_posix;
extern struct lc_collate lc_collate_posix;
extern struct _RuneLocale _DefaultRuneLocale;
/* locdata */
.locdata = {
},
.locname = "C",
.ctype = &lc_ctype_posix,
.numeric = &lc_numeric_posix,
.collate = &lc_collate_posix,
.time = &lc_time_posix,
};
__global_locale(void)
{
return (___global_locale);
}
/*
* Category names for getenv() Note that this was modified
* for Solaris. See <iso/locale_iso.h>.
*/
"LC_CTYPE",
"LC_NUMERIC",
"LC_TIME",
"LC_COLLATE",
"LC_MONETARY",
"LC_MESSAGES",
"LC_ALL",
};
/*
* Prototypes.
*/
static const char *get_locale_env(int);
static struct locdata *locdata_get(int, const const char *);
static struct locdata *locdata_get_cache(int, const char *);
/*
* Some utility routines.
*/
struct locdata *
{
return (NULL);
}
return (NULL);
}
return (ldata);
}
/*
* Normally we never free locale data truly, but if we failed to load it
* for some reason, this routine is used to cleanup the partial mess.
*/
void
{
for (int i = 0; i < NLOCDATA; i++)
}
/*
* It turns out that for performance reasons we would really like to
* cache the most recently referenced locale data to avoid wasteful
* loading from files.
*/
/*
* Returns the cached data if the locale name is the same. If not,
* returns NULL (cache miss). The locdata is returned with a hold on
* it, taken on behalf of the caller. The caller should drop the hold
* when it is finished.
*/
static struct locdata *
{
return (NULL);
/* Try cache first. */
return (loc);
}
/*
* Failing that try previously loaded locales (linear search) --
* this could be optimized to a hash, but its unlikely that a single
* application will ever need to work with more than a few locales.
*/
break;
}
}
/*
* Finally, if we still don't have one, try loading the locale
* data from the actual on-disk data.
*
* We drop the lock (libc wants to ensure no internal locks
* are held when we call other routines required to read from
* files, allocate memory, etc.) There is a small race here,
* but the consequences of the race are benign -- if multiple
* threads hit this at precisely the same point, we could
* wind up with duplicates of the locale data in the cache.
*
* This wastes the memory for an extra copy of the locale
* data, but there is no further harm beyond that. Its not
* worth the effort to recode this to something "safe"
* (which would require rescanning the list, etc.), given
* that this race will probably never actually occur.
*/
}
/*
* Assuming we got one, update the cache, and stick us on the list
* of loaded locale data. We insert into the head (more recent
* use is likely to win.)
*/
}
}
return (loc);
}
/*
* Routine to get the locdata for a given category and locale.
* This includes retrieving it from cache, retrieving it from
* a file, etc.
*/
static struct locdata *
{
char *slash;
int cnt;
int len;
}
/*
* Extract the locale name for the category if it is a composite
* locale.
*/
}
if (slash) {
}
} else {
}
}
}
/* tsd destructor */
static void
{
freelocale(*locptr);
}
static const char *
{
const char *env;
/* 1. check LC_ALL. */
/* 2. check LC_* */
/* 3. check LANG */
/* 4. if none is set, fall to "C" */
env = "C";
return (env);
}
/*
* This routine is exposed via the MB_CUR_MAX macro. Note that legacy
* code will continue to use _ctype[520], but we prefer this function as
* it is the only way to get thread-specific information.
*/
unsigned char
{
}
unsigned char
__mb_cur_max(void)
{
}
/*
* Public interfaces.
*/
{
int i;
return (NULL);
}
/* illumos extension: POSIX says LC_GLOBAL_LOCALE here */
}
for (i = 0; i < LC_ALL; i++) {
}
return (loc);
}
void
{
/*
* We take extra care never to free a saved locale created by
* setlocale(). This shouldn't be strictly necessary, but a little
* extra safety doesn't hurt here.
*/
}
{
int i, e;
if (catmask & ~(LC_ALL_MASK)) {
return (NULL);
}
/*
* Technically passing LC_GLOBAL_LOCALE here is illegal,
* but we allow it.
*/
} else {
}
return (NULL);
}
for (i = 0; i < LC_ALL; i++) {
if (((1 << i) & catmask) == 0) {
/* Default to base locale if not overriding */
continue;
}
e = errno;
errno = e;
return (NULL);
}
}
}
{
/* Should never occur */
return (NULL);
}
/* Argument loc is NULL if we are just querying. */
/*
* Set it to LC_GLOBAL_LOCAL to return to using
* the global locale (setlocale).
*/
if (loc == ___global_locale) {
} else {
/* No validation of the provided locale at present */
}
}
/*
* The caller is responsible for freeing, of course it would be
* gross error to call freelocale() on a locale object that is still
* in use.
*/
return (lastloc);
}
static locale_t
{
int composite = 0;
/* Look to see if any category is different */
for (int i = 1; i < LC_ALL; ++i) {
composite = 1;
break;
}
}
if (composite) {
/*
* Note ordering of these follows the numeric order,
* if the order is changed, then setlocale() will need
* to be changed as well.
*/
"%s/%s/%s/%s/%s/%s",
} else {
}
return (loc);
}