bindtextdom.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* Implementation of the bindtextdomain(3) function
  2. Copyright (C) 1995-1998, 2000-2003, 2005-2006 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. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <stddef.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "gettextP.h"
  22. #ifdef _LIBC
  23. # include <libintl.h>
  24. #else
  25. # include "libgnuintl.h"
  26. #endif
  27. /* Handle multi-threaded applications. */
  28. #ifdef _LIBC
  29. # include <bits/libc-lock.h>
  30. # define gl_rwlock_define __libc_rwlock_define
  31. # define gl_rwlock_wrlock __libc_rwlock_wrlock
  32. # define gl_rwlock_unlock __libc_rwlock_unlock
  33. #else
  34. # include "lock.h"
  35. #endif
  36. /* Some compilers, like SunOS4 cc, don't have offsetof in <stddef.h>. */
  37. #ifndef offsetof
  38. # define offsetof(type,ident) ((size_t)&(((type*)0)->ident))
  39. #endif
  40. /* @@ end of prolog @@ */
  41. /* Lock variable to protect the global data in the gettext implementation. */
  42. gl_rwlock_define (extern, _nl_state_lock attribute_hidden)
  43. /* Names for the libintl functions are a problem. They must not clash
  44. with existing names and they should follow ANSI C. But this source
  45. code is also used in GNU C Library where the names have a __
  46. prefix. So we have to make a difference here. */
  47. #ifdef _LIBC
  48. # define BINDTEXTDOMAIN __bindtextdomain
  49. # define BIND_TEXTDOMAIN_CODESET __bind_textdomain_codeset
  50. # ifndef strdup
  51. # define strdup(str) __strdup (str)
  52. # endif
  53. #else
  54. # define BINDTEXTDOMAIN libintl_bindtextdomain
  55. # define BIND_TEXTDOMAIN_CODESET libintl_bind_textdomain_codeset
  56. #endif
  57. /* Specifies the directory name *DIRNAMEP and the output codeset *CODESETP
  58. to be used for the DOMAINNAME message catalog.
  59. If *DIRNAMEP or *CODESETP is NULL, the corresponding attribute is not
  60. modified, only the current value is returned.
  61. If DIRNAMEP or CODESETP is NULL, the corresponding attribute is neither
  62. modified nor returned. */
  63. static void
  64. set_binding_values (const char *domainname,
  65. const char **dirnamep, const char **codesetp)
  66. {
  67. struct binding *binding;
  68. int modified;
  69. /* Some sanity checks. */
  70. if (domainname == NULL || domainname[0] == '\0')
  71. {
  72. if (dirnamep)
  73. *dirnamep = NULL;
  74. if (codesetp)
  75. *codesetp = NULL;
  76. return;
  77. }
  78. gl_rwlock_wrlock (_nl_state_lock);
  79. modified = 0;
  80. for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
  81. {
  82. int compare = strcmp (domainname, binding->domainname);
  83. if (compare == 0)
  84. /* We found it! */
  85. break;
  86. if (compare < 0)
  87. {
  88. /* It is not in the list. */
  89. binding = NULL;
  90. break;
  91. }
  92. }
  93. if (binding != NULL)
  94. {
  95. if (dirnamep)
  96. {
  97. const char *dirname = *dirnamep;
  98. if (dirname == NULL)
  99. /* The current binding has be to returned. */
  100. *dirnamep = binding->dirname;
  101. else
  102. {
  103. /* The domain is already bound. If the new value and the old
  104. one are equal we simply do nothing. Otherwise replace the
  105. old binding. */
  106. char *result = binding->dirname;
  107. if (strcmp (dirname, result) != 0)
  108. {
  109. if (strcmp (dirname, _nl_default_dirname) == 0)
  110. result = (char *) _nl_default_dirname;
  111. else
  112. {
  113. #if defined _LIBC || defined HAVE_STRDUP
  114. result = strdup (dirname);
  115. #else
  116. size_t len = strlen (dirname) + 1;
  117. result = (char *) malloc (len);
  118. if (__builtin_expect (result != NULL, 1))
  119. memcpy (result, dirname, len);
  120. #endif
  121. }
  122. if (__builtin_expect (result != NULL, 1))
  123. {
  124. if (binding->dirname != _nl_default_dirname)
  125. free (binding->dirname);
  126. binding->dirname = result;
  127. modified = 1;
  128. }
  129. }
  130. *dirnamep = result;
  131. }
  132. }
  133. if (codesetp)
  134. {
  135. const char *codeset = *codesetp;
  136. if (codeset == NULL)
  137. /* The current binding has be to returned. */
  138. *codesetp = binding->codeset;
  139. else
  140. {
  141. /* The domain is already bound. If the new value and the old
  142. one are equal we simply do nothing. Otherwise replace the
  143. old binding. */
  144. char *result = binding->codeset;
  145. if (result == NULL || strcmp (codeset, result) != 0)
  146. {
  147. #if defined _LIBC || defined HAVE_STRDUP
  148. result = strdup (codeset);
  149. #else
  150. size_t len = strlen (codeset) + 1;
  151. result = (char *) malloc (len);
  152. if (__builtin_expect (result != NULL, 1))
  153. memcpy (result, codeset, len);
  154. #endif
  155. if (__builtin_expect (result != NULL, 1))
  156. {
  157. if (binding->codeset != NULL)
  158. free (binding->codeset);
  159. binding->codeset = result;
  160. modified = 1;
  161. }
  162. }
  163. *codesetp = result;
  164. }
  165. }
  166. }
  167. else if ((dirnamep == NULL || *dirnamep == NULL)
  168. && (codesetp == NULL || *codesetp == NULL))
  169. {
  170. /* Simply return the default values. */
  171. if (dirnamep)
  172. *dirnamep = _nl_default_dirname;
  173. if (codesetp)
  174. *codesetp = NULL;
  175. }
  176. else
  177. {
  178. /* We have to create a new binding. */
  179. size_t len = strlen (domainname) + 1;
  180. struct binding *new_binding =
  181. (struct binding *) malloc (offsetof (struct binding, domainname) + len);
  182. if (__builtin_expect (new_binding == NULL, 0))
  183. goto failed;
  184. memcpy (new_binding->domainname, domainname, len);
  185. if (dirnamep)
  186. {
  187. const char *dirname = *dirnamep;
  188. if (dirname == NULL)
  189. /* The default value. */
  190. dirname = _nl_default_dirname;
  191. else
  192. {
  193. if (strcmp (dirname, _nl_default_dirname) == 0)
  194. dirname = _nl_default_dirname;
  195. else
  196. {
  197. char *result;
  198. #if defined _LIBC || defined HAVE_STRDUP
  199. result = strdup (dirname);
  200. if (__builtin_expect (result == NULL, 0))
  201. goto failed_dirname;
  202. #else
  203. size_t len = strlen (dirname) + 1;
  204. result = (char *) malloc (len);
  205. if (__builtin_expect (result == NULL, 0))
  206. goto failed_dirname;
  207. memcpy (result, dirname, len);
  208. #endif
  209. dirname = result;
  210. }
  211. }
  212. *dirnamep = dirname;
  213. new_binding->dirname = (char *) dirname;
  214. }
  215. else
  216. /* The default value. */
  217. new_binding->dirname = (char *) _nl_default_dirname;
  218. if (codesetp)
  219. {
  220. const char *codeset = *codesetp;
  221. if (codeset != NULL)
  222. {
  223. char *result;
  224. #if defined _LIBC || defined HAVE_STRDUP
  225. result = strdup (codeset);
  226. if (__builtin_expect (result == NULL, 0))
  227. goto failed_codeset;
  228. #else
  229. size_t len = strlen (codeset) + 1;
  230. result = (char *) malloc (len);
  231. if (__builtin_expect (result == NULL, 0))
  232. goto failed_codeset;
  233. memcpy (result, codeset, len);
  234. #endif
  235. codeset = result;
  236. }
  237. *codesetp = codeset;
  238. new_binding->codeset = (char *) codeset;
  239. }
  240. else
  241. new_binding->codeset = NULL;
  242. /* Now enqueue it. */
  243. if (_nl_domain_bindings == NULL
  244. || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
  245. {
  246. new_binding->next = _nl_domain_bindings;
  247. _nl_domain_bindings = new_binding;
  248. }
  249. else
  250. {
  251. binding = _nl_domain_bindings;
  252. while (binding->next != NULL
  253. && strcmp (domainname, binding->next->domainname) > 0)
  254. binding = binding->next;
  255. new_binding->next = binding->next;
  256. binding->next = new_binding;
  257. }
  258. modified = 1;
  259. /* Here we deal with memory allocation failures. */
  260. if (0)
  261. {
  262. failed_codeset:
  263. if (new_binding->dirname != _nl_default_dirname)
  264. free (new_binding->dirname);
  265. failed_dirname:
  266. free (new_binding);
  267. failed:
  268. if (dirnamep)
  269. *dirnamep = NULL;
  270. if (codesetp)
  271. *codesetp = NULL;
  272. }
  273. }
  274. /* If we modified any binding, we flush the caches. */
  275. if (modified)
  276. ++_nl_msg_cat_cntr;
  277. gl_rwlock_unlock (_nl_state_lock);
  278. }
  279. /* Specify that the DOMAINNAME message catalog will be found
  280. in DIRNAME rather than in the system locale data base. */
  281. char *
  282. BINDTEXTDOMAIN (const char *domainname, const char *dirname)
  283. {
  284. set_binding_values (domainname, &dirname, NULL);
  285. return (char *) dirname;
  286. }
  287. /* Specify the character encoding in which the messages from the
  288. DOMAINNAME message catalog will be returned. */
  289. char *
  290. BIND_TEXTDOMAIN_CODESET (const char *domainname, const char *codeset)
  291. {
  292. set_binding_values (domainname, NULL, &codeset);
  293. return (char *) codeset;
  294. }
  295. #ifdef _LIBC
  296. /* Aliases for function names in GNU C Library. */
  297. weak_alias (__bindtextdomain, bindtextdomain);
  298. weak_alias (__bind_textdomain_codeset, bind_textdomain_codeset);
  299. #endif