]>
Commit | Line | Data |
---|---|---|
dd9423a6 | 1 | /* Copyright (C) 1991, 92, 1995-2000, 2002, 2003 Free Software Foundation, Inc. |
c84142e8 | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
c84142e8 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
28f540f4 | 8 | |
c84142e8 UD |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
28f540f4 | 13 | |
41bdb6e2 AJ |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, write to the Free | |
16 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
17 | 02111-1307 USA. */ | |
28f540f4 | 18 | |
7a12c6bb RM |
19 | #include <alloca.h> |
20 | #include <argz.h> | |
28f540f4 | 21 | #include <errno.h> |
5107cf1d | 22 | #include <bits/libc-lock.h> |
933e73fa | 23 | #include <locale.h> |
7a12c6bb RM |
24 | #include <stdlib.h> |
25 | #include <string.h> | |
26 | #include <unistd.h> | |
27 | ||
933e73fa RM |
28 | #include "localeinfo.h" |
29 | ||
1a0d874e RM |
30 | #ifdef NL_CURRENT_INDIRECT |
31 | ||
32 | /* For each category declare a special external symbol | |
33 | _nl_current_CATEGORY_used with a weak reference. | |
34 | This symbol will is defined in lc-CATEGORY.c and will be linked in | |
35 | if anything uses _nl_current_CATEGORY (also defined in that module). | |
36 | Also use a weak reference for the _nl_current_CATEGORY thread variable. */ | |
37 | ||
38 | # define DEFINE_CATEGORY(category, category_name, items, a) \ | |
39 | extern char _nl_current_##category##_used; \ | |
40 | weak_extern (_nl_current_##category##_used) \ | |
41 | weak_extern (_nl_current_##category) | |
42 | # include "categories.def" | |
43 | # undef DEFINE_CATEGORY | |
44 | ||
45 | /* Now define a table of flags based on those special weak symbols' values. | |
46 | _nl_current_used[CATEGORY] will be zero if _nl_current_CATEGORY is not | |
47 | linked in. */ | |
48 | static char *const _nl_current_used[] = | |
036cc82f | 49 | { |
1a0d874e RM |
50 | # define DEFINE_CATEGORY(category, category_name, items, a) \ |
51 | [category] = &_nl_current_##category##_used, | |
52 | # include "categories.def" | |
53 | # undef DEFINE_CATEGORY | |
036cc82f | 54 | }; |
933e73fa | 55 | |
1a0d874e | 56 | # define CATEGORY_USED(category) (_nl_current_used[category] != 0) |
30c14c31 RM |
57 | |
58 | #else | |
59 | ||
60 | /* The shared library always loads all the categories, | |
61 | and the current global settings are kept in _nl_global_locale. */ | |
62 | ||
30c14c31 RM |
63 | # define CATEGORY_USED(category) (1) |
64 | ||
65 | #endif | |
66 | ||
933e73fa RM |
67 | |
68 | /* Define an array of category names (also the environment variable names), | |
4b10dd6c UD |
69 | indexed by integral category. */ |
70 | const char *const _nl_category_names[] = | |
933e73fa | 71 | { |
4b10dd6c | 72 | #define DEFINE_CATEGORY(category, category_name, items, a) \ |
933e73fa RM |
73 | [category] = category_name, |
74 | #include "categories.def" | |
75 | #undef DEFINE_CATEGORY | |
7a12c6bb | 76 | [LC_ALL] = "LC_ALL" |
933e73fa RM |
77 | }; |
78 | /* An array of their lengths, for convenience. */ | |
79 | const size_t _nl_category_name_sizes[] = | |
80 | { | |
4b10dd6c | 81 | #define DEFINE_CATEGORY(category, category_name, items, a) \ |
933e73fa RM |
82 | [category] = sizeof (category_name) - 1, |
83 | #include "categories.def" | |
84 | #undef DEFINE_CATEGORY | |
7a12c6bb | 85 | [LC_ALL] = sizeof ("LC_ALL") - 1 |
933e73fa | 86 | }; |
28f540f4 RM |
87 | |
88 | ||
0ba454fc RM |
89 | #ifdef NL_CURRENT_INDIRECT |
90 | # define WEAK_POSTLOAD(postload) weak_extern (postload) | |
91 | #else | |
92 | # define WEAK_POSTLOAD(postload) /* Need strong refs in static linking. */ | |
93 | #endif | |
94 | ||
933e73fa RM |
95 | /* Declare the postload functions used below. */ |
96 | #undef NO_POSTLOAD | |
97 | #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */ | |
4b10dd6c | 98 | #define DEFINE_CATEGORY(category, category_name, items, postload) \ |
0ba454fc | 99 | extern void postload (void); WEAK_POSTLOAD (postload) |
933e73fa RM |
100 | #include "categories.def" |
101 | #undef DEFINE_CATEGORY | |
102 | #undef NO_POSTLOAD | |
103 | ||
104 | /* Define an array indexed by category of postload functions to call after | |
105 | loading and installing that category's data. */ | |
a5113b14 | 106 | static void (*const _nl_category_postload[]) (void) = |
933e73fa | 107 | { |
4b10dd6c | 108 | #define DEFINE_CATEGORY(category, category_name, items, postload) \ |
933e73fa RM |
109 | [category] = postload, |
110 | #include "categories.def" | |
111 | #undef DEFINE_CATEGORY | |
112 | }; | |
113 | ||
114 | ||
a5113b14 | 115 | /* Lock for protecting global data. */ |
ab26a24a | 116 | __libc_lock_define_initialized (, __libc_setlocale_lock attribute_hidden) |
a5113b14 | 117 | |
111bb972 UD |
118 | /* Defined in loadmsgcat.c. */ |
119 | extern int _nl_msg_cat_cntr; | |
120 | ||
933e73fa | 121 | |
7a12c6bb RM |
122 | /* Use this when we come along an error. */ |
123 | #define ERROR_RETURN \ | |
124 | do { \ | |
c4029823 | 125 | __set_errno (EINVAL); \ |
7a12c6bb RM |
126 | return NULL; \ |
127 | } while (0) | |
933e73fa | 128 | |
7a12c6bb | 129 | |
7a12c6bb | 130 | /* Construct a new composite name. */ |
dd9423a6 | 131 | static char * |
4b10dd6c | 132 | new_composite_name (int category, const char *newnames[__LC_LAST]) |
7a12c6bb | 133 | { |
e918a7fe | 134 | size_t last_len = 0; |
7a12c6bb RM |
135 | size_t cumlen = 0; |
136 | int i; | |
137 | char *new, *p; | |
138 | int same = 1; | |
139 | ||
4b10dd6c UD |
140 | for (i = 0; i < __LC_LAST; ++i) |
141 | if (i != LC_ALL) | |
142 | { | |
143 | const char *name = (category == LC_ALL ? newnames[i] : | |
144 | category == i ? newnames[0] : | |
1ce8aaae | 145 | _nl_global_locale.__names[i]); |
4b10dd6c UD |
146 | last_len = strlen (name); |
147 | cumlen += _nl_category_name_sizes[i] + 1 + last_len + 1; | |
148 | if (i > 0 && same && strcmp (name, newnames[0]) != 0) | |
149 | same = 0; | |
150 | } | |
7a12c6bb RM |
151 | |
152 | if (same) | |
933e73fa | 153 | { |
7a12c6bb | 154 | /* All the categories use the same name. */ |
72c74375 UD |
155 | if (strcmp (newnames[0], _nl_C_name) == 0 |
156 | || strcmp (newnames[0], _nl_POSIX_name) == 0) | |
7a12c6bb RM |
157 | return (char *) _nl_C_name; |
158 | ||
159 | new = malloc (last_len + 1); | |
7a12c6bb | 160 | |
036cc82f | 161 | return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1); |
933e73fa | 162 | } |
7a12c6bb RM |
163 | |
164 | new = malloc (cumlen); | |
165 | if (new == NULL) | |
166 | return NULL; | |
167 | p = new; | |
4b10dd6c UD |
168 | for (i = 0; i < __LC_LAST; ++i) |
169 | if (i != LC_ALL) | |
170 | { | |
171 | /* Add "CATEGORY=NAME;" to the string. */ | |
172 | const char *name = (category == LC_ALL ? newnames[i] : | |
173 | category == i ? newnames[0] : | |
1ce8aaae | 174 | _nl_global_locale.__names[i]); |
4b10dd6c UD |
175 | p = __stpcpy (p, _nl_category_names[i]); |
176 | *p++ = '='; | |
177 | p = __stpcpy (p, name); | |
178 | *p++ = ';'; | |
179 | } | |
7a12c6bb RM |
180 | p[-1] = '\0'; /* Clobber the last ';'. */ |
181 | return new; | |
182 | } | |
933e73fa | 183 | |
933e73fa | 184 | |
1ce8aaae | 185 | /* Put NAME in _nl_global_locale.__names. */ |
7a12c6bb RM |
186 | static inline void |
187 | setname (int category, const char *name) | |
188 | { | |
1ce8aaae | 189 | if (_nl_global_locale.__names[category] == name) |
762a2918 UD |
190 | return; |
191 | ||
1ce8aaae RM |
192 | if (_nl_global_locale.__names[category] != _nl_C_name) |
193 | free ((char *) _nl_global_locale.__names[category]); | |
7a12c6bb | 194 | |
1ce8aaae | 195 | _nl_global_locale.__names[category] = name; |
7a12c6bb RM |
196 | } |
197 | ||
7a12c6bb RM |
198 | /* Put DATA in *_nl_current[CATEGORY]. */ |
199 | static inline void | |
c84142e8 | 200 | setdata (int category, struct locale_data *data) |
7a12c6bb | 201 | { |
30c14c31 | 202 | if (CATEGORY_USED (category)) |
933e73fa | 203 | { |
30c14c31 | 204 | _nl_global_locale.__locales[category] = data; |
7a12c6bb RM |
205 | if (_nl_category_postload[category]) |
206 | (*_nl_category_postload[category]) (); | |
933e73fa | 207 | } |
7a12c6bb | 208 | } |
933e73fa | 209 | |
7a12c6bb RM |
210 | char * |
211 | setlocale (int category, const char *locale) | |
212 | { | |
7a12c6bb RM |
213 | char *locale_path; |
214 | size_t locale_path_len; | |
d68171ed | 215 | const char *locpath_var; |
7a12c6bb | 216 | char *composite; |
933e73fa | 217 | |
7a12c6bb | 218 | /* Sanity check for CATEGORY argument. */ |
323fb88d UD |
219 | if (__builtin_expect (category, 0) < 0 |
220 | || __builtin_expect (category, 0) >= __LC_LAST) | |
7a12c6bb RM |
221 | ERROR_RETURN; |
222 | ||
223 | /* Does user want name of current locale? */ | |
224 | if (locale == NULL) | |
1ce8aaae | 225 | return (char *) _nl_global_locale.__names[category]; |
7a12c6bb | 226 | |
1ce8aaae | 227 | if (strcmp (locale, _nl_global_locale.__names[category]) == 0) |
933e73fa | 228 | /* Changing to the same thing. */ |
1ce8aaae | 229 | return (char *) _nl_global_locale.__names[category]; |
7a12c6bb RM |
230 | |
231 | /* We perhaps really have to load some data. So we determine the | |
a5113b14 UD |
232 | path in which to look for the data now. The environment variable |
233 | `LOCPATH' must only be used when the binary has no SUID or SGID | |
cb09a2cd RM |
234 | bit set. If using the default path, we tell _nl_find_locale |
235 | by passing null and it can check the canonical locale archive. */ | |
7a12c6bb RM |
236 | locale_path = NULL; |
237 | locale_path_len = 0; | |
238 | ||
74955460 | 239 | locpath_var = getenv ("LOCPATH"); |
d68171ed | 240 | if (locpath_var != NULL && locpath_var[0] != '\0') |
cb09a2cd RM |
241 | { |
242 | if (__argz_create_sep (locpath_var, ':', | |
243 | &locale_path, &locale_path_len) != 0) | |
244 | return NULL; | |
933e73fa | 245 | |
cb09a2cd RM |
246 | if (__argz_add_sep (&locale_path, &locale_path_len, |
247 | _nl_default_locale_path, ':') != 0) | |
248 | return NULL; | |
249 | } | |
db2286f6 | 250 | |
933e73fa RM |
251 | if (category == LC_ALL) |
252 | { | |
7a12c6bb RM |
253 | /* The user wants to set all categories. The desired locales |
254 | for the individual categories can be selected by using a | |
255 | composite locale name. This is a semi-colon separated list | |
256 | of entries of the form `CATEGORY=VALUE'. */ | |
4b10dd6c UD |
257 | const char *newnames[__LC_LAST]; |
258 | struct locale_data *newdata[__LC_LAST]; | |
933e73fa RM |
259 | |
260 | /* Set all name pointers to the argument name. */ | |
4b10dd6c UD |
261 | for (category = 0; category < __LC_LAST; ++category) |
262 | if (category != LC_ALL) | |
263 | newnames[category] = (char *) locale; | |
db2286f6 | 264 | |
323fb88d | 265 | if (__builtin_expect (strchr (locale, ';') != NULL, 0)) |
933e73fa | 266 | { |
7a12c6bb RM |
267 | /* This is a composite name. Make a copy and split it up. */ |
268 | char *np = strdupa (locale); | |
269 | char *cp; | |
270 | int cnt; | |
933e73fa | 271 | |
7a12c6bb | 272 | while ((cp = strchr (np, '=')) != NULL) |
933e73fa | 273 | { |
4b10dd6c UD |
274 | for (cnt = 0; cnt < __LC_LAST; ++cnt) |
275 | if (cnt != LC_ALL | |
276 | && (size_t) (cp - np) == _nl_category_name_sizes[cnt] | |
7a12c6bb | 277 | && memcmp (np, _nl_category_names[cnt], cp - np) == 0) |
933e73fa | 278 | break; |
7a12c6bb | 279 | |
4b10dd6c | 280 | if (cnt == __LC_LAST) |
7a12c6bb RM |
281 | /* Bogus category name. */ |
282 | ERROR_RETURN; | |
283 | ||
284 | /* Found the category this clause sets. */ | |
285 | newnames[cnt] = ++cp; | |
286 | cp = strchr (cp, ';'); | |
287 | if (cp != NULL) | |
933e73fa | 288 | { |
7a12c6bb RM |
289 | /* Examine the next clause. */ |
290 | *cp = '\0'; | |
291 | np = cp + 1; | |
933e73fa | 292 | } |
7a12c6bb RM |
293 | else |
294 | /* This was the last clause. We are done. */ | |
295 | break; | |
933e73fa RM |
296 | } |
297 | ||
4b10dd6c UD |
298 | for (cnt = 0; cnt < __LC_LAST; ++cnt) |
299 | if (cnt != LC_ALL && newnames[cnt] == locale) | |
933e73fa | 300 | /* The composite name did not specify all categories. */ |
7a12c6bb | 301 | ERROR_RETURN; |
933e73fa | 302 | } |
19bc17a9 | 303 | |
a5113b14 | 304 | /* Protect global data. */ |
c4029823 | 305 | __libc_lock_lock (__libc_setlocale_lock); |
a5113b14 | 306 | |
933e73fa RM |
307 | /* Load the new data for each category. */ |
308 | while (category-- > 0) | |
ef5d6645 UD |
309 | if (category != LC_ALL) |
310 | { | |
ef5d6645 UD |
311 | newdata[category] = _nl_find_locale (locale_path, locale_path_len, |
312 | category, | |
313 | &newnames[category]); | |
1fb05e3d | 314 | |
ef5d6645 | 315 | if (newdata[category] == NULL) |
9a411bf5 RM |
316 | { |
317 | #ifdef NL_CURRENT_INDIRECT | |
318 | if (newnames[category] == _nl_C_name) | |
319 | /* Null because it's the weak value of _nl_C_LC_FOO. */ | |
320 | continue; | |
321 | #endif | |
322 | break; | |
323 | } | |
1fb05e3d | 324 | |
ef5d6645 UD |
325 | /* We must not simply free a global locale since we have no |
326 | control over the usage. So we mark it as un-deletable. */ | |
327 | if (newdata[category]->usage_count != UNDELETABLE) | |
328 | newdata[category]->usage_count = UNDELETABLE; | |
411adb10 UD |
329 | |
330 | /* Make a copy of locale name. */ | |
331 | if (newnames[category] != _nl_C_name) | |
332 | { | |
1ce8aaae | 333 | newnames[category] = __strdup (newnames[category]); |
411adb10 UD |
334 | if (newnames[category] == NULL) |
335 | break; | |
336 | } | |
ef5d6645 | 337 | } |
933e73fa | 338 | |
7a12c6bb | 339 | /* Create new composite name. */ |
d4c5cf80 UD |
340 | composite = (category >= 0 |
341 | ? NULL : new_composite_name (LC_ALL, newnames)); | |
342 | if (composite != NULL) | |
933e73fa | 343 | { |
a5113b14 | 344 | /* Now we have loaded all the new data. Put it in place. */ |
4b10dd6c UD |
345 | for (category = 0; category < __LC_LAST; ++category) |
346 | if (category != LC_ALL) | |
347 | { | |
348 | setdata (category, newdata[category]); | |
349 | setname (category, newnames[category]); | |
350 | } | |
a5113b14 | 351 | setname (LC_ALL, composite); |
111bb972 UD |
352 | |
353 | /* We successfully loaded a new locale. Let the message catalog | |
354 | functions know about this. */ | |
355 | ++_nl_msg_cat_cntr; | |
933e73fa | 356 | } |
411adb10 UD |
357 | else |
358 | for (++category; category < __LC_LAST; ++category) | |
359 | if (category != LC_ALL && newnames[category] != _nl_C_name) | |
360 | free ((char *) newnames[category]); | |
a5113b14 UD |
361 | |
362 | /* Critical section left. */ | |
c4029823 | 363 | __libc_lock_unlock (__libc_setlocale_lock); |
933e73fa | 364 | |
714a562f UD |
365 | /* Free the resources (the locale path variable. */ |
366 | free (locale_path); | |
367 | ||
933e73fa RM |
368 | return composite; |
369 | } | |
370 | else | |
371 | { | |
c84142e8 | 372 | struct locale_data *newdata = NULL; |
650425ce | 373 | const char *newname[1] = { locale }; |
7a12c6bb | 374 | |
a5113b14 | 375 | /* Protect global data. */ |
c4029823 | 376 | __libc_lock_lock (__libc_setlocale_lock); |
a5113b14 | 377 | |
30c14c31 | 378 | if (CATEGORY_USED (category)) |
933e73fa | 379 | { |
7a12c6bb | 380 | /* Only actually load the data if anything will use it. */ |
7a12c6bb | 381 | newdata = _nl_find_locale (locale_path, locale_path_len, category, |
650425ce | 382 | &newname[0]); |
7a12c6bb | 383 | if (newdata == NULL) |
a5113b14 | 384 | goto abort_single; |
c84142e8 UD |
385 | |
386 | /* We must not simply free a global locale since we have no | |
a2b08ee5 UD |
387 | control over the usage. So we mark it as un-deletable. |
388 | ||
3081378b | 389 | Note: do not remove the `if', it's necessary to copy with |
a2b08ee5 | 390 | the builtin locale data. */ |
9756dfe1 UD |
391 | if (newdata->usage_count != UNDELETABLE) |
392 | newdata->usage_count = UNDELETABLE; | |
933e73fa RM |
393 | } |
394 | ||
411adb10 UD |
395 | /* Make a copy of locale name. */ |
396 | if (newname[0] != _nl_C_name) | |
397 | { | |
1ce8aaae | 398 | newname[0] = __strdup (newname[0]); |
411adb10 UD |
399 | if (newname[0] == NULL) |
400 | goto abort_single; | |
401 | } | |
402 | ||
7a12c6bb | 403 | /* Create new composite name. */ |
650425ce | 404 | composite = new_composite_name (category, newname); |
7a12c6bb | 405 | if (composite == NULL) |
933e73fa | 406 | { |
411adb10 UD |
407 | if (newname[0] != _nl_C_name) |
408 | free ((char *) newname[0]); | |
409 | ||
845dcb57 | 410 | /* Say that we don't have any data loaded. */ |
a5113b14 | 411 | abort_single: |
650425ce | 412 | newname[0] = NULL; |
933e73fa | 413 | } |
a5113b14 UD |
414 | else |
415 | { | |
30c14c31 | 416 | if (CATEGORY_USED (category)) |
a5113b14 | 417 | setdata (category, newdata); |
28f540f4 | 418 | |
650425ce | 419 | setname (category, newname[0]); |
a5113b14 | 420 | setname (LC_ALL, composite); |
111bb972 UD |
421 | |
422 | /* We successfully loaded a new locale. Let the message catalog | |
423 | functions know about this. */ | |
424 | ++_nl_msg_cat_cntr; | |
a5113b14 | 425 | } |
28f540f4 | 426 | |
a5113b14 | 427 | /* Critical section left. */ |
c4029823 | 428 | __libc_lock_unlock (__libc_setlocale_lock); |
28f540f4 | 429 | |
714a562f UD |
430 | /* Free the resources (the locale path variable. */ |
431 | free (locale_path); | |
432 | ||
650425ce | 433 | return (char *) newname[0]; |
933e73fa | 434 | } |
28f540f4 | 435 | } |
30c14c31 | 436 | libc_hidden_def (setlocale) |
f84ad0b1 | 437 | |
1a0d874e RM |
438 | static void |
439 | free_category (int category, | |
440 | struct locale_data *here, struct locale_data *c_data) | |
441 | { | |
442 | struct loaded_l10nfile *runp = _nl_locale_file_list[category]; | |
443 | ||
444 | /* If this category is already "C" don't do anything. */ | |
445 | if (here != c_data) | |
446 | { | |
447 | /* We have to be prepared that sometime later we still | |
448 | might need the locale information. */ | |
449 | setdata (category, c_data); | |
450 | setname (category, _nl_C_name); | |
451 | } | |
452 | ||
453 | while (runp != NULL) | |
454 | { | |
455 | struct loaded_l10nfile *curr = runp; | |
456 | struct locale_data *data = (struct locale_data *) runp->data; | |
457 | ||
458 | if (data != NULL && data != c_data) | |
459 | _nl_unload_locale (data); | |
460 | runp = runp->next; | |
461 | free ((char *) curr->filename); | |
462 | free (curr); | |
463 | } | |
464 | } | |
465 | ||
c877418f | 466 | libc_freeres_fn (free_mem) |
f84ad0b1 | 467 | { |
1a0d874e RM |
468 | #ifdef NL_CURRENT_INDIRECT |
469 | /* We don't use the loop because we want to have individual weak | |
470 | symbol references here. */ | |
471 | # define DEFINE_CATEGORY(category, category_name, items, a) \ | |
472 | if (CATEGORY_USED (category)) \ | |
473 | { \ | |
474 | extern struct locale_data _nl_C_##category; \ | |
475 | weak_extern (_nl_C_##category) \ | |
476 | free_category (category, *_nl_current_##category, &_nl_C_##category); \ | |
477 | } | |
478 | # include "categories.def" | |
479 | # undef DEFINE_CATEGORY | |
480 | #else | |
f84ad0b1 UD |
481 | int category; |
482 | ||
4b10dd6c UD |
483 | for (category = 0; category < __LC_LAST; ++category) |
484 | if (category != LC_ALL) | |
1a0d874e RM |
485 | free_category (category, _NL_CURRENT_DATA (category), |
486 | _nl_C_locobj.__locales[category]); | |
487 | #endif | |
f84ad0b1 UD |
488 | |
489 | setname (LC_ALL, _nl_C_name); | |
a89a3dab RM |
490 | |
491 | /* This frees the data structures associated with the locale archive. | |
492 | The locales from the archive are not in the file list, so we have | |
493 | not called _nl_unload_locale on them above. */ | |
494 | _nl_archive_subfreeres (); | |
f84ad0b1 | 495 | } |