Debian Codegraph @

pTk/mTk/tclGeneric/tclBasic.c

raw symbols SCIP
1 /*
2 * tclBasic.c --
3 *
4 * Contains the basic facilities for TCL command interpretation,
5 * including interpreter creation and deletion, command creation
6 * and deletion, and command/script execution.
7 *
8 * Copyright (c) 1987-1994 The Regents of the University of California.
9 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
10 * Copyright (c) 1998-1999 by Scriptics Corporation.
11 * Copyright (c) 2001, 2002 by Kevin B. Kenny. All rights reserved.
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: tclBasic.c,v 1.75.2.8 2003/10/08 23:18:17 dgp Exp $
17 */
18
19 #include "tclInt.h"
20 #include "tclCompile.h"
21 #ifndef TCL_GENERIC_ONLY
22 # include "tclPort.h"
23 #endif
24
25 /*
26 * Static procedures in this file:
27 */
28
29 static char * CallCommandTraces _ANSI_ARGS_((Interp *iPtr,
30 Command *cmdPtr, CONST char *oldName,
31 CONST char* newName, int flags));
32 static void DeleteInterpProc _ANSI_ARGS_((Tcl_Interp *interp));
33 static void ProcessUnexpectedResult _ANSI_ARGS_((
34 Tcl_Interp *interp, int returnCode));
35 static int StringTraceProc _ANSI_ARGS_((ClientData clientData,
36 Tcl_Interp* interp,
37 int level,
38 CONST char* command,
39 Tcl_Command commandInfo,
40 int objc,
41 Tcl_Obj *CONST objv[]));
42 static void StringTraceDeleteProc _ANSI_ARGS_((ClientData clientData));
43
44 extern TclStubs tclStubs;
45
46 /*
47 * The following structure defines the commands in the Tcl core.
48 */
49
50 typedef struct {
51 char *name; /* Name of object-based command. */
52 Tcl_CmdProc *proc; /* String-based procedure for command. */
53 Tcl_ObjCmdProc *objProc; /* Object-based procedure for command. */
54 CompileProc *compileProc; /* Procedure called to compile command. */
55 int isSafe; /* If non-zero, command will be present
56 * in safe interpreter. Otherwise it will
57 * be hidden. */
58 } CmdInfo;
59
60 /*
61 * The built-in commands, and the procedures that implement them:
62 */
63
64 static CmdInfo builtInCmds[] = {
65 /*
66 * Commands in the generic core. Note that at least one of the proc or
67 * objProc members should be non-NULL. This avoids infinitely recursive
68 * calls between TclInvokeObjectCommand and TclInvokeStringCommand if a
69 * command name is computed at runtime and results in the name of a
70 * compiled command.
71 */
72
73 {"append", (Tcl_CmdProc *) NULL, Tcl_AppendObjCmd,
74 TclCompileAppendCmd, 1},
75 {"array", (Tcl_CmdProc *) NULL, Tcl_ArrayObjCmd,
76 (CompileProc *) NULL, 1},
77 {"binary", (Tcl_CmdProc *) NULL, Tcl_BinaryObjCmd,
78 (CompileProc *) NULL, 1},
79 {"break", (Tcl_CmdProc *) NULL, Tcl_BreakObjCmd,
80 TclCompileBreakCmd, 1},
81 {"case", (Tcl_CmdProc *) NULL, Tcl_CaseObjCmd,
82 (CompileProc *) NULL, 1},
83 {"catch", (Tcl_CmdProc *) NULL, Tcl_CatchObjCmd,
84 TclCompileCatchCmd, 1},
85 {"clock", (Tcl_CmdProc *) NULL, Tcl_ClockObjCmd,
86 (CompileProc *) NULL, 1},
87 {"concat", (Tcl_CmdProc *) NULL, Tcl_ConcatObjCmd,
88 (CompileProc *) NULL, 1},
89 {"continue", (Tcl_CmdProc *) NULL, Tcl_ContinueObjCmd,
90 TclCompileContinueCmd, 1},
91 {"encoding", (Tcl_CmdProc *) NULL, Tcl_EncodingObjCmd,
92 (CompileProc *) NULL, 0},
93 {"error", (Tcl_CmdProc *) NULL, Tcl_ErrorObjCmd,
94 (CompileProc *) NULL, 1},
95 {"eval", (Tcl_CmdProc *) NULL, Tcl_EvalObjCmd,
96 (CompileProc *) NULL, 1},
97 {"exit", (Tcl_CmdProc *) NULL, Tcl_ExitObjCmd,
98 (CompileProc *) NULL, 0},
99 {"expr", (Tcl_CmdProc *) NULL, Tcl_ExprObjCmd,
100 TclCompileExprCmd, 1},
101 {"fcopy", (Tcl_CmdProc *) NULL, Tcl_FcopyObjCmd,
102 (CompileProc *) NULL, 1},
103 {"fileevent", (Tcl_CmdProc *) NULL, Tcl_FileEventObjCmd,
104 (CompileProc *) NULL, 1},
105 {"for", (Tcl_CmdProc *) NULL, Tcl_ForObjCmd,
106 TclCompileForCmd, 1},
107 {"foreach", (Tcl_CmdProc *) NULL, Tcl_ForeachObjCmd,
108 TclCompileForeachCmd, 1},
109 {"format", (Tcl_CmdProc *) NULL, Tcl_FormatObjCmd,
110 (CompileProc *) NULL, 1},
111 {"global", (Tcl_CmdProc *) NULL, Tcl_GlobalObjCmd,
112 (CompileProc *) NULL, 1},
113 {"if", (Tcl_CmdProc *) NULL, Tcl_IfObjCmd,
114 TclCompileIfCmd, 1},
115 {"incr", (Tcl_CmdProc *) NULL, Tcl_IncrObjCmd,
116 TclCompileIncrCmd, 1},
117 {"info", (Tcl_CmdProc *) NULL, Tcl_InfoObjCmd,
118 (CompileProc *) NULL, 1},
119 {"join", (Tcl_CmdProc *) NULL, Tcl_JoinObjCmd,
120 (CompileProc *) NULL, 1},
121 {"lappend", (Tcl_CmdProc *) NULL, Tcl_LappendObjCmd,
122 TclCompileLappendCmd, 1},
123 {"lindex", (Tcl_CmdProc *) NULL, Tcl_LindexObjCmd,
124 TclCompileLindexCmd, 1},
125 {"linsert", (Tcl_CmdProc *) NULL, Tcl_LinsertObjCmd,
126 (CompileProc *) NULL, 1},
127 {"list", (Tcl_CmdProc *) NULL, Tcl_ListObjCmd,
128 TclCompileListCmd, 1},
129 {"llength", (Tcl_CmdProc *) NULL, Tcl_LlengthObjCmd,
130 TclCompileLlengthCmd, 1},
131 {"load", (Tcl_CmdProc *) NULL, Tcl_LoadObjCmd,
132 (CompileProc *) NULL, 0},
133 {"lrange", (Tcl_CmdProc *) NULL, Tcl_LrangeObjCmd,
134 (CompileProc *) NULL, 1},
135 {"lreplace", (Tcl_CmdProc *) NULL, Tcl_LreplaceObjCmd,
136 (CompileProc *) NULL, 1},
137 {"lsearch", (Tcl_CmdProc *) NULL, Tcl_LsearchObjCmd,
138 (CompileProc *) NULL, 1},
139 {"lset", (Tcl_CmdProc *) NULL, Tcl_LsetObjCmd,
140 TclCompileLsetCmd, 1},
141 {"lsort", (Tcl_CmdProc *) NULL, Tcl_LsortObjCmd,
142 (CompileProc *) NULL, 1},
143 {"namespace", (Tcl_CmdProc *) NULL, Tcl_NamespaceObjCmd,
144 (CompileProc *) NULL, 1},
145 {"package", (Tcl_CmdProc *) NULL, Tcl_PackageObjCmd,
146 (CompileProc *) NULL, 1},
147 {"proc", (Tcl_CmdProc *) NULL, Tcl_ProcObjCmd,
148 (CompileProc *) NULL, 1},
149 {"regexp", (Tcl_CmdProc *) NULL, Tcl_RegexpObjCmd,
150 TclCompileRegexpCmd, 1},
151 {"regsub", (Tcl_CmdProc *) NULL, Tcl_RegsubObjCmd,
152 (CompileProc *) NULL, 1},
153 {"rename", (Tcl_CmdProc *) NULL, Tcl_RenameObjCmd,
154 (CompileProc *) NULL, 1},
155 {"return", (Tcl_CmdProc *) NULL, Tcl_ReturnObjCmd,
156 TclCompileReturnCmd, 1},
157 {"scan", (Tcl_CmdProc *) NULL, Tcl_ScanObjCmd,
158 (CompileProc *) NULL, 1},
159 {"set", (Tcl_CmdProc *) NULL, Tcl_SetObjCmd,
160 TclCompileSetCmd, 1},
161 {"split", (Tcl_CmdProc *) NULL, Tcl_SplitObjCmd,
162 (CompileProc *) NULL, 1},
163 {"string", (Tcl_CmdProc *) NULL, Tcl_StringObjCmd,
164 TclCompileStringCmd, 1},
165 {"subst", (Tcl_CmdProc *) NULL, Tcl_SubstObjCmd,
166 (CompileProc *) NULL, 1},
167 {"switch", (Tcl_CmdProc *) NULL, Tcl_SwitchObjCmd,
168 (CompileProc *) NULL, 1},
169 {"trace", (Tcl_CmdProc *) NULL, Tcl_TraceObjCmd,
170 (CompileProc *) NULL, 1},
171 {"unset", (Tcl_CmdProc *) NULL, Tcl_UnsetObjCmd,
172 (CompileProc *) NULL, 1},
173 {"uplevel", (Tcl_CmdProc *) NULL, Tcl_UplevelObjCmd,
174 (CompileProc *) NULL, 1},
175 {"upvar", (Tcl_CmdProc *) NULL, Tcl_UpvarObjCmd,
176 (CompileProc *) NULL, 1},
177 {"variable", (Tcl_CmdProc *) NULL, Tcl_VariableObjCmd,
178 (CompileProc *) NULL, 1},
179 {"while", (Tcl_CmdProc *) NULL, Tcl_WhileObjCmd,
180 TclCompileWhileCmd, 1},
181
182 /*
183 * Commands in the UNIX core:
184 */
185
186 #ifndef TCL_GENERIC_ONLY
187 {"after", (Tcl_CmdProc *) NULL, Tcl_AfterObjCmd,
188 (CompileProc *) NULL, 1},
189 {"cd", (Tcl_CmdProc *) NULL, Tcl_CdObjCmd,
190 (CompileProc *) NULL, 0},
191 {"close", (Tcl_CmdProc *) NULL, Tcl_CloseObjCmd,
192 (CompileProc *) NULL, 1},
193 {"eof", (Tcl_CmdProc *) NULL, Tcl_EofObjCmd,
194 (CompileProc *) NULL, 1},
195 {"fblocked", (Tcl_CmdProc *) NULL, Tcl_FblockedObjCmd,
196 (CompileProc *) NULL, 1},
197 {"fconfigure", (Tcl_CmdProc *) NULL, Tcl_FconfigureObjCmd,
198 (CompileProc *) NULL, 0},
199 {"file", (Tcl_CmdProc *) NULL, Tcl_FileObjCmd,
200 (CompileProc *) NULL, 0},
201 {"flush", (Tcl_CmdProc *) NULL, Tcl_FlushObjCmd,
202 (CompileProc *) NULL, 1},
203 {"gets", (Tcl_CmdProc *) NULL, Tcl_GetsObjCmd,
204 (CompileProc *) NULL, 1},
205 {"glob", (Tcl_CmdProc *) NULL, Tcl_GlobObjCmd,
206 (CompileProc *) NULL, 0},
207 {"open", (Tcl_CmdProc *) NULL, Tcl_OpenObjCmd,
208 (CompileProc *) NULL, 0},
209 {"pid", (Tcl_CmdProc *) NULL, Tcl_PidObjCmd,
210 (CompileProc *) NULL, 1},
211 {"puts", (Tcl_CmdProc *) NULL, Tcl_PutsObjCmd,
212 (CompileProc *) NULL, 1},
213 {"pwd", (Tcl_CmdProc *) NULL, Tcl_PwdObjCmd,
214 (CompileProc *) NULL, 0},
215 {"read", (Tcl_CmdProc *) NULL, Tcl_ReadObjCmd,
216 (CompileProc *) NULL, 1},
217 {"seek", (Tcl_CmdProc *) NULL, Tcl_SeekObjCmd,
218 (CompileProc *) NULL, 1},
219 {"socket", (Tcl_CmdProc *) NULL, Tcl_SocketObjCmd,
220 (CompileProc *) NULL, 0},
221 {"tell", (Tcl_CmdProc *) NULL, Tcl_TellObjCmd,
222 (CompileProc *) NULL, 1},
223 {"time", (Tcl_CmdProc *) NULL, Tcl_TimeObjCmd,
224 (CompileProc *) NULL, 1},
225 {"update", (Tcl_CmdProc *) NULL, Tcl_UpdateObjCmd,
226 (CompileProc *) NULL, 1},
227 {"vwait", (Tcl_CmdProc *) NULL, Tcl_VwaitObjCmd,
228 (CompileProc *) NULL, 1},
229
230 #ifdef MAC_TCL
231 {"beep", (Tcl_CmdProc *) NULL, Tcl_BeepObjCmd,
232 (CompileProc *) NULL, 0},
233 {"echo", Tcl_EchoCmd, (Tcl_ObjCmdProc *) NULL,
234 (CompileProc *) NULL, 0},
235 {"ls", (Tcl_CmdProc *) NULL, Tcl_LsObjCmd,
236 (CompileProc *) NULL, 0},
237 {"resource", (Tcl_CmdProc *) NULL, Tcl_ResourceObjCmd,
238 (CompileProc *) NULL, 1},
239 {"source", (Tcl_CmdProc *) NULL, Tcl_MacSourceObjCmd,
240 (CompileProc *) NULL, 0},
241 #else
242 {"exec", (Tcl_CmdProc *) NULL, Tcl_ExecObjCmd,
243 (CompileProc *) NULL, 0},
244 {"source", (Tcl_CmdProc *) NULL, Tcl_SourceObjCmd,
245 (CompileProc *) NULL, 0},
246 #endif /* MAC_TCL */
247
248 #endif /* TCL_GENERIC_ONLY */
249 {NULL, (Tcl_CmdProc *) NULL, (Tcl_ObjCmdProc *) NULL,
250 (CompileProc *) NULL, 0}
251 };
252
253 /*
254 * The following structure holds the client data for string-based
255 * trace procs
256 */
257
258 typedef struct StringTraceData {
259 ClientData clientData; /* Client data from Tcl_CreateTrace */
260 Tcl_CmdTraceProc* proc; /* Trace procedure from Tcl_CreateTrace */
261 } StringTraceData;
262
263 /*
264 *----------------------------------------------------------------------
265 *
266 * Tcl_CreateInterp --
267 *
268 * Create a new TCL command interpreter.
269 *
270 * Results:
271 * The return value is a token for the interpreter, which may be
272 * used in calls to procedures like Tcl_CreateCmd, Tcl_Eval, or
273 * Tcl_DeleteInterp.
274 *
275 * Side effects:
276 * The command interpreter is initialized with the built-in commands
277 * and with the variables documented in tclvars(n).
278 *
279 *----------------------------------------------------------------------
280 */
281
282 Tcl_Interp *
283 Tcl_CreateInterp()
284 {
285 Interp *iPtr;
286 Tcl_Interp *interp;
287 Command *cmdPtr;
288 BuiltinFunc *builtinFuncPtr;
289 MathFunc *mathFuncPtr;
290 Tcl_HashEntry *hPtr;
291 CmdInfo *cmdInfoPtr;
292 int i;
293 union {
294 char c[sizeof(short)];
295 short s;
296 } order;
297 #ifdef TCL_COMPILE_STATS
298 ByteCodeStats *statsPtr;
299 #endif /* TCL_COMPILE_STATS */
300
301 TclInitSubsystems(NULL);
302
303 /*
304 * Panic if someone updated the CallFrame structure without
305 * also updating the Tcl_CallFrame structure (or vice versa).
306 */
307
308 if (sizeof(Tcl_CallFrame) != sizeof(CallFrame)) {
309 /*NOTREACHED*/
310 panic("Tcl_CallFrame and CallFrame are not the same size");
311 }
312
313 /*
314 * Initialize support for namespaces and create the global namespace
315 * (whose name is ""; an alias is "::"). This also initializes the
316 * Tcl object type table and other object management code.
317 */
318
319 iPtr = (Interp *) ckalloc(sizeof(Interp));
320 interp = (Tcl_Interp *) iPtr;
321
322 iPtr->result = iPtr->resultSpace;
323 iPtr->freeProc = NULL;
324 iPtr->errorLine = 0;
325 iPtr->objResultPtr = Tcl_NewObj();
326 Tcl_IncrRefCount(iPtr->objResultPtr);
327 iPtr->handle = TclHandleCreate(iPtr);
328 iPtr->globalNsPtr = NULL;
329 iPtr->hiddenCmdTablePtr = NULL;
330 iPtr->interpInfo = NULL;
331 Tcl_InitHashTable(&iPtr->mathFuncTable, TCL_STRING_KEYS);
332
333 iPtr->numLevels = 0;
334 iPtr->maxNestingDepth = MAX_NESTING_DEPTH;
335 iPtr->framePtr = NULL;
336 iPtr->varFramePtr = NULL;
337 iPtr->activeVarTracePtr = NULL;
338 iPtr->returnCode = TCL_OK;
339 iPtr->errorInfo = NULL;
340 iPtr->errorCode = NULL;
341
342 iPtr->appendResult = NULL;
343 iPtr->appendAvl = 0;
344 iPtr->appendUsed = 0;
345
346 Tcl_InitHashTable(&iPtr->packageTable, TCL_STRING_KEYS);
347 iPtr->packageUnknown = NULL;
348 iPtr->cmdCount = 0;
349 iPtr->termOffset = 0;
350 TclInitLiteralTable(&(iPtr->literalTable));
351 iPtr->compileEpoch = 0;
352 iPtr->compiledProcPtr = NULL;
353 iPtr->resolverPtr = NULL;
354 iPtr->evalFlags = 0;
355 iPtr->scriptFile = NULL;
356 iPtr->flags = 0;
357 iPtr->tracePtr = NULL;
358 iPtr->tracesForbiddingInline = 0;
359 iPtr->activeCmdTracePtr = NULL;
360 iPtr->activeInterpTracePtr = NULL;
361 iPtr->assocData = (Tcl_HashTable *) NULL;
362 iPtr->execEnvPtr = NULL; /* set after namespaces initialized */
363 iPtr->emptyObjPtr = Tcl_NewObj(); /* another empty object */
364 Tcl_IncrRefCount(iPtr->emptyObjPtr);
365 iPtr->resultSpace[0] = 0;
366
367 iPtr->globalNsPtr = NULL; /* force creation of global ns below */
368 iPtr->globalNsPtr = (Namespace *) Tcl_CreateNamespace(interp, "",
369 (ClientData) NULL, (Tcl_NamespaceDeleteProc *) NULL);
370 if (iPtr->globalNsPtr == NULL) {
371 panic("Tcl_CreateInterp: can't create global namespace");
372 }
373
374 /*
375 * Initialize support for code compilation and execution. We call
376 * TclCreateExecEnv after initializing namespaces since it tries to
377 * reference a Tcl variable (it links to the Tcl "tcl_traceExec"
378 * variable).
379 */
380
381 iPtr->execEnvPtr = TclCreateExecEnv(interp);
382
383 /*
384 * Initialize the compilation and execution statistics kept for this
385 * interpreter.
386 */
387
388 #ifdef TCL_COMPILE_STATS
389 statsPtr = &(iPtr->stats);
390 statsPtr->numExecutions = 0;
391 statsPtr->numCompilations = 0;
392 statsPtr->numByteCodesFreed = 0;
393 (VOID *) memset(statsPtr->instructionCount, 0,
394 sizeof(statsPtr->instructionCount));
395
396 statsPtr->totalSrcBytes = 0.0;
397 statsPtr->totalByteCodeBytes = 0.0;
398 statsPtr->currentSrcBytes = 0.0;
399 statsPtr->currentByteCodeBytes = 0.0;
400 (VOID *) memset(statsPtr->srcCount, 0, sizeof(statsPtr->srcCount));
401 (VOID *) memset(statsPtr->byteCodeCount, 0,
402 sizeof(statsPtr->byteCodeCount));
403 (VOID *) memset(statsPtr->lifetimeCount, 0,
404 sizeof(statsPtr->lifetimeCount));
405
406 statsPtr->currentInstBytes = 0.0;
407 statsPtr->currentLitBytes = 0.0;
408 statsPtr->currentExceptBytes = 0.0;
409 statsPtr->currentAuxBytes = 0.0;
410 statsPtr->currentCmdMapBytes = 0.0;
411
412 statsPtr->numLiteralsCreated = 0;
413 statsPtr->totalLitStringBytes = 0.0;
414 statsPtr->currentLitStringBytes = 0.0;
415 (VOID *) memset(statsPtr->literalCount, 0,
416 sizeof(statsPtr->literalCount));
417 #endif /* TCL_COMPILE_STATS */
418
419 /*
420 * Initialise the stub table pointer.
421 */
422
423 iPtr->stubTable = &tclStubs;
424
425
426 /*
427 * Create the core commands. Do it here, rather than calling
428 * Tcl_CreateCommand, because it's faster (there's no need to check for
429 * a pre-existing command by the same name). If a command has a
430 * Tcl_CmdProc but no Tcl_ObjCmdProc, set the Tcl_ObjCmdProc to
431 * TclInvokeStringCommand. This is an object-based wrapper procedure
432 * that extracts strings, calls the string procedure, and creates an
433 * object for the result. Similarly, if a command has a Tcl_ObjCmdProc
434 * but no Tcl_CmdProc, set the Tcl_CmdProc to TclInvokeObjectCommand.
435 */
436
437 for (cmdInfoPtr = builtInCmds; cmdInfoPtr->name != NULL;
438 cmdInfoPtr++) {
439 int new;
440 Tcl_HashEntry *hPtr;
441
442 if ((cmdInfoPtr->proc == (Tcl_CmdProc *) NULL)
443 && (cmdInfoPtr->objProc == (Tcl_ObjCmdProc *) NULL)
444 && (cmdInfoPtr->compileProc == (CompileProc *) NULL)) {
445 panic("Tcl_CreateInterp: builtin command with NULL string and object command procs and a NULL compile proc\n");
446 }
447
448 hPtr = Tcl_CreateHashEntry(&iPtr->globalNsPtr->cmdTable,
449 cmdInfoPtr->name, &new);
450 if (new) {
451 cmdPtr = (Command *) ckalloc(sizeof(Command));
452 cmdPtr->hPtr = hPtr;
453 cmdPtr->nsPtr = iPtr->globalNsPtr;
454 cmdPtr->refCount = 1;
455 cmdPtr->cmdEpoch = 0;
456 cmdPtr->compileProc = cmdInfoPtr->compileProc;
457 if (cmdInfoPtr->proc == (Tcl_CmdProc *) NULL) {
458 cmdPtr->proc = TclInvokeObjectCommand;
459 cmdPtr->clientData = (ClientData) cmdPtr;
460 } else {
461 cmdPtr->proc = cmdInfoPtr->proc;
462 cmdPtr->clientData = (ClientData) NULL;
463 }
464 if (cmdInfoPtr->objProc == (Tcl_ObjCmdProc *) NULL) {
465 cmdPtr->objProc = TclInvokeStringCommand;
466 cmdPtr->objClientData = (ClientData) cmdPtr;
467 } else {
468 cmdPtr->objProc = cmdInfoPtr->objProc;
469 cmdPtr->objClientData = (ClientData) NULL;
470 }
471 cmdPtr->deleteProc = NULL;
472 cmdPtr->deleteData = (ClientData) NULL;
473 cmdPtr->flags = 0;
474 cmdPtr->importRefPtr = NULL;
475 cmdPtr->tracePtr = NULL;
476 Tcl_SetHashValue(hPtr, cmdPtr);
477 }
478 }
479
480 /*
481 * Register the builtin math functions.
482 */
483
484 i = 0;
485 for (builtinFuncPtr = tclBuiltinFuncTable; builtinFuncPtr->name != NULL;
486 builtinFuncPtr++) {
487 Tcl_CreateMathFunc((Tcl_Interp *) iPtr, builtinFuncPtr->name,
488 builtinFuncPtr->numArgs, builtinFuncPtr->argTypes,
489 (Tcl_MathProc *) NULL, (ClientData) 0);
490 hPtr = Tcl_FindHashEntry(&iPtr->mathFuncTable,
491 builtinFuncPtr->name);
492 if (hPtr == NULL) {
493 panic("Tcl_CreateInterp: Tcl_CreateMathFunc incorrectly registered '%s'", builtinFuncPtr->name);
494 return NULL;
495 }
496 mathFuncPtr = (MathFunc *) Tcl_GetHashValue(hPtr);
497 mathFuncPtr->builtinFuncIndex = i;
498 i++;
499 }
500 iPtr->flags |= EXPR_INITIALIZED;
501
502 /*
503 * Do Multiple/Safe Interps Tcl init stuff
504 */
505
506 TclInterpInit(interp);
507
508 /*
509 * We used to create the "errorInfo" and "errorCode" global vars at this
510 * point because so much of the Tcl implementation assumes they already
511 * exist. This is not quite enough, however, since they can be unset
512 * at any time.
513 *
514 * There are 2 choices:
515 * + Check every place where a GetVar of those is used
516 * and the NULL result is not checked (like in tclLoad.c)
517 * + Make SetVar,... NULL friendly
518 * We choose the second option because :
519 * + It is easy and low cost to check for NULL pointer before
520 * calling strlen()
521 * + It can be helpfull to other people using those API
522 * + Passing a NULL value to those closest 'meaning' is empty string
523 * (specially with the new objects where 0 bytes strings are ok)
524 * So the following init is commented out: -- dl
525 *
526 * (void) Tcl_SetVar2((Tcl_Interp *)iPtr, "errorInfo", (char *) NULL,
527 * "", TCL_GLOBAL_ONLY);
528 * (void) Tcl_SetVar2((Tcl_Interp *)iPtr, "errorCode", (char *) NULL,
529 * "NONE", TCL_GLOBAL_ONLY);
530 */
531
532 #ifndef TCL_GENERIC_ONLY
533 TclSetupEnv(interp);
534 #endif
535
536 /*
537 * Compute the byte order of this machine.
538 */
539
540 order.s = 1;
541 Tcl_SetVar2(interp, "tcl_platform", "byteOrder",
542 ((order.c[0] == 1) ? "littleEndian" : "bigEndian"),
543 TCL_GLOBAL_ONLY);
544
545 Tcl_SetVar2Ex(interp, "tcl_platform", "wordSize",
546 Tcl_NewLongObj((long) sizeof(long)), TCL_GLOBAL_ONLY);
547
548 /*
549 * Set up other variables such as tcl_version and tcl_library
550 */
551
552 Tcl_SetVar(interp, "tcl_patchLevel", TCL_PATCH_LEVEL, TCL_GLOBAL_ONLY);
553 Tcl_SetVar(interp, "tcl_version", TCL_VERSION, TCL_GLOBAL_ONLY);
554 Tcl_TraceVar2(interp, "tcl_precision", (char *) NULL,
555 TCL_GLOBAL_ONLY|TCL_TRACE_READS|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
556 TclPrecTraceProc, (ClientData) NULL);
557 TclpSetVariables(interp);
558
559 #ifdef TCL_THREADS
560 /*
561 * The existence of the "threaded" element of the tcl_platform array indicates
562 * that this particular Tcl shell has been compiled with threads turned on.
563 * Using "info exists tcl_platform(threaded)" a Tcl script can introspect on the
564 * interpreter level of thread safety.
565 */
566
567
568 Tcl_SetVar2(interp, "tcl_platform", "threaded", "1",
569 TCL_GLOBAL_ONLY);
570 #endif
571
572 /*
573 * Register Tcl's version number.
574 */
575
576 Tcl_PkgProvideEx(interp, "Tcl", TCL_VERSION, (ClientData) &tclStubs);
577
578 #ifdef Tcl_InitStubs
579 #undef Tcl_InitStubs
580 #endif
581 Tcl_InitStubs(interp, TCL_VERSION, 1);
582
583 return interp;
584 }
585
586 /*
587 *----------------------------------------------------------------------
588 *
589 * TclHideUnsafeCommands --
590 *
591 * Hides base commands that are not marked as safe from this
592 * interpreter.
593 *
594 * Results:
595 * TCL_OK if it succeeds, TCL_ERROR else.
596 *
597 * Side effects:
598 * Hides functionality in an interpreter.
599 *
600 *----------------------------------------------------------------------
601 */
602
603 int
604 TclHideUnsafeCommands(interp)
605 Tcl_Interp *interp; /* Hide commands in this interpreter. */
606 {
607 register CmdInfo *cmdInfoPtr;
608
609 if (interp == (Tcl_Interp *) NULL) {
610 return TCL_ERROR;
611 }
612 for (cmdInfoPtr = builtInCmds; cmdInfoPtr->name != NULL; cmdInfoPtr++) {
613 if (!cmdInfoPtr->isSafe) {
614 Tcl_HideCommand(interp, cmdInfoPtr->name, cmdInfoPtr->name);
615 }
616 }
617 return TCL_OK;
618 }
619
620 /*
621 *--------------------------------------------------------------
622 *
623 * Tcl_CallWhenDeleted --
624 *
625 * Arrange for a procedure to be called before a given
626 * interpreter is deleted. The procedure is called as soon
627 * as Tcl_DeleteInterp is called; if Tcl_CallWhenDeleted is
628 * called on an interpreter that has already been deleted,
629 * the procedure will be called when the last Tcl_Release is
630 * done on the interpreter.
631 *
632 * Results:
633 * None.
634 *
635 * Side effects:
636 * When Tcl_DeleteInterp is invoked to delete interp,
637 * proc will be invoked. See the manual entry for
638 * details.
639 *
640 *--------------------------------------------------------------
641 */
642
643 void
644 Tcl_CallWhenDeleted(interp, proc, clientData)
645 Tcl_Interp *interp; /* Interpreter to watch. */
646 Tcl_InterpDeleteProc *proc; /* Procedure to call when interpreter
647 * is about to be deleted. */
648 ClientData clientData; /* One-word value to pass to proc. */
649 {
650 Interp *iPtr = (Interp *) interp;
651 static int assocDataCounter = 0;
652 #ifdef TCL_THREADS
653 static Tcl_Mutex assocMutex;
654 #endif
655 int new;
656 char buffer[32 + TCL_INTEGER_SPACE];
657 AssocData *dPtr = (AssocData *) ckalloc(sizeof(AssocData));
658 Tcl_HashEntry *hPtr;
659
660 Tcl_MutexLock(&assocMutex);
661 sprintf(buffer, "Assoc Data Key #%d", assocDataCounter);
662 assocDataCounter++;
663 Tcl_MutexUnlock(&assocMutex);
664
665 if (iPtr->assocData == (Tcl_HashTable *) NULL) {
666 iPtr->assocData = (Tcl_HashTable *) ckalloc(sizeof(Tcl_HashTable));
667 Tcl_InitHashTable(iPtr->assocData, TCL_STRING_KEYS);
668 }
669 hPtr = Tcl_CreateHashEntry(iPtr->assocData, buffer, &new);
670 dPtr->proc = proc;
671 dPtr->clientData = clientData;
672 Tcl_SetHashValue(hPtr, dPtr);
673 }
674
675 /*
676 *--------------------------------------------------------------
677 *
678 * Tcl_DontCallWhenDeleted --
679 *
680 * Cancel the arrangement for a procedure to be called when
681 * a given interpreter is deleted.
682 *
683 * Results:
684 * None.
685 *
686 * Side effects:
687 * If proc and clientData were previously registered as a
688 * callback via Tcl_CallWhenDeleted, they are unregistered.
689 * If they weren't previously registered then nothing
690 * happens.
691 *
692 *--------------------------------------------------------------
693 */
694
695 void
696 Tcl_DontCallWhenDeleted(interp, proc, clientData)
697 Tcl_Interp *interp; /* Interpreter to watch. */
698 Tcl_InterpDeleteProc *proc; /* Procedure to call when interpreter
699 * is about to be deleted. */
700 ClientData clientData; /* One-word value to pass to proc. */
701 {
702 Interp *iPtr = (Interp *) interp;
703 Tcl_HashTable *hTablePtr;
704 Tcl_HashSearch hSearch;
705 Tcl_HashEntry *hPtr;
706 AssocData *dPtr;
707
708 hTablePtr = iPtr->assocData;
709 if (hTablePtr == (Tcl_HashTable *) NULL) {
710 return;
711 }
712 for (hPtr = Tcl_FirstHashEntry(hTablePtr, &hSearch); hPtr != NULL;
713 hPtr = Tcl_NextHashEntry(&hSearch)) {
714 dPtr = (AssocData *) Tcl_GetHashValue(hPtr);
715 if ((dPtr->proc == proc) && (dPtr->clientData == clientData)) {
716 ckfree((char *) dPtr);
717 Tcl_DeleteHashEntry(hPtr);
718 return;
719 }
720 }
721 }
722
723 /*
724 *----------------------------------------------------------------------
725 *
726 * Tcl_SetAssocData --
727 *
728 * Creates a named association between user-specified data, a delete
729 * function and this interpreter. If the association already exists
730 * the data is overwritten with the new data. The delete function will
731 * be invoked when the interpreter is deleted.
732 *
733 * Results:
734 * None.
735 *
736 * Side effects:
737 * Sets the associated data, creates the association if needed.
738 *
739 *----------------------------------------------------------------------
740 */
741
742 void
743 Tcl_SetAssocData(interp, name, proc, clientData)
744 Tcl_Interp *interp; /* Interpreter to associate with. */
745 CONST char *name; /* Name for association. */
746 Tcl_InterpDeleteProc *proc; /* Proc to call when interpreter is
747 * about to be deleted. */
748 ClientData clientData; /* One-word value to pass to proc. */
749 {
750 Interp *iPtr = (Interp *) interp;
751 AssocData *dPtr;
752 Tcl_HashEntry *hPtr;
753 int new;
754
755 if (iPtr->assocData == (Tcl_HashTable *) NULL) {
756 iPtr->assocData = (Tcl_HashTable *) ckalloc(sizeof(Tcl_HashTable));
757 Tcl_InitHashTable(iPtr->assocData, TCL_STRING_KEYS);
758 }
759 hPtr = Tcl_CreateHashEntry(iPtr->assocData, name, &new);
760 if (new == 0) {
761 dPtr = (AssocData *) Tcl_GetHashValue(hPtr);
762 } else {
763 dPtr = (AssocData *) ckalloc(sizeof(AssocData));
764 }
765 dPtr->proc = proc;
766 dPtr->clientData = clientData;
767
768 Tcl_SetHashValue(hPtr, dPtr);
769 }
770
771 /*
772 *----------------------------------------------------------------------
773 *
774 * Tcl_DeleteAssocData --
775 *
776 * Deletes a named association of user-specified data with
777 * the specified interpreter.
778 *
779 * Results:
780 * None.
781 *
782 * Side effects:
783 * Deletes the association.
784 *
785 *----------------------------------------------------------------------
786 */
787
788 void
789 Tcl_DeleteAssocData(interp, name)
790 Tcl_Interp *interp; /* Interpreter to associate with. */
791 CONST char *name; /* Name of association. */
792 {
793 Interp *iPtr = (Interp *) interp;
794 AssocData *dPtr;
795 Tcl_HashEntry *hPtr;
796
797 if (iPtr->assocData == (Tcl_HashTable *) NULL) {
798 return;
799 }
800 hPtr = Tcl_FindHashEntry(iPtr->assocData, name);
801 if (hPtr == (Tcl_HashEntry *) NULL) {
802 return;
803 }
804 dPtr = (AssocData *) Tcl_GetHashValue(hPtr);
805 if (dPtr->proc != NULL) {
806 (dPtr->proc) (dPtr->clientData, interp);
807 }
808 ckfree((char *) dPtr);
809 Tcl_DeleteHashEntry(hPtr);
810 }
811
812 /*
813 *----------------------------------------------------------------------
814 *
815 * Tcl_GetAssocData --
816 *
817 * Returns the client data associated with this name in the
818 * specified interpreter.
819 *
820 * Results:
821 * The client data in the AssocData record denoted by the named
822 * association, or NULL.
823 *
824 * Side effects:
825 * None.
826 *
827 *----------------------------------------------------------------------
828 */
829
830 ClientData
831 Tcl_GetAssocData(interp, name, procPtr)
832 Tcl_Interp *interp; /* Interpreter associated with. */
833 CONST char *name; /* Name of association. */
834 Tcl_InterpDeleteProc **procPtr; /* Pointer to place to store address
835 * of current deletion callback. */
836 {
837 Interp *iPtr = (Interp *) interp;
838 AssocData *dPtr;
839 Tcl_HashEntry *hPtr;
840
841 if (iPtr->assocData == (Tcl_HashTable *) NULL) {
842 return (ClientData) NULL;
843 }
844 hPtr = Tcl_FindHashEntry(iPtr->assocData, name);
845 if (hPtr == (Tcl_HashEntry *) NULL) {
846 return (ClientData) NULL;
847 }
848 dPtr = (AssocData *) Tcl_GetHashValue(hPtr);
849 if (procPtr != (Tcl_InterpDeleteProc **) NULL) {
850 *procPtr = dPtr->proc;
851 }
852 return dPtr->clientData;
853 }
854
855 /*
856 *----------------------------------------------------------------------
857 *
858 * Tcl_InterpDeleted --
859 *
860 * Returns nonzero if the interpreter has been deleted with a call
861 * to Tcl_DeleteInterp.
862 *
863 * Results:
864 * Nonzero if the interpreter is deleted, zero otherwise.
865 *
866 * Side effects:
867 * None.
868 *
869 *----------------------------------------------------------------------
870 */
871
872 int
873 Tcl_InterpDeleted(interp)
874 Tcl_Interp *interp;
875 {
876 return (((Interp *) interp)->flags & DELETED) ? 1 : 0;
877 }
878
879 /*
880 *----------------------------------------------------------------------
881 *
882 * Tcl_DeleteInterp --
883 *
884 * Ensures that the interpreter will be deleted eventually. If there
885 * are no Tcl_Preserve calls in effect for this interpreter, it is
886 * deleted immediately, otherwise the interpreter is deleted when
887 * the last Tcl_Preserve is matched by a call to Tcl_Release. In either
888 * case, the procedure runs the currently registered deletion callbacks.
889 *
890 * Results:
891 * None.
892 *
893 * Side effects:
894 * The interpreter is marked as deleted. The caller may still use it
895 * safely if there are calls to Tcl_Preserve in effect for the
896 * interpreter, but further calls to Tcl_Eval etc in this interpreter
897 * will fail.
898 *
899 *----------------------------------------------------------------------
900 */
901
902 void
903 Tcl_DeleteInterp(interp)
904 Tcl_Interp *interp; /* Token for command interpreter (returned
905 * by a previous call to Tcl_CreateInterp). */
906 {
907 Interp *iPtr = (Interp *) interp;
908
909 /*
910 * If the interpreter has already been marked deleted, just punt.
911 */
912
913 if (iPtr->flags & DELETED) {
914 return;
915 }
916
917 /*
918 * Mark the interpreter as deleted. No further evals will be allowed.
919 */
920
921 iPtr->flags |= DELETED;
922
923 /*
924 * Ensure that the interpreter is eventually deleted.
925 */
926
927 Tcl_EventuallyFree((ClientData) interp,
928 (Tcl_FreeProc *) DeleteInterpProc);
929 }
930
931 /*
932 *----------------------------------------------------------------------
933 *
934 * DeleteInterpProc --
935 *
936 * Helper procedure to delete an interpreter. This procedure is
937 * called when the last call to Tcl_Preserve on this interpreter
938 * is matched by a call to Tcl_Release. The procedure cleans up
939 * all resources used in the interpreter and calls all currently
940 * registered interpreter deletion callbacks.
941 *
942 * Results:
943 * None.
944 *
945 * Side effects:
946 * Whatever the interpreter deletion callbacks do. Frees resources
947 * used by the interpreter.
948 *
949 *----------------------------------------------------------------------
950 */
951
952 static void
953 DeleteInterpProc(interp)
954 Tcl_Interp *interp; /* Interpreter to delete. */
955 {
956 Interp *iPtr = (Interp *) interp;
957 Tcl_HashEntry *hPtr;
958 Tcl_HashSearch search;
959 Tcl_HashTable *hTablePtr;
960 ResolverScheme *resPtr, *nextResPtr;
961
962 /*
963 * Punt if there is an error in the Tcl_Release/Tcl_Preserve matchup.
964 */
965
966 if (iPtr->numLevels > 0) {
967 panic("DeleteInterpProc called with active evals");
968 }
969
970 /*
971 * The interpreter should already be marked deleted; otherwise how
972 * did we get here?
973 */
974
975 if (!(iPtr->flags & DELETED)) {
976 panic("DeleteInterpProc called on interpreter not marked deleted");
977 }
978
979 TclHandleFree(iPtr->handle);
980
981 /*
982 * Dismantle everything in the global namespace except for the
983 * "errorInfo" and "errorCode" variables. These remain until the
984 * namespace is actually destroyed, in case any errors occur.
985 *
986 * Dismantle the namespace here, before we clear the assocData. If any
987 * background errors occur here, they will be deleted below.
988 */
989
990 TclTeardownNamespace(iPtr->globalNsPtr);
991
992 /*
993 * Delete all the hidden commands.
994 */
995
996 hTablePtr = iPtr->hiddenCmdTablePtr;
997 if (hTablePtr != NULL) {
998 /*
999 * Non-pernicious deletion. The deletion callbacks will not be
1000 * allowed to create any new hidden or non-hidden commands.
1001 * Tcl_DeleteCommandFromToken() will remove the entry from the
1002 * hiddenCmdTablePtr.
1003 */
1004
1005 hPtr = Tcl_FirstHashEntry(hTablePtr, &search);
1006 for ( ; hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
1007 Tcl_DeleteCommandFromToken(interp,
1008 (Tcl_Command) Tcl_GetHashValue(hPtr));
1009 }
1010 Tcl_DeleteHashTable(hTablePtr);
1011 ckfree((char *) hTablePtr);
1012 }
1013 /*
1014 * Tear down the math function table.
1015 */
1016
1017 for (hPtr = Tcl_FirstHashEntry(&iPtr->mathFuncTable, &search);
1018 hPtr != NULL;
1019 hPtr = Tcl_NextHashEntry(&search)) {
1020 ckfree((char *) Tcl_GetHashValue(hPtr));
1021 }
1022 Tcl_DeleteHashTable(&iPtr->mathFuncTable);
1023
1024 /*
1025 * Invoke deletion callbacks; note that a callback can create new
1026 * callbacks, so we iterate.
1027 */
1028
1029 while (iPtr->assocData != (Tcl_HashTable *) NULL) {
1030 AssocData *dPtr;
1031
1032 hTablePtr = iPtr->assocData;
1033 iPtr->assocData = (Tcl_HashTable *) NULL;
1034 for (hPtr = Tcl_FirstHashEntry(hTablePtr, &search);
1035 hPtr != NULL;
1036 hPtr = Tcl_FirstHashEntry(hTablePtr, &search)) {
1037 dPtr = (AssocData *) Tcl_GetHashValue(hPtr);
1038 Tcl_DeleteHashEntry(hPtr);
1039 if (dPtr->proc != NULL) {
1040 (*dPtr->proc)(dPtr->clientData, interp);
1041 }
1042 ckfree((char *) dPtr);
1043 }
1044 Tcl_DeleteHashTable(hTablePtr);
1045 ckfree((char *) hTablePtr);
1046 }
1047
1048 /*
1049 * Finish deleting the global namespace.
1050 */
1051
1052 Tcl_DeleteNamespace((Tcl_Namespace *) iPtr->globalNsPtr);
1053
1054 /*
1055 * Free up the result *after* deleting variables, since variable
1056 * deletion could have transferred ownership of the result string
1057 * to Tcl.
1058 */
1059
1060 Tcl_FreeResult(interp);
1061 interp->result = NULL;
1062 Tcl_DecrRefCount(iPtr->objResultPtr);
1063 iPtr->objResultPtr = NULL;
1064 if (iPtr->errorInfo != NULL) {
1065 ckfree(iPtr->errorInfo);
1066 iPtr->errorInfo = NULL;
1067 }
1068 if (iPtr->errorCode != NULL) {
1069 ckfree(iPtr->errorCode);
1070 iPtr->errorCode = NULL;
1071 }
1072 if (iPtr->appendResult != NULL) {
1073 ckfree(iPtr->appendResult);
1074 iPtr->appendResult = NULL;
1075 }
1076 TclFreePackageInfo(iPtr);
1077 while (iPtr->tracePtr != NULL) {
1078 Tcl_DeleteTrace((Tcl_Interp*) iPtr, (Tcl_Trace) iPtr->tracePtr);
1079 }
1080 if (iPtr->execEnvPtr != NULL) {
1081 TclDeleteExecEnv(iPtr->execEnvPtr);
1082 }
1083 Tcl_DecrRefCount(iPtr->emptyObjPtr);
1084 iPtr->emptyObjPtr = NULL;
1085
1086 resPtr = iPtr->resolverPtr;
1087 while (resPtr) {
1088 nextResPtr = resPtr->nextPtr;
1089 ckfree(resPtr->name);
1090 ckfree((char *) resPtr);
1091 resPtr = nextResPtr;
1092 }
1093
1094 /*
1095 * Free up literal objects created for scripts compiled by the
1096 * interpreter.
1097 */
1098
1099 TclDeleteLiteralTable(interp, &(iPtr->literalTable));
1100 ckfree((char *) iPtr);
1101 }
1102
1103 /*
1104 *---------------------------------------------------------------------------
1105 *
1106 * Tcl_HideCommand --
1107 *
1108 * Makes a command hidden so that it cannot be invoked from within
1109 * an interpreter, only from within an ancestor.
1110 *
1111 * Results:
1112 * A standard Tcl result; also leaves a message in the interp's result
1113 * if an error occurs.
1114 *
1115 * Side effects:
1116 * Removes a command from the command table and create an entry
1117 * into the hidden command table under the specified token name.
1118 *
1119 *---------------------------------------------------------------------------
1120 */
1121
1122 int
1123 Tcl_HideCommand(interp, cmdName, hiddenCmdToken)
1124 Tcl_Interp *interp; /* Interpreter in which to hide command. */
1125 CONST char *cmdName; /* Name of command to hide. */
1126 CONST char *hiddenCmdToken; /* Token name of the to-be-hidden command. */
1127 {
1128 Interp *iPtr = (Interp *) interp;
1129 Tcl_Command cmd;
1130 Command *cmdPtr;
1131 Tcl_HashTable *hiddenCmdTablePtr;
1132 Tcl_HashEntry *hPtr;
1133 int new;
1134
1135 if (iPtr->flags & DELETED) {
1136
1137 /*
1138 * The interpreter is being deleted. Do not create any new
1139 * structures, because it is not safe to modify the interpreter.
1140 */
1141
1142 return TCL_ERROR;
1143 }
1144
1145 /*
1146 * Disallow hiding of commands that are currently in a namespace or
1147 * renaming (as part of hiding) into a namespace.
1148 *
1149 * (because the current implementation with a single global table
1150 * and the needed uniqueness of names cause problems with namespaces)
1151 *
1152 * we don't need to check for "::" in cmdName because the real check is
1153 * on the nsPtr below.
1154 *
1155 * hiddenCmdToken is just a string which is not interpreted in any way.
1156 * It may contain :: but the string is not interpreted as a namespace
1157 * qualifier command name. Thus, hiding foo::bar to foo::bar and then
1158 * trying to expose or invoke ::foo::bar will NOT work; but if the
1159 * application always uses the same strings it will get consistent
1160 * behaviour.
1161 *
1162 * But as we currently limit ourselves to the global namespace only
1163 * for the source, in order to avoid potential confusion,
1164 * lets prevent "::" in the token too. --dl
1165 */
1166
1167 if (strstr(hiddenCmdToken, "::") != NULL) {
1168 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1169 "cannot use namespace qualifiers in hidden command",
1170 " token (rename)", (char *) NULL);
1171 return TCL_ERROR;
1172 }
1173
1174 /*
1175 * Find the command to hide. An error is returned if cmdName can't
1176 * be found. Look up the command only from the global namespace.
1177 * Full path of the command must be given if using namespaces.
1178 */
1179
1180 cmd = Tcl_FindCommand(interp, cmdName, (Tcl_Namespace *) NULL,
1181 /*flags*/ TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
1182 if (cmd == (Tcl_Command) NULL) {
1183 return TCL_ERROR;
1184 }
1185 cmdPtr = (Command *) cmd;
1186
1187 /*
1188 * Check that the command is really in global namespace
1189 */
1190
1191 if ( cmdPtr->nsPtr != iPtr->globalNsPtr ) {
1192 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1193 "can only hide global namespace commands",
1194 " (use rename then hide)", (char *) NULL);
1195 return TCL_ERROR;
1196 }
1197
1198 /*
1199 * Initialize the hidden command table if necessary.
1200 */
1201
1202 hiddenCmdTablePtr = iPtr->hiddenCmdTablePtr;
1203 if (hiddenCmdTablePtr == NULL) {
1204 hiddenCmdTablePtr = (Tcl_HashTable *)
1205 ckalloc((unsigned) sizeof(Tcl_HashTable));
1206 Tcl_InitHashTable(hiddenCmdTablePtr, TCL_STRING_KEYS);
1207 iPtr->hiddenCmdTablePtr = hiddenCmdTablePtr;
1208 }
1209
1210 /*
1211 * It is an error to move an exposed command to a hidden command with
1212 * hiddenCmdToken if a hidden command with the name hiddenCmdToken already
1213 * exists.
1214 */
1215
1216 hPtr = Tcl_CreateHashEntry(hiddenCmdTablePtr, hiddenCmdToken, &new);
1217 if (!new) {
1218 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1219 "hidden command named \"", hiddenCmdToken, "\" already exists",
1220 (char *) NULL);
1221 return TCL_ERROR;
1222 }
1223
1224 /*
1225 * Nb : This code is currently 'like' a rename to a specialy set apart
1226 * name table. Changes here and in TclRenameCommand must
1227 * be kept in synch untill the common parts are actually
1228 * factorized out.
1229 */
1230
1231 /*
1232 * Remove the hash entry for the command from the interpreter command
1233 * table. This is like deleting the command, so bump its command epoch;
1234 * this invalidates any cached references that point to the command.
1235 */
1236
1237 if (cmdPtr->hPtr != NULL) {
1238 Tcl_DeleteHashEntry(cmdPtr->hPtr);
1239 cmdPtr->hPtr = (Tcl_HashEntry *) NULL;
1240 cmdPtr->cmdEpoch++;
1241 }
1242
1243 /*
1244 * Now link the hash table entry with the command structure.
1245 * We ensured above that the nsPtr was right.
1246 */
1247
1248 cmdPtr->hPtr = hPtr;
1249 Tcl_SetHashValue(hPtr, (ClientData) cmdPtr);
1250
1251 /*
1252 * If the command being hidden has a compile procedure, increment the
1253 * interpreter's compileEpoch to invalidate its compiled code. This
1254 * makes sure that we don't later try to execute old code compiled with
1255 * command-specific (i.e., inline) bytecodes for the now-hidden
1256 * command. This field is checked in Tcl_EvalObj and ObjInterpProc,
1257 * and code whose compilation epoch doesn't match is recompiled.
1258 */
1259
1260 if (cmdPtr->compileProc != NULL) {
1261 iPtr->compileEpoch++;
1262 }
1263 return TCL_OK;
1264 }
1265
1266 /*
1267 *----------------------------------------------------------------------
1268 *
1269 * Tcl_ExposeCommand --
1270 *
1271 * Makes a previously hidden command callable from inside the
1272 * interpreter instead of only by its ancestors.
1273 *
1274 * Results:
1275 * A standard Tcl result. If an error occurs, a message is left
1276 * in the interp's result.
1277 *
1278 * Side effects:
1279 * Moves commands from one hash table to another.
1280 *
1281 *----------------------------------------------------------------------
1282 */
1283
1284 int
1285 Tcl_ExposeCommand(interp, hiddenCmdToken, cmdName)
1286 Tcl_Interp *interp; /* Interpreter in which to make command
1287 * callable. */
1288 CONST char *hiddenCmdToken; /* Name of hidden command. */
1289 CONST char *cmdName; /* Name of to-be-exposed command. */
1290 {
1291 Interp *iPtr = (Interp *) interp;
1292 Command *cmdPtr;
1293 Namespace *nsPtr;
1294 Tcl_HashEntry *hPtr;
1295 Tcl_HashTable *hiddenCmdTablePtr;
1296 int new;
1297
1298 if (iPtr->flags & DELETED) {
1299 /*
1300 * The interpreter is being deleted. Do not create any new
1301 * structures, because it is not safe to modify the interpreter.
1302 */
1303
1304 return TCL_ERROR;
1305 }
1306
1307 /*
1308 * Check that we have a regular name for the command
1309 * (that the user is not trying to do an expose and a rename
1310 * (to another namespace) at the same time)
1311 */
1312
1313 if (strstr(cmdName, "::") != NULL) {
1314 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1315 "can not expose to a namespace ",
1316 "(use expose to toplevel, then rename)",
1317 (char *) NULL);
1318 return TCL_ERROR;
1319 }
1320
1321 /*
1322 * Get the command from the hidden command table:
1323 */
1324
1325 hPtr = NULL;
1326 hiddenCmdTablePtr = iPtr->hiddenCmdTablePtr;
1327 if (hiddenCmdTablePtr != NULL) {
1328 hPtr = Tcl_FindHashEntry(hiddenCmdTablePtr, hiddenCmdToken);
1329 }
1330 if (hPtr == (Tcl_HashEntry *) NULL) {
1331 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1332 "unknown hidden command \"", hiddenCmdToken,
1333 "\"", (char *) NULL);
1334 return TCL_ERROR;
1335 }
1336 cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
1337
1338
1339 /*
1340 * Check that we have a true global namespace
1341 * command (enforced by Tcl_HideCommand() but let's double
1342 * check. (If it was not, we would not really know how to
1343 * handle it).
1344 */
1345 if ( cmdPtr->nsPtr != iPtr->globalNsPtr ) {
1346 /*
1347 * This case is theoritically impossible,
1348 * we might rather panic() than 'nicely' erroring out ?
1349 */
1350 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1351 "trying to expose a non global command name space command",
1352 (char *) NULL);
1353 return TCL_ERROR;
1354 }
1355
1356 /* This is the global table */
1357 nsPtr = cmdPtr->nsPtr;
1358
1359 /*
1360 * It is an error to overwrite an existing exposed command as a result
1361 * of exposing a previously hidden command.
1362 */
1363
1364 hPtr = Tcl_CreateHashEntry(&nsPtr->cmdTable, cmdName, &new);
1365 if (!new) {
1366 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1367 "exposed command \"", cmdName,
1368 "\" already exists", (char *) NULL);
1369 return TCL_ERROR;
1370 }
1371
1372 /*
1373 * Remove the hash entry for the command from the interpreter hidden
1374 * command table.
1375 */
1376
1377 if (cmdPtr->hPtr != NULL) {
1378 Tcl_DeleteHashEntry(cmdPtr->hPtr);
1379 cmdPtr->hPtr = NULL;
1380 }
1381
1382 /*
1383 * Now link the hash table entry with the command structure.
1384 * This is like creating a new command, so deal with any shadowing
1385 * of commands in the global namespace.
1386 */
1387
1388 cmdPtr->hPtr = hPtr;
1389
1390 Tcl_SetHashValue(hPtr, (ClientData) cmdPtr);
1391
1392 /*
1393 * Not needed as we are only in the global namespace
1394 * (but would be needed again if we supported namespace command hiding)
1395 *
1396 * TclResetShadowedCmdRefs(interp, cmdPtr);
1397 */
1398
1399
1400 /*
1401 * If the command being exposed has a compile procedure, increment
1402 * interpreter's compileEpoch to invalidate its compiled code. This
1403 * makes sure that we don't later try to execute old code compiled
1404 * assuming the command is hidden. This field is checked in Tcl_EvalObj
1405 * and ObjInterpProc, and code whose compilation epoch doesn't match is
1406 * recompiled.
1407 */
1408
1409 if (cmdPtr->compileProc != NULL) {
1410 iPtr->compileEpoch++;
1411 }
1412 return TCL_OK;
1413 }
1414
1415 /*
1416 *----------------------------------------------------------------------
1417 *
1418 * Tcl_CreateCommand --
1419 *
1420 * Define a new command in a command table.
1421 *
1422 * Results:
1423 * The return value is a token for the command, which can
1424 * be used in future calls to Tcl_GetCommandName.
1425 *
1426 * Side effects:
1427 * If a command named cmdName already exists for interp, it is deleted.
1428 * In the future, when cmdName is seen as the name of a command by
1429 * Tcl_Eval, proc will be called. To support the bytecode interpreter,
1430 * the command is created with a wrapper Tcl_ObjCmdProc
1431 * (TclInvokeStringCommand) that eventially calls proc. When the
1432 * command is deleted from the table, deleteProc will be called.
1433 * See the manual entry for details on the calling sequence.
1434 *
1435 *----------------------------------------------------------------------
1436 */
1437
1438 Tcl_Command
1439 Tcl_CreateCommand(interp, cmdName, proc, clientData, deleteProc)
1440 Tcl_Interp *interp; /* Token for command interpreter returned by
1441 * a previous call to Tcl_CreateInterp. */
1442 CONST char *cmdName; /* Name of command. If it contains namespace
1443 * qualifiers, the new command is put in the
1444 * specified namespace; otherwise it is put
1445 * in the global namespace. */
1446 Tcl_CmdProc *proc; /* Procedure to associate with cmdName. */
1447 ClientData clientData; /* Arbitrary value passed to string proc. */
1448 Tcl_CmdDeleteProc *deleteProc;
1449 /* If not NULL, gives a procedure to call
1450 * when this command is deleted. */
1451 {
1452 Interp *iPtr = (Interp *) interp;
1453 ImportRef *oldRefPtr = NULL;
1454 Namespace *nsPtr, *dummy1, *dummy2;
1455 Command *cmdPtr, *refCmdPtr;
1456 Tcl_HashEntry *hPtr;
1457 CONST char *tail;
1458 int new;
1459 ImportedCmdData *dataPtr;
1460
1461 if (iPtr->flags & DELETED) {
1462 /*
1463 * The interpreter is being deleted. Don't create any new
1464 * commands; it's not safe to muck with the interpreter anymore.
1465 */
1466
1467 return (Tcl_Command) NULL;
1468 }
1469
1470 /*
1471 * Determine where the command should reside. If its name contains
1472 * namespace qualifiers, we put it in the specified namespace;
1473 * otherwise, we always put it in the global namespace.
1474 */
1475
1476 if (strstr(cmdName, "::") != NULL) {
1477 TclGetNamespaceForQualName(interp, cmdName, (Namespace *) NULL,
1478 CREATE_NS_IF_UNKNOWN, &nsPtr, &dummy1, &dummy2, &tail);
1479 if ((nsPtr == NULL) || (tail == NULL)) {
1480 return (Tcl_Command) NULL;
1481 }
1482 } else {
1483 nsPtr = iPtr->globalNsPtr;
1484 tail = cmdName;
1485 }
1486
1487 hPtr = Tcl_CreateHashEntry(&nsPtr->cmdTable, tail, &new);
1488 if (!new) {
1489 /*
1490 * Command already exists. Delete the old one.
1491 * Be careful to preserve any existing import links so we can
1492 * restore them down below. That way, you can redefine a
1493 * command and its import status will remain intact.
1494 */
1495
1496 cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
1497 oldRefPtr = cmdPtr->importRefPtr;
1498 cmdPtr->importRefPtr = NULL;
1499
1500 Tcl_DeleteCommandFromToken(interp, (Tcl_Command) cmdPtr);
1501 hPtr = Tcl_CreateHashEntry(&nsPtr->cmdTable, tail, &new);
1502 if (!new) {
1503 /*
1504 * If the deletion callback recreated the command, just throw
1505 * away the new command (if we try to delete it again, we
1506 * could get stuck in an infinite loop).
1507 */
1508
1509 ckfree((char*) Tcl_GetHashValue(hPtr));
1510 }
1511 }
1512 cmdPtr = (Command *) ckalloc(sizeof(Command));
1513 Tcl_SetHashValue(hPtr, cmdPtr);
1514 cmdPtr->hPtr = hPtr;
1515 cmdPtr->nsPtr = nsPtr;
1516 cmdPtr->refCount = 1;
1517 cmdPtr->cmdEpoch = 0;
1518 cmdPtr->compileProc = (CompileProc *) NULL;
1519 cmdPtr->objProc = TclInvokeStringCommand;
1520 cmdPtr->objClientData = (ClientData) cmdPtr;
1521 cmdPtr->proc = proc;
1522 cmdPtr->clientData = clientData;
1523 cmdPtr->deleteProc = deleteProc;
1524 cmdPtr->deleteData = clientData;
1525 cmdPtr->flags = 0;
1526 cmdPtr->importRefPtr = NULL;
1527 cmdPtr->tracePtr = NULL;
1528
1529 /*
1530 * Plug in any existing import references found above. Be sure
1531 * to update all of these references to point to the new command.
1532 */
1533
1534 if (oldRefPtr != NULL) {
1535 cmdPtr->importRefPtr = oldRefPtr;
1536 while (oldRefPtr != NULL) {
1537 refCmdPtr = oldRefPtr->importedCmdPtr;
1538 dataPtr = (ImportedCmdData*)refCmdPtr->objClientData;
1539 dataPtr->realCmdPtr = cmdPtr;
1540 oldRefPtr = oldRefPtr->nextPtr;
1541 }
1542 }
1543
1544 /*
1545 * We just created a command, so in its namespace and all of its parent
1546 * namespaces, it may shadow global commands with the same name. If any
1547 * shadowed commands are found, invalidate all cached command references
1548 * in the affected namespaces.
1549 */
1550
1551 TclResetShadowedCmdRefs(interp, cmdPtr);
1552 return (Tcl_Command) cmdPtr;
1553 }
1554
1555 /*
1556 *----------------------------------------------------------------------
1557 *
1558 * Tcl_CreateObjCommand --
1559 *
1560 * Define a new object-based command in a command table.
1561 *
1562 * Results:
1563 * The return value is a token for the command, which can
1564 * be used in future calls to Tcl_GetCommandName.
1565 *
1566 * Side effects:
1567 * If no command named "cmdName" already exists for interp, one is
1568 * created. Otherwise, if a command does exist, then if the
1569 * object-based Tcl_ObjCmdProc is TclInvokeStringCommand, we assume
1570 * Tcl_CreateCommand was called previously for the same command and
1571 * just set its Tcl_ObjCmdProc to the argument "proc"; otherwise, we
1572 * delete the old command.
1573 *
1574 * In the future, during bytecode evaluation when "cmdName" is seen as
1575 * the name of a command by Tcl_EvalObj or Tcl_Eval, the object-based
1576 * Tcl_ObjCmdProc proc will be called. When the command is deleted from
1577 * the table, deleteProc will be called. See the manual entry for
1578 * details on the calling sequence.
1579 *
1580 *----------------------------------------------------------------------
1581 */
1582
1583 Tcl_Command
1584 Tcl_CreateObjCommand(interp, cmdName, proc, clientData, deleteProc)
1585 Tcl_Interp *interp; /* Token for command interpreter (returned
1586 * by previous call to Tcl_CreateInterp). */
1587 CONST char *cmdName; /* Name of command. If it contains namespace
1588 * qualifiers, the new command is put in the
1589 * specified namespace; otherwise it is put
1590 * in the global namespace. */
1591 Tcl_ObjCmdProc *proc; /* Object-based procedure to associate with
1592 * name. */
1593 ClientData clientData; /* Arbitrary value to pass to object
1594 * procedure. */
1595 Tcl_CmdDeleteProc *deleteProc;
1596 /* If not NULL, gives a procedure to call
1597 * when this command is deleted. */
1598 {
1599 Interp *iPtr = (Interp *) interp;
1600 ImportRef *oldRefPtr = NULL;
1601 Namespace *nsPtr, *dummy1, *dummy2;
1602 Command *cmdPtr, *refCmdPtr;
1603 Tcl_HashEntry *hPtr;
1604 CONST char *tail;
1605 int new;
1606 ImportedCmdData *dataPtr;
1607
1608 if (iPtr->flags & DELETED) {
1609 /*
1610 * The interpreter is being deleted. Don't create any new
1611 * commands; it's not safe to muck with the interpreter anymore.
1612 */
1613
1614 return (Tcl_Command) NULL;
1615 }
1616
1617 /*
1618 * Determine where the command should reside. If its name contains
1619 * namespace qualifiers, we put it in the specified namespace;
1620 * otherwise, we always put it in the global namespace.
1621 */
1622
1623 if (strstr(cmdName, "::") != NULL) {
1624 TclGetNamespaceForQualName(interp, cmdName, (Namespace *) NULL,
1625 CREATE_NS_IF_UNKNOWN, &nsPtr, &dummy1, &dummy2, &tail);
1626 if ((nsPtr == NULL) || (tail == NULL)) {
1627 return (Tcl_Command) NULL;
1628 }
1629 } else {
1630 nsPtr = iPtr->globalNsPtr;
1631 tail = cmdName;
1632 }
1633
1634 hPtr = Tcl_CreateHashEntry(&nsPtr->cmdTable, tail, &new);
1635 if (!new) {
1636 cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
1637
1638 /*
1639 * Command already exists. If its object-based Tcl_ObjCmdProc is
1640 * TclInvokeStringCommand, we just set its Tcl_ObjCmdProc to the
1641 * argument "proc". Otherwise, we delete the old command.
1642 */
1643
1644 if (cmdPtr->objProc == TclInvokeStringCommand) {
1645 cmdPtr->objProc = proc;
1646 cmdPtr->objClientData = clientData;
1647 cmdPtr->deleteProc = deleteProc;
1648 cmdPtr->deleteData = clientData;
1649 return (Tcl_Command) cmdPtr;
1650 }
1651
1652 /*
1653 * Otherwise, we delete the old command. Be careful to preserve
1654 * any existing import links so we can restore them down below.
1655 * That way, you can redefine a command and its import status
1656 * will remain intact.
1657 */
1658
1659 oldRefPtr = cmdPtr->importRefPtr;
1660 cmdPtr->importRefPtr = NULL;
1661
1662 Tcl_DeleteCommandFromToken(interp, (Tcl_Command) cmdPtr);
1663 hPtr = Tcl_CreateHashEntry(&nsPtr->cmdTable, tail, &new);
1664 if (!new) {
1665 /*
1666 * If the deletion callback recreated the command, just throw
1667 * away the new command (if we try to delete it again, we
1668 * could get stuck in an infinite loop).
1669 */
1670
1671 ckfree((char *) Tcl_GetHashValue(hPtr));
1672 }
1673 }
1674 cmdPtr = (Command *) ckalloc(sizeof(Command));
1675 Tcl_SetHashValue(hPtr, cmdPtr);
1676 cmdPtr->hPtr = hPtr;
1677 cmdPtr->nsPtr = nsPtr;
1678 cmdPtr->refCount = 1;
1679 cmdPtr->cmdEpoch = 0;
1680 cmdPtr->compileProc = (CompileProc *) NULL;
1681 cmdPtr->objProc = proc;
1682 cmdPtr->objClientData = clientData;
1683 cmdPtr->proc = TclInvokeObjectCommand;
1684 cmdPtr->clientData = (ClientData) cmdPtr;
1685 cmdPtr->deleteProc = deleteProc;
1686 cmdPtr->deleteData = clientData;
1687 cmdPtr->flags = 0;
1688 cmdPtr->importRefPtr = NULL;
1689 cmdPtr->tracePtr = NULL;
1690
1691 /*
1692 * Plug in any existing import references found above. Be sure
1693 * to update all of these references to point to the new command.
1694 */
1695
1696 if (oldRefPtr != NULL) {
1697 cmdPtr->importRefPtr = oldRefPtr;
1698 while (oldRefPtr != NULL) {
1699 refCmdPtr = oldRefPtr->importedCmdPtr;
1700 dataPtr = (ImportedCmdData*)refCmdPtr->objClientData;
1701 dataPtr->realCmdPtr = cmdPtr;
1702 oldRefPtr = oldRefPtr->nextPtr;
1703 }
1704 }
1705
1706 /*
1707 * We just created a command, so in its namespace and all of its parent
1708 * namespaces, it may shadow global commands with the same name. If any
1709 * shadowed commands are found, invalidate all cached command references
1710 * in the affected namespaces.
1711 */
1712
1713 TclResetShadowedCmdRefs(interp, cmdPtr);
1714 return (Tcl_Command) cmdPtr;
1715 }
1716
1717 /*
1718 *----------------------------------------------------------------------
1719 *
1720 * TclInvokeStringCommand --
1721 *
1722 * "Wrapper" Tcl_ObjCmdProc used to call an existing string-based
1723 * Tcl_CmdProc if no object-based procedure exists for a command. A
1724 * pointer to this procedure is stored as the Tcl_ObjCmdProc in a
1725 * Command structure. It simply turns around and calls the string
1726 * Tcl_CmdProc in the Command structure.
1727 *
1728 * Results:
1729 * A standard Tcl object result value.
1730 *
1731 * Side effects:
1732 * Besides those side effects of the called Tcl_CmdProc,
1733 * TclInvokeStringCommand allocates and frees storage.
1734 *
1735 *----------------------------------------------------------------------
1736 */
1737
1738 int
1739 TclInvokeStringCommand(clientData, interp, objc, objv)
1740 ClientData clientData; /* Points to command's Command structure. */
1741 Tcl_Interp *interp; /* Current interpreter. */
1742 register int objc; /* Number of arguments. */
1743 Tcl_Obj *CONST objv[]; /* Argument objects. */
1744 {
1745 register Command *cmdPtr = (Command *) clientData;
1746 register int i;
1747 int result;
1748
1749 /*
1750 * This procedure generates an argv array for the string arguments. It
1751 * starts out with stack-allocated space but uses dynamically-allocated
1752 * storage if needed.
1753 */
1754
1755 #define NUM_ARGS 20
1756 CONST char *(argStorage[NUM_ARGS]);
1757 CONST char **argv = argStorage;
1758
1759 /*
1760 * Create the string argument array "argv". Make sure argv is large
1761 * enough to hold the objc arguments plus 1 extra for the zero
1762 * end-of-argv word.
1763 */
1764
1765 if ((objc + 1) > NUM_ARGS) {
1766 argv = (CONST char **) ckalloc((unsigned)(objc + 1) * sizeof(char *));
1767 }
1768
1769 for (i = 0; i < objc; i++) {
1770 argv[i] = Tcl_GetString(objv[i]);
1771 }
1772 argv[objc] = 0;
1773
1774 /*
1775 * Invoke the command's string-based Tcl_CmdProc.
1776 */
1777
1778 result = (*cmdPtr->proc)(cmdPtr->clientData, interp, objc, argv);
1779
1780 /*
1781 * Free the argv array if malloc'ed storage was used.
1782 */
1783
1784 if (argv != argStorage) {
1785 ckfree((char *) argv);
1786 }
1787 return result;
1788 #undef NUM_ARGS
1789 }
1790
1791 /*
1792 *----------------------------------------------------------------------
1793 *
1794 * TclInvokeObjectCommand --
1795 *
1796 * "Wrapper" Tcl_CmdProc used to call an existing object-based
1797 * Tcl_ObjCmdProc if no string-based procedure exists for a command.
1798 * A pointer to this procedure is stored as the Tcl_CmdProc in a
1799 * Command structure. It simply turns around and calls the object
1800 * Tcl_ObjCmdProc in the Command structure.
1801 *
1802 * Results:
1803 * A standard Tcl string result value.
1804 *
1805 * Side effects:
1806 * Besides those side effects of the called Tcl_CmdProc,
1807 * TclInvokeStringCommand allocates and frees storage.
1808 *
1809 *----------------------------------------------------------------------
1810 */
1811
1812 int
1813 TclInvokeObjectCommand(clientData, interp, argc, argv)
1814 ClientData clientData; /* Points to command's Command structure. */
1815 Tcl_Interp *interp; /* Current interpreter. */
1816 int argc; /* Number of arguments. */
1817 register CONST char **argv; /* Argument strings. */
1818 {
1819 Command *cmdPtr = (Command *) clientData;
1820 register Tcl_Obj *objPtr;
1821 register int i;
1822 int length, result;
1823
1824 /*
1825 * This procedure generates an objv array for object arguments that hold
1826 * the argv strings. It starts out with stack-allocated space but uses
1827 * dynamically-allocated storage if needed.
1828 */
1829
1830 #define NUM_ARGS 20
1831 Tcl_Obj *(argStorage[NUM_ARGS]);
1832 register Tcl_Obj **objv = argStorage;
1833
1834 /*
1835 * Create the object argument array "objv". Make sure objv is large
1836 * enough to hold the objc arguments plus 1 extra for the zero
1837 * end-of-objv word.
1838 */
1839
1840 if (argc > NUM_ARGS) {
1841 objv = (Tcl_Obj **)
1842 ckalloc((unsigned)(argc * sizeof(Tcl_Obj *)));
1843 }
1844
1845 for (i = 0; i < argc; i++) {
1846 length = strlen(argv[i]);
1847 TclNewObj(objPtr);
1848 TclInitStringRep(objPtr, argv[i], length);
1849 Tcl_IncrRefCount(objPtr);
1850 objv[i] = objPtr;
1851 }
1852
1853 /*
1854 * Invoke the command's object-based Tcl_ObjCmdProc.
1855 */
1856
1857 result = (*cmdPtr->objProc)(cmdPtr->objClientData, interp, argc, objv);
1858
1859 /*
1860 * Move the interpreter's object result to the string result,
1861 * then reset the object result.
1862 */
1863
1864 Tcl_SetResult(interp, TclGetString(Tcl_GetObjResult(interp)),
1865 TCL_VOLATILE);
1866
1867 /*
1868 * Decrement the ref counts for the argument objects created above,
1869 * then free the objv array if malloc'ed storage was used.
1870 */
1871
1872 for (i = 0; i < argc; i++) {
1873 objPtr = objv[i];
1874 Tcl_DecrRefCount(objPtr);
1875 }
1876 if (objv != argStorage) {
1877 ckfree((char *) objv);
1878 }
1879 return result;
1880 #undef NUM_ARGS
1881 }
1882
1883 /*
1884 *----------------------------------------------------------------------
1885 *
1886 * TclRenameCommand --
1887 *
1888 * Called to give an existing Tcl command a different name. Both the
1889 * old command name and the new command name can have "::" namespace
1890 * qualifiers. If the new command has a different namespace context,
1891 * the command will be moved to that namespace and will execute in
1892 * the context of that new namespace.
1893 *
1894 * If the new command name is NULL or the null string, the command is
1895 * deleted.
1896 *
1897 * Results:
1898 * Returns TCL_OK if successful, and TCL_ERROR if anything goes wrong.
1899 *
1900 * Side effects:
1901 * If anything goes wrong, an error message is returned in the
1902 * interpreter's result object.
1903 *
1904 *----------------------------------------------------------------------
1905 */
1906
1907 int
1908 TclRenameCommand(interp, oldName, newName)
1909 Tcl_Interp *interp; /* Current interpreter. */
1910 char *oldName; /* Existing command name. */
1911 char *newName; /* New command name. */
1912 {
1913 Interp *iPtr = (Interp *) interp;
1914 CONST char *newTail;
1915 Namespace *cmdNsPtr, *newNsPtr, *dummy1, *dummy2;
1916 Tcl_Command cmd;
1917 Command *cmdPtr;
1918 Tcl_HashEntry *hPtr, *oldHPtr;
1919 int new, result;
1920 Tcl_Obj* oldFullName;
1921 Tcl_DString newFullName;
1922
1923 /*
1924 * Find the existing command. An error is returned if cmdName can't
1925 * be found.
1926 */
1927
1928 cmd = Tcl_FindCommand(interp, oldName, (Tcl_Namespace *) NULL,
1929 /*flags*/ 0);
1930 cmdPtr = (Command *) cmd;
1931 if (cmdPtr == NULL) {
1932 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), "can't ",
1933 ((newName == NULL)||(*newName == '\0'))? "delete":"rename",
1934 " \"", oldName, "\": command doesn't exist", (char *) NULL);
1935 return TCL_ERROR;
1936 }
1937 cmdNsPtr = cmdPtr->nsPtr;
1938 oldFullName = Tcl_NewObj();
1939 Tcl_IncrRefCount( oldFullName );
1940 Tcl_GetCommandFullName( interp, cmd, oldFullName );
1941
1942 /*
1943 * If the new command name is NULL or empty, delete the command. Do this
1944 * with Tcl_DeleteCommandFromToken, since we already have the command.
1945 */
1946
1947 if ((newName == NULL) || (*newName == '\0')) {
1948 Tcl_DeleteCommandFromToken(interp, cmd);
1949 result = TCL_OK;
1950 goto done;
1951 }
1952
1953 /*
1954 * Make sure that the destination command does not already exist.
1955 * The rename operation is like creating a command, so we should
1956 * automatically create the containing namespaces just like
1957 * Tcl_CreateCommand would.
1958 */
1959
1960 TclGetNamespaceForQualName(interp, newName, (Namespace *) NULL,
1961 CREATE_NS_IF_UNKNOWN, &newNsPtr, &dummy1, &dummy2, &newTail);
1962
1963 if ((newNsPtr == NULL) || (newTail == NULL)) {
1964 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1965 "can't rename to \"", newName, "\": bad command name",
1966 (char *) NULL);
1967 result = TCL_ERROR;
1968 goto done;
1969 }
1970 if (Tcl_FindHashEntry(&newNsPtr->cmdTable, newTail) != NULL) {
1971 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1972 "can't rename to \"", newName,
1973 "\": command already exists", (char *) NULL);
1974 result = TCL_ERROR;
1975 goto done;
1976 }
1977
1978 /*
1979 * Warning: any changes done in the code here are likely
1980 * to be needed in Tcl_HideCommand() code too.
1981 * (until the common parts are extracted out) --dl
1982 */
1983
1984 /*
1985 * Put the command in the new namespace so we can check for an alias
1986 * loop. Since we are adding a new command to a namespace, we must
1987 * handle any shadowing of the global commands that this might create.
1988 */
1989
1990 oldHPtr = cmdPtr->hPtr;
1991 hPtr = Tcl_CreateHashEntry(&newNsPtr->cmdTable, newTail, &new);
1992 Tcl_SetHashValue(hPtr, (ClientData) cmdPtr);
1993 cmdPtr->hPtr = hPtr;
1994 cmdPtr->nsPtr = newNsPtr;
1995 TclResetShadowedCmdRefs(interp, cmdPtr);
1996
1997 /*
1998 * Now check for an alias loop. If we detect one, put everything back
1999 * the way it was and report the error.
2000 */
2001
2002 result = TclPreventAliasLoop(interp, interp, (Tcl_Command) cmdPtr);
2003 if (result != TCL_OK) {
2004 Tcl_DeleteHashEntry(cmdPtr->hPtr);
2005 cmdPtr->hPtr = oldHPtr;
2006 cmdPtr->nsPtr = cmdNsPtr;
2007 goto done;
2008 }
2009
2010 /*
2011 * Script for rename traces can delete the command "oldName".
2012 * Therefore increment the reference count for cmdPtr so that
2013 * it's Command structure is freed only towards the end of this
2014 * function by calling TclCleanupCommand.
2015 *
2016 * The trace procedure needs to get a fully qualified name for
2017 * old and new commands [Tcl bug #651271], or else there's no way
2018 * for the trace procedure to get the namespace from which the old
2019 * command is being renamed!
2020 */
2021
2022 Tcl_DStringInit( &newFullName );
2023 Tcl_DStringAppend( &newFullName, newNsPtr->fullName, -1 );
2024 if ( newNsPtr != iPtr->globalNsPtr ) {
2025 Tcl_DStringAppend( &newFullName, "::", 2 );
2026 }
2027 Tcl_DStringAppend( &newFullName, newTail, -1 );
2028 cmdPtr->refCount++;
2029 CallCommandTraces( iPtr, cmdPtr,
2030 Tcl_GetString( oldFullName ),
2031 Tcl_DStringValue( &newFullName ),
2032 TCL_TRACE_RENAME);
2033 Tcl_DStringFree( &newFullName );
2034
2035 /*
2036 * The new command name is okay, so remove the command from its
2037 * current namespace. This is like deleting the command, so bump
2038 * the cmdEpoch to invalidate any cached references to the command.
2039 */
2040
2041 Tcl_DeleteHashEntry(oldHPtr);
2042 cmdPtr->cmdEpoch++;
2043
2044 /*
2045 * If the command being renamed has a compile procedure, increment the
2046 * interpreter's compileEpoch to invalidate its compiled code. This
2047 * makes sure that we don't later try to execute old code compiled for
2048 * the now-renamed command.
2049 */
2050
2051 if (cmdPtr->compileProc != NULL) {
2052 iPtr->compileEpoch++;
2053 }
2054
2055 /*
2056 * Now free the Command structure, if the "oldName" command has
2057 * been deleted by invocation of rename traces.
2058 */
2059 TclCleanupCommand(cmdPtr);
2060 result = TCL_OK;
2061
2062 done:
2063 TclDecrRefCount( oldFullName );
2064 return result;
2065 }
2066
2067 /*
2068 *----------------------------------------------------------------------
2069 *
2070 * Tcl_SetCommandInfo --
2071 *
2072 * Modifies various information about a Tcl command. Note that
2073 * this procedure will not change a command's namespace; use
2074 * Tcl_RenameCommand to do that. Also, the isNativeObjectProc
2075 * member of *infoPtr is ignored.
2076 *
2077 * Results:
2078 * If cmdName exists in interp, then the information at *infoPtr
2079 * is stored with the command in place of the current information
2080 * and 1 is returned. If the command doesn't exist then 0 is
2081 * returned.
2082 *
2083 * Side effects:
2084 * None.
2085 *
2086 *----------------------------------------------------------------------
2087 */
2088
2089 int
2090 Tcl_SetCommandInfo(interp, cmdName, infoPtr)
2091 Tcl_Interp *interp; /* Interpreter in which to look
2092 * for command. */
2093 CONST char *cmdName; /* Name of desired command. */
2094 CONST Tcl_CmdInfo *infoPtr; /* Where to find information
2095 * to store in the command. */
2096 {
2097 Tcl_Command cmd;
2098
2099 cmd = Tcl_FindCommand(interp, cmdName, (Tcl_Namespace *) NULL,
2100 /*flags*/ 0);
2101
2102 return Tcl_SetCommandInfoFromToken( cmd, infoPtr );
2103
2104 }
2105
2106 /*
2107 *----------------------------------------------------------------------
2108 *
2109 * Tcl_SetCommandInfoFromToken --
2110 *
2111 * Modifies various information about a Tcl command. Note that
2112 * this procedure will not change a command's namespace; use
2113 * Tcl_RenameCommand to do that. Also, the isNativeObjectProc
2114 * member of *infoPtr is ignored.
2115 *
2116 * Results:
2117 * If cmdName exists in interp, then the information at *infoPtr
2118 * is stored with the command in place of the current information
2119 * and 1 is returned. If the command doesn't exist then 0 is
2120 * returned.
2121 *
2122 * Side effects:
2123 * None.
2124 *
2125 *----------------------------------------------------------------------
2126 */
2127
2128 int
2129 Tcl_SetCommandInfoFromToken( cmd, infoPtr )
2130 Tcl_Command cmd;
2131 CONST Tcl_CmdInfo* infoPtr;
2132 {
2133 Command* cmdPtr; /* Internal representation of the command */
2134
2135 if (cmd == (Tcl_Command) NULL) {
2136 return 0;
2137 }
2138
2139 /*
2140 * The isNativeObjectProc and nsPtr members of *infoPtr are ignored.
2141 */
2142
2143 cmdPtr = (Command *) cmd;
2144 cmdPtr->proc = infoPtr->proc;
2145 cmdPtr->clientData = infoPtr->clientData;
2146 if (infoPtr->objProc == (Tcl_ObjCmdProc *) NULL) {
2147 cmdPtr->objProc = TclInvokeStringCommand;
2148 cmdPtr->objClientData = (ClientData) cmdPtr;
2149 } else {
2150 cmdPtr->objProc = infoPtr->objProc;
2151 cmdPtr->objClientData = infoPtr->objClientData;
2152 }
2153 cmdPtr->deleteProc = infoPtr->deleteProc;
2154 cmdPtr->deleteData = infoPtr->deleteData;
2155 return 1;
2156 }
2157
2158 /*
2159 *----------------------------------------------------------------------
2160 *
2161 * Tcl_GetCommandInfo --
2162 *
2163 * Returns various information about a Tcl command.
2164 *
2165 * Results:
2166 * If cmdName exists in interp, then *infoPtr is modified to
2167 * hold information about cmdName and 1 is returned. If the
2168 * command doesn't exist then 0 is returned and *infoPtr isn't
2169 * modified.
2170 *
2171 * Side effects:
2172 * None.
2173 *
2174 *----------------------------------------------------------------------
2175 */
2176
2177 int
2178 Tcl_GetCommandInfo(interp, cmdName, infoPtr)
2179 Tcl_Interp *interp; /* Interpreter in which to look
2180 * for command. */
2181 CONST char *cmdName; /* Name of desired command. */
2182 Tcl_CmdInfo *infoPtr; /* Where to store information about
2183 * command. */
2184 {
2185 Tcl_Command cmd;
2186
2187 cmd = Tcl_FindCommand(interp, cmdName, (Tcl_Namespace *) NULL,
2188 /*flags*/ 0);
2189
2190 return Tcl_GetCommandInfoFromToken( cmd, infoPtr );
2191
2192 }
2193
2194 /*
2195 *----------------------------------------------------------------------
2196 *
2197 * Tcl_GetCommandInfoFromToken --
2198 *
2199 * Returns various information about a Tcl command.
2200 *
2201 * Results:
2202 * Copies information from the command identified by 'cmd' into
2203 * a caller-supplied structure and returns 1. If the 'cmd' is
2204 * NULL, leaves the structure untouched and returns 0.
2205 *
2206 * Side effects:
2207 * None.
2208 *
2209 *----------------------------------------------------------------------
2210 */
2211
2212 int
2213 Tcl_GetCommandInfoFromToken( cmd, infoPtr )
2214 Tcl_Command cmd;
2215 Tcl_CmdInfo* infoPtr;
2216 {
2217
2218 Command* cmdPtr; /* Internal representation of the command */
2219
2220 if ( cmd == (Tcl_Command) NULL ) {
2221 return 0;
2222 }
2223
2224 /*
2225 * Set isNativeObjectProc 1 if objProc was registered by a call to
2226 * Tcl_CreateObjCommand. Otherwise set it to 0.
2227 */
2228
2229 cmdPtr = (Command *) cmd;
2230 infoPtr->isNativeObjectProc =
2231 (cmdPtr->objProc != TclInvokeStringCommand);
2232 infoPtr->objProc = cmdPtr->objProc;
2233 infoPtr->objClientData = cmdPtr->objClientData;
2234 infoPtr->proc = cmdPtr->proc;
2235 infoPtr->clientData = cmdPtr->clientData;
2236 infoPtr->deleteProc = cmdPtr->deleteProc;
2237 infoPtr->deleteData = cmdPtr->deleteData;
2238 infoPtr->namespacePtr = (Tcl_Namespace *) cmdPtr->nsPtr;
2239
2240 return 1;
2241
2242 }
2243
2244 /*
2245 *----------------------------------------------------------------------
2246 *
2247 * Tcl_GetCommandName --
2248 *
2249 * Given a token returned by Tcl_CreateCommand, this procedure
2250 * returns the current name of the command (which may have changed
2251 * due to renaming).
2252 *
2253 * Results:
2254 * The return value is the name of the given command.
2255 *
2256 * Side effects:
2257 * None.
2258 *
2259 *----------------------------------------------------------------------
2260 */
2261
2262 CONST char *
2263 Tcl_GetCommandName(interp, command)
2264 Tcl_Interp *interp; /* Interpreter containing the command. */
2265 Tcl_Command command; /* Token for command returned by a previous
2266 * call to Tcl_CreateCommand. The command
2267 * must not have been deleted. */
2268 {
2269 Command *cmdPtr = (Command *) command;
2270
2271 if ((cmdPtr == NULL) || (cmdPtr->hPtr == NULL)) {
2272
2273 /*
2274 * This should only happen if command was "created" after the
2275 * interpreter began to be deleted, so there isn't really any
2276 * command. Just return an empty string.
2277 */
2278
2279 return "";
2280 }
2281 return Tcl_GetHashKey(cmdPtr->hPtr->tablePtr, cmdPtr->hPtr);
2282 }
2283
2284 /*
2285 *----------------------------------------------------------------------
2286 *
2287 * Tcl_GetCommandFullName --
2288 *
2289 * Given a token returned by, e.g., Tcl_CreateCommand or
2290 * Tcl_FindCommand, this procedure appends to an object the command's
2291 * full name, qualified by a sequence of parent namespace names. The
2292 * command's fully-qualified name may have changed due to renaming.
2293 *
2294 * Results:
2295 * None.
2296 *
2297 * Side effects:
2298 * The command's fully-qualified name is appended to the string
2299 * representation of objPtr.
2300 *
2301 *----------------------------------------------------------------------
2302 */
2303
2304 void
2305 Tcl_GetCommandFullName(interp, command, objPtr)
2306 Tcl_Interp *interp; /* Interpreter containing the command. */
2307 Tcl_Command command; /* Token for command returned by a previous
2308 * call to Tcl_CreateCommand. The command
2309 * must not have been deleted. */
2310 Tcl_Obj *objPtr; /* Points to the object onto which the
2311 * command's full name is appended. */
2312
2313 {
2314 Interp *iPtr = (Interp *) interp;
2315 register Command *cmdPtr = (Command *) command;
2316 char *name;
2317
2318 /*
2319 * Add the full name of the containing namespace, followed by the "::"
2320 * separator, and the command name.
2321 */
2322
2323 if (cmdPtr != NULL) {
2324 if (cmdPtr->nsPtr != NULL) {
2325 Tcl_AppendToObj(objPtr, cmdPtr->nsPtr->fullName, -1);
2326 if (cmdPtr->nsPtr != iPtr->globalNsPtr) {
2327 Tcl_AppendToObj(objPtr, "::", 2);
2328 }
2329 }
2330 if (cmdPtr->hPtr != NULL) {
2331 name = Tcl_GetHashKey(cmdPtr->hPtr->tablePtr, cmdPtr->hPtr);
2332 Tcl_AppendToObj(objPtr, name, -1);
2333 }
2334 }
2335 }
2336
2337 /*
2338 *----------------------------------------------------------------------
2339 *
2340 * Tcl_DeleteCommand --
2341 *
2342 * Remove the given command from the given interpreter.
2343 *
2344 * Results:
2345 * 0 is returned if the command was deleted successfully.
2346 * -1 is returned if there didn't exist a command by that name.
2347 *
2348 * Side effects:
2349 * cmdName will no longer be recognized as a valid command for
2350 * interp.
2351 *
2352 *----------------------------------------------------------------------
2353 */
2354
2355 int
2356 Tcl_DeleteCommand(interp, cmdName)
2357 Tcl_Interp *interp; /* Token for command interpreter (returned
2358 * by a previous Tcl_CreateInterp call). */
2359 CONST char *cmdName; /* Name of command to remove. */
2360 {
2361 Tcl_Command cmd;
2362
2363 /*
2364 * Find the desired command and delete it.
2365 */
2366
2367 cmd = Tcl_FindCommand(interp, cmdName, (Tcl_Namespace *) NULL,
2368 /*flags*/ 0);
2369 if (cmd == (Tcl_Command) NULL) {
2370 return -1;
2371 }
2372 return Tcl_DeleteCommandFromToken(interp, cmd);
2373 }
2374
2375 /*
2376 *----------------------------------------------------------------------
2377 *
2378 * Tcl_DeleteCommandFromToken --
2379 *
2380 * Removes the given command from the given interpreter. This procedure
2381 * resembles Tcl_DeleteCommand, but takes a Tcl_Command token instead
2382 * of a command name for efficiency.
2383 *
2384 * Results:
2385 * 0 is returned if the command was deleted successfully.
2386 * -1 is returned if there didn't exist a command by that name.
2387 *
2388 * Side effects:
2389 * The command specified by "cmd" will no longer be recognized as a
2390 * valid command for "interp".
2391 *
2392 *----------------------------------------------------------------------
2393 */
2394
2395 int
2396 Tcl_DeleteCommandFromToken(interp, cmd)
2397 Tcl_Interp *interp; /* Token for command interpreter returned by
2398 * a previous call to Tcl_CreateInterp. */
2399 Tcl_Command cmd; /* Token for command to delete. */
2400 {
2401 Interp *iPtr = (Interp *) interp;
2402 Command *cmdPtr = (Command *) cmd;
2403 ImportRef *refPtr, *nextRefPtr;
2404 Tcl_Command importCmd;
2405
2406 /*
2407 * The code here is tricky. We can't delete the hash table entry
2408 * before invoking the deletion callback because there are cases
2409 * where the deletion callback needs to invoke the command (e.g.
2410 * object systems such as OTcl). However, this means that the
2411 * callback could try to delete or rename the command. The deleted
2412 * flag allows us to detect these cases and skip nested deletes.
2413 */
2414
2415 if (cmdPtr->flags & CMD_IS_DELETED) {
2416 /*
2417 * Another deletion is already in progress. Remove the hash
2418 * table entry now, but don't invoke a callback or free the
2419 * command structure.
2420 */
2421
2422 Tcl_DeleteHashEntry(cmdPtr->hPtr);
2423 cmdPtr->hPtr = NULL;
2424 return 0;
2425 }
2426
2427 /*
2428 * We must delete this command, even though both traces and
2429 * delete procs may try to avoid this (renaming the command etc).
2430 * Also traces and delete procs may try to delete the command
2431 * themsevles. This flag declares that a delete is in progress
2432 * and that recursive deletes should be ignored.
2433 */
2434 cmdPtr->flags |= CMD_IS_DELETED;
2435
2436 /*
2437 * Call trace procedures for the command being deleted. Then delete
2438 * its traces.
2439 */
2440
2441 if (cmdPtr->tracePtr != NULL) {
2442 CommandTrace *tracePtr;
2443 CallCommandTraces(iPtr,cmdPtr,NULL,NULL,TCL_TRACE_DELETE);
2444 /* Now delete these traces */
2445 tracePtr = cmdPtr->tracePtr;
2446 while (tracePtr != NULL) {
2447 CommandTrace *nextPtr = tracePtr->nextPtr;
2448 if ((--tracePtr->refCount) <= 0) {
2449 ckfree((char*)tracePtr);
2450 }
2451 tracePtr = nextPtr;
2452 }
2453 cmdPtr->tracePtr = NULL;
2454 }
2455
2456 /*
2457 * If the command being deleted has a compile procedure, increment the
2458 * interpreter's compileEpoch to invalidate its compiled code. This
2459 * makes sure that we don't later try to execute old code compiled with
2460 * command-specific (i.e., inline) bytecodes for the now-deleted
2461 * command. This field is checked in Tcl_EvalObj and ObjInterpProc, and
2462 * code whose compilation epoch doesn't match is recompiled.
2463 */
2464
2465 if (cmdPtr->compileProc != NULL) {
2466 iPtr->compileEpoch++;
2467 }
2468
2469 if (cmdPtr->deleteProc != NULL) {
2470 /*
2471 * Delete the command's client data. If this was an imported command
2472 * created when a command was imported into a namespace, this client
2473 * data will be a pointer to a ImportedCmdData structure describing
2474 * the "real" command that this imported command refers to.
2475 */
2476
2477 /*
2478 * If you are getting a crash during the call to deleteProc and
2479 * cmdPtr->deleteProc is a pointer to the function free(), the
2480 * most likely cause is that your extension allocated memory
2481 * for the clientData argument to Tcl_CreateObjCommand() with
2482 * the ckalloc() macro and you are now trying to deallocate
2483 * this memory with free() instead of ckfree(). You should
2484 * pass a pointer to your own method that calls ckfree().
2485 */
2486
2487 (*cmdPtr->deleteProc)(cmdPtr->deleteData);
2488 }
2489
2490 /*
2491 * Bump the command epoch counter. This will invalidate all cached
2492 * references that point to this command.
2493 */
2494
2495 cmdPtr->cmdEpoch++;
2496
2497 /*
2498 * If this command was imported into other namespaces, then imported
2499 * commands were created that refer back to this command. Delete these
2500 * imported commands now.
2501 */
2502
2503 for (refPtr = cmdPtr->importRefPtr; refPtr != NULL;
2504 refPtr = nextRefPtr) {
2505 nextRefPtr = refPtr->nextPtr;
2506 importCmd = (Tcl_Command) refPtr->importedCmdPtr;
2507 Tcl_DeleteCommandFromToken(interp, importCmd);
2508 }
2509
2510 /*
2511 * Don't use hPtr to delete the hash entry here, because it's
2512 * possible that the deletion callback renamed the command.
2513 * Instead, use cmdPtr->hptr, and make sure that no-one else
2514 * has already deleted the hash entry.
2515 */
2516
2517 if (cmdPtr->hPtr != NULL) {
2518 Tcl_DeleteHashEntry(cmdPtr->hPtr);
2519 }
2520
2521 /*
2522 * Mark the Command structure as no longer valid. This allows
2523 * TclExecuteByteCode to recognize when a Command has logically been
2524 * deleted and a pointer to this Command structure cached in a CmdName
2525 * object is invalid. TclExecuteByteCode will look up the command again
2526 * in the interpreter's command hashtable.
2527 */
2528
2529 cmdPtr->objProc = NULL;
2530
2531 /*
2532 * Now free the Command structure, unless there is another reference to
2533 * it from a CmdName Tcl object in some ByteCode code sequence. In that
2534 * case, delay the cleanup until all references are either discarded
2535 * (when a ByteCode is freed) or replaced by a new reference (when a
2536 * cached CmdName Command reference is found to be invalid and
2537 * TclExecuteByteCode looks up the command in the command hashtable).
2538 */
2539
2540 TclCleanupCommand(cmdPtr);
2541 return 0;
2542 }
2543
2544 static char *
2545 CallCommandTraces(iPtr, cmdPtr, oldName, newName, flags)
2546 Interp *iPtr; /* Interpreter containing command. */
2547 Command *cmdPtr; /* Command whose traces are to be
2548 * invoked. */
2549 CONST char *oldName; /* Command's old name, or NULL if we
2550 * must get the name from cmdPtr */
2551 CONST char *newName; /* Command's new name, or NULL if
2552 * the command is not being renamed */
2553 int flags; /* Flags indicating the type of traces
2554 * to trigger, either TCL_TRACE_DELETE
2555 * or TCL_TRACE_RENAME. */
2556 {
2557 register CommandTrace *tracePtr;
2558 ActiveCommandTrace active;
2559 char *result;
2560 Tcl_Obj *oldNamePtr = NULL;
2561 int mask = (TCL_TRACE_DELETE | TCL_TRACE_RENAME); /* Safety */
2562
2563 flags &= mask;
2564
2565 if (cmdPtr->flags & CMD_TRACE_ACTIVE) {
2566 /*
2567 * While a rename trace is active, we will not process any more
2568 * rename traces; while a delete trace is active we will never
2569 * reach here -- because Tcl_DeleteCommandFromToken checks for the
2570 * condition (cmdPtr->flags & CMD_IS_DELETED) and returns immediately
2571 * when a command deletion is in progress. For all other traces,
2572 * delete traces will not be invoked but a call to TraceCommandProc
2573 * will ensure that tracePtr->clientData is freed whenever the
2574 * command "oldName" is deleted.
2575 */
2576 if (cmdPtr->flags & TCL_TRACE_RENAME) {
2577 flags &= ~TCL_TRACE_RENAME;
2578 }
2579 if (flags == 0) {
2580 return NULL;
2581 }
2582 }
2583 cmdPtr->flags |= CMD_TRACE_ACTIVE;
2584 cmdPtr->refCount++;
2585
2586 result = NULL;
2587 active.nextPtr = iPtr->activeCmdTracePtr;
2588 iPtr->activeCmdTracePtr = &active;
2589
2590 if (flags & TCL_TRACE_DELETE) {
2591 flags |= TCL_TRACE_DESTROYED;
2592 }
2593 active.cmdPtr = cmdPtr;
2594
2595 Tcl_Preserve((ClientData) iPtr);
2596
2597 for (tracePtr = cmdPtr->tracePtr; tracePtr != NULL;
2598 tracePtr = active.nextTracePtr) {
2599 int traceFlags = (tracePtr->flags & mask);
2600
2601 active.nextTracePtr = tracePtr->nextPtr;
2602 if (!(traceFlags & flags)) {
2603 continue;
2604 }
2605 cmdPtr->flags |= traceFlags;
2606 if (oldName == NULL) {
2607 TclNewObj(oldNamePtr);
2608 Tcl_IncrRefCount(oldNamePtr);
2609 Tcl_GetCommandFullName((Tcl_Interp *) iPtr,
2610 (Tcl_Command) cmdPtr, oldNamePtr);
2611 oldName = TclGetString(oldNamePtr);
2612 }
2613 tracePtr->refCount++;
2614 (*tracePtr->traceProc)(tracePtr->clientData,
2615 (Tcl_Interp *) iPtr, oldName, newName, flags);
2616 cmdPtr->flags &= ~traceFlags;
2617 if ((--tracePtr->refCount) <= 0) {
2618 ckfree((char*)tracePtr);
2619 }
2620 }
2621
2622 /*
2623 * If a new object was created to hold the full oldName,
2624 * free it now.
2625 */
2626
2627 if (oldNamePtr != NULL) {
2628 TclDecrRefCount(oldNamePtr);
2629 }
2630
2631 /*
2632 * Restore the variable's flags, remove the record of our active
2633 * traces, and then return.
2634 */
2635
2636 cmdPtr->flags &= ~CMD_TRACE_ACTIVE;
2637 cmdPtr->refCount--;
2638 iPtr->activeCmdTracePtr = active.nextPtr;
2639 Tcl_Release((ClientData) iPtr);
2640 return result;
2641 }
2642
2643 /*
2644 *----------------------------------------------------------------------
2645 *
2646 * TclCleanupCommand --
2647 *
2648 * This procedure frees up a Command structure unless it is still
2649 * referenced from an interpreter's command hashtable or from a CmdName
2650 * Tcl object representing the name of a command in a ByteCode
2651 * instruction sequence.
2652 *
2653 * Results:
2654 * None.
2655 *
2656 * Side effects:
2657 * Memory gets freed unless a reference to the Command structure still
2658 * exists. In that case the cleanup is delayed until the command is
2659 * deleted or when the last ByteCode referring to it is freed.
2660 *
2661 *----------------------------------------------------------------------
2662 */
2663
2664 void
2665 TclCleanupCommand(cmdPtr)
2666 register Command *cmdPtr; /* Points to the Command structure to
2667 * be freed. */
2668 {
2669 cmdPtr->refCount--;
2670 if (cmdPtr->refCount <= 0) {
2671 ckfree((char *) cmdPtr);
2672 }
2673 }
2674
2675 /*
2676 *----------------------------------------------------------------------
2677 *
2678 * Tcl_CreateMathFunc --
2679 *
2680 * Creates a new math function for expressions in a given
2681 * interpreter.
2682 *
2683 * Results:
2684 * None.
2685 *
2686 * Side effects:
2687 * The function defined by "name" is created or redefined. If the
2688 * function already exists then its definition is replaced; this
2689 * includes the builtin functions. Redefining a builtin function forces
2690 * all existing code to be invalidated since that code may be compiled
2691 * using an instruction specific to the replaced function. In addition,
2692 * redefioning a non-builtin function will force existing code to be
2693 * invalidated if the number of arguments has changed.
2694 *
2695 *----------------------------------------------------------------------
2696 */
2697
2698 void
2699 Tcl_CreateMathFunc(interp, name, numArgs, argTypes, proc, clientData)
2700 Tcl_Interp *interp; /* Interpreter in which function is
2701 * to be available. */
2702 CONST char *name; /* Name of function (e.g. "sin"). */
2703 int numArgs; /* Nnumber of arguments required by
2704 * function. */
2705 Tcl_ValueType *argTypes; /* Array of types acceptable for
2706 * each argument. */
2707 Tcl_MathProc *proc; /* Procedure that implements the
2708 * math function. */
2709 ClientData clientData; /* Additional value to pass to the
2710 * function. */
2711 {
2712 Interp *iPtr = (Interp *) interp;
2713 Tcl_HashEntry *hPtr;
2714 MathFunc *mathFuncPtr;
2715 int new, i;
2716
2717 hPtr = Tcl_CreateHashEntry(&iPtr->mathFuncTable, name, &new);
2718 if (new) {
2719 Tcl_SetHashValue(hPtr, ckalloc(sizeof(MathFunc)));
2720 }
2721 mathFuncPtr = (MathFunc *) Tcl_GetHashValue(hPtr);
2722
2723 if (!new) {
2724 if (mathFuncPtr->builtinFuncIndex >= 0) {
2725 /*
2726 * We are redefining a builtin math function. Invalidate the
2727 * interpreter's existing code by incrementing its
2728 * compileEpoch member. This field is checked in Tcl_EvalObj
2729 * and ObjInterpProc, and code whose compilation epoch doesn't
2730 * match is recompiled. Newly compiled code will no longer
2731 * treat the function as builtin.
2732 */
2733
2734 iPtr->compileEpoch++;
2735 } else {
2736 /*
2737 * A non-builtin function is being redefined. We must invalidate
2738 * existing code if the number of arguments has changed. This
2739 * is because existing code was compiled assuming that number.
2740 */
2741
2742 if (numArgs != mathFuncPtr->numArgs) {
2743 iPtr->compileEpoch++;
2744 }
2745 }
2746 }
2747
2748 mathFuncPtr->builtinFuncIndex = -1; /* can't be a builtin function */
2749 if (numArgs > MAX_MATH_ARGS) {
2750 numArgs = MAX_MATH_ARGS;
2751 }
2752 mathFuncPtr->numArgs = numArgs;
2753 for (i = 0; i < numArgs; i++) {
2754 mathFuncPtr->argTypes[i] = argTypes[i];
2755 }
2756 mathFuncPtr->proc = proc;
2757 mathFuncPtr->clientData = clientData;
2758 }
2759
2760 /*
2761 *----------------------------------------------------------------------
2762 *
2763 * Tcl_GetMathFuncInfo --
2764 *
2765 * Discovers how a particular math function was created in a given
2766 * interpreter.
2767 *
2768 * Results:
2769 * TCL_OK if it succeeds, TCL_ERROR else (leaving an error message
2770 * in the interpreter result if that happens.)
2771 *
2772 * Side effects:
2773 * If this function succeeds, the variables pointed to by the
2774 * numArgsPtr and argTypePtr arguments will be updated to detail the
2775 * arguments allowed by the function. The variable pointed to by the
2776 * procPtr argument will be set to NULL if the function is a builtin
2777 * function, and will be set to the address of the C function used to
2778 * implement the math function otherwise (in which case the variable
2779 * pointed to by the clientDataPtr argument will also be updated.)
2780 *
2781 *----------------------------------------------------------------------
2782 */
2783
2784 int
2785 Tcl_GetMathFuncInfo(interp, name, numArgsPtr, argTypesPtr, procPtr,
2786 clientDataPtr)
2787 Tcl_Interp *interp;
2788 CONST char *name;
2789 int *numArgsPtr;
2790 Tcl_ValueType **argTypesPtr;
2791 Tcl_MathProc **procPtr;
2792 ClientData *clientDataPtr;
2793 {
2794 Interp *iPtr = (Interp *) interp;
2795 Tcl_HashEntry *hPtr;
2796 MathFunc *mathFuncPtr;
2797 Tcl_ValueType *argTypes;
2798 int i,numArgs;
2799
2800 hPtr = Tcl_FindHashEntry(&iPtr->mathFuncTable, name);
2801 if (hPtr == NULL) {
2802 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
2803 "math function \"", name, "\" not known in this interpreter",
2804 (char *) NULL);
2805 return TCL_ERROR;
2806 }
2807 mathFuncPtr = (MathFunc *) Tcl_GetHashValue(hPtr);
2808
2809 *numArgsPtr = numArgs = mathFuncPtr->numArgs;
2810 if (numArgs == 0) {
2811 /* Avoid doing zero-sized allocs... */
2812 numArgs = 1;
2813 }
2814 *argTypesPtr = argTypes =
2815 (Tcl_ValueType *)ckalloc(numArgs * sizeof(Tcl_ValueType));
2816 for (i = 0; i < mathFuncPtr->numArgs; i++) {
2817 argTypes[i] = mathFuncPtr->argTypes[i];
2818 }
2819
2820 if (mathFuncPtr->builtinFuncIndex == -1) {
2821 *procPtr = (Tcl_MathProc *) NULL;
2822 } else {
2823 *procPtr = mathFuncPtr->proc;
2824 *clientDataPtr = mathFuncPtr->clientData;
2825 }
2826
2827 return TCL_OK;
2828 }
2829
2830 /*
2831 *----------------------------------------------------------------------
2832 *
2833 * Tcl_ListMathFuncs --
2834 *
2835 * Produces a list of all the math functions defined in a given
2836 * interpreter.
2837 *
2838 * Results:
2839 * A pointer to a Tcl_Obj structure with a reference count of zero,
2840 * or NULL in the case of an error (in which case a suitable error
2841 * message will be left in the interpreter result.)
2842 *
2843 * Side effects:
2844 * None.
2845 *
2846 *----------------------------------------------------------------------
2847 */
2848
2849 Tcl_Obj *
2850 Tcl_ListMathFuncs(interp, pattern)
2851 Tcl_Interp *interp;
2852 CONST char *pattern;
2853 {
2854 Interp *iPtr = (Interp *) interp;
2855 Tcl_Obj *resultList = Tcl_NewObj();
2856 register Tcl_HashEntry *hPtr;
2857 Tcl_HashSearch hSearch;
2858 CONST char *name;
2859
2860 for (hPtr = Tcl_FirstHashEntry(&iPtr->mathFuncTable, &hSearch);
2861 hPtr != NULL; hPtr = Tcl_NextHashEntry(&hSearch)) {
2862 name = Tcl_GetHashKey(&iPtr->mathFuncTable, hPtr);
2863 if ((pattern == NULL || Tcl_StringMatch(name, pattern)) &&
2864 /* I don't expect this to fail, but... */
2865 Tcl_ListObjAppendElement(interp, resultList,
2866 Tcl_NewStringObj(name,-1)) != TCL_OK) {
2867 Tcl_DecrRefCount(resultList);
2868 return NULL;
2869 }
2870 }
2871 return resultList;
2872 }
2873
2874 /*
2875 *----------------------------------------------------------------------
2876 *
2877 * TclInterpReady --
2878 *
2879 * Check if an interpreter is ready to eval commands or scripts,
2880 * i.e., if it was not deleted and if the nesting level is not
2881 * too high.
2882 *
2883 * Results:
2884 * The return value is TCL_OK if it the interpreter is ready,
2885 * TCL_ERROR otherwise.
2886 *
2887 * Side effects:
2888 * The interpreters object and string results are cleared.
2889 *
2890 *----------------------------------------------------------------------
2891 */
2892
2893 int
2894 TclInterpReady(interp)
2895 Tcl_Interp *interp;
2896 {
2897 register Interp *iPtr = (Interp *) interp;
2898
2899 /*
2900 * Reset both the interpreter's string and object results and clear
2901 * out any previous error information.
2902 */
2903
2904 Tcl_ResetResult(interp);
2905
2906 /*
2907 * If the interpreter has been deleted, return an error.
2908 */
2909
2910 if (iPtr->flags & DELETED) {
2911 Tcl_ResetResult(interp);
2912 Tcl_AppendToObj(Tcl_GetObjResult(interp),
2913 "attempt to call eval in deleted interpreter", -1);
2914 Tcl_SetErrorCode(interp, "CORE", "IDELETE",
2915 "attempt to call eval in deleted interpreter",
2916 (char *) NULL);
2917 return TCL_ERROR;
2918 }
2919
2920 /*
2921 * Check depth of nested calls to Tcl_Eval: if this gets too large,
2922 * it's probably because of an infinite loop somewhere.
2923 */
2924
2925 if (((iPtr->numLevels) > iPtr->maxNestingDepth)
2926 || (TclpCheckStackSpace() == 0)) {
2927 Tcl_AppendToObj(Tcl_GetObjResult(interp),
2928 "too many nested evaluations (infinite loop?)", -1);
2929 return TCL_ERROR;
2930 }
2931
2932 return TCL_OK;
2933 }
2934
2935 /*
2936 *----------------------------------------------------------------------
2937 *
2938 * TclEvalObjvInternal --
2939 *
2940 * This procedure evaluates a Tcl command that has already been
2941 * parsed into words, with one Tcl_Obj holding each word. The caller
2942 * is responsible for managing the iPtr->numLevels.
2943 *
2944 * Results:
2945 * The return value is a standard Tcl completion code such as
2946 * TCL_OK or TCL_ERROR. A result or error message is left in
2947 * interp's result. If an error occurs, this procedure does
2948 * NOT add any information to the errorInfo variable.
2949 *
2950 * Side effects:
2951 * Depends on the command.
2952 *
2953 *----------------------------------------------------------------------
2954 */
2955
2956 int
2957 TclEvalObjvInternal(interp, objc, objv, command, length, flags)
2958 Tcl_Interp *interp; /* Interpreter in which to evaluate the
2959 * command. Also used for error
2960 * reporting. */
2961 int objc; /* Number of words in command. */
2962 Tcl_Obj *CONST objv[]; /* An array of pointers to objects that are
2963 * the words that make up the command. */
2964 CONST char *command; /* Points to the beginning of the string
2965 * representation of the command; this
2966 * is used for traces. If the string
2967 * representation of the command is
2968 * unknown, an empty string should be
2969 * supplied. If it is NULL, no traces will
2970 * be called. */
2971 int length; /* Number of bytes in command; if -1, all
2972 * characters up to the first null byte are
2973 * used. */
2974 int flags; /* Collection of OR-ed bits that control
2975 * the evaluation of the script. Only
2976 * TCL_EVAL_GLOBAL and TCL_EVAL_INVOKE are
2977 * currently supported. */
2978
2979 {
2980 Command *cmdPtr;
2981 Interp *iPtr = (Interp *) interp;
2982 Tcl_Obj **newObjv;
2983 int i;
2984 CallFrame *savedVarFramePtr; /* Saves old copy of iPtr->varFramePtr
2985 * in case TCL_EVAL_GLOBAL was set. */
2986 int code = TCL_OK;
2987 int traceCode = TCL_OK;
2988 int checkTraces = 1;
2989
2990 if (TclInterpReady(interp) == TCL_ERROR) {
2991 return TCL_ERROR;
2992 }
2993
2994 if (objc == 0) {
2995 return TCL_OK;
2996 }
2997
2998 /*
2999 * If any execution traces rename or delete the current command,
3000 * we may need (at most) two passes here.
3001 */
3002 while (1) {
3003
3004 /*
3005 * Find the procedure to execute this command. If there isn't one,
3006 * then see if there is a command "unknown". If so, create a new
3007 * word array with "unknown" as the first word and the original
3008 * command words as arguments. Then call ourselves recursively
3009 * to execute it.
3010 *
3011 * If caller requests, or if we're resolving the target end of
3012 * an interpeter alias (TCL_EVAL_INVOKE), be sure to do command
3013 * name resolution in the global namespace.
3014 */
3015
3016 savedVarFramePtr = iPtr->varFramePtr;
3017 if (flags & (TCL_EVAL_INVOKE | TCL_EVAL_GLOBAL)) {
3018 iPtr->varFramePtr = NULL;
3019 }
3020 cmdPtr = (Command *) Tcl_GetCommandFromObj(interp, objv[0]);
3021 iPtr->varFramePtr = savedVarFramePtr;
3022
3023 if (cmdPtr == NULL) {
3024 newObjv = (Tcl_Obj **) ckalloc((unsigned)
3025 ((objc + 1) * sizeof (Tcl_Obj *)));
3026 for (i = objc-1; i >= 0; i--) {
3027 newObjv[i+1] = objv[i];
3028 }
3029 newObjv[0] = Tcl_NewStringObj("::unknown", -1);
3030 Tcl_IncrRefCount(newObjv[0]);
3031 cmdPtr = (Command *) Tcl_GetCommandFromObj(interp, newObjv[0]);
3032 if (cmdPtr == NULL) {
3033 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
3034 "invalid command name \"", Tcl_GetString(objv[0]), "\"",
3035 (char *) NULL);
3036 code = TCL_ERROR;
3037 } else {
3038 iPtr->numLevels++;
3039 code = TclEvalObjvInternal(interp, objc+1, newObjv, command, length, 0);
3040 iPtr->numLevels--;
3041 }
3042 Tcl_DecrRefCount(newObjv[0]);
3043 ckfree((char *) newObjv);
3044 goto done;
3045 }
3046
3047 /*
3048 * Call trace procedures if needed.
3049 */
3050 if ((checkTraces) && (command != NULL)) {
3051 int cmdEpoch = cmdPtr->cmdEpoch;
3052 cmdPtr->refCount++;
3053 /*
3054 * If the first set of traces modifies/deletes the command or
3055 * any existing traces, then the set checkTraces to 0 and
3056 * go through this while loop one more time.
3057 */
3058 if (iPtr->tracePtr != NULL && traceCode == TCL_OK) {
3059 traceCode = TclCheckInterpTraces(interp, command, length,
3060 cmdPtr, code, TCL_TRACE_ENTER_EXEC, objc, objv);
3061 }
3062 if ((cmdPtr->flags & CMD_HAS_EXEC_TRACES)
3063 && (traceCode == TCL_OK)) {
3064 traceCode = TclCheckExecutionTraces(interp, command, length,
3065 cmdPtr, code, TCL_TRACE_ENTER_EXEC, objc, objv);
3066 }
3067 cmdPtr->refCount--;
3068 if (cmdEpoch != cmdPtr->cmdEpoch) {
3069 /* The command has been modified in some way */
3070 checkTraces = 0;
3071 continue;
3072 }
3073 }
3074 break;
3075 }
3076
3077 /*
3078 * Finally, invoke the command's Tcl_ObjCmdProc.
3079 */
3080 cmdPtr->refCount++;
3081 iPtr->cmdCount++;
3082 if ( code == TCL_OK && traceCode == TCL_OK) {
3083 savedVarFramePtr = iPtr->varFramePtr;
3084 if (flags & TCL_EVAL_GLOBAL) {
3085 iPtr->varFramePtr = NULL;
3086 }
3087 code = (*cmdPtr->objProc)(cmdPtr->objClientData, interp, objc, objv);
3088 iPtr->varFramePtr = savedVarFramePtr;
3089 }
3090 if (Tcl_AsyncReady()) {
3091 code = Tcl_AsyncInvoke(interp, code);
3092 }
3093
3094 /*
3095 * Call 'leave' command traces
3096 */
3097 if (!(cmdPtr->flags & CMD_IS_DELETED)) {
3098 int saveErrFlags = iPtr->flags
3099 & (ERR_IN_PROGRESS | ERR_ALREADY_LOGGED | ERROR_CODE_SET);
3100 if ((cmdPtr->flags & CMD_HAS_EXEC_TRACES) && (traceCode == TCL_OK)) {
3101 traceCode = TclCheckExecutionTraces (interp, command, length,
3102 cmdPtr, code, TCL_TRACE_LEAVE_EXEC, objc, objv);
3103 }
3104 if (iPtr->tracePtr != NULL && traceCode == TCL_OK) {
3105 traceCode = TclCheckInterpTraces(interp, command, length,
3106 cmdPtr, code, TCL_TRACE_LEAVE_EXEC, objc, objv);
3107 }
3108 if (traceCode == TCL_OK) {
3109 iPtr->flags |= saveErrFlags;
3110 }
3111 }
3112 TclCleanupCommand(cmdPtr);
3113
3114 /*
3115 * If one of the trace invocation resulted in error, then
3116 * change the result code accordingly. Note, that the
3117 * interp->result should already be set correctly by the
3118 * call to TraceExecutionProc.
3119 */
3120
3121 if (traceCode != TCL_OK) {
3122 code = traceCode;
3123 }
3124
3125 /*
3126 * If the interpreter has a non-empty string result, the result
3127 * object is either empty or stale because some procedure set
3128 * interp->result directly. If so, move the string result to the
3129 * result object, then reset the string result.
3130 */
3131
3132 if (*(iPtr->result) != 0) {
3133 (void) Tcl_GetObjResult(interp);
3134 }
3135
3136 done:
3137 return code;
3138 }
3139
3140 /*
3141 *----------------------------------------------------------------------
3142 *
3143 * Tcl_EvalObjv --
3144 *
3145 * This procedure evaluates a Tcl command that has already been
3146 * parsed into words, with one Tcl_Obj holding each word.
3147 *
3148 * Results:
3149 * The return value is a standard Tcl completion code such as
3150 * TCL_OK or TCL_ERROR. A result or error message is left in
3151 * interp's result.
3152 *
3153 * Side effects:
3154 * Depends on the command.
3155 *
3156 *----------------------------------------------------------------------
3157 */
3158
3159 int
3160 Tcl_EvalObjv(interp, objc, objv, flags)
3161 Tcl_Interp *interp; /* Interpreter in which to evaluate the
3162 * command. Also used for error
3163 * reporting. */
3164 int objc; /* Number of words in command. */
3165 Tcl_Obj *CONST objv[]; /* An array of pointers to objects that are
3166 * the words that make up the command. */
3167 int flags; /* Collection of OR-ed bits that control
3168 * the evaluation of the script. Only
3169 * TCL_EVAL_GLOBAL and TCL_EVAL_INVOKE
3170 * are currently supported. */
3171 {
3172 Interp *iPtr = (Interp *)interp;
3173 Trace *tracePtr;
3174 Tcl_DString cmdBuf;
3175 char *cmdString = ""; /* A command string is only necessary for
3176 * command traces or error logs; it will be
3177 * generated to replace this default value if
3178 * necessary. */
3179 int cmdLen = 0; /* a non-zero value indicates that a command
3180 * string was generated. */
3181 int code = TCL_OK;
3182 int i;
3183 int allowExceptions = (iPtr->evalFlags & TCL_ALLOW_EXCEPTIONS);
3184
3185 for (tracePtr = iPtr->tracePtr; tracePtr; tracePtr = tracePtr->nextPtr) {
3186 if ((tracePtr->level == 0) || (iPtr->numLevels <= tracePtr->level)) {
3187 /*
3188 * The command may be needed for an execution trace. Generate a
3189 * command string.
3190 */
3191
3192 Tcl_DStringInit(&cmdBuf);
3193 for (i = 0; i < objc; i++) {
3194 Tcl_DStringAppendElement(&cmdBuf, Tcl_GetString(objv[i]));
3195 }
3196 cmdString = Tcl_DStringValue(&cmdBuf);
3197 cmdLen = Tcl_DStringLength(&cmdBuf);
3198 break;
3199 }
3200 }
3201
3202 iPtr->numLevels++;
3203 code = TclEvalObjvInternal(interp, objc, objv, cmdString, cmdLen, flags);
3204 iPtr->numLevels--;
3205
3206 /*
3207 * If we are again at the top level, process any unusual
3208 * return code returned by the evaluated code.
3209 */
3210
3211 if (iPtr->numLevels == 0) {
3212 if (code == TCL_RETURN) {
3213 code = TclUpdateReturnInfo(iPtr);
3214 }
3215 if ((code != TCL_OK) && (code != TCL_ERROR)
3216 && !allowExceptions) {
3217 ProcessUnexpectedResult(interp, code);
3218 code = TCL_ERROR;
3219 }
3220 }
3221
3222 if ((code == TCL_ERROR) && !(flags & TCL_EVAL_INVOKE)) {
3223
3224 /*
3225 * If there was an error, a command string will be needed for the
3226 * error log: generate it now if it was not done previously.
3227 */
3228
3229 if (cmdLen == 0) {
3230 Tcl_DStringInit(&cmdBuf);
3231 for (i = 0; i < objc; i++) {
3232 Tcl_DStringAppendElement(&cmdBuf, Tcl_GetString(objv[i]));
3233 }
3234 cmdString = Tcl_DStringValue(&cmdBuf);
3235 cmdLen = Tcl_DStringLength(&cmdBuf);
3236 }
3237 Tcl_LogCommandInfo(interp, cmdString, cmdString, cmdLen);
3238 }
3239
3240 if (cmdLen != 0) {
3241 Tcl_DStringFree(&cmdBuf);
3242 }
3243 return code;
3244 }
3245
3246 /*
3247 *----------------------------------------------------------------------
3248 *
3249 * Tcl_LogCommandInfo --
3250 *
3251 * This procedure is invoked after an error occurs in an interpreter.
3252 * It adds information to the "errorInfo" variable to describe the
3253 * command that was being executed when the error occurred.
3254 *
3255 * Results:
3256 * None.
3257 *
3258 * Side effects:
3259 * Information about the command is added to errorInfo and the
3260 * line number stored internally in the interpreter is set. If this
3261 * is the first call to this procedure or Tcl_AddObjErrorInfo since
3262 * an error occurred, then old information in errorInfo is
3263 * deleted.
3264 *
3265 *----------------------------------------------------------------------
3266 */
3267
3268 void
3269 Tcl_LogCommandInfo(interp, script, command, length)
3270 Tcl_Interp *interp; /* Interpreter in which to log information. */
3271 CONST char *script; /* First character in script containing
3272 * command (must be <= command). */
3273 CONST char *command; /* First character in command that
3274 * generated the error. */
3275 int length; /* Number of bytes in command (-1 means
3276 * use all bytes up to first null byte). */
3277 {
3278 char buffer[200];
3279 register CONST char *p;
3280 char *ellipsis = "";
3281 Interp *iPtr = (Interp *) interp;
3282
3283 if (iPtr->flags & ERR_ALREADY_LOGGED) {
3284 /*
3285 * Someone else has already logged error information for this
3286 * command; we shouldn't add anything more.
3287 */
3288
3289 return;
3290 }
3291
3292 /*
3293 * Compute the line number where the error occurred.
3294 */
3295
3296 iPtr->errorLine = 1;
3297 for (p = script; p != command; p++) {
3298 if (*p == '\n') {
3299 iPtr->errorLine++;
3300 }
3301 }
3302
3303 /*
3304 * Create an error message to add to errorInfo, including up to a
3305 * maximum number of characters of the command.
3306 */
3307
3308 if (length < 0) {
3309 length = strlen(command);
3310 }
3311 if (length > 150) {
3312 length = 150;
3313 ellipsis = "...";
3314 }
3315 while ( (command[length] & 0xC0) == 0x80 ) {
3316 /*
3317 * Back up truncation point so that we don't truncate in the
3318 * middle of a multi-byte character (in UTF-8)
3319 */
3320 length--;
3321 ellipsis = "...";
3322 }
3323 if (!(iPtr->flags & ERR_IN_PROGRESS)) {
3324 sprintf(buffer, "\n while executing\n\"%.*s%s\"",
3325 length, command, ellipsis);
3326 } else {
3327 sprintf(buffer, "\n invoked from within\n\"%.*s%s\"",
3328 length, command, ellipsis);
3329 }
3330 Tcl_AddObjErrorInfo(interp, buffer, -1);
3331 iPtr->flags &= ~ERR_ALREADY_LOGGED;
3332 }
3333
3334 /*
3335 *----------------------------------------------------------------------
3336 *
3337 * Tcl_EvalTokensStandard --
3338 *
3339 * Given an array of tokens parsed from a Tcl command (e.g., the
3340 * tokens that make up a word or the index for an array variable)
3341 * this procedure evaluates the tokens and concatenates their
3342 * values to form a single result value.
3343 *
3344 * Results:
3345 * The return value is a standard Tcl completion code such as
3346 * TCL_OK or TCL_ERROR. A result or error message is left in
3347 * interp's result.
3348 *
3349 * Side effects:
3350 * Depends on the array of tokens being evaled.
3351 *
3352 *----------------------------------------------------------------------
3353 */
3354
3355 int
3356 Tcl_EvalTokensStandard(interp, tokenPtr, count)
3357 Tcl_Interp *interp; /* Interpreter in which to lookup
3358 * variables, execute nested commands,
3359 * and report errors. */
3360 Tcl_Token *tokenPtr; /* Pointer to first in an array of tokens
3361 * to evaluate and concatenate. */
3362 int count; /* Number of tokens to consider at tokenPtr.
3363 * Must be at least 1. */
3364 {
3365 Tcl_Obj *resultPtr, *indexPtr, *valuePtr;
3366 char buffer[TCL_UTF_MAX];
3367 #ifdef TCL_MEM_DEBUG
3368 # define MAX_VAR_CHARS 5
3369 #else
3370 # define MAX_VAR_CHARS 30
3371 #endif
3372 char nameBuffer[MAX_VAR_CHARS+1];
3373 char *varName, *index;
3374 CONST char *p = NULL; /* Initialized to avoid compiler warning. */
3375 int length, code;
3376
3377 /*
3378 * The only tricky thing about this procedure is that it attempts to
3379 * avoid object creation and string copying whenever possible. For
3380 * example, if the value is just a nested command, then use the
3381 * command's result object directly.
3382 */
3383
3384 code = TCL_OK;
3385 resultPtr = NULL;
3386 Tcl_ResetResult(interp);
3387 for ( ; count > 0; count--, tokenPtr++) {
3388 valuePtr = NULL;
3389
3390 /*
3391 * The switch statement below computes the next value to be
3392 * concat to the result, as either a range of text or an
3393 * object.
3394 */
3395
3396 switch (tokenPtr->type) {
3397 case TCL_TOKEN_TEXT:
3398 p = tokenPtr->start;
3399 length = tokenPtr->size;
3400 break;
3401
3402 case TCL_TOKEN_BS:
3403 length = Tcl_UtfBackslash(tokenPtr->start, (int *) NULL,
3404 buffer);
3405 p = buffer;
3406 break;
3407
3408 case TCL_TOKEN_COMMAND:
3409 code = Tcl_EvalEx(interp, tokenPtr->start+1, tokenPtr->size-2,
3410 0);
3411 if (code != TCL_OK) {
3412 goto done;
3413 }
3414 valuePtr = Tcl_GetObjResult(interp);
3415 break;
3416
3417 case TCL_TOKEN_VARIABLE:
3418 if (tokenPtr->numComponents == 1) {
3419 indexPtr = NULL;
3420 index = NULL;
3421 } else {
3422 code = Tcl_EvalTokensStandard(interp, tokenPtr+2,
3423 tokenPtr->numComponents - 1);
3424 if (code != TCL_OK) {
3425 goto done;
3426 }
3427 indexPtr = Tcl_GetObjResult(interp);
3428 Tcl_IncrRefCount(indexPtr);
3429 index = Tcl_GetString(indexPtr);
3430 }
3431
3432 /*
3433 * We have to make a copy of the variable name in order
3434 * to have a null-terminated string. We can't make a
3435 * temporary modification to the script to null-terminate
3436 * the name, because a trace callback might potentially
3437 * reuse the script and be affected by the null character.
3438 */
3439
3440 if (tokenPtr[1].size <= MAX_VAR_CHARS) {
3441 varName = nameBuffer;
3442 } else {
3443 varName = ckalloc((unsigned) (tokenPtr[1].size + 1));
3444 }
3445 strncpy(varName, tokenPtr[1].start, (size_t) tokenPtr[1].size);
3446 varName[tokenPtr[1].size] = 0;
3447 valuePtr = Tcl_GetVar2Ex(interp, varName, index,
3448 TCL_LEAVE_ERR_MSG);
3449 if (varName != nameBuffer) {
3450 ckfree(varName);
3451 }
3452 if (indexPtr != NULL) {
3453 Tcl_DecrRefCount(indexPtr);
3454 }
3455 if (valuePtr == NULL) {
3456 code = TCL_ERROR;
3457 goto done;
3458 }
3459 count -= tokenPtr->numComponents;
3460 tokenPtr += tokenPtr->numComponents;
3461 break;
3462
3463 default:
3464 panic("unexpected token type in Tcl_EvalTokensStandard");
3465 }
3466
3467 /*
3468 * If valuePtr isn't NULL, the next piece of text comes from that
3469 * object; otherwise, take length bytes starting at p.
3470 */
3471
3472 if (resultPtr == NULL) {
3473 if (valuePtr != NULL) {
3474 resultPtr = valuePtr;
3475 } else {
3476 resultPtr = Tcl_NewStringObj(p, length);
3477 }
3478 Tcl_IncrRefCount(resultPtr);
3479 } else {
3480 if (Tcl_IsShared(resultPtr)) {
3481 Tcl_DecrRefCount(resultPtr);
3482 resultPtr = Tcl_DuplicateObj(resultPtr);
3483 Tcl_IncrRefCount(resultPtr);
3484 }
3485 if (valuePtr != NULL) {
3486 p = Tcl_GetStringFromObj(valuePtr, &length);
3487 }
3488 Tcl_AppendToObj(resultPtr, p, length);
3489 }
3490 }
3491 if (resultPtr != NULL) {
3492 Tcl_SetObjResult(interp, resultPtr);
3493 } else {
3494 code = TCL_ERROR;
3495 }
3496
3497 done:
3498 if (resultPtr != NULL) {
3499 Tcl_DecrRefCount(resultPtr);
3500 }
3501 return code;
3502 }
3503
3504
3505 /*
3506 *----------------------------------------------------------------------
3507 *
3508 * Tcl_EvalTokens --
3509 *
3510 * Given an array of tokens parsed from a Tcl command (e.g., the
3511 * tokens that make up a word or the index for an array variable)
3512 * this procedure evaluates the tokens and concatenates their
3513 * values to form a single result value.
3514 *
3515 * Results:
3516 * The return value is a pointer to a newly allocated Tcl_Obj
3517 * containing the value of the array of tokens. The reference
3518 * count of the returned object has been incremented. If an error
3519 * occurs in evaluating the tokens then a NULL value is returned
3520 * and an error message is left in interp's result.
3521 *
3522 * Side effects:
3523 * A new object is allocated to hold the result.
3524 *
3525 *----------------------------------------------------------------------
3526 *
3527 * This uses a non-standard return convention; its use is now deprecated.
3528 * It is a wrapper for the new function Tcl_EvalTokensStandard, and is not
3529 * used in the core any longer. It is only kept for backward compatibility.
3530 */
3531
3532 Tcl_Obj *
3533 Tcl_EvalTokens(interp, tokenPtr, count)
3534 Tcl_Interp *interp; /* Interpreter in which to lookup
3535 * variables, execute nested commands,
3536 * and report errors. */
3537 Tcl_Token *tokenPtr; /* Pointer to first in an array of tokens
3538 * to evaluate and concatenate. */
3539 int count; /* Number of tokens to consider at tokenPtr.
3540 * Must be at least 1. */
3541 {
3542 int code;
3543 Tcl_Obj *resPtr;
3544
3545 code = Tcl_EvalTokensStandard(interp, tokenPtr, count);
3546 if (code == TCL_OK) {
3547 resPtr = Tcl_GetObjResult(interp);
3548 Tcl_IncrRefCount(resPtr);
3549 Tcl_ResetResult(interp);
3550 return resPtr;
3551 } else {
3552 return NULL;
3553 }
3554 }
3555
3556
3557 /*
3558 *----------------------------------------------------------------------
3559 *
3560 * Tcl_EvalEx --
3561 *
3562 * This procedure evaluates a Tcl script without using the compiler
3563 * or byte-code interpreter. It just parses the script, creates
3564 * values for each word of each command, then calls EvalObjv
3565 * to execute each command.
3566 *
3567 * Results:
3568 * The return value is a standard Tcl completion code such as
3569 * TCL_OK or TCL_ERROR. A result or error message is left in
3570 * interp's result.
3571 *
3572 * Side effects:
3573 * Depends on the script.
3574 *
3575 *----------------------------------------------------------------------
3576 */
3577
3578 int
3579 Tcl_EvalEx(interp, script, numBytes, flags)
3580 Tcl_Interp *interp; /* Interpreter in which to evaluate the
3581 * script. Also used for error reporting. */
3582 CONST char *script; /* First character of script to evaluate. */
3583 int numBytes; /* Number of bytes in script. If < 0, the
3584 * script consists of all bytes up to the
3585 * first null character. */
3586 int flags; /* Collection of OR-ed bits that control
3587 * the evaluation of the script. Only
3588 * TCL_EVAL_GLOBAL is currently
3589 * supported. */
3590 {
3591 Interp *iPtr = (Interp *) interp;
3592 CONST char *p, *next;
3593 Tcl_Parse parse;
3594 #define NUM_STATIC_OBJS 20
3595 Tcl_Obj *staticObjArray[NUM_STATIC_OBJS], **objv;
3596 Tcl_Token *tokenPtr;
3597 int i, code, commandLength, bytesLeft, nested;
3598 CallFrame *savedVarFramePtr; /* Saves old copy of iPtr->varFramePtr
3599 * in case TCL_EVAL_GLOBAL was set. */
3600 int allowExceptions = (iPtr->evalFlags & TCL_ALLOW_EXCEPTIONS);
3601
3602 /*
3603 * The variables below keep track of how much state has been
3604 * allocated while evaluating the script, so that it can be freed
3605 * properly if an error occurs.
3606 */
3607
3608 int gotParse = 0, objectsUsed = 0;
3609
3610 if (numBytes < 0) {
3611 numBytes = strlen(script);
3612 }
3613 Tcl_ResetResult(interp);
3614
3615 savedVarFramePtr = iPtr->varFramePtr;
3616 if (flags & TCL_EVAL_GLOBAL) {
3617 iPtr->varFramePtr = NULL;
3618 }
3619
3620 /*
3621 * Each iteration through the following loop parses the next
3622 * command from the script and then executes it.
3623 */
3624
3625 objv = staticObjArray;
3626 p = script;
3627 bytesLeft = numBytes;
3628 if (iPtr->evalFlags & TCL_BRACKET_TERM) {
3629 nested = 1;
3630 } else {
3631 nested = 0;
3632 }
3633 iPtr->evalFlags = 0;
3634 do {
3635 if (Tcl_ParseCommand(interp, p, bytesLeft, nested, &parse)
3636 != TCL_OK) {
3637 code = TCL_ERROR;
3638 goto error;
3639 }
3640 gotParse = 1;
3641
3642 if (nested && parse.term == (script + numBytes)) {
3643 /*
3644 * A nested script can only terminate in ']'. If
3645 * the parsing got terminated at the end of the script,
3646 * there was no closing ']'. Report the syntax error.
3647 */
3648
3649 code = TCL_ERROR;
3650 goto error;
3651 }
3652
3653 if (parse.numWords > 0) {
3654 /*
3655 * Generate an array of objects for the words of the command.
3656 */
3657
3658 if (parse.numWords <= NUM_STATIC_OBJS) {
3659 objv = staticObjArray;
3660 } else {
3661 objv = (Tcl_Obj **) ckalloc((unsigned)
3662 (parse.numWords * sizeof (Tcl_Obj *)));
3663 }
3664 for (objectsUsed = 0, tokenPtr = parse.tokenPtr;
3665 objectsUsed < parse.numWords;
3666 objectsUsed++, tokenPtr += (tokenPtr->numComponents + 1)) {
3667 code = Tcl_EvalTokensStandard(interp, tokenPtr+1,
3668 tokenPtr->numComponents);
3669 if (code == TCL_OK) {
3670 objv[objectsUsed] = Tcl_GetObjResult(interp);
3671 Tcl_IncrRefCount(objv[objectsUsed]);
3672 } else {
3673 goto error;
3674 }
3675 }
3676
3677 /*
3678 * Execute the command and free the objects for its words.
3679 */
3680
3681 iPtr->numLevels++;
3682 code = TclEvalObjvInternal(interp, objectsUsed, objv,
3683 parse.commandStart, parse.commandSize, 0);
3684 iPtr->numLevels--;
3685 if (code != TCL_OK) {
3686 if (iPtr->numLevels == 0) {
3687 if (code == TCL_RETURN) {
3688 code = TclUpdateReturnInfo(iPtr);
3689 }
3690 if ((code != TCL_OK) && (code != TCL_ERROR)
3691 && !allowExceptions) {
3692 ProcessUnexpectedResult(interp, code);
3693 code = TCL_ERROR;
3694 }
3695 }
3696 goto error;
3697 }
3698 for (i = 0; i < objectsUsed; i++) {
3699 Tcl_DecrRefCount(objv[i]);
3700 }
3701 objectsUsed = 0;
3702 if (objv != staticObjArray) {
3703 ckfree((char *) objv);
3704 objv = staticObjArray;
3705 }
3706 }
3707
3708 /*
3709 * Advance to the next command in the script.
3710 */
3711
3712 next = parse.commandStart + parse.commandSize;
3713 bytesLeft -= next - p;
3714 p = next;
3715 Tcl_FreeParse(&parse);
3716 gotParse = 0;
3717 if (nested && (*parse.term == ']')) {
3718 /*
3719 * We get here in the special case where the TCL_BRACKET_TERM
3720 * flag was set in the interpreter and the latest parsed command
3721 * was terminated by the matching close-bracket we seek.
3722 * Return immediately.
3723 */
3724
3725 iPtr->termOffset = (p - 1) - script;
3726 iPtr->varFramePtr = savedVarFramePtr;
3727 return TCL_OK;
3728 }
3729 } while (bytesLeft > 0);
3730 iPtr->termOffset = p - script;
3731 iPtr->varFramePtr = savedVarFramePtr;
3732 return TCL_OK;
3733
3734 error:
3735 /*
3736 * Generate various pieces of error information, such as the line
3737 * number where the error occurred and information to add to the
3738 * errorInfo variable. Then free resources that had been allocated
3739 * to the command.
3740 */
3741
3742 if ((code == TCL_ERROR) && !(iPtr->flags & ERR_ALREADY_LOGGED)) {
3743 commandLength = parse.commandSize;
3744 if (parse.term == parse.commandStart + commandLength - 1) {
3745 /*
3746 * The terminator character (such as ; or ]) of the command where
3747 * the error occurred is the last character in the parsed command.
3748 * Reduce the length by one so that the error message doesn't
3749 * include the terminator character.
3750 */
3751
3752 commandLength -= 1;
3753 }
3754 Tcl_LogCommandInfo(interp, script, parse.commandStart, commandLength);
3755 }
3756
3757 for (i = 0; i < objectsUsed; i++) {
3758 Tcl_DecrRefCount(objv[i]);
3759 }
3760 if (gotParse) {
3761 Tcl_FreeParse(&parse);
3762 }
3763 if (objv != staticObjArray) {
3764 ckfree((char *) objv);
3765 }
3766 iPtr->varFramePtr = savedVarFramePtr;
3767
3768 /*
3769 * All that's left to do before returning is to set iPtr->termOffset
3770 * to point past the end of the script we just evaluated.
3771 */
3772
3773 next = parse.commandStart + parse.commandSize;
3774 bytesLeft -= next - p;
3775 p = next;
3776
3777 if (!nested) {
3778 iPtr->termOffset = p - script;
3779 return code;
3780 }
3781
3782 /*
3783 * When we are nested (the TCL_BRACKET_TERM flag was set in the
3784 * interpreter), we must find the matching close-bracket to
3785 * end the script we are evaluating.
3786 *
3787 * When our return code is TCL_CONTINUE or TCL_RETURN, we want
3788 * to correctly set iPtr->termOffset to point to that matching
3789 * close-bracket so our caller can move to the part of the
3790 * string beyond the script we were asked to evaluate.
3791 * So we try to parse past the rest of the commands.
3792 */
3793
3794 next = NULL;
3795 while (bytesLeft && (*parse.term != ']')) {
3796 if (TCL_OK != Tcl_ParseCommand(NULL, p, bytesLeft, 1, &parse)) {
3797 /*
3798 * Syntax error. Set the termOffset to the beginning of
3799 * the last command parsed.
3800 */
3801
3802 if (next == NULL) {
3803 iPtr->termOffset = (parse.commandStart - 1) - script;
3804 } else {
3805 iPtr->termOffset = (next - 1) - script;
3806 }
3807 return code;
3808 }
3809 next = parse.commandStart + parse.commandSize;
3810 bytesLeft -= next - p;
3811 p = next;
3812 next = parse.commandStart;
3813 Tcl_FreeParse(&parse);
3814 }
3815
3816 if (bytesLeft) {
3817 /*
3818 * parse.term points to the close-bracket.
3819 */
3820
3821 iPtr->termOffset = parse.term - script;
3822 } else if (parse.term == script + numBytes) {
3823 /*
3824 * There was no close-bracket. Syntax error.
3825 */
3826
3827 iPtr->termOffset = parse.term - script;
3828 Tcl_SetObjResult(interp,
3829 Tcl_NewStringObj("missing close-bracket", -1));
3830 return TCL_ERROR;
3831 } else if (*parse.term != ']') {
3832 /*
3833 * There was no close-bracket. Syntax error.
3834 */
3835
3836 iPtr->termOffset = (parse.term + 1) - script;
3837 Tcl_SetObjResult(interp,
3838 Tcl_NewStringObj("missing close-bracket", -1));
3839 return TCL_ERROR;
3840 } else {
3841 /*
3842 * parse.term points to the close-bracket.
3843 */
3844 iPtr->termOffset = parse.term - script;
3845 }
3846 return code;
3847 }
3848
3849 /*
3850 *----------------------------------------------------------------------
3851 *
3852 * Tcl_Eval --
3853 *
3854 * Execute a Tcl command in a string. This procedure executes the
3855 * script directly, rather than compiling it to bytecodes. Before
3856 * the arrival of the bytecode compiler in Tcl 8.0 Tcl_Eval was
3857 * the main procedure used for executing Tcl commands, but nowadays
3858 * it isn't used much.
3859 *
3860 * Results:
3861 * The return value is one of the return codes defined in tcl.h
3862 * (such as TCL_OK), and interp's result contains a value
3863 * to supplement the return code. The value of the result
3864 * will persist only until the next call to Tcl_Eval or Tcl_EvalObj:
3865 * you must copy it or lose it!
3866 *
3867 * Side effects:
3868 * Can be almost arbitrary, depending on the commands in the script.
3869 *
3870 *----------------------------------------------------------------------
3871 */
3872
3873 int
3874 Tcl_Eval(interp, string)
3875 Tcl_Interp *interp; /* Token for command interpreter (returned
3876 * by previous call to Tcl_CreateInterp). */
3877 CONST char *string; /* Pointer to TCL command to execute. */
3878 {
3879 int code = Tcl_EvalEx(interp, string, -1, 0);
3880
3881 /*
3882 * For backwards compatibility with old C code that predates the
3883 * object system in Tcl 8.0, we have to mirror the object result
3884 * back into the string result (some callers may expect it there).
3885 */
3886
3887 Tcl_SetResult(interp, TclGetString(Tcl_GetObjResult(interp)),
3888 TCL_VOLATILE);
3889 return code;
3890 }
3891
3892 /*
3893 *----------------------------------------------------------------------
3894 *
3895 * Tcl_EvalObj, Tcl_GlobalEvalObj --
3896 *
3897 * These functions are deprecated but we keep them around for backwards
3898 * compatibility reasons.
3899 *
3900 * Results:
3901 * See the functions they call.
3902 *
3903 * Side effects:
3904 * See the functions they call.
3905 *
3906 *----------------------------------------------------------------------
3907 */
3908
3909 #undef Tcl_EvalObj
3910 int
3911 Tcl_EvalObj(interp, objPtr)
3912 Tcl_Interp * interp;
3913 Tcl_Obj * objPtr;
3914 {
3915 return Tcl_EvalObjEx(interp, objPtr, 0);
3916 }
3917
3918 #undef Tcl_GlobalEvalObj
3919 int
3920 Tcl_GlobalEvalObj(interp, objPtr)
3921 Tcl_Interp * interp;
3922 Tcl_Obj * objPtr;
3923 {
3924 return Tcl_EvalObjEx(interp, objPtr, TCL_EVAL_GLOBAL);
3925 }
3926
3927 /*
3928 *----------------------------------------------------------------------
3929 *
3930 * Tcl_EvalObjEx --
3931 *
3932 * Execute Tcl commands stored in a Tcl object. These commands are
3933 * compiled into bytecodes if necessary, unless TCL_EVAL_DIRECT
3934 * is specified.
3935 *
3936 * Results:
3937 * The return value is one of the return codes defined in tcl.h
3938 * (such as TCL_OK), and the interpreter's result contains a value
3939 * to supplement the return code.
3940 *
3941 * Side effects:
3942 * The object is converted, if necessary, to a ByteCode object that
3943 * holds the bytecode instructions for the commands. Executing the
3944 * commands will almost certainly have side effects that depend
3945 * on those commands.
3946 *
3947 * Just as in Tcl_Eval, interp->termOffset is set to the offset of the
3948 * last character executed in the objPtr's string.
3949 *
3950 *----------------------------------------------------------------------
3951 */
3952
3953 int
3954 Tcl_EvalObjEx(interp, objPtr, flags)
3955 Tcl_Interp *interp; /* Token for command interpreter
3956 * (returned by a previous call to
3957 * Tcl_CreateInterp). */
3958 register Tcl_Obj *objPtr; /* Pointer to object containing
3959 * commands to execute. */
3960 int flags; /* Collection of OR-ed bits that
3961 * control the evaluation of the
3962 * script. Supported values are
3963 * TCL_EVAL_GLOBAL and
3964 * TCL_EVAL_DIRECT. */
3965 {
3966 register Interp *iPtr = (Interp *) interp;
3967 char *script;
3968 int numSrcBytes;
3969 int result;
3970 CallFrame *savedVarFramePtr; /* Saves old copy of iPtr->varFramePtr
3971 * in case TCL_EVAL_GLOBAL was set. */
3972 int allowExceptions = (iPtr->evalFlags & TCL_ALLOW_EXCEPTIONS);
3973
3974 Tcl_IncrRefCount(objPtr);
3975
3976 if ((iPtr->flags & USE_EVAL_DIRECT) || (flags & TCL_EVAL_DIRECT)) {
3977 /*
3978 * We're not supposed to use the compiler or byte-code interpreter.
3979 * Let Tcl_EvalEx evaluate the command directly (and probably
3980 * more slowly).
3981 *
3982 * Pure List Optimization (no string representation). In this
3983 * case, we can safely use Tcl_EvalObjv instead and get an
3984 * appreciable improvement in execution speed. This is because it
3985 * allows us to avoid a setFromAny step that would just pack
3986 * everything into a string and back out again.
3987 *
3988 * USE_EVAL_DIRECT is a special flag used for testing purpose only
3989 * (ensure we go into the TCL_EVAL_DIRECT path, avoiding opt)
3990 */
3991 if (!(iPtr->flags & USE_EVAL_DIRECT) &&
3992 (objPtr->typePtr == &tclListType) && /* is a list... */
3993 (objPtr->bytes == NULL) /* ...without a string rep */) {
3994 register List *listRepPtr =
3995 (List *) objPtr->internalRep.twoPtrValue.ptr1;
3996 result = Tcl_EvalObjv(interp, listRepPtr->elemCount,
3997 listRepPtr->elements, flags);
3998 } else {
3999 script = Tcl_GetStringFromObj(objPtr, &numSrcBytes);
4000 result = Tcl_EvalEx(interp, script, numSrcBytes, flags);
4001 }
4002 } else {
4003 /*
4004 * Let the compiler/engine subsystem do the evaluation.
4005 */
4006
4007 savedVarFramePtr = iPtr->varFramePtr;
4008 if (flags & TCL_EVAL_GLOBAL) {
4009 iPtr->varFramePtr = NULL;
4010 }
4011
4012 result = TclCompEvalObj(interp, objPtr);
4013
4014 /*
4015 * If we are again at the top level, process any unusual
4016 * return code returned by the evaluated code.
4017 */
4018
4019 if (iPtr->numLevels == 0) {
4020 if (result == TCL_RETURN) {
4021 result = TclUpdateReturnInfo(iPtr);
4022 }
4023 if ((result != TCL_OK) && (result != TCL_ERROR)
4024 && !allowExceptions) {
4025 ProcessUnexpectedResult(interp, result);
4026 result = TCL_ERROR;
4027
4028 /*
4029 * If an error was created here, record information about
4030 * what was being executed when the error occurred.
4031 */
4032
4033 if (!(iPtr->flags & ERR_ALREADY_LOGGED)) {
4034 script = Tcl_GetStringFromObj(objPtr, &numSrcBytes);
4035 Tcl_LogCommandInfo(interp, script, script, numSrcBytes);
4036 iPtr->flags &= ~ERR_ALREADY_LOGGED;
4037 }
4038 }
4039 }
4040 iPtr->evalFlags = 0;
4041 iPtr->varFramePtr = savedVarFramePtr;
4042 }
4043
4044 TclDecrRefCount(objPtr);
4045 return result;
4046 }
4047
4048 /*
4049 *----------------------------------------------------------------------
4050 *
4051 * ProcessUnexpectedResult --
4052 *
4053 * Procedure called by Tcl_EvalObj to set the interpreter's result
4054 * value to an appropriate error message when the code it evaluates
4055 * returns an unexpected result code (not TCL_OK and not TCL_ERROR) to
4056 * the topmost evaluation level.
4057 *
4058 * Results:
4059 * None.
4060 *
4061 * Side effects:
4062 * The interpreter result is set to an error message appropriate to
4063 * the result code.
4064 *
4065 *----------------------------------------------------------------------
4066 */
4067
4068 static void
4069 ProcessUnexpectedResult(interp, returnCode)
4070 Tcl_Interp *interp; /* The interpreter in which the unexpected
4071 * result code was returned. */
4072 int returnCode; /* The unexpected result code. */
4073 {
4074 Tcl_ResetResult(interp);
4075 if (returnCode == TCL_BREAK) {
4076 Tcl_AppendToObj(Tcl_GetObjResult(interp),
4077 "invoked \"break\" outside of a loop", -1);
4078 } else if (returnCode == TCL_CONTINUE) {
4079 Tcl_AppendToObj(Tcl_GetObjResult(interp),
4080 "invoked \"continue\" outside of a loop", -1);
4081 } else {
4082 char buf[30 + TCL_INTEGER_SPACE];
4083
4084 sprintf(buf, "command returned bad code: %d", returnCode);
4085 Tcl_SetResult(interp, buf, TCL_VOLATILE);
4086 }
4087 }
4088
4089 /*
4090 *---------------------------------------------------------------------------
4091 *
4092 * Tcl_ExprLong, Tcl_ExprDouble, Tcl_ExprBoolean --
4093 *
4094 * Procedures to evaluate an expression and return its value in a
4095 * particular form.
4096 *
4097 * Results:
4098 * Each of the procedures below returns a standard Tcl result. If an
4099 * error occurs then an error message is left in the interp's result.
4100 * Otherwise the value of the expression, in the appropriate form,
4101 * is stored at *ptr. If the expression had a result that was
4102 * incompatible with the desired form then an error is returned.
4103 *
4104 * Side effects:
4105 * None.
4106 *
4107 *---------------------------------------------------------------------------
4108 */
4109
4110 int
4111 Tcl_ExprLong(interp, string, ptr)
4112 Tcl_Interp *interp; /* Context in which to evaluate the
4113 * expression. */
4114 CONST char *string; /* Expression to evaluate. */
4115 long *ptr; /* Where to store result. */
4116 {
4117 register Tcl_Obj *exprPtr;
4118 Tcl_Obj *resultPtr;
4119 int length = strlen(string);
4120 int result = TCL_OK;
4121
4122 if (length > 0) {
4123 exprPtr = Tcl_NewStringObj(string, length);
4124 Tcl_IncrRefCount(exprPtr);
4125 result = Tcl_ExprObj(interp, exprPtr, &resultPtr);
4126 if (result == TCL_OK) {
4127 /*
4128 * Store an integer based on the expression result.
4129 */
4130
4131 if (resultPtr->typePtr == &tclIntType) {
4132 *ptr = resultPtr->internalRep.longValue;
4133 } else if (resultPtr->typePtr == &tclDoubleType) {
4134 *ptr = (long) resultPtr->internalRep.doubleValue;
4135 } else {
4136 Tcl_SetResult(interp,
4137 "expression didn't have numeric value", TCL_STATIC);
4138 result = TCL_ERROR;
4139 }
4140 Tcl_DecrRefCount(resultPtr); /* discard the result object */
4141 } else {
4142 /*
4143 * Move the interpreter's object result to the string result,
4144 * then reset the object result.
4145 */
4146
4147 Tcl_SetResult(interp, TclGetString(Tcl_GetObjResult(interp)),
4148 TCL_VOLATILE);
4149 }
4150 Tcl_DecrRefCount(exprPtr); /* discard the expression object */
4151 } else {
4152 /*
4153 * An empty string. Just set the result integer to 0.
4154 */
4155
4156 *ptr = 0;
4157 }
4158 return result;
4159 }
4160
4161 int
4162 Tcl_ExprDouble(interp, string, ptr)
4163 Tcl_Interp *interp; /* Context in which to evaluate the
4164 * expression. */
4165 CONST char *string; /* Expression to evaluate. */
4166 double *ptr; /* Where to store result. */
4167 {
4168 register Tcl_Obj *exprPtr;
4169 Tcl_Obj *resultPtr;
4170 int length = strlen(string);
4171 int result = TCL_OK;
4172
4173 if (length > 0) {
4174 exprPtr = Tcl_NewStringObj(string, length);
4175 Tcl_IncrRefCount(exprPtr);
4176 result = Tcl_ExprObj(interp, exprPtr, &resultPtr);
4177 if (result == TCL_OK) {
4178 /*
4179 * Store a double based on the expression result.
4180 */
4181
4182 if (resultPtr->typePtr == &tclIntType) {
4183 *ptr = (double) resultPtr->internalRep.longValue;
4184 } else if (resultPtr->typePtr == &tclDoubleType) {
4185 *ptr = resultPtr->internalRep.doubleValue;
4186 } else {
4187 Tcl_SetResult(interp,
4188 "expression didn't have numeric value", TCL_STATIC);
4189 result = TCL_ERROR;
4190 }
4191 Tcl_DecrRefCount(resultPtr); /* discard the result object */
4192 } else {
4193 /*
4194 * Move the interpreter's object result to the string result,
4195 * then reset the object result.
4196 */
4197
4198 Tcl_SetResult(interp, TclGetString(Tcl_GetObjResult(interp)),
4199 TCL_VOLATILE);
4200 }
4201 Tcl_DecrRefCount(exprPtr); /* discard the expression object */
4202 } else {
4203 /*
4204 * An empty string. Just set the result double to 0.0.
4205 */
4206
4207 *ptr = 0.0;
4208 }
4209 return result;
4210 }
4211
4212 int
4213 Tcl_ExprBoolean(interp, string, ptr)
4214 Tcl_Interp *interp; /* Context in which to evaluate the
4215 * expression. */
4216 CONST char *string; /* Expression to evaluate. */
4217 int *ptr; /* Where to store 0/1 result. */
4218 {
4219 register Tcl_Obj *exprPtr;
4220 Tcl_Obj *resultPtr;
4221 int length = strlen(string);
4222 int result = TCL_OK;
4223
4224 if (length > 0) {
4225 exprPtr = Tcl_NewStringObj(string, length);
4226 Tcl_IncrRefCount(exprPtr);
4227 result = Tcl_ExprObj(interp, exprPtr, &resultPtr);
4228 if (result == TCL_OK) {
4229 /*
4230 * Store a boolean based on the expression result.
4231 */
4232
4233 if (resultPtr->typePtr == &tclIntType) {
4234 *ptr = (resultPtr->internalRep.longValue != 0);
4235 } else if (resultPtr->typePtr == &tclDoubleType) {
4236 *ptr = (resultPtr->internalRep.doubleValue != 0.0);
4237 } else {
4238 result = Tcl_GetBooleanFromObj(interp, resultPtr, ptr);
4239 }
4240 Tcl_DecrRefCount(resultPtr); /* discard the result object */
4241 }
4242 if (result != TCL_OK) {
4243 /*
4244 * Move the interpreter's object result to the string result,
4245 * then reset the object result.
4246 */
4247
4248 Tcl_SetResult(interp, TclGetString(Tcl_GetObjResult(interp)),
4249 TCL_VOLATILE);
4250 }
4251 Tcl_DecrRefCount(exprPtr); /* discard the expression object */
4252 } else {
4253 /*
4254 * An empty string. Just set the result boolean to 0 (false).
4255 */
4256
4257 *ptr = 0;
4258 }
4259 return result;
4260 }
4261
4262 /*
4263 *--------------------------------------------------------------
4264 *
4265 * Tcl_ExprLongObj, Tcl_ExprDoubleObj, Tcl_ExprBooleanObj --
4266 *
4267 * Procedures to evaluate an expression in an object and return its
4268 * value in a particular form.
4269 *
4270 * Results:
4271 * Each of the procedures below returns a standard Tcl result
4272 * object. If an error occurs then an error message is left in the
4273 * interpreter's result. Otherwise the value of the expression, in the
4274 * appropriate form, is stored at *ptr. If the expression had a result
4275 * that was incompatible with the desired form then an error is
4276 * returned.
4277 *
4278 * Side effects:
4279 * None.
4280 *
4281 *--------------------------------------------------------------
4282 */
4283
4284 int
4285 Tcl_ExprLongObj(interp, objPtr, ptr)
4286 Tcl_Interp *interp; /* Context in which to evaluate the
4287 * expression. */
4288 register Tcl_Obj *objPtr; /* Expression to evaluate. */
4289 long *ptr; /* Where to store long result. */
4290 {
4291 Tcl_Obj *resultPtr;
4292 int result;
4293
4294 result = Tcl_ExprObj(interp, objPtr, &resultPtr);
4295 if (result == TCL_OK) {
4296 if (resultPtr->typePtr == &tclIntType) {
4297 *ptr = resultPtr->internalRep.longValue;
4298 } else if (resultPtr->typePtr == &tclDoubleType) {
4299 *ptr = (long) resultPtr->internalRep.doubleValue;
4300 } else {
4301 result = Tcl_GetLongFromObj(interp, resultPtr, ptr);
4302 if (result != TCL_OK) {
4303 return result;
4304 }
4305 }
4306 Tcl_DecrRefCount(resultPtr); /* discard the result object */
4307 }
4308 return result;
4309 }
4310
4311 int
4312 Tcl_ExprDoubleObj(interp, objPtr, ptr)
4313 Tcl_Interp *interp; /* Context in which to evaluate the
4314 * expression. */
4315 register Tcl_Obj *objPtr; /* Expression to evaluate. */
4316 double *ptr; /* Where to store double result. */
4317 {
4318 Tcl_Obj *resultPtr;
4319 int result;
4320
4321 result = Tcl_ExprObj(interp, objPtr, &resultPtr);
4322 if (result == TCL_OK) {
4323 if (resultPtr->typePtr == &tclIntType) {
4324 *ptr = (double) resultPtr->internalRep.longValue;
4325 } else if (resultPtr->typePtr == &tclDoubleType) {
4326 *ptr = resultPtr->internalRep.doubleValue;
4327 } else {
4328 result = Tcl_GetDoubleFromObj(interp, resultPtr, ptr);
4329 if (result != TCL_OK) {
4330 return result;
4331 }
4332 }
4333 Tcl_DecrRefCount(resultPtr); /* discard the result object */
4334 }
4335 return result;
4336 }
4337
4338 int
4339 Tcl_ExprBooleanObj(interp, objPtr, ptr)
4340 Tcl_Interp *interp; /* Context in which to evaluate the
4341 * expression. */
4342 register Tcl_Obj *objPtr; /* Expression to evaluate. */
4343 int *ptr; /* Where to store 0/1 result. */
4344 {
4345 Tcl_Obj *resultPtr;
4346 int result;
4347
4348 result = Tcl_ExprObj(interp, objPtr, &resultPtr);
4349 if (result == TCL_OK) {
4350 if (resultPtr->typePtr == &tclIntType) {
4351 *ptr = (resultPtr->internalRep.longValue != 0);
4352 } else if (resultPtr->typePtr == &tclDoubleType) {
4353 *ptr = (resultPtr->internalRep.doubleValue != 0.0);
4354 } else {
4355 result = Tcl_GetBooleanFromObj(interp, resultPtr, ptr);
4356 }
4357 Tcl_DecrRefCount(resultPtr); /* discard the result object */
4358 }
4359 return result;
4360 }
4361
4362 /*
4363 *----------------------------------------------------------------------
4364 *
4365 * TclInvoke --
4366 *
4367 * Invokes a Tcl command, given an argv/argc, from either the
4368 * exposed or the hidden sets of commands in the given interpreter.
4369 * NOTE: The command is invoked in the current stack frame of
4370 * the interpreter, thus it can modify local variables.
4371 *
4372 * Results:
4373 * A standard Tcl result.
4374 *
4375 * Side effects:
4376 * Whatever the command does.
4377 *
4378 *----------------------------------------------------------------------
4379 */
4380
4381 int
4382 TclInvoke(interp, argc, argv, flags)
4383 Tcl_Interp *interp; /* Where to invoke the command. */
4384 int argc; /* Count of args. */
4385 register CONST char **argv; /* The arg strings; argv[0] is the name of
4386 * the command to invoke. */
4387 int flags; /* Combination of flags controlling the
4388 * call: TCL_INVOKE_HIDDEN and
4389 * TCL_INVOKE_NO_UNKNOWN. */
4390 {
4391 register Tcl_Obj *objPtr;
4392 register int i;
4393 int length, result;
4394
4395 /*
4396 * This procedure generates an objv array for object arguments that hold
4397 * the argv strings. It starts out with stack-allocated space but uses
4398 * dynamically-allocated storage if needed.
4399 */
4400
4401 #define NUM_ARGS 20
4402 Tcl_Obj *(objStorage[NUM_ARGS]);
4403 register Tcl_Obj **objv = objStorage;
4404
4405 /*
4406 * Create the object argument array "objv". Make sure objv is large
4407 * enough to hold the objc arguments plus 1 extra for the zero
4408 * end-of-objv word.
4409 */
4410
4411 if ((argc + 1) > NUM_ARGS) {
4412 objv = (Tcl_Obj **)
4413 ckalloc((unsigned)(argc + 1) * sizeof(Tcl_Obj *));
4414 }
4415
4416 for (i = 0; i < argc; i++) {
4417 length = strlen(argv[i]);
4418 objv[i] = Tcl_NewStringObj(argv[i], length);
4419 Tcl_IncrRefCount(objv[i]);
4420 }
4421 objv[argc] = 0;
4422
4423 /*
4424 * Use TclObjInterpProc to actually invoke the command.
4425 */
4426
4427 result = TclObjInvoke(interp, argc, objv, flags);
4428
4429 /*
4430 * Move the interpreter's object result to the string result,
4431 * then reset the object result.
4432 */
4433
4434 Tcl_SetResult(interp, TclGetString(Tcl_GetObjResult(interp)),
4435 TCL_VOLATILE);
4436
4437 /*
4438 * Decrement the ref counts on the objv elements since we are done
4439 * with them.
4440 */
4441
4442 for (i = 0; i < argc; i++) {
4443 objPtr = objv[i];
4444 Tcl_DecrRefCount(objPtr);
4445 }
4446
4447 /*
4448 * Free the objv array if malloc'ed storage was used.
4449 */
4450
4451 if (objv != objStorage) {
4452 ckfree((char *) objv);
4453 }
4454 return result;
4455 #undef NUM_ARGS
4456 }
4457
4458 /*
4459 *----------------------------------------------------------------------
4460 *
4461 * TclGlobalInvoke --
4462 *
4463 * Invokes a Tcl command, given an argv/argc, from either the
4464 * exposed or hidden sets of commands in the given interpreter.
4465 * NOTE: The command is invoked in the global stack frame of
4466 * the interpreter, thus it cannot see any current state on
4467 * the stack for that interpreter.
4468 *
4469 * Results:
4470 * A standard Tcl result.
4471 *
4472 * Side effects:
4473 * Whatever the command does.
4474 *
4475 *----------------------------------------------------------------------
4476 */
4477
4478 int
4479 TclGlobalInvoke(interp, argc, argv, flags)
4480 Tcl_Interp *interp; /* Where to invoke the command. */
4481 int argc; /* Count of args. */
4482 register CONST char **argv; /* The arg strings; argv[0] is the name of
4483 * the command to invoke. */
4484 int flags; /* Combination of flags controlling the
4485 * call: TCL_INVOKE_HIDDEN and
4486 * TCL_INVOKE_NO_UNKNOWN. */
4487 {
4488 register Interp *iPtr = (Interp *) interp;
4489 int result;
4490 CallFrame *savedVarFramePtr;
4491
4492 savedVarFramePtr = iPtr->varFramePtr;
4493 iPtr->varFramePtr = NULL;
4494 result = TclInvoke(interp, argc, argv, flags);
4495 iPtr->varFramePtr = savedVarFramePtr;
4496 return result;
4497 }
4498
4499 /*
4500 *----------------------------------------------------------------------
4501 *
4502 * TclObjInvokeGlobal --
4503 *
4504 * Object version: Invokes a Tcl command, given an objv/objc, from
4505 * either the exposed or hidden set of commands in the given
4506 * interpreter.
4507 * NOTE: The command is invoked in the global stack frame of the
4508 * interpreter, thus it cannot see any current state on the
4509 * stack of that interpreter.
4510 *
4511 * Results:
4512 * A standard Tcl result.
4513 *
4514 * Side effects:
4515 * Whatever the command does.
4516 *
4517 *----------------------------------------------------------------------
4518 */
4519
4520 int
4521 TclObjInvokeGlobal(interp, objc, objv, flags)
4522 Tcl_Interp *interp; /* Interpreter in which command is to be
4523 * invoked. */
4524 int objc; /* Count of arguments. */
4525 Tcl_Obj *CONST objv[]; /* Argument objects; objv[0] points to the
4526 * name of the command to invoke. */
4527 int flags; /* Combination of flags controlling the
4528 * call: TCL_INVOKE_HIDDEN,
4529 * TCL_INVOKE_NO_UNKNOWN, or
4530 * TCL_INVOKE_NO_TRACEBACK. */
4531 {
4532 register Interp *iPtr = (Interp *) interp;
4533 int result;
4534 CallFrame *savedVarFramePtr;
4535
4536 savedVarFramePtr = iPtr->varFramePtr;
4537 iPtr->varFramePtr = NULL;
4538 result = TclObjInvoke(interp, objc, objv, flags);
4539 iPtr->varFramePtr = savedVarFramePtr;
4540 return result;
4541 }
4542
4543 /*
4544 *----------------------------------------------------------------------
4545 *
4546 * TclObjInvoke --
4547 *
4548 * Invokes a Tcl command, given an objv/objc, from either the
4549 * exposed or the hidden sets of commands in the given interpreter.
4550 *
4551 * Results:
4552 * A standard Tcl object result.
4553 *
4554 * Side effects:
4555 * Whatever the command does.
4556 *
4557 *----------------------------------------------------------------------
4558 */
4559
4560 int
4561 TclObjInvoke(interp, objc, objv, flags)
4562 Tcl_Interp *interp; /* Interpreter in which command is to be
4563 * invoked. */
4564 int objc; /* Count of arguments. */
4565 Tcl_Obj *CONST objv[]; /* Argument objects; objv[0] points to the
4566 * name of the command to invoke. */
4567 int flags; /* Combination of flags controlling the
4568 * call: TCL_INVOKE_HIDDEN,
4569 * TCL_INVOKE_NO_UNKNOWN, or
4570 * TCL_INVOKE_NO_TRACEBACK. */
4571 {
4572 register Interp *iPtr = (Interp *) interp;
4573 Tcl_HashTable *hTblPtr; /* Table of hidden commands. */
4574 char *cmdName; /* Name of the command from objv[0]. */
4575 register Tcl_HashEntry *hPtr;
4576 Tcl_Command cmd;
4577 Command *cmdPtr;
4578 int localObjc; /* Used to invoke "unknown" if the */
4579 Tcl_Obj **localObjv = NULL; /* command is not found. */
4580 register int i;
4581 int result;
4582
4583 if (interp == (Tcl_Interp *) NULL) {
4584 return TCL_ERROR;
4585 }
4586
4587 if ((objc < 1) || (objv == (Tcl_Obj **) NULL)) {
4588 Tcl_AppendToObj(Tcl_GetObjResult(interp),
4589 "illegal argument vector", -1);
4590 return TCL_ERROR;
4591 }
4592
4593 cmdName = Tcl_GetString(objv[0]);
4594 if (flags & TCL_INVOKE_HIDDEN) {
4595 /*
4596 * We never invoke "unknown" for hidden commands.
4597 */
4598
4599 hPtr = NULL;
4600 hTblPtr = ((Interp *) interp)->hiddenCmdTablePtr;
4601 if (hTblPtr != NULL) {
4602 hPtr = Tcl_FindHashEntry(hTblPtr, cmdName);
4603 }
4604 if (hPtr == NULL) {
4605 Tcl_ResetResult(interp);
4606 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
4607 "invalid hidden command name \"", cmdName, "\"",
4608 (char *) NULL);
4609 return TCL_ERROR;
4610 }
4611 cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
4612 } else {
4613 cmdPtr = NULL;
4614 cmd = Tcl_FindCommand(interp, cmdName,
4615 (Tcl_Namespace *) NULL, /*flags*/ TCL_GLOBAL_ONLY);
4616 if (cmd != (Tcl_Command) NULL) {
4617 cmdPtr = (Command *) cmd;
4618 }
4619 if (cmdPtr == NULL) {
4620 if (!(flags & TCL_INVOKE_NO_UNKNOWN)) {
4621 cmd = Tcl_FindCommand(interp, "unknown",
4622 (Tcl_Namespace *) NULL, /*flags*/ TCL_GLOBAL_ONLY);
4623 if (cmd != (Tcl_Command) NULL) {
4624 cmdPtr = (Command *) cmd;
4625 }
4626 if (cmdPtr != NULL) {
4627 localObjc = (objc + 1);
4628 localObjv = (Tcl_Obj **)
4629 ckalloc((unsigned) (sizeof(Tcl_Obj *) * localObjc));
4630 localObjv[0] = Tcl_NewStringObj("unknown", -1);
4631 Tcl_IncrRefCount(localObjv[0]);
4632 for (i = 0; i < objc; i++) {
4633 localObjv[i+1] = objv[i];
4634 }
4635 objc = localObjc;
4636 objv = localObjv;
4637 }
4638 }
4639
4640 /*
4641 * Check again if we found the command. If not, "unknown" is
4642 * not present and we cannot help, or the caller said not to
4643 * call "unknown" (they specified TCL_INVOKE_NO_UNKNOWN).
4644 */
4645
4646 if (cmdPtr == NULL) {
4647 Tcl_ResetResult(interp);
4648 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
4649 "invalid command name \"", cmdName, "\"",
4650 (char *) NULL);
4651 return TCL_ERROR;
4652 }
4653 }
4654 }
4655
4656 /*
4657 * Invoke the command procedure. First reset the interpreter's string
4658 * and object results to their default empty values since they could
4659 * have gotten changed by earlier invocations.
4660 */
4661
4662 Tcl_ResetResult(interp);
4663 iPtr->cmdCount++;
4664 result = (*cmdPtr->objProc)(cmdPtr->objClientData, interp, objc, objv);
4665
4666 /*
4667 * If an error occurred, record information about what was being
4668 * executed when the error occurred.
4669 */
4670
4671 if ((result == TCL_ERROR)
4672 && ((flags & TCL_INVOKE_NO_TRACEBACK) == 0)
4673 && ((iPtr->flags & ERR_ALREADY_LOGGED) == 0)) {
4674 Tcl_Obj *msg;
4675
4676 if (!(iPtr->flags & ERR_IN_PROGRESS)) {
4677 msg = Tcl_NewStringObj("\n while invoking\n\"", -1);
4678 } else {
4679 msg = Tcl_NewStringObj("\n invoked from within\n\"", -1);
4680 }
4681 Tcl_IncrRefCount(msg);
4682 for (i = 0; i < objc; i++) {
4683 CONST char *bytes;
4684 int length;
4685
4686 Tcl_AppendObjToObj(msg, objv[i]);
4687 bytes = Tcl_GetStringFromObj(msg, &length);
4688 if (length > 100) {
4689 /*
4690 * Back up truncation point so that we don't truncate
4691 * in the middle of a multi-byte character.
4692 */
4693 length = 100;
4694 while ( (bytes[length] & 0xC0) == 0x80 ) {
4695 length--;
4696 }
4697 Tcl_SetObjLength(msg, length);
4698 Tcl_AppendToObj(msg, "...", -1);
4699 break;
4700 }
4701 if (i != (objc - 1)) {
4702 Tcl_AppendToObj(msg, " ", -1);
4703 }
4704 }
4705
4706 Tcl_AppendToObj(msg, "\"", -1);
4707 Tcl_AddObjErrorInfo(interp, Tcl_GetString(msg), -1);
4708 Tcl_DecrRefCount(msg);
4709 iPtr->flags &= ~ERR_ALREADY_LOGGED;
4710 }
4711
4712 /*
4713 * Free any locally allocated storage used to call "unknown".
4714 */
4715
4716 if (localObjv != (Tcl_Obj **) NULL) {
4717 Tcl_DecrRefCount(localObjv[0]);
4718 ckfree((char *) localObjv);
4719 }
4720 return result;
4721 }
4722
4723 /*
4724 *---------------------------------------------------------------------------
4725 *
4726 * Tcl_ExprString --
4727 *
4728 * Evaluate an expression in a string and return its value in string
4729 * form.
4730 *
4731 * Results:
4732 * A standard Tcl result. If the result is TCL_OK, then the interp's
4733 * result is set to the string value of the expression. If the result
4734 * is TCL_ERROR, then the interp's result contains an error message.
4735 *
4736 * Side effects:
4737 * A Tcl object is allocated to hold a copy of the expression string.
4738 * This expression object is passed to Tcl_ExprObj and then
4739 * deallocated.
4740 *
4741 *---------------------------------------------------------------------------
4742 */
4743
4744 int
4745 Tcl_ExprString(interp, string)
4746 Tcl_Interp *interp; /* Context in which to evaluate the
4747 * expression. */
4748 CONST char *string; /* Expression to evaluate. */
4749 {
4750 register Tcl_Obj *exprPtr;
4751 Tcl_Obj *resultPtr;
4752 int length = strlen(string);
4753 char buf[TCL_DOUBLE_SPACE];
4754 int result = TCL_OK;
4755
4756 if (length > 0) {
4757 TclNewObj(exprPtr);
4758 TclInitStringRep(exprPtr, string, length);
4759 Tcl_IncrRefCount(exprPtr);
4760
4761 result = Tcl_ExprObj(interp, exprPtr, &resultPtr);
4762 if (result == TCL_OK) {
4763 /*
4764 * Set the interpreter's string result from the result object.
4765 */
4766
4767 if (resultPtr->typePtr == &tclIntType) {
4768 sprintf(buf, "%ld", resultPtr->internalRep.longValue);
4769 Tcl_SetResult(interp, buf, TCL_VOLATILE);
4770 } else if (resultPtr->typePtr == &tclDoubleType) {
4771 Tcl_PrintDouble((Tcl_Interp *) NULL,
4772 resultPtr->internalRep.doubleValue, buf);
4773 Tcl_SetResult(interp, buf, TCL_VOLATILE);
4774 } else {
4775 /*
4776 * Set interpreter's string result from the result object.
4777 */
4778
4779 Tcl_SetResult(interp, TclGetString(resultPtr),
4780 TCL_VOLATILE);
4781 }
4782 Tcl_DecrRefCount(resultPtr); /* discard the result object */
4783 } else {
4784 /*
4785 * Move the interpreter's object result to the string result,
4786 * then reset the object result.
4787 */
4788
4789 Tcl_SetResult(interp, TclGetString(Tcl_GetObjResult(interp)),
4790 TCL_VOLATILE);
4791 }
4792 Tcl_DecrRefCount(exprPtr); /* discard the expression object */
4793 } else {
4794 /*
4795 * An empty string. Just set the interpreter's result to 0.
4796 */
4797
4798 Tcl_SetResult(interp, "0", TCL_VOLATILE);
4799 }
4800 return result;
4801 }
4802
4803 /*
4804 *----------------------------------------------------------------------
4805 *
4806 * Tcl_CreateObjTrace --
4807 *
4808 * Arrange for a procedure to be called to trace command execution.
4809 *
4810 * Results:
4811 * The return value is a token for the trace, which may be passed
4812 * to Tcl_DeleteTrace to eliminate the trace.
4813 *
4814 * Side effects:
4815 * From now on, proc will be called just before a command procedure
4816 * is called to execute a Tcl command. Calls to proc will have the
4817 * following form:
4818 *
4819 * void proc( ClientData clientData,
4820 * Tcl_Interp* interp,
4821 * int level,
4822 * CONST char* command,
4823 * Tcl_Command commandInfo,
4824 * int objc,
4825 * Tcl_Obj *CONST objv[] );
4826 *
4827 * The 'clientData' and 'interp' arguments to 'proc' will be the
4828 * same as the arguments to Tcl_CreateObjTrace. The 'level'
4829 * argument gives the nesting depth of command interpretation within
4830 * the interpreter. The 'command' argument is the ASCII text of
4831 * the command being evaluated -- before any substitutions are
4832 * performed. The 'commandInfo' argument gives a handle to the
4833 * command procedure that will be evaluated. The 'objc' and 'objv'
4834 * parameters give the parameter vector that will be passed to the
4835 * command procedure. proc does not return a value.
4836 *
4837 * It is permissible for 'proc' to call Tcl_SetCommandTokenInfo
4838 * to change the command procedure or client data for the command
4839 * being evaluated, and these changes will take effect with the
4840 * current evaluation.
4841 *
4842 * The 'level' argument specifies the maximum nesting level of calls
4843 * to be traced. If the execution depth of the interpreter exceeds
4844 * 'level', the trace callback is not executed.
4845 *
4846 * The 'flags' argument is either zero or the value,
4847 * TCL_ALLOW_INLINE_COMPILATION. If the TCL_ALLOW_INLINE_COMPILATION
4848 * flag is not present, the bytecode compiler will not generate inline
4849 * code for Tcl's built-in commands. This behavior will have a significant
4850 * impact on performance, but will ensure that all command evaluations are
4851 * traced. If the TCL_ALLOW_INLINE_COMPILATION flag is present, the
4852 * bytecode compiler will have its normal behavior of compiling in-line
4853 * code for some of Tcl's built-in commands. In this case, the tracing
4854 * will be imprecise -- in-line code will not be traced -- but run-time
4855 * performance will be improved. The latter behavior is desired for
4856 * many applications such as profiling of run time.
4857 *
4858 * When the trace is deleted, the 'delProc' procedure will be invoked,
4859 * passing it the original client data.
4860 *
4861 *----------------------------------------------------------------------
4862 */
4863
4864 Tcl_Trace
4865 Tcl_CreateObjTrace( interp, level, flags, proc, clientData, delProc )
4866 Tcl_Interp* interp; /* Tcl interpreter */
4867 int level; /* Maximum nesting level */
4868 int flags; /* Flags, see above */
4869 Tcl_CmdObjTraceProc* proc; /* Trace callback */
4870 ClientData clientData; /* Client data for the callback */
4871 Tcl_CmdObjTraceDeleteProc* delProc;
4872 /* Procedure to call when trace is deleted */
4873 {
4874 register Trace *tracePtr;
4875 register Interp *iPtr = (Interp *) interp;
4876
4877 /* Test if this trace allows inline compilation of commands */
4878
4879 if (!(flags & TCL_ALLOW_INLINE_COMPILATION)) {
4880 if (iPtr->tracesForbiddingInline == 0) {
4881
4882 /*
4883 * When the first trace forbidding inline compilation is
4884 * created, invalidate existing compiled code for this
4885 * interpreter and arrange (by setting the
4886 * DONT_COMPILE_CMDS_INLINE flag) that when compiling new
4887 * code, no commands will be compiled inline (i.e., into
4888 * an inline sequence of instructions). We do this because
4889 * commands that were compiled inline will never result in
4890 * a command trace being called.
4891 */
4892
4893 iPtr->compileEpoch++;
4894 iPtr->flags |= DONT_COMPILE_CMDS_INLINE;
4895 }
4896 iPtr->tracesForbiddingInline++;
4897 }
4898
4899 tracePtr = (Trace *) ckalloc(sizeof(Trace));
4900 tracePtr->level = level;
4901 tracePtr->proc = proc;
4902 tracePtr->clientData = clientData;
4903 tracePtr->delProc = delProc;
4904 tracePtr->nextPtr = iPtr->tracePtr;
4905 tracePtr->flags = flags;
4906 iPtr->tracePtr = tracePtr;
4907
4908 return (Tcl_Trace) tracePtr;
4909 }
4910
4911 /*
4912 *----------------------------------------------------------------------
4913 *
4914 * Tcl_CreateTrace --
4915 *
4916 * Arrange for a procedure to be called to trace command execution.
4917 *
4918 * Results:
4919 * The return value is a token for the trace, which may be passed
4920 * to Tcl_DeleteTrace to eliminate the trace.
4921 *
4922 * Side effects:
4923 * From now on, proc will be called just before a command procedure
4924 * is called to execute a Tcl command. Calls to proc will have the
4925 * following form:
4926 *
4927 * void
4928 * proc(clientData, interp, level, command, cmdProc, cmdClientData,
4929 * argc, argv)
4930 * ClientData clientData;
4931 * Tcl_Interp *interp;
4932 * int level;
4933 * char *command;
4934 * int (*cmdProc)();
4935 * ClientData cmdClientData;
4936 * int argc;
4937 * char **argv;
4938 * {
4939 * }
4940 *
4941 * The clientData and interp arguments to proc will be the same
4942 * as the corresponding arguments to this procedure. Level gives
4943 * the nesting level of command interpretation for this interpreter
4944 * (0 corresponds to top level). Command gives the ASCII text of
4945 * the raw command, cmdProc and cmdClientData give the procedure that
4946 * will be called to process the command and the ClientData value it
4947 * will receive, and argc and argv give the arguments to the
4948 * command, after any argument parsing and substitution. Proc
4949 * does not return a value.
4950 *
4951 *----------------------------------------------------------------------
4952 */
4953
4954 Tcl_Trace
4955 Tcl_CreateTrace(interp, level, proc, clientData)
4956 Tcl_Interp *interp; /* Interpreter in which to create trace. */
4957 int level; /* Only call proc for commands at nesting
4958 * level<=argument level (1=>top level). */
4959 Tcl_CmdTraceProc *proc; /* Procedure to call before executing each
4960 * command. */
4961 ClientData clientData; /* Arbitrary value word to pass to proc. */
4962 {
4963 StringTraceData* data;
4964 data = (StringTraceData*) ckalloc( sizeof( *data ));
4965 data->clientData = clientData;
4966 data->proc = proc;
4967 return Tcl_CreateObjTrace( interp, level, 0, StringTraceProc,
4968 (ClientData) data, StringTraceDeleteProc );
4969 }
4970
4971 /*
4972 *----------------------------------------------------------------------
4973 *
4974 * StringTraceProc --
4975 *
4976 * Invoke a string-based trace procedure from an object-based
4977 * callback.
4978 *
4979 * Results:
4980 * None.
4981 *
4982 * Side effects:
4983 * Whatever the string-based trace procedure does.
4984 *
4985 *----------------------------------------------------------------------
4986 */
4987
4988 static int
4989 StringTraceProc( clientData, interp, level, command, commandInfo, objc, objv )
4990 ClientData clientData;
4991 Tcl_Interp* interp;
4992 int level;
4993 CONST char* command;
4994 Tcl_Command commandInfo;
4995 int objc;
4996 Tcl_Obj *CONST *objv;
4997 {
4998 StringTraceData* data = (StringTraceData*) clientData;
4999 Command* cmdPtr = (Command*) commandInfo;
5000
5001 CONST char** argv; /* Args to pass to string trace proc */
5002
5003 int i;
5004
5005 /*
5006 * This is a bit messy because we have to emulate the old trace
5007 * interface, which uses strings for everything.
5008 */
5009
5010 argv = (CONST char **) ckalloc((unsigned) ( (objc + 1)
5011 * sizeof(CONST char *) ));
5012 for (i = 0; i < objc; i++) {
5013 argv[i] = Tcl_GetString(objv[i]);
5014 }
5015 argv[objc] = 0;
5016
5017 /*
5018 * Invoke the command procedure. Note that we cast away const-ness
5019 * on two parameters for compatibility with legacy code; the code
5020 * MUST NOT modify either command or argv.
5021 */
5022
5023 ( data->proc )( data->clientData, interp, level,
5024 (char*) command, cmdPtr->proc, cmdPtr->clientData,
5025 objc, argv );
5026 ckfree( (char*) argv );
5027
5028 return TCL_OK;
5029 }
5030
5031 /*
5032 *----------------------------------------------------------------------
5033 *
5034 * StringTraceDeleteProc --
5035 *
5036 * Clean up memory when a string-based trace is deleted.
5037 *
5038 * Results:
5039 * None.
5040 *
5041 * Side effects:
5042 * Allocated memory is returned to the system.
5043 *
5044 *----------------------------------------------------------------------
5045 */
5046
5047 static void
5048 StringTraceDeleteProc( clientData )
5049 ClientData clientData;
5050 {
5051 ckfree( (char*) clientData );
5052 }
5053
5054 /*
5055 *----------------------------------------------------------------------
5056 *
5057 * Tcl_DeleteTrace --
5058 *
5059 * Remove a trace.
5060 *
5061 * Results:
5062 * None.
5063 *
5064 * Side effects:
5065 * From now on there will be no more calls to the procedure given
5066 * in trace.
5067 *
5068 *----------------------------------------------------------------------
5069 */
5070
5071 void
5072 Tcl_DeleteTrace(interp, trace)
5073 Tcl_Interp *interp; /* Interpreter that contains trace. */
5074 Tcl_Trace trace; /* Token for trace (returned previously by
5075 * Tcl_CreateTrace). */
5076 {
5077 Interp *iPtr = (Interp *) interp;
5078 Trace *tracePtr = (Trace *) trace;
5079 register Trace **tracePtr2 = &(iPtr->tracePtr);
5080
5081 /*
5082 * Locate the trace entry in the interpreter's trace list,
5083 * and remove it from the list.
5084 */
5085
5086 while ((*tracePtr2) != NULL && (*tracePtr2) != tracePtr) {
5087 tracePtr2 = &((*tracePtr2)->nextPtr);
5088 }
5089 if (*tracePtr2 == NULL) {
5090 return;
5091 }
5092 (*tracePtr2) = (*tracePtr2)->nextPtr;
5093
5094 /*
5095 * If the trace forbids bytecode compilation, change the interpreter's
5096 * state. If bytecode compilation is now permitted, flag the fact and
5097 * advance the compilation epoch so that procs will be recompiled to
5098 * take advantage of it.
5099 */
5100
5101 if (!(tracePtr->flags & TCL_ALLOW_INLINE_COMPILATION)) {
5102 iPtr->tracesForbiddingInline--;
5103 if (iPtr->tracesForbiddingInline == 0) {
5104 iPtr->flags &= ~DONT_COMPILE_CMDS_INLINE;
5105 iPtr->compileEpoch++;
5106 }
5107 }
5108
5109 /*
5110 * Execute any delete callback.
5111 */
5112
5113 if (tracePtr->delProc != NULL) {
5114 (tracePtr->delProc)(tracePtr->clientData);
5115 }
5116
5117 /* Delete the trace object */
5118
5119 Tcl_EventuallyFree((char*)tracePtr, TCL_DYNAMIC);
5120 }
5121
5122 /*
5123 *----------------------------------------------------------------------
5124 *
5125 * Tcl_AddErrorInfo --
5126 *
5127 * Add information to the "errorInfo" variable that describes the
5128 * current error.
5129 *
5130 * Results:
5131 * None.
5132 *
5133 * Side effects:
5134 * The contents of message are added to the "errorInfo" variable.
5135 * If Tcl_Eval has been called since the current value of errorInfo
5136 * was set, errorInfo is cleared before adding the new message.
5137 * If we are just starting to log an error, errorInfo is initialized
5138 * from the error message in the interpreter's result.
5139 *
5140 *----------------------------------------------------------------------
5141 */
5142
5143 void
5144 Tcl_AddErrorInfo(interp, message)
5145 Tcl_Interp *interp; /* Interpreter to which error information
5146 * pertains. */
5147 CONST char *message; /* Message to record. */
5148 {
5149 Tcl_AddObjErrorInfo(interp, message, -1);
5150 }
5151
5152 /*
5153 *----------------------------------------------------------------------
5154 *
5155 * Tcl_AddObjErrorInfo --
5156 *
5157 * Add information to the "errorInfo" variable that describes the
5158 * current error. This routine differs from Tcl_AddErrorInfo by
5159 * taking a byte pointer and length.
5160 *
5161 * Results:
5162 * None.
5163 *
5164 * Side effects:
5165 * "length" bytes from "message" are added to the "errorInfo" variable.
5166 * If "length" is negative, use bytes up to the first NULL byte.
5167 * If Tcl_EvalObj has been called since the current value of errorInfo
5168 * was set, errorInfo is cleared before adding the new message.
5169 * If we are just starting to log an error, errorInfo is initialized
5170 * from the error message in the interpreter's result.
5171 *
5172 *----------------------------------------------------------------------
5173 */
5174
5175 void
5176 Tcl_AddObjErrorInfo(interp, message, length)
5177 Tcl_Interp *interp; /* Interpreter to which error information
5178 * pertains. */
5179 CONST char *message; /* Points to the first byte of an array of
5180 * bytes of the message. */
5181 int length; /* The number of bytes in the message.
5182 * If < 0, then append all bytes up to a
5183 * NULL byte. */
5184 {
5185 register Interp *iPtr = (Interp *) interp;
5186 Tcl_Obj *messagePtr;
5187
5188 /*
5189 * If we are just starting to log an error, errorInfo is initialized
5190 * from the error message in the interpreter's result.
5191 */
5192
5193 if (!(iPtr->flags & ERR_IN_PROGRESS)) { /* just starting to log error */
5194 iPtr->flags |= ERR_IN_PROGRESS;
5195
5196 if (iPtr->result[0] == 0) {
5197 Tcl_ObjSetVar2(interp, iPtr->execEnvPtr->errorInfo, NULL,
5198 iPtr->objResultPtr, TCL_GLOBAL_ONLY);
5199 } else { /* use the string result */
5200 Tcl_ObjSetVar2(interp, iPtr->execEnvPtr->errorInfo, NULL,
5201 Tcl_NewStringObj(interp->result, -1), TCL_GLOBAL_ONLY);
5202 }
5203
5204 /*
5205 * If the errorCode variable wasn't set by the code that generated
5206 * the error, set it to "NONE".
5207 */
5208
5209 if (!(iPtr->flags & ERROR_CODE_SET)) {
5210 Tcl_ObjSetVar2(interp, iPtr->execEnvPtr->errorCode, NULL,
5211 Tcl_NewStringObj("NONE", -1), TCL_GLOBAL_ONLY);
5212 }
5213 }
5214
5215 /*
5216 * Now append "message" to the end of errorInfo.
5217 */
5218
5219 if (length != 0) {
5220 messagePtr = Tcl_NewStringObj(message, length);
5221 Tcl_IncrRefCount(messagePtr);
5222 Tcl_ObjSetVar2(interp, iPtr->execEnvPtr->errorInfo, NULL,
5223 messagePtr, (TCL_GLOBAL_ONLY | TCL_APPEND_VALUE));
5224 Tcl_DecrRefCount(messagePtr); /* free msg object appended above */
5225 }
5226 }
5227
5228 /*
5229 *---------------------------------------------------------------------------
5230 *
5231 * Tcl_VarEvalVA --
5232 *
5233 * Given a variable number of string arguments, concatenate them
5234 * all together and execute the result as a Tcl command.
5235 *
5236 * Results:
5237 * A standard Tcl return result. An error message or other result may
5238 * be left in the interp's result.
5239 *
5240 * Side effects:
5241 * Depends on what was done by the command.
5242 *
5243 *---------------------------------------------------------------------------
5244 */
5245
5246 int
5247 Tcl_VarEvalVA (interp, argList)
5248 Tcl_Interp *interp; /* Interpreter in which to evaluate command. */
5249 va_list argList; /* Variable argument list. */
5250 {
5251 Tcl_DString buf;
5252 char *string;
5253 int result;
5254
5255 /*
5256 * Copy the strings one after the other into a single larger
5257 * string. Use stack-allocated space for small commands, but if
5258 * the command gets too large than call ckalloc to create the
5259 * space.
5260 */
5261
5262 Tcl_DStringInit(&buf);
5263 while (1) {
5264 string = va_arg(argList, char *);
5265 if (string == NULL) {
5266 break;
5267 }
5268 Tcl_DStringAppend(&buf, string, -1);
5269 }
5270
5271 result = Tcl_Eval(interp, Tcl_DStringValue(&buf));
5272 Tcl_DStringFree(&buf);
5273 return result;
5274 }
5275
5276 /*
5277 *----------------------------------------------------------------------
5278 *
5279 * Tcl_VarEval --
5280 *
5281 * Given a variable number of string arguments, concatenate them
5282 * all together and execute the result as a Tcl command.
5283 *
5284 * Results:
5285 * A standard Tcl return result. An error message or other
5286 * result may be left in interp->result.
5287 *
5288 * Side effects:
5289 * Depends on what was done by the command.
5290 *
5291 *----------------------------------------------------------------------
5292 */
5293 /* VARARGS2 */ /* ARGSUSED */
5294 int
5295 Tcl_VarEval TCL_VARARGS_DEF(Tcl_Interp *,arg1)
5296 {
5297 Tcl_Interp *interp;
5298 va_list argList;
5299 int result;
5300
5301 interp = TCL_VARARGS_START(Tcl_Interp *,arg1,argList);
5302 result = Tcl_VarEvalVA(interp, argList);
5303 va_end(argList);
5304
5305 return result;
5306 }
5307
5308 /*
5309 *---------------------------------------------------------------------------
5310 *
5311 * Tcl_GlobalEval --
5312 *
5313 * Evaluate a command at global level in an interpreter.
5314 *
5315 * Results:
5316 * A standard Tcl result is returned, and the interp's result is
5317 * modified accordingly.
5318 *
5319 * Side effects:
5320 * The command string is executed in interp, and the execution
5321 * is carried out in the variable context of global level (no
5322 * procedures active), just as if an "uplevel #0" command were
5323 * being executed.
5324 *
5325 ---------------------------------------------------------------------------
5326 */
5327
5328 int
5329 Tcl_GlobalEval(interp, command)
5330 Tcl_Interp *interp; /* Interpreter in which to evaluate command. */
5331 CONST char *command; /* Command to evaluate. */
5332 {
5333 register Interp *iPtr = (Interp *) interp;
5334 int result;
5335 CallFrame *savedVarFramePtr;
5336
5337 savedVarFramePtr = iPtr->varFramePtr;
5338 iPtr->varFramePtr = NULL;
5339 result = Tcl_Eval(interp, command);
5340 iPtr->varFramePtr = savedVarFramePtr;
5341 return result;
5342 }
5343
5344 /*
5345 *----------------------------------------------------------------------
5346 *
5347 * Tcl_SetRecursionLimit --
5348 *
5349 * Set the maximum number of recursive calls that may be active
5350 * for an interpreter at once.
5351 *
5352 * Results:
5353 * The return value is the old limit on nesting for interp.
5354 *
5355 * Side effects:
5356 * None.
5357 *
5358 *----------------------------------------------------------------------
5359 */
5360
5361 int
5362 Tcl_SetRecursionLimit(interp, depth)
5363 Tcl_Interp *interp; /* Interpreter whose nesting limit
5364 * is to be set. */
5365 int depth; /* New value for maximimum depth. */
5366 {
5367 Interp *iPtr = (Interp *) interp;
5368 int old;
5369
5370 old = iPtr->maxNestingDepth;
5371 if (depth > 0) {
5372 iPtr->maxNestingDepth = depth;
5373 }
5374 return old;
5375 }
5376
5377 /*
5378 *----------------------------------------------------------------------
5379 *
5380 * Tcl_AllowExceptions --
5381 *
5382 * Sets a flag in an interpreter so that exceptions can occur
5383 * in the next call to Tcl_Eval without them being turned into
5384 * errors.
5385 *
5386 * Results:
5387 * None.
5388 *
5389 * Side effects:
5390 * The TCL_ALLOW_EXCEPTIONS flag gets set in the interpreter's
5391 * evalFlags structure. See the reference documentation for
5392 * more details.
5393 *
5394 *----------------------------------------------------------------------
5395 */
5396
5397 void
5398 Tcl_AllowExceptions(interp)
5399 Tcl_Interp *interp; /* Interpreter in which to set flag. */
5400 {
5401 Interp *iPtr = (Interp *) interp;
5402
5403 iPtr->evalFlags |= TCL_ALLOW_EXCEPTIONS;
5404 }
5405
5406
5407 /*
5408 *----------------------------------------------------------------------
5409 *
5410 * Tcl_GetVersion
5411 *
5412 * Get the Tcl major, minor, and patchlevel version numbers and
5413 * the release type. A patch is a release type TCL_FINAL_RELEASE
5414 * with a patchLevel > 0.
5415 *
5416 * Results:
5417 * None.
5418 *
5419 * Side effects:
5420 * None.
5421 *
5422 *----------------------------------------------------------------------
5423 */
5424
5425 void
5426 Tcl_GetVersion(majorV, minorV, patchLevelV, type)
5427 int *majorV;
5428 int *minorV;
5429 int *patchLevelV;
5430 int *type;
5431 {
5432 if (majorV != NULL) {
5433 *majorV = TCL_MAJOR_VERSION;
5434 }
5435 if (minorV != NULL) {
5436 *minorV = TCL_MINOR_VERSION;
5437 }
5438 if (patchLevelV != NULL) {
5439 *patchLevelV = TCL_RELEASE_SERIAL;
5440 }
5441 if (type != NULL) {
5442 *type = TCL_RELEASE_LEVEL;
5443 }
5444 }
5445