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