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