1 --- setrunelocale.c.bsdnew 2009-11-09 15:05:25.000000000 -0800
2 +++ setrunelocale.c 2009-11-09 17:20:45.000000000 -0800
5 __FBSDID("$FreeBSD: src/lib/libc/locale/setrunelocale.c,v 1.51 2008/01/23 03:05:35 ache Exp $");
7 +#include "xlocale_private.h"
12 @@ -45,67 +47,60 @@ __FBSDID("$FreeBSD: src/lib/libc/locale/
14 #include "setlocale.h"
16 -extern int __mb_sb_limit;
18 -extern _RuneLocale *_Read_RuneMagi(FILE *);
19 +extern struct __xlocale_st_runelocale *_Read_RuneMagi(FILE *);
21 -static int __setrunelocale(const char *);
22 +#ifdef UNIFDEF_LEGACY_RUNE_APIS
23 +/* depreciated interfaces */
24 +rune_t sgetrune(const char *, size_t, char const **);
25 +int sputrune(rune_t, char *, size_t, char **);
26 +#endif /* UNIFDEF_LEGACY_RUNE_APIS */
29 -__setrunelocale(const char *encoding)
30 +__private_extern__ int
31 +__setrunelocale(const char *encoding, locale_t loc)
35 + struct __xlocale_st_runelocale *xrl;
38 - size_t (*old__mbrtowc)(wchar_t * __restrict,
39 - const char * __restrict, size_t, mbstate_t * __restrict);
40 - size_t (*old__wcrtomb)(char * __restrict, wchar_t,
41 - mbstate_t * __restrict);
42 - int (*old__mbsinit)(const mbstate_t *);
43 - size_t (*old__mbsnrtowcs)(wchar_t * __restrict,
44 - const char ** __restrict, size_t, size_t, mbstate_t * __restrict);
45 - size_t (*old__wcsnrtombs)(char * __restrict,
46 - const wchar_t ** __restrict, size_t, size_t,
47 - mbstate_t * __restrict);
48 - static char ctype_encoding[ENCODING_LEN + 1];
49 - static _RuneLocale *CachedRuneLocale;
50 - static int Cached__mb_cur_max;
51 - static int Cached__mb_sb_limit;
52 - static size_t (*Cached__mbrtowc)(wchar_t * __restrict,
53 - const char * __restrict, size_t, mbstate_t * __restrict);
54 - static size_t (*Cached__wcrtomb)(char * __restrict, wchar_t,
55 - mbstate_t * __restrict);
56 - static int (*Cached__mbsinit)(const mbstate_t *);
57 - static size_t (*Cached__mbsnrtowcs)(wchar_t * __restrict,
58 - const char ** __restrict, size_t, size_t, mbstate_t * __restrict);
59 - static size_t (*Cached__wcsnrtombs)(char * __restrict,
60 - const wchar_t ** __restrict, size_t, size_t,
61 - mbstate_t * __restrict);
62 + static struct __xlocale_st_runelocale *CachedRuneLocale;
63 + extern int __mb_cur_max;
64 + extern int __mb_sb_limit;
65 + static pthread_lock_t cache_lock = LOCK_INITIALIZER;
68 * The "C" and "POSIX" locale are always here.
70 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
71 - (void) _none_init(&_DefaultRuneLocale);
72 + XL_RELEASE(loc->__lc_ctype);
73 + loc->__lc_ctype = &_DefaultRuneXLocale;
74 + /* no need to retain _DefaultRuneXLocale */
75 + if (loc == &__global_locale) {
76 + _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
77 + __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
78 + __mb_sb_limit = loc->__lc_ctype->__mb_sb_limit;
84 * If the locale name is the same as our cache, use the cache.
87 if (CachedRuneLocale != NULL &&
88 - strcmp(encoding, ctype_encoding) == 0) {
89 - _CurrentRuneLocale = CachedRuneLocale;
90 - __mb_cur_max = Cached__mb_cur_max;
91 - __mb_sb_limit = Cached__mb_sb_limit;
92 - __mbrtowc = Cached__mbrtowc;
93 - __mbsinit = Cached__mbsinit;
94 - __mbsnrtowcs = Cached__mbsnrtowcs;
95 - __wcrtomb = Cached__wcrtomb;
96 - __wcsnrtombs = Cached__wcsnrtombs;
97 + strcmp(encoding, CachedRuneLocale->__ctype_encoding) == 0) {
98 + XL_RELEASE(loc->__lc_ctype);
99 + loc->__lc_ctype = CachedRuneLocale;
100 + XL_RETAIN(loc->__lc_ctype);
101 + if (loc == &__global_locale) {
102 + _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
103 + __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
104 + __mb_sb_limit = loc->__lc_ctype->__mb_sb_limit;
106 + UNLOCK(cache_lock);
109 + UNLOCK(cache_lock);
112 * Slurp the locale file into the cache.
113 @@ -120,80 +115,90 @@ __setrunelocale(const char *encoding)
114 if ((fp = fopen(name, "r")) == NULL)
115 return (errno == 0 ? ENOENT : errno);
117 - if ((rl = _Read_RuneMagi(fp)) == NULL) {
118 + if ((xrl = _Read_RuneMagi(fp)) == NULL) {
119 saverr = (errno == 0 ? EFTYPE : errno);
125 - old__mbrtowc = __mbrtowc;
126 - old__mbsinit = __mbsinit;
127 - old__mbsnrtowcs = __mbsnrtowcs;
128 - old__wcrtomb = __wcrtomb;
129 - old__wcsnrtombs = __wcsnrtombs;
133 - __mbsnrtowcs = __mbsnrtowcs_std;
135 - __wcsnrtombs = __wcsnrtombs_std;
137 + xrl->__mbrtowc = NULL;
138 + xrl->__mbsinit = NULL;
139 + xrl->__mbsnrtowcs = __mbsnrtowcs_std;
140 + xrl->__wcrtomb = NULL;
141 + xrl->__wcsnrtombs = __wcsnrtombs_std;
143 + rl = &xrl->_CurrentRuneLocale;
145 +#ifdef UNIFDEF_LEGACY_RUNE_APIS
146 + /* provide backwards compatibility (depreciated interface) */
147 + rl->__sputrune = sputrune;
148 + rl->__sgetrune = sgetrune;
149 +#else /* UNIFDEF_LEGACY_RUNE_APIS */
150 rl->__sputrune = NULL;
151 rl->__sgetrune = NULL;
152 +#endif /* UNIFDEF_LEGACY_RUNE_APIS */
154 if (strcmp(rl->__encoding, "NONE") == 0)
155 - ret = _none_init(rl);
156 + ret = _none_init(xrl);
157 else if (strcmp(rl->__encoding, "ASCII") == 0)
158 - ret = _ascii_init(rl);
159 + ret = _ascii_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);
187 - if (CachedRuneLocale != NULL) {
189 - if (strcmp(CachedRuneLocale->__encoding, "EUC") == 0)
190 - free(CachedRuneLocale->__variable);
191 - free(CachedRuneLocale);
192 + (void)strcpy(xrl->__ctype_encoding, encoding);
193 + XL_RELEASE(loc->__lc_ctype);
194 + loc->__lc_ctype = xrl;
195 + if (loc == &__global_locale) {
196 + _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
197 + __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
198 + __mb_sb_limit = loc->__lc_ctype->__mb_sb_limit;
200 - CachedRuneLocale = _CurrentRuneLocale;
201 - Cached__mb_cur_max = __mb_cur_max;
202 - Cached__mb_sb_limit = __mb_sb_limit;
203 - Cached__mbrtowc = __mbrtowc;
204 - Cached__mbsinit = __mbsinit;
205 - Cached__mbsnrtowcs = __mbsnrtowcs;
206 - Cached__wcrtomb = __wcrtomb;
207 - Cached__wcsnrtombs = __wcsnrtombs;
208 - (void)strcpy(ctype_encoding, encoding);
210 - __mbrtowc = old__mbrtowc;
211 - __mbsinit = old__mbsinit;
212 - __mbsnrtowcs = old__mbsnrtowcs;
213 - __wcrtomb = old__wcrtomb;
214 - __wcsnrtombs = old__wcsnrtombs;
218 + XL_RELEASE(CachedRuneLocale);
219 + CachedRuneLocale = xrl;
220 + XL_RETAIN(CachedRuneLocale);
221 + UNLOCK(cache_lock);
228 +#ifdef UNIFDEF_LEGACY_RUNE_APIS
230 -__wrap_setrunelocale(const char *locale)
231 +setrunelocale(const char *encoding)
235 + XL_LOCK(&__global_locale);
236 + ret = __setrunelocale(encoding, &__global_locale);
237 + XL_UNLOCK(&__global_locale);
240 +#endif /* UNIFDEF_LEGACY_RUNE_APIS */
242 +__private_extern__ int
243 +__wrap_setrunelocale(const char *locale, locale_t loc)
245 - int ret = __setrunelocale(locale);
246 + int ret = __setrunelocale(locale, loc);