1 /* Copyright (C) 1991, 92, 95-99, 2000 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
22 #include <bits/libc-lock.h>
28 #include "localeinfo.h"
30 /* For each category declare two external variables (with weak references):
31 extern const struct locale_data *_nl_current_CATEGORY;
32 This points to the current locale's in-core data for CATEGORY.
33 extern const struct locale_data _nl_C_CATEGORY;
34 This contains the built-in "C"/"POSIX" locale's data for CATEGORY.
35 Both are weak references; if &_nl_current_CATEGORY is zero,
36 then nothing is using the locale data. */
37 #define DEFINE_CATEGORY(category, category_name, items, a) \
38 extern struct locale_data *_nl_current_##category; \
39 extern struct locale_data _nl_C_##category;
40 #include "categories.def"
41 #undef DEFINE_CATEGORY
43 /* Array indexed by category of pointers to _nl_current_CATEGORY slots.
44 Elements are zero for categories whose data is never used. */
45 struct locale_data
* *const _nl_current
[] =
47 #define DEFINE_CATEGORY(category, category_name, items, a) \
48 [category] = &_nl_current_##category,
49 #include "categories.def"
50 #undef DEFINE_CATEGORY
51 /* We need this additional element to simplify the code. It must
53 [LC_ALL
] = (struct locale_data
**) ~0ul
56 /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
57 Elements are zero for categories whose data is never used. */
58 struct locale_data
*const _nl_C
[] =
60 #define DEFINE_CATEGORY(category, category_name, items, a) \
61 [category] = &_nl_C_##category,
62 #include "categories.def"
63 #undef DEFINE_CATEGORY
67 /* Define an array of category names (also the environment variable names),
68 indexed by integral category. */
69 const char *const _nl_category_names
[] =
71 #define DEFINE_CATEGORY(category, category_name, items, a) \
72 [category] = category_name,
73 #include "categories.def"
74 #undef DEFINE_CATEGORY
77 /* An array of their lengths, for convenience. */
78 const size_t _nl_category_name_sizes
[] =
80 #define DEFINE_CATEGORY(category, category_name, items, a) \
81 [category] = sizeof (category_name) - 1,
82 #include "categories.def"
83 #undef DEFINE_CATEGORY
84 [LC_ALL
] = sizeof ("LC_ALL") - 1
88 /* Declare the postload functions used below. */
90 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
91 #define DEFINE_CATEGORY(category, category_name, items, postload) \
92 extern void postload (void);
93 #include "categories.def"
94 #undef DEFINE_CATEGORY
97 /* Define an array indexed by category of postload functions to call after
98 loading and installing that category's data. */
99 static void (*const _nl_category_postload
[]) (void) =
101 #define DEFINE_CATEGORY(category, category_name, items, postload) \
102 [category] = postload,
103 #include "categories.def"
104 #undef DEFINE_CATEGORY
108 /* Name of current locale for each individual category.
109 Each is malloc'd unless it is nl_C_name. */
110 static const char *_nl_current_names
[] =
112 #define DEFINE_CATEGORY(category, category_name, items, a) \
113 [category] = _nl_C_name,
114 #include "categories.def"
115 #undef DEFINE_CATEGORY
116 [LC_ALL
] = _nl_C_name
/* For LC_ALL. */
120 /* Lock for protecting global data. */
121 __libc_lock_define_initialized (, __libc_setlocale_lock
)
123 /* Defined in loadmsgcat.c. */
124 extern int _nl_msg_cat_cntr
;
127 /* Use this when we come along an error. */
128 #define ERROR_RETURN \
130 __set_errno (EINVAL); \
135 /* Construct a new composite name. */
137 new_composite_name (int category
, const char *newnames
[__LC_LAST
])
145 for (i
= 0; i
< __LC_LAST
; ++i
)
148 const char *name
= (category
== LC_ALL
? newnames
[i
] :
149 category
== i
? newnames
[0] :
150 _nl_current_names
[i
]);
151 last_len
= strlen (name
);
152 cumlen
+= _nl_category_name_sizes
[i
] + 1 + last_len
+ 1;
153 if (i
> 0 && same
&& strcmp (name
, newnames
[0]) != 0)
159 /* All the categories use the same name. */
160 if (strcmp (newnames
[0], _nl_C_name
) == 0
161 || strcmp (newnames
[0], _nl_POSIX_name
) == 0)
162 return (char *) _nl_C_name
;
164 new = malloc (last_len
+ 1);
166 return new == NULL
? NULL
: memcpy (new, newnames
[0], last_len
+ 1);
169 new = malloc (cumlen
);
173 for (i
= 0; i
< __LC_LAST
; ++i
)
176 /* Add "CATEGORY=NAME;" to the string. */
177 const char *name
= (category
== LC_ALL
? newnames
[i
] :
178 category
== i
? newnames
[0] :
179 _nl_current_names
[i
]);
180 p
= __stpcpy (p
, _nl_category_names
[i
]);
182 p
= __stpcpy (p
, name
);
185 p
[-1] = '\0'; /* Clobber the last ';'. */
190 /* Put NAME in _nl_current_names. */
192 setname (int category
, const char *name
)
194 if (_nl_current_names
[category
] == name
)
197 if (_nl_current_names
[category
] != _nl_C_name
)
198 free ((char *) _nl_current_names
[category
]);
200 _nl_current_names
[category
] = name
;
204 /* Put DATA in *_nl_current[CATEGORY]. */
206 setdata (int category
, struct locale_data
*data
)
208 if (_nl_current
[category
] != NULL
)
210 *_nl_current
[category
] = data
;
211 if (_nl_category_postload
[category
])
212 (*_nl_category_postload
[category
]) ();
218 setlocale (int category
, const char *locale
)
221 size_t locale_path_len
;
222 const char *locpath_var
;
225 /* Sanity check for CATEGORY argument. */
226 if (__builtin_expect (category
, 0) < 0
227 || __builtin_expect (category
, 0) >= __LC_LAST
)
230 /* Does user want name of current locale? */
232 return (char *) _nl_current_names
[category
];
234 if (strcmp (locale
, _nl_current_names
[category
]) == 0)
235 /* Changing to the same thing. */
236 return (char *) _nl_current_names
[category
];
238 /* We perhaps really have to load some data. So we determine the
239 path in which to look for the data now. The environment variable
240 `LOCPATH' must only be used when the binary has no SUID or SGID
245 locpath_var
= getenv ("LOCPATH");
246 if (locpath_var
!= NULL
&& locpath_var
[0] != '\0')
247 if (__argz_create_sep (locpath_var
, ':',
248 &locale_path
, &locale_path_len
) != 0)
251 if (__argz_add_sep (&locale_path
, &locale_path_len
, LOCALEDIR
, ':') != 0)
254 if (category
== LC_ALL
)
256 /* The user wants to set all categories. The desired locales
257 for the individual categories can be selected by using a
258 composite locale name. This is a semi-colon separated list
259 of entries of the form `CATEGORY=VALUE'. */
260 const char *newnames
[__LC_LAST
];
261 struct locale_data
*newdata
[__LC_LAST
];
263 /* Set all name pointers to the argument name. */
264 for (category
= 0; category
< __LC_LAST
; ++category
)
265 if (category
!= LC_ALL
)
266 newnames
[category
] = (char *) locale
;
268 if (__builtin_expect (strchr (locale
, ';') != NULL
, 0))
270 /* This is a composite name. Make a copy and split it up. */
271 char *np
= strdupa (locale
);
275 while ((cp
= strchr (np
, '=')) != NULL
)
277 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
279 && (size_t) (cp
- np
) == _nl_category_name_sizes
[cnt
]
280 && memcmp (np
, _nl_category_names
[cnt
], cp
- np
) == 0)
283 if (cnt
== __LC_LAST
)
284 /* Bogus category name. */
287 /* Found the category this clause sets. */
288 newnames
[cnt
] = ++cp
;
289 cp
= strchr (cp
, ';');
292 /* Examine the next clause. */
297 /* This was the last clause. We are done. */
301 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
302 if (cnt
!= LC_ALL
&& newnames
[cnt
] == locale
)
303 /* The composite name did not specify all categories. */
307 /* Protect global data. */
308 __libc_lock_lock (__libc_setlocale_lock
);
310 /* Load the new data for each category. */
311 while (category
-- > 0)
312 if (category
!= LC_ALL
)
314 newdata
[category
] = _nl_find_locale (locale_path
, locale_path_len
,
316 &newnames
[category
]);
318 if (newdata
[category
] == NULL
)
321 /* We must not simply free a global locale since we have no
322 control over the usage. So we mark it as un-deletable. */
323 if (newdata
[category
]->usage_count
!= UNDELETABLE
)
324 newdata
[category
]->usage_count
= UNDELETABLE
;
326 /* Make a copy of locale name. */
327 if (newnames
[category
] != _nl_C_name
)
329 newnames
[category
] = strdup (newnames
[category
]);
330 if (newnames
[category
] == NULL
)
335 /* Create new composite name. */
336 composite
= (category
>= 0
337 ? NULL
: new_composite_name (LC_ALL
, newnames
));
338 if (composite
!= NULL
)
340 /* Now we have loaded all the new data. Put it in place. */
341 for (category
= 0; category
< __LC_LAST
; ++category
)
342 if (category
!= LC_ALL
)
344 setdata (category
, newdata
[category
]);
345 setname (category
, newnames
[category
]);
347 setname (LC_ALL
, composite
);
349 /* We successfully loaded a new locale. Let the message catalog
350 functions know about this. */
354 for (++category
; category
< __LC_LAST
; ++category
)
355 if (category
!= LC_ALL
&& newnames
[category
] != _nl_C_name
)
356 free ((char *) newnames
[category
]);
358 /* Critical section left. */
359 __libc_lock_unlock (__libc_setlocale_lock
);
361 /* Free the resources (the locale path variable. */
368 struct locale_data
*newdata
= NULL
;
369 const char *newname
[1] = { locale
};
371 /* Protect global data. */
372 __libc_lock_lock (__libc_setlocale_lock
);
374 if (_nl_current
[category
] != NULL
)
376 /* Only actually load the data if anything will use it. */
377 newdata
= _nl_find_locale (locale_path
, locale_path_len
, category
,
382 /* We must not simply free a global locale since we have no
383 control over the usage. So we mark it as un-deletable.
385 Note: do not remove the `if', it's necessary to copy with
386 the builtin locale data. */
387 if (newdata
->usage_count
!= UNDELETABLE
)
388 newdata
->usage_count
= UNDELETABLE
;
391 /* Make a copy of locale name. */
392 if (newname
[0] != _nl_C_name
)
394 newname
[0] = strdup (newname
[0]);
395 if (newname
[0] == NULL
)
399 /* Create new composite name. */
400 composite
= new_composite_name (category
, newname
);
401 if (composite
== NULL
)
403 if (newname
[0] != _nl_C_name
)
404 free ((char *) newname
[0]);
406 /* Say that we don't have any data loaded. */
412 if (_nl_current
[category
] != NULL
)
413 setdata (category
, newdata
);
415 setname (category
, newname
[0]);
416 setname (LC_ALL
, composite
);
418 /* We successfully loaded a new locale. Let the message catalog
419 functions know about this. */
423 /* Critical section left. */
424 __libc_lock_unlock (__libc_setlocale_lock
);
426 /* Free the resources (the locale path variable. */
429 return (char *) newname
[0];
434 static void __attribute__ ((unused
))
439 for (category
= 0; category
< __LC_LAST
; ++category
)
440 if (category
!= LC_ALL
)
442 struct locale_data
*here
= *_nl_current
[category
];
444 /* If this category is already "C" don't do anything. */
445 if (here
== _nl_C
[category
])
448 /* We have to be prepared that sometime later me still might
449 need the locale information. */
450 setdata (category
, _nl_C
[category
]);
451 setname (category
, _nl_C_name
);
453 _nl_unload_locale (here
);
456 setname (LC_ALL
, _nl_C_name
);
458 text_set_element (__libc_subfreeres
, free_mem
);