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