home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Libraries / tcl7.4b3 / tclBasic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  38.1 KB  |  1,454 lines

  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 parsing and execution.
  7.  *
  8.  * Copyright (c) 1987-1994 The Regents of the University of California.
  9.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  10.  *
  11.  * See the file "license.terms" for information on usage and redistribution
  12.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13.  */
  14.  
  15. #ifndef lint
  16. static char sccsid[] = "@(#) tclBasic.c 1.162 95/02/17 17:22:04";
  17. #endif
  18.  
  19. #include "tclInt.h"
  20. #ifndef TCL_GENERIC_ONLY
  21. #   include "tclPort.h"
  22. #endif
  23.  
  24. /*
  25.  * The following variable holds the name of a user-specific startup script
  26.  * to source if the application is being run nteractively (e.g. "~/.tclshrc").
  27.  * Set by Tcl_AppInit.  NULL means don't source anything ever.
  28.  */
  29.  
  30. char *tcl_RcFileName = NULL;
  31.  
  32. /*
  33.  * The following structure defines all of the commands in the Tcl core,
  34.  * and the C procedures that execute them.
  35.  */
  36.  
  37. typedef struct {
  38.     char *name;            /* Name of command. */
  39.     Tcl_CmdProc *proc;        /* Procedure that executes command. */
  40. } CmdInfo;
  41.  
  42. /*
  43.  * Built-in commands, and the procedures associated with them:
  44.  */
  45.  
  46. static CmdInfo builtInCmds[] = {
  47.     /*
  48.      * Commands in the generic core:
  49.      */
  50.  
  51.     {"append",        Tcl_AppendCmd},
  52.     {"array",        Tcl_ArrayCmd},
  53.     {"break",        Tcl_BreakCmd},
  54.     {"case",        Tcl_CaseCmd},
  55.     {"catch",        Tcl_CatchCmd},
  56.     {"concat",        Tcl_ConcatCmd},
  57.     {"continue",    Tcl_ContinueCmd},
  58.     {"error",        Tcl_ErrorCmd},
  59.     {"eval",        Tcl_EvalCmd},
  60.     {"expr",        Tcl_ExprCmd},
  61.     {"for",        Tcl_ForCmd},
  62.     {"foreach",        Tcl_ForeachCmd},
  63.     {"format",        Tcl_FormatCmd},
  64.     {"global",        Tcl_GlobalCmd},
  65.     {"history",        Tcl_HistoryCmd},
  66.     {"if",        Tcl_IfCmd},
  67.     {"incr",        Tcl_IncrCmd},
  68.     {"info",        Tcl_InfoCmd},
  69.     {"join",        Tcl_JoinCmd},
  70.     {"lappend",        Tcl_LappendCmd},
  71.     {"lindex",        Tcl_LindexCmd},
  72.     {"linsert",        Tcl_LinsertCmd},
  73.     {"list",        Tcl_ListCmd},
  74.     {"llength",        Tcl_LlengthCmd},
  75.     {"lrange",        Tcl_LrangeCmd},
  76.     {"lreplace",    Tcl_LreplaceCmd},
  77.     {"lsearch",        Tcl_LsearchCmd},
  78.     {"lsort",        Tcl_LsortCmd},
  79.     {"proc",        Tcl_ProcCmd},
  80.     {"regexp",        Tcl_RegexpCmd},
  81.     {"regsub",        Tcl_RegsubCmd},
  82.     {"rename",        Tcl_RenameCmd},
  83.     {"return",        Tcl_ReturnCmd},
  84.     {"scan",        Tcl_ScanCmd},
  85.     {"set",        Tcl_SetCmd},
  86.     {"split",        Tcl_SplitCmd},
  87.     {"string",        Tcl_StringCmd},
  88.     {"subst",        Tcl_SubstCmd},
  89.     {"switch",        Tcl_SwitchCmd},
  90.     {"trace",        Tcl_TraceCmd},
  91.     {"unset",        Tcl_UnsetCmd},
  92.     {"uplevel",        Tcl_UplevelCmd},
  93.     {"upvar",        Tcl_UpvarCmd},
  94.     {"while",        Tcl_WhileCmd},
  95.  
  96.     /*
  97.      * Commands in the UNIX core:
  98.      */
  99.  
  100. #ifndef TCL_GENERIC_ONLY
  101.     {"cd",        Tcl_CdCmd},
  102.     {"close",        Tcl_CloseCmd},
  103.     {"eof",        Tcl_EofCmd},
  104.     {"exec",        Tcl_ExecCmd},
  105.     {"exit",        Tcl_ExitCmd},
  106.     {"file",        Tcl_FileCmd},
  107.     {"flush",        Tcl_FlushCmd},
  108.     {"gets",        Tcl_GetsCmd},
  109.     {"glob",        Tcl_GlobCmd},
  110.     {"open",        Tcl_OpenCmd},
  111.     {"pid",        Tcl_PidCmd},
  112.     {"puts",        Tcl_PutsCmd},
  113.     {"pwd",        Tcl_PwdCmd},
  114.     {"read",        Tcl_ReadCmd},
  115.     {"seek",        Tcl_SeekCmd},
  116.     {"source",        Tcl_SourceCmd},
  117.     {"tell",        Tcl_TellCmd},
  118.     {"time",        Tcl_TimeCmd},
  119. #endif /* TCL_GENERIC_ONLY */
  120.     {NULL,        (Tcl_CmdProc *) NULL}
  121. };
  122.  
  123. /*
  124.  *----------------------------------------------------------------------
  125.  *
  126.  * Tcl_CreateInterp --
  127.  *
  128.  *    Create a new TCL command interpreter.
  129.  *
  130.  * Results:
  131.  *    The return value is a token for the interpreter, which may be
  132.  *    used in calls to procedures like Tcl_CreateCmd, Tcl_Eval, or
  133.  *    Tcl_DeleteInterp.
  134.  *
  135.  * Side effects:
  136.  *    The command interpreter is initialized with an empty variable
  137.  *    table and the built-in commands.  SIGPIPE signals are set to
  138.  *    be ignored (see comment below for details).
  139.  *
  140.  *----------------------------------------------------------------------
  141.  */
  142.  
  143. Tcl_Interp *
  144. Tcl_CreateInterp()
  145. {
  146.     register Interp *iPtr;
  147.     register Command *cmdPtr;
  148.     register CmdInfo *cmdInfoPtr;
  149.     int i;
  150.     static int firstInterp = 1;
  151.  
  152.     iPtr = (Interp *) ckalloc(sizeof(Interp));
  153.     iPtr->result = iPtr->resultSpace;
  154.     iPtr->freeProc = 0;
  155.     iPtr->errorLine = 0;
  156.     Tcl_InitHashTable(&iPtr->commandTable, TCL_STRING_KEYS);
  157.     Tcl_InitHashTable(&iPtr->mathFuncTable, TCL_STRING_KEYS);
  158.     Tcl_InitHashTable(&iPtr->globalTable, TCL_STRING_KEYS);
  159.     iPtr->numLevels = 0;
  160.     iPtr->maxNestingDepth = 1000;
  161.     iPtr->framePtr = NULL;
  162.     iPtr->varFramePtr = NULL;
  163.     iPtr->activeTracePtr = NULL;
  164.     iPtr->returnCode = TCL_OK;
  165.     iPtr->errorInfo = NULL;
  166.     iPtr->errorCode = NULL;
  167.     iPtr->numEvents = 0;
  168.     iPtr->events = NULL;
  169.     iPtr->curEvent = 0;
  170.     iPtr->curEventNum = 0;
  171.     iPtr->revPtr = NULL;
  172.     iPtr->historyFirst = NULL;
  173.     iPtr->revDisables = 1;
  174.     iPtr->evalFirst = iPtr->evalLast = NULL;
  175.     iPtr->appendResult = NULL;
  176.     iPtr->appendAvl = 0;
  177.     iPtr->appendUsed = 0;
  178.     for (i = 0; i < NUM_REGEXPS; i++) {
  179.     iPtr->patterns[i] = NULL;
  180.     iPtr->patLengths[i] = -1;
  181.     iPtr->regexps[i] = NULL;
  182.     }
  183.     strcpy(iPtr->pdFormat, DEFAULT_PD_FORMAT);
  184.     iPtr->pdPrec = DEFAULT_PD_PREC;
  185.     iPtr->cmdCount = 0;
  186.     iPtr->noEval = 0;
  187.     iPtr->evalFlags = 0;
  188.     iPtr->scriptFile = NULL;
  189.     iPtr->flags = 0;
  190.     iPtr->tracePtr = NULL;
  191.     iPtr->deleteCallbackPtr = NULL;
  192.     iPtr->resultSpace[0] = 0;
  193.  
  194.     /*
  195.      * Create the built-in commands.  Do it here, rather than calling
  196.      * Tcl_CreateCommand, because it's faster (there's no need to
  197.      * check for a pre-existing command by the same name).
  198.      */
  199.  
  200.     for (cmdInfoPtr = builtInCmds; cmdInfoPtr->name != NULL; cmdInfoPtr++) {
  201.     int new;
  202.     Tcl_HashEntry *hPtr;
  203.  
  204.     hPtr = Tcl_CreateHashEntry(&iPtr->commandTable,
  205.         cmdInfoPtr->name, &new);
  206.     if (new) {
  207.         cmdPtr = (Command *) ckalloc(sizeof(Command));
  208.         cmdPtr->hPtr = hPtr;
  209.         cmdPtr->proc = cmdInfoPtr->proc;
  210.         cmdPtr->clientData = (ClientData) NULL;
  211.         cmdPtr->deleteProc = NULL;
  212.         cmdPtr->deleteData = (ClientData) NULL;
  213.         Tcl_SetHashValue(hPtr, cmdPtr);
  214.     }
  215.     }
  216.  
  217. #ifndef TCL_GENERIC_ONLY
  218.     TclSetupEnv((Tcl_Interp *) iPtr);
  219.  
  220.     /*
  221.      * The code below causes SIGPIPE (broken pipe) errors to
  222.      * be ignored.  This is needed so that Tcl processes don't
  223.      * die if they create child processes (e.g. using "exec" or
  224.      * "open") that terminate prematurely.  The signal handler
  225.      * is only set up when the first interpreter is created; 
  226.      * after this the application can override the handler with
  227.      * a different one of its own, if it wants.
  228.      */
  229.  
  230.     if (firstInterp) {
  231.     (void) signal(SIGPIPE, SIG_IGN);
  232.     firstInterp = 0;
  233.     }
  234. #endif
  235.  
  236.     Tcl_TraceVar2((Tcl_Interp *) iPtr, "tcl_precision", (char *) NULL,
  237.         TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  238.         TclPrecTraceProc, (ClientData) NULL);
  239.     return (Tcl_Interp *) iPtr;
  240. }
  241.  
  242. /*
  243.  *----------------------------------------------------------------------
  244.  *
  245.  * Tcl_Init --
  246.  *
  247.  *    This procedure is typically invoked by Tcl_AppInit procedures
  248.  *    to perform additional initialization for a Tcl interpreter,
  249.  *    such as sourcing the "init.tcl" script.
  250.  *
  251.  * Results:
  252.  *    Returns a standard Tcl completion code and sets interp->result
  253.  *    if there is an error.
  254.  *
  255.  * Side effects:
  256.  *    Depends on what's in the init.tcl script.
  257.  *
  258.  *----------------------------------------------------------------------
  259.  */
  260.  
  261. int
  262. Tcl_Init(interp)
  263.     Tcl_Interp *interp;        /* Interpreter to initialize. */
  264. {
  265.     static char initCmd[] =
  266.     "if [file exists [info library]/init.tcl] {\n\
  267.         source [info library]/init.tcl\n\
  268.     } else {\n\
  269.         set msg \"can't find [info library]/init.tcl; perhaps you \"\n\
  270.         append msg \"need to\\ninstall Tcl or set your TCL_LIBRARY \"\n\
  271.         append msg \"environment variable?\"\n\
  272.         error $msg\n\
  273.     }";
  274.  
  275.     return Tcl_Eval(interp, initCmd);
  276. }
  277.  
  278. /*
  279.  *--------------------------------------------------------------
  280.  *
  281.  * Tcl_CallWhenDeleted --
  282.  *
  283.  *    Arrange for a procedure to be called before a given
  284.  *    interpreter is deleted.
  285.  *
  286.  * Results:
  287.  *    None.
  288.  *
  289.  * Side effects:
  290.  *    When Tcl_DeleteInterp is invoked to delete interp,
  291.  *    proc will be invoked.  See the manual entry for
  292.  *    details.
  293.  *
  294.  *--------------------------------------------------------------
  295.  */
  296.  
  297. void
  298. Tcl_CallWhenDeleted(interp, proc, clientData)
  299.     Tcl_Interp *interp;        /* Interpreter to watch. */
  300.     Tcl_InterpDeleteProc *proc;    /* Procedure to call when interpreter
  301.                  * is about to be deleted. */
  302.     ClientData clientData;    /* One-word value to pass to proc. */
  303. {
  304.     DeleteCallback *dcPtr, *prevPtr;
  305.     Interp *iPtr = (Interp *) interp;
  306.  
  307.     dcPtr = (DeleteCallback *) ckalloc(sizeof(DeleteCallback));
  308.     dcPtr->proc = proc;
  309.     dcPtr->clientData = clientData;
  310.     dcPtr->nextPtr = NULL;
  311.     if (iPtr->deleteCallbackPtr == NULL) {
  312.     iPtr->deleteCallbackPtr = dcPtr;
  313.     } else {
  314.     prevPtr = iPtr->deleteCallbackPtr;
  315.     while (prevPtr->nextPtr != NULL) {
  316.         prevPtr = prevPtr->nextPtr;
  317.     }
  318.     prevPtr->nextPtr = dcPtr;
  319.     }
  320. }
  321.  
  322. /*
  323.  *--------------------------------------------------------------
  324.  *
  325.  * Tcl_DontCallWhenDeleted --
  326.  *
  327.  *    Cancel the arrangement for a procedure to be called when
  328.  *    a given interpreter is deleted.
  329.  *
  330.  * Results:
  331.  *    None.
  332.  *
  333.  * Side effects:
  334.  *    If proc and clientData were previously registered as a
  335.  *    callback via Tcl_CallWhenDeleted, they are unregistered.
  336.  *    If they weren't previously registered then nothing
  337.  *    happens.
  338.  *
  339.  *--------------------------------------------------------------
  340.  */
  341.  
  342. void
  343. Tcl_DontCallWhenDeleted(interp, proc, clientData)
  344.     Tcl_Interp *interp;        /* Interpreter to watch. */
  345.     Tcl_InterpDeleteProc *proc;    /* Procedure to call when interpreter
  346.                  * is about to be deleted. */
  347.     ClientData clientData;    /* One-word value to pass to proc. */
  348. {
  349.     DeleteCallback *prevPtr, *dcPtr;
  350.     Interp *iPtr = (Interp *) interp;
  351.  
  352.     for (prevPtr = NULL, dcPtr = iPtr->deleteCallbackPtr;
  353.         dcPtr != NULL; prevPtr = dcPtr, dcPtr = dcPtr->nextPtr) {
  354.     if ((dcPtr->proc != proc) || (dcPtr->clientData != clientData)) {
  355.         continue;
  356.     }
  357.     if (prevPtr == NULL) {
  358.         iPtr->deleteCallbackPtr = dcPtr->nextPtr;
  359.     } else {
  360.         prevPtr->nextPtr = dcPtr->nextPtr;
  361.     }
  362.     ckfree((char *) dcPtr);
  363.     break;
  364.     }
  365. }
  366.  
  367. /*
  368.  *----------------------------------------------------------------------
  369.  *
  370.  * Tcl_DeleteInterp --
  371.  *
  372.  *    Delete an interpreter and free up all of the resources associated
  373.  *    with it.
  374.  *
  375.  * Results:
  376.  *    None.
  377.  *
  378.  * Side effects:
  379.  *    The interpreter is destroyed.  The caller should never again
  380.  *    use the interp token.
  381.  *
  382.  *----------------------------------------------------------------------
  383.  */
  384.  
  385. void
  386. Tcl_DeleteInterp(interp)
  387.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  388.                  * by a previous call to Tcl_CreateInterp). */
  389. {
  390.     Interp *iPtr = (Interp *) interp;
  391.     Tcl_HashEntry *hPtr;
  392.     Tcl_HashSearch search;
  393.     register Command *cmdPtr;
  394.     DeleteCallback *dcPtr;
  395.     int i;
  396.  
  397.     /*
  398.      * If the interpreter is in use, delay the deletion until later.
  399.      */
  400.  
  401.     iPtr->flags |= DELETED;
  402.     if (iPtr->numLevels != 0) {
  403.     return;
  404.     }
  405.  
  406.     /*
  407.      * Invoke deletion callbacks.
  408.      */
  409.  
  410.     while (iPtr->deleteCallbackPtr != NULL) {
  411.     dcPtr = iPtr->deleteCallbackPtr;
  412.     iPtr->deleteCallbackPtr = dcPtr->nextPtr;
  413.     (*dcPtr->proc)(dcPtr->clientData, interp);
  414.     ckfree((char *) dcPtr);
  415.     }
  416.  
  417.     /*
  418.      * Free up any remaining resources associated with the
  419.      * interpreter.
  420.      */
  421.  
  422.     for (hPtr = Tcl_FirstHashEntry(&iPtr->commandTable, &search);
  423.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  424.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  425.     if (cmdPtr->deleteProc != NULL) { 
  426.         (*cmdPtr->deleteProc)(cmdPtr->deleteData);
  427.     }
  428.     ckfree((char *) cmdPtr);
  429.     }
  430.     Tcl_DeleteHashTable(&iPtr->commandTable);
  431.     for (hPtr = Tcl_FirstHashEntry(&iPtr->mathFuncTable, &search);
  432.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  433.     ckfree((char *) Tcl_GetHashValue(hPtr));
  434.     }
  435.     Tcl_DeleteHashTable(&iPtr->mathFuncTable);
  436.     TclDeleteVars(iPtr, &iPtr->globalTable);
  437.  
  438.     /*
  439.      * Free up the result *after* deleting variables, since variable
  440.      * deletion could have transferred ownership of the result string
  441.      * to Tcl.
  442.      */
  443.  
  444.     Tcl_FreeResult(interp);
  445.     if (iPtr->errorInfo != NULL) {
  446.     ckfree(iPtr->errorInfo);
  447.     }
  448.     if (iPtr->errorCode != NULL) {
  449.     ckfree(iPtr->errorCode);
  450.     }
  451.     if (iPtr->events != NULL) {
  452.     int i;
  453.  
  454.     for (i = 0; i < iPtr->numEvents; i++) {
  455.         ckfree(iPtr->events[i].command);
  456.     }
  457.     ckfree((char *) iPtr->events);
  458.     }
  459.     while (iPtr->revPtr != NULL) {
  460.     HistoryRev *nextPtr = iPtr->revPtr->nextPtr;
  461.  
  462.     ckfree((char *) iPtr->revPtr);
  463.     iPtr->revPtr = nextPtr;
  464.     }
  465.     if (iPtr->appendResult != NULL) {
  466.     ckfree(iPtr->appendResult);
  467.     }
  468.     for (i = 0; i < NUM_REGEXPS; i++) {
  469.     if (iPtr->patterns[i] == NULL) {
  470.         break;
  471.     }
  472.     ckfree(iPtr->patterns[i]);
  473.     ckfree((char *) iPtr->regexps[i]);
  474.     }
  475.     while (iPtr->tracePtr != NULL) {
  476.     Trace *nextPtr = iPtr->tracePtr->nextPtr;
  477.  
  478.     ckfree((char *) iPtr->tracePtr);
  479.     iPtr->tracePtr = nextPtr;
  480.     }
  481.     ckfree((char *) iPtr);
  482. }
  483.  
  484. /*
  485.  *----------------------------------------------------------------------
  486.  *
  487.  * Tcl_CreateCommand --
  488.  *
  489.  *    Define a new command in a command table.
  490.  *
  491.  * Results:
  492.  *    The return value is a token for the command, which can
  493.  *    be used in future calls to Tcl_NameOfCommand.
  494.  *
  495.  * Side effects:
  496.  *    If a command named cmdName already exists for interp, it is
  497.  *    deleted.  In the future, when cmdName is seen as the name of
  498.  *    a command by Tcl_Eval, proc will be called.  When the command
  499.  *    is deleted from the table, deleteProc will be called.  See the
  500.  *    manual entry for details on the calling sequence.
  501.  *
  502.  *----------------------------------------------------------------------
  503.  */
  504.  
  505. Tcl_Command
  506. Tcl_CreateCommand(interp, cmdName, proc, clientData, deleteProc)
  507.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  508.                  * by a previous call to Tcl_CreateInterp). */
  509.     char *cmdName;        /* Name of command. */
  510.     Tcl_CmdProc *proc;        /* Command procedure to associate with
  511.                  * cmdName. */
  512.     ClientData clientData;    /* Arbitrary one-word value to pass to proc. */
  513.     Tcl_CmdDeleteProc *deleteProc;
  514.                 /* If not NULL, gives a procedure to call when
  515.                  * this command is deleted. */
  516. {
  517.     Interp *iPtr = (Interp *) interp;
  518.     register Command *cmdPtr;
  519.     Tcl_HashEntry *hPtr;
  520.     int new;
  521.  
  522.     hPtr = Tcl_CreateHashEntry(&iPtr->commandTable, cmdName, &new);
  523.     if (!new) {
  524.     /*
  525.      * Command already exists:  delete the old one.
  526.      */
  527.  
  528.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  529.     if (cmdPtr->deleteProc != NULL) {
  530.         (*cmdPtr->deleteProc)(cmdPtr->deleteData);
  531.     }
  532.     } else {
  533.     cmdPtr = (Command *) ckalloc(sizeof(Command));
  534.     Tcl_SetHashValue(hPtr, cmdPtr);
  535.     }
  536.     cmdPtr->hPtr = hPtr;
  537.     cmdPtr->proc = proc;
  538.     cmdPtr->clientData = clientData;
  539.     cmdPtr->deleteProc = deleteProc;
  540.     cmdPtr->deleteData = clientData;
  541.     return (Tcl_Command) cmdPtr;
  542. }
  543.  
  544. /*
  545.  *----------------------------------------------------------------------
  546.  *
  547.  * Tcl_SetCommandInfo --
  548.  *
  549.  *    Modifies various information about a Tcl command.
  550.  *
  551.  * Results:
  552.  *    If cmdName exists in interp, then the information at *infoPtr
  553.  *    is stored with the command in place of the current information
  554.  *    and 1 is returned.  If the command doesn't exist then 0 is
  555.  *    returned.
  556.  *
  557.  * Side effects:
  558.  *    None.
  559.  *
  560.  *----------------------------------------------------------------------
  561.  */
  562.  
  563. int
  564. Tcl_SetCommandInfo(interp, cmdName, infoPtr)
  565.     Tcl_Interp *interp;            /* Interpreter in which to look
  566.                      * for command. */
  567.     char *cmdName;            /* Name of desired command. */
  568.     Tcl_CmdInfo *infoPtr;        /* Where to store information about
  569.                      * command. */
  570. {
  571.     Tcl_HashEntry *hPtr;
  572.     Command *cmdPtr;
  573.  
  574.     hPtr = Tcl_FindHashEntry(&((Interp *) interp)->commandTable, cmdName);
  575.     if (hPtr == NULL) {
  576.     return 0;
  577.     }
  578.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  579.     cmdPtr->proc = infoPtr->proc;
  580.     cmdPtr->clientData = infoPtr->clientData;
  581.     cmdPtr->deleteProc = infoPtr->deleteProc;
  582.     cmdPtr->deleteData = infoPtr->deleteData;
  583.     return 1;
  584. }
  585.  
  586. /*
  587.  *----------------------------------------------------------------------
  588.  *
  589.  * Tcl_GetCommandInfo --
  590.  *
  591.  *    Returns various information about a Tcl command.
  592.  *
  593.  * Results:
  594.  *    If cmdName exists in interp, then *infoPtr is modified to
  595.  *    hold information about cmdName and 1 is returned.  If the
  596.  *    command doesn't exist then 0 is returned and *infoPtr isn't
  597.  *    modified.
  598.  *
  599.  * Side effects:
  600.  *    None.
  601.  *
  602.  *----------------------------------------------------------------------
  603.  */
  604.  
  605. int
  606. Tcl_GetCommandInfo(interp, cmdName, infoPtr)
  607.     Tcl_Interp *interp;            /* Interpreter in which to look
  608.                      * for command. */
  609.     char *cmdName;            /* Name of desired command. */
  610.     Tcl_CmdInfo *infoPtr;        /* Where to store information about
  611.                      * command. */
  612. {
  613.     Tcl_HashEntry *hPtr;
  614.     Command *cmdPtr;
  615.  
  616.     hPtr = Tcl_FindHashEntry(&((Interp *) interp)->commandTable, cmdName);
  617.     if (hPtr == NULL) {
  618.     return 0;
  619.     }
  620.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  621.     infoPtr->proc = cmdPtr->proc;
  622.     infoPtr->clientData = cmdPtr->clientData;
  623.     infoPtr->deleteProc = cmdPtr->deleteProc;
  624.     infoPtr->deleteData = cmdPtr->deleteData;
  625.     return 1;
  626. }
  627.  
  628. /*
  629.  *----------------------------------------------------------------------
  630.  *
  631.  * Tcl_GetCommandName --
  632.  *
  633.  *    Given a token returned by Tcl_CreateCommand, this procedure
  634.  *    returns the current name of the command (which may have changed
  635.  *    due to renaming).
  636.  *
  637.  * Results:
  638.  *    The return value is the name of the given command.
  639.  *
  640.  * Side effects:
  641.  *    None.
  642.  *
  643.  *----------------------------------------------------------------------
  644.  */
  645.  
  646. char *
  647. Tcl_GetCommandName(interp, command)
  648.     Tcl_Interp *interp;        /* Interpreter containing the command. */
  649.     Tcl_Command command;    /* Token for the command, returned by a
  650.                  * previous call to Tcl_CreateCommand.
  651.                  * The command must not have been deleted. */
  652. {
  653.     Command *cmdPtr = (Command *) command;
  654.     Interp *iPtr = (Interp *) interp;
  655.  
  656.     return Tcl_GetHashKey(&iPtr->commandTable, cmdPtr->hPtr);
  657. }
  658.  
  659. /*
  660.  *----------------------------------------------------------------------
  661.  *
  662.  * Tcl_DeleteCommand --
  663.  *
  664.  *    Remove the given command from the given interpreter.
  665.  *
  666.  * Results:
  667.  *    0 is returned if the command was deleted successfully.
  668.  *    -1 is returned if there didn't exist a command by that
  669.  *    name.
  670.  *
  671.  * Side effects:
  672.  *    CmdName will no longer be recognized as a valid command for
  673.  *    interp.
  674.  *
  675.  *----------------------------------------------------------------------
  676.  */
  677.  
  678. int
  679. Tcl_DeleteCommand(interp, cmdName)
  680.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  681.                  * by a previous call to Tcl_CreateInterp). */
  682.     char *cmdName;        /* Name of command to remove. */
  683. {
  684.     Interp *iPtr = (Interp *) interp;
  685.     Tcl_HashEntry *hPtr;
  686.     Command *cmdPtr;
  687.  
  688.     hPtr = Tcl_FindHashEntry(&iPtr->commandTable, cmdName);
  689.     if (hPtr == NULL) {
  690.     return -1;
  691.     }
  692.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  693.     if (cmdPtr->deleteProc != NULL) {
  694.     (*cmdPtr->deleteProc)(cmdPtr->deleteData);
  695.     }
  696.     ckfree((char *) cmdPtr);
  697.     Tcl_DeleteHashEntry(hPtr);
  698.     return 0;
  699. }
  700.  
  701. /*
  702.  *-----------------------------------------------------------------
  703.  *
  704.  * Tcl_Eval --
  705.  *
  706.  *    Parse and execute a command in the Tcl language.
  707.  *
  708.  * Results:
  709.  *    The return value is one of the return codes defined in tcl.hd
  710.  *    (such as TCL_OK), and interp->result contains a string value
  711.  *    to supplement the return code.  The value of interp->result
  712.  *    will persist only until the next call to Tcl_Eval:  copy it or
  713.  *    lose it! *TermPtr is filled in with the character just after
  714.  *    the last one that was part of the command (usually a NULL
  715.  *    character or a closing bracket).
  716.  *
  717.  * Side effects:
  718.  *    Almost certainly;  depends on the command.
  719.  *
  720.  *-----------------------------------------------------------------
  721.  */
  722.  
  723. int
  724. Tcl_Eval(interp, cmd)
  725.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  726.                  * by a previous call to Tcl_CreateInterp). */
  727.     char *cmd;            /* Pointer to TCL command to interpret. */
  728. {
  729.     /*
  730.      * The storage immediately below is used to generate a copy
  731.      * of the command, after all argument substitutions.  Pv will
  732.      * contain the argv values passed to the command procedure.
  733.      */
  734.  
  735. #   define NUM_CHARS 200
  736.     char copyStorage[NUM_CHARS];
  737.     ParseValue pv;
  738.     char *oldBuffer;
  739.  
  740.     /*
  741.      * This procedure generates an (argv, argc) array for the command,
  742.      * It starts out with stack-allocated space but uses dynamically-
  743.      * allocated storage to increase it if needed.
  744.      */
  745.  
  746. #   define NUM_ARGS 10
  747.     char *(argStorage[NUM_ARGS]);
  748.     char **argv = argStorage;
  749.     int argc;
  750.     int argSize = NUM_ARGS;
  751.  
  752.     register char *src;            /* Points to current character
  753.                      * in cmd. */
  754.     char termChar;            /* Return when this character is found
  755.                      * (either ']' or '\0').  Zero means
  756.                      * that newlines terminate commands. */
  757.     int flags;                /* Interp->evalFlags value when the
  758.                      * procedure was called. */
  759.     int result;                /* Return value. */
  760.     register Interp *iPtr = (Interp *) interp;
  761.     Tcl_HashEntry *hPtr;
  762.     Command *cmdPtr;
  763.     char *termPtr;            /* Contains character just after the
  764.                      * last one in the command. */
  765.     char *cmdStart;            /* Points to first non-blank char. in
  766.                      * command (used in calling trace
  767.                      * procedures). */
  768.     char *ellipsis = "";        /* Used in setting errorInfo variable;
  769.                      * set to "..." to indicate that not
  770.                      * all of offending command is included
  771.                      * in errorInfo.  "" means that the
  772.                      * command is all there. */
  773.     register Trace *tracePtr;
  774.     int oldCount = iPtr->cmdCount;    /* Used to tell whether any commands
  775.                      * at all were executed. */
  776.  
  777.     /*
  778.      * Initialize the result to an empty string and clear out any
  779.      * error information.  This makes sure that we return an empty
  780.      * result if there are no commands in the command string.
  781.      */
  782.  
  783.     Tcl_FreeResult((Tcl_Interp *) iPtr);
  784.     iPtr->result = iPtr->resultSpace;
  785.     iPtr->resultSpace[0] = 0;
  786.     result = TCL_OK;
  787.  
  788.     /*
  789.      * Initialize the area in which command copies will be assembled.
  790.      */
  791.  
  792.     pv.buffer = copyStorage;
  793.     pv.end = copyStorage + NUM_CHARS - 1;
  794.     pv.expandProc = TclExpandParseValue;
  795.     pv.clientData = (ClientData) NULL;
  796.  
  797.     src = cmd;
  798.     flags = iPtr->evalFlags;
  799.     iPtr->evalFlags = 0;
  800.     if (flags & TCL_BRACKET_TERM) {
  801.     termChar = ']';
  802.     } else {
  803.     termChar = 0;
  804.     }
  805.     termPtr = src;
  806.     cmdStart = src;
  807.  
  808.     /*
  809.      * Check depth of nested calls to Tcl_Eval:  if this gets too large,
  810.      * it's probably because of an infinite loop somewhere.
  811.      */
  812.  
  813.     iPtr->numLevels++;
  814.     if (iPtr->numLevels > iPtr->maxNestingDepth) {
  815.     iPtr->numLevels--;
  816.     iPtr->result =  "too many nested calls to Tcl_Eval (infinite loop?)";
  817.     iPtr->termPtr = termPtr;
  818.     return TCL_ERROR;
  819.     }
  820.  
  821.     /*
  822.      * There can be many sub-commands (separated by semi-colons or
  823.      * newlines) in one command string.  This outer loop iterates over
  824.      * individual commands.
  825.      */
  826.  
  827.     while (*src != termChar) {
  828.     iPtr->flags &= ~(ERR_IN_PROGRESS | ERROR_CODE_SET);
  829.  
  830.     /*
  831.      * Skim off leading white space and semi-colons, and skip
  832.      * comments.
  833.      */
  834.  
  835.     while (1) {
  836.         register char c = *src;
  837.  
  838.         if ((CHAR_TYPE(c) != TCL_SPACE) && (c != ';') && (c != '\n')) {
  839.         break;
  840.         }
  841.         src += 1;
  842.     }
  843.     if (*src == '#') {
  844.         for (src++; *src != 0; src++) {
  845.         if ((*src == '\n') && (src[-1] != '\\')) {
  846.             src++;
  847.             break;
  848.         }
  849.         }
  850.         continue;
  851.     }
  852.     cmdStart = src;
  853.  
  854.     /*
  855.      * Parse the words of the command, generating the argc and
  856.      * argv for the command procedure.  May have to call
  857.      * TclParseWords several times, expanding the argv array
  858.      * between calls.
  859.      */
  860.  
  861.     pv.next = oldBuffer = pv.buffer;
  862.     argc = 0;
  863.     while (1) {
  864.         int newArgs, maxArgs;
  865.         char **newArgv;
  866.         int i;
  867.  
  868.         /*
  869.          * Note:  the "- 2" below guarantees that we won't use the
  870.          * last two argv slots here.  One is for a NULL pointer to
  871.          * mark the end of the list, and the other is to leave room
  872.          * for inserting the command name "unknown" as the first
  873.          * argument (see below).
  874.          */
  875.  
  876.         maxArgs = argSize - argc - 2;
  877.         result = TclParseWords((Tcl_Interp *) iPtr, src, flags,
  878.             maxArgs, &termPtr, &newArgs, &argv[argc], &pv);
  879.         src = termPtr;
  880.         if (result != TCL_OK) {
  881.         ellipsis = "...";
  882.         goto done;
  883.         }
  884.  
  885.         /*
  886.          * Careful!  Buffer space may have gotten reallocated while
  887.          * parsing words.  If this happened, be sure to update all
  888.          * of the older argv pointers to refer to the new space.
  889.          */
  890.  
  891.         if (oldBuffer != pv.buffer) {
  892.         int i;
  893.  
  894.         for (i = 0; i < argc; i++) {
  895.             argv[i] = pv.buffer + (argv[i] - oldBuffer);
  896.         }
  897.         oldBuffer = pv.buffer;
  898.         }
  899.         argc += newArgs;
  900.         if (newArgs < maxArgs) {
  901.         argv[argc] = (char *) NULL;
  902.         break;
  903.         }
  904.  
  905.         /*
  906.          * Args didn't all fit in the current array.  Make it bigger.
  907.          */
  908.  
  909.         argSize *= 2;
  910.         newArgv = (char **)
  911.             ckalloc((unsigned) argSize * sizeof(char *));
  912.         for (i = 0; i < argc; i++) {
  913.         newArgv[i] = argv[i];
  914.         }
  915.         if (argv != argStorage) {
  916.         ckfree((char *) argv);
  917.         }
  918.         argv = newArgv;
  919.     }
  920.  
  921.     /*
  922.      * If this is an empty command (or if we're just parsing
  923.      * commands without evaluating them), then just skip to the
  924.      * next command.
  925.      */
  926.  
  927.     if ((argc == 0) || iPtr->noEval) {
  928.         continue;
  929.     }
  930.     argv[argc] = NULL;
  931.  
  932.     /*
  933.      * Save information for the history module, if needed.
  934.      */
  935.  
  936.     if (flags & TCL_RECORD_BOUNDS) {
  937.         iPtr->evalFirst = cmdStart;
  938.         iPtr->evalLast = src-1;
  939.     }
  940.  
  941.     /*
  942.      * Find the procedure to execute this command.  If there isn't
  943.      * one, then see if there is a command "unknown".  If so,
  944.      * invoke it instead, passing it the words of the original
  945.      * command as arguments.
  946.      */
  947.  
  948.     hPtr = Tcl_FindHashEntry(&iPtr->commandTable, argv[0]);
  949.     if (hPtr == NULL) {
  950.         int i;
  951.  
  952.         hPtr = Tcl_FindHashEntry(&iPtr->commandTable, "unknown");
  953.         if (hPtr == NULL) {
  954.         Tcl_ResetResult(interp);
  955.         Tcl_AppendResult(interp, "invalid command name \"",
  956.             argv[0], "\"", (char *) NULL);
  957.         result = TCL_ERROR;
  958.         goto done;
  959.         }
  960.         for (i = argc; i >= 0; i--) {
  961.         argv[i+1] = argv[i];
  962.         }
  963.         argv[0] = "unknown";
  964.         argc++;
  965.     }
  966.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  967.  
  968.     /*
  969.      * Call trace procedures, if any.
  970.      */
  971.  
  972.     for (tracePtr = iPtr->tracePtr; tracePtr != NULL;
  973.         tracePtr = tracePtr->nextPtr) {
  974.         char saved;
  975.  
  976.         if (tracePtr->level < iPtr->numLevels) {
  977.         continue;
  978.         }
  979.         saved = *src;
  980.         *src = 0;
  981.         (*tracePtr->proc)(tracePtr->clientData, interp, iPtr->numLevels,
  982.             cmdStart, cmdPtr->proc, cmdPtr->clientData, argc, argv);
  983.         *src = saved;
  984.     }
  985.  
  986.     /*
  987.      * At long last, invoke the command procedure.  Reset the
  988.      * result to its default empty value first (it could have
  989.      * gotten changed by earlier commands in the same command
  990.      * string).
  991.      */
  992.  
  993.     iPtr->cmdCount++;
  994.     Tcl_FreeResult(iPtr);
  995.     iPtr->result = iPtr->resultSpace;
  996.     iPtr->resultSpace[0] = 0;
  997.     result = (*cmdPtr->proc)(cmdPtr->clientData, interp, argc, argv);
  998.     if (tcl_AsyncReady) {
  999.         result = Tcl_AsyncInvoke(interp, result);
  1000.     }
  1001.     if (result != TCL_OK) {
  1002.         break;
  1003.     }
  1004.     }
  1005.  
  1006.     done:
  1007.  
  1008.     /*
  1009.      * If no commands at all were executed, check for asynchronous
  1010.      * handlers so that they at least get one change to execute.
  1011.      * This is needed to handle event loops written in Tcl with
  1012.      * empty bodies (I'm not sure that loops like this are a good
  1013.      * idea, * but...).
  1014.      */
  1015.  
  1016.     if ((oldCount == iPtr->cmdCount) && (tcl_AsyncReady)) {
  1017.     result = Tcl_AsyncInvoke(interp, result);
  1018.     }
  1019.  
  1020.     /*
  1021.      * Free up any extra resources that were allocated.
  1022.      */
  1023.  
  1024.     if (pv.buffer != copyStorage) {
  1025.     ckfree((char *) pv.buffer);
  1026.     }
  1027.     if (argv != argStorage) {
  1028.     ckfree((char *) argv);
  1029.     }
  1030.     iPtr->numLevels--;
  1031.     if (iPtr->numLevels == 0) {
  1032.     if (result == TCL_RETURN) {
  1033.         result = TCL_OK;
  1034.     }
  1035.     if ((result != TCL_OK) && (result != TCL_ERROR)
  1036.         && !(flags & TCL_ALLOW_EXCEPTIONS)) {
  1037.         Tcl_ResetResult(interp);
  1038.         if (result == TCL_BREAK) {
  1039.         iPtr->result = "invoked \"break\" outside of a loop";
  1040.         } else if (result == TCL_CONTINUE) {
  1041.         iPtr->result = "invoked \"continue\" outside of a loop";
  1042.         } else {
  1043.         iPtr->result = iPtr->resultSpace;
  1044.         sprintf(iPtr->resultSpace, "command returned bad code: %d",
  1045.             result);
  1046.         }
  1047.         result = TCL_ERROR;
  1048.     }
  1049.     if (iPtr->flags & DELETED) {
  1050.         Tcl_DeleteInterp(interp);
  1051.     }
  1052.     }
  1053.  
  1054.     /*
  1055.      * If an error occurred, record information about what was being
  1056.      * executed when the error occurred.
  1057.      */
  1058.  
  1059.     if ((result == TCL_ERROR) && !(iPtr->flags & ERR_ALREADY_LOGGED)) {
  1060.     int numChars;
  1061.     register char *p;
  1062.  
  1063.     /*
  1064.      * Compute the line number where the error occurred.
  1065.      */
  1066.  
  1067.     iPtr->errorLine = 1;
  1068.     for (p = cmd; p != cmdStart; p++) {
  1069.         if (*p == '\n') {
  1070.         iPtr->errorLine++;
  1071.         }
  1072.     }
  1073.     for ( ; isspace(UCHAR(*p)) || (*p == ';'); p++) {
  1074.         if (*p == '\n') {
  1075.         iPtr->errorLine++;
  1076.         }
  1077.     }
  1078.  
  1079.     /*
  1080.      * Figure out how much of the command to print in the error
  1081.      * message (up to a certain number of characters, or up to
  1082.      * the first new-line).
  1083.      */
  1084.  
  1085.     numChars = src - cmdStart;
  1086.     if (numChars > (NUM_CHARS-50)) {
  1087.         numChars = NUM_CHARS-50;
  1088.         ellipsis = " ...";
  1089.     }
  1090.  
  1091.     if (!(iPtr->flags & ERR_IN_PROGRESS)) {
  1092.         sprintf(copyStorage, "\n    while executing\n\"%.*s%s\"",
  1093.             numChars, cmdStart, ellipsis);
  1094.     } else {
  1095.         sprintf(copyStorage, "\n    invoked from within\n\"%.*s%s\"",
  1096.             numChars, cmdStart, ellipsis);
  1097.     }
  1098.     Tcl_AddErrorInfo(interp, copyStorage);
  1099.     iPtr->flags &= ~ERR_ALREADY_LOGGED;
  1100.     } else {
  1101.     iPtr->flags &= ~ERR_ALREADY_LOGGED;
  1102.     }
  1103.     iPtr->termPtr = termPtr;
  1104.     return result;
  1105. }
  1106.  
  1107. /*
  1108.  *----------------------------------------------------------------------
  1109.  *
  1110.  * Tcl_CreateTrace --
  1111.  *
  1112.  *    Arrange for a procedure to be called to trace command execution.
  1113.  *
  1114.  * Results:
  1115.  *    The return value is a token for the trace, which may be passed
  1116.  *    to Tcl_DeleteTrace to eliminate the trace.
  1117.  *
  1118.  * Side effects:
  1119.  *    From now on, proc will be called just before a command procedure
  1120.  *    is called to execute a Tcl command.  Calls to proc will have the
  1121.  *    following form:
  1122.  *
  1123.  *    void
  1124.  *    proc(clientData, interp, level, command, cmdProc, cmdClientData,
  1125.  *        argc, argv)
  1126.  *        ClientData clientData;
  1127.  *        Tcl_Interp *interp;
  1128.  *        int level;
  1129.  *        char *command;
  1130.  *        int (*cmdProc)();
  1131.  *        ClientData cmdClientData;
  1132.  *        int argc;
  1133.  *        char **argv;
  1134.  *    {
  1135.  *    }
  1136.  *
  1137.  *    The clientData and interp arguments to proc will be the same
  1138.  *    as the corresponding arguments to this procedure.  Level gives
  1139.  *    the nesting level of command interpretation for this interpreter
  1140.  *    (0 corresponds to top level).  Command gives the ASCII text of
  1141.  *    the raw command, cmdProc and cmdClientData give the procedure that
  1142.  *    will be called to process the command and the ClientData value it
  1143.  *    will receive, and argc and argv give the arguments to the
  1144.  *    command, after any argument parsing and substitution.  Proc
  1145.  *    does not return a value.
  1146.  *
  1147.  *----------------------------------------------------------------------
  1148.  */
  1149.  
  1150. Tcl_Trace
  1151. Tcl_CreateTrace(interp, level, proc, clientData)
  1152.     Tcl_Interp *interp;        /* Interpreter in which to create the trace. */
  1153.     int level;            /* Only call proc for commands at nesting level
  1154.                  * <= level (1 => top level). */
  1155.     Tcl_CmdTraceProc *proc;    /* Procedure to call before executing each
  1156.                  * command. */
  1157.     ClientData clientData;    /* Arbitrary one-word value to pass to proc. */
  1158. {
  1159.     register Trace *tracePtr;
  1160.     register Interp *iPtr = (Interp *) interp;
  1161.  
  1162.     tracePtr = (Trace *) ckalloc(sizeof(Trace));
  1163.     tracePtr->level = level;
  1164.     tracePtr->proc = proc;
  1165.     tracePtr->clientData = clientData;
  1166.     tracePtr->nextPtr = iPtr->tracePtr;
  1167.     iPtr->tracePtr = tracePtr;
  1168.  
  1169.     return (Tcl_Trace) tracePtr;
  1170. }
  1171.  
  1172. /*
  1173.  *----------------------------------------------------------------------
  1174.  *
  1175.  * Tcl_DeleteTrace --
  1176.  *
  1177.  *    Remove a trace.
  1178.  *
  1179.  * Results:
  1180.  *    None.
  1181.  *
  1182.  * Side effects:
  1183.  *    From now on there will be no more calls to the procedure given
  1184.  *    in trace.
  1185.  *
  1186.  *----------------------------------------------------------------------
  1187.  */
  1188.  
  1189. void
  1190. Tcl_DeleteTrace(interp, trace)
  1191.     Tcl_Interp *interp;        /* Interpreter that contains trace. */
  1192.     Tcl_Trace trace;        /* Token for trace (returned previously by
  1193.                  * Tcl_CreateTrace). */
  1194. {
  1195.     register Interp *iPtr = (Interp *) interp;
  1196.     register Trace *tracePtr = (Trace *) trace;
  1197.     register Trace *tracePtr2;
  1198.  
  1199.     if (iPtr->tracePtr == tracePtr) {
  1200.     iPtr->tracePtr = tracePtr->nextPtr;
  1201.     ckfree((char *) tracePtr);
  1202.     } else {
  1203.     for (tracePtr2 = iPtr->tracePtr; tracePtr2 != NULL;
  1204.         tracePtr2 = tracePtr2->nextPtr) {
  1205.         if (tracePtr2->nextPtr == tracePtr) {
  1206.         tracePtr2->nextPtr = tracePtr->nextPtr;
  1207.         ckfree((char *) tracePtr);
  1208.         return;
  1209.         }
  1210.     }
  1211.     }
  1212. }
  1213.  
  1214. /*
  1215.  *----------------------------------------------------------------------
  1216.  *
  1217.  * Tcl_AddErrorInfo --
  1218.  *
  1219.  *    Add information to a message being accumulated that describes
  1220.  *    the current error.
  1221.  *
  1222.  * Results:
  1223.  *    None.
  1224.  *
  1225.  * Side effects:
  1226.  *    The contents of message are added to the "errorInfo" variable.
  1227.  *    If Tcl_Eval has been called since the current value of errorInfo
  1228.  *    was set, errorInfo is cleared before adding the new message.
  1229.  *
  1230.  *----------------------------------------------------------------------
  1231.  */
  1232.  
  1233. void
  1234. Tcl_AddErrorInfo(interp, message)
  1235.     Tcl_Interp *interp;        /* Interpreter to which error information
  1236.                  * pertains. */
  1237.     char *message;        /* Message to record. */
  1238. {
  1239.     register Interp *iPtr = (Interp *) interp;
  1240.  
  1241.     /*
  1242.      * If an error is already being logged, then the new errorInfo
  1243.      * is the concatenation of the old info and the new message.
  1244.      * If this is the first piece of info for the error, then the
  1245.      * new errorInfo is the concatenation of the message in
  1246.      * interp->result and the new message.
  1247.      */
  1248.  
  1249.     if (!(iPtr->flags & ERR_IN_PROGRESS)) {
  1250.     Tcl_SetVar2(interp, "errorInfo", (char *) NULL, interp->result,
  1251.         TCL_GLOBAL_ONLY);
  1252.     iPtr->flags |= ERR_IN_PROGRESS;
  1253.  
  1254.     /*
  1255.      * If the errorCode variable wasn't set by the code that generated
  1256.      * the error, set it to "NONE".
  1257.      */
  1258.  
  1259.     if (!(iPtr->flags & ERROR_CODE_SET)) {
  1260.         (void) Tcl_SetVar2(interp, "errorCode", (char *) NULL, "NONE",
  1261.             TCL_GLOBAL_ONLY);
  1262.     }
  1263.     }
  1264.     Tcl_SetVar2(interp, "errorInfo", (char *) NULL, message,
  1265.         TCL_GLOBAL_ONLY|TCL_APPEND_VALUE);
  1266. }
  1267.  
  1268. /*
  1269.  *----------------------------------------------------------------------
  1270.  *
  1271.  * Tcl_VarEval --
  1272.  *
  1273.  *    Given a variable number of string arguments, concatenate them
  1274.  *    all together and execute the result as a Tcl command.
  1275.  *
  1276.  * Results:
  1277.  *    A standard Tcl return result.  An error message or other
  1278.  *    result may be left in interp->result.
  1279.  *
  1280.  * Side effects:
  1281.  *    Depends on what was done by the command.
  1282.  *
  1283.  *----------------------------------------------------------------------
  1284.  */
  1285.     /* VARARGS2 */ /* ARGSUSED */
  1286. int
  1287. #ifndef lint
  1288. Tcl_VarEval(va_alist)
  1289. #else
  1290. Tcl_VarEval(iPtr, p, va_alist)
  1291.     Tcl_Interp *iPtr;        /* Interpreter in which to execute command. */
  1292.     char *p;            /* One or more strings to concatenate,
  1293.                  * terminated with a NULL string. */
  1294. #endif
  1295.     va_dcl
  1296. {
  1297.     va_list argList;
  1298. #define FIXED_SIZE 200
  1299.     char fixedSpace[FIXED_SIZE+1];
  1300.     int spaceAvl, spaceUsed, length;
  1301.     char *string, *cmd;
  1302.     Tcl_Interp *interp;
  1303.     int result;
  1304.  
  1305.     /*
  1306.      * Copy the strings one after the other into a single larger
  1307.      * string.  Use stack-allocated space for small commands, but if
  1308.      * the command gets too large than call ckalloc to create the
  1309.      * space.
  1310.      */
  1311.  
  1312.     va_start(argList);
  1313.     interp = va_arg(argList, Tcl_Interp *);
  1314.     spaceAvl = FIXED_SIZE;
  1315.     spaceUsed = 0;
  1316.     cmd = fixedSpace;
  1317.     while (1) {
  1318.     string = va_arg(argList, char *);
  1319.     if (string == NULL) {
  1320.         break;
  1321.     }
  1322.     length = strlen(string);
  1323.     if ((spaceUsed + length) > spaceAvl) {
  1324.         char *new;
  1325.  
  1326.         spaceAvl = spaceUsed + length;
  1327.         spaceAvl += spaceAvl/2;
  1328.         new = ckalloc((unsigned) spaceAvl);
  1329.         memcpy((VOID *) new, (VOID *) cmd, (size_t) spaceUsed);
  1330.         if (cmd != fixedSpace) {
  1331.         ckfree(cmd);
  1332.         }
  1333.         cmd = new;
  1334.     }
  1335.     strcpy(cmd + spaceUsed, string);
  1336.     spaceUsed += length;
  1337.     }
  1338.     va_end(argList);
  1339.     cmd[spaceUsed] = '\0';
  1340.  
  1341.     result = Tcl_Eval(interp, cmd);
  1342.     if (cmd != fixedSpace) {
  1343.     ckfree(cmd);
  1344.     }
  1345.     return result;
  1346. }
  1347.  
  1348. /*
  1349.  *----------------------------------------------------------------------
  1350.  *
  1351.  * Tcl_GlobalEval --
  1352.  *
  1353.  *    Evaluate a command at global level in an interpreter.
  1354.  *
  1355.  * Results:
  1356.  *    A standard Tcl result is returned, and interp->result is
  1357.  *    modified accordingly.
  1358.  *
  1359.  * Side effects:
  1360.  *    The command string is executed in interp, and the execution
  1361.  *    is carried out in the variable context of global level (no
  1362.  *    procedures active), just as if an "uplevel #0" command were
  1363.  *    being executed.
  1364.  *
  1365.  *----------------------------------------------------------------------
  1366.  */
  1367.  
  1368. int
  1369. Tcl_GlobalEval(interp, command)
  1370.     Tcl_Interp *interp;        /* Interpreter in which to evaluate command. */
  1371.     char *command;        /* Command to evaluate. */
  1372. {
  1373.     register Interp *iPtr = (Interp *) interp;
  1374.     int result;
  1375.     CallFrame *savedVarFramePtr;
  1376.  
  1377.     savedVarFramePtr = iPtr->varFramePtr;
  1378.     iPtr->varFramePtr = NULL;
  1379.     result = Tcl_Eval(interp, command);
  1380.     iPtr->varFramePtr = savedVarFramePtr;
  1381.     return result;
  1382. }
  1383.  
  1384. /*
  1385.  *----------------------------------------------------------------------
  1386.  *
  1387.  * Tcl_SetRecursionLimit --
  1388.  *
  1389.  *    Set the maximum number of recursive calls that may be active
  1390.  *    for an interpreter at once.
  1391.  *
  1392.  * Results:
  1393.  *    The return value is the old limit on nesting for interp.
  1394.  *
  1395.  * Side effects:
  1396.  *    None.
  1397.  *
  1398.  *----------------------------------------------------------------------
  1399.  */
  1400.  
  1401. int
  1402. Tcl_SetRecursionLimit(interp, depth)
  1403.     Tcl_Interp *interp;            /* Interpreter whose nesting limit
  1404.                      * is to be set. */
  1405.     int depth;                /* New value for maximimum depth. */
  1406. {
  1407.     Interp *iPtr = (Interp *) interp;
  1408.     int old;
  1409.  
  1410.     old = iPtr->maxNestingDepth;
  1411.     if (depth > 0) {
  1412.     iPtr->maxNestingDepth = depth;
  1413.     }
  1414.     return old;
  1415. }
  1416.  
  1417. /*
  1418.  *----------------------------------------------------------------------
  1419.  *
  1420.  * Tcl_AllowExceptions --
  1421.  *
  1422.  *    Sets a flag in an interpreter so that exceptions can occur
  1423.  *    in the next call to Tcl_Eval without them being turned into
  1424.  *    errors.
  1425.  *
  1426.  * Results:
  1427.  *    None.
  1428.  *
  1429.  * Side effects:
  1430.  *    The TCL_ALLOW_EXCEPTIONS flag gets set in the interpreter's
  1431.  *    evalFlags structure.  See the reference documentation for
  1432.  *    more details.
  1433.  *
  1434.  *----------------------------------------------------------------------
  1435.  */
  1436.  
  1437. void
  1438. Tcl_AllowExceptions(interp)
  1439.     Tcl_Interp *interp;        /* Interpreter in which to set flag. */
  1440. {
  1441.     Interp *iPtr = (Interp *) interp;
  1442.  
  1443.     iPtr->evalFlags |= TCL_ALLOW_EXCEPTIONS;
  1444. }
  1445.  
  1446. int
  1447. evil_Tcl_Level_written_by_wave(interp)
  1448.     Tcl_Interp *interp;
  1449. {
  1450.     Interp *iPtr = (Interp *) interp;
  1451.  
  1452.     return (iPtr->numLevels);
  1453. }
  1454.