]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Locale/setlocale.c
b6090d2ea0ab994b051bdbef89e3a5d25b07aaae
[mirror_edk2.git] / StdLib / LibC / Locale / setlocale.c
1 /* $NetBSD: setlocale.c,v 1.50 2006/02/16 19:19:49 tnozaki Exp $ */
2
3 /*
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Paul Borman at Krystal Technologies.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34 #include <LibConfig.h>
35 #include <sys/EfiCdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93";
39 #else
40 __RCSID("$NetBSD: setlocale.c,v 1.50 2006/02/16 19:19:49 tnozaki Exp $");
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43
44 #if defined(_MSC_VER)
45 // Disable warnings about assignment within conditional expressions.
46 #pragma warning ( disable : 4706 )
47 #endif
48
49 #define _CTYPE_PRIVATE
50
51 #include "namespace.h"
52 #include <sys/localedef.h>
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <assert.h>
56 #include <limits.h>
57 #include <ctype.h>
58 #define __SETLOCALE_SOURCE__
59 #include <locale.h>
60 #include <paths.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <sys/EfiSysCall.h>
65 #include "rune.h"
66 #ifdef WITH_RUNE
67 #include "rune_local.h"
68 #else
69 #include "ctypeio.h"
70 #endif
71
72 #ifdef CITRUS
73 #include <citrus/citrus_namespace.h>
74 #include <citrus/citrus_region.h>
75 #include <citrus/citrus_lookup.h>
76 #include <citrus/citrus_bcs.h>
77 #else
78 #include "aliasname_local.h"
79 #define _lookup_alias(p, a, b, s, c) __unaliasname((p), (a), (b), (s))
80 #define _bcs_strcasecmp(a, b) strcasecmp((a), (b))
81 #endif
82
83 #define _LOCALE_SYM_FORCE "/force"
84
85 #ifndef WITH_RUNE
86 const char *_PathLocale = NULL;
87 #endif
88
89 /*
90 * Category names for getenv()
91 */
92 static const char *const categories[_LC_LAST] = {
93 "LC_ALL",
94 "LC_COLLATE",
95 "LC_CTYPE",
96 "LC_MONETARY",
97 "LC_NUMERIC",
98 "LC_TIME",
99 "LC_MESSAGES"
100 };
101
102 /*
103 * Current locales for each category
104 */
105 static char current_categories[_LC_LAST][32] = {
106 "C",
107 "C",
108 "C",
109 "C",
110 "C",
111 "C",
112 "C"
113 };
114
115 /*
116 * The locales we are going to try and load
117 */
118 static char new_categories[_LC_LAST][32];
119
120 static char current_locale_string[_LC_LAST * 33];
121
122 static char *currentlocale(void);
123 static void revert_to_default(int);
124 static int force_locale_enable(int);
125 static int load_locale_sub(int, const char *, int);
126 static char *loadlocale(int);
127 static const char *__get_locale_env(int);
128
129 char *
130 __setlocale(int category, const char *locale)
131 {
132 int i, loadlocale_success;
133 size_t len;
134 const char *env, *r;
135
136 //if (issetugid() ||
137 // (!_PathLocale && !(_PathLocale = getenv("PATH_LOCALE"))))
138 // _PathLocale = _PATH_LOCALE;
139
140 if (category < 0 || category >= _LC_LAST)
141 return (NULL);
142
143 if (!locale)
144 return (category ?
145 current_categories[category] : currentlocale());
146
147 /*
148 * Default to the current locale for everything.
149 */
150 for (i = 1; i < _LC_LAST; ++i)
151 (void)strncpyX(new_categories[i], current_categories[i],
152 sizeof(new_categories[i]));
153
154 /*
155 * Now go fill up new_categories from the locale argument
156 */
157 if (!*locale) {
158 if (category == LC_ALL) {
159 for (i = 1; i < _LC_LAST; ++i) {
160 env = __get_locale_env(i);
161 (void)strncpyX(new_categories[i], env,
162 sizeof(new_categories[i]));
163 }
164 }
165 else {
166 env = __get_locale_env(category);
167 (void)strncpyX(new_categories[category], env,
168 sizeof(new_categories[category]));
169 }
170 } else if (category) {
171 (void)strncpyX(new_categories[category], locale,
172 sizeof(new_categories[category]));
173 } else {
174 if ((r = strchr(locale, '/')) == 0) {
175 for (i = 1; i < _LC_LAST; ++i) {
176 (void)strncpyX(new_categories[i], locale,
177 sizeof(new_categories[i]));
178 }
179 } else {
180 for (i = 1;;) {
181 _DIAGASSERT(*r == '/' || *r == 0);
182 _DIAGASSERT(*locale != 0);
183 if (*locale == '/')
184 return (NULL); /* invalid format. */
185 len = r - locale;
186 if (len + 1 > sizeof(new_categories[i]))
187 return (NULL); /* too long */
188 (void)memcpy(new_categories[i], locale, len);
189 new_categories[i][len] = '\0';
190 if (*r == 0)
191 break;
192 _DIAGASSERT(*r == '/');
193 if (*(locale = ++r) == 0)
194 /* slash followed by NUL */
195 return (NULL);
196 /* skip until NUL or '/' */
197 while (*r && *r != '/')
198 r++;
199 if (++i == _LC_LAST)
200 return (NULL); /* too many slashes. */
201 }
202 if (i + 1 != _LC_LAST)
203 return (NULL); /* too few slashes. */
204 }
205 }
206
207 if (category)
208 return (loadlocale(category));
209
210 loadlocale_success = 0;
211 for (i = 1; i < _LC_LAST; ++i) {
212 if (loadlocale(i) != NULL)
213 loadlocale_success = 1;
214 }
215
216 /*
217 * If all categories failed, return NULL; we don't need to back
218 * changes off, since none happened.
219 */
220 if (!loadlocale_success)
221 return NULL;
222
223 return (currentlocale());
224 }
225
226 static char *
227 currentlocale()
228 {
229 int i;
230
231 (void)strncpyX(current_locale_string, current_categories[1],
232 sizeof(current_locale_string));
233
234 for (i = 2; i < _LC_LAST; ++i)
235 if (strcmp(current_categories[1], current_categories[i])) {
236 (void)snprintf(current_locale_string,
237 sizeof(current_locale_string), "%s/%s/%s/%s/%s/%s",
238 current_categories[1], current_categories[2],
239 current_categories[3], current_categories[4],
240 current_categories[5], current_categories[6]);
241 break;
242 }
243 return (current_locale_string);
244 }
245
246 static void
247 revert_to_default(int category)
248 {
249 switch (category) {
250 case LC_CTYPE:
251 #ifdef WITH_RUNE
252 (void)_xpg4_setrunelocale("C");
253 (void)__runetable_to_netbsd_ctype("C");
254 #else
255 if (_cClass != _C_CharClassTable) {
256 /* LINTED const castaway */
257 free((void *)_cClass);
258 _cClass = _C_CharClassTable;
259 }
260 if (_uConvT != _C_ToUpperTable) {
261 /* LINTED const castaway */
262 free((void *)_uConvT);
263 _uConvT = _C_ToUpperTable;
264 }
265 if (_lConvT != _C_ToLowerTable) {
266 /* LINTED const castaway */
267 free((void *)_lConvT);
268 _lConvT = _C_ToLowerTable;
269 }
270 #endif
271 break;
272 case LC_MESSAGES:
273 case LC_COLLATE:
274 case LC_MONETARY:
275 case LC_NUMERIC:
276 case LC_TIME:
277 break;
278 }
279 }
280
281 static int
282 force_locale_enable(int category)
283 {
284 revert_to_default(category);
285
286 return 0;
287 }
288
289 static int
290 load_locale_sub(
291 int category,
292 const char *locname,
293 int isspecial
294 )
295 {
296 char name[PATH_MAX];
297
298 /* check for the default locales */
299 if (!strcmp(new_categories[category], "C") ||
300 !strcmp(new_categories[category], "POSIX")) {
301 revert_to_default(category);
302 return 0;
303 }
304
305 /* check whether special symbol */
306 if (isspecial && _bcs_strcasecmp(locname, _LOCALE_SYM_FORCE) == 0)
307 return force_locale_enable(category);
308
309 /* sanity check */
310 if (strchr(locname, '/') != NULL)
311 return -1;
312
313 (void)snprintf(name, sizeof(name), "%s/%s/%s",
314 _PathLocale, locname, categories[category]);
315
316 switch (category) {
317 case LC_CTYPE:
318 #ifdef WITH_RUNE
319 if (_xpg4_setrunelocale(__UNCONST(locname)))
320 return -1;
321 if (__runetable_to_netbsd_ctype(locname)) {
322 /* very unfortunate, but need to go to "C" locale */
323 revert_to_default(category);
324 return -1;
325 }
326 #else
327 if (!__loadctype(name))
328 return -1;
329 #endif
330 break;
331
332 case LC_MESSAGES:
333 /*
334 * XXX we don't have LC_MESSAGES support yet,
335 * but catopen may use the value of LC_MESSAGES category.
336 * so return successfully if locale directory is present.
337 */
338 (void)snprintf(name, sizeof(name), "%s/%s",
339 _PathLocale, locname);
340 /* local */
341 {
342 struct stat st;
343 if (stat(name, &st) < 0)
344 return -1;
345 if (!S_ISDIR(st.st_mode))
346 return -1;
347 }
348 break;
349
350 case LC_COLLATE:
351 case LC_MONETARY:
352 case LC_NUMERIC:
353 case LC_TIME:
354 return -1;
355 }
356
357 return 0;
358 }
359
360 static char *
361 loadlocale(int category)
362 {
363 //char aliaspath[PATH_MAX], loccat[PATH_MAX], buf[PATH_MAX];
364 //const char *alias;
365
366 _DIAGASSERT(0 < category && category < _LC_LAST);
367
368 if (strcmp(new_categories[category], current_categories[category]) == 0)
369 return (current_categories[category]);
370
371 /* (1) non-aliased file */
372 if (!load_locale_sub(category, new_categories[category], 0))
373 goto success;
374
375 ///* (2) lookup locname/catname type alias */
376 //(void)snprintf(aliaspath, sizeof(aliaspath),
377 // "%s/" _LOCALE_ALIAS_NAME, _PathLocale);
378 //(void)snprintf(loccat, sizeof(loccat), "%s/%s",
379 // new_categories[category], categories[category]);
380 //alias = _lookup_alias(aliaspath, loccat, buf, sizeof(buf),
381 // _LOOKUP_CASE_SENSITIVE);
382 //if (!load_locale_sub(category, alias, 1))
383 // goto success;
384
385 ///* (3) lookup locname type alias */
386 //alias = _lookup_alias(aliaspath, new_categories[category],
387 // buf, sizeof(buf), _LOOKUP_CASE_SENSITIVE);
388 //if (!load_locale_sub(category, alias, 1))
389 // goto success;
390
391 return NULL;
392
393 success:
394 (void)strncpyX(current_categories[category],
395 new_categories[category],
396 sizeof(current_categories[category]));
397 return current_categories[category];
398 }
399
400 static const char *
401 __get_locale_env(int category)
402 {
403 const char *env;
404
405 //_DIAGASSERT(category != LC_ALL);
406
407 ///* 1. check LC_ALL. */
408 //env = getenv(categories[0]);
409
410 ///* 2. check LC_* */
411 //if (!env || !*env)
412 // env = getenv(categories[category]);
413
414 ///* 3. check LANG */
415 //if (!env || !*env)
416 // env = getenv("LANG");
417
418 ///* 4. if none is set, fall to "C" */
419 //if (!env || !*env || strchr(env, '/'))
420 env = "C";
421
422 return env;
423 }