]> git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/setrunelocale.c.patch
Libc-498.1.5.tar.gz
[apple/libc.git] / locale / FreeBSD / setrunelocale.c.patch
1 --- setrunelocale.c.orig 2008-04-28 16:25:31.000000000 -0700
2 +++ setrunelocale.c 2008-04-28 17:02:02.000000000 -0700
3 @@ -37,6 +37,8 @@
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD: src/lib/libc/locale/setrunelocale.c,v 1.44 2004/10/18 02:06:18 ache Exp $");
6
7 +#include "xlocale_private.h"
8 +
9 #include <runetype.h>
10 #include <errno.h>
11 #include <limits.h>
12 @@ -49,67 +51,69 @@ __FBSDID("$FreeBSD: src/lib/libc/locale/
13 #include "mblocal.h"
14 #include "setlocale.h"
15
16 -extern int _none_init(_RuneLocale *);
17 -extern int _UTF8_init(_RuneLocale *);
18 -extern int _EUC_init(_RuneLocale *);
19 -extern int _GB18030_init(_RuneLocale *);
20 -extern int _GB2312_init(_RuneLocale *);
21 -extern int _GBK_init(_RuneLocale *);
22 -extern int _BIG5_init(_RuneLocale *);
23 -extern int _MSKanji_init(_RuneLocale *);
24 -extern _RuneLocale *_Read_RuneMagi(FILE *);
25 -
26 -static int __setrunelocale(const char *);
27 +extern int _none_init(struct __xlocale_st_runelocale *);
28 +extern int _UTF8_init(struct __xlocale_st_runelocale *);
29 +extern int _EUC_init(struct __xlocale_st_runelocale *);
30 +extern int _GB18030_init(struct __xlocale_st_runelocale *);
31 +extern int _GB2312_init(struct __xlocale_st_runelocale *);
32 +extern int _GBK_init(struct __xlocale_st_runelocale *);
33 +extern int _BIG5_init(struct __xlocale_st_runelocale *);
34 +extern int _MSKanji_init(struct __xlocale_st_runelocale *);
35 +extern int _UTF2_init(struct __xlocale_st_runelocale *); /* deprecated */
36 +extern struct __xlocale_st_runelocale *_Read_RuneMagi(FILE *);
37 +
38 +extern void spin_lock(int *);
39 +extern void spin_unlock(int *);
40 +
41 +#ifdef LEGACY_RUNE_APIS
42 +/* depreciated interfaces */
43 +rune_t sgetrune(const char *, size_t, char const **);
44 +int sputrune(rune_t, char *, size_t, char **);
45 +#endif /* LEGACY_RUNE_APIS */
46
47 -static int
48 -__setrunelocale(const char *encoding)
49 +__private_extern__ int
50 +__setrunelocale(const char *encoding, locale_t loc)
51 {
52 FILE *fp;
53 char name[PATH_MAX];
54 + struct __xlocale_st_runelocale *xrl;
55 _RuneLocale *rl;
56 int saverr, ret;
57 - static char ctype_encoding[ENCODING_LEN + 1];
58 - static _RuneLocale *CachedRuneLocale;
59 - static int Cached__mb_cur_max;
60 - static size_t (*Cached__mbrtowc)(wchar_t * __restrict,
61 - const char * __restrict, size_t, mbstate_t * __restrict);
62 - static size_t (*Cached__wcrtomb)(char * __restrict, wchar_t,
63 - mbstate_t * __restrict);
64 - static int (*Cached__mbsinit)(const mbstate_t *);
65 - static size_t (*Cached__mbsnrtowcs)(wchar_t * __restrict,
66 - const char ** __restrict, size_t, size_t, mbstate_t * __restrict);
67 - static size_t (*Cached__wcsnrtombs)(char * __restrict,
68 - const wchar_t ** __restrict, size_t, size_t,
69 - mbstate_t * __restrict);
70 + static struct __xlocale_st_runelocale *CachedRuneLocale;
71 + extern int __mb_cur_max;
72 + static int cache_lock = 0;
73
74 /*
75 * The "C" and "POSIX" locale are always here.
76 */
77 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
78 - _CurrentRuneLocale = &_DefaultRuneLocale;
79 - __mb_cur_max = 1;
80 - __mbrtowc = _none_mbrtowc;
81 - __mbsinit = _none_mbsinit;
82 - __mbsnrtowcs = _none_mbsnrtowcs;
83 - __wcrtomb = _none_wcrtomb;
84 - __wcsnrtombs = _none_wcsnrtombs;
85 + XL_RELEASE(loc->__lc_ctype);
86 + loc->__lc_ctype = &_DefaultRuneXLocale;
87 + /* no need to retain _DefaultRuneXLocale */
88 + if (loc == &__global_locale) {
89 + _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
90 + __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
91 + }
92 return (0);
93 }
94
95 /*
96 * If the locale name is the same as our cache, use the cache.
97 */
98 + spin_lock(&cache_lock);
99 if (CachedRuneLocale != NULL &&
100 - strcmp(encoding, ctype_encoding) == 0) {
101 - _CurrentRuneLocale = CachedRuneLocale;
102 - __mb_cur_max = Cached__mb_cur_max;
103 - __mbrtowc = Cached__mbrtowc;
104 - __mbsinit = Cached__mbsinit;
105 - __mbsnrtowcs = Cached__mbsnrtowcs;
106 - __wcrtomb = Cached__wcrtomb;
107 - __wcsnrtombs = Cached__wcsnrtombs;
108 + strcmp(encoding, CachedRuneLocale->__ctype_encoding) == 0) {
109 + XL_RELEASE(loc->__lc_ctype);
110 + loc->__lc_ctype = CachedRuneLocale;
111 + XL_RETAIN(loc->__lc_ctype);
112 + if (loc == &__global_locale) {
113 + _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
114 + __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
115 + }
116 + spin_unlock(&cache_lock);
117 return (0);
118 }
119 + spin_unlock(&cache_lock);
120
121 /*
122 * Slurp the locale file into the cache.
123 @@ -124,63 +128,81 @@ __setrunelocale(const char *encoding)
124 if ((fp = fopen(name, "r")) == NULL)
125 return (errno == 0 ? ENOENT : errno);
126
127 - if ((rl = _Read_RuneMagi(fp)) == NULL) {
128 + if ((xrl = _Read_RuneMagi(fp)) == NULL) {
129 saverr = (errno == 0 ? EFTYPE : errno);
130 (void)fclose(fp);
131 return (saverr);
132 }
133 (void)fclose(fp);
134
135 - __mbrtowc = NULL;
136 - __mbsinit = NULL;
137 - __mbsnrtowcs = __mbsnrtowcs_std;
138 - __wcrtomb = NULL;
139 - __wcsnrtombs = __wcsnrtombs_std;
140 + xrl->__mbrtowc = NULL;
141 + xrl->__mbsinit = NULL;
142 + xrl->__mbsnrtowcs = __mbsnrtowcs_std;
143 + xrl->__wcrtomb = NULL;
144 + xrl->__wcsnrtombs = __wcsnrtombs_std;
145 +
146 + rl = &xrl->_CurrentRuneLocale;
147 +
148 +#ifdef LEGACY_RUNE_APIS
149 + /* provide backwards compatibility (depreciated interface) */
150 + rl->__sputrune = sputrune;
151 + rl->__sgetrune = sgetrune;
152 +#else /* LEGACY_RUNE_APIS */
153 rl->__sputrune = NULL;
154 rl->__sgetrune = NULL;
155 +#endif /* LEGACY_RUNE_APIS */
156 +
157 if (strcmp(rl->__encoding, "NONE") == 0)
158 - ret = _none_init(rl);
159 + ret = _none_init(xrl);
160 else if (strcmp(rl->__encoding, "UTF-8") == 0)
161 - ret = _UTF8_init(rl);
162 + ret = _UTF8_init(xrl);
163 else if (strcmp(rl->__encoding, "EUC") == 0)
164 - ret = _EUC_init(rl);
165 + ret = _EUC_init(xrl);
166 else if (strcmp(rl->__encoding, "GB18030") == 0)
167 - ret = _GB18030_init(rl);
168 + ret = _GB18030_init(xrl);
169 else if (strcmp(rl->__encoding, "GB2312") == 0)
170 - ret = _GB2312_init(rl);
171 + ret = _GB2312_init(xrl);
172 else if (strcmp(rl->__encoding, "GBK") == 0)
173 - ret = _GBK_init(rl);
174 + ret = _GBK_init(xrl);
175 else if (strcmp(rl->__encoding, "BIG5") == 0)
176 - ret = _BIG5_init(rl);
177 + ret = _BIG5_init(xrl);
178 else if (strcmp(rl->__encoding, "MSKanji") == 0)
179 - ret = _MSKanji_init(rl);
180 + ret = _MSKanji_init(xrl);
181 + else if (strcmp(rl->__encoding, "UTF2") == 0)
182 + ret = _UTF2_init(xrl);
183 else
184 ret = EFTYPE;
185 if (ret == 0) {
186 - if (CachedRuneLocale != NULL) {
187 - /* See euc.c */
188 - if (strcmp(CachedRuneLocale->__encoding, "EUC") == 0)
189 - free(CachedRuneLocale->__variable);
190 - free(CachedRuneLocale);
191 + (void)strcpy(xrl->__ctype_encoding, encoding);
192 + XL_RELEASE(loc->__lc_ctype);
193 + loc->__lc_ctype = xrl;
194 + if (loc == &__global_locale) {
195 + _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
196 + __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
197 }
198 - CachedRuneLocale = _CurrentRuneLocale;
199 - Cached__mb_cur_max = __mb_cur_max;
200 - Cached__mbrtowc = __mbrtowc;
201 - Cached__mbsinit = __mbsinit;
202 - Cached__mbsnrtowcs = __mbsnrtowcs;
203 - Cached__wcrtomb = __wcrtomb;
204 - Cached__wcsnrtombs = __wcsnrtombs;
205 - (void)strcpy(ctype_encoding, encoding);
206 + spin_lock(&cache_lock);
207 + XL_RELEASE(CachedRuneLocale);
208 + CachedRuneLocale = xrl;
209 + XL_RETAIN(CachedRuneLocale);
210 + spin_unlock(&cache_lock);
211 } else
212 - free(rl);
213 + XL_RELEASE(xrl);
214
215 return (ret);
216 }
217
218 +#ifdef LEGACY_RUNE_APIS
219 int
220 -__wrap_setrunelocale(const char *locale)
221 +setrunelocale(const char *encoding)
222 +{
223 + return __setrunelocale(encoding, &__global_locale);
224 +}
225 +#endif /* LEGACY_RUNE_APIS */
226 +
227 +__private_extern__ int
228 +__wrap_setrunelocale(const char *locale, locale_t loc)
229 {
230 - int ret = __setrunelocale(locale);
231 + int ret = __setrunelocale(locale, loc);
232
233 if (ret != 0) {
234 errno = ret;