Update.
[glibc.git] / intl / finddomain.c
blob0b697ef053c7163936d47c91c179b231f53c2ed4
1 /* Handle list of needed message catalogs
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 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
50 #if !HAVE_STRCHR && !defined _LIBC
51 # ifndef strchr
52 # define strchr index
53 # endif
54 #endif
56 #if defined HAVE_UNISTD_H || defined _LIBC
57 # include <unistd.h>
58 #endif
60 #include "gettext.h"
61 #include "gettextP.h"
62 #ifdef _LIBC
63 # include <libintl.h>
64 #else
65 # include "libgettext.h"
66 #endif
68 /* @@ end of prolog @@ */
69 /* List of already loaded domains. */
70 static struct loaded_l10nfile *_nl_loaded_domains;
73 /* Return a data structure describing the message catalog described by
74 the DOMAINNAME and CATEGORY parameters with respect to the currently
75 established bindings. */
76 struct loaded_l10nfile *
77 internal_function
78 _nl_find_domain (dirname, locale, domainname)
79 const char *dirname;
80 char *locale;
81 const char *domainname;
83 struct loaded_l10nfile *retval;
84 const char *language;
85 const char *modifier;
86 const char *territory;
87 const char *codeset;
88 const char *normalized_codeset;
89 const char *special;
90 const char *sponsor;
91 const char *revision;
92 const char *alias_value;
93 int mask;
95 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
97 language[_territory[.codeset]][@modifier]
99 and six parts for the CEN syntax:
101 language[_territory][+audience][+special][,[sponsor][_revision]]
103 Beside the first part all of them are allowed to be missing. If
104 the full specified locale is not found, the less specific one are
105 looked for. The various parts will be stripped off according to
106 the following order:
107 (1) revision
108 (2) sponsor
109 (3) special
110 (4) codeset
111 (5) normalized codeset
112 (6) territory
113 (7) audience/modifier
116 /* If we have already tested for this locale entry there has to
117 be one data set in the list of loaded domains. */
118 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
119 strlen (dirname) + 1, 0, locale, NULL, NULL,
120 NULL, NULL, NULL, NULL, NULL, domainname, 0);
121 if (retval != NULL)
123 /* We know something about this locale. */
124 int cnt;
126 if (retval->decided == 0)
127 _nl_load_domain (retval);
129 if (retval->data != NULL)
130 return retval;
132 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
134 if (retval->successor[cnt]->decided == 0)
135 _nl_load_domain (retval->successor[cnt]);
137 if (retval->successor[cnt]->data != NULL)
138 break;
140 return cnt >= 0 ? retval : NULL;
141 /* NOTREACHED */
144 /* See whether the locale value is an alias. If yes its value
145 *overwrites* the alias name. No test for the original value is
146 done. */
147 alias_value = _nl_expand_alias (locale);
148 if (alias_value != NULL)
150 #if defined _LIBC || defined HAVE_STRDUP
151 locale = strdup (alias_value);
152 if (locale == NULL)
153 return NULL;
154 #else
155 size_t len = strlen (alias_value) + 1;
156 locale = (char *) malloc (len);
157 if (locale == NULL)
158 return NULL;
160 memcpy (locale, alias_value, len);
161 #endif
164 /* Now we determine the single parts of the locale name. First
165 look for the language. Termination symbols are `_' and `@' if
166 we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
167 mask = _nl_explode_name (locale, &language, &modifier, &territory,
168 &codeset, &normalized_codeset, &special,
169 &sponsor, &revision);
171 /* Create all possible locale entries which might be interested in
172 generalization. */
173 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
174 strlen (dirname) + 1, mask, language, territory,
175 codeset, normalized_codeset, modifier, special,
176 sponsor, revision, domainname, 1);
177 if (retval == NULL)
178 /* This means we are out of core. */
179 return NULL;
181 if (retval->decided == 0)
182 _nl_load_domain (retval);
183 if (retval->data == NULL)
185 int cnt;
186 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
188 if (retval->successor[cnt]->decided == 0)
189 _nl_load_domain (retval->successor[cnt]);
190 if (retval->successor[cnt]->data != NULL)
191 break;
195 /* The room for an alias was dynamically allocated. Free it now. */
196 if (alias_value != NULL)
197 free (locale);
199 return retval;
203 #ifdef _LIBC
204 static void __attribute__ ((unused))
205 free_mem (void)
207 struct loaded_l10nfile *runp = _nl_loaded_domains;
209 while (runp != NULL)
211 struct loaded_l10nfile *here = runp;
212 if (runp->data != NULL)
213 _nl_unload_domain ((struct loaded_domain *) runp->data);
214 runp = runp->next;
215 free (here);
219 text_set_element (__libc_subfreeres, free_mem);
220 #endif