dcigettext.c 46 KB

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