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"
32 /* For each category declare two external variables (with weak references):
33 extern const struct locale_data *_nl_current_CATEGORY;
34 This points to the current locale's in-core data for CATEGORY.
35 extern const struct locale_data _nl_C_CATEGORY;
36 This contains the built-in "C"/"POSIX" locale's data for CATEGORY.
37 Both are weak references; if &_nl_current_CATEGORY is zero,
38 then nothing is using the locale data. */
39 #define DEFINE_CATEGORY(category, category_name, items, a) \
40 weak_extern (_nl_current_##category) \
41 weak_extern (_nl_C_##category) \
42 extern struct locale_data *_nl_current_##category; \
43 extern struct locale_data _nl_C_##category;
44 #include "categories.def"
45 #undef DEFINE_CATEGORY
47 /* Array indexed by category of pointers to _nl_current_CATEGORY slots.
48 Elements are zero for categories whose data is never used. */
49 struct locale_data
* *const _nl_current
[] =
51 #define DEFINE_CATEGORY(category, category_name, items, a) \
52 [category] = &_nl_current_##category,
53 #include "categories.def"
54 #undef DEFINE_CATEGORY
55 /* We need this additional element to simplify the code. It must
57 [LC_ALL
] = (struct locale_data
**) ~0ul
60 /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
61 Elements are zero for categories whose data is never used. */
62 struct locale_data
*const _nl_C
[] attribute_hidden
=
64 #define DEFINE_CATEGORY(category, category_name, items, a) \
65 [category] = &_nl_C_##category,
66 #include "categories.def"
67 #undef DEFINE_CATEGORY
70 # define CATEGORY_USED(category) (_nl_current[category] != NULL)
74 /* The shared library always loads all the categories,
75 and the current global settings are kept in _nl_global_locale. */
77 # define _nl_C (_nl_C_locobj.__locales)
79 # define CATEGORY_USED(category) (1)
84 /* Define an array of category names (also the environment variable names),
85 indexed by integral category. */
86 const char *const _nl_category_names
[] =
88 #define DEFINE_CATEGORY(category, category_name, items, a) \
89 [category] = category_name,
90 #include "categories.def"
91 #undef DEFINE_CATEGORY
94 /* An array of their lengths, for convenience. */
95 const size_t _nl_category_name_sizes
[] =
97 #define DEFINE_CATEGORY(category, category_name, items, a) \
98 [category] = sizeof (category_name) - 1,
99 #include "categories.def"
100 #undef DEFINE_CATEGORY
101 [LC_ALL
] = sizeof ("LC_ALL") - 1
105 /* Declare the postload functions used below. */
107 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
108 #define DEFINE_CATEGORY(category, category_name, items, postload) \
109 extern void postload (void);
110 #include "categories.def"
111 #undef DEFINE_CATEGORY
114 /* Define an array indexed by category of postload functions to call after
115 loading and installing that category's data. */
116 static void (*const _nl_category_postload
[]) (void) =
118 #define DEFINE_CATEGORY(category, category_name, items, postload) \
119 [category] = postload,
120 #include "categories.def"
121 #undef DEFINE_CATEGORY
125 /* Lock for protecting global data. */
126 __libc_lock_define_initialized (, __libc_setlocale_lock attribute_hidden
)
128 /* Defined in loadmsgcat.c. */
129 extern int _nl_msg_cat_cntr
;
132 /* Use this when we come along an error. */
133 #define ERROR_RETURN \
135 __set_errno (EINVAL); \
140 /* Construct a new composite name. */
142 new_composite_name (int category
, const char *newnames
[__LC_LAST
])
150 for (i
= 0; i
< __LC_LAST
; ++i
)
153 const char *name
= (category
== LC_ALL
? newnames
[i
] :
154 category
== i
? newnames
[0] :
155 _nl_current_names
[i
]);
156 last_len
= strlen (name
);
157 cumlen
+= _nl_category_name_sizes
[i
] + 1 + last_len
+ 1;
158 if (i
> 0 && same
&& strcmp (name
, newnames
[0]) != 0)
164 /* All the categories use the same name. */
165 if (strcmp (newnames
[0], _nl_C_name
) == 0
166 || strcmp (newnames
[0], _nl_POSIX_name
) == 0)
167 return (char *) _nl_C_name
;
169 new = malloc (last_len
+ 1);
171 return new == NULL
? NULL
: memcpy (new, newnames
[0], last_len
+ 1);
174 new = malloc (cumlen
);
178 for (i
= 0; i
< __LC_LAST
; ++i
)
181 /* Add "CATEGORY=NAME;" to the string. */
182 const char *name
= (category
== LC_ALL
? newnames
[i
] :
183 category
== i
? newnames
[0] :
184 _nl_current_names
[i
]);
185 p
= __stpcpy (p
, _nl_category_names
[i
]);
187 p
= __stpcpy (p
, name
);
190 p
[-1] = '\0'; /* Clobber the last ';'. */
195 /* Put NAME in _nl_current_names. */
197 setname (int category
, const char *name
)
199 if (_nl_current_names
[category
] == name
)
202 if (_nl_current_names
[category
] != _nl_C_name
)
203 free ((char *) _nl_current_names
[category
]);
205 _nl_current_names
[category
] = name
;
208 /* Put DATA in *_nl_current[CATEGORY]. */
210 setdata (int category
, struct locale_data
*data
)
212 if (CATEGORY_USED (category
))
215 _nl_global_locale
.__locales
[category
] = data
;
218 # warning when uselocale exists it will need the line above too
219 *_nl_current
[category
] = data
;
221 if (_nl_category_postload
[category
])
222 (*_nl_category_postload
[category
]) ();
227 setlocale (int category
, const char *locale
)
230 size_t locale_path_len
;
231 const char *locpath_var
;
234 /* Sanity check for CATEGORY argument. */
235 if (__builtin_expect (category
, 0) < 0
236 || __builtin_expect (category
, 0) >= __LC_LAST
)
239 /* Does user want name of current locale? */
241 return (char *) _nl_current_names
[category
];
243 if (strcmp (locale
, _nl_current_names
[category
]) == 0)
244 /* Changing to the same thing. */
245 return (char *) _nl_current_names
[category
];
247 /* We perhaps really have to load some data. So we determine the
248 path in which to look for the data now. The environment variable
249 `LOCPATH' must only be used when the binary has no SUID or SGID
254 locpath_var
= getenv ("LOCPATH");
255 if (locpath_var
!= NULL
&& locpath_var
[0] != '\0')
256 if (__argz_create_sep (locpath_var
, ':',
257 &locale_path
, &locale_path_len
) != 0)
260 if (__argz_add_sep (&locale_path
, &locale_path_len
, LOCALEDIR
, ':') != 0)
263 if (category
== LC_ALL
)
265 /* The user wants to set all categories. The desired locales
266 for the individual categories can be selected by using a
267 composite locale name. This is a semi-colon separated list
268 of entries of the form `CATEGORY=VALUE'. */
269 const char *newnames
[__LC_LAST
];
270 struct locale_data
*newdata
[__LC_LAST
];
272 /* Set all name pointers to the argument name. */
273 for (category
= 0; category
< __LC_LAST
; ++category
)
274 if (category
!= LC_ALL
)
275 newnames
[category
] = (char *) locale
;
277 if (__builtin_expect (strchr (locale
, ';') != NULL
, 0))
279 /* This is a composite name. Make a copy and split it up. */
280 char *np
= strdupa (locale
);
284 while ((cp
= strchr (np
, '=')) != NULL
)
286 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
288 && (size_t) (cp
- np
) == _nl_category_name_sizes
[cnt
]
289 && memcmp (np
, _nl_category_names
[cnt
], cp
- np
) == 0)
292 if (cnt
== __LC_LAST
)
293 /* Bogus category name. */
296 /* Found the category this clause sets. */
297 newnames
[cnt
] = ++cp
;
298 cp
= strchr (cp
, ';');
301 /* Examine the next clause. */
306 /* This was the last clause. We are done. */
310 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
311 if (cnt
!= LC_ALL
&& newnames
[cnt
] == locale
)
312 /* The composite name did not specify all categories. */
316 /* Protect global data. */
317 __libc_lock_lock (__libc_setlocale_lock
);
319 /* Load the new data for each category. */
320 while (category
-- > 0)
321 if (category
!= LC_ALL
)
323 newdata
[category
] = _nl_find_locale (locale_path
, locale_path_len
,
325 &newnames
[category
]);
327 if (newdata
[category
] == NULL
)
330 /* We must not simply free a global locale since we have no
331 control over the usage. So we mark it as un-deletable. */
332 if (newdata
[category
]->usage_count
!= UNDELETABLE
)
333 newdata
[category
]->usage_count
= UNDELETABLE
;
335 /* Make a copy of locale name. */
336 if (newnames
[category
] != _nl_C_name
)
338 newnames
[category
] = strdup (newnames
[category
]);
339 if (newnames
[category
] == NULL
)
344 /* Create new composite name. */
345 composite
= (category
>= 0
346 ? NULL
: new_composite_name (LC_ALL
, newnames
));
347 if (composite
!= NULL
)
349 /* Now we have loaded all the new data. Put it in place. */
350 for (category
= 0; category
< __LC_LAST
; ++category
)
351 if (category
!= LC_ALL
)
353 setdata (category
, newdata
[category
]);
354 setname (category
, newnames
[category
]);
356 setname (LC_ALL
, composite
);
358 /* We successfully loaded a new locale. Let the message catalog
359 functions know about this. */
363 for (++category
; category
< __LC_LAST
; ++category
)
364 if (category
!= LC_ALL
&& newnames
[category
] != _nl_C_name
)
365 free ((char *) newnames
[category
]);
367 /* Critical section left. */
368 __libc_lock_unlock (__libc_setlocale_lock
);
370 /* Free the resources (the locale path variable. */
377 struct locale_data
*newdata
= NULL
;
378 const char *newname
[1] = { locale
};
380 /* Protect global data. */
381 __libc_lock_lock (__libc_setlocale_lock
);
383 if (CATEGORY_USED (category
))
385 /* Only actually load the data if anything will use it. */
386 newdata
= _nl_find_locale (locale_path
, locale_path_len
, category
,
391 /* We must not simply free a global locale since we have no
392 control over the usage. So we mark it as un-deletable.
394 Note: do not remove the `if', it's necessary to copy with
395 the builtin locale data. */
396 if (newdata
->usage_count
!= UNDELETABLE
)
397 newdata
->usage_count
= UNDELETABLE
;
400 /* Make a copy of locale name. */
401 if (newname
[0] != _nl_C_name
)
403 newname
[0] = strdup (newname
[0]);
404 if (newname
[0] == NULL
)
408 /* Create new composite name. */
409 composite
= new_composite_name (category
, newname
);
410 if (composite
== NULL
)
412 if (newname
[0] != _nl_C_name
)
413 free ((char *) newname
[0]);
415 /* Say that we don't have any data loaded. */
421 if (CATEGORY_USED (category
))
422 setdata (category
, newdata
);
424 setname (category
, newname
[0]);
425 setname (LC_ALL
, composite
);
427 /* We successfully loaded a new locale. Let the message catalog
428 functions know about this. */
432 /* Critical section left. */
433 __libc_lock_unlock (__libc_setlocale_lock
);
435 /* Free the resources (the locale path variable. */
438 return (char *) newname
[0];
441 libc_hidden_def (setlocale
)
443 static void __attribute__ ((unused
))
448 for (category
= 0; category
< __LC_LAST
; ++category
)
449 if (category
!= LC_ALL
)
451 struct locale_data
*here
= _NL_CURRENT_DATA (category
);
452 struct loaded_l10nfile
*runp
= _nl_locale_file_list
[category
];
454 /* If this category is already "C" don't do anything. */
455 if (here
!= _nl_C
[category
])
457 /* We have to be prepared that sometime later me still
458 might need the locale information. */
459 setdata (category
, _nl_C
[category
]);
460 setname (category
, _nl_C_name
);
462 _nl_unload_locale (here
);
467 struct loaded_l10nfile
*curr
= runp
;
468 struct locale_data
*data
= (struct locale_data
*) runp
->data
;
470 if (data
!= NULL
&& data
!= here
&& data
!= _nl_C
[category
])
471 _nl_unload_locale (data
);
473 free ((char *) curr
->filename
);
478 setname (LC_ALL
, _nl_C_name
);
480 text_set_element (__libc_subfreeres
, free_mem
);