Sat Jun 22 21:29:52 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / intl / dcgettext.c
blob958b0d4cc43fa1ec56250cb6e5447490b9d2b60d
1 /* dcgettext.c -- implementation of the dcgettext(3) function
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
4 This file is part of the GNU C Library. Its master source is NOT part of
5 the C library, however. The master source lives in /gd/gnu/lib.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, USA. */
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include <sys/types.h>
28 #ifdef __GNUC__
29 # define alloca __builtin_alloca
30 # define HAVE_ALLOCA 1
31 #else
32 # if defined HAVE_ALLOCA_H || defined _LIBC
33 # include <alloca.h>
34 # else
35 # ifdef _AIX
36 #pragma alloca
37 # else
38 # ifndef alloca
39 char *alloca ();
40 # endif
41 # endif
42 # endif
43 #endif
45 #include <errno.h>
46 #ifndef errno
47 extern int errno;
48 #endif
50 #if defined STDC_HEADERS || defined _LIBC
51 # include <stdlib.h>
52 #else
53 char *getenv ();
54 # ifdef HAVE_MALLOC_H
55 # include <malloc.h>
56 # else
57 void free ();
58 # endif
59 #endif
61 #if defined HAVE_STRING_H || defined _LIBC
62 # include <string.h>
63 #else
64 # include <strings.h>
65 #endif
66 #if !HAVE_STRCHR && !defined _LIBC
67 # ifndef strchr
68 # define strchr index
69 # endif
70 #endif
72 #if defined HAVE_UNISTD_H || defined _LIBC
73 # include <unistd.h>
74 #endif
76 #include "gettext.h"
77 #include "gettextP.h"
78 #ifdef _LIBC
79 # include <libintl.h>
80 #else
81 # include "libgettext.h"
82 #endif
83 #include "hash-string.h"
85 /* @@ end of prolog @@ */
87 #ifdef _LIBC
88 /* Rename the non ANSI C functions. This is required by the standard
89 because some ANSI C functions will require linking with this object
90 file and the name space must not be polluted. */
91 # define getcwd __getcwd
92 # define stpcpy __stpcpy
93 #else
94 # if !defined HAVE_GETCWD
95 char *getwd ();
96 # define getcwd(buf, max) getwd (buf)
97 # else
98 char *getcwd ();
99 # endif
100 # ifndef HAVE_STPCPY
101 static char *stpcpy PARAMS ((char *dest, const char *src));
102 # endif
103 #endif
105 /* Amount to increase buffer size by in each try. */
106 #define PATH_INCR 32
108 /* The following is from pathmax.h. */
109 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
110 PATH_MAX but might cause redefinition warnings when sys/param.h is
111 later included (as on MORE/BSD 4.3). */
112 #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
113 # include <limits.h>
114 #endif
116 #ifndef _POSIX_PATH_MAX
117 # define _POSIX_PATH_MAX 255
118 #endif
120 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
121 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
122 #endif
124 /* Don't include sys/param.h if it already has been. */
125 #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
126 # include <sys/param.h>
127 #endif
129 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
130 # define PATH_MAX MAXPATHLEN
131 #endif
133 #ifndef PATH_MAX
134 # define PATH_MAX _POSIX_PATH_MAX
135 #endif
137 /* XPG3 defines the result of `setlocale (category, NULL)' as:
138 ``Directs `setlocale()' to query `category' and return the current
139 setting of `local'.''
140 However it does not specify the exact format. And even worse: POSIX
141 defines this not at all. So we can use this feature only on selected
142 system (e.g. those using GNU C Library). */
143 #ifdef _LIBC
144 # define HAVE_LOCALE_NULL
145 #endif
147 /* Name of the default domain used for gettext(3) prior any call to
148 textdomain(3). The default value for this is "messages". */
149 const char _nl_default_default_domain[] = "messages";
151 /* Value used as the default domain for gettext(3). */
152 const char *_nl_current_default_domain = _nl_default_default_domain;
154 /* Contains the default location of the message catalogs. */
155 const char _nl_default_dirname[] = GNULOCALEDIR;
157 /* List with bindings of specific domains created by bindtextdomain()
158 calls. */
159 struct binding *_nl_domain_bindings;
161 /* Prototypes for local functions. */
162 static char *find_msg PARAMS ((struct loaded_l10nfile *domain_file,
163 const char *msgid));
164 static const char *category_to_name PARAMS ((int category));
165 static const char *guess_category_value PARAMS ((int category,
166 const char *categoryname));
169 /* For those loosing systems which don't have `alloca' we have to add
170 some additional code emulating it. */
171 #ifdef HAVE_ALLOCA
172 /* Nothing has to be done. */
173 # define ADD_BLOCK(list, address) /* nothing */
174 # define FREE_BLOCKS(list) /* nothing */
175 #else
176 struct block_list
178 void *address;
179 struct block_list *next;
181 # define ADD_BLOCK(list, addr) \
182 do { \
183 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
184 /* If we cannot get a free block we cannot add the new element to \
185 the list. */ \
186 if (newp != NULL) { \
187 newp->address = (addr); \
188 newp->next = (list); \
189 (list) = newp; \
191 } while (0)
192 # define FREE_BLOCKS(list) \
193 do { \
194 while (list != NULL) { \
195 struct block_list *old = list; \
196 list = list->next; \
197 free (old); \
199 } while (0)
200 # undef alloca
201 # define alloca(size) (malloc (size))
202 #endif /* have alloca */
205 /* Names for the libintl functions are a problem. They must not clash
206 with existing names and they should follow ANSI C. But this source
207 code is also used in GNU C Library where the names have a __
208 prefix. So we have to make a difference here. */
209 #ifdef _LIBC
210 # define DCGETTEXT __dcgettext
211 #else
212 # define DCGETTEXT dcgettext__
213 #endif
215 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
216 locale. */
217 char *
218 DCGETTEXT (domainname, msgid, category)
219 const char *domainname;
220 const char *msgid;
221 int category;
223 #ifndef HAVE_ALLOCA
224 struct block_list *alloca_list = NULL;
225 #endif
226 struct loaded_l10nfile *domain;
227 struct binding *binding;
228 const char *categoryname;
229 const char *categoryvalue;
230 char *dirname, *xdomainname;
231 char *single_locale;
232 char *retval;
233 int saved_errno = errno;
235 /* If no real MSGID is given return NULL. */
236 if (msgid == NULL)
237 return NULL;
239 /* If DOMAINNAME is NULL, we are interested in the default domain. If
240 CATEGORY is not LC_MESSAGES this might not make much sense but the
241 defintion left this undefined. */
242 if (domainname == NULL)
243 domainname = _nl_current_default_domain;
245 /* First find matching binding. */
246 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
248 int compare = strcmp (domainname, binding->domainname);
249 if (compare == 0)
250 /* We found it! */
251 break;
252 if (compare < 0)
254 /* It is not in the list. */
255 binding = NULL;
256 break;
260 if (binding == NULL)
261 dirname = (char *) _nl_default_dirname;
262 else if (binding->dirname[0] == '/')
263 dirname = binding->dirname;
264 else
266 /* We have a relative path. Make it absolute now. */
267 size_t dirname_len = strlen (binding->dirname) + 1;
268 size_t path_max;
269 char *ret;
271 path_max = (unsigned) PATH_MAX;
272 path_max += 2; /* The getcwd docs say to do this. */
274 dirname = (char *) alloca (path_max + dirname_len);
275 ADD_BLOCK (block_list, dirname);
277 errno = 0;
278 while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
280 path_max += PATH_INCR;
281 dirname = (char *) alloca (path_max + dirname_len);
282 ADD_BLOCK (block_list, dirname);
283 errno = 0;
286 if (ret == NULL)
288 /* We cannot get the current working directory. Don't signal an
289 error but simply return the default string. */
290 FREE_BLOCKS (block_list);
291 errno = saved_errno;
292 return (char *) msgid;
295 /* We don't want libintl.a to depend on any other library. So
296 we avoid the non-standard function stpcpy. In GNU C Library
297 this function is available, though. Also allow the symbol
298 HAVE_STPCPY to be defined. */
299 stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
302 /* Now determine the symbolic name of CATEGORY and its value. */
303 categoryname = category_to_name (category);
304 categoryvalue = guess_category_value (category, categoryname);
306 xdomainname = (char *) alloca (strlen (categoryname)
307 + strlen (domainname) + 5);
308 ADD_BLOCK (block_list, xdomainname);
309 /* We don't want libintl.a to depend on any other library. So we
310 avoid the non-standard function stpcpy. In GNU C Library this
311 function is available, though. Also allow the symbol HAVE_STPCPY
312 to be defined. */
313 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
314 domainname),
315 ".mo");
317 /* Creating working area. */
318 single_locale = (char *) alloca (strlen (categoryvalue) + 1);
319 ADD_BLOCK (block_list, single_locale);
322 /* Search for the given string. This is a loop because we perhaps
323 got an ordered list of languages to consider for th translation. */
324 while (1)
326 /* Make CATEGORYVALUE point to the next element of the list. */
327 while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
328 ++categoryvalue;
329 if (categoryvalue[0] == '\0')
331 /* The whole contents of CATEGORYVALUE has been searched but
332 no valid entry has been found. We solve this situation
333 by implicitely appending a "C" entry, i.e. no translation
334 will take place. */
335 single_locale[0] = 'C';
336 single_locale[1] = '\0';
338 else
340 char *cp = single_locale;
341 while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
342 *cp++ = *categoryvalue++;
343 *cp = '\0';
346 /* If the current locale value is C (or POSIX) we don't load a
347 domain. Return the MSGID. */
348 if (strcmp (single_locale, "C") == 0
349 || strcmp (single_locale, "POSIX") == 0)
351 FREE_BLOCKS (block_list);
352 errno = saved_errno;
353 return (char *) msgid;
357 /* Find structure describing the message catalog matching the
358 DOMAINNAME and CATEGORY. */
359 domain = _nl_find_domain (dirname, single_locale, xdomainname);
361 if (domain != NULL)
363 retval = find_msg (domain, msgid);
365 if (retval == NULL)
367 int cnt;
369 for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
371 retval = find_msg (domain->successor[cnt], msgid);
373 if (retval != NULL)
374 break;
378 if (retval != NULL)
380 FREE_BLOCKS (block_list);
381 errno = saved_errno;
382 return retval;
386 /* NOTREACHED */
389 #ifdef _LIBC
390 /* Alias for function name in GNU C Library. */
391 weak_alias (__dcgettext, dcgettext);
392 #endif
395 static char *
396 find_msg (domain_file, msgid)
397 struct loaded_l10nfile *domain_file;
398 const char *msgid;
400 size_t top, act, bottom;
401 struct loaded_domain *domain;
403 if (domain_file->decided == 0)
404 _nl_load_domain (domain_file);
406 if (domain_file->data == NULL)
407 return NULL;
409 domain = (struct loaded_domain *) domain_file->data;
411 /* Locate the MSGID and its translation. */
412 if (domain->hash_size > 2 && domain->hash_tab != NULL)
414 /* Use the hashing table. */
415 nls_uint32 len = strlen (msgid);
416 nls_uint32 hash_val = hash_string (msgid);
417 nls_uint32 idx = hash_val % domain->hash_size;
418 nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
419 nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
421 if (nstr == 0)
422 /* Hash table entry is empty. */
423 return NULL;
425 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
426 && strcmp (msgid,
427 domain->data + W (domain->must_swap,
428 domain->orig_tab[nstr - 1].offset)) == 0)
429 return (char *) domain->data + W (domain->must_swap,
430 domain->trans_tab[nstr - 1].offset);
432 while (1)
434 if (idx >= domain->hash_size - incr)
435 idx -= domain->hash_size - incr;
436 else
437 idx += incr;
439 nstr = W (domain->must_swap, domain->hash_tab[idx]);
440 if (nstr == 0)
441 /* Hash table entry is empty. */
442 return NULL;
444 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
445 && strcmp (msgid,
446 domain->data + W (domain->must_swap,
447 domain->orig_tab[nstr - 1].offset))
448 == 0)
449 return (char *) domain->data
450 + W (domain->must_swap, domain->trans_tab[nstr - 1].offset);
452 /* NOTREACHED */
455 /* Now we try the default method: binary search in the sorted
456 array of messages. */
457 bottom = 0;
458 top = domain->nstrings;
459 while (bottom < top)
461 int cmp_val;
463 act = (bottom + top) / 2;
464 cmp_val = strcmp (msgid, domain->data
465 + W (domain->must_swap,
466 domain->orig_tab[act].offset));
467 if (cmp_val < 0)
468 top = act;
469 else if (cmp_val > 0)
470 bottom = act + 1;
471 else
472 break;
475 /* If an translation is found return this. */
476 return bottom >= top ? NULL : (char *) domain->data
477 + W (domain->must_swap,
478 domain->trans_tab[act].offset);
482 /* Return string representation of locale CATEGORY. */
483 static const char *
484 category_to_name (category)
485 int category;
487 const char *retval;
489 switch (category)
491 #ifdef LC_COLLATE
492 case LC_COLLATE:
493 retval = "LC_COLLATE";
494 break;
495 #endif
496 #ifdef LC_CTYPE
497 case LC_CTYPE:
498 retval = "LC_CTYPE";
499 break;
500 #endif
501 #ifdef LC_MONETARY
502 case LC_MONETARY:
503 retval = "LC_MONETARY";
504 break;
505 #endif
506 #ifdef LC_NUMERIC
507 case LC_NUMERIC:
508 retval = "LC_NUMERIC";
509 break;
510 #endif
511 #ifdef LC_TIME
512 case LC_TIME:
513 retval = "LC_TIME";
514 break;
515 #endif
516 #ifdef LC_MESSAGES
517 case LC_MESSAGES:
518 retval = "LC_MESSAGES";
519 break;
520 #endif
521 #ifdef LC_RESPONSE
522 case LC_RESPONSE:
523 retval = "LC_RESPONSE";
524 break;
525 #endif
526 #ifdef LC_ALL
527 case LC_ALL:
528 /* This might not make sense but is perhaps better than any other
529 value. */
530 retval = "LC_ALL";
531 break;
532 #endif
533 default:
534 /* If you have a better idea for a default value let me know. */
535 retval = "LC_XXX";
538 return retval;
541 /* Guess value of current locale from value of the environment variables. */
542 static const char *guess_category_value (category, categoryname)
543 int category;
544 const char *categoryname;
546 const char *retval;
548 /* The highest priority value is the `LANGUAGE' environment
549 variable. This is a GNU extension. */
550 retval = getenv ("LANGUAGE");
551 if (retval != NULL && retval[0] != '\0')
552 return retval;
554 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
555 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
556 systems this can be done by the `setlocale' function itself. */
557 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
558 return setlocale (category, NULL);
559 #else
560 /* Setting of LC_ALL overwrites all other. */
561 retval = getenv ("LC_ALL");
562 if (retval != NULL && retval[0] != '\0')
563 return retval;
565 /* Next comes the name of the desired category. */
566 retval = getenv (categoryname);
567 if (retval != NULL && retval[0] != '\0')
568 return retval;
570 /* Last possibility is the LANG environment variable. */
571 retval = getenv ("LANG");
572 if (retval != NULL && retval[0] != '\0')
573 return retval;
575 /* We use C as the default domain. POSIX says this is implementation
576 defined. */
577 return "C";
578 #endif
581 /* @@ begin of epilog @@ */
583 /* We don't want libintl.a to depend on any other library. So we
584 avoid the non-standard function stpcpy. In GNU C Library this
585 function is available, though. Also allow the symbol HAVE_STPCPY
586 to be defined. */
587 #if !_LIBC && !HAVE_STPCPY
588 static char *
589 stpcpy (dest, src)
590 char *dest;
591 const char *src;
593 while ((*dest++ = *src++) != '\0')
594 /* Do nothing. */ ;
595 return dest - 1;
597 #endif