Debian Codegraph @

generic/tclHistory.c

raw
1 /*
2 * tclHistory.c --
3 *
4 * This module and the Tcl library file history.tcl together implement
5 * Tcl command history. Tcl_RecordAndEval(Obj) can be called to record
6 * commands ("events") before they are executed. Commands defined in
7 * history.tcl may be used to perform history substitutions.
8 *
9 * Copyright (c) 1990-1993 The Regents of the University of California.
10 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
11 *
12 * See the file "license.terms" for information on usage and redistribution of
13 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 */
15
16 #include "tclInt.h"
17
18 /*
19 * Type of the assocData structure used to hold the reference to the [history
20 * add] subcommand, used in Tcl_RecordAndEvalObj.
21 */
22
23 typedef struct {
24 Tcl_Obj *historyObj; /* == "::history" */
25 Tcl_Obj *addObj; /* == "add" */
26 } HistoryObjs;
27
28 #define HISTORY_OBJS_KEY "::tcl::HistoryObjs"
29
30 /*
31 * Static functions in this file.
32 */
33
34 static Tcl_InterpDeleteProc DeleteHistoryObjs;
35
36 /*
37 *----------------------------------------------------------------------
38 *
39 * Tcl_RecordAndEval --
40 *
41 * This procedure adds its command argument to the current list of
42 * recorded events and then executes the command by calling Tcl_Eval.
43 *
44 * Results:
45 * The return value is a standard Tcl return value, the result of
46 * executing cmd.
47 *
48 * Side effects:
49 * The command is recorded and executed.
50 *
51 *----------------------------------------------------------------------
52 */
53
54 int
55 Tcl_RecordAndEval(
56 Tcl_Interp *interp, /* Token for interpreter in which command will
57 * be executed. */
58 const char *cmd, /* Command to record. */
59 int flags) /* Additional flags. TCL_NO_EVAL means only
60 * record: don't execute command.
61 * TCL_EVAL_GLOBAL means evaluate the script
62 * in global variable context instead of the
63 * current procedure. */
64 {
65 Tcl_Obj *cmdPtr;
66 int length = strlen(cmd);
67 int result;
68
69 if (length > 0) {
70 /*
71 * Call Tcl_RecordAndEvalObj to do the actual work.
72 */
73
74 cmdPtr = Tcl_NewStringObj(cmd, length);
75 Tcl_IncrRefCount(cmdPtr);
76 result = Tcl_RecordAndEvalObj(interp, cmdPtr, flags);
77
78 /*
79 * Move the interpreter's object result to the string result, then
80 * reset the object result.
81 */
82
83 (void) Tcl_GetStringResult(interp);
84
85 /*
86 * Discard the Tcl object created to hold the command.
87 */
88
89 Tcl_DecrRefCount(cmdPtr);
90 } else {
91 /*
92 * An empty string. Just reset the interpreter's result.
93 */
94
95 Tcl_ResetResult(interp);
96 result = TCL_OK;
97 }
98 return result;
99 }
100
101 /*
102 *----------------------------------------------------------------------
103 *
104 * Tcl_RecordAndEvalObj --
105 *
106 * This procedure adds the command held in its argument object to the
107 * current list of recorded events and then executes the command by
108 * calling Tcl_EvalObj.
109 *
110 * Results:
111 * The return value is a standard Tcl return value, the result of
112 * executing the command.
113 *
114 * Side effects:
115 * The command is recorded and executed.
116 *
117 *----------------------------------------------------------------------
118 */
119
120 int
121 Tcl_RecordAndEvalObj(
122 Tcl_Interp *interp, /* Token for interpreter in which command will
123 * be executed. */
124 Tcl_Obj *cmdPtr, /* Points to object holding the command to
125 * record and execute. */
126 int flags) /* Additional flags. TCL_NO_EVAL means record
127 * only: don't execute the command.
128 * TCL_EVAL_GLOBAL means evaluate the script
129 * in global variable context instead of the
130 * current procedure. */
131 {
132 int result, call = 1;
133 Tcl_CmdInfo info;
134 HistoryObjs *histObjsPtr = (HistoryObjs *)
135 Tcl_GetAssocData(interp, HISTORY_OBJS_KEY, NULL);
136
137 /*
138 * Create the references to the [::history add] command if necessary.
139 */
140
141 if (histObjsPtr == NULL) {
142 histObjsPtr = (HistoryObjs *)ckalloc(sizeof(HistoryObjs));
143 TclNewLiteralStringObj(histObjsPtr->historyObj, "::history");
144 TclNewLiteralStringObj(histObjsPtr->addObj, "add");
145 Tcl_IncrRefCount(histObjsPtr->historyObj);
146 Tcl_IncrRefCount(histObjsPtr->addObj);
147 Tcl_SetAssocData(interp, HISTORY_OBJS_KEY, DeleteHistoryObjs,
148 histObjsPtr);
149 }
150
151 /*
152 * Do not call [history] if it has been replaced by an empty proc
153 */
154
155 result = Tcl_GetCommandInfo(interp, "::history", &info);
156 if (result && (info.deleteProc == TclProcDeleteProc)) {
157 Proc *procPtr = (Proc *) info.objClientData;
158 call = (procPtr->cmdPtr->compileProc != TclCompileNoOp);
159 }
160
161 if (call) {
162 Tcl_Obj *list[3];
163
164 /*
165 * Do recording by eval'ing a tcl history command: history add $cmd.
166 */
167
168 list[0] = histObjsPtr->historyObj;
169 list[1] = histObjsPtr->addObj;
170 list[2] = cmdPtr;
171
172 Tcl_IncrRefCount(cmdPtr);
173 (void) Tcl_EvalObjv(interp, 3, list, TCL_EVAL_GLOBAL);
174 Tcl_DecrRefCount(cmdPtr);
175
176 /*
177 * One possible failure mode above: exceeding a resource limit.
178 */
179
180 if (Tcl_LimitExceeded(interp)) {
181 return TCL_ERROR;
182 }
183 }
184
185 /*
186 * Execute the command.
187 */
188
189 result = TCL_OK;
190 if (!(flags & TCL_NO_EVAL)) {
191 result = Tcl_EvalObjEx(interp, cmdPtr, flags & TCL_EVAL_GLOBAL);
192 }
193 return result;
194 }
195
196 /*
197 *----------------------------------------------------------------------
198 *
199 * DeleteHistoryObjs --
200 *
201 * Called to delete the references to the constant words used when adding
202 * to the history.
203 *
204 * Results:
205 * None.
206 *
207 * Side effects:
208 * The constant words may be deleted.
209 *
210 *----------------------------------------------------------------------
211 */
212
213 static void
214 DeleteHistoryObjs(
215 ClientData clientData,
216 Tcl_Interp *interp)
217 {
218 HistoryObjs *histObjsPtr = (HistoryObjs *)clientData;
219
220 TclDecrRefCount(histObjsPtr->historyObj);
221 TclDecrRefCount(histObjsPtr->addObj);
222 ckfree(histObjsPtr);
223 }
224
225 /*
226 * Local Variables:
227 * mode: c
228 * c-basic-offset: 4
229 * fill-column: 78
230 * End:
231 */