]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Locale/setlocale.c
b6090d2ea0ab994b051bdbef89e3a5d25b07aaae
1 /* $NetBSD: setlocale.c,v 1.50 2006/02/16 19:19:49 tnozaki Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Paul Borman at Krystal Technologies.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 #include <LibConfig.h>
35 #include <sys/EfiCdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93";
40 __RCSID("$NetBSD: setlocale.c,v 1.50 2006/02/16 19:19:49 tnozaki Exp $");
42 #endif /* LIBC_SCCS and not lint */
45 // Disable warnings about assignment within conditional expressions.
46 #pragma warning ( disable : 4706 )
49 #define _CTYPE_PRIVATE
51 #include "namespace.h"
52 #include <sys/localedef.h>
53 #include <sys/types.h>
58 #define __SETLOCALE_SOURCE__
64 #include <sys/EfiSysCall.h>
67 #include "rune_local.h"
73 #include <citrus/citrus_namespace.h>
74 #include <citrus/citrus_region.h>
75 #include <citrus/citrus_lookup.h>
76 #include <citrus/citrus_bcs.h>
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))
83 #define _LOCALE_SYM_FORCE "/force"
86 const char *_PathLocale
= NULL
;
90 * Category names for getenv()
92 static const char *const categories
[_LC_LAST
] = {
103 * Current locales for each category
105 static char current_categories
[_LC_LAST
][32] = {
116 * The locales we are going to try and load
118 static char new_categories
[_LC_LAST
][32];
120 static char current_locale_string
[_LC_LAST
* 33];
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);
130 __setlocale(int category
, const char *locale
)
132 int i
, loadlocale_success
;
137 // (!_PathLocale && !(_PathLocale = getenv("PATH_LOCALE"))))
138 // _PathLocale = _PATH_LOCALE;
140 if (category
< 0 || category
>= _LC_LAST
)
145 current_categories
[category
] : currentlocale());
148 * Default to the current locale for everything.
150 for (i
= 1; i
< _LC_LAST
; ++i
)
151 (void)strncpyX(new_categories
[i
], current_categories
[i
],
152 sizeof(new_categories
[i
]));
155 * Now go fill up new_categories from the locale argument
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
]));
166 env
= __get_locale_env(category
);
167 (void)strncpyX(new_categories
[category
], env
,
168 sizeof(new_categories
[category
]));
170 } else if (category
) {
171 (void)strncpyX(new_categories
[category
], locale
,
172 sizeof(new_categories
[category
]));
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
]));
181 _DIAGASSERT(*r
== '/' || *r
== 0);
182 _DIAGASSERT(*locale
!= 0);
184 return (NULL
); /* invalid format. */
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';
192 _DIAGASSERT(*r
== '/');
193 if (*(locale
= ++r
) == 0)
194 /* slash followed by NUL */
196 /* skip until NUL or '/' */
197 while (*r
&& *r
!= '/')
200 return (NULL
); /* too many slashes. */
202 if (i
+ 1 != _LC_LAST
)
203 return (NULL
); /* too few slashes. */
208 return (loadlocale(category
));
210 loadlocale_success
= 0;
211 for (i
= 1; i
< _LC_LAST
; ++i
) {
212 if (loadlocale(i
) != NULL
)
213 loadlocale_success
= 1;
217 * If all categories failed, return NULL; we don't need to back
218 * changes off, since none happened.
220 if (!loadlocale_success
)
223 return (currentlocale());
231 (void)strncpyX(current_locale_string
, current_categories
[1],
232 sizeof(current_locale_string
));
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]);
243 return (current_locale_string
);
247 revert_to_default(int category
)
252 (void)_xpg4_setrunelocale("C");
253 (void)__runetable_to_netbsd_ctype("C");
255 if (_cClass
!= _C_CharClassTable
) {
256 /* LINTED const castaway */
257 free((void *)_cClass
);
258 _cClass
= _C_CharClassTable
;
260 if (_uConvT
!= _C_ToUpperTable
) {
261 /* LINTED const castaway */
262 free((void *)_uConvT
);
263 _uConvT
= _C_ToUpperTable
;
265 if (_lConvT
!= _C_ToLowerTable
) {
266 /* LINTED const castaway */
267 free((void *)_lConvT
);
268 _lConvT
= _C_ToLowerTable
;
282 force_locale_enable(int category
)
284 revert_to_default(category
);
298 /* check for the default locales */
299 if (!strcmp(new_categories
[category
], "C") ||
300 !strcmp(new_categories
[category
], "POSIX")) {
301 revert_to_default(category
);
305 /* check whether special symbol */
306 if (isspecial
&& _bcs_strcasecmp(locname
, _LOCALE_SYM_FORCE
) == 0)
307 return force_locale_enable(category
);
310 if (strchr(locname
, '/') != NULL
)
313 (void)snprintf(name
, sizeof(name
), "%s/%s/%s",
314 _PathLocale
, locname
, categories
[category
]);
319 if (_xpg4_setrunelocale(__UNCONST(locname
)))
321 if (__runetable_to_netbsd_ctype(locname
)) {
322 /* very unfortunate, but need to go to "C" locale */
323 revert_to_default(category
);
327 if (!__loadctype(name
))
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.
338 (void)snprintf(name
, sizeof(name
), "%s/%s",
339 _PathLocale
, locname
);
343 if (stat(name
, &st
) < 0)
345 if (!S_ISDIR(st
.st_mode
))
361 loadlocale(int category
)
363 //char aliaspath[PATH_MAX], loccat[PATH_MAX], buf[PATH_MAX];
366 _DIAGASSERT(0 < category
&& category
< _LC_LAST
);
368 if (strcmp(new_categories
[category
], current_categories
[category
]) == 0)
369 return (current_categories
[category
]);
371 /* (1) non-aliased file */
372 if (!load_locale_sub(category
, new_categories
[category
], 0))
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))
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))
394 (void)strncpyX(current_categories
[category
],
395 new_categories
[category
],
396 sizeof(current_categories
[category
]));
397 return current_categories
[category
];
401 __get_locale_env(int category
)
405 //_DIAGASSERT(category != LC_ALL);
407 ///* 1. check LC_ALL. */
408 //env = getenv(categories[0]);
410 ///* 2. check LC_* */
412 // env = getenv(categories[category]);
414 ///* 3. check LANG */
416 // env = getenv("LANG");
418 ///* 4. if none is set, fall to "C" */
419 //if (!env || !*env || strchr(env, '/'))