]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Locale/setlocale.c
2 Worker functions for the setlocale function.
4 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 * Copyright (c) 1991, 1993
14 * The Regents of the University of California. All rights reserved.
16 * This code is derived from software contributed to Berkeley by
17 * Paul Borman at Krystal Technologies.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 setlocale.c 8.1 (Berkeley) 7/4/93
44 * NetBSD: setlocale.c,v 1.50 2006/02/16 19:19:49 tnozaki Exp
46 #include <LibConfig.h>
47 #include <sys/EfiCdefs.h>
50 // Disable warnings about assignment within conditional expressions.
51 #pragma warning ( disable : 4706 )
54 #define _CTYPE_PRIVATE
56 #include "namespace.h"
57 #include <sys/localedef.h>
58 #include <sys/types.h>
63 #define __SETLOCALE_SOURCE__
72 #include "rune_local.h"
78 #include <citrus/citrus_namespace.h>
79 #include <citrus/citrus_region.h>
80 #include <citrus/citrus_lookup.h>
81 #include <citrus/citrus_bcs.h>
83 #include "aliasname_local.h"
84 #define _lookup_alias(p, a, b, s, c) __unaliasname((p), (a), (b), (s))
85 #define _bcs_strcasecmp(a, b) strcasecmp((a), (b))
88 #define _LOCALE_SYM_FORCE "/force"
91 const char *_PathLocale
= NULL
;
94 /** Category names for getenv(). **/
95 static const char *const categories
[_LC_LAST
] = {
105 /** Current locales for each category. **/
106 static char current_categories
[_LC_LAST
][32] = {
117 * The locales we are going to try and load
119 static char new_categories
[_LC_LAST
][32];
121 static char current_locale_string
[_LC_LAST
* 33];
123 static char *currentlocale(void);
124 static void revert_to_default(int);
125 static int force_locale_enable(int);
126 static int load_locale_sub(int, const char *, int);
127 static char *loadlocale(int);
128 static const char *__get_locale_env(int);
131 __setlocale(int category
, const char *locale
)
133 int i
, loadlocale_success
;
138 // (!_PathLocale && !(_PathLocale = getenv("PATH_LOCALE"))))
139 // _PathLocale = _PATH_LOCALE;
141 if (category
< 0 || category
>= _LC_LAST
)
146 current_categories
[category
] : currentlocale());
149 * Default to the current locale for everything.
151 for (i
= 1; i
< _LC_LAST
; ++i
)
152 (void)strncpyX(new_categories
[i
], current_categories
[i
],
153 sizeof(new_categories
[i
]));
156 * Now go fill up new_categories from the locale argument
159 if (category
== LC_ALL
) {
160 for (i
= 1; i
< _LC_LAST
; ++i
) {
161 env
= __get_locale_env(i
);
162 (void)strncpyX(new_categories
[i
], env
,
163 sizeof(new_categories
[i
]));
167 env
= __get_locale_env(category
);
168 (void)strncpyX(new_categories
[category
], env
,
169 sizeof(new_categories
[category
]));
171 } else if (category
) {
172 (void)strncpyX(new_categories
[category
], locale
,
173 sizeof(new_categories
[category
]));
175 if ((r
= strchr(locale
, '/')) == 0) {
176 for (i
= 1; i
< _LC_LAST
; ++i
) {
177 (void)strncpyX(new_categories
[i
], locale
,
178 sizeof(new_categories
[i
]));
182 _DIAGASSERT(*r
== '/' || *r
== 0);
183 _DIAGASSERT(*locale
!= 0);
185 return (NULL
); /* invalid format. */
187 if (len
+ 1 > sizeof(new_categories
[i
]))
188 return (NULL
); /* too long */
189 (void)memcpy(new_categories
[i
], locale
, len
);
190 new_categories
[i
][len
] = '\0';
193 _DIAGASSERT(*r
== '/');
194 if (*(locale
= ++r
) == 0)
195 /* slash followed by NUL */
197 /* skip until NUL or '/' */
198 while (*r
&& *r
!= '/')
201 return (NULL
); /* too many slashes. */
203 if (i
+ 1 != _LC_LAST
)
204 return (NULL
); /* too few slashes. */
209 return (loadlocale(category
));
211 loadlocale_success
= 0;
212 for (i
= 1; i
< _LC_LAST
; ++i
) {
213 if (loadlocale(i
) != NULL
)
214 loadlocale_success
= 1;
218 * If all categories failed, return NULL; we don't need to back
219 * changes off, since none happened.
221 if (!loadlocale_success
)
224 return (currentlocale());
232 (void)strncpyX(current_locale_string
, current_categories
[1],
233 sizeof(current_locale_string
));
235 for (i
= 2; i
< _LC_LAST
; ++i
)
236 if (strcmp(current_categories
[1], current_categories
[i
])) {
237 (void)snprintf(current_locale_string
,
238 sizeof(current_locale_string
), "%s/%s/%s/%s/%s/%s",
239 current_categories
[1], current_categories
[2],
240 current_categories
[3], current_categories
[4],
241 current_categories
[5], current_categories
[6]);
244 return (current_locale_string
);
248 revert_to_default(int category
)
253 (void)_xpg4_setrunelocale("C");
254 (void)__runetable_to_netbsd_ctype("C");
256 if (_cClass
!= _C_CharClassTable
) {
257 /* LINTED const castaway */
258 free((void *)_cClass
);
259 _cClass
= _C_CharClassTable
;
261 if (_uConvT
!= _C_ToUpperTable
) {
262 /* LINTED const castaway */
263 free((void *)_uConvT
);
264 _uConvT
= _C_ToUpperTable
;
266 if (_lConvT
!= _C_ToLowerTable
) {
267 /* LINTED const castaway */
268 free((void *)_lConvT
);
269 _lConvT
= _C_ToLowerTable
;
283 force_locale_enable(int category
)
285 revert_to_default(category
);
299 /* check for the default locales */
300 if (!strcmp(new_categories
[category
], "C") ||
301 !strcmp(new_categories
[category
], "POSIX")) {
302 revert_to_default(category
);
306 /* check whether special symbol */
307 if (isspecial
&& _bcs_strcasecmp(locname
, _LOCALE_SYM_FORCE
) == 0)
308 return force_locale_enable(category
);
311 if (strchr(locname
, '/') != NULL
)
314 (void)snprintf(name
, sizeof(name
), "%s/%s/%s",
315 _PathLocale
, locname
, categories
[category
]);
320 if (_xpg4_setrunelocale(__UNCONST(locname
)))
322 if (__runetable_to_netbsd_ctype(locname
)) {
323 /* very unfortunate, but need to go to "C" locale */
324 revert_to_default(category
);
328 if (!__loadctype(name
))
335 * XXX we don't have LC_MESSAGES support yet,
336 * but catopen may use the value of LC_MESSAGES category.
337 * so return successfully if locale directory is present.
339 (void)snprintf(name
, sizeof(name
), "%s/%s",
340 _PathLocale
, locname
);
344 if (stat(name
, &st
) < 0)
346 if (!S_ISDIR(st
.st_mode
))
362 loadlocale(int category
)
364 //char aliaspath[PATH_MAX], loccat[PATH_MAX], buf[PATH_MAX];
367 _DIAGASSERT(0 < category
&& category
< _LC_LAST
);
369 if (strcmp(new_categories
[category
], current_categories
[category
]) == 0)
370 return (current_categories
[category
]);
372 /* (1) non-aliased file */
373 if (!load_locale_sub(category
, new_categories
[category
], 0))
376 ///* (2) lookup locname/catname type alias */
377 //(void)snprintf(aliaspath, sizeof(aliaspath),
378 // "%s/" _LOCALE_ALIAS_NAME, _PathLocale);
379 //(void)snprintf(loccat, sizeof(loccat), "%s/%s",
380 // new_categories[category], categories[category]);
381 //alias = _lookup_alias(aliaspath, loccat, buf, sizeof(buf),
382 // _LOOKUP_CASE_SENSITIVE);
383 //if (!load_locale_sub(category, alias, 1))
386 ///* (3) lookup locname type alias */
387 //alias = _lookup_alias(aliaspath, new_categories[category],
388 // buf, sizeof(buf), _LOOKUP_CASE_SENSITIVE);
389 //if (!load_locale_sub(category, alias, 1))
395 (void)strncpyX(current_categories
[category
],
396 new_categories
[category
],
397 sizeof(current_categories
[category
]));
398 return current_categories
[category
];
402 __get_locale_env(int category
)
406 //_DIAGASSERT(category != LC_ALL);
408 ///* 1. check LC_ALL. */
409 //env = getenv(categories[0]);
411 ///* 2. check LC_* */
413 // env = getenv(categories[category]);
415 ///* 3. check LANG */
417 // env = getenv("LANG");
419 ///* 4. if none is set, fall to "C" */
420 //if (!env || !*env || strchr(env, '/'))