]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/lib/libc/locale/setrunelocale.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / lib / libc / locale / setrunelocale.c
1 /*-
2  * Copyright (c) 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Paul Borman at Krystal Technologies.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <runetype.h>
37 #include <errno.h>
38 #include <limits.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <wchar.h>
44 #include "ldpart.h"
45 #include "mblocal.h"
46 #include "setlocale.h"
47
48 extern _RuneLocale      *_Read_RuneMagi(FILE *);
49
50 static int              __setrunelocale(const char *);
51
52 static int
53 __setrunelocale(const char *encoding)
54 {
55         FILE *fp;
56         char name[PATH_MAX];
57         _RuneLocale *rl;
58         int saverr, ret;
59         size_t (*old__mbrtowc)(wchar_t * __restrict,
60             const char * __restrict, size_t, mbstate_t * __restrict);
61         size_t (*old__wcrtomb)(char * __restrict, wchar_t,
62             mbstate_t * __restrict);
63         int (*old__mbsinit)(const mbstate_t *);
64         size_t (*old__mbsnrtowcs)(wchar_t * __restrict,
65             const char ** __restrict, size_t, size_t, mbstate_t * __restrict);
66         size_t (*old__wcsnrtombs)(char * __restrict,
67             const wchar_t ** __restrict, size_t, size_t,
68             mbstate_t * __restrict);
69         static char ctype_encoding[ENCODING_LEN + 1];
70         static _RuneLocale *CachedRuneLocale;
71         static int Cached__mb_cur_max;
72         static size_t (*Cached__mbrtowc)(wchar_t * __restrict,
73             const char * __restrict, size_t, mbstate_t * __restrict);
74         static size_t (*Cached__wcrtomb)(char * __restrict, wchar_t,
75             mbstate_t * __restrict);
76         static int (*Cached__mbsinit)(const mbstate_t *);
77         static size_t (*Cached__mbsnrtowcs)(wchar_t * __restrict,
78             const char ** __restrict, size_t, size_t, mbstate_t * __restrict);
79         static size_t (*Cached__wcsnrtombs)(char * __restrict,
80             const wchar_t ** __restrict, size_t, size_t,
81             mbstate_t * __restrict);
82
83         /*
84          * The "C" and "POSIX" locale are always here.
85          */
86         if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
87                 (void) _none_init(&_DefaultRuneLocale);
88                 return (0);
89         }
90
91         /*
92          * If the locale name is the same as our cache, use the cache.
93          */
94         if (CachedRuneLocale != NULL &&
95             strcmp(encoding, ctype_encoding) == 0) {
96                 _CurrentRuneLocale = CachedRuneLocale;
97                 __mb_cur_max = Cached__mb_cur_max;
98                 __mbrtowc = Cached__mbrtowc;
99                 __mbsinit = Cached__mbsinit;
100                 __mbsnrtowcs = Cached__mbsnrtowcs;
101                 __wcrtomb = Cached__wcrtomb;
102                 __wcsnrtombs = Cached__wcsnrtombs;
103                 return (0);
104         }
105
106         /*
107          * Slurp the locale file into the cache.
108          */
109
110         /* Range checking not needed, encoding length already checked before */
111         (void) strcpy(name, _PathLocale);
112         (void) strcat(name, "/");
113         (void) strcat(name, encoding);
114         (void) strcat(name, "/LC_CTYPE");
115
116         if ((fp = fopen(name, "r")) == NULL)
117                 return (errno == 0 ? ENOENT : errno);
118
119         if ((rl = _Read_RuneMagi(fp)) == NULL) {
120                 saverr = (errno == 0 ? EFTYPE : errno);
121                 (void)fclose(fp);
122                 return (saverr);
123         }
124         (void)fclose(fp);
125
126         old__mbrtowc = __mbrtowc;
127         old__mbsinit = __mbsinit;
128         old__mbsnrtowcs = __mbsnrtowcs;
129         old__wcrtomb = __wcrtomb;
130         old__wcsnrtombs = __wcsnrtombs;
131
132         __mbrtowc = NULL;
133         __mbsinit = NULL;
134         __mbsnrtowcs = __mbsnrtowcs_std;
135         __wcrtomb = NULL;
136         __wcsnrtombs = __wcsnrtombs_std;
137
138         rl->__sputrune = NULL;
139         rl->__sgetrune = NULL;
140         if (strcmp(rl->__encoding, "NONE") == 0)
141                 ret = _none_init(rl);
142         else if (strcmp(rl->__encoding, "UTF-8") == 0)
143                 ret = _UTF8_init(rl);
144         else if (strcmp(rl->__encoding, "EUC") == 0)
145                 ret = _EUC_init(rl);
146         else if (strcmp(rl->__encoding, "GB18030") == 0)
147                 ret = _GB18030_init(rl);
148         else if (strcmp(rl->__encoding, "GB2312") == 0)
149                 ret = _GB2312_init(rl);
150         else if (strcmp(rl->__encoding, "GBK") == 0)
151                 ret = _GBK_init(rl);
152         else if (strcmp(rl->__encoding, "BIG5") == 0)
153                 ret = _BIG5_init(rl);
154         else if (strcmp(rl->__encoding, "MSKanji") == 0)
155                 ret = _MSKanji_init(rl);
156         else
157                 ret = EFTYPE;
158
159         if (ret == 0) {
160                 if (CachedRuneLocale != NULL) {
161                         /* See euc.c */
162                         if (strcmp(CachedRuneLocale->__encoding, "EUC") == 0)
163                                 free(CachedRuneLocale->__variable);
164                         free(CachedRuneLocale);
165                 }
166                 CachedRuneLocale = _CurrentRuneLocale;
167                 Cached__mb_cur_max = __mb_cur_max;
168                 Cached__mbrtowc = __mbrtowc;
169                 Cached__mbsinit = __mbsinit;
170                 Cached__mbsnrtowcs = __mbsnrtowcs;
171                 Cached__wcrtomb = __wcrtomb;
172                 Cached__wcsnrtombs = __wcsnrtombs;
173                 (void)strcpy(ctype_encoding, encoding);
174         } else {
175                 __mbrtowc = old__mbrtowc;
176                 __mbsinit = old__mbsinit;
177                 __mbsnrtowcs = old__mbsnrtowcs;
178                 __wcrtomb = old__wcrtomb;
179                 __wcsnrtombs = old__wcsnrtombs;
180                 free(rl);
181         }
182
183         return (ret);
184 }
185
186 int
187 __wrap_setrunelocale(const char *locale)
188 {
189         int ret = __setrunelocale(locale);
190
191         if (ret != 0) {
192                 errno = ret;
193                 return (_LDP_ERROR);
194         }
195         return (_LDP_LOADED);
196 }
197