1 --- setrunelocale.c.orig 2004-11-25 11:38:19.000000000 -0800
2 +++ setrunelocale.c 2005-02-17 10:00:02.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 +/* depreciated interfaces */
39 +rune_t sgetrune(const char *, size_t, char const **);
40 +int sputrune(rune_t, char *, size_t, char **);
43 -__setrunelocale(const char *encoding)
44 +__private_extern__ int
45 +__setrunelocale(const char *encoding, locale_t loc)
49 + struct __xlocale_st_runelocale *xrl;
52 - static char ctype_encoding[ENCODING_LEN + 1];
53 - static _RuneLocale *CachedRuneLocale;
54 - static int Cached__mb_cur_max;
55 - static size_t (*Cached__mbrtowc)(wchar_t * __restrict,
56 - const char * __restrict, size_t, mbstate_t * __restrict);
57 - static size_t (*Cached__wcrtomb)(char * __restrict, wchar_t,
58 - mbstate_t * __restrict);
59 - static int (*Cached__mbsinit)(const mbstate_t *);
60 - static size_t (*Cached__mbsnrtowcs)(wchar_t * __restrict,
61 - const char ** __restrict, size_t, size_t, mbstate_t * __restrict);
62 - static size_t (*Cached__wcsnrtombs)(char * __restrict,
63 - const wchar_t ** __restrict, size_t, size_t,
64 - mbstate_t * __restrict);
65 + static struct __xlocale_st_runelocale *CachedRuneLocale;
66 + extern int __mb_cur_max;
69 * The "C" and "POSIX" locale are always here.
71 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
72 - _CurrentRuneLocale = &_DefaultRuneLocale;
74 - __mbrtowc = _none_mbrtowc;
75 - __mbsinit = _none_mbsinit;
76 - __mbsnrtowcs = _none_mbsnrtowcs;
77 - __wcrtomb = _none_wcrtomb;
78 - __wcsnrtombs = _none_wcsnrtombs;
79 + XL_RELEASE(loc->__lc_ctype);
80 + loc->__lc_ctype = &_DefaultRuneXLocale;
81 + /* no need to retain _DefaultRuneXLocale */
82 + if (loc == &__global_locale) {
83 + _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
84 + __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
90 * If the locale name is the same as our cache, use the cache.
92 if (CachedRuneLocale != NULL &&
93 - strcmp(encoding, ctype_encoding) == 0) {
94 - _CurrentRuneLocale = CachedRuneLocale;
95 - __mb_cur_max = Cached__mb_cur_max;
96 - __mbrtowc = Cached__mbrtowc;
97 - __mbsinit = Cached__mbsinit;
98 - __mbsnrtowcs = Cached__mbsnrtowcs;
99 - __wcrtomb = Cached__wcrtomb;
100 - __wcsnrtombs = Cached__wcsnrtombs;
101 + strcmp(encoding, CachedRuneLocale->__ctype_encoding) == 0) {
102 + XL_RELEASE(loc->__lc_ctype);
103 + loc->__lc_ctype = CachedRuneLocale;
104 + XL_RETAIN(loc->__lc_ctype);
105 + if (loc == &__global_locale) {
106 + _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
107 + __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
112 @@ -124,63 +119,72 @@
113 if ((fp = fopen(name, "r")) == NULL)
114 return (errno == 0 ? ENOENT : errno);
116 - if ((rl = _Read_RuneMagi(fp)) == NULL) {
117 + if ((xrl = _Read_RuneMagi(fp)) == NULL) {
118 saverr = (errno == 0 ? EFTYPE : errno);
126 - __mbsnrtowcs = __mbsnrtowcs_std;
128 - __wcsnrtombs = __wcsnrtombs_std;
129 - rl->__sputrune = NULL;
130 - rl->__sgetrune = NULL;
131 + xrl->__mbrtowc = NULL;
132 + xrl->__mbsinit = NULL;
133 + xrl->__mbsnrtowcs = __mbsnrtowcs_std;
134 + xrl->__wcrtomb = NULL;
135 + xrl->__wcsnrtombs = __wcsnrtombs_std;
137 + rl = &xrl->_CurrentRuneLocale;
139 + /* provide backwards compatibility (depreciated interface) */
140 + rl->__sputrune = sputrune;
141 + rl->__sgetrune = sgetrune;
143 if (strcmp(rl->__encoding, "NONE") == 0)
144 - ret = _none_init(rl);
145 + ret = _none_init(xrl);
146 else if (strcmp(rl->__encoding, "UTF-8") == 0)
147 - ret = _UTF8_init(rl);
148 + ret = _UTF8_init(xrl);
149 else if (strcmp(rl->__encoding, "EUC") == 0)
150 - ret = _EUC_init(rl);
151 + ret = _EUC_init(xrl);
152 else if (strcmp(rl->__encoding, "GB18030") == 0)
153 - ret = _GB18030_init(rl);
154 + ret = _GB18030_init(xrl);
155 else if (strcmp(rl->__encoding, "GB2312") == 0)
156 - ret = _GB2312_init(rl);
157 + ret = _GB2312_init(xrl);
158 else if (strcmp(rl->__encoding, "GBK") == 0)
159 - ret = _GBK_init(rl);
160 + ret = _GBK_init(xrl);
161 else if (strcmp(rl->__encoding, "BIG5") == 0)
162 - ret = _BIG5_init(rl);
163 + ret = _BIG5_init(xrl);
164 else if (strcmp(rl->__encoding, "MSKanji") == 0)
165 - ret = _MSKanji_init(rl);
166 + ret = _MSKanji_init(xrl);
167 + else if (strcmp(rl->__encoding, "UTF2") == 0)
168 + ret = _UTF2_init(xrl);
172 - if (CachedRuneLocale != NULL) {
174 - if (strcmp(CachedRuneLocale->__encoding, "EUC") == 0)
175 - free(CachedRuneLocale->__variable);
176 - free(CachedRuneLocale);
177 + (void)strcpy(xrl->__ctype_encoding, encoding);
178 + XL_RELEASE(loc->__lc_ctype);
179 + loc->__lc_ctype = xrl;
180 + if (loc == &__global_locale) {
181 + _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
182 + __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
184 - CachedRuneLocale = _CurrentRuneLocale;
185 - Cached__mb_cur_max = __mb_cur_max;
186 - Cached__mbrtowc = __mbrtowc;
187 - Cached__mbsinit = __mbsinit;
188 - Cached__mbsnrtowcs = __mbsnrtowcs;
189 - Cached__wcrtomb = __wcrtomb;
190 - Cached__wcsnrtombs = __wcsnrtombs;
191 - (void)strcpy(ctype_encoding, encoding);
192 + XL_RELEASE(CachedRuneLocale);
193 + CachedRuneLocale = xrl;
194 + XL_RETAIN(CachedRuneLocale);
203 -__wrap_setrunelocale(const char *locale)
204 +setrunelocale(const char *encoding)
206 + return __setrunelocale(encoding, &__global_locale);
209 +__private_extern__ int
210 +__wrap_setrunelocale(const char *locale, locale_t loc)
212 - int ret = __setrunelocale(locale);
213 + int ret = __setrunelocale(locale, loc);