1 --- setrunelocale.c.orig 2007-02-13 00:16:13.000000000 -0800
2 +++ setrunelocale.c 2007-02-13 00:22:39.000000000 -0800
5 __FBSDID("$FreeBSD: src/lib/libc/locale/setrunelocale.c,v 1.44 2004/10/18 02:06:18 ache Exp $");
7 +#include "xlocale_private.h"
14 #include "setlocale.h"
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 *);
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 *);
38 +extern void spin_lock(int *);
39 +extern void spin_unlock(int *);
41 +/* depreciated interfaces */
42 +rune_t sgetrune(const char *, size_t, char const **);
43 +int sputrune(rune_t, char *, size_t, char **);
46 -__setrunelocale(const char *encoding)
47 +__private_extern__ int
48 +__setrunelocale(const char *encoding, locale_t loc)
52 + struct __xlocale_st_runelocale *xrl;
55 - static char ctype_encoding[ENCODING_LEN + 1];
56 - static _RuneLocale *CachedRuneLocale;
57 - static int Cached__mb_cur_max;
58 - static size_t (*Cached__mbrtowc)(wchar_t * __restrict,
59 - const char * __restrict, size_t, mbstate_t * __restrict);
60 - static size_t (*Cached__wcrtomb)(char * __restrict, wchar_t,
61 - mbstate_t * __restrict);
62 - static int (*Cached__mbsinit)(const mbstate_t *);
63 - static size_t (*Cached__mbsnrtowcs)(wchar_t * __restrict,
64 - const char ** __restrict, size_t, size_t, mbstate_t * __restrict);
65 - static size_t (*Cached__wcsnrtombs)(char * __restrict,
66 - const wchar_t ** __restrict, size_t, size_t,
67 - mbstate_t * __restrict);
68 + static struct __xlocale_st_runelocale *CachedRuneLocale;
69 + extern int __mb_cur_max;
70 + static int cache_lock = 0;
73 * The "C" and "POSIX" locale are always here.
75 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
76 - _CurrentRuneLocale = &_DefaultRuneLocale;
78 - __mbrtowc = _none_mbrtowc;
79 - __mbsinit = _none_mbsinit;
80 - __mbsnrtowcs = _none_mbsnrtowcs;
81 - __wcrtomb = _none_wcrtomb;
82 - __wcsnrtombs = _none_wcsnrtombs;
83 + XL_RELEASE(loc->__lc_ctype);
84 + loc->__lc_ctype = &_DefaultRuneXLocale;
85 + /* no need to retain _DefaultRuneXLocale */
86 + if (loc == &__global_locale) {
87 + _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
88 + __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
94 * If the locale name is the same as our cache, use the cache.
96 + spin_lock(&cache_lock);
97 if (CachedRuneLocale != NULL &&
98 - strcmp(encoding, ctype_encoding) == 0) {
99 - _CurrentRuneLocale = CachedRuneLocale;
100 - __mb_cur_max = Cached__mb_cur_max;
101 - __mbrtowc = Cached__mbrtowc;
102 - __mbsinit = Cached__mbsinit;
103 - __mbsnrtowcs = Cached__mbsnrtowcs;
104 - __wcrtomb = Cached__wcrtomb;
105 - __wcsnrtombs = Cached__wcsnrtombs;
106 + strcmp(encoding, CachedRuneLocale->__ctype_encoding) == 0) {
107 + XL_RELEASE(loc->__lc_ctype);
108 + loc->__lc_ctype = CachedRuneLocale;
109 + XL_RETAIN(loc->__lc_ctype);
110 + if (loc == &__global_locale) {
111 + _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
112 + __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
114 + spin_unlock(&cache_lock);
117 + spin_unlock(&cache_lock);
120 * Slurp the locale file into the cache.
121 @@ -124,63 +126,74 @@
122 if ((fp = fopen(name, "r")) == NULL)
123 return (errno == 0 ? ENOENT : errno);
125 - if ((rl = _Read_RuneMagi(fp)) == NULL) {
126 + if ((xrl = _Read_RuneMagi(fp)) == NULL) {
127 saverr = (errno == 0 ? EFTYPE : errno);
135 - __mbsnrtowcs = __mbsnrtowcs_std;
137 - __wcsnrtombs = __wcsnrtombs_std;
138 - rl->__sputrune = NULL;
139 - rl->__sgetrune = NULL;
140 + xrl->__mbrtowc = NULL;
141 + xrl->__mbsinit = NULL;
142 + xrl->__mbsnrtowcs = __mbsnrtowcs_std;
143 + xrl->__wcrtomb = NULL;
144 + xrl->__wcsnrtombs = __wcsnrtombs_std;
146 + rl = &xrl->_CurrentRuneLocale;
148 + /* provide backwards compatibility (depreciated interface) */
149 + rl->__sputrune = sputrune;
150 + rl->__sgetrune = sgetrune;
152 if (strcmp(rl->__encoding, "NONE") == 0)
153 - ret = _none_init(rl);
154 + ret = _none_init(xrl);
155 else if (strcmp(rl->__encoding, "UTF-8") == 0)
156 - ret = _UTF8_init(rl);
157 + ret = _UTF8_init(xrl);
158 else if (strcmp(rl->__encoding, "EUC") == 0)
159 - ret = _EUC_init(rl);
160 + ret = _EUC_init(xrl);
161 else if (strcmp(rl->__encoding, "GB18030") == 0)
162 - ret = _GB18030_init(rl);
163 + ret = _GB18030_init(xrl);
164 else if (strcmp(rl->__encoding, "GB2312") == 0)
165 - ret = _GB2312_init(rl);
166 + ret = _GB2312_init(xrl);
167 else if (strcmp(rl->__encoding, "GBK") == 0)
168 - ret = _GBK_init(rl);
169 + ret = _GBK_init(xrl);
170 else if (strcmp(rl->__encoding, "BIG5") == 0)
171 - ret = _BIG5_init(rl);
172 + ret = _BIG5_init(xrl);
173 else if (strcmp(rl->__encoding, "MSKanji") == 0)
174 - ret = _MSKanji_init(rl);
175 + ret = _MSKanji_init(xrl);
176 + else if (strcmp(rl->__encoding, "UTF2") == 0)
177 + ret = _UTF2_init(xrl);
181 - if (CachedRuneLocale != NULL) {
183 - if (strcmp(CachedRuneLocale->__encoding, "EUC") == 0)
184 - free(CachedRuneLocale->__variable);
185 - free(CachedRuneLocale);
186 + (void)strcpy(xrl->__ctype_encoding, encoding);
187 + XL_RELEASE(loc->__lc_ctype);
188 + loc->__lc_ctype = xrl;
189 + if (loc == &__global_locale) {
190 + _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
191 + __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
193 - CachedRuneLocale = _CurrentRuneLocale;
194 - Cached__mb_cur_max = __mb_cur_max;
195 - Cached__mbrtowc = __mbrtowc;
196 - Cached__mbsinit = __mbsinit;
197 - Cached__mbsnrtowcs = __mbsnrtowcs;
198 - Cached__wcrtomb = __wcrtomb;
199 - Cached__wcsnrtombs = __wcsnrtombs;
200 - (void)strcpy(ctype_encoding, encoding);
201 + spin_lock(&cache_lock);
202 + XL_RELEASE(CachedRuneLocale);
203 + CachedRuneLocale = xrl;
204 + XL_RETAIN(CachedRuneLocale);
205 + spin_unlock(&cache_lock);
214 -__wrap_setrunelocale(const char *locale)
215 +setrunelocale(const char *encoding)
217 + return __setrunelocale(encoding, &__global_locale);
220 +__private_extern__ int
221 +__wrap_setrunelocale(const char *locale, locale_t loc)
223 - int ret = __setrunelocale(locale);
224 + int ret = __setrunelocale(locale, loc);