2013-12-06 12:04:52 +01:00
|
|
|
/* $NetBSD: nb_lc_template.h,v 1.8 2013/09/13 13:13:32 joerg Exp $ */
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c)1999, 2008 Citrus Project,
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Paul Kranenburg.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Paul Borman at Krystal Technologies.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _NB_LC_TEMPLATE_H_
|
|
|
|
#define _NB_LC_TEMPLATE_H_
|
|
|
|
|
|
|
|
#define _nb_part_t _PREFIX(part_t)
|
|
|
|
#define _nb_part_cache _PREFIX(part_cache)
|
|
|
|
#define _nb_mutex _PREFIX(mutex)
|
|
|
|
|
|
|
|
typedef struct _nb_part_t {
|
|
|
|
char name[_LOCALENAME_LEN_MAX];
|
|
|
|
_CATEGORY_TYPE *impl;
|
|
|
|
SIMPLEQ_ENTRY(_nb_part_t) entry;
|
|
|
|
} _nb_part_t;
|
|
|
|
|
|
|
|
static SIMPLEQ_HEAD(, _nb_part_t) _nb_part_cache =
|
|
|
|
SIMPLEQ_HEAD_INITIALIZER(_nb_part_cache);
|
|
|
|
|
|
|
|
#ifdef _REENTRANT
|
|
|
|
static mutex_t _nb_mutex = MUTEX_INITIALIZER;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
|
|
|
_PREFIX(load_sub)(const char * __restrict name, const char * __restrict real,
|
2013-12-06 12:04:52 +01:00
|
|
|
const char ** __restrict out_name, _CATEGORY_TYPE ** __restrict out_impl,
|
|
|
|
int force)
|
2011-02-14 20:36:03 +01:00
|
|
|
{
|
2013-12-06 12:04:52 +01:00
|
|
|
const char *cached_name;
|
|
|
|
_CATEGORY_TYPE *cached_impl;
|
|
|
|
_nb_part_t *p;
|
2011-02-14 20:36:03 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
_DIAGASSERT(name != NULL);
|
2013-12-06 12:04:52 +01:00
|
|
|
_DIAGASSERT(out_name != NULL);
|
|
|
|
_DIAGASSERT(out_impl != NULL);
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
if (!strcmp(_C_LOCALE, name)) {
|
2013-12-06 12:04:52 +01:00
|
|
|
cached_name = _lc_C_locale.part_name[_CATEGORY_ID];
|
|
|
|
cached_impl = _lc_C_locale.part_impl[_CATEGORY_ID];
|
2011-02-14 20:36:03 +01:00
|
|
|
} else if (!strcmp(_POSIX_LOCALE, name)) {
|
2013-12-06 12:04:52 +01:00
|
|
|
cached_name = _POSIX_LOCALE;
|
|
|
|
cached_impl = _lc_C_locale.part_impl[_CATEGORY_ID];
|
2011-02-14 20:36:03 +01:00
|
|
|
} else {
|
|
|
|
SIMPLEQ_FOREACH(p, &_nb_part_cache, entry) {
|
2013-12-06 12:04:52 +01:00
|
|
|
if (!strcmp((const char *)&p->name[0], name)) {
|
|
|
|
cached_name = p->name;
|
|
|
|
cached_impl = p->impl;
|
2011-02-14 20:36:03 +01:00
|
|
|
goto found;
|
2013-12-06 12:04:52 +01:00
|
|
|
}
|
2011-02-14 20:36:03 +01:00
|
|
|
}
|
|
|
|
p = malloc(sizeof(*p));
|
|
|
|
if (p == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
if (force) {
|
2013-12-06 12:04:52 +01:00
|
|
|
p->impl = _lc_C_locale.part_impl[_CATEGORY_ID];
|
2011-02-14 20:36:03 +01:00
|
|
|
} else {
|
|
|
|
_DIAGASSERT(_PathLocale != NULL);
|
|
|
|
ret = _PREFIX(create_impl)((const char *)_PathLocale,
|
|
|
|
name, &p->impl);
|
|
|
|
if (ret) {
|
|
|
|
free(p);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strlcpy(&p->name[0], name, sizeof(p->name));
|
|
|
|
SIMPLEQ_INSERT_TAIL(&_nb_part_cache, p, entry);
|
2013-12-06 12:04:52 +01:00
|
|
|
cached_name = p->name;
|
|
|
|
cached_impl = p->impl;
|
2011-02-14 20:36:03 +01:00
|
|
|
}
|
|
|
|
found:
|
|
|
|
if (real != NULL) {
|
2013-12-06 12:04:52 +01:00
|
|
|
p = malloc(sizeof(*p));
|
|
|
|
if (p == NULL)
|
2011-02-14 20:36:03 +01:00
|
|
|
return ENOMEM;
|
2013-12-06 12:04:52 +01:00
|
|
|
strlcpy(&p->name[0], real, sizeof(p->name));
|
|
|
|
cached_name = p->name;
|
|
|
|
p->impl = cached_impl;
|
|
|
|
SIMPLEQ_INSERT_TAIL(&_nb_part_cache, p, entry);
|
2011-02-14 20:36:03 +01:00
|
|
|
}
|
2013-12-06 12:04:52 +01:00
|
|
|
*out_name = cached_name;
|
|
|
|
*out_impl = cached_impl;
|
2011-02-14 20:36:03 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline int
|
|
|
|
_PREFIX(load)(const char * __restrict name,
|
2013-12-06 12:04:52 +01:00
|
|
|
const char ** __restrict out_name, _CATEGORY_TYPE ** __restrict out_impl)
|
2011-02-14 20:36:03 +01:00
|
|
|
{
|
|
|
|
int ret, force;
|
|
|
|
char path[PATH_MAX + 1], loccat[PATH_MAX + 1], buf[PATH_MAX + 1];
|
|
|
|
const char *aliaspath, *alias;
|
|
|
|
|
|
|
|
#define _LOAD_SUB_ALIAS(key) \
|
|
|
|
do { \
|
|
|
|
alias = __unaliasname(aliaspath, key, &buf[0], sizeof(buf)); \
|
|
|
|
if (alias != NULL) { \
|
|
|
|
ret = (force = !__isforcemapping(alias)) \
|
2013-12-06 12:04:52 +01:00
|
|
|
? _PREFIX(load_sub)(name, NULL, out_name, out_impl, \
|
|
|
|
force) \
|
|
|
|
: _PREFIX(load_sub)(alias, name, out_name, out_impl, \
|
|
|
|
force); \
|
2011-02-14 20:36:03 +01:00
|
|
|
_DIAGASSERT(!ret || !force); \
|
|
|
|
goto done; \
|
|
|
|
} \
|
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
|
|
|
|
/* (1) non-aliased file */
|
|
|
|
mutex_lock(&_nb_mutex);
|
2013-12-06 12:04:52 +01:00
|
|
|
ret = _PREFIX(load_sub)(name, NULL, out_name, out_impl, 0);
|
2011-02-14 20:36:03 +01:00
|
|
|
if (ret != ENOENT)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* (2) lookup locname/catname type alias */
|
|
|
|
_DIAGASSERT(_PathLocale != NULL);
|
|
|
|
snprintf(&path[0], sizeof(path),
|
|
|
|
"%s/" _LOCALE_ALIAS_NAME, _PathLocale);
|
|
|
|
aliaspath = (const char *)&path[0];
|
|
|
|
snprintf(&loccat[0], sizeof(loccat),
|
|
|
|
"%s/" _CATEGORY_NAME, name);
|
|
|
|
_LOAD_SUB_ALIAS((const char *)&loccat[0]);
|
|
|
|
|
|
|
|
/* (3) lookup locname type alias */
|
|
|
|
_LOAD_SUB_ALIAS(name);
|
|
|
|
|
|
|
|
done:
|
|
|
|
mutex_unlock(&_nb_mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-15 12:06:41 +01:00
|
|
|
const char *
|
2011-02-14 20:36:03 +01:00
|
|
|
_PREFIX(setlocale)(const char * __restrict name,
|
2013-12-06 12:04:52 +01:00
|
|
|
struct _locale * __restrict locale)
|
2011-02-14 20:36:03 +01:00
|
|
|
{
|
2013-12-06 12:04:52 +01:00
|
|
|
const char *loaded_name;
|
|
|
|
_CATEGORY_TYPE *loaded_impl;
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
/* name may be NULL */
|
|
|
|
_DIAGASSERT(locale != NULL);
|
|
|
|
|
|
|
|
if (name != NULL) {
|
|
|
|
if (*name == '\0')
|
|
|
|
name = _get_locale_env(_CATEGORY_NAME);
|
|
|
|
_DIAGASSERT(name != NULL);
|
|
|
|
_DIAGASSERT(locale->part_name[(size_t)_CATEGORY_ID] != NULL);
|
|
|
|
if (strcmp(name, locale->part_name[(size_t)_CATEGORY_ID])) {
|
2013-12-06 12:04:52 +01:00
|
|
|
if (_PREFIX(load)(name, &loaded_name, &loaded_impl))
|
2011-02-14 20:36:03 +01:00
|
|
|
return NULL;
|
2013-12-06 12:04:52 +01:00
|
|
|
locale->part_name[(size_t)_CATEGORY_ID] = loaded_name;
|
|
|
|
locale->part_impl[(size_t)_CATEGORY_ID] = loaded_impl;
|
|
|
|
if (locale == &_lc_global_locale)
|
|
|
|
_PREFIX(update_global)(loaded_impl);
|
2011-02-14 20:36:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return locale->part_name[(size_t)_CATEGORY_ID];
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*_NB_LC_TEMPLATE_H_*/
|