Merge branch 'master' into cde-next
[oweals/cde.git] / cde / lib / DtHelp / HelpXlate.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: HelpXlate.c /main/1 1996/08/22 09:16:03 rswiston $ */
24 /****************************************************************************
25 $FILEBEG$:    HelpXlate.c
26 $PROJECT$:    Cde 1.0
27 $COMPONENT$:  DtXlate service
28 $1LINER$:     Implements a translation service using tables and regex search
29 $COPYRIGHT$:
30  (c) Copyright 1993, 1994 Hewlett-Packard Company
31  (c) Copyright 1993, 1994 International Business Machines Corp.
32  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
33  (c) Copyright 1993, 1994 Unix System Labs, Inc., a subsidiary of Novell, Inc.
34 $END$
35  ****************************************************************************
36  ************************************<+>*************************************/
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/param.h>        /* MAXPATHLEN */
42 #include <time.h>
43
44 /* for Xrm */
45 #include <X11/Intrinsic.h>
46
47 /*=================================================================
48 $SHAREDBEG$:  This header appears in all appropriate DtXlate topics
49 =======================================================$SKIP$======*/
50 /*$INCLUDE$*/ 
51 #include "HelpXlate.h"
52 #include "Lock.h"
53 /*$END$*/
54
55 static char       MyPlatform[_DtPLATFORM_MAX_LEN+1];
56 static _DtXlateDb MyDb = NULL;
57 static int        ExecVer;
58 static int        CompVer;
59
60 static const char *DfltStdCharset = "ISO-8859-1";
61 static const char *DfltStdLang = "C";
62
63 /*========================================================*/
64 /*================== Private routines ====================*/
65 /*========================================================*/
66 /******************************************************************************
67  * Function:    static int OpenLcxDb ()
68  *
69  * Parameters:   none
70  *
71  * Return Value:  0: ok
72  *               -1: error
73  *
74  * errno Values:
75  *
76  * Purpose: Opens the Ce-private Lcx database
77  *
78  *****************************************************************************/
79 static int
80 OpenLcxDb (void)
81 {
82     time_t      time1  = 0;
83     time_t      time2  = 0;
84     static short          MyProcess = False;
85     static short          MyFirst   = True;
86
87     /*
88      * wait 30 sec. until another thread or enter is done modifying the table
89      */
90     while (MyProcess == True)
91       {
92         /* if time out, return */
93         if (time(&time2) == (time_t)-1)
94             return -1;
95
96         if (time1 == 0)
97             time1 = time2;
98         else if (time2 - time1 >= (time_t)30)
99             return -1;
100       }
101
102     _DtHelpProcessLock();
103     if (MyFirst == True)
104       {
105         MyProcess = True;
106         if (_DtLcxOpenAllDbs(&MyDb) == 0 &&
107                 _DtXlateGetXlateEnv(MyDb,MyPlatform,&ExecVer,&CompVer) != 0)
108           {
109             _DtLcxCloseDb(&MyDb);
110             MyDb = NULL;
111           }
112         MyFirst = False;
113         MyProcess = False;
114       }
115     _DtHelpProcessUnlock();
116
117     return (MyDb == NULL ? -1 : 0 );
118 }
119
120 /******************************************************************************
121  * Function:    int _DtHelpCeXlateStdToOpLocale ( char *operation, char *stdLoc
122 ale,
123  *                                      char *dflt_opLocale, char **ret_opLocale
124 )
125  *
126  * Parameters:
127  *    operation         operation whose locale value will be retrieved
128  *    stdLocale         standard locale value
129  *    dflt_opLocale     operation-specific locale-value
130  *                      This is the default value used in error case
131  *    ret_opLocale      operation-specific locale-value placed here
132  *                      Caller must free this string.
133  *
134  * Return Value:
135  *
136  * Purpose: Gets an operation-specific locale string given the standard string
137  *
138  *****************************************************************************/
139 void
140 _DtHelpCeXlateStdToOpLocale (
141      char       *operation,
142      char       *stdLocale,
143      char       *dflt_opLocale,
144      char       **ret_opLocale)
145 {
146     int result = OpenLcxDb();
147
148     _DtHelpProcessLock();
149     if (result == 0)
150       {
151         (void) _DtLcxXlateStdToOp(MyDb, MyPlatform, CompVer,
152                         operation, stdLocale, NULL, NULL, NULL, ret_opLocale);
153       }
154     _DtHelpProcessUnlock();
155
156     /* if translation fails, use a default value */
157     if (ret_opLocale && (result != 0 || *ret_opLocale == NULL))
158       {
159         if (dflt_opLocale) *ret_opLocale = strdup(dflt_opLocale);
160         else if (stdLocale) *ret_opLocale = strdup(stdLocale);
161       }
162 }
163
164
165 /******************************************************************************
166  * Function:    int _DtHelpCeXlateOpToStdLocale (char *operation, char *opLocale
167 ,
168  *                         char **ret_stdLocale, char **ret_stdLang, char **ret_
169 stdSet)
170  *
171  * Parameters:
172  *              operation       Operation associated with the locale value
173  *              opLocale        An operation-specific locale string
174  *              ret_locale      Returns the std locale
175  *                              Caller must free this string.
176  *              ret_stdLang        Returns the std language & territory string.
177  *                              Caller must free this string.
178  *              ret_stdSet         Returns the std code set string.
179  *                              Caller must free this string.
180  *
181  * Return Value:
182  *
183  * Purpose:  Gets the standard locale given an operation and its locale
184  *
185  *****************************************************************************/
186 void
187 _DtHelpCeXlateOpToStdLocale (
188      char       *operation,
189      char       *opLocale,
190      char       **ret_stdLocale,
191      char       **ret_stdLang,
192      char       **ret_stdSet)
193 {
194     int result = OpenLcxDb();
195
196     _DtHelpProcessLock();
197     if (result == 0)
198       {
199         (void) _DtLcxXlateOpToStd(MyDb, MyPlatform, CompVer,
200                                 operation,opLocale,
201                                 ret_stdLocale, ret_stdLang, ret_stdSet, NULL);
202       }
203     _DtHelpProcessUnlock();
204
205     /* if failed, give default values */
206     if (ret_stdLocale != NULL && (result != 0 || *ret_stdLocale == NULL))
207       {
208         *ret_stdLocale = malloc(
209                                 strlen(DfltStdLang)+strlen(DfltStdCharset)+3);
210         sprintf(*ret_stdLocale,"%s.%s",DfltStdLang,DfltStdCharset);
211       }
212     if (ret_stdLang != NULL && (result != 0 || *ret_stdLang == NULL))
213         *ret_stdLang = strdup(DfltStdLang);
214     if (ret_stdSet != NULL && (result != 0 || *ret_stdSet == NULL))
215         *ret_stdSet = strdup(DfltStdCharset);
216 }
217
218 /******************************************************************************
219  * Function:    int _DtHelpCeGetMbLen (char *lang, char *char_set)
220  *
221  * Parameters:
222  *              lang            Represents the language. A NULL value
223  *                              defaults to "C".
224  *              char_set        Represents the character set. A NULL
225  *                              value value defaults to "ISO-8859-1"
226  *
227  * Return Value:        Returns the MB_CUR_MAX for the combination
228  *                      lang.charset.
229  *
230  * errno Values:
231  *
232  * Purpose: To determine the maximum number of bytes required to display
233  *          a character if/when the environment is set to 'lang.charset'
234  *
235  *****************************************************************************/
236 int
237 _DtHelpCeGetMbLen (
238      char       *lang,
239      char       *char_set)
240 {
241     int           retLen = 1;
242
243     if (lang == NULL)
244         lang = (char *)DfltStdLang;
245
246     if (char_set == NULL)
247         char_set = (char *)DfltStdCharset;
248
249     _DtHelpProcessLock();
250     if (OpenLcxDb() == 0)
251       {
252         /* if translation is present, lang.charset are a multibyte locale */
253         if (_DtLcxXlateStdToOp(MyDb, MyPlatform, CompVer, DtLCX_OPER_MULTIBYTE,
254                                 NULL, lang, char_set, NULL, NULL) == 0)
255             retLen = MB_CUR_MAX;
256       }
257     _DtHelpProcessUnlock();
258
259     return retLen;
260 }
261