setrunelocale.c revision 323548
111695Sache/*-
211695Sache * Copyright (c) 1993
311695Sache *	The Regents of the University of California.  All rights reserved.
411695Sache *
511695Sache * This code is derived from software contributed to Berkeley by
611695Sache * Paul Borman at Krystal Technologies.
711695Sache *
8227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
9227753Stheraven * All rights reserved.
10227753Stheraven * Portions of this software were developed by David Chisnall
11227753Stheraven * under sponsorship from the FreeBSD Foundation.
12227753Stheraven *
1311695Sache * Redistribution and use in source and binary forms, with or without
1411695Sache * modification, are permitted provided that the following conditions
1511695Sache * are met:
1611695Sache * 1. Redistributions of source code must retain the above copyright
1711695Sache *    notice, this list of conditions and the following disclaimer.
1811695Sache * 2. Redistributions in binary form must reproduce the above copyright
1911695Sache *    notice, this list of conditions and the following disclaimer in the
2011695Sache *    documentation and/or other materials provided with the distribution.
2111695Sache * 4. Neither the name of the University nor the names of its contributors
2211695Sache *    may be used to endorse or promote products derived from this software
2311695Sache *    without specific prior written permission.
2411695Sache *
2511695Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2611695Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2711695Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2811695Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2911695Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3011695Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3111695Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3211695Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3311695Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3411695Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3511695Sache * SUCH DAMAGE.
3611695Sache */
3711695Sache
3892986Sobrien#include <sys/cdefs.h>
3992986Sobrien__FBSDID("$FreeBSD: stable/11/lib/libc/locale/setrunelocale.c 323548 2017-09-13 16:21:11Z pfg $");
4092986Sobrien
41323548Spfg#define	__RUNETYPE_INTERNAL 1
42232498Stheraven
43136609Stjr#include <runetype.h>
4411695Sache#include <errno.h>
4511695Sache#include <limits.h>
4611695Sache#include <string.h>
4711695Sache#include <stdio.h>
4811695Sache#include <stdlib.h>
4924694Sache#include <unistd.h>
50121845Stjr#include <wchar.h>
51117270Sache#include "ldpart.h"
52129153Stjr#include "mblocal.h"
5322330Sache#include "setlocale.h"
5411695Sache
55232498Stheraven#undef _CurrentRuneLocale
56232498Stheravenextern _RuneLocale const *_CurrentRuneLocale;
57232498Stheraven#ifndef __NO_TLS
58232498Stheraven/*
59232498Stheraven * A cached version of the runes for this thread.  Used by ctype.h
60232498Stheraven */
61232498Stheraven_Thread_local const _RuneLocale *_ThreadRuneLocale;
62232498Stheraven#endif
63232498Stheraven
64172619Sacheextern int __mb_sb_limit;
65172619Sache
66290494Sbaptextern _RuneLocale	*_Read_RuneMagi(const char *);
6711695Sache
68227753Stheravenstatic int		__setrunelocale(struct xlocale_ctype *l, const char *);
69117270Sache
70244091Sbrooksstatic void
71244091Sbrooksdestruct_ctype(void *v)
72227753Stheraven{
73227753Stheraven	struct xlocale_ctype *l = v;
74244091Sbrooks
75323548Spfg	if (&_DefaultRuneLocale != l->runes)
76227753Stheraven		free(l->runes);
77227753Stheraven	free(l);
78227753Stheraven}
79232498Stheraven
80244091Sbrooksconst _RuneLocale *
81244091Sbrooks__getCurrentRuneLocale(void)
82227753Stheraven{
83244091Sbrooks
84323548Spfg	return (XLOCALE_CTYPE(__get_locale())->runes);
85227753Stheraven}
86227753Stheraven
87244091Sbrooksstatic void
88244091Sbrooksfree_runes(_RuneLocale *rl)
89236889Stheraven{
90236889Stheraven	if ((rl != &_DefaultRuneLocale) && (rl)) {
91236889Stheraven		free(rl);
92236889Stheraven	}
93236889Stheraven}
94236889Stheraven
95117270Sachestatic int
96227753Stheraven__setrunelocale(struct xlocale_ctype *l, const char *encoding)
97117270Sache{
9811695Sache	_RuneLocale *rl;
99290494Sbapt	int ret;
100290494Sbapt	char *path;
101227753Stheraven	struct xlocale_ctype saved = *l;
10211695Sache
10311695Sache	/*
10411695Sache	 * The "C" and "POSIX" locale are always here.
10511695Sache	 */
106101498Sache	if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
107236889Stheraven		free_runes(saved.runes);
108227753Stheraven		(void) _none_init(l, (_RuneLocale*)&_DefaultRuneLocale);
109101263Sache		return (0);
11011695Sache	}
11111695Sache
112117270Sache	/* Range checking not needed, encoding length already checked before */
113290494Sbapt	asprintf(&path, "%s/%s/LC_CTYPE", _PathLocale, encoding);
114290494Sbapt	if (path == NULL)
115290494Sbapt		return (0);
11611695Sache
117290494Sbapt	if ((rl = _Read_RuneMagi(path)) == NULL) {
118290494Sbapt		free(path);
119290494Sbapt		errno = EINVAL;
120290494Sbapt		return (errno);
12111695Sache	}
122290494Sbapt	free(path);
12311695Sache
124227753Stheraven	l->__mbrtowc = NULL;
125227753Stheraven	l->__mbsinit = NULL;
126290494Sbapt	l->__mbsnrtowcs = NULL;
127227753Stheraven	l->__wcrtomb = NULL;
128290494Sbapt	l->__wcsnrtombs = NULL;
129175586Sache
130136609Stjr	rl->__sputrune = NULL;
131136609Stjr	rl->__sgetrune = NULL;
132290618Sbapt	if (strcmp(rl->__encoding, "NONE:US-ASCII") == 0)
133290618Sbapt		ret = _ascii_init(l, rl);
134290618Sbapt	else if (strncmp(rl->__encoding, "NONE", 4) == 0)
135227753Stheraven		ret = _none_init(l, rl);
136175586Sache	else if (strcmp(rl->__encoding, "UTF-8") == 0)
137227753Stheraven		ret = _UTF8_init(l, rl);
138290494Sbapt	else if (strcmp(rl->__encoding, "EUC-CN") == 0)
139290494Sbapt		ret = _EUC_CN_init(l, rl);
140290494Sbapt	else if (strcmp(rl->__encoding, "EUC-JP") == 0)
141290494Sbapt		ret = _EUC_JP_init(l, rl);
142290494Sbapt	else if (strcmp(rl->__encoding, "EUC-KR") == 0)
143290494Sbapt		ret = _EUC_KR_init(l, rl);
144290494Sbapt	else if (strcmp(rl->__encoding, "EUC-TW") == 0)
145290494Sbapt		ret = _EUC_TW_init(l, rl);
146175586Sache	else if (strcmp(rl->__encoding, "GB18030") == 0)
147323548Spfg		ret = _GB18030_init(l, rl);
148175586Sache	else if (strcmp(rl->__encoding, "GB2312") == 0)
149227753Stheraven		ret = _GB2312_init(l, rl);
150175586Sache	else if (strcmp(rl->__encoding, "GBK") == 0)
151227753Stheraven		ret = _GBK_init(l, rl);
152175586Sache	else if (strcmp(rl->__encoding, "BIG5") == 0)
153227753Stheraven		ret = _BIG5_init(l, rl);
154175586Sache	else if (strcmp(rl->__encoding, "MSKanji") == 0)
155227753Stheraven		ret = _MSKanji_init(l, rl);
156175586Sache	else
157101498Sache		ret = EFTYPE;
158175585Sache
159101498Sache	if (ret == 0) {
160227753Stheraven		/* Free the old runes if it exists. */
161236889Stheraven		free_runes(saved.runes);
162175586Sache	} else {
163227753Stheraven		/* Restore the saved version if this failed. */
164227753Stheraven		memcpy(l, &saved, sizeof(struct xlocale_ctype));
165101498Sache		free(rl);
166175586Sache	}
167101498Sache
168101498Sache	return (ret);
16911695Sache}
17011695Sache
171117270Sacheint
172117270Sache__wrap_setrunelocale(const char *locale)
173117270Sache{
174227753Stheraven	int ret = __setrunelocale(&__xlocale_global_ctype, locale);
175117270Sache
176117270Sache	if (ret != 0) {
177117270Sache		errno = ret;
178117270Sache		return (_LDP_ERROR);
179117270Sache	}
180227753Stheraven	__mb_cur_max = __xlocale_global_ctype.__mb_cur_max;
181227753Stheraven	__mb_sb_limit = __xlocale_global_ctype.__mb_sb_limit;
182232498Stheraven	_CurrentRuneLocale = __xlocale_global_ctype.runes;
183117270Sache	return (_LDP_LOADED);
184117270Sache}
185232498Stheraven
186232498Stheraven#ifndef __NO_TLS
187232498Stheravenvoid
188244091Sbrooks__set_thread_rune_locale(locale_t loc)
189244091Sbrooks{
190232498Stheraven
191232498Stheraven	if (loc == NULL) {
192232498Stheraven		_ThreadRuneLocale = &_DefaultRuneLocale;
193264038Stheraven	} else if (loc == LC_GLOBAL_LOCALE) {
194264038Stheraven		_ThreadRuneLocale = 0;
195232498Stheraven	} else {
196232498Stheraven		_ThreadRuneLocale = XLOCALE_CTYPE(loc)->runes;
197232498Stheraven	}
198232498Stheraven}
199232498Stheraven#endif
200232498Stheraven
201232498Stheravenvoid *
202290494Sbapt__ctype_load(const char *locale, locale_t unused __unused)
203227753Stheraven{
204227753Stheraven	struct xlocale_ctype *l = calloc(sizeof(struct xlocale_ctype), 1);
205244091Sbrooks
206227753Stheraven	l->header.header.destructor = destruct_ctype;
207323548Spfg	if (__setrunelocale(l, locale)) {
208227753Stheraven		free(l);
209323548Spfg		return (NULL);
210227753Stheraven	}
211323548Spfg	return (l);
212227753Stheraven}
213