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
250 bit set. If using the default path, we tell _nl_find_locale
251 by passing null and it can check the canonical locale archive. */
255 locpath_var
= getenv ("LOCPATH");
256 if (locpath_var
!= NULL
&& locpath_var
[0] != '\0')
258 if (__argz_create_sep (locpath_var
, ':',
259 &locale_path
, &locale_path_len
) != 0)
262 if (__argz_add_sep (&locale_path
, &locale_path_len
,
263 _nl_default_locale_path
, ':') != 0)
267 if (category
== LC_ALL
)
269 /* The user wants to set all categories. The desired locales
270 for the individual categories can be selected by using a
271 composite locale name. This is a semi-colon separated list
272 of entries of the form `CATEGORY=VALUE'. */
273 const char *newnames
[__LC_LAST
];
274 struct locale_data
*newdata
[__LC_LAST
];
276 /* Set all name pointers to the argument name. */
277 for (category
= 0; category
< __LC_LAST
; ++category
)
278 if (category
!= LC_ALL
)
279 newnames
[category
] = (char *) locale
;
281 if (__builtin_expect (strchr (locale
, ';') != NULL
, 0))
283 /* This is a composite name. Make a copy and split it up. */
284 char *np
= strdupa (locale
);
288 while ((cp
= strchr (np
, '=')) != NULL
)
290 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
292 && (size_t) (cp
- np
) == _nl_category_name_sizes
[cnt
]
293 && memcmp (np
, _nl_category_names
[cnt
], cp
- np
) == 0)
296 if (cnt
== __LC_LAST
)
297 /* Bogus category name. */
300 /* Found the category this clause sets. */
301 newnames
[cnt
] = ++cp
;
302 cp
= strchr (cp
, ';');
305 /* Examine the next clause. */
310 /* This was the last clause. We are done. */
314 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
315 if (cnt
!= LC_ALL
&& newnames
[cnt
] == locale
)
316 /* The composite name did not specify all categories. */
320 /* Protect global data. */
321 __libc_lock_lock (__libc_setlocale_lock
);
323 /* Load the new data for each category. */
324 while (category
-- > 0)
325 if (category
!= LC_ALL
)
327 newdata
[category
] = _nl_find_locale (locale_path
, locale_path_len
,
329 &newnames
[category
]);
331 if (newdata
[category
] == NULL
)
334 /* We must not simply free a global locale since we have no
335 control over the usage. So we mark it as un-deletable. */
336 if (newdata
[category
]->usage_count
!= UNDELETABLE
)
337 newdata
[category
]->usage_count
= UNDELETABLE
;
339 /* Make a copy of locale name. */
340 if (newnames
[category
] != _nl_C_name
)
342 newnames
[category
] = strdup (newnames
[category
]);
343 if (newnames
[category
] == NULL
)
348 /* Create new composite name. */
349 composite
= (category
>= 0
350 ? NULL
: new_composite_name (LC_ALL
, newnames
));
351 if (composite
!= NULL
)
353 /* Now we have loaded all the new data. Put it in place. */
354 for (category
= 0; category
< __LC_LAST
; ++category
)
355 if (category
!= LC_ALL
)
357 setdata (category
, newdata
[category
]);
358 setname (category
, newnames
[category
]);
360 setname (LC_ALL
, composite
);
362 /* We successfully loaded a new locale. Let the message catalog
363 functions know about this. */
367 for (++category
; category
< __LC_LAST
; ++category
)
368 if (category
!= LC_ALL
&& newnames
[category
] != _nl_C_name
)
369 free ((char *) newnames
[category
]);
371 /* Critical section left. */
372 __libc_lock_unlock (__libc_setlocale_lock
);
374 /* Free the resources (the locale path variable. */
381 struct locale_data
*newdata
= NULL
;
382 const char *newname
[1] = { locale
};
384 /* Protect global data. */
385 __libc_lock_lock (__libc_setlocale_lock
);
387 if (CATEGORY_USED (category
))
389 /* Only actually load the data if anything will use it. */
390 newdata
= _nl_find_locale (locale_path
, locale_path_len
, category
,
395 /* We must not simply free a global locale since we have no
396 control over the usage. So we mark it as un-deletable.
398 Note: do not remove the `if', it's necessary to copy with
399 the builtin locale data. */
400 if (newdata
->usage_count
!= UNDELETABLE
)
401 newdata
->usage_count
= UNDELETABLE
;
404 /* Make a copy of locale name. */
405 if (newname
[0] != _nl_C_name
)
407 newname
[0] = strdup (newname
[0]);
408 if (newname
[0] == NULL
)
412 /* Create new composite name. */
413 composite
= new_composite_name (category
, newname
);
414 if (composite
== NULL
)
416 if (newname
[0] != _nl_C_name
)
417 free ((char *) newname
[0]);
419 /* Say that we don't have any data loaded. */
425 if (CATEGORY_USED (category
))
426 setdata (category
, newdata
);
428 setname (category
, newname
[0]);
429 setname (LC_ALL
, composite
);
431 /* We successfully loaded a new locale. Let the message catalog
432 functions know about this. */
436 /* Critical section left. */
437 __libc_lock_unlock (__libc_setlocale_lock
);
439 /* Free the resources (the locale path variable. */
442 return (char *) newname
[0];
445 libc_hidden_def (setlocale
)
447 static void __attribute__ ((unused
))
452 for (category
= 0; category
< __LC_LAST
; ++category
)
453 if (category
!= LC_ALL
)
455 struct locale_data
*here
= _NL_CURRENT_DATA (category
);
456 struct loaded_l10nfile
*runp
= _nl_locale_file_list
[category
];
458 /* If this category is already "C" don't do anything. */
459 if (here
!= _nl_C
[category
])
461 /* We have to be prepared that sometime later me still
462 might need the locale information. */
463 setdata (category
, _nl_C
[category
]);
464 setname (category
, _nl_C_name
);
466 _nl_unload_locale (here
);
471 struct loaded_l10nfile
*curr
= runp
;
472 struct locale_data
*data
= (struct locale_data
*) runp
->data
;
474 if (data
!= NULL
&& data
!= here
&& data
!= _nl_C
[category
])
475 _nl_unload_locale (data
);
477 free ((char *) curr
->filename
);
482 setname (LC_ALL
, _nl_C_name
);
484 text_set_element (__libc_subfreeres
, free_mem
);