]> git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/setrunelocale.c.patch
Libc-391.2.3.tar.gz
[apple/libc.git] / locale / FreeBSD / setrunelocale.c.patch
1 --- setrunelocale.c.orig 2004-11-25 11:38:19.000000000 -0800
2 +++ setrunelocale.c 2005-02-17 10:00:02.000000000 -0800
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,50 +51,43 @@
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 +/* depreciated interfaces */
39 +rune_t sgetrune(const char *, size_t, char const **);
40 +int sputrune(rune_t, char *, size_t, char **);
41
42 -static int
43 -__setrunelocale(const char *encoding)
44 +__private_extern__ int
45 +__setrunelocale(const char *encoding, locale_t loc)
46 {
47 FILE *fp;
48 char name[PATH_MAX];
49 + struct __xlocale_st_runelocale *xrl;
50 _RuneLocale *rl;
51 int saverr, ret;
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;
67
68 /*
69 * The "C" and "POSIX" locale are always here.
70 */
71 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
72 - _CurrentRuneLocale = &_DefaultRuneLocale;
73 - __mb_cur_max = 1;
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;
85 + }
86 return (0);
87 }
88
89 @@ -100,14 +95,14 @@
90 * If the locale name is the same as our cache, use the cache.
91 */
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;
108 + }
109 return (0);
110 }
111
112 @@ -124,63 +119,72 @@
113 if ((fp = fopen(name, "r")) == NULL)
114 return (errno == 0 ? ENOENT : errno);
115
116 - if ((rl = _Read_RuneMagi(fp)) == NULL) {
117 + if ((xrl = _Read_RuneMagi(fp)) == NULL) {
118 saverr = (errno == 0 ? EFTYPE : errno);
119 (void)fclose(fp);
120 return (saverr);
121 }
122 (void)fclose(fp);
123
124 - __mbrtowc = NULL;
125 - __mbsinit = NULL;
126 - __mbsnrtowcs = __mbsnrtowcs_std;
127 - __wcrtomb = NULL;
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;
136 +
137 + rl = &xrl->_CurrentRuneLocale;
138 +
139 + /* provide backwards compatibility (depreciated interface) */
140 + rl->__sputrune = sputrune;
141 + rl->__sgetrune = sgetrune;
142 +
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);
169 else
170 ret = EFTYPE;
171 if (ret == 0) {
172 - if (CachedRuneLocale != NULL) {
173 - /* See euc.c */
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;
183 }
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);
195 } else
196 - free(rl);
197 + XL_RELEASE(xrl);
198
199 return (ret);
200 }
201
202 int
203 -__wrap_setrunelocale(const char *locale)
204 +setrunelocale(const char *encoding)
205 +{
206 + return __setrunelocale(encoding, &__global_locale);
207 +}
208 +
209 +__private_extern__ int
210 +__wrap_setrunelocale(const char *locale, locale_t loc)
211 {
212 - int ret = __setrunelocale(locale);
213 + int ret = __setrunelocale(locale, loc);
214
215 if (ret != 0) {
216 errno = ret;