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