xref: /freebsd/lib/libc/locale/setrunelocale.c (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 /* setrunelocale() is obsolete in FreeBSD 6 -- use ANSI functions instead. */
38 #define	OBSOLETE_IN_6
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include <rune.h>
44 #include <errno.h>
45 #include <limits.h>
46 #include <string.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <wchar.h>
51 #include "ldpart.h"
52 #include "mblocal.h"
53 #include "setlocale.h"
54 
55 extern int		_none_init(_RuneLocale *);
56 extern int		_UTF2_init(_RuneLocale *);
57 extern int		_UTF8_init(_RuneLocale *);
58 extern int		_EUC_init(_RuneLocale *);
59 extern int		_GB18030_init(_RuneLocale *);
60 extern int		_GB2312_init(_RuneLocale *);
61 extern int		_GBK_init(_RuneLocale *);
62 extern int		_BIG5_init(_RuneLocale *);
63 extern int		_MSKanji_init(_RuneLocale *);
64 extern _RuneLocale	*_Read_RuneMagi(FILE *);
65 
66 static int		__setrunelocale(const char *);
67 
68 __warn_references(setrunelocale, "warning: setrunelocale() is deprecated. See setrunelocale(3).");
69 int
70 setrunelocale(char *encoding)
71 {
72 	int ret;
73 
74 	if (!encoding || !*encoding || strlen(encoding) > ENCODING_LEN ||
75 	    (encoding[0] == '.' &&
76 	     (encoding[1] == '\0' ||
77 	      (encoding[1] == '.' && encoding[2] == '\0'))) ||
78 	    strchr(encoding, '/') != NULL)
79 		return (EINVAL);
80 
81 	ret = __detect_path_locale();
82 	if (ret != 0)
83 		return (ret);
84 
85 	return (__setrunelocale((const char *)encoding));
86 }
87 
88 static int
89 __setrunelocale(const char *encoding)
90 {
91 	FILE *fp;
92 	char name[PATH_MAX];
93 	_RuneLocale *rl;
94 	int saverr, ret;
95 	static char ctype_encoding[ENCODING_LEN + 1];
96 	static _RuneLocale *CachedRuneLocale;
97 	static int Cached__mb_cur_max;
98 	static size_t (*Cached__mbrtowc)(wchar_t * __restrict,
99 	    const char * __restrict, size_t, mbstate_t * __restrict);
100 	static size_t (*Cached__wcrtomb)(char * __restrict, wchar_t,
101 	    mbstate_t * __restrict);
102 	static int (*Cached__mbsinit)(const mbstate_t *);
103 	static size_t (*Cached__mbsrtowcs)(wchar_t * __restrict,
104 	    const char ** __restrict, size_t, mbstate_t * __restrict);
105 	static size_t (*Cached__wcsrtombs)(char * __restrict,
106 	    const wchar_t ** __restrict, size_t, mbstate_t * __restrict);
107 
108 	/*
109 	 * The "C" and "POSIX" locale are always here.
110 	 */
111 	if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
112 		_CurrentRuneLocale = &_DefaultRuneLocale;
113 		__mb_cur_max = 1;
114 		__mbrtowc = _none_mbrtowc;
115 		__mbsinit = _none_mbsinit;
116 		__mbsrtowcs = _none_mbsrtowcs;
117 		__wcrtomb = _none_wcrtomb;
118 		__wcsrtombs = _none_wcsrtombs;
119 		return (0);
120 	}
121 
122 	/*
123 	 * If the locale name is the same as our cache, use the cache.
124 	 */
125 	if (CachedRuneLocale != NULL &&
126 	    strcmp(encoding, ctype_encoding) == 0) {
127 		_CurrentRuneLocale = CachedRuneLocale;
128 		__mb_cur_max = Cached__mb_cur_max;
129 		__mbrtowc = Cached__mbrtowc;
130 		__mbsinit = Cached__mbsinit;
131 		__mbsrtowcs = Cached__mbsrtowcs;
132 		__wcrtomb = Cached__wcrtomb;
133 		__wcsrtombs = Cached__wcsrtombs;
134 		return (0);
135 	}
136 
137 	/*
138 	 * Slurp the locale file into the cache.
139 	 */
140 
141 	/* Range checking not needed, encoding length already checked before */
142 	(void) strcpy(name, _PathLocale);
143 	(void) strcat(name, "/");
144 	(void) strcat(name, encoding);
145 	(void) strcat(name, "/LC_CTYPE");
146 
147 	if ((fp = fopen(name, "r")) == NULL)
148 		return (errno == 0 ? ENOENT : errno);
149 
150 	if ((rl = _Read_RuneMagi(fp)) == NULL) {
151 		saverr = (errno == 0 ? EFTYPE : errno);
152 		(void)fclose(fp);
153 		return (saverr);
154 	}
155 	(void)fclose(fp);
156 
157 	__mbrtowc = NULL;
158 	__mbsinit = NULL;
159 	__mbsrtowcs = __mbsrtowcs_std;
160 	__wcrtomb = NULL;
161 	__wcsrtombs = __wcsrtombs_std;
162 	rl->__sputrune = __emulated_sputrune;
163 	rl->__sgetrune = __emulated_sgetrune;
164 	if (strcmp(rl->__encoding, "NONE") == 0)
165 		ret = _none_init(rl);
166 	else if (strcmp(rl->__encoding, "UTF2") == 0)
167 		ret = _UTF2_init(rl);
168 	else if (strcmp(rl->__encoding, "UTF-8") == 0)
169 		ret = _UTF8_init(rl);
170 	else if (strcmp(rl->__encoding, "EUC") == 0)
171 		ret = _EUC_init(rl);
172  	else if (strcmp(rl->__encoding, "GB18030") == 0)
173  		ret = _GB18030_init(rl);
174 	else if (strcmp(rl->__encoding, "GB2312") == 0)
175 		ret = _GB2312_init(rl);
176 	else if (strcmp(rl->__encoding, "GBK") == 0)
177 		ret = _GBK_init(rl);
178 	else if (strcmp(rl->__encoding, "BIG5") == 0)
179 		ret = _BIG5_init(rl);
180 	else if (strcmp(rl->__encoding, "MSKanji") == 0)
181 		ret = _MSKanji_init(rl);
182 	else
183 		ret = EFTYPE;
184 	if (ret == 0) {
185 		if (CachedRuneLocale != NULL) {
186 			/* See euc.c */
187 			if (strcmp(CachedRuneLocale->__encoding, "EUC") == 0)
188 				free(CachedRuneLocale->__variable);
189 			free(CachedRuneLocale);
190 		}
191 		CachedRuneLocale = _CurrentRuneLocale;
192 		Cached__mb_cur_max = __mb_cur_max;
193 		Cached__mbrtowc = __mbrtowc;
194 		Cached__mbsinit = __mbsinit;
195 		Cached__mbsrtowcs = __mbsrtowcs;
196 		Cached__wcrtomb = __wcrtomb;
197 		Cached__wcsrtombs = __wcsrtombs;
198 		(void)strcpy(ctype_encoding, encoding);
199 	} else
200 		free(rl);
201 
202 	return (ret);
203 }
204 
205 int
206 __wrap_setrunelocale(const char *locale)
207 {
208 	int ret = __setrunelocale(locale);
209 
210 	if (ret != 0) {
211 		errno = ret;
212 		return (_LDP_ERROR);
213 	}
214 	return (_LDP_LOADED);
215 }
216 
217