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