]> sourceware.org Git - glibc.git/blame - intl/dcgettext.c
Update.
[glibc.git] / intl / dcgettext.c
CommitLineData
c84142e8 1/* Implementation of the dcgettext(3) function.
4b8f94d3 2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
5f2eab42 3
c84142e8 4 This file is part of the GNU C Library. Its master source is NOT part of
40a55d20 5 the C library, however.
24906b43 6
c84142e8
UD
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.
0393dfd6 11
c84142e8
UD
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.
24906b43 16
c84142e8
UD
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. */
24906b43
RM
21
22#ifdef HAVE_CONFIG_H
23# include <config.h>
24#endif
25
26#include <sys/types.h>
27
28#ifdef __GNUC__
29# define alloca __builtin_alloca
fa00327f 30# define HAVE_ALLOCA 1
24906b43 31#else
be10a868 32# if defined HAVE_ALLOCA_H || defined _LIBC
24906b43
RM
33# include <alloca.h>
34# else
35# ifdef _AIX
36 #pragma alloca
37# else
38# ifndef alloca
39char *alloca ();
40# endif
41# endif
42# endif
43#endif
44
45#include <errno.h>
46#ifndef errno
47extern int errno;
48#endif
c4029823
UD
49#ifndef __set_errno
50# define __set_errno(val) errno = (val)
51#endif
24906b43
RM
52
53#if defined STDC_HEADERS || defined _LIBC
54# include <stdlib.h>
55#else
56char *getenv ();
57# ifdef HAVE_MALLOC_H
58# include <malloc.h>
59# else
60void free ();
61# endif
62#endif
63
64#if defined HAVE_STRING_H || defined _LIBC
842907c6
RM
65# ifndef _GNU_SOURCE
66# define _GNU_SOURCE 1
67# endif
24906b43
RM
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
77
78#if defined HAVE_UNISTD_H || defined _LIBC
79# include <unistd.h>
80#endif
81
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"
90
91/* @@ end of prolog @@ */
92
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
9a0a462c
UD
98# ifndef stpcpy
99# define stpcpy __stpcpy
100# endif
24906b43 101#else
fa0bc87c
RM
102# if !defined HAVE_GETCWD
103char *getwd ();
104# define getcwd(buf, max) getwd (buf)
105# else
24906b43 106char *getcwd ();
fa0bc87c 107# endif
92f3773b
RM
108# ifndef HAVE_STPCPY
109static char *stpcpy PARAMS ((char *dest, const char *src));
110# endif
24906b43
RM
111#endif
112
113/* Amount to increase buffer size by in each try. */
114#define PATH_INCR 32
115
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
123
124#ifndef _POSIX_PATH_MAX
125# define _POSIX_PATH_MAX 255
126#endif
127
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
131
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
136
137#if !defined(PATH_MAX) && defined(MAXPATHLEN)
138# define PATH_MAX MAXPATHLEN
139#endif
140
141#ifndef PATH_MAX
142# define PATH_MAX _POSIX_PATH_MAX
143#endif
144
145/* XPG3 defines the result of `setlocale (category, NULL)' as:
146 ``Directs `setlocale()' to query `category' and return the current
147 setting of `local'.''
148 However it does not specify the exact format. And even worse: POSIX
149 defines this not at all. So we can use this feature only on selected
150 system (e.g. those using GNU C Library). */
151#ifdef _LIBC
152# define HAVE_LOCALE_NULL
153#endif
154
155/* Name of the default domain used for gettext(3) prior any call to
156 textdomain(3). The default value for this is "messages". */
157const char _nl_default_default_domain[] = "messages";
158
159/* Value used as the default domain for gettext(3). */
160const char *_nl_current_default_domain = _nl_default_default_domain;
161
162/* Contains the default location of the message catalogs. */
163const char _nl_default_dirname[] = GNULOCALEDIR;
164
165/* List with bindings of specific domains created by bindtextdomain()
166 calls. */
167struct binding *_nl_domain_bindings;
168
169/* Prototypes for local functions. */
7a12c6bb 170static char *find_msg PARAMS ((struct loaded_l10nfile *domain_file,
dfd2257a
UD
171 const char *msgid)) internal_function;
172static const char *category_to_name PARAMS ((int category)) internal_function;
be10a868 173static const char *guess_category_value PARAMS ((int category,
dfd2257a
UD
174 const char *categoryname))
175 internal_function;
24906b43
RM
176
177
5f2eab42
RM
178/* For those loosing systems which don't have `alloca' we have to add
179 some additional code emulating it. */
180#ifdef HAVE_ALLOCA
181/* Nothing has to be done. */
182# define ADD_BLOCK(list, address) /* nothing */
183# define FREE_BLOCKS(list) /* nothing */
184#else
185struct block_list
186{
187 void *address;
188 struct block_list *next;
189};
190# define ADD_BLOCK(list, addr) \
191 do { \
192 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
193 /* If we cannot get a free block we cannot add the new element to \
194 the list. */ \
195 if (newp != NULL) { \
196 newp->address = (addr); \
197 newp->next = (list); \
198 (list) = newp; \
199 } \
200 } while (0)
201# define FREE_BLOCKS(list) \
202 do { \
203 while (list != NULL) { \
204 struct block_list *old = list; \
205 list = list->next; \
206 free (old); \
207 } \
208 } while (0)
209# undef alloca
210# define alloca(size) (malloc (size))
211#endif /* have alloca */
212
213
24906b43
RM
214/* Names for the libintl functions are a problem. They must not clash
215 with existing names and they should follow ANSI C. But this source
216 code is also used in GNU C Library where the names have a __
217 prefix. So we have to make a difference here. */
218#ifdef _LIBC
219# define DCGETTEXT __dcgettext
220#else
221# define DCGETTEXT dcgettext__
222#endif
223
3081378b
UD
224/* Checking whether the binaries runs SUID must be done and glibc provides
225 easier methods therefore we make a difference here. */
226#ifdef _LIBC
227# define ENABLE_SECURE __libc_enable_secure
228# define DETERMINE_SECURE
229#else
230static int enable_secure;
231# define ENABLE_SECURE (enable_secure == 1)
232# define DETERMINE_SECURE \
233 if (enable_secure == 0) \
234 { \
235 if (getuid () != geteuid () || getgid () != getegid ()) \
236 enable_secure = 1; \
237 else \
238 enable_secure = -1; \
239 }
240#endif
241
24906b43
RM
242/* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
243 locale. */
244char *
245DCGETTEXT (domainname, msgid, category)
246 const char *domainname;
247 const char *msgid;
248 int category;
249{
5f2eab42 250#ifndef HAVE_ALLOCA
842907c6 251 struct block_list *block_list = NULL;
5f2eab42 252#endif
7a12c6bb 253 struct loaded_l10nfile *domain;
24906b43
RM
254 struct binding *binding;
255 const char *categoryname;
256 const char *categoryvalue;
257 char *dirname, *xdomainname;
258 char *single_locale;
259 char *retval;
be10a868 260 int saved_errno = errno;
24906b43
RM
261
262 /* If no real MSGID is given return NULL. */
263 if (msgid == NULL)
264 return NULL;
265
3081378b
UD
266 /* See whether this is a SUID binary or not. */
267 DETERMINE_SECURE;
268
24906b43
RM
269 /* If DOMAINNAME is NULL, we are interested in the default domain. If
270 CATEGORY is not LC_MESSAGES this might not make much sense but the
3081378b 271 definition left this undefined. */
24906b43
RM
272 if (domainname == NULL)
273 domainname = _nl_current_default_domain;
274
275 /* First find matching binding. */
276 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
277 {
278 int compare = strcmp (domainname, binding->domainname);
279 if (compare == 0)
280 /* We found it! */
281 break;
282 if (compare < 0)
283 {
284 /* It is not in the list. */
285 binding = NULL;
286 break;
287 }
288 }
289
290 if (binding == NULL)
291 dirname = (char *) _nl_default_dirname;
292 else if (binding->dirname[0] == '/')
293 dirname = binding->dirname;
294 else
295 {
296 /* We have a relative path. Make it absolute now. */
297 size_t dirname_len = strlen (binding->dirname) + 1;
298 size_t path_max;
299 char *ret;
300
301 path_max = (unsigned) PATH_MAX;
302 path_max += 2; /* The getcwd docs say to do this. */
303
304 dirname = (char *) alloca (path_max + dirname_len);
5f2eab42 305 ADD_BLOCK (block_list, dirname);
24906b43 306
c4029823 307 __set_errno (0);
24906b43
RM
308 while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
309 {
310 path_max += PATH_INCR;
311 dirname = (char *) alloca (path_max + dirname_len);
5f2eab42 312 ADD_BLOCK (block_list, dirname);
c4029823 313 __set_errno (0);
24906b43
RM
314 }
315
316 if (ret == NULL)
be10a868
RM
317 {
318 /* We cannot get the current working directory. Don't signal an
319 error but simply return the default string. */
5f2eab42 320 FREE_BLOCKS (block_list);
c4029823 321 __set_errno (saved_errno);
be10a868
RM
322 return (char *) msgid;
323 }
24906b43 324
24906b43 325 stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
24906b43
RM
326 }
327
328 /* Now determine the symbolic name of CATEGORY and its value. */
329 categoryname = category_to_name (category);
330 categoryvalue = guess_category_value (category, categoryname);
331
332 xdomainname = (char *) alloca (strlen (categoryname)
333 + strlen (domainname) + 5);
5f2eab42 334 ADD_BLOCK (block_list, xdomainname);
40a55d20 335
24906b43
RM
336 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
337 domainname),
338 ".mo");
24906b43
RM
339
340 /* Creating working area. */
341 single_locale = (char *) alloca (strlen (categoryvalue) + 1);
5f2eab42 342 ADD_BLOCK (block_list, single_locale);
24906b43
RM
343
344
345 /* Search for the given string. This is a loop because we perhaps
3081378b 346 got an ordered list of languages to consider for the translation. */
24906b43
RM
347 while (1)
348 {
349 /* Make CATEGORYVALUE point to the next element of the list. */
350 while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
351 ++categoryvalue;
352 if (categoryvalue[0] == '\0')
353 {
354 /* The whole contents of CATEGORYVALUE has been searched but
355 no valid entry has been found. We solve this situation
6d52618b 356 by implicitly appending a "C" entry, i.e. no translation
24906b43
RM
357 will take place. */
358 single_locale[0] = 'C';
359 single_locale[1] = '\0';
360 }
361 else
362 {
363 char *cp = single_locale;
364 while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
365 *cp++ = *categoryvalue++;
366 *cp = '\0';
3081378b
UD
367
368 /* When this is a SUID binary we must not allow accessing files
369 outside the dedicated directories. */
370 if (ENABLE_SECURE
371 && (memchr (single_locale, '/',
372 _nl_find_language (single_locale) - single_locale)
373 != NULL))
374 /* Ingore this entry. */
375 continue;
24906b43
RM
376 }
377
378 /* If the current locale value is C (or POSIX) we don't load a
379 domain. Return the MSGID. */
380 if (strcmp (single_locale, "C") == 0
381 || strcmp (single_locale, "POSIX") == 0)
be10a868 382 {
5f2eab42 383 FREE_BLOCKS (block_list);
c4029823 384 __set_errno (saved_errno);
be10a868
RM
385 return (char *) msgid;
386 }
24906b43
RM
387
388
389 /* Find structure describing the message catalog matching the
390 DOMAINNAME and CATEGORY. */
391 domain = _nl_find_domain (dirname, single_locale, xdomainname);
392
393 if (domain != NULL)
394 {
395 retval = find_msg (domain, msgid);
396
397 if (retval == NULL)
398 {
399 int cnt;
400
be10a868 401 for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
75914335
RM
402 {
403 retval = find_msg (domain->successor[cnt], msgid);
404
405 if (retval != NULL)
406 break;
407 }
24906b43
RM
408 }
409
410 if (retval != NULL)
be10a868 411 {
5f2eab42 412 FREE_BLOCKS (block_list);
c4029823 413 __set_errno (saved_errno);
be10a868
RM
414 return retval;
415 }
24906b43
RM
416 }
417 }
418 /* NOTREACHED */
419}
420
421#ifdef _LIBC
422/* Alias for function name in GNU C Library. */
423weak_alias (__dcgettext, dcgettext);
424#endif
425
426
427static char *
dfd2257a 428internal_function
7a12c6bb
RM
429find_msg (domain_file, msgid)
430 struct loaded_l10nfile *domain_file;
24906b43
RM
431 const char *msgid;
432{
433 size_t top, act, bottom;
7a12c6bb 434 struct loaded_domain *domain;
24906b43 435
7a12c6bb
RM
436 if (domain_file->decided == 0)
437 _nl_load_domain (domain_file);
24906b43 438
7a12c6bb 439 if (domain_file->data == NULL)
24906b43
RM
440 return NULL;
441
7a12c6bb
RM
442 domain = (struct loaded_domain *) domain_file->data;
443
24906b43
RM
444 /* Locate the MSGID and its translation. */
445 if (domain->hash_size > 2 && domain->hash_tab != NULL)
446 {
447 /* Use the hashing table. */
448 nls_uint32 len = strlen (msgid);
449 nls_uint32 hash_val = hash_string (msgid);
450 nls_uint32 idx = hash_val % domain->hash_size;
451 nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
452 nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
453
454 if (nstr == 0)
455 /* Hash table entry is empty. */
456 return NULL;
457
458 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
459 && strcmp (msgid,
460 domain->data + W (domain->must_swap,
461 domain->orig_tab[nstr - 1].offset)) == 0)
462 return (char *) domain->data + W (domain->must_swap,
463 domain->trans_tab[nstr - 1].offset);
464
465 while (1)
466 {
be10a868
RM
467 if (idx >= domain->hash_size - incr)
468 idx -= domain->hash_size - incr;
24906b43
RM
469 else
470 idx += incr;
471
472 nstr = W (domain->must_swap, domain->hash_tab[idx]);
473 if (nstr == 0)
474 /* Hash table entry is empty. */
475 return NULL;
476
477 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
478 && strcmp (msgid,
479 domain->data + W (domain->must_swap,
480 domain->orig_tab[nstr - 1].offset))
481 == 0)
482 return (char *) domain->data
483 + W (domain->must_swap, domain->trans_tab[nstr - 1].offset);
484 }
485 /* NOTREACHED */
486 }
487
488 /* Now we try the default method: binary search in the sorted
489 array of messages. */
490 bottom = 0;
491 top = domain->nstrings;
492 while (bottom < top)
493 {
494 int cmp_val;
495
496 act = (bottom + top) / 2;
497 cmp_val = strcmp (msgid, domain->data
498 + W (domain->must_swap,
499 domain->orig_tab[act].offset));
500 if (cmp_val < 0)
501 top = act;
502 else if (cmp_val > 0)
503 bottom = act + 1;
504 else
505 break;
506 }
507
508 /* If an translation is found return this. */
509 return bottom >= top ? NULL : (char *) domain->data
510 + W (domain->must_swap,
511 domain->trans_tab[act].offset);
512}
513
514
515/* Return string representation of locale CATEGORY. */
7a12c6bb 516static const char *
dfd2257a 517internal_function
7a12c6bb 518category_to_name (category)
24906b43
RM
519 int category;
520{
521 const char *retval;
522
523 switch (category)
524 {
525#ifdef LC_COLLATE
526 case LC_COLLATE:
527 retval = "LC_COLLATE";
528 break;
529#endif
530#ifdef LC_CTYPE
531 case LC_CTYPE:
532 retval = "LC_CTYPE";
533 break;
534#endif
535#ifdef LC_MONETARY
536 case LC_MONETARY:
537 retval = "LC_MONETARY";
538 break;
539#endif
540#ifdef LC_NUMERIC
541 case LC_NUMERIC:
542 retval = "LC_NUMERIC";
543 break;
544#endif
545#ifdef LC_TIME
546 case LC_TIME:
547 retval = "LC_TIME";
548 break;
549#endif
550#ifdef LC_MESSAGES
551 case LC_MESSAGES:
552 retval = "LC_MESSAGES";
553 break;
554#endif
555#ifdef LC_RESPONSE
556 case LC_RESPONSE:
557 retval = "LC_RESPONSE";
558 break;
559#endif
560#ifdef LC_ALL
561 case LC_ALL:
562 /* This might not make sense but is perhaps better than any other
563 value. */
564 retval = "LC_ALL";
565 break;
566#endif
567 default:
568 /* If you have a better idea for a default value let me know. */
569 retval = "LC_XXX";
570 }
571
572 return retval;
573}
574
575/* Guess value of current locale from value of the environment variables. */
8f2ece69 576static const char *
dfd2257a 577internal_function
8f2ece69 578guess_category_value (category, categoryname)
24906b43
RM
579 int category;
580 const char *categoryname;
581{
582 const char *retval;
583
584 /* The highest priority value is the `LANGUAGE' environment
585 variable. This is a GNU extension. */
586 retval = getenv ("LANGUAGE");
587 if (retval != NULL && retval[0] != '\0')
588 return retval;
589
590 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
591 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
592 systems this can be done by the `setlocale' function itself. */
593#if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
594 return setlocale (category, NULL);
595#else
596 /* Setting of LC_ALL overwrites all other. */
597 retval = getenv ("LC_ALL");
598 if (retval != NULL && retval[0] != '\0')
599 return retval;
600
601 /* Next comes the name of the desired category. */
602 retval = getenv (categoryname);
603 if (retval != NULL && retval[0] != '\0')
604 return retval;
605
606 /* Last possibility is the LANG environment variable. */
607 retval = getenv ("LANG");
608 if (retval != NULL && retval[0] != '\0')
609 return retval;
610
611 /* We use C as the default domain. POSIX says this is implementation
612 defined. */
613 return "C";
614#endif
615}
92f3773b
RM
616
617/* @@ begin of epilog @@ */
618
619/* We don't want libintl.a to depend on any other library. So we
620 avoid the non-standard function stpcpy. In GNU C Library this
621 function is available, though. Also allow the symbol HAVE_STPCPY
622 to be defined. */
623#if !_LIBC && !HAVE_STPCPY
624static char *
625stpcpy (dest, src)
626 char *dest;
627 const char *src;
628{
629 while ((*dest++ = *src++) != '\0')
630 /* Do nothing. */ ;
631 return dest - 1;
632}
633#endif
a5a0310d
UD
634
635
636#ifdef _LIBC
637/* If we want to free all resources we have to do some work at
638 program's end. */
639static void __attribute__ ((unused))
640free_mem (void)
641{
642 struct binding *runp;
643
644 for (runp = _nl_domain_bindings; runp != NULL; runp = runp->next)
645 {
646 free (runp->domainname);
647 if (runp->dirname != _nl_default_dirname)
648 /* Yes, this is a pointer comparison. */
649 free (runp->dirname);
650 }
651
652 if (_nl_current_default_domain != _nl_default_default_domain)
653 /* Yes, again a pointer comparison. */
654 free ((char *) _nl_current_default_domain);
655}
656
657text_set_element (__libc_subfreeres, free_mem);
658#endif
This page took 0.116937 seconds and 5 git commands to generate.