Use i386 bits/wchar.h for i386 and x86-64
[glibc.git] / intl / finddomain.c
blobfec31569850512345d724abb69e37ccbf2e05e9b
1 /* Handle list of needed message catalogs
2 Copyright (C) 1995-1999, 2000, 2001, 2002, 2004, 2006, 2007
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Written by Ulrich Drepper <drepper@gnu.org>, 1995.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #if defined HAVE_UNISTD_H || defined _LIBC
31 # include <unistd.h>
32 #endif
34 #include "gettextP.h"
35 #ifdef _LIBC
36 # include <libintl.h>
37 # include <bits/libc-lock.h>
38 #else
39 # include "libgnuintl.h"
40 #endif
42 /* @@ end of prolog @@ */
43 /* List of already loaded domains. */
44 static struct loaded_l10nfile *_nl_loaded_domains;
47 /* Return a data structure describing the message catalog described by
48 the DOMAINNAME and CATEGORY parameters with respect to the currently
49 established bindings. */
50 struct loaded_l10nfile *
51 internal_function
52 _nl_find_domain (dirname, locale, domainname, domainbinding)
53 const char *dirname;
54 char *locale;
55 const char *domainname;
56 struct binding *domainbinding;
58 struct loaded_l10nfile *retval;
59 const char *language;
60 const char *modifier;
61 const char *territory;
62 const char *codeset;
63 const char *normalized_codeset;
64 const char *alias_value;
65 int mask;
67 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
69 language[_territory[.codeset]][@modifier]
71 Beside the first part all of them are allowed to be missing. If
72 the full specified locale is not found, the less specific one are
73 looked for. The various parts will be stripped off according to
74 the following order:
75 (1) codeset
76 (2) normalized codeset
77 (3) territory
78 (4) modifier
81 /* We need to protect modifying the _NL_LOADED_DOMAINS data. */
82 __libc_rwlock_define_initialized (static, lock);
83 __libc_rwlock_rdlock (lock);
85 /* If we have already tested for this locale entry there has to
86 be one data set in the list of loaded domains. */
87 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
88 strlen (dirname) + 1, 0, locale, NULL, NULL,
89 NULL, NULL, domainname, 0);
90 __libc_rwlock_unlock (lock);
92 if (retval != NULL)
94 /* We know something about this locale. */
95 int cnt;
97 if (retval->decided <= 0)
98 _nl_load_domain (retval, domainbinding);
100 if (retval->data != NULL)
101 return retval;
103 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
105 if (retval->successor[cnt]->decided <= 0)
106 _nl_load_domain (retval->successor[cnt], domainbinding);
108 if (retval->successor[cnt]->data != NULL)
109 break;
112 return retval;
113 /* NOTREACHED */
116 /* See whether the locale value is an alias. If yes its value
117 *overwrites* the alias name. No test for the original value is
118 done. */
119 alias_value = _nl_expand_alias (locale);
120 if (alias_value != NULL)
121 locale = strdupa (alias_value);
123 /* Now we determine the single parts of the locale name. First
124 look for the language. Termination symbols are `_' and `@' if
125 we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
126 mask = _nl_explode_name (locale, &language, &modifier, &territory,
127 &codeset, &normalized_codeset);
128 if (mask == -1)
129 /* This means we are out of core. */
130 return NULL;
132 /* We need to protect modifying the _NL_LOADED_DOMAINS data. */
133 __libc_rwlock_wrlock (lock);
135 /* Create all possible locale entries which might be interested in
136 generalization. */
137 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
138 strlen (dirname) + 1, mask, language, territory,
139 codeset, normalized_codeset, modifier,
140 domainname, 1);
141 __libc_rwlock_unlock (lock);
143 if (retval == NULL)
144 /* This means we are out of core. */
145 goto out;
147 if (retval->decided <= 0)
148 _nl_load_domain (retval, domainbinding);
149 if (retval->data == NULL)
151 int cnt;
152 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
154 if (retval->successor[cnt]->decided <= 0)
155 _nl_load_domain (retval->successor[cnt], domainbinding);
156 if (retval->successor[cnt]->data != NULL)
157 break;
161 out:
162 /* The space for normalized_codeset is dynamically allocated. Free it. */
163 if (mask & XPG_NORM_CODESET)
164 free ((void *) normalized_codeset);
166 return retval;
170 #ifdef _LIBC
171 /* This is called from iconv/gconv_db.c's free_mem, as locales must
172 be freed before freeing gconv steps arrays. */
173 void __libc_freeres_fn_section
174 _nl_finddomain_subfreeres ()
176 struct loaded_l10nfile *runp = _nl_loaded_domains;
178 while (runp != NULL)
180 struct loaded_l10nfile *here = runp;
181 if (runp->data != NULL)
182 _nl_unload_domain ((struct loaded_domain *) runp->data);
183 runp = runp->next;
184 free ((char *) here->filename);
185 free (here);
188 #endif