*** empty log message ***
[glibc.git] / intl / finddomain.c
blobbb4609e8642eec459c3e1db14ff68c58222f5975
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 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.
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.
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. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <ctype.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <sys/types.h>
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
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
50 #if defined HAVE_UNISTD_H || defined _LIBC
51 # include <unistd.h>
52 #endif
54 #include "gettext.h"
55 #include "gettextP.h"
56 #ifdef _LIBC
57 # include <libintl.h>
58 #else
59 # include "libgettext.h"
60 #endif
62 /* @@ end of prolog @@ */
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
71 /* List of already loaded domains. */
72 static struct loaded_l10nfile *_nl_loaded_domains;
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
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;
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;
102 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
104 language[_territory[.codeset]][@modifier]
106 and six parts for the CEN syntax:
108 language[_territory][+audience][+special][,sponsor][_revision]
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
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)
130 /* We know something about this locale. */
131 int cnt;
133 if (retval->decided == 0)
134 _nl_load_domain (retval);
136 if (retval->data != NULL)
137 return retval;
139 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
141 if (retval->successor[cnt]->decided == 0)
142 _nl_load_domain (retval->successor[cnt]);
144 if (retval->successor[cnt]->data != NULL)
145 break;
147 return cnt >= 0 ? retval : NULL;
148 /* NOTREACHED */
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)
157 size_t len = strlen (alias_value) + 1;
158 locale = (char *) malloc (len);
159 if (locale == NULL)
160 return NULL;
162 memcpy (locale, alias_value, len);
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);
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;
182 if (retval->decided == 0)
183 _nl_load_domain (retval);
184 if (retval->data == NULL)
186 int cnt;
187 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
189 if (retval->successor[cnt]->decided == 0)
190 _nl_load_domain (retval->successor[cnt]);
191 if (retval->successor[cnt]->data != NULL)
192 break;
196 /* The room for an alias was dynamically allocated. Free it now. */
197 if (alias_value != NULL)
198 free (locale);
200 return retval;
203 /* @@ begin of epilog @@ */
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;
215 while ((*dest++ = *src++) != '\0')
216 /* Do nothing. */ ;
217 return dest - 1;
219 #endif