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