Update.
[glibc.git] / intl / finddomain.c
blob7dd18633a9d8a90baaa796b0f03f90ef7d7bea7d
1 /* Handle list of needed message catalogs
2 Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Ulrich Drepper <drepper@gnu.org>, 1995.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <stdio.h>
26 #include <sys/types.h>
28 #if defined STDC_HEADERS || defined _LIBC
29 # include <stdlib.h>
30 #else
31 # ifdef HAVE_MALLOC_H
32 # include <malloc.h>
33 # else
34 void free ();
35 # endif
36 #endif
38 #if defined HAVE_STRING_H || defined _LIBC
39 # include <string.h>
40 #else
41 # include <strings.h>
42 # ifndef memcpy
43 # define memcpy(Dst, Src, Num) (bcopy (Src, Dst, Num), (Dst))
44 # endif
45 #endif
47 #if defined HAVE_UNISTD_H || defined _LIBC
48 # include <unistd.h>
49 #endif
51 #include "gettextP.h"
52 #ifdef _LIBC
53 # include <libintl.h>
54 #else
55 # include "libgnuintl.h"
56 #endif
58 /* @@ end of prolog @@ */
59 /* List of already loaded domains. */
60 static struct loaded_l10nfile *_nl_loaded_domains;
63 /* Return a data structure describing the message catalog described by
64 the DOMAINNAME and CATEGORY parameters with respect to the currently
65 established bindings. */
66 struct loaded_l10nfile *
67 internal_function
68 _nl_find_domain (dirname, locale, domainname, domainbinding)
69 const char *dirname;
70 char *locale;
71 const char *domainname;
72 struct binding *domainbinding;
74 struct loaded_l10nfile *retval;
75 const char *language;
76 const char *modifier;
77 const char *territory;
78 const char *codeset;
79 const char *normalized_codeset;
80 const char *special;
81 const char *sponsor;
82 const char *revision;
83 const char *alias_value;
84 int mask;
86 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
88 language[_territory[.codeset]][@modifier]
90 and six parts for the CEN syntax:
92 language[_territory][+audience][+special][,[sponsor][_revision]]
94 Beside the first part all of them are allowed to be missing. If
95 the full specified locale is not found, the less specific one are
96 looked for. The various parts will be stripped off according to
97 the following order:
98 (1) revision
99 (2) sponsor
100 (3) special
101 (4) codeset
102 (5) normalized codeset
103 (6) territory
104 (7) audience/modifier
107 /* If we have already tested for this locale entry there has to
108 be one data set in the list of loaded domains. */
109 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
110 strlen (dirname) + 1, 0, locale, NULL, NULL,
111 NULL, NULL, NULL, NULL, NULL, domainname, 0);
112 if (retval != NULL)
114 /* We know something about this locale. */
115 int cnt;
117 if (retval->decided == 0)
118 _nl_load_domain (retval, domainbinding);
120 if (retval->data != NULL)
121 return retval;
123 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
125 if (retval->successor[cnt]->decided == 0)
126 _nl_load_domain (retval->successor[cnt], domainbinding);
128 if (retval->successor[cnt]->data != NULL)
129 break;
131 return cnt >= 0 ? retval : NULL;
132 /* NOTREACHED */
135 /* See whether the locale value is an alias. If yes its value
136 *overwrites* the alias name. No test for the original value is
137 done. */
138 alias_value = _nl_expand_alias (locale);
139 if (alias_value != NULL)
141 #if defined _LIBC || defined HAVE_STRDUP
142 locale = strdup (alias_value);
143 if (locale == NULL)
144 return NULL;
145 #else
146 size_t len = strlen (alias_value) + 1;
147 locale = (char *) malloc (len);
148 if (locale == NULL)
149 return NULL;
151 memcpy (locale, alias_value, len);
152 #endif
155 /* Now we determine the single parts of the locale name. First
156 look for the language. Termination symbols are `_' and `@' if
157 we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
158 mask = _nl_explode_name (locale, &language, &modifier, &territory,
159 &codeset, &normalized_codeset, &special,
160 &sponsor, &revision);
162 /* Create all possible locale entries which might be interested in
163 generalization. */
164 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
165 strlen (dirname) + 1, mask, language, territory,
166 codeset, normalized_codeset, modifier, special,
167 sponsor, revision, domainname, 1);
168 if (retval == NULL)
169 /* This means we are out of core. */
170 return NULL;
172 if (retval->decided == 0)
173 _nl_load_domain (retval, domainbinding);
174 if (retval->data == NULL)
176 int cnt;
177 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
179 if (retval->successor[cnt]->decided == 0)
180 _nl_load_domain (retval->successor[cnt], domainbinding);
181 if (retval->successor[cnt]->data != NULL)
182 break;
186 /* The room for an alias was dynamically allocated. Free it now. */
187 if (alias_value != NULL)
188 free (locale);
190 /* The space for normalized_codeset is dynamically allocated. Free it. */
191 if (mask & XPG_NORM_CODESET)
192 free ((void *) normalized_codeset);
194 return retval;
198 #ifdef _LIBC
199 static void __attribute__ ((unused))
200 free_mem (void)
202 struct loaded_l10nfile *runp = _nl_loaded_domains;
204 while (runp != NULL)
206 struct loaded_l10nfile *here = runp;
207 if (runp->data != NULL)
208 _nl_unload_domain ((struct loaded_domain *) runp->data);
209 runp = runp->next;
210 free ((char *) here->filename);
211 free (here);
215 text_set_element (__libc_subfreeres, free_mem);
216 #endif