xdrmem_setpos): Don't compare addresses as signed longs, check for
[glibc/history.git] / locale / localeinfo.h
blob4f746a298d1b3587b60aaadd77712acf3bdfd708
1 /* Declarations for internal libc locale interfaces
2 Copyright (C) 1995-2003, 2005, 2006 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) \
35 (category == LC_COLLATE \
36 ? ((unsigned int) (0x20051014 ^ (category))) \
37 : ((unsigned int) (0x20031115 ^ (category))))
39 /* Two special weight constants for the collation data. */
40 #define IGNORE_CHAR 2
42 /* We use a special value for the usage counter in `locale_data' to
43 signal that this data must never be removed anymore. */
44 #define MAX_USAGE_COUNT (UINT_MAX - 1)
45 #define UNDELETABLE UINT_MAX
47 /* Structure describing locale data in core for a category. */
48 struct locale_data
50 const char *name;
51 const char *filedata; /* Region mapping the file data. */
52 off_t filesize; /* Size of the file (and the region). */
53 enum /* Flavor of storage used for those. */
55 ld_malloced, /* Both are malloc'd. */
56 ld_mapped, /* name is malloc'd, filedata mmap'd */
57 ld_archive /* Both point into mmap'd archive regions. */
58 } alloc;
60 /* This provides a slot for category-specific code to cache data computed
61 about this locale. That code can set a cleanup function to deallocate
62 the data. */
63 struct
65 void (*cleanup) (struct locale_data *) internal_function;
66 union
68 void *data;
69 struct lc_time_data *time;
70 const struct gconv_fcts *ctype;
72 } private;
74 unsigned int usage_count; /* Counter for users. */
76 int use_translit; /* Nonzero if the mb*towv*() and wc*tomb()
77 functions should use transliteration. */
79 unsigned int nstrings; /* Number of strings below. */
80 union locale_data_value
82 const uint32_t *wstr;
83 const char *string;
84 unsigned int word; /* Note endian issues vs 64-bit pointers. */
86 values __flexarr; /* Items, usually pointers into `filedata'. */
89 /* We know three kinds of collation sorting rules. */
90 enum coll_sort_rule
92 illegal_0__,
93 sort_forward,
94 sort_backward,
95 illegal_3__,
96 sort_position,
97 sort_forward_position,
98 sort_backward_position,
99 sort_mask
102 /* We can map the types of the entries into a few categories. */
103 enum value_type
105 none,
106 string,
107 stringarray,
108 byte,
109 bytearray,
110 word,
111 stringlist,
112 wordarray,
113 wstring,
114 wstringarray,
115 wstringlist
119 /* Definitions for `era' information from LC_TIME. */
120 #define ERA_NAME_FORMAT_MEMBERS 4
121 #define ERA_M_NAME 0
122 #define ERA_M_FORMAT 1
123 #define ERA_W_NAME 2
124 #define ERA_W_FORMAT 3
127 /* Structure to access `era' information from LC_TIME. */
128 struct era_entry
130 uint32_t direction; /* Contains '+' or '-'. */
131 int32_t offset;
132 int32_t start_date[3];
133 int32_t stop_date[3];
134 const char *era_name;
135 const char *era_format;
136 const wchar_t *era_wname;
137 const wchar_t *era_wformat;
138 int absolute_direction;
139 /* absolute direction:
140 +1 indicates that year number is higher in the future. (like A.D.)
141 -1 indicates that year number is higher in the past. (like B.C.) */
144 /* Structure caching computed data about information from LC_TIME.
145 The `private.time' member of `struct locale_data' points to this. */
146 struct lc_time_data
148 struct era_entry *eras;
149 size_t num_eras;
150 int era_initialized;
152 const char **alt_digits;
153 const wchar_t **walt_digits;
154 int alt_digits_initialized;
155 int walt_digits_initialized;
159 /* LC_CTYPE specific:
160 Hardwired indices for standard wide character translation mappings. */
161 enum
163 __TOW_toupper = 0,
164 __TOW_tolower = 1
168 /* LC_CTYPE specific:
169 Access a wide character class with a single character index.
170 _ISCTYPE (c, desc) = iswctype (btowc (c), desc).
171 c must be an `unsigned char'. desc must be a nonzero wctype_t. */
172 #define _ISCTYPE(c, desc) \
173 (((((const uint32_t *) (desc)) - 8)[(c) >> 5] >> ((c) & 0x1f)) & 1)
175 /* Category name handling variables. */
176 #define CATNAMEMF(line) CATNAMEMF1 (line)
177 #define CATNAMEMF1(line) str##line
178 extern const union catnamestr_t
180 struct
182 #define DEFINE_CATEGORY(category, category_name, items, a) \
183 char CATNAMEMF (__LINE__)[sizeof (category_name)];
184 #include "categories.def"
185 #undef DEFINE_CATEGORY
187 char str[0];
188 } _nl_category_names attribute_hidden;
189 const uint8_t _nl_category_name_idxs[__LC_LAST] attribute_hidden;
190 extern const uint8_t _nl_category_name_sizes[__LC_LAST] attribute_hidden;
192 /* Name of the standard locales. */
193 extern const char _nl_C_name[] attribute_hidden;
194 extern const char _nl_POSIX_name[] attribute_hidden;
196 /* The standard codeset. */
197 extern const char _nl_C_codeset[] attribute_hidden;
199 /* This is the internal locale_t object that holds the global locale
200 controlled by calls to setlocale. A thread's TSD locale pointer
201 points to this when `uselocale (LC_GLOBAL_LOCALE)' is in effect. */
202 extern struct __locale_struct _nl_global_locale attribute_hidden;
204 /* This fetches the thread-local locale_t pointer, either one set with
205 uselocale or &_nl_global_locale. */
206 #define _NL_CURRENT_LOCALE ((__locale_t) __libc_tsd_get (LOCALE))
207 #include <bits/libc-tsd.h>
208 __libc_tsd_define (extern, LOCALE)
211 /* For static linking it is desireable to avoid always linking in the code
212 and data for every category when we can tell at link time that they are
213 unused. We can manage this playing some tricks with weak references.
214 But with thread-local locale settings, it becomes quite ungainly unless
215 we can use __thread variables. So only in that case do we attempt this. */
216 #if !defined SHARED && defined HAVE___THREAD && defined HAVE_WEAK_SYMBOLS
217 # include <tls.h>
218 # if USE_TLS
219 # define NL_CURRENT_INDIRECT 1
220 # endif
221 #endif
223 #ifdef NL_CURRENT_INDIRECT
225 /* For each category declare the thread-local variable for the current
226 locale data. This has an extra indirection so it points at the
227 __locales[CATEGORY] element in either _nl_global_locale or the current
228 locale object set by uselocale, which points at the actual data. The
229 reason for having these variables is so that references to particular
230 categories will link in the lc-CATEGORY.c module to define this symbol,
231 and we arrange that linking that module is what brings in all the code
232 associated with this category. */
233 #define DEFINE_CATEGORY(category, category_name, items, a) \
234 extern __thread struct locale_data *const *_nl_current_##category \
235 attribute_hidden attribute_tls_model_ie;
236 #include "categories.def"
237 #undef DEFINE_CATEGORY
239 /* Return a pointer to the current `struct locale_data' for CATEGORY. */
240 #define _NL_CURRENT_DATA(category) (*_nl_current_##category)
242 /* Extract the current CATEGORY locale's string for ITEM. */
243 #define _NL_CURRENT(category, item) \
244 ((*_nl_current_##category)->values[_NL_ITEM_INDEX (item)].string)
246 /* Extract the current CATEGORY locale's string for ITEM. */
247 #define _NL_CURRENT_WSTR(category, item) \
248 ((wchar_t *) (*_nl_current_##category)->values[_NL_ITEM_INDEX (item)].wstr)
250 /* Extract the current CATEGORY locale's word for ITEM. */
251 #define _NL_CURRENT_WORD(category, item) \
252 ((uint32_t) (*_nl_current_##category)->values[_NL_ITEM_INDEX (item)].word)
254 /* This is used in lc-CATEGORY.c to define _nl_current_CATEGORY. */
255 #define _NL_CURRENT_DEFINE(category) \
256 __thread struct locale_data *const *_nl_current_##category \
257 attribute_hidden = &_nl_global_locale.__locales[category]; \
258 asm (_NL_CURRENT_DEFINE_STRINGIFY (ASM_GLOBAL_DIRECTIVE) \
259 " " __SYMBOL_PREFIX "_nl_current_" #category "_used\n" \
260 _NL_CURRENT_DEFINE_ABS (_nl_current_##category##_used, 1));
261 #define _NL_CURRENT_DEFINE_STRINGIFY(x) _NL_CURRENT_DEFINE_STRINGIFY_1 (x)
262 #define _NL_CURRENT_DEFINE_STRINGIFY_1(x) #x
263 #ifdef HAVE_ASM_SET_DIRECTIVE
264 # define _NL_CURRENT_DEFINE_ABS(sym, val) ".set " #sym ", " #val
265 #else
266 # define _NL_CURRENT_DEFINE_ABS(sym, val) #sym " = " #val
267 #endif
269 #else
271 /* All categories are always loaded in the shared library, so there is no
272 point in having lots of separate symbols for linking. */
274 /* Return a pointer to the current `struct locale_data' for CATEGORY. */
275 # define _NL_CURRENT_DATA(category) \
276 (_NL_CURRENT_LOCALE->__locales[category])
278 /* Extract the current CATEGORY locale's string for ITEM. */
279 # define _NL_CURRENT(category, item) \
280 (_NL_CURRENT_DATA (category)->values[_NL_ITEM_INDEX (item)].string)
282 /* Extract the current CATEGORY locale's string for ITEM. */
283 # define _NL_CURRENT_WSTR(category, item) \
284 ((wchar_t *) _NL_CURRENT_DATA (category)->values[_NL_ITEM_INDEX (item)].wstr)
286 /* Extract the current CATEGORY locale's word for ITEM. */
287 # define _NL_CURRENT_WORD(category, item) \
288 ((uint32_t) _NL_CURRENT_DATA (category)->values[_NL_ITEM_INDEX (item)].word)
290 /* This is used in lc-CATEGORY.c to define _nl_current_CATEGORY. */
291 # define _NL_CURRENT_DEFINE(category) \
292 /* No per-category variable here. */
294 #endif
297 /* Default search path if no LOCPATH environment variable. */
298 extern const char _nl_default_locale_path[] attribute_hidden;
300 /* Load the locale data for CATEGORY from the file specified by *NAME.
301 If *NAME is "", use environment variables as specified by POSIX, and
302 fill in *NAME with the actual name used. If LOCALE_PATH is not null,
303 those directories are searched for the locale files. If it's null,
304 the locale archive is checked first and then _nl_default_locale_path
305 is searched for locale files. */
306 extern struct locale_data *_nl_find_locale (const char *locale_path,
307 size_t locale_path_len,
308 int category, const char **name)
309 internal_function attribute_hidden;
311 /* Try to load the file described by FILE. */
312 extern void _nl_load_locale (struct loaded_l10nfile *file, int category)
313 internal_function attribute_hidden;
315 /* Free all resource. */
316 extern void _nl_unload_locale (struct locale_data *locale)
317 internal_function attribute_hidden;
319 /* Free the locale and give back all memory if the usage count is one. */
320 extern void _nl_remove_locale (int locale, struct locale_data *data)
321 internal_function attribute_hidden;
323 /* Find the locale *NAMEP in the locale archive, and return the
324 internalized data structure for its CATEGORY data. If this locale has
325 already been loaded from the archive, just returns the existing data
326 structure. If successful, sets *NAMEP to point directly into the mapped
327 archive string table; that way, the next call can short-circuit strcmp. */
328 extern struct locale_data *_nl_load_locale_from_archive (int category,
329 const char **namep)
330 internal_function attribute_hidden;
332 /* Subroutine of setlocale's __libc_subfreeres hook. */
333 extern void _nl_archive_subfreeres (void) attribute_hidden;
335 /* Subroutine of gconv-db's __libc_subfreeres hook. */
336 extern void _nl_locale_subfreeres (void) attribute_hidden;
338 /* Validate the contents of a locale file and set up the in-core
339 data structure to point into the data. This leaves the `alloc'
340 and `name' fields uninitialized, for the caller to fill in.
341 If any bogons are detected in the data, this will refuse to
342 intern it, and return a null pointer instead. */
343 extern struct locale_data *_nl_intern_locale_data (int category,
344 const void *data,
345 size_t datasize)
346 internal_function attribute_hidden;
349 /* Return `era' entry which corresponds to TP. Used in strftime. */
350 extern struct era_entry *_nl_get_era_entry (const struct tm *tp,
351 struct locale_data *lc_time)
352 internal_function attribute_hidden;
354 /* Return `era' cnt'th entry . Used in strptime. */
355 extern struct era_entry *_nl_select_era_entry (int cnt,
356 struct locale_data *lc_time)
357 internal_function attribute_hidden;
359 /* Return `alt_digit' which corresponds to NUMBER. Used in strftime. */
360 extern const char *_nl_get_alt_digit (unsigned int number,
361 struct locale_data *lc_time)
362 internal_function attribute_hidden;
364 /* Similar, but now for wide characters. */
365 extern const wchar_t *_nl_get_walt_digit (unsigned int number,
366 struct locale_data *lc_time)
367 internal_function attribute_hidden;
369 /* Parse string as alternative digit and return numeric value. */
370 extern int _nl_parse_alt_digit (const char **strp,
371 struct locale_data *lc_time)
372 internal_function attribute_hidden;
374 /* Postload processing. */
375 extern void _nl_postload_ctype (void);
377 /* Functions used for the `private.cleanup' hook. */
378 extern void _nl_cleanup_time (struct locale_data *)
379 internal_function attribute_hidden;
382 #endif /* localeinfo.h */