You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dcigettext.c 33KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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. # if VMS
  132. # define getcwd(buf, max) (getcwd) (buf, max, 0)
  133. # else
  134. char *getcwd ();
  135. # endif
  136. # endif
  137. # ifndef HAVE_STPCPY
  138. static char *stpcpy (char *dest, const char *src);
  139. # endif
  140. # ifndef HAVE_MEMPCPY
  141. static void *mempcpy (void *dest, const void *src, size_t n);
  142. # endif
  143. #endif
  144. /* Amount to increase buffer size by in each try. */
  145. #define PATH_INCR 32
  146. /* The following is from pathmax.h. */
  147. /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
  148. PATH_MAX but might cause redefinition warnings when sys/param.h is
  149. later included (as on MORE/BSD 4.3). */
  150. #if defined _POSIX_VERSION || (defined HAVE_LIMITS_H && !defined __GNUC__)
  151. # include <limits.h>
  152. #endif
  153. #ifndef _POSIX_PATH_MAX
  154. # define _POSIX_PATH_MAX 255
  155. #endif
  156. #if !defined PATH_MAX && defined _PC_PATH_MAX
  157. # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
  158. #endif
  159. /* Don't include sys/param.h if it already has been. */
  160. #if defined HAVE_SYS_PARAM_H && !defined PATH_MAX && !defined MAXPATHLEN
  161. # include <sys/param.h>
  162. #endif
  163. #if !defined PATH_MAX && defined MAXPATHLEN
  164. # define PATH_MAX MAXPATHLEN
  165. #endif
  166. #ifndef PATH_MAX
  167. # define PATH_MAX _POSIX_PATH_MAX
  168. #endif
  169. /* Pathname support.
  170. ISSLASH(C) tests whether C is a directory separator character.
  171. IS_ABSOLUTE_PATH(P) tests whether P is an absolute path. If it is not,
  172. it may be concatenated to a directory pathname.
  173. IS_PATH_WITH_DIR(P) tests whether P contains a directory specification.
  174. */
  175. #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
  176. /* Win32, OS/2, DOS */
  177. # define ISSLASH(C) ((C) == '/' || (C) == '\\')
  178. # define HAS_DEVICE(P) \
  179. ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
  180. && (P)[1] == ':')
  181. # define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P))
  182. # define IS_PATH_WITH_DIR(P) \
  183. (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
  184. #else
  185. /* Unix */
  186. # define ISSLASH(C) ((C) == '/')
  187. # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0])
  188. # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
  189. #endif
  190. /* This is the type used for the search tree where known translations
  191. are stored. */
  192. struct known_translation_t
  193. {
  194. /* Domain in which to search. */
  195. char *domainname;
  196. /* The category. */
  197. int category;
  198. /* State of the catalog counter at the point the string was found. */
  199. int counter;
  200. /* Catalog where the string was found. */
  201. struct loaded_l10nfile *domain;
  202. /* And finally the translation. */
  203. const char *translation;
  204. size_t translation_length;
  205. /* Pointer to the string in question. */
  206. char msgid[ZERO];
  207. };
  208. /* Root of the search tree with known translations. We can use this
  209. only if the system provides the `tsearch' function family. */
  210. #if defined HAVE_TSEARCH || defined _LIBC
  211. # include <search.h>
  212. static void *root;
  213. # ifdef _LIBC
  214. # define tsearch __tsearch
  215. # endif
  216. /* Function to compare two entries in the table of known translations. */
  217. static int
  218. transcmp (const void *p1, const void *p2)
  219. {
  220. const struct known_translation_t *s1;
  221. const struct known_translation_t *s2;
  222. int result;
  223. s1 = (const struct known_translation_t *) p1;
  224. s2 = (const struct known_translation_t *) p2;
  225. result = strcmp (s1->msgid, s2->msgid);
  226. if (result == 0)
  227. {
  228. result = strcmp (s1->domainname, s2->domainname);
  229. if (result == 0)
  230. /* We compare the category last (though this is the cheapest
  231. operation) since it is hopefully always the same (namely
  232. LC_MESSAGES). */
  233. result = s1->category - s2->category;
  234. }
  235. return result;
  236. }
  237. #endif
  238. #ifndef INTVARDEF
  239. # define INTVARDEF(name)
  240. #endif
  241. #ifndef INTUSE
  242. # define INTUSE(name) name
  243. #endif
  244. /* Name of the default domain used for gettext(3) prior any call to
  245. textdomain(3). The default value for this is "messages". */
  246. const char _nl_default_default_domain[] attribute_hidden = "messages";
  247. /* Value used as the default domain for gettext(3). */
  248. const char *_nl_current_default_domain attribute_hidden
  249. = _nl_default_default_domain;
  250. /* Contains the default location of the message catalogs. */
  251. #if defined __EMX__
  252. extern const char _nl_default_dirname[];
  253. #else
  254. const char _nl_default_dirname[] = LOCALEDIR;
  255. INTVARDEF (_nl_default_dirname)
  256. #endif
  257. /* List with bindings of specific domains created by bindtextdomain()
  258. calls. */
  259. struct binding *_nl_domain_bindings;
  260. /* Prototypes for local functions. */
  261. static char *plural_lookup (struct loaded_l10nfile *domain,
  262. unsigned long int n,
  263. const char *translation, size_t translation_len)
  264. internal_function;
  265. static const char *guess_category_value (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 (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 (const char *domainname, const char *msgid1, const char *msgid2,
  371. int plural, unsigned long int n, int category)
  372. {
  373. #ifndef HAVE_ALLOCA
  374. struct block_list *block_list = NULL;
  375. #endif
  376. struct loaded_l10nfile *domain;
  377. struct binding *binding;
  378. const char *categoryname;
  379. const char *categoryvalue;
  380. char *dirname, *xdomainname;
  381. char *single_locale;
  382. char *retval;
  383. size_t retlen;
  384. int saved_errno;
  385. #if defined HAVE_TSEARCH || defined _LIBC
  386. struct known_translation_t *search;
  387. struct known_translation_t **foundp = NULL;
  388. size_t msgid_len;
  389. #endif
  390. size_t domainname_len;
  391. /* If no real MSGID is given return NULL. */
  392. if (msgid1 == NULL)
  393. return NULL;
  394. #ifdef _LIBC
  395. if (category < 0 || category >= __LC_LAST || category == LC_ALL)
  396. /* Bogus. */
  397. return (plural == 0
  398. ? (char *) msgid1
  399. /* Use the Germanic plural rule. */
  400. : n == 1 ? (char *) msgid1 : (char *) msgid2);
  401. #endif
  402. __libc_rwlock_rdlock (_nl_state_lock);
  403. /* If DOMAINNAME is NULL, we are interested in the default domain. If
  404. CATEGORY is not LC_MESSAGES this might not make much sense but the
  405. definition left this undefined. */
  406. if (domainname == NULL)
  407. domainname = _nl_current_default_domain;
  408. /* OS/2 specific: backward compatibility with older libintl versions */
  409. #ifdef LC_MESSAGES_COMPAT
  410. if (category == LC_MESSAGES_COMPAT)
  411. category = LC_MESSAGES;
  412. #endif
  413. #if defined HAVE_TSEARCH || defined _LIBC
  414. msgid_len = strlen (msgid1) + 1;
  415. /* Try to find the translation among those which we found at
  416. some time. */
  417. search = (struct known_translation_t *)
  418. alloca (offsetof (struct known_translation_t, msgid) + msgid_len);
  419. memcpy (search->msgid, msgid1, msgid_len);
  420. search->domainname = (char *) domainname;
  421. search->category = category;
  422. foundp = (struct known_translation_t **) tfind (search, &root, transcmp);
  423. freea (search);
  424. if (foundp != NULL && (*foundp)->counter == _nl_msg_cat_cntr)
  425. {
  426. /* Now deal with plural. */
  427. if (plural)
  428. retval = plural_lookup ((*foundp)->domain, n, (*foundp)->translation,
  429. (*foundp)->translation_length);
  430. else
  431. retval = (char *) (*foundp)->translation;
  432. __libc_rwlock_unlock (_nl_state_lock);
  433. return retval;
  434. }
  435. #endif
  436. /* Preserve the `errno' value. */
  437. saved_errno = errno;
  438. /* See whether this is a SUID binary or not. */
  439. DETERMINE_SECURE;
  440. /* First find matching binding. */
  441. for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
  442. {
  443. int compare = strcmp (domainname, binding->domainname);
  444. if (compare == 0)
  445. /* We found it! */
  446. break;
  447. if (compare < 0)
  448. {
  449. /* It is not in the list. */
  450. binding = NULL;
  451. break;
  452. }
  453. }
  454. if (binding == NULL)
  455. dirname = (char *) INTUSE(_nl_default_dirname);
  456. else if (IS_ABSOLUTE_PATH (binding->dirname))
  457. dirname = binding->dirname;
  458. else
  459. {
  460. /* We have a relative path. Make it absolute now. */
  461. size_t dirname_len = strlen (binding->dirname) + 1;
  462. size_t path_max;
  463. char *ret;
  464. path_max = (unsigned int) PATH_MAX;
  465. path_max += 2; /* The getcwd docs say to do this. */
  466. for (;;)
  467. {
  468. dirname = (char *) alloca (path_max + dirname_len);
  469. ADD_BLOCK (block_list, dirname);
  470. __set_errno (0);
  471. ret = getcwd (dirname, path_max);
  472. if (ret != NULL || errno != ERANGE)
  473. break;
  474. path_max += path_max / 2;
  475. path_max += PATH_INCR;
  476. }
  477. if (ret == NULL)
  478. /* We cannot get the current working directory. Don't signal an
  479. error but simply return the default string. */
  480. goto return_untranslated;
  481. stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
  482. }
  483. /* Now determine the symbolic name of CATEGORY and its value. */
  484. categoryname = category_to_name (category);
  485. categoryvalue = guess_category_value (category, categoryname);
  486. domainname_len = strlen (domainname);
  487. xdomainname = (char *) alloca (strlen (categoryname)
  488. + domainname_len + 5);
  489. ADD_BLOCK (block_list, xdomainname);
  490. stpcpy (mempcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
  491. domainname, domainname_len),
  492. ".mo");
  493. /* Creating working area. */
  494. single_locale = (char *) alloca (strlen (categoryvalue) + 1);
  495. ADD_BLOCK (block_list, single_locale);
  496. /* Search for the given string. This is a loop because we perhaps
  497. got an ordered list of languages to consider for the translation. */
  498. while (1)
  499. {
  500. /* Make CATEGORYVALUE point to the next element of the list. */
  501. while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
  502. ++categoryvalue;
  503. if (categoryvalue[0] == '\0')
  504. {
  505. /* The whole contents of CATEGORYVALUE has been searched but
  506. no valid entry has been found. We solve this situation
  507. by implicitly appending a "C" entry, i.e. no translation
  508. will take place. */
  509. single_locale[0] = 'C';
  510. single_locale[1] = '\0';
  511. }
  512. else
  513. {
  514. char *cp = single_locale;
  515. while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
  516. *cp++ = *categoryvalue++;
  517. *cp = '\0';
  518. /* When this is a SUID binary we must not allow accessing files
  519. outside the dedicated directories. */
  520. if (ENABLE_SECURE && IS_PATH_WITH_DIR (single_locale))
  521. /* Ingore this entry. */
  522. continue;
  523. }
  524. /* If the current locale value is C (or POSIX) we don't load a
  525. domain. Return the MSGID. */
  526. if (strcmp (single_locale, "C") == 0
  527. || strcmp (single_locale, "POSIX") == 0)
  528. break;
  529. /* Find structure describing the message catalog matching the
  530. DOMAINNAME and CATEGORY. */
  531. domain = _nl_find_domain (dirname, single_locale, xdomainname, binding);
  532. if (domain != NULL)
  533. {
  534. retval = _nl_find_msg (domain, binding, msgid1, &retlen);
  535. if (retval == NULL)
  536. {
  537. int cnt;
  538. for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
  539. {
  540. retval = _nl_find_msg (domain->successor[cnt], binding,
  541. msgid1, &retlen);
  542. if (retval != NULL)
  543. {
  544. domain = domain->successor[cnt];
  545. break;
  546. }
  547. }
  548. }
  549. if (retval != NULL)
  550. {
  551. /* Found the translation of MSGID1 in domain DOMAIN:
  552. starting at RETVAL, RETLEN bytes. */
  553. FREE_BLOCKS (block_list);
  554. #if defined HAVE_TSEARCH || defined _LIBC
  555. if (foundp == NULL)
  556. {
  557. /* Create a new entry and add it to the search tree. */
  558. struct known_translation_t *newp;
  559. newp = (struct known_translation_t *)
  560. malloc (offsetof (struct known_translation_t, msgid)
  561. + msgid_len + domainname_len + 1);
  562. if (newp != NULL)
  563. {
  564. newp->domainname =
  565. mempcpy (newp->msgid, msgid1, msgid_len);
  566. memcpy (newp->domainname, domainname, domainname_len + 1);
  567. newp->category = category;
  568. newp->counter = _nl_msg_cat_cntr;
  569. newp->domain = domain;
  570. newp->translation = retval;
  571. newp->translation_length = retlen;
  572. /* Insert the entry in the search tree. */
  573. foundp = (struct known_translation_t **)
  574. tsearch (newp, &root, transcmp);
  575. if (foundp == NULL
  576. || __builtin_expect (*foundp != newp, 0))
  577. /* The insert failed. */
  578. free (newp);
  579. }
  580. }
  581. else
  582. {
  583. /* We can update the existing entry. */
  584. (*foundp)->counter = _nl_msg_cat_cntr;
  585. (*foundp)->domain = domain;
  586. (*foundp)->translation = retval;
  587. (*foundp)->translation_length = retlen;
  588. }
  589. #endif
  590. __set_errno (saved_errno);
  591. /* Now deal with plural. */
  592. if (plural)
  593. retval = plural_lookup (domain, n, retval, retlen);
  594. __libc_rwlock_unlock (_nl_state_lock);
  595. return retval;
  596. }
  597. }
  598. }
  599. return_untranslated:
  600. /* Return the untranslated MSGID. */
  601. FREE_BLOCKS (block_list);
  602. __libc_rwlock_unlock (_nl_state_lock);
  603. #ifndef _LIBC
  604. if (!ENABLE_SECURE)
  605. {
  606. extern void _nl_log_untranslated (const char *logfilename,
  607. const char *domainname,
  608. const char *msgid1, const char *msgid2,
  609. int plural);
  610. const char *logfilename = getenv ("GETTEXT_LOG_UNTRANSLATED");
  611. if (logfilename != NULL && logfilename[0] != '\0')
  612. _nl_log_untranslated (logfilename, domainname, msgid1, msgid2, plural);
  613. }
  614. #endif
  615. __set_errno (saved_errno);
  616. return (plural == 0
  617. ? (char *) msgid1
  618. /* Use the Germanic plural rule. */
  619. : n == 1 ? (char *) msgid1 : (char *) msgid2);
  620. }
  621. char *
  622. internal_function
  623. _nl_find_msg (struct loaded_l10nfile *domain_file,
  624. struct binding *domainbinding, const char *msgid,
  625. size_t *lengthp)
  626. {
  627. struct loaded_domain *domain;
  628. nls_uint32 nstrings;
  629. size_t act;
  630. char *result;
  631. size_t resultlen;
  632. if (domain_file->decided == 0)
  633. _nl_load_domain (domain_file, domainbinding);
  634. if (domain_file->data == NULL)
  635. return NULL;
  636. domain = (struct loaded_domain *) domain_file->data;
  637. nstrings = domain->nstrings;
  638. /* Locate the MSGID and its translation. */
  639. if (domain->hash_tab != NULL)
  640. {
  641. /* Use the hashing table. */
  642. nls_uint32 len = strlen (msgid);
  643. nls_uint32 hash_val = hash_string (msgid);
  644. nls_uint32 idx = hash_val % domain->hash_size;
  645. nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
  646. while (1)
  647. {
  648. nls_uint32 nstr =
  649. W (domain->must_swap_hash_tab, domain->hash_tab[idx]);
  650. if (nstr == 0)
  651. /* Hash table entry is empty. */
  652. return NULL;
  653. nstr--;
  654. /* Compare msgid with the original string at index nstr.
  655. We compare the lengths with >=, not ==, because plural entries
  656. are represented by strings with an embedded NUL. */
  657. if (nstr < nstrings
  658. ? W (domain->must_swap, domain->orig_tab[nstr].length) >= len
  659. && (strcmp (msgid,
  660. domain->data + W (domain->must_swap,
  661. domain->orig_tab[nstr].offset))
  662. == 0)
  663. : domain->orig_sysdep_tab[nstr - nstrings].length > len
  664. && (strcmp (msgid,
  665. domain->orig_sysdep_tab[nstr - nstrings].pointer)
  666. == 0))
  667. {
  668. act = nstr;
  669. goto found;
  670. }
  671. if (idx >= domain->hash_size - incr)
  672. idx -= domain->hash_size - incr;
  673. else
  674. idx += incr;
  675. }
  676. /* NOTREACHED */
  677. }
  678. else
  679. {
  680. /* Try the default method: binary search in the sorted array of
  681. messages. */
  682. size_t top, bottom;
  683. bottom = 0;
  684. top = nstrings;
  685. while (bottom < top)
  686. {
  687. int cmp_val;
  688. act = (bottom + top) / 2;
  689. cmp_val = strcmp (msgid, (domain->data
  690. + W (domain->must_swap,
  691. domain->orig_tab[act].offset)));
  692. if (cmp_val < 0)
  693. top = act;
  694. else if (cmp_val > 0)
  695. bottom = act + 1;
  696. else
  697. goto found;
  698. }
  699. /* No translation was found. */
  700. return NULL;
  701. }
  702. found:
  703. /* The translation was found at index ACT. If we have to convert the
  704. string to use a different character set, this is the time. */
  705. if (act < nstrings)
  706. {
  707. result = (char *)
  708. (domain->data + W (domain->must_swap, domain->trans_tab[act].offset));
  709. resultlen = W (domain->must_swap, domain->trans_tab[act].length) + 1;
  710. }
  711. else
  712. {
  713. result = (char *) domain->trans_sysdep_tab[act - nstrings].pointer;
  714. resultlen = domain->trans_sysdep_tab[act - nstrings].length;
  715. }
  716. #if defined _LIBC || HAVE_ICONV
  717. if (domain->codeset_cntr
  718. != (domainbinding != NULL ? domainbinding->codeset_cntr : 0))
  719. {
  720. /* The domain's codeset has changed through bind_textdomain_codeset()
  721. since the message catalog was initialized or last accessed. We
  722. have to reinitialize the converter. */
  723. _nl_free_domain_conv (domain);
  724. _nl_init_domain_conv (domain_file, domain, domainbinding);
  725. }
  726. if (
  727. # ifdef _LIBC
  728. domain->conv != (__gconv_t) -1
  729. # else
  730. # if HAVE_ICONV
  731. domain->conv != (iconv_t) -1
  732. # endif
  733. # endif
  734. )
  735. {
  736. /* We are supposed to do a conversion. First allocate an
  737. appropriate table with the same structure as the table
  738. of translations in the file, where we can put the pointers
  739. to the converted strings in.
  740. There is a slight complication with plural entries. They
  741. are represented by consecutive NUL terminated strings. We
  742. handle this case by converting RESULTLEN bytes, including
  743. NULs. */
  744. if (domain->conv_tab == NULL
  745. && ((domain->conv_tab =
  746. (char **) calloc (nstrings + domain->n_sysdep_strings,
  747. sizeof (char *)))
  748. == NULL))
  749. /* Mark that we didn't succeed allocating a table. */
  750. domain->conv_tab = (char **) -1;
  751. if (__builtin_expect (domain->conv_tab == (char **) -1, 0))
  752. /* Nothing we can do, no more memory. */
  753. goto converted;
  754. if (domain->conv_tab[act] == NULL)
  755. {
  756. /* We haven't used this string so far, so it is not
  757. translated yet. Do this now. */
  758. /* We use a bit more efficient memory handling.
  759. We allocate always larger blocks which get used over
  760. time. This is faster than many small allocations. */
  761. __libc_lock_define_initialized (static, lock)
  762. # define INITIAL_BLOCK_SIZE 4080
  763. static unsigned char *freemem;
  764. static size_t freemem_size;
  765. const unsigned char *inbuf;
  766. unsigned char *outbuf;
  767. int malloc_count;
  768. # ifndef _LIBC
  769. transmem_block_t *transmem_list = NULL;
  770. # endif
  771. __libc_lock_lock (lock);
  772. inbuf = (const unsigned char *) result;
  773. outbuf = freemem + sizeof (size_t);
  774. malloc_count = 0;
  775. while (1)
  776. {
  777. transmem_block_t *newmem;
  778. # ifdef _LIBC
  779. size_t non_reversible;
  780. int res;
  781. if (freemem_size < sizeof (size_t))
  782. goto resize_freemem;
  783. res = __gconv (domain->conv,
  784. &inbuf, inbuf + resultlen,
  785. &outbuf,
  786. outbuf + freemem_size - sizeof (size_t),
  787. &non_reversible);
  788. if (res == __GCONV_OK || res == __GCONV_EMPTY_INPUT)
  789. break;
  790. if (res != __GCONV_FULL_OUTPUT)
  791. {
  792. __libc_lock_unlock (lock);
  793. goto converted;
  794. }
  795. inbuf = result;
  796. # else
  797. # if HAVE_ICONV
  798. const char *inptr = (const char *) inbuf;
  799. size_t inleft = resultlen;
  800. char *outptr = (char *) outbuf;
  801. size_t outleft;
  802. if (freemem_size < sizeof (size_t))
  803. goto resize_freemem;
  804. outleft = freemem_size - sizeof (size_t);
  805. if (iconv (domain->conv,
  806. (ICONV_CONST char **) &inptr, &inleft,
  807. &outptr, &outleft)
  808. != (size_t) (-1))
  809. {
  810. outbuf = (unsigned char *) outptr;
  811. break;
  812. }
  813. if (errno != E2BIG)
  814. {
  815. __libc_lock_unlock (lock);
  816. goto converted;
  817. }
  818. # endif
  819. # endif
  820. resize_freemem:
  821. /* We must allocate a new buffer or resize the old one. */
  822. if (malloc_count > 0)
  823. {
  824. ++malloc_count;
  825. freemem_size = malloc_count * INITIAL_BLOCK_SIZE;
  826. newmem = (transmem_block_t *) realloc (transmem_list,
  827. freemem_size);
  828. # ifdef _LIBC
  829. if (newmem != NULL)
  830. transmem_list = transmem_list->next;
  831. else
  832. {
  833. struct transmem_list *old = transmem_list;
  834. transmem_list = transmem_list->next;
  835. free (old);
  836. }
  837. # endif
  838. }
  839. else
  840. {
  841. malloc_count = 1;
  842. freemem_size = INITIAL_BLOCK_SIZE;
  843. newmem = (transmem_block_t *) malloc (freemem_size);
  844. }
  845. if (__builtin_expect (newmem == NULL, 0))
  846. {
  847. freemem = NULL;
  848. freemem_size = 0;
  849. __libc_lock_unlock (lock);
  850. goto converted;
  851. }
  852. # ifdef _LIBC
  853. /* Add the block to the list of blocks we have to free
  854. at some point. */
  855. newmem->next = transmem_list;
  856. transmem_list = newmem;
  857. freemem = newmem->data;
  858. freemem_size -= offsetof (struct transmem_list, data);
  859. # else
  860. transmem_list = newmem;
  861. freemem = newmem;
  862. # endif
  863. outbuf = freemem + sizeof (size_t);
  864. }
  865. /* We have now in our buffer a converted string. Put this
  866. into the table of conversions. */
  867. *(size_t *) freemem = outbuf - freemem - sizeof (size_t);
  868. domain->conv_tab[act] = (char *) freemem;
  869. /* Shrink freemem, but keep it aligned. */
  870. freemem_size -= outbuf - freemem;
  871. freemem = outbuf;
  872. freemem += freemem_size & (alignof (size_t) - 1);
  873. freemem_size = freemem_size & ~ (alignof (size_t) - 1);
  874. __libc_lock_unlock (lock);
  875. }
  876. /* Now domain->conv_tab[act] contains the translation of all
  877. the plural variants. */
  878. result = domain->conv_tab[act] + sizeof (size_t);
  879. resultlen = *(size_t *) domain->conv_tab[act];
  880. }
  881. converted:
  882. /* The result string is converted. */
  883. #endif /* _LIBC || HAVE_ICONV */
  884. *lengthp = resultlen;
  885. return result;
  886. }
  887. /* Look up a plural variant. */
  888. static char *
  889. internal_function
  890. plural_lookup (struct loaded_l10nfile *domain, unsigned long int n,
  891. const char *translation, size_t translation_len)
  892. {
  893. struct loaded_domain *domaindata = (struct loaded_domain *) domain->data;
  894. unsigned long int index;
  895. const char *p;
  896. index = plural_eval (domaindata->plural, n);
  897. if (index >= domaindata->nplurals)
  898. /* This should never happen. It means the plural expression and the
  899. given maximum value do not match. */
  900. index = 0;
  901. /* Skip INDEX strings at TRANSLATION. */
  902. p = translation;
  903. while (index-- > 0)
  904. {
  905. #ifdef _LIBC
  906. p = __rawmemchr (p, '\0');
  907. #else
  908. p = strchr (p, '\0');
  909. #endif
  910. /* And skip over the NUL byte. */
  911. p++;
  912. if (p >= translation + translation_len)
  913. /* This should never happen. It means the plural expression
  914. evaluated to a value larger than the number of variants
  915. available for MSGID1. */
  916. return (char *) translation;
  917. }
  918. return (char *) p;
  919. }
  920. #ifndef _LIBC
  921. /* Return string representation of locale CATEGORY. */
  922. static const char *
  923. internal_function
  924. category_to_name (int category)
  925. {
  926. const char *retval;
  927. switch (category)
  928. {
  929. #ifdef LC_COLLATE
  930. case LC_COLLATE:
  931. retval = "LC_COLLATE";
  932. break;
  933. #endif
  934. #ifdef LC_CTYPE
  935. case LC_CTYPE:
  936. retval = "LC_CTYPE";
  937. break;
  938. #endif
  939. #ifdef LC_MONETARY
  940. case LC_MONETARY:
  941. retval = "LC_MONETARY";
  942. break;
  943. #endif
  944. #ifdef LC_NUMERIC
  945. case LC_NUMERIC:
  946. retval = "LC_NUMERIC";
  947. break;
  948. #endif
  949. #ifdef LC_TIME
  950. case LC_TIME:
  951. retval = "LC_TIME";
  952. break;
  953. #endif
  954. #ifdef LC_MESSAGES
  955. case LC_MESSAGES:
  956. retval = "LC_MESSAGES";
  957. break;
  958. #endif
  959. #ifdef LC_RESPONSE
  960. case LC_RESPONSE:
  961. retval = "LC_RESPONSE";
  962. break;
  963. #endif
  964. #ifdef LC_ALL
  965. case LC_ALL:
  966. /* This might not make sense but is perhaps better than any other
  967. value. */
  968. retval = "LC_ALL";
  969. break;
  970. #endif
  971. default:
  972. /* If you have a better idea for a default value let me know. */
  973. retval = "LC_XXX";
  974. }
  975. return retval;
  976. }
  977. #endif
  978. /* Guess value of current locale from value of the environment variables. */
  979. static const char *
  980. internal_function
  981. guess_category_value (int category, const char *categoryname)
  982. {
  983. const char *language;
  984. const char *retval;
  985. /* The highest priority value is the `LANGUAGE' environment
  986. variable. But we don't use the value if the currently selected
  987. locale is the C locale. This is a GNU extension. */
  988. language = getenv ("LANGUAGE");
  989. if (language != NULL && language[0] == '\0')
  990. language = NULL;
  991. /* We have to proceed with the POSIX methods of looking to `LC_ALL',
  992. `LC_xxx', and `LANG'. On some systems this can be done by the
  993. `setlocale' function itself. */
  994. #ifdef _LIBC
  995. retval = __current_locale_name (category);
  996. #else
  997. retval = _nl_locale_name (category, categoryname);
  998. #endif
  999. /* Ignore LANGUAGE if the locale is set to "C" because
  1000. 1. "C" locale usually uses the ASCII encoding, and most international
  1001. messages use non-ASCII characters. These characters get displayed
  1002. as question marks (if using glibc's iconv()) or as invalid 8-bit
  1003. characters (because other iconv()s refuse to convert most non-ASCII
  1004. characters to ASCII). In any case, the output is ugly.
  1005. 2. The precise output of some programs in the "C" locale is specified
  1006. by POSIX and should not depend on environment variables like
  1007. "LANGUAGE". We allow such programs to use gettext(). */
  1008. return language != NULL && strcmp (retval, "C") != 0 ? language : retval;
  1009. }
  1010. /* @@ begin of epilog @@ */
  1011. /* We don't want libintl.a to depend on any other library. So we
  1012. avoid the non-standard function stpcpy. In GNU C Library this
  1013. function is available, though. Also allow the symbol HAVE_STPCPY
  1014. to be defined. */
  1015. #if !_LIBC && !HAVE_STPCPY
  1016. static char *
  1017. stpcpy (char *dest, const char *src)
  1018. {
  1019. while ((*dest++ = *src++) != '\0')
  1020. /* Do nothing. */ ;
  1021. return dest - 1;
  1022. }
  1023. #endif
  1024. #if !_LIBC && !HAVE_MEMPCPY
  1025. static void *
  1026. mempcpy (void *dest, const void *src, size_t n)
  1027. {
  1028. return (void *) ((char *) memcpy (dest, src, n) + n);
  1029. }
  1030. #endif
  1031. #ifdef _LIBC
  1032. /* If we want to free all resources we have to do some work at
  1033. program's end. */
  1034. libc_freeres_fn (free_mem)
  1035. {
  1036. void *old;
  1037. while (_nl_domain_bindings != NULL)
  1038. {
  1039. struct binding *oldp = _nl_domain_bindings;
  1040. _nl_domain_bindings = _nl_domain_bindings->next;
  1041. if (oldp->dirname != INTUSE(_nl_default_dirname))
  1042. /* Yes, this is a pointer comparison. */
  1043. free (oldp->dirname);
  1044. free (oldp->codeset);
  1045. free (oldp);
  1046. }
  1047. if (_nl_current_default_domain != _nl_default_default_domain)
  1048. /* Yes, again a pointer comparison. */
  1049. free ((char *) _nl_current_default_domain);
  1050. /* Remove the search tree with the known translations. */
  1051. __tdestroy (root, free);
  1052. root = NULL;
  1053. while (transmem_list != NULL)
  1054. {
  1055. old = transmem_list;
  1056. transmem_list = transmem_list->next;
  1057. free (old);
  1058. }
  1059. }
  1060. #endif