* libjava.jni/jni.exp (gcj_jni_compile_c_to_so): Fix path for
[official-gcc.git] / gcc / intl / dcgettext.c
blob65de78b9cfbbf89a7c09f4a9d1497ad2f5370687
1 /* Implementation of the dcgettext(3) function.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 # ifndef _GNU_SOURCE
23 # define _GNU_SOURCE 1
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 # include <string.h>
66 #else
67 # include <strings.h>
68 #endif
69 #if !HAVE_STRCHR && !defined _LIBC
70 # ifndef strchr
71 # define strchr index
72 # endif
73 #endif
75 #if defined HAVE_UNISTD_H || defined _LIBC
76 # include <unistd.h>
77 #endif
79 #include "gettext.h"
80 #include "gettextP.h"
81 #ifdef _LIBC
82 # include <libintl.h>
83 #else
84 # include "libgettext.h"
85 #endif
86 #include "hash-string.h"
88 /* The internal variables in the standalone libintl.a must have different
89 names than the internal variables in GNU libc, otherwise programs
90 using libintl.a cannot be linked statically. */
91 #if !defined _LIBC
92 # define _nl_default_default_domain _nl_default_default_domain__
93 # define _nl_current_default_domain _nl_current_default_domain__
94 # define _nl_default_dirname _nl_default_dirname__
95 # define _nl_domain_bindings _nl_domain_bindings__
96 #endif
98 /* @@ end of prolog @@ */
100 #ifdef _LIBC
101 /* Rename the non ANSI C functions. This is required by the standard
102 because some ANSI C functions will require linking with this object
103 file and the name space must not be polluted. */
104 # define getcwd __getcwd
105 # ifndef stpcpy
106 # define stpcpy __stpcpy
107 # endif
108 #else
109 # if !defined HAVE_GETCWD
110 char *getwd ();
111 # define getcwd(buf, max) getwd (buf)
112 # elif !defined (HAVE_DECL_GETCWD)
113 char *getcwd ();
114 # endif
115 # ifndef HAVE_STPCPY
116 static char *stpcpy PARAMS ((char *dest, const char *src));
117 # endif
118 #endif
120 /* Amount to increase buffer size by in each try. */
121 #define PATH_INCR 32
123 /* The following is from pathmax.h. */
124 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
125 PATH_MAX but might cause redefinition warnings when sys/param.h is
126 later included (as on MORE/BSD 4.3). */
127 #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
128 # include <limits.h>
129 #endif
131 #ifndef _POSIX_PATH_MAX
132 # define _POSIX_PATH_MAX 255
133 #endif
135 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
136 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
137 #endif
139 /* Don't include sys/param.h if it already has been. */
140 #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
141 # include <sys/param.h>
142 #endif
144 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
145 # define PATH_MAX MAXPATHLEN
146 #endif
148 #ifndef PATH_MAX
149 # define PATH_MAX _POSIX_PATH_MAX
150 #endif
152 /* XPG3 defines the result of `setlocale (category, NULL)' as:
153 ``Directs `setlocale()' to query `category' and return the current
154 setting of `local'.''
155 However it does not specify the exact format. And even worse: POSIX
156 defines this not at all. So we can use this feature only on selected
157 system (e.g. those using GNU C Library). */
158 #ifdef _LIBC
159 # define HAVE_LOCALE_NULL
160 #endif
162 /* Name of the default domain used for gettext(3) prior any call to
163 textdomain(3). The default value for this is "messages". */
164 const char _nl_default_default_domain[] = "messages";
166 /* Value used as the default domain for gettext(3). */
167 const char *_nl_current_default_domain = _nl_default_default_domain;
169 /* Contains the default location of the message catalogs. */
170 const char _nl_default_dirname[] = GNULOCALEDIR;
172 /* List with bindings of specific domains created by bindtextdomain()
173 calls. */
174 struct binding *_nl_domain_bindings;
176 /* Prototypes for local functions. */
177 static char *find_msg PARAMS ((struct loaded_l10nfile *domain_file,
178 const char *msgid)) internal_function;
179 static const char *category_to_name PARAMS ((int category)) internal_function;
180 static const char *guess_category_value PARAMS ((int category,
181 const char *categoryname))
182 internal_function;
185 /* For those loosing systems which don't have `alloca' we have to add
186 some additional code emulating it. */
187 #ifdef HAVE_ALLOCA
188 /* Nothing has to be done. */
189 # define ADD_BLOCK(list, address) /* nothing */
190 # define FREE_BLOCKS(list) /* nothing */
191 #else
192 struct block_list
194 void *address;
195 struct block_list *next;
197 # define ADD_BLOCK(list, addr) \
198 do { \
199 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
200 /* If we cannot get a free block we cannot add the new element to \
201 the list. */ \
202 if (newp != NULL) { \
203 newp->address = (addr); \
204 newp->next = (list); \
205 (list) = newp; \
207 } while (0)
208 # define FREE_BLOCKS(list) \
209 do { \
210 while (list != NULL) { \
211 struct block_list *old = list; \
212 list = list->next; \
213 free (old); \
215 } while (0)
216 # undef alloca
217 # define alloca(size) (malloc (size))
218 #endif /* have alloca */
221 /* Names for the libintl functions are a problem. They must not clash
222 with existing names and they should follow ANSI C. But this source
223 code is also used in GNU C Library where the names have a __
224 prefix. So we have to make a difference here. */
225 #ifdef _LIBC
226 # define DCGETTEXT __dcgettext
227 #else
228 # define DCGETTEXT dcgettext__
229 #endif
231 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
232 locale. */
233 char *
234 DCGETTEXT (domainname, msgid, category)
235 const char *domainname;
236 const char *msgid;
237 int category;
239 #ifndef HAVE_ALLOCA
240 struct block_list *block_list = NULL;
241 #endif
242 struct loaded_l10nfile *domain;
243 struct binding *binding;
244 const char *categoryname;
245 const char *categoryvalue;
246 char *dirname, *xdomainname;
247 char *single_locale;
248 char *retval;
249 int saved_errno = errno;
251 /* If no real MSGID is given return NULL. */
252 if (msgid == NULL)
253 return NULL;
255 /* If DOMAINNAME is NULL, we are interested in the default domain. If
256 CATEGORY is not LC_MESSAGES this might not make much sense but the
257 defintion left this undefined. */
258 if (domainname == NULL)
259 domainname = _nl_current_default_domain;
261 /* First find matching binding. */
262 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
264 int compare = strcmp (domainname, binding->domainname);
265 if (compare == 0)
266 /* We found it! */
267 break;
268 if (compare < 0)
270 /* It is not in the list. */
271 binding = NULL;
272 break;
276 if (binding == NULL)
277 dirname = (char *) _nl_default_dirname;
278 else if (binding->dirname[0] == '/')
279 dirname = binding->dirname;
280 else
282 /* We have a relative path. Make it absolute now. */
283 size_t dirname_len = strlen (binding->dirname) + 1;
284 size_t path_max;
285 char *ret;
287 path_max = (unsigned) PATH_MAX;
288 path_max += 2; /* The getcwd docs say to do this. */
290 dirname = (char *) alloca (path_max + dirname_len);
291 ADD_BLOCK (block_list, dirname);
293 __set_errno (0);
294 while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
296 path_max += PATH_INCR;
297 dirname = (char *) alloca (path_max + dirname_len);
298 ADD_BLOCK (block_list, dirname);
299 __set_errno (0);
302 if (ret == NULL)
304 /* We cannot get the current working directory. Don't signal an
305 error but simply return the default string. */
306 FREE_BLOCKS (block_list);
307 __set_errno (saved_errno);
308 return (char *) msgid;
311 stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
314 /* Now determine the symbolic name of CATEGORY and its value. */
315 categoryname = category_to_name (category);
316 categoryvalue = guess_category_value (category, categoryname);
318 xdomainname = (char *) alloca (strlen (categoryname)
319 + strlen (domainname) + 5);
320 ADD_BLOCK (block_list, xdomainname);
322 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
323 domainname),
324 ".mo");
326 /* Creating working area. */
327 single_locale = (char *) alloca (strlen (categoryvalue) + 1);
328 ADD_BLOCK (block_list, single_locale);
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)
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')
340 /* The whole contents of CATEGORYVALUE has been searched but
341 no valid entry has been found. We solve this situation
342 by implicitly appending a "C" entry, i.e. no translation
343 will take place. */
344 single_locale[0] = 'C';
345 single_locale[1] = '\0';
347 else
349 char *cp = single_locale;
350 while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
351 *cp++ = *categoryvalue++;
352 *cp = '\0';
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)
360 FREE_BLOCKS (block_list);
361 __set_errno (saved_errno);
362 return (char *) msgid;
366 /* Find structure describing the message catalog matching the
367 DOMAINNAME and CATEGORY. */
368 domain = _nl_find_domain (dirname, single_locale, xdomainname);
370 if (domain != NULL)
372 retval = find_msg (domain, msgid);
374 if (retval == NULL)
376 int cnt;
378 for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
380 retval = find_msg (domain->successor[cnt], msgid);
382 if (retval != NULL)
383 break;
387 if (retval != NULL)
389 FREE_BLOCKS (block_list);
390 __set_errno (saved_errno);
391 return retval;
395 /* NOTREACHED */
398 #ifdef _LIBC
399 /* Alias for function name in GNU C Library. */
400 weak_alias (__dcgettext, dcgettext);
401 #endif
404 static char *
405 internal_function
406 find_msg (domain_file, msgid)
407 struct loaded_l10nfile *domain_file;
408 const char *msgid;
410 size_t top, act, bottom;
411 struct loaded_domain *domain;
413 if (domain_file->decided == 0)
414 _nl_load_domain (domain_file);
416 if (domain_file->data == NULL)
417 return NULL;
419 domain = (struct loaded_domain *) domain_file->data;
421 /* Locate the MSGID and its translation. */
422 if (domain->hash_size > 2 && domain->hash_tab != NULL)
424 /* Use the hashing table. */
425 nls_uint32 len = strlen (msgid);
426 nls_uint32 hash_val = hash_string (msgid);
427 nls_uint32 idx = hash_val % domain->hash_size;
428 nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
429 nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
431 if (nstr == 0)
432 /* Hash table entry is empty. */
433 return NULL;
435 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
436 && strcmp (msgid,
437 domain->data + W (domain->must_swap,
438 domain->orig_tab[nstr - 1].offset)) == 0)
439 return (char *) domain->data + W (domain->must_swap,
440 domain->trans_tab[nstr - 1].offset);
442 while (1)
444 if (idx >= domain->hash_size - incr)
445 idx -= domain->hash_size - incr;
446 else
447 idx += incr;
449 nstr = W (domain->must_swap, domain->hash_tab[idx]);
450 if (nstr == 0)
451 /* Hash table entry is empty. */
452 return NULL;
454 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
455 && strcmp (msgid,
456 domain->data + W (domain->must_swap,
457 domain->orig_tab[nstr - 1].offset))
458 == 0)
459 return (char *) domain->data
460 + W (domain->must_swap, domain->trans_tab[nstr - 1].offset);
462 /* NOTREACHED */
465 /* Now we try the default method: binary search in the sorted
466 array of messages. */
467 bottom = 0;
468 act = 0;
469 top = domain->nstrings;
470 while (bottom < top)
472 int cmp_val;
474 act = (bottom + top) / 2;
475 cmp_val = strcmp (msgid, domain->data
476 + W (domain->must_swap,
477 domain->orig_tab[act].offset));
478 if (cmp_val < 0)
479 top = act;
480 else if (cmp_val > 0)
481 bottom = act + 1;
482 else
483 break;
486 /* If an translation is found return this. */
487 return bottom >= top ? NULL : (char *) domain->data
488 + W (domain->must_swap,
489 domain->trans_tab[act].offset);
493 /* Return string representation of locale CATEGORY. */
494 static const char *
495 internal_function
496 category_to_name (category)
497 int category;
499 const char *retval;
501 switch (category)
503 #ifdef LC_COLLATE
504 case LC_COLLATE:
505 retval = "LC_COLLATE";
506 break;
507 #endif
508 #ifdef LC_CTYPE
509 case LC_CTYPE:
510 retval = "LC_CTYPE";
511 break;
512 #endif
513 #ifdef LC_MONETARY
514 case LC_MONETARY:
515 retval = "LC_MONETARY";
516 break;
517 #endif
518 #ifdef LC_NUMERIC
519 case LC_NUMERIC:
520 retval = "LC_NUMERIC";
521 break;
522 #endif
523 #ifdef LC_TIME
524 case LC_TIME:
525 retval = "LC_TIME";
526 break;
527 #endif
528 #ifdef LC_MESSAGES
529 case LC_MESSAGES:
530 retval = "LC_MESSAGES";
531 break;
532 #endif
533 #ifdef LC_RESPONSE
534 case LC_RESPONSE:
535 retval = "LC_RESPONSE";
536 break;
537 #endif
538 #ifdef LC_ALL
539 case LC_ALL:
540 /* This might not make sense but is perhaps better than any other
541 value. */
542 retval = "LC_ALL";
543 break;
544 #endif
545 default:
546 /* If you have a better idea for a default value let me know. */
547 retval = "LC_XXX";
550 return retval;
553 /* Guess value of current locale from value of the environment variables. */
554 static const char *
555 internal_function
556 guess_category_value (category, categoryname)
557 int category;
558 const char *categoryname;
560 const char *retval;
561 (void) category; /* shut up compiler */
562 (void) categoryname; /* ditto */
564 /* The highest priority value is the `LANGUAGE' environment
565 variable. This is a GNU extension. */
566 retval = getenv ("LANGUAGE");
567 if (retval != NULL && retval[0] != '\0')
568 return retval;
570 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
571 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
572 systems this can be done by the `setlocale' function itself. */
573 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
574 return setlocale (category, NULL);
575 #else
576 /* Setting of LC_ALL overwrites all other. */
577 retval = getenv ("LC_ALL");
578 if (retval != NULL && retval[0] != '\0')
579 return retval;
581 /* Next comes the name of the desired category. */
582 retval = getenv (categoryname);
583 if (retval != NULL && retval[0] != '\0')
584 return retval;
586 /* Last possibility is the LANG environment variable. */
587 retval = getenv ("LANG");
588 if (retval != NULL && retval[0] != '\0')
589 return retval;
591 /* We use C as the default domain. POSIX says this is implementation
592 defined. */
593 return "C";
594 #endif
597 /* @@ begin of epilog @@ */
599 /* We don't want libintl.a to depend on any other library. So we
600 avoid the non-standard function stpcpy. In GNU C Library this
601 function is available, though. Also allow the symbol HAVE_STPCPY
602 to be defined. */
603 #if !_LIBC && !HAVE_STPCPY
604 static char *
605 stpcpy (dest, src)
606 char *dest;
607 const char *src;
609 while ((*dest++ = *src++) != '\0')
610 /* Do nothing. */ ;
611 return dest - 1;
613 #endif
616 #ifdef _LIBC
617 /* If we want to free all resources we have to do some work at
618 program's end. */
619 static void __attribute__ ((unused))
620 free_mem (void)
622 struct binding *runp;
624 for (runp = _nl_domain_bindings; runp != NULL; runp = runp->next)
626 free (runp->domainname);
627 if (runp->dirname != _nl_default_dirname)
628 /* Yes, this is a pointer comparison. */
629 free (runp->dirname);
632 if (_nl_current_default_domain != _nl_default_default_domain)
633 /* Yes, again a pointer comparison. */
634 free ((char *) _nl_current_default_domain);
637 text_set_element (__libc_subfreeres, free_mem);
638 #endif