Update.
[glibc.git] / intl / finddomain.c
blob29bbcf95801d969c0d3735638520c3d0925d986d
1 /* Handle list of needed message catalogs
2 Copyright (C) 1995-1999, 2000, 2001 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 <stdio.h>
25 #include <sys/types.h>
27 #if defined STDC_HEADERS || defined _LIBC
28 # include <stdlib.h>
29 #else
30 # ifdef HAVE_MALLOC_H
31 # include <malloc.h>
32 # else
33 void free ();
34 # endif
35 #endif
37 #if defined HAVE_STRING_H || defined _LIBC
38 # include <string.h>
39 #else
40 # include <strings.h>
41 # ifndef memcpy
42 # define memcpy(Dst, Src, Num) (bcopy (Src, Dst, Num), (Dst))
43 # endif
44 #endif
46 #if defined HAVE_UNISTD_H || defined _LIBC
47 # include <unistd.h>
48 #endif
50 #include "gettextP.h"
51 #ifdef _LIBC
52 # include <libintl.h>
53 #else
54 # include "libgnuintl.h"
55 #endif
57 /* @@ end of prolog @@ */
58 /* List of already loaded domains. */
59 static struct loaded_l10nfile *_nl_loaded_domains;
62 /* Return a data structure describing the message catalog described by
63 the DOMAINNAME and CATEGORY parameters with respect to the currently
64 established bindings. */
65 struct loaded_l10nfile *
66 internal_function
67 _nl_find_domain (dirname, locale, domainname, domainbinding)
68 const char *dirname;
69 char *locale;
70 const char *domainname;
71 struct binding *domainbinding;
73 struct loaded_l10nfile *retval;
74 const char *language;
75 const char *modifier;
76 const char *territory;
77 const char *codeset;
78 const char *normalized_codeset;
79 const char *special;
80 const char *sponsor;
81 const char *revision;
82 const char *alias_value;
83 int mask;
85 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
87 language[_territory[.codeset]][@modifier]
89 and six parts for the CEN syntax:
91 language[_territory][+audience][+special][,[sponsor][_revision]]
93 Beside the first part all of them are allowed to be missing. If
94 the full specified locale is not found, the less specific one are
95 looked for. The various parts will be stripped off according to
96 the following order:
97 (1) revision
98 (2) sponsor
99 (3) special
100 (4) codeset
101 (5) normalized codeset
102 (6) territory
103 (7) audience/modifier
106 /* If we have already tested for this locale entry there has to
107 be one data set in the list of loaded domains. */
108 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
109 strlen (dirname) + 1, 0, locale, NULL, NULL,
110 NULL, NULL, NULL, NULL, NULL, domainname, 0);
111 if (retval != NULL)
113 /* We know something about this locale. */
114 int cnt;
116 if (retval->decided == 0)
117 _nl_load_domain (retval, domainbinding);
119 if (retval->data != NULL)
120 return retval;
122 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
124 if (retval->successor[cnt]->decided == 0)
125 _nl_load_domain (retval->successor[cnt], domainbinding);
127 if (retval->successor[cnt]->data != NULL)
128 break;
130 return cnt >= 0 ? retval : NULL;
131 /* NOTREACHED */
134 /* See whether the locale value is an alias. If yes its value
135 *overwrites* the alias name. No test for the original value is
136 done. */
137 alias_value = _nl_expand_alias (locale);
138 if (alias_value != NULL)
140 #if defined _LIBC || defined HAVE_STRDUP
141 locale = strdup (alias_value);
142 if (locale == NULL)
143 return NULL;
144 #else
145 size_t len = strlen (alias_value) + 1;
146 locale = (char *) malloc (len);
147 if (locale == NULL)
148 return NULL;
150 memcpy (locale, alias_value, len);
151 #endif
154 /* Now we determine the single parts of the locale name. First
155 look for the language. Termination symbols are `_' and `@' if
156 we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
157 mask = _nl_explode_name (locale, &language, &modifier, &territory,
158 &codeset, &normalized_codeset, &special,
159 &sponsor, &revision);
161 /* Create all possible locale entries which might be interested in
162 generalization. */
163 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
164 strlen (dirname) + 1, mask, language, territory,
165 codeset, normalized_codeset, modifier, special,
166 sponsor, revision, domainname, 1);
167 if (retval == NULL)
168 /* This means we are out of core. */
169 return NULL;
171 if (retval->decided == 0)
172 _nl_load_domain (retval, domainbinding);
173 if (retval->data == NULL)
175 int cnt;
176 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
178 if (retval->successor[cnt]->decided == 0)
179 _nl_load_domain (retval->successor[cnt], domainbinding);
180 if (retval->successor[cnt]->data != NULL)
181 break;
185 /* The room for an alias was dynamically allocated. Free it now. */
186 if (alias_value != NULL)
187 free (locale);
189 /* The space for normalized_codeset is dynamically allocated. Free it. */
190 if (mask & XPG_NORM_CODESET)
191 free ((void *) normalized_codeset);
193 return retval;
197 #ifdef _LIBC
198 static void __attribute__ ((unused))
199 free_mem (void)
201 struct loaded_l10nfile *runp = _nl_loaded_domains;
203 while (runp != NULL)
205 struct loaded_l10nfile *here = runp;
206 if (runp->data != NULL)
207 _nl_unload_domain ((struct loaded_domain *) runp->data);
208 runp = runp->next;
209 free ((char *) here->filename);
210 free (here);
214 text_set_element (__libc_subfreeres, free_mem);
215 #endif