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