Home | History | Annotate | Line # | Download | only in locale
nb_lc_template.h revision 1.4.2.1
      1  1.4.2.1      tls /* $NetBSD: nb_lc_template.h,v 1.4.2.1 2013/06/23 06:21:05 tls Exp $ */
      2      1.1  tnozaki 
      3      1.1  tnozaki /*-
      4      1.1  tnozaki  * Copyright (c)1999, 2008 Citrus Project,
      5      1.1  tnozaki  * All rights reserved.
      6      1.1  tnozaki  *
      7      1.1  tnozaki  * Redistribution and use in source and binary forms, with or without
      8      1.1  tnozaki  * modification, are permitted provided that the following conditions
      9      1.1  tnozaki  * are met:
     10      1.1  tnozaki  * 1. Redistributions of source code must retain the above copyright
     11      1.1  tnozaki  *    notice, this list of conditions and the following disclaimer.
     12      1.1  tnozaki  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  tnozaki  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  tnozaki  *    documentation and/or other materials provided with the distribution.
     15      1.1  tnozaki  *
     16      1.1  tnozaki  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17      1.1  tnozaki  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18      1.1  tnozaki  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19      1.1  tnozaki  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20      1.1  tnozaki  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21      1.1  tnozaki  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22      1.1  tnozaki  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23      1.1  tnozaki  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24      1.1  tnozaki  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25      1.1  tnozaki  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26      1.1  tnozaki  * SUCH DAMAGE.
     27      1.1  tnozaki  */
     28      1.1  tnozaki 
     29      1.1  tnozaki /*-
     30      1.1  tnozaki  * Copyright (c) 1998 The NetBSD Foundation, Inc.
     31      1.1  tnozaki  * All rights reserved.
     32      1.1  tnozaki  *
     33      1.1  tnozaki  * This code is derived from software contributed to The NetBSD Foundation
     34      1.1  tnozaki  * by Paul Kranenburg.
     35      1.1  tnozaki  *
     36      1.1  tnozaki  * Redistribution and use in source and binary forms, with or without
     37      1.1  tnozaki  * modification, are permitted provided that the following conditions
     38      1.1  tnozaki  * are met:
     39      1.1  tnozaki  * 1. Redistributions of source code must retain the above copyright
     40      1.1  tnozaki  *    notice, this list of conditions and the following disclaimer.
     41      1.1  tnozaki  * 2. Redistributions in binary form must reproduce the above copyright
     42      1.1  tnozaki  *    notice, this list of conditions and the following disclaimer in the
     43      1.1  tnozaki  *    documentation and/or other materials provided with the distribution.
     44      1.1  tnozaki  *
     45      1.1  tnozaki  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     46      1.1  tnozaki  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     47      1.1  tnozaki  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     48      1.1  tnozaki  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     49      1.1  tnozaki  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     50      1.1  tnozaki  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     51      1.1  tnozaki  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     52      1.1  tnozaki  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     53      1.1  tnozaki  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     54      1.1  tnozaki  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     55      1.1  tnozaki  * POSSIBILITY OF SUCH DAMAGE.
     56      1.1  tnozaki  */
     57      1.1  tnozaki 
     58      1.1  tnozaki /*-
     59      1.1  tnozaki  * Copyright (c) 1993
     60      1.1  tnozaki  *	The Regents of the University of California.  All rights reserved.
     61      1.1  tnozaki  *
     62      1.1  tnozaki  * This code is derived from software contributed to Berkeley by
     63      1.1  tnozaki  * Paul Borman at Krystal Technologies.
     64      1.1  tnozaki  *
     65      1.1  tnozaki  * Redistribution and use in source and binary forms, with or without
     66      1.1  tnozaki  * modification, are permitted provided that the following conditions
     67      1.1  tnozaki  * are met:
     68      1.1  tnozaki  * 1. Redistributions of source code must retain the above copyright
     69      1.1  tnozaki  *    notice, this list of conditions and the following disclaimer.
     70      1.1  tnozaki  * 2. Redistributions in binary form must reproduce the above copyright
     71      1.1  tnozaki  *    notice, this list of conditions and the following disclaimer in the
     72      1.1  tnozaki  *    documentation and/or other materials provided with the distribution.
     73      1.1  tnozaki  * 3. Neither the name of the University nor the names of its contributors
     74      1.1  tnozaki  *    may be used to endorse or promote products derived from this software
     75      1.1  tnozaki  *    without specific prior written permission.
     76      1.1  tnozaki  *
     77      1.1  tnozaki  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     78      1.1  tnozaki  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     79      1.1  tnozaki  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     80      1.1  tnozaki  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     81      1.1  tnozaki  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     82      1.1  tnozaki  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     83      1.1  tnozaki  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     84      1.1  tnozaki  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     85      1.1  tnozaki  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     86      1.1  tnozaki  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     87      1.1  tnozaki  * SUCH DAMAGE.
     88      1.1  tnozaki  */
     89      1.1  tnozaki 
     90      1.1  tnozaki #ifndef _NB_LC_TEMPLATE_H_
     91      1.1  tnozaki #define _NB_LC_TEMPLATE_H_
     92      1.1  tnozaki 
     93      1.1  tnozaki #define _nb_part_t		_PREFIX(part_t)
     94      1.1  tnozaki #define _nb_part_cache		_PREFIX(part_cache)
     95      1.1  tnozaki #define _nb_default_c		_PREFIX(default_c)
     96      1.1  tnozaki #define _nb_default_posix	_PREFIX(default_posix)
     97      1.1  tnozaki #define _nb_mutex		_PREFIX(mutex)
     98      1.1  tnozaki 
     99      1.1  tnozaki typedef struct _nb_part_t {
    100      1.1  tnozaki 	char name[_LOCALENAME_LEN_MAX];
    101      1.1  tnozaki 	_CATEGORY_TYPE *impl;
    102      1.1  tnozaki 	SIMPLEQ_ENTRY(_nb_part_t) entry;
    103      1.1  tnozaki } _nb_part_t;
    104      1.1  tnozaki 
    105      1.1  tnozaki static SIMPLEQ_HEAD(, _nb_part_t) _nb_part_cache =
    106      1.1  tnozaki     SIMPLEQ_HEAD_INITIALIZER(_nb_part_cache);
    107      1.1  tnozaki 
    108      1.1  tnozaki static const _nb_part_t _nb_default_c = {
    109      1.1  tnozaki     _C_LOCALE,
    110      1.1  tnozaki     __UNCONST(&_CATEGORY_DEFAULT),
    111      1.1  tnozaki     { NULL },
    112      1.1  tnozaki };
    113      1.1  tnozaki 
    114      1.1  tnozaki static const _nb_part_t _nb_default_posix = {
    115      1.1  tnozaki     _POSIX_LOCALE,
    116      1.1  tnozaki     __UNCONST(&_CATEGORY_DEFAULT),
    117      1.1  tnozaki     { NULL },
    118      1.1  tnozaki };
    119      1.1  tnozaki 
    120      1.1  tnozaki #ifdef _REENTRANT
    121      1.1  tnozaki static mutex_t _nb_mutex = MUTEX_INITIALIZER;
    122      1.1  tnozaki #endif
    123      1.1  tnozaki 
    124      1.1  tnozaki static int
    125      1.1  tnozaki _PREFIX(load_sub)(const char * __restrict name, const char * __restrict real,
    126      1.1  tnozaki     _nb_part_t ** __restrict part, int force)
    127      1.1  tnozaki {
    128      1.1  tnozaki 	_nb_part_t *p, *q;
    129      1.1  tnozaki 	int ret;
    130      1.1  tnozaki 
    131      1.1  tnozaki 	_DIAGASSERT(name != NULL);
    132      1.1  tnozaki 	_DIAGASSERT(part != NULL);
    133      1.1  tnozaki 
    134      1.1  tnozaki 	if (!strcmp(_C_LOCALE, name)) {
    135      1.1  tnozaki 		p = __UNCONST(&_nb_default_c);
    136      1.1  tnozaki 	} else if (!strcmp(_POSIX_LOCALE, name)) {
    137      1.1  tnozaki 		p = __UNCONST(&_nb_default_posix);
    138      1.1  tnozaki 	} else {
    139      1.1  tnozaki 		SIMPLEQ_FOREACH(p, &_nb_part_cache, entry) {
    140      1.1  tnozaki 			if (!strcmp((const char *)&p->name[0], name))
    141      1.1  tnozaki 				goto found;
    142      1.1  tnozaki 		}
    143      1.1  tnozaki 		p = malloc(sizeof(*p));
    144      1.1  tnozaki 		if (p == NULL)
    145      1.1  tnozaki 			return ENOMEM;
    146      1.1  tnozaki 		if (force) {
    147      1.1  tnozaki 			p->impl = __UNCONST(&_CATEGORY_DEFAULT);
    148      1.1  tnozaki 		} else {
    149      1.1  tnozaki 			_DIAGASSERT(_PathLocale != NULL);
    150      1.1  tnozaki 			ret = _PREFIX(create_impl)((const char *)_PathLocale,
    151      1.1  tnozaki 			    name, &p->impl);
    152      1.1  tnozaki 			if (ret) {
    153      1.1  tnozaki 				free(p);
    154      1.1  tnozaki 				return ret;
    155      1.1  tnozaki 			}
    156      1.1  tnozaki 		}
    157      1.1  tnozaki 		strlcpy(&p->name[0], name, sizeof(p->name));
    158      1.1  tnozaki 		SIMPLEQ_INSERT_TAIL(&_nb_part_cache, p, entry);
    159      1.1  tnozaki 	}
    160      1.1  tnozaki found:
    161      1.1  tnozaki 	if (real != NULL) {
    162      1.1  tnozaki 		q = malloc(sizeof(*q));
    163      1.1  tnozaki 		if (q == NULL)
    164      1.1  tnozaki 			return ENOMEM;
    165      1.1  tnozaki 		strlcpy(&q->name[0], real, sizeof(p->name));
    166      1.1  tnozaki 		q->impl = p->impl;
    167      1.1  tnozaki 		SIMPLEQ_INSERT_TAIL(&_nb_part_cache, q, entry);
    168      1.1  tnozaki 		p = q;
    169      1.1  tnozaki 	}
    170      1.1  tnozaki 	*part = p;
    171      1.1  tnozaki 	return 0;
    172      1.1  tnozaki }
    173      1.1  tnozaki 
    174      1.1  tnozaki static __inline int
    175      1.1  tnozaki _PREFIX(load)(const char * __restrict name,
    176      1.1  tnozaki     _nb_part_t ** __restrict part)
    177      1.1  tnozaki {
    178      1.1  tnozaki 	int ret, force;
    179      1.1  tnozaki 	char path[PATH_MAX + 1], loccat[PATH_MAX + 1], buf[PATH_MAX + 1];
    180      1.1  tnozaki 	const char *aliaspath, *alias;
    181      1.1  tnozaki 
    182      1.1  tnozaki #define _LOAD_SUB_ALIAS(key)						\
    183      1.1  tnozaki do {									\
    184      1.1  tnozaki 	alias = __unaliasname(aliaspath, key, &buf[0], sizeof(buf));	\
    185      1.1  tnozaki 	if (alias != NULL) {						\
    186      1.1  tnozaki 		ret = (force = !__isforcemapping(alias))		\
    187      1.1  tnozaki 		    ? _PREFIX(load_sub)(name, NULL, part, force)	\
    188      1.1  tnozaki 		    : _PREFIX(load_sub)(alias, name, part, force);	\
    189      1.1  tnozaki 		_DIAGASSERT(!ret || !force);				\
    190      1.1  tnozaki 		goto done;						\
    191      1.1  tnozaki 	}								\
    192      1.1  tnozaki } while (/*CONSTCOND*/0)
    193      1.1  tnozaki 
    194      1.1  tnozaki 	/* (1) non-aliased file */
    195      1.1  tnozaki 	mutex_lock(&_nb_mutex);
    196      1.1  tnozaki 	ret = _PREFIX(load_sub)(name, NULL, part, 0);
    197      1.1  tnozaki 	if (ret != ENOENT)
    198      1.1  tnozaki 		goto done;
    199      1.1  tnozaki 
    200      1.1  tnozaki 	/* (2) lookup locname/catname type alias */
    201      1.1  tnozaki 	_DIAGASSERT(_PathLocale != NULL);
    202      1.1  tnozaki 	snprintf(&path[0], sizeof(path),
    203      1.1  tnozaki 	    "%s/" _LOCALE_ALIAS_NAME, _PathLocale);
    204      1.1  tnozaki 	aliaspath = (const char *)&path[0];
    205      1.1  tnozaki 	snprintf(&loccat[0], sizeof(loccat),
    206      1.1  tnozaki 	    "%s/" _CATEGORY_NAME, name);
    207      1.1  tnozaki 	_LOAD_SUB_ALIAS((const char *)&loccat[0]);
    208      1.1  tnozaki 
    209      1.1  tnozaki 	/* (3) lookup locname type alias */
    210      1.1  tnozaki 	_LOAD_SUB_ALIAS(name);
    211      1.1  tnozaki 
    212      1.1  tnozaki done:
    213      1.1  tnozaki 	mutex_unlock(&_nb_mutex);
    214      1.1  tnozaki 	return ret;
    215      1.1  tnozaki }
    216      1.1  tnozaki 
    217      1.4  tnozaki const char *
    218      1.1  tnozaki _PREFIX(setlocale)(const char * __restrict name,
    219  1.4.2.1      tls     struct _locale * __restrict locale)
    220      1.1  tnozaki {
    221      1.1  tnozaki 	_nb_part_t *part;
    222      1.1  tnozaki 
    223      1.1  tnozaki 	/* name may be NULL */
    224      1.1  tnozaki 	_DIAGASSERT(locale != NULL);
    225      1.1  tnozaki 
    226      1.1  tnozaki 	if (name != NULL) {
    227      1.1  tnozaki 		if (*name == '\0')
    228      1.1  tnozaki 			name = _get_locale_env(_CATEGORY_NAME);
    229      1.1  tnozaki 		_DIAGASSERT(name != NULL);
    230      1.1  tnozaki 		_DIAGASSERT(locale->part_name[(size_t)_CATEGORY_ID] != NULL);
    231      1.1  tnozaki 		if (strcmp(name, locale->part_name[(size_t)_CATEGORY_ID])) {
    232      1.1  tnozaki 			if (_PREFIX(load)(name, &part))
    233      1.1  tnozaki 				return NULL;
    234      1.1  tnozaki 			locale->part_name[(size_t)_CATEGORY_ID]
    235      1.1  tnozaki 			    = &part->name[0];
    236      1.1  tnozaki 			locale->part_impl[(size_t)_CATEGORY_ID]
    237      1.1  tnozaki 			    = part->impl;
    238      1.3  tnozaki 			_PREFIX(build_cache)(locale->cache, part->impl);
    239  1.4.2.1      tls 			if (locale == &_lc_global_locale)
    240      1.1  tnozaki 				_PREFIX(fixup)(part->impl);
    241      1.1  tnozaki 		}
    242      1.1  tnozaki 	}
    243      1.1  tnozaki 	return locale->part_name[(size_t)_CATEGORY_ID];
    244      1.1  tnozaki }
    245      1.1  tnozaki 
    246      1.1  tnozaki #endif /*_NB_LC_TEMPLATE_H_*/
    247