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 (c) 1996, by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * MKS interface to XPG message internationalization routines.
29*7c478bd9Sstevel@tonic-gate  * Copyright 1989, 1992 by Mortice Kern Systems Inc.  All rights reserved.
30*7c478bd9Sstevel@tonic-gate  *
31*7c478bd9Sstevel@tonic-gate  * Written by T. J. Thompson
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate #ifdef M_RCSID
34*7c478bd9Sstevel@tonic-gate #ifndef lint
35*7c478bd9Sstevel@tonic-gate static char rcsID[] = "$Header: /rd/src/libc/i18n/rcs/m_text.c 1.18 1995/02/02 16:42:09 jeffhe Exp $";
36*7c478bd9Sstevel@tonic-gate #endif
37*7c478bd9Sstevel@tonic-gate #endif
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #define	I18N	1	/* InternaltionalizatioN on */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include <mks.h>
42*7c478bd9Sstevel@tonic-gate #include <locale.h>
43*7c478bd9Sstevel@tonic-gate #include <nl_types.h>
44*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
45*7c478bd9Sstevel@tonic-gate #include <errno.h>
46*7c478bd9Sstevel@tonic-gate #include <string.h>
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate static nl_catd catd = (nl_catd)-1;
49*7c478bd9Sstevel@tonic-gate static char *domain = NULL;	/* remember domain chosen */
50*7c478bd9Sstevel@tonic-gate static char *locale = NULL;	/* remember locale loaded */
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #ifdef	M_VARIANTS
53*7c478bd9Sstevel@tonic-gate /*f
54*7c478bd9Sstevel@tonic-gate  * Note: All the text strings in the messaging database must be stored in
55*7c478bd9Sstevel@tonic-gate  * the codeset of the compiled program.  xlate will convert from that codeset,
56*7c478bd9Sstevel@tonic-gate  * into that of the user.
57*7c478bd9Sstevel@tonic-gate  */
58*7c478bd9Sstevel@tonic-gate static char *
xlate(char * s)59*7c478bd9Sstevel@tonic-gate xlate(char *s)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate 	char *new = strdup(s);
62*7c478bd9Sstevel@tonic-gate 	static char *lastmsg;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	/* No memory? Return untranslated string */
65*7c478bd9Sstevel@tonic-gate 	if (new == NULL)
66*7c478bd9Sstevel@tonic-gate 		return s;
67*7c478bd9Sstevel@tonic-gate 	/* Free previour string */
68*7c478bd9Sstevel@tonic-gate 	if (lastmsg != NULL)
69*7c478bd9Sstevel@tonic-gate 		free(lastmsg);
70*7c478bd9Sstevel@tonic-gate 	lastmsg = new;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	/* Do *not* translate the leading ! which indicates perror */
73*7c478bd9Sstevel@tonic-gate 	if (*new == '!')
74*7c478bd9Sstevel@tonic-gate 		new++;
75*7c478bd9Sstevel@tonic-gate 	/* And translate the string */
76*7c478bd9Sstevel@tonic-gate 	M_INVARIANTINIT();
77*7c478bd9Sstevel@tonic-gate 	for ( ; *new != '\0'; new++)
78*7c478bd9Sstevel@tonic-gate 		*new = M_UNVARIANT(*new);
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	return lastmsg;
81*7c478bd9Sstevel@tonic-gate }
82*7c478bd9Sstevel@tonic-gate #else
83*7c478bd9Sstevel@tonic-gate #define	xlate(s)	(s)
84*7c478bd9Sstevel@tonic-gate #endif
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate STATIC void
textclose()87*7c478bd9Sstevel@tonic-gate textclose()
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate 	m_textdomain(NULL);
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate void
m_textdomain(str)93*7c478bd9Sstevel@tonic-gate m_textdomain(str)
94*7c478bd9Sstevel@tonic-gate char *str;
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	if (catd != (nl_catd)-1)
97*7c478bd9Sstevel@tonic-gate 		(void)catclose(catd);
98*7c478bd9Sstevel@tonic-gate 	catd = (nl_catd)-1;
99*7c478bd9Sstevel@tonic-gate 	if (domain != NULL)
100*7c478bd9Sstevel@tonic-gate 		free(domain);
101*7c478bd9Sstevel@tonic-gate 	domain = str==NULL ? NULL : strdup(str);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /*f
105*7c478bd9Sstevel@tonic-gate  * Given a message id number, and a default string, call the XPG cat*
106*7c478bd9Sstevel@tonic-gate  * functions to look up the message, or return just the default string.
107*7c478bd9Sstevel@tonic-gate  */
108*7c478bd9Sstevel@tonic-gate char *
m_textmsg(id,str,cls)109*7c478bd9Sstevel@tonic-gate m_textmsg(id, str, cls)
110*7c478bd9Sstevel@tonic-gate int id;
111*7c478bd9Sstevel@tonic-gate const char *str;
112*7c478bd9Sstevel@tonic-gate char *cls;	/* NOT USED */
113*7c478bd9Sstevel@tonic-gate {
114*7c478bd9Sstevel@tonic-gate 	int errsave = errno;
115*7c478bd9Sstevel@tonic-gate 	char *nlocale;
116*7c478bd9Sstevel@tonic-gate 	char *cp;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	nlocale = setlocale(LC_MESSAGES, NULL);	/* Query current locale */
119*7c478bd9Sstevel@tonic-gate 	if (catd == (nl_catd)-1			        /* catalog not open */
120*7c478bd9Sstevel@tonic-gate 	||  nlocale == NULL				/* impossible? */
121*7c478bd9Sstevel@tonic-gate 	||  locale == NULL				/* locale never set */
122*7c478bd9Sstevel@tonic-gate 	||  strcmp(locale, nlocale)!=0) {		/* locale changed */
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 		/* Do not re-try a failed catopen */
125*7c478bd9Sstevel@tonic-gate 		if (locale != NULL && nlocale != NULL && domain != NULL
126*7c478bd9Sstevel@tonic-gate 		&& strcmp(locale, nlocale) == 0) {
127*7c478bd9Sstevel@tonic-gate 			errno = errsave;
128*7c478bd9Sstevel@tonic-gate 			return (xlate((char *)str));
129*7c478bd9Sstevel@tonic-gate 		}
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 		if (catd != (nl_catd)-1)
132*7c478bd9Sstevel@tonic-gate 			(void)catclose(catd);
133*7c478bd9Sstevel@tonic-gate 		if (domain==NULL)
134*7c478bd9Sstevel@tonic-gate 			m_textdomain(M_NL_DOM);
135*7c478bd9Sstevel@tonic-gate 		if (locale != NULL)
136*7c478bd9Sstevel@tonic-gate 			free(locale);
137*7c478bd9Sstevel@tonic-gate 		locale = nlocale==NULL ? NULL : strdup(nlocale);
138*7c478bd9Sstevel@tonic-gate #ifdef NL_CAT_LOCALE /* XPG4 - April 1992 - not final version! */
139*7c478bd9Sstevel@tonic-gate 		if ((catd = catopen(domain, NL_CAT_LOCALE)) == (nl_catd)-1) {
140*7c478bd9Sstevel@tonic-gate #else
141*7c478bd9Sstevel@tonic-gate 		if ((catd = catopen(domain, 0)) == (nl_catd)-1) {
142*7c478bd9Sstevel@tonic-gate #endif
143*7c478bd9Sstevel@tonic-gate 			errno = errsave;
144*7c478bd9Sstevel@tonic-gate 			return (xlate((char *)str));
145*7c478bd9Sstevel@tonic-gate 		}
146*7c478bd9Sstevel@tonic-gate 		atexit(textclose);
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 	cp = catgets(catd, NL_SETD, id, (char *)str);
149*7c478bd9Sstevel@tonic-gate 	errno = errsave;
150*7c478bd9Sstevel@tonic-gate 	return xlate(cp);
151*7c478bd9Sstevel@tonic-gate }
152