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