dcigettext.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /* Implementation of the internal dcigettext function.
  2. Copyright (C) 1995-1999, 2000-2003 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published
  5. by 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 GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  14. USA. */
  15. /* Tell glibc's <string.h> to provide a prototype for mempcpy().
  16. This must come before <config.h> because <config.h> may include
  17. <features.h>, and once <features.h> has been included, it's too late. */
  18. #ifndef _GNU_SOURCE
  19. # define _GNU_SOURCE 1
  20. #endif
  21. #ifdef HAVE_CONFIG_H
  22. # include <config.h>
  23. #endif
  24. #include <sys/types.h>
  25. #ifdef __GNUC__
  26. # define alloca __builtin_alloca
  27. # define HAVE_ALLOCA 1
  28. #else
  29. # ifdef _MSC_VER
  30. # include <malloc.h>
  31. # define alloca _alloca
  32. # else
  33. # if defined HAVE_ALLOCA_H || defined _LIBC
  34. # include <alloca.h>
  35. # else
  36. # ifdef _AIX
  37. #pragma alloca
  38. # else
  39. # ifndef alloca
  40. char *alloca ();
  41. # endif
  42. # endif
  43. # endif
  44. # endif
  45. #endif
  46. #include <errno.h>
  47. #ifndef errno
  48. extern int errno;
  49. #endif
  50. #ifndef __set_errno
  51. # define __set_errno(val) errno = (val)
  52. #endif
  53. #include <stddef.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #if defined HAVE_UNISTD_H || defined _LIBC
  57. # include <unistd.h>
  58. #endif
  59. #include <locale.h>
  60. #ifdef _LIBC
  61. /* Guess whether integer division by zero raises signal SIGFPE.
  62. Set to 1 only if you know for sure. In case of doubt, set to 0. */
  63. # if defined __alpha__ || defined __arm__ || defined __i386__ \
  64. || defined __m68k__ || defined __s390__
  65. # define INTDIV0_RAISES_SIGFPE 1
  66. # else
  67. # define INTDIV0_RAISES_SIGFPE 0
  68. # endif
  69. #endif
  70. #if !INTDIV0_RAISES_SIGFPE
  71. # include <signal.h>
  72. #endif
  73. #if defined HAVE_SYS_PARAM_H || defined _LIBC
  74. # include <sys/param.h>
  75. #endif
  76. #include "gettextP.h"
  77. #include "plural-exp.h"
  78. #ifdef _LIBC
  79. # include <libintl.h>
  80. #else
  81. # include "libgnuintl.h"
  82. #endif
  83. #include "hash-string.h"
  84. /* Thread safetyness. */
  85. #ifdef _LIBC
  86. # include <bits/libc-lock.h>
  87. #else
  88. /* Provide dummy implementation if this is outside glibc. */
  89. # define __libc_lock_define_initialized(CLASS, NAME)
  90. # define __libc_lock_lock(NAME)
  91. # define __libc_lock_unlock(NAME)
  92. # define __libc_rwlock_define_initialized(CLASS, NAME)
  93. # define __libc_rwlock_rdlock(NAME)
  94. # define __libc_rwlock_unlock(NAME)
  95. #endif
  96. /* Alignment of types. */
  97. #if defined __GNUC__ && __GNUC__ >= 2
  98. # define alignof(TYPE) __alignof__ (TYPE)
  99. #else
  100. # define alignof(TYPE) \
  101. ((int) &((struct { char dummy1; TYPE dummy2; } *) 0)->dummy2)
  102. #endif
  103. /* The internal variables in the standalone libintl.a must have different
  104. names than the internal variables in GNU libc, otherwise programs
  105. using libintl.a cannot be linked statically. */
  106. #if !defined _LIBC
  107. # define _nl_default_default_domain libintl_nl_default_default_domain
  108. # define _nl_current_default_domain libintl_nl_current_default_domain
  109. # define _nl_default_dirname libintl_nl_default_dirname
  110. # define _nl_domain_bindings libintl_nl_domain_bindings
  111. #endif
  112. /* Some compilers, like SunOS4 cc, don't have offsetof in <stddef.h>. */
  113. #ifndef offsetof
  114. # define offsetof(type,ident) ((size_t)&(((type*)0)->ident))
  115. #endif
  116. /* @@ end of prolog @@ */
  117. #ifdef _LIBC
  118. /* Rename the non ANSI C functions. This is required by the standard
  119. because some ANSI C functions will require linking with this object
  120. file and the name space must not be polluted. */
  121. # define getcwd __getcwd
  122. # ifndef stpcpy
  123. # define stpcpy __stpcpy
  124. # endif
  125. # define tfind __tfind
  126. #else
  127. # if !defined HAVE_GETCWD
  128. char *getwd ();
  129. # define getcwd(buf, max) getwd (buf)
  130. # else
  131. char *getcwd ();
  132. # endif
  133. # ifndef HAVE_STPCPY
  134. static char *stpcpy PARAMS ((char *dest, const char *src));
  135. # endif
  136. # ifndef HAVE_MEMPCPY
  137. static void *mempcpy PARAMS ((void *dest, const void *src, size_t n));
  138. # endif
  139. #endif
  140. /* Amount to increase buffer size by in each try. */
  141. #define PATH_INCR 32
  142. /* The following is from pathmax.h. */
  143. /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
  144. PATH_MAX but might cause redefinition warnings when sys/param.h is
  145. later included (as on MORE/BSD 4.3). */
  146. #if defined _POSIX_VERSION || (defined HAVE_LIMITS_H && !defined __GNUC__)
  147. # include <limits.h>
  148. #endif
  149. #ifndef _POSIX_PATH_MAX
  150. # define _POSIX_PATH_MAX 255
  151. #endif
  152. #if !defined PATH_MAX && defined _PC_PATH_MAX
  153. # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
  154. #endif
  155. /* Don't include sys/param.h if it already has been. */
  156. #if defined HAVE_SYS_PARAM_H && !defined PATH_MAX && !defined MAXPATHLEN
  157. # include <sys/param.h>
  158. #endif
  159. #if !defined PATH_MAX && defined MAXPATHLEN
  160. # define PATH_MAX MAXPATHLEN
  161. #endif
  162. #ifndef PATH_MAX
  163. # define PATH_MAX _POSIX_PATH_MAX
  164. #endif
  165. /* Pathname support.
  166. ISSLASH(C) tests whether C is a directory separator character.
  167. IS_ABSOLUTE_PATH(P) tests whether P is an absolute path. If it is not,
  168. it may be concatenated to a directory pathname.
  169. IS_PATH_WITH_DIR(P) tests whether P contains a directory specification.
  170. */
  171. #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
  172. /* Win32, OS/2, DOS */
  173. # define ISSLASH(C) ((C) == '/' || (C) == '\\')
  174. # define HAS_DEVICE(P) \
  175. ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
  176. && (P)[1] == ':')
  177. # define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P))
  178. # define IS_PATH_WITH_DIR(P) \
  179. (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
  180. #else
  181. /* Unix */
  182. # define ISSLASH(C) ((C) == '/')
  183. # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0])
  184. # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
  185. #endif
  186. /* This is the type used for the search tree where known translations
  187. are stored. */
  188. struct known_translation_t
  189. {
  190. /* Domain in which to search. */
  191. char *domainname;
  192. /* The category. */
  193. int category;
  194. /* State of the catalog counter at the point the string was found. */
  195. int counter;
  196. /* Catalog where the string was found. */
  197. struct loaded_l10nfile *domain;
  198. /* And finally the translation. */
  199. const char *translation;
  200. size_t translation_length;
  201. /* Pointer to the string in question. */
  202. char msgid[ZERO];
  203. };
  204. /* Root of the search tree with known translations. We can use this
  205. only if the system provides the `tsearch' function family. */
  206. #if defined HAVE_TSEARCH || defined _LIBC
  207. # include <search.h>
  208. static void *root;
  209. # ifdef _LIBC
  210. # define tsearch __tsearch
  211. # endif
  212. /* Function to compare two entries in the table of known translations. */
  213. static int transcmp PARAMS ((const void *p1, const void *p2));
  214. static int
  215. transcmp (p1, p2)
  216. const void *p1;
  217. const void *p2;
  218. {
  219. const struct known_translation_t *s1;
  220. const struct known_translation_t *s2;
  221. int result;
  222. s1 = (const struct known_translation_t *) p1;
  223. s2 = (const struct known_translation_t *) p2;
  224. result = strcmp (s1->msgid, s2->msgid);
  225. if (result == 0)
  226. {
  227. result = strcmp (s1->domainname, s2->domainname);
  228. if (result == 0)
  229. /* We compare the category last (though this is the cheapest
  230. operation) since it is hopefully always the same (namely
  231. LC_MESSAGES). */
  232. result = s1->category - s2->category;
  233. }
  234. return result;
  235. }
  236. #endif
  237. #ifndef INTVARDEF
  238. # define INTVARDEF(name)
  239. #endif
  240. #ifndef INTUSE
  241. # define INTUSE(name) name
  242. #endif
  243. /* Name of the default domain used for gettext(3) prior any call to
  244. textdomain(3). The default value for this is "messages". */
  245. const char _nl_default_default_domain[] attribute_hidden = "messages";
  246. /* Value used as the default domain for gettext(3). */
  247. const char *_nl_current_default_domain attribute_hidden
  248. = _nl_default_default_domain;
  249. /* Contains the default location of the message catalogs. */
  250. #if defined __EMX__
  251. extern const char _nl_default_dirname[];
  252. #else
  253. const char _nl_default_dirname[] = LOCALEDIR;
  254. INTVARDEF (_nl_default_dirname)
  255. #endif
  256. /* List with bindings of specific domains created by bindtextdomain()
  257. calls. */
  258. struct binding *_nl_domain_bindings;
  259. /* Prototypes for local functions. */
  260. static char *plural_lookup PARAMS ((struct loaded_l10nfile *domain,
  261. unsigned long int n,
  262. const char *translation,
  263. size_t translation_len))
  264. internal_function;
  265. static const char *guess_category_value PARAMS ((int category,
  266. const char *categoryname))
  267. internal_function;
  268. #ifdef _LIBC
  269. # include "../locale/localeinfo.h"
  270. # define category_to_name(category) _nl_category_names[category]
  271. #else
  272. static const char *category_to_name PARAMS ((int category)) internal_function;
  273. #endif
  274. /* For those loosing systems which don't have `alloca' we have to add
  275. some additional code emulating it. */
  276. #ifdef HAVE_ALLOCA
  277. /* Nothing has to be done. */
  278. # define freea(p) /* nothing */
  279. # define ADD_BLOCK(list, address) /* nothing */
  280. # define FREE_BLOCKS(list) /* nothing */
  281. #else
  282. struct block_list
  283. {
  284. void *address;
  285. struct block_list *next;
  286. };
  287. # define ADD_BLOCK(list, addr) \
  288. do { \
  289. struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
  290. /* If we cannot get a free block we cannot add the new element to \
  291. the list. */ \
  292. if (newp != NULL) { \
  293. newp->address = (addr); \
  294. newp->next = (list); \
  295. (list) = newp; \
  296. } \
  297. } while (0)
  298. # define FREE_BLOCKS(list) \
  299. do { \
  300. while (list != NULL) { \
  301. struct block_list *old = list; \
  302. list = list->next; \
  303. free (old->address); \
  304. free (old); \
  305. } \
  306. } while (0)
  307. # undef alloca
  308. # define alloca(size) (malloc (size))
  309. # define freea(p) free (p)
  310. #endif /* have alloca */
  311. #ifdef _LIBC
  312. /* List of blocks allocated for translations. */
  313. typedef struct transmem_list
  314. {
  315. struct transmem_list *next;
  316. char data[ZERO];
  317. } transmem_block_t;
  318. static struct transmem_list *transmem_list;
  319. #else
  320. typedef unsigned char transmem_block_t;
  321. #endif
  322. /* Names for the libintl functions are a problem. They must not clash
  323. with existing names and they should follow ANSI C. But this source
  324. code is also used in GNU C Library where the names have a __
  325. prefix. So we have to make a difference here. */
  326. #ifdef _LIBC
  327. # define DCIGETTEXT __dcigettext
  328. #else
  329. # define DCIGETTEXT libintl_dcigettext
  330. #endif
  331. /* Lock variable to protect the global data in the gettext implementation. */
  332. #ifdef _LIBC
  333. __libc_rwlock_define_initialized (, _nl_state_lock attribute_hidden)
  334. #endif
  335. /* Checking whether the binaries runs SUID must be done and glibc provides
  336. easier methods therefore we make a difference here. */
  337. #ifdef _LIBC
  338. # define ENABLE_SECURE __libc_enable_secure
  339. # define DETERMINE_SECURE
  340. #else
  341. # ifndef HAVE_GETUID
  342. # define getuid() 0
  343. # endif
  344. # ifndef HAVE_GETGID
  345. # define getgid() 0
  346. # endif
  347. # ifndef HAVE_GETEUID
  348. # define geteuid() getuid()
  349. # endif
  350. # ifndef HAVE_GETEGID
  351. # define getegid() getgid()
  352. # endif
  353. static int enable_secure;
  354. # define ENABLE_SECURE (enable_secure == 1)
  355. # define DETERMINE_SECURE \
  356. if (enable_secure == 0) \
  357. { \
  358. if (getuid () != geteuid () || getgid () != getegid ()) \
  359. enable_secure = 1; \
  360. else \
  361. enable_secure = -1; \
  362. }
  363. #endif
  364. /* Get the function to evaluate the plural expression. */
  365. #include "eval-plural.h"
  366. /* Look up MSGID in the DOMAINNAME message catalog for the current
  367. CATEGORY locale and, if PLURAL is nonzero, search over string
  368. depending on the plural form determined by N. */
  369. char *
  370. DCIGETTEXT (domainname, msgid1, msgid2, plural, n, category)
  371. const char *domainname;
  372. const char *msgid1;
  373. const char *msgid2;
  374. int plural;
  375. unsigned long int n;
  376. int category;
  377. {
  378. #ifndef HAVE_ALLOCA
  379. struct block_list *block_list = NULL;
  380. #endif
  381. struct loaded_l10nfile *domain;
  382. struct binding *binding;
  383. const char *categoryname;
  384. const char *categoryvalue;
  385. char *dirname, *xdomainname;
  386. char *single_locale;
  387. char *retval;
  388. size_t retlen;
  389. int saved_errno;
  390. #if defined HAVE_TSEARCH || defined _LIBC
  391. struct known_translation_t *search;
  392. struct known_translation_t **foundp = NULL;
  393. size_t msgid_len;
  394. #endif
  395. size_t domainname_len;
  396. /* If no real MSGID is given return NULL. */
  397. if (msgid1 == NULL)
  398. return NULL;
  399. #ifdef _LIBC
  400. if (category < 0 || category >= __LC_LAST || category == LC_ALL)
  401. /* Bogus. */
  402. return (plural == 0
  403. ? (char *) msgid1
  404. /* Use the Germanic plural rule. */
  405. : n == 1 ? (char *) msgid1 : (char *) msgid2);
  406. #endif
  407. __libc_rwlock_rdlock (_nl_state_lock);
  408. /* If DOMAINNAME is NULL, we are interested in the default domain. If
  409. CATEGORY is not LC_MESSAGES this might not make much sense but the
  410. definition left this undefined. */
  411. if (domainname == NULL)
  412. domainname = _nl_current_default_domain;
  413. /* OS/2 specific: backward compatibility with older libintl versions */
  414. #ifdef LC_MESSAGES_COMPAT
  415. if (category == LC_MESSAGES_COMPAT)
  416. category = LC_MESSAGES;
  417. #endif
  418. #if defined HAVE_TSEARCH || defined _LIBC
  419. msgid_len = strlen (msgid1) + 1;
  420. /* Try to find the translation among those which we found at
  421. some time. */
  422. search = (struct known_translation_t *)
  423. alloca (offsetof (struct known_translation_t, msgid) + msgid_len);
  424. memcpy (search->msgid, msgid1, msgid_len);
  425. search->domainname = (char *) domainname;
  426. search->category = category;
  427. foundp = (struct known_translation_t **) tfind (search, &root, transcmp);
  428. freea (search);
  429. if (foundp != NULL && (*foundp)->counter == _nl_msg_cat_cntr)
  430. {
  431. /* Now deal with plural. */
  432. if (plural)
  433. retval = plural_lookup ((*foundp)->domain, n, (*foundp)->translation,
  434. (*foundp)->translation_length);
  435. else
  436. retval = (char *) (*foundp)->translation;
  437. __libc_rwlock_unlock (_nl_state_lock);
  438. return retval;
  439. }
  440. #endif
  441. /* Preserve the `errno' value. */
  442. saved_errno = errno;
  443. /* See whether this is a SUID binary or not. */
  444. DETERMINE_SECURE;
  445. /* First find matching binding. */
  446. for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
  447. {
  448. int compare = strcmp (domainname, binding->domainname);
  449. if (compare == 0)
  450. /* We found it! */
  451. break;
  452. if (compare < 0)
  453. {
  454. /* It is not in the list. */
  455. binding = NULL;
  456. break;
  457. }
  458. }
  459. if (binding == NULL)
  460. dirname = (char *) INTUSE(_nl_default_dirname);
  461. else if (IS_ABSOLUTE_PATH (binding->dirname))
  462. dirname = binding->dirname;
  463. else
  464. {
  465. /* We have a relative path. Make it absolute now. */
  466. size_t dirname_len = strlen (binding->dirname) + 1;
  467. size_t path_max;
  468. char *ret;
  469. path_max = (unsigned int) PATH_MAX;
  470. path_max += 2; /* The getcwd docs say to do this. */
  471. for (;;)
  472. {
  473. dirname = (char *) alloca (path_max + dirname_len);
  474. ADD_BLOCK (block_list, dirname);
  475. __set_errno (0);
  476. ret = getcwd (dirname, path_max);
  477. if (ret != NULL || errno != ERANGE)
  478. break;
  479. path_max += path_max / 2;
  480. path_max += PATH_INCR;
  481. }
  482. if (ret == NULL)
  483. /* We cannot get the current working directory. Don't signal an
  484. error but simply return the default string. */
  485. goto return_untranslated;
  486. stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
  487. }
  488. /* Now determine the symbolic name of CATEGORY and its value. */
  489. categoryname = category_to_name (category);
  490. categoryvalue = guess_category_value (category, categoryname);
  491. domainname_len = strlen (domainname);
  492. xdomainname = (char *) alloca (strlen (categoryname)
  493. + domainname_len + 5);
  494. ADD_BLOCK (block_list, xdomainname);
  495. stpcpy (mempcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
  496. domainname, domainname_len),
  497. ".mo");
  498. /* Creating working area. */
  499. single_locale = (char *) alloca (strlen (categoryvalue) + 1);
  500. ADD_BLOCK (block_list, single_locale);
  501. /* Search for the given string. This is a loop because we perhaps
  502. got an ordered list of languages to consider for the translation. */
  503. while (1)
  504. {
  505. /* Make CATEGORYVALUE point to the next element of the list. */
  506. while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
  507. ++categoryvalue;
  508. if (categoryvalue[0] == '\0')
  509. {
  510. /* The whole contents of CATEGORYVALUE has been searched but
  511. no valid entry has been found. We solve this situation
  512. by implicitly appending a "C" entry, i.e. no translation
  513. will take place. */
  514. single_locale[0] = 'C';
  515. single_locale[1] = '\0';
  516. }
  517. else
  518. {
  519. char *cp = single_locale;
  520. while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
  521. *cp++ = *categoryvalue++;
  522. *cp = '\0';
  523. /* When this is a SUID binary we must not allow accessing files
  524. outside the dedicated directories. */
  525. if (ENABLE_SECURE && IS_PATH_WITH_DIR (single_locale))
  526. /* Ingore this entry. */
  527. continue;
  528. }
  529. /* If the current locale value is C (or POSIX) we don't load a
  530. domain. Return the MSGID. */
  531. if (strcmp (single_locale, "C") == 0
  532. || strcmp (single_locale, "POSIX") == 0)
  533. break;
  534. /* Find structure describing the message catalog matching the
  535. DOMAINNAME and CATEGORY. */
  536. domain = _nl_find_domain (dirname, single_locale, xdomainname, binding);
  537. if (domain != NULL)
  538. {
  539. retval = _nl_find_msg (domain, binding, msgid1, &retlen);
  540. if (retval == NULL)
  541. {
  542. int cnt;
  543. for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
  544. {
  545. retval = _nl_find_msg (domain->successor[cnt], binding,
  546. msgid1, &retlen);
  547. if (retval != NULL)
  548. {
  549. domain = domain->successor[cnt];
  550. break;
  551. }
  552. }
  553. }
  554. if (retval != NULL)
  555. {
  556. /* Found the translation of MSGID1 in domain DOMAIN:
  557. starting at RETVAL, RETLEN bytes. */
  558. FREE_BLOCKS (block_list);
  559. #if defined HAVE_TSEARCH || defined _LIBC
  560. if (foundp == NULL)
  561. {
  562. /* Create a new entry and add it to the search tree. */
  563. struct known_translation_t *newp;
  564. newp = (struct known_translation_t *)
  565. malloc (offsetof (struct known_translation_t, msgid)
  566. + msgid_len + domainname_len + 1);
  567. if (newp != NULL)
  568. {
  569. newp->domainname =
  570. mempcpy (newp->msgid, msgid1, msgid_len);
  571. memcpy (newp->domainname, domainname, domainname_len + 1);
  572. newp->category = category;
  573. newp->counter = _nl_msg_cat_cntr;
  574. newp->domain = domain;
  575. newp->translation = retval;
  576. newp->translation_length = retlen;
  577. /* Insert the entry in the search tree. */
  578. foundp = (struct known_translation_t **)
  579. tsearch (newp, &root, transcmp);
  580. if (foundp == NULL
  581. || __builtin_expect (*foundp != newp, 0))
  582. /* The insert failed. */
  583. free (newp);
  584. }
  585. }
  586. else
  587. {
  588. /* We can update the existing entry. */
  589. (*foundp)->counter = _nl_msg_cat_cntr;
  590. (*foundp)->domain = domain;
  591. (*foundp)->translation = retval;
  592. (*foundp)->translation_length = retlen;
  593. }
  594. #endif
  595. __set_errno (saved_errno);
  596. /* Now deal with plural. */
  597. if (plural)
  598. retval = plural_lookup (domain, n, retval, retlen);
  599. __libc_rwlock_unlock (_nl_state_lock);
  600. return retval;
  601. }
  602. }
  603. }
  604. return_untranslated:
  605. /* Return the untranslated MSGID. */
  606. FREE_BLOCKS (block_list);
  607. __libc_rwlock_unlock (_nl_state_lock);
  608. #ifndef _LIBC
  609. if (!ENABLE_SECURE)
  610. {
  611. extern void _nl_log_untranslated PARAMS ((const char *logfilename,
  612. const char *domainname,
  613. const char *msgid1,
  614. const char *msgid2,
  615. int plural));
  616. const char *logfilename = getenv ("GETTEXT_LOG_UNTRANSLATED");
  617. if (logfilename != NULL && logfilename[0] != '\0')
  618. _nl_log_untranslated (logfilename, domainname, msgid1, msgid2, plural);
  619. }
  620. #endif
  621. __set_errno (saved_errno);
  622. return (plural == 0
  623. ? (char *) msgid1
  624. /* Use the Germanic plural rule. */
  625. : n == 1 ? (char *) msgid1 : (char *) msgid2);
  626. }
  627. char *
  628. internal_function
  629. _nl_find_msg (domain_file, domainbinding, msgid, lengthp)
  630. struct loaded_l10nfile *domain_file;
  631. struct binding *domainbinding;
  632. const char *msgid;
  633. size_t *lengthp;
  634. {
  635. struct loaded_domain *domain;
  636. nls_uint32 nstrings;
  637. size_t act;
  638. char *result;
  639. size_t resultlen;
  640. if (domain_file->decided == 0)
  641. _nl_load_domain (domain_file, domainbinding);
  642. if (domain_file->data == NULL)
  643. return NULL;
  644. domain = (struct loaded_domain *) domain_file->data;
  645. nstrings = domain->nstrings;
  646. /* Locate the MSGID and its translation. */
  647. if (domain->hash_tab != NULL)
  648. {
  649. /* Use the hashing table. */
  650. nls_uint32 len = strlen (msgid);
  651. nls_uint32 hash_val = hash_string (msgid);
  652. nls_uint32 idx = hash_val % domain->hash_size;
  653. nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
  654. while (1)
  655. {
  656. nls_uint32 nstr =
  657. W (domain->must_swap_hash_tab, domain->hash_tab[idx]);
  658. if (nstr == 0)
  659. /* Hash table entry is empty. */
  660. return NULL;
  661. nstr--;
  662. /* Compare msgid with the original string at index nstr.
  663. We compare the lengths with >=, not ==, because plural entries
  664. are represented by strings with an embedded NUL. */
  665. if (nstr < nstrings
  666. ? W (domain->must_swap, domain->orig_tab[nstr].length) >= len
  667. && (strcmp (msgid,
  668. domain->data + W (domain->must_swap,
  669. domain->orig_tab[nstr].offset))
  670. == 0)
  671. : domain->orig_sysdep_tab[nstr - nstrings].length > len
  672. && (strcmp (msgid,
  673. domain->orig_sysdep_tab[nstr - nstrings].pointer)
  674. == 0))
  675. {
  676. act = nstr;
  677. goto found;
  678. }
  679. if (idx >= domain->hash_size - incr)
  680. idx -= domain->hash_size - incr;
  681. else
  682. idx += incr;
  683. }
  684. /* NOTREACHED */
  685. }
  686. else
  687. {
  688. /* Try the default method: binary search in the sorted array of
  689. messages. */
  690. size_t top, bottom;
  691. bottom = 0;
  692. top = nstrings;
  693. while (bottom < top)
  694. {
  695. int cmp_val;
  696. act = (bottom + top) / 2;
  697. cmp_val = strcmp (msgid, (domain->data
  698. + W (domain->must_swap,
  699. domain->orig_tab[act].offset)));
  700. if (cmp_val < 0)
  701. top = act;
  702. else if (cmp_val > 0)
  703. bottom = act + 1;
  704. else
  705. goto found;
  706. }
  707. /* No translation was found. */
  708. return NULL;
  709. }
  710. found:
  711. /* The translation was found at index ACT. If we have to convert the
  712. string to use a different character set, this is the time. */
  713. if (act < nstrings)
  714. {
  715. result = (char *)
  716. (domain->data + W (domain->must_swap, domain->trans_tab[act].offset));
  717. resultlen = W (domain->must_swap, domain->trans_tab[act].length) + 1;
  718. }
  719. else
  720. {
  721. result = (char *) domain->trans_sysdep_tab[act - nstrings].pointer;
  722. resultlen = domain->trans_sysdep_tab[act - nstrings].length;
  723. }
  724. #if defined _LIBC || HAVE_ICONV
  725. if (domain->codeset_cntr
  726. != (domainbinding != NULL ? domainbinding->codeset_cntr : 0))
  727. {
  728. /* The domain's codeset has changed through bind_textdomain_codeset()
  729. since the message catalog was initialized or last accessed. We
  730. have to reinitialize the converter. */
  731. _nl_free_domain_conv (domain);
  732. _nl_init_domain_conv (domain_file, domain, domainbinding);
  733. }
  734. if (
  735. # ifdef _LIBC
  736. domain->conv != (__gconv_t) -1
  737. # else
  738. # if HAVE_ICONV
  739. domain->conv != (iconv_t) -1
  740. # endif
  741. # endif
  742. )
  743. {
  744. /* We are supposed to do a conversion. First allocate an
  745. appropriate table with the same structure as the table
  746. of translations in the file, where we can put the pointers
  747. to the converted strings in.
  748. There is a slight complication with plural entries. They
  749. are represented by consecutive NUL terminated strings. We
  750. handle this case by converting RESULTLEN bytes, including
  751. NULs. */
  752. if (domain->conv_tab == NULL
  753. && ((domain->conv_tab =
  754. (char **) calloc (nstrings + domain->n_sysdep_strings,
  755. sizeof (char *)))
  756. == NULL))
  757. /* Mark that we didn't succeed allocating a table. */
  758. domain->conv_tab = (char **) -1;
  759. if (__builtin_expect (domain->conv_tab == (char **) -1, 0))
  760. /* Nothing we can do, no more memory. */
  761. goto converted;
  762. if (domain->conv_tab[act] == NULL)
  763. {
  764. /* We haven't used this string so far, so it is not
  765. translated yet. Do this now. */
  766. /* We use a bit more efficient memory handling.
  767. We allocate always larger blocks which get used over
  768. time. This is faster than many small allocations. */
  769. __libc_lock_define_initialized (static, lock)
  770. # define INITIAL_BLOCK_SIZE 4080
  771. static unsigned char *freemem;
  772. static size_t freemem_size;
  773. const unsigned char *inbuf;
  774. unsigned char *outbuf;
  775. int malloc_count;
  776. # ifndef _LIBC
  777. transmem_block_t *transmem_list = NULL;
  778. # endif
  779. __libc_lock_lock (lock);
  780. inbuf = (const unsigned char *) result;
  781. outbuf = freemem + sizeof (size_t);
  782. malloc_count = 0;
  783. while (1)
  784. {
  785. transmem_block_t *newmem;
  786. # ifdef _LIBC
  787. size_t non_reversible;
  788. int res;
  789. if (freemem_size < sizeof (size_t))
  790. goto resize_freemem;
  791. res = __gconv (domain->conv,
  792. &inbuf, inbuf + resultlen,
  793. &outbuf,
  794. outbuf + freemem_size - sizeof (size_t),
  795. &non_reversible);
  796. if (res == __GCONV_OK || res == __GCONV_EMPTY_INPUT)
  797. break;
  798. if (res != __GCONV_FULL_OUTPUT)
  799. {
  800. __libc_lock_unlock (lock);
  801. goto converted;
  802. }
  803. inbuf = result;
  804. # else
  805. # if HAVE_ICONV
  806. const char *inptr = (const char *) inbuf;
  807. size_t inleft = resultlen;
  808. char *outptr = (char *) outbuf;
  809. size_t outleft;
  810. if (freemem_size < sizeof (size_t))
  811. goto resize_freemem;
  812. outleft = freemem_size - sizeof (size_t);
  813. if (iconv (domain->conv,
  814. (ICONV_CONST char **) &inptr, &inleft,
  815. &outptr, &outleft)
  816. != (size_t) (-1))
  817. {
  818. outbuf = (unsigned char *) outptr;
  819. break;
  820. }
  821. if (errno != E2BIG)
  822. {
  823. __libc_lock_unlock (lock);
  824. goto converted;
  825. }
  826. # endif
  827. # endif
  828. resize_freemem:
  829. /* We must allocate a new buffer or resize the old one. */
  830. if (malloc_count > 0)
  831. {
  832. ++malloc_count;
  833. freemem_size = malloc_count * INITIAL_BLOCK_SIZE;
  834. newmem = (transmem_block_t *) realloc (transmem_list,
  835. freemem_size);
  836. # ifdef _LIBC
  837. if (newmem != NULL)
  838. transmem_list = transmem_list->next;
  839. else
  840. {
  841. struct transmem_list *old = transmem_list;
  842. transmem_list = transmem_list->next;
  843. free (old);
  844. }
  845. # endif
  846. }
  847. else
  848. {
  849. malloc_count = 1;
  850. freemem_size = INITIAL_BLOCK_SIZE;
  851. newmem = (transmem_block_t *) malloc (freemem_size);
  852. }
  853. if (__builtin_expect (newmem == NULL, 0))
  854. {
  855. freemem = NULL;
  856. freemem_size = 0;
  857. __libc_lock_unlock (lock);
  858. goto converted;
  859. }
  860. # ifdef _LIBC
  861. /* Add the block to the list of blocks we have to free
  862. at some point. */
  863. newmem->next = transmem_list;
  864. transmem_list = newmem;
  865. freemem = newmem->data;
  866. freemem_size -= offsetof (struct transmem_list, data);
  867. # else
  868. transmem_list = newmem;
  869. freemem = newmem;
  870. # endif
  871. outbuf = freemem + sizeof (size_t);
  872. }
  873. /* We have now in our buffer a converted string. Put this
  874. into the table of conversions. */
  875. *(size_t *) freemem = outbuf - freemem - sizeof (size_t);
  876. domain->conv_tab[act] = (char *) freemem;
  877. /* Shrink freemem, but keep it aligned. */
  878. freemem_size -= outbuf - freemem;
  879. freemem = outbuf;
  880. freemem += freemem_size & (alignof (size_t) - 1);
  881. freemem_size = freemem_size & ~ (alignof (size_t) - 1);
  882. __libc_lock_unlock (lock);
  883. }
  884. /* Now domain->conv_tab[act] contains the translation of all
  885. the plural variants. */
  886. result = domain->conv_tab[act] + sizeof (size_t);
  887. resultlen = *(size_t *) domain->conv_tab[act];
  888. }
  889. converted:
  890. /* The result string is converted. */
  891. #endif /* _LIBC || HAVE_ICONV */
  892. *lengthp = resultlen;
  893. return result;
  894. }
  895. /* Look up a plural variant. */
  896. static char *
  897. internal_function
  898. plural_lookup (domain, n, translation, translation_len)
  899. struct loaded_l10nfile *domain;
  900. unsigned long int n;
  901. const char *translation;
  902. size_t translation_len;
  903. {
  904. struct loaded_domain *domaindata = (struct loaded_domain *) domain->data;
  905. unsigned long int index;
  906. const char *p;
  907. index = plural_eval (domaindata->plural, n);
  908. if (index >= domaindata->nplurals)
  909. /* This should never happen. It means the plural expression and the
  910. given maximum value do not match. */
  911. index = 0;
  912. /* Skip INDEX strings at TRANSLATION. */
  913. p = translation;
  914. while (index-- > 0)
  915. {
  916. #ifdef _LIBC
  917. p = __rawmemchr (p, '\0');
  918. #else
  919. p = strchr (p, '\0');
  920. #endif
  921. /* And skip over the NUL byte. */
  922. p++;
  923. if (p >= translation + translation_len)
  924. /* This should never happen. It means the plural expression
  925. evaluated to a value larger than the number of variants
  926. available for MSGID1. */
  927. return (char *) translation;
  928. }
  929. return (char *) p;
  930. }
  931. #ifndef _LIBC
  932. /* Return string representation of locale CATEGORY. */
  933. static const char *
  934. internal_function
  935. category_to_name (category)
  936. int category;
  937. {
  938. const char *retval;
  939. switch (category)
  940. {
  941. #ifdef LC_COLLATE
  942. case LC_COLLATE:
  943. retval = "LC_COLLATE";
  944. break;
  945. #endif
  946. #ifdef LC_CTYPE
  947. case LC_CTYPE:
  948. retval = "LC_CTYPE";
  949. break;
  950. #endif
  951. #ifdef LC_MONETARY
  952. case LC_MONETARY:
  953. retval = "LC_MONETARY";
  954. break;
  955. #endif
  956. #ifdef LC_NUMERIC
  957. case LC_NUMERIC:
  958. retval = "LC_NUMERIC";
  959. break;
  960. #endif
  961. #ifdef LC_TIME
  962. case LC_TIME:
  963. retval = "LC_TIME";
  964. break;
  965. #endif
  966. #ifdef LC_MESSAGES
  967. case LC_MESSAGES:
  968. retval = "LC_MESSAGES";
  969. break;
  970. #endif
  971. #ifdef LC_RESPONSE
  972. case LC_RESPONSE:
  973. retval = "LC_RESPONSE";
  974. break;
  975. #endif
  976. #ifdef LC_ALL
  977. case LC_ALL:
  978. /* This might not make sense but is perhaps better than any other
  979. value. */
  980. retval = "LC_ALL";
  981. break;
  982. #endif
  983. default:
  984. /* If you have a better idea for a default value let me know. */
  985. retval = "LC_XXX";
  986. }
  987. return retval;
  988. }
  989. #endif
  990. /* Guess value of current locale from value of the environment variables. */
  991. static const char *
  992. internal_function
  993. guess_category_value (category, categoryname)
  994. int category;
  995. const char *categoryname;
  996. {
  997. const char *language;
  998. const char *retval;
  999. /* The highest priority value is the `LANGUAGE' environment
  1000. variable. But we don't use the value if the currently selected
  1001. locale is the C locale. This is a GNU extension. */
  1002. language = getenv ("LANGUAGE");
  1003. if (language != NULL && language[0] == '\0')
  1004. language = NULL;
  1005. /* We have to proceed with the POSIX methods of looking to `LC_ALL',
  1006. `LC_xxx', and `LANG'. On some systems this can be done by the
  1007. `setlocale' function itself. */
  1008. #ifdef _LIBC
  1009. retval = __current_locale_name (category);
  1010. #else
  1011. retval = _nl_locale_name (category, categoryname);
  1012. #endif
  1013. /* Ignore LANGUAGE if the locale is set to "C" because
  1014. 1. "C" locale usually uses the ASCII encoding, and most international
  1015. messages use non-ASCII characters. These characters get displayed
  1016. as question marks (if using glibc's iconv()) or as invalid 8-bit
  1017. characters (because other iconv()s refuse to convert most non-ASCII
  1018. characters to ASCII). In any case, the output is ugly.
  1019. 2. The precise output of some programs in the "C" locale is specified
  1020. by POSIX and should not depend on environment variables like
  1021. "LANGUAGE". We allow such programs to use gettext(). */
  1022. return language != NULL && strcmp (retval, "C") != 0 ? language : retval;
  1023. }
  1024. /* @@ begin of epilog @@ */
  1025. /* We don't want libintl.a to depend on any other library. So we
  1026. avoid the non-standard function stpcpy. In GNU C Library this
  1027. function is available, though. Also allow the symbol HAVE_STPCPY
  1028. to be defined. */
  1029. #if !_LIBC && !HAVE_STPCPY
  1030. static char *
  1031. stpcpy (dest, src)
  1032. char *dest;
  1033. const char *src;
  1034. {
  1035. while ((*dest++ = *src++) != '\0')
  1036. /* Do nothing. */ ;
  1037. return dest - 1;
  1038. }
  1039. #endif
  1040. #if !_LIBC && !HAVE_MEMPCPY
  1041. static void *
  1042. mempcpy (dest, src, n)
  1043. void *dest;
  1044. const void *src;
  1045. size_t n;
  1046. {
  1047. return (void *) ((char *) memcpy (dest, src, n) + n);
  1048. }
  1049. #endif
  1050. #ifdef _LIBC
  1051. /* If we want to free all resources we have to do some work at
  1052. program's end. */
  1053. libc_freeres_fn (free_mem)
  1054. {
  1055. void *old;
  1056. while (_nl_domain_bindings != NULL)
  1057. {
  1058. struct binding *oldp = _nl_domain_bindings;
  1059. _nl_domain_bindings = _nl_domain_bindings->next;
  1060. if (oldp->dirname != INTUSE(_nl_default_dirname))
  1061. /* Yes, this is a pointer comparison. */
  1062. free (oldp->dirname);
  1063. free (oldp->codeset);
  1064. free (oldp);
  1065. }
  1066. if (_nl_current_default_domain != _nl_default_default_domain)
  1067. /* Yes, again a pointer comparison. */
  1068. free ((char *) _nl_current_default_domain);
  1069. /* Remove the search tree with the known translations. */
  1070. __tdestroy (root, free);
  1071. root = NULL;
  1072. while (transmem_list != NULL)
  1073. {
  1074. old = transmem_list;
  1075. transmem_list = transmem_list->next;
  1076. free (old);
  1077. }
  1078. }
  1079. #endif