(ptrace) [PTRACE_TRACEME]: Notify the proc server that we are now traced.
[glibc.git] / intl / finddomain.c
blob19cf2d7581898cf5205121ed9164b5bd50cd08e8
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)
8 any later version.
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. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <errno.h>
24 #include <stdio.h>
25 #include <sys/types.h>
27 #if defined STDC_HEADERS || defined _LIBC
28 # include <stdlib.h>
29 #else
30 # ifdef HAVE_MALLOC_H
31 # include <malloc.h>
32 # else
33 void free ();
34 # endif
35 #endif
37 #if defined HAVE_STRING_H || defined _LIBC
38 # include <string.h>
39 #else
40 # include <strings.h>
41 #endif
42 #if !HAVE_STRCHR && !defined _LIBC
43 # ifndef strchr
44 # define strchr index
45 # endif
46 #endif
48 #if defined HAVE_UNISTD_H || defined _LIBC
49 # include <unistd.h>
50 #endif
52 #include "gettext.h"
53 #include "gettextP.h"
54 #ifdef _LIBC
55 # include <libintl.h>
56 #else
57 # include "libgettext.h"
58 #endif
60 /* @@ end of prolog @@ */
62 #ifdef _LIBC
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
67 #endif
69 /* Encoding of locale name parts. */
70 #define CEN_REVISION 1
71 #define CEN_SPONSOR 2
72 #define CEN_SPECIAL 4
73 #define XPG_CODESET 8
74 #define TERRITORY 16
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,
87 int mask,
88 const char *language,
89 const char *territory,
90 const char *codeset,
91 const char *modifier,
92 const char *special,
93 const char *sponsor,
94 const char *revision,
95 const char *domainname,
96 int do_allocate));
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)
104 const char *dirname;
105 char *locale;
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;
118 char *cp;
119 int mask;
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
134 the following order:
135 (1) revision
136 (2) sponsor
137 (3) special
138 (4) codeset
139 (5) territory
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);
147 if (retval != NULL)
149 /* We know something about this locale. */
150 int cnt;
152 if (retval->decided == 0)
153 _nl_load_domain (retval); /* @@@ */
155 if (retval->data != NULL)
156 return retval;
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)
164 break;
167 /* We really found some usable information. */
168 return cnt >= 0 ? retval : NULL;
169 /* NOTREACHED */
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
174 done. */
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);
180 if (locale == NULL)
181 return NULL;
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. */
189 mask = 0;
190 syntax = undecided;
191 language = cp = locale;
192 while (cp[0] != '\0' && cp[0] != '_' && cp[0] != '@'
193 && cp[0] != '+' && cp[0] != ',')
194 ++cp;
196 if (language == cp)
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. */
203 cp[0] = '\0';
204 territory = ++cp;
206 while (cp[0] != '\0' && cp[0] != '.' && cp[0] != '@'
207 && cp[0] != '+' && cp[0] != ',' && cp[0] != '_')
208 ++cp;
210 mask |= TERRITORY;
212 if (cp[0] == '.')
214 /* Next is the codeset. */
215 syntax = xpg;
216 cp[0] = '\0';
217 codeset = ++cp;
219 while (cp[0] != '\0' && cp[0] != '@')
220 ++cp;
222 mask |= XPG_CODESET;
226 if (cp[0] == '@' || (syntax != xpg && cp[0] == '+'))
228 /* Next is the modifier. */
229 syntax = cp[0] == '@' ? xpg : cen;
230 cp[0] = '\0';
231 modifier = ++cp;
233 while (syntax == cen && cp[0] != '\0' && cp[0] != '+'
234 && cp[0] != ',' && cp[0] != '_')
235 ++cp;
237 mask |= XPG_MODIFIER | CEN_AUDIENCE;
240 if (syntax != xpg && (cp[0] == '+' || cp[0] == ',' || cp[0] == '_'))
242 syntax = cen;
244 if (cp[0] == '+')
246 /* Next is special application (CEN syntax). */
247 cp[0] = '\0';
248 special = ++cp;
250 while (cp[0] != '\0' && cp[0] != ',' && cp[0] != '_')
251 ++cp;
253 mask |= CEN_SPECIAL;
256 if (cp[0] == ',')
258 /* Next is sponsor (CEN syntax). */
259 cp[0] = '\0';
260 sponsor = ++cp;
262 while (cp[0] != '\0' && cp[0] != '_')
263 ++cp;
265 mask |= CEN_SPONSOR;
268 if (cp[0] == '_')
270 /* Next is revision (CEN syntax). */
271 cp[0] = '\0';
272 revision = ++cp;
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. */
280 if (syntax == xpg)
282 if (territory[0] == '\0')
283 mask &= ~TERRITORY;
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
293 generalzation. */
294 retval = make_entry_rec (dirname, mask, language, territory, codeset,
295 modifier, special, sponsor, revision,
296 domainname, 1);
297 if (retval == NULL)
298 /* This means we are out of core. */
299 return NULL;
301 if (retval->decided == 0)
302 _nl_load_domain (retval);
303 if (retval->data == NULL)
305 int cnt;
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)
311 break;
313 /* Signal that locale is not available. */
314 retval->successor[cnt] = NULL;
316 if (retval->successor[cnt] == NULL)
317 retval = NULL;
320 /* The room for an alias was dynamically allocated. Free it now. */
321 if (alias_value != NULL)
322 free (locale);
324 return retval;
328 static struct loaded_domain *
329 make_entry_rec (dirname, mask, language, territory, codeset, modifier,
330 special, sponsor, revision, domain, do_allocate)
331 const char *dirname;
332 int mask;
333 const char *language;
334 const char *territory;
335 const char *codeset;
336 const char *modifier;
337 const char *special;
338 const char *sponsor;
339 const char *revision;
340 const char *domain;
341 int do_allocate;
343 struct loaded_domain *retval, *last;
344 char *filename, *cp;
345 size_t entries;
346 int cnt;
348 /* Allocate room for the full file name. */
349 filename = (char *) malloc (strlen (dirname) + 1
350 + strlen (language)
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)
366 return NULL;
368 retval = NULL;
369 last = 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
374 to be defined. */
375 #if !defined _LIBC && !defined HAVE_STPCPY
376 # define stpcpy(p, s) \
377 (strcpy (p, s), strchr (p, '\0'))
378 #endif
380 /* Construct file name. */
381 cp = stpcpy (filename, dirname);
382 *cp++ = '/';
383 cp = stpcpy (cp, language);
385 if ((mask & TERRITORY) != 0)
387 *cp++ = '_';
388 cp = stpcpy (cp, territory);
390 if ((mask & XPG_CODESET) != 0)
392 *cp++ = '.';
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)
404 *cp++ = '+';
405 cp = stpcpy (cp, special);
407 if ((mask & CEN_SPONSOR) != 0)
409 *cp++ = ',';
410 cp = stpcpy (cp, sponsor);
412 if ((mask & CEN_REVISION) != 0)
414 *cp++ = '_';
415 cp = stpcpy (cp, revision);
418 *cp++ = '/';
419 stpcpy (cp, domain);
421 /* Look in list of already loaded domains whether it is already
422 available. */
423 last = NULL;
424 for (retval = _nl_loaded_domains; retval != NULL; retval = retval->next)
426 int compare = strcmp (retval->filename, filename);
427 if (compare == 0)
428 /* We found it! */
429 break;
430 if (compare < 0)
432 /* It's not in the list. */
433 retval = NULL;
434 break;
437 last = retval;
440 if (retval != NULL || do_allocate == 0)
442 free (filename);
443 return retval;
446 retval = (struct loaded_domain *) malloc (sizeof (*retval));
447 if (retval == NULL)
448 return NULL;
450 retval->filename = filename;
451 retval->decided = 0;
453 if (last == NULL)
455 retval->next = _nl_loaded_domains;
456 _nl_loaded_domains = retval;
458 else
460 retval->next = last->next;
461 last->next = retval;
464 entries = 0;
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,
469 language, territory,
470 codeset, modifier,
471 special, sponsor,
472 revision, domain, 1);
473 retval->successor[entries] = NULL;
475 return retval;