xref: /freebsd/lib/libc/locale/setrunelocale.c (revision f0a75d274af375d15b97b830966b99a02b7db911)
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 	static char ctype_encoding[ENCODING_LEN + 1];
60 	static _RuneLocale *CachedRuneLocale;
61 	static int Cached__mb_cur_max;
62 	static size_t (*Cached__mbrtowc)(wchar_t * __restrict,
63 	    const char * __restrict, size_t, mbstate_t * __restrict);
64 	static size_t (*Cached__wcrtomb)(char * __restrict, wchar_t,
65 	    mbstate_t * __restrict);
66 	static int (*Cached__mbsinit)(const mbstate_t *);
67 	static size_t (*Cached__mbsnrtowcs)(wchar_t * __restrict,
68 	    const char ** __restrict, size_t, size_t, mbstate_t * __restrict);
69 	static size_t (*Cached__wcsnrtombs)(char * __restrict,
70 	    const wchar_t ** __restrict, size_t, size_t,
71 	    mbstate_t * __restrict);
72 
73 	/*
74 	 * The "C" and "POSIX" locale are always here.
75 	 */
76 	if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
77 		_none_init(&_DefaultRuneLocale);
78 		return (0);
79 	}
80 
81 	/*
82 	 * If the locale name is the same as our cache, use the cache.
83 	 */
84 	if (CachedRuneLocale != NULL &&
85 	    strcmp(encoding, ctype_encoding) == 0) {
86 		_CurrentRuneLocale = CachedRuneLocale;
87 		__mb_cur_max = Cached__mb_cur_max;
88 		__mbrtowc = Cached__mbrtowc;
89 		__mbsinit = Cached__mbsinit;
90 		__mbsnrtowcs = Cached__mbsnrtowcs;
91 		__wcrtomb = Cached__wcrtomb;
92 		__wcsnrtombs = Cached__wcsnrtombs;
93 		return (0);
94 	}
95 
96 	/*
97 	 * Slurp the locale file into the cache.
98 	 */
99 
100 	/* Range checking not needed, encoding length already checked before */
101 	(void) strcpy(name, _PathLocale);
102 	(void) strcat(name, "/");
103 	(void) strcat(name, encoding);
104 	(void) strcat(name, "/LC_CTYPE");
105 
106 	if ((fp = fopen(name, "r")) == NULL)
107 		return (errno == 0 ? ENOENT : errno);
108 
109 	if ((rl = _Read_RuneMagi(fp)) == NULL) {
110 		saverr = (errno == 0 ? EFTYPE : errno);
111 		(void)fclose(fp);
112 		return (saverr);
113 	}
114 	(void)fclose(fp);
115 
116 	__mbrtowc = NULL;
117 	__mbsinit = NULL;
118 	__mbsnrtowcs = __mbsnrtowcs_std;
119 	__wcrtomb = NULL;
120 	__wcsnrtombs = __wcsnrtombs_std;
121 	rl->__sputrune = NULL;
122 	rl->__sgetrune = NULL;
123 	if (strcmp(rl->__encoding, "NONE") == 0)
124 		ret = _none_init(rl);
125 	else if (strcmp(rl->__encoding, "UTF-8") == 0)
126 		ret = _UTF8_init(rl);
127 	else if (strcmp(rl->__encoding, "EUC") == 0)
128 		ret = _EUC_init(rl);
129  	else if (strcmp(rl->__encoding, "GB18030") == 0)
130  		ret = _GB18030_init(rl);
131 	else if (strcmp(rl->__encoding, "GB2312") == 0)
132 		ret = _GB2312_init(rl);
133 	else if (strcmp(rl->__encoding, "GBK") == 0)
134 		ret = _GBK_init(rl);
135 	else if (strcmp(rl->__encoding, "BIG5") == 0)
136 		ret = _BIG5_init(rl);
137 	else if (strcmp(rl->__encoding, "MSKanji") == 0)
138 		ret = _MSKanji_init(rl);
139 	else
140 		ret = EFTYPE;
141 	if (ret == 0) {
142 		if (CachedRuneLocale != NULL) {
143 			/* See euc.c */
144 			if (strcmp(CachedRuneLocale->__encoding, "EUC") == 0)
145 				free(CachedRuneLocale->__variable);
146 			free(CachedRuneLocale);
147 		}
148 		CachedRuneLocale = _CurrentRuneLocale;
149 		Cached__mb_cur_max = __mb_cur_max;
150 		Cached__mbrtowc = __mbrtowc;
151 		Cached__mbsinit = __mbsinit;
152 		Cached__mbsnrtowcs = __mbsnrtowcs;
153 		Cached__wcrtomb = __wcrtomb;
154 		Cached__wcsnrtombs = __wcsnrtombs;
155 		(void)strcpy(ctype_encoding, encoding);
156 	} else
157 		free(rl);
158 
159 	return (ret);
160 }
161 
162 int
163 __wrap_setrunelocale(const char *locale)
164 {
165 	int ret = __setrunelocale(locale);
166 
167 	if (ret != 0) {
168 		errno = ret;
169 		return (_LDP_ERROR);
170 	}
171 	return (_LDP_LOADED);
172 }
173 
174