-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
tclmain.cc
377 lines (332 loc) · 11.4 KB
/
tclmain.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
@copyright Russell Standish 2000
@author Russell Standish
This file is a modified version of a similarly named file in EcoLab.
It is released as public domain.
*/
#include <exception> // for exception
#include <fstream> // for std
#include <map> // for map
#include <string> // for string, operator<
#include <boost/filesystem.hpp>
#include <cairoSurfaceImage.h> // for CairoSurface
#include <ctype.h> // for isdigit
#include <pango.h> // for Pango, Pango::defaultFamily
#include <signal.h> // for SIGABRT, SIGBUS, SIGSEGV
#include <stdio.h> // for fprintf, stderr, fputs, puts
#include <stdlib.h> // for atoi, free, srand
#include <string.h> // for strcmp, strlen
#include <time.h> // for time
#include <tk.h> // for Tcl_GetStringResult, Tcl_...
#include <tkDecls.h> // for Tk_Init, Tk_MainWindow
#include "eco_hashmap.h" // for hash_map
#include "eco_strstream.h" // for eco_strstream
#include "ecolab.h" // for eco_string, classdesc
#include "error.h" // for error, ecolab
#include "tcl++.h" // for interp, tclvar, NEWCMD
#include "timer.h" // for print_timers, stop_timer
#include "version.h" // for VERSION
#include <ecolab_epilogue.h>
using boost::filesystem::path;
#ifndef SIG_DFL
#define SIG_DFL ((__sighandler_t) 0) /* Default action. */
#endif
#define SIGILL 4 /* Illegal instruction (ANSI). */
#ifndef SIGABRT
#define SIGABRT 6 /* Abort (ANSI). */
#endif
#ifndef SIGBUS
#define SIGBUS 7 /* BUS error (4.2 BSD). */
#endif
#ifndef SIGSEGV
#define SIGSEGV 11 /* Segmentation violation (ANSI). */
#endif
using namespace std;
using namespace ecolab;
using namespace classdesc;
#include "version.h"
NEWCMD(minsky_version,0)
{
tclreturn r;
r<<VERSION;
}
namespace ecolab
{
Tk_Window mainWin=0;
}
void printTimersAtEnd()
{
stop_timer("main");
print_timers();
}
#if defined(_WIN32)
// for GetConsoleWindow()
#define _WIN32_WINNT 0x0501
#include <windows.h>
#endif
int main(int argc, char* argv[])
{
const Timer timer("main");
// atexit(printTimersAtEnd);
#ifdef _WIN32
ecolab::Pango::defaultFamily="Arial Unicode MS";
#else
ecolab::Pango::defaultFamily="Adobe Courier Regular";
#endif
#if defined(NDEBUG) && defined(_WIN32)
ShowWindow(GetConsoleWindow(),SW_HIDE);
#endif
Tcl_FindExecutable(argv[0]);
int r;
#ifndef MXE
// For MXE builds, override tcl_library and tk_library to the self-contained versions
r=Tcl_Init(interp());
if (r!=TCL_OK)
#endif
// one possible reason is that it failed to locate the TCL library, we we set tcl_library and try again
{
const path exeDir=path(Tcl_GetNameOfExecutable()).parent_path();
tclvar tcl_library("tcl_library", (exeDir/"library"/"tcl").string().c_str());
tclvar tk_library("tk_library", (exeDir/"library"/"tk").string().c_str());
if ((r=Tcl_Init(interp()))!=TCL_OK)
{
// on Macs, the TCL library may be located in
// ../lib/minsky/library to get around codesign's
// pernicketyness
tcl_library=(exeDir/".."/"lib"/"minsky"/"library"/"tcl").string();
tk_library=(exeDir/".."/"lib"/"minsky"/"library"/"tcl").string();
r=Tcl_Init(interp());
}
}
if (r!=TCL_OK)
{
fprintf(stderr,"%s\n",Tcl_GetStringResult(interp()));
fprintf(stderr,"%s\n",Tcl_GetVar(interp(),"errorInfo",0)); /* print out trace */
return 1; /* not a clean execution */
}
/* set the TCL variables argc and argv to contain the
arguments. */
tclvar tcl_argc("argc"), tcl_argv("argv");
tcl_argc=argc;
for (int i=0; i<argc; i++) tcl_argv[i]=argv[i];
path minskydir=path(Tcl_GetNameOfExecutable()).parent_path();
// if Minsky's libraries are not located with the executable, look in the lib
// directory (unix style installation)
if (!exists(minskydir/"minsky.tcl"))
minskydir/="../lib/minsky";
if (!exists(minskydir/"minsky.tcl"))
{
fprintf(stderr,"Minsky library not found!\n");
return 1;
}
const tclvar minskyHome("minskyHome",minskydir.string().c_str());
srand(time(nullptr));
if (Tcl_EvalFile(interp(), (minskydir/"minsky.tcl").string().c_str())!=TCL_OK)
{
fprintf(stderr,"%s\n",Tcl_GetStringResult(interp()));
fprintf(stderr,"%s\n",Tcl_GetVar(interp(),"errorInfo",0)); /* print out trace */
return 1; /* not a clean execution */
}
while (mainWin) /* we are running GUI mode */
Tcl_DoOneEvent(0);
Tcl_DeleteInterp(interp());
Tcl_Finalize();
}
namespace minsky
{
// check and perform a GUI event
void doOneEvent(bool idletasksOnly)
{
Tcl_DoOneEvent(TCL_DONT_WAIT | (idletasksOnly? TCL_IDLE_EVENTS: 0));
}
}
// useful for debugging X11 errors
//int minskyXErrorHandler(Display *d, XErrorEvent* ev)
//{
// if (ev->error_code==BadWindow) return 0;
// char msg[1024];
// XGetErrorText(d,ev->error_code,msg,1024);
// puts(msg);
// // throw error("Xlib error %s",msg);
//}
NEWCMD(GUI,0)
{
if (Tk_Init(interp())==TCL_ERROR)
{
fprintf(stderr,"Error initialising Tk: %s",Tcl_GetStringResult(interp()));
fprintf(stderr,"%s\n",Tcl_GetVar(interp(),"errorInfo",0));
/* If Tk_Init fails, it is not necessarily a fatal error. For
example, on unpatched macs, we get an error from the attempt
to create a console, yet Minsky works just fine without
one. */
}
CairoSurface::registerImage();
mainWin = Tk_MainWindow(interp());
//XSetErrorHandler(minskyXErrorHandler);
}
//this seems to be needed to get Tkinit to function correctly!!
NEWCMD(tcl_findLibrary,-1) {}
NEWCMD(exit_ecolab,0)
{
mainWin=0;
}
namespace TCLcmd
{
namespace trap
{
eco_string sigcmd[32];
void sighand(int s) {Tcl_Eval(interp(),sigcmd[s].c_str());}
hash_map<eco_string,int> signum; /* signal name to number table */
struct init_t
{
init_t()
{
signum["HUP"]= 1; /* Hangup (POSIX). */
signum["INT"]= 2; /* Interrupt (ANSI). */
signum["QUIT"]= 3; /* Quit (POSIX). */
signum["ILL"]= 4; /* Illegal instruction (ANSI). */
signum["TRAP"]= 5; /* Trace trap (POSIX). */
signum["ABRT"]= 6; /* Abort (ANSI). */
signum["IOT"]= 6; /* IOT trap (4.2 BSD). */
signum["BUS"]= 7; /* BUS error (4.2 BSD). */
signum["FPE"]= 8; /* Floating-point exception (ANSI). */
signum["KILL"]= 9; /* Kill, unblockable (POSIX). */
signum["USR1"]= 10; /* User-defined signal 1 (POSIX). */
signum["SEGV"]= 11; /* Segmentation violation (ANSI). */
signum["USR2"]= 12; /* User-defined signal 2 (POSIX). */
signum["PIPE"]= 13; /* Broken pipe (POSIX). */
signum["ALRM"]= 14; /* Alarm clock (POSIX). */
signum["TERM"]= 15; /* Termination (ANSI). */
signum["STKFLT"]= 16; /* Stack fault. */
signum["CLD"]= 17; /* Same as SIGCHLD (System V). */
signum["CHLD"]= 17; /* Child status has changed (POSIX). */
signum["CONT"]= 18; /* Continue (POSIX). */
signum["STOP"]= 19; /* Stop, unblockable (POSIX). */
signum["TSTP"]= 20; /* Keyboard stop (POSIX). */
signum["TTIN"]= 21; /* Background read from tty (POSIX). */
signum["TTOU"]= 22; /* Background write to tty (POSIX). */
signum["URG"]= 23; /* Urgent condition on socket (4.2 BSD). */
signum["XCPU"]= 24; /* CPU limit exceeded (4.2 BSD). */
signum["XFSZ"]= 25; /* File size limit exceeded (4.2 BSD). */
signum["VTALRM"]= 26; /* Virtual alarm clock (4.2 BSD). */
signum["PROF"]= 27; /* Profiling alarm clock (4.2 BSD). */
signum["WINCH"]= 28; /* Window size change (4.3 BSD, Sun). */
signum["POLL"]= 29;/* Pollable event occurred (System V). */
signum["IO"]= 29; /* I/O now possible (4.2 BSD). */
signum["PWR"]= 30; /* Power failure restart (System V). */
signum["SYS"]= 31; /* Bad system call. */
signum["UNUSED"]= 31;
}
} init;
NEWCMD(trap,2) /* trap argv[2] to excute argv[1] */
{
const int signo = (isdigit(argv[1][0]))? atoi(argv[1]):
signum[const_cast<char*>(argv[1])];
sigcmd[signo]=argv[2];
signal(signo,sighand);
}
void aborthand(int) {throw error("Fatal Error: Execution recovered");}
NEWCMD(trapabort,-1)
{
void (*hand)(int);
if (argc>1 && strcmp(argv[1],"off")==0) hand=SIG_DFL;
else hand=aborthand;
signal(SIGABRT,hand);
signal(SIGSEGV,hand);
signal(SIGBUS,hand);
signal(SIGILL,hand);
}
}
#ifdef READLINE
extern "C" char *readline(char *);
extern "C" void add_history(char *);
#endif
NEWCMD(cli,0)
{
int braces=0, brackets=0;
bool inString=false;
tclcmd cmd;
cmd << "savebgerror\n";
#ifdef READLINE
char *c;
eco_string prompt((const char*)tclvar("argv(0)")); prompt+='>';
while ( (c=readline(const_cast<char*>(prompt.c_str())))!=NULL && strcmp(c,"exit")!=0)
#else
char c[512];
while (fgets(c,512,stdin)!=NULL && strcmp(c,"exit\n")!=0)
#endif
{
// count up number of braces, brackets etc
for (char* cc=c; *cc!='\0'; ++cc)
switch(*cc)
{
case '{':
if (!inString) braces++;
break;
case '[':
if (!inString) brackets++;
break;
case '}':
if (!inString) braces--;
break;
case ']':
if (!inString) brackets--;
break;
case '\\':
if (inString) cc++; //skip next character (is literal)
break;
case '"':
inString = !inString;
break;
}
cmd << chomp(c);
if (!inString && braces<=0 && brackets <=0)
{ // we have a complete command, so attempt to execute it
try
{
cmd.exec();
}
catch (std::exception& e) {fputs(e.what(),stderr);}
catch (...) {fputs("caught unknown exception",stderr);}
puts(cmd.result.c_str());
}
#ifdef READLINE
if (strlen(c)) add_history(c);
free(c);
#endif
}
cmd << "restorebgerror\n";
}
#ifdef _WIN32
#include <windef.h>
#include <winbase.h>
#include <shellapi.h>
NEWCMD(shellOpen,1)
{
ShellExecute(NULL,"open",argv[1],NULL,NULL,1);
}
#endif
// support double-click opening of files on Macintosh, by adding a
// handler for the open event
int tk_mac_OpenDocument(ClientData,Tcl_Interp*,int argc,const char** argv)
{
if (argc>1)
{
tclvar("argv")[1]=argv[1];
tclvar("argc")=2;
}
return TCL_OK;
}
static int tk_mac_OpenDocument_dummy=
(Tcl_CreateCommand
(interp(),"::tk::mac::OpenDocument",(Tcl_CmdProc*)tk_mac_OpenDocument,0,NULL),
0);
}
// In order to get around a missing mkostemp function on MacOSX 10.11 and earlier systems
#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101200
extern "C" int mkostemp(char* templ, int flags)
{
mktemp(templ);
return open(templ,flags);
}
#endif