setrunelocale.c revision 136609
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 *
811695Sache * Redistribution and use in source and binary forms, with or without
911695Sache * modification, are permitted provided that the following conditions
1011695Sache * are met:
1111695Sache * 1. Redistributions of source code must retain the above copyright
1211695Sache *    notice, this list of conditions and the following disclaimer.
1311695Sache * 2. Redistributions in binary form must reproduce the above copyright
1411695Sache *    notice, this list of conditions and the following disclaimer in the
1511695Sache *    documentation and/or other materials provided with the distribution.
1611695Sache * 3. All advertising materials mentioning features or use of this software
1711695Sache *    must display the following acknowledgement:
1811695Sache *	This product includes software developed by the University of
1911695Sache *	California, Berkeley and its contributors.
2011695Sache * 4. Neither the name of the University nor the names of its contributors
2111695Sache *    may be used to endorse or promote products derived from this software
2211695Sache *    without specific prior written permission.
2311695Sache *
2411695Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2511695Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2611695Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2711695Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2811695Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2911695Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3011695Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3111695Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3211695Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3311695Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3411695Sache * SUCH DAMAGE.
3511695Sache */
3611695Sache
37117270Sache/* setrunelocale() is obsolete in FreeBSD 6 -- use ANSI functions instead. */
38117270Sache#define	OBSOLETE_IN_6
39117270Sache
4092986Sobrien#include <sys/cdefs.h>
4192986Sobrien__FBSDID("$FreeBSD: head/lib/libc/locale/setrunelocale.c 136609 2004-10-17 06:51:50Z tjr $");
4292986Sobrien
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
5592905Sobrienextern int		_none_init(_RuneLocale *);
56104828Stjrextern int		_UTF8_init(_RuneLocale *);
5792905Sobrienextern int		_EUC_init(_RuneLocale *);
58118146Sacheextern int		_GB18030_init(_RuneLocale *);
59122145Sdavidxuextern int		_GB2312_init(_RuneLocale *);
60115626Sacheextern int		_GBK_init(_RuneLocale *);
6192905Sobrienextern int		_BIG5_init(_RuneLocale *);
6292905Sobrienextern int		_MSKanji_init(_RuneLocale *);
63115626Sacheextern _RuneLocale	*_Read_RuneMagi(FILE *);
6411695Sache
65117274Sachestatic int		__setrunelocale(const char *);
66117270Sache
67134104Stjr__warn_references(setrunelocale, "warning: setrunelocale() is obsolete. See setrunelocale(3).");
6811695Sacheint
69101498Sachesetrunelocale(char *encoding)
7011695Sache{
71117270Sache	int ret;
72117270Sache
73117270Sache	if (!encoding || !*encoding || strlen(encoding) > ENCODING_LEN ||
74117270Sache	    (encoding[0] == '.' &&
75117270Sache	     (encoding[1] == '\0' ||
76117270Sache	      (encoding[1] == '.' && encoding[2] == '\0'))) ||
77117270Sache	    strchr(encoding, '/') != NULL)
78117270Sache		return (EINVAL);
79117270Sache
80117270Sache	ret = __detect_path_locale();
81117270Sache	if (ret != 0)
82117270Sache		return (ret);
83117270Sache
84117274Sache	return (__setrunelocale((const char *)encoding));
85117270Sache}
86117270Sache
87117270Sachestatic int
88117274Sache__setrunelocale(const char *encoding)
89117270Sache{
9011695Sache	FILE *fp;
9111695Sache	char name[PATH_MAX];
9211695Sache	_RuneLocale *rl;
93101498Sache	int saverr, ret;
94101498Sache	static char ctype_encoding[ENCODING_LEN + 1];
95101498Sache	static _RuneLocale *CachedRuneLocale;
96101498Sache	static int Cached__mb_cur_max;
97123308Stjr	static size_t (*Cached__mbrtowc)(wchar_t * __restrict,
98123308Stjr	    const char * __restrict, size_t, mbstate_t * __restrict);
99123308Stjr	static size_t (*Cached__wcrtomb)(char * __restrict, wchar_t,
100123308Stjr	    mbstate_t * __restrict);
101128004Stjr	static int (*Cached__mbsinit)(const mbstate_t *);
102132497Stjr	static size_t (*Cached__mbsnrtowcs)(wchar_t * __restrict,
103132497Stjr	    const char ** __restrict, size_t, size_t, mbstate_t * __restrict);
104132497Stjr	static size_t (*Cached__wcsnrtombs)(char * __restrict,
105132497Stjr	    const wchar_t ** __restrict, size_t, size_t,
106132497Stjr	    mbstate_t * __restrict);
10711695Sache
10811695Sache	/*
10911695Sache	 * The "C" and "POSIX" locale are always here.
11011695Sache	 */
111101498Sache	if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
11211695Sache		_CurrentRuneLocale = &_DefaultRuneLocale;
113101488Sache		__mb_cur_max = 1;
114123308Stjr		__mbrtowc = _none_mbrtowc;
115129179Stjr		__mbsinit = _none_mbsinit;
116132497Stjr		__mbsnrtowcs = _none_mbsnrtowcs;
117123308Stjr		__wcrtomb = _none_wcrtomb;
118132497Stjr		__wcsnrtombs = _none_wcsnrtombs;
119101263Sache		return (0);
12011695Sache	}
12111695Sache
122101498Sache	/*
123101498Sache	 * If the locale name is the same as our cache, use the cache.
124101498Sache	 */
125101498Sache	if (CachedRuneLocale != NULL &&
126101498Sache	    strcmp(encoding, ctype_encoding) == 0) {
127101498Sache		_CurrentRuneLocale = CachedRuneLocale;
128101498Sache		__mb_cur_max = Cached__mb_cur_max;
129123308Stjr		__mbrtowc = Cached__mbrtowc;
130129179Stjr		__mbsinit = Cached__mbsinit;
131132497Stjr		__mbsnrtowcs = Cached__mbsnrtowcs;
132123308Stjr		__wcrtomb = Cached__wcrtomb;
133132497Stjr		__wcsnrtombs = Cached__wcsnrtombs;
134101498Sache		return (0);
135101498Sache	}
136101498Sache
137101498Sache	/*
138101498Sache	 * Slurp the locale file into the cache.
139101498Sache	 */
14024694Sache
141117270Sache	/* Range checking not needed, encoding length already checked before */
14220810Sjoerg	(void) strcpy(name, _PathLocale);
14320810Sjoerg	(void) strcat(name, "/");
14420810Sjoerg	(void) strcat(name, encoding);
14520810Sjoerg	(void) strcat(name, "/LC_CTYPE");
14611695Sache
14711695Sache	if ((fp = fopen(name, "r")) == NULL)
148101566Sache		return (errno == 0 ? ENOENT : errno);
14911695Sache
150101498Sache	if ((rl = _Read_RuneMagi(fp)) == NULL) {
151101566Sache		saverr = (errno == 0 ? EFTYPE : errno);
152101498Sache		(void)fclose(fp);
153101498Sache		return (saverr);
15411695Sache	}
155101498Sache	(void)fclose(fp);
15611695Sache
157127835Stjr	__mbrtowc = NULL;
158129179Stjr	__mbsinit = NULL;
159132497Stjr	__mbsnrtowcs = __mbsnrtowcs_std;
160127835Stjr	__wcrtomb = NULL;
161132497Stjr	__wcsnrtombs = __wcsnrtombs_std;
162136609Stjr	rl->__sputrune = NULL;
163136609Stjr	rl->__sgetrune = NULL;
164130961Stjr	if (strcmp(rl->__encoding, "NONE") == 0)
165101498Sache		ret = _none_init(rl);
166130961Stjr	else if (strcmp(rl->__encoding, "UTF-8") == 0)
167104828Stjr		ret = _UTF8_init(rl);
168130961Stjr	else if (strcmp(rl->__encoding, "EUC") == 0)
169101498Sache		ret = _EUC_init(rl);
170130961Stjr 	else if (strcmp(rl->__encoding, "GB18030") == 0)
171118146Sache 		ret = _GB18030_init(rl);
172130961Stjr	else if (strcmp(rl->__encoding, "GB2312") == 0)
173122145Sdavidxu		ret = _GB2312_init(rl);
174130961Stjr	else if (strcmp(rl->__encoding, "GBK") == 0)
175115626Sache		ret = _GBK_init(rl);
176130961Stjr	else if (strcmp(rl->__encoding, "BIG5") == 0)
177101498Sache		ret = _BIG5_init(rl);
178130961Stjr	else if (strcmp(rl->__encoding, "MSKanji") == 0)
179101498Sache		ret = _MSKanji_init(rl);
18029857Sache	else
181101498Sache		ret = EFTYPE;
182101498Sache	if (ret == 0) {
183101498Sache		if (CachedRuneLocale != NULL) {
184101498Sache			/* See euc.c */
185130961Stjr			if (strcmp(CachedRuneLocale->__encoding, "EUC") == 0)
186130961Stjr				free(CachedRuneLocale->__variable);
187101498Sache			free(CachedRuneLocale);
188101498Sache		}
189101498Sache		CachedRuneLocale = _CurrentRuneLocale;
190101498Sache		Cached__mb_cur_max = __mb_cur_max;
191123308Stjr		Cached__mbrtowc = __mbrtowc;
192128004Stjr		Cached__mbsinit = __mbsinit;
193132497Stjr		Cached__mbsnrtowcs = __mbsnrtowcs;
194123308Stjr		Cached__wcrtomb = __wcrtomb;
195132497Stjr		Cached__wcsnrtombs = __wcsnrtombs;
196101498Sache		(void)strcpy(ctype_encoding, encoding);
197101498Sache	} else
198101498Sache		free(rl);
199101498Sache
200101498Sache	return (ret);
20111695Sache}
20211695Sache
203117270Sacheint
204117270Sache__wrap_setrunelocale(const char *locale)
205117270Sache{
206117274Sache	int ret = __setrunelocale(locale);
207117270Sache
208117270Sache	if (ret != 0) {
209117270Sache		errno = ret;
210117270Sache		return (_LDP_ERROR);
211117270Sache	}
212117270Sache	return (_LDP_LOADED);
213117270Sache}
214117270Sache
215