Fri May 3 13:32:08 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[platform/upstream/glibc.git] / intl / finddomain.c
1 /* finddomain.c -- handle list of needed message catalogs
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library 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 GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB.  If
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <ctype.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28
29 #if defined STDC_HEADERS || defined _LIBC
30 # include <stdlib.h>
31 #else
32 # ifdef HAVE_MALLOC_H
33 #  include <malloc.h>
34 # else
35 void free ();
36 # endif
37 #endif
38
39 #if defined HAVE_STRING_H || defined _LIBC
40 # include <string.h>
41 #else
42 # include <strings.h>
43 #endif
44 #if !HAVE_STRCHR && !defined _LIBC
45 # ifndef strchr
46 #  define strchr index
47 # endif
48 #endif
49
50 #if defined HAVE_UNISTD_H || defined _LIBC
51 # include <unistd.h>
52 #endif
53
54 #include "gettext.h"
55 #include "gettextP.h"
56 #ifdef _LIBC
57 # include <libintl.h>
58 #else
59 # include "libgettext.h"
60 #endif
61
62 /* @@ end of prolog @@ */
63
64 #ifdef _LIBC
65 /* Rename the non ANSI C functions.  This is required by the standard
66    because some ANSI C functions will require linking with this object
67    file and the name space must not be polluted.  */
68 # define stpcpy(dest, src) __stpcpy(dest, src)
69 #endif
70
71 /* List of already loaded domains.  */
72 static struct loaded_l10nfile *_nl_loaded_domains;
73
74 /* Substitution for systems lacking this function in their C library.  */
75 #if !_LIBC && !HAVE_STPCPY
76 static char *stpcpy__ PARAMS ((char *dest, const char *src));
77 # define stpcpy(dest, src) stpcpy__ (dest, src)
78 #endif
79
80
81 /* Return a data structure describing the message catalog described by
82    the DOMAINNAME and CATEGORY parameters with respect to the currently
83    established bindings.  */
84 struct loaded_l10nfile *
85 _nl_find_domain (dirname, locale, domainname)
86      const char *dirname;
87      char *locale;
88      const char *domainname;
89 {
90   struct loaded_l10nfile *retval;
91   const char *language;
92   const char *modifier;
93   const char *territory;
94   const char *codeset;
95   const char *normalized_codeset;
96   const char *special;
97   const char *sponsor;
98   const char *revision;
99   const char *alias_value;
100   int mask;
101
102   /* LOCALE can consist of up to four recognized parts for the XPG syntax:
103
104                 language[_territory[.codeset]][@modifier]
105
106      and six parts for the CEN syntax:
107
108         language[_territory][+audience][+special][,sponsor][_revision]
109
110      Beside the first all of them are allowed to be missing.  If the
111      full specified locale is not found, the less specific one are
112      looked for.  The various part will be stripped of according to
113      the following order:
114                 (1) revision
115                 (2) sponsor
116                 (3) special
117                 (4) codeset
118                 (5) normalized codeset
119                 (6) territory
120                 (7) audience/modifier
121    */
122
123   /* If we have already tested for this locale entry there has to
124      be one data set in the list of loaded domains.  */
125   retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
126                                strlen (dirname) + 1, 0, locale, NULL, NULL,
127                                NULL, NULL, NULL, NULL, NULL, domainname, 0);
128   if (retval != NULL)
129     {
130       /* We know something about this locale.  */
131       int cnt;
132
133       if (retval->decided == 0)
134         _nl_load_domain (retval);
135
136       if (retval->data != NULL)
137         return retval;
138
139       for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
140         {
141           if (retval->successor[cnt]->decided == 0)
142             _nl_load_domain (retval->successor[cnt]);
143
144           if (retval->successor[cnt]->data != NULL)
145             break;
146         }
147       return cnt >= 0 ? retval : NULL;
148       /* NOTREACHED */
149     }
150
151   /* See whether the locale value is an alias.  If yes its value
152      *overwrites* the alias name.  No test for the original value is
153      done.  */
154   alias_value = _nl_expand_alias (locale);
155   if (alias_value != NULL)
156     {
157       size_t len = strlen (alias_value) + 1;
158       locale = (char *) malloc (len);
159       if (locale == NULL)
160         return NULL;
161
162       memcpy (locale, alias_value, len);
163     }
164
165   /* Now we determine the single parts of the locale name.  First
166      look for the language.  Termination symbols are `_' and `@' if
167      we use XPG4 style, and `_', `+', and `,' if we use CEN syntax.  */
168   mask = _nl_explode_name (locale, &language, &modifier, &territory,
169                            &codeset, &normalized_codeset, &special,
170                            &sponsor, &revision);
171
172   /* Create all possible locale entries which might be interested in
173      generalzation.  */
174   retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
175                                strlen (dirname) + 1, mask, language, territory,
176                                codeset, normalized_codeset, modifier, special,
177                                sponsor, revision, domainname, 1);
178   if (retval == NULL)
179     /* This means we are out of core.  */
180     return NULL;
181
182   if (retval->decided == 0)
183     _nl_load_domain (retval);
184   if (retval->data == NULL)
185     {
186       int cnt;
187       for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
188         {
189           if (retval->successor[cnt]->decided == 0)
190             _nl_load_domain (retval->successor[cnt]);
191           if (retval->successor[cnt]->data != NULL)
192             break;
193         }
194     }
195
196   /* The room for an alias was dynamically allocated.  Free it now.  */
197   if (alias_value != NULL)
198     free (locale);
199
200   return retval;
201 }
202
203 /* @@ begin of epilog @@ */
204
205 /* We don't want libintl.a to depend on any other library.  So we
206    avoid the non-standard function stpcpy.  In GNU C Library this
207    function is available, though.  Also allow the symbol HAVE_STPCPY
208    to be defined.  */
209 #if !_LIBC && !HAVE_STPCPY
210 static char *
211 stpcpy__ (dest, src)
212      char *dest;
213      const char *src;
214 {
215   while ((*dest++ = *src++) != '\0')
216     /* Do nothing. */ ;
217   return dest - 1;
218 }
219 #endif