]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/findlocale.c
* locale/findlocale.c [NL_CURRENT_INDIRECT] (_nl_C): New variable.
[thirdparty/glibc.git] / locale / findlocale.c
1 /* Copyright (C) 1996-2001, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 #include <assert.h>
21 #include <locale.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #ifdef _POSIX_MAPPED_FILES
26 # include <sys/mman.h>
27 #endif
28
29 #include "localeinfo.h"
30 #include "../iconv/gconv_charset.h"
31
32
33 #ifdef NL_CURRENT_INDIRECT
34 # define DEFINE_CATEGORY(category, category_name, items, a) \
35 extern struct locale_data _nl_C_##category; \
36 weak_extern (_nl_C_##category)
37 # include "categories.def"
38 # undef DEFINE_CATEGORY
39
40 /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
41 Elements are zero for categories whose data is never used. */
42 struct locale_data *const _nl_C[] attribute_hidden =
43 {
44 # define DEFINE_CATEGORY(category, category_name, items, a) \
45 [category] = &_nl_C_##category,
46 # include "categories.def"
47 # undef DEFINE_CATEGORY
48 };
49 #else
50 # define _nl_C (_nl_C_locobj.__locales)
51 #endif
52
53
54 /* For each category we keep a list of records for the locale files
55 which are somehow addressed. */
56 struct loaded_l10nfile *_nl_locale_file_list[__LC_LAST];
57
58 const char _nl_default_locale_path[] attribute_hidden = LOCALEDIR;
59
60
61 struct locale_data *
62 internal_function
63 _nl_find_locale (const char *locale_path, size_t locale_path_len,
64 int category, const char **name)
65 {
66 int mask;
67 /* Name of the locale for this category. */
68 char *loc_name;
69 const char *language;
70 const char *modifier;
71 const char *territory;
72 const char *codeset;
73 const char *normalized_codeset;
74 struct loaded_l10nfile *locale_file;
75
76 if ((*name)[0] == '\0')
77 {
78 /* The user decides which locale to use by setting environment
79 variables. */
80 *name = getenv ("LC_ALL");
81 if (*name == NULL || (*name)[0] == '\0')
82 *name = getenv (_nl_category_names[category]);
83 if (*name == NULL || (*name)[0] == '\0')
84 *name = getenv ("LANG");
85 }
86
87 if (*name == NULL || (*name)[0] == '\0'
88 || (__builtin_expect (__libc_enable_secure, 0)
89 && strchr (*name, '/') != NULL))
90 *name = (char *) _nl_C_name;
91
92 if (__builtin_expect (strcmp (*name, _nl_C_name), 1) == 0
93 || __builtin_expect (strcmp (*name, _nl_POSIX_name), 1) == 0)
94 {
95 /* We need not load anything. The needed data is contained in
96 the library itself. */
97 *name = (char *) _nl_C_name;
98 return _nl_C[category];
99 }
100
101 /* We really have to load some data. First we try the archive,
102 but only if there was no LOCPATH environment variable specified. */
103 if (__builtin_expect (locale_path == NULL, 1))
104 {
105 struct locale_data *data = _nl_load_locale_from_archive (category, name);
106 if (__builtin_expect (data != NULL, 1))
107 return data;
108
109 /* Nothing in the archive. Set the default path to search below. */
110 locale_path = _nl_default_locale_path;
111 locale_path_len = sizeof _nl_default_locale_path;
112 }
113
114 /* We really have to load some data. First see whether the name is
115 an alias. Please note that this makes it impossible to have "C"
116 or "POSIX" as aliases. */
117 loc_name = (char *) _nl_expand_alias (*name);
118 if (loc_name == NULL)
119 /* It is no alias. */
120 loc_name = (char *) *name;
121
122 /* Make a writable copy of the locale name. */
123 loc_name = strdupa (loc_name);
124
125 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
126
127 language[_territory[.codeset]][@modifier]
128
129 Beside the first all of them are allowed to be missing. If the
130 full specified locale is not found, the less specific one are
131 looked for. The various part will be stripped off according to
132 the following order:
133 (1) codeset
134 (2) normalized codeset
135 (3) territory
136 (4) modifier
137 */
138 mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
139 &codeset, &normalized_codeset);
140
141 /* If exactly this locale was already asked for we have an entry with
142 the complete name. */
143 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
144 locale_path, locale_path_len, mask,
145 language, territory, codeset,
146 normalized_codeset, modifier,
147 _nl_category_names[category], 0);
148
149 if (locale_file == NULL)
150 {
151 /* Find status record for addressed locale file. We have to search
152 through all directories in the locale path. */
153 locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
154 locale_path, locale_path_len, mask,
155 language, territory, codeset,
156 normalized_codeset, modifier,
157 _nl_category_names[category], 1);
158 if (locale_file == NULL)
159 /* This means we are out of core. */
160 return NULL;
161 }
162
163 /* The space for normalized_codeset is dynamically allocated. Free it. */
164 if (mask & XPG_NORM_CODESET)
165 free ((void *) normalized_codeset);
166
167 if (locale_file->decided == 0)
168 _nl_load_locale (locale_file, category);
169
170 if (locale_file->data == NULL)
171 {
172 int cnt;
173 for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
174 {
175 if (locale_file->successor[cnt]->decided == 0)
176 _nl_load_locale (locale_file->successor[cnt], category);
177 if (locale_file->successor[cnt]->data != NULL)
178 break;
179 }
180 /* Move the entry we found (or NULL) to the first place of
181 successors. */
182 locale_file->successor[0] = locale_file->successor[cnt];
183 locale_file = locale_file->successor[cnt];
184
185 if (locale_file == NULL)
186 return NULL;
187 }
188
189 /* The LC_CTYPE category allows to check whether a locale is really
190 usable. If the locale name contains a charset name and the
191 charset name used in the locale (present in the LC_CTYPE data) is
192 not the same (after resolving aliases etc) we reject the locale
193 since using it would irritate users expecting the charset named
194 in the locale name. */
195 if (codeset != NULL)
196 {
197 /* Get the codeset information from the locale file. */
198 static const int codeset_idx[] =
199 {
200 [__LC_CTYPE] = _NL_ITEM_INDEX (CODESET),
201 [__LC_NUMERIC] = _NL_ITEM_INDEX (_NL_NUMERIC_CODESET),
202 [__LC_TIME] = _NL_ITEM_INDEX (_NL_TIME_CODESET),
203 [__LC_COLLATE] = _NL_ITEM_INDEX (_NL_COLLATE_CODESET),
204 [__LC_MONETARY] = _NL_ITEM_INDEX (_NL_MONETARY_CODESET),
205 [__LC_MESSAGES] = _NL_ITEM_INDEX (_NL_MESSAGES_CODESET),
206 [__LC_PAPER] = _NL_ITEM_INDEX (_NL_PAPER_CODESET),
207 [__LC_NAME] = _NL_ITEM_INDEX (_NL_NAME_CODESET),
208 [__LC_ADDRESS] = _NL_ITEM_INDEX (_NL_ADDRESS_CODESET),
209 [__LC_TELEPHONE] = _NL_ITEM_INDEX (_NL_TELEPHONE_CODESET),
210 [__LC_MEASUREMENT] = _NL_ITEM_INDEX (_NL_MEASUREMENT_CODESET),
211 [__LC_IDENTIFICATION] = _NL_ITEM_INDEX (_NL_IDENTIFICATION_CODESET)
212 };
213 const struct locale_data *data;
214 const char *locale_codeset;
215 char *clocale_codeset;
216 char *ccodeset;
217
218 data = (const struct locale_data *) locale_file->data;
219 locale_codeset =
220 (const char *) data->values[codeset_idx[category]].string;
221 assert (locale_codeset != NULL);
222 /* Note the length of the allocated memory: +3 for up to two slashes
223 and the NUL byte. */
224 clocale_codeset = (char *) alloca (strlen (locale_codeset) + 3);
225 strip (clocale_codeset, locale_codeset);
226
227 ccodeset = (char *) alloca (strlen (codeset) + 3);
228 strip (ccodeset, codeset);
229
230 if (__gconv_compare_alias (upstr (ccodeset, ccodeset),
231 upstr (clocale_codeset,
232 clocale_codeset)) != 0)
233 /* The codesets are not identical, don't use the locale. */
234 return NULL;
235 }
236
237 /* Determine the locale name for which loading succeeded. This
238 information comes from the file name. The form is
239 <path>/<locale>/LC_foo. We must extract the <locale> part. */
240 if (((const struct locale_data *) locale_file->data)->name == NULL)
241 {
242 char *cp, *endp;
243
244 endp = strrchr (locale_file->filename, '/');
245 cp = endp - 1;
246 while (cp[-1] != '/')
247 --cp;
248 ((struct locale_data *) locale_file->data)->name = __strndup (cp,
249 endp - cp);
250 }
251
252 /* Determine whether the user wants transliteration or not. */
253 if (modifier != NULL && __strcasecmp (modifier, "TRANSLIT") == 0)
254 ((struct locale_data *) locale_file->data)->use_translit = 1;
255
256 /* Increment the usage count. */
257 if (((const struct locale_data *) locale_file->data)->usage_count
258 < MAX_USAGE_COUNT)
259 ++((struct locale_data *) locale_file->data)->usage_count;
260
261 return (struct locale_data *) locale_file->data;
262 }
263
264
265 /* Calling this function assumes the lock for handling global locale data
266 is acquired. */
267 void
268 internal_function
269 _nl_remove_locale (int locale, struct locale_data *data)
270 {
271 if (--data->usage_count == 0)
272 {
273 /* First search the entry in the list of loaded files. */
274 struct loaded_l10nfile *ptr = _nl_locale_file_list[locale];
275
276 /* Search for the entry. It must be in the list. Otherwise it
277 is a bug and we crash badly. */
278 while ((struct locale_data *) ptr->data != data)
279 ptr = ptr->next;
280
281 /* Mark the data as not available anymore. So when the data has
282 to be used again it is reloaded. */
283 ptr->decided = 0;
284 ptr->data = NULL;
285
286 /* Free the name. */
287 free ((char *) data->name);
288
289 #ifdef _POSIX_MAPPED_FILES
290 /* Really delete the data. First delete the real data. */
291 if (__builtin_expect (data->alloc == ld_mapped, 1))
292 {
293 /* Try to unmap the area. If this fails we mark the area as
294 permanent. */
295 if (__munmap ((caddr_t) data->filedata, data->filesize) != 0)
296 {
297 data->usage_count = UNDELETABLE;
298 return;
299 }
300 }
301 else
302 #endif /* _POSIX_MAPPED_FILES */
303 /* The memory was malloced. */
304 free ((void *) data->filedata);
305
306 /* Now free the structure itself. */
307 free (data);
308 }
309 }