Home | History | Annotate | Line # | Download | only in locale
      1  1.9   rillig /* $NetBSD: nb_lc_template.h,v 1.9 2022/04/19 20:32:15 rillig 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_mutex		_PREFIX(mutex)
     96  1.1  tnozaki 
     97  1.1  tnozaki typedef struct _nb_part_t {
     98  1.1  tnozaki 	char name[_LOCALENAME_LEN_MAX];
     99  1.1  tnozaki 	_CATEGORY_TYPE *impl;
    100  1.1  tnozaki 	SIMPLEQ_ENTRY(_nb_part_t) entry;
    101  1.1  tnozaki } _nb_part_t;
    102  1.1  tnozaki 
    103  1.1  tnozaki static SIMPLEQ_HEAD(, _nb_part_t) _nb_part_cache =
    104  1.1  tnozaki     SIMPLEQ_HEAD_INITIALIZER(_nb_part_cache);
    105  1.1  tnozaki 
    106  1.1  tnozaki #ifdef _REENTRANT
    107  1.1  tnozaki static mutex_t _nb_mutex = MUTEX_INITIALIZER;
    108  1.1  tnozaki #endif
    109  1.1  tnozaki 
    110  1.1  tnozaki static int
    111  1.1  tnozaki _PREFIX(load_sub)(const char * __restrict name, const char * __restrict real,
    112  1.7    joerg     const char ** __restrict out_name, _CATEGORY_TYPE ** __restrict out_impl,
    113  1.7    joerg     int force)
    114  1.1  tnozaki {
    115  1.7    joerg 	const char *cached_name;
    116  1.7    joerg 	_CATEGORY_TYPE *cached_impl;
    117  1.7    joerg 	_nb_part_t *p;
    118  1.1  tnozaki 	int ret;
    119  1.1  tnozaki 
    120  1.1  tnozaki 	_DIAGASSERT(name != NULL);
    121  1.7    joerg 	_DIAGASSERT(out_name != NULL);
    122  1.7    joerg 	_DIAGASSERT(out_impl != NULL);
    123  1.1  tnozaki 
    124  1.1  tnozaki 	if (!strcmp(_C_LOCALE, name)) {
    125  1.7    joerg 		cached_name = _lc_C_locale.part_name[_CATEGORY_ID];
    126  1.7    joerg 		cached_impl = _lc_C_locale.part_impl[_CATEGORY_ID];
    127  1.1  tnozaki 	} else if (!strcmp(_POSIX_LOCALE, name)) {
    128  1.7    joerg 		cached_name = _POSIX_LOCALE;
    129  1.7    joerg 		cached_impl = _lc_C_locale.part_impl[_CATEGORY_ID];
    130  1.1  tnozaki 	} else {
    131  1.1  tnozaki 		SIMPLEQ_FOREACH(p, &_nb_part_cache, entry) {
    132  1.7    joerg 			if (!strcmp((const char *)&p->name[0], name)) {
    133  1.7    joerg 				cached_name = p->name;
    134  1.7    joerg 				cached_impl = p->impl;
    135  1.1  tnozaki 				goto found;
    136  1.7    joerg 			}
    137  1.1  tnozaki 		}
    138  1.1  tnozaki 		p = malloc(sizeof(*p));
    139  1.1  tnozaki 		if (p == NULL)
    140  1.1  tnozaki 			return ENOMEM;
    141  1.1  tnozaki 		if (force) {
    142  1.7    joerg 			p->impl = _lc_C_locale.part_impl[_CATEGORY_ID];
    143  1.1  tnozaki 		} else {
    144  1.1  tnozaki 			_DIAGASSERT(_PathLocale != NULL);
    145  1.1  tnozaki 			ret = _PREFIX(create_impl)((const char *)_PathLocale,
    146  1.1  tnozaki 			    name, &p->impl);
    147  1.1  tnozaki 			if (ret) {
    148  1.1  tnozaki 				free(p);
    149  1.1  tnozaki 				return ret;
    150  1.1  tnozaki 			}
    151  1.1  tnozaki 		}
    152  1.1  tnozaki 		strlcpy(&p->name[0], name, sizeof(p->name));
    153  1.1  tnozaki 		SIMPLEQ_INSERT_TAIL(&_nb_part_cache, p, entry);
    154  1.7    joerg 		cached_name = p->name;
    155  1.7    joerg 		cached_impl = p->impl;
    156  1.1  tnozaki 	}
    157  1.1  tnozaki found:
    158  1.1  tnozaki 	if (real != NULL) {
    159  1.7    joerg 		p = malloc(sizeof(*p));
    160  1.7    joerg 		if (p == NULL)
    161  1.1  tnozaki 			return ENOMEM;
    162  1.7    joerg 		strlcpy(&p->name[0], real, sizeof(p->name));
    163  1.7    joerg 		cached_name = p->name;
    164  1.7    joerg 		p->impl = cached_impl;
    165  1.7    joerg 		SIMPLEQ_INSERT_TAIL(&_nb_part_cache, p, entry);
    166  1.1  tnozaki 	}
    167  1.7    joerg 	*out_name = cached_name;
    168  1.7    joerg 	*out_impl = cached_impl;
    169  1.1  tnozaki 	return 0;
    170  1.1  tnozaki }
    171  1.1  tnozaki 
    172  1.1  tnozaki static __inline int
    173  1.1  tnozaki _PREFIX(load)(const char * __restrict name,
    174  1.7    joerg     const char ** __restrict out_name, _CATEGORY_TYPE ** __restrict out_impl)
    175  1.1  tnozaki {
    176  1.1  tnozaki 	int ret, force;
    177  1.1  tnozaki 	char path[PATH_MAX + 1], loccat[PATH_MAX + 1], buf[PATH_MAX + 1];
    178  1.1  tnozaki 	const char *aliaspath, *alias;
    179  1.1  tnozaki 
    180  1.1  tnozaki #define _LOAD_SUB_ALIAS(key)						\
    181  1.1  tnozaki do {									\
    182  1.1  tnozaki 	alias = __unaliasname(aliaspath, key, &buf[0], sizeof(buf));	\
    183  1.1  tnozaki 	if (alias != NULL) {						\
    184  1.1  tnozaki 		ret = (force = !__isforcemapping(alias))		\
    185  1.7    joerg 		    ? _PREFIX(load_sub)(name, NULL, out_name, out_impl, \
    186  1.7    joerg 				        force)	\
    187  1.7    joerg 		    : _PREFIX(load_sub)(alias, name, out_name, out_impl, \
    188  1.7    joerg 				        force);	\
    189  1.1  tnozaki 		_DIAGASSERT(!ret || !force);				\
    190  1.1  tnozaki 		goto done;						\
    191  1.1  tnozaki 	}								\
    192  1.9   rillig } while (0)
    193  1.1  tnozaki 
    194  1.1  tnozaki 	/* (1) non-aliased file */
    195  1.1  tnozaki 	mutex_lock(&_nb_mutex);
    196  1.7    joerg 	ret = _PREFIX(load_sub)(name, NULL, out_name, out_impl, 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.5    joerg     struct _locale * __restrict locale)
    220  1.1  tnozaki {
    221  1.7    joerg 	const char *loaded_name;
    222  1.7    joerg 	_CATEGORY_TYPE *loaded_impl;
    223  1.1  tnozaki 
    224  1.1  tnozaki 	/* name may be NULL */
    225  1.1  tnozaki 	_DIAGASSERT(locale != NULL);
    226  1.1  tnozaki 
    227  1.1  tnozaki 	if (name != NULL) {
    228  1.1  tnozaki 		if (*name == '\0')
    229  1.1  tnozaki 			name = _get_locale_env(_CATEGORY_NAME);
    230  1.1  tnozaki 		_DIAGASSERT(name != NULL);
    231  1.1  tnozaki 		_DIAGASSERT(locale->part_name[(size_t)_CATEGORY_ID] != NULL);
    232  1.1  tnozaki 		if (strcmp(name, locale->part_name[(size_t)_CATEGORY_ID])) {
    233  1.7    joerg 			if (_PREFIX(load)(name, &loaded_name, &loaded_impl))
    234  1.1  tnozaki 				return NULL;
    235  1.7    joerg 			locale->part_name[(size_t)_CATEGORY_ID] = loaded_name;
    236  1.7    joerg 			locale->part_impl[(size_t)_CATEGORY_ID] = loaded_impl;
    237  1.6    joerg 			if (locale == &_lc_global_locale)
    238  1.7    joerg 				_PREFIX(update_global)(loaded_impl);
    239  1.1  tnozaki 		}
    240  1.1  tnozaki 	}
    241  1.1  tnozaki 	return locale->part_name[(size_t)_CATEGORY_ID];
    242  1.1  tnozaki }
    243  1.1  tnozaki 
    244  1.1  tnozaki #endif /*_NB_LC_TEMPLATE_H_*/
    245