blob: 4992a8c9e3352ec6c52086d05110e663753348c1 [file] [log] [blame]
Theodore Ts'o5bc5a892000-02-08 20:20:46 +00001/* Handle list of needed message catalogs
Theodore Ts'ob0cacab2004-11-30 19:00:19 -05002 Copyright (C) 1995-1999, 2000-2001, 2003 Free Software Foundation, Inc.
Theodore Ts'oa04eba32003-05-03 16:35:17 -04003 Written by Ulrich Drepper <drepper@gnu.org>, 1995.
Theodore Ts'o5bc5a892000-02-08 20:20:46 +00004
Theodore Ts'oa04eba32003-05-03 16:35:17 -04005 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published
7 by the Free Software Foundation; either version 2, or (at your option)
Theodore Ts'o5bc5a892000-02-08 20:20:46 +00008 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
Theodore Ts'oa04eba32003-05-03 16:35:17 -040012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
Theodore Ts'o5bc5a892000-02-08 20:20:46 +000014
Theodore Ts'oa04eba32003-05-03 16:35:17 -040015 You should have received a copy of the GNU Library General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 USA. */
Theodore Ts'o5bc5a892000-02-08 20:20:46 +000019
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
Theodore Ts'o5bc5a892000-02-08 20:20:46 +000024#include <stdio.h>
25#include <sys/types.h>
Theodore Ts'oa04eba32003-05-03 16:35:17 -040026#include <stdlib.h>
27#include <string.h>
Theodore Ts'o5bc5a892000-02-08 20:20:46 +000028
29#if defined HAVE_UNISTD_H || defined _LIBC
30# include <unistd.h>
31#endif
32
Theodore Ts'o5bc5a892000-02-08 20:20:46 +000033#include "gettextP.h"
34#ifdef _LIBC
35# include <libintl.h>
36#else
Theodore Ts'oa04eba32003-05-03 16:35:17 -040037# include "libgnuintl.h"
Theodore Ts'o5bc5a892000-02-08 20:20:46 +000038#endif
39
40/* @@ end of prolog @@ */
41/* List of already loaded domains. */
42static struct loaded_l10nfile *_nl_loaded_domains;
43
44
45/* Return a data structure describing the message catalog described by
46 the DOMAINNAME and CATEGORY parameters with respect to the currently
47 established bindings. */
48struct loaded_l10nfile *
49internal_function
Theodore Ts'ob0cacab2004-11-30 19:00:19 -050050_nl_find_domain (const char *dirname, char *locale,
51 const char *domainname, struct binding *domainbinding)
Theodore Ts'o5bc5a892000-02-08 20:20:46 +000052{
53 struct loaded_l10nfile *retval;
54 const char *language;
55 const char *modifier;
56 const char *territory;
57 const char *codeset;
58 const char *normalized_codeset;
59 const char *special;
60 const char *sponsor;
61 const char *revision;
62 const char *alias_value;
63 int mask;
64
65 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
66
67 language[_territory[.codeset]][@modifier]
68
69 and six parts for the CEN syntax:
70
71 language[_territory][+audience][+special][,[sponsor][_revision]]
72
73 Beside the first part all of them are allowed to be missing. If
74 the full specified locale is not found, the less specific one are
75 looked for. The various parts will be stripped off according to
76 the following order:
77 (1) revision
78 (2) sponsor
79 (3) special
80 (4) codeset
81 (5) normalized codeset
82 (6) territory
83 (7) audience/modifier
84 */
85
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, NULL, NULL, NULL, domainname, 0);
91 if (retval != NULL)
92 {
93 /* We know something about this locale. */
94 int cnt;
95
96 if (retval->decided == 0)
Theodore Ts'oa04eba32003-05-03 16:35:17 -040097 _nl_load_domain (retval, domainbinding);
Theodore Ts'o5bc5a892000-02-08 20:20:46 +000098
99 if (retval->data != NULL)
100 return retval;
101
102 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
103 {
104 if (retval->successor[cnt]->decided == 0)
Theodore Ts'oa04eba32003-05-03 16:35:17 -0400105 _nl_load_domain (retval->successor[cnt], domainbinding);
Theodore Ts'o5bc5a892000-02-08 20:20:46 +0000106
107 if (retval->successor[cnt]->data != NULL)
108 break;
109 }
110 return cnt >= 0 ? retval : NULL;
111 /* NOTREACHED */
112 }
113
114 /* See whether the locale value is an alias. If yes its value
115 *overwrites* the alias name. No test for the original value is
116 done. */
117 alias_value = _nl_expand_alias (locale);
118 if (alias_value != NULL)
119 {
120#if defined _LIBC || defined HAVE_STRDUP
121 locale = strdup (alias_value);
122 if (locale == NULL)
123 return NULL;
124#else
125 size_t len = strlen (alias_value) + 1;
126 locale = (char *) malloc (len);
127 if (locale == NULL)
128 return NULL;
129
130 memcpy (locale, alias_value, len);
131#endif
132 }
133
134 /* Now we determine the single parts of the locale name. First
135 look for the language. Termination symbols are `_' and `@' if
136 we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
137 mask = _nl_explode_name (locale, &language, &modifier, &territory,
138 &codeset, &normalized_codeset, &special,
139 &sponsor, &revision);
140
141 /* Create all possible locale entries which might be interested in
142 generalization. */
143 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
144 strlen (dirname) + 1, mask, language, territory,
145 codeset, normalized_codeset, modifier, special,
146 sponsor, revision, domainname, 1);
147 if (retval == NULL)
148 /* This means we are out of core. */
149 return NULL;
150
151 if (retval->decided == 0)
Theodore Ts'oa04eba32003-05-03 16:35:17 -0400152 _nl_load_domain (retval, domainbinding);
Theodore Ts'o5bc5a892000-02-08 20:20:46 +0000153 if (retval->data == NULL)
154 {
155 int cnt;
156 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
157 {
158 if (retval->successor[cnt]->decided == 0)
Theodore Ts'oa04eba32003-05-03 16:35:17 -0400159 _nl_load_domain (retval->successor[cnt], domainbinding);
Theodore Ts'o5bc5a892000-02-08 20:20:46 +0000160 if (retval->successor[cnt]->data != NULL)
161 break;
162 }
163 }
164
165 /* The room for an alias was dynamically allocated. Free it now. */
166 if (alias_value != NULL)
167 free (locale);
168
Theodore Ts'oa04eba32003-05-03 16:35:17 -0400169 /* The space for normalized_codeset is dynamically allocated. Free it. */
170 if (mask & XPG_NORM_CODESET)
171 free ((void *) normalized_codeset);
172
Theodore Ts'o5bc5a892000-02-08 20:20:46 +0000173 return retval;
174}
175
176
177#ifdef _LIBC
Theodore Ts'ob0cacab2004-11-30 19:00:19 -0500178libc_freeres_fn (free_mem)
Theodore Ts'o5bc5a892000-02-08 20:20:46 +0000179{
180 struct loaded_l10nfile *runp = _nl_loaded_domains;
181
182 while (runp != NULL)
183 {
184 struct loaded_l10nfile *here = runp;
185 if (runp->data != NULL)
186 _nl_unload_domain ((struct loaded_domain *) runp->data);
187 runp = runp->next;
Theodore Ts'oa04eba32003-05-03 16:35:17 -0400188 free ((char *) here->filename);
Theodore Ts'o5bc5a892000-02-08 20:20:46 +0000189 free (here);
190 }
191}
Theodore Ts'o5bc5a892000-02-08 20:20:46 +0000192#endif