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