xref: /illumos-gate/usr/src/lib/libc/port/i18n/gettext.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #pragma weak bindtextdomain = _bindtextdomain
30*7c478bd9Sstevel@tonic-gate #pragma weak textdomain = _textdomain
31*7c478bd9Sstevel@tonic-gate #pragma weak gettext = _gettext
32*7c478bd9Sstevel@tonic-gate #pragma weak dgettext = _dgettext
33*7c478bd9Sstevel@tonic-gate #pragma weak dcgettext = _dcgettext
34*7c478bd9Sstevel@tonic-gate #pragma weak ngettext = _ngettext
35*7c478bd9Sstevel@tonic-gate #pragma weak dngettext = _dngettext
36*7c478bd9Sstevel@tonic-gate #pragma weak dcngettext = _dcngettext
37*7c478bd9Sstevel@tonic-gate #pragma weak bind_textdomain_codeset = _bind_textdomain_codeset
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "synonyms.h"
40*7c478bd9Sstevel@tonic-gate #include "mtlib.h"
41*7c478bd9Sstevel@tonic-gate #include <errno.h>
42*7c478bd9Sstevel@tonic-gate #include <ctype.h>
43*7c478bd9Sstevel@tonic-gate #include <locale.h>
44*7c478bd9Sstevel@tonic-gate #include <stdio.h>
45*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
48*7c478bd9Sstevel@tonic-gate #include <libintl.h>
49*7c478bd9Sstevel@tonic-gate #include <thread.h>
50*7c478bd9Sstevel@tonic-gate #include <synch.h>
51*7c478bd9Sstevel@tonic-gate #include "libc.h"
52*7c478bd9Sstevel@tonic-gate #include "_loc_path.h"
53*7c478bd9Sstevel@tonic-gate #include "msgfmt.h"
54*7c478bd9Sstevel@tonic-gate #include "gettext.h"
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #define	INIT_GT(def) \
57*7c478bd9Sstevel@tonic-gate 	if (!global_gt) { \
58*7c478bd9Sstevel@tonic-gate 		global_gt = (Gettext_t *)calloc(1, sizeof (Gettext_t)); \
59*7c478bd9Sstevel@tonic-gate 		if (global_gt) \
60*7c478bd9Sstevel@tonic-gate 			global_gt->cur_domain = (char *)default_domain; \
61*7c478bd9Sstevel@tonic-gate 		else { \
62*7c478bd9Sstevel@tonic-gate 			fork_lock_exit(); \
63*7c478bd9Sstevel@tonic-gate 			return ((def)); \
64*7c478bd9Sstevel@tonic-gate 		} \
65*7c478bd9Sstevel@tonic-gate 	}
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate const char	*defaultbind = DEFAULT_BINDING;
68*7c478bd9Sstevel@tonic-gate const char	default_domain[] = DEFAULT_DOMAIN;
69*7c478bd9Sstevel@tonic-gate Gettext_t	*global_gt = NULL;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate char *
72*7c478bd9Sstevel@tonic-gate _bindtextdomain(const char *domain, const char *binding)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	char	*res;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	(void) fork_lock_enter(NULL);
77*7c478bd9Sstevel@tonic-gate 	INIT_GT(NULL);
78*7c478bd9Sstevel@tonic-gate 	res = _real_bindtextdomain_u(domain, binding, TP_BINDING);
79*7c478bd9Sstevel@tonic-gate 	fork_lock_exit();
80*7c478bd9Sstevel@tonic-gate 	return (res);
81*7c478bd9Sstevel@tonic-gate }
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate char *
84*7c478bd9Sstevel@tonic-gate _bind_textdomain_codeset(const char *domain, const char *codeset)
85*7c478bd9Sstevel@tonic-gate {
86*7c478bd9Sstevel@tonic-gate 	char	*res;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	(void) fork_lock_enter(NULL);
89*7c478bd9Sstevel@tonic-gate 	INIT_GT(NULL);
90*7c478bd9Sstevel@tonic-gate 	res = _real_bindtextdomain_u(domain, codeset, TP_CODESET);
91*7c478bd9Sstevel@tonic-gate 	fork_lock_exit();
92*7c478bd9Sstevel@tonic-gate 	return (res);
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate /*
96*7c478bd9Sstevel@tonic-gate  * textdomain() sets or queries the name of the current domain of
97*7c478bd9Sstevel@tonic-gate  * the active LC_MESSAGES locale category.
98*7c478bd9Sstevel@tonic-gate  */
99*7c478bd9Sstevel@tonic-gate char *
100*7c478bd9Sstevel@tonic-gate _textdomain(const char *domain)
101*7c478bd9Sstevel@tonic-gate {
102*7c478bd9Sstevel@tonic-gate 	char	*res;
103*7c478bd9Sstevel@tonic-gate 	char	tmp_domain[TEXTDOMAINMAX + 1];
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	(void) fork_lock_enter(NULL);
106*7c478bd9Sstevel@tonic-gate 	INIT_GT(NULL);
107*7c478bd9Sstevel@tonic-gate 	res = _textdomain_u(domain, tmp_domain);
108*7c478bd9Sstevel@tonic-gate 	if (res == NULL) {
109*7c478bd9Sstevel@tonic-gate 		fork_lock_exit();
110*7c478bd9Sstevel@tonic-gate 		return (NULL);
111*7c478bd9Sstevel@tonic-gate 	}
112*7c478bd9Sstevel@tonic-gate 	fork_lock_exit();
113*7c478bd9Sstevel@tonic-gate 	return (CURRENT_DOMAIN(global_gt));
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate /*
117*7c478bd9Sstevel@tonic-gate  * gettext() is a pass-thru to _real_gettext_u() with a NULL pointer passed
118*7c478bd9Sstevel@tonic-gate  * for domain and LC_MESSAGES passed for category.
119*7c478bd9Sstevel@tonic-gate  */
120*7c478bd9Sstevel@tonic-gate char *
121*7c478bd9Sstevel@tonic-gate _gettext(const char *msg_id)
122*7c478bd9Sstevel@tonic-gate {
123*7c478bd9Sstevel@tonic-gate 	char	*res;
124*7c478bd9Sstevel@tonic-gate 	int	errno_save = errno;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	(void) fork_lock_enter(NULL);
127*7c478bd9Sstevel@tonic-gate 	INIT_GT((char *)msg_id);
128*7c478bd9Sstevel@tonic-gate 	res = _real_gettext_u(NULL, msg_id, NULL, 0, LC_MESSAGES, 0);
129*7c478bd9Sstevel@tonic-gate 	fork_lock_exit();
130*7c478bd9Sstevel@tonic-gate 	errno = errno_save;
131*7c478bd9Sstevel@tonic-gate 	return (res);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate /*
136*7c478bd9Sstevel@tonic-gate  * In dcgettext() call, domain is valid only for this call.
137*7c478bd9Sstevel@tonic-gate  */
138*7c478bd9Sstevel@tonic-gate char *
139*7c478bd9Sstevel@tonic-gate _dgettext(const char *domain, const char *msg_id)
140*7c478bd9Sstevel@tonic-gate {
141*7c478bd9Sstevel@tonic-gate 	char	*res;
142*7c478bd9Sstevel@tonic-gate 	int	errno_save = errno;
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	(void) fork_lock_enter(NULL);
145*7c478bd9Sstevel@tonic-gate 	INIT_GT((char *)msg_id);
146*7c478bd9Sstevel@tonic-gate 	res = _real_gettext_u(domain, msg_id, NULL, 0, LC_MESSAGES, 0);
147*7c478bd9Sstevel@tonic-gate 	fork_lock_exit();
148*7c478bd9Sstevel@tonic-gate 	errno = errno_save;
149*7c478bd9Sstevel@tonic-gate 	return (res);
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate char *
153*7c478bd9Sstevel@tonic-gate _dcgettext(const char *domain, const char *msg_id, const int category)
154*7c478bd9Sstevel@tonic-gate {
155*7c478bd9Sstevel@tonic-gate 	char	*res;
156*7c478bd9Sstevel@tonic-gate 	int	errno_save = errno;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	(void) fork_lock_enter(NULL);
159*7c478bd9Sstevel@tonic-gate 	INIT_GT((char *)msg_id);
160*7c478bd9Sstevel@tonic-gate 	res = _real_gettext_u(domain, msg_id, NULL, 0, category, 0);
161*7c478bd9Sstevel@tonic-gate 	fork_lock_exit();
162*7c478bd9Sstevel@tonic-gate 	errno = errno_save;
163*7c478bd9Sstevel@tonic-gate 	return (res);
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate char *
167*7c478bd9Sstevel@tonic-gate _ngettext(const char *msgid1, const char *msgid2, unsigned long int n)
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate 	char	*res;
170*7c478bd9Sstevel@tonic-gate 	int	errno_save = errno;
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	(void) fork_lock_enter(NULL);
173*7c478bd9Sstevel@tonic-gate 	INIT_GT((char *)msgid1);
174*7c478bd9Sstevel@tonic-gate 	res = _real_gettext_u(NULL, msgid1, msgid2, n, LC_MESSAGES, 1);
175*7c478bd9Sstevel@tonic-gate 	fork_lock_exit();
176*7c478bd9Sstevel@tonic-gate 	errno = errno_save;
177*7c478bd9Sstevel@tonic-gate 	return (res);
178*7c478bd9Sstevel@tonic-gate }
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate char *
181*7c478bd9Sstevel@tonic-gate _dngettext(const char *domain, const char *msgid1, const char *msgid2,
182*7c478bd9Sstevel@tonic-gate 	unsigned long int n)
183*7c478bd9Sstevel@tonic-gate {
184*7c478bd9Sstevel@tonic-gate 	char	*res;
185*7c478bd9Sstevel@tonic-gate 	int	errno_save = errno;
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	(void) fork_lock_enter(NULL);
188*7c478bd9Sstevel@tonic-gate 	INIT_GT((char *)msgid1);
189*7c478bd9Sstevel@tonic-gate 	res = _real_gettext_u(domain, msgid1, msgid2, n, LC_MESSAGES, 1);
190*7c478bd9Sstevel@tonic-gate 	fork_lock_exit();
191*7c478bd9Sstevel@tonic-gate 	errno = errno_save;
192*7c478bd9Sstevel@tonic-gate 	return (res);
193*7c478bd9Sstevel@tonic-gate }
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate char *
196*7c478bd9Sstevel@tonic-gate _dcngettext(const char *domain, const char *msgid1, const char *msgid2,
197*7c478bd9Sstevel@tonic-gate 	unsigned long int n, int category)
198*7c478bd9Sstevel@tonic-gate {
199*7c478bd9Sstevel@tonic-gate 	char	*res;
200*7c478bd9Sstevel@tonic-gate 	int	errno_save = errno;
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	(void) fork_lock_enter(NULL);
203*7c478bd9Sstevel@tonic-gate 	INIT_GT((char *)msgid1);
204*7c478bd9Sstevel@tonic-gate 	res = _real_gettext_u(domain, msgid1, msgid2, n, category, 1);
205*7c478bd9Sstevel@tonic-gate 	fork_lock_exit();
206*7c478bd9Sstevel@tonic-gate 	errno = errno_save;
207*7c478bd9Sstevel@tonic-gate 	return (res);
208*7c478bd9Sstevel@tonic-gate }
209