dcigettext.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  1. /* Implementation of the internal dcigettext function.
  2. Copyright (C) 1995-2019 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 Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Tell glibc's <string.h> to provide a prototype for mempcpy().
  14. This must come before <config.h> because <config.h> may include
  15. <features.h>, and once <features.h> has been included, it's too late. */
  16. #ifndef _GNU_SOURCE
  17. # define _GNU_SOURCE 1
  18. #endif
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <sys/types.h>
  23. #ifdef __GNUC__
  24. # define alloca __builtin_alloca
  25. # define HAVE_ALLOCA 1
  26. #else
  27. # ifdef _MSC_VER
  28. # include <malloc.h>
  29. # define alloca _alloca
  30. # else
  31. # if defined HAVE_ALLOCA_H || defined _LIBC
  32. # include <alloca.h>
  33. # else
  34. # ifdef _AIX
  35. #pragma alloca
  36. # else
  37. # ifndef alloca
  38. char *alloca ();
  39. # endif
  40. # endif
  41. # endif
  42. # endif
  43. #endif
  44. #include <errno.h>
  45. #ifndef errno
  46. extern int errno;
  47. #endif
  48. #ifndef __set_errno
  49. # define __set_errno(val) errno = (val)
  50. #endif
  51. #include <stddef.h>
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include <string.h>
  55. #if defined HAVE_UNISTD_H || defined _LIBC
  56. # include <unistd.h>
  57. #endif
  58. #include <locale.h>
  59. #ifdef _LIBC
  60. /* Guess whether integer division by zero raises signal SIGFPE.
  61. Set to 1 only if you know for sure. In case of doubt, set to 0. */
  62. # if defined __alpha__ || defined __arm__ || defined __i386__ \
  63. || defined __m68k__ || defined __s390__
  64. # define INTDIV0_RAISES_SIGFPE 1
  65. # else
  66. # define INTDIV0_RAISES_SIGFPE 0
  67. # endif
  68. #endif
  69. #if !INTDIV0_RAISES_SIGFPE
  70. # include <signal.h>
  71. #endif
  72. #if defined HAVE_SYS_PARAM_H || defined _LIBC
  73. # include <sys/param.h>
  74. #endif
  75. #if !defined _LIBC
  76. # include "localcharset.h"
  77. #endif
  78. #include "gettextP.h"
  79. #include "plural-exp.h"
  80. #ifdef _LIBC
  81. # include <libintl.h>
  82. #else
  83. # ifdef IN_LIBGLOCALE
  84. # include <libintl.h>
  85. # endif
  86. # include "libgnuintl.h"
  87. #endif
  88. #include "hash-string.h"
  89. /* Handle multi-threaded applications. */
  90. #ifdef _LIBC
  91. # include <libc-lock.h>
  92. # define gl_rwlock_define_initialized __libc_rwlock_define_initialized
  93. # define gl_rwlock_rdlock __libc_rwlock_rdlock
  94. # define gl_rwlock_wrlock __libc_rwlock_wrlock
  95. # define gl_rwlock_unlock __libc_rwlock_unlock
  96. #else
  97. # include "lock.h"
  98. #endif
  99. /* Alignment of types. */
  100. #if defined __GNUC__ && __GNUC__ >= 2
  101. # define alignof(TYPE) __alignof__ (TYPE)
  102. #else
  103. # define alignof(TYPE) \
  104. ((int) &((struct { char dummy1; TYPE dummy2; } *) 0)->dummy2)
  105. #endif
  106. /* Some compilers, like SunOS4 cc, don't have offsetof in <stddef.h>. */
  107. #ifndef offsetof
  108. # define offsetof(type,ident) ((size_t)&(((type*)0)->ident))
  109. #endif
  110. /* @@ end of prolog @@ */
  111. #ifdef _LIBC
  112. /* Rename the non ANSI C functions. This is required by the standard
  113. because some ANSI C functions will require linking with this object
  114. file and the name space must not be polluted. */
  115. # define strdup __strdup
  116. # define getcwd __getcwd
  117. # ifndef stpcpy
  118. # define stpcpy __stpcpy
  119. # endif
  120. # define tfind __tfind
  121. #else
  122. # if !defined HAVE_GETCWD
  123. char *getwd ();
  124. # define getcwd(buf, max) getwd (buf)
  125. # else
  126. # if VMS
  127. # define getcwd(buf, max) (getcwd) (buf, max, 0)
  128. # else
  129. char *getcwd ();
  130. # endif
  131. # endif
  132. # ifndef HAVE_STPCPY
  133. static char *stpcpy (char *dest, const char *src);
  134. # endif
  135. # ifndef HAVE_MEMPCPY
  136. static void *mempcpy (void *dest, const void *src, size_t n);
  137. # endif
  138. #endif
  139. /* Use a replacement if the system does not provide the `tsearch' function
  140. family. */
  141. #if defined HAVE_TSEARCH || defined _LIBC
  142. # include <search.h>
  143. #else
  144. # define tsearch libintl_tsearch
  145. # define tfind libintl_tfind
  146. # define tdelete libintl_tdelete
  147. # define twalk libintl_twalk
  148. # include "tsearch.h"
  149. #endif
  150. #ifdef _LIBC
  151. # define tsearch __tsearch
  152. #endif
  153. /* Amount to increase buffer size by in each try. */
  154. #define PATH_INCR 32
  155. /* The following is from pathmax.h. */
  156. /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
  157. PATH_MAX but might cause redefinition warnings when sys/param.h is
  158. later included (as on MORE/BSD 4.3). */
  159. #if defined _POSIX_VERSION || (defined HAVE_LIMITS_H && !defined __GNUC__)
  160. # include <limits.h>
  161. #endif
  162. #ifndef _POSIX_PATH_MAX
  163. # define _POSIX_PATH_MAX 255
  164. #endif
  165. #if !defined PATH_MAX && defined _PC_PATH_MAX
  166. # define PATH_MAX (__pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : __pathconf ("/", _PC_PATH_MAX))
  167. #endif
  168. /* Don't include sys/param.h if it already has been. */
  169. #if defined HAVE_SYS_PARAM_H && !defined PATH_MAX && !defined MAXPATHLEN
  170. # include <sys/param.h>
  171. #endif
  172. #if !defined PATH_MAX && defined MAXPATHLEN
  173. # define PATH_MAX MAXPATHLEN
  174. #endif
  175. #ifndef PATH_MAX
  176. # define PATH_MAX _POSIX_PATH_MAX
  177. #endif
  178. /* Pathname support.
  179. ISSLASH(C) tests whether C is a directory separator character.
  180. IS_ABSOLUTE_PATH(P) tests whether P is an absolute path. If it is not,
  181. it may be concatenated to a directory pathname.
  182. IS_PATH_WITH_DIR(P) tests whether P contains a directory specification.
  183. */
  184. #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
  185. /* Win32, Cygwin, OS/2, DOS */
  186. # define ISSLASH(C) ((C) == '/' || (C) == '\\')
  187. # define HAS_DEVICE(P) \
  188. ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
  189. && (P)[1] == ':')
  190. # define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P))
  191. # define IS_PATH_WITH_DIR(P) \
  192. (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
  193. #else
  194. /* Unix */
  195. # define ISSLASH(C) ((C) == '/')
  196. # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0])
  197. # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
  198. #endif
  199. /* Whether to support different locales in different threads. */
  200. #if defined _LIBC || HAVE_USELOCALE || defined IN_LIBGLOCALE
  201. # define HAVE_PER_THREAD_LOCALE
  202. #endif
  203. /* This is the type used for the search tree where known translations
  204. are stored. */
  205. struct known_translation_t
  206. {
  207. /* Domain in which to search. */
  208. const char *domainname;
  209. /* The category. */
  210. int category;
  211. #ifdef HAVE_PER_THREAD_LOCALE
  212. /* Name of the relevant locale category, or "" for the global locale. */
  213. const char *localename;
  214. #endif
  215. #ifdef IN_LIBGLOCALE
  216. /* The character encoding. */
  217. const char *encoding;
  218. #endif
  219. /* State of the catalog counter at the point the string was found. */
  220. int counter;
  221. /* Catalog where the string was found. */
  222. struct loaded_l10nfile *domain;
  223. /* And finally the translation. */
  224. const char *translation;
  225. size_t translation_length;
  226. /* Pointer to the string in question. */
  227. union
  228. {
  229. char appended[ZERO]; /* used if domain != NULL */
  230. const char *ptr; /* used if domain == NULL */
  231. }
  232. msgid;
  233. };
  234. gl_rwlock_define_initialized (static, tree_lock)
  235. /* Root of the search tree with known translations. */
  236. static void *root;
  237. /* Function to compare two entries in the table of known translations. */
  238. static int
  239. transcmp (const void *p1, const void *p2)
  240. {
  241. const struct known_translation_t *s1;
  242. const struct known_translation_t *s2;
  243. int result;
  244. s1 = (const struct known_translation_t *) p1;
  245. s2 = (const struct known_translation_t *) p2;
  246. result = strcmp (s1->domain != NULL ? s1->msgid.appended : s1->msgid.ptr,
  247. s2->domain != NULL ? s2->msgid.appended : s2->msgid.ptr);
  248. if (result == 0)
  249. {
  250. result = strcmp (s1->domainname, s2->domainname);
  251. if (result == 0)
  252. {
  253. #ifdef HAVE_PER_THREAD_LOCALE
  254. result = strcmp (s1->localename, s2->localename);
  255. if (result == 0)
  256. #endif
  257. {
  258. #ifdef IN_LIBGLOCALE
  259. result = strcmp (s1->encoding, s2->encoding);
  260. if (result == 0)
  261. #endif
  262. /* We compare the category last (though this is the cheapest
  263. operation) since it is hopefully always the same (namely
  264. LC_MESSAGES). */
  265. result = s1->category - s2->category;
  266. }
  267. }
  268. }
  269. return result;
  270. }
  271. /* Name of the default domain used for gettext(3) prior any call to
  272. textdomain(3). The default value for this is "messages". */
  273. const char _nl_default_default_domain[] attribute_hidden = "messages";
  274. #ifndef IN_LIBGLOCALE
  275. /* Value used as the default domain for gettext(3). */
  276. const char *_nl_current_default_domain attribute_hidden
  277. = _nl_default_default_domain;
  278. #endif
  279. /* Contains the default location of the message catalogs. */
  280. #if defined __EMX__
  281. extern const char _nl_default_dirname[];
  282. #else
  283. # ifdef _LIBC
  284. extern const char _nl_default_dirname[];
  285. libc_hidden_proto (_nl_default_dirname)
  286. # endif
  287. const char _nl_default_dirname[] = LOCALEDIR;
  288. # ifdef _LIBC
  289. libc_hidden_data_def (_nl_default_dirname)
  290. # endif
  291. #endif
  292. #ifndef IN_LIBGLOCALE
  293. /* List with bindings of specific domains created by bindtextdomain()
  294. calls. */
  295. struct binding *_nl_domain_bindings;
  296. #endif
  297. /* Prototypes for local functions. */
  298. static char *plural_lookup (struct loaded_l10nfile *domain,
  299. unsigned long int n,
  300. const char *translation, size_t translation_len);
  301. #ifdef IN_LIBGLOCALE
  302. static const char *guess_category_value (int category,
  303. const char *categoryname,
  304. const char *localename);
  305. #else
  306. static const char *guess_category_value (int category,
  307. const char *categoryname);
  308. #endif
  309. #ifdef _LIBC
  310. # include "../locale/localeinfo.h"
  311. # define category_to_name(category) \
  312. _nl_category_names.str + _nl_category_name_idxs[category]
  313. #else
  314. static const char *category_to_name (int category);
  315. #endif
  316. #if (defined _LIBC || HAVE_ICONV) && !defined IN_LIBGLOCALE
  317. static const char *get_output_charset (struct binding *domainbinding);
  318. #endif
  319. /* For those losing systems which don't have `alloca' we have to add
  320. some additional code emulating it. */
  321. #ifdef HAVE_ALLOCA
  322. /* Nothing has to be done. */
  323. # define freea(p) /* nothing */
  324. # define ADD_BLOCK(list, address) /* nothing */
  325. # define FREE_BLOCKS(list) /* nothing */
  326. #else
  327. struct block_list
  328. {
  329. void *address;
  330. struct block_list *next;
  331. };
  332. # define ADD_BLOCK(list, addr) \
  333. do { \
  334. struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
  335. /* If we cannot get a free block we cannot add the new element to \
  336. the list. */ \
  337. if (newp != NULL) { \
  338. newp->address = (addr); \
  339. newp->next = (list); \
  340. (list) = newp; \
  341. } \
  342. } while (0)
  343. # define FREE_BLOCKS(list) \
  344. do { \
  345. while (list != NULL) { \
  346. struct block_list *old = list; \
  347. list = list->next; \
  348. free (old->address); \
  349. free (old); \
  350. } \
  351. } while (0)
  352. # undef alloca
  353. # define alloca(size) (malloc (size))
  354. # define freea(p) free (p)
  355. #endif /* have alloca */
  356. #ifdef _LIBC
  357. /* List of blocks allocated for translations. */
  358. typedef struct transmem_list
  359. {
  360. struct transmem_list *next;
  361. char data[ZERO];
  362. } transmem_block_t;
  363. static struct transmem_list *transmem_list;
  364. #else
  365. typedef unsigned char transmem_block_t;
  366. #endif
  367. /* Names for the libintl functions are a problem. They must not clash
  368. with existing names and they should follow ANSI C. But this source
  369. code is also used in GNU C Library where the names have a __
  370. prefix. So we have to make a difference here. */
  371. #ifdef _LIBC
  372. # define DCIGETTEXT __dcigettext
  373. #else
  374. # define DCIGETTEXT libintl_dcigettext
  375. #endif
  376. /* Lock variable to protect the global data in the gettext implementation. */
  377. gl_rwlock_define_initialized (, _nl_state_lock attribute_hidden)
  378. /* Checking whether the binaries runs SUID must be done and glibc provides
  379. easier methods therefore we make a difference here. */
  380. #ifdef _LIBC
  381. # define ENABLE_SECURE __libc_enable_secure
  382. # define DETERMINE_SECURE
  383. #else
  384. # ifndef HAVE_GETUID
  385. # define getuid() 0
  386. # endif
  387. # ifndef HAVE_GETGID
  388. # define getgid() 0
  389. # endif
  390. # ifndef HAVE_GETEUID
  391. # define geteuid() getuid()
  392. # endif
  393. # ifndef HAVE_GETEGID
  394. # define getegid() getgid()
  395. # endif
  396. static int enable_secure;
  397. # define ENABLE_SECURE (enable_secure == 1)
  398. # define DETERMINE_SECURE \
  399. if (enable_secure == 0) \
  400. { \
  401. if (getuid () != geteuid () || getgid () != getegid ()) \
  402. enable_secure = 1; \
  403. else \
  404. enable_secure = -1; \
  405. }
  406. #endif
  407. /* Get the function to evaluate the plural expression. */
  408. #include "eval-plural.h"
  409. /* Look up MSGID in the DOMAINNAME message catalog for the current
  410. CATEGORY locale and, if PLURAL is nonzero, search over string
  411. depending on the plural form determined by N. */
  412. #ifdef IN_LIBGLOCALE
  413. char *
  414. gl_dcigettext (const char *domainname,
  415. const char *msgid1, const char *msgid2,
  416. int plural, unsigned long int n,
  417. int category,
  418. const char *localename, const char *encoding)
  419. #else
  420. char *
  421. DCIGETTEXT (const char *domainname, const char *msgid1, const char *msgid2,
  422. int plural, unsigned long int n, int category)
  423. #endif
  424. {
  425. #ifndef HAVE_ALLOCA
  426. struct block_list *block_list = NULL;
  427. #endif
  428. struct loaded_l10nfile *domain;
  429. struct binding *binding;
  430. const char *categoryname;
  431. const char *categoryvalue;
  432. const char *dirname;
  433. char *xdirname = NULL;
  434. char *xdomainname;
  435. char *single_locale;
  436. char *retval;
  437. size_t retlen;
  438. int saved_errno;
  439. struct known_translation_t search;
  440. struct known_translation_t **foundp = NULL;
  441. #if defined HAVE_PER_THREAD_LOCALE && !defined IN_LIBGLOCALE
  442. const char *localename;
  443. #endif
  444. size_t domainname_len;
  445. /* If no real MSGID is given return NULL. */
  446. if (msgid1 == NULL)
  447. return NULL;
  448. #ifdef _LIBC
  449. if (category < 0 || category >= __LC_LAST || category == LC_ALL)
  450. /* Bogus. */
  451. return (plural == 0
  452. ? (char *) msgid1
  453. /* Use the Germanic plural rule. */
  454. : n == 1 ? (char *) msgid1 : (char *) msgid2);
  455. #endif
  456. /* Preserve the `errno' value. */
  457. saved_errno = errno;
  458. #ifdef _LIBC
  459. __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden)
  460. __libc_rwlock_rdlock (__libc_setlocale_lock);
  461. #endif
  462. gl_rwlock_rdlock (_nl_state_lock);
  463. /* If DOMAINNAME is NULL, we are interested in the default domain. If
  464. CATEGORY is not LC_MESSAGES this might not make much sense but the
  465. definition left this undefined. */
  466. if (domainname == NULL)
  467. domainname = _nl_current_default_domain;
  468. /* OS/2 specific: backward compatibility with older libintl versions */
  469. #ifdef LC_MESSAGES_COMPAT
  470. if (category == LC_MESSAGES_COMPAT)
  471. category = LC_MESSAGES;
  472. #endif
  473. /* Try to find the translation among those which we found at
  474. some time. */
  475. search.domain = NULL;
  476. search.msgid.ptr = msgid1;
  477. search.domainname = domainname;
  478. search.category = category;
  479. #ifdef HAVE_PER_THREAD_LOCALE
  480. # ifndef IN_LIBGLOCALE
  481. # ifdef _LIBC
  482. localename = __current_locale_name (category);
  483. # else
  484. categoryname = category_to_name (category);
  485. # define CATEGORYNAME_INITIALIZED
  486. localename = _nl_locale_name_thread_unsafe (category, categoryname);
  487. if (localename == NULL)
  488. localename = "";
  489. # endif
  490. # endif
  491. search.localename = localename;
  492. # ifdef IN_LIBGLOCALE
  493. search.encoding = encoding;
  494. # endif
  495. /* Since tfind/tsearch manage a balanced tree, concurrent tfind and
  496. tsearch calls can be fatal. */
  497. gl_rwlock_rdlock (tree_lock);
  498. foundp = (struct known_translation_t **) tfind (&search, &root, transcmp);
  499. gl_rwlock_unlock (tree_lock);
  500. if (foundp != NULL && (*foundp)->counter == _nl_msg_cat_cntr)
  501. {
  502. /* Now deal with plural. */
  503. if (plural)
  504. retval = plural_lookup ((*foundp)->domain, n, (*foundp)->translation,
  505. (*foundp)->translation_length);
  506. else
  507. retval = (char *) (*foundp)->translation;
  508. gl_rwlock_unlock (_nl_state_lock);
  509. # ifdef _LIBC
  510. __libc_rwlock_unlock (__libc_setlocale_lock);
  511. # endif
  512. __set_errno (saved_errno);
  513. return retval;
  514. }
  515. #endif
  516. /* See whether this is a SUID binary or not. */
  517. DETERMINE_SECURE;
  518. /* First find matching binding. */
  519. #ifdef IN_LIBGLOCALE
  520. /* We can use a trivial binding, since _nl_find_msg will ignore it anyway,
  521. and _nl_load_domain and _nl_find_domain just pass it through. */
  522. binding = NULL;
  523. dirname = bindtextdomain (domainname, NULL);
  524. #else
  525. for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
  526. {
  527. int compare = strcmp (domainname, binding->domainname);
  528. if (compare == 0)
  529. /* We found it! */
  530. break;
  531. if (compare < 0)
  532. {
  533. /* It is not in the list. */
  534. binding = NULL;
  535. break;
  536. }
  537. }
  538. if (binding == NULL)
  539. dirname = _nl_default_dirname;
  540. else
  541. {
  542. dirname = binding->dirname;
  543. #endif
  544. if (!IS_ABSOLUTE_PATH (dirname))
  545. {
  546. /* We have a relative path. Make it absolute now. */
  547. char *cwd = getcwd (NULL, 0);
  548. if (cwd == NULL)
  549. /* We cannot get the current working directory. Don't
  550. signal an error but simply return the default
  551. string. */
  552. goto return_untranslated;
  553. int ret = __asprintf (&xdirname, "%s/%s", cwd, dirname);
  554. free (cwd);
  555. if (ret < 0)
  556. goto return_untranslated;
  557. dirname = xdirname;
  558. }
  559. #ifndef IN_LIBGLOCALE
  560. }
  561. #endif
  562. /* Now determine the symbolic name of CATEGORY and its value. */
  563. #ifndef CATEGORYNAME_INITIALIZED
  564. categoryname = category_to_name (category);
  565. #endif
  566. #ifdef IN_LIBGLOCALE
  567. categoryvalue = guess_category_value (category, categoryname, localename);
  568. #else
  569. categoryvalue = guess_category_value (category, categoryname);
  570. #endif
  571. domainname_len = strlen (domainname);
  572. xdomainname = (char *) alloca (strlen (categoryname)
  573. + domainname_len + 5);
  574. ADD_BLOCK (block_list, xdomainname);
  575. stpcpy ((char *) mempcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
  576. domainname, domainname_len),
  577. ".mo");
  578. /* Creating working area. */
  579. single_locale = (char *) alloca (strlen (categoryvalue) + 1);
  580. ADD_BLOCK (block_list, single_locale);
  581. /* Search for the given string. This is a loop because we perhaps
  582. got an ordered list of languages to consider for the translation. */
  583. while (1)
  584. {
  585. /* Make CATEGORYVALUE point to the next element of the list. */
  586. while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
  587. ++categoryvalue;
  588. if (categoryvalue[0] == '\0')
  589. {
  590. /* The whole contents of CATEGORYVALUE has been searched but
  591. no valid entry has been found. We solve this situation
  592. by implicitly appending a "C" entry, i.e. no translation
  593. will take place. */
  594. single_locale[0] = 'C';
  595. single_locale[1] = '\0';
  596. }
  597. else
  598. {
  599. char *cp = single_locale;
  600. while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
  601. *cp++ = *categoryvalue++;
  602. *cp = '\0';
  603. /* When this is a SUID binary we must not allow accessing files
  604. outside the dedicated directories. */
  605. if (ENABLE_SECURE && IS_PATH_WITH_DIR (single_locale))
  606. /* Ingore this entry. */
  607. continue;
  608. }
  609. /* If the current locale value is C (or POSIX) we don't load a
  610. domain. Return the MSGID. */
  611. if (strcmp (single_locale, "C") == 0
  612. || strcmp (single_locale, "POSIX") == 0)
  613. break;
  614. /* Find structure describing the message catalog matching the
  615. DOMAINNAME and CATEGORY. */
  616. domain = _nl_find_domain (dirname, single_locale, xdomainname, binding);
  617. if (domain != NULL)
  618. {
  619. #if defined IN_LIBGLOCALE
  620. retval = _nl_find_msg (domain, binding, encoding, msgid1, &retlen);
  621. #else
  622. retval = _nl_find_msg (domain, binding, msgid1, 1, &retlen);
  623. #endif
  624. if (retval == NULL)
  625. {
  626. int cnt;
  627. for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
  628. {
  629. #if defined IN_LIBGLOCALE
  630. retval = _nl_find_msg (domain->successor[cnt], binding,
  631. encoding, msgid1, &retlen);
  632. #else
  633. retval = _nl_find_msg (domain->successor[cnt], binding,
  634. msgid1, 1, &retlen);
  635. #endif
  636. /* Resource problems are not fatal, instead we return no
  637. translation. */
  638. if (__builtin_expect (retval == (char *) -1, 0))
  639. goto return_untranslated;
  640. if (retval != NULL)
  641. {
  642. domain = domain->successor[cnt];
  643. break;
  644. }
  645. }
  646. }
  647. /* Returning -1 means that some resource problem exists
  648. (likely memory) and that the strings could not be
  649. converted. Return the original strings. */
  650. if (__builtin_expect (retval == (char *) -1, 0))
  651. break;
  652. if (retval != NULL)
  653. {
  654. /* Found the translation of MSGID1 in domain DOMAIN:
  655. starting at RETVAL, RETLEN bytes. */
  656. free (xdirname);
  657. FREE_BLOCKS (block_list);
  658. if (foundp == NULL)
  659. {
  660. /* Create a new entry and add it to the search tree. */
  661. size_t msgid_len;
  662. size_t size;
  663. struct known_translation_t *newp;
  664. msgid_len = strlen (msgid1) + 1;
  665. size = offsetof (struct known_translation_t, msgid)
  666. + msgid_len + domainname_len + 1;
  667. #ifdef HAVE_PER_THREAD_LOCALE
  668. size += strlen (localename) + 1;
  669. #endif
  670. newp = (struct known_translation_t *) malloc (size);
  671. if (newp != NULL)
  672. {
  673. char *new_domainname;
  674. #ifdef HAVE_PER_THREAD_LOCALE
  675. char *new_localename;
  676. #endif
  677. new_domainname =
  678. (char *) mempcpy (newp->msgid.appended, msgid1,
  679. msgid_len);
  680. memcpy (new_domainname, domainname, domainname_len + 1);
  681. #ifdef HAVE_PER_THREAD_LOCALE
  682. new_localename = new_domainname + domainname_len + 1;
  683. strcpy (new_localename, localename);
  684. #endif
  685. newp->domainname = new_domainname;
  686. newp->category = category;
  687. #ifdef HAVE_PER_THREAD_LOCALE
  688. newp->localename = new_localename;
  689. #endif
  690. #ifdef IN_LIBGLOCALE
  691. newp->encoding = encoding;
  692. #endif
  693. newp->counter = _nl_msg_cat_cntr;
  694. newp->domain = domain;
  695. newp->translation = retval;
  696. newp->translation_length = retlen;
  697. gl_rwlock_wrlock (tree_lock);
  698. /* Insert the entry in the search tree. */
  699. foundp = (struct known_translation_t **)
  700. tsearch (newp, &root, transcmp);
  701. gl_rwlock_unlock (tree_lock);
  702. if (foundp == NULL
  703. || __builtin_expect (*foundp != newp, 0))
  704. /* The insert failed. */
  705. free (newp);
  706. }
  707. }
  708. else
  709. {
  710. /* We can update the existing entry. */
  711. (*foundp)->counter = _nl_msg_cat_cntr;
  712. (*foundp)->domain = domain;
  713. (*foundp)->translation = retval;
  714. (*foundp)->translation_length = retlen;
  715. }
  716. __set_errno (saved_errno);
  717. /* Now deal with plural. */
  718. if (plural)
  719. retval = plural_lookup (domain, n, retval, retlen);
  720. gl_rwlock_unlock (_nl_state_lock);
  721. #ifdef _LIBC
  722. __libc_rwlock_unlock (__libc_setlocale_lock);
  723. #endif
  724. return retval;
  725. }
  726. }
  727. }
  728. return_untranslated:
  729. /* Return the untranslated MSGID. */
  730. free (xdirname);
  731. FREE_BLOCKS (block_list);
  732. gl_rwlock_unlock (_nl_state_lock);
  733. #ifdef _LIBC
  734. __libc_rwlock_unlock (__libc_setlocale_lock);
  735. #endif
  736. #ifndef _LIBC
  737. if (!ENABLE_SECURE)
  738. {
  739. extern void _nl_log_untranslated (const char *logfilename,
  740. const char *domainname,
  741. const char *msgid1, const char *msgid2,
  742. int plural);
  743. const char *logfilename = getenv ("GETTEXT_LOG_UNTRANSLATED");
  744. if (logfilename != NULL && logfilename[0] != '\0')
  745. _nl_log_untranslated (logfilename, domainname, msgid1, msgid2, plural);
  746. }
  747. #endif
  748. __set_errno (saved_errno);
  749. return (plural == 0
  750. ? (char *) msgid1
  751. /* Use the Germanic plural rule. */
  752. : n == 1 ? (char *) msgid1 : (char *) msgid2);
  753. }
  754. /* Look up the translation of msgid within DOMAIN_FILE and DOMAINBINDING.
  755. Return it if found. Return NULL if not found or in case of a conversion
  756. failure (problem in the particular message catalog). Return (char *) -1
  757. in case of a memory allocation failure during conversion (only if
  758. ENCODING != NULL resp. CONVERT == true). */
  759. char *
  760. #ifdef IN_LIBGLOCALE
  761. _nl_find_msg (struct loaded_l10nfile *domain_file,
  762. struct binding *domainbinding, const char *encoding,
  763. const char *msgid,
  764. size_t *lengthp)
  765. #else
  766. _nl_find_msg (struct loaded_l10nfile *domain_file,
  767. struct binding *domainbinding,
  768. const char *msgid, int convert,
  769. size_t *lengthp)
  770. #endif
  771. {
  772. struct loaded_domain *domain;
  773. nls_uint32 nstrings;
  774. size_t act;
  775. char *result;
  776. size_t resultlen;
  777. if (domain_file->decided <= 0)
  778. _nl_load_domain (domain_file, domainbinding);
  779. if (domain_file->data == NULL)
  780. return NULL;
  781. domain = (struct loaded_domain *) domain_file->data;
  782. nstrings = domain->nstrings;
  783. /* Locate the MSGID and its translation. */
  784. if (domain->hash_tab != NULL)
  785. {
  786. /* Use the hashing table. */
  787. nls_uint32 len = strlen (msgid);
  788. nls_uint32 hash_val = __hash_string (msgid);
  789. nls_uint32 idx = hash_val % domain->hash_size;
  790. nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
  791. while (1)
  792. {
  793. nls_uint32 nstr =
  794. W (domain->must_swap_hash_tab, domain->hash_tab[idx]);
  795. if (nstr == 0)
  796. /* Hash table entry is empty. */
  797. return NULL;
  798. nstr--;
  799. /* Compare msgid with the original string at index nstr.
  800. We compare the lengths with >=, not ==, because plural entries
  801. are represented by strings with an embedded NUL. */
  802. if (nstr < nstrings
  803. ? W (domain->must_swap, domain->orig_tab[nstr].length) >= len
  804. && (strcmp (msgid,
  805. domain->data + W (domain->must_swap,
  806. domain->orig_tab[nstr].offset))
  807. == 0)
  808. : domain->orig_sysdep_tab[nstr - nstrings].length > len
  809. && (strcmp (msgid,
  810. domain->orig_sysdep_tab[nstr - nstrings].pointer)
  811. == 0))
  812. {
  813. act = nstr;
  814. goto found;
  815. }
  816. if (idx >= domain->hash_size - incr)
  817. idx -= domain->hash_size - incr;
  818. else
  819. idx += incr;
  820. }
  821. /* NOTREACHED */
  822. }
  823. else
  824. {
  825. /* Try the default method: binary search in the sorted array of
  826. messages. */
  827. size_t top, bottom;
  828. bottom = 0;
  829. top = nstrings;
  830. while (bottom < top)
  831. {
  832. int cmp_val;
  833. act = (bottom + top) / 2;
  834. cmp_val = strcmp (msgid, (domain->data
  835. + W (domain->must_swap,
  836. domain->orig_tab[act].offset)));
  837. if (cmp_val < 0)
  838. top = act;
  839. else if (cmp_val > 0)
  840. bottom = act + 1;
  841. else
  842. goto found;
  843. }
  844. /* No translation was found. */
  845. return NULL;
  846. }
  847. found:
  848. /* The translation was found at index ACT. If we have to convert the
  849. string to use a different character set, this is the time. */
  850. if (act < nstrings)
  851. {
  852. result = (char *)
  853. (domain->data + W (domain->must_swap, domain->trans_tab[act].offset));
  854. resultlen = W (domain->must_swap, domain->trans_tab[act].length) + 1;
  855. }
  856. else
  857. {
  858. result = (char *) domain->trans_sysdep_tab[act - nstrings].pointer;
  859. resultlen = domain->trans_sysdep_tab[act - nstrings].length;
  860. }
  861. #if defined _LIBC || HAVE_ICONV
  862. # ifdef IN_LIBGLOCALE
  863. if (encoding != NULL)
  864. # else
  865. if (convert)
  866. # endif
  867. {
  868. /* We are supposed to do a conversion. */
  869. # ifndef IN_LIBGLOCALE
  870. const char *encoding = get_output_charset (domainbinding);
  871. # endif
  872. size_t nconversions;
  873. struct converted_domain *convd;
  874. size_t i;
  875. /* Protect against reallocation of the table. */
  876. gl_rwlock_rdlock (domain->conversions_lock);
  877. /* Search whether a table with converted translations for this
  878. encoding has already been allocated. */
  879. nconversions = domain->nconversions;
  880. convd = NULL;
  881. for (i = nconversions; i > 0; )
  882. {
  883. i--;
  884. if (strcmp (domain->conversions[i].encoding, encoding) == 0)
  885. {
  886. convd = &domain->conversions[i];
  887. break;
  888. }
  889. }
  890. gl_rwlock_unlock (domain->conversions_lock);
  891. if (convd == NULL)
  892. {
  893. /* We have to allocate a new conversions table. */
  894. gl_rwlock_wrlock (domain->conversions_lock);
  895. nconversions = domain->nconversions;
  896. /* Maybe in the meantime somebody added the translation.
  897. Recheck. */
  898. for (i = nconversions; i > 0; )
  899. {
  900. i--;
  901. if (strcmp (domain->conversions[i].encoding, encoding) == 0)
  902. {
  903. convd = &domain->conversions[i];
  904. goto found_convd;
  905. }
  906. }
  907. {
  908. /* Allocate a table for the converted translations for this
  909. encoding. */
  910. struct converted_domain *new_conversions =
  911. (struct converted_domain *)
  912. (domain->conversions != NULL
  913. ? realloc (domain->conversions,
  914. (nconversions + 1) * sizeof (struct converted_domain))
  915. : malloc ((nconversions + 1) * sizeof (struct converted_domain)));
  916. if (__builtin_expect (new_conversions == NULL, 0))
  917. {
  918. /* Nothing we can do, no more memory. We cannot use the
  919. translation because it might be encoded incorrectly. */
  920. unlock_fail:
  921. gl_rwlock_unlock (domain->conversions_lock);
  922. return (char *) -1;
  923. }
  924. domain->conversions = new_conversions;
  925. /* Copy the 'encoding' string to permanent storage. */
  926. encoding = strdup (encoding);
  927. if (__builtin_expect (encoding == NULL, 0))
  928. /* Nothing we can do, no more memory. We cannot use the
  929. translation because it might be encoded incorrectly. */
  930. goto unlock_fail;
  931. convd = &new_conversions[nconversions];
  932. convd->encoding = encoding;
  933. /* Find out about the character set the file is encoded with.
  934. This can be found (in textual form) in the entry "". If this
  935. entry does not exist or if this does not contain the 'charset='
  936. information, we will assume the charset matches the one the
  937. current locale and we don't have to perform any conversion. */
  938. # ifdef _LIBC
  939. convd->conv = (__gconv_t) -1;
  940. # else
  941. # if HAVE_ICONV
  942. convd->conv = (iconv_t) -1;
  943. # endif
  944. # endif
  945. {
  946. char *nullentry;
  947. size_t nullentrylen;
  948. /* Get the header entry. This is a recursion, but it doesn't
  949. reallocate domain->conversions because we pass
  950. encoding = NULL or convert = 0, respectively. */
  951. nullentry =
  952. # ifdef IN_LIBGLOCALE
  953. _nl_find_msg (domain_file, domainbinding, NULL, "",
  954. &nullentrylen);
  955. # else
  956. _nl_find_msg (domain_file, domainbinding, "", 0, &nullentrylen);
  957. # endif
  958. /* Resource problems are fatal. If we continue onwards we will
  959. only attempt to calloc a new conv_tab and fail later. */
  960. if (__builtin_expect (nullentry == (char *) -1, 0))
  961. return (char *) -1;
  962. if (nullentry != NULL)
  963. {
  964. const char *charsetstr;
  965. charsetstr = strstr (nullentry, "charset=");
  966. if (charsetstr != NULL)
  967. {
  968. size_t len;
  969. char *charset;
  970. const char *outcharset;
  971. charsetstr += strlen ("charset=");
  972. len = strcspn (charsetstr, " \t\n");
  973. charset = (char *) alloca (len + 1);
  974. # if defined _LIBC || HAVE_MEMPCPY
  975. *((char *) mempcpy (charset, charsetstr, len)) = '\0';
  976. # else
  977. memcpy (charset, charsetstr, len);
  978. charset[len] = '\0';
  979. # endif
  980. outcharset = encoding;
  981. # ifdef _LIBC
  982. /* We always want to use transliteration. */
  983. outcharset = norm_add_slashes (outcharset, "TRANSLIT");
  984. charset = norm_add_slashes (charset, "");
  985. int r = __gconv_open (outcharset, charset, &convd->conv,
  986. GCONV_AVOID_NOCONV);
  987. if (__builtin_expect (r != __GCONV_OK, 0))
  988. {
  989. /* If the output encoding is the same there is
  990. nothing to do. Otherwise do not use the
  991. translation at all. */
  992. if (__builtin_expect (r != __GCONV_NULCONV, 1))
  993. {
  994. gl_rwlock_unlock (domain->conversions_lock);
  995. free ((char *) encoding);
  996. return NULL;
  997. }
  998. convd->conv = (__gconv_t) -1;
  999. }
  1000. # else
  1001. # if HAVE_ICONV
  1002. /* When using GNU libc >= 2.2 or GNU libiconv >= 1.5,
  1003. we want to use transliteration. */
  1004. # if (((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2) \
  1005. && !defined __UCLIBC__) \
  1006. || _LIBICONV_VERSION >= 0x0105
  1007. if (strchr (outcharset, '/') == NULL)
  1008. {
  1009. char *tmp;
  1010. len = strlen (outcharset);
  1011. tmp = (char *) alloca (len + 10 + 1);
  1012. memcpy (tmp, outcharset, len);
  1013. memcpy (tmp + len, "//TRANSLIT", 10 + 1);
  1014. outcharset = tmp;
  1015. convd->conv = iconv_open (outcharset, charset);
  1016. freea (outcharset);
  1017. }
  1018. else
  1019. # endif
  1020. convd->conv = iconv_open (outcharset, charset);
  1021. # endif
  1022. # endif
  1023. freea (charset);
  1024. }
  1025. }
  1026. }
  1027. convd->conv_tab = NULL;
  1028. /* Here domain->conversions is still == new_conversions. */
  1029. domain->nconversions++;
  1030. }
  1031. found_convd:
  1032. gl_rwlock_unlock (domain->conversions_lock);
  1033. }
  1034. if (
  1035. # ifdef _LIBC
  1036. convd->conv != (__gconv_t) -1
  1037. # else
  1038. # if HAVE_ICONV
  1039. convd->conv != (iconv_t) -1
  1040. # endif
  1041. # endif
  1042. )
  1043. {
  1044. /* We are supposed to do a conversion. First allocate an
  1045. appropriate table with the same structure as the table
  1046. of translations in the file, where we can put the pointers
  1047. to the converted strings in.
  1048. There is a slight complication with plural entries. They
  1049. are represented by consecutive NUL terminated strings. We
  1050. handle this case by converting RESULTLEN bytes, including
  1051. NULs. */
  1052. /* This lock primarily protects the memory management variables
  1053. freemem, freemem_size. It also protects write accesses to
  1054. convd->conv_tab. It's not worth using a separate lock (such
  1055. as domain->conversions_lock) for this purpose, because when
  1056. modifying convd->conv_tab, we also need to lock freemem,
  1057. freemem_size for most of the time. */
  1058. __libc_lock_define_initialized (static, lock)
  1059. if (__builtin_expect (convd->conv_tab == NULL, 0))
  1060. {
  1061. __libc_lock_lock (lock);
  1062. if (convd->conv_tab == NULL)
  1063. {
  1064. convd->conv_tab =
  1065. (char **) calloc (nstrings + domain->n_sysdep_strings,
  1066. sizeof (char *));
  1067. if (convd->conv_tab != NULL)
  1068. goto not_translated_yet;
  1069. /* Mark that we didn't succeed allocating a table. */
  1070. convd->conv_tab = (char **) -1;
  1071. }
  1072. __libc_lock_unlock (lock);
  1073. }
  1074. if (__builtin_expect (convd->conv_tab == (char **) -1, 0))
  1075. /* Nothing we can do, no more memory. We cannot use the
  1076. translation because it might be encoded incorrectly. */
  1077. return (char *) -1;
  1078. if (convd->conv_tab[act] == NULL)
  1079. {
  1080. /* We haven't used this string so far, so it is not
  1081. translated yet. Do this now. */
  1082. /* We use a bit more efficient memory handling.
  1083. We allocate always larger blocks which get used over
  1084. time. This is faster than many small allocations. */
  1085. # define INITIAL_BLOCK_SIZE 4080
  1086. static unsigned char *freemem;
  1087. static size_t freemem_size;
  1088. const unsigned char *inbuf;
  1089. unsigned char *outbuf;
  1090. int malloc_count;
  1091. # ifndef _LIBC
  1092. transmem_block_t *transmem_list;
  1093. # endif
  1094. __libc_lock_lock (lock);
  1095. not_translated_yet:
  1096. inbuf = (const unsigned char *) result;
  1097. outbuf = freemem + sizeof (size_t);
  1098. # ifndef _LIBC
  1099. transmem_list = NULL;
  1100. # endif
  1101. malloc_count = 0;
  1102. while (1)
  1103. {
  1104. transmem_block_t *newmem;
  1105. # ifdef _LIBC
  1106. size_t non_reversible;
  1107. int res;
  1108. if (freemem_size < sizeof (size_t))
  1109. goto resize_freemem;
  1110. res = __gconv (convd->conv,
  1111. &inbuf, inbuf + resultlen,
  1112. &outbuf,
  1113. outbuf + freemem_size - sizeof (size_t),
  1114. &non_reversible);
  1115. if (res == __GCONV_OK || res == __GCONV_EMPTY_INPUT)
  1116. break;
  1117. if (res != __GCONV_FULL_OUTPUT)
  1118. {
  1119. /* We should not use the translation at all, it
  1120. is incorrectly encoded. */
  1121. __libc_lock_unlock (lock);
  1122. return NULL;
  1123. }
  1124. inbuf = (const unsigned char *) result;
  1125. # else
  1126. # if HAVE_ICONV
  1127. const char *inptr = (const char *) inbuf;
  1128. size_t inleft = resultlen;
  1129. char *outptr = (char *) outbuf;
  1130. size_t outleft;
  1131. if (freemem_size < sizeof (size_t))
  1132. goto resize_freemem;
  1133. outleft = freemem_size - sizeof (size_t);
  1134. if (iconv (convd->conv,
  1135. (ICONV_CONST char **) &inptr, &inleft,
  1136. &outptr, &outleft)
  1137. != (size_t) (-1))
  1138. {
  1139. outbuf = (unsigned char *) outptr;
  1140. break;
  1141. }
  1142. if (errno != E2BIG)
  1143. {
  1144. __libc_lock_unlock (lock);
  1145. return NULL;
  1146. }
  1147. # endif
  1148. # endif
  1149. resize_freemem:
  1150. /* We must allocate a new buffer or resize the old one. */
  1151. if (malloc_count > 0)
  1152. {
  1153. ++malloc_count;
  1154. freemem_size = malloc_count * INITIAL_BLOCK_SIZE;
  1155. newmem = (transmem_block_t *) realloc (transmem_list,
  1156. freemem_size);
  1157. # ifdef _LIBC
  1158. if (newmem != NULL)
  1159. transmem_list = newmem;
  1160. else
  1161. {
  1162. struct transmem_list *old = transmem_list;
  1163. transmem_list = transmem_list->next;
  1164. free (old);
  1165. }
  1166. # endif
  1167. }
  1168. else
  1169. {
  1170. malloc_count = 1;
  1171. freemem_size = INITIAL_BLOCK_SIZE;
  1172. newmem = (transmem_block_t *) malloc (freemem_size);
  1173. # ifdef _LIBC
  1174. if (newmem != NULL)
  1175. {
  1176. /* Add the block to the list of blocks we have to free
  1177. at some point. */
  1178. newmem->next = transmem_list;
  1179. transmem_list = newmem;
  1180. }
  1181. /* Fall through and return -1. */
  1182. # endif
  1183. }
  1184. if (__builtin_expect (newmem == NULL, 0))
  1185. {
  1186. freemem = NULL;
  1187. freemem_size = 0;
  1188. __libc_lock_unlock (lock);
  1189. return (char *) -1;
  1190. }
  1191. # ifdef _LIBC
  1192. freemem = (unsigned char *) newmem->data;
  1193. freemem_size -= offsetof (struct transmem_list, data);
  1194. # else
  1195. transmem_list = newmem;
  1196. freemem = newmem;
  1197. # endif
  1198. outbuf = freemem + sizeof (size_t);
  1199. }
  1200. /* We have now in our buffer a converted string. Put this
  1201. into the table of conversions. */
  1202. *(size_t *) freemem = outbuf - freemem - sizeof (size_t);
  1203. convd->conv_tab[act] = (char *) freemem;
  1204. /* Shrink freemem, but keep it aligned. */
  1205. freemem_size -= outbuf - freemem;
  1206. freemem = outbuf;
  1207. freemem += freemem_size & (alignof (size_t) - 1);
  1208. freemem_size = freemem_size & ~ (alignof (size_t) - 1);
  1209. __libc_lock_unlock (lock);
  1210. }
  1211. /* Now convd->conv_tab[act] contains the translation of all
  1212. the plural variants. */
  1213. result = convd->conv_tab[act] + sizeof (size_t);
  1214. resultlen = *(size_t *) convd->conv_tab[act];
  1215. }
  1216. }
  1217. /* The result string is converted. */
  1218. #endif /* _LIBC || HAVE_ICONV */
  1219. *lengthp = resultlen;
  1220. return result;
  1221. }
  1222. /* Look up a plural variant. */
  1223. static char *
  1224. plural_lookup (struct loaded_l10nfile *domain, unsigned long int n,
  1225. const char *translation, size_t translation_len)
  1226. {
  1227. struct loaded_domain *domaindata = (struct loaded_domain *) domain->data;
  1228. unsigned long int index;
  1229. const char *p;
  1230. index = plural_eval (domaindata->plural, n);
  1231. if (index >= domaindata->nplurals)
  1232. /* This should never happen. It means the plural expression and the
  1233. given maximum value do not match. */
  1234. index = 0;
  1235. /* Skip INDEX strings at TRANSLATION. */
  1236. p = translation;
  1237. while (index-- > 0)
  1238. {
  1239. #ifdef _LIBC
  1240. p = __rawmemchr (p, '\0');
  1241. #else
  1242. p = strchr (p, '\0');
  1243. #endif
  1244. /* And skip over the NUL byte. */
  1245. p++;
  1246. if (p >= translation + translation_len)
  1247. /* This should never happen. It means the plural expression
  1248. evaluated to a value larger than the number of variants
  1249. available for MSGID1. */
  1250. return (char *) translation;
  1251. }
  1252. return (char *) p;
  1253. }
  1254. #ifndef _LIBC
  1255. /* Return string representation of locale CATEGORY. */
  1256. static const char *
  1257. category_to_name (int category)
  1258. {
  1259. const char *retval;
  1260. switch (category)
  1261. {
  1262. #ifdef LC_COLLATE
  1263. case LC_COLLATE:
  1264. retval = "LC_COLLATE";
  1265. break;
  1266. #endif
  1267. #ifdef LC_CTYPE
  1268. case LC_CTYPE:
  1269. retval = "LC_CTYPE";
  1270. break;
  1271. #endif
  1272. #ifdef LC_MONETARY
  1273. case LC_MONETARY:
  1274. retval = "LC_MONETARY";
  1275. break;
  1276. #endif
  1277. #ifdef LC_NUMERIC
  1278. case LC_NUMERIC:
  1279. retval = "LC_NUMERIC";
  1280. break;
  1281. #endif
  1282. #ifdef LC_TIME
  1283. case LC_TIME:
  1284. retval = "LC_TIME";
  1285. break;
  1286. #endif
  1287. #ifdef LC_MESSAGES
  1288. case LC_MESSAGES:
  1289. retval = "LC_MESSAGES";
  1290. break;
  1291. #endif
  1292. #ifdef LC_RESPONSE
  1293. case LC_RESPONSE:
  1294. retval = "LC_RESPONSE";
  1295. break;
  1296. #endif
  1297. #ifdef LC_ALL
  1298. case LC_ALL:
  1299. /* This might not make sense but is perhaps better than any other
  1300. value. */
  1301. retval = "LC_ALL";
  1302. break;
  1303. #endif
  1304. default:
  1305. /* If you have a better idea for a default value let me know. */
  1306. retval = "LC_XXX";
  1307. }
  1308. return retval;
  1309. }
  1310. #endif
  1311. /* Guess value of current locale from value of the environment variables
  1312. or system-dependent defaults. */
  1313. static const char *
  1314. #ifdef IN_LIBGLOCALE
  1315. guess_category_value (int category, const char *categoryname,
  1316. const char *locale)
  1317. #else
  1318. guess_category_value (int category, const char *categoryname)
  1319. #endif
  1320. {
  1321. const char *language;
  1322. #ifndef IN_LIBGLOCALE
  1323. const char *locale;
  1324. # ifndef _LIBC
  1325. const char *language_default;
  1326. int locale_defaulted;
  1327. # endif
  1328. #endif
  1329. /* We use the settings in the following order:
  1330. 1. The value of the environment variable 'LANGUAGE'. This is a GNU
  1331. extension. Its value can be a colon-separated list of locale names.
  1332. 2. The value of the environment variable 'LC_ALL', 'LC_xxx', or 'LANG'.
  1333. More precisely, the first among these that is set to a non-empty value.
  1334. This is how POSIX specifies it. The value is a single locale name.
  1335. 3. A system-dependent preference list of languages. Its value can be a
  1336. colon-separated list of locale names.
  1337. 4. A system-dependent default locale name.
  1338. This way:
  1339. - System-dependent settings can be overridden by environment variables.
  1340. - If the system provides both a list of languages and a default locale,
  1341. the former is used. */
  1342. #ifndef IN_LIBGLOCALE
  1343. /* Fetch the locale name, through the POSIX method of looking to `LC_ALL',
  1344. `LC_xxx', and `LANG'. On some systems this can be done by the
  1345. `setlocale' function itself. */
  1346. # ifdef _LIBC
  1347. locale = __current_locale_name (category);
  1348. # else
  1349. locale_defaulted = 0;
  1350. # if HAVE_USELOCALE
  1351. locale = _nl_locale_name_thread_unsafe (category, categoryname);
  1352. if (locale == NULL)
  1353. # endif
  1354. {
  1355. locale = _nl_locale_name_posix (category, categoryname);
  1356. if (locale == NULL)
  1357. {
  1358. locale = _nl_locale_name_default ();
  1359. locale_defaulted = 1;
  1360. }
  1361. }
  1362. # endif
  1363. #endif
  1364. /* Ignore LANGUAGE and its system-dependent analogon if the locale is set
  1365. to "C" because
  1366. 1. "C" locale usually uses the ASCII encoding, and most international
  1367. messages use non-ASCII characters. These characters get displayed
  1368. as question marks (if using glibc's iconv()) or as invalid 8-bit
  1369. characters (because other iconv()s refuse to convert most non-ASCII
  1370. characters to ASCII). In any case, the output is ugly.
  1371. 2. The precise output of some programs in the "C" locale is specified
  1372. by POSIX and should not depend on environment variables like
  1373. "LANGUAGE" or system-dependent information. We allow such programs
  1374. to use gettext(). */
  1375. if (strcmp (locale, "C") == 0)
  1376. return locale;
  1377. /* The highest priority value is the value of the 'LANGUAGE' environment
  1378. variable. */
  1379. language = getenv ("LANGUAGE");
  1380. if (language != NULL && language[0] != '\0')
  1381. return language;
  1382. #if !defined IN_LIBGLOCALE && !defined _LIBC
  1383. /* The next priority value is the locale name, if not defaulted. */
  1384. if (locale_defaulted)
  1385. {
  1386. /* The next priority value is the default language preferences list. */
  1387. language_default = _nl_language_preferences_default ();
  1388. if (language_default != NULL)
  1389. return language_default;
  1390. }
  1391. /* The least priority value is the locale name, if defaulted. */
  1392. #endif
  1393. return locale;
  1394. }
  1395. #if (defined _LIBC || HAVE_ICONV) && !defined IN_LIBGLOCALE
  1396. /* Returns the output charset. */
  1397. static const char *
  1398. get_output_charset (struct binding *domainbinding)
  1399. {
  1400. /* The output charset should normally be determined by the locale. But
  1401. sometimes the locale is not used or not correctly set up, so we provide
  1402. a possibility for the user to override this: the OUTPUT_CHARSET
  1403. environment variable. Moreover, the value specified through
  1404. bind_textdomain_codeset overrides both. */
  1405. if (domainbinding != NULL && domainbinding->codeset != NULL)
  1406. return domainbinding->codeset;
  1407. else
  1408. {
  1409. /* For speed reasons, we look at the value of OUTPUT_CHARSET only
  1410. once. This is a user variable that is not supposed to change
  1411. during a program run. */
  1412. static char *output_charset_cache;
  1413. static int output_charset_cached;
  1414. if (!output_charset_cached)
  1415. {
  1416. const char *value = getenv ("OUTPUT_CHARSET");
  1417. if (value != NULL && value[0] != '\0')
  1418. {
  1419. size_t len = strlen (value) + 1;
  1420. char *value_copy = (char *) malloc (len);
  1421. if (value_copy != NULL)
  1422. memcpy (value_copy, value, len);
  1423. output_charset_cache = value_copy;
  1424. }
  1425. output_charset_cached = 1;
  1426. }
  1427. if (output_charset_cache != NULL)
  1428. return output_charset_cache;
  1429. else
  1430. {
  1431. # ifdef _LIBC
  1432. return _NL_CURRENT (LC_CTYPE, CODESET);
  1433. # else
  1434. # if HAVE_ICONV
  1435. return locale_charset ();
  1436. # endif
  1437. # endif
  1438. }
  1439. }
  1440. }
  1441. #endif
  1442. /* @@ begin of epilog @@ */
  1443. /* We don't want libintl.a to depend on any other library. So we
  1444. avoid the non-standard function stpcpy. In GNU C Library this
  1445. function is available, though. Also allow the symbol HAVE_STPCPY
  1446. to be defined. */
  1447. #if !_LIBC && !HAVE_STPCPY
  1448. static char *
  1449. stpcpy (char *dest, const char *src)
  1450. {
  1451. while ((*dest++ = *src++) != '\0')
  1452. /* Do nothing. */ ;
  1453. return dest - 1;
  1454. }
  1455. #endif
  1456. #if !_LIBC && !HAVE_MEMPCPY
  1457. static void *
  1458. mempcpy (void *dest, const void *src, size_t n)
  1459. {
  1460. return (void *) ((char *) memcpy (dest, src, n) + n);
  1461. }
  1462. #endif
  1463. #if !_LIBC && !HAVE_TSEARCH
  1464. # include "tsearch.c"
  1465. #endif
  1466. #ifdef _LIBC
  1467. /* If we want to free all resources we have to do some work at
  1468. program's end. */
  1469. libc_freeres_fn (free_mem)
  1470. {
  1471. void *old;
  1472. while (_nl_domain_bindings != NULL)
  1473. {
  1474. struct binding *oldp = _nl_domain_bindings;
  1475. _nl_domain_bindings = _nl_domain_bindings->next;
  1476. if (oldp->dirname != _nl_default_dirname)
  1477. /* Yes, this is a pointer comparison. */
  1478. free (oldp->dirname);
  1479. free (oldp->codeset);
  1480. free (oldp);
  1481. }
  1482. if (_nl_current_default_domain != _nl_default_default_domain)
  1483. /* Yes, again a pointer comparison. */
  1484. free ((char *) _nl_current_default_domain);
  1485. /* Remove the search tree with the known translations. */
  1486. __tdestroy (root, free);
  1487. root = NULL;
  1488. while (transmem_list != NULL)
  1489. {
  1490. old = transmem_list;
  1491. transmem_list = transmem_list->next;
  1492. free (old);
  1493. }
  1494. }
  1495. #endif