userinit.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /* $TOG: userinit.c /main/6 1998/04/20 12:55:18 mgreess $ */
  24. #include "defs.h"
  25. #include "name.h"
  26. #include "variables.h"
  27. #include <Dt/DtNlUtils.h>
  28. #include <Dt/EnvControlP.h>
  29. #include <stdio.h>
  30. #include <nl_types.h>
  31. #include <X11/X.h>
  32. #include <X11/Intrinsic.h>
  33. #include <X11/IntrinsicP.h>
  34. #include <X11/CoreP.h>
  35. #include <X11/StringDefs.h>
  36. #include <Xm/XmStrDefs.h>
  37. #include <setjmp.h>
  38. #include <string.h>
  39. #include <ctype.h>
  40. #include <Xm/Xm.h>
  41. #include <Xm/Protocols.h>
  42. #include "hash.h"
  43. #include "stdio.h"
  44. #define NO_AST
  45. #include "dtksh.h"
  46. #undef NO_AST
  47. #include "xmksh.h"
  48. #include "dtkcmds.h"
  49. #include "xmcvt.h"
  50. #include "widget.h"
  51. #include "extra.h"
  52. #include "xmwidgets.h"
  53. #include "msgs.h"
  54. #include <locale.h>
  55. /*
  56. * LocaleChanged is defined in ksh93/src/cmd/ksh93/sh/init.c
  57. */
  58. extern void LocaleChanged (
  59. Namval_t * np,
  60. const char * val,
  61. int flags,
  62. Namfun_t * fp );
  63. static Namdisc_t localeDisc = { 0, LocaleChanged, NULL, NULL, NULL, NULL, NULL, NULL };
  64. static Namfun_t localeFun = {NULL, NULL };
  65. extern char *savedNlsPath; /* in ./ksh93/src/cmd/ksh93/sh/init.c */
  66. void
  67. SyncEnv(
  68. char *name)
  69. {
  70. char *value, *buf;
  71. value = getenv(name);
  72. if(value != (char *)NULL)
  73. {
  74. buf = malloc(strlen(name) + strlen(value) + 2);
  75. strcpy(buf, name);
  76. strcat(buf, "=");
  77. strcat(buf, value);
  78. ksh_putenv(buf);
  79. free(buf); /* I hope it's legal to free this! */
  80. }
  81. }
  82. /*
  83. * This is a hook for an additional initialization routine
  84. * A function of this name is called in main after sh_init().
  85. */
  86. void
  87. sh_userinit( void )
  88. {
  89. int * lockedFds;
  90. lockedFds = LockKshFileDescriptors();
  91. (void) XtSetLanguageProc((XtAppContext)NULL, (XtLanguageProc)NULL,
  92. (XtPointer)NULL);
  93. setlocale(LC_ALL, "");
  94. DtNlInitialize();
  95. _DtEnvControl(DT_ENV_SET);
  96. localeFun.disc = &localeDisc;
  97. nv_stack(LANGNOD, &localeFun);
  98. UnlockKshFileDescriptors(lockedFds);
  99. /*
  100. * Save the current setting of NLSPATH. The user/script may want to
  101. * set its own NLSPATH to access its message catalog, so we need to
  102. * remember where to find our own catalog(s). This saved path is used
  103. * in ksh93/src/cmd/ksh93/sh/init.c: _DtGetMessage(). We don't mess
  104. * with the user/script's setting of LANG as we want to track changes
  105. * in LANG.
  106. */
  107. savedNlsPath = strdup(getenv("NLSPATH"));
  108. /*
  109. * Sync the libc environment (set up by DtEnvControl) with our internal
  110. * hash table environment.
  111. */
  112. SyncEnv("NLSPATH");
  113. SyncEnv("LANG");
  114. }
  115. /*
  116. * The following routines are used to query a CDE database to determine
  117. * if the current character encoding requires special care in the ksh
  118. * parser. They are used in updateShSpecialParse(). These are copied
  119. * from the DtHelp code.
  120. */
  121. #include <XlationSvc.h>
  122. #include <LocaleXlate.h>
  123. static const char *DfltStdCharset = "ISO-8859-1";
  124. static const char *DfltStdLang = "C";
  125. static char MyPlatform[_DtPLATFORM_MAX_LEN+1];
  126. static int CompVer;
  127. /******************************************************************************
  128. * Function: static _DtXlateDb OpenLcxDb ()
  129. *
  130. * Parameters: none
  131. *
  132. * Return Value: NULL: error, else a _DtXlateDb
  133. *
  134. * errno Values:
  135. *
  136. * Purpose: Opens the Ce-private Lcx database
  137. *
  138. *****************************************************************************/
  139. static _DtXlateDb
  140. OpenLcxDb (void)
  141. {
  142. static _DtXlateDb MyDb;
  143. static Boolean MyFirst = True;
  144. static Boolean MyProcess = False;
  145. static int ExecVer;
  146. time_t time1 = 0;
  147. time_t time2 = 0;
  148. /*
  149. * wait up to 30 sec. until another thread or enter is done
  150. * modifying the table.
  151. */
  152. while (MyProcess == True)
  153. {
  154. /* if time out, return */
  155. if (time(&time2) == (time_t)-1)
  156. return (_DtXlateDb)NULL;
  157. if (time1 == 0)
  158. time1 = time2;
  159. else if (time2 - time1 >= (time_t)30)
  160. return (_DtXlateDb)NULL;
  161. }
  162. if (MyFirst == True)
  163. {
  164. MyProcess = True;
  165. if (_DtLcxOpenAllDbs(&MyDb) == 0 &&
  166. _DtXlateGetXlateEnv(MyDb,MyPlatform,&ExecVer,&CompVer) != 0)
  167. {
  168. _DtLcxCloseDb(&MyDb);
  169. MyDb = NULL;
  170. }
  171. MyFirst = False;
  172. MyProcess = False;
  173. }
  174. return MyDb;
  175. }
  176. /******************************************************************************
  177. * Function: static void XlateOpToStdLocale(char *operation, char *opLocale,
  178. * char **ret_stdLocale,
  179. * char **ret_stdLang,
  180. * char **ret_stdSet)
  181. *
  182. * Parameters:
  183. * operation Operation associated with the locale value
  184. * opLocale An operation-specific locale string
  185. * ret_locale Returns the std locale
  186. * Caller must free this string.
  187. * ret_stdLang Returns the std language & territory string.
  188. * Caller must free this string.
  189. * ret_stdSet Returns the std code set string.
  190. * Caller must free this string.
  191. *
  192. * Return Value:
  193. *
  194. * Purpose: Gets the standard locale given an operation and its locale
  195. *
  196. *****************************************************************************/
  197. static void
  198. XlateOpToStdLocale (
  199. char *operation,
  200. char *opLocale,
  201. char **ret_stdLocale,
  202. char **ret_stdLang,
  203. char **ret_stdSet)
  204. {
  205. _DtXlateDb MyDb;
  206. MyDb = OpenLcxDb();
  207. if (MyDb != NULL)
  208. {
  209. (void) _DtLcxXlateOpToStd(MyDb, MyPlatform, CompVer,
  210. operation,opLocale,
  211. ret_stdLocale, ret_stdLang, ret_stdSet, NULL);
  212. }
  213. /* if failed, give default values */
  214. if (ret_stdLocale != NULL && *ret_stdLocale == NULL)
  215. {
  216. *ret_stdLocale = malloc(strlen(DfltStdLang)+strlen(DfltStdCharset)+3);
  217. sprintf(*ret_stdLocale,"%s.%s",DfltStdLang,DfltStdCharset);
  218. }
  219. if (ret_stdLang != NULL && *ret_stdLang == NULL)
  220. *ret_stdLang = strdup(DfltStdLang);
  221. if (ret_stdSet != NULL && *ret_stdSet == NULL)
  222. *ret_stdSet = strdup(DfltStdCharset);
  223. }
  224. /******************************************************************************
  225. * Function: static void XlateStdToOpLocale(char *operation,
  226. * char *stdLocale, char *dflt_opLocale,
  227. * char **ret_opLocale)
  228. *
  229. * Parameters:
  230. * operation operation whose locale value will be retrieved
  231. * stdLocale standard locale value
  232. * dflt_opLocale operation-specific locale-value
  233. * This is the default value used in error case
  234. * ret_opLocale operation-specific locale-value placed here
  235. * Caller must free this string.
  236. *
  237. * Return Value:
  238. *
  239. * Purpose: Gets an operation-specific locale string given the standard string
  240. *
  241. *****************************************************************************/
  242. static void
  243. XlateStdToOpLocale (
  244. char *operation,
  245. char *stdLocale,
  246. char *dflt_opLocale,
  247. char **ret_opLocale)
  248. {
  249. _DtXlateDb MyDb;
  250. MyDb = OpenLcxDb();
  251. if (MyDb != NULL)
  252. (void) _DtLcxXlateStdToOp(MyDb, MyPlatform, CompVer,
  253. operation, stdLocale, NULL, NULL, NULL, ret_opLocale);
  254. }
  255. extern int shSpecialParse; /* in ksh93/src/cmd/ksh93/sh/lex.c */
  256. /*
  257. * updateShellSpecialParse uses the libXvh database to determine if the
  258. * current character encoding requires special care in the ksh parser.
  259. * It sets or clears a global flag (shSpecialParse) based on the value
  260. * from the database. This flag is declared and inspected in sh_lex() in
  261. * ksh93/src/cmd/ksh93/sh/lex.c. This routine is stubbed in the
  262. * file .../sh/userinit.c to allow ksh93 to compile & run, albeit
  263. * without any knowledge of when to do special parsing.
  264. */
  265. void
  266. updateShSpecialParse( void )
  267. {
  268. char *locale = (char *)NULL, *parseVal = (char *)NULL;
  269. int * lockedFds;
  270. lockedFds = LockKshFileDescriptors();
  271. XlateOpToStdLocale(DtLCX_OPER_SETLOCALE, setlocale(LC_CTYPE,NULL),
  272. &locale, NULL, NULL);
  273. XlateStdToOpLocale("dtkshSpecialParse", locale, NULL, &parseVal);
  274. XtFree(locale);
  275. UnlockKshFileDescriptors(lockedFds);
  276. if(parseVal != (char *)NULL)
  277. {
  278. shSpecialParse = 1;
  279. XtFree(parseVal);
  280. }
  281. else
  282. shSpecialParse = 0;
  283. }