]> sourceware.org Git - glibc.git/blame - intl/dcgettext.c
Update.
[glibc.git] / intl / dcgettext.c
CommitLineData
c84142e8
UD
1/* Implementation of the dcgettext(3) function.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
5f2eab42 3
c84142e8
UD
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.
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
98# define stpcpy __stpcpy
24906b43 99#else
fa0bc87c
RM
100# if !defined HAVE_GETCWD
101char *getwd ();
102# define getcwd(buf, max) getwd (buf)
103# else
24906b43 104char *getcwd ();
fa0bc87c 105# endif
92f3773b
RM
106# ifndef HAVE_STPCPY
107static char *stpcpy PARAMS ((char *dest, const char *src));
108# endif
24906b43
RM
109#endif
110
111/* Amount to increase buffer size by in each try. */
112#define PATH_INCR 32
113
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
121
122#ifndef _POSIX_PATH_MAX
123# define _POSIX_PATH_MAX 255
124#endif
125
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
129
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
134
135#if !defined(PATH_MAX) && defined(MAXPATHLEN)
136# define PATH_MAX MAXPATHLEN
137#endif
138
139#ifndef PATH_MAX
140# define PATH_MAX _POSIX_PATH_MAX
141#endif
142
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
152
153/* Name of the default domain used for gettext(3) prior any call to
154 textdomain(3). The default value for this is "messages". */
155const char _nl_default_default_domain[] = "messages";
156
157/* Value used as the default domain for gettext(3). */
158const char *_nl_current_default_domain = _nl_default_default_domain;
159
160/* Contains the default location of the message catalogs. */
161const char _nl_default_dirname[] = GNULOCALEDIR;
162
163/* List with bindings of specific domains created by bindtextdomain()
164 calls. */
165struct binding *_nl_domain_bindings;
166
167/* Prototypes for local functions. */
7a12c6bb 168static char *find_msg PARAMS ((struct loaded_l10nfile *domain_file,
be10a868
RM
169 const char *msgid));
170static const char *category_to_name PARAMS ((int category));
171static const char *guess_category_value PARAMS ((int category,
172 const char *categoryname));
24906b43
RM
173
174
5f2eab42
RM
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
182struct block_list
183{
184 void *address;
185 struct block_list *next;
186};
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; \
196 } \
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); \
204 } \
205 } while (0)
206# undef alloca
207# define alloca(size) (malloc (size))
208#endif /* have alloca */
209
210
24906b43
RM
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
220
221/* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
222 locale. */
223char *
224DCGETTEXT (domainname, msgid, category)
225 const char *domainname;
226 const char *msgid;
227 int category;
228{
5f2eab42 229#ifndef HAVE_ALLOCA
842907c6 230 struct block_list *block_list = NULL;
5f2eab42 231#endif
7a12c6bb 232 struct loaded_l10nfile *domain;
24906b43
RM
233 struct binding *binding;
234 const char *categoryname;
235 const char *categoryvalue;
236 char *dirname, *xdomainname;
237 char *single_locale;
238 char *retval;
be10a868 239 int saved_errno = errno;
24906b43
RM
240
241 /* If no real MSGID is given return NULL. */
242 if (msgid == NULL)
243 return NULL;
244
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;
250
251 /* First find matching binding. */
252 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
253 {
254 int compare = strcmp (domainname, binding->domainname);
255 if (compare == 0)
256 /* We found it! */
257 break;
258 if (compare < 0)
259 {
260 /* It is not in the list. */
261 binding = NULL;
262 break;
263 }
264 }
265
266 if (binding == NULL)
267 dirname = (char *) _nl_default_dirname;
268 else if (binding->dirname[0] == '/')
269 dirname = binding->dirname;
270 else
271 {
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;
276
277 path_max = (unsigned) PATH_MAX;
278 path_max += 2; /* The getcwd docs say to do this. */
279
280 dirname = (char *) alloca (path_max + dirname_len);
5f2eab42 281 ADD_BLOCK (block_list, dirname);
24906b43 282
c4029823 283 __set_errno (0);
24906b43
RM
284 while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
285 {
286 path_max += PATH_INCR;
287 dirname = (char *) alloca (path_max + dirname_len);
5f2eab42 288 ADD_BLOCK (block_list, dirname);
c4029823 289 __set_errno (0);
24906b43
RM
290 }
291
292 if (ret == NULL)
be10a868
RM
293 {
294 /* We cannot get the current working directory. Don't signal an
295 error but simply return the default string. */
5f2eab42 296 FREE_BLOCKS (block_list);
c4029823 297 __set_errno (saved_errno);
be10a868
RM
298 return (char *) msgid;
299 }
24906b43
RM
300
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. */
24906b43 305 stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
24906b43
RM
306 }
307
308 /* Now determine the symbolic name of CATEGORY and its value. */
309 categoryname = category_to_name (category);
310 categoryvalue = guess_category_value (category, categoryname);
311
312 xdomainname = (char *) alloca (strlen (categoryname)
313 + strlen (domainname) + 5);
5f2eab42 314 ADD_BLOCK (block_list, xdomainname);
24906b43
RM
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. */
24906b43
RM
319 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
320 domainname),
321 ".mo");
24906b43
RM
322
323 /* Creating working area. */
324 single_locale = (char *) alloca (strlen (categoryvalue) + 1);
5f2eab42 325 ADD_BLOCK (block_list, single_locale);
24906b43
RM
326
327
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)
331 {
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')
336 {
337 /* The whole contents of CATEGORYVALUE has been searched but
338 no valid entry has been found. We solve this situation
6d52618b 339 by implicitly appending a "C" entry, i.e. no translation
24906b43
RM
340 will take place. */
341 single_locale[0] = 'C';
342 single_locale[1] = '\0';
343 }
344 else
345 {
346 char *cp = single_locale;
347 while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
348 *cp++ = *categoryvalue++;
349 *cp = '\0';
350 }
351
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)
be10a868 356 {
5f2eab42 357 FREE_BLOCKS (block_list);
c4029823 358 __set_errno (saved_errno);
be10a868
RM
359 return (char *) msgid;
360 }
24906b43
RM
361
362
363 /* Find structure describing the message catalog matching the
364 DOMAINNAME and CATEGORY. */
365 domain = _nl_find_domain (dirname, single_locale, xdomainname);
366
367 if (domain != NULL)
368 {
369 retval = find_msg (domain, msgid);
370
371 if (retval == NULL)
372 {
373 int cnt;
374
be10a868 375 for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
75914335
RM
376 {
377 retval = find_msg (domain->successor[cnt], msgid);
378
379 if (retval != NULL)
380 break;
381 }
24906b43
RM
382 }
383
384 if (retval != NULL)
be10a868 385 {
5f2eab42 386 FREE_BLOCKS (block_list);
c4029823 387 __set_errno (saved_errno);
be10a868
RM
388 return retval;
389 }
24906b43
RM
390 }
391 }
392 /* NOTREACHED */
393}
394
395#ifdef _LIBC
396/* Alias for function name in GNU C Library. */
397weak_alias (__dcgettext, dcgettext);
398#endif
399
400
401static char *
7a12c6bb
RM
402find_msg (domain_file, msgid)
403 struct loaded_l10nfile *domain_file;
24906b43
RM
404 const char *msgid;
405{
406 size_t top, act, bottom;
7a12c6bb 407 struct loaded_domain *domain;
24906b43 408
7a12c6bb
RM
409 if (domain_file->decided == 0)
410 _nl_load_domain (domain_file);
24906b43 411
7a12c6bb 412 if (domain_file->data == NULL)
24906b43
RM
413 return NULL;
414
7a12c6bb
RM
415 domain = (struct loaded_domain *) domain_file->data;
416
24906b43
RM
417 /* Locate the MSGID and its translation. */
418 if (domain->hash_size > 2 && domain->hash_tab != NULL)
419 {
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]);
426
427 if (nstr == 0)
428 /* Hash table entry is empty. */
429 return NULL;
430
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);
437
438 while (1)
439 {
be10a868
RM
440 if (idx >= domain->hash_size - incr)
441 idx -= domain->hash_size - incr;
24906b43
RM
442 else
443 idx += incr;
444
445 nstr = W (domain->must_swap, domain->hash_tab[idx]);
446 if (nstr == 0)
447 /* Hash table entry is empty. */
448 return NULL;
449
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);
457 }
458 /* NOTREACHED */
459 }
460
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)
466 {
467 int cmp_val;
468
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;
479 }
480
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);
485}
486
487
488/* Return string representation of locale CATEGORY. */
7a12c6bb
RM
489static const char *
490category_to_name (category)
24906b43
RM
491 int category;
492{
493 const char *retval;
494
495 switch (category)
496 {
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";
542 }
543
544 return retval;
545}
546
547/* Guess value of current locale from value of the environment variables. */
8f2ece69
UD
548static const char *
549guess_category_value (category, categoryname)
24906b43
RM
550 int category;
551 const char *categoryname;
552{
553 const char *retval;
554
555 /* The highest priority value is the `LANGUAGE' environment
556 variable. This is a GNU extension. */
557 retval = getenv ("LANGUAGE");
558 if (retval != NULL && retval[0] != '\0')
559 return retval;
560
561 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
562 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
563 systems this can be done by the `setlocale' function itself. */
564#if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
565 return setlocale (category, NULL);
566#else
567 /* Setting of LC_ALL overwrites all other. */
568 retval = getenv ("LC_ALL");
569 if (retval != NULL && retval[0] != '\0')
570 return retval;
571
572 /* Next comes the name of the desired category. */
573 retval = getenv (categoryname);
574 if (retval != NULL && retval[0] != '\0')
575 return retval;
576
577 /* Last possibility is the LANG environment variable. */
578 retval = getenv ("LANG");
579 if (retval != NULL && retval[0] != '\0')
580 return retval;
581
582 /* We use C as the default domain. POSIX says this is implementation
583 defined. */
584 return "C";
585#endif
586}
92f3773b
RM
587
588/* @@ begin of epilog @@ */
589
590/* We don't want libintl.a to depend on any other library. So we
591 avoid the non-standard function stpcpy. In GNU C Library this
592 function is available, though. Also allow the symbol HAVE_STPCPY
593 to be defined. */
594#if !_LIBC && !HAVE_STPCPY
595static char *
596stpcpy (dest, src)
597 char *dest;
598 const char *src;
599{
600 while ((*dest++ = *src++) != '\0')
601 /* Do nothing. */ ;
602 return dest - 1;
603}
604#endif
This page took 0.113264 seconds and 5 git commands to generate.