1 /* Load needed message catalogs.
2 Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 /* Tell glibc's <string.h> to provide a prototype for mempcpy().
20 This must come before <config.h> because <config.h> may include
21 <features.h>, and once <features.h> has been included, it's too late. */
23 # define _GNU_SOURCE 1
33 #include <sys/types.h>
37 # define alloca __builtin_alloca
38 # define HAVE_ALLOCA 1
40 # if defined HAVE_ALLOCA_H || defined _LIBC
53 #if defined STDC_HEADERS || defined _LIBC
57 #if defined HAVE_STRING_H || defined _LIBC
63 #if defined HAVE_UNISTD_H || defined _LIBC
68 # include <langinfo.h>
72 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
73 || (defined _LIBC && defined _POSIX_MAPPED_FILES)
74 # include <sys/mman.h>
85 # include "../locale/localeinfo.h"
88 /* @@ end of prolog @@ */
91 /* Rename the non ISO C functions. This is required by the standard
92 because some ISO C functions will require linking with this object
93 file and the name space must not be polluted. */
95 # define close __close
98 # define munmap __munmap
101 /* Names for the libintl functions are a problem. They must not clash
102 with existing names and they should follow ANSI C. But this source
103 code is also used in GNU C Library where the names have a __
104 prefix. So we have to make a difference here. */
106 # define PLURAL_PARSE __gettextparse
108 # define PLURAL_PARSE gettextparse__
111 /* For those losing systems which don't have `alloca' we have to add
112 some additional code emulating it. */
114 # define freea(p) /* nothing */
116 # define alloca(n) malloc (n)
117 # define freea(p) free (p)
120 /* We need a sign, whether a new catalog was loaded, which can be associated
121 with all translations. This is important if the translations are
122 cached by one of GCC's features. */
123 int _nl_msg_cat_cntr
;
125 #if defined __GNUC__ \
126 || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
128 /* These structs are the constant expression for the germanic plural
129 form determination. It represents the expression "n != 1". */
130 static const struct expression plvar
=
135 static const struct expression plone
=
144 static struct expression germanic_plural
=
147 .operation
= not_equal
,
152 [0] = (struct expression
*) &plvar
,
153 [1] = (struct expression
*) &plone
158 # define INIT_GERMANIC_PLURAL()
162 /* For compilers without support for ISO C 99 struct/union initializers:
163 Initialization at run-time. */
165 static struct expression plvar
;
166 static struct expression plone
;
167 static struct expression germanic_plural
;
170 init_germanic_plural ()
172 if (plone
.val
.num
== 0)
175 plvar
.operation
= var
;
178 plone
.operation
= num
;
181 germanic_plural
.nargs
= 2;
182 germanic_plural
.operation
= not_equal
;
183 germanic_plural
.val
.args
[0] = &plvar
;
184 germanic_plural
.val
.args
[1] = &plone
;
188 # define INIT_GERMANIC_PLURAL() init_germanic_plural ()
193 /* Load the message catalogs specified by FILENAME. If it is no valid
194 message catalog do nothing. */
197 _nl_load_domain (domain_file
)
198 struct loaded_l10nfile
*domain_file
;
207 struct mo_file_header
*data
= (struct mo_file_header
*) -1;
209 struct loaded_domain
*domain
;
213 domain_file
->decided
= 1;
214 domain_file
->data
= NULL
;
216 /* If the record does not represent a valid locale the FILENAME
217 might be NULL. This can happen when according to the given
218 specification the locale file name is different for XPG and CEN
220 if (domain_file
->filename
== NULL
)
223 /* Try to open the addressed file. */
224 fd
= open (domain_file
->filename
, O_RDONLY
);
228 /* We must know about the size of the file. */
231 __builtin_expect (fstat64 (fd
, &st
) != 0, 0)
233 __builtin_expect (fstat (fd
, &st
) != 0, 0)
235 || __builtin_expect ((size
= (size_t) st
.st_size
) != st
.st_size
, 0)
236 || __builtin_expect (size
< sizeof (struct mo_file_header
), 0))
238 /* Something went wrong. */
244 /* Now we are ready to load the file. If mmap() is available we try
245 this first. If not available or it failed we try to load it. */
246 data
= (struct mo_file_header
*) mmap (NULL
, size
, PROT_READ
,
249 if (__builtin_expect (data
!= (struct mo_file_header
*) -1, 1))
251 /* mmap() call was successful. */
257 /* If the data is not yet available (i.e. mmap'ed) we try to load
259 if (data
== (struct mo_file_header
*) -1)
264 data
= (struct mo_file_header
*) malloc (size
);
269 read_ptr
= (char *) data
;
272 long int nb
= (long int) read (fd
, read_ptr
, to_read
);
276 if (nb
== -1 && errno
== EINTR
)
290 /* Using the magic number we can test whether it really is a message
292 if (__builtin_expect (data
->magic
!= _MAGIC
&& data
->magic
!= _MAGIC_SWAPPED
,
295 /* The magic number is wrong: not a message catalog file. */
298 munmap ((caddr_t
) data
, size
);
305 domain
= (struct loaded_domain
*) malloc (sizeof (struct loaded_domain
));
308 domain_file
->data
= domain
;
310 domain
->data
= (char *) data
;
311 domain
->use_mmap
= use_mmap
;
312 domain
->mmap_size
= size
;
313 domain
->must_swap
= data
->magic
!= _MAGIC
;
315 /* Fill in the information about the available tables. */
316 switch (W (domain
->must_swap
, data
->revision
))
319 domain
->nstrings
= W (domain
->must_swap
, data
->nstrings
);
320 domain
->orig_tab
= (struct string_desc
*)
321 ((char *) data
+ W (domain
->must_swap
, data
->orig_tab_offset
));
322 domain
->trans_tab
= (struct string_desc
*)
323 ((char *) data
+ W (domain
->must_swap
, data
->trans_tab_offset
));
324 domain
->hash_size
= W (domain
->must_swap
, data
->hash_tab_size
);
325 domain
->hash_tab
= (nls_uint32
*)
326 ((char *) data
+ W (domain
->must_swap
, data
->hash_tab_offset
));
329 /* This is an invalid revision. */
332 munmap ((caddr_t
) data
, size
);
337 domain_file
->data
= NULL
;
341 /* Now find out about the character set the file is encoded with.
342 This can be found (in textual form) in the entry "". If this
343 entry does not exist or if this does not contain the `charset='
344 information, we will assume the charset matches the one the
345 current locale and we don't have to perform any conversion. */
347 domain
->conv
= (__gconv_t
) -1;
350 domain
->conv
= (iconv_t
) -1;
353 domain
->conv_tab
= NULL
;
354 nullentry
= _nl_find_msg (domain_file
, "", &nullentrylen
);
355 if (nullentry
!= NULL
)
357 #if defined _LIBC || HAVE_ICONV
358 const char *charsetstr
;
360 charsetstr
= strstr (nullentry
, "charset=");
361 if (charsetstr
!= NULL
)
365 const char *outcharset
;
367 charsetstr
+= strlen ("charset=");
368 len
= strcspn (charsetstr
, " \t\n");
370 charset
= (char *) alloca (len
+ 1);
371 # if defined _LIBC || HAVE_MEMPCPY
372 *((char *) mempcpy (charset
, charsetstr
, len
)) = '\0';
374 memcpy (charset
, charsetstr
, len
);
378 /* The output charset should normally be determined by the
379 locale. But sometimes the locale is not used or not correctly
380 set up, so we provide a possibility for the user to override
381 this. Moreover, the value specified through
382 bind_textdomain_codeset overrides both. */
383 if (domain_file
->domainbinding
!= NULL
384 && domain_file
->domainbinding
->codeset
!= NULL
)
385 outcharset
= domain_file
->domainbinding
->codeset
;
388 outcharset
= getenv ("OUTPUT_CHARSET");
389 if (outcharset
== NULL
|| outcharset
[0] == '\0')
392 outcharset
= (*_nl_current
[LC_CTYPE
])->values
[_NL_ITEM_INDEX (CODESET
)].string
;
395 extern const char *locale_charset (void);
396 outcharset
= locale_charset ();
403 /* We always want to use transliteration. */
404 outcharset
= norm_add_slashes (outcharset
, "TRANSLIT");
405 charset
= norm_add_slashes (charset
, NULL
);
406 if (__gconv_open (outcharset
, charset
, &domain
->conv
,
409 domain
->conv
= (__gconv_t
) -1;
412 domain
->conv
= iconv_open (outcharset
, charset
);
418 #endif /* _LIBC || HAVE_ICONV */
421 /* Also look for a plural specification. */
422 if (nullentry
!= NULL
)
425 const char *nplurals
;
427 plural
= strstr (nullentry
, "plural=");
428 nplurals
= strstr (nullentry
, "nplurals=");
429 if (plural
== NULL
|| nplurals
== NULL
)
433 /* First get the number. */
436 struct parse_args args
;
439 while (*nplurals
!= '\0' && isspace (*nplurals
))
441 #if defined HAVE_STRTOUL || defined _LIBC
442 n
= strtoul (nplurals
, &endp
, 10);
444 for (endp
= nplurals
, n
= 0; *endp
>= '0' && *endp
<= '9'; endp
++)
445 n
= n
* 10 + (*endp
- '0');
447 domain
->nplurals
= n
;
448 if (nplurals
== endp
)
451 /* Due to the restrictions bison imposes onto the interface of the
452 scanner function we have to put the input string and the result
453 passed up from the parser into the same structure which address
454 is passed down to the parser. */
457 if (PLURAL_PARSE (&args
) != 0)
459 domain
->plural
= args
.res
;
464 /* By default we are using the Germanic form: singular form only
465 for `one', the plural form otherwise. Yes, this is also what
466 English is using since English is a Germanic language. */
468 INIT_GERMANIC_PLURAL ();
469 domain
->plural
= &germanic_plural
;
470 domain
->nplurals
= 2;
478 _nl_unload_domain (domain
)
479 struct loaded_domain
*domain
;
481 if (domain
->plural
!= &germanic_plural
)
482 __gettext_free_exp (domain
->plural
);
484 if (domain
->conv_tab
!= NULL
&& domain
->conv_tab
!= (char **) -1)
485 free (domain
->conv_tab
);
487 if (domain
->conv
!= (__gconv_t
) -1)
488 __gconv_close (domain
->conv
);
490 # ifdef _POSIX_MAPPED_FILES
491 if (domain
->use_mmap
)
492 munmap ((caddr_t
) domain
->data
, domain
->mmap_size
);
494 # endif /* _POSIX_MAPPED_FILES */
495 free ((void *) domain
->data
);