1 /* finddomain.c -- handle list of needed message catalogs
2 Copyright (C) 1995 Software Foundation, Inc.
3 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
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)
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.
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
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include <sys/types.h>
27 #if defined STDC_HEADERS || defined _LIBC
37 #if defined HAVE_STRING_H || defined _LIBC
42 #if !HAVE_STRCHR && !defined _LIBC
48 #if defined HAVE_UNISTD_H || defined _LIBC
57 # include "libgettext.h"
60 /* @@ end of prolog @@ */
63 /* Rename the non ANSI C functions. This is required by the standard
64 because some ANSI C functions will require linking with this object
65 file and the name space must not be polluted. */
66 # define stpcpy __stpcpy
69 /* Encoding of locale name parts. */
70 #define CEN_REVISION 1
75 #define CEN_AUDIENCE 32
76 #define XPG_MODIFIER 64
78 #define CEN_SPECIFIC (CEN_REVISION|CEN_SPONSOR|CEN_SPECIAL|CEN_AUDIENCE)
79 #define XPG_SPECIFIC (XPG_CODESET|XPG_MODIFIER)
82 /* List of already loaded domains. */
83 static struct loaded_domain *_nl_loaded_domains;
85 /* Prototypes for local functions. */
86 static struct loaded_domain *make_entry_rec __P ((const char *dirname,
89 const char *territory,
95 const char *domainname,
98 /* Substitution for systems lacking this function in their C library. */
99 #if !_LIBC && !HAVE_STPCPY
100 static char *stpcpy __P ((char *dest, const char *src));
104 /* Return a data structure describing the message catalog described by
105 the DOMAINNAME and CATEGORY parameters with respect to the currently
106 established bindings. */
107 struct loaded_domain *
108 _nl_find_domain (dirname, locale, domainname)
111 const char *domainname;
113 enum { undecided, xpg, cen } syntax;
114 struct loaded_domain *retval;
115 const char *language;
116 const char *modifier = NULL;
117 const char *territory = NULL;
118 const char *codeset = NULL;
119 const char *special = NULL;
120 const char *sponsor = NULL;
121 const char *revision = NULL;
122 const char *alias_value = NULL;
126 /* CATEGORYVALUE now possibly contains a colon separated list of
127 locales. Each single locale can consist of up to four recognized
128 parts for the XPG syntax:
130 language[_territory[.codeset]][@modifier]
132 and six parts for the CEN syntax:
134 language[_territory][+audience][+special][,sponsor][_revision]
136 Beside the first all of them are allowed to be missing. If the
137 full specified locale is not found, the less specific one are
138 looked for. The various part will be stripped of according to
145 (6) audience/modifier
148 /* If we have already tested for this locale entry there has to
149 be one data set in the list of loaded domains. */
150 retval = make_entry_rec (dirname, 0, locale, NULL, NULL, NULL,
151 NULL, NULL, NULL, domainname, 0);
154 /* We know something about this locale. */
157 if (retval->decided == 0)
158 _nl_load_domain (retval); /* @@@ */
160 if (retval->data != NULL)
163 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
165 if (retval->successor[cnt]->decided == 0)
166 _nl_load_domain (retval->successor[cnt]);
168 if (retval->successor[cnt]->data != NULL)
172 /* We really found some usable information. */
173 return cnt >= 0 ? retval : NULL;
177 /* See whether the locale value is an alias. If yes its value
178 *overwrites* the alias name. No test for the original value is
180 alias_value = _nl_expand_alias (locale);
181 if (alias_value != NULL)
183 size_t len = strlen (alias_value) + 1;
184 locale = (char *) malloc (len);
188 memcpy (locale, alias_value, len);
191 /* Now we determine the single parts of the locale name. First
192 look for the language. Termination symbols are `_' and `@' if
193 we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
196 language = cp = locale;
197 while (cp[0] != '\0' && cp[0] != '_' && cp[0] != '@'
198 && cp[0] != '+' && cp[0] != ',')
202 /* This does not make sense: language has to be specified. Use
203 this entry as it is without exploding. Perhaps it is an alias. */
204 cp = strchr (language, '\0');
205 else if (cp[0] == '_')
207 /* Next is the territory. */
211 while (cp[0] != '\0' && cp[0] != '.' && cp[0] != '@'
212 && cp[0] != '+' && cp[0] != ',' && cp[0] != '_')
219 /* Next is the codeset. */
224 while (cp[0] != '\0' && cp[0] != '@')
231 if (cp[0] == '@' || (syntax != xpg && cp[0] == '+'))
233 /* Next is the modifier. */
234 syntax = cp[0] == '@' ? xpg : cen;
238 while (syntax == cen && cp[0] != '\0' && cp[0] != '+'
239 && cp[0] != ',' && cp[0] != '_')
242 mask |= XPG_MODIFIER | CEN_AUDIENCE;
245 if (syntax != xpg && (cp[0] == '+' || cp[0] == ',' || cp[0] == '_'))
251 /* Next is special application (CEN syntax). */
255 while (cp[0] != '\0' && cp[0] != ',' && cp[0] != '_')
263 /* Next is sponsor (CEN syntax). */
267 while (cp[0] != '\0' && cp[0] != '_')
275 /* Next is revision (CEN syntax). */
279 mask |= CEN_REVISION;
283 /* For CEN sytnax values it might be important to have the
284 separator character in the file name, not for XPG syntax. */
287 if (territory != NULL && territory[0] == '\0')
290 if (codeset != NULL && codeset[0] == '\0')
291 mask &= ~XPG_CODESET;
293 if (modifier != NULL && modifier[0] == '\0')
294 mask &= ~XPG_MODIFIER;
297 /* Create all possible locale entries which might be interested in
299 retval = make_entry_rec (dirname, mask, language, territory, codeset,
300 modifier, special, sponsor, revision,
303 /* This means we are out of core. */
306 if (retval->decided == 0)
307 _nl_load_domain (retval);
308 if (retval->data == NULL)
311 for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
313 if (retval->successor[cnt]->decided == 0)
314 _nl_load_domain (retval->successor[cnt]);
315 if (retval->successor[cnt]->data != NULL)
318 /* Signal that locale is not available. */
319 retval->successor[cnt] = NULL;
321 if (retval->successor[cnt] == NULL)
325 /* The room for an alias was dynamically allocated. Free it now. */
326 if (alias_value != NULL)
333 static struct loaded_domain *
334 make_entry_rec (dirname, mask, language, territory, codeset, modifier,
335 special, sponsor, revision, domain, do_allocate)
338 const char *language;
339 const char *territory;
341 const char *modifier;
344 const char *revision;
348 char *filename = NULL;
349 struct loaded_domain *last = NULL;
350 struct loaded_domain *retval;
356 /* Process the current entry described by the MASK only when it is
357 valid. Because the mask can have in the first call bits from
358 both syntaces set this is necessary to prevent constructing
359 illegal local names. */
360 /* FIXME: Rewrite because test is necessary only in first round. */
361 if ((mask & CEN_SPECIFIC) == 0 || (mask & XPG_SPECIFIC) == 0)
363 /* Allocate room for the full file name. */
364 filename = (char *) malloc (strlen (dirname) + 1
366 + ((mask & TERRITORY) != 0
367 ? strlen (territory) : 0)
368 + ((mask & XPG_CODESET) != 0
369 ? strlen (codeset) : 0)
370 + ((mask & XPG_MODIFIER) != 0 ?
371 strlen (modifier) : 0)
372 + ((mask & CEN_SPECIAL) != 0
373 ? strlen (special) : 0)
374 + ((mask & CEN_SPONSOR) != 0
375 ? strlen (sponsor) : 0)
376 + ((mask & CEN_REVISION) != 0
377 ? strlen (revision) : 0) + 1
378 + strlen (domain) + 1);
380 if (filename == NULL)
386 /* Construct file name. */
387 cp = stpcpy (filename, dirname);
389 cp = stpcpy (cp, language);
391 if ((mask & TERRITORY) != 0)
394 cp = stpcpy (cp, territory);
396 if ((mask & XPG_CODESET) != 0)
399 cp = stpcpy (cp, codeset);
401 if ((mask & (XPG_MODIFIER | CEN_AUDIENCE)) != 0)
403 /* This component can be part of both syntaces but has different
404 leading characters. For CEN we use `+', else `@'. */
405 *cp++ = (mask & CEN_AUDIENCE) != 0 ? '+' : '@';
406 cp = stpcpy (cp, modifier);
408 if ((mask & CEN_SPECIAL) != 0)
411 cp = stpcpy (cp, special);
413 if ((mask & CEN_SPONSOR) != 0)
416 cp = stpcpy (cp, sponsor);
418 if ((mask & CEN_REVISION) != 0)
421 cp = stpcpy (cp, revision);
427 /* Look in list of already loaded domains whether it is already
430 for (retval = _nl_loaded_domains; retval != NULL; retval = retval->next)
431 if (retval->filename != NULL)
433 int compare = strcmp (retval->filename, filename);
439 /* It's not in the list. */
447 if (retval != NULL || do_allocate == 0)
454 retval = (struct loaded_domain *) malloc (sizeof (*retval));
458 retval->filename = filename;
463 retval->next = _nl_loaded_domains;
464 _nl_loaded_domains = retval;
468 retval->next = last->next;
473 for (cnt = 126; cnt >= 0; --cnt)
474 if (cnt < mask && (cnt & ~mask) == 0
475 && ((cnt & CEN_SPECIFIC) == 0 || (cnt & XPG_SPECIFIC) == 0))
476 retval->successor[entries++] = make_entry_rec (dirname, cnt,
480 revision, domain, 1);
481 retval->successor[entries] = NULL;
487 /* @@ begin of epilog @@ */
489 /* We don't want libintl.a to depend on any other library. So we
490 avoid the non-standard function stpcpy. In GNU C Library this
491 function is available, though. Also allow the symbol HAVE_STPCPY
493 #if !_LIBC && !HAVE_STPCPY
499 while ((*dest++ = *src++) != '\0')