1 /* Handle list of needed message catalogs
2 Copyright (C) 1995-1999, 2000-2001, 2003-2006 Free Software Foundation, Inc.
3 Written by Ulrich Drepper <drepper@gnu.org>, 1995.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published
7 by the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 USA. */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #if defined HAVE_UNISTD_H || defined _LIBC
30 # include <unistd.h>
31 #endif
32
33 #include "gettextP.h"
34 #ifdef _LIBC
35 # include <libintl.h>
36 #else
37 # include "libgnuintl.h"
38 #endif
39
40 /* Handle multi-threaded applications. */
41 #ifdef _LIBC
42 # include <bits/libc-lock.h>
43 # define gl_rwlock_define_initialized __libc_rwlock_define_initialized
44 # define gl_rwlock_rdlock __libc_rwlock_rdlock
45 # define gl_rwlock_wrlock __libc_rwlock_wrlock
46 # define gl_rwlock_unlock __libc_rwlock_unlock
47 #else
48 # include "lock.h"
49 #endif
50
51 /* @@ end of prolog @@ */
52 /* List of already loaded domains. */
53 static struct loaded_l10nfile *_nl_loaded_domains;
54
55
56 /* Return a data structure describing the message catalog described by
57 the DOMAINNAME and CATEGORY parameters with respect to the currently
58 established bindings. */
59 struct loaded_l10nfile *
60 internal_function
_nl_find_domain(const char * dirname,char * locale,const char * domainname,struct binding * domainbinding)61 _nl_find_domain (const char *dirname, char *locale,
62 const char *domainname, struct binding *domainbinding)
63 {
64 struct loaded_l10nfile *retval;
65 const char *language;
66 const char *modifier;
67 const char *territory;
68 const char *codeset;
69 const char *normalized_codeset;
70 const char *alias_value;
71 int mask;
72
73 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
74
75 language[_territory][.codeset][@modifier]
76
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) codeset
82 (2) normalized codeset
83 (3) territory
84 (4) modifier
85 */
86
87 /* We need to protect modifying the _NL_LOADED_DOMAINS data. */
88 gl_rwlock_define_initialized (static, lock);
89 gl_rwlock_rdlock (lock);
90
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, domainname, 0);
96
97 gl_rwlock_unlock (lock);
98
99 if (retval != NULL)
100 {
101 /* We know something about this locale. */
102 int cnt;
103
104 if (retval->decided <= 0)
105 _nl_load_domain (retval, domainbinding);
106
107 if (retval->data != NULL)
108 return retval;
109
110 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
111 {
112 if (retval->successor[cnt]->decided <= 0)
113 _nl_load_domain (retval->successor[cnt], domainbinding);
114
115 if (retval->successor[cnt]->data != NULL)
116 break;
117 }
118
119 return retval;
120 /* NOTREACHED */
121 }
122
123 /* See whether the locale value is an alias. If yes its value
124 *overwrites* the alias name. No test for the original value is
125 done. */
126 alias_value = _nl_expand_alias (locale);
127 if (alias_value != NULL)
128 {
129 #if defined _LIBC || defined HAVE_STRDUP
130 locale = strdup (alias_value);
131 if (locale == NULL)
132 return NULL;
133 #else
134 size_t len = strlen (alias_value) + 1;
135 locale = (char *) malloc (len);
136 if (locale == NULL)
137 return NULL;
138
139 memcpy (locale, alias_value, len);
140 #endif
141 }
142
143 /* Now we determine the single parts of the locale name. First
144 look for the language. Termination symbols are `_', '.', and `@'. */
145 mask = _nl_explode_name (locale, &language, &modifier, &territory,
146 &codeset, &normalized_codeset);
147
148 /* We need to protect modifying the _NL_LOADED_DOMAINS data. */
149 gl_rwlock_wrlock (lock);
150
151 /* Create all possible locale entries which might be interested in
152 generalization. */
153 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
154 strlen (dirname) + 1, mask, language, territory,
155 codeset, normalized_codeset, modifier,
156 domainname, 1);
157
158 gl_rwlock_unlock (lock);
159
160 if (retval == NULL)
161 /* This means we are out of core. */
162 return NULL;
163
164 if (retval->decided <= 0)
165 _nl_load_domain (retval, domainbinding);
166 if (retval->data == NULL)
167 {
168 int cnt;
169 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
170 {
171 if (retval->successor[cnt]->decided <= 0)
172 _nl_load_domain (retval->successor[cnt], domainbinding);
173 if (retval->successor[cnt]->data != NULL)
174 break;
175 }
176 }
177
178 /* The room for an alias was dynamically allocated. Free it now. */
179 if (alias_value != NULL)
180 free (locale);
181
182 /* The space for normalized_codeset is dynamically allocated. Free it. */
183 if (mask & XPG_NORM_CODESET)
184 free ((void *) normalized_codeset);
185
186 return retval;
187 }
188
189
190 #ifdef _LIBC
191 /* This is called from iconv/gconv_db.c's free_mem, as locales must
192 be freed before freeing gconv steps arrays. */
193 void __libc_freeres_fn_section
_nl_finddomain_subfreeres()194 _nl_finddomain_subfreeres ()
195 {
196 struct loaded_l10nfile *runp = _nl_loaded_domains;
197
198 while (runp != NULL)
199 {
200 struct loaded_l10nfile *here = runp;
201 if (runp->data != NULL)
202 _nl_unload_domain ((struct loaded_domain *) runp->data);
203 runp = runp->next;
204 free ((char *) here->filename);
205 free (here);
206 }
207 }
208 #endif
209