1 /* Handle list of needed message catalogs
2 Copyright (C) 1995-2020 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
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) 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
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #if defined HAVE_UNISTD_H || defined _LIBC
28 # include <unistd.h>
29 #endif
30
31 #include "gettextP.h"
32 #ifdef _LIBC
33 # include <libintl.h>
34 #else
35 # include "libgnuintl.h"
36 #endif
37
38 /* Handle multi-threaded applications. */
39 #ifdef _LIBC
40 # include <bits/libc-lock.h>
41 # define gl_rwlock_define_initialized __libc_rwlock_define_initialized
42 # define gl_rwlock_rdlock __libc_rwlock_rdlock
43 # define gl_rwlock_wrlock __libc_rwlock_wrlock
44 # define gl_rwlock_unlock __libc_rwlock_unlock
45 #else
46 # include "lock.h"
47 #endif
48
49 /* @@ end of prolog @@ */
50 /* List of already loaded domains. */
51 static struct loaded_l10nfile *_nl_loaded_domains;
52
53 /* Lock that protects the access to _NL_LOADED_DOMAINS. */
54 gl_rwlock_define_initialized (static, lock);
55
56
57 /* Return a data structure describing the message catalog described by
58 the DIRNAME or WDIRNAME, LOCALE, and DOMAINNAME parameters with respect
59 to the currently established bindings. */
60 struct loaded_l10nfile *
61 internal_function
_nl_find_domain(const char * dirname,const wchar_t * wdirname,char * locale,const char * domainname,struct binding * domainbinding)62 _nl_find_domain (const char *dirname,
63 #if defined _WIN32 && !defined __CYGWIN__
64 const wchar_t *wdirname,
65 #endif
66 char *locale,
67 const char *domainname, struct binding *domainbinding)
68 {
69 struct loaded_l10nfile *retval;
70 const char *language;
71 const char *modifier;
72 const char *territory;
73 const char *codeset;
74 const char *normalized_codeset;
75 const char *alias_value;
76 int mask;
77
78 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
79
80 language[_territory][.codeset][@modifier]
81
82 Beside the first part all of them are allowed to be missing. If
83 the full specified locale is not found, the less specific one are
84 looked for. The various parts will be stripped off according to
85 the following order:
86 (1) codeset
87 (2) normalized codeset
88 (3) territory
89 (4) modifier
90 */
91
92 /* We need to protect modifying the _NL_LOADED_DOMAINS data. */
93 gl_rwlock_rdlock (lock);
94
95 /* If we have already tested for this locale entry there has to
96 be one data set in the list of loaded domains. */
97 retval = _nl_make_l10nflist (&_nl_loaded_domains,
98 dirname,
99 dirname != NULL ? strlen (dirname) + 1 : 0,
100 #if defined _WIN32 && !defined __CYGWIN__
101 wdirname,
102 wdirname != NULL ? wcslen (wdirname) + 1 : 0,
103 #endif
104 0, locale, NULL, NULL, NULL, NULL,
105 domainname, 0);
106
107 gl_rwlock_unlock (lock);
108
109 if (retval != NULL)
110 {
111 /* We know something about this locale. */
112 int cnt;
113
114 if (retval->decided <= 0)
115 _nl_load_domain (retval, domainbinding);
116
117 if (retval->data != NULL)
118 return retval;
119
120 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
121 {
122 if (retval->successor[cnt]->decided <= 0)
123 _nl_load_domain (retval->successor[cnt], domainbinding);
124
125 if (retval->successor[cnt]->data != NULL)
126 break;
127 }
128
129 return retval;
130 /* NOTREACHED */
131 }
132
133 /* See whether the locale value is an alias. If yes its value
134 *overwrites* the alias name. No test for the original value is
135 done. */
136 alias_value = _nl_expand_alias (locale);
137 if (alias_value != NULL)
138 {
139 #if defined _LIBC || defined HAVE_STRDUP
140 locale = strdup (alias_value);
141 if (locale == NULL)
142 return NULL;
143 #else
144 size_t len = strlen (alias_value) + 1;
145 locale = (char *) malloc (len);
146 if (locale == NULL)
147 return NULL;
148
149 memcpy (locale, alias_value, len);
150 #endif
151 }
152
153 /* Now we determine the single parts of the locale name. First
154 look for the language. Termination symbols are `_', '.', and `@'. */
155 mask = _nl_explode_name (locale, &language, &modifier, &territory,
156 &codeset, &normalized_codeset);
157 if (mask == -1)
158 /* This means we are out of core. */
159 return NULL;
160
161 /* We need to protect modifying the _NL_LOADED_DOMAINS data. */
162 gl_rwlock_wrlock (lock);
163
164 /* Create all possible locale entries which might be interested in
165 generalization. */
166 retval = _nl_make_l10nflist (&_nl_loaded_domains,
167 dirname,
168 dirname != NULL ? strlen (dirname) + 1 : 0,
169 #if defined _WIN32 && !defined __CYGWIN__
170 wdirname,
171 wdirname != NULL ? wcslen (wdirname) + 1 : 0,
172 #endif
173 mask, language, territory,
174 codeset, normalized_codeset, modifier,
175 domainname, 1);
176
177 gl_rwlock_unlock (lock);
178
179 if (retval == NULL)
180 /* This means we are out of core. */
181 goto out;
182
183 if (retval->decided <= 0)
184 _nl_load_domain (retval, domainbinding);
185 if (retval->data == NULL)
186 {
187 int cnt;
188 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
189 {
190 if (retval->successor[cnt]->decided <= 0)
191 _nl_load_domain (retval->successor[cnt], domainbinding);
192 if (retval->successor[cnt]->data != NULL)
193 break;
194 }
195 }
196
197 /* The room for an alias was dynamically allocated. Free it now. */
198 if (alias_value != NULL)
199 free (locale);
200
201 out:
202 /* The space for normalized_codeset is dynamically allocated. Free it. */
203 if (mask & XPG_NORM_CODESET)
204 free ((void *) normalized_codeset);
205
206 return retval;
207 }
208
209
210 #ifdef _LIBC
211 /* This is called from iconv/gconv_db.c's free_mem, as locales must
212 be freed before freeing gconv steps arrays. */
213 void __libc_freeres_fn_section
_nl_finddomain_subfreeres(void)214 _nl_finddomain_subfreeres (void)
215 {
216 struct loaded_l10nfile *runp = _nl_loaded_domains;
217
218 while (runp != NULL)
219 {
220 struct loaded_l10nfile *here = runp;
221 if (runp->data != NULL)
222 _nl_unload_domain ((struct loaded_domain *) runp->data);
223 runp = runp->next;
224 free ((char *) here->filename);
225 free (here);
226 }
227 }
228 #endif
229