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