Merge commit '281819e5f8b19cd8627541a22d261906fd190276' into merges
[unleashed.git] / usr / src / lib / libc / port / locale / setrunelocale.c
blobc4b9a801a2c20d72624687cf100ed7e8468898fc
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.
7 * This code is derived from software contributed to Berkeley by
8 * Paul Borman at Krystal Technologies.
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.
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.
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"
52 extern _RuneLocale *_Read_RuneMagi(const char *);
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,
67 struct locdata __posix_ctype_locdata = {
68 .l_lname = "C",
69 .l_data = { &lc_ctype_posix, &_DefaultRuneLocale }
74 * Table of initializers for encodings. When you add a new encoding type,
75 * this table should be updated. "NONE" is a special case and handled
76 * separately in __lc_ctype_load.
78 static struct {
79 const char *e_name;
80 void (*e_init)(struct lc_ctype *);
81 } encodings[] = {
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 }
96 struct locdata *
97 __lc_ctype_load(const char *name)
99 struct locdata *ldata;
100 struct lc_ctype *lct;
101 _RuneLocale *rl;
102 int i;
103 char path[PATH_MAX];
105 if ((ldata = __locdata_alloc(name, sizeof (*lct))) == NULL)
106 return (NULL);
107 lct = ldata->l_data[0];
109 * Slurp the locale file into the cache.
112 (void) snprintf(path, sizeof (path), "%s/%s/LC_CTYPE/LCL_DATA",
113 _PathLocale, name);
115 if ((rl = _Read_RuneMagi(path)) == NULL) {
116 __locdata_free(ldata);
117 errno = EINVAL;
118 return (NULL);
120 ldata->l_data[1] = rl;
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;
132 * Set up the function pointers. "NONE" is accepted either as is, or if
133 * followed by a colon, as a prefix.
135 if ((strcmp(rl->__encoding, "NONE") == 0) ||
136 (strncmp(rl->__encoding, "NONE:", 5) == 0)) {
137 _none_init(lct);
138 return (ldata);
140 for (i = 0; encodings[i].e_name != NULL; i++) {
141 if (strcmp(rl->__encoding, encodings[i].e_name) == 0) {
142 encodings[i].e_init(lct);
143 break;
146 if (encodings[i].e_name == NULL) {
147 __locdata_free(ldata);
148 errno = EINVAL;
149 return (NULL);
153 return (ldata);