* sysdeps/generic/dl-lookupcfg.h: Include <tls.h>.
[glibc.git] / locale / localeinfo.h
blob28256e9a2b194d4b12978058d7f121c5524ad5b5
1 /* Declarations for internal libc locale interfaces
2 Copyright (C) 1995-2001, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef _LOCALEINFO_H
21 #define _LOCALEINFO_H 1
23 #include <stddef.h>
24 #include <langinfo.h>
25 #include <limits.h>
26 #include <locale.h>
27 #include <time.h>
28 #include <stdint.h>
29 #include <sys/types.h>
31 #include <intl/loadinfo.h> /* For loaded_l10nfile definition. */
33 /* Magic number at the beginning of a locale data file for CATEGORY. */
34 #define LIMAGIC(category) ((unsigned int) (0x20000828 ^ (category)))
36 /* Two special weight constants for the collation data. */
37 #define IGNORE_CHAR 2
39 /* We use a special value for the usage counter in `locale_data' to
40 signal that this data must never be removed anymore. */
41 #define MAX_USAGE_COUNT (UINT_MAX - 1)
42 #define UNDELETABLE UINT_MAX
44 /* Structure describing locale data in core for a category. */
45 struct locale_data
47 const char *name;
48 const char *filedata; /* Region mapping the file data. */
49 off_t filesize; /* Size of the file (and the region). */
50 enum /* Flavor of storage used for those. */
52 ld_malloced, /* Both are malloc'd. */
53 ld_mapped, /* name is malloc'd, filedata mmap'd */
54 ld_archive /* Both point into mmap'd archive regions. */
55 } alloc;
57 /* This provides a slot for category-specific code to cache data computed
58 about this locale. That code can set a cleanup function to deallocate
59 the data. */
60 struct
62 void (*cleanup) (struct locale_data *) internal_function;
63 union
65 void *data;
66 struct lc_time_data *time;
67 const struct gconv_fcts *ctype;
69 } private;
71 unsigned int usage_count; /* Counter for users. */
73 int use_translit; /* Nonzero if the mb*towv*() and wc*tomb()
74 functions should use transliteration. */
76 unsigned int nstrings; /* Number of strings below. */
77 union locale_data_value
79 const uint32_t *wstr;
80 const char *string;
81 unsigned int word;
83 values __flexarr; /* Items, usually pointers into `filedata'. */
86 /* We know three kinds of collation sorting rules. */
87 enum coll_sort_rule
89 illegal_0__,
90 sort_forward,
91 sort_backward,
92 illegal_3__,
93 sort_position,
94 sort_forward_position,
95 sort_backward_position,
96 sort_mask
99 /* We can map the types of the entries into a few categories. */
100 enum value_type
102 none,
103 string,
104 stringarray,
105 byte,
106 bytearray,
107 word,
108 stringlist,
109 wordarray,
110 wstring,
111 wstringarray,
112 wstringlist
116 /* Definitions for `era' information from LC_TIME. */
117 #define ERA_NAME_FORMAT_MEMBERS 4
118 #define ERA_M_NAME 0
119 #define ERA_M_FORMAT 1
120 #define ERA_W_NAME 2
121 #define ERA_W_FORMAT 3
124 /* Structure to access `era' information from LC_TIME. */
125 struct era_entry
127 uint32_t direction; /* Contains '+' or '-'. */
128 int32_t offset;
129 int32_t start_date[3];
130 int32_t stop_date[3];
131 const char *era_name;
132 const char *era_format;
133 const wchar_t *era_wname;
134 const wchar_t *era_wformat;
135 int absolute_direction;
136 /* absolute direction:
137 +1 indicates that year number is higher in the future. (like A.D.)
138 -1 indicates that year number is higher in the past. (like B.C.) */
141 /* Structure caching computed data about information from LC_TIME.
142 The `private.time' member of `struct locale_data' points to this. */
143 struct lc_time_data
145 struct era_entry *eras;
146 size_t num_eras;
147 int era_initialized;
149 const char **alt_digits;
150 const wchar_t **walt_digits;
151 int alt_digits_initialized;
152 int walt_digits_initialized;
156 /* LC_CTYPE specific:
157 Hardwired indices for standard wide character translation mappings. */
158 enum
160 __TOW_toupper = 0,
161 __TOW_tolower = 1
165 /* LC_CTYPE specific:
166 Access a wide character class with a single character index.
167 _ISCTYPE (c, desc) = iswctype (btowc (c), desc).
168 c must be an `unsigned char'. desc must be a nonzero wctype_t. */
169 #define _ISCTYPE(c, desc) \
170 (((((const uint32_t *) (desc)) - 8)[(c) >> 5] >> ((c) & 0x1f)) & 1)
172 extern const char *const _nl_category_names[__LC_LAST] attribute_hidden;
173 extern const size_t _nl_category_name_sizes[__LC_LAST] attribute_hidden;
175 /* Name of the standard locales. */
176 extern const char _nl_C_name[] attribute_hidden;
177 extern const char _nl_POSIX_name[] attribute_hidden;
179 /* The standard codeset. */
180 extern const char _nl_C_codeset[] attribute_hidden;
182 /* This is the internal locale_t object that holds the global locale
183 controlled by calls to setlocale. A thread's TSD locale pointer
184 points to this when `uselocale (LC_GLOBAL_LOCALE)' is in effect. */
185 extern struct __locale_struct _nl_global_locale attribute_hidden;
187 /* This fetches the thread-local locale_t pointer, either one set with
188 uselocale or &_nl_global_locale. */
189 #define _NL_CURRENT_LOCALE ((__locale_t) __libc_tsd_get (LOCALE))
190 #include <bits/libc-tsd.h>
191 __libc_tsd_define (extern, LOCALE)
194 /* For static linking it is desireable to avoid always linking in the code
195 and data for every category when we can tell at link time that they are
196 unused. We can manage this playing some tricks with weak references.
197 But with thread-local locale settings, it becomes quite ungainly unless
198 we can use __thread variables. So only in that case do we attempt this. */
199 #if !defined SHARED && defined HAVE___THREAD && defined HAVE_WEAK_SYMBOLS
200 # include <tls.h>
201 # if USE_TLS
202 # define NL_CURRENT_INDIRECT 1
203 # endif
204 #endif
206 #ifdef NL_CURRENT_INDIRECT
208 /* For each category declare the thread-local variable for the current
209 locale data. This has an extra indirection so it points at the
210 __locales[CATEGORY] element in either _nl_global_locale or the current
211 locale object set by uselocale, which points at the actual data. The
212 reason for having these variables is so that references to particular
213 categories will link in the lc-CATEGORY.c module to define this symbol,
214 and we arrange that linking that module is what brings in all the code
215 associated with this category. */
216 #define DEFINE_CATEGORY(category, category_name, items, a) \
217 extern __thread struct locale_data *const *_nl_current_##category \
218 attribute_hidden;
219 #include "categories.def"
220 #undef DEFINE_CATEGORY
222 /* Return a pointer to the current `struct locale_data' for CATEGORY. */
223 #define _NL_CURRENT_DATA(category) (*_nl_current_##category)
225 /* Extract the current CATEGORY locale's string for ITEM. */
226 #define _NL_CURRENT(category, item) \
227 ((*_nl_current_##category)->values[_NL_ITEM_INDEX (item)].string)
229 /* Extract the current CATEGORY locale's string for ITEM. */
230 #define _NL_CURRENT_WSTR(category, item) \
231 ((wchar_t *) (*_nl_current_##category)->values[_NL_ITEM_INDEX (item)].wstr)
233 /* Extract the current CATEGORY locale's word for ITEM. */
234 #define _NL_CURRENT_WORD(category, item) \
235 ((*_nl_current_##category)->values[_NL_ITEM_INDEX (item)].word)
237 /* This is used in lc-CATEGORY.c to define _nl_current_CATEGORY. */
238 #define _NL_CURRENT_DEFINE(category) \
239 __thread struct locale_data *const *_nl_current_##category \
240 attribute_hidden = &_nl_global_locale.__locales[category]; \
241 asm (_NL_CURRENT_DEFINE_STRINGIFY (ASM_GLOBAL_DIRECTIVE) \
242 " " __SYMBOL_PREFIX "_nl_current_" #category "_used\n" \
243 _NL_CURRENT_DEFINE_ABS (_nl_current_##category##_used, 1));
244 #define _NL_CURRENT_DEFINE_STRINGIFY(x) _NL_CURRENT_DEFINE_STRINGIFY_1 (x)
245 #define _NL_CURRENT_DEFINE_STRINGIFY_1(x) #x
246 #ifdef HAVE_ASM_SET_DIRECTIVE
247 # define _NL_CURRENT_DEFINE_ABS(sym, val) ".set " #sym ", " #val
248 #else
249 # define _NL_CURRENT_DEFINE_ABS(sym, val) #sym " = " #val
250 #endif
252 #else
254 /* All categories are always loaded in the shared library, so there is no
255 point in having lots of separate symbols for linking. */
257 /* Return a pointer to the current `struct locale_data' for CATEGORY. */
258 # define _NL_CURRENT_DATA(category) \
259 (_NL_CURRENT_LOCALE->__locales[category])
261 /* Extract the current CATEGORY locale's string for ITEM. */
262 # define _NL_CURRENT(category, item) \
263 (_NL_CURRENT_DATA (category)->values[_NL_ITEM_INDEX (item)].string)
265 /* Extract the current CATEGORY locale's string for ITEM. */
266 # define _NL_CURRENT_WSTR(category, item) \
267 ((wchar_t *) _NL_CURRENT_DATA (category)->values[_NL_ITEM_INDEX (item)].wstr)
269 /* Extract the current CATEGORY locale's word for ITEM. */
270 # define _NL_CURRENT_WORD(category, item) \
271 (_NL_CURRENT_DATA (category)->values[_NL_ITEM_INDEX (item)].word)
273 /* This is used in lc-CATEGORY.c to define _nl_current_CATEGORY. */
274 # define _NL_CURRENT_DEFINE(category) \
275 /* No per-category variable here. */
277 #endif
280 /* Default search path if no LOCPATH environment variable. */
281 extern const char _nl_default_locale_path[] attribute_hidden;
283 /* Load the locale data for CATEGORY from the file specified by *NAME.
284 If *NAME is "", use environment variables as specified by POSIX, and
285 fill in *NAME with the actual name used. If LOCALE_PATH is not null,
286 those directories are searched for the locale files. If it's null,
287 the locale archive is checked first and then _nl_default_locale_path
288 is searched for locale files. */
289 extern struct locale_data *_nl_find_locale (const char *locale_path,
290 size_t locale_path_len,
291 int category, const char **name)
292 internal_function attribute_hidden;
294 /* Try to load the file described by FILE. */
295 extern void _nl_load_locale (struct loaded_l10nfile *file, int category)
296 internal_function attribute_hidden;
298 /* Free all resource. */
299 extern void _nl_unload_locale (struct locale_data *locale)
300 internal_function attribute_hidden;
302 /* Free the locale and give back all memory if the usage count is one. */
303 extern void _nl_remove_locale (int locale, struct locale_data *data)
304 internal_function attribute_hidden;
306 /* Find the locale *NAMEP in the locale archive, and return the
307 internalized data structure for its CATEGORY data. If this locale has
308 already been loaded from the archive, just returns the existing data
309 structure. If successful, sets *NAMEP to point directly into the mapped
310 archive string table; that way, the next call can short-circuit strcmp. */
311 extern struct locale_data *_nl_load_locale_from_archive (int category,
312 const char **namep)
313 internal_function attribute_hidden;
315 /* Subroutine of setlocale's __libc_subfreeres hook. */
316 extern void _nl_archive_subfreeres (void) attribute_hidden;
318 /* Validate the contents of a locale file and set up the in-core
319 data structure to point into the data. This leaves the `alloc'
320 and `name' fields uninitialized, for the caller to fill in.
321 If any bogons are detected in the data, this will refuse to
322 intern it, and return a null pointer instead. */
323 extern struct locale_data *_nl_intern_locale_data (int category,
324 const void *data,
325 size_t datasize)
326 internal_function attribute_hidden;
329 /* Return `era' entry which corresponds to TP. Used in strftime. */
330 extern struct era_entry *_nl_get_era_entry (const struct tm *tp,
331 struct locale_data *lc_time)
332 internal_function attribute_hidden;
334 /* Return `era' cnt'th entry . Used in strptime. */
335 extern struct era_entry *_nl_select_era_entry (int cnt,
336 struct locale_data *lc_time)
337 internal_function attribute_hidden;
339 /* Return `alt_digit' which corresponds to NUMBER. Used in strftime. */
340 extern const char *_nl_get_alt_digit (unsigned int number,
341 struct locale_data *lc_time)
342 internal_function attribute_hidden;
344 /* Similar, but now for wide characters. */
345 extern const wchar_t *_nl_get_walt_digit (unsigned int number,
346 struct locale_data *lc_time)
347 internal_function attribute_hidden;
349 /* Parse string as alternative digit and return numeric value. */
350 extern int _nl_parse_alt_digit (const char **strp,
351 struct locale_data *lc_time)
352 internal_function attribute_hidden;
354 /* Postload processing. */
355 extern void _nl_postload_ctype (void);
357 /* Functions used for the `private.cleanup' hook. */
358 extern void _nl_cleanup_time (struct locale_data *)
359 internal_function attribute_hidden;
362 #endif /* localeinfo.h */