m_text.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright (c) 1996, by Sun Microsystems, Inc.
24 * All rights reserved.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29/*
30 * MKS interface to XPG message internationalization routines.
31 * Copyright 1989, 1992 by Mortice Kern Systems Inc.  All rights reserved.
32 *
33 * Written by T. J. Thompson
34 */
35#ifdef M_RCSID
36#ifndef lint
37static char rcsID[] = "$Header: /rd/src/libc/i18n/rcs/m_text.c 1.18 1995/02/02 16:42:09 jeffhe Exp $";
38#endif
39#endif
40
41#define	I18N	1	/* InternaltionalizatioN on */
42
43#include <mks.h>
44#include <locale.h>
45#include <nl_types.h>
46#include <stdlib.h>
47#include <errno.h>
48#include <string.h>
49
50static nl_catd catd = (nl_catd)-1;
51static char *domain = NULL;	/* remember domain chosen */
52static char *locale = NULL;	/* remember locale loaded */
53
54#ifdef	M_VARIANTS
55/*f
56 * Note: All the text strings in the messaging database must be stored in
57 * the codeset of the compiled program.  xlate will convert from that codeset,
58 * into that of the user.
59 */
60static char *
61xlate(char *s)
62{
63	char *new = strdup(s);
64	static char *lastmsg;
65
66	/* No memory? Return untranslated string */
67	if (new == NULL)
68		return s;
69	/* Free previour string */
70	if (lastmsg != NULL)
71		free(lastmsg);
72	lastmsg = new;
73
74	/* Do *not* translate the leading ! which indicates perror */
75	if (*new == '!')
76		new++;
77	/* And translate the string */
78	M_INVARIANTINIT();
79	for ( ; *new != '\0'; new++)
80		*new = M_UNVARIANT(*new);
81
82	return lastmsg;
83}
84#else
85#define	xlate(s)	(s)
86#endif
87
88STATIC void
89textclose()
90{
91	m_textdomain(NULL);
92}
93
94void
95m_textdomain(str)
96char *str;
97{
98	if (catd != (nl_catd)-1)
99		(void)catclose(catd);
100	catd = (nl_catd)-1;
101	if (domain != NULL)
102		free(domain);
103	domain = str==NULL ? NULL : strdup(str);
104}
105
106/*f
107 * Given a message id number, and a default string, call the XPG cat*
108 * functions to look up the message, or return just the default string.
109 */
110char *
111m_textmsg(id, str, cls)
112int id;
113const char *str;
114char *cls;	/* NOT USED */
115{
116	int errsave = errno;
117	char *nlocale;
118	char *cp;
119
120	nlocale = setlocale(LC_MESSAGES, NULL);	/* Query current locale */
121	if (catd == (nl_catd)-1			        /* catalog not open */
122	||  nlocale == NULL				/* impossible? */
123	||  locale == NULL				/* locale never set */
124	||  strcmp(locale, nlocale)!=0) {		/* locale changed */
125
126		/* Do not re-try a failed catopen */
127		if (locale != NULL && nlocale != NULL && domain != NULL
128		&& strcmp(locale, nlocale) == 0) {
129			errno = errsave;
130			return (xlate((char *)str));
131		}
132
133		if (catd != (nl_catd)-1)
134			(void)catclose(catd);
135		if (domain==NULL)
136			m_textdomain(M_NL_DOM);
137		if (locale != NULL)
138			free(locale);
139		locale = nlocale==NULL ? NULL : strdup(nlocale);
140#ifdef NL_CAT_LOCALE /* XPG4 - April 1992 - not final version! */
141		if ((catd = catopen(domain, NL_CAT_LOCALE)) == (nl_catd)-1) {
142#else
143		if ((catd = catopen(domain, 0)) == (nl_catd)-1) {
144#endif
145			errno = errsave;
146			return (xlate((char *)str));
147		}
148		atexit(textclose);
149	}
150	cp = catgets(catd, NL_SETD, id, (char *)str);
151	errno = errsave;
152	return xlate(cp);
153}
154