finddomain.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 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. /* @@ end of prolog @@ */
  33. /* List of already loaded domains. */
  34. static struct loaded_l10nfile *_nl_loaded_domains;
  35. /* Return a data structure describing the message catalog described by
  36. the DOMAINNAME and CATEGORY parameters with respect to the currently
  37. established bindings. */
  38. struct loaded_l10nfile *
  39. internal_function
  40. _nl_find_domain (dirname, locale, domainname, domainbinding)
  41. const char *dirname;
  42. char *locale;
  43. const char *domainname;
  44. struct binding *domainbinding;
  45. {
  46. struct loaded_l10nfile *retval;
  47. const char *language;
  48. const char *modifier;
  49. const char *territory;
  50. const char *codeset;
  51. const char *normalized_codeset;
  52. const char *special;
  53. const char *sponsor;
  54. const char *revision;
  55. const char *alias_value;
  56. int mask;
  57. /* LOCALE can consist of up to four recognized parts for the XPG syntax:
  58. language[_territory[.codeset]][@modifier]
  59. and six parts for the CEN syntax:
  60. language[_territory][+audience][+special][,[sponsor][_revision]]
  61. Beside the first part all of them are allowed to be missing. If
  62. the full specified locale is not found, the less specific one are
  63. looked for. The various parts will be stripped off according to
  64. the following order:
  65. (1) revision
  66. (2) sponsor
  67. (3) special
  68. (4) codeset
  69. (5) normalized codeset
  70. (6) territory
  71. (7) audience/modifier
  72. */
  73. /* If we have already tested for this locale entry there has to
  74. be one data set in the list of loaded domains. */
  75. retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
  76. strlen (dirname) + 1, 0, locale, NULL, NULL,
  77. NULL, NULL, NULL, NULL, NULL, domainname, 0);
  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 cnt >= 0 ? retval : NULL;
  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. #if defined _LIBC || defined HAVE_STRDUP
  103. locale = strdup (alias_value);
  104. if (locale == NULL)
  105. return NULL;
  106. #else
  107. size_t len = strlen (alias_value) + 1;
  108. locale = (char *) malloc (len);
  109. if (locale == NULL)
  110. return NULL;
  111. memcpy (locale, alias_value, len);
  112. #endif
  113. }
  114. /* Now we determine the single parts of the locale name. First
  115. look for the language. Termination symbols are `_' and `@' if
  116. we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
  117. mask = _nl_explode_name (locale, &language, &modifier, &territory,
  118. &codeset, &normalized_codeset, &special,
  119. &sponsor, &revision);
  120. /* Create all possible locale entries which might be interested in
  121. generalization. */
  122. retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
  123. strlen (dirname) + 1, mask, language, territory,
  124. codeset, normalized_codeset, modifier, special,
  125. sponsor, revision, domainname, 1);
  126. if (retval == NULL)
  127. /* This means we are out of core. */
  128. return NULL;
  129. if (retval->decided == 0)
  130. _nl_load_domain (retval, domainbinding);
  131. if (retval->data == NULL)
  132. {
  133. int cnt;
  134. for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
  135. {
  136. if (retval->successor[cnt]->decided == 0)
  137. _nl_load_domain (retval->successor[cnt], domainbinding);
  138. if (retval->successor[cnt]->data != NULL)
  139. break;
  140. }
  141. }
  142. /* The room for an alias was dynamically allocated. Free it now. */
  143. if (alias_value != NULL)
  144. free (locale);
  145. /* The space for normalized_codeset is dynamically allocated. Free it. */
  146. if (mask & XPG_NORM_CODESET)
  147. free ((void *) normalized_codeset);
  148. return retval;
  149. }
  150. #ifdef _LIBC
  151. libc_freeres_fn (free_mem)
  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. #endif