setrunelocale.c revision 122145
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 122145 2003-11-05 22:52:51Z davidxu $"); 4292986Sobrien 4311695Sache#include <rune.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" 5222330Sache#include "setlocale.h" 5311695Sache 5492905Sobrienextern int _none_init(_RuneLocale *); 5592905Sobrienextern int _UTF2_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 65121845Stjrextern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict, 66121845Stjr size_t, mbstate_t * __restrict); 67121845Stjrextern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict); 68121845Stjrextern size_t __emulated_mbrtowc(wchar_t * __restrict, const char * __restrict, 69121845Stjr size_t, mbstate_t * __restrict ps); 70121845Stjrextern size_t __emulated_wcrtomb(char * __restrict, wchar_t, 71121845Stjr mbstate_t * __restrict ps); 72121845Stjrextern rune_t __emulated_sgetrune(const char *, size_t, const char **); 73121845Stjrextern int __emulated_sputrune(rune_t, char *, size_t, char **); 74121845Stjr 75117274Sachestatic int __setrunelocale(const char *); 76117270Sache 77117270Sache__warn_references(setrunelocale, "warning: setrunelocale() is deprecated. See setrunelocale(3)."); 7811695Sacheint 79101498Sachesetrunelocale(char *encoding) 8011695Sache{ 81117270Sache int ret; 82117270Sache 83117270Sache if (!encoding || !*encoding || strlen(encoding) > ENCODING_LEN || 84117270Sache (encoding[0] == '.' && 85117270Sache (encoding[1] == '\0' || 86117270Sache (encoding[1] == '.' && encoding[2] == '\0'))) || 87117270Sache strchr(encoding, '/') != NULL) 88117270Sache return (EINVAL); 89117270Sache 90117270Sache ret = __detect_path_locale(); 91117270Sache if (ret != 0) 92117270Sache return (ret); 93117270Sache 94117274Sache return (__setrunelocale((const char *)encoding)); 95117270Sache} 96117270Sache 97117270Sachestatic int 98117274Sache__setrunelocale(const char *encoding) 99117270Sache{ 10011695Sache FILE *fp; 10111695Sache char name[PATH_MAX]; 10211695Sache _RuneLocale *rl; 103101498Sache int saverr, ret; 104101498Sache static char ctype_encoding[ENCODING_LEN + 1]; 105101498Sache static _RuneLocale *CachedRuneLocale; 106101498Sache static int Cached__mb_cur_max; 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; 114101263Sache return (0); 11511695Sache } 11611695Sache 117101498Sache /* 118101498Sache * If the locale name is the same as our cache, use the cache. 119101498Sache */ 120101498Sache if (CachedRuneLocale != NULL && 121101498Sache strcmp(encoding, ctype_encoding) == 0) { 122101498Sache _CurrentRuneLocale = CachedRuneLocale; 123101498Sache __mb_cur_max = Cached__mb_cur_max; 124101498Sache return (0); 125101498Sache } 126101498Sache 127101498Sache /* 128101498Sache * Slurp the locale file into the cache. 129101498Sache */ 13024694Sache 131117270Sache /* Range checking not needed, encoding length already checked before */ 13220810Sjoerg (void) strcpy(name, _PathLocale); 13320810Sjoerg (void) strcat(name, "/"); 13420810Sjoerg (void) strcat(name, encoding); 13520810Sjoerg (void) strcat(name, "/LC_CTYPE"); 13611695Sache 13711695Sache if ((fp = fopen(name, "r")) == NULL) 138101566Sache return (errno == 0 ? ENOENT : errno); 13911695Sache 140101498Sache if ((rl = _Read_RuneMagi(fp)) == NULL) { 141101566Sache saverr = (errno == 0 ? EFTYPE : errno); 142101498Sache (void)fclose(fp); 143101498Sache return (saverr); 14411695Sache } 145101498Sache (void)fclose(fp); 14611695Sache 147121845Stjr __mbrtowc = __emulated_mbrtowc; 148121845Stjr __wcrtomb = __emulated_wcrtomb; 149121845Stjr rl->sputrune = __emulated_sputrune; 150121845Stjr rl->sgetrune = __emulated_sgetrune; 151101498Sache if (strcmp(rl->encoding, "NONE") == 0) 152101498Sache ret = _none_init(rl); 153101498Sache else if (strcmp(rl->encoding, "UTF2") == 0) 154101498Sache ret = _UTF2_init(rl); 155104828Stjr else if (strcmp(rl->encoding, "UTF-8") == 0) 156104828Stjr ret = _UTF8_init(rl); 157101498Sache else if (strcmp(rl->encoding, "EUC") == 0) 158101498Sache ret = _EUC_init(rl); 159118146Sache else if (strcmp(rl->encoding, "GB18030") == 0) 160118146Sache ret = _GB18030_init(rl); 161122145Sdavidxu else if (strcmp(rl->encoding, "GB2312") == 0) 162122145Sdavidxu ret = _GB2312_init(rl); 163115626Sache else if (strcmp(rl->encoding, "GBK") == 0) 164115626Sache ret = _GBK_init(rl); 165101498Sache else if (strcmp(rl->encoding, "BIG5") == 0) 166101498Sache ret = _BIG5_init(rl); 167101498Sache else if (strcmp(rl->encoding, "MSKanji") == 0) 168101498Sache ret = _MSKanji_init(rl); 16929857Sache else 170101498Sache ret = EFTYPE; 171101498Sache if (ret == 0) { 172101498Sache if (CachedRuneLocale != NULL) { 173101498Sache /* See euc.c */ 174101498Sache if (strcmp(CachedRuneLocale->encoding, "EUC") == 0) 175101498Sache free(CachedRuneLocale->variable); 176101498Sache free(CachedRuneLocale); 177101498Sache } 178101498Sache CachedRuneLocale = _CurrentRuneLocale; 179101498Sache Cached__mb_cur_max = __mb_cur_max; 180101498Sache (void)strcpy(ctype_encoding, encoding); 181101498Sache } else 182101498Sache free(rl); 183101498Sache 184101498Sache return (ret); 18511695Sache} 18611695Sache 187117270Sacheint 188117270Sache__wrap_setrunelocale(const char *locale) 189117270Sache{ 190117274Sache int ret = __setrunelocale(locale); 191117270Sache 192117270Sache if (ret != 0) { 193117270Sache errno = ret; 194117270Sache return (_LDP_ERROR); 195117270Sache } 196117270Sache return (_LDP_LOADED); 197117270Sache} 198117270Sache 199