HelpXlate.c 8.5 KB

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