Update.
[glibc.git] / intl / finddomain.c
blobd487bbdf42d8555f251050d35e0416fb006381e1
1 /* Handle list of needed message catalogs
2 Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Ulrich Drepper <drepper@gnu.org>, 1995.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #if defined HAVE_UNISTD_H || defined _LIBC
31 # include <unistd.h>
32 #endif
34 #include "gettextP.h"
35 #ifdef _LIBC
36 # include <libintl.h>
37 #else
38 # include "libgnuintl.h"
39 #endif
41 /* @@ end of prolog @@ */
42 /* List of already loaded domains. */
43 static struct loaded_l10nfile *_nl_loaded_domains;
46 /* Return a data structure describing the message catalog described by
47 the DOMAINNAME and CATEGORY parameters with respect to the currently
48 established bindings. */
49 struct loaded_l10nfile *
50 internal_function
51 _nl_find_domain (dirname, locale, domainname, domainbinding)
52 const char *dirname;
53 char *locale;
54 const char *domainname;
55 struct binding *domainbinding;
57 struct loaded_l10nfile *retval;
58 const char *language;
59 const char *modifier;
60 const char *territory;
61 const char *codeset;
62 const char *normalized_codeset;
63 const char *special;
64 const char *sponsor;
65 const char *revision;
66 const char *alias_value;
67 int mask;
69 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
71 language[_territory[.codeset]][@modifier]
73 and six parts for the CEN syntax:
75 language[_territory][+audience][+special][,[sponsor][_revision]]
77 Beside the first part all of them are allowed to be missing. If
78 the full specified locale is not found, the less specific one are
79 looked for. The various parts will be stripped off according to
80 the following order:
81 (1) revision
82 (2) sponsor
83 (3) special
84 (4) codeset
85 (5) normalized codeset
86 (6) territory
87 (7) audience/modifier
90 /* If we have already tested for this locale entry there has to
91 be one data set in the list of loaded domains. */
92 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
93 strlen (dirname) + 1, 0, locale, NULL, NULL,
94 NULL, NULL, NULL, NULL, NULL, domainname, 0);
95 if (retval != NULL)
97 /* We know something about this locale. */
98 int cnt;
100 if (retval->decided == 0)
101 _nl_load_domain (retval, domainbinding);
103 if (retval->data != NULL)
104 return retval;
106 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
108 if (retval->successor[cnt]->decided == 0)
109 _nl_load_domain (retval->successor[cnt], domainbinding);
111 if (retval->successor[cnt]->data != NULL)
112 break;
114 return cnt >= 0 ? retval : NULL;
115 /* NOTREACHED */
118 /* See whether the locale value is an alias. If yes its value
119 *overwrites* the alias name. No test for the original value is
120 done. */
121 alias_value = _nl_expand_alias (locale);
122 if (alias_value != NULL)
124 #if defined _LIBC || defined HAVE_STRDUP
125 locale = strdup (alias_value);
126 if (locale == NULL)
127 return NULL;
128 #else
129 size_t len = strlen (alias_value) + 1;
130 locale = (char *) malloc (len);
131 if (locale == NULL)
132 return NULL;
134 memcpy (locale, alias_value, len);
135 #endif
138 /* Now we determine the single parts of the locale name. First
139 look for the language. Termination symbols are `_' and `@' if
140 we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
141 mask = _nl_explode_name (locale, &language, &modifier, &territory,
142 &codeset, &normalized_codeset, &special,
143 &sponsor, &revision);
145 /* Create all possible locale entries which might be interested in
146 generalization. */
147 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
148 strlen (dirname) + 1, mask, language, territory,
149 codeset, normalized_codeset, modifier, special,
150 sponsor, revision, domainname, 1);
151 if (retval == NULL)
152 /* This means we are out of core. */
153 return NULL;
155 if (retval->decided == 0)
156 _nl_load_domain (retval, domainbinding);
157 if (retval->data == NULL)
159 int cnt;
160 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
162 if (retval->successor[cnt]->decided == 0)
163 _nl_load_domain (retval->successor[cnt], domainbinding);
164 if (retval->successor[cnt]->data != NULL)
165 break;
169 /* The room for an alias was dynamically allocated. Free it now. */
170 if (alias_value != NULL)
171 free (locale);
173 /* The space for normalized_codeset is dynamically allocated. Free it. */
174 if (mask & XPG_NORM_CODESET)
175 free ((void *) normalized_codeset);
177 return retval;
181 #ifdef _LIBC
182 static void __attribute__ ((unused))
183 free_mem (void)
185 struct loaded_l10nfile *runp = _nl_loaded_domains;
187 while (runp != NULL)
189 struct loaded_l10nfile *here = runp;
190 if (runp->data != NULL)
191 _nl_unload_domain ((struct loaded_domain *) runp->data);
192 runp = runp->next;
193 free ((char *) here->filename);
194 free (here);
198 text_set_element (__libc_subfreeres, free_mem);
199 #endif