1/* Handle list of needed message catalogs
2 Copyright (C) 1995-2017 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 <http://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 <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. */
51static struct loaded_l10nfile *_nl_loaded_domains;
52
53
54/* Return a data structure describing the message catalog described by
55 the DOMAINNAME and CATEGORY parameters with respect to the currently
56 established bindings. */
57struct loaded_l10nfile *
58internal_function
59_nl_find_domain (const char *dirname, char *locale,
60 const char *domainname, struct binding *domainbinding)
61{
62 struct loaded_l10nfile *retval;
63 const char *language;
64 const char *modifier;
65 const char *territory;
66 const char *codeset;
67 const char *normalized_codeset;
68 const char *alias_value;
69 int mask;
70
71 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
72
73 language[_territory][.codeset][@modifier]
74
75 Beside the first part all of them are allowed to be missing. If
76 the full specified locale is not found, the less specific one are
77 looked for. The various parts will be stripped off according to
78 the following order:
79 (1) codeset
80 (2) normalized codeset
81 (3) territory
82 (4) modifier
83 */
84
85 /* We need to protect modifying the _NL_LOADED_DOMAINS data. */
86 gl_rwlock_define_initialized (static, lock);
87 gl_rwlock_rdlock (lock);
88
89 /* If we have already tested for this locale entry there has to
90 be one data set in the list of loaded domains. */
91 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
92 strlen (dirname) + 1, 0, locale, NULL, NULL,
93 NULL, NULL, domainname, 0);
94
95 gl_rwlock_unlock (lock);
96
97 if (retval != NULL)
98 {
99 /* We know something about this locale. */
100 int cnt;
101
102 if (retval->decided <= 0)
103 _nl_load_domain (retval, domainbinding);
104
105 if (retval->data != NULL)
106 return retval;
107
108 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
109 {
110 if (retval->successor[cnt]->decided <= 0)
111 _nl_load_domain (retval->successor[cnt], domainbinding);
112
113 if (retval->successor[cnt]->data != NULL)
114 break;
115 }
116
117 return retval;
118 /* NOTREACHED */
119 }
120
121 /* See whether the locale value is an alias. If yes its value
122 *overwrites* the alias name. No test for the original value is
123 done. */
124 alias_value = _nl_expand_alias (locale);
125 if (alias_value != NULL)
126 {
127 size_t len = strlen (alias_value) + 1;
128 locale = (char *) malloc (len);
129 if (locale == NULL)
130 return NULL;
131
132 memcpy (locale, alias_value, len);
133 }
134
135 /* Now we determine the single parts of the locale name. First
136 look for the language. Termination symbols are `_', '.', and `@'. */
137 mask = _nl_explode_name (locale, &language, &modifier, &territory,
138 &codeset, &normalized_codeset);
139 if (mask == -1)
140 /* This means we are out of core. */
141 return NULL;
142
143 /* We need to protect modifying the _NL_LOADED_DOMAINS data. */
144 gl_rwlock_wrlock (lock);
145
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,
151 domainname, 1);
152
153 gl_rwlock_unlock (lock);
154
155 if (retval == NULL)
156 /* This means we are out of core. */
157 goto out;
158
159 if (retval->decided <= 0)
160 _nl_load_domain (retval, domainbinding);
161 if (retval->data == NULL)
162 {
163 int cnt;
164 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
165 {
166 if (retval->successor[cnt]->decided <= 0)
167 _nl_load_domain (retval->successor[cnt], domainbinding);
168 if (retval->successor[cnt]->data != NULL)
169 break;
170 }
171 }
172
173 /* The room for an alias was dynamically allocated. Free it now. */
174 if (alias_value != NULL)
175 free (locale);
176
177out:
178 /* The space for normalized_codeset is dynamically allocated. Free it. */
179 if (mask & XPG_NORM_CODESET)
180 free ((void *) normalized_codeset);
181
182 return retval;
183}
184
185
186#ifdef _LIBC
187/* This is called from iconv/gconv_db.c's free_mem, as locales must
188 be freed before freeing gconv steps arrays. */
189void __libc_freeres_fn_section
190_nl_finddomain_subfreeres (void)
191{
192 struct loaded_l10nfile *runp = _nl_loaded_domains;
193
194 while (runp != NULL)
195 {
196 struct loaded_l10nfile *here = runp;
197 if (runp->data != NULL)
198 _nl_unload_domain ((struct loaded_domain *) runp->data);
199 runp = runp->next;
200 free ((char *) here->filename);
201 free (here);
202 }
203}
204#endif
205