gettext_iconv.c revision 1.3 1 1.3 yamt /* $NetBSD: gettext_iconv.c,v 1.3 2004/01/18 08:54:02 yamt Exp $ */
2 1.1 yamt
3 1.1 yamt /*-
4 1.1 yamt * Copyright (c) 2004 Citrus Project,
5 1.1 yamt * All rights reserved.
6 1.1 yamt *
7 1.1 yamt * Redistribution and use in source and binary forms, with or without
8 1.1 yamt * modification, are permitted provided that the following conditions
9 1.1 yamt * are met:
10 1.1 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1 yamt * notice, this list of conditions and the following disclaimer.
12 1.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1 yamt * documentation and/or other materials provided with the distribution.
15 1.1 yamt *
16 1.1 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 yamt * SUCH DAMAGE.
27 1.1 yamt *
28 1.1 yamt * $Citrus$
29 1.1 yamt */
30 1.1 yamt
31 1.1 yamt
32 1.1 yamt #include <sys/types.h>
33 1.1 yamt #include <sys/param.h>
34 1.1 yamt
35 1.1 yamt #include <errno.h>
36 1.1 yamt #include <iconv.h>
37 1.1 yamt #include <libintl.h>
38 1.1 yamt #include <langinfo.h>
39 1.1 yamt #include <search.h>
40 1.1 yamt #include <stdlib.h>
41 1.1 yamt #include <string.h>
42 1.1 yamt
43 1.1 yamt #include "libintl_local.h"
44 1.1 yamt
45 1.1 yamt struct cache {
46 1.1 yamt const char *c_origmsg;
47 1.1 yamt const char *c_resultmsg;
48 1.1 yamt };
49 1.1 yamt
50 1.1 yamt static const struct cache *cache_find(const char *, struct domainbinding *);
51 1.1 yamt static int cache_enter(const char *, const char *);
52 1.1 yamt static int cache_cmp(const void *, const void *);
53 1.1 yamt
54 1.1 yamt static void *cacheroot;
55 1.1 yamt
56 1.3 yamt /* ARGSUSED1 */
57 1.1 yamt static const struct cache *
58 1.1 yamt cache_find(const char *msg, struct domainbinding *db)
59 1.1 yamt {
60 1.1 yamt struct cache key;
61 1.1 yamt struct cache **c;
62 1.1 yamt
63 1.1 yamt key.c_origmsg = msg;
64 1.1 yamt c = tfind(&key, &cacheroot, cache_cmp);
65 1.1 yamt
66 1.1 yamt return c ? *c : NULL;
67 1.1 yamt }
68 1.1 yamt
69 1.1 yamt static int
70 1.1 yamt cache_enter(const char *origmsg, const char *resultmsg)
71 1.1 yamt {
72 1.1 yamt struct cache *c;
73 1.1 yamt
74 1.1 yamt c = malloc(sizeof(*c));
75 1.1 yamt if (c == NULL)
76 1.1 yamt return -1;
77 1.1 yamt
78 1.1 yamt c->c_origmsg = origmsg;
79 1.1 yamt c->c_resultmsg = resultmsg;
80 1.1 yamt
81 1.1 yamt if (tsearch(c, &cacheroot, cache_cmp) == NULL) {
82 1.1 yamt free(c);
83 1.1 yamt return -1;
84 1.1 yamt }
85 1.1 yamt
86 1.1 yamt return 0;
87 1.1 yamt }
88 1.1 yamt
89 1.1 yamt static int
90 1.1 yamt cache_cmp(const void *va, const void *vb)
91 1.1 yamt {
92 1.1 yamt const struct cache *a = va;
93 1.1 yamt const struct cache *b = vb;
94 1.1 yamt int result;
95 1.1 yamt
96 1.1 yamt result = a->c_origmsg - b->c_origmsg;
97 1.1 yamt
98 1.1 yamt return result;
99 1.1 yamt }
100 1.1 yamt
101 1.1 yamt #define GETTEXT_ICONV_MALLOC_CHUNK (16*1024)
102 1.1 yamt
103 1.1 yamt const char *
104 1.1 yamt __gettext_iconv(const char *origmsg, struct domainbinding *db)
105 1.1 yamt {
106 1.1 yamt const char *tocode;
107 1.1 yamt const char *fromcode = db->mohandle.mo.mo_charset;
108 1.1 yamt const struct cache *cache;
109 1.1 yamt const char *result;
110 1.1 yamt iconv_t cd;
111 1.1 yamt const char *src;
112 1.1 yamt char *dst;
113 1.1 yamt size_t origlen;
114 1.1 yamt size_t srclen;
115 1.1 yamt size_t dstlen;
116 1.1 yamt size_t nvalid;
117 1.1 yamt int savederrno = errno;
118 1.1 yamt
119 1.2 yamt /*
120 1.2 yamt * static buffer for converted texts.
121 1.2 yamt *
122 1.2 yamt * note:
123 1.2 yamt * we never free buffers once returned to callers.
124 1.2 yamt * because of interface design of gettext, we can't know
125 1.2 yamt * the lifetime of them.
126 1.2 yamt */
127 1.1 yamt static char *buffer;
128 1.1 yamt static size_t bufferlen;
129 1.1 yamt
130 1.1 yamt tocode = db->codeset;
131 1.1 yamt if (tocode == NULL) {
132 1.1 yamt /*
133 1.2 yamt * codeset isn't specified explicitly by
134 1.2 yamt * bind_textdomain_codeset().
135 1.2 yamt * use current locale(LC_MESSAGE)'s codeset.
136 1.1 yamt *
137 1.1 yamt * XXX maybe wrong; it can mismatch with
138 1.1 yamt * environment variable setting.
139 1.1 yamt */
140 1.1 yamt tocode = nl_langinfo(CODESET);
141 1.1 yamt }
142 1.1 yamt
143 1.1 yamt /*
144 1.1 yamt * shortcut if possible.
145 1.1 yamt * XXX should handle aliases
146 1.1 yamt */
147 1.1 yamt if (!strcasecmp(tocode, fromcode))
148 1.1 yamt return origmsg;
149 1.1 yamt
150 1.1 yamt /* XXX LOCK */
151 1.1 yamt
152 1.1 yamt /* XXX should detect change of tocode and purge caches? */
153 1.1 yamt
154 1.1 yamt /*
155 1.1 yamt * see if we have already converted this message.
156 1.1 yamt */
157 1.1 yamt cache = cache_find(origmsg, db);
158 1.1 yamt if (cache) {
159 1.1 yamt result = cache->c_resultmsg;
160 1.1 yamt goto out;
161 1.1 yamt }
162 1.1 yamt
163 1.1 yamt origlen = strlen(origmsg) + 1;
164 1.1 yamt again:
165 1.1 yamt cd = iconv_open(tocode, fromcode);
166 1.1 yamt if (cd == (iconv_t)-1) {
167 1.1 yamt result = origmsg;
168 1.1 yamt goto out;
169 1.1 yamt }
170 1.1 yamt
171 1.1 yamt src = origmsg;
172 1.1 yamt srclen = origlen;
173 1.1 yamt dst = buffer;
174 1.1 yamt dstlen = bufferlen;
175 1.1 yamt nvalid = iconv(cd, &src, &srclen, &dst, &dstlen);
176 1.1 yamt iconv_close(cd);
177 1.1 yamt
178 1.1 yamt if (nvalid == (size_t)-1) {
179 1.1 yamt /*
180 1.1 yamt * try to allocate a new buffer.
181 1.1 yamt *
182 1.1 yamt * just give up if GETTEXT_ICONV_MALLOC_CHUNK was not enough.
183 1.1 yamt */
184 1.1 yamt if (errno == E2BIG &&
185 1.1 yamt bufferlen != GETTEXT_ICONV_MALLOC_CHUNK) {
186 1.1 yamt buffer = malloc(GETTEXT_ICONV_MALLOC_CHUNK);
187 1.1 yamt if (buffer) {
188 1.1 yamt bufferlen = GETTEXT_ICONV_MALLOC_CHUNK;
189 1.1 yamt goto again;
190 1.1 yamt }
191 1.1 yamt }
192 1.1 yamt
193 1.1 yamt result = origmsg;
194 1.1 yamt } else if (cache_enter(origmsg, buffer)) {
195 1.1 yamt /*
196 1.1 yamt * failed to enter cache. give up.
197 1.1 yamt */
198 1.1 yamt result = origmsg;
199 1.1 yamt } else {
200 1.1 yamt size_t resultlen = dst - buffer;
201 1.1 yamt
202 1.1 yamt result = buffer;
203 1.1 yamt bufferlen -= resultlen;
204 1.1 yamt buffer += resultlen;
205 1.1 yamt }
206 1.1 yamt
207 1.1 yamt out:
208 1.1 yamt /* XXX UNLOCK */
209 1.1 yamt errno = savederrno;
210 1.1 yamt
211 1.1 yamt return result;
212 1.1 yamt }
213