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