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
,
99 /* Return a data structure describing the message catalog described by
100 the DOMAINNAME and CATEGORY parameters with respect to the currently
101 established bindings. */
102 struct loaded_domain
*
103 _nl_find_domain (dirname
, locale
, domainname
)
106 const char *domainname
;
108 enum { undecided
, xpg
, cen
} syntax
;
109 struct loaded_domain
*retval
;
110 const char *language
;
111 const char *modifier
= NULL
;
112 const char *territory
= NULL
;
113 const char *codeset
= NULL
;
114 const char *special
= NULL
;
115 const char *sponsor
= NULL
;
116 const char *revision
= NULL
;
117 const char *alias_value
= NULL
;
121 /* CATEGORYVALUE now possibly contains a colon separated list of
122 locales. Each single locale can consist of up to four recognized
123 parts for the XPG syntax:
125 language[_territory[.codeset]][@modifier]
127 and six parts for the CEN syntax:
129 language[_territory][+audience][+special][,sponsor][_revision]
131 Beside the first all of them are allowed to be missing. If the
132 full specified locale is not found, the less specific one are
133 looked for. The various part will be stripped of according to
140 (6) audience/modifier
143 /* If we have already tested for this locale entry there has to
144 be one data set in the list of loaded domains. */
145 retval
= make_entry_rec (dirname
, 0, locale
, NULL
, NULL
, NULL
,
146 NULL
, NULL
, NULL
, domainname
, 0);
149 /* We know something about this locale. */
152 if (retval
->decided
== 0)
153 _nl_load_domain (retval
); /* @@@ */
155 if (retval
->data
!= NULL
)
158 for (cnt
= 6; cnt
>= 0; --cnt
)
160 if (retval
->successor
[cnt
] == 0)
161 _nl_load_domain (retval
->successor
[cnt
]);
163 if (retval
->successor
[cnt
]->data
!= NULL
)
167 /* We really found some usable information. */
168 return cnt
>= 0 ? retval
: NULL
;
172 /* See whether the locale value is an alias. If yes its value
173 *overwrites* the alias name. No test for the original value is
175 alias_value
= _nl_expand_alias (locale
);
176 if (alias_value
!= NULL
)
178 size_t len
= strlen (alias_value
) + 1;
179 locale
= (char *) malloc (len
);
183 memcpy (locale
, alias_value
, len
);
186 /* Now we determine the single parts of the locale name. First
187 look for the language. Termination symbols are `_' and `@' if
188 we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
191 language
= cp
= locale
;
192 while (cp
[0] != '\0' && cp
[0] != '_' && cp
[0] != '@'
193 && cp
[0] != '+' && cp
[0] != ',')
197 /* This does not make sense: language has to be specified. Use
198 this entry as it is without exploding. Perhaps it is an alias. */
199 cp
= strchr (language
, '\0');
200 else if (cp
[0] == '_')
202 /* Next is the territory. */
206 while (cp
[0] != '\0' && cp
[0] != '.' && cp
[0] != '@'
207 && cp
[0] != '+' && cp
[0] != ',' && cp
[0] != '_')
214 /* Next is the codeset. */
219 while (cp
[0] != '\0' && cp
[0] != '@')
226 if (cp
[0] == '@' || (syntax
!= xpg
&& cp
[0] == '+'))
228 /* Next is the modifier. */
229 syntax
= cp
[0] == '@' ? xpg
: cen
;
233 while (syntax
== cen
&& cp
[0] != '\0' && cp
[0] != '+'
234 && cp
[0] != ',' && cp
[0] != '_')
237 mask
|= XPG_MODIFIER
| CEN_AUDIENCE
;
240 if (syntax
!= xpg
&& (cp
[0] == '+' || cp
[0] == ',' || cp
[0] == '_'))
246 /* Next is special application (CEN syntax). */
250 while (cp
[0] != '\0' && cp
[0] != ',' && cp
[0] != '_')
258 /* Next is sponsor (CEN syntax). */
262 while (cp
[0] != '\0' && cp
[0] != '_')
270 /* Next is revision (CEN syntax). */
274 mask
|= CEN_REVISION
;
278 /* For CEN sytnax values it might be important to have the
279 separator character in the file name, not for XPG syntax. */
282 if (territory
[0] == '\0')
285 if (codeset
[0] == '\0')
286 mask
&= ~XPG_CODESET
;
288 if (modifier
[0] == '\0')
289 mask
&= ~XPG_MODIFIER
;
292 /* Create all possible locale entries which might be interested in
294 retval
= make_entry_rec (dirname
, mask
, language
, territory
, codeset
,
295 modifier
, special
, sponsor
, revision
,
298 /* This means we are out of core. */
301 if (retval
->decided
== 0)
302 _nl_load_domain (retval
);
303 if (retval
->data
== NULL
)
306 for (cnt
= 0; retval
->successor
[cnt
] != NULL
; ++cnt
)
308 if (retval
->successor
[cnt
]->decided
== 0)
309 _nl_load_domain (retval
->successor
[cnt
]);
310 if (retval
->successor
[cnt
]->data
!= NULL
)
313 /* Signal that locale is not available. */
314 retval
->successor
[cnt
] = NULL
;
316 if (retval
->successor
[cnt
] == NULL
)
320 /* The room for an alias was dynamically allocated. Free it now. */
321 if (alias_value
!= NULL
)
328 static struct loaded_domain
*
329 make_entry_rec (dirname
, mask
, language
, territory
, codeset
, modifier
,
330 special
, sponsor
, revision
, domain
, do_allocate
)
333 const char *language
;
334 const char *territory
;
336 const char *modifier
;
339 const char *revision
;
343 struct loaded_domain
*retval
, *last
;
348 /* Allocate room for the full file name. */
349 filename
= (char *) malloc (strlen (dirname
) + 1
351 + ((mask
& TERRITORY
) != 0
352 ? strlen (territory
) : 0)
353 + ((mask
& XPG_CODESET
) != 0
354 ? strlen (codeset
) : 0)
355 + ((mask
& XPG_MODIFIER
) != 0 ?
356 strlen (modifier
) : 0)
357 + ((mask
& CEN_SPECIAL
) != 0
358 ? strlen (special
) : 0)
359 + ((mask
& CEN_SPONSOR
) != 0
360 ? strlen (sponsor
) : 0)
361 + ((mask
& CEN_REVISION
) != 0
362 ? strlen (revision
) : 0) + 1
363 + strlen (domain
) + 1);
365 if (filename
== NULL
)
371 /* We don't want libintl.a to depend on any other library. So we
372 avoid the non-standard function stpcpy. In GNU C Library this
373 function is available, though. Also allow the symbol HAVE_STPCPY
375 #if !defined _LIBC && !defined HAVE_STPCPY
376 # define stpcpy(p, s) \
377 (strcpy (p, s), strchr (p, '\0'))
380 /* Construct file name. */
381 cp
= stpcpy (filename
, dirname
);
383 cp
= stpcpy (cp
, language
);
385 if ((mask
& TERRITORY
) != 0)
388 cp
= stpcpy (cp
, territory
);
390 if ((mask
& XPG_CODESET
) != 0)
393 cp
= stpcpy (cp
, codeset
);
395 if ((mask
& (XPG_MODIFIER
| CEN_AUDIENCE
)) != 0)
397 /* This component can be part of both syntaces but has different
398 leading characters. For CEN we use `+', else `@'. */
399 *cp
++ = (mask
& CEN_AUDIENCE
) != 0 ? '+' : '@';
400 cp
= stpcpy (cp
, modifier
);
402 if ((mask
& CEN_SPECIAL
) != 0)
405 cp
= stpcpy (cp
, special
);
407 if ((mask
& CEN_SPONSOR
) != 0)
410 cp
= stpcpy (cp
, sponsor
);
412 if ((mask
& CEN_REVISION
) != 0)
415 cp
= stpcpy (cp
, revision
);
421 /* Look in list of already loaded domains whether it is already
424 for (retval
= _nl_loaded_domains
; retval
!= NULL
; retval
= retval
->next
)
426 int compare
= strcmp (retval
->filename
, filename
);
432 /* It's not in the list. */
440 if (retval
!= NULL
|| do_allocate
== 0)
446 retval
= (struct loaded_domain
*) malloc (sizeof (*retval
));
450 retval
->filename
= filename
;
455 retval
->next
= _nl_loaded_domains
;
456 _nl_loaded_domains
= retval
;
460 retval
->next
= last
->next
;
465 for (cnt
= 126; cnt
>= 0; --cnt
)
466 if (cnt
< mask
&& (cnt
& ~mask
) == 0
467 && ((cnt
& CEN_SPECIFIC
) == 0 || (cnt
& XPG_SPECIFIC
) == 0))
468 retval
->successor
[entries
++] = make_entry_rec (dirname
, cnt
,
472 revision
, domain
, 1);
473 retval
->successor
[entries
] = NULL
;