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