xref: /netbsd-src/external/gpl3/gdb.old/dist/intl/textdomain.c (revision bb16d22702ff57c46e117881dd16b08ca16721cc)
1*bb16d227Schristos /* Implementation of the textdomain(3) function.
2*bb16d227Schristos    Copyright (C) 1995-1998, 2000, 2001, 2002 Free Software Foundation, Inc.
3*bb16d227Schristos 
4*bb16d227Schristos    This program is free software; you can redistribute it and/or modify it
5*bb16d227Schristos    under the terms of the GNU Library General Public License as published
6*bb16d227Schristos    by the Free Software Foundation; either version 2, or (at your option)
7*bb16d227Schristos    any later version.
8*bb16d227Schristos 
9*bb16d227Schristos    This program is distributed in the hope that it will be useful,
10*bb16d227Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*bb16d227Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12*bb16d227Schristos    Library General Public License for more details.
13*bb16d227Schristos 
14*bb16d227Schristos    You should have received a copy of the GNU Library General Public
15*bb16d227Schristos    License along with this program; if not, write to the Free Software
16*bb16d227Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
17*bb16d227Schristos    USA.  */
18*bb16d227Schristos 
19*bb16d227Schristos #ifdef HAVE_CONFIG_H
20*bb16d227Schristos # include <config.h>
21*bb16d227Schristos #endif
22*bb16d227Schristos 
23*bb16d227Schristos #include <stdlib.h>
24*bb16d227Schristos #include <string.h>
25*bb16d227Schristos 
26*bb16d227Schristos #ifdef _LIBC
27*bb16d227Schristos # include <libintl.h>
28*bb16d227Schristos #else
29*bb16d227Schristos # include "libgnuintl.h"
30*bb16d227Schristos #endif
31*bb16d227Schristos #include "gettextP.h"
32*bb16d227Schristos 
33*bb16d227Schristos #ifdef _LIBC
34*bb16d227Schristos /* We have to handle multi-threaded applications.  */
35*bb16d227Schristos # include <bits/libc-lock.h>
36*bb16d227Schristos #else
37*bb16d227Schristos /* Provide dummy implementation if this is outside glibc.  */
38*bb16d227Schristos # define __libc_rwlock_define(CLASS, NAME)
39*bb16d227Schristos # define __libc_rwlock_wrlock(NAME)
40*bb16d227Schristos # define __libc_rwlock_unlock(NAME)
41*bb16d227Schristos #endif
42*bb16d227Schristos 
43*bb16d227Schristos /* The internal variables in the standalone libintl.a must have different
44*bb16d227Schristos    names than the internal variables in GNU libc, otherwise programs
45*bb16d227Schristos    using libintl.a cannot be linked statically.  */
46*bb16d227Schristos #if !defined _LIBC
47*bb16d227Schristos # define _nl_default_default_domain libintl_nl_default_default_domain
48*bb16d227Schristos # define _nl_current_default_domain libintl_nl_current_default_domain
49*bb16d227Schristos #endif
50*bb16d227Schristos 
51*bb16d227Schristos /* @@ end of prolog @@ */
52*bb16d227Schristos 
53*bb16d227Schristos /* Name of the default text domain.  */
54*bb16d227Schristos extern const char _nl_default_default_domain[] attribute_hidden;
55*bb16d227Schristos 
56*bb16d227Schristos /* Default text domain in which entries for gettext(3) are to be found.  */
57*bb16d227Schristos extern const char *_nl_current_default_domain attribute_hidden;
58*bb16d227Schristos 
59*bb16d227Schristos 
60*bb16d227Schristos /* Names for the libintl functions are a problem.  They must not clash
61*bb16d227Schristos    with existing names and they should follow ANSI C.  But this source
62*bb16d227Schristos    code is also used in GNU C Library where the names have a __
63*bb16d227Schristos    prefix.  So we have to make a difference here.  */
64*bb16d227Schristos #ifdef _LIBC
65*bb16d227Schristos # define TEXTDOMAIN __textdomain
66*bb16d227Schristos # ifndef strdup
67*bb16d227Schristos #  define strdup(str) __strdup (str)
68*bb16d227Schristos # endif
69*bb16d227Schristos #else
70*bb16d227Schristos # define TEXTDOMAIN libintl_textdomain
71*bb16d227Schristos #endif
72*bb16d227Schristos 
73*bb16d227Schristos /* Lock variable to protect the global data in the gettext implementation.  */
74*bb16d227Schristos __libc_rwlock_define (extern, _nl_state_lock attribute_hidden)
75*bb16d227Schristos 
76*bb16d227Schristos /* Set the current default message catalog to DOMAINNAME.
77*bb16d227Schristos    If DOMAINNAME is null, return the current default.
78*bb16d227Schristos    If DOMAINNAME is "", reset to the default of "messages".  */
79*bb16d227Schristos char *
80*bb16d227Schristos TEXTDOMAIN (domainname)
81*bb16d227Schristos      const char *domainname;
82*bb16d227Schristos {
83*bb16d227Schristos   char *new_domain;
84*bb16d227Schristos   char *old_domain;
85*bb16d227Schristos 
86*bb16d227Schristos   /* A NULL pointer requests the current setting.  */
87*bb16d227Schristos   if (domainname == NULL)
88*bb16d227Schristos     return (char *) _nl_current_default_domain;
89*bb16d227Schristos 
90*bb16d227Schristos   __libc_rwlock_wrlock (_nl_state_lock);
91*bb16d227Schristos 
92*bb16d227Schristos   old_domain = (char *) _nl_current_default_domain;
93*bb16d227Schristos 
94*bb16d227Schristos   /* If domain name is the null string set to default domain "messages".  */
95*bb16d227Schristos   if (domainname[0] == '\0'
96*bb16d227Schristos       || strcmp (domainname, _nl_default_default_domain) == 0)
97*bb16d227Schristos     {
98*bb16d227Schristos       _nl_current_default_domain = _nl_default_default_domain;
99*bb16d227Schristos       new_domain = (char *) _nl_current_default_domain;
100*bb16d227Schristos     }
101*bb16d227Schristos   else if (strcmp (domainname, old_domain) == 0)
102*bb16d227Schristos     /* This can happen and people will use it to signal that some
103*bb16d227Schristos        environment variable changed.  */
104*bb16d227Schristos     new_domain = old_domain;
105*bb16d227Schristos   else
106*bb16d227Schristos     {
107*bb16d227Schristos       /* If the following malloc fails `_nl_current_default_domain'
108*bb16d227Schristos 	 will be NULL.  This value will be returned and so signals we
109*bb16d227Schristos 	 are out of core.  */
110*bb16d227Schristos #if defined _LIBC || defined HAVE_STRDUP
111*bb16d227Schristos       new_domain = strdup (domainname);
112*bb16d227Schristos #else
113*bb16d227Schristos       size_t len = strlen (domainname) + 1;
114*bb16d227Schristos       new_domain = (char *) malloc (len);
115*bb16d227Schristos       if (new_domain != NULL)
116*bb16d227Schristos 	memcpy (new_domain, domainname, len);
117*bb16d227Schristos #endif
118*bb16d227Schristos 
119*bb16d227Schristos       if (new_domain != NULL)
120*bb16d227Schristos 	_nl_current_default_domain = new_domain;
121*bb16d227Schristos     }
122*bb16d227Schristos 
123*bb16d227Schristos   /* We use this possibility to signal a change of the loaded catalogs
124*bb16d227Schristos      since this is most likely the case and there is no other easy we
125*bb16d227Schristos      to do it.  Do it only when the call was successful.  */
126*bb16d227Schristos   if (new_domain != NULL)
127*bb16d227Schristos     {
128*bb16d227Schristos       ++_nl_msg_cat_cntr;
129*bb16d227Schristos 
130*bb16d227Schristos       if (old_domain != new_domain && old_domain != _nl_default_default_domain)
131*bb16d227Schristos 	free (old_domain);
132*bb16d227Schristos     }
133*bb16d227Schristos 
134*bb16d227Schristos   __libc_rwlock_unlock (_nl_state_lock);
135*bb16d227Schristos 
136*bb16d227Schristos   return new_domain;
137*bb16d227Schristos }
138*bb16d227Schristos 
139*bb16d227Schristos #ifdef _LIBC
140*bb16d227Schristos /* Alias for function name in GNU C Library.  */
141*bb16d227Schristos weak_alias (__textdomain, textdomain);
142*bb16d227Schristos #endif
143