update from main archive 961009
[glibc.git] / intl / dcgettext.c
blob592f724eb12a1f46b9b4e41689c02700b0f94bd5
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
49 #ifndef __set_errno
50 # define __set_errno(val) errno = (val)
51 #endif
53 #if defined STDC_HEADERS || defined _LIBC
54 # include <stdlib.h>
55 #else
56 char *getenv ();
57 # ifdef HAVE_MALLOC_H
58 # include <malloc.h>
59 # else
60 void free ();
61 # endif
62 #endif
64 #if defined HAVE_STRING_H || defined _LIBC
65 # ifndef _GNU_SOURCE
66 # define _GNU_SOURCE 1
67 # endif
68 # include <string.h>
69 #else
70 # include <strings.h>
71 #endif
72 #if !HAVE_STRCHR && !defined _LIBC
73 # ifndef strchr
74 # define strchr index
75 # endif
76 #endif
78 #if defined HAVE_UNISTD_H || defined _LIBC
79 # include <unistd.h>
80 #endif
82 #include "gettext.h"
83 #include "gettextP.h"
84 #ifdef _LIBC
85 # include <libintl.h>
86 #else
87 # include "libgettext.h"
88 #endif
89 #include "hash-string.h"
91 /* @@ end of prolog @@ */
93 #ifdef _LIBC
94 /* Rename the non ANSI C functions. This is required by the standard
95 because some ANSI C functions will require linking with this object
96 file and the name space must not be polluted. */
97 # define getcwd __getcwd
98 # define stpcpy __stpcpy
99 #else
100 # if !defined HAVE_GETCWD
101 char *getwd ();
102 # define getcwd(buf, max) getwd (buf)
103 # else
104 char *getcwd ();
105 # endif
106 # ifndef HAVE_STPCPY
107 static char *stpcpy PARAMS ((char *dest, const char *src));
108 # endif
109 #endif
111 /* Amount to increase buffer size by in each try. */
112 #define PATH_INCR 32
114 /* The following is from pathmax.h. */
115 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
116 PATH_MAX but might cause redefinition warnings when sys/param.h is
117 later included (as on MORE/BSD 4.3). */
118 #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
119 # include <limits.h>
120 #endif
122 #ifndef _POSIX_PATH_MAX
123 # define _POSIX_PATH_MAX 255
124 #endif
126 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
127 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
128 #endif
130 /* Don't include sys/param.h if it already has been. */
131 #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
132 # include <sys/param.h>
133 #endif
135 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
136 # define PATH_MAX MAXPATHLEN
137 #endif
139 #ifndef PATH_MAX
140 # define PATH_MAX _POSIX_PATH_MAX
141 #endif
143 /* XPG3 defines the result of `setlocale (category, NULL)' as:
144 ``Directs `setlocale()' to query `category' and return the current
145 setting of `local'.''
146 However it does not specify the exact format. And even worse: POSIX
147 defines this not at all. So we can use this feature only on selected
148 system (e.g. those using GNU C Library). */
149 #ifdef _LIBC
150 # define HAVE_LOCALE_NULL
151 #endif
153 /* Name of the default domain used for gettext(3) prior any call to
154 textdomain(3). The default value for this is "messages". */
155 const char _nl_default_default_domain[] = "messages";
157 /* Value used as the default domain for gettext(3). */
158 const char *_nl_current_default_domain = _nl_default_default_domain;
160 /* Contains the default location of the message catalogs. */
161 const char _nl_default_dirname[] = GNULOCALEDIR;
163 /* List with bindings of specific domains created by bindtextdomain()
164 calls. */
165 struct binding *_nl_domain_bindings;
167 /* Prototypes for local functions. */
168 static char *find_msg PARAMS ((struct loaded_l10nfile *domain_file,
169 const char *msgid));
170 static const char *category_to_name PARAMS ((int category));
171 static const char *guess_category_value PARAMS ((int category,
172 const char *categoryname));
175 /* For those loosing systems which don't have `alloca' we have to add
176 some additional code emulating it. */
177 #ifdef HAVE_ALLOCA
178 /* Nothing has to be done. */
179 # define ADD_BLOCK(list, address) /* nothing */
180 # define FREE_BLOCKS(list) /* nothing */
181 #else
182 struct block_list
184 void *address;
185 struct block_list *next;
187 # define ADD_BLOCK(list, addr) \
188 do { \
189 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
190 /* If we cannot get a free block we cannot add the new element to \
191 the list. */ \
192 if (newp != NULL) { \
193 newp->address = (addr); \
194 newp->next = (list); \
195 (list) = newp; \
197 } while (0)
198 # define FREE_BLOCKS(list) \
199 do { \
200 while (list != NULL) { \
201 struct block_list *old = list; \
202 list = list->next; \
203 free (old); \
205 } while (0)
206 # undef alloca
207 # define alloca(size) (malloc (size))
208 #endif /* have alloca */
211 /* Names for the libintl functions are a problem. They must not clash
212 with existing names and they should follow ANSI C. But this source
213 code is also used in GNU C Library where the names have a __
214 prefix. So we have to make a difference here. */
215 #ifdef _LIBC
216 # define DCGETTEXT __dcgettext
217 #else
218 # define DCGETTEXT dcgettext__
219 #endif
221 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
222 locale. */
223 char *
224 DCGETTEXT (domainname, msgid, category)
225 const char *domainname;
226 const char *msgid;
227 int category;
229 #ifndef HAVE_ALLOCA
230 struct block_list *block_list = NULL;
231 #endif
232 struct loaded_l10nfile *domain;
233 struct binding *binding;
234 const char *categoryname;
235 const char *categoryvalue;
236 char *dirname, *xdomainname;
237 char *single_locale;
238 char *retval;
239 int saved_errno = errno;
241 /* If no real MSGID is given return NULL. */
242 if (msgid == NULL)
243 return NULL;
245 /* If DOMAINNAME is NULL, we are interested in the default domain. If
246 CATEGORY is not LC_MESSAGES this might not make much sense but the
247 defintion left this undefined. */
248 if (domainname == NULL)
249 domainname = _nl_current_default_domain;
251 /* First find matching binding. */
252 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
254 int compare = strcmp (domainname, binding->domainname);
255 if (compare == 0)
256 /* We found it! */
257 break;
258 if (compare < 0)
260 /* It is not in the list. */
261 binding = NULL;
262 break;
266 if (binding == NULL)
267 dirname = (char *) _nl_default_dirname;
268 else if (binding->dirname[0] == '/')
269 dirname = binding->dirname;
270 else
272 /* We have a relative path. Make it absolute now. */
273 size_t dirname_len = strlen (binding->dirname) + 1;
274 size_t path_max;
275 char *ret;
277 path_max = (unsigned) PATH_MAX;
278 path_max += 2; /* The getcwd docs say to do this. */
280 dirname = (char *) alloca (path_max + dirname_len);
281 ADD_BLOCK (block_list, dirname);
283 __set_errno (0);
284 while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
286 path_max += PATH_INCR;
287 dirname = (char *) alloca (path_max + dirname_len);
288 ADD_BLOCK (block_list, dirname);
289 __set_errno (0);
292 if (ret == NULL)
294 /* We cannot get the current working directory. Don't signal an
295 error but simply return the default string. */
296 FREE_BLOCKS (block_list);
297 __set_errno (saved_errno);
298 return (char *) msgid;
301 /* We don't want libintl.a to depend on any other library. So
302 we avoid the non-standard function stpcpy. In GNU C Library
303 this function is available, though. Also allow the symbol
304 HAVE_STPCPY to be defined. */
305 stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
308 /* Now determine the symbolic name of CATEGORY and its value. */
309 categoryname = category_to_name (category);
310 categoryvalue = guess_category_value (category, categoryname);
312 xdomainname = (char *) alloca (strlen (categoryname)
313 + strlen (domainname) + 5);
314 ADD_BLOCK (block_list, xdomainname);
315 /* We don't want libintl.a to depend on any other library. So we
316 avoid the non-standard function stpcpy. In GNU C Library this
317 function is available, though. Also allow the symbol HAVE_STPCPY
318 to be defined. */
319 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
320 domainname),
321 ".mo");
323 /* Creating working area. */
324 single_locale = (char *) alloca (strlen (categoryvalue) + 1);
325 ADD_BLOCK (block_list, single_locale);
328 /* Search for the given string. This is a loop because we perhaps
329 got an ordered list of languages to consider for th translation. */
330 while (1)
332 /* Make CATEGORYVALUE point to the next element of the list. */
333 while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
334 ++categoryvalue;
335 if (categoryvalue[0] == '\0')
337 /* The whole contents of CATEGORYVALUE has been searched but
338 no valid entry has been found. We solve this situation
339 by implicitely appending a "C" entry, i.e. no translation
340 will take place. */
341 single_locale[0] = 'C';
342 single_locale[1] = '\0';
344 else
346 char *cp = single_locale;
347 while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
348 *cp++ = *categoryvalue++;
349 *cp = '\0';
352 /* If the current locale value is C (or POSIX) we don't load a
353 domain. Return the MSGID. */
354 if (strcmp (single_locale, "C") == 0
355 || strcmp (single_locale, "POSIX") == 0)
357 FREE_BLOCKS (block_list);
358 __set_errno (saved_errno);
359 return (char *) msgid;
363 /* Find structure describing the message catalog matching the
364 DOMAINNAME and CATEGORY. */
365 domain = _nl_find_domain (dirname, single_locale, xdomainname);
367 if (domain != NULL)
369 retval = find_msg (domain, msgid);
371 if (retval == NULL)
373 int cnt;
375 for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
377 retval = find_msg (domain->successor[cnt], msgid);
379 if (retval != NULL)
380 break;
384 if (retval != NULL)
386 FREE_BLOCKS (block_list);
387 __set_errno (saved_errno);
388 return retval;
392 /* NOTREACHED */
395 #ifdef _LIBC
396 /* Alias for function name in GNU C Library. */
397 weak_alias (__dcgettext, dcgettext);
398 #endif
401 static char *
402 find_msg (domain_file, msgid)
403 struct loaded_l10nfile *domain_file;
404 const char *msgid;
406 size_t top, act, bottom;
407 struct loaded_domain *domain;
409 if (domain_file->decided == 0)
410 _nl_load_domain (domain_file);
412 if (domain_file->data == NULL)
413 return NULL;
415 domain = (struct loaded_domain *) domain_file->data;
417 /* Locate the MSGID and its translation. */
418 if (domain->hash_size > 2 && domain->hash_tab != NULL)
420 /* Use the hashing table. */
421 nls_uint32 len = strlen (msgid);
422 nls_uint32 hash_val = hash_string (msgid);
423 nls_uint32 idx = hash_val % domain->hash_size;
424 nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
425 nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
427 if (nstr == 0)
428 /* Hash table entry is empty. */
429 return NULL;
431 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
432 && strcmp (msgid,
433 domain->data + W (domain->must_swap,
434 domain->orig_tab[nstr - 1].offset)) == 0)
435 return (char *) domain->data + W (domain->must_swap,
436 domain->trans_tab[nstr - 1].offset);
438 while (1)
440 if (idx >= domain->hash_size - incr)
441 idx -= domain->hash_size - incr;
442 else
443 idx += incr;
445 nstr = W (domain->must_swap, domain->hash_tab[idx]);
446 if (nstr == 0)
447 /* Hash table entry is empty. */
448 return NULL;
450 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
451 && strcmp (msgid,
452 domain->data + W (domain->must_swap,
453 domain->orig_tab[nstr - 1].offset))
454 == 0)
455 return (char *) domain->data
456 + W (domain->must_swap, domain->trans_tab[nstr - 1].offset);
458 /* NOTREACHED */
461 /* Now we try the default method: binary search in the sorted
462 array of messages. */
463 bottom = 0;
464 top = domain->nstrings;
465 while (bottom < top)
467 int cmp_val;
469 act = (bottom + top) / 2;
470 cmp_val = strcmp (msgid, domain->data
471 + W (domain->must_swap,
472 domain->orig_tab[act].offset));
473 if (cmp_val < 0)
474 top = act;
475 else if (cmp_val > 0)
476 bottom = act + 1;
477 else
478 break;
481 /* If an translation is found return this. */
482 return bottom >= top ? NULL : (char *) domain->data
483 + W (domain->must_swap,
484 domain->trans_tab[act].offset);
488 /* Return string representation of locale CATEGORY. */
489 static const char *
490 category_to_name (category)
491 int category;
493 const char *retval;
495 switch (category)
497 #ifdef LC_COLLATE
498 case LC_COLLATE:
499 retval = "LC_COLLATE";
500 break;
501 #endif
502 #ifdef LC_CTYPE
503 case LC_CTYPE:
504 retval = "LC_CTYPE";
505 break;
506 #endif
507 #ifdef LC_MONETARY
508 case LC_MONETARY:
509 retval = "LC_MONETARY";
510 break;
511 #endif
512 #ifdef LC_NUMERIC
513 case LC_NUMERIC:
514 retval = "LC_NUMERIC";
515 break;
516 #endif
517 #ifdef LC_TIME
518 case LC_TIME:
519 retval = "LC_TIME";
520 break;
521 #endif
522 #ifdef LC_MESSAGES
523 case LC_MESSAGES:
524 retval = "LC_MESSAGES";
525 break;
526 #endif
527 #ifdef LC_RESPONSE
528 case LC_RESPONSE:
529 retval = "LC_RESPONSE";
530 break;
531 #endif
532 #ifdef LC_ALL
533 case LC_ALL:
534 /* This might not make sense but is perhaps better than any other
535 value. */
536 retval = "LC_ALL";
537 break;
538 #endif
539 default:
540 /* If you have a better idea for a default value let me know. */
541 retval = "LC_XXX";
544 return retval;
547 /* Guess value of current locale from value of the environment variables. */
548 static const char *guess_category_value (category, categoryname)
549 int category;
550 const char *categoryname;
552 const char *retval;
554 /* The highest priority value is the `LANGUAGE' environment
555 variable. This is a GNU extension. */
556 retval = getenv ("LANGUAGE");
557 if (retval != NULL && retval[0] != '\0')
558 return retval;
560 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
561 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
562 systems this can be done by the `setlocale' function itself. */
563 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
564 return setlocale (category, NULL);
565 #else
566 /* Setting of LC_ALL overwrites all other. */
567 retval = getenv ("LC_ALL");
568 if (retval != NULL && retval[0] != '\0')
569 return retval;
571 /* Next comes the name of the desired category. */
572 retval = getenv (categoryname);
573 if (retval != NULL && retval[0] != '\0')
574 return retval;
576 /* Last possibility is the LANG environment variable. */
577 retval = getenv ("LANG");
578 if (retval != NULL && retval[0] != '\0')
579 return retval;
581 /* We use C as the default domain. POSIX says this is implementation
582 defined. */
583 return "C";
584 #endif
587 /* @@ begin of epilog @@ */
589 /* We don't want libintl.a to depend on any other library. So we
590 avoid the non-standard function stpcpy. In GNU C Library this
591 function is available, though. Also allow the symbol HAVE_STPCPY
592 to be defined. */
593 #if !_LIBC && !HAVE_STPCPY
594 static char *
595 stpcpy (dest, src)
596 char *dest;
597 const char *src;
599 while ((*dest++ = *src++) != '\0')
600 /* Do nothing. */ ;
601 return dest - 1;
603 #endif