setrunelocale.c revision 244091
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 * Copyright (c) 2011 The FreeBSD Foundation
9 * All rights reserved.
10 * Portions of this software were developed by David Chisnall
11 * under sponsorship from the FreeBSD Foundation.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/lib/libc/locale/setrunelocale.c 244091 2012-12-10 17:34:33Z brooks $");
40
41#define __RUNETYPE_INTERNAL 1
42
43#include <runetype.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#undef _CurrentRuneLocale
56extern _RuneLocale const *_CurrentRuneLocale;
57#ifndef __NO_TLS
58/*
59 * A cached version of the runes for this thread.  Used by ctype.h
60 */
61_Thread_local const _RuneLocale *_ThreadRuneLocale;
62#endif
63
64extern int __mb_sb_limit;
65
66extern _RuneLocale	*_Read_RuneMagi(FILE *);
67
68static int		__setrunelocale(struct xlocale_ctype *l, const char *);
69
70#define __collate_substitute_nontrivial (table->__collate_substitute_nontrivial)
71#define __collate_substitute_table_ptr (table->__collate_substitute_table_ptr)
72#define __collate_char_pri_table_ptr (table->__collate_char_pri_table_ptr)
73#define __collate_chain_pri_table (table->__collate_chain_pri_table)
74
75
76static void
77destruct_ctype(void *v)
78{
79	struct xlocale_ctype *l = v;
80
81	if (strcmp(l->runes->__encoding, "EUC") == 0)
82		free(l->runes->__variable);
83	if (&_DefaultRuneLocale != l->runes)
84		free(l->runes);
85	free(l);
86}
87
88const _RuneLocale *
89__getCurrentRuneLocale(void)
90{
91
92	return XLOCALE_CTYPE(__get_locale())->runes;
93}
94
95static void
96free_runes(_RuneLocale *rl)
97{
98
99	/* FIXME: The "EUC" check here is a hideous abstraction violation. */
100	if ((rl != &_DefaultRuneLocale) && (rl)) {
101		if (strcmp(rl->__encoding, "EUC") == 0) {
102			free(rl->__variable);
103		}
104		free(rl);
105	}
106}
107
108static int
109__setrunelocale(struct xlocale_ctype *l, const char *encoding)
110{
111	FILE *fp;
112	char name[PATH_MAX];
113	_RuneLocale *rl;
114	int saverr, ret;
115	struct xlocale_ctype saved = *l;
116
117	/*
118	 * The "C" and "POSIX" locale are always here.
119	 */
120	if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
121		free_runes(saved.runes);
122		(void) _none_init(l, (_RuneLocale*)&_DefaultRuneLocale);
123		return (0);
124	}
125
126	/* Range checking not needed, encoding length already checked before */
127	(void) strcpy(name, _PathLocale);
128	(void) strcat(name, "/");
129	(void) strcat(name, encoding);
130	(void) strcat(name, "/LC_CTYPE");
131
132	if ((fp = fopen(name, "r")) == NULL)
133		return (errno == 0 ? ENOENT : errno);
134
135	if ((rl = _Read_RuneMagi(fp)) == NULL) {
136		saverr = (errno == 0 ? EFTYPE : errno);
137		(void)fclose(fp);
138		return (saverr);
139	}
140	(void)fclose(fp);
141
142	l->__mbrtowc = NULL;
143	l->__mbsinit = NULL;
144	l->__mbsnrtowcs = __mbsnrtowcs_std;
145	l->__wcrtomb = NULL;
146	l->__wcsnrtombs = __wcsnrtombs_std;
147
148	rl->__sputrune = NULL;
149	rl->__sgetrune = NULL;
150	if (strcmp(rl->__encoding, "NONE") == 0)
151		ret = _none_init(l, rl);
152	else if (strcmp(rl->__encoding, "ASCII") == 0)
153		ret = _ascii_init(l, rl);
154	else if (strcmp(rl->__encoding, "UTF-8") == 0)
155		ret = _UTF8_init(l, rl);
156	else if (strcmp(rl->__encoding, "EUC") == 0)
157		ret = _EUC_init(l, rl);
158	else if (strcmp(rl->__encoding, "GB18030") == 0)
159 		ret = _GB18030_init(l, rl);
160	else if (strcmp(rl->__encoding, "GB2312") == 0)
161		ret = _GB2312_init(l, rl);
162	else if (strcmp(rl->__encoding, "GBK") == 0)
163		ret = _GBK_init(l, rl);
164	else if (strcmp(rl->__encoding, "BIG5") == 0)
165		ret = _BIG5_init(l, rl);
166	else if (strcmp(rl->__encoding, "MSKanji") == 0)
167		ret = _MSKanji_init(l, rl);
168	else
169		ret = EFTYPE;
170
171	if (ret == 0) {
172		/* Free the old runes if it exists. */
173		free_runes(saved.runes);
174	} else {
175		/* Restore the saved version if this failed. */
176		memcpy(l, &saved, sizeof(struct xlocale_ctype));
177		free(rl);
178	}
179
180	return (ret);
181}
182
183int
184__wrap_setrunelocale(const char *locale)
185{
186	int ret = __setrunelocale(&__xlocale_global_ctype, locale);
187
188	if (ret != 0) {
189		errno = ret;
190		return (_LDP_ERROR);
191	}
192	__mb_cur_max = __xlocale_global_ctype.__mb_cur_max;
193	__mb_sb_limit = __xlocale_global_ctype.__mb_sb_limit;
194	_CurrentRuneLocale = __xlocale_global_ctype.runes;
195	return (_LDP_LOADED);
196}
197
198#ifndef __NO_TLS
199void
200__set_thread_rune_locale(locale_t loc)
201{
202
203	if (loc == NULL) {
204		_ThreadRuneLocale = &_DefaultRuneLocale;
205	} else {
206		_ThreadRuneLocale = XLOCALE_CTYPE(loc)->runes;
207	}
208}
209#endif
210
211void *
212__ctype_load(const char *locale, locale_t unused)
213{
214	struct xlocale_ctype *l = calloc(sizeof(struct xlocale_ctype), 1);
215
216	l->header.header.destructor = destruct_ctype;
217	if (__setrunelocale(l, locale))
218	{
219		free(l);
220		return NULL;
221	}
222	return l;
223}
224