loadmsgcat.c 15 KB

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