tkAppInit.cc
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:10k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /* 
  2.  * tkAppInit.c --
  3.  *
  4.  * Provides a default version of the main program and Tcl_AppInit
  5.  * procedure for Tcl applications (without Tk).
  6.  *
  7.  * Copyright (C) 2000 USC/ISI
  8.  * Copyright (c) 1993 The Regents of the University of California.
  9.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  10.  *
  11.  * See the file "license.terms" for information on usage and redistribution
  12.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13.  *
  14.  * SCCS: @(#) tkAppInit.c 1.21 96/03/26 16:47:07
  15.  *
  16.  *  pmsrve 29/June/2005 - Ported a new tkAppinit.cc for NS2, based from: 
  17.  *      ns-2.28's common/tclAppinit.cc (....)
  18.  *     tk8.4.5's wish.c (v 1.7 2002/06/21 20:24:29 dgp Exp)
  19.  *
  20.  */
  21. #include <tk.h>
  22. #include "locale.h"
  23. #include "config.h"
  24. extern void init_misc(void);
  25. extern EmbeddedTcl et_ns_lib;
  26. extern EmbeddedTcl et_ns_ptypes;
  27. extern EmbeddedTcl et_tk;
  28. /* MSVC requires this global var declaration to be outside of 'extern "C"' */
  29. #ifdef MEMDEBUG_SIMULATIONS
  30. #include "mem-trace.h"
  31. MemTrace *globalMemTrace;
  32. #endif
  33. #define NS_BEGIN_EXTERN_C extern "C" {
  34. #define NS_END_EXTERN_C }
  35. NS_BEGIN_EXTERN_C
  36. #ifdef HAVE_FENV_H
  37. #include <fenv.h>
  38. #endif /* HAVE_FENV_H */
  39. /*
  40.  * The following variable is a special hack that is needed in order for
  41.  * Sun shared libraries to be used for Tcl.
  42.  */
  43. #ifdef TK_TEST
  44. extern int Tktest_Init _ANSI_ARGS_((Tcl_Interp *interp));
  45. #endif /* TK_TEST */
  46. #include "bitmap/play.xbm"
  47. #include "bitmap/stop.xbm"
  48. #include "bitmap/rewind.xbm"
  49. #include "bitmap/ff.xbm"
  50. void loadbitmaps(Tcl_Interp* tcl)
  51. {
  52. Tk_DefineBitmap(tcl, Tk_GetUid("play"),
  53. play_bits, play_width, play_height);
  54. Tk_DefineBitmap(tcl, Tk_GetUid("stop"),
  55. stop_bits, stop_width, stop_height);
  56. Tk_DefineBitmap(tcl, Tk_GetUid("rewind"),
  57. rewind_bits, rewind_width, rewind_height);
  58. Tk_DefineBitmap(tcl, Tk_GetUid("ff"),
  59. ff_bits, ff_width, ff_height);
  60. }
  61. /*
  62.  *----------------------------------------------------------------------
  63.  *
  64.  * main --
  65.  *
  66.  * This is the main program for the application.
  67.  *
  68.  * Results:
  69.  * None: Tk_Main never returns here, so this procedure never
  70.  * returns either.
  71.  *
  72.  * Side effects:
  73.  * Whatever the application does.
  74.  *
  75.  *----------------------------------------------------------------------
  76.  */
  77. int
  78. main(int argc, char **argv)
  79. {
  80.     /*
  81.      * The following #if block allows you to change the AppInit
  82.      * function by using a #define of TCL_LOCAL_APPINIT instead
  83.      * of rewriting this entire file.  The #if checks for that
  84.      * #define and uses Tcl_AppInit if it doesn't exist.
  85.      */
  86.     
  87. #ifndef TK_LOCAL_APPINIT
  88. #define TK_LOCAL_APPINIT Tcl_AppInit    
  89. #endif
  90.     extern int TK_LOCAL_APPINIT _ANSI_ARGS_((Tcl_Interp *interp));
  91.     
  92.     /*
  93.      * The following #if block allows you to change how Tcl finds the startup
  94.      * script, prime the library or encoding paths, fiddle with the argv,
  95.      * etc., without needing to rewrite Tk_Main()
  96.      */
  97.     
  98. #ifdef TK_LOCAL_MAIN_HOOK
  99.     extern int TK_LOCAL_MAIN_HOOK _ANSI_ARGS_((int *argc, char ***argv));
  100.     TK_LOCAL_MAIN_HOOK(&argc, &argv);
  101. #endif
  102.     Tk_Main(argc, argv, TK_LOCAL_APPINIT);
  103.     return 0; /* Needed only to prevent compiler warning. */
  104. }
  105. #if defined(__i386__) && defined(__GNUC__)
  106. #define HAVE_NS_SETUP_FPU /* convenience flag to check on later */
  107. /* This function is supposed to set up a uniform FPU state on all i386
  108.  * platforms.  It may (should) be called instead of functions in the
  109.  * fe- family.
  110.  */
  111. static void ns_setup_fpu() {
  112. static const int NS_FPU_CW_IC  = 0x1000; /* Infty control(12): support +/- infinity */
  113. static const int NS_FPU_CW_RC  = 0x0000; /* Round control(11,10): to nearest */
  114. static const int NS_FPU_CW_PC  = 0x0200; /* Precision control(9,8): 53 bits */
  115. static const int NS_FPU_CW_IEM = 0x0000; /* Interrupt enable mask(7): enabled */
  116. static const int NS_FPU_CW_B6  = 0x0040; /* undefined, set to one in my FPU */
  117. static const int NS_FPU_CW_PM  = 0x0020; /* Precision mask(5), inexact exception: disabled */
  118. static const int NS_FPU_CW_UM  = 0x0010; /* Underflow mask(4): disabled */
  119. static const int NS_FPU_CW_OM  = 0x0000; /* Overflow mask(3): enabled */
  120. static const int NS_FPU_CW_ZM  = 0x0000; /* Zero divide mask(2): enabled */
  121. static const int NS_FPU_CW_DM  = 0x0002; /* Denormalized operand(1): disabled */
  122. static const int NS_FPU_CW_IM  = 0x0000; /* Invalid operation mask(0): enabled */
  123. static const int NS_FPU_CW    = NS_FPU_CW_IC
  124. | NS_FPU_CW_RC
  125. | NS_FPU_CW_PC  
  126. | NS_FPU_CW_IEM 
  127. | NS_FPU_CW_B6  
  128. | NS_FPU_CW_PM  
  129. | NS_FPU_CW_UM  
  130. | NS_FPU_CW_OM  
  131. | NS_FPU_CW_ZM
  132. | NS_FPU_CW_DM  
  133. | NS_FPU_CW_IM;
  134. unsigned short _cw = NS_FPU_CW;
  135. asm ("fldcw %0" : : "m" (*&_cw));
  136. }
  137. #endif /* !HAVE_NS_SETUP_FPU && __i386__ && __GNUC__ */
  138. #if !defined(HAVE_FESETPRECISION) && defined(__i386__) && defined(__GNUC__)
  139. // use our own!
  140. #define HAVE_FESETPRECISION
  141. /*
  142.  * From:
  143.  |  Floating-point environment <fenvwm.h>                                    |
  144.  | Copyright (C) 1996, 1997, 1998, 1999                                      |
  145.  |                     W. Metzenthen, 22 Parker St, Ormond, Vic 3163,        |
  146.  |                     Australia.                                            |
  147.  |                     E-mail   billm@melbpc.org.au                          |
  148.  * used here with permission.
  149.  */
  150. #define FE_FLTPREC       0x000
  151. #define FE_INVALIDPREC   0x100
  152. #define FE_DBLPREC       0x200
  153. #define FE_LDBLPREC      0x300
  154. /*
  155.  * From:
  156.  * fenvwm.c
  157.  | Copyright (C) 1999                                                        |
  158.  |                     W. Metzenthen, 22 Parker St, Ormond, Vic 3163,        |
  159.  |                     Australia.  E-mail   billm@melbpc.org.au              |
  160.  * used here with permission.
  161.  */
  162. /*
  163.   Set the precision to prec if it is a valid
  164.   floating point precision macro.
  165.   Returns 1 if precision set, 0 otherwise.
  166.   */
  167. static inline int fesetprecision(int prec)
  168. {
  169.   if ( !(prec & ~FE_LDBLPREC) && (prec != FE_INVALIDPREC) )
  170.     {
  171.       unsigned short cw;
  172.       asm ("fnstcw %0":"=m" (*&cw));
  173.       asm ("fwait");
  174.       cw = (cw & ~FE_LDBLPREC) | (prec & FE_LDBLPREC);
  175.       asm volatile ("fldcw %0" : /* Don't push these colons together */ : "m" (*&cw));
  176.       return 1;
  177.     }
  178.   else
  179.     return 0;
  180. }
  181. #endif /* !HAVE_FESETPRECISION && __i386__ && __GNUC__ */
  182. /*
  183.  * setup_floating_point_environment
  184.  *
  185.  * Set up the floating point environment to be as standard as possible.
  186.  *
  187.  * For example:
  188.  * Linux i386 uses 60-bit floats for calculation,
  189.  * not 56-bit floats, giving different results.
  190.  * Fix that.
  191.  *
  192.  * See <http://www.linuxsupportline.com/~billm/faq.html>
  193.  * for why we do this fix.
  194.  *
  195.  * This function is derived from wmexcep
  196.  *
  197.  */
  198. static inline void
  199. setup_floating_point_environment()
  200. {
  201. #ifdef HAVE_NS_SETUP_FPU
  202. ns_setup_fpu();
  203. #else /* !HAVE_NS_SETUP_FPU */
  204. // In general, try to use the C99 standards to set things up.
  205. // If we can't do that, do nothing and hope the default is right.
  206. #ifdef HAVE_FESETPRECISION
  207. fesetprecision(FE_DBLPREC);
  208. #endif
  209. #ifdef HAVE_FEENABLEEXCEPT
  210. /*
  211.  * In general we'd like to catch some serious exceptions (div by zero)
  212.  * and ignore the boring ones (overflow/underflow).
  213.  * We set up that up here.
  214.  * This depends on feenableexcept which is (currently) GNU
  215.  * specific.
  216.  */
  217. int trap_exceptions = 0;
  218. #ifdef FE_DIVBYZERO
  219. trap_exceptions |= FE_DIVBYZERO;
  220. #endif
  221. #ifdef FE_INVALID
  222. trap_exceptions |= FE_INVALID;
  223. #endif
  224. #ifdef FE_OVERFLOW
  225. trap_exceptions |= FE_OVERFLOW;
  226. #endif
  227. //#ifdef FE_UNDERFLOW
  228. // trap_exceptions |= FE_UNDERFLOW;
  229. //#endif
  230. feenableexcept(trap_exceptions);
  231. #endif /* HAVE_FEENABLEEXCEPT */
  232. #endif /* !HAVE_NS_SETUP_FPU */
  233. }
  234. /*
  235.  *----------------------------------------------------------------------
  236.  *
  237.  * Tcl_AppInit --
  238.  *
  239.  * This procedure performs application-specific initialization.
  240.  * Most applications, especially those that incorporate additional
  241.  * packages, will have their own version of this procedure.
  242.  *
  243.  * Results:
  244.  * Returns a standard Tcl completion code, and leaves an error
  245.  * message in interp->result if an error occurs.
  246.  *
  247.  * Side effects:
  248.  * Depends on the startup script.
  249.  *
  250.  *----------------------------------------------------------------------
  251.  */
  252. int
  253. Tcl_AppInit(Tcl_Interp *interp)
  254. {
  255. #ifdef MEMDEBUG_SIMULATIONS
  256.         extern MemTrace *globalMemTrace;
  257.         globalMemTrace = new MemTrace;
  258. #endif
  259. setup_floating_point_environment();
  260.        
  261. if (Tcl_Init(interp) == TCL_ERROR ||
  262.     Otcl_Init(interp) == TCL_ERROR)
  263. return TCL_ERROR;
  264.     if (Tk_Init(interp) == TCL_ERROR) {
  265. return TCL_ERROR;
  266.     }
  267.     Tcl_StaticPackage(interp, "Tk", Tk_Init, Tk_SafeInit);
  268. #ifdef TK_TEST
  269.     if (Tktest_Init(interp) == TCL_ERROR) {
  270. return TCL_ERROR;
  271.     }
  272.     Tcl_StaticPackage(interp, "Tktest", Tktest_Init,
  273.             (Tcl_PackageInitProc *) NULL);
  274. #endif /* TK_TEST */
  275.     /*
  276.      * Call the init procedures for included packages.  Each call should
  277.      * look like this:
  278.      *
  279.      * if (Mod_Init(interp) == TCL_ERROR) {
  280.      *     return TCL_ERROR;
  281.      * }
  282.      *
  283.      * where "Mod" is the name of the module.
  284.      */
  285. #ifdef HAVE_LIBTCLDBG
  286. extern int Tcldbg_Init(Tcl_Interp *);   // hackorama
  287. if (Tcldbg_Init(interp) == TCL_ERROR) {
  288. return TCL_ERROR;
  289. }
  290. #endif
  291. // copied from ns2.28's tclAppinit.cc
  292. Tcl_SetVar(interp, "tcl_rcFileName", "~/.ns.tcl", TCL_GLOBAL_ONLY);
  293. Tcl::init(interp, "ns");
  294. init_misc();
  295.     et_ns_ptypes.load();
  296. et_ns_lib.load();
  297. // copied from ns2.28's old tkAppinit.cc
  298. // it seems that using tk8.4.5 this line isnt' needed anymore. enabling it produces an error.
  299. // HOWEVER, do enable it if using tk8.3 and below!
  300. // et_tk.load();                  //      <<<<-------- this line
  301.     Tcl::instance().tkmain(Tk_MainWindow(interp));
  302.     loadbitmaps(interp);
  303. #ifdef TK_TEST
  304. if (Tcltest_Init(interp) == TCL_ERROR) {
  305. return TCL_ERROR;
  306. }
  307. Tcl_StaticPackage(interp, "Tcltest", Tcltest_Init,
  308.   (Tcl_PackageInitProc *) NULL);
  309. #endif /* TK_TEST */
  310. return TCL_OK;
  311. }
  312. #ifndef WIN32
  313. void
  314. abort()
  315. {
  316. Tcl& tcl = Tcl::instance();
  317. tcl.evalc("[Simulator instance] flush-trace");
  318. #ifdef abort
  319. #undef abort
  320. abort();
  321. #else
  322. exit(1);
  323. #endif /*abort*/
  324. /*NOTREACHED*/
  325. }
  326. #endif
  327. NS_END_EXTERN_C