tclHistory.c

Go to the documentation of this file.
00001 /*
00002  * tclHistory.c --
00003  *
00004  *      This module and the Tcl library file history.tcl together implement
00005  *      Tcl command history. Tcl_RecordAndEval(Obj) can be called to record
00006  *      commands ("events") before they are executed. Commands defined in
00007  *      history.tcl may be used to perform history substitutions.
00008  *
00009  * Copyright (c) 1990-1993 The Regents of the University of California.
00010  * Copyright (c) 1994-1997 Sun Microsystems, Inc.
00011  *
00012  * See the file "license.terms" for information on usage and redistribution of
00013  * this file, and for a DISCLAIMER OF ALL WARRANTIES.
00014  *
00015  * RCS: @(#) $Id: tclHistory.c,v 1.10 2007/04/10 14:47:15 dkf Exp $
00016  */
00017 
00018 #include "tclInt.h"
00019 
00020 /*
00021  *----------------------------------------------------------------------
00022  *
00023  * Tcl_RecordAndEval --
00024  *
00025  *      This procedure adds its command argument to the current list of
00026  *      recorded events and then executes the command by calling Tcl_Eval.
00027  *
00028  * Results:
00029  *      The return value is a standard Tcl return value, the result of
00030  *      executing cmd.
00031  *
00032  * Side effects:
00033  *      The command is recorded and executed.
00034  *
00035  *----------------------------------------------------------------------
00036  */
00037 
00038 int
00039 Tcl_RecordAndEval(
00040     Tcl_Interp *interp,         /* Token for interpreter in which command will
00041                                  * be executed. */
00042     CONST char *cmd,            /* Command to record. */
00043     int flags)                  /* Additional flags. TCL_NO_EVAL means only
00044                                  * record: don't execute command.
00045                                  * TCL_EVAL_GLOBAL means use Tcl_GlobalEval
00046                                  * instead of Tcl_Eval. */
00047 {
00048     register Tcl_Obj *cmdPtr;
00049     int length = strlen(cmd);
00050     int result;
00051 
00052     if (length > 0) {
00053         /*
00054          * Call Tcl_RecordAndEvalObj to do the actual work.
00055          */
00056 
00057         cmdPtr = Tcl_NewStringObj(cmd, length);
00058         Tcl_IncrRefCount(cmdPtr);
00059         result = Tcl_RecordAndEvalObj(interp, cmdPtr, flags);
00060 
00061         /*
00062          * Move the interpreter's object result to the string result, then
00063          * reset the object result.
00064          */
00065 
00066         (void) Tcl_GetStringResult(interp);
00067 
00068         /*
00069          * Discard the Tcl object created to hold the command.
00070          */
00071 
00072         Tcl_DecrRefCount(cmdPtr);
00073     } else {
00074         /*
00075          * An empty string. Just reset the interpreter's result.
00076          */
00077 
00078         Tcl_ResetResult(interp);
00079         result = TCL_OK;
00080     }
00081     return result;
00082 }
00083 
00084 /*
00085  *----------------------------------------------------------------------
00086  *
00087  * Tcl_RecordAndEvalObj --
00088  *
00089  *      This procedure adds the command held in its argument object to the
00090  *      current list of recorded events and then executes the command by
00091  *      calling Tcl_EvalObj.
00092  *
00093  * Results:
00094  *      The return value is a standard Tcl return value, the result of
00095  *      executing the command.
00096  *
00097  * Side effects:
00098  *      The command is recorded and executed.
00099  *
00100  *----------------------------------------------------------------------
00101  */
00102 
00103 int
00104 Tcl_RecordAndEvalObj(
00105     Tcl_Interp *interp,         /* Token for interpreter in which command will
00106                                  * be executed. */
00107     Tcl_Obj *cmdPtr,            /* Points to object holding the command to
00108                                  * record and execute. */
00109     int flags)                  /* Additional flags. TCL_NO_EVAL means record
00110                                  * only: don't execute the command.
00111                                  * TCL_EVAL_GLOBAL means evaluate the script
00112                                  * in global variable context instead of the
00113                                  * current procedure. */
00114 {
00115     int result, call = 1;
00116     Tcl_Obj *list[3];
00117     register Tcl_Obj *objPtr;
00118     Tcl_CmdInfo info;
00119 
00120     /*
00121      * Do not call [history] if it has been replaced by an empty proc
00122      */
00123 
00124     result = Tcl_GetCommandInfo(interp, "history", &info);
00125 
00126     if (result && (info.objProc == TclObjInterpProc)) {
00127         Proc *procPtr = (Proc *)(info.objClientData);
00128         call = (procPtr->cmdPtr->compileProc != TclCompileNoOp);
00129     }
00130 
00131     if (call) {
00132 
00133         /*
00134          * Do recording by eval'ing a tcl history command: history add $cmd. 
00135          */
00136 
00137         TclNewLiteralStringObj(list[0], "history");
00138         TclNewLiteralStringObj(list[1], "add");
00139         list[2] = cmdPtr;
00140         
00141         objPtr = Tcl_NewListObj(3, list);
00142         Tcl_IncrRefCount(objPtr);
00143         (void) Tcl_EvalObjEx(interp, objPtr, TCL_EVAL_GLOBAL);
00144         Tcl_DecrRefCount(objPtr);
00145         
00146         /*
00147          * One possible failure mode above: exceeding a resource limit.
00148          */
00149         
00150         if (Tcl_LimitExceeded(interp)) {
00151             return TCL_ERROR;
00152         }
00153     }
00154 
00155     /*
00156      * Execute the command.
00157      */
00158 
00159     result = TCL_OK;
00160     if (!(flags & TCL_NO_EVAL)) {
00161         result = Tcl_EvalObjEx(interp, cmdPtr, flags & TCL_EVAL_GLOBAL);
00162     }
00163     return result;
00164 }
00165 
00166 /*
00167  * Local Variables:
00168  * mode: c
00169  * c-basic-offset: 4
00170  * fill-column: 78
00171  * End:
00172  */



Generated on Wed Mar 12 12:18:16 2008 by  doxygen 1.5.1