1/*	$NetBSD: finddomain.c,v 1.1.1.1 2016/01/10 21:36:17 christos Exp $	*/
2
3/* Handle list of needed message catalogs
4   Copyright (C) 1995-1999, 2000, 2001 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.  */
44static 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.  */
50struct loaded_l10nfile *
51internal_function
52_nl_find_domain (dirname, locale, domainname, domainbinding)
53     const char *dirname;
54     char *locale;
55     const char *domainname;
56     struct binding *domainbinding;
57{
58  struct loaded_l10nfile *retval;
59  const char *language;
60  const char *modifier;
61  const char *territory;
62  const char *codeset;
63  const char *normalized_codeset;
64  const char *special;
65  const char *sponsor;
66  const char *revision;
67  const char *alias_value;
68  int mask;
69
70  /* LOCALE can consist of up to four recognized parts for the XPG syntax:
71
72		language[_territory[.codeset]][@modifier]
73
74     and six parts for the CEN syntax:
75
76	language[_territory][+audience][+special][,[sponsor][_revision]]
77
78     Beside the first part all of them are allowed to be missing.  If
79     the full specified locale is not found, the less specific one are
80     looked for.  The various parts will be stripped off according to
81     the following order:
82		(1) revision
83		(2) sponsor
84		(3) special
85		(4) codeset
86		(5) normalized codeset
87		(6) territory
88		(7) audience/modifier
89   */
90
91  /* If we have already tested for this locale entry there has to
92     be one data set in the list of loaded domains.  */
93  retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
94			       strlen (dirname) + 1, 0, locale, NULL, NULL,
95			       NULL, NULL, NULL, NULL, NULL, domainname, 0);
96  if (retval != NULL)
97    {
98      /* We know something about this locale.  */
99      int cnt;
100
101      if (retval->decided == 0)
102	_nl_load_domain (retval, domainbinding);
103
104      if (retval->data != NULL)
105	return retval;
106
107      for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
108	{
109	  if (retval->successor[cnt]->decided == 0)
110	    _nl_load_domain (retval->successor[cnt], domainbinding);
111
112	  if (retval->successor[cnt]->data != NULL)
113	    break;
114	}
115      return cnt >= 0 ? retval : NULL;
116      /* NOTREACHED */
117    }
118
119  /* See whether the locale value is an alias.  If yes its value
120     *overwrites* the alias name.  No test for the original value is
121     done.  */
122  alias_value = _nl_expand_alias (locale);
123  if (alias_value != NULL)
124    {
125#if defined _LIBC || defined HAVE_STRDUP
126      locale = strdup (alias_value);
127      if (locale == NULL)
128	return NULL;
129#else
130      size_t len = strlen (alias_value) + 1;
131      locale = (char *) malloc (len);
132      if (locale == NULL)
133	return NULL;
134
135      memcpy (locale, alias_value, len);
136#endif
137    }
138
139  /* Now we determine the single parts of the locale name.  First
140     look for the language.  Termination symbols are `_' and `@' if
141     we use XPG4 style, and `_', `+', and `,' if we use CEN syntax.  */
142  mask = _nl_explode_name (locale, &language, &modifier, &territory,
143			   &codeset, &normalized_codeset, &special,
144			   &sponsor, &revision);
145
146  /* Create all possible locale entries which might be interested in
147     generalization.  */
148  retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
149			       strlen (dirname) + 1, mask, language, territory,
150			       codeset, normalized_codeset, modifier, special,
151			       sponsor, revision, domainname, 1);
152  if (retval == NULL)
153    /* This means we are out of core.  */
154    return NULL;
155
156  if (retval->decided == 0)
157    _nl_load_domain (retval, domainbinding);
158  if (retval->data == NULL)
159    {
160      int cnt;
161      for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
162	{
163	  if (retval->successor[cnt]->decided == 0)
164	    _nl_load_domain (retval->successor[cnt], domainbinding);
165	  if (retval->successor[cnt]->data != NULL)
166	    break;
167	}
168    }
169
170  /* The room for an alias was dynamically allocated.  Free it now.  */
171  if (alias_value != NULL)
172    free (locale);
173
174  /* The space for normalized_codeset is dynamically allocated.  Free it.  */
175  if (mask & XPG_NORM_CODESET)
176    free ((void *) normalized_codeset);
177
178  return retval;
179}
180
181
182#ifdef _LIBC
183static void __attribute__ ((unused))
184free_mem (void)
185{
186  struct loaded_l10nfile *runp = _nl_loaded_domains;
187
188  while (runp != NULL)
189    {
190      struct loaded_l10nfile *here = runp;
191      if (runp->data != NULL)
192	_nl_unload_domain ((struct loaded_domain *) runp->data);
193      runp = runp->next;
194      free ((char *) here->filename);
195      free (here);
196    }
197}
198
199text_set_element (__libc_subfreeres, free_mem);
200#endif
201