gettext.c revision 1.23 1 1.23 yamt /* $NetBSD: gettext.c,v 1.23 2005/05/15 09:58:06 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.23 yamt __RCSID("$NetBSD: gettext.c,v 1.23 2005/05/15 09:58:06 yamt Exp $");
33 1.1 itojun
34 1.1 itojun #include <sys/param.h>
35 1.1 itojun #include <sys/stat.h>
36 1.1 itojun #include <sys/mman.h>
37 1.1 itojun #include <sys/uio.h>
38 1.1 itojun
39 1.19 tshiozak #include <assert.h>
40 1.1 itojun #include <fcntl.h>
41 1.1 itojun #include <stdio.h>
42 1.1 itojun #include <stdlib.h>
43 1.1 itojun #include <unistd.h>
44 1.1 itojun #include <string.h>
45 1.1 itojun #if 0
46 1.1 itojun #include <util.h>
47 1.1 itojun #endif
48 1.1 itojun #include <libintl.h>
49 1.1 itojun #include <locale.h>
50 1.1 itojun #include "libintl_local.h"
51 1.22 tshiozak #include "plural_parser.h"
52 1.1 itojun #include "pathnames.h"
53 1.1 itojun
54 1.1 itojun static const char *lookup_category __P((int));
55 1.1 itojun static const char *split_locale __P((const char *));
56 1.1 itojun static const char *lookup_mofile __P((char *, size_t, const char *,
57 1.10 yamt const char *, const char *, const char *, struct domainbinding *));
58 1.1 itojun static u_int32_t flip __P((u_int32_t, u_int32_t));
59 1.9 minoura static int validate __P((void *, struct mohandle *));
60 1.9 minoura static int mapit __P((const char *, struct domainbinding *));
61 1.9 minoura static int unmapit __P((struct domainbinding *));
62 1.22 tshiozak static const char *lookup_hash __P((const char *, struct domainbinding *,
63 1.22 tshiozak size_t *));
64 1.22 tshiozak static const char *lookup_bsearch __P((const char *, struct domainbinding *,
65 1.22 tshiozak size_t *));
66 1.22 tshiozak static const char *lookup __P((const char *, struct domainbinding *,
67 1.22 tshiozak size_t *));
68 1.18 yamt static const char *get_lang_env __P((const char *));
69 1.1 itojun
70 1.1 itojun /*
71 1.1 itojun * shortcut functions. the main implementation resides in dcngettext().
72 1.1 itojun */
73 1.1 itojun char *
74 1.1 itojun gettext(msgid)
75 1.1 itojun const char *msgid;
76 1.1 itojun {
77 1.1 itojun
78 1.1 itojun return dcngettext(NULL, msgid, NULL, 1UL, LC_MESSAGES);
79 1.1 itojun }
80 1.1 itojun
81 1.1 itojun char *
82 1.1 itojun dgettext(domainname, msgid)
83 1.1 itojun const char *domainname;
84 1.1 itojun const char *msgid;
85 1.1 itojun {
86 1.1 itojun
87 1.1 itojun return dcngettext(domainname, msgid, NULL, 1UL, LC_MESSAGES);
88 1.1 itojun }
89 1.1 itojun
90 1.1 itojun char *
91 1.1 itojun dcgettext(domainname, msgid, category)
92 1.1 itojun const char *domainname;
93 1.1 itojun const char *msgid;
94 1.1 itojun int category;
95 1.1 itojun {
96 1.1 itojun
97 1.1 itojun return dcngettext(domainname, msgid, NULL, 1UL, category);
98 1.1 itojun }
99 1.1 itojun
100 1.1 itojun char *
101 1.1 itojun ngettext(msgid1, msgid2, n)
102 1.1 itojun const char *msgid1;
103 1.1 itojun const char *msgid2;
104 1.1 itojun unsigned long int n;
105 1.1 itojun {
106 1.1 itojun
107 1.1 itojun return dcngettext(NULL, msgid1, msgid2, n, LC_MESSAGES);
108 1.1 itojun }
109 1.1 itojun
110 1.1 itojun char *
111 1.1 itojun dngettext(domainname, msgid1, msgid2, n)
112 1.1 itojun const char *domainname;
113 1.1 itojun const char *msgid1;
114 1.1 itojun const char *msgid2;
115 1.1 itojun unsigned long int n;
116 1.1 itojun {
117 1.1 itojun
118 1.1 itojun return dcngettext(domainname, msgid1, msgid2, n, LC_MESSAGES);
119 1.1 itojun }
120 1.1 itojun
121 1.1 itojun /*
122 1.1 itojun * dcngettext() -
123 1.1 itojun * lookup internationalized message on database locale/category/domainname
124 1.1 itojun * (like ja_JP.eucJP/LC_MESSAGES/domainname).
125 1.1 itojun * if n equals to 1, internationalized message will be looked up for msgid1.
126 1.1 itojun * otherwise, message will be looked up for msgid2.
127 1.1 itojun * if the lookup fails, the function will return msgid1 or msgid2 as is.
128 1.1 itojun *
129 1.1 itojun * Even though the return type is "char *", caller should not rewrite the
130 1.1 itojun * region pointed to by the return value (should be "const char *", but can't
131 1.1 itojun * change it for compatibility with other implementations).
132 1.1 itojun *
133 1.1 itojun * by default (if domainname == NULL), domainname is taken from the value set
134 1.1 itojun * by textdomain(). usually name of the application (like "ls") is used as
135 1.1 itojun * domainname. category is usually LC_MESSAGES.
136 1.1 itojun *
137 1.1 itojun * the code reads in *.mo files generated by GNU gettext. *.mo is a host-
138 1.1 itojun * endian encoded file. both endians are supported here, as the files are in
139 1.1 itojun * /usr/share/locale! (or we should move those files into /usr/libdata)
140 1.1 itojun */
141 1.1 itojun
142 1.1 itojun static const char *
143 1.1 itojun lookup_category(category)
144 1.1 itojun int category;
145 1.1 itojun {
146 1.1 itojun
147 1.1 itojun switch (category) {
148 1.1 itojun case LC_COLLATE: return "LC_COLLATE";
149 1.1 itojun case LC_CTYPE: return "LC_CTYPE";
150 1.1 itojun case LC_MONETARY: return "LC_MONETARY";
151 1.1 itojun case LC_NUMERIC: return "LC_NUMERIC";
152 1.1 itojun case LC_TIME: return "LC_TIME";
153 1.1 itojun case LC_MESSAGES: return "LC_MESSAGES";
154 1.1 itojun }
155 1.1 itojun return NULL;
156 1.1 itojun }
157 1.1 itojun
158 1.1 itojun /*
159 1.1 itojun * XPG syntax: language[_territory[.codeset]][@modifier]
160 1.1 itojun * XXX boundary check on "result" is lacking
161 1.1 itojun */
162 1.1 itojun static const char *
163 1.1 itojun split_locale(lname)
164 1.1 itojun const char *lname;
165 1.1 itojun {
166 1.1 itojun char buf[BUFSIZ], tmp[BUFSIZ];
167 1.1 itojun char *l, *t, *c, *m;
168 1.1 itojun static char result[BUFSIZ];
169 1.1 itojun
170 1.1 itojun memset(result, 0, sizeof(result));
171 1.1 itojun
172 1.1 itojun if (strlen(lname) + 1 > sizeof(buf)) {
173 1.1 itojun fail:
174 1.1 itojun return lname;
175 1.1 itojun }
176 1.1 itojun
177 1.1 itojun strlcpy(buf, lname, sizeof(buf));
178 1.1 itojun m = strrchr(buf, '@');
179 1.1 itojun if (m)
180 1.1 itojun *m++ = '\0';
181 1.1 itojun c = strrchr(buf, '.');
182 1.1 itojun if (c)
183 1.1 itojun *c++ = '\0';
184 1.1 itojun t = strrchr(buf, '_');
185 1.1 itojun if (t)
186 1.1 itojun *t++ = '\0';
187 1.1 itojun l = buf;
188 1.1 itojun if (strlen(l) == 0)
189 1.1 itojun goto fail;
190 1.1 itojun if (c && !t)
191 1.1 itojun goto fail;
192 1.1 itojun
193 1.1 itojun if (m) {
194 1.1 itojun if (t) {
195 1.1 itojun if (c) {
196 1.1 itojun snprintf(tmp, sizeof(tmp), "%s_%s.%s@%s",
197 1.19 tshiozak l, t, c, m);
198 1.1 itojun strlcat(result, tmp, sizeof(result));
199 1.1 itojun strlcat(result, ":", sizeof(result));
200 1.1 itojun }
201 1.19 tshiozak snprintf(tmp, sizeof(tmp), "%s_%s@%s", l, t, m);
202 1.1 itojun strlcat(result, tmp, sizeof(result));
203 1.1 itojun strlcat(result, ":", sizeof(result));
204 1.1 itojun }
205 1.19 tshiozak snprintf(tmp, sizeof(tmp), "%s@%s", l, m);
206 1.1 itojun strlcat(result, tmp, sizeof(result));
207 1.1 itojun strlcat(result, ":", sizeof(result));
208 1.1 itojun }
209 1.1 itojun if (t) {
210 1.1 itojun if (c) {
211 1.19 tshiozak snprintf(tmp, sizeof(tmp), "%s_%s.%s", l, t, c);
212 1.1 itojun strlcat(result, tmp, sizeof(result));
213 1.1 itojun strlcat(result, ":", sizeof(result));
214 1.1 itojun }
215 1.19 tshiozak snprintf(tmp, sizeof(tmp), "%s_%s", l, t);
216 1.1 itojun strlcat(result, tmp, sizeof(result));
217 1.1 itojun strlcat(result, ":", sizeof(result));
218 1.1 itojun }
219 1.1 itojun strlcat(result, l, sizeof(result));
220 1.1 itojun
221 1.1 itojun return result;
222 1.1 itojun }
223 1.1 itojun
224 1.1 itojun static const char *
225 1.9 minoura lookup_mofile(buf, len, dir, lpath, category, domainname, db)
226 1.1 itojun char *buf;
227 1.1 itojun size_t len;
228 1.1 itojun const char *dir;
229 1.10 yamt const char *lpath; /* list of locales to be tried */
230 1.1 itojun const char *category;
231 1.1 itojun const char *domainname;
232 1.9 minoura struct domainbinding *db;
233 1.1 itojun {
234 1.1 itojun struct stat st;
235 1.1 itojun char *p, *q;
236 1.10 yamt char lpath_tmp[BUFSIZ];
237 1.1 itojun
238 1.10 yamt strlcpy(lpath_tmp, lpath, sizeof(lpath_tmp));
239 1.10 yamt q = lpath_tmp;
240 1.9 minoura /* CONSTCOND */
241 1.1 itojun while (1) {
242 1.1 itojun p = strsep(&q, ":");
243 1.1 itojun if (!p)
244 1.1 itojun break;
245 1.1 itojun if (!*p)
246 1.1 itojun continue;
247 1.1 itojun
248 1.1 itojun /* don't mess with default locales */
249 1.1 itojun if (strcmp(p, "C") == 0 || strcmp(p, "POSIX") == 0)
250 1.1 itojun return NULL;
251 1.1 itojun
252 1.1 itojun /* validate pathname */
253 1.1 itojun if (strchr(p, '/') || strchr(category, '/'))
254 1.1 itojun continue;
255 1.1 itojun #if 1 /*?*/
256 1.1 itojun if (strchr(domainname, '/'))
257 1.1 itojun continue;
258 1.1 itojun #endif
259 1.1 itojun
260 1.1 itojun snprintf(buf, len, "%s/%s/%s/%s.mo", dir, p,
261 1.1 itojun category, domainname);
262 1.1 itojun if (stat(buf, &st) < 0)
263 1.1 itojun continue;
264 1.1 itojun if ((st.st_mode & S_IFMT) != S_IFREG)
265 1.1 itojun continue;
266 1.1 itojun
267 1.9 minoura if (mapit(buf, db) == 0)
268 1.1 itojun return buf;
269 1.1 itojun }
270 1.1 itojun
271 1.1 itojun return NULL;
272 1.1 itojun }
273 1.1 itojun
274 1.1 itojun static u_int32_t
275 1.1 itojun flip(v, magic)
276 1.1 itojun u_int32_t v;
277 1.1 itojun u_int32_t magic;
278 1.1 itojun {
279 1.1 itojun
280 1.1 itojun if (magic == MO_MAGIC)
281 1.1 itojun return v;
282 1.1 itojun else if (magic == MO_MAGIC_SWAPPED) {
283 1.1 itojun v = ((v >> 24) & 0xff) | ((v >> 8) & 0xff00) |
284 1.1 itojun ((v << 8) & 0xff0000) | ((v << 24) & 0xff000000);
285 1.1 itojun return v;
286 1.1 itojun } else {
287 1.1 itojun abort();
288 1.1 itojun /*NOTREACHED*/
289 1.1 itojun }
290 1.1 itojun }
291 1.1 itojun
292 1.1 itojun static int
293 1.9 minoura validate(arg, mohandle)
294 1.1 itojun void *arg;
295 1.9 minoura struct mohandle *mohandle;
296 1.1 itojun {
297 1.1 itojun char *p;
298 1.1 itojun
299 1.1 itojun p = (char *)arg;
300 1.9 minoura if (p < (char *)mohandle->addr ||
301 1.9 minoura p > (char *)mohandle->addr + mohandle->len)
302 1.1 itojun return 0;
303 1.1 itojun else
304 1.1 itojun return 1;
305 1.1 itojun }
306 1.1 itojun
307 1.20 tshiozak /*
308 1.20 tshiozak * calculate the step value if the hash value is conflicted.
309 1.20 tshiozak */
310 1.20 tshiozak static __inline u_int32_t
311 1.20 tshiozak calc_collision_step(u_int32_t hashval, u_int32_t hashsize)
312 1.20 tshiozak {
313 1.20 tshiozak _DIAGASSERT(hashsize>2);
314 1.20 tshiozak return (hashval % (hashsize - 2)) + 1;
315 1.20 tshiozak }
316 1.20 tshiozak
317 1.20 tshiozak /*
318 1.20 tshiozak * calculate the next index while conflicting.
319 1.20 tshiozak */
320 1.20 tshiozak static __inline u_int32_t
321 1.20 tshiozak calc_next_index(u_int32_t curidx, u_int32_t hashsize, u_int32_t step)
322 1.20 tshiozak {
323 1.20 tshiozak return curidx+step - (curidx >= hashsize-step ? hashsize : 0);
324 1.20 tshiozak }
325 1.20 tshiozak
326 1.20 tshiozak static int
327 1.20 tshiozak get_sysdep_string_table(struct mosysdepstr_h **table_h,
328 1.20 tshiozak u_int32_t *ofstable, uint32_t nstrings,
329 1.20 tshiozak u_int32_t magic, char *base)
330 1.20 tshiozak {
331 1.20 tshiozak int i, j;
332 1.20 tshiozak int count;
333 1.20 tshiozak size_t l;
334 1.20 tshiozak struct mosysdepstr *table;
335 1.20 tshiozak
336 1.20 tshiozak for (i=0; i<nstrings; i++) {
337 1.20 tshiozak /* get mosysdepstr record */
338 1.20 tshiozak /* LINTED: ignore the alignment problem. */
339 1.20 tshiozak table = (struct mosysdepstr *)(base + flip(ofstable[i], magic));
340 1.20 tshiozak /* count number of segments */
341 1.20 tshiozak count = 0;
342 1.20 tshiozak while (flip(table->segs[count++].ref, magic) != MO_LASTSEG)
343 1.20 tshiozak ;
344 1.20 tshiozak /* get table */
345 1.20 tshiozak l = sizeof(struct mosysdepstr_h) +
346 1.20 tshiozak sizeof(struct mosysdepsegentry_h) * (count-1);
347 1.20 tshiozak table_h[i] = (struct mosysdepstr_h *)malloc(l);
348 1.20 tshiozak if (!table_h[i])
349 1.20 tshiozak return -1;
350 1.20 tshiozak memset(table_h[i], 0, l);
351 1.20 tshiozak table_h[i]->off = (const char *)(base + flip(table->off, magic));
352 1.20 tshiozak for (j=0; j<count; j++) {
353 1.20 tshiozak table_h[i]->segs[j].len =
354 1.20 tshiozak flip(table->segs[j].len, magic);
355 1.20 tshiozak table_h[i]->segs[j].ref =
356 1.20 tshiozak flip(table->segs[j].ref, magic);
357 1.20 tshiozak }
358 1.20 tshiozak /* LINTED: ignore the alignment problem. */
359 1.20 tshiozak table = (struct mosysdepstr *)&table->segs[count];
360 1.20 tshiozak }
361 1.20 tshiozak return 0;
362 1.20 tshiozak }
363 1.20 tshiozak
364 1.20 tshiozak static int
365 1.20 tshiozak expand_sysdep(struct mohandle *mohandle, struct mosysdepstr_h *str)
366 1.20 tshiozak {
367 1.20 tshiozak int i;
368 1.20 tshiozak const char *src;
369 1.20 tshiozak char *dst;
370 1.20 tshiozak
371 1.20 tshiozak /* check whether already expanded */
372 1.20 tshiozak if (str->expanded)
373 1.20 tshiozak return 0;
374 1.20 tshiozak
375 1.20 tshiozak /* calc total length */
376 1.20 tshiozak str->expanded_len = 1;
377 1.20 tshiozak for (i=0; /*CONSTCOND*/1; i++) {
378 1.20 tshiozak str->expanded_len += str->segs[i].len;
379 1.20 tshiozak if (str->segs[i].ref == MO_LASTSEG)
380 1.20 tshiozak break;
381 1.20 tshiozak str->expanded_len +=
382 1.20 tshiozak mohandle->mo.mo_sysdep_segs[str->segs[i].ref].len;
383 1.20 tshiozak }
384 1.20 tshiozak /* expand */
385 1.20 tshiozak str->expanded = malloc(str->expanded_len);
386 1.20 tshiozak if (!str->expanded)
387 1.20 tshiozak return -1;
388 1.20 tshiozak src = str->off;
389 1.20 tshiozak dst = str->expanded;
390 1.20 tshiozak for (i=0; /*CONSTCOND*/1; i++) {
391 1.20 tshiozak memcpy(dst, src, str->segs[i].len);
392 1.20 tshiozak src += str->segs[i].len;
393 1.20 tshiozak dst += str->segs[i].len;
394 1.20 tshiozak if (str->segs[i].ref == MO_LASTSEG)
395 1.20 tshiozak break;
396 1.20 tshiozak memcpy(dst, mohandle->mo.mo_sysdep_segs[str->segs[i].ref].str,
397 1.20 tshiozak mohandle->mo.mo_sysdep_segs[str->segs[i].ref].len);
398 1.20 tshiozak dst += mohandle->mo.mo_sysdep_segs[str->segs[i].ref].len;
399 1.20 tshiozak }
400 1.20 tshiozak *dst = '\0';
401 1.20 tshiozak
402 1.20 tshiozak return 0;
403 1.20 tshiozak }
404 1.20 tshiozak
405 1.20 tshiozak static void
406 1.20 tshiozak insert_to_hash(u_int32_t *htable, u_int32_t hsize, const char *str,
407 1.20 tshiozak u_int32_t ref)
408 1.20 tshiozak {
409 1.20 tshiozak u_int32_t hashval, idx, step;
410 1.20 tshiozak
411 1.20 tshiozak hashval = __intl_string_hash(str);
412 1.20 tshiozak step = calc_collision_step(hashval, hsize);
413 1.20 tshiozak idx = hashval % hsize;
414 1.20 tshiozak
415 1.20 tshiozak while (htable[idx])
416 1.20 tshiozak idx = calc_next_index(idx, hsize, step);
417 1.20 tshiozak
418 1.20 tshiozak htable[idx] = ref;
419 1.20 tshiozak }
420 1.20 tshiozak
421 1.20 tshiozak static int
422 1.20 tshiozak setup_sysdep_stuffs(struct mo *mo, struct mohandle *mohandle, char *base)
423 1.20 tshiozak {
424 1.20 tshiozak u_int32_t magic;
425 1.20 tshiozak struct moentry *stable;
426 1.20 tshiozak size_t l;
427 1.20 tshiozak int i;
428 1.20 tshiozak char *v;
429 1.20 tshiozak u_int32_t *ofstable;
430 1.20 tshiozak
431 1.20 tshiozak magic = mo->mo_magic;
432 1.20 tshiozak
433 1.20 tshiozak mohandle->mo.mo_sysdep_nsegs = flip(mo->mo_sysdep_nsegs, magic);
434 1.20 tshiozak mohandle->mo.mo_sysdep_nstring = flip(mo->mo_sysdep_nstring, magic);
435 1.20 tshiozak
436 1.20 tshiozak if (mohandle->mo.mo_sysdep_nstring == 0)
437 1.20 tshiozak return 0;
438 1.20 tshiozak
439 1.20 tshiozak /* check hash size */
440 1.20 tshiozak if (mohandle->mo.mo_hsize <= 2 ||
441 1.20 tshiozak mohandle->mo.mo_hsize <
442 1.20 tshiozak (mohandle->mo.mo_nstring + mohandle->mo.mo_sysdep_nstring))
443 1.20 tshiozak return -1;
444 1.20 tshiozak
445 1.20 tshiozak /* get sysdep segments */
446 1.21 yamt l = sizeof(struct mosysdepsegs_h) * mohandle->mo.mo_sysdep_nsegs;
447 1.20 tshiozak mohandle->mo.mo_sysdep_segs = (struct mosysdepsegs_h *)malloc(l);
448 1.20 tshiozak if (!mohandle->mo.mo_sysdep_segs)
449 1.20 tshiozak return -1;
450 1.20 tshiozak /* LINTED: ignore the alignment problem. */
451 1.20 tshiozak stable = (struct moentry *)(base + flip(mo->mo_sysdep_segoff, magic));
452 1.20 tshiozak for (i=0; i<mohandle->mo.mo_sysdep_nsegs; i++) {
453 1.20 tshiozak v = base + flip(stable[i].off, magic);
454 1.20 tshiozak mohandle->mo.mo_sysdep_segs[i].str =
455 1.20 tshiozak __intl_sysdep_get_string_by_tag(
456 1.20 tshiozak v,
457 1.20 tshiozak &mohandle->mo.mo_sysdep_segs[i].len);
458 1.20 tshiozak }
459 1.20 tshiozak
460 1.20 tshiozak /* get sysdep string table */
461 1.20 tshiozak mohandle->mo.mo_sysdep_otable =
462 1.20 tshiozak (struct mosysdepstr_h **)calloc(mohandle->mo.mo_sysdep_nstring,
463 1.20 tshiozak sizeof(struct mosysdepstr_h *));
464 1.20 tshiozak if (!mohandle->mo.mo_sysdep_otable)
465 1.20 tshiozak return -1;
466 1.20 tshiozak /* LINTED: ignore the alignment problem. */
467 1.20 tshiozak ofstable = (u_int32_t *)(base + flip(mo->mo_sysdep_otable, magic));
468 1.20 tshiozak if (get_sysdep_string_table(mohandle->mo.mo_sysdep_otable, ofstable,
469 1.20 tshiozak mohandle->mo.mo_sysdep_nstring, magic,
470 1.20 tshiozak base))
471 1.20 tshiozak return -1;
472 1.20 tshiozak mohandle->mo.mo_sysdep_ttable =
473 1.20 tshiozak (struct mosysdepstr_h **)calloc(mohandle->mo.mo_sysdep_nstring,
474 1.20 tshiozak sizeof(struct mosysdepstr_h *));
475 1.20 tshiozak if (!mohandle->mo.mo_sysdep_ttable)
476 1.20 tshiozak return -1;
477 1.20 tshiozak /* LINTED: ignore the alignment problem. */
478 1.20 tshiozak ofstable = (u_int32_t *)(base + flip(mo->mo_sysdep_ttable, magic));
479 1.20 tshiozak if (get_sysdep_string_table(mohandle->mo.mo_sysdep_ttable, ofstable,
480 1.20 tshiozak mohandle->mo.mo_sysdep_nstring, magic,
481 1.20 tshiozak base))
482 1.20 tshiozak return -1;
483 1.20 tshiozak
484 1.20 tshiozak /* update hash */
485 1.20 tshiozak for (i=0; i<mohandle->mo.mo_sysdep_nstring; i++) {
486 1.20 tshiozak if (expand_sysdep(mohandle, mohandle->mo.mo_sysdep_otable[i]))
487 1.20 tshiozak return -1;
488 1.20 tshiozak insert_to_hash(mohandle->mo.mo_htable,
489 1.20 tshiozak mohandle->mo.mo_hsize,
490 1.20 tshiozak mohandle->mo.mo_sysdep_otable[i]->expanded,
491 1.20 tshiozak (i+1) | MO_HASH_SYSDEP_MASK);
492 1.20 tshiozak }
493 1.20 tshiozak
494 1.20 tshiozak return 0;
495 1.20 tshiozak }
496 1.20 tshiozak
497 1.1 itojun int
498 1.9 minoura mapit(path, db)
499 1.1 itojun const char *path;
500 1.9 minoura struct domainbinding *db;
501 1.1 itojun {
502 1.1 itojun int fd;
503 1.1 itojun struct stat st;
504 1.1 itojun char *base;
505 1.20 tshiozak u_int32_t magic, revision, flags = 0;
506 1.1 itojun struct moentry *otable, *ttable;
507 1.19 tshiozak const u_int32_t *htable;
508 1.1 itojun struct moentry_h *p;
509 1.1 itojun struct mo *mo;
510 1.22 tshiozak size_t l, headerlen;
511 1.1 itojun int i;
512 1.1 itojun char *v;
513 1.9 minoura struct mohandle *mohandle = &db->mohandle;
514 1.1 itojun
515 1.9 minoura if (mohandle->addr && mohandle->addr != MAP_FAILED &&
516 1.9 minoura mohandle->mo.mo_magic)
517 1.1 itojun return 0; /*already opened*/
518 1.1 itojun
519 1.9 minoura unmapit(db);
520 1.1 itojun
521 1.1 itojun #if 0
522 1.1 itojun if (secure_path(path) != 0)
523 1.1 itojun goto fail;
524 1.1 itojun #endif
525 1.1 itojun if (stat(path, &st) < 0)
526 1.1 itojun goto fail;
527 1.1 itojun if ((st.st_mode & S_IFMT) != S_IFREG || st.st_size > GETTEXT_MMAP_MAX)
528 1.1 itojun goto fail;
529 1.1 itojun fd = open(path, O_RDONLY);
530 1.1 itojun if (fd < 0)
531 1.1 itojun goto fail;
532 1.2 itojun if (read(fd, &magic, sizeof(magic)) != sizeof(magic) ||
533 1.1 itojun (magic != MO_MAGIC && magic != MO_MAGIC_SWAPPED)) {
534 1.1 itojun close(fd);
535 1.1 itojun goto fail;
536 1.1 itojun }
537 1.19 tshiozak if (read(fd, &revision, sizeof(revision)) != sizeof(revision)) {
538 1.19 tshiozak close(fd);
539 1.19 tshiozak goto fail;
540 1.19 tshiozak }
541 1.19 tshiozak switch (flip(revision, magic)) {
542 1.19 tshiozak case MO_MAKE_REV(0, 0):
543 1.20 tshiozak break;
544 1.19 tshiozak case MO_MAKE_REV(0, 1):
545 1.19 tshiozak case MO_MAKE_REV(1, 1):
546 1.20 tshiozak flags |= MO_F_SYSDEP;
547 1.19 tshiozak break;
548 1.19 tshiozak default:
549 1.1 itojun close(fd);
550 1.1 itojun goto fail;
551 1.1 itojun }
552 1.9 minoura mohandle->addr = mmap(NULL, (size_t)st.st_size, PROT_READ,
553 1.4 itojun MAP_FILE | MAP_SHARED, fd, (off_t)0);
554 1.9 minoura if (!mohandle->addr || mohandle->addr == MAP_FAILED) {
555 1.1 itojun close(fd);
556 1.1 itojun goto fail;
557 1.1 itojun }
558 1.1 itojun close(fd);
559 1.9 minoura mohandle->len = (size_t)st.st_size;
560 1.1 itojun
561 1.9 minoura base = mohandle->addr;
562 1.9 minoura mo = (struct mo *)mohandle->addr;
563 1.1 itojun
564 1.1 itojun /* flip endian. do not flip magic number! */
565 1.9 minoura mohandle->mo.mo_magic = mo->mo_magic;
566 1.9 minoura mohandle->mo.mo_revision = flip(mo->mo_revision, magic);
567 1.9 minoura mohandle->mo.mo_nstring = flip(mo->mo_nstring, magic);
568 1.19 tshiozak mohandle->mo.mo_hsize = flip(mo->mo_hsize, magic);
569 1.20 tshiozak mohandle->mo.mo_flags = flags;
570 1.1 itojun
571 1.1 itojun /* validate otable/ttable */
572 1.19 tshiozak /* LINTED: ignore the alignment problem. */
573 1.1 itojun otable = (struct moentry *)(base + flip(mo->mo_otable, magic));
574 1.19 tshiozak /* LINTED: ignore the alignment problem. */
575 1.1 itojun ttable = (struct moentry *)(base + flip(mo->mo_ttable, magic));
576 1.9 minoura if (!validate(otable, mohandle) ||
577 1.9 minoura !validate(&otable[mohandle->mo.mo_nstring], mohandle)) {
578 1.9 minoura unmapit(db);
579 1.1 itojun goto fail;
580 1.1 itojun }
581 1.9 minoura if (!validate(ttable, mohandle) ||
582 1.9 minoura !validate(&ttable[mohandle->mo.mo_nstring], mohandle)) {
583 1.9 minoura unmapit(db);
584 1.1 itojun goto fail;
585 1.1 itojun }
586 1.1 itojun
587 1.1 itojun /* allocate [ot]table, and convert to normal pointer representation. */
588 1.9 minoura l = sizeof(struct moentry_h) * mohandle->mo.mo_nstring;
589 1.9 minoura mohandle->mo.mo_otable = (struct moentry_h *)malloc(l);
590 1.9 minoura if (!mohandle->mo.mo_otable) {
591 1.9 minoura unmapit(db);
592 1.1 itojun goto fail;
593 1.1 itojun }
594 1.9 minoura mohandle->mo.mo_ttable = (struct moentry_h *)malloc(l);
595 1.9 minoura if (!mohandle->mo.mo_ttable) {
596 1.9 minoura unmapit(db);
597 1.1 itojun goto fail;
598 1.1 itojun }
599 1.9 minoura p = mohandle->mo.mo_otable;
600 1.9 minoura for (i = 0; i < mohandle->mo.mo_nstring; i++) {
601 1.1 itojun p[i].len = flip(otable[i].len, magic);
602 1.1 itojun p[i].off = base + flip(otable[i].off, magic);
603 1.1 itojun
604 1.9 minoura if (!validate(p[i].off, mohandle) ||
605 1.9 minoura !validate(p[i].off + p[i].len + 1, mohandle)) {
606 1.9 minoura unmapit(db);
607 1.1 itojun goto fail;
608 1.1 itojun }
609 1.1 itojun }
610 1.9 minoura p = mohandle->mo.mo_ttable;
611 1.9 minoura for (i = 0; i < mohandle->mo.mo_nstring; i++) {
612 1.1 itojun p[i].len = flip(ttable[i].len, magic);
613 1.1 itojun p[i].off = base + flip(ttable[i].off, magic);
614 1.1 itojun
615 1.9 minoura if (!validate(p[i].off, mohandle) ||
616 1.9 minoura !validate(p[i].off + p[i].len + 1, mohandle)) {
617 1.9 minoura unmapit(db);
618 1.1 itojun goto fail;
619 1.1 itojun }
620 1.1 itojun }
621 1.19 tshiozak /* allocate htable, and convert it to the host order. */
622 1.19 tshiozak if (mohandle->mo.mo_hsize > 2) {
623 1.19 tshiozak l = sizeof(u_int32_t) * mohandle->mo.mo_hsize;
624 1.19 tshiozak mohandle->mo.mo_htable = (u_int32_t *)malloc(l);
625 1.19 tshiozak if (!mohandle->mo.mo_htable) {
626 1.19 tshiozak unmapit(db);
627 1.19 tshiozak goto fail;
628 1.19 tshiozak }
629 1.19 tshiozak /* LINTED: ignore the alignment problem. */
630 1.19 tshiozak htable = (const u_int32_t *)(base+flip(mo->mo_hoffset, magic));
631 1.19 tshiozak for (i=0; i < mohandle->mo.mo_hsize; i++) {
632 1.19 tshiozak mohandle->mo.mo_htable[i] = flip(htable[i], magic);
633 1.19 tshiozak if (mohandle->mo.mo_htable[i] >=
634 1.19 tshiozak mohandle->mo.mo_nstring+1) {
635 1.19 tshiozak /* illegal string number. */
636 1.19 tshiozak unmapit(db);
637 1.19 tshiozak goto fail;
638 1.19 tshiozak }
639 1.19 tshiozak }
640 1.19 tshiozak }
641 1.1 itojun /* grab MIME-header and charset field */
642 1.22 tshiozak mohandle->mo.mo_header = lookup("", db, &headerlen);
643 1.9 minoura if (mohandle->mo.mo_header)
644 1.9 minoura v = strstr(mohandle->mo.mo_header, "charset=");
645 1.1 itojun else
646 1.1 itojun v = NULL;
647 1.1 itojun if (v) {
648 1.9 minoura mohandle->mo.mo_charset = strdup(v + 8);
649 1.9 minoura if (!mohandle->mo.mo_charset)
650 1.6 itojun goto fail;
651 1.9 minoura v = strchr(mohandle->mo.mo_charset, '\n');
652 1.1 itojun if (v)
653 1.1 itojun *v = '\0';
654 1.1 itojun }
655 1.22 tshiozak if (_gettext_parse_plural(&mohandle->mo.mo_plural,
656 1.22 tshiozak &mohandle->mo.mo_nplurals,
657 1.22 tshiozak mohandle->mo.mo_header, headerlen))
658 1.22 tshiozak mohandle->mo.mo_plural = NULL;
659 1.1 itojun
660 1.1 itojun /*
661 1.1 itojun * XXX check charset, reject it if we are unable to support the charset
662 1.1 itojun * with the current locale.
663 1.1 itojun * for example, if we are using euc-jp locale and we are looking at
664 1.1 itojun * *.mo file encoded by euc-kr (charset=euc-kr), we should reject
665 1.1 itojun * the *.mo file as we cannot support it.
666 1.1 itojun */
667 1.1 itojun
668 1.20 tshiozak /* system dependent string support */
669 1.20 tshiozak if ((mohandle->mo.mo_flags & MO_F_SYSDEP) != 0) {
670 1.20 tshiozak if (setup_sysdep_stuffs(mo, mohandle, base)) {
671 1.20 tshiozak unmapit(db);
672 1.20 tshiozak goto fail;
673 1.20 tshiozak }
674 1.20 tshiozak }
675 1.20 tshiozak
676 1.1 itojun return 0;
677 1.1 itojun
678 1.1 itojun fail:
679 1.1 itojun return -1;
680 1.1 itojun }
681 1.1 itojun
682 1.20 tshiozak static void
683 1.20 tshiozak free_sysdep_table(struct mosysdepstr_h **table, u_int32_t nstring)
684 1.20 tshiozak {
685 1.20 tshiozak u_int32_t i;
686 1.20 tshiozak
687 1.20 tshiozak for (i=0; i<nstring; i++) {
688 1.20 tshiozak if (table[i]) {
689 1.20 tshiozak if (table[i]->expanded)
690 1.20 tshiozak free(table[i]->expanded);
691 1.20 tshiozak free(table[i]);
692 1.20 tshiozak }
693 1.20 tshiozak }
694 1.20 tshiozak free(table);
695 1.20 tshiozak }
696 1.20 tshiozak
697 1.1 itojun static int
698 1.9 minoura unmapit(db)
699 1.9 minoura struct domainbinding *db;
700 1.1 itojun {
701 1.9 minoura struct mohandle *mohandle = &db->mohandle;
702 1.1 itojun
703 1.1 itojun /* unmap if there's already mapped region */
704 1.9 minoura if (mohandle->addr && mohandle->addr != MAP_FAILED)
705 1.9 minoura munmap(mohandle->addr, mohandle->len);
706 1.9 minoura mohandle->addr = NULL;
707 1.9 minoura if (mohandle->mo.mo_otable)
708 1.9 minoura free(mohandle->mo.mo_otable);
709 1.9 minoura if (mohandle->mo.mo_ttable)
710 1.9 minoura free(mohandle->mo.mo_ttable);
711 1.9 minoura if (mohandle->mo.mo_charset)
712 1.9 minoura free(mohandle->mo.mo_charset);
713 1.19 tshiozak if (mohandle->mo.mo_htable)
714 1.19 tshiozak free(mohandle->mo.mo_htable);
715 1.20 tshiozak if (mohandle->mo.mo_sysdep_segs)
716 1.20 tshiozak free(mohandle->mo.mo_sysdep_segs);
717 1.20 tshiozak if (mohandle->mo.mo_sysdep_otable) {
718 1.20 tshiozak free_sysdep_table(mohandle->mo.mo_sysdep_otable,
719 1.20 tshiozak mohandle->mo.mo_sysdep_nstring);
720 1.20 tshiozak }
721 1.20 tshiozak if (mohandle->mo.mo_sysdep_ttable) {
722 1.20 tshiozak free_sysdep_table(mohandle->mo.mo_sysdep_ttable,
723 1.20 tshiozak mohandle->mo.mo_sysdep_nstring);
724 1.20 tshiozak }
725 1.22 tshiozak if (mohandle->mo.mo_plural)
726 1.22 tshiozak _gettext_free_plural(mohandle->mo.mo_plural);
727 1.9 minoura memset(&mohandle->mo, 0, sizeof(mohandle->mo));
728 1.1 itojun return 0;
729 1.1 itojun }
730 1.1 itojun
731 1.9 minoura /* ARGSUSED */
732 1.1 itojun static const char *
733 1.22 tshiozak lookup_hash(msgid, db, rlen)
734 1.1 itojun const char *msgid;
735 1.9 minoura struct domainbinding *db;
736 1.22 tshiozak size_t *rlen;
737 1.1 itojun {
738 1.19 tshiozak struct mohandle *mohandle = &db->mohandle;
739 1.19 tshiozak u_int32_t idx, hashval, step, strno;
740 1.19 tshiozak size_t len;
741 1.20 tshiozak struct mosysdepstr_h *sysdep_otable, *sysdep_ttable;
742 1.19 tshiozak
743 1.19 tshiozak if (mohandle->mo.mo_hsize <= 2 || mohandle->mo.mo_htable == NULL)
744 1.19 tshiozak return NULL;
745 1.1 itojun
746 1.19 tshiozak hashval = __intl_string_hash(msgid);
747 1.19 tshiozak step = calc_collision_step(hashval, mohandle->mo.mo_hsize);
748 1.19 tshiozak idx = hashval % mohandle->mo.mo_hsize;
749 1.19 tshiozak len = strlen(msgid);
750 1.19 tshiozak while (/*CONSTCOND*/1) {
751 1.19 tshiozak strno = mohandle->mo.mo_htable[idx];
752 1.19 tshiozak if (strno == 0) {
753 1.19 tshiozak /* unexpected miss */
754 1.19 tshiozak return NULL;
755 1.19 tshiozak }
756 1.19 tshiozak strno--;
757 1.20 tshiozak if ((strno & MO_HASH_SYSDEP_MASK) == 0) {
758 1.20 tshiozak /* system independent strings */
759 1.20 tshiozak if (len <= mohandle->mo.mo_otable[strno].len &&
760 1.20 tshiozak !strcmp(msgid, mohandle->mo.mo_otable[strno].off)) {
761 1.20 tshiozak /* hit */
762 1.22 tshiozak if (rlen)
763 1.22 tshiozak *rlen =
764 1.22 tshiozak mohandle->mo.mo_ttable[strno].len;
765 1.20 tshiozak return mohandle->mo.mo_ttable[strno].off;
766 1.20 tshiozak }
767 1.20 tshiozak } else {
768 1.20 tshiozak /* system dependent strings */
769 1.20 tshiozak strno &= ~MO_HASH_SYSDEP_MASK;
770 1.20 tshiozak sysdep_otable = mohandle->mo.mo_sysdep_otable[strno];
771 1.20 tshiozak sysdep_ttable = mohandle->mo.mo_sysdep_ttable[strno];
772 1.20 tshiozak if (len <= sysdep_otable->expanded_len &&
773 1.20 tshiozak !strcmp(msgid, sysdep_otable->expanded)) {
774 1.20 tshiozak /* hit */
775 1.20 tshiozak if (expand_sysdep(mohandle, sysdep_ttable))
776 1.20 tshiozak /* memory exhausted */
777 1.20 tshiozak return NULL;
778 1.22 tshiozak if (rlen)
779 1.22 tshiozak *rlen = sysdep_ttable->expanded_len;
780 1.20 tshiozak return sysdep_ttable->expanded;
781 1.20 tshiozak }
782 1.19 tshiozak }
783 1.19 tshiozak idx = calc_next_index(idx, mohandle->mo.mo_hsize, step);
784 1.19 tshiozak }
785 1.19 tshiozak /*NOTREACHED*/
786 1.1 itojun }
787 1.1 itojun
788 1.1 itojun static const char *
789 1.22 tshiozak lookup_bsearch(msgid, db, rlen)
790 1.1 itojun const char *msgid;
791 1.9 minoura struct domainbinding *db;
792 1.22 tshiozak size_t *rlen;
793 1.1 itojun {
794 1.1 itojun int top, bottom, middle, omiddle;
795 1.1 itojun int n;
796 1.9 minoura struct mohandle *mohandle = &db->mohandle;
797 1.1 itojun
798 1.1 itojun top = 0;
799 1.9 minoura bottom = mohandle->mo.mo_nstring;
800 1.1 itojun omiddle = -1;
801 1.9 minoura /* CONSTCOND */
802 1.1 itojun while (1) {
803 1.1 itojun if (top > bottom)
804 1.4 itojun break;
805 1.1 itojun middle = (top + bottom) / 2;
806 1.1 itojun /* avoid possible infinite loop, when the data is not sorted */
807 1.1 itojun if (omiddle == middle)
808 1.4 itojun break;
809 1.9 minoura if (middle < 0 || middle >= mohandle->mo.mo_nstring)
810 1.4 itojun break;
811 1.1 itojun
812 1.9 minoura n = strcmp(msgid, mohandle->mo.mo_otable[middle].off);
813 1.22 tshiozak if (n == 0) {
814 1.22 tshiozak if (rlen)
815 1.22 tshiozak *rlen = mohandle->mo.mo_ttable[middle].len;
816 1.9 minoura return (const char *)mohandle->mo.mo_ttable[middle].off;
817 1.22 tshiozak }
818 1.1 itojun else if (n < 0)
819 1.1 itojun bottom = middle;
820 1.1 itojun else
821 1.1 itojun top = middle;
822 1.1 itojun omiddle = middle;
823 1.1 itojun }
824 1.1 itojun
825 1.1 itojun return NULL;
826 1.1 itojun }
827 1.1 itojun
828 1.1 itojun static const char *
829 1.22 tshiozak lookup(msgid, db, rlen)
830 1.1 itojun const char *msgid;
831 1.9 minoura struct domainbinding *db;
832 1.22 tshiozak size_t *rlen;
833 1.1 itojun {
834 1.1 itojun const char *v;
835 1.1 itojun
836 1.22 tshiozak v = lookup_hash(msgid, db, rlen);
837 1.1 itojun if (v)
838 1.1 itojun return v;
839 1.1 itojun
840 1.22 tshiozak return lookup_bsearch(msgid, db, rlen);
841 1.1 itojun }
842 1.1 itojun
843 1.16 itojun static const char *
844 1.16 itojun get_lang_env(const char *category_name)
845 1.10 yamt {
846 1.10 yamt const char *lang;
847 1.10 yamt
848 1.10 yamt /* 1. see LANGUAGE variable first. */
849 1.10 yamt lang = getenv("LANGUAGE");
850 1.10 yamt if (lang)
851 1.10 yamt return lang;
852 1.10 yamt
853 1.10 yamt /* 2. if LANGUAGE isn't set, see LC_ALL, LC_xxx, LANG. */
854 1.13 yamt lang = getenv("LC_ALL");
855 1.10 yamt if (!lang)
856 1.13 yamt lang = getenv(category_name);
857 1.10 yamt if (!lang)
858 1.10 yamt lang = getenv("LANG");
859 1.10 yamt
860 1.10 yamt if (!lang)
861 1.10 yamt return 0; /* error */
862 1.10 yamt
863 1.10 yamt return split_locale(lang);
864 1.10 yamt }
865 1.10 yamt
866 1.22 tshiozak static const char *
867 1.22 tshiozak get_indexed_string(const char *str, size_t len, unsigned long idx)
868 1.22 tshiozak {
869 1.22 tshiozak while (idx > 0) {
870 1.22 tshiozak if (len <= 1)
871 1.22 tshiozak return str;
872 1.22 tshiozak if (*str == '\0')
873 1.22 tshiozak idx--;
874 1.22 tshiozak if (len > 0) {
875 1.22 tshiozak str++;
876 1.22 tshiozak len--;
877 1.22 tshiozak }
878 1.22 tshiozak }
879 1.22 tshiozak return str;
880 1.22 tshiozak }
881 1.22 tshiozak
882 1.23 yamt #define _NGETTEXT_DEFAULT(msgid1, msgid2, n) \
883 1.23 yamt ((char *)__UNCONST((n) == 1 ? (msgid1) : (msgid2)))
884 1.23 yamt
885 1.1 itojun char *
886 1.1 itojun dcngettext(domainname, msgid1, msgid2, n, category)
887 1.1 itojun const char *domainname;
888 1.1 itojun const char *msgid1;
889 1.1 itojun const char *msgid2;
890 1.1 itojun unsigned long int n;
891 1.1 itojun int category;
892 1.1 itojun {
893 1.1 itojun const char *msgid;
894 1.1 itojun char path[PATH_MAX];
895 1.10 yamt const char *lpath;
896 1.1 itojun static char olpath[PATH_MAX];
897 1.6 itojun const char *cname = NULL;
898 1.1 itojun const char *v;
899 1.6 itojun static char *ocname = NULL;
900 1.6 itojun static char *odomainname = NULL;
901 1.5 itojun struct domainbinding *db;
902 1.22 tshiozak unsigned long plural_index;
903 1.22 tshiozak size_t len;
904 1.1 itojun
905 1.1 itojun if (!domainname)
906 1.9 minoura domainname = __current_domainname;
907 1.1 itojun cname = lookup_category(category);
908 1.1 itojun if (!domainname || !cname)
909 1.1 itojun goto fail;
910 1.1 itojun
911 1.10 yamt lpath = get_lang_env(cname);
912 1.10 yamt if (!lpath)
913 1.1 itojun goto fail;
914 1.19 tshiozak
915 1.9 minoura for (db = __bindings; db; db = db->next)
916 1.5 itojun if (strcmp(db->domainname, domainname) == 0)
917 1.5 itojun break;
918 1.9 minoura if (!db) {
919 1.9 minoura if (!bindtextdomain(domainname, _PATH_TEXTDOMAIN))
920 1.9 minoura goto fail;
921 1.9 minoura db = __bindings;
922 1.11 yamt }
923 1.11 yamt
924 1.11 yamt /* resolve relative path */
925 1.11 yamt /* XXX not necessary? */
926 1.11 yamt if (db->path[0] != '/') {
927 1.11 yamt char buf[PATH_MAX];
928 1.11 yamt
929 1.11 yamt if (getcwd(buf, sizeof(buf)) == 0)
930 1.11 yamt goto fail;
931 1.11 yamt if (strlcat(buf, "/", sizeof(buf)) >= sizeof(buf))
932 1.11 yamt goto fail;
933 1.11 yamt if (strlcat(buf, db->path, sizeof(buf)) >= sizeof(buf))
934 1.11 yamt goto fail;
935 1.15 itojun strlcpy(db->path, buf, sizeof(db->path));
936 1.9 minoura }
937 1.5 itojun
938 1.1 itojun /* don't bother looking it up if the values are the same */
939 1.5 itojun if (odomainname && strcmp(domainname, odomainname) == 0 &&
940 1.9 minoura ocname && strcmp(cname, ocname) == 0 && strcmp(lpath, olpath) == 0 &&
941 1.9 minoura db->mohandle.mo.mo_magic)
942 1.1 itojun goto found;
943 1.1 itojun
944 1.1 itojun /* try to find appropriate file, from $LANGUAGE */
945 1.5 itojun if (lookup_mofile(path, sizeof(path), db->path, lpath, cname,
946 1.9 minoura domainname, db) == NULL)
947 1.3 itojun goto fail;
948 1.5 itojun
949 1.5 itojun if (odomainname)
950 1.5 itojun free(odomainname);
951 1.5 itojun if (ocname)
952 1.5 itojun free(ocname);
953 1.6 itojun odomainname = strdup(domainname);
954 1.5 itojun ocname = strdup(cname);
955 1.6 itojun if (!odomainname || !ocname) {
956 1.6 itojun if (odomainname)
957 1.6 itojun free(odomainname);
958 1.6 itojun if (ocname)
959 1.6 itojun free(ocname);
960 1.6 itojun odomainname = ocname = NULL;
961 1.6 itojun }
962 1.10 yamt else
963 1.10 yamt strlcpy(olpath, lpath, sizeof(olpath));
964 1.1 itojun
965 1.1 itojun found:
966 1.22 tshiozak if (db->mohandle.mo.mo_plural) {
967 1.22 tshiozak plural_index =
968 1.22 tshiozak _gettext_calculate_plural(db->mohandle.mo.mo_plural, n);
969 1.22 tshiozak if (plural_index >= db->mohandle.mo.mo_nplurals)
970 1.22 tshiozak plural_index = 0;
971 1.22 tshiozak msgid = msgid1;
972 1.22 tshiozak } else
973 1.23 yamt msgid = _NGETTEXT_DEFAULT(msgid1, msgid2, n);
974 1.22 tshiozak
975 1.22 tshiozak if (msgid == NULL)
976 1.22 tshiozak return NULL;
977 1.22 tshiozak
978 1.22 tshiozak v = lookup(msgid, db, &len);
979 1.1 itojun if (v) {
980 1.22 tshiozak if (db->mohandle.mo.mo_plural)
981 1.22 tshiozak v = get_indexed_string(v, len, plural_index);
982 1.1 itojun /*
983 1.18 yamt * convert the translated message's encoding.
984 1.18 yamt *
985 1.18 yamt * special case:
986 1.18 yamt * a result of gettext("") shouldn't need any conversion.
987 1.1 itojun */
988 1.18 yamt if (msgid[0])
989 1.18 yamt v = __gettext_iconv(v, db);
990 1.1 itojun
991 1.1 itojun /*
992 1.1 itojun * Given the amount of printf-format security issues, it may
993 1.1 itojun * be a good idea to validate if the original msgid and the
994 1.1 itojun * translated message format string carry the same printf-like
995 1.1 itojun * format identifiers.
996 1.1 itojun */
997 1.1 itojun
998 1.1 itojun msgid = v;
999 1.1 itojun }
1000 1.1 itojun
1001 1.23 yamt return (char *)__UNCONST(msgid);
1002 1.23 yamt
1003 1.1 itojun fail:
1004 1.23 yamt return _NGETTEXT_DEFAULT(msgid1, msgid2, n);
1005 1.1 itojun }
1006