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