finddomain.c 5.7 KB

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