Update.
[glibc.git] / intl / finddomain.c
bloba3083b10e21cd2a279437a5f6c3d26321cec0151
1 /* Handle list of needed message catalogs
2 Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
3 Written by Ulrich Drepper <drepper@gnu.org>, 1995.
5 This file is part of the GNU C Library. Its master source is NOT part of
6 the C library, however.
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
13 The GNU C Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with the GNU C Library; see the file COPYING.LIB. If not,
20 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
27 #include <ctype.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <sys/types.h>
32 #if defined STDC_HEADERS || defined _LIBC
33 # include <stdlib.h>
34 #else
35 # ifdef HAVE_MALLOC_H
36 # include <malloc.h>
37 # else
38 void free ();
39 # endif
40 #endif
42 #if defined HAVE_STRING_H || defined _LIBC
43 # include <string.h>
44 #else
45 # include <strings.h>
46 # ifndef memcpy
47 # define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
48 # endif
49 #endif
51 #if defined HAVE_UNISTD_H || defined _LIBC
52 # include <unistd.h>
53 #endif
55 #include "gettext.h"
56 #include "gettextP.h"
57 #ifdef _LIBC
58 # include <libintl.h>
59 #else
60 # include "libgettext.h"
61 #endif
63 /* @@ end of prolog @@ */
64 /* List of already loaded domains. */
65 static struct loaded_l10nfile *_nl_loaded_domains;
68 /* Return a data structure describing the message catalog described by
69 the DOMAINNAME and CATEGORY parameters with respect to the currently
70 established bindings. */
71 struct loaded_l10nfile *
72 internal_function
73 _nl_find_domain (dirname, locale, domainname)
74 const char *dirname;
75 char *locale;
76 const char *domainname;
78 struct loaded_l10nfile *retval;
79 const char *language;
80 const char *modifier;
81 const char *territory;
82 const char *codeset;
83 const char *normalized_codeset;
84 const char *special;
85 const char *sponsor;
86 const char *revision;
87 const char *alias_value;
88 int mask;
90 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
92 language[_territory[.codeset]][@modifier]
94 and six parts for the CEN syntax:
96 language[_territory][+audience][+special][,[sponsor][_revision]]
98 Beside the first part all of them are allowed to be missing. If
99 the full specified locale is not found, the less specific one are
100 looked for. The various parts will be stripped off according to
101 the following order:
102 (1) revision
103 (2) sponsor
104 (3) special
105 (4) codeset
106 (5) normalized codeset
107 (6) territory
108 (7) audience/modifier
111 /* If we have already tested for this locale entry there has to
112 be one data set in the list of loaded domains. */
113 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
114 strlen (dirname) + 1, 0, locale, NULL, NULL,
115 NULL, NULL, NULL, NULL, NULL, domainname, 0);
116 if (retval != NULL)
118 /* We know something about this locale. */
119 int cnt;
121 if (retval->decided == 0)
122 _nl_load_domain (retval);
124 if (retval->data != NULL)
125 return retval;
127 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
129 if (retval->successor[cnt]->decided == 0)
130 _nl_load_domain (retval->successor[cnt]);
132 if (retval->successor[cnt]->data != NULL)
133 break;
135 return cnt >= 0 ? retval : NULL;
136 /* NOTREACHED */
139 /* See whether the locale value is an alias. If yes its value
140 *overwrites* the alias name. No test for the original value is
141 done. */
142 alias_value = _nl_expand_alias (locale);
143 if (alias_value != NULL)
145 #if defined _LIBC || defined HAVE_STRDUP
146 locale = strdup (alias_value);
147 if (locale == NULL)
148 return NULL;
149 #else
150 size_t len = strlen (alias_value) + 1;
151 locale = (char *) malloc (len);
152 if (locale == NULL)
153 return NULL;
155 memcpy (locale, alias_value, len);
156 #endif
159 /* Now we determine the single parts of the locale name. First
160 look for the language. Termination symbols are `_' and `@' if
161 we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
162 mask = _nl_explode_name (locale, &language, &modifier, &territory,
163 &codeset, &normalized_codeset, &special,
164 &sponsor, &revision);
166 /* Create all possible locale entries which might be interested in
167 generalization. */
168 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
169 strlen (dirname) + 1, mask, language, territory,
170 codeset, normalized_codeset, modifier, special,
171 sponsor, revision, domainname, 1);
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 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