HelpXlate.c 8.5 KB

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