1 | /*
|
---|
2 | * tclAppInit.c --
|
---|
3 | *
|
---|
4 | * Provides a default version of the main program and Tcl_AppInit
|
---|
5 | * procedure for Tcl applications (without Tk). Note that this
|
---|
6 | * program must be built in Win32 console mode to work properly.
|
---|
7 | *
|
---|
8 | * Copyright (c) 1996-1997 by Sun Microsystems, Inc.
|
---|
9 | * Copyright (c) 1998-1999 by Scriptics Corporation.
|
---|
10 | * Copyright (c) 2000-2006 Jean-Claude Wippler <jcw@equi4.com>
|
---|
11 | * Copyright (c) 2003-2006 ActiveState Software Inc.
|
---|
12 | *
|
---|
13 | * See the file "license.terms" for information on usage and redistribution
|
---|
14 | * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
---|
15 | *
|
---|
16 | * RCS: @(#) $Id: kitInit.c,v 1.3 2008/05/11 20:36:50 demin Exp $
|
---|
17 | */
|
---|
18 |
|
---|
19 | #ifdef KIT_INCLUDES_TK
|
---|
20 | #include <tk.h>
|
---|
21 | #else
|
---|
22 | #include <tcl.h>
|
---|
23 | #endif
|
---|
24 |
|
---|
25 | #include <string.h>
|
---|
26 |
|
---|
27 | #ifdef _WIN32
|
---|
28 | #define WIN32_LEAN_AND_MEAN
|
---|
29 | #include <windows.h>
|
---|
30 | #undef WIN32_LEAN_AND_MEAN
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | /* defined in tclInt.h */
|
---|
34 | extern Tcl_Obj* TclGetStartupScriptPath();
|
---|
35 | extern void TclSetStartupScriptPath(Tcl_Obj*);
|
---|
36 |
|
---|
37 | Tcl_AppInitProc Vfs_Init, Zlib_Init;
|
---|
38 | #ifdef TCL_THREADS
|
---|
39 | Tcl_AppInitProc Thread_Init;
|
---|
40 | #endif
|
---|
41 | #ifdef _WIN32
|
---|
42 | Tcl_AppInitProc Dde_Init, Registry_Init;
|
---|
43 | #endif
|
---|
44 | #ifdef KIT_INCLUDES_TK
|
---|
45 | Tcl_AppInitProc Blt_Init, Blt_SafeInit;
|
---|
46 | Tcl_AppInitProc Tktable_Init, Tktable_SafeInit;
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | Tcl_AppInitProc Tdom_Init, Tdom_SafeInit;
|
---|
50 | Tcl_AppInitProc G2lite_Init;
|
---|
51 | Tcl_AppInitProc Usb_Init;
|
---|
52 | Tcl_AppInitProc Swt_Init;
|
---|
53 | Tcl_AppInitProc Csr_Init;
|
---|
54 | Tcl_AppInitProc Xotcl_Init;
|
---|
55 | Tcl_AppInitProc Sqlite3_Init;
|
---|
56 |
|
---|
57 | #ifdef WIN32
|
---|
58 | #define DEV_NULL "NUL"
|
---|
59 | #else
|
---|
60 | #define DEV_NULL "/dev/null"
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | static void TclKit_InitStdChannels(void);
|
---|
64 |
|
---|
65 | static char appInitCmd[] =
|
---|
66 | "proc tclKitInit {} {\n"
|
---|
67 | "rename tclKitInit {}\n"
|
---|
68 | "if {![info exists ::tcl::basekit]} {\n"
|
---|
69 | "namespace eval ::tcl { variable basekit [info nameofexecutable] }\n"
|
---|
70 | "}\n"
|
---|
71 | "if {[file isfile /zvfs/boot.tcl]} {\n"
|
---|
72 | "source /zvfs/boot.tcl\n"
|
---|
73 | "} elseif {[lindex $::argv 0] eq \"-init-\"} {\n"
|
---|
74 | "uplevel #0 { source [lindex $::argv 1] }\n"
|
---|
75 | "exit\n"
|
---|
76 | "} else {\n"
|
---|
77 | "error \"\n $::tcl::basekit has no VFS data to start up\"\n"
|
---|
78 | "}\n"
|
---|
79 | "}\n"
|
---|
80 | "tclKitInit"
|
---|
81 | ;
|
---|
82 |
|
---|
83 | static const char initScript[] =
|
---|
84 | "if {[file isfile [file join /zvfs main.tcl]]} {\n"
|
---|
85 | "if {[info commands console] != {}} { console hide }\n"
|
---|
86 | "set tcl_interactive 0\n"
|
---|
87 | "incr argc\n"
|
---|
88 | "set argv [linsert $argv 0 $argv0]\n"
|
---|
89 | "set argv0 [file join /zvfs main.tcl]\n"
|
---|
90 | "} else continue\n"
|
---|
91 | ;
|
---|
92 |
|
---|
93 | #ifdef WIN32
|
---|
94 | __declspec(dllexport) int
|
---|
95 | #else
|
---|
96 | extern int
|
---|
97 | #endif
|
---|
98 | TclKit_AppInit(Tcl_Interp *interp)
|
---|
99 | {
|
---|
100 | /*
|
---|
101 | * Ensure that std channels exist (creating them if necessary)
|
---|
102 | */
|
---|
103 | TclKit_InitStdChannels();
|
---|
104 |
|
---|
105 | Tcl_StaticPackage(0, "vfs", Vfs_Init, NULL);
|
---|
106 | Tcl_StaticPackage(0, "zlib", Zlib_Init, NULL);
|
---|
107 | #ifdef TCL_THREADS
|
---|
108 | Tcl_StaticPackage(0, "Thread", Thread_Init, NULL);
|
---|
109 | #endif
|
---|
110 | #ifdef _WIN32
|
---|
111 | Tcl_StaticPackage(0, "dde", Dde_Init, NULL);
|
---|
112 | Tcl_StaticPackage(0, "registry", Registry_Init, NULL);
|
---|
113 | #endif
|
---|
114 | #ifdef KIT_INCLUDES_TK
|
---|
115 | Tcl_StaticPackage(0, "Tk", Tk_Init, Tk_SafeInit);
|
---|
116 | Tcl_StaticPackage(0, "Blt", Blt_Init, Blt_SafeInit);
|
---|
117 | Tcl_StaticPackage(0, "Tktable", Tktable_Init, Tktable_SafeInit);
|
---|
118 | #endif
|
---|
119 |
|
---|
120 | Tcl_StaticPackage(0, "XOTcl", Xotcl_Init, NULL);
|
---|
121 | Tcl_StaticPackage(0, "g2lite", G2lite_Init, NULL);
|
---|
122 | Tcl_StaticPackage(0, "usb", Usb_Init, NULL);
|
---|
123 | Tcl_StaticPackage(0, "swt", Swt_Init, NULL);
|
---|
124 | Tcl_StaticPackage(0, "csr", Csr_Init, NULL);
|
---|
125 | Tcl_StaticPackage(0, "sqlite3", Sqlite3_Init, NULL);
|
---|
126 | Tcl_StaticPackage(0, "tdom", Tdom_Init, Tdom_SafeInit);
|
---|
127 |
|
---|
128 | /* the tcl_rcFileName variable only exists in the initial interpreter */
|
---|
129 | #ifdef _WIN32
|
---|
130 | Tcl_SetVar(interp, "tcl_rcFileName", "~/tclkitrc.tcl", TCL_GLOBAL_ONLY);
|
---|
131 | #else
|
---|
132 | Tcl_SetVar(interp, "tcl_rcFileName", "~/.tclkitrc", TCL_GLOBAL_ONLY);
|
---|
133 | #endif
|
---|
134 |
|
---|
135 | Zvfs_Init(interp);
|
---|
136 | Tcl_SetVar(interp, "extname", "", TCL_GLOBAL_ONLY);
|
---|
137 | Zvfs_Mount(interp, (char *)Tcl_GetNameOfExecutable(), "/zvfs");
|
---|
138 | Tcl_SetVar2(interp, "env", "TCL_LIBRARY", "/zvfs/lib/tcl", TCL_GLOBAL_ONLY);
|
---|
139 | Tcl_SetVar2(interp, "env", "TK_LIBRARY", "/zvfs/lib/tk", TCL_GLOBAL_ONLY);
|
---|
140 |
|
---|
141 | if ((Tcl_EvalEx(interp, appInitCmd, -1, TCL_EVAL_GLOBAL) == TCL_ERROR)
|
---|
142 | || (Tcl_Init(interp) == TCL_ERROR))
|
---|
143 | goto error;
|
---|
144 |
|
---|
145 | #ifdef KIT_INCLUDES_TK
|
---|
146 | if (Tk_Init(interp) == TCL_ERROR)
|
---|
147 | goto error;
|
---|
148 | #ifdef _WIN32
|
---|
149 | if (Tk_CreateConsoleWindow(interp) == TCL_ERROR)
|
---|
150 | goto error;
|
---|
151 | #endif
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | /* messy because TclSetStartupScriptPath is called slightly too late */
|
---|
155 | if (Tcl_Eval(interp, initScript) == TCL_OK) {
|
---|
156 | Tcl_Obj* path = TclGetStartupScriptPath();
|
---|
157 | TclSetStartupScriptPath(Tcl_GetObjResult(interp));
|
---|
158 | if (path == NULL)
|
---|
159 | Tcl_Eval(interp, "incr argc -1; set argv [lrange $argv 1 end]");
|
---|
160 | }
|
---|
161 |
|
---|
162 | Tcl_SetVar(interp, "errorInfo", "", TCL_GLOBAL_ONLY);
|
---|
163 | Tcl_ResetResult(interp);
|
---|
164 | return TCL_OK;
|
---|
165 |
|
---|
166 | error:
|
---|
167 | #if defined(KIT_INCLUDES_TK) && defined(_WIN32)
|
---|
168 | MessageBeep(MB_ICONEXCLAMATION);
|
---|
169 | MessageBox(NULL, Tcl_GetStringResult(interp), "Error in Tclkit",
|
---|
170 | MB_ICONSTOP | MB_OK | MB_TASKMODAL | MB_SETFOREGROUND);
|
---|
171 | ExitProcess(1);
|
---|
172 | /* we won't reach this, but we need the return */
|
---|
173 | #endif
|
---|
174 | return TCL_ERROR;
|
---|
175 | }
|
---|
176 |
|
---|
177 | static void
|
---|
178 | TclKit_InitStdChannels(void)
|
---|
179 | {
|
---|
180 | Tcl_Channel chan;
|
---|
181 |
|
---|
182 | /*
|
---|
183 | * We need to verify if we have the standard channels and create them if
|
---|
184 | * not. Otherwise internals channels may get used as standard channels
|
---|
185 | * (like for encodings) and panic.
|
---|
186 | */
|
---|
187 | chan = Tcl_GetStdChannel(TCL_STDIN);
|
---|
188 | if (chan == NULL) {
|
---|
189 | chan = Tcl_OpenFileChannel(NULL, DEV_NULL, "r", 0);
|
---|
190 | if (chan != NULL) {
|
---|
191 | Tcl_SetChannelOption(NULL, chan, "-encoding", "utf-8");
|
---|
192 | }
|
---|
193 | Tcl_SetStdChannel(chan, TCL_STDIN);
|
---|
194 | }
|
---|
195 | chan = Tcl_GetStdChannel(TCL_STDOUT);
|
---|
196 | if (chan == NULL) {
|
---|
197 | chan = Tcl_OpenFileChannel(NULL, DEV_NULL, "w", 0);
|
---|
198 | if (chan != NULL) {
|
---|
199 | Tcl_SetChannelOption(NULL, chan, "-encoding", "utf-8");
|
---|
200 | }
|
---|
201 | Tcl_SetStdChannel(chan, TCL_STDOUT);
|
---|
202 | }
|
---|
203 | chan = Tcl_GetStdChannel(TCL_STDERR);
|
---|
204 | if (chan == NULL) {
|
---|
205 | chan = Tcl_OpenFileChannel(NULL, DEV_NULL, "w", 0);
|
---|
206 | if (chan != NULL) {
|
---|
207 | Tcl_SetChannelOption(NULL, chan, "-encoding", "utf-8");
|
---|
208 | }
|
---|
209 | Tcl_SetStdChannel(chan, TCL_STDERR);
|
---|
210 | }
|
---|
211 | }
|
---|