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