You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

finddomain.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Handle list of needed message catalogs
  2. Copyright (C) 1995-1999, 2000-2001, 2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  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 (const char *dirname, char *locale,
  41. const char *domainname, struct binding *domainbinding)
  42. {
  43. struct loaded_l10nfile *retval;
  44. const char *language;
  45. const char *modifier;
  46. const char *territory;
  47. const char *codeset;
  48. const char *normalized_codeset;
  49. const char *special;
  50. const char *sponsor;
  51. const char *revision;
  52. const char *alias_value;
  53. int mask;
  54. /* LOCALE can consist of up to four recognized parts for the XPG syntax:
  55. language[_territory[.codeset]][@modifier]
  56. and six parts for the CEN syntax:
  57. language[_territory][+audience][+special][,[sponsor][_revision]]
  58. Beside the first part all of them are allowed to be missing. If
  59. the full specified locale is not found, the less specific one are
  60. looked for. The various parts will be stripped off according to
  61. the following order:
  62. (1) revision
  63. (2) sponsor
  64. (3) special
  65. (4) codeset
  66. (5) normalized codeset
  67. (6) territory
  68. (7) audience/modifier
  69. */
  70. /* If we have already tested for this locale entry there has to
  71. be one data set in the list of loaded domains. */
  72. retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
  73. strlen (dirname) + 1, 0, locale, NULL, NULL,
  74. NULL, NULL, NULL, NULL, NULL, domainname, 0);
  75. if (retval != NULL)
  76. {
  77. /* We know something about this locale. */
  78. int cnt;
  79. if (retval->decided == 0)
  80. _nl_load_domain (retval, domainbinding);
  81. if (retval->data != NULL)
  82. return retval;
  83. for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
  84. {
  85. if (retval->successor[cnt]->decided == 0)
  86. _nl_load_domain (retval->successor[cnt], domainbinding);
  87. if (retval->successor[cnt]->data != NULL)
  88. break;
  89. }
  90. return cnt >= 0 ? retval : NULL;
  91. /* NOTREACHED */
  92. }
  93. /* See whether the locale value is an alias. If yes its value
  94. *overwrites* the alias name. No test for the original value is
  95. done. */
  96. alias_value = _nl_expand_alias (locale);
  97. if (alias_value != NULL)
  98. {
  99. #if defined _LIBC || defined HAVE_STRDUP
  100. locale = strdup (alias_value);
  101. if (locale == NULL)
  102. return NULL;
  103. #else
  104. size_t len = strlen (alias_value) + 1;
  105. locale = (char *) malloc (len);
  106. if (locale == NULL)
  107. return NULL;
  108. memcpy (locale, alias_value, len);
  109. #endif
  110. }
  111. /* Now we determine the single parts of the locale name. First
  112. look for the language. Termination symbols are `_' and `@' if
  113. we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
  114. mask = _nl_explode_name (locale, &language, &modifier, &territory,
  115. &codeset, &normalized_codeset, &special,
  116. &sponsor, &revision);
  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, special,
  122. sponsor, revision, domainname, 1);
  123. if (retval == NULL)
  124. /* This means we are out of core. */
  125. return NULL;
  126. if (retval->decided == 0)
  127. _nl_load_domain (retval, domainbinding);
  128. if (retval->data == NULL)
  129. {
  130. int cnt;
  131. for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
  132. {
  133. if (retval->successor[cnt]->decided == 0)
  134. _nl_load_domain (retval->successor[cnt], domainbinding);
  135. if (retval->successor[cnt]->data != NULL)
  136. break;
  137. }
  138. }
  139. /* The room for an alias was dynamically allocated. Free it now. */
  140. if (alias_value != NULL)
  141. free (locale);
  142. /* The space for normalized_codeset is dynamically allocated. Free it. */
  143. if (mask & XPG_NORM_CODESET)
  144. free ((void *) normalized_codeset);
  145. return retval;
  146. }
  147. #ifdef _LIBC
  148. libc_freeres_fn (free_mem)
  149. {
  150. struct loaded_l10nfile *runp = _nl_loaded_domains;
  151. while (runp != NULL)
  152. {
  153. struct loaded_l10nfile *here = runp;
  154. if (runp->data != NULL)
  155. _nl_unload_domain ((struct loaded_domain *) runp->data);
  156. runp = runp->next;
  157. free ((char *) here->filename);
  158. free (here);
  159. }
  160. }
  161. #endif