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