-
Notifications
You must be signed in to change notification settings - Fork 1
/
ckMain.c
324 lines (287 loc) · 8.44 KB
/
ckMain.c
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
/*
* ckMain.c --
*
* This file contains a generic main program for Ck-based applications.
* It can be used as-is for many applications, just by supplying a
* different appInitProc procedure for each specific application.
* Or, it can be used as a template for creating new main programs
* for applications.
*
* Copyright (c) 1990-1994 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
* Copyright (c) 1995 Christian Werner
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "ckPort.h"
#include "ck.h"
/*
* Global variables used by the main program:
*/
static Tcl_Interp *interp; /* Interpreter for this application. */
static char *fileName = NULL; /* Script to source, if any. */
#ifdef TCL_MEM_DEBUG
static char dumpFile[100]; /* Records where to dump memory allocation
* information. */
static int quitFlag = 0; /* 1 means the "checkmem" command was
* invoked, so the application should quit
* and dump memory allocation information. */
static int CheckmemCmd _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int argc, char *argv[]));
#endif
/*
*----------------------------------------------------------------------
*
* Ck_Main --
*
* Main program for curses wish.
*
* Results:
* None. This procedure never returns (it exits the process when
* it's done.
*
* Side effects:
* This procedure initializes the toolkit and then starts
* interpreting commands; almost anything could happen, depending
* on the script being interpreted.
*
*----------------------------------------------------------------------
*/
void
Ck_Main(argc, argv, appInitProc)
int argc; /* Number of arguments. */
char **argv; /* Array of argument strings. */
int (*appInitProc)(); /* Application-specific initialization
* procedure to call after most
* initialization but before starting
* to execute commands. */
{
char *args, *msg, *argv0;
char buf[20];
int code;
#if !((TCL_MAJOR_VERSION == 7) && (TCL_MINOR_VERSION <= 4))
Tcl_Channel errChannel;
#endif
#if !((TCL_MAJOR_VERSION == 7) && (TCL_MINOR_VERSION <= 4))
Tcl_FindExecutable(argv[0]);
#endif
interp = Tcl_CreateInterp();
#ifndef __WIN32__
if (!isatty(0) || !isatty(1)) {
#if (TCL_MAJOR_VERSION == 7) && (TCL_MINOR_VERSION <= 4)
fprintf(stderr, "standard input/output must be terminal\n");
#else
errChannel = Tcl_GetStdChannel(TCL_STDERR);
if (errChannel)
Tcl_Write(errChannel,
"standard input/output must be terminal\n", -1);
#endif
Tcl_Eval(interp, "exit 1");
#if (TCL_MAJOR_VERSION >= 8)
Tcl_Exit(1);
#else
exit(1); /* Just in case */
#endif
}
#endif
#ifdef TCL_MEM_DEBUG
Tcl_InitMemory(interp);
Tcl_InitMemory(interp);
Tcl_CreateCommand(interp, "checkmem", CheckmemCmd, (ClientData) 0,
(Tcl_CmdDeleteProc *) NULL);
#endif
/*
* Parse command-line arguments. Argv[1] must contain the name
* of the script file to process.
*/
argv0 = argv[0];
if (argc > 1) {
fileName = argv[1];
argc--;
argv++;
}
/*
* Make command-line arguments available in the Tcl variables "argc"
* and "argv".
*/
args = Tcl_Merge(argc-1, argv+1);
Tcl_SetVar(interp, "argv", args, TCL_GLOBAL_ONLY);
ckfree(args);
sprintf(buf, "%d", argc-1);
Tcl_SetVar(interp, "argc", buf, TCL_GLOBAL_ONLY);
Tcl_SetVar(interp, "argv0", (fileName != NULL) ? fileName : argv0,
TCL_GLOBAL_ONLY);
Tcl_SetVar(interp, "tcl_interactive", (fileName == NULL) ? "1" : "0",
TCL_GLOBAL_ONLY);
/*
* Invoke application-specific initialization.
*/
if ((*appInitProc)(interp) != TCL_OK) {
#if (TCL_MAJOR_VERSION == 7) && (TCL_MINOR_VERSION <= 4)
fprintf(stderr, "application-specific initialization failed: %s\n",
interp->result);
#else
errChannel = Tcl_GetStdChannel(TCL_STDERR);
if (errChannel) {
Tcl_Write(errChannel,
"application-specific initialization failed: ", -1);
Tcl_Write(errChannel, Tcl_GetStringResult(interp), -1);
Tcl_Write(errChannel, "\n", 1);
}
#endif
msg = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
goto errorExit;
}
/*
* Invoke the script specified on the command line, if any.
*/
if (fileName != NULL) {
code = Tcl_VarEval(interp, "source ", fileName, (char *) NULL);
if (code != TCL_OK)
goto error;
Tcl_ResetResult(interp);
goto mainLoop;
}
/*
* We're running interactively. Source a user-specific startup
* file if the application specified one and if the file exists.
*/
#if (TCL_MAJOR_VERSION == 7) && (TCL_MINOR_VERSION <= 4)
if (tcl_RcFileName != NULL) {
Tcl_DString temp;
char *fullName;
FILE *f;
Tcl_DStringInit(&temp);
fullName = Tcl_TildeSubst(interp, fileName, &temp);
if (fullName == NULL)
fprintf(stderr, "%s\n", interp->result);
else {
/*
* Test for the existence of the rc file before trying to read it.
*/
f = fopen(fullName, "r");
if (f != NULL) {
fclose(f);
if (Tcl_EvalFile(interp, fullName) != TCL_OK)
fprintf(stderr, "%s\n", interp->result);
}
Tcl_DStringFree(&temp);
}
}
#else
fileName = Tcl_GetVar(interp, "tcl_rcFileName", TCL_GLOBAL_ONLY);
if (fileName != NULL) {
Tcl_Channel c;
Tcl_DString temp;
char *fullName;
Tcl_DStringInit(&temp);
fullName = Tcl_TranslateFileName(interp, fileName, &temp);
if (fullName == NULL) {
errChannel = Tcl_GetStdChannel(TCL_STDERR);
if (errChannel) {
Tcl_Write(errChannel, Tcl_GetStringResult(interp), -1);
Tcl_Write(errChannel, "\n", 1);
}
} else {
/*
* Test for the existence of the rc file before trying to read it.
*/
c = Tcl_OpenFileChannel(NULL, fullName, "r", 0);
if (c != (Tcl_Channel) NULL) {
Tcl_Close(NULL, c);
if (Tcl_EvalFile(interp, fullName) != TCL_OK) {
errChannel = Tcl_GetStdChannel(TCL_STDERR);
if (errChannel) {
Tcl_Write(errChannel, Tcl_GetStringResult(interp), -1);
Tcl_Write(errChannel, "\n", 1);
}
}
}
Tcl_DStringFree(&temp);
}
}
#endif
mainLoop:
/*
* Loop infinitely, waiting for commands to execute.
*/
#ifdef TCL_MEM_DEBUG
Tcl_Eval(interp, "proc exit {{code 0}} {destroy .}");
#endif
Ck_MainLoop();
#ifdef TCL_MEM_DEBUG
if (quitFlag) {
Tcl_DeleteInterp(interp);
Tcl_DumpActiveMemory(dumpFile);
}
#endif
/*
* Invoke Tcl exit command.
*/
Tcl_Eval(interp, "exit");
#if (TCL_MAJOR_VERSION >= 8)
Tcl_Exit(1);
#else
exit(1); /* Just in case */
#endif
error:
msg = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
if (msg == NULL) {
msg = Tcl_GetStringResult(interp);
}
errorExit:
if (msg != NULL) {
#if (TCL_MAJOR_VERSION == 7) && (TCL_MINOR_VERSION <= 4)
fprintf(stderr, "%s\n", msg);
#else
errChannel = Tcl_GetStdChannel(TCL_STDERR);
if (errChannel) {
Tcl_Write(errChannel, msg, -1);
Tcl_Write(errChannel, "\n", 1);
}
#endif
}
Tcl_Eval(interp, "exit 1");
#if (TCL_MAJOR_VERSION >= 8)
Tcl_Exit(1);
#else
exit(1); /* Just in case */
#endif
}
/*
*----------------------------------------------------------------------
*
* CheckmemCmd --
*
* This is the command procedure for the "checkmem" command, which
* causes the application to exit after printing information about
* memory usage to the file passed to this command as its first
* argument.
*
* Results:
* Returns a standard Tcl completion code.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
#ifdef TCL_MEM_DEBUG
static int
CheckmemCmd(clientData, interp, argc, argv)
ClientData clientData; /* Not used. */
Tcl_Interp *interp; /* Interpreter for evaluation. */
int argc; /* Number of arguments. */
char *argv[]; /* String values of arguments. */
{
if (argc != 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" fileName\"", (char *) NULL);
return TCL_ERROR;
}
strcpy(dumpFile, argv[1]);
quitFlag = 1;
return TCL_OK;
}
#endif