FileUtils.c: fix CERT VU#575804
[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 librararies 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
43 /* for Xrm */
44 #include <X11/Intrinsic.h>
45
46 /*=================================================================
47 $SHAREDBEG$:  This header appears in all appropriate DtXlate topics
48 =======================================================$SKIP$======*/
49 /*$INCLUDE$*/ 
50 #include "HelpXlate.h"
51 #include "Lock.h"
52 /*$END$*/
53
54 static char       MyPlatform[_DtPLATFORM_MAX_LEN+1];
55 static _DtXlateDb MyDb = NULL;
56 static int        ExecVer;
57 static int        CompVer;
58
59 static const char *DfltStdCharset = "ISO-8859-1";
60 static const char *DfltStdLang = "C";
61
62 /*========================================================*/
63 /*================== Private routines ====================*/
64 /*========================================================*/
65 /******************************************************************************
66  * Function:    static int OpenLcxDb ()
67  *
68  * Parameters:   none
69  *
70  * Return Value:  0: ok
71  *               -1: error
72  *
73  * errno Values:
74  *
75  * Purpose: Opens the Ce-private Lcx database
76  *
77  *****************************************************************************/
78 static int
79 OpenLcxDb (void)
80 {
81     time_t      time1  = 0;
82     time_t      time2  = 0;
83     static short          MyProcess = False;
84     static short          MyFirst   = True;
85
86     /*
87      * wait 30 sec. until another thread or enter is done modifying the table
88      */
89     while (MyProcess == True)
90       {
91         /* if time out, return */
92         if (time(&time2) == (time_t)-1)
93             return -1;
94
95         if (time1 == 0)
96             time1 = time2;
97         else if (time2 - time1 >= (time_t)30)
98             return -1;
99       }
100
101     _DtHelpProcessLock();
102     if (MyFirst == True)
103       {
104         MyProcess = True;
105         if (_DtLcxOpenAllDbs(&MyDb) == 0 &&
106                 _DtXlateGetXlateEnv(MyDb,MyPlatform,&ExecVer,&CompVer) != 0)
107           {
108             _DtLcxCloseDb(&MyDb);
109             MyDb = NULL;
110           }
111         MyFirst = False;
112         MyProcess = False;
113       }
114     _DtHelpProcessUnlock();
115
116     return (MyDb == NULL ? -1 : 0 );
117 }
118
119 /******************************************************************************
120  * Function:    int _DtHelpCeXlateStdToOpLocale ( char *operation, char *stdLoc
121 ale,
122  *                                      char *dflt_opLocale, char **ret_opLocale
123 )
124  *
125  * Parameters:
126  *    operation         operation whose locale value will be retrieved
127  *    stdLocale         standard locale value
128  *    dflt_opLocale     operation-specific locale-value
129  *                      This is the default value used in error case
130  *    ret_opLocale      operation-specific locale-value placed here
131  *                      Caller must free this string.
132  *
133  * Return Value:
134  *
135  * Purpose: Gets an operation-specific locale string given the standard string
136  *
137  *****************************************************************************/
138 void
139 _DtHelpCeXlateStdToOpLocale (
140      char       *operation,
141      char       *stdLocale,
142      char       *dflt_opLocale,
143      char       **ret_opLocale)
144 {
145     int result = OpenLcxDb();
146
147     _DtHelpProcessLock();
148     if (result == 0)
149       {
150         (void) _DtLcxXlateStdToOp(MyDb, MyPlatform, CompVer,
151                         operation, stdLocale, NULL, NULL, NULL, ret_opLocale);
152       }
153     _DtHelpProcessUnlock();
154
155     /* if translation fails, use a default value */
156     if (ret_opLocale && (result != 0 || *ret_opLocale == NULL))
157       {
158         if (dflt_opLocale) *ret_opLocale = strdup(dflt_opLocale);
159         else if (stdLocale) *ret_opLocale = strdup(stdLocale);
160       }
161 }
162
163
164 /******************************************************************************
165  * Function:    int _DtHelpCeXlateOpToStdLocale (char *operation, char *opLocale
166 ,
167  *                         char **ret_stdLocale, char **ret_stdLang, char **ret_
168 stdSet)
169  *
170  * Parameters:
171  *              operation       Operation associated with the locale value
172  *              opLocale        An operation-specific locale string
173  *              ret_locale      Returns the std locale
174  *                              Caller must free this string.
175  *              ret_stdLang        Returns the std language & territory string.
176  *                              Caller must free this string.
177  *              ret_stdSet         Returns the std code set string.
178  *                              Caller must free this string.
179  *
180  * Return Value:
181  *
182  * Purpose:  Gets the standard locale given an operation and its locale
183  *
184  *****************************************************************************/
185 void
186 _DtHelpCeXlateOpToStdLocale (
187      char       *operation,
188      char       *opLocale,
189      char       **ret_stdLocale,
190      char       **ret_stdLang,
191      char       **ret_stdSet)
192 {
193     int result = OpenLcxDb();
194
195     _DtHelpProcessLock();
196     if (result == 0)
197       {
198         (void) _DtLcxXlateOpToStd(MyDb, MyPlatform, CompVer,
199                                 operation,opLocale,
200                                 ret_stdLocale, ret_stdLang, ret_stdSet, NULL);
201       }
202     _DtHelpProcessUnlock();
203
204     /* if failed, give default values */
205     if (ret_stdLocale != NULL && (result != 0 || *ret_stdLocale == NULL))
206       {
207         *ret_stdLocale = malloc(
208                                 strlen(DfltStdLang)+strlen(DfltStdCharset)+3);
209         sprintf(*ret_stdLocale,"%s.%s",DfltStdLang,DfltStdCharset);
210       }
211     if (ret_stdLang != NULL && (result != 0 || *ret_stdLang == NULL))
212         *ret_stdLang = strdup(DfltStdLang);
213     if (ret_stdSet != NULL && (result != 0 || *ret_stdSet == NULL))
214         *ret_stdSet = strdup(DfltStdCharset);
215 }
216
217 /******************************************************************************
218  * Function:    int _DtHelpCeGetMbLen (char *lang, char *char_set)
219  *
220  * Parameters:
221  *              lang            Represents the language. A NULL value
222  *                              defaults to "C".
223  *              char_set        Represents the character set. A NULL
224  *                              value value defaults to "ISO-8859-1"
225  *
226  * Return Value:        Returns the MB_CUR_MAX for the combination
227  *                      lang.charset.
228  *
229  * errno Values:
230  *
231  * Purpose: To determine the maximum number of bytes required to display
232  *          a character if/when the environment is set to 'lang.charset'
233  *
234  *****************************************************************************/
235 int
236 _DtHelpCeGetMbLen (
237      char       *lang,
238      char       *char_set)
239 {
240     int           retLen = 1;
241
242     if (lang == NULL)
243         lang = (char *)DfltStdLang;
244
245     if (char_set == NULL)
246         char_set = (char *)DfltStdCharset;
247
248     _DtHelpProcessLock();
249     if (OpenLcxDb() == 0)
250       {
251         /* if translation is present, lang.charset are a multibyte locale */
252         if (_DtLcxXlateStdToOp(MyDb, MyPlatform, CompVer, DtLCX_OPER_MULTIBYTE,
253                                 NULL, lang, char_set, NULL, NULL) == 0)
254             retLen = MB_CUR_MAX;
255       }
256     _DtHelpProcessUnlock();
257
258     return retLen;
259 }
260