Patch-ID: bash40-030
[bash.git] / lib / intl / finddomain.c
blob69a3586e07d4830161202c90bb1ad660f440f861
1 /* finddomain.c - Handle list of needed message catalogs */
3 /* Copyright (C) 1995-1999, 2000, 2001, 2005-2009 Free Software Foundation, Inc.
4 Written by Ulrich Drepper <drepper@gnu.org>, 1995.
6 This file is part of GNU Bash.
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #if defined HAVE_UNISTD_H || defined _LIBC
32 # include <unistd.h>
33 #endif
35 #include "gettextP.h"
36 #ifdef _LIBC
37 # include <libintl.h>
38 #else
39 # include "libgnuintl.h"
40 #endif
42 /* @@ end of prolog @@ */
43 /* List of already loaded domains. */
44 static struct loaded_l10nfile *_nl_loaded_domains;
47 /* Return a data structure describing the message catalog described by
48 the DOMAINNAME and CATEGORY parameters with respect to the currently
49 established bindings. */
50 struct loaded_l10nfile *
51 internal_function
52 _nl_find_domain (dirname, locale, domainname, domainbinding)
53 const char *dirname;
54 char *locale;
55 const char *domainname;
56 struct binding *domainbinding;
58 struct loaded_l10nfile *retval;
59 const char *language;
60 const char *modifier;
61 const char *territory;
62 const char *codeset;
63 const char *normalized_codeset;
64 const char *special;
65 const char *sponsor;
66 const char *revision;
67 const char *alias_value;
68 int mask;
70 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
72 language[_territory[.codeset]][@modifier]
74 and six parts for the CEN syntax:
76 language[_territory][+audience][+special][,[sponsor][_revision]]
78 Beside the first part all of them are allowed to be missing. If
79 the full specified locale is not found, the less specific one are
80 looked for. The various parts will be stripped off according to
81 the following order:
82 (1) revision
83 (2) sponsor
84 (3) special
85 (4) codeset
86 (5) normalized codeset
87 (6) territory
88 (7) audience/modifier
91 /* If we have already tested for this locale entry there has to
92 be one data set in the list of loaded domains. */
93 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
94 strlen (dirname) + 1, 0, locale, NULL, NULL,
95 NULL, NULL, NULL, NULL, NULL, domainname, 0);
96 if (retval != NULL)
98 /* We know something about this locale. */
99 int cnt;
101 if (retval->decided == 0)
102 _nl_load_domain (retval, domainbinding);
104 if (retval->data != NULL)
105 return retval;
107 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
109 if (retval->successor[cnt]->decided == 0)
110 _nl_load_domain (retval->successor[cnt], domainbinding);
112 if (retval->successor[cnt]->data != NULL)
113 break;
115 return cnt >= 0 ? retval : NULL;
116 /* NOTREACHED */
119 /* See whether the locale value is an alias. If yes its value
120 *overwrites* the alias name. No test for the original value is
121 done. */
122 alias_value = _nl_expand_alias (locale);
123 if (alias_value != NULL)
125 #if defined _LIBC || defined HAVE_STRDUP
126 locale = strdup (alias_value);
127 if (locale == NULL)
128 return NULL;
129 #else
130 size_t len = strlen (alias_value) + 1;
131 locale = (char *) malloc (len);
132 if (locale == NULL)
133 return NULL;
135 memcpy (locale, alias_value, len);
136 #endif
139 /* Now we determine the single parts of the locale name. First
140 look for the language. Termination symbols are `_' and `@' if
141 we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
142 mask = _nl_explode_name (locale, &language, &modifier, &territory,
143 &codeset, &normalized_codeset, &special,
144 &sponsor, &revision);
146 /* Create all possible locale entries which might be interested in
147 generalization. */
148 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
149 strlen (dirname) + 1, mask, language, territory,
150 codeset, normalized_codeset, modifier, special,
151 sponsor, revision, domainname, 1);
152 if (retval == NULL)
153 /* This means we are out of core. */
154 return NULL;
156 if (retval->decided == 0)
157 _nl_load_domain (retval, domainbinding);
158 if (retval->data == NULL)
160 int cnt;
161 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
163 if (retval->successor[cnt]->decided == 0)
164 _nl_load_domain (retval->successor[cnt], domainbinding);
165 if (retval->successor[cnt]->data != NULL)
166 break;
170 /* The room for an alias was dynamically allocated. Free it now. */
171 if (alias_value != NULL)
172 free (locale);
174 /* The space for normalized_codeset is dynamically allocated. Free it. */
175 if (mask & XPG_NORM_CODESET)
176 free ((void *) normalized_codeset);
178 return retval;
182 #ifdef _LIBC
183 libc_freeres_fn (free_mem)
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);
197 #endif