xref: /openbsd-src/gnu/gcc/intl/finddomain.c (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert /* Handle list of needed message catalogs
2*404b540aSrobert    Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
3*404b540aSrobert    Written by Ulrich Drepper <drepper@gnu.org>, 1995.
4*404b540aSrobert 
5*404b540aSrobert    This program is free software; you can redistribute it and/or modify it
6*404b540aSrobert    under the terms of the GNU Library General Public License as published
7*404b540aSrobert    by the Free Software Foundation; either version 2, or (at your option)
8*404b540aSrobert    any later version.
9*404b540aSrobert 
10*404b540aSrobert    This program is distributed in the hope that it will be useful,
11*404b540aSrobert    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*404b540aSrobert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*404b540aSrobert    Library General Public License for more details.
14*404b540aSrobert 
15*404b540aSrobert    You should have received a copy of the GNU Library General Public
16*404b540aSrobert    License along with this program; if not, write to the Free Software
17*404b540aSrobert    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
18*404b540aSrobert    USA.  */
19*404b540aSrobert 
20*404b540aSrobert #ifdef HAVE_CONFIG_H
21*404b540aSrobert # include <config.h>
22*404b540aSrobert #endif
23*404b540aSrobert 
24*404b540aSrobert #include <stdio.h>
25*404b540aSrobert #include <sys/types.h>
26*404b540aSrobert #include <stdlib.h>
27*404b540aSrobert #include <string.h>
28*404b540aSrobert 
29*404b540aSrobert #if defined HAVE_UNISTD_H || defined _LIBC
30*404b540aSrobert # include <unistd.h>
31*404b540aSrobert #endif
32*404b540aSrobert 
33*404b540aSrobert #include "gettextP.h"
34*404b540aSrobert #ifdef _LIBC
35*404b540aSrobert # include <libintl.h>
36*404b540aSrobert #else
37*404b540aSrobert # include "libgnuintl.h"
38*404b540aSrobert #endif
39*404b540aSrobert 
40*404b540aSrobert /* @@ end of prolog @@ */
41*404b540aSrobert /* List of already loaded domains.  */
42*404b540aSrobert static struct loaded_l10nfile *_nl_loaded_domains;
43*404b540aSrobert 
44*404b540aSrobert 
45*404b540aSrobert /* Return a data structure describing the message catalog described by
46*404b540aSrobert    the DOMAINNAME and CATEGORY parameters with respect to the currently
47*404b540aSrobert    established bindings.  */
48*404b540aSrobert struct loaded_l10nfile *
49*404b540aSrobert internal_function
_nl_find_domain(dirname,locale,domainname,domainbinding)50*404b540aSrobert _nl_find_domain (dirname, locale, domainname, domainbinding)
51*404b540aSrobert      const char *dirname;
52*404b540aSrobert      char *locale;
53*404b540aSrobert      const char *domainname;
54*404b540aSrobert      struct binding *domainbinding;
55*404b540aSrobert {
56*404b540aSrobert   struct loaded_l10nfile *retval;
57*404b540aSrobert   const char *language;
58*404b540aSrobert   const char *modifier;
59*404b540aSrobert   const char *territory;
60*404b540aSrobert   const char *codeset;
61*404b540aSrobert   const char *normalized_codeset;
62*404b540aSrobert   const char *special;
63*404b540aSrobert   const char *sponsor;
64*404b540aSrobert   const char *revision;
65*404b540aSrobert   const char *alias_value;
66*404b540aSrobert   int mask;
67*404b540aSrobert 
68*404b540aSrobert   /* LOCALE can consist of up to four recognized parts for the XPG syntax:
69*404b540aSrobert 
70*404b540aSrobert 		language[_territory[.codeset]][@modifier]
71*404b540aSrobert 
72*404b540aSrobert      and six parts for the CEN syntax:
73*404b540aSrobert 
74*404b540aSrobert 	language[_territory][+audience][+special][,[sponsor][_revision]]
75*404b540aSrobert 
76*404b540aSrobert      Beside the first part all of them are allowed to be missing.  If
77*404b540aSrobert      the full specified locale is not found, the less specific one are
78*404b540aSrobert      looked for.  The various parts will be stripped off according to
79*404b540aSrobert      the following order:
80*404b540aSrobert 		(1) revision
81*404b540aSrobert 		(2) sponsor
82*404b540aSrobert 		(3) special
83*404b540aSrobert 		(4) codeset
84*404b540aSrobert 		(5) normalized codeset
85*404b540aSrobert 		(6) territory
86*404b540aSrobert 		(7) audience/modifier
87*404b540aSrobert    */
88*404b540aSrobert 
89*404b540aSrobert   /* If we have already tested for this locale entry there has to
90*404b540aSrobert      be one data set in the list of loaded domains.  */
91*404b540aSrobert   retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
92*404b540aSrobert 			       strlen (dirname) + 1, 0, locale, NULL, NULL,
93*404b540aSrobert 			       NULL, NULL, NULL, NULL, NULL, domainname, 0);
94*404b540aSrobert   if (retval != NULL)
95*404b540aSrobert     {
96*404b540aSrobert       /* We know something about this locale.  */
97*404b540aSrobert       int cnt;
98*404b540aSrobert 
99*404b540aSrobert       if (retval->decided == 0)
100*404b540aSrobert 	_nl_load_domain (retval, domainbinding);
101*404b540aSrobert 
102*404b540aSrobert       if (retval->data != NULL)
103*404b540aSrobert 	return retval;
104*404b540aSrobert 
105*404b540aSrobert       for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
106*404b540aSrobert 	{
107*404b540aSrobert 	  if (retval->successor[cnt]->decided == 0)
108*404b540aSrobert 	    _nl_load_domain (retval->successor[cnt], domainbinding);
109*404b540aSrobert 
110*404b540aSrobert 	  if (retval->successor[cnt]->data != NULL)
111*404b540aSrobert 	    break;
112*404b540aSrobert 	}
113*404b540aSrobert       return cnt >= 0 ? retval : NULL;
114*404b540aSrobert       /* NOTREACHED */
115*404b540aSrobert     }
116*404b540aSrobert 
117*404b540aSrobert   /* See whether the locale value is an alias.  If yes its value
118*404b540aSrobert      *overwrites* the alias name.  No test for the original value is
119*404b540aSrobert      done.  */
120*404b540aSrobert   alias_value = _nl_expand_alias (locale);
121*404b540aSrobert   if (alias_value != NULL)
122*404b540aSrobert     {
123*404b540aSrobert #if defined _LIBC || defined HAVE_STRDUP
124*404b540aSrobert       locale = strdup (alias_value);
125*404b540aSrobert       if (locale == NULL)
126*404b540aSrobert 	return NULL;
127*404b540aSrobert #else
128*404b540aSrobert       size_t len = strlen (alias_value) + 1;
129*404b540aSrobert       locale = (char *) malloc (len);
130*404b540aSrobert       if (locale == NULL)
131*404b540aSrobert 	return NULL;
132*404b540aSrobert 
133*404b540aSrobert       memcpy (locale, alias_value, len);
134*404b540aSrobert #endif
135*404b540aSrobert     }
136*404b540aSrobert 
137*404b540aSrobert   /* Now we determine the single parts of the locale name.  First
138*404b540aSrobert      look for the language.  Termination symbols are `_' and `@' if
139*404b540aSrobert      we use XPG4 style, and `_', `+', and `,' if we use CEN syntax.  */
140*404b540aSrobert   mask = _nl_explode_name (locale, &language, &modifier, &territory,
141*404b540aSrobert 			   &codeset, &normalized_codeset, &special,
142*404b540aSrobert 			   &sponsor, &revision);
143*404b540aSrobert 
144*404b540aSrobert   /* Create all possible locale entries which might be interested in
145*404b540aSrobert      generalization.  */
146*404b540aSrobert   retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
147*404b540aSrobert 			       strlen (dirname) + 1, mask, language, territory,
148*404b540aSrobert 			       codeset, normalized_codeset, modifier, special,
149*404b540aSrobert 			       sponsor, revision, domainname, 1);
150*404b540aSrobert   if (retval == NULL)
151*404b540aSrobert     /* This means we are out of core.  */
152*404b540aSrobert     return NULL;
153*404b540aSrobert 
154*404b540aSrobert   if (retval->decided == 0)
155*404b540aSrobert     _nl_load_domain (retval, domainbinding);
156*404b540aSrobert   if (retval->data == NULL)
157*404b540aSrobert     {
158*404b540aSrobert       int cnt;
159*404b540aSrobert       for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
160*404b540aSrobert 	{
161*404b540aSrobert 	  if (retval->successor[cnt]->decided == 0)
162*404b540aSrobert 	    _nl_load_domain (retval->successor[cnt], domainbinding);
163*404b540aSrobert 	  if (retval->successor[cnt]->data != NULL)
164*404b540aSrobert 	    break;
165*404b540aSrobert 	}
166*404b540aSrobert     }
167*404b540aSrobert 
168*404b540aSrobert   /* The room for an alias was dynamically allocated.  Free it now.  */
169*404b540aSrobert   if (alias_value != NULL)
170*404b540aSrobert     free (locale);
171*404b540aSrobert 
172*404b540aSrobert   /* The space for normalized_codeset is dynamically allocated.  Free it.  */
173*404b540aSrobert   if (mask & XPG_NORM_CODESET)
174*404b540aSrobert     free ((void *) normalized_codeset);
175*404b540aSrobert 
176*404b540aSrobert   return retval;
177*404b540aSrobert }
178*404b540aSrobert 
179*404b540aSrobert 
180*404b540aSrobert #ifdef _LIBC
libc_freeres_fn(free_mem)181*404b540aSrobert libc_freeres_fn (free_mem)
182*404b540aSrobert {
183*404b540aSrobert   struct loaded_l10nfile *runp = _nl_loaded_domains;
184*404b540aSrobert 
185*404b540aSrobert   while (runp != NULL)
186*404b540aSrobert     {
187*404b540aSrobert       struct loaded_l10nfile *here = runp;
188*404b540aSrobert       if (runp->data != NULL)
189*404b540aSrobert 	_nl_unload_domain ((struct loaded_domain *) runp->data);
190*404b540aSrobert       runp = runp->next;
191*404b540aSrobert       free ((char *) here->filename);
192*404b540aSrobert       free (here);
193*404b540aSrobert     }
194*404b540aSrobert }
195*404b540aSrobert #endif
196