]>
Commit | Line | Data |
---|---|---|
5b2abdfb | 1 | /*- |
e9ce8d39 A |
2 | * Copyright (c) 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Paul Borman at Krystal Technologies. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
e9ce8d39 A |
16 | * 4. Neither the name of the University nor the names of its contributors |
17 | * may be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | * SUCH DAMAGE. | |
31 | */ | |
32 | ||
9385eb3d | 33 | #include <sys/cdefs.h> |
1f2f436a | 34 | __FBSDID("$FreeBSD: src/lib/libc/locale/setrunelocale.c,v 1.51 2008/01/23 03:05:35 ache Exp $"); |
9385eb3d | 35 | |
ad3c9f2a A |
36 | #include "xlocale_private.h" |
37 | ||
3d9156a7 | 38 | #include <runetype.h> |
e9ce8d39 | 39 | #include <errno.h> |
5b2abdfb A |
40 | #include <limits.h> |
41 | #include <string.h> | |
42 | #include <stdio.h> | |
e9ce8d39 | 43 | #include <stdlib.h> |
5b2abdfb | 44 | #include <unistd.h> |
3d9156a7 A |
45 | #include <wchar.h> |
46 | #include "ldpart.h" | |
47 | #include "mblocal.h" | |
5b2abdfb | 48 | #include "setlocale.h" |
e9ce8d39 | 49 | |
ad3c9f2a | 50 | extern struct __xlocale_st_runelocale *_Read_RuneMagi(FILE *); |
e9ce8d39 | 51 | |
ad3c9f2a A |
52 | #ifdef UNIFDEF_LEGACY_RUNE_APIS |
53 | /* depreciated interfaces */ | |
54 | rune_t sgetrune(const char *, size_t, char const **); | |
55 | int sputrune(rune_t, char *, size_t, char **); | |
56 | #endif /* UNIFDEF_LEGACY_RUNE_APIS */ | |
3d9156a7 | 57 | |
ad3c9f2a A |
58 | __private_extern__ int |
59 | __setrunelocale(const char *encoding, locale_t loc) | |
e9ce8d39 | 60 | { |
5b2abdfb A |
61 | FILE *fp; |
62 | char name[PATH_MAX]; | |
ad3c9f2a | 63 | struct __xlocale_st_runelocale *xrl; |
5b2abdfb | 64 | _RuneLocale *rl; |
9385eb3d | 65 | int saverr, ret; |
ad3c9f2a A |
66 | static struct __xlocale_st_runelocale *CachedRuneLocale; |
67 | extern int __mb_cur_max; | |
68 | extern int __mb_sb_limit; | |
b061a43b | 69 | static os_unfair_lock cache_lock = OS_UNFAIR_LOCK_INIT; |
e9ce8d39 | 70 | |
5b2abdfb A |
71 | /* |
72 | * The "C" and "POSIX" locale are always here. | |
73 | */ | |
9385eb3d | 74 | if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) { |
ad3c9f2a A |
75 | XL_RELEASE(loc->__lc_ctype); |
76 | loc->__lc_ctype = &_DefaultRuneXLocale; | |
77 | /* no need to retain _DefaultRuneXLocale */ | |
78 | if (loc == &__global_locale) { | |
79 | _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale; | |
80 | __mb_cur_max = loc->__lc_ctype->__mb_cur_max; | |
81 | __mb_sb_limit = loc->__lc_ctype->__mb_sb_limit; | |
82 | } | |
9385eb3d | 83 | return (0); |
e9ce8d39 | 84 | } |
e9ce8d39 | 85 | |
9385eb3d A |
86 | /* |
87 | * If the locale name is the same as our cache, use the cache. | |
88 | */ | |
b061a43b | 89 | os_unfair_lock_lock(&cache_lock); |
9385eb3d | 90 | if (CachedRuneLocale != NULL && |
ad3c9f2a A |
91 | strcmp(encoding, CachedRuneLocale->__ctype_encoding) == 0) { |
92 | XL_RELEASE(loc->__lc_ctype); | |
93 | loc->__lc_ctype = CachedRuneLocale; | |
94 | XL_RETAIN(loc->__lc_ctype); | |
95 | if (loc == &__global_locale) { | |
96 | _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale; | |
97 | __mb_cur_max = loc->__lc_ctype->__mb_cur_max; | |
98 | __mb_sb_limit = loc->__lc_ctype->__mb_sb_limit; | |
99 | } | |
b061a43b | 100 | os_unfair_lock_unlock(&cache_lock); |
9385eb3d A |
101 | return (0); |
102 | } | |
b061a43b | 103 | os_unfair_lock_unlock(&cache_lock); |
9385eb3d A |
104 | |
105 | /* | |
106 | * Slurp the locale file into the cache. | |
107 | */ | |
3d9156a7 A |
108 | |
109 | /* Range checking not needed, encoding length already checked before */ | |
974e3884 | 110 | (void) strcpy(name, encoding); |
5b2abdfb A |
111 | (void) strcat(name, "/LC_CTYPE"); |
112 | ||
974e3884 | 113 | if ((fp = fdopen(__open_path_locale(name), "r")) == NULL) |
9385eb3d | 114 | return (errno == 0 ? ENOENT : errno); |
5b2abdfb | 115 | |
ad3c9f2a | 116 | if ((xrl = _Read_RuneMagi(fp)) == NULL) { |
9385eb3d A |
117 | saverr = (errno == 0 ? EFTYPE : errno); |
118 | (void)fclose(fp); | |
119 | return (saverr); | |
5b2abdfb | 120 | } |
9385eb3d | 121 | (void)fclose(fp); |
5b2abdfb | 122 | |
ad3c9f2a A |
123 | xrl->__mbrtowc = NULL; |
124 | xrl->__mbsinit = NULL; | |
125 | xrl->__mbsnrtowcs = __mbsnrtowcs_std; | |
126 | xrl->__wcrtomb = NULL; | |
127 | xrl->__wcsnrtombs = __wcsnrtombs_std; | |
1f2f436a | 128 | |
ad3c9f2a | 129 | rl = &xrl->_CurrentRuneLocale; |
1f2f436a | 130 | |
ad3c9f2a A |
131 | #ifdef UNIFDEF_LEGACY_RUNE_APIS |
132 | /* provide backwards compatibility (depreciated interface) */ | |
133 | rl->__sputrune = sputrune; | |
134 | rl->__sgetrune = sgetrune; | |
135 | #else /* UNIFDEF_LEGACY_RUNE_APIS */ | |
3d9156a7 A |
136 | rl->__sputrune = NULL; |
137 | rl->__sgetrune = NULL; | |
ad3c9f2a A |
138 | #endif /* UNIFDEF_LEGACY_RUNE_APIS */ |
139 | ||
3d9156a7 | 140 | if (strcmp(rl->__encoding, "NONE") == 0) |
ad3c9f2a | 141 | ret = _none_init(xrl); |
1f2f436a | 142 | else if (strcmp(rl->__encoding, "ASCII") == 0) |
ad3c9f2a | 143 | ret = _ascii_init(xrl); |
3d9156a7 | 144 | else if (strcmp(rl->__encoding, "UTF-8") == 0) |
ad3c9f2a | 145 | ret = _UTF8_init(xrl); |
3d9156a7 | 146 | else if (strcmp(rl->__encoding, "EUC") == 0) |
ad3c9f2a | 147 | ret = _EUC_init(xrl); |
1f2f436a | 148 | else if (strcmp(rl->__encoding, "GB18030") == 0) |
ad3c9f2a | 149 | ret = _GB18030_init(xrl); |
3d9156a7 | 150 | else if (strcmp(rl->__encoding, "GB2312") == 0) |
ad3c9f2a | 151 | ret = _GB2312_init(xrl); |
3d9156a7 | 152 | else if (strcmp(rl->__encoding, "GBK") == 0) |
ad3c9f2a | 153 | ret = _GBK_init(xrl); |
3d9156a7 | 154 | else if (strcmp(rl->__encoding, "BIG5") == 0) |
ad3c9f2a | 155 | ret = _BIG5_init(xrl); |
3d9156a7 | 156 | else if (strcmp(rl->__encoding, "MSKanji") == 0) |
ad3c9f2a A |
157 | ret = _MSKanji_init(xrl); |
158 | else if (strcmp(rl->__encoding, "UTF2") == 0) | |
159 | ret = _UTF2_init(xrl); | |
5b2abdfb | 160 | else |
9385eb3d | 161 | ret = EFTYPE; |
1f2f436a | 162 | |
9385eb3d | 163 | if (ret == 0) { |
ad3c9f2a A |
164 | (void)strcpy(xrl->__ctype_encoding, encoding); |
165 | XL_RELEASE(loc->__lc_ctype); | |
166 | loc->__lc_ctype = xrl; | |
167 | if (loc == &__global_locale) { | |
168 | _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale; | |
169 | __mb_cur_max = loc->__lc_ctype->__mb_cur_max; | |
170 | __mb_sb_limit = loc->__lc_ctype->__mb_sb_limit; | |
9385eb3d | 171 | } |
b061a43b | 172 | os_unfair_lock_lock(&cache_lock); |
ad3c9f2a A |
173 | XL_RELEASE(CachedRuneLocale); |
174 | CachedRuneLocale = xrl; | |
175 | XL_RETAIN(CachedRuneLocale); | |
b061a43b | 176 | os_unfair_lock_unlock(&cache_lock); |
ad3c9f2a A |
177 | } else |
178 | XL_RELEASE(xrl); | |
9385eb3d A |
179 | |
180 | return (ret); | |
e9ce8d39 | 181 | } |
5b2abdfb | 182 | |
ad3c9f2a | 183 | #ifdef UNIFDEF_LEGACY_RUNE_APIS |
3d9156a7 | 184 | int |
ad3c9f2a A |
185 | setrunelocale(const char *encoding) |
186 | { | |
187 | int ret; | |
188 | ||
189 | XL_LOCK(&__global_locale); | |
190 | ret = __setrunelocale(encoding, &__global_locale); | |
191 | XL_UNLOCK(&__global_locale); | |
192 | return ret; | |
193 | } | |
194 | #endif /* UNIFDEF_LEGACY_RUNE_APIS */ | |
195 | ||
196 | __private_extern__ int | |
197 | __wrap_setrunelocale(const char *locale, locale_t loc) | |
3d9156a7 | 198 | { |
ad3c9f2a | 199 | int ret = __setrunelocale(locale, loc); |
3d9156a7 A |
200 | |
201 | if (ret != 0) { | |
202 | errno = ret; | |
203 | return (_LDP_ERROR); | |
204 | } | |
205 | return (_LDP_LOADED); | |
206 | } | |
207 |