1 /* Copyright (C) 1991, 92, 95-99, 2000, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
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.
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
12 Lesser General Public License for more details.
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
22 #include <bits/libc-lock.h>
28 #include "localeinfo.h"
30 #ifdef NL_CURRENT_INDIRECT
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. */
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
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
48 static char *const _nl_current_used
[] =
50 # define DEFINE_CATEGORY(category, category_name, items, a) \
51 [category] = &_nl_current_##category##_used,
52 # include "categories.def"
53 # undef DEFINE_CATEGORY
56 # define CATEGORY_USED(category) (_nl_current_used[category] != 0)
60 /* The shared library always loads all the categories,
61 and the current global settings are kept in _nl_global_locale. */
63 # define CATEGORY_USED(category) (1)
68 /* Define an array of category names (also the environment variable names),
69 indexed by integral category. */
70 const char *const _nl_category_names
[] =
72 #define DEFINE_CATEGORY(category, category_name, items, a) \
73 [category] = category_name,
74 #include "categories.def"
75 #undef DEFINE_CATEGORY
78 /* An array of their lengths, for convenience. */
79 const size_t _nl_category_name_sizes
[] =
81 #define DEFINE_CATEGORY(category, category_name, items, a) \
82 [category] = sizeof (category_name) - 1,
83 #include "categories.def"
84 #undef DEFINE_CATEGORY
85 [LC_ALL
] = sizeof ("LC_ALL") - 1
89 #ifdef NL_CURRENT_INDIRECT
90 # define WEAK_POSTLOAD(postload) weak_extern (postload)
92 # define WEAK_POSTLOAD(postload) /* Need strong refs in static linking. */
95 /* Declare the postload functions used below. */
97 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
98 #define DEFINE_CATEGORY(category, category_name, items, postload) \
99 extern void postload (void); WEAK_POSTLOAD (postload)
100 #include "categories.def"
101 #undef DEFINE_CATEGORY
104 /* Define an array indexed by category of postload functions to call after
105 loading and installing that category's data. */
106 static void (*const _nl_category_postload
[]) (void) =
108 #define DEFINE_CATEGORY(category, category_name, items, postload) \
109 [category] = postload,
110 #include "categories.def"
111 #undef DEFINE_CATEGORY
115 /* Lock for protecting global data. */
116 __libc_lock_define_initialized (, __libc_setlocale_lock attribute_hidden
)
118 /* Defined in loadmsgcat.c. */
119 extern int _nl_msg_cat_cntr
;
122 /* Use this when we come along an error. */
123 #define ERROR_RETURN \
125 __set_errno (EINVAL); \
130 /* Construct a new composite name. */
132 new_composite_name (int category
, const char *newnames
[__LC_LAST
])
140 for (i
= 0; i
< __LC_LAST
; ++i
)
143 const char *name
= (category
== LC_ALL
? newnames
[i
] :
144 category
== i
? newnames
[0] :
145 _nl_global_locale
.__names
[i
]);
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)
154 /* All the categories use the same name. */
155 if (strcmp (newnames
[0], _nl_C_name
) == 0
156 || strcmp (newnames
[0], _nl_POSIX_name
) == 0)
157 return (char *) _nl_C_name
;
159 new = malloc (last_len
+ 1);
161 return new == NULL
? NULL
: memcpy (new, newnames
[0], last_len
+ 1);
164 new = malloc (cumlen
);
168 for (i
= 0; i
< __LC_LAST
; ++i
)
171 /* Add "CATEGORY=NAME;" to the string. */
172 const char *name
= (category
== LC_ALL
? newnames
[i
] :
173 category
== i
? newnames
[0] :
174 _nl_global_locale
.__names
[i
]);
175 p
= __stpcpy (p
, _nl_category_names
[i
]);
177 p
= __stpcpy (p
, name
);
180 p
[-1] = '\0'; /* Clobber the last ';'. */
185 /* Put NAME in _nl_global_locale.__names. */
187 setname (int category
, const char *name
)
189 if (_nl_global_locale
.__names
[category
] == name
)
192 if (_nl_global_locale
.__names
[category
] != _nl_C_name
)
193 free ((char *) _nl_global_locale
.__names
[category
]);
195 _nl_global_locale
.__names
[category
] = name
;
198 /* Put DATA in *_nl_current[CATEGORY]. */
200 setdata (int category
, struct locale_data
*data
)
202 if (CATEGORY_USED (category
))
204 _nl_global_locale
.__locales
[category
] = data
;
205 if (_nl_category_postload
[category
])
206 (*_nl_category_postload
[category
]) ();
211 setlocale (int category
, const char *locale
)
214 size_t locale_path_len
;
215 const char *locpath_var
;
218 /* Sanity check for CATEGORY argument. */
219 if (__builtin_expect (category
, 0) < 0
220 || __builtin_expect (category
, 0) >= __LC_LAST
)
223 /* Does user want name of current locale? */
225 return (char *) _nl_global_locale
.__names
[category
];
227 if (strcmp (locale
, _nl_global_locale
.__names
[category
]) == 0)
228 /* Changing to the same thing. */
229 return (char *) _nl_global_locale
.__names
[category
];
231 /* We perhaps really have to load some data. So we determine the
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
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. */
239 locpath_var
= getenv ("LOCPATH");
240 if (locpath_var
!= NULL
&& locpath_var
[0] != '\0')
242 if (__argz_create_sep (locpath_var
, ':',
243 &locale_path
, &locale_path_len
) != 0)
246 if (__argz_add_sep (&locale_path
, &locale_path_len
,
247 _nl_default_locale_path
, ':') != 0)
251 if (category
== LC_ALL
)
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'. */
257 const char *newnames
[__LC_LAST
];
258 struct locale_data
*newdata
[__LC_LAST
];
260 /* Set all name pointers to the argument name. */
261 for (category
= 0; category
< __LC_LAST
; ++category
)
262 if (category
!= LC_ALL
)
263 newnames
[category
] = (char *) locale
;
265 if (__builtin_expect (strchr (locale
, ';') != NULL
, 0))
267 /* This is a composite name. Make a copy and split it up. */
268 char *np
= strdupa (locale
);
272 while ((cp
= strchr (np
, '=')) != NULL
)
274 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
276 && (size_t) (cp
- np
) == _nl_category_name_sizes
[cnt
]
277 && memcmp (np
, _nl_category_names
[cnt
], cp
- np
) == 0)
280 if (cnt
== __LC_LAST
)
281 /* Bogus category name. */
284 /* Found the category this clause sets. */
285 newnames
[cnt
] = ++cp
;
286 cp
= strchr (cp
, ';');
289 /* Examine the next clause. */
294 /* This was the last clause. We are done. */
298 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
299 if (cnt
!= LC_ALL
&& newnames
[cnt
] == locale
)
300 /* The composite name did not specify all categories. */
304 /* Protect global data. */
305 __libc_lock_lock (__libc_setlocale_lock
);
307 /* Load the new data for each category. */
308 while (category
-- > 0)
309 if (category
!= LC_ALL
)
311 newdata
[category
] = _nl_find_locale (locale_path
, locale_path_len
,
313 &newnames
[category
]);
315 if (newdata
[category
] == NULL
)
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. */
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
;
330 /* Make a copy of locale name. */
331 if (newnames
[category
] != _nl_C_name
)
333 newnames
[category
] = __strdup (newnames
[category
]);
334 if (newnames
[category
] == NULL
)
339 /* Create new composite name. */
340 composite
= (category
>= 0
341 ? NULL
: new_composite_name (LC_ALL
, newnames
));
342 if (composite
!= NULL
)
344 /* Now we have loaded all the new data. Put it in place. */
345 for (category
= 0; category
< __LC_LAST
; ++category
)
346 if (category
!= LC_ALL
)
348 setdata (category
, newdata
[category
]);
349 setname (category
, newnames
[category
]);
351 setname (LC_ALL
, composite
);
353 /* We successfully loaded a new locale. Let the message catalog
354 functions know about this. */
358 for (++category
; category
< __LC_LAST
; ++category
)
359 if (category
!= LC_ALL
&& newnames
[category
] != _nl_C_name
)
360 free ((char *) newnames
[category
]);
362 /* Critical section left. */
363 __libc_lock_unlock (__libc_setlocale_lock
);
365 /* Free the resources (the locale path variable. */
372 struct locale_data
*newdata
= NULL
;
373 const char *newname
[1] = { locale
};
375 /* Protect global data. */
376 __libc_lock_lock (__libc_setlocale_lock
);
378 if (CATEGORY_USED (category
))
380 /* Only actually load the data if anything will use it. */
381 newdata
= _nl_find_locale (locale_path
, locale_path_len
, category
,
386 /* We must not simply free a global locale since we have no
387 control over the usage. So we mark it as un-deletable.
389 Note: do not remove the `if', it's necessary to copy with
390 the builtin locale data. */
391 if (newdata
->usage_count
!= UNDELETABLE
)
392 newdata
->usage_count
= UNDELETABLE
;
395 /* Make a copy of locale name. */
396 if (newname
[0] != _nl_C_name
)
398 newname
[0] = __strdup (newname
[0]);
399 if (newname
[0] == NULL
)
403 /* Create new composite name. */
404 composite
= new_composite_name (category
, newname
);
405 if (composite
== NULL
)
407 if (newname
[0] != _nl_C_name
)
408 free ((char *) newname
[0]);
410 /* Say that we don't have any data loaded. */
416 if (CATEGORY_USED (category
))
417 setdata (category
, newdata
);
419 setname (category
, newname
[0]);
420 setname (LC_ALL
, composite
);
422 /* We successfully loaded a new locale. Let the message catalog
423 functions know about this. */
427 /* Critical section left. */
428 __libc_lock_unlock (__libc_setlocale_lock
);
430 /* Free the resources (the locale path variable. */
433 return (char *) newname
[0];
436 libc_hidden_def (setlocale
)
439 free_category (int category
,
440 struct locale_data
*here
, struct locale_data
*c_data
)
442 struct loaded_l10nfile
*runp
= _nl_locale_file_list
[category
];
444 /* If this category is already "C" don't do anything. */
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
);
455 struct loaded_l10nfile
*curr
= runp
;
456 struct locale_data
*data
= (struct locale_data
*) runp
->data
;
458 if (data
!= NULL
&& data
!= c_data
)
459 _nl_unload_locale (data
);
461 free ((char *) curr
->filename
);
466 libc_freeres_fn (free_mem
)
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)) \
474 extern struct locale_data _nl_C_##category; \
475 weak_extern (_nl_C_##category) \
476 free_category (category, *_nl_current_##category, &_nl_C_##category); \
478 # include "categories.def"
479 # undef DEFINE_CATEGORY
483 for (category
= 0; category
< __LC_LAST
; ++category
)
484 if (category
!= LC_ALL
)
485 free_category (category
, _NL_CURRENT_DATA (category
),
486 _nl_C_locobj
.__locales
[category
]);
489 setname (LC_ALL
, _nl_C_name
);
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 ();