* sysdeps/unix/sysv/linux/jmp-unwind.c: Include pthreadP.h instead
[glibc.git] / intl / finddomain.c
blob39e54755d249b22eb7fb8f73c5d2e1c9de79a85e
1 /* Handle list of needed message catalogs
2 Copyright (C) 1995-1999, 2000, 2001, 2002, 2004
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, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #if defined HAVE_UNISTD_H || defined _LIBC
32 # include <unistd.h>
33 #endif
35 #include "gettextP.h"
36 #ifdef _LIBC
37 # include <libintl.h>
38 # include <bits/libc-lock.h>
39 #else
40 # include "libgnuintl.h"
41 #endif
43 /* @@ end of prolog @@ */
44 /* List of already loaded domains. */
45 static struct loaded_l10nfile *_nl_loaded_domains;
48 /* Return a data structure describing the message catalog described by
49 the DOMAINNAME and CATEGORY parameters with respect to the currently
50 established bindings. */
51 struct loaded_l10nfile *
52 internal_function
53 _nl_find_domain (dirname, locale, domainname, domainbinding)
54 const char *dirname;
55 char *locale;
56 const char *domainname;
57 struct binding *domainbinding;
59 struct loaded_l10nfile *retval;
60 const char *language;
61 const char *modifier;
62 const char *territory;
63 const char *codeset;
64 const char *normalized_codeset;
65 const char *alias_value;
66 int mask;
68 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
70 language[_territory[.codeset]][@modifier]
72 Beside the first part all of them are allowed to be missing. If
73 the full specified locale is not found, the less specific one are
74 looked for. The various parts will be stripped off according to
75 the following order:
76 (1) codeset
77 (2) normalized codeset
78 (3) territory
79 (4) modifier
82 /* We need to protect modifying the _NL_LOADED_DOMAINS data. */
83 __libc_rwlock_define_initialized (static, lock);
84 __libc_rwlock_rdlock (lock);
86 /* If we have already tested for this locale entry there has to
87 be one data set in the list of loaded domains. */
88 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
89 strlen (dirname) + 1, 0, locale, NULL, NULL,
90 NULL, NULL, domainname, 0);
91 __libc_rwlock_unlock (lock);
93 if (retval != NULL)
95 /* We know something about this locale. */
96 int cnt;
98 if (retval->decided <= 0)
99 _nl_load_domain (retval, domainbinding);
101 if (retval->data != NULL)
102 return retval;
104 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
106 if (retval->successor[cnt]->decided <= 0)
107 _nl_load_domain (retval->successor[cnt], domainbinding);
109 if (retval->successor[cnt]->data != NULL)
110 break;
113 return cnt >= 0 ? retval : NULL;
114 /* NOTREACHED */
117 /* See whether the locale value is an alias. If yes its value
118 *overwrites* the alias name. No test for the original value is
119 done. */
120 alias_value = _nl_expand_alias (locale);
121 if (alias_value != NULL)
123 #if defined _LIBC || defined HAVE_STRDUP
124 locale = strdup (alias_value);
125 if (locale == NULL)
126 return NULL;
127 #else
128 size_t len = strlen (alias_value) + 1;
129 locale = (char *) malloc (len);
130 if (locale == NULL)
131 return NULL;
133 memcpy (locale, alias_value, len);
134 #endif
137 /* Now we determine the single parts of the locale name. First
138 look for the language. Termination symbols are `_' and `@' if
139 we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
140 mask = _nl_explode_name (locale, &language, &modifier, &territory,
141 &codeset, &normalized_codeset);
143 /* We need to protect modifying the _NL_LOADED_DOMAINS data. */
144 __libc_rwlock_wrlock (lock);
146 /* Create all possible locale entries which might be interested in
147 generalization. */
148 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
149 strlen (dirname) + 1, mask, language, territory,
150 codeset, normalized_codeset, modifier,
151 domainname, 1);
152 __libc_rwlock_unlock (lock);
154 if (retval == NULL)
155 /* This means we are out of core. */
156 return NULL;
158 if (retval->decided <= 0)
159 _nl_load_domain (retval, domainbinding);
160 if (retval->data == NULL)
162 int cnt;
163 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
165 if (retval->successor[cnt]->decided <= 0)
166 _nl_load_domain (retval->successor[cnt], domainbinding);
167 if (retval->successor[cnt]->data != NULL)
168 break;
172 /* The room for an alias was dynamically allocated. Free it now. */
173 if (alias_value != NULL)
174 free (locale);
176 /* The space for normalized_codeset is dynamically allocated. Free it. */
177 if (mask & XPG_NORM_CODESET)
178 free ((void *) normalized_codeset);
180 return retval;
184 #ifdef _LIBC
185 /* This is called from iconv/gconv_db.c's free_mem, as locales must
186 be freed before freeing gconv steps arrays. */
187 void __libc_freeres_fn_section
188 _nl_finddomain_subfreeres ()
190 struct loaded_l10nfile *runp = _nl_loaded_domains;
192 while (runp != NULL)
194 struct loaded_l10nfile *here = runp;
195 if (runp->data != NULL)
196 _nl_unload_domain ((struct loaded_domain *) runp->data);
197 runp = runp->next;
198 free ((char *) here->filename);
199 free (here);
202 #endif