Merged changes made for version 4.1.20 onto the trunk
[findutils.git] / intl / finddomain.c
blob7bb6d0d01b4658093022ea6733e7a2524d499ae2
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 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 # include <gnulib/config.h>
21 # undef VERSION
22 # undef PACKAGE_VERSION
23 # undef PACKAGE_TARNAME
24 # undef PACKAGE_STRING
25 # include <config.h>
26 #endif
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #if defined HAVE_UNISTD_H || defined _LIBC
34 # include <unistd.h>
35 #endif
37 #include "gettextP.h"
38 #ifdef _LIBC
39 # include <libintl.h>
40 #else
41 # include "libgnuintl.h"
42 #endif
44 /* @@ end of prolog @@ */
45 /* List of already loaded domains. */
46 static struct loaded_l10nfile *_nl_loaded_domains;
49 /* Return a data structure describing the message catalog described by
50 the DOMAINNAME and CATEGORY parameters with respect to the currently
51 established bindings. */
52 struct loaded_l10nfile *
53 internal_function
54 _nl_find_domain (dirname, locale, domainname, domainbinding)
55 const char *dirname;
56 char *locale;
57 const char *domainname;
58 struct binding *domainbinding;
60 struct loaded_l10nfile *retval;
61 const char *language;
62 const char *modifier;
63 const char *territory;
64 const char *codeset;
65 const char *normalized_codeset;
66 const char *special;
67 const char *sponsor;
68 const char *revision;
69 const char *alias_value;
70 int mask;
72 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
74 language[_territory[.codeset]][@modifier]
76 and six parts for the CEN syntax:
78 language[_territory][+audience][+special][,[sponsor][_revision]]
80 Beside the first part all of them are allowed to be missing. If
81 the full specified locale is not found, the less specific one are
82 looked for. The various parts will be stripped off according to
83 the following order:
84 (1) revision
85 (2) sponsor
86 (3) special
87 (4) codeset
88 (5) normalized codeset
89 (6) territory
90 (7) audience/modifier
93 /* If we have already tested for this locale entry there has to
94 be one data set in the list of loaded domains. */
95 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
96 strlen (dirname) + 1, 0, locale, NULL, NULL,
97 NULL, NULL, NULL, NULL, NULL, domainname, 0);
98 if (retval != NULL)
100 /* We know something about this locale. */
101 int cnt;
103 if (retval->decided == 0)
104 _nl_load_domain (retval, domainbinding);
106 if (retval->data != NULL)
107 return retval;
109 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
111 if (retval->successor[cnt]->decided == 0)
112 _nl_load_domain (retval->successor[cnt], domainbinding);
114 if (retval->successor[cnt]->data != NULL)
115 break;
117 return cnt >= 0 ? retval : NULL;
118 /* NOTREACHED */
121 /* See whether the locale value is an alias. If yes its value
122 *overwrites* the alias name. No test for the original value is
123 done. */
124 alias_value = _nl_expand_alias (locale);
125 if (alias_value != NULL)
127 #if defined _LIBC || defined HAVE_STRDUP
128 locale = strdup (alias_value);
129 if (locale == NULL)
130 return NULL;
131 #else
132 size_t len = strlen (alias_value) + 1;
133 locale = (char *) malloc (len);
134 if (locale == NULL)
135 return NULL;
137 memcpy (locale, alias_value, len);
138 #endif
141 /* Now we determine the single parts of the locale name. First
142 look for the language. Termination symbols are `_' and `@' if
143 we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
144 mask = _nl_explode_name (locale, &language, &modifier, &territory,
145 &codeset, &normalized_codeset, &special,
146 &sponsor, &revision);
148 /* Create all possible locale entries which might be interested in
149 generalization. */
150 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
151 strlen (dirname) + 1, mask, language, territory,
152 codeset, normalized_codeset, modifier, special,
153 sponsor, revision, domainname, 1);
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 static void __attribute__ ((unused))
186 free_mem (void)
188 struct loaded_l10nfile *runp = _nl_loaded_domains;
190 while (runp != NULL)
192 struct loaded_l10nfile *here = runp;
193 if (runp->data != NULL)
194 _nl_unload_domain ((struct loaded_domain *) runp->data);
195 runp = runp->next;
196 free ((char *) here->filename);
197 free (here);
201 text_set_element (__libc_subfreeres, free_mem);
202 #endif