gettext.c revision 1.13 1 1.13 yamt /* $NetBSD: gettext.c,v 1.13 2002/02/13 08:01:13 yamt Exp $ */
2 1.1 itojun
3 1.1 itojun /*-
4 1.9 minoura * Copyright (c) 2000, 2001 Citrus Project,
5 1.1 itojun * All rights reserved.
6 1.1 itojun *
7 1.1 itojun * Redistribution and use in source and binary forms, with or without
8 1.1 itojun * modification, are permitted provided that the following conditions
9 1.1 itojun * are met:
10 1.1 itojun * 1. Redistributions of source code must retain the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer.
12 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer in the
14 1.1 itojun * documentation and/or other materials provided with the distribution.
15 1.1 itojun *
16 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 itojun * SUCH DAMAGE.
27 1.10 yamt *
28 1.10 yamt * $Citrus: xpg4dl/FreeBSD/lib/libintl/gettext.c,v 1.31 2001/09/27 15:18:45 yamt Exp $
29 1.1 itojun */
30 1.1 itojun
31 1.1 itojun #include <sys/cdefs.h>
32 1.1 itojun #if defined(LIBC_SCCS) && !defined(lint)
33 1.13 yamt __RCSID("$NetBSD: gettext.c,v 1.13 2002/02/13 08:01:13 yamt Exp $");
34 1.1 itojun #endif /* LIBC_SCCS and not lint */
35 1.1 itojun
36 1.1 itojun #include <sys/types.h>
37 1.1 itojun #include <sys/param.h>
38 1.1 itojun #include <sys/stat.h>
39 1.1 itojun #include <sys/mman.h>
40 1.1 itojun #include <sys/uio.h>
41 1.1 itojun
42 1.1 itojun #include <fcntl.h>
43 1.1 itojun #include <stdio.h>
44 1.1 itojun #include <stdlib.h>
45 1.1 itojun #include <unistd.h>
46 1.1 itojun #include <string.h>
47 1.1 itojun #if 0
48 1.1 itojun #include <util.h>
49 1.1 itojun #endif
50 1.1 itojun #include <libintl.h>
51 1.1 itojun #include <locale.h>
52 1.1 itojun #include "libintl_local.h"
53 1.1 itojun #include "pathnames.h"
54 1.1 itojun
55 1.1 itojun static const char *lookup_category __P((int));
56 1.1 itojun static const char *split_locale __P((const char *));
57 1.1 itojun static const char *lookup_mofile __P((char *, size_t, const char *,
58 1.10 yamt const char *, const char *, const char *, struct domainbinding *));
59 1.1 itojun static u_int32_t flip __P((u_int32_t, u_int32_t));
60 1.9 minoura static int validate __P((void *, struct mohandle *));
61 1.9 minoura static int mapit __P((const char *, struct domainbinding *));
62 1.9 minoura static int unmapit __P((struct domainbinding *));
63 1.9 minoura static const char *lookup_hash __P((const char *, struct domainbinding *));
64 1.9 minoura static const char *lookup_bsearch __P((const char *, struct domainbinding *));
65 1.9 minoura static const char *lookup __P((const char *, struct domainbinding *));
66 1.10 yamt static const char *get_lang_env(const char *);
67 1.1 itojun
68 1.1 itojun /*
69 1.1 itojun * shortcut functions. the main implementation resides in dcngettext().
70 1.1 itojun */
71 1.1 itojun char *
72 1.1 itojun gettext(msgid)
73 1.1 itojun const char *msgid;
74 1.1 itojun {
75 1.1 itojun
76 1.1 itojun return dcngettext(NULL, msgid, NULL, 1UL, LC_MESSAGES);
77 1.1 itojun }
78 1.1 itojun
79 1.1 itojun char *
80 1.1 itojun dgettext(domainname, msgid)
81 1.1 itojun const char *domainname;
82 1.1 itojun const char *msgid;
83 1.1 itojun {
84 1.1 itojun
85 1.1 itojun return dcngettext(domainname, msgid, NULL, 1UL, LC_MESSAGES);
86 1.1 itojun }
87 1.1 itojun
88 1.1 itojun char *
89 1.1 itojun dcgettext(domainname, msgid, category)
90 1.1 itojun const char *domainname;
91 1.1 itojun const char *msgid;
92 1.1 itojun int category;
93 1.1 itojun {
94 1.1 itojun
95 1.1 itojun return dcngettext(domainname, msgid, NULL, 1UL, category);
96 1.1 itojun }
97 1.1 itojun
98 1.1 itojun char *
99 1.1 itojun ngettext(msgid1, msgid2, n)
100 1.1 itojun const char *msgid1;
101 1.1 itojun const char *msgid2;
102 1.1 itojun unsigned long int n;
103 1.1 itojun {
104 1.1 itojun
105 1.1 itojun return dcngettext(NULL, msgid1, msgid2, n, LC_MESSAGES);
106 1.1 itojun }
107 1.1 itojun
108 1.1 itojun char *
109 1.1 itojun dngettext(domainname, msgid1, msgid2, n)
110 1.1 itojun const char *domainname;
111 1.1 itojun const char *msgid1;
112 1.1 itojun const char *msgid2;
113 1.1 itojun unsigned long int n;
114 1.1 itojun {
115 1.1 itojun
116 1.1 itojun return dcngettext(domainname, msgid1, msgid2, n, LC_MESSAGES);
117 1.1 itojun }
118 1.1 itojun
119 1.1 itojun /*
120 1.1 itojun * dcngettext() -
121 1.1 itojun * lookup internationalized message on database locale/category/domainname
122 1.1 itojun * (like ja_JP.eucJP/LC_MESSAGES/domainname).
123 1.1 itojun * if n equals to 1, internationalized message will be looked up for msgid1.
124 1.1 itojun * otherwise, message will be looked up for msgid2.
125 1.1 itojun * if the lookup fails, the function will return msgid1 or msgid2 as is.
126 1.1 itojun *
127 1.1 itojun * Even though the return type is "char *", caller should not rewrite the
128 1.1 itojun * region pointed to by the return value (should be "const char *", but can't
129 1.1 itojun * change it for compatibility with other implementations).
130 1.1 itojun *
131 1.1 itojun * by default (if domainname == NULL), domainname is taken from the value set
132 1.1 itojun * by textdomain(). usually name of the application (like "ls") is used as
133 1.1 itojun * domainname. category is usually LC_MESSAGES.
134 1.1 itojun *
135 1.1 itojun * the code reads in *.mo files generated by GNU gettext. *.mo is a host-
136 1.1 itojun * endian encoded file. both endians are supported here, as the files are in
137 1.1 itojun * /usr/share/locale! (or we should move those files into /usr/libdata)
138 1.1 itojun */
139 1.1 itojun
140 1.1 itojun static const char *
141 1.1 itojun lookup_category(category)
142 1.1 itojun int category;
143 1.1 itojun {
144 1.1 itojun
145 1.1 itojun switch (category) {
146 1.1 itojun case LC_COLLATE: return "LC_COLLATE";
147 1.1 itojun case LC_CTYPE: return "LC_CTYPE";
148 1.1 itojun case LC_MONETARY: return "LC_MONETARY";
149 1.1 itojun case LC_NUMERIC: return "LC_NUMERIC";
150 1.1 itojun case LC_TIME: return "LC_TIME";
151 1.1 itojun case LC_MESSAGES: return "LC_MESSAGES";
152 1.1 itojun }
153 1.1 itojun return NULL;
154 1.1 itojun }
155 1.1 itojun
156 1.1 itojun /*
157 1.1 itojun * XPG syntax: language[_territory[.codeset]][@modifier]
158 1.1 itojun * XXX boundary check on "result" is lacking
159 1.1 itojun */
160 1.1 itojun static const char *
161 1.1 itojun split_locale(lname)
162 1.1 itojun const char *lname;
163 1.1 itojun {
164 1.1 itojun char buf[BUFSIZ], tmp[BUFSIZ];
165 1.1 itojun char *l, *t, *c, *m;
166 1.1 itojun static char result[BUFSIZ];
167 1.1 itojun
168 1.1 itojun memset(result, 0, sizeof(result));
169 1.1 itojun
170 1.1 itojun if (strlen(lname) + 1 > sizeof(buf)) {
171 1.1 itojun fail:
172 1.1 itojun return lname;
173 1.1 itojun }
174 1.1 itojun
175 1.1 itojun strlcpy(buf, lname, sizeof(buf));
176 1.1 itojun m = strrchr(buf, '@');
177 1.1 itojun if (m)
178 1.1 itojun *m++ = '\0';
179 1.1 itojun c = strrchr(buf, '.');
180 1.1 itojun if (c)
181 1.1 itojun *c++ = '\0';
182 1.1 itojun t = strrchr(buf, '_');
183 1.1 itojun if (t)
184 1.1 itojun *t++ = '\0';
185 1.1 itojun l = buf;
186 1.1 itojun if (strlen(l) == 0)
187 1.1 itojun goto fail;
188 1.1 itojun if (c && !t)
189 1.1 itojun goto fail;
190 1.1 itojun
191 1.1 itojun if (m) {
192 1.1 itojun if (t) {
193 1.1 itojun if (c) {
194 1.1 itojun snprintf(tmp, sizeof(tmp), "%s_%s.%s@%s",
195 1.1 itojun l, t, c, m);
196 1.1 itojun strlcat(result, tmp, sizeof(result));
197 1.1 itojun strlcat(result, ":", sizeof(result));
198 1.1 itojun }
199 1.1 itojun snprintf(tmp, sizeof(tmp), "%s_%s@%s", l, t, m);
200 1.1 itojun strlcat(result, tmp, sizeof(result));
201 1.1 itojun strlcat(result, ":", sizeof(result));
202 1.1 itojun }
203 1.1 itojun snprintf(tmp, sizeof(tmp), "%s@%s", l, m);
204 1.1 itojun strlcat(result, tmp, sizeof(result));
205 1.1 itojun strlcat(result, ":", sizeof(result));
206 1.1 itojun }
207 1.1 itojun if (t) {
208 1.1 itojun if (c) {
209 1.1 itojun snprintf(tmp, sizeof(tmp), "%s_%s.%s", l, t, c);
210 1.1 itojun strlcat(result, tmp, sizeof(result));
211 1.1 itojun strlcat(result, ":", sizeof(result));
212 1.1 itojun }
213 1.12 yamt snprintf(tmp, sizeof(tmp), "%s_%s", l, t);
214 1.1 itojun strlcat(result, tmp, sizeof(result));
215 1.1 itojun strlcat(result, ":", sizeof(result));
216 1.1 itojun }
217 1.1 itojun strlcat(result, l, sizeof(result));
218 1.1 itojun
219 1.1 itojun return result;
220 1.1 itojun }
221 1.1 itojun
222 1.1 itojun static const char *
223 1.9 minoura lookup_mofile(buf, len, dir, lpath, category, domainname, db)
224 1.1 itojun char *buf;
225 1.1 itojun size_t len;
226 1.1 itojun const char *dir;
227 1.10 yamt const char *lpath; /* list of locales to be tried */
228 1.1 itojun const char *category;
229 1.1 itojun const char *domainname;
230 1.9 minoura struct domainbinding *db;
231 1.1 itojun {
232 1.1 itojun struct stat st;
233 1.1 itojun char *p, *q;
234 1.10 yamt char lpath_tmp[BUFSIZ];
235 1.1 itojun
236 1.10 yamt strlcpy(lpath_tmp, lpath, sizeof(lpath_tmp));
237 1.10 yamt q = lpath_tmp;
238 1.9 minoura /* CONSTCOND */
239 1.1 itojun while (1) {
240 1.1 itojun p = strsep(&q, ":");
241 1.1 itojun if (!p)
242 1.1 itojun break;
243 1.1 itojun if (!*p)
244 1.1 itojun continue;
245 1.1 itojun
246 1.1 itojun /* don't mess with default locales */
247 1.1 itojun if (strcmp(p, "C") == 0 || strcmp(p, "POSIX") == 0)
248 1.1 itojun return NULL;
249 1.1 itojun
250 1.1 itojun /* validate pathname */
251 1.1 itojun if (strchr(p, '/') || strchr(category, '/'))
252 1.1 itojun continue;
253 1.1 itojun #if 1 /*?*/
254 1.1 itojun if (strchr(domainname, '/'))
255 1.1 itojun continue;
256 1.1 itojun #endif
257 1.1 itojun
258 1.1 itojun snprintf(buf, len, "%s/%s/%s/%s.mo", dir, p,
259 1.1 itojun category, domainname);
260 1.1 itojun if (stat(buf, &st) < 0)
261 1.1 itojun continue;
262 1.1 itojun if ((st.st_mode & S_IFMT) != S_IFREG)
263 1.1 itojun continue;
264 1.1 itojun
265 1.9 minoura if (mapit(buf, db) == 0)
266 1.1 itojun return buf;
267 1.1 itojun }
268 1.1 itojun
269 1.1 itojun return NULL;
270 1.1 itojun }
271 1.1 itojun
272 1.1 itojun static u_int32_t
273 1.1 itojun flip(v, magic)
274 1.1 itojun u_int32_t v;
275 1.1 itojun u_int32_t magic;
276 1.1 itojun {
277 1.1 itojun
278 1.1 itojun if (magic == MO_MAGIC)
279 1.1 itojun return v;
280 1.1 itojun else if (magic == MO_MAGIC_SWAPPED) {
281 1.1 itojun v = ((v >> 24) & 0xff) | ((v >> 8) & 0xff00) |
282 1.1 itojun ((v << 8) & 0xff0000) | ((v << 24) & 0xff000000);
283 1.1 itojun return v;
284 1.1 itojun } else {
285 1.1 itojun abort();
286 1.1 itojun /*NOTREACHED*/
287 1.1 itojun }
288 1.1 itojun }
289 1.1 itojun
290 1.1 itojun static int
291 1.9 minoura validate(arg, mohandle)
292 1.1 itojun void *arg;
293 1.9 minoura struct mohandle *mohandle;
294 1.1 itojun {
295 1.1 itojun char *p;
296 1.1 itojun
297 1.1 itojun p = (char *)arg;
298 1.9 minoura if (p < (char *)mohandle->addr ||
299 1.9 minoura p > (char *)mohandle->addr + mohandle->len)
300 1.1 itojun return 0;
301 1.1 itojun else
302 1.1 itojun return 1;
303 1.1 itojun }
304 1.1 itojun
305 1.1 itojun int
306 1.9 minoura mapit(path, db)
307 1.1 itojun const char *path;
308 1.9 minoura struct domainbinding *db;
309 1.1 itojun {
310 1.1 itojun int fd;
311 1.1 itojun struct stat st;
312 1.1 itojun char *base;
313 1.1 itojun u_int32_t magic, revision;
314 1.1 itojun struct moentry *otable, *ttable;
315 1.1 itojun struct moentry_h *p;
316 1.1 itojun struct mo *mo;
317 1.1 itojun size_t l;
318 1.1 itojun int i;
319 1.1 itojun char *v;
320 1.9 minoura struct mohandle *mohandle = &db->mohandle;
321 1.1 itojun
322 1.9 minoura if (mohandle->addr && mohandle->addr != MAP_FAILED &&
323 1.9 minoura mohandle->mo.mo_magic)
324 1.1 itojun return 0; /*already opened*/
325 1.1 itojun
326 1.9 minoura unmapit(db);
327 1.1 itojun
328 1.1 itojun #if 0
329 1.1 itojun if (secure_path(path) != 0)
330 1.1 itojun goto fail;
331 1.1 itojun #endif
332 1.1 itojun if (stat(path, &st) < 0)
333 1.1 itojun goto fail;
334 1.1 itojun if ((st.st_mode & S_IFMT) != S_IFREG || st.st_size > GETTEXT_MMAP_MAX)
335 1.1 itojun goto fail;
336 1.1 itojun fd = open(path, O_RDONLY);
337 1.1 itojun if (fd < 0)
338 1.1 itojun goto fail;
339 1.2 itojun if (read(fd, &magic, sizeof(magic)) != sizeof(magic) ||
340 1.1 itojun (magic != MO_MAGIC && magic != MO_MAGIC_SWAPPED)) {
341 1.1 itojun close(fd);
342 1.1 itojun goto fail;
343 1.1 itojun }
344 1.2 itojun if (read(fd, &revision, sizeof(revision)) != sizeof(revision) ||
345 1.1 itojun flip(revision, magic) != MO_REVISION) {
346 1.1 itojun close(fd);
347 1.1 itojun goto fail;
348 1.1 itojun }
349 1.9 minoura mohandle->addr = mmap(NULL, (size_t)st.st_size, PROT_READ,
350 1.4 itojun MAP_FILE | MAP_SHARED, fd, (off_t)0);
351 1.9 minoura if (!mohandle->addr || mohandle->addr == MAP_FAILED) {
352 1.1 itojun close(fd);
353 1.1 itojun goto fail;
354 1.1 itojun }
355 1.1 itojun close(fd);
356 1.9 minoura mohandle->len = (size_t)st.st_size;
357 1.1 itojun
358 1.9 minoura base = mohandle->addr;
359 1.9 minoura mo = (struct mo *)mohandle->addr;
360 1.1 itojun
361 1.1 itojun /* flip endian. do not flip magic number! */
362 1.9 minoura mohandle->mo.mo_magic = mo->mo_magic;
363 1.9 minoura mohandle->mo.mo_revision = flip(mo->mo_revision, magic);
364 1.9 minoura mohandle->mo.mo_nstring = flip(mo->mo_nstring, magic);
365 1.1 itojun
366 1.1 itojun /* validate otable/ttable */
367 1.1 itojun otable = (struct moentry *)(base + flip(mo->mo_otable, magic));
368 1.1 itojun ttable = (struct moentry *)(base + flip(mo->mo_ttable, magic));
369 1.9 minoura if (!validate(otable, mohandle) ||
370 1.9 minoura !validate(&otable[mohandle->mo.mo_nstring], mohandle)) {
371 1.9 minoura unmapit(db);
372 1.1 itojun goto fail;
373 1.1 itojun }
374 1.9 minoura if (!validate(ttable, mohandle) ||
375 1.9 minoura !validate(&ttable[mohandle->mo.mo_nstring], mohandle)) {
376 1.9 minoura unmapit(db);
377 1.1 itojun goto fail;
378 1.1 itojun }
379 1.1 itojun
380 1.1 itojun /* allocate [ot]table, and convert to normal pointer representation. */
381 1.9 minoura l = sizeof(struct moentry_h) * mohandle->mo.mo_nstring;
382 1.9 minoura mohandle->mo.mo_otable = (struct moentry_h *)malloc(l);
383 1.9 minoura if (!mohandle->mo.mo_otable) {
384 1.9 minoura unmapit(db);
385 1.1 itojun goto fail;
386 1.1 itojun }
387 1.9 minoura mohandle->mo.mo_ttable = (struct moentry_h *)malloc(l);
388 1.9 minoura if (!mohandle->mo.mo_ttable) {
389 1.9 minoura unmapit(db);
390 1.1 itojun goto fail;
391 1.1 itojun }
392 1.9 minoura p = mohandle->mo.mo_otable;
393 1.9 minoura for (i = 0; i < mohandle->mo.mo_nstring; i++) {
394 1.1 itojun p[i].len = flip(otable[i].len, magic);
395 1.1 itojun p[i].off = base + flip(otable[i].off, magic);
396 1.1 itojun
397 1.9 minoura if (!validate(p[i].off, mohandle) ||
398 1.9 minoura !validate(p[i].off + p[i].len + 1, mohandle)) {
399 1.9 minoura unmapit(db);
400 1.1 itojun goto fail;
401 1.1 itojun }
402 1.1 itojun }
403 1.9 minoura p = mohandle->mo.mo_ttable;
404 1.9 minoura for (i = 0; i < mohandle->mo.mo_nstring; i++) {
405 1.1 itojun p[i].len = flip(ttable[i].len, magic);
406 1.1 itojun p[i].off = base + flip(ttable[i].off, magic);
407 1.1 itojun
408 1.9 minoura if (!validate(p[i].off, mohandle) ||
409 1.9 minoura !validate(p[i].off + p[i].len + 1, mohandle)) {
410 1.9 minoura unmapit(db);
411 1.1 itojun goto fail;
412 1.1 itojun }
413 1.1 itojun }
414 1.1 itojun
415 1.1 itojun /* grab MIME-header and charset field */
416 1.9 minoura mohandle->mo.mo_header = lookup("", db);
417 1.9 minoura if (mohandle->mo.mo_header)
418 1.9 minoura v = strstr(mohandle->mo.mo_header, "charset=");
419 1.1 itojun else
420 1.1 itojun v = NULL;
421 1.1 itojun if (v) {
422 1.9 minoura mohandle->mo.mo_charset = strdup(v + 8);
423 1.9 minoura if (!mohandle->mo.mo_charset)
424 1.6 itojun goto fail;
425 1.9 minoura v = strchr(mohandle->mo.mo_charset, '\n');
426 1.1 itojun if (v)
427 1.1 itojun *v = '\0';
428 1.1 itojun }
429 1.1 itojun
430 1.1 itojun /*
431 1.1 itojun * XXX check charset, reject it if we are unable to support the charset
432 1.1 itojun * with the current locale.
433 1.1 itojun * for example, if we are using euc-jp locale and we are looking at
434 1.1 itojun * *.mo file encoded by euc-kr (charset=euc-kr), we should reject
435 1.1 itojun * the *.mo file as we cannot support it.
436 1.1 itojun */
437 1.1 itojun
438 1.1 itojun return 0;
439 1.1 itojun
440 1.1 itojun fail:
441 1.1 itojun return -1;
442 1.1 itojun }
443 1.1 itojun
444 1.1 itojun static int
445 1.9 minoura unmapit(db)
446 1.9 minoura struct domainbinding *db;
447 1.1 itojun {
448 1.9 minoura struct mohandle *mohandle = &db->mohandle;
449 1.1 itojun
450 1.1 itojun /* unmap if there's already mapped region */
451 1.9 minoura if (mohandle->addr && mohandle->addr != MAP_FAILED)
452 1.9 minoura munmap(mohandle->addr, mohandle->len);
453 1.9 minoura mohandle->addr = NULL;
454 1.9 minoura if (mohandle->mo.mo_otable)
455 1.9 minoura free(mohandle->mo.mo_otable);
456 1.9 minoura if (mohandle->mo.mo_ttable)
457 1.9 minoura free(mohandle->mo.mo_ttable);
458 1.9 minoura if (mohandle->mo.mo_charset)
459 1.9 minoura free(mohandle->mo.mo_charset);
460 1.9 minoura memset(&mohandle->mo, 0, sizeof(mohandle->mo));
461 1.1 itojun return 0;
462 1.1 itojun }
463 1.1 itojun
464 1.9 minoura /* ARGSUSED */
465 1.1 itojun static const char *
466 1.9 minoura lookup_hash(msgid, db)
467 1.1 itojun const char *msgid;
468 1.9 minoura struct domainbinding *db;
469 1.1 itojun {
470 1.1 itojun
471 1.1 itojun /*
472 1.1 itojun * XXX should try a hashed lookup here, but to do so, we need to
473 1.1 itojun * look inside the GPL'ed *.c and re-implement...
474 1.1 itojun */
475 1.1 itojun return NULL;
476 1.1 itojun }
477 1.1 itojun
478 1.1 itojun static const char *
479 1.9 minoura lookup_bsearch(msgid, db)
480 1.1 itojun const char *msgid;
481 1.9 minoura struct domainbinding *db;
482 1.1 itojun {
483 1.1 itojun int top, bottom, middle, omiddle;
484 1.1 itojun int n;
485 1.9 minoura struct mohandle *mohandle = &db->mohandle;
486 1.1 itojun
487 1.1 itojun top = 0;
488 1.9 minoura bottom = mohandle->mo.mo_nstring;
489 1.1 itojun omiddle = -1;
490 1.9 minoura /* CONSTCOND */
491 1.1 itojun while (1) {
492 1.1 itojun if (top > bottom)
493 1.4 itojun break;
494 1.1 itojun middle = (top + bottom) / 2;
495 1.1 itojun /* avoid possible infinite loop, when the data is not sorted */
496 1.1 itojun if (omiddle == middle)
497 1.4 itojun break;
498 1.9 minoura if (middle < 0 || middle >= mohandle->mo.mo_nstring)
499 1.4 itojun break;
500 1.1 itojun
501 1.9 minoura n = strcmp(msgid, mohandle->mo.mo_otable[middle].off);
502 1.1 itojun if (n == 0)
503 1.9 minoura return (const char *)mohandle->mo.mo_ttable[middle].off;
504 1.1 itojun else if (n < 0)
505 1.1 itojun bottom = middle;
506 1.1 itojun else
507 1.1 itojun top = middle;
508 1.1 itojun omiddle = middle;
509 1.1 itojun }
510 1.1 itojun
511 1.1 itojun return NULL;
512 1.1 itojun }
513 1.1 itojun
514 1.1 itojun static const char *
515 1.9 minoura lookup(msgid, db)
516 1.1 itojun const char *msgid;
517 1.9 minoura struct domainbinding *db;
518 1.1 itojun {
519 1.1 itojun const char *v;
520 1.1 itojun
521 1.9 minoura v = lookup_hash(msgid, db);
522 1.1 itojun if (v)
523 1.1 itojun return v;
524 1.1 itojun
525 1.9 minoura return lookup_bsearch(msgid, db);
526 1.1 itojun }
527 1.1 itojun
528 1.10 yamt static const char *get_lang_env(const char *category_name)
529 1.10 yamt {
530 1.10 yamt const char *lang;
531 1.10 yamt
532 1.10 yamt /* 1. see LANGUAGE variable first. */
533 1.10 yamt lang = getenv("LANGUAGE");
534 1.10 yamt if (lang)
535 1.10 yamt return lang;
536 1.10 yamt
537 1.10 yamt /* 2. if LANGUAGE isn't set, see LC_ALL, LC_xxx, LANG. */
538 1.13 yamt lang = getenv("LC_ALL");
539 1.10 yamt if (!lang)
540 1.13 yamt lang = getenv(category_name);
541 1.10 yamt if (!lang)
542 1.10 yamt lang = getenv("LANG");
543 1.10 yamt
544 1.10 yamt if (!lang)
545 1.10 yamt return 0; /* error */
546 1.10 yamt
547 1.10 yamt return split_locale(lang);
548 1.10 yamt }
549 1.10 yamt
550 1.1 itojun char *
551 1.1 itojun dcngettext(domainname, msgid1, msgid2, n, category)
552 1.1 itojun const char *domainname;
553 1.1 itojun const char *msgid1;
554 1.1 itojun const char *msgid2;
555 1.1 itojun unsigned long int n;
556 1.1 itojun int category;
557 1.1 itojun {
558 1.1 itojun const char *msgid;
559 1.1 itojun char path[PATH_MAX];
560 1.10 yamt const char *lpath;
561 1.1 itojun static char olpath[PATH_MAX];
562 1.6 itojun const char *cname = NULL;
563 1.1 itojun const char *v;
564 1.6 itojun static char *ocname = NULL;
565 1.6 itojun static char *odomainname = NULL;
566 1.5 itojun struct domainbinding *db;
567 1.1 itojun
568 1.1 itojun msgid = (n == 1) ? msgid1 : msgid2;
569 1.8 minoura if (msgid == NULL)
570 1.8 minoura return NULL;
571 1.1 itojun
572 1.1 itojun if (!domainname)
573 1.9 minoura domainname = __current_domainname;
574 1.1 itojun cname = lookup_category(category);
575 1.1 itojun if (!domainname || !cname)
576 1.1 itojun goto fail;
577 1.1 itojun
578 1.10 yamt lpath = get_lang_env(cname);
579 1.10 yamt if (!lpath)
580 1.1 itojun goto fail;
581 1.10 yamt
582 1.9 minoura for (db = __bindings; db; db = db->next)
583 1.5 itojun if (strcmp(db->domainname, domainname) == 0)
584 1.5 itojun break;
585 1.9 minoura if (!db) {
586 1.9 minoura if (!bindtextdomain(domainname, _PATH_TEXTDOMAIN))
587 1.9 minoura goto fail;
588 1.9 minoura db = __bindings;
589 1.11 yamt }
590 1.11 yamt
591 1.11 yamt /* resolve relative path */
592 1.11 yamt /* XXX not necessary? */
593 1.11 yamt if (db->path[0] != '/') {
594 1.11 yamt char buf[PATH_MAX];
595 1.11 yamt
596 1.11 yamt if (getcwd(buf, sizeof(buf)) == 0)
597 1.11 yamt goto fail;
598 1.11 yamt if (strlcat(buf, "/", sizeof(buf)) >= sizeof(buf))
599 1.11 yamt goto fail;
600 1.11 yamt if (strlcat(buf, db->path, sizeof(buf)) >= sizeof(buf))
601 1.11 yamt goto fail;
602 1.11 yamt strcpy(db->path, buf);
603 1.9 minoura }
604 1.5 itojun
605 1.1 itojun /* don't bother looking it up if the values are the same */
606 1.5 itojun if (odomainname && strcmp(domainname, odomainname) == 0 &&
607 1.9 minoura ocname && strcmp(cname, ocname) == 0 && strcmp(lpath, olpath) == 0 &&
608 1.9 minoura db->mohandle.mo.mo_magic)
609 1.1 itojun goto found;
610 1.1 itojun
611 1.1 itojun /* try to find appropriate file, from $LANGUAGE */
612 1.5 itojun if (lookup_mofile(path, sizeof(path), db->path, lpath, cname,
613 1.9 minoura domainname, db) == NULL)
614 1.3 itojun goto fail;
615 1.5 itojun
616 1.5 itojun if (odomainname)
617 1.5 itojun free(odomainname);
618 1.5 itojun if (ocname)
619 1.5 itojun free(ocname);
620 1.6 itojun odomainname = strdup(domainname);
621 1.5 itojun ocname = strdup(cname);
622 1.6 itojun if (!odomainname || !ocname) {
623 1.6 itojun if (odomainname)
624 1.6 itojun free(odomainname);
625 1.6 itojun if (ocname)
626 1.6 itojun free(ocname);
627 1.6 itojun odomainname = ocname = NULL;
628 1.6 itojun }
629 1.10 yamt else
630 1.10 yamt strlcpy(olpath, lpath, sizeof(olpath));
631 1.1 itojun
632 1.1 itojun found:
633 1.9 minoura v = lookup(msgid, db);
634 1.1 itojun if (v) {
635 1.1 itojun /*
636 1.1 itojun * XXX call iconv() here, if translated text is encoded
637 1.1 itojun * differently from currently-selected encoding (locale).
638 1.1 itojun * look at Content-type header in *.mo file, in string obtained
639 1.1 itojun * by gettext("").
640 1.1 itojun */
641 1.1 itojun
642 1.1 itojun /*
643 1.1 itojun * Given the amount of printf-format security issues, it may
644 1.1 itojun * be a good idea to validate if the original msgid and the
645 1.1 itojun * translated message format string carry the same printf-like
646 1.1 itojun * format identifiers.
647 1.1 itojun */
648 1.1 itojun
649 1.1 itojun msgid = v;
650 1.1 itojun }
651 1.1 itojun
652 1.1 itojun fail:
653 1.1 itojun /* LINTED const cast */
654 1.1 itojun return (char *)msgid;
655 1.1 itojun }
656