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