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