setrunelocale.c revision 290494
1255670Sdes/*-
298937Sdes * Copyright (c) 1993
398937Sdes *	The Regents of the University of California.  All rights reserved.
498937Sdes *
598937Sdes * This code is derived from software contributed to Berkeley by
6124208Sdes * Paul Borman at Krystal Technologies.
7124208Sdes *
8124208Sdes * Copyright (c) 2011 The FreeBSD Foundation
9124208Sdes * All rights reserved.
10124208Sdes * Portions of this software were developed by David Chisnall
11124208Sdes * under sponsorship from the FreeBSD Foundation.
12124208Sdes *
1398937Sdes * Redistribution and use in source and binary forms, with or without
1498937Sdes * modification, are permitted provided that the following conditions
1598937Sdes * are met:
1698937Sdes * 1. Redistributions of source code must retain the above copyright
1798937Sdes *    notice, this list of conditions and the following disclaimer.
1898937Sdes * 2. Redistributions in binary form must reproduce the above copyright
19204861Sdes *    notice, this list of conditions and the following disclaimer in the
20204861Sdes *    documentation and/or other materials provided with the distribution.
21124208Sdes * 4. Neither the name of the University nor the names of its contributors
2298937Sdes *    may be used to endorse or promote products derived from this software
2398937Sdes *    without specific prior written permission.
2498937Sdes *
25124208Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26124208Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27124208Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2898937Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2998937Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3098937Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31124208Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32124208Sdes * 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 290494 2015-11-07 12:43:35Z bapt $");
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(const char *);
67
68static int		__setrunelocale(struct xlocale_ctype *l, const char *);
69
70static void
71destruct_ctype(void *v)
72{
73	struct xlocale_ctype *l = v;
74
75	if (&_DefaultRuneLocale != l->runes)
76		free(l->runes);
77	free(l);
78}
79
80const _RuneLocale *
81__getCurrentRuneLocale(void)
82{
83
84	return XLOCALE_CTYPE(__get_locale())->runes;
85}
86
87static void
88free_runes(_RuneLocale *rl)
89{
90	if ((rl != &_DefaultRuneLocale) && (rl)) {
91		free(rl);
92	}
93}
94
95static int
96__setrunelocale(struct xlocale_ctype *l, const char *encoding)
97{
98	_RuneLocale *rl;
99	int ret;
100	char *path;
101	struct xlocale_ctype saved = *l;
102
103	/*
104	 * The "C" and "POSIX" locale are always here.
105	 */
106	if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
107		free_runes(saved.runes);
108		(void) _none_init(l, (_RuneLocale*)&_DefaultRuneLocale);
109		return (0);
110	}
111
112	/* Range checking not needed, encoding length already checked before */
113	asprintf(&path, "%s/%s/LC_CTYPE", _PathLocale, encoding);
114	if (path == NULL)
115		return (0);
116
117	if ((rl = _Read_RuneMagi(path)) == NULL) {
118		free(path);
119		errno = EINVAL;
120		return (errno);
121	}
122	free(path);
123
124	l->__mbrtowc = NULL;
125	l->__mbsinit = NULL;
126	l->__mbsnrtowcs = NULL;
127	l->__wcrtomb = NULL;
128	l->__wcsnrtombs = NULL;
129
130	rl->__sputrune = NULL;
131	rl->__sgetrune = NULL;
132	if (strncmp(rl->__encoding, "NONE", 4) == 0)
133		ret = _none_init(l, rl);
134	else if (strcmp(rl->__encoding, "UTF-8") == 0)
135		ret = _UTF8_init(l, rl);
136	else if (strcmp(rl->__encoding, "EUC-CN") == 0)
137		ret = _EUC_CN_init(l, rl);
138	else if (strcmp(rl->__encoding, "EUC-JP") == 0)
139		ret = _EUC_JP_init(l, rl);
140	else if (strcmp(rl->__encoding, "EUC-KR") == 0)
141		ret = _EUC_KR_init(l, rl);
142	else if (strcmp(rl->__encoding, "EUC-TW") == 0)
143		ret = _EUC_TW_init(l, rl);
144	else if (strcmp(rl->__encoding, "GB18030") == 0)
145 		ret = _GB18030_init(l, rl);
146	else if (strcmp(rl->__encoding, "GB2312") == 0)
147		ret = _GB2312_init(l, rl);
148	else if (strcmp(rl->__encoding, "GBK") == 0)
149		ret = _GBK_init(l, rl);
150	else if (strcmp(rl->__encoding, "BIG5") == 0)
151		ret = _BIG5_init(l, rl);
152	else if (strcmp(rl->__encoding, "MSKanji") == 0)
153		ret = _MSKanji_init(l, rl);
154	else
155		ret = EFTYPE;
156
157	if (ret == 0) {
158		/* Free the old runes if it exists. */
159		free_runes(saved.runes);
160	} else {
161		/* Restore the saved version if this failed. */
162		memcpy(l, &saved, sizeof(struct xlocale_ctype));
163		free(rl);
164	}
165
166	return (ret);
167}
168
169int
170__wrap_setrunelocale(const char *locale)
171{
172	int ret = __setrunelocale(&__xlocale_global_ctype, locale);
173
174	if (ret != 0) {
175		errno = ret;
176		return (_LDP_ERROR);
177	}
178	__mb_cur_max = __xlocale_global_ctype.__mb_cur_max;
179	__mb_sb_limit = __xlocale_global_ctype.__mb_sb_limit;
180	_CurrentRuneLocale = __xlocale_global_ctype.runes;
181	return (_LDP_LOADED);
182}
183
184#ifndef __NO_TLS
185void
186__set_thread_rune_locale(locale_t loc)
187{
188
189	if (loc == NULL) {
190		_ThreadRuneLocale = &_DefaultRuneLocale;
191	} else if (loc == LC_GLOBAL_LOCALE) {
192		_ThreadRuneLocale = 0;
193	} else {
194		_ThreadRuneLocale = XLOCALE_CTYPE(loc)->runes;
195	}
196}
197#endif
198
199void *
200__ctype_load(const char *locale, locale_t unused __unused)
201{
202	struct xlocale_ctype *l = calloc(sizeof(struct xlocale_ctype), 1);
203
204	l->header.header.destructor = destruct_ctype;
205	if (__setrunelocale(l, locale))
206	{
207		free(l);
208		return NULL;
209	}
210	return l;
211}
212