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