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