bindtextdom.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* Implementation of the bindtextdomain(3) function
  2. Copyright (C) 1995-1998, 2000, 2001, 2002 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published
  5. by the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  14. USA. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <stddef.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #ifdef _LIBC
  22. # include <libintl.h>
  23. #else
  24. # include "libgnuintl.h"
  25. #endif
  26. #include "gettextP.h"
  27. #ifdef _LIBC
  28. /* We have to handle multi-threaded applications. */
  29. # include <bits/libc-lock.h>
  30. #else
  31. /* Provide dummy implementation if this is outside glibc. */
  32. # define __libc_rwlock_define(CLASS, NAME)
  33. # define __libc_rwlock_wrlock(NAME)
  34. # define __libc_rwlock_unlock(NAME)
  35. #endif
  36. /* The internal variables in the standalone libintl.a must have different
  37. names than the internal variables in GNU libc, otherwise programs
  38. using libintl.a cannot be linked statically. */
  39. #if !defined _LIBC
  40. # define _nl_default_dirname libintl_nl_default_dirname
  41. # define _nl_domain_bindings libintl_nl_domain_bindings
  42. #endif
  43. /* Some compilers, like SunOS4 cc, don't have offsetof in <stddef.h>. */
  44. #ifndef offsetof
  45. # define offsetof(type,ident) ((size_t)&(((type*)0)->ident))
  46. #endif
  47. /* @@ end of prolog @@ */
  48. /* Contains the default location of the message catalogs. */
  49. extern const char _nl_default_dirname[];
  50. #ifdef _LIBC
  51. extern const char _nl_default_dirname_internal[] attribute_hidden;
  52. #else
  53. # define INTUSE(name) name
  54. #endif
  55. /* List with bindings of specific domains. */
  56. extern struct binding *_nl_domain_bindings;
  57. /* Lock variable to protect the global data in the gettext implementation. */
  58. __libc_rwlock_define (extern, _nl_state_lock attribute_hidden)
  59. /* Names for the libintl functions are a problem. They must not clash
  60. with existing names and they should follow ANSI C. But this source
  61. code is also used in GNU C Library where the names have a __
  62. prefix. So we have to make a difference here. */
  63. #ifdef _LIBC
  64. # define BINDTEXTDOMAIN __bindtextdomain
  65. # define BIND_TEXTDOMAIN_CODESET __bind_textdomain_codeset
  66. # ifndef strdup
  67. # define strdup(str) __strdup (str)
  68. # endif
  69. #else
  70. # define BINDTEXTDOMAIN libintl_bindtextdomain
  71. # define BIND_TEXTDOMAIN_CODESET libintl_bind_textdomain_codeset
  72. #endif
  73. /* Prototypes for local functions. */
  74. static void set_binding_values PARAMS ((const char *domainname,
  75. const char **dirnamep,
  76. const char **codesetp));
  77. /* Specifies the directory name *DIRNAMEP and the output codeset *CODESETP
  78. to be used for the DOMAINNAME message catalog.
  79. If *DIRNAMEP or *CODESETP is NULL, the corresponding attribute is not
  80. modified, only the current value is returned.
  81. If DIRNAMEP or CODESETP is NULL, the corresponding attribute is neither
  82. modified nor returned. */
  83. static void
  84. set_binding_values (domainname, dirnamep, codesetp)
  85. const char *domainname;
  86. const char **dirnamep;
  87. const char **codesetp;
  88. {
  89. struct binding *binding;
  90. int modified;
  91. /* Some sanity checks. */
  92. if (domainname == NULL || domainname[0] == '\0')
  93. {
  94. if (dirnamep)
  95. *dirnamep = NULL;
  96. if (codesetp)
  97. *codesetp = NULL;
  98. return;
  99. }
  100. __libc_rwlock_wrlock (_nl_state_lock);
  101. modified = 0;
  102. for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
  103. {
  104. int compare = strcmp (domainname, binding->domainname);
  105. if (compare == 0)
  106. /* We found it! */
  107. break;
  108. if (compare < 0)
  109. {
  110. /* It is not in the list. */
  111. binding = NULL;
  112. break;
  113. }
  114. }
  115. if (binding != NULL)
  116. {
  117. if (dirnamep)
  118. {
  119. const char *dirname = *dirnamep;
  120. if (dirname == NULL)
  121. /* The current binding has be to returned. */
  122. *dirnamep = binding->dirname;
  123. else
  124. {
  125. /* The domain is already bound. If the new value and the old
  126. one are equal we simply do nothing. Otherwise replace the
  127. old binding. */
  128. char *result = binding->dirname;
  129. if (strcmp (dirname, result) != 0)
  130. {
  131. if (strcmp (dirname, INTUSE(_nl_default_dirname)) == 0)
  132. result = (char *) INTUSE(_nl_default_dirname);
  133. else
  134. {
  135. #if defined _LIBC || defined HAVE_STRDUP
  136. result = strdup (dirname);
  137. #else
  138. size_t len = strlen (dirname) + 1;
  139. result = (char *) malloc (len);
  140. if (__builtin_expect (result != NULL, 1))
  141. memcpy (result, dirname, len);
  142. #endif
  143. }
  144. if (__builtin_expect (result != NULL, 1))
  145. {
  146. if (binding->dirname != INTUSE(_nl_default_dirname))
  147. free (binding->dirname);
  148. binding->dirname = result;
  149. modified = 1;
  150. }
  151. }
  152. *dirnamep = result;
  153. }
  154. }
  155. if (codesetp)
  156. {
  157. const char *codeset = *codesetp;
  158. if (codeset == NULL)
  159. /* The current binding has be to returned. */
  160. *codesetp = binding->codeset;
  161. else
  162. {
  163. /* The domain is already bound. If the new value and the old
  164. one are equal we simply do nothing. Otherwise replace the
  165. old binding. */
  166. char *result = binding->codeset;
  167. if (result == NULL || strcmp (codeset, result) != 0)
  168. {
  169. #if defined _LIBC || defined HAVE_STRDUP
  170. result = strdup (codeset);
  171. #else
  172. size_t len = strlen (codeset) + 1;
  173. result = (char *) malloc (len);
  174. if (__builtin_expect (result != NULL, 1))
  175. memcpy (result, codeset, len);
  176. #endif
  177. if (__builtin_expect (result != NULL, 1))
  178. {
  179. if (binding->codeset != NULL)
  180. free (binding->codeset);
  181. binding->codeset = result;
  182. binding->codeset_cntr++;
  183. modified = 1;
  184. }
  185. }
  186. *codesetp = result;
  187. }
  188. }
  189. }
  190. else if ((dirnamep == NULL || *dirnamep == NULL)
  191. && (codesetp == NULL || *codesetp == NULL))
  192. {
  193. /* Simply return the default values. */
  194. if (dirnamep)
  195. *dirnamep = INTUSE(_nl_default_dirname);
  196. if (codesetp)
  197. *codesetp = NULL;
  198. }
  199. else
  200. {
  201. /* We have to create a new binding. */
  202. size_t len = strlen (domainname) + 1;
  203. struct binding *new_binding =
  204. (struct binding *) malloc (offsetof (struct binding, domainname) + len);
  205. if (__builtin_expect (new_binding == NULL, 0))
  206. goto failed;
  207. memcpy (new_binding->domainname, domainname, len);
  208. if (dirnamep)
  209. {
  210. const char *dirname = *dirnamep;
  211. if (dirname == NULL)
  212. /* The default value. */
  213. dirname = INTUSE(_nl_default_dirname);
  214. else
  215. {
  216. if (strcmp (dirname, INTUSE(_nl_default_dirname)) == 0)
  217. dirname = INTUSE(_nl_default_dirname);
  218. else
  219. {
  220. char *result;
  221. #if defined _LIBC || defined HAVE_STRDUP
  222. result = strdup (dirname);
  223. if (__builtin_expect (result == NULL, 0))
  224. goto failed_dirname;
  225. #else
  226. size_t len = strlen (dirname) + 1;
  227. result = (char *) malloc (len);
  228. if (__builtin_expect (result == NULL, 0))
  229. goto failed_dirname;
  230. memcpy (result, dirname, len);
  231. #endif
  232. dirname = result;
  233. }
  234. }
  235. *dirnamep = dirname;
  236. new_binding->dirname = (char *) dirname;
  237. }
  238. else
  239. /* The default value. */
  240. new_binding->dirname = (char *) INTUSE(_nl_default_dirname);
  241. new_binding->codeset_cntr = 0;
  242. if (codesetp)
  243. {
  244. const char *codeset = *codesetp;
  245. if (codeset != NULL)
  246. {
  247. char *result;
  248. #if defined _LIBC || defined HAVE_STRDUP
  249. result = strdup (codeset);
  250. if (__builtin_expect (result == NULL, 0))
  251. goto failed_codeset;
  252. #else
  253. size_t len = strlen (codeset) + 1;
  254. result = (char *) malloc (len);
  255. if (__builtin_expect (result == NULL, 0))
  256. goto failed_codeset;
  257. memcpy (result, codeset, len);
  258. #endif
  259. codeset = result;
  260. new_binding->codeset_cntr++;
  261. }
  262. *codesetp = codeset;
  263. new_binding->codeset = (char *) codeset;
  264. }
  265. else
  266. new_binding->codeset = NULL;
  267. /* Now enqueue it. */
  268. if (_nl_domain_bindings == NULL
  269. || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
  270. {
  271. new_binding->next = _nl_domain_bindings;
  272. _nl_domain_bindings = new_binding;
  273. }
  274. else
  275. {
  276. binding = _nl_domain_bindings;
  277. while (binding->next != NULL
  278. && strcmp (domainname, binding->next->domainname) > 0)
  279. binding = binding->next;
  280. new_binding->next = binding->next;
  281. binding->next = new_binding;
  282. }
  283. modified = 1;
  284. /* Here we deal with memory allocation failures. */
  285. if (0)
  286. {
  287. failed_codeset:
  288. if (new_binding->dirname != INTUSE(_nl_default_dirname))
  289. free (new_binding->dirname);
  290. failed_dirname:
  291. free (new_binding);
  292. failed:
  293. if (dirnamep)
  294. *dirnamep = NULL;
  295. if (codesetp)
  296. *codesetp = NULL;
  297. }
  298. }
  299. /* If we modified any binding, we flush the caches. */
  300. if (modified)
  301. ++_nl_msg_cat_cntr;
  302. __libc_rwlock_unlock (_nl_state_lock);
  303. }
  304. /* Specify that the DOMAINNAME message catalog will be found
  305. in DIRNAME rather than in the system locale data base. */
  306. char *
  307. BINDTEXTDOMAIN (domainname, dirname)
  308. const char *domainname;
  309. const char *dirname;
  310. {
  311. set_binding_values (domainname, &dirname, NULL);
  312. return (char *) dirname;
  313. }
  314. /* Specify the character encoding in which the messages from the
  315. DOMAINNAME message catalog will be returned. */
  316. char *
  317. BIND_TEXTDOMAIN_CODESET (domainname, codeset)
  318. const char *domainname;
  319. const char *codeset;
  320. {
  321. set_binding_values (domainname, NULL, &codeset);
  322. return (char *) codeset;
  323. }
  324. #ifdef _LIBC
  325. /* Aliases for function names in GNU C Library. */
  326. weak_alias (__bindtextdomain, bindtextdomain);
  327. weak_alias (__bind_textdomain_codeset, bind_textdomain_codeset);
  328. #endif