Complete Citrus import. Import message catalog implement from
[dragonfly.git] / lib / libc / locale / setrunelocale.c
1 /*      $NetBSD: src/lib/libc/locale/setrunelocale.c,v 1.14 2003/08/07 16:43:07 agc Exp $       */
2 /*      $DragonFly: src/lib/libc/locale/setrunelocale.c,v 1.5 2005/04/21 16:36:34 joerg Exp $ */
3
4 /*-
5  * Copyright (c)1999 Citrus Project,
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 /*-
31  * Copyright (c) 1998 The NetBSD Foundation, Inc.
32  * All rights reserved.
33  *
34  * This code is derived from software contributed to The NetBSD Foundation
35  * by Paul Kranenburg.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *        This product includes software developed by the NetBSD
48  *        Foundation, Inc. and its contributors.
49  * 4. Neither the name of The NetBSD Foundation nor the names of its
50  *    contributors may be used to endorse or promote products derived
51  *    from this software without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63  * POSSIBILITY OF SUCH DAMAGE.
64  */
65
66 /*-
67  * Copyright (c) 1993
68  *      The Regents of the University of California.  All rights reserved.
69  *
70  * This code is derived from software contributed to Berkeley by
71  * Paul Borman at Krystal Technologies.
72  *
73  * Redistribution and use in source and binary forms, with or without
74  * modification, are permitted provided that the following conditions
75  * are met:
76  * 1. Redistributions of source code must retain the above copyright
77  *    notice, this list of conditions and the following disclaimer.
78  * 2. Redistributions in binary form must reproduce the above copyright
79  *    notice, this list of conditions and the following disclaimer in the
80  *    documentation and/or other materials provided with the distribution.
81  * 3. Neither the name of the University nor the names of its contributors
82  *    may be used to endorse or promote products derived from this software
83  *    without specific prior written permission.
84  *
85  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95  * SUCH DAMAGE.
96  */
97
98 #include <assert.h>
99 #include <errno.h>
100 #include <limits.h>
101 #include <locale.h>
102 #include <stddef.h>
103 #include <stdio.h>
104 #include <stdlib.h>
105 #include <string.h>
106 #include <unistd.h>
107 #include <wchar.h>
108 #include "../citrus/citrus_module.h"
109 #include "../citrus/citrus_ctype.h"
110 #include "rune.h"
111 #include "rune_local.h"
112 #include "multibyte.h"
113
114 struct localetable {
115         char path[PATH_MAX];
116         _RuneLocale *runelocale;
117         struct localetable *next;
118 };
119 static struct localetable *localetable_head;
120
121 _RuneLocale *
122 _findrunelocale(char *path)
123 {
124         struct localetable *lt;
125
126         _DIAGASSERT(path != NULL);
127
128         /* ones which we have seen already */
129         for (lt = localetable_head; lt; lt = lt->next) {
130                 if (strcmp(path, lt->path) == 0)
131                         return(lt->runelocale);
132         }
133
134         return(NULL);
135 }
136
137 int
138 _newrunelocale(char *path)
139 {
140         struct localetable *lt;
141         FILE *fp;
142         _RuneLocale *rl;
143         int ret;
144
145         /* path may be NULL (actually, it's checked below) */
146
147         if (path == NULL || strlen(path) + 1 > sizeof(lt->path))
148                 return(EFAULT);
149
150         rl = _findrunelocale(path);
151         if (rl)
152                 return(0);
153
154         if ((fp = fopen(path, "r")) == NULL)
155                 return(ENOENT);
156
157         if ((rl = _Read_RuneMagi(fp)) != NULL)
158                 goto found;
159         /* necessary for backward compatibility */
160         if ((rl = _Read_CTypeAsRune(fp)) != NULL)
161                 goto found;
162
163         fclose(fp);
164         return(EFTYPE);
165
166 found:
167         fclose(fp);
168
169         rl->rl_citrus_ctype = NULL;
170         ret = _citrus_ctype_open(&rl->rl_citrus_ctype, rl->rl_encoding,
171                                  rl->rl_variable, rl->rl_variable_len,
172                                  _PRIVSIZE);
173         if (ret) {
174                 _NukeRune(rl);
175                 return(ret);
176         }
177         if (__MB_LEN_MAX_RUNTIME <
178             _citrus_ctype_get_mb_cur_max(rl->rl_citrus_ctype)) {
179                 _NukeRune(rl);
180                 return(EINVAL);
181         }
182
183         /* register it */
184         lt = malloc(sizeof(struct localetable));
185         if (lt == NULL) {
186                 _NukeRune(rl);
187                 return(ENOMEM);
188         }
189         strlcpy(lt->path, path, sizeof(lt->path));
190         lt->runelocale = rl;
191         lt->next = localetable_head;
192         localetable_head = lt;
193
194         return(0);
195 }
196
197 int
198 _xpg4_setrunelocale(const char *encoding)
199 {
200         char path[PATH_MAX];
201         _RuneLocale *rl;
202         int error;
203
204         _DIAGASSERT(encoding != NULL);
205
206         if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
207                 rl = &_DefaultRuneLocale;
208                 goto found;
209         }
210
211         snprintf(path, sizeof(path), "%s/%s/LC_CTYPE", _PathLocale, encoding);
212
213         error = _newrunelocale(path);
214         if (error)
215                 return(error);
216         rl = _findrunelocale(path);
217         if (!rl)
218                 return(ENOENT);
219
220 found:
221         _CurrentRuneLocale = rl;
222         __mb_cur_max = _citrus_ctype_get_mb_cur_max(rl->rl_citrus_ctype);
223
224         return(0);
225 }