xref: /openbsd-src/gnu/usr.bin/binutils/intl/finddomain.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /* Handle list of needed message catalogs
2    Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3    Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
4 
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.
9 
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.
14 
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18 
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 
23 #include <ctype.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 
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
37 
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
51 
52 #if defined HAVE_UNISTD_H || defined _LIBC
53 # include <unistd.h>
54 #endif
55 
56 #include "gettext.h"
57 #include "gettextP.h"
58 #ifdef _LIBC
59 # include <libintl.h>
60 #else
61 # include "libgettext.h"
62 #endif
63 
64 /* @@ end of prolog @@ */
65 /* List of already loaded domains.  */
66 static struct loaded_l10nfile *_nl_loaded_domains;
67 
68 
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 _nl_find_domain (dirname, locale, domainname)
74      const char *dirname;
75      char *locale;
76      const char *domainname;
77 {
78   struct loaded_l10nfile *retval;
79   const char *language;
80   const char *modifier;
81   const char *territory;
82   const char *codeset;
83   const char *normalized_codeset;
84   const char *special;
85   const char *sponsor;
86   const char *revision;
87   const char *alias_value;
88   int mask;
89 
90   /* LOCALE can consist of up to four recognized parts for the XPG syntax:
91 
92 		language[_territory[.codeset]][@modifier]
93 
94      and six parts for the CEN syntax:
95 
96 	language[_territory][+audience][+special][,[sponsor][_revision]]
97 
98      Beside the first all of them are allowed to be missing.  If the
99      full specified locale is not found, the less specific one are
100      looked for.  The various part will be stripped of according to
101      the following order:
102 		(1) revision
103 		(2) sponsor
104 		(3) special
105 		(4) codeset
106 		(5) normalized codeset
107 		(6) territory
108 		(7) audience/modifier
109    */
110 
111   /* If we have already tested for this locale entry there has to
112      be one data set in the list of loaded domains.  */
113   retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
114 			       strlen (dirname) + 1, 0, locale, NULL, NULL,
115 			       NULL, NULL, NULL, NULL, NULL, domainname, 0);
116   if (retval != NULL)
117     {
118       /* We know something about this locale.  */
119       int cnt;
120 
121       if (retval->decided == 0)
122 	_nl_load_domain (retval);
123 
124       if (retval->data != NULL)
125 	return retval;
126 
127       for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
128 	{
129 	  if (retval->successor[cnt]->decided == 0)
130 	    _nl_load_domain (retval->successor[cnt]);
131 
132 	  if (retval->successor[cnt]->data != NULL)
133 	    break;
134 	}
135       return cnt >= 0 ? retval : NULL;
136       /* NOTREACHED */
137     }
138 
139   /* See whether the locale value is an alias.  If yes its value
140      *overwrites* the alias name.  No test for the original value is
141      done.  */
142   alias_value = _nl_expand_alias (locale);
143   if (alias_value != NULL)
144     {
145       size_t len = strlen (alias_value) + 1;
146       locale = (char *) malloc (len);
147       if (locale == NULL)
148 	return NULL;
149 
150       memcpy (locale, alias_value, len);
151     }
152 
153   /* Now we determine the single parts of the locale name.  First
154      look for the language.  Termination symbols are `_' and `@' if
155      we use XPG4 style, and `_', `+', and `,' if we use CEN syntax.  */
156   mask = _nl_explode_name (locale, &language, &modifier, &territory,
157 			   &codeset, &normalized_codeset, &special,
158 			   &sponsor, &revision);
159 
160   /* Create all possible locale entries which might be interested in
161      generalization.  */
162   retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
163 			       strlen (dirname) + 1, mask, language, territory,
164 			       codeset, normalized_codeset, modifier, special,
165 			       sponsor, revision, domainname, 1);
166   if (retval == NULL)
167     /* This means we are out of core.  */
168     return NULL;
169 
170   if (retval->decided == 0)
171     _nl_load_domain (retval);
172   if (retval->data == NULL)
173     {
174       int cnt;
175       for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
176 	{
177 	  if (retval->successor[cnt]->decided == 0)
178 	    _nl_load_domain (retval->successor[cnt]);
179 	  if (retval->successor[cnt]->data != NULL)
180 	    break;
181 	}
182     }
183 
184   /* The room for an alias was dynamically allocated.  Free it now.  */
185   if (alias_value != NULL)
186     free (locale);
187 
188   return retval;
189 }
190