1*f7313695Schristos /* $NetBSD: loadmsgcat.c,v 1.1.1.1 2016/01/10 21:36:18 christos Exp $ */
2*f7313695Schristos
3*f7313695Schristos /* Load needed message catalogs.
4*f7313695Schristos Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
5*f7313695Schristos
6*f7313695Schristos This program is free software; you can redistribute it and/or modify it
7*f7313695Schristos under the terms of the GNU Library General Public License as published
8*f7313695Schristos by the Free Software Foundation; either version 2, or (at your option)
9*f7313695Schristos any later version.
10*f7313695Schristos
11*f7313695Schristos This program is distributed in the hope that it will be useful,
12*f7313695Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
13*f7313695Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14*f7313695Schristos Library General Public License for more details.
15*f7313695Schristos
16*f7313695Schristos You should have received a copy of the GNU Library General Public
17*f7313695Schristos License along with this program; if not, write to the Free Software
18*f7313695Schristos Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19*f7313695Schristos USA. */
20*f7313695Schristos
21*f7313695Schristos /* Tell glibc's <string.h> to provide a prototype for mempcpy().
22*f7313695Schristos This must come before <config.h> because <config.h> may include
23*f7313695Schristos <features.h>, and once <features.h> has been included, it's too late. */
24*f7313695Schristos #ifndef _GNU_SOURCE
25*f7313695Schristos # define _GNU_SOURCE 1
26*f7313695Schristos #endif
27*f7313695Schristos
28*f7313695Schristos #ifdef HAVE_CONFIG_H
29*f7313695Schristos # include <config.h>
30*f7313695Schristos #endif
31*f7313695Schristos
32*f7313695Schristos #include <ctype.h>
33*f7313695Schristos #include <errno.h>
34*f7313695Schristos #include <fcntl.h>
35*f7313695Schristos #include <sys/types.h>
36*f7313695Schristos #include <sys/stat.h>
37*f7313695Schristos
38*f7313695Schristos #ifdef __GNUC__
39*f7313695Schristos # define alloca __builtin_alloca
40*f7313695Schristos # define HAVE_ALLOCA 1
41*f7313695Schristos #else
42*f7313695Schristos # if defined HAVE_ALLOCA_H || defined _LIBC
43*f7313695Schristos # include <alloca.h>
44*f7313695Schristos # else
45*f7313695Schristos # ifdef _AIX
46*f7313695Schristos #pragma alloca
47*f7313695Schristos # else
48*f7313695Schristos # ifndef alloca
49*f7313695Schristos char *alloca ();
50*f7313695Schristos # endif
51*f7313695Schristos # endif
52*f7313695Schristos # endif
53*f7313695Schristos #endif
54*f7313695Schristos
55*f7313695Schristos #include <stdlib.h>
56*f7313695Schristos #include <string.h>
57*f7313695Schristos
58*f7313695Schristos #if defined HAVE_UNISTD_H || defined _LIBC
59*f7313695Schristos # include <unistd.h>
60*f7313695Schristos #endif
61*f7313695Schristos
62*f7313695Schristos #ifdef _LIBC
63*f7313695Schristos # include <langinfo.h>
64*f7313695Schristos # include <locale.h>
65*f7313695Schristos #endif
66*f7313695Schristos
67*f7313695Schristos #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
68*f7313695Schristos || (defined _LIBC && defined _POSIX_MAPPED_FILES)
69*f7313695Schristos # include <sys/mman.h>
70*f7313695Schristos # undef HAVE_MMAP
71*f7313695Schristos # define HAVE_MMAP 1
72*f7313695Schristos #else
73*f7313695Schristos # undef HAVE_MMAP
74*f7313695Schristos #endif
75*f7313695Schristos
76*f7313695Schristos #include "gmo.h"
77*f7313695Schristos #include "gettextP.h"
78*f7313695Schristos #include "plural-exp.h"
79*f7313695Schristos
80*f7313695Schristos #ifdef _LIBC
81*f7313695Schristos # include "../locale/localeinfo.h"
82*f7313695Schristos #endif
83*f7313695Schristos
84*f7313695Schristos /* @@ end of prolog @@ */
85*f7313695Schristos
86*f7313695Schristos #ifdef _LIBC
87*f7313695Schristos /* Rename the non ISO C functions. This is required by the standard
88*f7313695Schristos because some ISO C functions will require linking with this object
89*f7313695Schristos file and the name space must not be polluted. */
90*f7313695Schristos # define open __open
91*f7313695Schristos # define close __close
92*f7313695Schristos # define read __read
93*f7313695Schristos # define mmap __mmap
94*f7313695Schristos # define munmap __munmap
95*f7313695Schristos #endif
96*f7313695Schristos
97*f7313695Schristos /* For those losing systems which don't have `alloca' we have to add
98*f7313695Schristos some additional code emulating it. */
99*f7313695Schristos #ifdef HAVE_ALLOCA
100*f7313695Schristos # define freea(p) /* nothing */
101*f7313695Schristos #else
102*f7313695Schristos # define alloca(n) malloc (n)
103*f7313695Schristos # define freea(p) free (p)
104*f7313695Schristos #endif
105*f7313695Schristos
106*f7313695Schristos /* For systems that distinguish between text and binary I/O.
107*f7313695Schristos O_BINARY is usually declared in <fcntl.h>. */
108*f7313695Schristos #if !defined O_BINARY && defined _O_BINARY
109*f7313695Schristos /* For MSC-compatible compilers. */
110*f7313695Schristos # define O_BINARY _O_BINARY
111*f7313695Schristos # define O_TEXT _O_TEXT
112*f7313695Schristos #endif
113*f7313695Schristos #ifdef __BEOS__
114*f7313695Schristos /* BeOS 5 has O_BINARY and O_TEXT, but they have no effect. */
115*f7313695Schristos # undef O_BINARY
116*f7313695Schristos # undef O_TEXT
117*f7313695Schristos #endif
118*f7313695Schristos /* On reasonable systems, binary I/O is the default. */
119*f7313695Schristos #ifndef O_BINARY
120*f7313695Schristos # define O_BINARY 0
121*f7313695Schristos #endif
122*f7313695Schristos
123*f7313695Schristos /* We need a sign, whether a new catalog was loaded, which can be associated
124*f7313695Schristos with all translations. This is important if the translations are
125*f7313695Schristos cached by one of GCC's features. */
126*f7313695Schristos int _nl_msg_cat_cntr;
127*f7313695Schristos
128*f7313695Schristos
129*f7313695Schristos /* Initialize the codeset dependent parts of an opened message catalog.
130*f7313695Schristos Return the header entry. */
131*f7313695Schristos const char *
132*f7313695Schristos internal_function
_nl_init_domain_conv(domain_file,domain,domainbinding)133*f7313695Schristos _nl_init_domain_conv (domain_file, domain, domainbinding)
134*f7313695Schristos struct loaded_l10nfile *domain_file;
135*f7313695Schristos struct loaded_domain *domain;
136*f7313695Schristos struct binding *domainbinding;
137*f7313695Schristos {
138*f7313695Schristos /* Find out about the character set the file is encoded with.
139*f7313695Schristos This can be found (in textual form) in the entry "". If this
140*f7313695Schristos entry does not exist or if this does not contain the `charset='
141*f7313695Schristos information, we will assume the charset matches the one the
142*f7313695Schristos current locale and we don't have to perform any conversion. */
143*f7313695Schristos char *nullentry;
144*f7313695Schristos size_t nullentrylen;
145*f7313695Schristos
146*f7313695Schristos /* Preinitialize fields, to avoid recursion during _nl_find_msg. */
147*f7313695Schristos domain->codeset_cntr =
148*f7313695Schristos (domainbinding != NULL ? domainbinding->codeset_cntr : 0);
149*f7313695Schristos #ifdef _LIBC
150*f7313695Schristos domain->conv = (__gconv_t) -1;
151*f7313695Schristos #else
152*f7313695Schristos # if HAVE_ICONV
153*f7313695Schristos domain->conv = (iconv_t) -1;
154*f7313695Schristos # endif
155*f7313695Schristos #endif
156*f7313695Schristos domain->conv_tab = NULL;
157*f7313695Schristos
158*f7313695Schristos /* Get the header entry. */
159*f7313695Schristos nullentry = _nl_find_msg (domain_file, domainbinding, "", &nullentrylen);
160*f7313695Schristos
161*f7313695Schristos if (nullentry != NULL)
162*f7313695Schristos {
163*f7313695Schristos #if defined _LIBC || HAVE_ICONV
164*f7313695Schristos const char *charsetstr;
165*f7313695Schristos
166*f7313695Schristos charsetstr = strstr (nullentry, "charset=");
167*f7313695Schristos if (charsetstr != NULL)
168*f7313695Schristos {
169*f7313695Schristos size_t len;
170*f7313695Schristos char *charset;
171*f7313695Schristos const char *outcharset;
172*f7313695Schristos
173*f7313695Schristos charsetstr += strlen ("charset=");
174*f7313695Schristos len = strcspn (charsetstr, " \t\n");
175*f7313695Schristos
176*f7313695Schristos charset = (char *) alloca (len + 1);
177*f7313695Schristos # if defined _LIBC || HAVE_MEMPCPY
178*f7313695Schristos *((char *) mempcpy (charset, charsetstr, len)) = '\0';
179*f7313695Schristos # else
180*f7313695Schristos memcpy (charset, charsetstr, len);
181*f7313695Schristos charset[len] = '\0';
182*f7313695Schristos # endif
183*f7313695Schristos
184*f7313695Schristos /* The output charset should normally be determined by the
185*f7313695Schristos locale. But sometimes the locale is not used or not correctly
186*f7313695Schristos set up, so we provide a possibility for the user to override
187*f7313695Schristos this. Moreover, the value specified through
188*f7313695Schristos bind_textdomain_codeset overrides both. */
189*f7313695Schristos if (domainbinding != NULL && domainbinding->codeset != NULL)
190*f7313695Schristos outcharset = domainbinding->codeset;
191*f7313695Schristos else
192*f7313695Schristos {
193*f7313695Schristos outcharset = getenv ("OUTPUT_CHARSET");
194*f7313695Schristos if (outcharset == NULL || outcharset[0] == '\0')
195*f7313695Schristos {
196*f7313695Schristos # ifdef _LIBC
197*f7313695Schristos outcharset = (*_nl_current[LC_CTYPE])->values[_NL_ITEM_INDEX (CODESET)].string;
198*f7313695Schristos # else
199*f7313695Schristos # if HAVE_ICONV
200*f7313695Schristos extern const char *locale_charset PARAMS ((void));
201*f7313695Schristos outcharset = locale_charset ();
202*f7313695Schristos # endif
203*f7313695Schristos # endif
204*f7313695Schristos }
205*f7313695Schristos }
206*f7313695Schristos
207*f7313695Schristos # ifdef _LIBC
208*f7313695Schristos /* We always want to use transliteration. */
209*f7313695Schristos outcharset = norm_add_slashes (outcharset, "TRANSLIT");
210*f7313695Schristos charset = norm_add_slashes (charset, NULL);
211*f7313695Schristos if (__gconv_open (outcharset, charset, &domain->conv,
212*f7313695Schristos GCONV_AVOID_NOCONV)
213*f7313695Schristos != __GCONV_OK)
214*f7313695Schristos domain->conv = (__gconv_t) -1;
215*f7313695Schristos # else
216*f7313695Schristos # if HAVE_ICONV
217*f7313695Schristos /* When using GNU libc >= 2.2 or GNU libiconv >= 1.5,
218*f7313695Schristos we want to use transliteration. */
219*f7313695Schristos # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 \
220*f7313695Schristos || _LIBICONV_VERSION >= 0x0105
221*f7313695Schristos len = strlen (outcharset);
222*f7313695Schristos {
223*f7313695Schristos char *tmp = (char *) alloca (len + 10 + 1);
224*f7313695Schristos memcpy (tmp, outcharset, len);
225*f7313695Schristos memcpy (tmp + len, "//TRANSLIT", 10 + 1);
226*f7313695Schristos outcharset = tmp;
227*f7313695Schristos }
228*f7313695Schristos # endif
229*f7313695Schristos domain->conv = iconv_open (outcharset, charset);
230*f7313695Schristos # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 \
231*f7313695Schristos || _LIBICONV_VERSION >= 0x0105
232*f7313695Schristos freea (outcharset);
233*f7313695Schristos # endif
234*f7313695Schristos # endif
235*f7313695Schristos # endif
236*f7313695Schristos
237*f7313695Schristos freea (charset);
238*f7313695Schristos }
239*f7313695Schristos #endif /* _LIBC || HAVE_ICONV */
240*f7313695Schristos }
241*f7313695Schristos
242*f7313695Schristos return nullentry;
243*f7313695Schristos }
244*f7313695Schristos
245*f7313695Schristos /* Frees the codeset dependent parts of an opened message catalog. */
246*f7313695Schristos void
247*f7313695Schristos internal_function
_nl_free_domain_conv(domain)248*f7313695Schristos _nl_free_domain_conv (domain)
249*f7313695Schristos struct loaded_domain *domain;
250*f7313695Schristos {
251*f7313695Schristos if (domain->conv_tab != NULL && domain->conv_tab != (char **) -1)
252*f7313695Schristos free (domain->conv_tab);
253*f7313695Schristos
254*f7313695Schristos #ifdef _LIBC
255*f7313695Schristos if (domain->conv != (__gconv_t) -1)
256*f7313695Schristos __gconv_close (domain->conv);
257*f7313695Schristos #else
258*f7313695Schristos # if HAVE_ICONV
259*f7313695Schristos if (domain->conv != (iconv_t) -1)
260*f7313695Schristos iconv_close (domain->conv);
261*f7313695Schristos # endif
262*f7313695Schristos #endif
263*f7313695Schristos }
264*f7313695Schristos
265*f7313695Schristos /* Load the message catalogs specified by FILENAME. If it is no valid
266*f7313695Schristos message catalog do nothing. */
267*f7313695Schristos void
268*f7313695Schristos internal_function
_nl_load_domain(domain_file,domainbinding)269*f7313695Schristos _nl_load_domain (domain_file, domainbinding)
270*f7313695Schristos struct loaded_l10nfile *domain_file;
271*f7313695Schristos struct binding *domainbinding;
272*f7313695Schristos {
273*f7313695Schristos int fd;
274*f7313695Schristos size_t size;
275*f7313695Schristos #ifdef _LIBC
276*f7313695Schristos struct stat64 st;
277*f7313695Schristos #else
278*f7313695Schristos struct stat st;
279*f7313695Schristos #endif
280*f7313695Schristos struct mo_file_header *data = (struct mo_file_header *) -1;
281*f7313695Schristos int use_mmap = 0;
282*f7313695Schristos struct loaded_domain *domain;
283*f7313695Schristos const char *nullentry;
284*f7313695Schristos
285*f7313695Schristos domain_file->decided = 1;
286*f7313695Schristos domain_file->data = NULL;
287*f7313695Schristos
288*f7313695Schristos /* Note that it would be useless to store domainbinding in domain_file
289*f7313695Schristos because domainbinding might be == NULL now but != NULL later (after
290*f7313695Schristos a call to bind_textdomain_codeset). */
291*f7313695Schristos
292*f7313695Schristos /* If the record does not represent a valid locale the FILENAME
293*f7313695Schristos might be NULL. This can happen when according to the given
294*f7313695Schristos specification the locale file name is different for XPG and CEN
295*f7313695Schristos syntax. */
296*f7313695Schristos if (domain_file->filename == NULL)
297*f7313695Schristos return;
298*f7313695Schristos
299*f7313695Schristos /* Try to open the addressed file. */
300*f7313695Schristos fd = open (domain_file->filename, O_RDONLY | O_BINARY);
301*f7313695Schristos if (fd == -1)
302*f7313695Schristos return;
303*f7313695Schristos
304*f7313695Schristos /* We must know about the size of the file. */
305*f7313695Schristos if (
306*f7313695Schristos #ifdef _LIBC
307*f7313695Schristos __builtin_expect (fstat64 (fd, &st) != 0, 0)
308*f7313695Schristos #else
309*f7313695Schristos __builtin_expect (fstat (fd, &st) != 0, 0)
310*f7313695Schristos #endif
311*f7313695Schristos || __builtin_expect ((size = (size_t) st.st_size) != st.st_size, 0)
312*f7313695Schristos || __builtin_expect (size < sizeof (struct mo_file_header), 0))
313*f7313695Schristos {
314*f7313695Schristos /* Something went wrong. */
315*f7313695Schristos close (fd);
316*f7313695Schristos return;
317*f7313695Schristos }
318*f7313695Schristos
319*f7313695Schristos #ifdef HAVE_MMAP
320*f7313695Schristos /* Now we are ready to load the file. If mmap() is available we try
321*f7313695Schristos this first. If not available or it failed we try to load it. */
322*f7313695Schristos data = (struct mo_file_header *) mmap (NULL, size, PROT_READ,
323*f7313695Schristos MAP_PRIVATE, fd, 0);
324*f7313695Schristos
325*f7313695Schristos if (__builtin_expect (data != (struct mo_file_header *) -1, 1))
326*f7313695Schristos {
327*f7313695Schristos /* mmap() call was successful. */
328*f7313695Schristos close (fd);
329*f7313695Schristos use_mmap = 1;
330*f7313695Schristos }
331*f7313695Schristos #endif
332*f7313695Schristos
333*f7313695Schristos /* If the data is not yet available (i.e. mmap'ed) we try to load
334*f7313695Schristos it manually. */
335*f7313695Schristos if (data == (struct mo_file_header *) -1)
336*f7313695Schristos {
337*f7313695Schristos size_t to_read;
338*f7313695Schristos char *read_ptr;
339*f7313695Schristos
340*f7313695Schristos data = (struct mo_file_header *) malloc (size);
341*f7313695Schristos if (data == NULL)
342*f7313695Schristos return;
343*f7313695Schristos
344*f7313695Schristos to_read = size;
345*f7313695Schristos read_ptr = (char *) data;
346*f7313695Schristos do
347*f7313695Schristos {
348*f7313695Schristos long int nb = (long int) read (fd, read_ptr, to_read);
349*f7313695Schristos if (nb <= 0)
350*f7313695Schristos {
351*f7313695Schristos #ifdef EINTR
352*f7313695Schristos if (nb == -1 && errno == EINTR)
353*f7313695Schristos continue;
354*f7313695Schristos #endif
355*f7313695Schristos close (fd);
356*f7313695Schristos return;
357*f7313695Schristos }
358*f7313695Schristos read_ptr += nb;
359*f7313695Schristos to_read -= nb;
360*f7313695Schristos }
361*f7313695Schristos while (to_read > 0);
362*f7313695Schristos
363*f7313695Schristos close (fd);
364*f7313695Schristos }
365*f7313695Schristos
366*f7313695Schristos /* Using the magic number we can test whether it really is a message
367*f7313695Schristos catalog file. */
368*f7313695Schristos if (__builtin_expect (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED,
369*f7313695Schristos 0))
370*f7313695Schristos {
371*f7313695Schristos /* The magic number is wrong: not a message catalog file. */
372*f7313695Schristos #ifdef HAVE_MMAP
373*f7313695Schristos if (use_mmap)
374*f7313695Schristos munmap ((caddr_t) data, size);
375*f7313695Schristos else
376*f7313695Schristos #endif
377*f7313695Schristos free (data);
378*f7313695Schristos return;
379*f7313695Schristos }
380*f7313695Schristos
381*f7313695Schristos domain = (struct loaded_domain *) malloc (sizeof (struct loaded_domain));
382*f7313695Schristos if (domain == NULL)
383*f7313695Schristos return;
384*f7313695Schristos domain_file->data = domain;
385*f7313695Schristos
386*f7313695Schristos domain->data = (char *) data;
387*f7313695Schristos domain->use_mmap = use_mmap;
388*f7313695Schristos domain->mmap_size = size;
389*f7313695Schristos domain->must_swap = data->magic != _MAGIC;
390*f7313695Schristos
391*f7313695Schristos /* Fill in the information about the available tables. */
392*f7313695Schristos switch (W (domain->must_swap, data->revision))
393*f7313695Schristos {
394*f7313695Schristos case 0:
395*f7313695Schristos domain->nstrings = W (domain->must_swap, data->nstrings);
396*f7313695Schristos domain->orig_tab = (struct string_desc *)
397*f7313695Schristos ((char *) data + W (domain->must_swap, data->orig_tab_offset));
398*f7313695Schristos domain->trans_tab = (struct string_desc *)
399*f7313695Schristos ((char *) data + W (domain->must_swap, data->trans_tab_offset));
400*f7313695Schristos domain->hash_size = W (domain->must_swap, data->hash_tab_size);
401*f7313695Schristos domain->hash_tab = (nls_uint32 *)
402*f7313695Schristos ((char *) data + W (domain->must_swap, data->hash_tab_offset));
403*f7313695Schristos break;
404*f7313695Schristos default:
405*f7313695Schristos /* This is an invalid revision. */
406*f7313695Schristos #ifdef HAVE_MMAP
407*f7313695Schristos if (use_mmap)
408*f7313695Schristos munmap ((caddr_t) data, size);
409*f7313695Schristos else
410*f7313695Schristos #endif
411*f7313695Schristos free (data);
412*f7313695Schristos free (domain);
413*f7313695Schristos domain_file->data = NULL;
414*f7313695Schristos return;
415*f7313695Schristos }
416*f7313695Schristos
417*f7313695Schristos /* Now initialize the character set converter from the character set
418*f7313695Schristos the file is encoded with (found in the header entry) to the domain's
419*f7313695Schristos specified character set or the locale's character set. */
420*f7313695Schristos nullentry = _nl_init_domain_conv (domain_file, domain, domainbinding);
421*f7313695Schristos
422*f7313695Schristos /* Also look for a plural specification. */
423*f7313695Schristos EXTRACT_PLURAL_EXPRESSION (nullentry, &domain->plural, &domain->nplurals);
424*f7313695Schristos }
425*f7313695Schristos
426*f7313695Schristos
427*f7313695Schristos #ifdef _LIBC
428*f7313695Schristos void
429*f7313695Schristos internal_function
_nl_unload_domain(domain)430*f7313695Schristos _nl_unload_domain (domain)
431*f7313695Schristos struct loaded_domain *domain;
432*f7313695Schristos {
433*f7313695Schristos if (domain->plural != &__gettext_germanic_plural)
434*f7313695Schristos __gettext_free_exp (domain->plural);
435*f7313695Schristos
436*f7313695Schristos _nl_free_domain_conv (domain);
437*f7313695Schristos
438*f7313695Schristos # ifdef _POSIX_MAPPED_FILES
439*f7313695Schristos if (domain->use_mmap)
440*f7313695Schristos munmap ((caddr_t) domain->data, domain->mmap_size);
441*f7313695Schristos else
442*f7313695Schristos # endif /* _POSIX_MAPPED_FILES */
443*f7313695Schristos free ((void *) domain->data);
444*f7313695Schristos
445*f7313695Schristos free (domain);
446*f7313695Schristos }
447*f7313695Schristos #endif
448