]> sourceware.org Git - glibc.git/blame - intl/dcgettext.c
Tue Jun 4 22:03:02 1996 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
[glibc.git] / intl / dcgettext.c
CommitLineData
be10a868 1/* dcgettext.c -- implementation of the dcgettext(3) function
5f2eab42
RM
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3
4This file is part of the GNU C Library. Its master source is NOT part of
5the C library, however. The master source lives in /gd/gnu/lib.
24906b43 6
0393dfd6
RM
7The GNU C Library is free software; you can redistribute it and/or
8modify it under the terms of the GNU Library General Public License as
9published by the Free Software Foundation; either version 2 of the
10License, or (at your option) any later version.
11
12The GNU C Library is distributed in the hope that it will be useful,
24906b43 13but WITHOUT ANY WARRANTY; without even the implied warranty of
0393dfd6
RM
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15Library General Public License for more details.
24906b43 16
0393dfd6
RM
17You should have received a copy of the GNU Library General Public
18License along with the GNU C Library; see the file COPYING.LIB. If
5f2eab42
RM
19not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20Cambridge, MA 02139, 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
49
50#if defined STDC_HEADERS || defined _LIBC
51# include <stdlib.h>
52#else
53char *getenv ();
54# ifdef HAVE_MALLOC_H
55# include <malloc.h>
56# else
57void free ();
58# endif
59#endif
60
61#if defined HAVE_STRING_H || defined _LIBC
62# include <string.h>
63#else
64# include <strings.h>
65#endif
66#if !HAVE_STRCHR && !defined _LIBC
67# ifndef strchr
68# define strchr index
69# endif
70#endif
71
72#if defined HAVE_UNISTD_H || defined _LIBC
73# include <unistd.h>
74#endif
75
76#include "gettext.h"
77#include "gettextP.h"
78#ifdef _LIBC
79# include <libintl.h>
80#else
81# include "libgettext.h"
82#endif
83#include "hash-string.h"
84
85/* @@ end of prolog @@ */
86
87#ifdef _LIBC
88/* Rename the non ANSI C functions. This is required by the standard
89 because some ANSI C functions will require linking with this object
90 file and the name space must not be polluted. */
91# define getcwd __getcwd
92# define stpcpy __stpcpy
24906b43 93#else
fa0bc87c
RM
94# if !defined HAVE_GETCWD
95char *getwd ();
96# define getcwd(buf, max) getwd (buf)
97# else
24906b43 98char *getcwd ();
fa0bc87c 99# endif
24906b43
RM
100#endif
101
102/* Amount to increase buffer size by in each try. */
103#define PATH_INCR 32
104
105/* The following is from pathmax.h. */
106/* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
107 PATH_MAX but might cause redefinition warnings when sys/param.h is
108 later included (as on MORE/BSD 4.3). */
109#if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
110# include <limits.h>
111#endif
112
113#ifndef _POSIX_PATH_MAX
114# define _POSIX_PATH_MAX 255
115#endif
116
117#if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
118# define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
119#endif
120
121/* Don't include sys/param.h if it already has been. */
122#if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
123# include <sys/param.h>
124#endif
125
126#if !defined(PATH_MAX) && defined(MAXPATHLEN)
127# define PATH_MAX MAXPATHLEN
128#endif
129
130#ifndef PATH_MAX
131# define PATH_MAX _POSIX_PATH_MAX
132#endif
133
134/* XPG3 defines the result of `setlocale (category, NULL)' as:
135 ``Directs `setlocale()' to query `category' and return the current
136 setting of `local'.''
137 However it does not specify the exact format. And even worse: POSIX
138 defines this not at all. So we can use this feature only on selected
139 system (e.g. those using GNU C Library). */
140#ifdef _LIBC
141# define HAVE_LOCALE_NULL
142#endif
143
144/* Name of the default domain used for gettext(3) prior any call to
145 textdomain(3). The default value for this is "messages". */
146const char _nl_default_default_domain[] = "messages";
147
148/* Value used as the default domain for gettext(3). */
149const char *_nl_current_default_domain = _nl_default_default_domain;
150
151/* Contains the default location of the message catalogs. */
152const char _nl_default_dirname[] = GNULOCALEDIR;
153
154/* List with bindings of specific domains created by bindtextdomain()
155 calls. */
156struct binding *_nl_domain_bindings;
157
158/* Prototypes for local functions. */
7a12c6bb 159static char *find_msg PARAMS ((struct loaded_l10nfile *domain_file,
be10a868
RM
160 const char *msgid));
161static const char *category_to_name PARAMS ((int category));
162static const char *guess_category_value PARAMS ((int category,
163 const char *categoryname));
24906b43
RM
164
165
5f2eab42
RM
166/* For those loosing systems which don't have `alloca' we have to add
167 some additional code emulating it. */
168#ifdef HAVE_ALLOCA
169/* Nothing has to be done. */
170# define ADD_BLOCK(list, address) /* nothing */
171# define FREE_BLOCKS(list) /* nothing */
172#else
173struct block_list
174{
175 void *address;
176 struct block_list *next;
177};
178# define ADD_BLOCK(list, addr) \
179 do { \
180 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
181 /* If we cannot get a free block we cannot add the new element to \
182 the list. */ \
183 if (newp != NULL) { \
184 newp->address = (addr); \
185 newp->next = (list); \
186 (list) = newp; \
187 } \
188 } while (0)
189# define FREE_BLOCKS(list) \
190 do { \
191 while (list != NULL) { \
192 struct block_list *old = list; \
193 list = list->next; \
194 free (old); \
195 } \
196 } while (0)
197# undef alloca
198# define alloca(size) (malloc (size))
199#endif /* have alloca */
200
201
24906b43
RM
202/* Names for the libintl functions are a problem. They must not clash
203 with existing names and they should follow ANSI C. But this source
204 code is also used in GNU C Library where the names have a __
205 prefix. So we have to make a difference here. */
206#ifdef _LIBC
207# define DCGETTEXT __dcgettext
208#else
209# define DCGETTEXT dcgettext__
210#endif
211
212/* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
213 locale. */
214char *
215DCGETTEXT (domainname, msgid, category)
216 const char *domainname;
217 const char *msgid;
218 int category;
219{
5f2eab42
RM
220#ifndef HAVE_ALLOCA
221 struct block_list *alloca_list = NULL;
222#endif
7a12c6bb 223 struct loaded_l10nfile *domain;
24906b43
RM
224 struct binding *binding;
225 const char *categoryname;
226 const char *categoryvalue;
227 char *dirname, *xdomainname;
228 char *single_locale;
229 char *retval;
be10a868 230 int saved_errno = errno;
24906b43
RM
231
232 /* If no real MSGID is given return NULL. */
233 if (msgid == NULL)
234 return NULL;
235
236 /* If DOMAINNAME is NULL, we are interested in the default domain. If
237 CATEGORY is not LC_MESSAGES this might not make much sense but the
238 defintion left this undefined. */
239 if (domainname == NULL)
240 domainname = _nl_current_default_domain;
241
242 /* First find matching binding. */
243 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
244 {
245 int compare = strcmp (domainname, binding->domainname);
246 if (compare == 0)
247 /* We found it! */
248 break;
249 if (compare < 0)
250 {
251 /* It is not in the list. */
252 binding = NULL;
253 break;
254 }
255 }
256
257 if (binding == NULL)
258 dirname = (char *) _nl_default_dirname;
259 else if (binding->dirname[0] == '/')
260 dirname = binding->dirname;
261 else
262 {
263 /* We have a relative path. Make it absolute now. */
264 size_t dirname_len = strlen (binding->dirname) + 1;
265 size_t path_max;
266 char *ret;
267
268 path_max = (unsigned) PATH_MAX;
269 path_max += 2; /* The getcwd docs say to do this. */
270
271 dirname = (char *) alloca (path_max + dirname_len);
5f2eab42 272 ADD_BLOCK (block_list, dirname);
24906b43
RM
273
274 errno = 0;
275 while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
276 {
277 path_max += PATH_INCR;
278 dirname = (char *) alloca (path_max + dirname_len);
5f2eab42 279 ADD_BLOCK (block_list, dirname);
24906b43
RM
280 errno = 0;
281 }
282
283 if (ret == NULL)
be10a868
RM
284 {
285 /* We cannot get the current working directory. Don't signal an
286 error but simply return the default string. */
5f2eab42 287 FREE_BLOCKS (block_list);
be10a868
RM
288 errno = saved_errno;
289 return (char *) msgid;
290 }
24906b43
RM
291
292 /* We don't want libintl.a to depend on any other library. So
293 we avoid the non-standard function stpcpy. In GNU C Library
294 this function is available, though. Also allow the symbol
295 HAVE_STPCPY to be defined. */
296#if defined _LIBC || defined HAVE_STPCPY
297 stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
298#else
299 strcat (dirname, "/");
300 strcat (dirname, binding->dirname);
301#endif
302 }
303
304 /* Now determine the symbolic name of CATEGORY and its value. */
305 categoryname = category_to_name (category);
306 categoryvalue = guess_category_value (category, categoryname);
307
308 xdomainname = (char *) alloca (strlen (categoryname)
309 + strlen (domainname) + 5);
5f2eab42 310 ADD_BLOCK (block_list, xdomainname);
24906b43
RM
311 /* We don't want libintl.a to depend on any other library. So we
312 avoid the non-standard function stpcpy. In GNU C Library this
313 function is available, though. Also allow the symbol HAVE_STPCPY
314 to be defined. */
315#if defined _LIBC || defined HAVE_STPCPY
316 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
317 domainname),
318 ".mo");
319#else
320 strcpy (xdomainname, categoryname);
321 strcat (xdomainname, "/");
322 strcat (xdomainname, domainname);
323 strcat (xdomainname, ".mo");
324#endif
325
326 /* Creating working area. */
327 single_locale = (char *) alloca (strlen (categoryvalue) + 1);
5f2eab42 328 ADD_BLOCK (block_list, single_locale);
24906b43
RM
329
330
331 /* Search for the given string. This is a loop because we perhaps
332 got an ordered list of languages to consider for th translation. */
333 while (1)
334 {
335 /* Make CATEGORYVALUE point to the next element of the list. */
336 while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
337 ++categoryvalue;
338 if (categoryvalue[0] == '\0')
339 {
340 /* The whole contents of CATEGORYVALUE has been searched but
341 no valid entry has been found. We solve this situation
342 by implicitely appending a "C" entry, i.e. no translation
343 will take place. */
344 single_locale[0] = 'C';
345 single_locale[1] = '\0';
346 }
347 else
348 {
349 char *cp = single_locale;
350 while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
351 *cp++ = *categoryvalue++;
352 *cp = '\0';
353 }
354
355 /* If the current locale value is C (or POSIX) we don't load a
356 domain. Return the MSGID. */
357 if (strcmp (single_locale, "C") == 0
358 || strcmp (single_locale, "POSIX") == 0)
be10a868 359 {
5f2eab42 360 FREE_BLOCKS (block_list);
be10a868
RM
361 errno = saved_errno;
362 return (char *) msgid;
363 }
24906b43
RM
364
365
366 /* Find structure describing the message catalog matching the
367 DOMAINNAME and CATEGORY. */
368 domain = _nl_find_domain (dirname, single_locale, xdomainname);
369
370 if (domain != NULL)
371 {
372 retval = find_msg (domain, msgid);
373
374 if (retval == NULL)
375 {
376 int cnt;
377
be10a868 378 for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
75914335
RM
379 {
380 retval = find_msg (domain->successor[cnt], msgid);
381
382 if (retval != NULL)
383 break;
384 }
24906b43
RM
385 }
386
387 if (retval != NULL)
be10a868 388 {
5f2eab42 389 FREE_BLOCKS (block_list);
be10a868
RM
390 errno = saved_errno;
391 return retval;
392 }
24906b43
RM
393 }
394 }
395 /* NOTREACHED */
396}
397
398#ifdef _LIBC
399/* Alias for function name in GNU C Library. */
400weak_alias (__dcgettext, dcgettext);
401#endif
402
403
404static char *
7a12c6bb
RM
405find_msg (domain_file, msgid)
406 struct loaded_l10nfile *domain_file;
24906b43
RM
407 const char *msgid;
408{
409 size_t top, act, bottom;
7a12c6bb 410 struct loaded_domain *domain;
24906b43 411
7a12c6bb
RM
412 if (domain_file->decided == 0)
413 _nl_load_domain (domain_file);
24906b43 414
7a12c6bb 415 if (domain_file->data == NULL)
24906b43
RM
416 return NULL;
417
7a12c6bb
RM
418 domain = (struct loaded_domain *) domain_file->data;
419
24906b43
RM
420 /* Locate the MSGID and its translation. */
421 if (domain->hash_size > 2 && domain->hash_tab != NULL)
422 {
423 /* Use the hashing table. */
424 nls_uint32 len = strlen (msgid);
425 nls_uint32 hash_val = hash_string (msgid);
426 nls_uint32 idx = hash_val % domain->hash_size;
427 nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
428 nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
429
430 if (nstr == 0)
431 /* Hash table entry is empty. */
432 return NULL;
433
434 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
435 && strcmp (msgid,
436 domain->data + W (domain->must_swap,
437 domain->orig_tab[nstr - 1].offset)) == 0)
438 return (char *) domain->data + W (domain->must_swap,
439 domain->trans_tab[nstr - 1].offset);
440
441 while (1)
442 {
be10a868
RM
443 if (idx >= domain->hash_size - incr)
444 idx -= domain->hash_size - incr;
24906b43
RM
445 else
446 idx += incr;
447
448 nstr = W (domain->must_swap, domain->hash_tab[idx]);
449 if (nstr == 0)
450 /* Hash table entry is empty. */
451 return NULL;
452
453 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
454 && strcmp (msgid,
455 domain->data + W (domain->must_swap,
456 domain->orig_tab[nstr - 1].offset))
457 == 0)
458 return (char *) domain->data
459 + W (domain->must_swap, domain->trans_tab[nstr - 1].offset);
460 }
461 /* NOTREACHED */
462 }
463
464 /* Now we try the default method: binary search in the sorted
465 array of messages. */
466 bottom = 0;
467 top = domain->nstrings;
468 while (bottom < top)
469 {
470 int cmp_val;
471
472 act = (bottom + top) / 2;
473 cmp_val = strcmp (msgid, domain->data
474 + W (domain->must_swap,
475 domain->orig_tab[act].offset));
476 if (cmp_val < 0)
477 top = act;
478 else if (cmp_val > 0)
479 bottom = act + 1;
480 else
481 break;
482 }
483
484 /* If an translation is found return this. */
485 return bottom >= top ? NULL : (char *) domain->data
486 + W (domain->must_swap,
487 domain->trans_tab[act].offset);
488}
489
490
491/* Return string representation of locale CATEGORY. */
7a12c6bb
RM
492static const char *
493category_to_name (category)
24906b43
RM
494 int category;
495{
496 const char *retval;
497
498 switch (category)
499 {
500#ifdef LC_COLLATE
501 case LC_COLLATE:
502 retval = "LC_COLLATE";
503 break;
504#endif
505#ifdef LC_CTYPE
506 case LC_CTYPE:
507 retval = "LC_CTYPE";
508 break;
509#endif
510#ifdef LC_MONETARY
511 case LC_MONETARY:
512 retval = "LC_MONETARY";
513 break;
514#endif
515#ifdef LC_NUMERIC
516 case LC_NUMERIC:
517 retval = "LC_NUMERIC";
518 break;
519#endif
520#ifdef LC_TIME
521 case LC_TIME:
522 retval = "LC_TIME";
523 break;
524#endif
525#ifdef LC_MESSAGES
526 case LC_MESSAGES:
527 retval = "LC_MESSAGES";
528 break;
529#endif
530#ifdef LC_RESPONSE
531 case LC_RESPONSE:
532 retval = "LC_RESPONSE";
533 break;
534#endif
535#ifdef LC_ALL
536 case LC_ALL:
537 /* This might not make sense but is perhaps better than any other
538 value. */
539 retval = "LC_ALL";
540 break;
541#endif
542 default:
543 /* If you have a better idea for a default value let me know. */
544 retval = "LC_XXX";
545 }
546
547 return retval;
548}
549
550/* Guess value of current locale from value of the environment variables. */
551static const char *guess_category_value (category, categoryname)
552 int category;
553 const char *categoryname;
554{
555 const char *retval;
556
557 /* The highest priority value is the `LANGUAGE' environment
558 variable. This is a GNU extension. */
559 retval = getenv ("LANGUAGE");
560 if (retval != NULL && retval[0] != '\0')
561 return retval;
562
563 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
564 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
565 systems this can be done by the `setlocale' function itself. */
566#if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
567 return setlocale (category, NULL);
568#else
569 /* Setting of LC_ALL overwrites all other. */
570 retval = getenv ("LC_ALL");
571 if (retval != NULL && retval[0] != '\0')
572 return retval;
573
574 /* Next comes the name of the desired category. */
575 retval = getenv (categoryname);
576 if (retval != NULL && retval[0] != '\0')
577 return retval;
578
579 /* Last possibility is the LANG environment variable. */
580 retval = getenv ("LANG");
581 if (retval != NULL && retval[0] != '\0')
582 return retval;
583
584 /* We use C as the default domain. POSIX says this is implementation
585 defined. */
586 return "C";
587#endif
588}
This page took 0.09601 seconds and 5 git commands to generate.