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