Sat Jul 13 02:47:53 1996 David Mosberger-Tang <davidm@azstarnet.com>
[glibc/history.git] / intl / dcgettext.c
blob7bbe170b76dc7a39dffcda01c28811bd735af7c7
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 # ifndef _GNU_SOURCE
63 # define _GNU_SOURCE 1
64 # endif
65 # include <string.h>
66 #else
67 # include <strings.h>
68 #endif
69 #if !HAVE_STRCHR && !defined _LIBC
70 # ifndef strchr
71 # define strchr index
72 # endif
73 #endif
75 #if defined HAVE_UNISTD_H || defined _LIBC
76 # include <unistd.h>
77 #endif
79 #include "gettext.h"
80 #include "gettextP.h"
81 #ifdef _LIBC
82 # include <libintl.h>
83 #else
84 # include "libgettext.h"
85 #endif
86 #include "hash-string.h"
88 /* @@ end of prolog @@ */
90 #ifdef _LIBC
91 /* Rename the non ANSI C functions. This is required by the standard
92 because some ANSI C functions will require linking with this object
93 file and the name space must not be polluted. */
94 # define getcwd __getcwd
95 # define stpcpy __stpcpy
96 #else
97 # if !defined HAVE_GETCWD
98 char *getwd ();
99 # define getcwd(buf, max) getwd (buf)
100 # else
101 char *getcwd ();
102 # endif
103 # ifndef HAVE_STPCPY
104 static char *stpcpy PARAMS ((char *dest, const char *src));
105 # endif
106 #endif
108 /* Amount to increase buffer size by in each try. */
109 #define PATH_INCR 32
111 /* The following is from pathmax.h. */
112 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
113 PATH_MAX but might cause redefinition warnings when sys/param.h is
114 later included (as on MORE/BSD 4.3). */
115 #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
116 # include <limits.h>
117 #endif
119 #ifndef _POSIX_PATH_MAX
120 # define _POSIX_PATH_MAX 255
121 #endif
123 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
124 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
125 #endif
127 /* Don't include sys/param.h if it already has been. */
128 #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
129 # include <sys/param.h>
130 #endif
132 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
133 # define PATH_MAX MAXPATHLEN
134 #endif
136 #ifndef PATH_MAX
137 # define PATH_MAX _POSIX_PATH_MAX
138 #endif
140 /* XPG3 defines the result of `setlocale (category, NULL)' as:
141 ``Directs `setlocale()' to query `category' and return the current
142 setting of `local'.''
143 However it does not specify the exact format. And even worse: POSIX
144 defines this not at all. So we can use this feature only on selected
145 system (e.g. those using GNU C Library). */
146 #ifdef _LIBC
147 # define HAVE_LOCALE_NULL
148 #endif
150 /* Name of the default domain used for gettext(3) prior any call to
151 textdomain(3). The default value for this is "messages". */
152 const char _nl_default_default_domain[] = "messages";
154 /* Value used as the default domain for gettext(3). */
155 const char *_nl_current_default_domain = _nl_default_default_domain;
157 /* Contains the default location of the message catalogs. */
158 const char _nl_default_dirname[] = GNULOCALEDIR;
160 /* List with bindings of specific domains created by bindtextdomain()
161 calls. */
162 struct binding *_nl_domain_bindings;
164 /* Prototypes for local functions. */
165 static char *find_msg PARAMS ((struct loaded_l10nfile *domain_file,
166 const char *msgid));
167 static const char *category_to_name PARAMS ((int category));
168 static const char *guess_category_value PARAMS ((int category,
169 const char *categoryname));
172 /* For those loosing systems which don't have `alloca' we have to add
173 some additional code emulating it. */
174 #ifdef HAVE_ALLOCA
175 /* Nothing has to be done. */
176 # define ADD_BLOCK(list, address) /* nothing */
177 # define FREE_BLOCKS(list) /* nothing */
178 #else
179 struct block_list
181 void *address;
182 struct block_list *next;
184 # define ADD_BLOCK(list, addr) \
185 do { \
186 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
187 /* If we cannot get a free block we cannot add the new element to \
188 the list. */ \
189 if (newp != NULL) { \
190 newp->address = (addr); \
191 newp->next = (list); \
192 (list) = newp; \
194 } while (0)
195 # define FREE_BLOCKS(list) \
196 do { \
197 while (list != NULL) { \
198 struct block_list *old = list; \
199 list = list->next; \
200 free (old); \
202 } while (0)
203 # undef alloca
204 # define alloca(size) (malloc (size))
205 #endif /* have alloca */
208 /* Names for the libintl functions are a problem. They must not clash
209 with existing names and they should follow ANSI C. But this source
210 code is also used in GNU C Library where the names have a __
211 prefix. So we have to make a difference here. */
212 #ifdef _LIBC
213 # define DCGETTEXT __dcgettext
214 #else
215 # define DCGETTEXT dcgettext__
216 #endif
218 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
219 locale. */
220 char *
221 DCGETTEXT (domainname, msgid, category)
222 const char *domainname;
223 const char *msgid;
224 int category;
226 #ifndef HAVE_ALLOCA
227 struct block_list *block_list = NULL;
228 #endif
229 struct loaded_l10nfile *domain;
230 struct binding *binding;
231 const char *categoryname;
232 const char *categoryvalue;
233 char *dirname, *xdomainname;
234 char *single_locale;
235 char *retval;
236 int saved_errno = errno;
238 /* If no real MSGID is given return NULL. */
239 if (msgid == NULL)
240 return NULL;
242 /* If DOMAINNAME is NULL, we are interested in the default domain. If
243 CATEGORY is not LC_MESSAGES this might not make much sense but the
244 defintion left this undefined. */
245 if (domainname == NULL)
246 domainname = _nl_current_default_domain;
248 /* First find matching binding. */
249 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
251 int compare = strcmp (domainname, binding->domainname);
252 if (compare == 0)
253 /* We found it! */
254 break;
255 if (compare < 0)
257 /* It is not in the list. */
258 binding = NULL;
259 break;
263 if (binding == NULL)
264 dirname = (char *) _nl_default_dirname;
265 else if (binding->dirname[0] == '/')
266 dirname = binding->dirname;
267 else
269 /* We have a relative path. Make it absolute now. */
270 size_t dirname_len = strlen (binding->dirname) + 1;
271 size_t path_max;
272 char *ret;
274 path_max = (unsigned) PATH_MAX;
275 path_max += 2; /* The getcwd docs say to do this. */
277 dirname = (char *) alloca (path_max + dirname_len);
278 ADD_BLOCK (block_list, dirname);
280 errno = 0;
281 while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
283 path_max += PATH_INCR;
284 dirname = (char *) alloca (path_max + dirname_len);
285 ADD_BLOCK (block_list, dirname);
286 errno = 0;
289 if (ret == NULL)
291 /* We cannot get the current working directory. Don't signal an
292 error but simply return the default string. */
293 FREE_BLOCKS (block_list);
294 errno = saved_errno;
295 return (char *) msgid;
298 /* We don't want libintl.a to depend on any other library. So
299 we avoid the non-standard function stpcpy. In GNU C Library
300 this function is available, though. Also allow the symbol
301 HAVE_STPCPY to be defined. */
302 stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
305 /* Now determine the symbolic name of CATEGORY and its value. */
306 categoryname = category_to_name (category);
307 categoryvalue = guess_category_value (category, categoryname);
309 xdomainname = (char *) alloca (strlen (categoryname)
310 + strlen (domainname) + 5);
311 ADD_BLOCK (block_list, xdomainname);
312 /* We don't want libintl.a to depend on any other library. So we
313 avoid the non-standard function stpcpy. In GNU C Library this
314 function is available, though. Also allow the symbol HAVE_STPCPY
315 to be defined. */
316 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
317 domainname),
318 ".mo");
320 /* Creating working area. */
321 single_locale = (char *) alloca (strlen (categoryvalue) + 1);
322 ADD_BLOCK (block_list, single_locale);
325 /* Search for the given string. This is a loop because we perhaps
326 got an ordered list of languages to consider for th translation. */
327 while (1)
329 /* Make CATEGORYVALUE point to the next element of the list. */
330 while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
331 ++categoryvalue;
332 if (categoryvalue[0] == '\0')
334 /* The whole contents of CATEGORYVALUE has been searched but
335 no valid entry has been found. We solve this situation
336 by implicitely appending a "C" entry, i.e. no translation
337 will take place. */
338 single_locale[0] = 'C';
339 single_locale[1] = '\0';
341 else
343 char *cp = single_locale;
344 while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
345 *cp++ = *categoryvalue++;
346 *cp = '\0';
349 /* If the current locale value is C (or POSIX) we don't load a
350 domain. Return the MSGID. */
351 if (strcmp (single_locale, "C") == 0
352 || strcmp (single_locale, "POSIX") == 0)
354 FREE_BLOCKS (block_list);
355 errno = saved_errno;
356 return (char *) msgid;
360 /* Find structure describing the message catalog matching the
361 DOMAINNAME and CATEGORY. */
362 domain = _nl_find_domain (dirname, single_locale, xdomainname);
364 if (domain != NULL)
366 retval = find_msg (domain, msgid);
368 if (retval == NULL)
370 int cnt;
372 for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
374 retval = find_msg (domain->successor[cnt], msgid);
376 if (retval != NULL)
377 break;
381 if (retval != NULL)
383 FREE_BLOCKS (block_list);
384 errno = saved_errno;
385 return retval;
389 /* NOTREACHED */
392 #ifdef _LIBC
393 /* Alias for function name in GNU C Library. */
394 weak_alias (__dcgettext, dcgettext);
395 #endif
398 static char *
399 find_msg (domain_file, msgid)
400 struct loaded_l10nfile *domain_file;
401 const char *msgid;
403 size_t top, act, bottom;
404 struct loaded_domain *domain;
406 if (domain_file->decided == 0)
407 _nl_load_domain (domain_file);
409 if (domain_file->data == NULL)
410 return NULL;
412 domain = (struct loaded_domain *) domain_file->data;
414 /* Locate the MSGID and its translation. */
415 if (domain->hash_size > 2 && domain->hash_tab != NULL)
417 /* Use the hashing table. */
418 nls_uint32 len = strlen (msgid);
419 nls_uint32 hash_val = hash_string (msgid);
420 nls_uint32 idx = hash_val % domain->hash_size;
421 nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
422 nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
424 if (nstr == 0)
425 /* Hash table entry is empty. */
426 return NULL;
428 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
429 && strcmp (msgid,
430 domain->data + W (domain->must_swap,
431 domain->orig_tab[nstr - 1].offset)) == 0)
432 return (char *) domain->data + W (domain->must_swap,
433 domain->trans_tab[nstr - 1].offset);
435 while (1)
437 if (idx >= domain->hash_size - incr)
438 idx -= domain->hash_size - incr;
439 else
440 idx += incr;
442 nstr = W (domain->must_swap, domain->hash_tab[idx]);
443 if (nstr == 0)
444 /* Hash table entry is empty. */
445 return NULL;
447 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
448 && strcmp (msgid,
449 domain->data + W (domain->must_swap,
450 domain->orig_tab[nstr - 1].offset))
451 == 0)
452 return (char *) domain->data
453 + W (domain->must_swap, domain->trans_tab[nstr - 1].offset);
455 /* NOTREACHED */
458 /* Now we try the default method: binary search in the sorted
459 array of messages. */
460 bottom = 0;
461 top = domain->nstrings;
462 while (bottom < top)
464 int cmp_val;
466 act = (bottom + top) / 2;
467 cmp_val = strcmp (msgid, domain->data
468 + W (domain->must_swap,
469 domain->orig_tab[act].offset));
470 if (cmp_val < 0)
471 top = act;
472 else if (cmp_val > 0)
473 bottom = act + 1;
474 else
475 break;
478 /* If an translation is found return this. */
479 return bottom >= top ? NULL : (char *) domain->data
480 + W (domain->must_swap,
481 domain->trans_tab[act].offset);
485 /* Return string representation of locale CATEGORY. */
486 static const char *
487 category_to_name (category)
488 int category;
490 const char *retval;
492 switch (category)
494 #ifdef LC_COLLATE
495 case LC_COLLATE:
496 retval = "LC_COLLATE";
497 break;
498 #endif
499 #ifdef LC_CTYPE
500 case LC_CTYPE:
501 retval = "LC_CTYPE";
502 break;
503 #endif
504 #ifdef LC_MONETARY
505 case LC_MONETARY:
506 retval = "LC_MONETARY";
507 break;
508 #endif
509 #ifdef LC_NUMERIC
510 case LC_NUMERIC:
511 retval = "LC_NUMERIC";
512 break;
513 #endif
514 #ifdef LC_TIME
515 case LC_TIME:
516 retval = "LC_TIME";
517 break;
518 #endif
519 #ifdef LC_MESSAGES
520 case LC_MESSAGES:
521 retval = "LC_MESSAGES";
522 break;
523 #endif
524 #ifdef LC_RESPONSE
525 case LC_RESPONSE:
526 retval = "LC_RESPONSE";
527 break;
528 #endif
529 #ifdef LC_ALL
530 case LC_ALL:
531 /* This might not make sense but is perhaps better than any other
532 value. */
533 retval = "LC_ALL";
534 break;
535 #endif
536 default:
537 /* If you have a better idea for a default value let me know. */
538 retval = "LC_XXX";
541 return retval;
544 /* Guess value of current locale from value of the environment variables. */
545 static const char *guess_category_value (category, categoryname)
546 int category;
547 const char *categoryname;
549 const char *retval;
551 /* The highest priority value is the `LANGUAGE' environment
552 variable. This is a GNU extension. */
553 retval = getenv ("LANGUAGE");
554 if (retval != NULL && retval[0] != '\0')
555 return retval;
557 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
558 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
559 systems this can be done by the `setlocale' function itself. */
560 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
561 return setlocale (category, NULL);
562 #else
563 /* Setting of LC_ALL overwrites all other. */
564 retval = getenv ("LC_ALL");
565 if (retval != NULL && retval[0] != '\0')
566 return retval;
568 /* Next comes the name of the desired category. */
569 retval = getenv (categoryname);
570 if (retval != NULL && retval[0] != '\0')
571 return retval;
573 /* Last possibility is the LANG environment variable. */
574 retval = getenv ("LANG");
575 if (retval != NULL && retval[0] != '\0')
576 return retval;
578 /* We use C as the default domain. POSIX says this is implementation
579 defined. */
580 return "C";
581 #endif
584 /* @@ begin of epilog @@ */
586 /* We don't want libintl.a to depend on any other library. So we
587 avoid the non-standard function stpcpy. In GNU C Library this
588 function is available, though. Also allow the symbol HAVE_STPCPY
589 to be defined. */
590 #if !_LIBC && !HAVE_STPCPY
591 static char *
592 stpcpy (dest, src)
593 char *dest;
594 const char *src;
596 while ((*dest++ = *src++) != '\0')
597 /* Do nothing. */ ;
598 return dest - 1;
600 #endif