dcigettext.c 33 KB

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