xref: /titanic_50/usr/src/lib/libc/port/locale/setrunelocale.c (revision 6a37fc30652374065d6e4ab52366c499e5a34b66)
1 /*
2  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
4  * Copyright (c) 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Paul Borman at Krystal Technologies.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include "lint.h"
36 #include "file64.h"
37 #include <errno.h>
38 #include <limits.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <wchar.h>
44 #include "runetype.h"
45 #include "ldpart.h"
46 #include "mblocal.h"
47 #include "setlocale.h"
48 #include "_ctype.h"
49 #include "lctype.h"
50 #include "localeimpl.h"
51 
52 extern _RuneLocale	*_Read_RuneMagi(const char *);
53 
54 struct lc_ctype lc_ctype_posix = {
55 	.lc_mbrtowc = __mbrtowc_ascii,
56 	.lc_mbsinit = __mbsinit_ascii,
57 	.lc_mbsnrtowcs = __mbsnrtowcs_ascii,
58 	.lc_wcrtomb = __wcrtomb_ascii,
59 	.lc_wcsnrtombs = __wcsnrtombs_ascii,
60 	.lc_is_ascii = 1,
61 	.lc_max_mblen = 1,
62 	.lc_trans_upper = _DefaultRuneLocale.__mapupper,
63 	.lc_trans_lower = _DefaultRuneLocale.__maplower,
64 	.lc_ctype_mask = _DefaultRuneLocale.__runetype,
65 };
66 
67 struct locdata __posix_ctype_locdata = {
68 	.l_lname = "C",
69 	.l_data = { &lc_ctype_posix, &_DefaultRuneLocale }
70 };
71 
72 
73 /*
74  * Table of initializers for encodings.  When you add a new encoding type,
75  * this table should be updated.
76  */
77 static struct {
78 	const char *e_name;
79 	void (*e_init)(struct lc_ctype *);
80 } encodings[] = {
81 	{ "NONE", _none_init },
82 	{ "UTF-8",	_UTF8_init },
83 	{ "EUC-CN",	_EUC_CN_init },
84 	{ "EUC-JP",	_EUC_JP_init },
85 	{ "EUC-KR",	_EUC_KR_init },
86 	{ "EUC-TW",	_EUC_TW_init },
87 	{ "GB18030",	_GB18030_init },
88 	{ "GB2312",	_GB2312_init },
89 	{ "GBK",	_GBK_init },
90 	{ "BIG5",	_BIG5_init },
91 	{ "MSKanji",	_MSKanji_init },
92 	{ NULL,		NULL }
93 };
94 
95 
96 struct locdata *
97 __lc_ctype_load(const char *name)
98 {
99 	struct locdata *ldata;
100 	struct lc_ctype *lct;
101 	_RuneLocale *rl;
102 	int i;
103 	char path[PATH_MAX];
104 
105 	if ((ldata = __locdata_alloc(name, sizeof (*lct))) == NULL)
106 		return (NULL);
107 	lct = ldata->l_data[0];
108 	/*
109 	 * Slurp the locale file into the cache.
110 	 */
111 
112 	(void) snprintf(path, sizeof (path), "%s/%s/LC_CTYPE/LCL_DATA",
113 	    _PathLocale, name);
114 
115 	if ((rl = _Read_RuneMagi(path)) == NULL) {
116 		__locdata_free(ldata);
117 		errno = EINVAL;
118 		return (NULL);
119 	}
120 	ldata->l_data[1] = rl;
121 
122 	lct->lc_mbrtowc = NULL;
123 	lct->lc_mbsinit = NULL;
124 	lct->lc_mbsnrtowcs = NULL;
125 	lct->lc_wcrtomb = NULL;
126 	lct->lc_wcsnrtombs = NULL;
127 	lct->lc_ctype_mask = rl->__runetype;
128 	lct->lc_trans_upper = rl->__mapupper;
129 	lct->lc_trans_lower = rl->__maplower;
130 
131 	/* set up the function pointers */
132 	for (i = 0; encodings[i].e_name != NULL; i++) {
133 		int l = strlen(encodings[i].e_name);
134 		if ((strncmp(rl->__encoding, encodings[i].e_name, l) == 0) &&
135 		    (rl->__encoding[l] == '\0' || rl->__encoding[l] == '@')) {
136 			encodings[i].e_init(lct);
137 			break;
138 		}
139 	}
140 	if (encodings[i].e_name == NULL) {
141 		__locdata_free(ldata);
142 		errno = EINVAL;
143 		return (NULL);
144 	}
145 
146 
147 	return (ldata);
148 }
149