Update.
[glibc.git] / intl / dcgettext.c
blob3557202ea567abe1524ee705b16b8deb664fc833
1 /* Implementation of the dcgettext(3) function.
2 Copyright (C) 1995, 1996, 1997 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.
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 not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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 # ifndef stpcpy
99 # define stpcpy __stpcpy
100 # endif
101 #else
102 # if !defined HAVE_GETCWD
103 char *getwd ();
104 # define getcwd(buf, max) getwd (buf)
105 # else
106 char *getcwd ();
107 # endif
108 # ifndef HAVE_STPCPY
109 static char *stpcpy PARAMS ((char *dest, const char *src));
110 # endif
111 #endif
113 /* Amount to increase buffer size by in each try. */
114 #define PATH_INCR 32
116 /* The following is from pathmax.h. */
117 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
118 PATH_MAX but might cause redefinition warnings when sys/param.h is
119 later included (as on MORE/BSD 4.3). */
120 #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
121 # include <limits.h>
122 #endif
124 #ifndef _POSIX_PATH_MAX
125 # define _POSIX_PATH_MAX 255
126 #endif
128 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
129 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
130 #endif
132 /* Don't include sys/param.h if it already has been. */
133 #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
134 # include <sys/param.h>
135 #endif
137 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
138 # define PATH_MAX MAXPATHLEN
139 #endif
141 #ifndef PATH_MAX
142 # define PATH_MAX _POSIX_PATH_MAX
143 #endif
145 #ifndef internal_function
146 # define internal_function
147 #endif
149 /* XPG3 defines the result of `setlocale (category, NULL)' as:
150 ``Directs `setlocale()' to query `category' and return the current
151 setting of `local'.''
152 However it does not specify the exact format. And even worse: POSIX
153 defines this not at all. So we can use this feature only on selected
154 system (e.g. those using GNU C Library). */
155 #ifdef _LIBC
156 # define HAVE_LOCALE_NULL
157 #endif
159 /* Name of the default domain used for gettext(3) prior any call to
160 textdomain(3). The default value for this is "messages". */
161 const char _nl_default_default_domain[] = "messages";
163 /* Value used as the default domain for gettext(3). */
164 const char *_nl_current_default_domain = _nl_default_default_domain;
166 /* Contains the default location of the message catalogs. */
167 const char _nl_default_dirname[] = GNULOCALEDIR;
169 /* List with bindings of specific domains created by bindtextdomain()
170 calls. */
171 struct binding *_nl_domain_bindings;
173 /* Prototypes for local functions. */
174 static char *find_msg PARAMS ((struct loaded_l10nfile *domain_file,
175 const char *msgid)) internal_function;
176 static const char *category_to_name PARAMS ((int category)) internal_function;
177 static const char *guess_category_value PARAMS ((int category,
178 const char *categoryname))
179 internal_function;
182 /* For those loosing systems which don't have `alloca' we have to add
183 some additional code emulating it. */
184 #ifdef HAVE_ALLOCA
185 /* Nothing has to be done. */
186 # define ADD_BLOCK(list, address) /* nothing */
187 # define FREE_BLOCKS(list) /* nothing */
188 #else
189 struct block_list
191 void *address;
192 struct block_list *next;
194 # define ADD_BLOCK(list, addr) \
195 do { \
196 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
197 /* If we cannot get a free block we cannot add the new element to \
198 the list. */ \
199 if (newp != NULL) { \
200 newp->address = (addr); \
201 newp->next = (list); \
202 (list) = newp; \
204 } while (0)
205 # define FREE_BLOCKS(list) \
206 do { \
207 while (list != NULL) { \
208 struct block_list *old = list; \
209 list = list->next; \
210 free (old); \
212 } while (0)
213 # undef alloca
214 # define alloca(size) (malloc (size))
215 #endif /* have alloca */
218 /* Names for the libintl functions are a problem. They must not clash
219 with existing names and they should follow ANSI C. But this source
220 code is also used in GNU C Library where the names have a __
221 prefix. So we have to make a difference here. */
222 #ifdef _LIBC
223 # define DCGETTEXT __dcgettext
224 #else
225 # define DCGETTEXT dcgettext__
226 #endif
228 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
229 locale. */
230 char *
231 DCGETTEXT (domainname, msgid, category)
232 const char *domainname;
233 const char *msgid;
234 int category;
236 #ifndef HAVE_ALLOCA
237 struct block_list *block_list = NULL;
238 #endif
239 struct loaded_l10nfile *domain;
240 struct binding *binding;
241 const char *categoryname;
242 const char *categoryvalue;
243 char *dirname, *xdomainname;
244 char *single_locale;
245 char *retval;
246 int saved_errno = errno;
248 /* If no real MSGID is given return NULL. */
249 if (msgid == NULL)
250 return NULL;
252 /* If DOMAINNAME is NULL, we are interested in the default domain. If
253 CATEGORY is not LC_MESSAGES this might not make much sense but the
254 defintion left this undefined. */
255 if (domainname == NULL)
256 domainname = _nl_current_default_domain;
258 /* First find matching binding. */
259 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
261 int compare = strcmp (domainname, binding->domainname);
262 if (compare == 0)
263 /* We found it! */
264 break;
265 if (compare < 0)
267 /* It is not in the list. */
268 binding = NULL;
269 break;
273 if (binding == NULL)
274 dirname = (char *) _nl_default_dirname;
275 else if (binding->dirname[0] == '/')
276 dirname = binding->dirname;
277 else
279 /* We have a relative path. Make it absolute now. */
280 size_t dirname_len = strlen (binding->dirname) + 1;
281 size_t path_max;
282 char *ret;
284 path_max = (unsigned) PATH_MAX;
285 path_max += 2; /* The getcwd docs say to do this. */
287 dirname = (char *) alloca (path_max + dirname_len);
288 ADD_BLOCK (block_list, dirname);
290 __set_errno (0);
291 while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
293 path_max += PATH_INCR;
294 dirname = (char *) alloca (path_max + dirname_len);
295 ADD_BLOCK (block_list, dirname);
296 __set_errno (0);
299 if (ret == NULL)
301 /* We cannot get the current working directory. Don't signal an
302 error but simply return the default string. */
303 FREE_BLOCKS (block_list);
304 __set_errno (saved_errno);
305 return (char *) msgid;
308 stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
311 /* Now determine the symbolic name of CATEGORY and its value. */
312 categoryname = category_to_name (category);
313 categoryvalue = guess_category_value (category, categoryname);
315 xdomainname = (char *) alloca (strlen (categoryname)
316 + strlen (domainname) + 5);
317 ADD_BLOCK (block_list, xdomainname);
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 implicitly 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 internal_function
403 find_msg (domain_file, msgid)
404 struct loaded_l10nfile *domain_file;
405 const char *msgid;
407 size_t top, act, bottom;
408 struct loaded_domain *domain;
410 if (domain_file->decided == 0)
411 _nl_load_domain (domain_file);
413 if (domain_file->data == NULL)
414 return NULL;
416 domain = (struct loaded_domain *) domain_file->data;
418 /* Locate the MSGID and its translation. */
419 if (domain->hash_size > 2 && domain->hash_tab != NULL)
421 /* Use the hashing table. */
422 nls_uint32 len = strlen (msgid);
423 nls_uint32 hash_val = hash_string (msgid);
424 nls_uint32 idx = hash_val % domain->hash_size;
425 nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
426 nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
428 if (nstr == 0)
429 /* Hash table entry is empty. */
430 return NULL;
432 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
433 && strcmp (msgid,
434 domain->data + W (domain->must_swap,
435 domain->orig_tab[nstr - 1].offset)) == 0)
436 return (char *) domain->data + W (domain->must_swap,
437 domain->trans_tab[nstr - 1].offset);
439 while (1)
441 if (idx >= domain->hash_size - incr)
442 idx -= domain->hash_size - incr;
443 else
444 idx += incr;
446 nstr = W (domain->must_swap, domain->hash_tab[idx]);
447 if (nstr == 0)
448 /* Hash table entry is empty. */
449 return NULL;
451 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
452 && strcmp (msgid,
453 domain->data + W (domain->must_swap,
454 domain->orig_tab[nstr - 1].offset))
455 == 0)
456 return (char *) domain->data
457 + W (domain->must_swap, domain->trans_tab[nstr - 1].offset);
459 /* NOTREACHED */
462 /* Now we try the default method: binary search in the sorted
463 array of messages. */
464 bottom = 0;
465 top = domain->nstrings;
466 while (bottom < top)
468 int cmp_val;
470 act = (bottom + top) / 2;
471 cmp_val = strcmp (msgid, domain->data
472 + W (domain->must_swap,
473 domain->orig_tab[act].offset));
474 if (cmp_val < 0)
475 top = act;
476 else if (cmp_val > 0)
477 bottom = act + 1;
478 else
479 break;
482 /* If an translation is found return this. */
483 return bottom >= top ? NULL : (char *) domain->data
484 + W (domain->must_swap,
485 domain->trans_tab[act].offset);
489 /* Return string representation of locale CATEGORY. */
490 static const char *
491 internal_function
492 category_to_name (category)
493 int category;
495 const char *retval;
497 switch (category)
499 #ifdef LC_COLLATE
500 case LC_COLLATE:
501 retval = "LC_COLLATE";
502 break;
503 #endif
504 #ifdef LC_CTYPE
505 case LC_CTYPE:
506 retval = "LC_CTYPE";
507 break;
508 #endif
509 #ifdef LC_MONETARY
510 case LC_MONETARY:
511 retval = "LC_MONETARY";
512 break;
513 #endif
514 #ifdef LC_NUMERIC
515 case LC_NUMERIC:
516 retval = "LC_NUMERIC";
517 break;
518 #endif
519 #ifdef LC_TIME
520 case LC_TIME:
521 retval = "LC_TIME";
522 break;
523 #endif
524 #ifdef LC_MESSAGES
525 case LC_MESSAGES:
526 retval = "LC_MESSAGES";
527 break;
528 #endif
529 #ifdef LC_RESPONSE
530 case LC_RESPONSE:
531 retval = "LC_RESPONSE";
532 break;
533 #endif
534 #ifdef LC_ALL
535 case LC_ALL:
536 /* This might not make sense but is perhaps better than any other
537 value. */
538 retval = "LC_ALL";
539 break;
540 #endif
541 default:
542 /* If you have a better idea for a default value let me know. */
543 retval = "LC_XXX";
546 return retval;
549 /* Guess value of current locale from value of the environment variables. */
550 static const char *
551 internal_function
552 guess_category_value (category, categoryname)
553 int category;
554 const char *categoryname;
556 const char *retval;
558 /* The highest priority value is the `LANGUAGE' environment
559 variable. This is a GNU extension. */
560 retval = getenv ("LANGUAGE");
561 if (retval != NULL && retval[0] != '\0')
562 return retval;
564 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
565 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
566 systems this can be done by the `setlocale' function itself. */
567 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
568 return setlocale (category, NULL);
569 #else
570 /* Setting of LC_ALL overwrites all other. */
571 retval = getenv ("LC_ALL");
572 if (retval != NULL && retval[0] != '\0')
573 return retval;
575 /* Next comes the name of the desired category. */
576 retval = getenv (categoryname);
577 if (retval != NULL && retval[0] != '\0')
578 return retval;
580 /* Last possibility is the LANG environment variable. */
581 retval = getenv ("LANG");
582 if (retval != NULL && retval[0] != '\0')
583 return retval;
585 /* We use C as the default domain. POSIX says this is implementation
586 defined. */
587 return "C";
588 #endif
591 /* @@ begin of epilog @@ */
593 /* We don't want libintl.a to depend on any other library. So we
594 avoid the non-standard function stpcpy. In GNU C Library this
595 function is available, though. Also allow the symbol HAVE_STPCPY
596 to be defined. */
597 #if !_LIBC && !HAVE_STPCPY
598 static char *
599 stpcpy (dest, src)
600 char *dest;
601 const char *src;
603 while ((*dest++ = *src++) != '\0')
604 /* Do nothing. */ ;
605 return dest - 1;
607 #endif
610 #ifdef _LIBC
611 /* If we want to free all resources we have to do some work at
612 program's end. */
613 static void __attribute__ ((unused))
614 free_mem (void)
616 struct binding *runp;
618 for (runp = _nl_domain_bindings; runp != NULL; runp = runp->next)
620 free (runp->domainname);
621 if (runp->dirname != _nl_default_dirname)
622 /* Yes, this is a pointer comparison. */
623 free (runp->dirname);
626 if (_nl_current_default_domain != _nl_default_default_domain)
627 /* Yes, again a pointer comparison. */
628 free ((char *) _nl_current_default_domain);
631 text_set_element (__libc_subfreeres, free_mem);
632 #endif