merge from gcc
[binutils.git] / intl / finddomain.c
blob5409565fd3d8d415d0c8c01a0de8b6d51f3ff7ee
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 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <ctype.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <sys/types.h>
28 #if defined STDC_HEADERS || defined _LIBC
29 # include <stdlib.h>
30 #else
31 # ifdef HAVE_MALLOC_H
32 # include <malloc.h>
33 # else
34 void free ();
35 # endif
36 #endif
38 #if defined HAVE_STRING_H || defined _LIBC
39 # include <string.h>
40 #else
41 # include <strings.h>
42 # ifndef memcpy
43 # define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
44 # endif
45 #endif
46 #if !HAVE_STRCHR && !defined _LIBC
47 # ifndef strchr
48 # define strchr index
49 # endif
50 #endif
52 #if defined HAVE_UNISTD_H || defined _LIBC
53 # include <unistd.h>
54 #endif
56 #include "gettext.h"
57 #include "gettextP.h"
58 #ifdef _LIBC
59 # include <libintl.h>
60 #else
61 # include "libgettext.h"
62 #endif
64 /* @@ end of prolog @@ */
65 /* List of already loaded domains. */
66 static struct loaded_l10nfile *_nl_loaded_domains;
69 /* Return a data structure describing the message catalog described by
70 the DOMAINNAME and CATEGORY parameters with respect to the currently
71 established bindings. */
72 struct loaded_l10nfile *
73 internal_function
74 _nl_find_domain (dirname, locale, domainname)
75 const char *dirname;
76 char *locale;
77 const char *domainname;
79 struct loaded_l10nfile *retval;
80 const char *language;
81 const char *modifier;
82 const char *territory;
83 const char *codeset;
84 const char *normalized_codeset;
85 const char *special;
86 const char *sponsor;
87 const char *revision;
88 const char *alias_value;
89 int mask;
91 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
93 language[_territory[.codeset]][@modifier]
95 and six parts for the CEN syntax:
97 language[_territory][+audience][+special][,[sponsor][_revision]]
99 Beside the first part all of them are allowed to be missing. If
100 the full specified locale is not found, the less specific one are
101 looked for. The various parts will be stripped off according to
102 the following order:
103 (1) revision
104 (2) sponsor
105 (3) special
106 (4) codeset
107 (5) normalized codeset
108 (6) territory
109 (7) audience/modifier
112 /* If we have already tested for this locale entry there has to
113 be one data set in the list of loaded domains. */
114 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
115 strlen (dirname) + 1, 0, locale, NULL, NULL,
116 NULL, NULL, NULL, NULL, NULL, domainname, 0);
117 if (retval != NULL)
119 /* We know something about this locale. */
120 int cnt;
122 if (retval->decided == 0)
123 _nl_load_domain (retval);
125 if (retval->data != NULL)
126 return retval;
128 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
130 if (retval->successor[cnt]->decided == 0)
131 _nl_load_domain (retval->successor[cnt]);
133 if (retval->successor[cnt]->data != NULL)
134 break;
136 return cnt >= 0 ? retval : NULL;
137 /* NOTREACHED */
140 /* See whether the locale value is an alias. If yes its value
141 *overwrites* the alias name. No test for the original value is
142 done. */
143 alias_value = _nl_expand_alias (locale);
144 if (alias_value != NULL)
146 #if defined _LIBC || defined HAVE_STRDUP
147 locale = strdup (alias_value);
148 if (locale == NULL)
149 return NULL;
150 #else
151 size_t len = strlen (alias_value) + 1;
152 locale = (char *) malloc (len);
153 if (locale == NULL)
154 return NULL;
156 memcpy (locale, alias_value, len);
157 #endif
160 /* Now we determine the single parts of the locale name. First
161 look for the language. Termination symbols are `_' and `@' if
162 we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
163 mask = _nl_explode_name (locale, &language, &modifier, &territory,
164 &codeset, &normalized_codeset, &special,
165 &sponsor, &revision);
167 /* Create all possible locale entries which might be interested in
168 generalization. */
169 retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
170 strlen (dirname) + 1, mask, language, territory,
171 codeset, normalized_codeset, modifier, special,
172 sponsor, revision, domainname, 1);
173 if (retval == NULL)
174 /* This means we are out of core. */
175 return NULL;
177 if (retval->decided == 0)
178 _nl_load_domain (retval);
179 if (retval->data == NULL)
181 int cnt;
182 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
184 if (retval->successor[cnt]->decided == 0)
185 _nl_load_domain (retval->successor[cnt]);
186 if (retval->successor[cnt]->data != NULL)
187 break;
191 /* The room for an alias was dynamically allocated. Free it now. */
192 if (alias_value != NULL)
193 free (locale);
195 return retval;
199 #ifdef _LIBC
200 static void __attribute__ ((unused))
201 free_mem (void)
203 struct loaded_l10nfile *runp = _nl_loaded_domains;
205 while (runp != NULL)
207 struct loaded_l10nfile *here = runp;
208 if (runp->data != NULL)
209 _nl_unload_domain ((struct loaded_domain *) runp->data);
210 runp = runp->next;
211 free (here);
215 text_set_element (__libc_subfreeres, free_mem);
216 #endif