finddomain.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Handle list of needed message catalogs
  2. Copyright (C) 1995-1999, 2000-2001, 2003-2006 Free Software Foundation, Inc.
  3. Written by Ulrich Drepper <drepper@gnu.org>, 1995.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU Library General Public License as published
  6. by the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  15. USA. */
  16. #ifdef HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #if defined HAVE_UNISTD_H || defined _LIBC
  24. # include <unistd.h>
  25. #endif
  26. #include "gettextP.h"
  27. #ifdef _LIBC
  28. # include <libintl.h>
  29. #else
  30. # include "libgnuintl.h"
  31. #endif
  32. /* Handle multi-threaded applications. */
  33. #ifdef _LIBC
  34. # include <bits/libc-lock.h>
  35. # define gl_rwlock_define_initialized __libc_rwlock_define_initialized
  36. # define gl_rwlock_rdlock __libc_rwlock_rdlock
  37. # define gl_rwlock_wrlock __libc_rwlock_wrlock
  38. # define gl_rwlock_unlock __libc_rwlock_unlock
  39. #else
  40. # include "lock.h"
  41. #endif
  42. /* @@ end of prolog @@ */
  43. /* List of already loaded domains. */
  44. static struct loaded_l10nfile *_nl_loaded_domains;
  45. /* Return a data structure describing the message catalog described by
  46. the DOMAINNAME and CATEGORY parameters with respect to the currently
  47. established bindings. */
  48. struct loaded_l10nfile *
  49. internal_function
  50. _nl_find_domain (const char *dirname, char *locale,
  51. const char *domainname, struct binding *domainbinding)
  52. {
  53. struct loaded_l10nfile *retval;
  54. const char *language;
  55. const char *modifier;
  56. const char *territory;
  57. const char *codeset;
  58. const char *normalized_codeset;
  59. const char *alias_value;
  60. int mask;
  61. /* LOCALE can consist of up to four recognized parts for the XPG syntax:
  62. language[_territory][.codeset][@modifier]
  63. Beside the first part all of them are allowed to be missing. If
  64. the full specified locale is not found, the less specific one are
  65. looked for. The various parts will be stripped off according to
  66. the following order:
  67. (1) codeset
  68. (2) normalized codeset
  69. (3) territory
  70. (4) modifier
  71. */
  72. /* We need to protect modifying the _NL_LOADED_DOMAINS data. */
  73. gl_rwlock_define_initialized (static, lock);
  74. gl_rwlock_rdlock (lock);
  75. /* If we have already tested for this locale entry there has to
  76. be one data set in the list of loaded domains. */
  77. retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
  78. strlen (dirname) + 1, 0, locale, NULL, NULL,
  79. NULL, NULL, domainname, 0);
  80. gl_rwlock_unlock (lock);
  81. if (retval != NULL)
  82. {
  83. /* We know something about this locale. */
  84. int cnt;
  85. if (retval->decided <= 0)
  86. _nl_load_domain (retval, domainbinding);
  87. if (retval->data != NULL)
  88. return retval;
  89. for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
  90. {
  91. if (retval->successor[cnt]->decided <= 0)
  92. _nl_load_domain (retval->successor[cnt], domainbinding);
  93. if (retval->successor[cnt]->data != NULL)
  94. break;
  95. }
  96. return retval;
  97. /* NOTREACHED */
  98. }
  99. /* See whether the locale value is an alias. If yes its value
  100. *overwrites* the alias name. No test for the original value is
  101. done. */
  102. alias_value = _nl_expand_alias (locale);
  103. if (alias_value != NULL)
  104. {
  105. #if defined _LIBC || defined HAVE_STRDUP
  106. locale = strdup (alias_value);
  107. if (locale == NULL)
  108. return NULL;
  109. #else
  110. size_t len = strlen (alias_value) + 1;
  111. locale = (char *) malloc (len);
  112. if (locale == NULL)
  113. return NULL;
  114. memcpy (locale, alias_value, len);
  115. #endif
  116. }
  117. /* Now we determine the single parts of the locale name. First
  118. look for the language. Termination symbols are `_', '.', and `@'. */
  119. mask = _nl_explode_name (locale, &language, &modifier, &territory,
  120. &codeset, &normalized_codeset);
  121. /* We need to protect modifying the _NL_LOADED_DOMAINS data. */
  122. gl_rwlock_wrlock (lock);
  123. /* Create all possible locale entries which might be interested in
  124. generalization. */
  125. retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
  126. strlen (dirname) + 1, mask, language, territory,
  127. codeset, normalized_codeset, modifier,
  128. domainname, 1);
  129. gl_rwlock_unlock (lock);
  130. if (retval == NULL)
  131. /* This means we are out of core. */
  132. return NULL;
  133. if (retval->decided <= 0)
  134. _nl_load_domain (retval, domainbinding);
  135. if (retval->data == NULL)
  136. {
  137. int cnt;
  138. for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
  139. {
  140. if (retval->successor[cnt]->decided <= 0)
  141. _nl_load_domain (retval->successor[cnt], domainbinding);
  142. if (retval->successor[cnt]->data != NULL)
  143. break;
  144. }
  145. }
  146. /* The room for an alias was dynamically allocated. Free it now. */
  147. if (alias_value != NULL)
  148. free (locale);
  149. /* The space for normalized_codeset is dynamically allocated. Free it. */
  150. if (mask & XPG_NORM_CODESET)
  151. free ((void *) normalized_codeset);
  152. return retval;
  153. }
  154. #ifdef _LIBC
  155. /* This is called from iconv/gconv_db.c's free_mem, as locales must
  156. be freed before freeing gconv steps arrays. */
  157. void __libc_freeres_fn_section
  158. _nl_finddomain_subfreeres ()
  159. {
  160. struct loaded_l10nfile *runp = _nl_loaded_domains;
  161. while (runp != NULL)
  162. {
  163. struct loaded_l10nfile *here = runp;
  164. if (runp->data != NULL)
  165. _nl_unload_domain ((struct loaded_domain *) runp->data);
  166. runp = runp->next;
  167. free ((char *) here->filename);
  168. free (here);
  169. }
  170. }
  171. #endif