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