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 /* Declare the postload functions used below. */
91 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
92 #define DEFINE_CATEGORY(category, category_name, items, postload) \
93 extern void postload (void); weak_extern (postload)
94 #include "categories.def"
95 #undef DEFINE_CATEGORY
98 /* Define an array indexed by category of postload functions to call after
99 loading and installing that category's data. */
100 static void (*const _nl_category_postload
[]) (void) =
102 #define DEFINE_CATEGORY(category, category_name, items, postload) \
103 [category] = postload,
104 #include "categories.def"
105 #undef DEFINE_CATEGORY
109 /* Lock for protecting global data. */
110 __libc_lock_define_initialized (, __libc_setlocale_lock attribute_hidden
)
112 /* Defined in loadmsgcat.c. */
113 extern int _nl_msg_cat_cntr
;
116 /* Use this when we come along an error. */
117 #define ERROR_RETURN \
119 __set_errno (EINVAL); \
124 /* Construct a new composite name. */
126 new_composite_name (int category
, const char *newnames
[__LC_LAST
])
134 for (i
= 0; i
< __LC_LAST
; ++i
)
137 const char *name
= (category
== LC_ALL
? newnames
[i
] :
138 category
== i
? newnames
[0] :
139 _nl_current_names
[i
]);
140 last_len
= strlen (name
);
141 cumlen
+= _nl_category_name_sizes
[i
] + 1 + last_len
+ 1;
142 if (i
> 0 && same
&& strcmp (name
, newnames
[0]) != 0)
148 /* All the categories use the same name. */
149 if (strcmp (newnames
[0], _nl_C_name
) == 0
150 || strcmp (newnames
[0], _nl_POSIX_name
) == 0)
151 return (char *) _nl_C_name
;
153 new = malloc (last_len
+ 1);
155 return new == NULL
? NULL
: memcpy (new, newnames
[0], last_len
+ 1);
158 new = malloc (cumlen
);
162 for (i
= 0; i
< __LC_LAST
; ++i
)
165 /* Add "CATEGORY=NAME;" to the string. */
166 const char *name
= (category
== LC_ALL
? newnames
[i
] :
167 category
== i
? newnames
[0] :
168 _nl_current_names
[i
]);
169 p
= __stpcpy (p
, _nl_category_names
[i
]);
171 p
= __stpcpy (p
, name
);
174 p
[-1] = '\0'; /* Clobber the last ';'. */
179 /* Put NAME in _nl_current_names. */
181 setname (int category
, const char *name
)
183 if (_nl_current_names
[category
] == name
)
186 if (_nl_current_names
[category
] != _nl_C_name
)
187 free ((char *) _nl_current_names
[category
]);
189 _nl_current_names
[category
] = name
;
192 /* Put DATA in *_nl_current[CATEGORY]. */
194 setdata (int category
, struct locale_data
*data
)
196 if (CATEGORY_USED (category
))
198 _nl_global_locale
.__locales
[category
] = data
;
199 if (_nl_category_postload
[category
])
200 (*_nl_category_postload
[category
]) ();
205 setlocale (int category
, const char *locale
)
208 size_t locale_path_len
;
209 const char *locpath_var
;
212 /* Sanity check for CATEGORY argument. */
213 if (__builtin_expect (category
, 0) < 0
214 || __builtin_expect (category
, 0) >= __LC_LAST
)
217 /* Does user want name of current locale? */
219 return (char *) _nl_current_names
[category
];
221 if (strcmp (locale
, _nl_current_names
[category
]) == 0)
222 /* Changing to the same thing. */
223 return (char *) _nl_current_names
[category
];
225 /* We perhaps really have to load some data. So we determine the
226 path in which to look for the data now. The environment variable
227 `LOCPATH' must only be used when the binary has no SUID or SGID
228 bit set. If using the default path, we tell _nl_find_locale
229 by passing null and it can check the canonical locale archive. */
233 locpath_var
= getenv ("LOCPATH");
234 if (locpath_var
!= NULL
&& locpath_var
[0] != '\0')
236 if (__argz_create_sep (locpath_var
, ':',
237 &locale_path
, &locale_path_len
) != 0)
240 if (__argz_add_sep (&locale_path
, &locale_path_len
,
241 _nl_default_locale_path
, ':') != 0)
245 if (category
== LC_ALL
)
247 /* The user wants to set all categories. The desired locales
248 for the individual categories can be selected by using a
249 composite locale name. This is a semi-colon separated list
250 of entries of the form `CATEGORY=VALUE'. */
251 const char *newnames
[__LC_LAST
];
252 struct locale_data
*newdata
[__LC_LAST
];
254 /* Set all name pointers to the argument name. */
255 for (category
= 0; category
< __LC_LAST
; ++category
)
256 if (category
!= LC_ALL
)
257 newnames
[category
] = (char *) locale
;
259 if (__builtin_expect (strchr (locale
, ';') != NULL
, 0))
261 /* This is a composite name. Make a copy and split it up. */
262 char *np
= strdupa (locale
);
266 while ((cp
= strchr (np
, '=')) != NULL
)
268 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
270 && (size_t) (cp
- np
) == _nl_category_name_sizes
[cnt
]
271 && memcmp (np
, _nl_category_names
[cnt
], cp
- np
) == 0)
274 if (cnt
== __LC_LAST
)
275 /* Bogus category name. */
278 /* Found the category this clause sets. */
279 newnames
[cnt
] = ++cp
;
280 cp
= strchr (cp
, ';');
283 /* Examine the next clause. */
288 /* This was the last clause. We are done. */
292 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
293 if (cnt
!= LC_ALL
&& newnames
[cnt
] == locale
)
294 /* The composite name did not specify all categories. */
298 /* Protect global data. */
299 __libc_lock_lock (__libc_setlocale_lock
);
301 /* Load the new data for each category. */
302 while (category
-- > 0)
303 if (category
!= LC_ALL
)
305 newdata
[category
] = _nl_find_locale (locale_path
, locale_path_len
,
307 &newnames
[category
]);
309 if (newdata
[category
] == NULL
)
312 /* We must not simply free a global locale since we have no
313 control over the usage. So we mark it as un-deletable. */
314 if (newdata
[category
]->usage_count
!= UNDELETABLE
)
315 newdata
[category
]->usage_count
= UNDELETABLE
;
317 /* Make a copy of locale name. */
318 if (newnames
[category
] != _nl_C_name
)
320 newnames
[category
] = strdup (newnames
[category
]);
321 if (newnames
[category
] == NULL
)
326 /* Create new composite name. */
327 composite
= (category
>= 0
328 ? NULL
: new_composite_name (LC_ALL
, newnames
));
329 if (composite
!= NULL
)
331 /* Now we have loaded all the new data. Put it in place. */
332 for (category
= 0; category
< __LC_LAST
; ++category
)
333 if (category
!= LC_ALL
)
335 setdata (category
, newdata
[category
]);
336 setname (category
, newnames
[category
]);
338 setname (LC_ALL
, composite
);
340 /* We successfully loaded a new locale. Let the message catalog
341 functions know about this. */
345 for (++category
; category
< __LC_LAST
; ++category
)
346 if (category
!= LC_ALL
&& newnames
[category
] != _nl_C_name
)
347 free ((char *) newnames
[category
]);
349 /* Critical section left. */
350 __libc_lock_unlock (__libc_setlocale_lock
);
352 /* Free the resources (the locale path variable. */
359 struct locale_data
*newdata
= NULL
;
360 const char *newname
[1] = { locale
};
362 /* Protect global data. */
363 __libc_lock_lock (__libc_setlocale_lock
);
365 if (CATEGORY_USED (category
))
367 /* Only actually load the data if anything will use it. */
368 newdata
= _nl_find_locale (locale_path
, locale_path_len
, category
,
373 /* We must not simply free a global locale since we have no
374 control over the usage. So we mark it as un-deletable.
376 Note: do not remove the `if', it's necessary to copy with
377 the builtin locale data. */
378 if (newdata
->usage_count
!= UNDELETABLE
)
379 newdata
->usage_count
= UNDELETABLE
;
382 /* Make a copy of locale name. */
383 if (newname
[0] != _nl_C_name
)
385 newname
[0] = strdup (newname
[0]);
386 if (newname
[0] == NULL
)
390 /* Create new composite name. */
391 composite
= new_composite_name (category
, newname
);
392 if (composite
== NULL
)
394 if (newname
[0] != _nl_C_name
)
395 free ((char *) newname
[0]);
397 /* Say that we don't have any data loaded. */
403 if (CATEGORY_USED (category
))
404 setdata (category
, newdata
);
406 setname (category
, newname
[0]);
407 setname (LC_ALL
, composite
);
409 /* We successfully loaded a new locale. Let the message catalog
410 functions know about this. */
414 /* Critical section left. */
415 __libc_lock_unlock (__libc_setlocale_lock
);
417 /* Free the resources (the locale path variable. */
420 return (char *) newname
[0];
423 libc_hidden_def (setlocale
)
426 free_category (int category
,
427 struct locale_data
*here
, struct locale_data
*c_data
)
429 struct loaded_l10nfile
*runp
= _nl_locale_file_list
[category
];
431 /* If this category is already "C" don't do anything. */
434 /* We have to be prepared that sometime later we still
435 might need the locale information. */
436 setdata (category
, c_data
);
437 setname (category
, _nl_C_name
);
442 struct loaded_l10nfile
*curr
= runp
;
443 struct locale_data
*data
= (struct locale_data
*) runp
->data
;
445 if (data
!= NULL
&& data
!= c_data
)
446 _nl_unload_locale (data
);
448 free ((char *) curr
->filename
);
453 static void __attribute__ ((unused
))
456 #ifdef NL_CURRENT_INDIRECT
457 /* We don't use the loop because we want to have individual weak
458 symbol references here. */
459 # define DEFINE_CATEGORY(category, category_name, items, a) \
460 if (CATEGORY_USED (category)) \
462 extern struct locale_data _nl_C_##category; \
463 weak_extern (_nl_C_##category) \
464 free_category (category, *_nl_current_##category, &_nl_C_##category); \
466 # include "categories.def"
467 # undef DEFINE_CATEGORY
471 for (category
= 0; category
< __LC_LAST
; ++category
)
472 if (category
!= LC_ALL
)
473 free_category (category
, _NL_CURRENT_DATA (category
),
474 _nl_C_locobj
.__locales
[category
]);
477 setname (LC_ALL
, _nl_C_name
);
479 /* This frees the data structures associated with the locale archive.
480 The locales from the archive are not in the file list, so we have
481 not called _nl_unload_locale on them above. */
482 _nl_archive_subfreeres ();
484 text_set_element (__libc_subfreeres
, free_mem
);