finddomain.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* Handle list of needed message catalogs
  2. Copyright (C) 1995-1999, 2000, 2001 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 General Public License as published by
  6. 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #if defined HAVE_UNISTD_H || defined _LIBC
  23. # include <unistd.h>
  24. #endif
  25. #include "gettextP.h"
  26. #ifdef _LIBC
  27. # include <libintl.h>
  28. #else
  29. # include "libgnuintl.h"
  30. #endif
  31. /* @@ end of prolog @@ */
  32. /* List of already loaded domains. */
  33. static struct loaded_l10nfile *_nl_loaded_domains;
  34. /* Return a data structure describing the message catalog described by
  35. the DOMAINNAME and CATEGORY parameters with respect to the currently
  36. established bindings. */
  37. struct loaded_l10nfile *
  38. internal_function
  39. _nl_find_domain (dirname, locale, domainname, domainbinding)
  40. const char *dirname;
  41. char *locale;
  42. const char *domainname;
  43. struct binding *domainbinding;
  44. {
  45. struct loaded_l10nfile *retval;
  46. const char *language;
  47. const char *modifier;
  48. const char *territory;
  49. const char *codeset;
  50. const char *normalized_codeset;
  51. const char *special;
  52. const char *sponsor;
  53. const char *revision;
  54. const char *alias_value;
  55. int mask;
  56. /* LOCALE can consist of up to four recognized parts for the XPG syntax:
  57. language[_territory[.codeset]][@modifier]
  58. and six parts for the CEN syntax:
  59. language[_territory][+audience][+special][,[sponsor][_revision]]
  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) revision
  65. (2) sponsor
  66. (3) special
  67. (4) codeset
  68. (5) normalized codeset
  69. (6) territory
  70. (7) audience/modifier
  71. */
  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, NULL, NULL, NULL, domainname, 0);
  77. if (retval != NULL)
  78. {
  79. /* We know something about this locale. */
  80. int cnt;
  81. if (retval->decided == 0)
  82. _nl_load_domain (retval, domainbinding);
  83. if (retval->data != NULL)
  84. return retval;
  85. for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
  86. {
  87. if (retval->successor[cnt]->decided == 0)
  88. _nl_load_domain (retval->successor[cnt], domainbinding);
  89. if (retval->successor[cnt]->data != NULL)
  90. break;
  91. }
  92. return cnt >= 0 ? retval : NULL;
  93. /* NOTREACHED */
  94. }
  95. /* See whether the locale value is an alias. If yes its value
  96. *overwrites* the alias name. No test for the original value is
  97. done. */
  98. alias_value = _nl_expand_alias (locale);
  99. if (alias_value != NULL)
  100. {
  101. #if defined _LIBC || defined HAVE_STRDUP
  102. locale = strdup (alias_value);
  103. if (locale == NULL)
  104. return NULL;
  105. #else
  106. size_t len = strlen (alias_value) + 1;
  107. locale = (char *) malloc (len);
  108. if (locale == NULL)
  109. return NULL;
  110. memcpy (locale, alias_value, len);
  111. #endif
  112. }
  113. /* Now we determine the single parts of the locale name. First
  114. look for the language. Termination symbols are `_' and `@' if
  115. we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
  116. mask = _nl_explode_name (locale, &language, &modifier, &territory,
  117. &codeset, &normalized_codeset, &special,
  118. &sponsor, &revision);
  119. /* Create all possible locale entries which might be interested in
  120. generalization. */
  121. retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
  122. strlen (dirname) + 1, mask, language, territory,
  123. codeset, normalized_codeset, modifier, special,
  124. sponsor, revision, domainname, 1);
  125. if (retval == NULL)
  126. /* This means we are out of core. */
  127. return NULL;
  128. if (retval->decided == 0)
  129. _nl_load_domain (retval, domainbinding);
  130. if (retval->data == NULL)
  131. {
  132. int cnt;
  133. for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
  134. {
  135. if (retval->successor[cnt]->decided == 0)
  136. _nl_load_domain (retval->successor[cnt], domainbinding);
  137. if (retval->successor[cnt]->data != NULL)
  138. break;
  139. }
  140. }
  141. /* The room for an alias was dynamically allocated. Free it now. */
  142. if (alias_value != NULL)
  143. free (locale);
  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. static void __attribute__ ((unused))
  151. free_mem (void)
  152. {
  153. struct loaded_l10nfile *runp = _nl_loaded_domains;
  154. while (runp != NULL)
  155. {
  156. struct loaded_l10nfile *here = runp;
  157. if (runp->data != NULL)
  158. _nl_unload_domain ((struct loaded_domain *) runp->data);
  159. runp = runp->next;
  160. free ((char *) here->filename);
  161. free (here);
  162. }
  163. }
  164. text_set_element (__libc_subfreeres, free_mem);
  165. #endif