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