dcigettext.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  1. /* Implementation of the internal dcigettext function.
  2. Copyright (C) 1995-2026 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 <https://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) _nl_category_names_get (category)
  312. #else
  313. static const char *category_to_name (int category);
  314. #endif
  315. #if (defined _LIBC || HAVE_ICONV) && !defined IN_LIBGLOCALE
  316. static const char *get_output_charset (struct binding *domainbinding);
  317. #endif
  318. /* For those losing systems which don't have `alloca' we have to add
  319. some additional code emulating it. */
  320. #ifdef HAVE_ALLOCA
  321. /* Nothing has to be done. */
  322. # define freea(p) /* nothing */
  323. #else
  324. # undef alloca
  325. # define alloca(size) (malloc (size))
  326. # define freea(p) free (p)
  327. #endif /* have alloca */
  328. #ifdef _LIBC
  329. /* List of blocks allocated for translations. */
  330. typedef struct transmem_list
  331. {
  332. struct transmem_list *next;
  333. char data[ZERO];
  334. } transmem_block_t;
  335. static struct transmem_list *transmem_list;
  336. #else
  337. typedef unsigned char transmem_block_t;
  338. #endif
  339. /* Names for the libintl functions are a problem. They must not clash
  340. with existing names and they should follow ANSI C. But this source
  341. code is also used in GNU C Library where the names have a __
  342. prefix. So we have to make a difference here. */
  343. #ifdef _LIBC
  344. # define DCIGETTEXT __dcigettext
  345. #else
  346. # define DCIGETTEXT libintl_dcigettext
  347. #endif
  348. /* Lock variable to protect the global data in the gettext implementation. */
  349. gl_rwlock_define_initialized (, _nl_state_lock attribute_hidden)
  350. /* Checking whether the binaries runs SUID must be done and glibc provides
  351. easier methods therefore we make a difference here. */
  352. #ifdef _LIBC
  353. # define ENABLE_SECURE __libc_enable_secure
  354. # define DETERMINE_SECURE
  355. #else
  356. # ifndef HAVE_GETUID
  357. # define getuid() 0
  358. # endif
  359. # ifndef HAVE_GETGID
  360. # define getgid() 0
  361. # endif
  362. # ifndef HAVE_GETEUID
  363. # define geteuid() getuid()
  364. # endif
  365. # ifndef HAVE_GETEGID
  366. # define getegid() getgid()
  367. # endif
  368. static int enable_secure;
  369. # define ENABLE_SECURE (enable_secure == 1)
  370. # define DETERMINE_SECURE \
  371. if (enable_secure == 0) \
  372. { \
  373. if (getuid () != geteuid () || getgid () != getegid ()) \
  374. enable_secure = 1; \
  375. else \
  376. enable_secure = -1; \
  377. }
  378. #endif
  379. /* Get the function to evaluate the plural expression. */
  380. #include "eval-plural.h"
  381. /* Look up MSGID in the DOMAINNAME message catalog for the current
  382. CATEGORY locale and, if PLURAL is nonzero, search over string
  383. depending on the plural form determined by N. */
  384. #ifdef IN_LIBGLOCALE
  385. char *
  386. gl_dcigettext (const char *domainname,
  387. const char *msgid1, const char *msgid2,
  388. int plural, unsigned long int n,
  389. int category,
  390. const char *localename, const char *encoding)
  391. #else
  392. char *
  393. DCIGETTEXT (const char *domainname, const char *msgid1, const char *msgid2,
  394. int plural, unsigned long int n, int category)
  395. #endif
  396. {
  397. struct loaded_l10nfile *domain;
  398. struct binding *binding;
  399. const char *categoryname;
  400. const char *categoryvalue;
  401. const char *dirname;
  402. char *xdirname = NULL;
  403. char *xdomainname = NULL;
  404. char *single_locale = NULL;
  405. char *retval;
  406. size_t retlen;
  407. int saved_errno;
  408. struct known_translation_t search;
  409. struct known_translation_t **foundp = NULL;
  410. #if defined HAVE_PER_THREAD_LOCALE && !defined IN_LIBGLOCALE
  411. const char *localename;
  412. #endif
  413. size_t domainname_len;
  414. /* If no real MSGID is given return NULL. */
  415. if (msgid1 == NULL)
  416. return NULL;
  417. #ifdef _LIBC
  418. if (category < 0 || category >= __LC_LAST || category == LC_ALL)
  419. /* Bogus. */
  420. return (plural == 0
  421. ? (char *) msgid1
  422. /* Use the Germanic plural rule. */
  423. : n == 1 ? (char *) msgid1 : (char *) msgid2);
  424. #endif
  425. /* Preserve the `errno' value. */
  426. saved_errno = errno;
  427. #ifdef _LIBC
  428. __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden)
  429. __libc_rwlock_rdlock (__libc_setlocale_lock);
  430. #endif
  431. gl_rwlock_rdlock (_nl_state_lock);
  432. /* If DOMAINNAME is NULL, we are interested in the default domain. If
  433. CATEGORY is not LC_MESSAGES this might not make much sense but the
  434. definition left this undefined. */
  435. if (domainname == NULL)
  436. domainname = _nl_current_default_domain;
  437. /* OS/2 specific: backward compatibility with older libintl versions */
  438. #ifdef LC_MESSAGES_COMPAT
  439. if (category == LC_MESSAGES_COMPAT)
  440. category = LC_MESSAGES;
  441. #endif
  442. /* Try to find the translation among those which we found at
  443. some time. */
  444. search.domain = NULL;
  445. search.msgid.ptr = msgid1;
  446. search.domainname = domainname;
  447. search.category = category;
  448. #ifdef HAVE_PER_THREAD_LOCALE
  449. # ifndef IN_LIBGLOCALE
  450. # ifdef _LIBC
  451. localename = __current_locale_name (category);
  452. # else
  453. categoryname = category_to_name (category);
  454. # define CATEGORYNAME_INITIALIZED
  455. localename = _nl_locale_name_thread_unsafe (category, categoryname);
  456. if (localename == NULL)
  457. localename = "";
  458. # endif
  459. # endif
  460. search.localename = localename;
  461. # ifdef IN_LIBGLOCALE
  462. search.encoding = encoding;
  463. # endif
  464. /* Since tfind/tsearch manage a balanced tree, concurrent tfind and
  465. tsearch calls can be fatal. */
  466. gl_rwlock_rdlock (tree_lock);
  467. foundp = (struct known_translation_t **) tfind (&search, &root, transcmp);
  468. gl_rwlock_unlock (tree_lock);
  469. if (foundp != NULL && (*foundp)->counter == _nl_msg_cat_cntr)
  470. {
  471. /* Now deal with plural. */
  472. if (plural)
  473. retval = plural_lookup ((*foundp)->domain, n, (*foundp)->translation,
  474. (*foundp)->translation_length);
  475. else
  476. retval = (char *) (*foundp)->translation;
  477. gl_rwlock_unlock (_nl_state_lock);
  478. # ifdef _LIBC
  479. __libc_rwlock_unlock (__libc_setlocale_lock);
  480. # endif
  481. __set_errno (saved_errno);
  482. return retval;
  483. }
  484. #endif
  485. /* See whether this is a SUID binary or not. */
  486. DETERMINE_SECURE;
  487. /* First find matching binding. */
  488. #ifdef IN_LIBGLOCALE
  489. /* We can use a trivial binding, since _nl_find_msg will ignore it anyway,
  490. and _nl_load_domain and _nl_find_domain just pass it through. */
  491. binding = NULL;
  492. dirname = bindtextdomain (domainname, NULL);
  493. #else
  494. for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
  495. {
  496. int compare = strcmp (domainname, binding->domainname);
  497. if (compare == 0)
  498. /* We found it! */
  499. break;
  500. if (compare < 0)
  501. {
  502. /* It is not in the list. */
  503. binding = NULL;
  504. break;
  505. }
  506. }
  507. if (binding == NULL)
  508. dirname = _nl_default_dirname;
  509. else
  510. {
  511. dirname = binding->dirname;
  512. #endif
  513. if (!IS_ABSOLUTE_PATH (dirname))
  514. {
  515. /* We have a relative path. Make it absolute now. */
  516. char *cwd = getcwd (NULL, 0);
  517. if (cwd == NULL)
  518. /* We cannot get the current working directory. Don't
  519. signal an error but simply return the default
  520. string. */
  521. goto return_untranslated;
  522. int ret = __asprintf (&xdirname, "%s/%s", cwd, dirname);
  523. free (cwd);
  524. if (ret < 0)
  525. goto return_untranslated;
  526. dirname = xdirname;
  527. }
  528. #ifndef IN_LIBGLOCALE
  529. }
  530. #endif
  531. /* Now determine the symbolic name of CATEGORY and its value. */
  532. #ifndef CATEGORYNAME_INITIALIZED
  533. categoryname = category_to_name (category);
  534. #endif
  535. #ifdef IN_LIBGLOCALE
  536. categoryvalue = guess_category_value (category, categoryname, localename);
  537. #else
  538. categoryvalue = guess_category_value (category, categoryname);
  539. #endif
  540. domainname_len = strlen (domainname);
  541. xdomainname = (char *) malloc (strlen (categoryname)
  542. + domainname_len + 5);
  543. if (xdomainname == NULL)
  544. goto return_untranslated;
  545. stpcpy ((char *) mempcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
  546. domainname, domainname_len),
  547. ".mo");
  548. /* Creating working area. */
  549. single_locale = (char *) malloc (strlen (categoryvalue) + 1);
  550. if (single_locale == NULL)
  551. goto return_untranslated;
  552. /* Search for the given string. This is a loop because we perhaps
  553. got an ordered list of languages to consider for the translation. */
  554. while (1)
  555. {
  556. /* Make CATEGORYVALUE point to the next element of the list. */
  557. while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
  558. ++categoryvalue;
  559. if (categoryvalue[0] == '\0')
  560. {
  561. /* The whole contents of CATEGORYVALUE has been searched but
  562. no valid entry has been found. We solve this situation
  563. by implicitly appending a "C" entry, i.e. no translation
  564. will take place. */
  565. single_locale[0] = 'C';
  566. single_locale[1] = '\0';
  567. }
  568. else
  569. {
  570. char *cp = single_locale;
  571. while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
  572. *cp++ = *categoryvalue++;
  573. *cp = '\0';
  574. /* When this is a SUID binary we must not allow accessing files
  575. outside the dedicated directories. */
  576. if (ENABLE_SECURE && IS_PATH_WITH_DIR (single_locale))
  577. /* Ignore this entry. */
  578. continue;
  579. }
  580. /* If the current locale value is "C" or "C.<encoding>" or "POSIX",
  581. we don't load a domain. Return the MSGID. */
  582. if ((single_locale[0] == 'C'
  583. && (single_locale[1] == '\0' || single_locale[1] == '.'))
  584. || strcmp (single_locale, "POSIX") == 0)
  585. break;
  586. /* Find structure describing the message catalog matching the
  587. DOMAINNAME and CATEGORY. */
  588. domain = _nl_find_domain (dirname, single_locale, xdomainname, binding);
  589. if (domain != NULL)
  590. {
  591. #if defined IN_LIBGLOCALE
  592. retval = _nl_find_msg (domain, binding, encoding, msgid1, &retlen);
  593. #else
  594. retval = _nl_find_msg (domain, binding, msgid1, 1, &retlen);
  595. #endif
  596. if (retval == NULL)
  597. {
  598. int cnt;
  599. for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
  600. {
  601. #if defined IN_LIBGLOCALE
  602. retval = _nl_find_msg (domain->successor[cnt], binding,
  603. encoding, msgid1, &retlen);
  604. #else
  605. retval = _nl_find_msg (domain->successor[cnt], binding,
  606. msgid1, 1, &retlen);
  607. #endif
  608. /* Resource problems are not fatal, instead we return no
  609. translation. */
  610. if (__builtin_expect (retval == (char *) -1, 0))
  611. goto return_untranslated;
  612. if (retval != NULL)
  613. {
  614. domain = domain->successor[cnt];
  615. break;
  616. }
  617. }
  618. }
  619. /* Returning -1 means that some resource problem exists
  620. (likely memory) and that the strings could not be
  621. converted. Return the original strings. */
  622. if (__builtin_expect (retval == (char *) -1, 0))
  623. break;
  624. if (retval != NULL)
  625. {
  626. /* Found the translation of MSGID1 in domain DOMAIN:
  627. starting at RETVAL, RETLEN bytes. */
  628. free (xdirname);
  629. free (xdomainname);
  630. free (single_locale);
  631. if (foundp == NULL)
  632. {
  633. /* Create a new entry and add it to the search tree. */
  634. size_t msgid_len;
  635. size_t size;
  636. struct known_translation_t *newp;
  637. msgid_len = strlen (msgid1) + 1;
  638. size = offsetof (struct known_translation_t, msgid)
  639. + msgid_len + domainname_len + 1;
  640. #ifdef HAVE_PER_THREAD_LOCALE
  641. size += strlen (localename) + 1;
  642. #endif
  643. newp = (struct known_translation_t *) malloc (size);
  644. if (newp != NULL)
  645. {
  646. char *new_domainname;
  647. #ifdef HAVE_PER_THREAD_LOCALE
  648. char *new_localename;
  649. #endif
  650. new_domainname =
  651. (char *) mempcpy (newp->msgid.appended, msgid1,
  652. msgid_len);
  653. memcpy (new_domainname, domainname, domainname_len + 1);
  654. #ifdef HAVE_PER_THREAD_LOCALE
  655. new_localename = new_domainname + domainname_len + 1;
  656. strcpy (new_localename, localename);
  657. #endif
  658. newp->domainname = new_domainname;
  659. newp->category = category;
  660. #ifdef HAVE_PER_THREAD_LOCALE
  661. newp->localename = new_localename;
  662. #endif
  663. #ifdef IN_LIBGLOCALE
  664. newp->encoding = encoding;
  665. #endif
  666. newp->counter = _nl_msg_cat_cntr;
  667. newp->domain = domain;
  668. newp->translation = retval;
  669. newp->translation_length = retlen;
  670. gl_rwlock_wrlock (tree_lock);
  671. /* Insert the entry in the search tree. */
  672. foundp = (struct known_translation_t **)
  673. tsearch (newp, &root, transcmp);
  674. gl_rwlock_unlock (tree_lock);
  675. if (foundp == NULL
  676. || __builtin_expect (*foundp != newp, 0))
  677. /* The insert failed. */
  678. free (newp);
  679. }
  680. }
  681. else
  682. {
  683. /* We can update the existing entry. */
  684. (*foundp)->counter = _nl_msg_cat_cntr;
  685. (*foundp)->domain = domain;
  686. (*foundp)->translation = retval;
  687. (*foundp)->translation_length = retlen;
  688. }
  689. __set_errno (saved_errno);
  690. /* Now deal with plural. */
  691. if (plural)
  692. retval = plural_lookup (domain, n, retval, retlen);
  693. gl_rwlock_unlock (_nl_state_lock);
  694. #ifdef _LIBC
  695. __libc_rwlock_unlock (__libc_setlocale_lock);
  696. #endif
  697. return retval;
  698. }
  699. }
  700. }
  701. return_untranslated:
  702. /* Return the untranslated MSGID. */
  703. free (xdirname);
  704. free (xdomainname);
  705. free (single_locale);
  706. gl_rwlock_unlock (_nl_state_lock);
  707. #ifdef _LIBC
  708. __libc_rwlock_unlock (__libc_setlocale_lock);
  709. #endif
  710. #ifndef _LIBC
  711. if (!ENABLE_SECURE)
  712. {
  713. extern void _nl_log_untranslated (const char *logfilename,
  714. const char *domainname,
  715. const char *msgid1, const char *msgid2,
  716. int plural);
  717. const char *logfilename = getenv ("GETTEXT_LOG_UNTRANSLATED");
  718. if (logfilename != NULL && logfilename[0] != '\0')
  719. _nl_log_untranslated (logfilename, domainname, msgid1, msgid2, plural);
  720. }
  721. #endif
  722. __set_errno (saved_errno);
  723. return (plural == 0
  724. ? (char *) msgid1
  725. /* Use the Germanic plural rule. */
  726. : n == 1 ? (char *) msgid1 : (char *) msgid2);
  727. }
  728. /* Look up the translation of msgid within DOMAIN_FILE and DOMAINBINDING.
  729. Return it if found. Return NULL if not found or in case of a conversion
  730. failure (problem in the particular message catalog). Return (char *) -1
  731. in case of a memory allocation failure during conversion (only if
  732. ENCODING != NULL resp. CONVERT == true). */
  733. char *
  734. #ifdef IN_LIBGLOCALE
  735. _nl_find_msg (struct loaded_l10nfile *domain_file,
  736. struct binding *domainbinding, const char *encoding,
  737. const char *msgid,
  738. size_t *lengthp)
  739. #else
  740. _nl_find_msg (struct loaded_l10nfile *domain_file,
  741. struct binding *domainbinding,
  742. const char *msgid, int convert,
  743. size_t *lengthp)
  744. #endif
  745. {
  746. struct loaded_domain *domain;
  747. nls_uint32 nstrings;
  748. size_t act;
  749. char *result;
  750. size_t resultlen;
  751. if (domain_file->decided <= 0)
  752. _nl_load_domain (domain_file, domainbinding);
  753. if (domain_file->data == NULL)
  754. return NULL;
  755. domain = (struct loaded_domain *) domain_file->data;
  756. nstrings = domain->nstrings;
  757. /* Locate the MSGID and its translation. */
  758. if (domain->hash_tab != NULL)
  759. {
  760. /* Use the hashing table. */
  761. nls_uint32 len = strlen (msgid);
  762. nls_uint32 hash_val = __hash_string (msgid);
  763. nls_uint32 idx = hash_val % domain->hash_size;
  764. nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
  765. while (1)
  766. {
  767. nls_uint32 nstr =
  768. W (domain->must_swap_hash_tab, domain->hash_tab[idx]);
  769. if (nstr == 0)
  770. /* Hash table entry is empty. */
  771. return NULL;
  772. nstr--;
  773. /* Compare msgid with the original string at index nstr.
  774. We compare the lengths with >=, not ==, because plural entries
  775. are represented by strings with an embedded NUL. */
  776. if (nstr < nstrings
  777. ? W (domain->must_swap, domain->orig_tab[nstr].length) >= len
  778. && (strcmp (msgid,
  779. domain->data + W (domain->must_swap,
  780. domain->orig_tab[nstr].offset))
  781. == 0)
  782. : domain->orig_sysdep_tab[nstr - nstrings].length > len
  783. && (strcmp (msgid,
  784. domain->orig_sysdep_tab[nstr - nstrings].pointer)
  785. == 0))
  786. {
  787. act = nstr;
  788. goto found;
  789. }
  790. if (idx >= domain->hash_size - incr)
  791. idx -= domain->hash_size - incr;
  792. else
  793. idx += incr;
  794. }
  795. /* NOTREACHED */
  796. }
  797. else
  798. {
  799. /* Try the default method: binary search in the sorted array of
  800. messages. */
  801. size_t top, bottom;
  802. bottom = 0;
  803. top = nstrings;
  804. while (bottom < top)
  805. {
  806. int cmp_val;
  807. act = (bottom + top) / 2;
  808. cmp_val = strcmp (msgid, (domain->data
  809. + W (domain->must_swap,
  810. domain->orig_tab[act].offset)));
  811. if (cmp_val < 0)
  812. top = act;
  813. else if (cmp_val > 0)
  814. bottom = act + 1;
  815. else
  816. goto found;
  817. }
  818. /* No translation was found. */
  819. return NULL;
  820. }
  821. found:
  822. /* The translation was found at index ACT. If we have to convert the
  823. string to use a different character set, this is the time. */
  824. if (act < nstrings)
  825. {
  826. result = (char *)
  827. (domain->data + W (domain->must_swap, domain->trans_tab[act].offset));
  828. resultlen = W (domain->must_swap, domain->trans_tab[act].length) + 1;
  829. }
  830. else
  831. {
  832. result = (char *) domain->trans_sysdep_tab[act - nstrings].pointer;
  833. resultlen = domain->trans_sysdep_tab[act - nstrings].length;
  834. }
  835. #if defined _LIBC || HAVE_ICONV
  836. # ifdef IN_LIBGLOCALE
  837. if (encoding != NULL)
  838. # else
  839. if (convert)
  840. # endif
  841. {
  842. /* We are supposed to do a conversion. */
  843. # ifndef IN_LIBGLOCALE
  844. const char *encoding = get_output_charset (domainbinding);
  845. # endif
  846. size_t nconversions;
  847. struct converted_domain *convd;
  848. size_t i;
  849. /* Protect against reallocation of the table. */
  850. gl_rwlock_rdlock (domain->conversions_lock);
  851. /* Search whether a table with converted translations for this
  852. encoding has already been allocated. */
  853. nconversions = domain->nconversions;
  854. convd = NULL;
  855. for (i = nconversions; i > 0; )
  856. {
  857. i--;
  858. if (strcmp (domain->conversions[i].encoding, encoding) == 0)
  859. {
  860. convd = &domain->conversions[i];
  861. break;
  862. }
  863. }
  864. gl_rwlock_unlock (domain->conversions_lock);
  865. if (convd == NULL)
  866. {
  867. /* We have to allocate a new conversions table. */
  868. gl_rwlock_wrlock (domain->conversions_lock);
  869. nconversions = domain->nconversions;
  870. /* Maybe in the meantime somebody added the translation.
  871. Recheck. */
  872. for (i = nconversions; i > 0; )
  873. {
  874. i--;
  875. if (strcmp (domain->conversions[i].encoding, encoding) == 0)
  876. {
  877. convd = &domain->conversions[i];
  878. goto found_convd;
  879. }
  880. }
  881. {
  882. /* Allocate a table for the converted translations for this
  883. encoding. */
  884. struct converted_domain *new_conversions =
  885. (struct converted_domain *)
  886. (domain->conversions != NULL
  887. ? realloc (domain->conversions,
  888. (nconversions + 1) * sizeof (struct converted_domain))
  889. : malloc ((nconversions + 1) * sizeof (struct converted_domain)));
  890. if (__builtin_expect (new_conversions == NULL, 0))
  891. {
  892. /* Nothing we can do, no more memory. We cannot use the
  893. translation because it might be encoded incorrectly. */
  894. unlock_fail:
  895. gl_rwlock_unlock (domain->conversions_lock);
  896. return (char *) -1;
  897. }
  898. domain->conversions = new_conversions;
  899. /* Copy the 'encoding' string to permanent storage. */
  900. encoding = strdup (encoding);
  901. if (__builtin_expect (encoding == NULL, 0))
  902. /* Nothing we can do, no more memory. We cannot use the
  903. translation because it might be encoded incorrectly. */
  904. goto unlock_fail;
  905. convd = &new_conversions[nconversions];
  906. convd->encoding = encoding;
  907. /* Find out about the character set the file is encoded with.
  908. This can be found (in textual form) in the entry "". If this
  909. entry does not exist or if this does not contain the 'charset='
  910. information, we will assume the charset matches the one the
  911. current locale and we don't have to perform any conversion. */
  912. # ifdef _LIBC
  913. convd->conv = (__gconv_t) -1;
  914. # else
  915. # if HAVE_ICONV
  916. convd->conv = (iconv_t) -1;
  917. # endif
  918. # endif
  919. {
  920. char *nullentry;
  921. size_t nullentrylen;
  922. /* Get the header entry. This is a recursion, but it doesn't
  923. reallocate domain->conversions because we pass
  924. encoding = NULL or convert = 0, respectively. */
  925. nullentry =
  926. # ifdef IN_LIBGLOCALE
  927. _nl_find_msg (domain_file, domainbinding, NULL, "",
  928. &nullentrylen);
  929. # else
  930. _nl_find_msg (domain_file, domainbinding, "", 0, &nullentrylen);
  931. # endif
  932. /* Resource problems are fatal. If we continue onwards we will
  933. only attempt to calloc a new conv_tab and fail later. */
  934. if (__builtin_expect (nullentry == (char *) -1, 0))
  935. return (char *) -1;
  936. if (nullentry != NULL)
  937. {
  938. const char *charsetstr;
  939. charsetstr = strstr (nullentry, "charset=");
  940. if (charsetstr != NULL)
  941. {
  942. size_t len;
  943. char *charset;
  944. const char *outcharset;
  945. charsetstr += strlen ("charset=");
  946. len = strcspn (charsetstr, " \t\n");
  947. charset = (char *) alloca (len + 1);
  948. # if defined _LIBC || HAVE_MEMPCPY
  949. *((char *) mempcpy (charset, charsetstr, len)) = '\0';
  950. # else
  951. memcpy (charset, charsetstr, len);
  952. charset[len] = '\0';
  953. # endif
  954. outcharset = encoding;
  955. # ifdef _LIBC
  956. struct gconv_spec conv_spec;
  957. __gconv_create_spec (&conv_spec, charset, outcharset);
  958. /* We always want to use transliteration. */
  959. conv_spec.translit = true;
  960. int r = __gconv_open (&conv_spec, &convd->conv,
  961. GCONV_AVOID_NOCONV);
  962. __gconv_destroy_spec (&conv_spec);
  963. if (__builtin_expect (r != __GCONV_OK, 0))
  964. {
  965. /* If the output encoding is the same there is
  966. nothing to do. Otherwise do not use the
  967. translation at all. */
  968. if (__builtin_expect (r != __GCONV_NULCONV, 1))
  969. {
  970. gl_rwlock_unlock (domain->conversions_lock);
  971. free ((char *) encoding);
  972. return NULL;
  973. }
  974. convd->conv = (__gconv_t) -1;
  975. }
  976. # else
  977. # if HAVE_ICONV
  978. /* When using GNU libc >= 2.2 or GNU libiconv >= 1.5,
  979. we want to use transliteration. */
  980. # if (((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2) \
  981. && !defined __UCLIBC__) \
  982. || _LIBICONV_VERSION >= 0x0105
  983. if (strchr (outcharset, '/') == NULL)
  984. {
  985. char *tmp;
  986. len = strlen (outcharset);
  987. tmp = (char *) alloca (len + 10 + 1);
  988. memcpy (tmp, outcharset, len);
  989. memcpy (tmp + len, "//TRANSLIT", 10 + 1);
  990. outcharset = tmp;
  991. convd->conv = iconv_open (outcharset, charset);
  992. freea (outcharset);
  993. }
  994. else
  995. # endif
  996. convd->conv = iconv_open (outcharset, charset);
  997. # endif
  998. # endif
  999. freea (charset);
  1000. }
  1001. }
  1002. }
  1003. convd->conv_tab = NULL;
  1004. /* Here domain->conversions is still == new_conversions. */
  1005. domain->nconversions++;
  1006. }
  1007. found_convd:
  1008. gl_rwlock_unlock (domain->conversions_lock);
  1009. }
  1010. if (
  1011. # ifdef _LIBC
  1012. convd->conv != (__gconv_t) -1
  1013. # else
  1014. # if HAVE_ICONV
  1015. convd->conv != (iconv_t) -1
  1016. # endif
  1017. # endif
  1018. )
  1019. {
  1020. /* We are supposed to do a conversion. First allocate an
  1021. appropriate table with the same structure as the table
  1022. of translations in the file, where we can put the pointers
  1023. to the converted strings in.
  1024. There is a slight complication with plural entries. They
  1025. are represented by consecutive NUL terminated strings. We
  1026. handle this case by converting RESULTLEN bytes, including
  1027. NULs. */
  1028. /* This lock primarily protects the memory management variables
  1029. freemem, freemem_size. It also protects write accesses to
  1030. convd->conv_tab. It's not worth using a separate lock (such
  1031. as domain->conversions_lock) for this purpose, because when
  1032. modifying convd->conv_tab, we also need to lock freemem,
  1033. freemem_size for most of the time. */
  1034. __libc_lock_define_initialized (static, lock)
  1035. if (__builtin_expect (convd->conv_tab == NULL, 0))
  1036. {
  1037. __libc_lock_lock (lock);
  1038. if (convd->conv_tab == NULL)
  1039. {
  1040. convd->conv_tab =
  1041. (char **) calloc (nstrings + domain->n_sysdep_strings,
  1042. sizeof (char *));
  1043. if (convd->conv_tab != NULL)
  1044. goto not_translated_yet;
  1045. /* Mark that we didn't succeed allocating a table. */
  1046. convd->conv_tab = (char **) -1;
  1047. }
  1048. __libc_lock_unlock (lock);
  1049. }
  1050. if (__builtin_expect (convd->conv_tab == (char **) -1, 0))
  1051. /* Nothing we can do, no more memory. We cannot use the
  1052. translation because it might be encoded incorrectly. */
  1053. return (char *) -1;
  1054. if (convd->conv_tab[act] == NULL)
  1055. {
  1056. /* We haven't used this string so far, so it is not
  1057. translated yet. Do this now. */
  1058. /* We use a bit more efficient memory handling.
  1059. We allocate always larger blocks which get used over
  1060. time. This is faster than many small allocations. */
  1061. # define INITIAL_BLOCK_SIZE 4080
  1062. static unsigned char *freemem;
  1063. static size_t freemem_size;
  1064. const unsigned char *inbuf;
  1065. unsigned char *outbuf;
  1066. int malloc_count;
  1067. # ifndef _LIBC
  1068. transmem_block_t *transmem_list;
  1069. # endif
  1070. __libc_lock_lock (lock);
  1071. not_translated_yet:
  1072. inbuf = (const unsigned char *) result;
  1073. outbuf = freemem + sizeof (size_t);
  1074. # ifndef _LIBC
  1075. transmem_list = NULL;
  1076. # endif
  1077. malloc_count = 0;
  1078. while (1)
  1079. {
  1080. transmem_block_t *newmem;
  1081. # ifdef _LIBC
  1082. size_t non_reversible;
  1083. int res;
  1084. if (freemem_size < sizeof (size_t))
  1085. goto resize_freemem;
  1086. res = __gconv (convd->conv,
  1087. &inbuf, inbuf + resultlen,
  1088. &outbuf,
  1089. outbuf + freemem_size - sizeof (size_t),
  1090. &non_reversible);
  1091. if (res == __GCONV_OK || res == __GCONV_EMPTY_INPUT)
  1092. break;
  1093. if (res != __GCONV_FULL_OUTPUT)
  1094. {
  1095. /* We should not use the translation at all, it
  1096. is incorrectly encoded. */
  1097. __libc_lock_unlock (lock);
  1098. return NULL;
  1099. }
  1100. inbuf = (const unsigned char *) result;
  1101. # else
  1102. # if HAVE_ICONV
  1103. const char *inptr = (const char *) inbuf;
  1104. size_t inleft = resultlen;
  1105. char *outptr = (char *) outbuf;
  1106. size_t outleft;
  1107. if (freemem_size < sizeof (size_t))
  1108. goto resize_freemem;
  1109. outleft = freemem_size - sizeof (size_t);
  1110. if (iconv (convd->conv,
  1111. (ICONV_CONST char **) &inptr, &inleft,
  1112. &outptr, &outleft)
  1113. != (size_t) (-1))
  1114. {
  1115. outbuf = (unsigned char *) outptr;
  1116. break;
  1117. }
  1118. if (errno != E2BIG)
  1119. {
  1120. __libc_lock_unlock (lock);
  1121. return NULL;
  1122. }
  1123. # endif
  1124. # endif
  1125. resize_freemem:
  1126. /* We must allocate a new buffer or resize the old one. */
  1127. if (malloc_count > 0)
  1128. {
  1129. ++malloc_count;
  1130. freemem_size = malloc_count * INITIAL_BLOCK_SIZE;
  1131. newmem = (transmem_block_t *) realloc (transmem_list,
  1132. freemem_size);
  1133. # ifdef _LIBC
  1134. if (newmem != NULL)
  1135. transmem_list = newmem;
  1136. else
  1137. {
  1138. struct transmem_list *old = transmem_list;
  1139. transmem_list = transmem_list->next;
  1140. free (old);
  1141. }
  1142. # endif
  1143. }
  1144. else
  1145. {
  1146. malloc_count = 1;
  1147. freemem_size = INITIAL_BLOCK_SIZE;
  1148. newmem = (transmem_block_t *) malloc (freemem_size);
  1149. # ifdef _LIBC
  1150. if (newmem != NULL)
  1151. {
  1152. /* Add the block to the list of blocks we have to free
  1153. at some point. */
  1154. newmem->next = transmem_list;
  1155. transmem_list = newmem;
  1156. }
  1157. /* Fall through and return -1. */
  1158. # endif
  1159. }
  1160. if (__builtin_expect (newmem == NULL, 0))
  1161. {
  1162. freemem = NULL;
  1163. freemem_size = 0;
  1164. __libc_lock_unlock (lock);
  1165. return (char *) -1;
  1166. }
  1167. # ifdef _LIBC
  1168. freemem = (unsigned char *) newmem->data;
  1169. freemem_size -= offsetof (struct transmem_list, data);
  1170. # else
  1171. transmem_list = newmem;
  1172. freemem = newmem;
  1173. # endif
  1174. outbuf = freemem + sizeof (size_t);
  1175. }
  1176. /* We have now in our buffer a converted string. Put this
  1177. into the table of conversions. */
  1178. *(size_t *) freemem = outbuf - freemem - sizeof (size_t);
  1179. convd->conv_tab[act] = (char *) freemem;
  1180. /* Shrink freemem, but keep it aligned. */
  1181. freemem_size -= outbuf - freemem;
  1182. freemem = outbuf;
  1183. freemem += freemem_size & (alignof (size_t) - 1);
  1184. freemem_size = freemem_size & ~ (alignof (size_t) - 1);
  1185. __libc_lock_unlock (lock);
  1186. }
  1187. /* Now convd->conv_tab[act] contains the translation of all
  1188. the plural variants. */
  1189. result = convd->conv_tab[act] + sizeof (size_t);
  1190. resultlen = *(size_t *) convd->conv_tab[act];
  1191. }
  1192. }
  1193. /* The result string is converted. */
  1194. #endif /* _LIBC || HAVE_ICONV */
  1195. *lengthp = resultlen;
  1196. return result;
  1197. }
  1198. /* Look up a plural variant. */
  1199. static char *
  1200. plural_lookup (struct loaded_l10nfile *domain, unsigned long int n,
  1201. const char *translation, size_t translation_len)
  1202. {
  1203. struct loaded_domain *domaindata = (struct loaded_domain *) domain->data;
  1204. unsigned long int index;
  1205. const char *p;
  1206. index = plural_eval (domaindata->plural, n);
  1207. if (index >= domaindata->nplurals)
  1208. /* This should never happen. It means the plural expression and the
  1209. given maximum value do not match. */
  1210. index = 0;
  1211. /* Skip INDEX strings at TRANSLATION. */
  1212. p = translation;
  1213. while (index-- > 0)
  1214. {
  1215. p = strchr (p, '\0');
  1216. /* And skip over the NUL byte. */
  1217. p++;
  1218. if (p >= translation + translation_len)
  1219. /* This should never happen. It means the plural expression
  1220. evaluated to a value larger than the number of variants
  1221. available for MSGID1. */
  1222. return (char *) translation;
  1223. }
  1224. return (char *) p;
  1225. }
  1226. #ifndef _LIBC
  1227. /* Return string representation of locale CATEGORY. */
  1228. static const char *
  1229. category_to_name (int category)
  1230. {
  1231. const char *retval;
  1232. switch (category)
  1233. {
  1234. #ifdef LC_COLLATE
  1235. case LC_COLLATE:
  1236. retval = "LC_COLLATE";
  1237. break;
  1238. #endif
  1239. #ifdef LC_CTYPE
  1240. case LC_CTYPE:
  1241. retval = "LC_CTYPE";
  1242. break;
  1243. #endif
  1244. #ifdef LC_MONETARY
  1245. case LC_MONETARY:
  1246. retval = "LC_MONETARY";
  1247. break;
  1248. #endif
  1249. #ifdef LC_NUMERIC
  1250. case LC_NUMERIC:
  1251. retval = "LC_NUMERIC";
  1252. break;
  1253. #endif
  1254. #ifdef LC_TIME
  1255. case LC_TIME:
  1256. retval = "LC_TIME";
  1257. break;
  1258. #endif
  1259. #ifdef LC_MESSAGES
  1260. case LC_MESSAGES:
  1261. retval = "LC_MESSAGES";
  1262. break;
  1263. #endif
  1264. #ifdef LC_RESPONSE
  1265. case LC_RESPONSE:
  1266. retval = "LC_RESPONSE";
  1267. break;
  1268. #endif
  1269. #ifdef LC_ALL
  1270. case LC_ALL:
  1271. /* This might not make sense but is perhaps better than any other
  1272. value. */
  1273. retval = "LC_ALL";
  1274. break;
  1275. #endif
  1276. default:
  1277. /* If you have a better idea for a default value let me know. */
  1278. retval = "LC_XXX";
  1279. }
  1280. return retval;
  1281. }
  1282. #endif
  1283. /* Guess value of current locale from value of the environment variables
  1284. or system-dependent defaults. */
  1285. static const char *
  1286. #ifdef IN_LIBGLOCALE
  1287. guess_category_value (int category, const char *categoryname,
  1288. const char *locale)
  1289. #else
  1290. guess_category_value (int category, const char *categoryname)
  1291. #endif
  1292. {
  1293. const char *language;
  1294. #ifndef IN_LIBGLOCALE
  1295. const char *locale;
  1296. # ifndef _LIBC
  1297. const char *language_default;
  1298. int locale_defaulted;
  1299. # endif
  1300. #endif
  1301. /* We use the settings in the following order:
  1302. 1. The value of the environment variable 'LANGUAGE'. This is a GNU
  1303. extension. Its value can be a colon-separated list of locale names.
  1304. 2. The value of the environment variable 'LC_ALL', 'LC_xxx', or 'LANG'.
  1305. More precisely, the first among these that is set to a non-empty value.
  1306. This is how POSIX specifies it. The value is a single locale name.
  1307. 3. A system-dependent preference list of languages. Its value can be a
  1308. colon-separated list of locale names.
  1309. 4. A system-dependent default locale name.
  1310. This way:
  1311. - System-dependent settings can be overridden by environment variables.
  1312. - If the system provides both a list of languages and a default locale,
  1313. the former is used. */
  1314. #ifndef IN_LIBGLOCALE
  1315. /* Fetch the locale name, through the POSIX method of looking to `LC_ALL',
  1316. `LC_xxx', and `LANG'. On some systems this can be done by the
  1317. `setlocale' function itself. */
  1318. # ifdef _LIBC
  1319. locale = __current_locale_name (category);
  1320. # else
  1321. locale_defaulted = 0;
  1322. # if HAVE_USELOCALE
  1323. locale = _nl_locale_name_thread_unsafe (category, categoryname);
  1324. if (locale == NULL)
  1325. # endif
  1326. {
  1327. locale = _nl_locale_name_posix (category, categoryname);
  1328. if (locale == NULL)
  1329. {
  1330. locale = _nl_locale_name_default ();
  1331. locale_defaulted = 1;
  1332. }
  1333. }
  1334. # endif
  1335. #endif
  1336. /* Ignore LANGUAGE and its system-dependent analogon if the locale is set
  1337. to "C" because
  1338. 1. "C" locale usually uses the ASCII encoding, and most international
  1339. messages use non-ASCII characters. These characters get displayed
  1340. as question marks (if using glibc's iconv()) or as invalid 8-bit
  1341. characters (because other iconv()s refuse to convert most non-ASCII
  1342. characters to ASCII). In any case, the output is ugly.
  1343. 2. The precise output of some programs in the "C" locale is specified
  1344. by POSIX and should not depend on environment variables like
  1345. "LANGUAGE" or system-dependent information. We allow such programs
  1346. to use gettext().
  1347. Ignore LANGUAGE and its system-dependent analogon also if the locale is
  1348. set to "C.UTF-8" or, more generally, to "C.<encoding>", because that's
  1349. the by-design behaviour for glibc, see
  1350. <https://sourceware.org/glibc/wiki/Proposals/C.UTF-8>. */
  1351. if (locale[0] == 'C' && (locale[1] == '\0' || locale[1] == '.'))
  1352. return locale;
  1353. /* The highest priority value is the value of the 'LANGUAGE' environment
  1354. variable. */
  1355. language = getenv ("LANGUAGE");
  1356. if (language != NULL && language[0] != '\0')
  1357. return language;
  1358. #if !defined IN_LIBGLOCALE && !defined _LIBC
  1359. /* The next priority value is the locale name, if not defaulted. */
  1360. if (locale_defaulted)
  1361. {
  1362. /* The next priority value is the default language preferences list. */
  1363. language_default = _nl_language_preferences_default ();
  1364. if (language_default != NULL)
  1365. return language_default;
  1366. }
  1367. /* The least priority value is the locale name, if defaulted. */
  1368. #endif
  1369. return locale;
  1370. }
  1371. #if (defined _LIBC || HAVE_ICONV) && !defined IN_LIBGLOCALE
  1372. /* Returns the output charset. */
  1373. static const char *
  1374. get_output_charset (struct binding *domainbinding)
  1375. {
  1376. /* The output charset should normally be determined by the locale. But
  1377. sometimes the locale is not used or not correctly set up, so we provide
  1378. a possibility for the user to override this: the OUTPUT_CHARSET
  1379. environment variable. Moreover, the value specified through
  1380. bind_textdomain_codeset overrides both. */
  1381. if (domainbinding != NULL && domainbinding->codeset != NULL)
  1382. return domainbinding->codeset;
  1383. else
  1384. {
  1385. /* For speed reasons, we look at the value of OUTPUT_CHARSET only
  1386. once. This is a user variable that is not supposed to change
  1387. during a program run. */
  1388. static char *output_charset_cache;
  1389. static int output_charset_cached;
  1390. if (!output_charset_cached)
  1391. {
  1392. const char *value = getenv ("OUTPUT_CHARSET");
  1393. if (value != NULL && value[0] != '\0')
  1394. {
  1395. size_t len = strlen (value) + 1;
  1396. char *value_copy = (char *) malloc (len);
  1397. if (value_copy != NULL)
  1398. memcpy (value_copy, value, len);
  1399. output_charset_cache = value_copy;
  1400. }
  1401. output_charset_cached = 1;
  1402. }
  1403. if (output_charset_cache != NULL)
  1404. return output_charset_cache;
  1405. else
  1406. {
  1407. # ifdef _LIBC
  1408. return _NL_CURRENT (LC_CTYPE, CODESET);
  1409. # else
  1410. # if HAVE_ICONV
  1411. return locale_charset ();
  1412. # endif
  1413. # endif
  1414. }
  1415. }
  1416. }
  1417. #endif
  1418. /* @@ begin of epilog @@ */
  1419. /* We don't want libintl.a to depend on any other library. So we
  1420. avoid the non-standard function stpcpy. In GNU C Library this
  1421. function is available, though. Also allow the symbol HAVE_STPCPY
  1422. to be defined. */
  1423. #if !_LIBC && !HAVE_STPCPY
  1424. static char *
  1425. stpcpy (char *dest, const char *src)
  1426. {
  1427. while ((*dest++ = *src++) != '\0')
  1428. /* Do nothing. */ ;
  1429. return dest - 1;
  1430. }
  1431. #endif
  1432. #if !_LIBC && !HAVE_MEMPCPY
  1433. static void *
  1434. mempcpy (void *dest, const void *src, size_t n)
  1435. {
  1436. return (void *) ((char *) memcpy (dest, src, n) + n);
  1437. }
  1438. #endif
  1439. #if !_LIBC && !HAVE_TSEARCH
  1440. # include "tsearch.c"
  1441. #endif
  1442. #ifdef _LIBC
  1443. /* If we want to free all resources we have to do some work at
  1444. program's end. */
  1445. void
  1446. __intl_freemem (void)
  1447. {
  1448. void *old;
  1449. while (_nl_domain_bindings != NULL)
  1450. {
  1451. struct binding *oldp = _nl_domain_bindings;
  1452. _nl_domain_bindings = _nl_domain_bindings->next;
  1453. if (oldp->dirname != _nl_default_dirname)
  1454. /* Yes, this is a pointer comparison. */
  1455. free (oldp->dirname);
  1456. free (oldp->codeset);
  1457. free (oldp);
  1458. }
  1459. if (_nl_current_default_domain != _nl_default_default_domain)
  1460. /* Yes, again a pointer comparison. */
  1461. free ((char *) _nl_current_default_domain);
  1462. /* Remove the search tree with the known translations. */
  1463. __tdestroy (root, free);
  1464. root = NULL;
  1465. while (transmem_list != NULL)
  1466. {
  1467. old = transmem_list;
  1468. transmem_list = transmem_list->next;
  1469. free (old);
  1470. }
  1471. }
  1472. #endif