1 | /*
|
---|
2 | * tclVar.c --
|
---|
3 | *
|
---|
4 | * This file contains routines that implement Tcl variables
|
---|
5 | * (both scalars and arrays).
|
---|
6 | *
|
---|
7 | * The implementation of arrays is modelled after an initial
|
---|
8 | * implementation by Mark Diekhans and Karl Lehenbauer.
|
---|
9 | *
|
---|
10 | * Copyright (c) 1987-1994 The Regents of the University of California.
|
---|
11 | * Copyright (c) 1994-1997 Sun Microsystems, Inc.
|
---|
12 | * Copyright (c) 1998-1999 by Scriptics Corporation.
|
---|
13 | *
|
---|
14 | * See the file "license.terms" for information on usage and redistribution
|
---|
15 | * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
---|
16 | *
|
---|
17 | * RCS: @(#) $Id: tclVar.c,v 1.1 2008-06-04 13:58:11 demin Exp $
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "tclInt.h"
|
---|
21 | #include "tclPort.h"
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * The strings below are used to indicate what went wrong when a
|
---|
25 | * variable access is denied.
|
---|
26 | */
|
---|
27 |
|
---|
28 | static char *noSuchVar = "no such variable";
|
---|
29 | static char *isArray = "variable is array";
|
---|
30 | static char *needArray = "variable isn't array";
|
---|
31 | static char *noSuchElement = "no such element in array";
|
---|
32 | static char *danglingElement = "upvar refers to element in deleted array";
|
---|
33 | static char *danglingVar = "upvar refers to variable in deleted namespace";
|
---|
34 | static char *badNamespace = "parent namespace doesn't exist";
|
---|
35 | static char *missingName = "missing variable name";
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * Forward references to procedures defined later in this file:
|
---|
39 | */
|
---|
40 |
|
---|
41 | static char * CallTraces _ANSI_ARGS_((Interp *iPtr, Var *arrayPtr,
|
---|
42 | Var *varPtr, char *part1, char *part2,
|
---|
43 | int flags));
|
---|
44 | static void CleanupVar _ANSI_ARGS_((Var *varPtr,
|
---|
45 | Var *arrayPtr));
|
---|
46 | static void DeleteSearches _ANSI_ARGS_((Var *arrayVarPtr));
|
---|
47 | static void DeleteArray _ANSI_ARGS_((Interp *iPtr,
|
---|
48 | char *arrayName, Var *varPtr, int flags));
|
---|
49 | static int MakeUpvar _ANSI_ARGS_((
|
---|
50 | Interp *iPtr, CallFrame *framePtr,
|
---|
51 | char *otherP1, char *otherP2, int otherFlags,
|
---|
52 | char *myName, int myFlags));
|
---|
53 | static Var * NewVar _ANSI_ARGS_((void));
|
---|
54 | static ArraySearch * ParseSearchId _ANSI_ARGS_((Tcl_Interp *interp,
|
---|
55 | Var *varPtr, char *varName, char *string));
|
---|
56 | static void VarErrMsg _ANSI_ARGS_((Tcl_Interp *interp,
|
---|
57 | char *part1, char *part2, char *operation,
|
---|
58 | char *reason));
|
---|
59 | |
---|
60 |
|
---|
61 | /*
|
---|
62 | *----------------------------------------------------------------------
|
---|
63 | *
|
---|
64 | * TclLookupVar --
|
---|
65 | *
|
---|
66 | * This procedure is used by virtually all of the variable code to
|
---|
67 | * locate a variable given its name(s).
|
---|
68 | *
|
---|
69 | * Results:
|
---|
70 | * The return value is a pointer to the variable structure indicated by
|
---|
71 | * part1 and part2, or NULL if the variable couldn't be found. If the
|
---|
72 | * variable is found, *arrayPtrPtr is filled in with the address of the
|
---|
73 | * variable structure for the array that contains the variable (or NULL
|
---|
74 | * if the variable is a scalar). If the variable can't be found and
|
---|
75 | * either createPart1 or createPart2 are 1, a new as-yet-undefined
|
---|
76 | * (VAR_UNDEFINED) variable structure is created, entered into a hash
|
---|
77 | * table, and returned.
|
---|
78 | *
|
---|
79 | * If the variable isn't found and creation wasn't specified, or some
|
---|
80 | * other error occurs, NULL is returned and an error message is left in
|
---|
81 | * interp->result if TCL_LEAVE_ERR_MSG is set in flags. (The result
|
---|
82 | * isn't put in interp->objResultPtr because this procedure is used
|
---|
83 | * by so many string-based routines.)
|
---|
84 | *
|
---|
85 | * Note: it's possible for the variable returned to be VAR_UNDEFINED
|
---|
86 | * even if createPart1 or createPart2 are 1 (these only cause the hash
|
---|
87 | * table entry or array to be created). For example, the variable might
|
---|
88 | * be a global that has been unset but is still referenced by a
|
---|
89 | * procedure, or a variable that has been unset but it only being kept
|
---|
90 | * in existence (if VAR_UNDEFINED) by a trace.
|
---|
91 | *
|
---|
92 | * Side effects:
|
---|
93 | * New hashtable entries may be created if createPart1 or createPart2
|
---|
94 | * are 1.
|
---|
95 | *
|
---|
96 | *----------------------------------------------------------------------
|
---|
97 | */
|
---|
98 |
|
---|
99 | Var *
|
---|
100 | TclLookupVar(interp, part1, part2, flags, msg, createPart1, createPart2,
|
---|
101 | arrayPtrPtr)
|
---|
102 | Tcl_Interp *interp; /* Interpreter to use for lookup. */
|
---|
103 | char *part1; /* If part2 isn't NULL, this is the name of
|
---|
104 | * an array. Otherwise, if the
|
---|
105 | * TCL_PARSE_PART1 flag bit is set this
|
---|
106 | * is a full variable name that could
|
---|
107 | * include a parenthesized array elemnt. If
|
---|
108 | * TCL_PARSE_PART1 isn't present, then
|
---|
109 | * this is the name of a scalar variable. */
|
---|
110 | char *part2; /* Name of element within array, or NULL. */
|
---|
111 | int flags; /* Only TCL_GLOBAL_ONLY, TCL_NAMESPACE_ONLY,
|
---|
112 | * TCL_LEAVE_ERR_MSG, and
|
---|
113 | * TCL_PARSE_PART1 bits matter. */
|
---|
114 | char *msg; /* Verb to use in error messages, e.g.
|
---|
115 | * "read" or "set". Only needed if
|
---|
116 | * TCL_LEAVE_ERR_MSG is set in flags. */
|
---|
117 | int createPart1; /* If 1, create hash table entry for part 1
|
---|
118 | * of name, if it doesn't already exist. If
|
---|
119 | * 0, return error if it doesn't exist. */
|
---|
120 | int createPart2; /* If 1, create hash table entry for part 2
|
---|
121 | * of name, if it doesn't already exist. If
|
---|
122 | * 0, return error if it doesn't exist. */
|
---|
123 | Var **arrayPtrPtr; /* If the name refers to an element of an
|
---|
124 | * array, *arrayPtrPtr gets filled in with
|
---|
125 | * address of array variable. Otherwise
|
---|
126 | * this is set to NULL. */
|
---|
127 | {
|
---|
128 | Interp *iPtr = (Interp *) interp;
|
---|
129 | CallFrame *varFramePtr = iPtr->varFramePtr;
|
---|
130 | /* Points to the procedure call frame whose
|
---|
131 | * variables are currently in use. Same as
|
---|
132 | * the current procedure's frame, if any,
|
---|
133 | * unless an "uplevel" is executing. */
|
---|
134 | Tcl_HashTable *tablePtr; /* Points to the hashtable, if any, in which
|
---|
135 | * to look up the variable. */
|
---|
136 | Tcl_Var var; /* Used to search for global names. */
|
---|
137 | Var *varPtr; /* Points to the Var structure returned for
|
---|
138 | * the variable. */
|
---|
139 | char *elName; /* Name of array element or NULL; may be
|
---|
140 | * same as part2, or may be openParen+1. */
|
---|
141 | char *openParen, *closeParen;
|
---|
142 | /* If this procedure parses a name into
|
---|
143 | * array and index, these point to the
|
---|
144 | * parens around the index. Otherwise they
|
---|
145 | * are NULL. These are needed to restore
|
---|
146 | * the parens after parsing the name. */
|
---|
147 | Namespace *varNsPtr, *cxtNsPtr, *dummy1Ptr, *dummy2Ptr;
|
---|
148 | ResolverScheme *resPtr;
|
---|
149 | Tcl_HashEntry *hPtr;
|
---|
150 | register char *p;
|
---|
151 | int new, i, result;
|
---|
152 |
|
---|
153 | varPtr = NULL;
|
---|
154 | *arrayPtrPtr = NULL;
|
---|
155 | openParen = closeParen = NULL;
|
---|
156 | varNsPtr = NULL; /* set non-NULL if a nonlocal variable */
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * If the name hasn't been parsed into array name and index yet,
|
---|
160 | * do it now.
|
---|
161 | */
|
---|
162 |
|
---|
163 | elName = part2;
|
---|
164 | if (flags & TCL_PARSE_PART1) {
|
---|
165 | for (p = part1; ; p++) {
|
---|
166 | if (*p == 0) {
|
---|
167 | elName = NULL;
|
---|
168 | break;
|
---|
169 | }
|
---|
170 | if (*p == '(') {
|
---|
171 | openParen = p;
|
---|
172 | do {
|
---|
173 | p++;
|
---|
174 | } while (*p != '\0');
|
---|
175 | p--;
|
---|
176 | if (*p == ')') {
|
---|
177 | closeParen = p;
|
---|
178 | *openParen = 0;
|
---|
179 | elName = openParen+1;
|
---|
180 | } else {
|
---|
181 | openParen = NULL;
|
---|
182 | elName = NULL;
|
---|
183 | }
|
---|
184 | break;
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | /*
|
---|
190 | * If this namespace has a variable resolver, then give it first
|
---|
191 | * crack at the variable resolution. It may return a Tcl_Var
|
---|
192 | * value, it may signal to continue onward, or it may signal
|
---|
193 | * an error.
|
---|
194 | */
|
---|
195 | if ((flags & TCL_GLOBAL_ONLY) != 0 || iPtr->varFramePtr == NULL) {
|
---|
196 | cxtNsPtr = iPtr->globalNsPtr;
|
---|
197 | } else {
|
---|
198 | cxtNsPtr = iPtr->varFramePtr->nsPtr;
|
---|
199 | }
|
---|
200 |
|
---|
201 | if (cxtNsPtr->varResProc != NULL || iPtr->resolverPtr != NULL) {
|
---|
202 | resPtr = iPtr->resolverPtr;
|
---|
203 |
|
---|
204 | if (cxtNsPtr->varResProc) {
|
---|
205 | result = (*cxtNsPtr->varResProc)(interp, part1,
|
---|
206 | (Tcl_Namespace *) cxtNsPtr, flags, &var);
|
---|
207 | } else {
|
---|
208 | result = TCL_CONTINUE;
|
---|
209 | }
|
---|
210 |
|
---|
211 | while (result == TCL_CONTINUE && resPtr) {
|
---|
212 | if (resPtr->varResProc) {
|
---|
213 | result = (*resPtr->varResProc)(interp, part1,
|
---|
214 | (Tcl_Namespace *) cxtNsPtr, flags, &var);
|
---|
215 | }
|
---|
216 | resPtr = resPtr->nextPtr;
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (result == TCL_OK) {
|
---|
220 | varPtr = (Var *) var;
|
---|
221 | goto lookupVarPart2;
|
---|
222 | } else if (result != TCL_CONTINUE) {
|
---|
223 | return (Var *) NULL;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * Look up part1. Look it up as either a namespace variable or as a
|
---|
229 | * local variable in a procedure call frame (varFramePtr).
|
---|
230 | * Interpret part1 as a namespace variable if:
|
---|
231 | * 1) so requested by a TCL_GLOBAL_ONLY or TCL_NAMESPACE_ONLY flag,
|
---|
232 | * 2) there is no active frame (we're at the global :: scope),
|
---|
233 | * 3) the active frame was pushed to define the namespace context
|
---|
234 | * for a "namespace eval" or "namespace inscope" command,
|
---|
235 | * 4) the name has namespace qualifiers ("::"s).
|
---|
236 | * Otherwise, if part1 is a local variable, search first in the
|
---|
237 | * frame's array of compiler-allocated local variables, then in its
|
---|
238 | * hashtable for runtime-created local variables.
|
---|
239 | *
|
---|
240 | * If createPart1 and the variable isn't found, create the variable and,
|
---|
241 | * if necessary, create varFramePtr's local var hashtable.
|
---|
242 | */
|
---|
243 |
|
---|
244 | if (((flags & (TCL_GLOBAL_ONLY | TCL_NAMESPACE_ONLY)) != 0)
|
---|
245 | || (varFramePtr == NULL)
|
---|
246 | || !varFramePtr->isProcCallFrame
|
---|
247 | || (strstr(part1, "::") != NULL)) {
|
---|
248 | char *tail;
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * Don't pass TCL_LEAVE_ERR_MSG, we may yet create the variable,
|
---|
252 | * or otherwise generate our own error!
|
---|
253 | */
|
---|
254 | var = Tcl_FindNamespaceVar(interp, part1, (Tcl_Namespace *) NULL,
|
---|
255 | flags & ~TCL_LEAVE_ERR_MSG);
|
---|
256 | if (var != (Tcl_Var) NULL) {
|
---|
257 | varPtr = (Var *) var;
|
---|
258 | }
|
---|
259 | if (varPtr == NULL) {
|
---|
260 | if (createPart1) { /* var wasn't found so create it */
|
---|
261 | TclGetNamespaceForQualName(interp, part1, (Namespace *) NULL,
|
---|
262 | flags, &varNsPtr, &dummy1Ptr, &dummy2Ptr, &tail);
|
---|
263 | if (varNsPtr == NULL) {
|
---|
264 | if (flags & TCL_LEAVE_ERR_MSG) {
|
---|
265 | VarErrMsg(interp, part1, part2, msg, badNamespace);
|
---|
266 | }
|
---|
267 | goto done;
|
---|
268 | }
|
---|
269 | if (tail == NULL) {
|
---|
270 | if (flags & TCL_LEAVE_ERR_MSG) {
|
---|
271 | VarErrMsg(interp, part1, part2, msg, missingName);
|
---|
272 | }
|
---|
273 | goto done;
|
---|
274 | }
|
---|
275 | hPtr = Tcl_CreateHashEntry(&varNsPtr->varTable, tail, &new);
|
---|
276 | varPtr = NewVar();
|
---|
277 | Tcl_SetHashValue(hPtr, varPtr);
|
---|
278 | varPtr->hPtr = hPtr;
|
---|
279 | varPtr->nsPtr = varNsPtr;
|
---|
280 | } else { /* var wasn't found and not to create it */
|
---|
281 | if (flags & TCL_LEAVE_ERR_MSG) {
|
---|
282 | VarErrMsg(interp, part1, part2, msg, noSuchVar);
|
---|
283 | }
|
---|
284 | goto done;
|
---|
285 | }
|
---|
286 | }
|
---|
287 | } else { /* local var: look in frame varFramePtr */
|
---|
288 | Proc *procPtr = varFramePtr->procPtr;
|
---|
289 | int localCt = procPtr->numCompiledLocals;
|
---|
290 | CompiledLocal *localPtr = procPtr->firstLocalPtr;
|
---|
291 | Var *localVarPtr = varFramePtr->compiledLocals;
|
---|
292 | int part1Len = strlen(part1);
|
---|
293 |
|
---|
294 | for (i = 0; i < localCt; i++) {
|
---|
295 | if (!TclIsVarTemporary(localPtr)) {
|
---|
296 | char *localName = localVarPtr->name;
|
---|
297 | if ((part1[0] == localName[0])
|
---|
298 | && (part1Len == localPtr->nameLength)
|
---|
299 | && (strcmp(part1, localName) == 0)) {
|
---|
300 | varPtr = localVarPtr;
|
---|
301 | break;
|
---|
302 | }
|
---|
303 | }
|
---|
304 | localVarPtr++;
|
---|
305 | localPtr = localPtr->nextPtr;
|
---|
306 | }
|
---|
307 | if (varPtr == NULL) { /* look in the frame's var hash table */
|
---|
308 | tablePtr = varFramePtr->varTablePtr;
|
---|
309 | if (createPart1) {
|
---|
310 | if (tablePtr == NULL) {
|
---|
311 | tablePtr = (Tcl_HashTable *)
|
---|
312 | ckalloc(sizeof(Tcl_HashTable));
|
---|
313 | Tcl_InitHashTable(tablePtr, TCL_STRING_KEYS);
|
---|
314 | varFramePtr->varTablePtr = tablePtr;
|
---|
315 | }
|
---|
316 | hPtr = Tcl_CreateHashEntry(tablePtr, part1, &new);
|
---|
317 | if (new) {
|
---|
318 | varPtr = NewVar();
|
---|
319 | Tcl_SetHashValue(hPtr, varPtr);
|
---|
320 | varPtr->hPtr = hPtr;
|
---|
321 | varPtr->nsPtr = NULL; /* a local variable */
|
---|
322 | } else {
|
---|
323 | varPtr = (Var *) Tcl_GetHashValue(hPtr);
|
---|
324 | }
|
---|
325 | } else {
|
---|
326 | hPtr = NULL;
|
---|
327 | if (tablePtr != NULL) {
|
---|
328 | hPtr = Tcl_FindHashEntry(tablePtr, part1);
|
---|
329 | }
|
---|
330 | if (hPtr == NULL) {
|
---|
331 | if (flags & TCL_LEAVE_ERR_MSG) {
|
---|
332 | VarErrMsg(interp, part1, part2, msg, noSuchVar);
|
---|
333 | }
|
---|
334 | goto done;
|
---|
335 | }
|
---|
336 | varPtr = (Var *) Tcl_GetHashValue(hPtr);
|
---|
337 | }
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 | lookupVarPart2:
|
---|
342 | if (openParen != NULL) {
|
---|
343 | *openParen = '(';
|
---|
344 | openParen = NULL;
|
---|
345 | }
|
---|
346 |
|
---|
347 | /*
|
---|
348 | * If varPtr is a link variable, we have a reference to some variable
|
---|
349 | * that was created through an "upvar" or "global" command. Traverse
|
---|
350 | * through any links until we find the referenced variable.
|
---|
351 | */
|
---|
352 |
|
---|
353 | while (TclIsVarLink(varPtr)) {
|
---|
354 | varPtr = varPtr->value.linkPtr;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /*
|
---|
358 | * If we're not dealing with an array element, return varPtr.
|
---|
359 | */
|
---|
360 |
|
---|
361 | if (elName == NULL) {
|
---|
362 | goto done;
|
---|
363 | }
|
---|
364 |
|
---|
365 | /*
|
---|
366 | * We're dealing with an array element. Make sure the variable is an
|
---|
367 | * array and look up the element (create the element if desired).
|
---|
368 | */
|
---|
369 |
|
---|
370 | if (TclIsVarUndefined(varPtr) && !TclIsVarArrayElement(varPtr)) {
|
---|
371 | if (!createPart1) {
|
---|
372 | if (flags & TCL_LEAVE_ERR_MSG) {
|
---|
373 | VarErrMsg(interp, part1, part2, msg, noSuchVar);
|
---|
374 | }
|
---|
375 | varPtr = NULL;
|
---|
376 | goto done;
|
---|
377 | }
|
---|
378 |
|
---|
379 | /*
|
---|
380 | * Make sure we are not resurrecting a namespace variable from a
|
---|
381 | * deleted namespace!
|
---|
382 | */
|
---|
383 | if ((varPtr->flags & VAR_IN_HASHTABLE) && (varPtr->hPtr == NULL)) {
|
---|
384 | if (flags & TCL_LEAVE_ERR_MSG) {
|
---|
385 | VarErrMsg(interp, part1, part2, msg, danglingVar);
|
---|
386 | }
|
---|
387 | varPtr = NULL;
|
---|
388 | goto done;
|
---|
389 | }
|
---|
390 |
|
---|
391 | TclSetVarArray(varPtr);
|
---|
392 | TclClearVarUndefined(varPtr);
|
---|
393 | varPtr->value.tablePtr =
|
---|
394 | (Tcl_HashTable *) ckalloc(sizeof(Tcl_HashTable));
|
---|
395 | Tcl_InitHashTable(varPtr->value.tablePtr, TCL_STRING_KEYS);
|
---|
396 | } else if (!TclIsVarArray(varPtr)) {
|
---|
397 | if (flags & TCL_LEAVE_ERR_MSG) {
|
---|
398 | VarErrMsg(interp, part1, part2, msg, needArray);
|
---|
399 | }
|
---|
400 | varPtr = NULL;
|
---|
401 | goto done;
|
---|
402 | }
|
---|
403 | *arrayPtrPtr = varPtr;
|
---|
404 | if (closeParen != NULL) {
|
---|
405 | *closeParen = 0;
|
---|
406 | }
|
---|
407 | if (createPart2) {
|
---|
408 | hPtr = Tcl_CreateHashEntry(varPtr->value.tablePtr, elName, &new);
|
---|
409 | if (closeParen != NULL) {
|
---|
410 | *closeParen = ')';
|
---|
411 | }
|
---|
412 | if (new) {
|
---|
413 | if (varPtr->searchPtr != NULL) {
|
---|
414 | DeleteSearches(varPtr);
|
---|
415 | }
|
---|
416 | varPtr = NewVar();
|
---|
417 | Tcl_SetHashValue(hPtr, varPtr);
|
---|
418 | varPtr->hPtr = hPtr;
|
---|
419 | varPtr->nsPtr = varNsPtr;
|
---|
420 | TclSetVarArrayElement(varPtr);
|
---|
421 | }
|
---|
422 | } else {
|
---|
423 | hPtr = Tcl_FindHashEntry(varPtr->value.tablePtr, elName);
|
---|
424 | if (closeParen != NULL) {
|
---|
425 | *closeParen = ')';
|
---|
426 | }
|
---|
427 | if (hPtr == NULL) {
|
---|
428 | if (flags & TCL_LEAVE_ERR_MSG) {
|
---|
429 | VarErrMsg(interp, part1, part2, msg, noSuchElement);
|
---|
430 | }
|
---|
431 | varPtr = NULL;
|
---|
432 | goto done;
|
---|
433 | }
|
---|
434 | }
|
---|
435 | varPtr = (Var *) Tcl_GetHashValue(hPtr);
|
---|
436 |
|
---|
437 | done:
|
---|
438 | if (openParen != NULL) {
|
---|
439 | *openParen = '(';
|
---|
440 | }
|
---|
441 | return varPtr;
|
---|
442 | }
|
---|
443 | |
---|
444 |
|
---|
445 | /*
|
---|
446 | *----------------------------------------------------------------------
|
---|
447 | *
|
---|
448 | * Tcl_GetVar --
|
---|
449 | *
|
---|
450 | * Return the value of a Tcl variable as a string.
|
---|
451 | *
|
---|
452 | * Results:
|
---|
453 | * The return value points to the current value of varName as a string.
|
---|
454 | * If the variable is not defined or can't be read because of a clash
|
---|
455 | * in array usage then a NULL pointer is returned and an error message
|
---|
456 | * is left in interp->result if the TCL_LEAVE_ERR_MSG flag is set.
|
---|
457 | * Note: the return value is only valid up until the next change to the
|
---|
458 | * variable; if you depend on the value lasting longer than that, then
|
---|
459 | * make yourself a private copy.
|
---|
460 | *
|
---|
461 | * Side effects:
|
---|
462 | * None.
|
---|
463 | *
|
---|
464 | *----------------------------------------------------------------------
|
---|
465 | */
|
---|
466 |
|
---|
467 | char *
|
---|
468 | Tcl_GetVar(interp, varName, flags)
|
---|
469 | Tcl_Interp *interp; /* Command interpreter in which varName is
|
---|
470 | * to be looked up. */
|
---|
471 | char *varName; /* Name of a variable in interp. */
|
---|
472 | int flags; /* OR-ed combination of TCL_GLOBAL_ONLY,
|
---|
473 | * TCL_NAMESPACE_ONLY or TCL_LEAVE_ERR_MSG
|
---|
474 | * bits. */
|
---|
475 | {
|
---|
476 | return Tcl_GetVar2(interp, varName, (char *) NULL,
|
---|
477 | (flags | TCL_PARSE_PART1));
|
---|
478 | }
|
---|
479 | |
---|
480 |
|
---|
481 | /*
|
---|
482 | *----------------------------------------------------------------------
|
---|
483 | *
|
---|
484 | * Tcl_GetVar2 --
|
---|
485 | *
|
---|
486 | * Return the value of a Tcl variable as a string, given a two-part
|
---|
487 | * name consisting of array name and element within array.
|
---|
488 | *
|
---|
489 | * Results:
|
---|
490 | * The return value points to the current value of the variable given
|
---|
491 | * by part1 and part2 as a string. If the specified variable doesn't
|
---|
492 | * exist, or if there is a clash in array usage, then NULL is returned
|
---|
493 | * and a message will be left in interp->result if the
|
---|
494 | * TCL_LEAVE_ERR_MSG flag is set. Note: the return value is only valid
|
---|
495 | * up until the next change to the variable; if you depend on the value
|
---|
496 | * lasting longer than that, then make yourself a private copy.
|
---|
497 | *
|
---|
498 | * Side effects:
|
---|
499 | * None.
|
---|
500 | *
|
---|
501 | *----------------------------------------------------------------------
|
---|
502 | */
|
---|
503 |
|
---|
504 | char *
|
---|
505 | Tcl_GetVar2(interp, part1, part2, flags)
|
---|
506 | Tcl_Interp *interp; /* Command interpreter in which variable is
|
---|
507 | * to be looked up. */
|
---|
508 | char *part1; /* Name of an array (if part2 is non-NULL)
|
---|
509 | * or the name of a variable. */
|
---|
510 | char *part2; /* If non-NULL, gives the name of an element
|
---|
511 | * in the array part1. */
|
---|
512 | int flags; /* OR-ed combination of TCL_GLOBAL_ONLY,
|
---|
513 | * TCL_NAMESPACE_ONLY, TCL_LEAVE_ERR_MSG,
|
---|
514 | * and TCL_PARSE_PART1 bits. */
|
---|
515 | {
|
---|
516 | register Tcl_Obj *part1Ptr;
|
---|
517 | register Tcl_Obj *part2Ptr = NULL;
|
---|
518 | Tcl_Obj *objPtr;
|
---|
519 | int length;
|
---|
520 |
|
---|
521 | length = strlen(part1);
|
---|
522 | TclNewObj(part1Ptr);
|
---|
523 | TclInitStringRep(part1Ptr, part1, length);
|
---|
524 | Tcl_IncrRefCount(part1Ptr);
|
---|
525 |
|
---|
526 | if (part2 != NULL) {
|
---|
527 | length = strlen(part2);
|
---|
528 | TclNewObj(part2Ptr);
|
---|
529 | TclInitStringRep(part2Ptr, part2, length);
|
---|
530 | Tcl_IncrRefCount(part2Ptr);
|
---|
531 | }
|
---|
532 |
|
---|
533 | objPtr = Tcl_ObjGetVar2(interp, part1Ptr, part2Ptr, flags);
|
---|
534 |
|
---|
535 | TclDecrRefCount(part1Ptr); /* done with the part1 name object */
|
---|
536 | if (part2Ptr != NULL) {
|
---|
537 | TclDecrRefCount(part2Ptr); /* and the part2 name object */
|
---|
538 | }
|
---|
539 |
|
---|
540 | if (objPtr == NULL) {
|
---|
541 | /*
|
---|
542 | * Move the interpreter's object result to the string result,
|
---|
543 | * then reset the object result.
|
---|
544 | * FAILS IF OBJECT RESULT'S STRING REPRESENTATION CONTAINS NULLS.
|
---|
545 | */
|
---|
546 |
|
---|
547 | Tcl_SetResult(interp,
|
---|
548 | TclGetStringFromObj(Tcl_GetObjResult(interp), (int *) NULL),
|
---|
549 | TCL_VOLATILE);
|
---|
550 | return NULL;
|
---|
551 | }
|
---|
552 |
|
---|
553 | /*
|
---|
554 | * THIS FAILS IF Tcl_ObjGetVar2's RESULT'S STRING REP HAS A NULL BYTE.
|
---|
555 | */
|
---|
556 |
|
---|
557 | return TclGetStringFromObj(objPtr, (int *) NULL);
|
---|
558 | }
|
---|
559 | |
---|
560 |
|
---|
561 | /*
|
---|
562 | *----------------------------------------------------------------------
|
---|
563 | *
|
---|
564 | * Tcl_ObjGetVar2 --
|
---|
565 | *
|
---|
566 | * Return the value of a Tcl variable as a Tcl object, given a
|
---|
567 | * two-part name consisting of array name and element within array.
|
---|
568 | *
|
---|
569 | * Results:
|
---|
570 | * The return value points to the current object value of the variable
|
---|
571 | * given by part1Ptr and part2Ptr. If the specified variable doesn't
|
---|
572 | * exist, or if there is a clash in array usage, then NULL is returned
|
---|
573 | * and a message will be left in the interpreter's result if the
|
---|
574 | * TCL_LEAVE_ERR_MSG flag is set.
|
---|
575 | *
|
---|
576 | * Side effects:
|
---|
577 | * The ref count for the returned object is _not_ incremented to
|
---|
578 | * reflect the returned reference; if you want to keep a reference to
|
---|
579 | * the object you must increment its ref count yourself.
|
---|
580 | *
|
---|
581 | *----------------------------------------------------------------------
|
---|
582 | */
|
---|
583 |
|
---|
584 | Tcl_Obj *
|
---|
585 | Tcl_ObjGetVar2(interp, part1Ptr, part2Ptr, flags)
|
---|
586 | Tcl_Interp *interp; /* Command interpreter in which variable is
|
---|
587 | * to be looked up. */
|
---|
588 | register Tcl_Obj *part1Ptr; /* Points to an object holding the name of
|
---|
589 | * an array (if part2 is non-NULL) or the
|
---|
590 | * name of a variable. */
|
---|
591 | register Tcl_Obj *part2Ptr; /* If non-null, points to an object holding
|
---|
592 | * the name of an element in the array
|
---|
593 | * part1Ptr. */
|
---|
594 | int flags; /* OR-ed combination of TCL_GLOBAL_ONLY,
|
---|
595 | * TCL_LEAVE_ERR_MSG, and
|
---|
596 | * TCL_PARSE_PART1 bits. */
|
---|
597 | {
|
---|
598 | Interp *iPtr = (Interp *) interp;
|
---|
599 | register Var *varPtr;
|
---|
600 | Var *arrayPtr;
|
---|
601 | char *part1, *msg;
|
---|
602 | char *part2 = NULL;
|
---|
603 |
|
---|
604 | /*
|
---|
605 | * THIS FAILS IF A NAME OBJECT'S STRING REP HAS A NULL BYTE.
|
---|
606 | */
|
---|
607 |
|
---|
608 | part1 = TclGetStringFromObj(part1Ptr, (int *) NULL);
|
---|
609 | if (part2Ptr != NULL) {
|
---|
610 | part2 = TclGetStringFromObj(part2Ptr, (int *) NULL);
|
---|
611 | }
|
---|
612 | varPtr = TclLookupVar(interp, part1, part2, flags, "read",
|
---|
613 | /*createPart1*/ 0, /*createPart2*/ 1, &arrayPtr);
|
---|
614 | if (varPtr == NULL) {
|
---|
615 | return NULL;
|
---|
616 | }
|
---|
617 |
|
---|
618 | /*
|
---|
619 | * Invoke any traces that have been set for the variable.
|
---|
620 | */
|
---|
621 |
|
---|
622 | if ((varPtr->tracePtr != NULL)
|
---|
623 | || ((arrayPtr != NULL) && (arrayPtr->tracePtr != NULL))) {
|
---|
624 | msg = CallTraces(iPtr, arrayPtr, varPtr, part1, part2,
|
---|
625 | (flags & (TCL_NAMESPACE_ONLY|TCL_GLOBAL_ONLY|TCL_PARSE_PART1)) | TCL_TRACE_READS);
|
---|
626 | if (msg != NULL) {
|
---|
627 | if (flags & TCL_LEAVE_ERR_MSG) {
|
---|
628 | VarErrMsg(interp, part1, part2, "read", msg);
|
---|
629 | }
|
---|
630 | goto errorReturn;
|
---|
631 | }
|
---|
632 | }
|
---|
633 |
|
---|
634 | /*
|
---|
635 | * Return the element if it's an existing scalar variable.
|
---|
636 | */
|
---|
637 |
|
---|
638 | if (TclIsVarScalar(varPtr) && !TclIsVarUndefined(varPtr)) {
|
---|
639 | return varPtr->value.objPtr;
|
---|
640 | }
|
---|
641 |
|
---|
642 | if (flags & TCL_LEAVE_ERR_MSG) {
|
---|
643 | if (TclIsVarUndefined(varPtr) && (arrayPtr != NULL)
|
---|
644 | && !TclIsVarUndefined(arrayPtr)) {
|
---|
645 | msg = noSuchElement;
|
---|
646 | } else if (TclIsVarArray(varPtr)) {
|
---|
647 | msg = isArray;
|
---|
648 | } else {
|
---|
649 | msg = noSuchVar;
|
---|
650 | }
|
---|
651 | VarErrMsg(interp, part1, part2, "read", msg);
|
---|
652 | }
|
---|
653 |
|
---|
654 | /*
|
---|
655 | * An error. If the variable doesn't exist anymore and no-one's using
|
---|
656 | * it, then free up the relevant structures and hash table entries.
|
---|
657 | */
|
---|
658 |
|
---|
659 | errorReturn:
|
---|
660 | if (TclIsVarUndefined(varPtr)) {
|
---|
661 | CleanupVar(varPtr, arrayPtr);
|
---|
662 | }
|
---|
663 | return NULL;
|
---|
664 | }
|
---|
665 | |
---|
666 |
|
---|
667 | /*
|
---|
668 | *----------------------------------------------------------------------
|
---|
669 | *
|
---|
670 | * TclGetIndexedScalar --
|
---|
671 | *
|
---|
672 | * Return the Tcl object value of a local scalar variable in the active
|
---|
673 | * procedure, given its index in the procedure's array of compiler
|
---|
674 | * allocated local variables.
|
---|
675 | *
|
---|
676 | * Results:
|
---|
677 | * The return value points to the current object value of the variable
|
---|
678 | * given by localIndex. If the specified variable doesn't exist, or
|
---|
679 | * there is a clash in array usage, or an error occurs while executing
|
---|
680 | * variable traces, then NULL is returned and a message will be left in
|
---|
681 | * the interpreter's result if leaveErrorMsg is 1.
|
---|
682 | *
|
---|
683 | * Side effects:
|
---|
684 | * The ref count for the returned object is _not_ incremented to
|
---|
685 | * reflect the returned reference; if you want to keep a reference to
|
---|
686 | * the object you must increment its ref count yourself.
|
---|
687 | *
|
---|
688 | *----------------------------------------------------------------------
|
---|
689 | */
|
---|
690 |
|
---|
691 | Tcl_Obj *
|
---|
692 | TclGetIndexedScalar(interp, localIndex, leaveErrorMsg)
|
---|
693 | Tcl_Interp *interp; /* Command interpreter in which variable is
|
---|
694 | * to be looked up. */
|
---|
695 | int localIndex; /* Index of variable in procedure's array
|
---|
696 | * of local variables. */
|
---|
697 | int leaveErrorMsg; /* 1 if to leave an error message in
|
---|
698 | * interpreter's result on an error.
|
---|
699 | * Otherwise no error message is left. */
|
---|
700 | {
|
---|
701 | Interp *iPtr = (Interp *) interp;
|
---|
702 | CallFrame *varFramePtr = iPtr->varFramePtr;
|
---|
703 | /* Points to the procedure call frame whose
|
---|
704 | * variables are currently in use. Same as
|
---|
705 | * the current procedure's frame, if any,
|
---|
706 | * unless an "uplevel" is executing. */
|
---|
707 | Var *compiledLocals = varFramePtr->compiledLocals;
|
---|
708 | Var *varPtr; /* Points to the variable's in-frame Var
|
---|
709 | * structure. */
|
---|
710 | char *varName; /* Name of the local variable. */
|
---|
711 | char *msg;
|
---|
712 |
|
---|
713 | varPtr = &(compiledLocals[localIndex]);
|
---|
714 | varName = varPtr->name;
|
---|
715 |
|
---|
716 | /*
|
---|
717 | * If varPtr is a link variable, we have a reference to some variable
|
---|
718 | * that was created through an "upvar" or "global" command, or we have a
|
---|
719 | * reference to a variable in an enclosing namespace. Traverse through
|
---|
720 | * any links until we find the referenced variable.
|
---|
721 | */
|
---|
722 |
|
---|
723 | while (TclIsVarLink(varPtr)) {
|
---|
724 | varPtr = varPtr->value.linkPtr;
|
---|
725 | }
|
---|
726 |
|
---|
727 | /*
|
---|
728 | * Invoke any traces that have been set for the variable.
|
---|
729 | */
|
---|
730 |
|
---|
731 | if (varPtr->tracePtr != NULL) {
|
---|
732 | msg = CallTraces(iPtr, /*arrayPtr*/ NULL, varPtr, varName, NULL,
|
---|
733 | TCL_TRACE_READS);
|
---|
734 | if (msg != NULL) {
|
---|
735 | if (leaveErrorMsg) {
|
---|
736 | VarErrMsg(interp, varName, NULL, "read", msg);
|
---|
737 | }
|
---|
738 | return NULL;
|
---|
739 | }
|
---|
740 | }
|
---|
741 |
|
---|
742 | /*
|
---|
743 | * Make sure we're dealing with a scalar variable and not an array, and
|
---|
744 | * that the variable exists (isn't undefined).
|
---|
745 | */
|
---|
746 |
|
---|
747 | if (!TclIsVarScalar(varPtr) || TclIsVarUndefined(varPtr)) {
|
---|
748 | if (leaveErrorMsg) {
|
---|
749 | if (TclIsVarArray(varPtr)) {
|
---|
750 | msg = isArray;
|
---|
751 | } else {
|
---|
752 | msg = noSuchVar;
|
---|
753 | }
|
---|
754 | VarErrMsg(interp, varName, NULL, "read", msg);
|
---|
755 | }
|
---|
756 | return NULL;
|
---|
757 | }
|
---|
758 | return varPtr->value.objPtr;
|
---|
759 | }
|
---|
760 | |
---|
761 |
|
---|
762 | /*
|
---|
763 | *----------------------------------------------------------------------
|
---|
764 | *
|
---|
765 | * TclGetElementOfIndexedArray --
|
---|
766 | *
|
---|
767 | * Return the Tcl object value for an element in a local array
|
---|
768 | * variable. The element is named by the object elemPtr while the
|
---|
769 | * array is specified by its index in the active procedure's array
|
---|
770 | * of compiler allocated local variables.
|
---|
771 | *
|
---|
772 | * Results:
|
---|
773 | * The return value points to the current object value of the
|
---|
774 | * element. If the specified array or element doesn't exist, or there
|
---|
775 | * is a clash in array usage, or an error occurs while executing
|
---|
776 | * variable traces, then NULL is returned and a message will be left in
|
---|
777 | * the interpreter's result if leaveErrorMsg is 1.
|
---|
778 | *
|
---|
779 | * Side effects:
|
---|
780 | * The ref count for the returned object is _not_ incremented to
|
---|
781 | * reflect the returned reference; if you want to keep a reference to
|
---|
782 | * the object you must increment its ref count yourself.
|
---|
783 | *
|
---|
784 | *----------------------------------------------------------------------
|
---|
785 | */
|
---|
786 |
|
---|
787 | Tcl_Obj *
|
---|
788 | TclGetElementOfIndexedArray(interp, localIndex, elemPtr, leaveErrorMsg)
|
---|
789 | Tcl_Interp *interp; /* Command interpreter in which variable is
|
---|
790 | * to be looked up. */
|
---|
791 | int localIndex; /* Index of array variable in procedure's
|
---|
792 | * array of local variables. */
|
---|
793 | Tcl_Obj *elemPtr; /* Points to an object holding the name of
|
---|
794 | * an element to get in the array. */
|
---|
795 | int leaveErrorMsg; /* 1 if to leave an error message in
|
---|
796 | * the interpreter's result on an error.
|
---|
797 | * Otherwise no error message is left. */
|
---|
798 | {
|
---|
799 | Interp *iPtr = (Interp *) interp;
|
---|
800 | CallFrame *varFramePtr = iPtr->varFramePtr;
|
---|
801 | /* Points to the procedure call frame whose
|
---|
802 | * variables are currently in use. Same as
|
---|
803 | * the current procedure's frame, if any,
|
---|
804 | * unless an "uplevel" is executing. */
|
---|
805 | Var *compiledLocals = varFramePtr->compiledLocals;
|
---|
806 | Var *arrayPtr; /* Points to the array's in-frame Var
|
---|
807 | * structure. */
|
---|
808 | char *arrayName; /* Name of the local array. */
|
---|
809 | Tcl_HashEntry *hPtr;
|
---|
810 | Var *varPtr = NULL; /* Points to the element's Var structure
|
---|
811 | * that we return. Initialized to avoid
|
---|
812 | * compiler warning. */
|
---|
813 | char *elem, *msg;
|
---|
814 | int new;
|
---|
815 |
|
---|
816 | /*
|
---|
817 | * THIS FAILS IF THE ELEMENT NAME OBJECT'S STRING REP HAS A NULL BYTE.
|
---|
818 | */
|
---|
819 |
|
---|
820 | elem = Tcl_GetStringFromObj(elemPtr, (int *) NULL);
|
---|
821 | arrayPtr = &(compiledLocals[localIndex]);
|
---|
822 | arrayName = arrayPtr->name;
|
---|
823 |
|
---|
824 | /*
|
---|
825 | * If arrayPtr is a link variable, we have a reference to some variable
|
---|
826 | * that was created through an "upvar" or "global" command, or we have a
|
---|
827 | * reference to a variable in an enclosing namespace. Traverse through
|
---|
828 | * any links until we find the referenced variable.
|
---|
829 | */
|
---|
830 |
|
---|
831 | while (TclIsVarLink(arrayPtr)) {
|
---|
832 | arrayPtr = arrayPtr->value.linkPtr;
|
---|
833 | }
|
---|
834 |
|
---|
835 | /*
|
---|
836 | * Make sure we're dealing with an array and that the array variable
|
---|
837 | * exists (isn't undefined).
|
---|
838 | */
|
---|
839 |
|
---|
840 | if (!TclIsVarArray(arrayPtr) || TclIsVarUndefined(arrayPtr)) {
|
---|
841 | if (leaveErrorMsg) {
|
---|
842 | VarErrMsg(interp, arrayName, elem, "read", noSuchVar);
|
---|
843 | }
|
---|
844 | goto errorReturn;
|
---|
845 | }
|
---|
846 |
|
---|
847 | /*
|
---|
848 | * Look up the element. Note that we must create the element (but leave
|
---|
849 | * it marked undefined) if it does not already exist. This allows a
|
---|
850 | * trace to create new array elements "on the fly" that did not exist
|
---|
851 | * before. A trace is always passed a variable for the array element. If
|
---|
852 | * the trace does not define the variable, it will be deleted below (at
|
---|
853 | * errorReturn) and an error returned.
|
---|
854 | */
|
---|
855 |
|
---|
856 | hPtr = Tcl_CreateHashEntry(arrayPtr->value.tablePtr, elem, &new);
|
---|
857 | if (new) {
|
---|
858 | if (arrayPtr->searchPtr != NULL) {
|
---|
859 | DeleteSearches(arrayPtr);
|
---|
860 | }
|
---|
861 | varPtr = NewVar();
|
---|
862 | Tcl_SetHashValue(hPtr, varPtr);
|
---|
863 | varPtr->hPtr = hPtr;
|
---|
864 | varPtr->nsPtr = varFramePtr->nsPtr;
|
---|
865 | TclSetVarArrayElement(varPtr);
|
---|
866 | } else {
|
---|
867 | varPtr = (Var *) Tcl_GetHashValue(hPtr);
|
---|
868 | }
|
---|
869 |
|
---|
870 | /*
|
---|
871 | * Invoke any traces that have been set for the element variable.
|
---|
872 | */
|
---|
873 |
|
---|
874 | if ((varPtr->tracePtr != NULL)
|
---|
875 | || ((arrayPtr != NULL) && (arrayPtr->tracePtr != NULL))) {
|
---|
876 | msg = CallTraces(iPtr, arrayPtr, varPtr, arrayName, elem,
|
---|
877 | TCL_TRACE_READS);
|
---|
878 | if (msg != NULL) {
|
---|
879 | if (leaveErrorMsg) {
|
---|
880 | VarErrMsg(interp, arrayName, elem, "read", msg);
|
---|
881 | }
|
---|
882 | goto errorReturn;
|
---|
883 | }
|
---|
884 | }
|
---|
885 |
|
---|
886 | /*
|
---|
887 | * Return the element if it's an existing scalar variable.
|
---|
888 | */
|
---|
889 |
|
---|
890 | if (TclIsVarScalar(varPtr) && !TclIsVarUndefined(varPtr)) {
|
---|
891 | return varPtr->value.objPtr;
|
---|
892 | }
|
---|
893 |
|
---|
894 | if (leaveErrorMsg) {
|
---|
895 | if (TclIsVarArray(varPtr)) {
|
---|
896 | msg = isArray;
|
---|
897 | } else {
|
---|
898 | msg = noSuchVar;
|
---|
899 | }
|
---|
900 | VarErrMsg(interp, arrayName, elem, "read", msg);
|
---|
901 | }
|
---|
902 |
|
---|
903 | /*
|
---|
904 | * An error. If the variable doesn't exist anymore and no-one's using
|
---|
905 | * it, then free up the relevant structures and hash table entries.
|
---|
906 | */
|
---|
907 |
|
---|
908 | errorReturn:
|
---|
909 | if ((varPtr != NULL) && TclIsVarUndefined(varPtr)) {
|
---|
910 | CleanupVar(varPtr, NULL); /* the array is not in a hashtable */
|
---|
911 | }
|
---|
912 | return NULL;
|
---|
913 | }
|
---|
914 | |
---|
915 |
|
---|
916 | /*
|
---|
917 | *----------------------------------------------------------------------
|
---|
918 | *
|
---|
919 | * Tcl_SetCmd --
|
---|
920 | *
|
---|
921 | * This procedure is invoked to process the "set" Tcl command.
|
---|
922 | * See the user documentation for details on what it does.
|
---|
923 | *
|
---|
924 | * Results:
|
---|
925 | * A standard Tcl result value.
|
---|
926 | *
|
---|
927 | * Side effects:
|
---|
928 | * A variable's value may be changed.
|
---|
929 | *
|
---|
930 | *----------------------------------------------------------------------
|
---|
931 | */
|
---|
932 |
|
---|
933 | /* ARGSUSED */
|
---|
934 | int
|
---|
935 | Tcl_SetCmd(dummy, interp, argc, argv)
|
---|
936 | ClientData dummy; /* Not used. */
|
---|
937 | register Tcl_Interp *interp; /* Current interpreter. */
|
---|
938 | int argc; /* Number of arguments. */
|
---|
939 | char **argv; /* Argument strings. */
|
---|
940 | {
|
---|
941 | if (argc == 2) {
|
---|
942 | char *value;
|
---|
943 |
|
---|
944 | value = Tcl_GetVar2(interp, argv[1], (char *) NULL,
|
---|
945 | TCL_LEAVE_ERR_MSG|TCL_PARSE_PART1);
|
---|
946 | if (value == NULL) {
|
---|
947 | return TCL_ERROR;
|
---|
948 | }
|
---|
949 | Tcl_SetResult(interp, value, TCL_VOLATILE);
|
---|
950 | return TCL_OK;
|
---|
951 | } else if (argc == 3) {
|
---|
952 | char *result;
|
---|
953 |
|
---|
954 | result = Tcl_SetVar2(interp, argv[1], (char *) NULL, argv[2],
|
---|
955 | TCL_LEAVE_ERR_MSG|TCL_PARSE_PART1);
|
---|
956 | if (result == NULL) {
|
---|
957 | return TCL_ERROR;
|
---|
958 | }
|
---|
959 | Tcl_SetResult(interp, result, TCL_VOLATILE);
|
---|
960 | return TCL_OK;
|
---|
961 | } else {
|
---|
962 | Tcl_AppendResult(interp, "wrong # args: should be \"",
|
---|
963 | argv[0], " varName ?newValue?\"", (char *) NULL);
|
---|
964 | return TCL_ERROR;
|
---|
965 | }
|
---|
966 | }
|
---|
967 | |
---|
968 |
|
---|
969 | /*
|
---|
970 | *----------------------------------------------------------------------
|
---|
971 | *
|
---|
972 | * Tcl_SetVar --
|
---|
973 | *
|
---|
974 | * Change the value of a variable.
|
---|
975 | *
|
---|
976 | * Results:
|
---|
977 | * Returns a pointer to the malloc'ed string which is the character
|
---|
978 | * representation of the variable's new value. The caller must not
|
---|
979 | * modify this string. If the write operation was disallowed then NULL
|
---|
980 | * is returned; if the TCL_LEAVE_ERR_MSG flag is set, then an
|
---|
981 | * explanatory message will be left in interp->result. Note that the
|
---|
982 | * returned string may not be the same as newValue; this is because
|
---|
983 | * variable traces may modify the variable's value.
|
---|
984 | *
|
---|
985 | * Side effects:
|
---|
986 | * If varName is defined as a local or global variable in interp,
|
---|
987 | * its value is changed to newValue. If varName isn't currently
|
---|
988 | * defined, then a new global variable by that name is created.
|
---|
989 | *
|
---|
990 | *----------------------------------------------------------------------
|
---|
991 | */
|
---|
992 |
|
---|
993 | char *
|
---|
994 | Tcl_SetVar(interp, varName, newValue, flags)
|
---|
995 | Tcl_Interp *interp; /* Command interpreter in which varName is
|
---|
996 | * to be looked up. */
|
---|
997 | char *varName; /* Name of a variable in interp. */
|
---|
998 | char *newValue; /* New value for varName. */
|
---|
999 | int flags; /* Various flags that tell how to set value:
|
---|
1000 | * any of TCL_GLOBAL_ONLY,
|
---|
1001 | * TCL_NAMESPACE_ONLY, TCL_APPEND_VALUE,
|
---|
1002 | * TCL_LIST_ELEMENT, TCL_LEAVE_ERR_MSG. */
|
---|
1003 | {
|
---|
1004 | return Tcl_SetVar2(interp, varName, (char *) NULL, newValue,
|
---|
1005 | (flags | TCL_PARSE_PART1));
|
---|
1006 | }
|
---|
1007 | |
---|
1008 |
|
---|
1009 | /*
|
---|
1010 | *----------------------------------------------------------------------
|
---|
1011 | *
|
---|
1012 | * Tcl_SetVar2 --
|
---|
1013 | *
|
---|
1014 | * Given a two-part variable name, which may refer either to a
|
---|
1015 | * scalar variable or an element of an array, change the value
|
---|
1016 | * of the variable. If the named scalar or array or element
|
---|
1017 | * doesn't exist then create one.
|
---|
1018 | *
|
---|
1019 | * Results:
|
---|
1020 | * Returns a pointer to the malloc'ed string which is the character
|
---|
1021 | * representation of the variable's new value. The caller must not
|
---|
1022 | * modify this string. If the write operation was disallowed because an
|
---|
1023 | * array was expected but not found (or vice versa), then NULL is
|
---|
1024 | * returned; if the TCL_LEAVE_ERR_MSG flag is set, then an explanatory
|
---|
1025 | * message will be left in interp->result. Note that the returned
|
---|
1026 | * string may not be the same as newValue; this is because variable
|
---|
1027 | * traces may modify the variable's value.
|
---|
1028 | *
|
---|
1029 | * Side effects:
|
---|
1030 | * The value of the given variable is set. If either the array
|
---|
1031 | * or the entry didn't exist then a new one is created.
|
---|
1032 | *
|
---|
1033 | *----------------------------------------------------------------------
|
---|
1034 | */
|
---|
1035 |
|
---|
1036 | char *
|
---|
1037 | Tcl_SetVar2(interp, part1, part2, newValue, flags)
|
---|
1038 | Tcl_Interp *interp; /* Command interpreter in which variable is
|
---|
1039 | * to be looked up. */
|
---|
1040 | char *part1; /* If part2 is NULL, this is name of scalar
|
---|
1041 | * variable. Otherwise it is the name of
|
---|
1042 | * an array. */
|
---|
1043 | char *part2; /* Name of an element within an array, or
|
---|
1044 | * NULL. */
|
---|
1045 | char *newValue; /* New value for variable. */
|
---|
1046 | int flags; /* Various flags that tell how to set value:
|
---|
1047 | * any of TCL_GLOBAL_ONLY,
|
---|
1048 | * TCL_NAMESPACE_ONLY, TCL_APPEND_VALUE,
|
---|
1049 | * TCL_LIST_ELEMENT, TCL_LEAVE_ERR_MSG, or
|
---|
1050 | * TCL_PARSE_PART1. */
|
---|
1051 | {
|
---|
1052 | register Tcl_Obj *valuePtr;
|
---|
1053 | register Tcl_Obj *part1Ptr;
|
---|
1054 | register Tcl_Obj *part2Ptr = NULL;
|
---|
1055 | Tcl_Obj *varValuePtr;
|
---|
1056 | int length;
|
---|
1057 |
|
---|
1058 | /*
|
---|
1059 | * Create an object holding the variable's new value and use
|
---|
1060 | * Tcl_ObjSetVar2 to actually set the variable.
|
---|
1061 | */
|
---|
1062 |
|
---|
1063 | length = newValue ? strlen(newValue) : 0;
|
---|
1064 | TclNewObj(valuePtr);
|
---|
1065 | TclInitStringRep(valuePtr, newValue, length);
|
---|
1066 | Tcl_IncrRefCount(valuePtr);
|
---|
1067 |
|
---|
1068 | length = strlen(part1) ;
|
---|
1069 | TclNewObj(part1Ptr);
|
---|
1070 | TclInitStringRep(part1Ptr, part1, length);
|
---|
1071 | Tcl_IncrRefCount(part1Ptr);
|
---|
1072 |
|
---|
1073 | if (part2 != NULL) {
|
---|
1074 | length = strlen(part2);
|
---|
1075 | TclNewObj(part2Ptr);
|
---|
1076 | TclInitStringRep(part2Ptr, part2, length);
|
---|
1077 | Tcl_IncrRefCount(part2Ptr);
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | varValuePtr = Tcl_ObjSetVar2(interp, part1Ptr, part2Ptr, valuePtr,
|
---|
1081 | flags);
|
---|
1082 |
|
---|
1083 | TclDecrRefCount(part1Ptr); /* done with the part1 name object */
|
---|
1084 | if (part2Ptr != NULL) {
|
---|
1085 | TclDecrRefCount(part2Ptr); /* and the part2 name object */
|
---|
1086 | }
|
---|
1087 | Tcl_DecrRefCount(valuePtr); /* done with the object */
|
---|
1088 |
|
---|
1089 | if (varValuePtr == NULL) {
|
---|
1090 | /*
|
---|
1091 | * Move the interpreter's object result to the string result,
|
---|
1092 | * then reset the object result.
|
---|
1093 | * FAILS IF OBJECT RESULT'S STRING REPRESENTATION CONTAINS NULLS.
|
---|
1094 | */
|
---|
1095 |
|
---|
1096 | Tcl_SetResult(interp,
|
---|
1097 | TclGetStringFromObj(Tcl_GetObjResult(interp), (int *) NULL),
|
---|
1098 | TCL_VOLATILE);
|
---|
1099 | return NULL;
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | /*
|
---|
1103 | * THIS FAILS IF Tcl_ObjSetVar2's RESULT'S STRING REP HAS A NULL BYTE.
|
---|
1104 | */
|
---|
1105 |
|
---|
1106 | return TclGetStringFromObj(varValuePtr, (int *) NULL);
|
---|
1107 | }
|
---|
1108 | |
---|
1109 |
|
---|
1110 | /*
|
---|
1111 | *----------------------------------------------------------------------
|
---|
1112 | *
|
---|
1113 | * Tcl_ObjSetVar2 --
|
---|
1114 | *
|
---|
1115 | * Given a two-part variable name, which may refer either to a scalar
|
---|
1116 | * variable or an element of an array, change the value of the variable
|
---|
1117 | * to a new Tcl object value. If the named scalar or array or element
|
---|
1118 | * doesn't exist then create one.
|
---|
1119 | *
|
---|
1120 | * Results:
|
---|
1121 | * Returns a pointer to the Tcl_Obj holding the new value of the
|
---|
1122 | * variable. If the write operation was disallowed because an array was
|
---|
1123 | * expected but not found (or vice versa), then NULL is returned; if
|
---|
1124 | * the TCL_LEAVE_ERR_MSG flag is set, then an explanatory message will
|
---|
1125 | * be left in the interpreter's result. Note that the returned object
|
---|
1126 | * may not be the same one referenced by newValuePtr; this is because
|
---|
1127 | * variable traces may modify the variable's value.
|
---|
1128 | *
|
---|
1129 | * Side effects:
|
---|
1130 | * The value of the given variable is set. If either the array or the
|
---|
1131 | * entry didn't exist then a new variable is created.
|
---|
1132 | *
|
---|
1133 | * The reference count is decremented for any old value of the variable
|
---|
1134 | * and incremented for its new value. If the new value for the variable
|
---|
1135 | * is not the same one referenced by newValuePtr (perhaps as a result
|
---|
1136 | * of a variable trace), then newValuePtr's ref count is left unchanged
|
---|
1137 | * by Tcl_ObjSetVar2. newValuePtr's ref count is also left unchanged if
|
---|
1138 | * we are appending it as a string value: that is, if "flags" includes
|
---|
1139 | * TCL_APPEND_VALUE but not TCL_LIST_ELEMENT.
|
---|
1140 | *
|
---|
1141 | * The reference count for the returned object is _not_ incremented: if
|
---|
1142 | * you want to keep a reference to the object you must increment its
|
---|
1143 | * ref count yourself.
|
---|
1144 | *
|
---|
1145 | *----------------------------------------------------------------------
|
---|
1146 | */
|
---|
1147 |
|
---|
1148 | Tcl_Obj *
|
---|
1149 | Tcl_ObjSetVar2(interp, part1Ptr, part2Ptr, newValuePtr, flags)
|
---|
1150 | Tcl_Interp *interp; /* Command interpreter in which variable is
|
---|
1151 | * to be found. */
|
---|
1152 | register Tcl_Obj *part1Ptr; /* Points to an object holding the name of
|
---|
1153 | * an array (if part2 is non-NULL) or the
|
---|
1154 | * name of a variable. */
|
---|
1155 | register Tcl_Obj *part2Ptr; /* If non-null, points to an object holding
|
---|
1156 | * the name of an element in the array
|
---|
1157 | * part1Ptr. */
|
---|
1158 | Tcl_Obj *newValuePtr; /* New value for variable. */
|
---|
1159 | int flags; /* Various flags that tell how to set value:
|
---|
1160 | * any of TCL_GLOBAL_ONLY,
|
---|
1161 | * TCL_NAMESPACE_ONLY, TCL_APPEND_VALUE,
|
---|
1162 | * TCL_LIST_ELEMENT, TCL_LEAVE_ERR_MSG, or
|
---|
1163 | * TCL_PARSE_PART1. */
|
---|
1164 | {
|
---|
1165 | Interp *iPtr = (Interp *) interp;
|
---|
1166 | register Var *varPtr;
|
---|
1167 | Var *arrayPtr;
|
---|
1168 | Tcl_Obj *oldValuePtr;
|
---|
1169 | Tcl_Obj *resultPtr = NULL;
|
---|
1170 | char *part1, *bytes;
|
---|
1171 | char *part2 = NULL;
|
---|
1172 | int length, result;
|
---|
1173 |
|
---|
1174 | /*
|
---|
1175 | * THIS FAILS IF A NAME OBJECT'S STRING REP HAS A NULL BYTE.
|
---|
1176 | */
|
---|
1177 |
|
---|
1178 | part1 = TclGetStringFromObj(part1Ptr, (int *) NULL);
|
---|
1179 | if (part2Ptr != NULL) {
|
---|
1180 | part2 = TclGetStringFromObj(part2Ptr, (int *) NULL);
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | varPtr = TclLookupVar(interp, part1, part2, flags, "set",
|
---|
1184 | /*createPart1*/ 1, /*createPart2*/ 1, &arrayPtr);
|
---|
1185 | if (varPtr == NULL) {
|
---|
1186 | return NULL;
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | /*
|
---|
1190 | * If the variable is in a hashtable and its hPtr field is NULL, then we
|
---|
1191 | * may have an upvar to an array element where the array was deleted
|
---|
1192 | * or an upvar to a namespace variable whose namespace was deleted.
|
---|
1193 | * Generate an error (allowing the variable to be reset would screw up
|
---|
1194 | * our storage allocation and is meaningless anyway).
|
---|
1195 | */
|
---|
1196 |
|
---|
1197 | if ((varPtr->flags & VAR_IN_HASHTABLE) && (varPtr->hPtr == NULL)) {
|
---|
1198 | if (flags & TCL_LEAVE_ERR_MSG) {
|
---|
1199 | if (TclIsVarArrayElement(varPtr)) {
|
---|
1200 | VarErrMsg(interp, part1, part2, "set", danglingElement);
|
---|
1201 | } else {
|
---|
1202 | VarErrMsg(interp, part1, part2, "set", danglingVar);
|
---|
1203 | }
|
---|
1204 | }
|
---|
1205 | return NULL;
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | /*
|
---|
1209 | * It's an error to try to set an array variable itself.
|
---|
1210 | */
|
---|
1211 |
|
---|
1212 | if (TclIsVarArray(varPtr) && !TclIsVarUndefined(varPtr)) {
|
---|
1213 | if (flags & TCL_LEAVE_ERR_MSG) {
|
---|
1214 | VarErrMsg(interp, part1, part2, "set", isArray);
|
---|
1215 | }
|
---|
1216 | return NULL;
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | /*
|
---|
1220 | * At this point, if we were appending, we used to call read traces: we
|
---|
1221 | * treated append as a read-modify-write. However, it seemed unlikely to
|
---|
1222 | * us that a real program would be interested in such reads being done
|
---|
1223 | * during a set operation.
|
---|
1224 | */
|
---|
1225 |
|
---|
1226 | /*
|
---|
1227 | * Set the variable's new value. If appending, append the new value to
|
---|
1228 | * the variable, either as a list element or as a string. Also, if
|
---|
1229 | * appending, then if the variable's old value is unshared we can modify
|
---|
1230 | * it directly, otherwise we must create a new copy to modify: this is
|
---|
1231 | * "copy on write".
|
---|
1232 | */
|
---|
1233 |
|
---|
1234 | oldValuePtr = varPtr->value.objPtr;
|
---|
1235 | if (flags & TCL_APPEND_VALUE) {
|
---|
1236 | if (TclIsVarUndefined(varPtr) && (oldValuePtr != NULL)) {
|
---|
1237 | Tcl_DecrRefCount(oldValuePtr); /* discard old value */
|
---|
1238 | varPtr->value.objPtr = NULL;
|
---|
1239 | oldValuePtr = NULL;
|
---|
1240 | }
|
---|
1241 | if (flags & TCL_LIST_ELEMENT) { /* append list element */
|
---|
1242 | if (oldValuePtr == NULL) {
|
---|
1243 | TclNewObj(oldValuePtr);
|
---|
1244 | varPtr->value.objPtr = oldValuePtr;
|
---|
1245 | Tcl_IncrRefCount(oldValuePtr); /* since var is reference */
|
---|
1246 | } else if (Tcl_IsShared(oldValuePtr)) {
|
---|
1247 | varPtr->value.objPtr = Tcl_DuplicateObj(oldValuePtr);
|
---|
1248 | Tcl_DecrRefCount(oldValuePtr);
|
---|
1249 | oldValuePtr = varPtr->value.objPtr;
|
---|
1250 | Tcl_IncrRefCount(oldValuePtr); /* since var is reference */
|
---|
1251 | }
|
---|
1252 | result = Tcl_ListObjAppendElement(interp, oldValuePtr,
|
---|
1253 | newValuePtr);
|
---|
1254 | if (result != TCL_OK) {
|
---|
1255 | return NULL;
|
---|
1256 | }
|
---|
1257 | } else { /* append string */
|
---|
1258 | /*
|
---|
1259 | * We append newValuePtr's bytes but don't change its ref count.
|
---|
1260 | */
|
---|
1261 |
|
---|
1262 | bytes = Tcl_GetStringFromObj(newValuePtr, &length);
|
---|
1263 | if (oldValuePtr == NULL) {
|
---|
1264 | varPtr->value.objPtr = Tcl_NewStringObj(bytes, length);
|
---|
1265 | Tcl_IncrRefCount(varPtr->value.objPtr);
|
---|
1266 | } else {
|
---|
1267 | if (Tcl_IsShared(oldValuePtr)) { /* append to copy */
|
---|
1268 | varPtr->value.objPtr = Tcl_DuplicateObj(oldValuePtr);
|
---|
1269 | TclDecrRefCount(oldValuePtr);
|
---|
1270 | oldValuePtr = varPtr->value.objPtr;
|
---|
1271 | Tcl_IncrRefCount(oldValuePtr); /* since var is ref */
|
---|
1272 | }
|
---|
1273 | Tcl_AppendToObj(oldValuePtr, bytes, length);
|
---|
1274 | }
|
---|
1275 | }
|
---|
1276 | } else {
|
---|
1277 | if (flags & TCL_LIST_ELEMENT) { /* set var to list element */
|
---|
1278 | int neededBytes, listFlags;
|
---|
1279 |
|
---|
1280 | /*
|
---|
1281 | * We set the variable to the result of converting newValuePtr's
|
---|
1282 | * string rep to a list element. We do not change newValuePtr's
|
---|
1283 | * ref count.
|
---|
1284 | */
|
---|
1285 |
|
---|
1286 | if (oldValuePtr != NULL) {
|
---|
1287 | Tcl_DecrRefCount(oldValuePtr); /* discard old value */
|
---|
1288 | }
|
---|
1289 | bytes = Tcl_GetStringFromObj(newValuePtr, &length);
|
---|
1290 | neededBytes = Tcl_ScanElement(bytes, &listFlags);
|
---|
1291 | oldValuePtr = Tcl_NewObj();
|
---|
1292 | oldValuePtr->bytes = (char *)
|
---|
1293 | ckalloc((unsigned) (neededBytes + 1));
|
---|
1294 | oldValuePtr->length = Tcl_ConvertElement(bytes,
|
---|
1295 | oldValuePtr->bytes, listFlags);
|
---|
1296 | varPtr->value.objPtr = oldValuePtr;
|
---|
1297 | Tcl_IncrRefCount(varPtr->value.objPtr);
|
---|
1298 | } else if (newValuePtr != oldValuePtr) {
|
---|
1299 | varPtr->value.objPtr = newValuePtr;
|
---|
1300 | Tcl_IncrRefCount(newValuePtr); /* var is another ref */
|
---|
1301 | if (oldValuePtr != NULL) {
|
---|
1302 | TclDecrRefCount(oldValuePtr); /* discard old value */
|
---|
1303 | }
|
---|
1304 | }
|
---|
1305 | }
|
---|
1306 | TclSetVarScalar(varPtr);
|
---|
1307 | TclClearVarUndefined(varPtr);
|
---|
1308 | if (arrayPtr != NULL) {
|
---|
1309 | TclClearVarUndefined(arrayPtr);
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | /*
|
---|
1313 | * Invoke any write traces for the variable.
|
---|
1314 | */
|
---|
1315 |
|
---|
1316 | if ((varPtr->tracePtr != NULL)
|
---|
1317 | || ((arrayPtr != NULL) && (arrayPtr->tracePtr != NULL))) {
|
---|
1318 | char *msg = CallTraces(iPtr, arrayPtr, varPtr, part1, part2,
|
---|
1319 | (flags & (TCL_GLOBAL_ONLY|TCL_NAMESPACE_ONLY|TCL_PARSE_PART1)) | TCL_TRACE_WRITES);
|
---|
1320 | if (msg != NULL) {
|
---|
1321 | if (flags & TCL_LEAVE_ERR_MSG) {
|
---|
1322 | VarErrMsg(interp, part1, part2, "set", msg);
|
---|
1323 | }
|
---|
1324 | goto cleanup;
|
---|
1325 | }
|
---|
1326 | }
|
---|
1327 |
|
---|
1328 | /*
|
---|
1329 | * Return the variable's value unless the variable was changed in some
|
---|
1330 | * gross way by a trace (e.g. it was unset and then recreated as an
|
---|
1331 | * array).
|
---|
1332 | */
|
---|
1333 |
|
---|
1334 | if (TclIsVarScalar(varPtr) && !TclIsVarUndefined(varPtr)) {
|
---|
1335 | return varPtr->value.objPtr;
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | /*
|
---|
1339 | * A trace changed the value in some gross way. Return an empty string
|
---|
1340 | * object.
|
---|
1341 | */
|
---|
1342 |
|
---|
1343 | resultPtr = iPtr->emptyObjPtr;
|
---|
1344 |
|
---|
1345 | /*
|
---|
1346 | * If the variable doesn't exist anymore and no-one's using it, then
|
---|
1347 | * free up the relevant structures and hash table entries.
|
---|
1348 | */
|
---|
1349 |
|
---|
1350 | cleanup:
|
---|
1351 | if (TclIsVarUndefined(varPtr)) {
|
---|
1352 | CleanupVar(varPtr, arrayPtr);
|
---|
1353 | }
|
---|
1354 | return resultPtr;
|
---|
1355 | }
|
---|
1356 | |
---|
1357 |
|
---|
1358 | /*
|
---|
1359 | *----------------------------------------------------------------------
|
---|
1360 | *
|
---|
1361 | * TclSetIndexedScalar --
|
---|
1362 | *
|
---|
1363 | * Change the Tcl object value of a local scalar variable in the active
|
---|
1364 | * procedure, given its compile-time allocated index in the procedure's
|
---|
1365 | * array of local variables.
|
---|
1366 | *
|
---|
1367 | * Results:
|
---|
1368 | * Returns a pointer to the Tcl_Obj holding the new value of the
|
---|
1369 | * variable given by localIndex. If the specified variable doesn't
|
---|
1370 | * exist, or there is a clash in array usage, or an error occurs while
|
---|
1371 | * executing variable traces, then NULL is returned and a message will
|
---|
1372 | * be left in the interpreter's result if leaveErrorMsg is 1. Note
|
---|
1373 | * that the returned object may not be the same one referenced by
|
---|
1374 | * newValuePtr; this is because variable traces may modify the
|
---|
1375 | * variable's value.
|
---|
1376 | *
|
---|
1377 | * Side effects:
|
---|
1378 | * The value of the given variable is set. The reference count is
|
---|
1379 | * decremented for any old value of the variable and incremented for
|
---|
1380 | * its new value. If as a result of a variable trace the new value for
|
---|
1381 | * the variable is not the same one referenced by newValuePtr, then
|
---|
1382 | * newValuePtr's ref count is left unchanged. The ref count for the
|
---|
1383 | * returned object is _not_ incremented to reflect the returned
|
---|
1384 | * reference; if you want to keep a reference to the object you must
|
---|
1385 | * increment its ref count yourself. This procedure does not create
|
---|
1386 | * new variables, but only sets those recognized at compile time.
|
---|
1387 | *
|
---|
1388 | *----------------------------------------------------------------------
|
---|
1389 | */
|
---|
1390 |
|
---|
1391 | Tcl_Obj *
|
---|
1392 | TclSetIndexedScalar(interp, localIndex, newValuePtr, leaveErrorMsg)
|
---|
1393 | Tcl_Interp *interp; /* Command interpreter in which variable is
|
---|
1394 | * to be found. */
|
---|
1395 | int localIndex; /* Index of variable in procedure's array
|
---|
1396 | * of local variables. */
|
---|
1397 | Tcl_Obj *newValuePtr; /* New value for variable. */
|
---|
1398 | int leaveErrorMsg; /* 1 if to leave an error message in
|
---|
1399 | * the interpreter's result on an error.
|
---|
1400 | * Otherwise no error message is left. */
|
---|
1401 | {
|
---|
1402 | Interp *iPtr = (Interp *) interp;
|
---|
1403 | CallFrame *varFramePtr = iPtr->varFramePtr;
|
---|
1404 | /* Points to the procedure call frame whose
|
---|
1405 | * variables are currently in use. Same as
|
---|
1406 | * the current procedure's frame, if any,
|
---|
1407 | * unless an "uplevel" is executing. */
|
---|
1408 | Var *compiledLocals = varFramePtr->compiledLocals;
|
---|
1409 | register Var *varPtr; /* Points to the variable's in-frame Var
|
---|
1410 | * structure. */
|
---|
1411 | char *varName; /* Name of the local variable. */
|
---|
1412 | Tcl_Obj *oldValuePtr;
|
---|
1413 | Tcl_Obj *resultPtr = NULL;
|
---|
1414 |
|
---|
1415 | varPtr = &(compiledLocals[localIndex]);
|
---|
1416 | varName = varPtr->name;
|
---|
1417 |
|
---|
1418 | /*
|
---|
1419 | * If varPtr is a link variable, we have a reference to some variable
|
---|
1420 | * that was created through an "upvar" or "global" command, or we have a
|
---|
1421 | * reference to a variable in an enclosing namespace. Traverse through
|
---|
1422 | * any links until we find the referenced variable.
|
---|
1423 | */
|
---|
1424 |
|
---|
1425 | while (TclIsVarLink(varPtr)) {
|
---|
1426 | varPtr = varPtr->value.linkPtr;
|
---|
1427 | }
|
---|
1428 |
|
---|
1429 | /*
|
---|
1430 | * If the variable is in a hashtable and its hPtr field is NULL, then we
|
---|
1431 | * may have an upvar to an array element where the array was deleted
|
---|
1432 | * or an upvar to a namespace variable whose namespace was deleted.
|
---|
1433 | * Generate an error (allowing the variable to be reset would screw up
|
---|
1434 | * our storage allocation and is meaningless anyway).
|
---|
1435 | */
|
---|
1436 |
|
---|
1437 | if ((varPtr->flags & VAR_IN_HASHTABLE) && (varPtr->hPtr == NULL)) {
|
---|
1438 | if (leaveErrorMsg) {
|
---|
1439 | if (TclIsVarArrayElement(varPtr)) {
|
---|
1440 | VarErrMsg(interp, varName, NULL, "set", danglingElement);
|
---|
1441 | } else {
|
---|
1442 | VarErrMsg(interp, varName, NULL, "set", danglingVar);
|
---|
1443 | }
|
---|
1444 | }
|
---|
1445 | return NULL;
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | /*
|
---|
1449 | * It's an error to try to set an array variable itself.
|
---|
1450 | */
|
---|
1451 |
|
---|
1452 | if (TclIsVarArray(varPtr) && !TclIsVarUndefined(varPtr)) {
|
---|
1453 | if (leaveErrorMsg) {
|
---|
1454 | VarErrMsg(interp, varName, NULL, "set", isArray);
|
---|
1455 | }
|
---|
1456 | return NULL;
|
---|
1457 | }
|
---|
1458 |
|
---|
1459 | /*
|
---|
1460 | * Set the variable's new value and discard its old value. We don't
|
---|
1461 | * append with this "set" procedure so the old value isn't needed.
|
---|
1462 | */
|
---|
1463 |
|
---|
1464 | oldValuePtr = varPtr->value.objPtr;
|
---|
1465 | if (newValuePtr != oldValuePtr) { /* set new value */
|
---|
1466 | varPtr->value.objPtr = newValuePtr;
|
---|
1467 | Tcl_IncrRefCount(newValuePtr); /* var is another ref to obj */
|
---|
1468 | if (oldValuePtr != NULL) {
|
---|
1469 | TclDecrRefCount(oldValuePtr); /* discard old value */
|
---|
1470 | }
|
---|
1471 | }
|
---|
1472 | TclSetVarScalar(varPtr);
|
---|
1473 | TclClearVarUndefined(varPtr);
|
---|
1474 |
|
---|
1475 | /*
|
---|
1476 | * Invoke any write traces for the variable.
|
---|
1477 | */
|
---|
1478 |
|
---|
1479 | if (varPtr->tracePtr != NULL) {
|
---|
1480 | char *msg = CallTraces(iPtr, /*arrayPtr*/ NULL, varPtr,
|
---|
1481 | varName, (char *) NULL, TCL_TRACE_WRITES);
|
---|
1482 | if (msg != NULL) {
|
---|
1483 | if (leaveErrorMsg) {
|
---|
1484 | VarErrMsg(interp, varName, NULL, "set", msg);
|
---|
1485 | }
|
---|
1486 | goto cleanup;
|
---|
1487 | }
|
---|
1488 | }
|
---|
1489 |
|
---|
1490 | /*
|
---|
1491 | * Return the variable's value unless the variable was changed in some
|
---|
1492 | * gross way by a trace (e.g. it was unset and then recreated as an
|
---|
1493 | * array). If it was changed is a gross way, just return an empty string
|
---|
1494 | * object.
|
---|
1495 | */
|
---|
1496 |
|
---|
1497 | if (TclIsVarScalar(varPtr) && !TclIsVarUndefined(varPtr)) {
|
---|
1498 | return varPtr->value.objPtr;
|
---|
1499 | }
|
---|
1500 |
|
---|
1501 | resultPtr = Tcl_NewObj();
|
---|
1502 |
|
---|
1503 | /*
|
---|
1504 | * If the variable doesn't exist anymore and no-one's using it, then
|
---|
1505 | * free up the relevant structures and hash table entries.
|
---|
1506 | */
|
---|
1507 |
|
---|
1508 | cleanup:
|
---|
1509 | if (TclIsVarUndefined(varPtr)) {
|
---|
1510 | CleanupVar(varPtr, NULL);
|
---|
1511 | }
|
---|
1512 | return resultPtr;
|
---|
1513 | }
|
---|
1514 | |
---|
1515 |
|
---|
1516 | /*
|
---|
1517 | *----------------------------------------------------------------------
|
---|
1518 | *
|
---|
1519 | * TclSetElementOfIndexedArray --
|
---|
1520 | *
|
---|
1521 | * Change the Tcl object value of an element in a local array
|
---|
1522 | * variable. The element is named by the object elemPtr while the array
|
---|
1523 | * is specified by its index in the active procedure's array of
|
---|
1524 | * compiler allocated local variables.
|
---|
1525 | *
|
---|
1526 | * Results:
|
---|
1527 | * Returns a pointer to the Tcl_Obj holding the new value of the
|
---|
1528 | * element. If the specified array or element doesn't exist, or there
|
---|
1529 | * is a clash in array usage, or an error occurs while executing
|
---|
1530 | * variable traces, then NULL is returned and a message will be left in
|
---|
1531 | * the interpreter's result if leaveErrorMsg is 1. Note that the
|
---|
1532 | * returned object may not be the same one referenced by newValuePtr;
|
---|
1533 | * this is because variable traces may modify the variable's value.
|
---|
1534 | *
|
---|
1535 | * Side effects:
|
---|
1536 | * The value of the given array element is set. The reference count is
|
---|
1537 | * decremented for any old value of the element and incremented for its
|
---|
1538 | * new value. If as a result of a variable trace the new value for the
|
---|
1539 | * element is not the same one referenced by newValuePtr, then
|
---|
1540 | * newValuePtr's ref count is left unchanged. The ref count for the
|
---|
1541 | * returned object is _not_ incremented to reflect the returned
|
---|
1542 | * reference; if you want to keep a reference to the object you must
|
---|
1543 | * increment its ref count yourself. This procedure will not create new
|
---|
1544 | * array variables, but only sets elements of those arrays recognized
|
---|
1545 | * at compile time. However, if the entry doesn't exist then a new
|
---|
1546 | * variable is created.
|
---|
1547 | *
|
---|
1548 | *----------------------------------------------------------------------
|
---|
1549 | */
|
---|
1550 |
|
---|
1551 | Tcl_Obj *
|
---|
1552 | TclSetElementOfIndexedArray(interp, localIndex, elemPtr, newValuePtr,
|
---|
1553 | leaveErrorMsg)
|
---|
1554 | Tcl_Interp *interp; /* Command interpreter in which the array is
|
---|
1555 | * to be found. */
|
---|
1556 | int localIndex; /* Index of array variable in procedure's
|
---|
1557 | * array of local variables. */
|
---|
1558 | Tcl_Obj *elemPtr; /* Points to an object holding the name of
|
---|
1559 | * an element to set in the array. */
|
---|
1560 | Tcl_Obj *newValuePtr; /* New value for variable. */
|
---|
1561 | int leaveErrorMsg; /* 1 if to leave an error message in
|
---|
1562 | * the interpreter's result on an error.
|
---|
1563 | * Otherwise no error message is left. */
|
---|
1564 | {
|
---|
1565 | Interp *iPtr = (Interp *) interp;
|
---|
1566 | CallFrame *varFramePtr = iPtr->varFramePtr;
|
---|
1567 | /* Points to the procedure call frame whose
|
---|
1568 | * variables are currently in use. Same as
|
---|
1569 | * the current procedure's frame, if any,
|
---|
1570 | * unless an "uplevel" is executing. */
|
---|
1571 | Var *compiledLocals = varFramePtr->compiledLocals;
|
---|
1572 | Var *arrayPtr; /* Points to the array's in-frame Var
|
---|
1573 | * structure. */
|
---|
1574 | char *arrayName; /* Name of the local array. */
|
---|
1575 | char *elem;
|
---|
1576 | Tcl_HashEntry *hPtr;
|
---|
1577 | Var *varPtr = NULL; /* Points to the element's Var structure
|
---|
1578 | * that we return. */
|
---|
1579 | Tcl_Obj *resultPtr = NULL;
|
---|
1580 | Tcl_Obj *oldValuePtr;
|
---|
1581 | int new;
|
---|
1582 |
|
---|
1583 | /*
|
---|
1584 | * THIS FAILS IF THE ELEMENT NAME OBJECT'S STRING REP HAS A NULL BYTE.
|
---|
1585 | */
|
---|
1586 |
|
---|
1587 | elem = Tcl_GetStringFromObj(elemPtr, (int *) NULL);
|
---|
1588 | arrayPtr = &(compiledLocals[localIndex]);
|
---|
1589 | arrayName = arrayPtr->name;
|
---|
1590 |
|
---|
1591 | /*
|
---|
1592 | * If arrayPtr is a link variable, we have a reference to some variable
|
---|
1593 | * that was created through an "upvar" or "global" command, or we have a
|
---|
1594 | * reference to a variable in an enclosing namespace. Traverse through
|
---|
1595 | * any links until we find the referenced variable.
|
---|
1596 | */
|
---|
1597 |
|
---|
1598 | while (TclIsVarLink(arrayPtr)) {
|
---|
1599 | arrayPtr = arrayPtr->value.linkPtr;
|
---|
1600 | }
|
---|
1601 |
|
---|
1602 | /*
|
---|
1603 | * If the variable is in a hashtable and its hPtr field is NULL, then we
|
---|
1604 | * may have an upvar to an array element where the array was deleted
|
---|
1605 | * or an upvar to a namespace variable whose namespace was deleted.
|
---|
1606 | * Generate an error (allowing the variable to be reset would screw up
|
---|
1607 | * our storage allocation and is meaningless anyway).
|
---|
1608 | */
|
---|
1609 |
|
---|
1610 | if ((arrayPtr->flags & VAR_IN_HASHTABLE) && (arrayPtr->hPtr == NULL)) {
|
---|
1611 | if (leaveErrorMsg) {
|
---|
1612 | if (TclIsVarArrayElement(arrayPtr)) {
|
---|
1613 | VarErrMsg(interp, arrayName, elem, "set", danglingElement);
|
---|
1614 | } else {
|
---|
1615 | VarErrMsg(interp, arrayName, elem, "set", danglingVar);
|
---|
1616 | }
|
---|
1617 | }
|
---|
1618 | goto errorReturn;
|
---|
1619 | }
|
---|
1620 |
|
---|
1621 | /*
|
---|
1622 | * Make sure we're dealing with an array.
|
---|
1623 | */
|
---|
1624 |
|
---|
1625 | if (TclIsVarUndefined(arrayPtr) && !TclIsVarArrayElement(arrayPtr)) {
|
---|
1626 | TclSetVarArray(arrayPtr);
|
---|
1627 | arrayPtr->value.tablePtr =
|
---|
1628 | (Tcl_HashTable *) ckalloc(sizeof(Tcl_HashTable));
|
---|
1629 | Tcl_InitHashTable(arrayPtr->value.tablePtr, TCL_STRING_KEYS);
|
---|
1630 | TclClearVarUndefined(arrayPtr);
|
---|
1631 | } else if (!TclIsVarArray(arrayPtr)) {
|
---|
1632 | if (leaveErrorMsg) {
|
---|
1633 | VarErrMsg(interp, arrayName, elem, "set", needArray);
|
---|
1634 | }
|
---|
1635 | goto errorReturn;
|
---|
1636 | }
|
---|
1637 |
|
---|
1638 | /*
|
---|
1639 | * Look up the element.
|
---|
1640 | */
|
---|
1641 |
|
---|
1642 | hPtr = Tcl_CreateHashEntry(arrayPtr->value.tablePtr, elem, &new);
|
---|
1643 | if (new) {
|
---|
1644 | if (arrayPtr->searchPtr != NULL) {
|
---|
1645 | DeleteSearches(arrayPtr);
|
---|
1646 | }
|
---|
1647 | varPtr = NewVar();
|
---|
1648 | Tcl_SetHashValue(hPtr, varPtr);
|
---|
1649 | varPtr->hPtr = hPtr;
|
---|
1650 | varPtr->nsPtr = varFramePtr->nsPtr;
|
---|
1651 | TclSetVarArrayElement(varPtr);
|
---|
1652 | }
|
---|
1653 | varPtr = (Var *) Tcl_GetHashValue(hPtr);
|
---|
1654 |
|
---|
1655 | /*
|
---|
1656 | * It's an error to try to set an array variable itself.
|
---|
1657 | */
|
---|
1658 |
|
---|
1659 | if (TclIsVarArray(varPtr)) {
|
---|
1660 | if (leaveErrorMsg) {
|
---|
1661 | VarErrMsg(interp, arrayName, elem, "set", isArray);
|
---|
1662 | }
|
---|
1663 | goto errorReturn;
|
---|
1664 | }
|
---|
1665 |
|
---|
1666 | /*
|
---|
1667 | * Set the variable's new value and discard the old one. We don't
|
---|
1668 | * append with this "set" procedure so the old value isn't needed.
|
---|
1669 | */
|
---|
1670 |
|
---|
1671 | oldValuePtr = varPtr->value.objPtr;
|
---|
1672 | if (newValuePtr != oldValuePtr) { /* set new value */
|
---|
1673 | varPtr->value.objPtr = newValuePtr;
|
---|
1674 | Tcl_IncrRefCount(newValuePtr); /* var is another ref to obj */
|
---|
1675 | if (oldValuePtr != NULL) {
|
---|
1676 | TclDecrRefCount(oldValuePtr); /* discard old value */
|
---|
1677 | }
|
---|
1678 | }
|
---|
1679 | TclSetVarScalar(varPtr);
|
---|
1680 | TclClearVarUndefined(varPtr);
|
---|
1681 |
|
---|
1682 | /*
|
---|
1683 | * Invoke any write traces for the element variable.
|
---|
1684 | */
|
---|
1685 |
|
---|
1686 | if ((varPtr->tracePtr != NULL)
|
---|
1687 | || ((arrayPtr != NULL) && (arrayPtr->tracePtr != NULL))) {
|
---|
1688 | char *msg = CallTraces(iPtr, arrayPtr, varPtr, arrayName, elem,
|
---|
1689 | TCL_TRACE_WRITES);
|
---|
1690 | if (msg != NULL) {
|
---|
1691 | if (leaveErrorMsg) {
|
---|
1692 | VarErrMsg(interp, arrayName, elem, "set", msg);
|
---|
1693 | }
|
---|
1694 | goto errorReturn;
|
---|
1695 | }
|
---|
1696 | }
|
---|
1697 |
|
---|
1698 | /*
|
---|
1699 | * Return the element's value unless it was changed in some gross way by
|
---|
1700 | * a trace (e.g. it was unset and then recreated as an array). If it was
|
---|
1701 | * changed is a gross way, just return an empty string object.
|
---|
1702 | */
|
---|
1703 |
|
---|
1704 | if (TclIsVarScalar(varPtr) && !TclIsVarUndefined(varPtr)) {
|
---|
1705 | return varPtr->value.objPtr;
|
---|
1706 | }
|
---|
1707 |
|
---|
1708 | resultPtr = Tcl_NewObj();
|
---|
1709 |
|
---|
1710 | /*
|
---|
1711 | * An error. If the variable doesn't exist anymore and no-one's using
|
---|
1712 | * it, then free up the relevant structures and hash table entries.
|
---|
1713 | */
|
---|
1714 |
|
---|
1715 | errorReturn:
|
---|
1716 | if (varPtr != NULL) {
|
---|
1717 | if (TclIsVarUndefined(varPtr)) {
|
---|
1718 | CleanupVar(varPtr, NULL); /* note: array isn't in hashtable */
|
---|
1719 | }
|
---|
1720 | }
|
---|
1721 | return resultPtr;
|
---|
1722 | }
|
---|
1723 | |
---|
1724 |
|
---|
1725 | /*
|
---|
1726 | *----------------------------------------------------------------------
|
---|
1727 | *
|
---|
1728 | * TclIncrVar2 --
|
---|
1729 | *
|
---|
1730 | * Given a two-part variable name, which may refer either to a scalar
|
---|
1731 | * variable or an element of an array, increment the Tcl object value
|
---|
1732 | * of the variable by a specified amount.
|
---|
1733 | *
|
---|
1734 | * Results:
|
---|
1735 | * Returns a pointer to the Tcl_Obj holding the new value of the
|
---|
1736 | * variable. If the specified variable doesn't exist, or there is a
|
---|
1737 | * clash in array usage, or an error occurs while executing variable
|
---|
1738 | * traces, then NULL is returned and a message will be left in
|
---|
1739 | * the interpreter's result.
|
---|
1740 | *
|
---|
1741 | * Side effects:
|
---|
1742 | * The value of the given variable is incremented by the specified
|
---|
1743 | * amount. If either the array or the entry didn't exist then a new
|
---|
1744 | * variable is created. The ref count for the returned object is _not_
|
---|
1745 | * incremented to reflect the returned reference; if you want to keep a
|
---|
1746 | * reference to the object you must increment its ref count yourself.
|
---|
1747 | *
|
---|
1748 | *----------------------------------------------------------------------
|
---|
1749 | */
|
---|
1750 |
|
---|
1751 | Tcl_Obj *
|
---|
1752 | TclIncrVar2(interp, part1Ptr, part2Ptr, incrAmount, part1NotParsed)
|
---|
1753 | Tcl_Interp *interp; /* Command interpreter in which variable is
|
---|
1754 | * to be found. */
|
---|
1755 | Tcl_Obj *part1Ptr; /* Points to an object holding the name of
|
---|
1756 | * an array (if part2 is non-NULL) or the
|
---|
1757 | * name of a variable. */
|
---|
1758 | Tcl_Obj *part2Ptr; /* If non-null, points to an object holding
|
---|
1759 | * the name of an element in the array
|
---|
1760 | * part1Ptr. */
|
---|
1761 | long incrAmount; /* Amount to be added to variable. */
|
---|
1762 | int part1NotParsed; /* 1 if part1 hasn't yet been parsed into
|
---|
1763 | * an array name and index (if any). */
|
---|
1764 | {
|
---|
1765 | register Tcl_Obj *varValuePtr;
|
---|
1766 | Tcl_Obj *resultPtr;
|
---|
1767 | int createdNewObj; /* Set 1 if var's value object is shared
|
---|
1768 | * so we must increment a copy (i.e. copy
|
---|
1769 | * on write). */
|
---|
1770 | long i;
|
---|
1771 | int flags, result;
|
---|
1772 |
|
---|
1773 | flags = TCL_LEAVE_ERR_MSG;
|
---|
1774 | if (part1NotParsed) {
|
---|
1775 | flags |= TCL_PARSE_PART1;
|
---|
1776 | }
|
---|
1777 |
|
---|
1778 | varValuePtr = Tcl_ObjGetVar2(interp, part1Ptr, part2Ptr, flags);
|
---|
1779 | if (varValuePtr == NULL) {
|
---|
1780 | Tcl_AddObjErrorInfo(interp,
|
---|
1781 | "\n (reading value of variable to increment)", -1);
|
---|
1782 | return NULL;
|
---|
1783 | }
|
---|
1784 |
|
---|
1785 | /*
|
---|
1786 | * Increment the variable's value. If the object is unshared we can
|
---|
1787 | * modify it directly, otherwise we must create a new copy to modify:
|
---|
1788 | * this is "copy on write". Then free the variable's old string
|
---|
1789 | * representation, if any, since it will no longer be valid.
|
---|
1790 | */
|
---|
1791 |
|
---|
1792 | createdNewObj = 0;
|
---|
1793 | if (Tcl_IsShared(varValuePtr)) {
|
---|
1794 | varValuePtr = Tcl_DuplicateObj(varValuePtr);
|
---|
1795 | createdNewObj = 1;
|
---|
1796 | }
|
---|
1797 | result = Tcl_GetLongFromObj(interp, varValuePtr, &i);
|
---|
1798 | if (result != TCL_OK) {
|
---|
1799 | if (createdNewObj) {
|
---|
1800 | Tcl_DecrRefCount(varValuePtr); /* free unneeded copy */
|
---|
1801 | }
|
---|
1802 | return NULL;
|
---|
1803 | }
|
---|
1804 | Tcl_SetLongObj(varValuePtr, (i + incrAmount));
|
---|
1805 |
|
---|
1806 | /*
|
---|
1807 | * Store the variable's new value and run any write traces.
|
---|
1808 | */
|
---|
1809 |
|
---|
1810 | resultPtr = Tcl_ObjSetVar2(interp, part1Ptr, part2Ptr, varValuePtr,
|
---|
1811 | flags);
|
---|
1812 | if (resultPtr == NULL) {
|
---|
1813 | return NULL;
|
---|
1814 | }
|
---|
1815 | return resultPtr;
|
---|
1816 | }
|
---|
1817 | |
---|
1818 |
|
---|
1819 | /*
|
---|
1820 | *----------------------------------------------------------------------
|
---|
1821 | *
|
---|
1822 | * TclIncrIndexedScalar --
|
---|
1823 | *
|
---|
1824 | * Increments the Tcl object value of a local scalar variable in the
|
---|
1825 | * active procedure, given its compile-time allocated index in the
|
---|
1826 | * procedure's array of local variables.
|
---|
1827 | *
|
---|
1828 | * Results:
|
---|
1829 | * Returns a pointer to the Tcl_Obj holding the new value of the
|
---|
1830 | * variable given by localIndex. If the specified variable doesn't
|
---|
1831 | * exist, or there is a clash in array usage, or an error occurs while
|
---|
1832 | * executing variable traces, then NULL is returned and a message will
|
---|
1833 | * be left in the interpreter's result.
|
---|
1834 | *
|
---|
1835 | * Side effects:
|
---|
1836 | * The value of the given variable is incremented by the specified
|
---|
1837 | * amount. The ref count for the returned object is _not_ incremented
|
---|
1838 | * to reflect the returned reference; if you want to keep a reference
|
---|
1839 | * to the object you must increment its ref count yourself.
|
---|
1840 | *
|
---|
1841 | *----------------------------------------------------------------------
|
---|
1842 | */
|
---|
1843 |
|
---|
1844 | Tcl_Obj *
|
---|
1845 | TclIncrIndexedScalar(interp, localIndex, incrAmount)
|
---|
1846 | Tcl_Interp *interp; /* Command interpreter in which variable is
|
---|
1847 | * to be found. */
|
---|
1848 | int localIndex; /* Index of variable in procedure's array
|
---|
1849 | * of local variables. */
|
---|
1850 | long incrAmount; /* Amount to be added to variable. */
|
---|
1851 | {
|
---|
1852 | register Tcl_Obj *varValuePtr;
|
---|
1853 | Tcl_Obj *resultPtr;
|
---|
1854 | int createdNewObj; /* Set 1 if var's value object is shared
|
---|
1855 | * so we must increment a copy (i.e. copy
|
---|
1856 | * on write). */
|
---|
1857 | long i;
|
---|
1858 | int result;
|
---|
1859 |
|
---|
1860 | varValuePtr = TclGetIndexedScalar(interp, localIndex,
|
---|
1861 | /*leaveErrorMsg*/ 1);
|
---|
1862 | if (varValuePtr == NULL) {
|
---|
1863 | Tcl_AddObjErrorInfo(interp,
|
---|
1864 | "\n (reading value of variable to increment)", -1);
|
---|
1865 | return NULL;
|
---|
1866 | }
|
---|
1867 |
|
---|
1868 | /*
|
---|
1869 | * Reach into the object's representation to extract and increment the
|
---|
1870 | * variable's value. If the object is unshared we can modify it
|
---|
1871 | * directly, otherwise we must create a new copy to modify: this is
|
---|
1872 | * "copy on write". Then free the variable's old string representation,
|
---|
1873 | * if any, since it will no longer be valid.
|
---|
1874 | */
|
---|
1875 |
|
---|
1876 | createdNewObj = 0;
|
---|
1877 | if (Tcl_IsShared(varValuePtr)) {
|
---|
1878 | createdNewObj = 1;
|
---|
1879 | varValuePtr = Tcl_DuplicateObj(varValuePtr);
|
---|
1880 | }
|
---|
1881 | result = Tcl_GetLongFromObj(interp, varValuePtr, &i);
|
---|
1882 | if (result != TCL_OK) {
|
---|
1883 | if (createdNewObj) {
|
---|
1884 | Tcl_DecrRefCount(varValuePtr); /* free unneeded copy */
|
---|
1885 | }
|
---|
1886 | return NULL;
|
---|
1887 | }
|
---|
1888 | Tcl_SetLongObj(varValuePtr, (i + incrAmount));
|
---|
1889 |
|
---|
1890 | /*
|
---|
1891 | * Store the variable's new value and run any write traces.
|
---|
1892 | */
|
---|
1893 |
|
---|
1894 | resultPtr = TclSetIndexedScalar(interp, localIndex, varValuePtr,
|
---|
1895 | /*leaveErrorMsg*/ 1);
|
---|
1896 | if (resultPtr == NULL) {
|
---|
1897 | return NULL;
|
---|
1898 | }
|
---|
1899 | return resultPtr;
|
---|
1900 | }
|
---|
1901 | |
---|
1902 |
|
---|
1903 | /*
|
---|
1904 | *----------------------------------------------------------------------
|
---|
1905 | *
|
---|
1906 | * TclIncrElementOfIndexedArray --
|
---|
1907 | *
|
---|
1908 | * Increments the Tcl object value of an element in a local array
|
---|
1909 | * variable. The element is named by the object elemPtr while the array
|
---|
1910 | * is specified by its index in the active procedure's array of
|
---|
1911 | * compiler allocated local variables.
|
---|
1912 | *
|
---|
1913 | * Results:
|
---|
1914 | * Returns a pointer to the Tcl_Obj holding the new value of the
|
---|
1915 | * element. If the specified array or element doesn't exist, or there
|
---|
1916 | * is a clash in array usage, or an error occurs while executing
|
---|
1917 | * variable traces, then NULL is returned and a message will be left in
|
---|
1918 | * the interpreter's result.
|
---|
1919 | *
|
---|
1920 | * Side effects:
|
---|
1921 | * The value of the given array element is incremented by the specified
|
---|
1922 | * amount. The ref count for the returned object is _not_ incremented
|
---|
1923 | * to reflect the returned reference; if you want to keep a reference
|
---|
1924 | * to the object you must increment its ref count yourself. If the
|
---|
1925 | * entry doesn't exist then a new variable is created.
|
---|
1926 | *
|
---|
1927 | *----------------------------------------------------------------------
|
---|
1928 | */
|
---|
1929 |
|
---|
1930 | Tcl_Obj *
|
---|
1931 | TclIncrElementOfIndexedArray(interp, localIndex, elemPtr, incrAmount)
|
---|
1932 | Tcl_Interp *interp; /* Command interpreter in which the array is
|
---|
1933 | * to be found. */
|
---|
1934 | int localIndex; /* Index of array variable in procedure's
|
---|
1935 | * array of local variables. */
|
---|
1936 | Tcl_Obj *elemPtr; /* Points to an object holding the name of
|
---|
1937 | * an element to increment in the array. */
|
---|
1938 | long incrAmount; /* Amount to be added to variable. */
|
---|
1939 | {
|
---|
1940 | register Tcl_Obj *varValuePtr;
|
---|
1941 | Tcl_Obj *resultPtr;
|
---|
1942 | int createdNewObj; /* Set 1 if var's value object is shared
|
---|
1943 | * so we must increment a copy (i.e. copy
|
---|
1944 | * on write). */
|
---|
1945 | long i;
|
---|
1946 | int result;
|
---|
1947 |
|
---|
1948 | varValuePtr = TclGetElementOfIndexedArray(interp, localIndex, elemPtr,
|
---|
1949 | /*leaveErrorMsg*/ 1);
|
---|
1950 | if (varValuePtr == NULL) {
|
---|
1951 | Tcl_AddObjErrorInfo(interp,
|
---|
1952 | "\n (reading value of variable to increment)", -1);
|
---|
1953 | return NULL;
|
---|
1954 | }
|
---|
1955 |
|
---|
1956 | /*
|
---|
1957 | * Reach into the object's representation to extract and increment the
|
---|
1958 | * variable's value. If the object is unshared we can modify it
|
---|
1959 | * directly, otherwise we must create a new copy to modify: this is
|
---|
1960 | * "copy on write". Then free the variable's old string representation,
|
---|
1961 | * if any, since it will no longer be valid.
|
---|
1962 | */
|
---|
1963 |
|
---|
1964 | createdNewObj = 0;
|
---|
1965 | if (Tcl_IsShared(varValuePtr)) {
|
---|
1966 | createdNewObj = 1;
|
---|
1967 | varValuePtr = Tcl_DuplicateObj(varValuePtr);
|
---|
1968 | }
|
---|
1969 | result = Tcl_GetLongFromObj(interp, varValuePtr, &i);
|
---|
1970 | if (result != TCL_OK) {
|
---|
1971 | if (createdNewObj) {
|
---|
1972 | Tcl_DecrRefCount(varValuePtr); /* free unneeded copy */
|
---|
1973 | }
|
---|
1974 | return NULL;
|
---|
1975 | }
|
---|
1976 | Tcl_SetLongObj(varValuePtr, (i + incrAmount));
|
---|
1977 |
|
---|
1978 | /*
|
---|
1979 | * Store the variable's new value and run any write traces.
|
---|
1980 | */
|
---|
1981 |
|
---|
1982 | resultPtr = TclSetElementOfIndexedArray(interp, localIndex, elemPtr,
|
---|
1983 | varValuePtr,
|
---|
1984 | /*leaveErrorMsg*/ 1);
|
---|
1985 | if (resultPtr == NULL) {
|
---|
1986 | return NULL;
|
---|
1987 | }
|
---|
1988 | return resultPtr;
|
---|
1989 | }
|
---|
1990 | |
---|
1991 |
|
---|
1992 | /*
|
---|
1993 | *----------------------------------------------------------------------
|
---|
1994 | *
|
---|
1995 | * Tcl_UnsetVar --
|
---|
1996 | *
|
---|
1997 | * Delete a variable, so that it may not be accessed anymore.
|
---|
1998 | *
|
---|
1999 | * Results:
|
---|
2000 | * Returns TCL_OK if the variable was successfully deleted, TCL_ERROR
|
---|
2001 | * if the variable can't be unset. In the event of an error,
|
---|
2002 | * if the TCL_LEAVE_ERR_MSG flag is set then an error message
|
---|
2003 | * is left in interp->result.
|
---|
2004 | *
|
---|
2005 | * Side effects:
|
---|
2006 | * If varName is defined as a local or global variable in interp,
|
---|
2007 | * it is deleted.
|
---|
2008 | *
|
---|
2009 | *----------------------------------------------------------------------
|
---|
2010 | */
|
---|
2011 |
|
---|
2012 | int
|
---|
2013 | Tcl_UnsetVar(interp, varName, flags)
|
---|
2014 | Tcl_Interp *interp; /* Command interpreter in which varName is
|
---|
2015 | * to be looked up. */
|
---|
2016 | char *varName; /* Name of a variable in interp. May be
|
---|
2017 | * either a scalar name or an array name
|
---|
2018 | * or an element in an array. */
|
---|
2019 | int flags; /* OR-ed combination of any of
|
---|
2020 | * TCL_GLOBAL_ONLY, TCL_NAMESPACE_ONLY or
|
---|
2021 | * TCL_LEAVE_ERR_MSG. */
|
---|
2022 | {
|
---|
2023 | return Tcl_UnsetVar2(interp, varName, (char *) NULL,
|
---|
2024 | (flags | TCL_PARSE_PART1));
|
---|
2025 | }
|
---|
2026 | |
---|
2027 |
|
---|
2028 | /*
|
---|
2029 | *----------------------------------------------------------------------
|
---|
2030 | *
|
---|
2031 | * Tcl_UnsetVar2 --
|
---|
2032 | *
|
---|
2033 | * Delete a variable, given a 2-part name.
|
---|
2034 | *
|
---|
2035 | * Results:
|
---|
2036 | * Returns TCL_OK if the variable was successfully deleted, TCL_ERROR
|
---|
2037 | * if the variable can't be unset. In the event of an error,
|
---|
2038 | * if the TCL_LEAVE_ERR_MSG flag is set then an error message
|
---|
2039 | * is left in interp->result.
|
---|
2040 | *
|
---|
2041 | * Side effects:
|
---|
2042 | * If part1 and part2 indicate a local or global variable in interp,
|
---|
2043 | * it is deleted. If part1 is an array name and part2 is NULL, then
|
---|
2044 | * the whole array is deleted.
|
---|
2045 | *
|
---|
2046 | *----------------------------------------------------------------------
|
---|
2047 | */
|
---|
2048 |
|
---|
2049 | int
|
---|
2050 | Tcl_UnsetVar2(interp, part1, part2, flags)
|
---|
2051 | Tcl_Interp *interp; /* Command interpreter in which varName is
|
---|
2052 | * to be looked up. */
|
---|
2053 | char *part1; /* Name of variable or array. */
|
---|
2054 | char *part2; /* Name of element within array or NULL. */
|
---|
2055 | int flags; /* OR-ed combination of any of
|
---|
2056 | * TCL_GLOBAL_ONLY, TCL_NAMESPACE_ONLY,
|
---|
2057 | * TCL_LEAVE_ERR_MSG, or
|
---|
2058 | * TCL_PARSE_PART1. */
|
---|
2059 | {
|
---|
2060 | Var dummyVar;
|
---|
2061 | Var *varPtr, *dummyVarPtr;
|
---|
2062 | Interp *iPtr = (Interp *) interp;
|
---|
2063 | Var *arrayPtr;
|
---|
2064 | ActiveVarTrace *activePtr;
|
---|
2065 | Tcl_Obj *objPtr;
|
---|
2066 | int result;
|
---|
2067 |
|
---|
2068 | varPtr = TclLookupVar(interp, part1, part2, flags, "unset",
|
---|
2069 | /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr);
|
---|
2070 | if (varPtr == NULL) {
|
---|
2071 | return TCL_ERROR;
|
---|
2072 | }
|
---|
2073 | result = (TclIsVarUndefined(varPtr)? TCL_ERROR : TCL_OK);
|
---|
2074 |
|
---|
2075 | if ((arrayPtr != NULL) && (arrayPtr->searchPtr != NULL)) {
|
---|
2076 | DeleteSearches(arrayPtr);
|
---|
2077 | }
|
---|
2078 |
|
---|
2079 | /*
|
---|
2080 | * The code below is tricky, because of the possibility that
|
---|
2081 | * a trace procedure might try to access a variable being
|
---|
2082 | * deleted. To handle this situation gracefully, do things
|
---|
2083 | * in three steps:
|
---|
2084 | * 1. Copy the contents of the variable to a dummy variable
|
---|
2085 | * structure, and mark the original Var structure as undefined.
|
---|
2086 | * 2. Invoke traces and clean up the variable, using the dummy copy.
|
---|
2087 | * 3. If at the end of this the original variable is still
|
---|
2088 | * undefined and has no outstanding references, then delete
|
---|
2089 | * it (but it could have gotten recreated by a trace).
|
---|
2090 | */
|
---|
2091 |
|
---|
2092 | dummyVar = *varPtr;
|
---|
2093 | TclSetVarUndefined(varPtr);
|
---|
2094 | TclSetVarScalar(varPtr);
|
---|
2095 | varPtr->value.objPtr = NULL; /* dummyVar points to any value object */
|
---|
2096 | varPtr->tracePtr = NULL;
|
---|
2097 | varPtr->searchPtr = NULL;
|
---|
2098 |
|
---|
2099 | /*
|
---|
2100 | * Call trace procedures for the variable being deleted. Then delete
|
---|
2101 | * its traces. Be sure to abort any other traces for the variable
|
---|
2102 | * that are still pending. Special tricks:
|
---|
2103 | * 1. We need to increment varPtr's refCount around this: CallTraces
|
---|
2104 | * will use dummyVar so it won't increment varPtr's refCount itself.
|
---|
2105 | * 2. Turn off the VAR_TRACE_ACTIVE flag in dummyVar: we want to
|
---|
2106 | * call unset traces even if other traces are pending.
|
---|
2107 | */
|
---|
2108 |
|
---|
2109 | if ((dummyVar.tracePtr != NULL)
|
---|
2110 | || ((arrayPtr != NULL) && (arrayPtr->tracePtr != NULL))) {
|
---|
2111 | varPtr->refCount++;
|
---|
2112 | dummyVar.flags &= ~VAR_TRACE_ACTIVE;
|
---|
2113 | (void) CallTraces(iPtr, arrayPtr, &dummyVar, part1, part2,
|
---|
2114 | (flags & (TCL_GLOBAL_ONLY|TCL_NAMESPACE_ONLY|TCL_PARSE_PART1)) | TCL_TRACE_UNSETS);
|
---|
2115 | while (dummyVar.tracePtr != NULL) {
|
---|
2116 | VarTrace *tracePtr = dummyVar.tracePtr;
|
---|
2117 | dummyVar.tracePtr = tracePtr->nextPtr;
|
---|
2118 | ckfree((char *) tracePtr);
|
---|
2119 | }
|
---|
2120 | for (activePtr = iPtr->activeTracePtr; activePtr != NULL;
|
---|
2121 | activePtr = activePtr->nextPtr) {
|
---|
2122 | if (activePtr->varPtr == varPtr) {
|
---|
2123 | activePtr->nextTracePtr = NULL;
|
---|
2124 | }
|
---|
2125 | }
|
---|
2126 | varPtr->refCount--;
|
---|
2127 | }
|
---|
2128 |
|
---|
2129 | /*
|
---|
2130 | * If the variable is an array, delete all of its elements. This must be
|
---|
2131 | * done after calling the traces on the array, above (that's the way
|
---|
2132 | * traces are defined). If it is a scalar, "discard" its object
|
---|
2133 | * (decrement the ref count of its object, if any).
|
---|
2134 | */
|
---|
2135 |
|
---|
2136 | dummyVarPtr = &dummyVar;
|
---|
2137 | if (TclIsVarArray(dummyVarPtr) && !TclIsVarUndefined(dummyVarPtr)) {
|
---|
2138 | DeleteArray(iPtr, part1, dummyVarPtr,
|
---|
2139 | (flags & (TCL_GLOBAL_ONLY|TCL_NAMESPACE_ONLY)) | TCL_TRACE_UNSETS);
|
---|
2140 | }
|
---|
2141 | if (TclIsVarScalar(dummyVarPtr)
|
---|
2142 | && (dummyVarPtr->value.objPtr != NULL)) {
|
---|
2143 | objPtr = dummyVarPtr->value.objPtr;
|
---|
2144 | TclDecrRefCount(objPtr);
|
---|
2145 | dummyVarPtr->value.objPtr = NULL;
|
---|
2146 | }
|
---|
2147 |
|
---|
2148 | /*
|
---|
2149 | * If the variable was a namespace variable, decrement its reference count.
|
---|
2150 | */
|
---|
2151 |
|
---|
2152 | if (varPtr->flags & VAR_NAMESPACE_VAR) {
|
---|
2153 | varPtr->flags &= ~VAR_NAMESPACE_VAR;
|
---|
2154 | varPtr->refCount--;
|
---|
2155 | }
|
---|
2156 |
|
---|
2157 | /*
|
---|
2158 | * It's an error to unset an undefined variable.
|
---|
2159 | */
|
---|
2160 |
|
---|
2161 | if (result != TCL_OK) {
|
---|
2162 | if (flags & TCL_LEAVE_ERR_MSG) {
|
---|
2163 | VarErrMsg(interp, part1, part2, "unset",
|
---|
2164 | ((arrayPtr == NULL) ? noSuchVar : noSuchElement));
|
---|
2165 | }
|
---|
2166 | }
|
---|
2167 |
|
---|
2168 | /*
|
---|
2169 | * Finally, if the variable is truly not in use then free up its Var
|
---|
2170 | * structure and remove it from its hash table, if any. The ref count of
|
---|
2171 | * its value object, if any, was decremented above.
|
---|
2172 | */
|
---|
2173 |
|
---|
2174 | CleanupVar(varPtr, arrayPtr);
|
---|
2175 | return result;
|
---|
2176 | }
|
---|
2177 | |
---|
2178 |
|
---|
2179 | /*
|
---|
2180 | *----------------------------------------------------------------------
|
---|
2181 | *
|
---|
2182 | * Tcl_TraceVar --
|
---|
2183 | *
|
---|
2184 | * Arrange for reads and/or writes to a variable to cause a
|
---|
2185 | * procedure to be invoked, which can monitor the operations
|
---|
2186 | * and/or change their actions.
|
---|
2187 | *
|
---|
2188 | * Results:
|
---|
2189 | * A standard Tcl return value.
|
---|
2190 | *
|
---|
2191 | * Side effects:
|
---|
2192 | * A trace is set up on the variable given by varName, such that
|
---|
2193 | * future references to the variable will be intermediated by
|
---|
2194 | * proc. See the manual entry for complete details on the calling
|
---|
2195 | * sequence for proc.
|
---|
2196 | *
|
---|
2197 | *----------------------------------------------------------------------
|
---|
2198 | */
|
---|
2199 |
|
---|
2200 | int
|
---|
2201 | Tcl_TraceVar(interp, varName, flags, proc, clientData)
|
---|
2202 | Tcl_Interp *interp; /* Interpreter in which variable is
|
---|
2203 | * to be traced. */
|
---|
2204 | char *varName; /* Name of variable; may end with "(index)"
|
---|
2205 | * to signify an array reference. */
|
---|
2206 | int flags; /* OR-ed collection of bits, including any
|
---|
2207 | * of TCL_TRACE_READS, TCL_TRACE_WRITES,
|
---|
2208 | * TCL_TRACE_UNSETS, TCL_GLOBAL_ONLY, and
|
---|
2209 | * TCL_NAMESPACE_ONLY. */
|
---|
2210 | Tcl_VarTraceProc *proc; /* Procedure to call when specified ops are
|
---|
2211 | * invoked upon varName. */
|
---|
2212 | ClientData clientData; /* Arbitrary argument to pass to proc. */
|
---|
2213 | {
|
---|
2214 | return Tcl_TraceVar2(interp, varName, (char *) NULL,
|
---|
2215 | (flags | TCL_PARSE_PART1), proc, clientData);
|
---|
2216 | }
|
---|
2217 | |
---|
2218 |
|
---|
2219 | /*
|
---|
2220 | *----------------------------------------------------------------------
|
---|
2221 | *
|
---|
2222 | * Tcl_TraceVar2 --
|
---|
2223 | *
|
---|
2224 | * Arrange for reads and/or writes to a variable to cause a
|
---|
2225 | * procedure to be invoked, which can monitor the operations
|
---|
2226 | * and/or change their actions.
|
---|
2227 | *
|
---|
2228 | * Results:
|
---|
2229 | * A standard Tcl return value.
|
---|
2230 | *
|
---|
2231 | * Side effects:
|
---|
2232 | * A trace is set up on the variable given by part1 and part2, such
|
---|
2233 | * that future references to the variable will be intermediated by
|
---|
2234 | * proc. See the manual entry for complete details on the calling
|
---|
2235 | * sequence for proc.
|
---|
2236 | *
|
---|
2237 | *----------------------------------------------------------------------
|
---|
2238 | */
|
---|
2239 |
|
---|
2240 | int
|
---|
2241 | Tcl_TraceVar2(interp, part1, part2, flags, proc, clientData)
|
---|
2242 | Tcl_Interp *interp; /* Interpreter in which variable is
|
---|
2243 | * to be traced. */
|
---|
2244 | char *part1; /* Name of scalar variable or array. */
|
---|
2245 | char *part2; /* Name of element within array; NULL means
|
---|
2246 | * trace applies to scalar variable or array
|
---|
2247 | * as-a-whole. */
|
---|
2248 | int flags; /* OR-ed collection of bits, including any
|
---|
2249 | * of TCL_TRACE_READS, TCL_TRACE_WRITES,
|
---|
2250 | * TCL_TRACE_UNSETS, TCL_GLOBAL_ONLY,
|
---|
2251 | * TCL_NAMESPACE_ONLY and
|
---|
2252 | * TCL_PARSE_PART1. */
|
---|
2253 | Tcl_VarTraceProc *proc; /* Procedure to call when specified ops are
|
---|
2254 | * invoked upon varName. */
|
---|
2255 | ClientData clientData; /* Arbitrary argument to pass to proc. */
|
---|
2256 | {
|
---|
2257 | Var *varPtr, *arrayPtr;
|
---|
2258 | register VarTrace *tracePtr;
|
---|
2259 |
|
---|
2260 | varPtr = TclLookupVar(interp, part1, part2, (flags | TCL_LEAVE_ERR_MSG),
|
---|
2261 | "trace", /*createPart1*/ 1, /*createPart2*/ 1, &arrayPtr);
|
---|
2262 | if (varPtr == NULL) {
|
---|
2263 | return TCL_ERROR;
|
---|
2264 | }
|
---|
2265 |
|
---|
2266 | /*
|
---|
2267 | * Set up trace information.
|
---|
2268 | */
|
---|
2269 |
|
---|
2270 | tracePtr = (VarTrace *) ckalloc(sizeof(VarTrace));
|
---|
2271 | tracePtr->traceProc = proc;
|
---|
2272 | tracePtr->clientData = clientData;
|
---|
2273 | tracePtr->flags =
|
---|
2274 | flags & (TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS);
|
---|
2275 | tracePtr->nextPtr = varPtr->tracePtr;
|
---|
2276 | varPtr->tracePtr = tracePtr;
|
---|
2277 | return TCL_OK;
|
---|
2278 | }
|
---|
2279 | |
---|
2280 |
|
---|
2281 | /*
|
---|
2282 | *----------------------------------------------------------------------
|
---|
2283 | *
|
---|
2284 | * Tcl_UntraceVar --
|
---|
2285 | *
|
---|
2286 | * Remove a previously-created trace for a variable.
|
---|
2287 | *
|
---|
2288 | * Results:
|
---|
2289 | * None.
|
---|
2290 | *
|
---|
2291 | * Side effects:
|
---|
2292 | * If there exists a trace for the variable given by varName
|
---|
2293 | * with the given flags, proc, and clientData, then that trace
|
---|
2294 | * is removed.
|
---|
2295 | *
|
---|
2296 | *----------------------------------------------------------------------
|
---|
2297 | */
|
---|
2298 |
|
---|
2299 | void
|
---|
2300 | Tcl_UntraceVar(interp, varName, flags, proc, clientData)
|
---|
2301 | Tcl_Interp *interp; /* Interpreter containing variable. */
|
---|
2302 | char *varName; /* Name of variable; may end with "(index)"
|
---|
2303 | * to signify an array reference. */
|
---|
2304 | int flags; /* OR-ed collection of bits describing
|
---|
2305 | * current trace, including any of
|
---|
2306 | * TCL_TRACE_READS, TCL_TRACE_WRITES,
|
---|
2307 | * TCL_TRACE_UNSETS, TCL_GLOBAL_ONLY
|
---|
2308 | * and TCL_NAMESPACE_ONLY. */
|
---|
2309 | Tcl_VarTraceProc *proc; /* Procedure assocated with trace. */
|
---|
2310 | ClientData clientData; /* Arbitrary argument to pass to proc. */
|
---|
2311 | {
|
---|
2312 | Tcl_UntraceVar2(interp, varName, (char *) NULL,
|
---|
2313 | (flags | TCL_PARSE_PART1), proc, clientData);
|
---|
2314 | }
|
---|
2315 | |
---|
2316 |
|
---|
2317 | /*
|
---|
2318 | *----------------------------------------------------------------------
|
---|
2319 | *
|
---|
2320 | * Tcl_UntraceVar2 --
|
---|
2321 | *
|
---|
2322 | * Remove a previously-created trace for a variable.
|
---|
2323 | *
|
---|
2324 | * Results:
|
---|
2325 | * None.
|
---|
2326 | *
|
---|
2327 | * Side effects:
|
---|
2328 | * If there exists a trace for the variable given by part1
|
---|
2329 | * and part2 with the given flags, proc, and clientData, then
|
---|
2330 | * that trace is removed.
|
---|
2331 | *
|
---|
2332 | *----------------------------------------------------------------------
|
---|
2333 | */
|
---|
2334 |
|
---|
2335 | void
|
---|
2336 | Tcl_UntraceVar2(interp, part1, part2, flags, proc, clientData)
|
---|
2337 | Tcl_Interp *interp; /* Interpreter containing variable. */
|
---|
2338 | char *part1; /* Name of variable or array. */
|
---|
2339 | char *part2; /* Name of element within array; NULL means
|
---|
2340 | * trace applies to scalar variable or array
|
---|
2341 | * as-a-whole. */
|
---|
2342 | int flags; /* OR-ed collection of bits describing
|
---|
2343 | * current trace, including any of
|
---|
2344 | * TCL_TRACE_READS, TCL_TRACE_WRITES,
|
---|
2345 | * TCL_TRACE_UNSETS, TCL_GLOBAL_ONLY,
|
---|
2346 | * TCL_NAMESPACE_ONLY and
|
---|
2347 | * TCL_PARSE_PART1. */
|
---|
2348 | Tcl_VarTraceProc *proc; /* Procedure assocated with trace. */
|
---|
2349 | ClientData clientData; /* Arbitrary argument to pass to proc. */
|
---|
2350 | {
|
---|
2351 | register VarTrace *tracePtr;
|
---|
2352 | VarTrace *prevPtr;
|
---|
2353 | Var *varPtr, *arrayPtr;
|
---|
2354 | Interp *iPtr = (Interp *) interp;
|
---|
2355 | ActiveVarTrace *activePtr;
|
---|
2356 |
|
---|
2357 | varPtr = TclLookupVar(interp, part1, part2,
|
---|
2358 | flags & (TCL_GLOBAL_ONLY|TCL_NAMESPACE_ONLY|TCL_PARSE_PART1),
|
---|
2359 | /*msg*/ (char *) NULL,
|
---|
2360 | /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr);
|
---|
2361 | if (varPtr == NULL) {
|
---|
2362 | return;
|
---|
2363 | }
|
---|
2364 |
|
---|
2365 | flags &= (TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS);
|
---|
2366 | for (tracePtr = varPtr->tracePtr, prevPtr = NULL; ;
|
---|
2367 | prevPtr = tracePtr, tracePtr = tracePtr->nextPtr) {
|
---|
2368 | if (tracePtr == NULL) {
|
---|
2369 | return;
|
---|
2370 | }
|
---|
2371 | if ((tracePtr->traceProc == proc) && (tracePtr->flags == flags)
|
---|
2372 | && (tracePtr->clientData == clientData)) {
|
---|
2373 | break;
|
---|
2374 | }
|
---|
2375 | }
|
---|
2376 |
|
---|
2377 | /*
|
---|
2378 | * The code below makes it possible to delete traces while traces
|
---|
2379 | * are active: it makes sure that the deleted trace won't be
|
---|
2380 | * processed by CallTraces.
|
---|
2381 | */
|
---|
2382 |
|
---|
2383 | for (activePtr = iPtr->activeTracePtr; activePtr != NULL;
|
---|
2384 | activePtr = activePtr->nextPtr) {
|
---|
2385 | if (activePtr->nextTracePtr == tracePtr) {
|
---|
2386 | activePtr->nextTracePtr = tracePtr->nextPtr;
|
---|
2387 | }
|
---|
2388 | }
|
---|
2389 | if (prevPtr == NULL) {
|
---|
2390 | varPtr->tracePtr = tracePtr->nextPtr;
|
---|
2391 | } else {
|
---|
2392 | prevPtr->nextPtr = tracePtr->nextPtr;
|
---|
2393 | }
|
---|
2394 | ckfree((char *) tracePtr);
|
---|
2395 |
|
---|
2396 | /*
|
---|
2397 | * If this is the last trace on the variable, and the variable is
|
---|
2398 | * unset and unused, then free up the variable.
|
---|
2399 | */
|
---|
2400 |
|
---|
2401 | if (TclIsVarUndefined(varPtr)) {
|
---|
2402 | CleanupVar(varPtr, (Var *) NULL);
|
---|
2403 | }
|
---|
2404 | }
|
---|
2405 | |
---|
2406 |
|
---|
2407 | /*
|
---|
2408 | *----------------------------------------------------------------------
|
---|
2409 | *
|
---|
2410 | * Tcl_VarTraceInfo --
|
---|
2411 | *
|
---|
2412 | * Return the clientData value associated with a trace on a
|
---|
2413 | * variable. This procedure can also be used to step through
|
---|
2414 | * all of the traces on a particular variable that have the
|
---|
2415 | * same trace procedure.
|
---|
2416 | *
|
---|
2417 | * Results:
|
---|
2418 | * The return value is the clientData value associated with
|
---|
2419 | * a trace on the given variable. Information will only be
|
---|
2420 | * returned for a trace with proc as trace procedure. If
|
---|
2421 | * the clientData argument is NULL then the first such trace is
|
---|
2422 | * returned; otherwise, the next relevant one after the one
|
---|
2423 | * given by clientData will be returned. If the variable
|
---|
2424 | * doesn't exist, or if there are no (more) traces for it,
|
---|
2425 | * then NULL is returned.
|
---|
2426 | *
|
---|
2427 | * Side effects:
|
---|
2428 | * None.
|
---|
2429 | *
|
---|
2430 | *----------------------------------------------------------------------
|
---|
2431 | */
|
---|
2432 |
|
---|
2433 | ClientData
|
---|
2434 | Tcl_VarTraceInfo(interp, varName, flags, proc, prevClientData)
|
---|
2435 | Tcl_Interp *interp; /* Interpreter containing variable. */
|
---|
2436 | char *varName; /* Name of variable; may end with "(index)"
|
---|
2437 | * to signify an array reference. */
|
---|
2438 | int flags; /* 0, TCL_GLOBAL_ONLY, or
|
---|
2439 | * TCL_NAMESPACE_ONLY. */
|
---|
2440 | Tcl_VarTraceProc *proc; /* Procedure assocated with trace. */
|
---|
2441 | ClientData prevClientData; /* If non-NULL, gives last value returned
|
---|
2442 | * by this procedure, so this call will
|
---|
2443 | * return the next trace after that one.
|
---|
2444 | * If NULL, this call will return the
|
---|
2445 | * first trace. */
|
---|
2446 | {
|
---|
2447 | return Tcl_VarTraceInfo2(interp, varName, (char *) NULL,
|
---|
2448 | (flags | TCL_PARSE_PART1), proc, prevClientData);
|
---|
2449 | }
|
---|
2450 | |
---|
2451 |
|
---|
2452 | /*
|
---|
2453 | *----------------------------------------------------------------------
|
---|
2454 | *
|
---|
2455 | * Tcl_VarTraceInfo2 --
|
---|
2456 | *
|
---|
2457 | * Same as Tcl_VarTraceInfo, except takes name in two pieces
|
---|
2458 | * instead of one.
|
---|
2459 | *
|
---|
2460 | * Results:
|
---|
2461 | * Same as Tcl_VarTraceInfo.
|
---|
2462 | *
|
---|
2463 | * Side effects:
|
---|
2464 | * None.
|
---|
2465 | *
|
---|
2466 | *----------------------------------------------------------------------
|
---|
2467 | */
|
---|
2468 |
|
---|
2469 | ClientData
|
---|
2470 | Tcl_VarTraceInfo2(interp, part1, part2, flags, proc, prevClientData)
|
---|
2471 | Tcl_Interp *interp; /* Interpreter containing variable. */
|
---|
2472 | char *part1; /* Name of variable or array. */
|
---|
2473 | char *part2; /* Name of element within array; NULL means
|
---|
2474 | * trace applies to scalar variable or array
|
---|
2475 | * as-a-whole. */
|
---|
2476 | int flags; /* OR-ed combination of TCL_GLOBAL_ONLY,
|
---|
2477 | * TCL_NAMESPACE_ONLY, and
|
---|
2478 | * TCL_PARSE_PART1. */
|
---|
2479 | Tcl_VarTraceProc *proc; /* Procedure assocated with trace. */
|
---|
2480 | ClientData prevClientData; /* If non-NULL, gives last value returned
|
---|
2481 | * by this procedure, so this call will
|
---|
2482 | * return the next trace after that one.
|
---|
2483 | * If NULL, this call will return the
|
---|
2484 | * first trace. */
|
---|
2485 | {
|
---|
2486 | register VarTrace *tracePtr;
|
---|
2487 | Var *varPtr, *arrayPtr;
|
---|
2488 |
|
---|
2489 | varPtr = TclLookupVar(interp, part1, part2,
|
---|
2490 | flags & (TCL_GLOBAL_ONLY|TCL_NAMESPACE_ONLY|TCL_PARSE_PART1),
|
---|
2491 | /*msg*/ (char *) NULL,
|
---|
2492 | /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr);
|
---|
2493 | if (varPtr == NULL) {
|
---|
2494 | return NULL;
|
---|
2495 | }
|
---|
2496 |
|
---|
2497 | /*
|
---|
2498 | * Find the relevant trace, if any, and return its clientData.
|
---|
2499 | */
|
---|
2500 |
|
---|
2501 | tracePtr = varPtr->tracePtr;
|
---|
2502 | if (prevClientData != NULL) {
|
---|
2503 | for ( ; tracePtr != NULL; tracePtr = tracePtr->nextPtr) {
|
---|
2504 | if ((tracePtr->clientData == prevClientData)
|
---|
2505 | && (tracePtr->traceProc == proc)) {
|
---|
2506 | tracePtr = tracePtr->nextPtr;
|
---|
2507 | break;
|
---|
2508 | }
|
---|
2509 | }
|
---|
2510 | }
|
---|
2511 | for ( ; tracePtr != NULL; tracePtr = tracePtr->nextPtr) {
|
---|
2512 | if (tracePtr->traceProc == proc) {
|
---|
2513 | return tracePtr->clientData;
|
---|
2514 | }
|
---|
2515 | }
|
---|
2516 | return NULL;
|
---|
2517 | }
|
---|
2518 | |
---|
2519 |
|
---|
2520 | /*
|
---|
2521 | *----------------------------------------------------------------------
|
---|
2522 | *
|
---|
2523 | * Tcl_UnsetObjCmd --
|
---|
2524 | *
|
---|
2525 | * This object-based procedure is invoked to process the "unset" Tcl
|
---|
2526 | * command. See the user documentation for details on what it does.
|
---|
2527 | *
|
---|
2528 | * Results:
|
---|
2529 | * A standard Tcl object result value.
|
---|
2530 | *
|
---|
2531 | * Side effects:
|
---|
2532 | * See the user documentation.
|
---|
2533 | *
|
---|
2534 | *----------------------------------------------------------------------
|
---|
2535 | */
|
---|
2536 |
|
---|
2537 | /* ARGSUSED */
|
---|
2538 | int
|
---|
2539 | Tcl_UnsetObjCmd(dummy, interp, objc, objv)
|
---|
2540 | ClientData dummy; /* Not used. */
|
---|
2541 | Tcl_Interp *interp; /* Current interpreter. */
|
---|
2542 | int objc; /* Number of arguments. */
|
---|
2543 | Tcl_Obj *CONST objv[]; /* Argument objects. */
|
---|
2544 | {
|
---|
2545 | register int i;
|
---|
2546 | register char *name;
|
---|
2547 |
|
---|
2548 | if (objc < 2) {
|
---|
2549 | Tcl_WrongNumArgs(interp, 1, objv, "varName ?varName ...?");
|
---|
2550 | return TCL_ERROR;
|
---|
2551 | }
|
---|
2552 |
|
---|
2553 | for (i = 1; i < objc; i++) {
|
---|
2554 | /*
|
---|
2555 | * THIS FAILS IF A NAME OBJECT'S STRING REP HAS A NULL BYTE.
|
---|
2556 | */
|
---|
2557 |
|
---|
2558 | name = Tcl_GetStringFromObj(objv[i], (int *) NULL);
|
---|
2559 | if (Tcl_UnsetVar2(interp, name, (char *) NULL,
|
---|
2560 | (TCL_LEAVE_ERR_MSG | TCL_PARSE_PART1)) != TCL_OK) {
|
---|
2561 | return TCL_ERROR;
|
---|
2562 | }
|
---|
2563 | }
|
---|
2564 | return TCL_OK;
|
---|
2565 | }
|
---|
2566 | |
---|
2567 |
|
---|
2568 | /*
|
---|
2569 | *----------------------------------------------------------------------
|
---|
2570 | *
|
---|
2571 | * Tcl_AppendObjCmd --
|
---|
2572 | *
|
---|
2573 | * This object-based procedure is invoked to process the "append"
|
---|
2574 | * Tcl command. See the user documentation for details on what it does.
|
---|
2575 | *
|
---|
2576 | * Results:
|
---|
2577 | * A standard Tcl object result value.
|
---|
2578 | *
|
---|
2579 | * Side effects:
|
---|
2580 | * A variable's value may be changed.
|
---|
2581 | *
|
---|
2582 | *----------------------------------------------------------------------
|
---|
2583 | */
|
---|
2584 |
|
---|
2585 | /* ARGSUSED */
|
---|
2586 | int
|
---|
2587 | Tcl_AppendObjCmd(dummy, interp, objc, objv)
|
---|
2588 | ClientData dummy; /* Not used. */
|
---|
2589 | Tcl_Interp *interp; /* Current interpreter. */
|
---|
2590 | int objc; /* Number of arguments. */
|
---|
2591 | Tcl_Obj *CONST objv[]; /* Argument objects. */
|
---|
2592 | {
|
---|
2593 | register Tcl_Obj *varValuePtr = NULL;
|
---|
2594 | /* Initialized to avoid compiler
|
---|
2595 | * warning. */
|
---|
2596 | int i;
|
---|
2597 |
|
---|
2598 | if (objc < 2) {
|
---|
2599 | Tcl_WrongNumArgs(interp, 1, objv, "varName ?value value ...?");
|
---|
2600 | return TCL_ERROR;
|
---|
2601 | }
|
---|
2602 |
|
---|
2603 | if (objc == 2) {
|
---|
2604 | varValuePtr = Tcl_ObjGetVar2(interp, objv[1], (Tcl_Obj *) NULL,
|
---|
2605 | (TCL_LEAVE_ERR_MSG | TCL_PARSE_PART1));
|
---|
2606 | if (varValuePtr == NULL) {
|
---|
2607 | return TCL_ERROR;
|
---|
2608 | }
|
---|
2609 | } else {
|
---|
2610 | for (i = 2; i < objc; i++) {
|
---|
2611 | varValuePtr = Tcl_ObjSetVar2(interp, objv[1], (Tcl_Obj *) NULL,
|
---|
2612 | objv[i],
|
---|
2613 | (TCL_APPEND_VALUE | TCL_LEAVE_ERR_MSG | TCL_PARSE_PART1));
|
---|
2614 | if (varValuePtr == NULL) {
|
---|
2615 | return TCL_ERROR;
|
---|
2616 | }
|
---|
2617 | }
|
---|
2618 | }
|
---|
2619 |
|
---|
2620 | Tcl_SetObjResult(interp, varValuePtr);
|
---|
2621 | return TCL_OK;
|
---|
2622 | }
|
---|
2623 | |
---|
2624 |
|
---|
2625 | /*
|
---|
2626 | *----------------------------------------------------------------------
|
---|
2627 | *
|
---|
2628 | * Tcl_LappendObjCmd --
|
---|
2629 | *
|
---|
2630 | * This object-based procedure is invoked to process the "lappend"
|
---|
2631 | * Tcl command. See the user documentation for details on what it does.
|
---|
2632 | *
|
---|
2633 | * Results:
|
---|
2634 | * A standard Tcl object result value.
|
---|
2635 | *
|
---|
2636 | * Side effects:
|
---|
2637 | * A variable's value may be changed.
|
---|
2638 | *
|
---|
2639 | *----------------------------------------------------------------------
|
---|
2640 | */
|
---|
2641 |
|
---|
2642 | /* ARGSUSED */
|
---|
2643 | int
|
---|
2644 | Tcl_LappendObjCmd(dummy, interp, objc, objv)
|
---|
2645 | ClientData dummy; /* Not used. */
|
---|
2646 | Tcl_Interp *interp; /* Current interpreter. */
|
---|
2647 | int objc; /* Number of arguments. */
|
---|
2648 | Tcl_Obj *CONST objv[]; /* Argument objects. */
|
---|
2649 | {
|
---|
2650 | Tcl_Obj *varValuePtr, *newValuePtr;
|
---|
2651 | register List *listRepPtr;
|
---|
2652 | register Tcl_Obj **elemPtrs;
|
---|
2653 | int numElems, numRequired, createdNewObj, createVar, i, j;
|
---|
2654 |
|
---|
2655 | if (objc < 2) {
|
---|
2656 | Tcl_WrongNumArgs(interp, 1, objv, "varName ?value value ...?");
|
---|
2657 | return TCL_ERROR;
|
---|
2658 | }
|
---|
2659 |
|
---|
2660 | if (objc == 2) {
|
---|
2661 | newValuePtr = Tcl_ObjGetVar2(interp, objv[1], (Tcl_Obj *) NULL,
|
---|
2662 | (TCL_LEAVE_ERR_MSG | TCL_PARSE_PART1));
|
---|
2663 | if (newValuePtr == NULL) {
|
---|
2664 | /*
|
---|
2665 | * The variable doesn't exist yet. Just create it with an empty
|
---|
2666 | * initial value.
|
---|
2667 | */
|
---|
2668 |
|
---|
2669 | Tcl_Obj *nullObjPtr = Tcl_NewObj();
|
---|
2670 | newValuePtr = Tcl_ObjSetVar2(interp, objv[1], NULL,
|
---|
2671 | nullObjPtr, (TCL_LEAVE_ERR_MSG | TCL_PARSE_PART1));
|
---|
2672 | if (newValuePtr == NULL) {
|
---|
2673 | Tcl_DecrRefCount(nullObjPtr); /* free unneeded object */
|
---|
2674 | return TCL_ERROR;
|
---|
2675 | }
|
---|
2676 | }
|
---|
2677 | } else {
|
---|
2678 | /*
|
---|
2679 | * We have arguments to append. We used to call Tcl_ObjSetVar2 to
|
---|
2680 | * append each argument one at a time to ensure that traces were run
|
---|
2681 | * for each append step. We now append the arguments all at once
|
---|
2682 | * because it's faster. Note that a read trace and a write trace for
|
---|
2683 | * the variable will now each only be called once. Also, if the
|
---|
2684 | * variable's old value is unshared we modify it directly, otherwise
|
---|
2685 | * we create a new copy to modify: this is "copy on write".
|
---|
2686 | */
|
---|
2687 |
|
---|
2688 | createdNewObj = 0;
|
---|
2689 | createVar = 1;
|
---|
2690 | varValuePtr = Tcl_ObjGetVar2(interp, objv[1], (Tcl_Obj *) NULL,
|
---|
2691 | TCL_PARSE_PART1);
|
---|
2692 | if (varValuePtr == NULL) {
|
---|
2693 | /*
|
---|
2694 | * We couldn't read the old value: either the var doesn't yet
|
---|
2695 | * exist or it's an array element. If it's new, we will try to
|
---|
2696 | * create it with Tcl_ObjSetVar2 below.
|
---|
2697 | */
|
---|
2698 |
|
---|
2699 | char *name, *p;
|
---|
2700 | int nameBytes, i;
|
---|
2701 |
|
---|
2702 | name = TclGetStringFromObj(objv[1], &nameBytes);
|
---|
2703 | for (i = 0, p = name; i < nameBytes; i++, p++) {
|
---|
2704 | if (*p == '(') {
|
---|
2705 | p = (name + nameBytes-1);
|
---|
2706 | if (*p == ')') { /* last char is ')' => array ref */
|
---|
2707 | createVar = 0;
|
---|
2708 | }
|
---|
2709 | break;
|
---|
2710 | }
|
---|
2711 | }
|
---|
2712 | varValuePtr = Tcl_NewObj();
|
---|
2713 | createdNewObj = 1;
|
---|
2714 | } else if (Tcl_IsShared(varValuePtr)) {
|
---|
2715 | varValuePtr = Tcl_DuplicateObj(varValuePtr);
|
---|
2716 | createdNewObj = 1;
|
---|
2717 | }
|
---|
2718 |
|
---|
2719 | /*
|
---|
2720 | * Convert the variable's old value to a list object if necessary.
|
---|
2721 | */
|
---|
2722 |
|
---|
2723 | if (varValuePtr->typePtr != &tclListType) {
|
---|
2724 | int result = tclListType.setFromAnyProc(interp, varValuePtr);
|
---|
2725 | if (result != TCL_OK) {
|
---|
2726 | if (createdNewObj) {
|
---|
2727 | Tcl_DecrRefCount(varValuePtr); /* free unneeded obj. */
|
---|
2728 | }
|
---|
2729 | return result;
|
---|
2730 | }
|
---|
2731 | }
|
---|
2732 | listRepPtr = (List *) varValuePtr->internalRep.otherValuePtr;
|
---|
2733 | elemPtrs = listRepPtr->elements;
|
---|
2734 | numElems = listRepPtr->elemCount;
|
---|
2735 |
|
---|
2736 | /*
|
---|
2737 | * If there is no room in the current array of element pointers,
|
---|
2738 | * allocate a new, larger array and copy the pointers to it.
|
---|
2739 | */
|
---|
2740 |
|
---|
2741 | numRequired = numElems + (objc-2);
|
---|
2742 | if (numRequired > listRepPtr->maxElemCount) {
|
---|
2743 | int newMax = (2 * numRequired);
|
---|
2744 | Tcl_Obj **newElemPtrs = (Tcl_Obj **)
|
---|
2745 | ckalloc((unsigned) (newMax * sizeof(Tcl_Obj *)));
|
---|
2746 |
|
---|
2747 | memcpy((VOID *) newElemPtrs, (VOID *) elemPtrs,
|
---|
2748 | (size_t) (numElems * sizeof(Tcl_Obj *)));
|
---|
2749 | listRepPtr->maxElemCount = newMax;
|
---|
2750 | listRepPtr->elements = newElemPtrs;
|
---|
2751 | ckfree((char *) elemPtrs);
|
---|
2752 | elemPtrs = newElemPtrs;
|
---|
2753 | }
|
---|
2754 |
|
---|
2755 | /*
|
---|
2756 | * Insert the new elements at the end of the list.
|
---|
2757 | */
|
---|
2758 |
|
---|
2759 | for (i = 2, j = numElems; i < objc; i++, j++) {
|
---|
2760 | elemPtrs[j] = objv[i];
|
---|
2761 | Tcl_IncrRefCount(objv[i]);
|
---|
2762 | }
|
---|
2763 | listRepPtr->elemCount = numRequired;
|
---|
2764 |
|
---|
2765 | /*
|
---|
2766 | * Invalidate and free any old string representation since it no
|
---|
2767 | * longer reflects the list's internal representation.
|
---|
2768 | */
|
---|
2769 |
|
---|
2770 | Tcl_InvalidateStringRep(varValuePtr);
|
---|
2771 |
|
---|
2772 | /*
|
---|
2773 | * Now store the list object back into the variable. If there is an
|
---|
2774 | * error setting the new value, decrement its ref count if it
|
---|
2775 | * was new and we didn't create the variable.
|
---|
2776 | */
|
---|
2777 |
|
---|
2778 | newValuePtr = Tcl_ObjSetVar2(interp, objv[1], (Tcl_Obj *) NULL,
|
---|
2779 | varValuePtr, (TCL_LEAVE_ERR_MSG | TCL_PARSE_PART1));
|
---|
2780 | if (newValuePtr == NULL) {
|
---|
2781 | if (createdNewObj && !createVar) {
|
---|
2782 | Tcl_DecrRefCount(varValuePtr); /* free unneeded obj */
|
---|
2783 | }
|
---|
2784 | return TCL_ERROR;
|
---|
2785 | }
|
---|
2786 | }
|
---|
2787 |
|
---|
2788 | /*
|
---|
2789 | * Set the interpreter's object result to refer to the variable's value
|
---|
2790 | * object.
|
---|
2791 | */
|
---|
2792 |
|
---|
2793 | Tcl_SetObjResult(interp, newValuePtr);
|
---|
2794 | return TCL_OK;
|
---|
2795 | }
|
---|
2796 | |
---|
2797 |
|
---|
2798 | /*
|
---|
2799 | *----------------------------------------------------------------------
|
---|
2800 | *
|
---|
2801 | * Tcl_ArrayObjCmd --
|
---|
2802 | *
|
---|
2803 | * This object-based procedure is invoked to process the "array" Tcl
|
---|
2804 | * command. See the user documentation for details on what it does.
|
---|
2805 | *
|
---|
2806 | * Results:
|
---|
2807 | * A standard Tcl result object.
|
---|
2808 | *
|
---|
2809 | * Side effects:
|
---|
2810 | * See the user documentation.
|
---|
2811 | *
|
---|
2812 | *----------------------------------------------------------------------
|
---|
2813 | */
|
---|
2814 |
|
---|
2815 | /* ARGSUSED */
|
---|
2816 | int
|
---|
2817 | Tcl_ArrayObjCmd(dummy, interp, objc, objv)
|
---|
2818 | ClientData dummy; /* Not used. */
|
---|
2819 | Tcl_Interp *interp; /* Current interpreter. */
|
---|
2820 | int objc; /* Number of arguments. */
|
---|
2821 | Tcl_Obj *CONST objv[]; /* Argument objects. */
|
---|
2822 | {
|
---|
2823 | /*
|
---|
2824 | * The list of constants below should match the arrayOptions string array
|
---|
2825 | * below.
|
---|
2826 | */
|
---|
2827 |
|
---|
2828 | enum {ARRAY_ANYMORE, ARRAY_DONESEARCH, ARRAY_EXISTS, ARRAY_GET,
|
---|
2829 | ARRAY_NAMES, ARRAY_NEXTELEMENT, ARRAY_SET, ARRAY_SIZE,
|
---|
2830 | ARRAY_STARTSEARCH};
|
---|
2831 | static char *arrayOptions[] = {"anymore", "donesearch", "exists",
|
---|
2832 | "get", "names", "nextelement", "set", "size", "startsearch",
|
---|
2833 | (char *) NULL};
|
---|
2834 |
|
---|
2835 | Var *varPtr, *arrayPtr;
|
---|
2836 | Tcl_HashEntry *hPtr;
|
---|
2837 | Tcl_Obj *resultPtr = Tcl_GetObjResult(interp);
|
---|
2838 | int notArray;
|
---|
2839 | char *varName;
|
---|
2840 | int index, result;
|
---|
2841 |
|
---|
2842 |
|
---|
2843 | if (objc < 3) {
|
---|
2844 | Tcl_WrongNumArgs(interp, 1, objv, "option arrayName ?arg ...?");
|
---|
2845 | return TCL_ERROR;
|
---|
2846 | }
|
---|
2847 |
|
---|
2848 | if (Tcl_GetIndexFromObj(interp, objv[1], arrayOptions, "option", 0, &index)
|
---|
2849 | != TCL_OK) {
|
---|
2850 | return TCL_ERROR;
|
---|
2851 | }
|
---|
2852 |
|
---|
2853 | /*
|
---|
2854 | * Locate the array variable (and it better be an array).
|
---|
2855 | * THIS FAILS IF A NAME OBJECT'S STRING REP HAS A NULL BYTE.
|
---|
2856 | */
|
---|
2857 |
|
---|
2858 | varName = TclGetStringFromObj(objv[2], (int *) NULL);
|
---|
2859 | varPtr = TclLookupVar(interp, varName, (char *) NULL, /*flags*/ 0,
|
---|
2860 | /*msg*/ 0, /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr);
|
---|
2861 |
|
---|
2862 | notArray = 0;
|
---|
2863 | if ((varPtr == NULL) || !TclIsVarArray(varPtr)
|
---|
2864 | || TclIsVarUndefined(varPtr)) {
|
---|
2865 | notArray = 1;
|
---|
2866 | }
|
---|
2867 |
|
---|
2868 | switch (index) {
|
---|
2869 | case ARRAY_ANYMORE: {
|
---|
2870 | ArraySearch *searchPtr;
|
---|
2871 | char *searchId;
|
---|
2872 |
|
---|
2873 | if (objc != 4) {
|
---|
2874 | Tcl_WrongNumArgs(interp, 2, objv,
|
---|
2875 | "arrayName searchId");
|
---|
2876 | return TCL_ERROR;
|
---|
2877 | }
|
---|
2878 | if (notArray) {
|
---|
2879 | goto error;
|
---|
2880 | }
|
---|
2881 | searchId = Tcl_GetStringFromObj(objv[3], (int *) NULL);
|
---|
2882 | searchPtr = ParseSearchId(interp, varPtr, varName, searchId);
|
---|
2883 | if (searchPtr == NULL) {
|
---|
2884 | return TCL_ERROR;
|
---|
2885 | }
|
---|
2886 | while (1) {
|
---|
2887 | Var *varPtr2;
|
---|
2888 |
|
---|
2889 | if (searchPtr->nextEntry != NULL) {
|
---|
2890 | varPtr2 = (Var *) Tcl_GetHashValue(searchPtr->nextEntry);
|
---|
2891 | if (!TclIsVarUndefined(varPtr2)) {
|
---|
2892 | break;
|
---|
2893 | }
|
---|
2894 | }
|
---|
2895 | searchPtr->nextEntry = Tcl_NextHashEntry(&searchPtr->search);
|
---|
2896 | if (searchPtr->nextEntry == NULL) {
|
---|
2897 | Tcl_SetIntObj(resultPtr, 0);
|
---|
2898 | return TCL_OK;
|
---|
2899 | }
|
---|
2900 | }
|
---|
2901 | Tcl_SetIntObj(resultPtr, 1);
|
---|
2902 | break;
|
---|
2903 | }
|
---|
2904 | case ARRAY_DONESEARCH: {
|
---|
2905 | ArraySearch *searchPtr, *prevPtr;
|
---|
2906 | char *searchId;
|
---|
2907 |
|
---|
2908 | if (objc != 4) {
|
---|
2909 | Tcl_WrongNumArgs(interp, 2, objv,
|
---|
2910 | "arrayName searchId");
|
---|
2911 | return TCL_ERROR;
|
---|
2912 | }
|
---|
2913 | if (notArray) {
|
---|
2914 | goto error;
|
---|
2915 | }
|
---|
2916 | searchId = Tcl_GetStringFromObj(objv[3], (int *) NULL);
|
---|
2917 | searchPtr = ParseSearchId(interp, varPtr, varName, searchId);
|
---|
2918 | if (searchPtr == NULL) {
|
---|
2919 | return TCL_ERROR;
|
---|
2920 | }
|
---|
2921 | if (varPtr->searchPtr == searchPtr) {
|
---|
2922 | varPtr->searchPtr = searchPtr->nextPtr;
|
---|
2923 | } else {
|
---|
2924 | for (prevPtr = varPtr->searchPtr; ;
|
---|
2925 | prevPtr = prevPtr->nextPtr) {
|
---|
2926 | if (prevPtr->nextPtr == searchPtr) {
|
---|
2927 | prevPtr->nextPtr = searchPtr->nextPtr;
|
---|
2928 | break;
|
---|
2929 | }
|
---|
2930 | }
|
---|
2931 | }
|
---|
2932 | ckfree((char *) searchPtr);
|
---|
2933 | break;
|
---|
2934 | }
|
---|
2935 | case ARRAY_EXISTS: {
|
---|
2936 | if (objc != 3) {
|
---|
2937 | Tcl_WrongNumArgs(interp, 2, objv, "arrayName");
|
---|
2938 | return TCL_ERROR;
|
---|
2939 | }
|
---|
2940 | Tcl_SetIntObj(resultPtr, !notArray);
|
---|
2941 | break;
|
---|
2942 | }
|
---|
2943 | case ARRAY_GET: {
|
---|
2944 | Tcl_HashSearch search;
|
---|
2945 | Var *varPtr2;
|
---|
2946 | char *pattern = NULL;
|
---|
2947 | char *name;
|
---|
2948 | Tcl_Obj *namePtr, *valuePtr;
|
---|
2949 |
|
---|
2950 | if ((objc != 3) && (objc != 4)) {
|
---|
2951 | Tcl_WrongNumArgs(interp, 2, objv, "arrayName ?pattern?");
|
---|
2952 | return TCL_ERROR;
|
---|
2953 | }
|
---|
2954 | if (notArray) {
|
---|
2955 | return TCL_OK;
|
---|
2956 | }
|
---|
2957 | if (objc == 4) {
|
---|
2958 | pattern = Tcl_GetStringFromObj(objv[3], (int *) NULL);
|
---|
2959 | }
|
---|
2960 | for (hPtr = Tcl_FirstHashEntry(varPtr->value.tablePtr, &search);
|
---|
2961 | hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
|
---|
2962 | varPtr2 = (Var *) Tcl_GetHashValue(hPtr);
|
---|
2963 | if (TclIsVarUndefined(varPtr2)) {
|
---|
2964 | continue;
|
---|
2965 | }
|
---|
2966 | name = Tcl_GetHashKey(varPtr->value.tablePtr, hPtr);
|
---|
2967 | if ((objc == 4) && !Tcl_StringMatch(name, pattern)) {
|
---|
2968 | continue; /* element name doesn't match pattern */
|
---|
2969 | }
|
---|
2970 |
|
---|
2971 | namePtr = Tcl_NewStringObj(name, -1);
|
---|
2972 | result = Tcl_ListObjAppendElement(interp, resultPtr,
|
---|
2973 | namePtr);
|
---|
2974 | if (result != TCL_OK) {
|
---|
2975 | Tcl_DecrRefCount(namePtr); /* free unneeded name obj */
|
---|
2976 | return result;
|
---|
2977 | }
|
---|
2978 |
|
---|
2979 | valuePtr = Tcl_ObjGetVar2(interp, objv[2], namePtr,
|
---|
2980 | TCL_LEAVE_ERR_MSG);
|
---|
2981 | if (valuePtr == NULL) {
|
---|
2982 | Tcl_DecrRefCount(namePtr); /* free unneeded name obj */
|
---|
2983 | return result;
|
---|
2984 | }
|
---|
2985 | result = Tcl_ListObjAppendElement(interp, resultPtr,
|
---|
2986 | valuePtr);
|
---|
2987 | if (result != TCL_OK) {
|
---|
2988 | Tcl_DecrRefCount(namePtr); /* free unneeded name obj */
|
---|
2989 | return result;
|
---|
2990 | }
|
---|
2991 | }
|
---|
2992 | break;
|
---|
2993 | }
|
---|
2994 | case ARRAY_NAMES: {
|
---|
2995 | Tcl_HashSearch search;
|
---|
2996 | Var *varPtr2;
|
---|
2997 | char *pattern = NULL;
|
---|
2998 | char *name;
|
---|
2999 | Tcl_Obj *namePtr;
|
---|
3000 |
|
---|
3001 | if ((objc != 3) && (objc != 4)) {
|
---|
3002 | Tcl_WrongNumArgs(interp, 2, objv, "arrayName ?pattern?");
|
---|
3003 | return TCL_ERROR;
|
---|
3004 | }
|
---|
3005 | if (notArray) {
|
---|
3006 | return TCL_OK;
|
---|
3007 | }
|
---|
3008 | if (objc == 4) {
|
---|
3009 | pattern = Tcl_GetStringFromObj(objv[3], (int *) NULL);
|
---|
3010 | }
|
---|
3011 | for (hPtr = Tcl_FirstHashEntry(varPtr->value.tablePtr, &search);
|
---|
3012 | hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
|
---|
3013 | varPtr2 = (Var *) Tcl_GetHashValue(hPtr);
|
---|
3014 | if (TclIsVarUndefined(varPtr2)) {
|
---|
3015 | continue;
|
---|
3016 | }
|
---|
3017 | name = Tcl_GetHashKey(varPtr->value.tablePtr, hPtr);
|
---|
3018 | if ((objc == 4) && !Tcl_StringMatch(name, pattern)) {
|
---|
3019 | continue; /* element name doesn't match pattern */
|
---|
3020 | }
|
---|
3021 |
|
---|
3022 | namePtr = Tcl_NewStringObj(name, -1);
|
---|
3023 | result = Tcl_ListObjAppendElement(interp, resultPtr, namePtr);
|
---|
3024 | if (result != TCL_OK) {
|
---|
3025 | Tcl_DecrRefCount(namePtr); /* free unneeded name object */
|
---|
3026 | return result;
|
---|
3027 | }
|
---|
3028 | }
|
---|
3029 | break;
|
---|
3030 | }
|
---|
3031 | case ARRAY_NEXTELEMENT: {
|
---|
3032 | ArraySearch *searchPtr;
|
---|
3033 | char *searchId;
|
---|
3034 | Tcl_HashEntry *hPtr;
|
---|
3035 |
|
---|
3036 | if (objc != 4) {
|
---|
3037 | Tcl_WrongNumArgs(interp, 2, objv,
|
---|
3038 | "arrayName searchId");
|
---|
3039 | return TCL_ERROR;
|
---|
3040 | }
|
---|
3041 | if (notArray) {
|
---|
3042 | goto error;
|
---|
3043 | }
|
---|
3044 | searchId = Tcl_GetStringFromObj(objv[3], (int *) NULL);
|
---|
3045 | searchPtr = ParseSearchId(interp, varPtr, varName, searchId);
|
---|
3046 | if (searchPtr == NULL) {
|
---|
3047 | return TCL_ERROR;
|
---|
3048 | }
|
---|
3049 | while (1) {
|
---|
3050 | Var *varPtr2;
|
---|
3051 |
|
---|
3052 | hPtr = searchPtr->nextEntry;
|
---|
3053 | if (hPtr == NULL) {
|
---|
3054 | hPtr = Tcl_NextHashEntry(&searchPtr->search);
|
---|
3055 | if (hPtr == NULL) {
|
---|
3056 | return TCL_OK;
|
---|
3057 | }
|
---|
3058 | } else {
|
---|
3059 | searchPtr->nextEntry = NULL;
|
---|
3060 | }
|
---|
3061 | varPtr2 = (Var *) Tcl_GetHashValue(hPtr);
|
---|
3062 | if (!TclIsVarUndefined(varPtr2)) {
|
---|
3063 | break;
|
---|
3064 | }
|
---|
3065 | }
|
---|
3066 | Tcl_SetStringObj(resultPtr,
|
---|
3067 | Tcl_GetHashKey(varPtr->value.tablePtr, hPtr), -1);
|
---|
3068 | break;
|
---|
3069 | }
|
---|
3070 | case ARRAY_SET: {
|
---|
3071 | Tcl_Obj **elemPtrs;
|
---|
3072 | int listLen, i, result;
|
---|
3073 |
|
---|
3074 | if (objc != 4) {
|
---|
3075 | Tcl_WrongNumArgs(interp, 2, objv, "arrayName list");
|
---|
3076 | return TCL_ERROR;
|
---|
3077 | }
|
---|
3078 | result = Tcl_ListObjGetElements(interp, objv[3], &listLen,
|
---|
3079 | &elemPtrs);
|
---|
3080 | if (result != TCL_OK) {
|
---|
3081 | return result;
|
---|
3082 | }
|
---|
3083 | if (listLen & 1) {
|
---|
3084 | Tcl_ResetResult(interp);
|
---|
3085 | Tcl_AppendToObj(Tcl_GetObjResult(interp),
|
---|
3086 | "list must have an even number of elements", -1);
|
---|
3087 | return TCL_ERROR;
|
---|
3088 | }
|
---|
3089 | if (listLen > 0) {
|
---|
3090 | for (i = 0; i < listLen; i += 2) {
|
---|
3091 | if (Tcl_ObjSetVar2(interp, objv[2], elemPtrs[i],
|
---|
3092 | elemPtrs[i+1], TCL_LEAVE_ERR_MSG) == NULL) {
|
---|
3093 | result = TCL_ERROR;
|
---|
3094 | break;
|
---|
3095 | }
|
---|
3096 | }
|
---|
3097 | return result;
|
---|
3098 | }
|
---|
3099 |
|
---|
3100 | /*
|
---|
3101 | * The list is empty make sure we have an array, or create
|
---|
3102 | * one if necessary.
|
---|
3103 | */
|
---|
3104 |
|
---|
3105 | if (varPtr != NULL) {
|
---|
3106 | if (!TclIsVarUndefined(varPtr) && TclIsVarArray(varPtr)) {
|
---|
3107 | /*
|
---|
3108 | * Already an array, done.
|
---|
3109 | */
|
---|
3110 |
|
---|
3111 | return TCL_OK;
|
---|
3112 | }
|
---|
3113 | if (TclIsVarArrayElement(varPtr) ||
|
---|
3114 | !TclIsVarUndefined(varPtr)) {
|
---|
3115 | /*
|
---|
3116 | * Either an array element, or a scalar: lose!
|
---|
3117 | */
|
---|
3118 |
|
---|
3119 | VarErrMsg(interp, varName, (char *)NULL, "array set",
|
---|
3120 | needArray);
|
---|
3121 | return TCL_ERROR;
|
---|
3122 | }
|
---|
3123 | } else {
|
---|
3124 | /*
|
---|
3125 | * Create variable for new array.
|
---|
3126 | */
|
---|
3127 |
|
---|
3128 | varPtr = TclLookupVar(interp, varName, (char *) NULL, 0, 0,
|
---|
3129 | /*createPart1*/ 1, /*createPart2*/ 0,
|
---|
3130 | &arrayPtr);
|
---|
3131 | }
|
---|
3132 | TclSetVarArray(varPtr);
|
---|
3133 | TclClearVarUndefined(varPtr);
|
---|
3134 | varPtr->value.tablePtr =
|
---|
3135 | (Tcl_HashTable *) ckalloc(sizeof(Tcl_HashTable));
|
---|
3136 | Tcl_InitHashTable(varPtr->value.tablePtr, TCL_STRING_KEYS);
|
---|
3137 | return TCL_OK;
|
---|
3138 | }
|
---|
3139 | case ARRAY_SIZE: {
|
---|
3140 | Tcl_HashSearch search;
|
---|
3141 | Var *varPtr2;
|
---|
3142 | int size;
|
---|
3143 |
|
---|
3144 | if (objc != 3) {
|
---|
3145 | Tcl_WrongNumArgs(interp, 2, objv, "arrayName");
|
---|
3146 | return TCL_ERROR;
|
---|
3147 | }
|
---|
3148 | size = 0;
|
---|
3149 | if (!notArray) {
|
---|
3150 | for (hPtr = Tcl_FirstHashEntry(varPtr->value.tablePtr,
|
---|
3151 | &search);
|
---|
3152 | hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
|
---|
3153 | varPtr2 = (Var *) Tcl_GetHashValue(hPtr);
|
---|
3154 | if (TclIsVarUndefined(varPtr2)) {
|
---|
3155 | continue;
|
---|
3156 | }
|
---|
3157 | size++;
|
---|
3158 | }
|
---|
3159 | }
|
---|
3160 | Tcl_SetIntObj(resultPtr, size);
|
---|
3161 | break;
|
---|
3162 | }
|
---|
3163 | case ARRAY_STARTSEARCH: {
|
---|
3164 | ArraySearch *searchPtr;
|
---|
3165 |
|
---|
3166 | if (objc != 3) {
|
---|
3167 | Tcl_WrongNumArgs(interp, 2, objv, "arrayName");
|
---|
3168 | return TCL_ERROR;
|
---|
3169 | }
|
---|
3170 | if (notArray) {
|
---|
3171 | goto error;
|
---|
3172 | }
|
---|
3173 | searchPtr = (ArraySearch *) ckalloc(sizeof(ArraySearch));
|
---|
3174 | if (varPtr->searchPtr == NULL) {
|
---|
3175 | searchPtr->id = 1;
|
---|
3176 | Tcl_AppendStringsToObj(resultPtr, "s-1-", varName,
|
---|
3177 | (char *) NULL);
|
---|
3178 | } else {
|
---|
3179 | char string[20];
|
---|
3180 |
|
---|
3181 | searchPtr->id = varPtr->searchPtr->id + 1;
|
---|
3182 | TclFormatInt(string, searchPtr->id);
|
---|
3183 | Tcl_AppendStringsToObj(resultPtr, "s-", string, "-", varName,
|
---|
3184 | (char *) NULL);
|
---|
3185 | }
|
---|
3186 | searchPtr->varPtr = varPtr;
|
---|
3187 | searchPtr->nextEntry = Tcl_FirstHashEntry(varPtr->value.tablePtr,
|
---|
3188 | &searchPtr->search);
|
---|
3189 | searchPtr->nextPtr = varPtr->searchPtr;
|
---|
3190 | varPtr->searchPtr = searchPtr;
|
---|
3191 | break;
|
---|
3192 | }
|
---|
3193 | }
|
---|
3194 | return TCL_OK;
|
---|
3195 |
|
---|
3196 | error:
|
---|
3197 | Tcl_AppendStringsToObj(resultPtr, "\"", varName, "\" isn't an array",
|
---|
3198 | (char *) NULL);
|
---|
3199 | return TCL_ERROR;
|
---|
3200 | }
|
---|
3201 | |
---|
3202 |
|
---|
3203 | /*
|
---|
3204 | *----------------------------------------------------------------------
|
---|
3205 | *
|
---|
3206 | * MakeUpvar --
|
---|
3207 | *
|
---|
3208 | * This procedure does all of the work of the "global" and "upvar"
|
---|
3209 | * commands.
|
---|
3210 | *
|
---|
3211 | * Results:
|
---|
3212 | * A standard Tcl completion code. If an error occurs then an
|
---|
3213 | * error message is left in iPtr->result.
|
---|
3214 | *
|
---|
3215 | * Side effects:
|
---|
3216 | * The variable given by myName is linked to the variable in framePtr
|
---|
3217 | * given by otherP1 and otherP2, so that references to myName are
|
---|
3218 | * redirected to the other variable like a symbolic link.
|
---|
3219 | *
|
---|
3220 | *----------------------------------------------------------------------
|
---|
3221 | */
|
---|
3222 |
|
---|
3223 | static int
|
---|
3224 | MakeUpvar(iPtr, framePtr, otherP1, otherP2, otherFlags, myName, myFlags)
|
---|
3225 | Interp *iPtr; /* Interpreter containing variables. Used
|
---|
3226 | * for error messages, too. */
|
---|
3227 | CallFrame *framePtr; /* Call frame containing "other" variable.
|
---|
3228 | * NULL means use global :: context. */
|
---|
3229 | char *otherP1, *otherP2; /* Two-part name of variable in framePtr. */
|
---|
3230 | int otherFlags; /* 0, TCL_GLOBAL_ONLY or TCL_NAMESPACE_ONLY:
|
---|
3231 | * indicates scope of "other" variable. */
|
---|
3232 | char *myName; /* Name of variable which will refer to
|
---|
3233 | * otherP1/otherP2. Must be a scalar. */
|
---|
3234 | int myFlags; /* 0, TCL_GLOBAL_ONLY or TCL_NAMESPACE_ONLY:
|
---|
3235 | * indicates scope of myName. */
|
---|
3236 | {
|
---|
3237 | Tcl_HashEntry *hPtr;
|
---|
3238 | Var *otherPtr, *varPtr, *arrayPtr;
|
---|
3239 | CallFrame *varFramePtr;
|
---|
3240 | CallFrame *savedFramePtr = NULL; /* Init. to avoid compiler warning. */
|
---|
3241 | Tcl_HashTable *tablePtr;
|
---|
3242 | Namespace *nsPtr, *altNsPtr, *dummyNsPtr;
|
---|
3243 | char *tail;
|
---|
3244 | int new;
|
---|
3245 |
|
---|
3246 | /*
|
---|
3247 | * Find "other" in "framePtr". If not looking up other in just the
|
---|
3248 | * current namespace, temporarily replace the current var frame
|
---|
3249 | * pointer in the interpreter in order to use TclLookupVar.
|
---|
3250 | */
|
---|
3251 |
|
---|
3252 | if (!(otherFlags & TCL_NAMESPACE_ONLY)) {
|
---|
3253 | savedFramePtr = iPtr->varFramePtr;
|
---|
3254 | iPtr->varFramePtr = framePtr;
|
---|
3255 | }
|
---|
3256 | otherPtr = TclLookupVar((Tcl_Interp *) iPtr, otherP1, otherP2,
|
---|
3257 | (otherFlags | TCL_LEAVE_ERR_MSG), "access",
|
---|
3258 | /*createPart1*/ 1, /*createPart2*/ 1, &arrayPtr);
|
---|
3259 | if (!(otherFlags & TCL_NAMESPACE_ONLY)) {
|
---|
3260 | iPtr->varFramePtr = savedFramePtr;
|
---|
3261 | }
|
---|
3262 | if (otherPtr == NULL) {
|
---|
3263 | return TCL_ERROR;
|
---|
3264 | }
|
---|
3265 |
|
---|
3266 | /*
|
---|
3267 | * Now create a hashtable entry for "myName". Create it as either a
|
---|
3268 | * namespace variable or as a local variable in a procedure call
|
---|
3269 | * frame. Interpret myName as a namespace variable if:
|
---|
3270 | * 1) so requested by a TCL_GLOBAL_ONLY or TCL_NAMESPACE_ONLY flag,
|
---|
3271 | * 2) there is no active frame (we're at the global :: scope),
|
---|
3272 | * 3) the active frame was pushed to define the namespace context
|
---|
3273 | * for a "namespace eval" or "namespace inscope" command,
|
---|
3274 | * 4) the name has namespace qualifiers ("::"s).
|
---|
3275 | * If creating myName in the active procedure, look first in the
|
---|
3276 | * frame's array of compiler-allocated local variables, then in its
|
---|
3277 | * hashtable for runtime-created local variables. Create that
|
---|
3278 | * procedure's local variable hashtable if necessary.
|
---|
3279 | */
|
---|
3280 |
|
---|
3281 | varFramePtr = iPtr->varFramePtr;
|
---|
3282 | if ((myFlags & (TCL_GLOBAL_ONLY | TCL_NAMESPACE_ONLY))
|
---|
3283 | || (varFramePtr == NULL)
|
---|
3284 | || !varFramePtr->isProcCallFrame
|
---|
3285 | || (strstr(myName, "::") != NULL)) {
|
---|
3286 | TclGetNamespaceForQualName((Tcl_Interp *) iPtr, myName,
|
---|
3287 | (Namespace *) NULL, myFlags, &nsPtr, &altNsPtr, &dummyNsPtr, &tail);
|
---|
3288 |
|
---|
3289 | if (nsPtr == NULL) {
|
---|
3290 | nsPtr = altNsPtr;
|
---|
3291 | }
|
---|
3292 | if (nsPtr == NULL) {
|
---|
3293 | Tcl_AppendResult((Tcl_Interp *) iPtr, "bad variable name \"",
|
---|
3294 | myName, "\": unknown namespace", (char *) NULL);
|
---|
3295 | return TCL_ERROR;
|
---|
3296 | }
|
---|
3297 |
|
---|
3298 | /*
|
---|
3299 | * Check that we are not trying to create a namespace var linked to
|
---|
3300 | * a local variable in a procedure. If we allowed this, the local
|
---|
3301 | * variable in the shorter-lived procedure frame could go away
|
---|
3302 | * leaving the namespace var's reference invalid.
|
---|
3303 | */
|
---|
3304 |
|
---|
3305 | if ((otherP2 ? arrayPtr->nsPtr : otherPtr->nsPtr) == NULL) {
|
---|
3306 | Tcl_AppendResult((Tcl_Interp *) iPtr, "bad variable name \"",
|
---|
3307 | myName, "\": upvar won't create namespace variable that refers to procedure variable",
|
---|
3308 | (char *) NULL);
|
---|
3309 | return TCL_ERROR;
|
---|
3310 | }
|
---|
3311 |
|
---|
3312 | hPtr = Tcl_CreateHashEntry(&nsPtr->varTable, tail, &new);
|
---|
3313 | if (new) {
|
---|
3314 | varPtr = NewVar();
|
---|
3315 | Tcl_SetHashValue(hPtr, varPtr);
|
---|
3316 | varPtr->hPtr = hPtr;
|
---|
3317 | varPtr->nsPtr = nsPtr;
|
---|
3318 | } else {
|
---|
3319 | varPtr = (Var *) Tcl_GetHashValue(hPtr);
|
---|
3320 | }
|
---|
3321 | } else { /* look in the call frame */
|
---|
3322 | Proc *procPtr = varFramePtr->procPtr;
|
---|
3323 | int localCt = procPtr->numCompiledLocals;
|
---|
3324 | CompiledLocal *localPtr = procPtr->firstLocalPtr;
|
---|
3325 | Var *localVarPtr = varFramePtr->compiledLocals;
|
---|
3326 | int nameLen = strlen(myName);
|
---|
3327 | int i;
|
---|
3328 |
|
---|
3329 | varPtr = NULL;
|
---|
3330 | for (i = 0; i < localCt; i++) {
|
---|
3331 | if (!TclIsVarTemporary(localPtr)) {
|
---|
3332 | char *localName = localVarPtr->name;
|
---|
3333 | if ((myName[0] == localName[0])
|
---|
3334 | && (nameLen == localPtr->nameLength)
|
---|
3335 | && (strcmp(myName, localName) == 0)) {
|
---|
3336 | varPtr = localVarPtr;
|
---|
3337 | new = 0;
|
---|
3338 | break;
|
---|
3339 | }
|
---|
3340 | }
|
---|
3341 | localVarPtr++;
|
---|
3342 | localPtr = localPtr->nextPtr;
|
---|
3343 | }
|
---|
3344 | if (varPtr == NULL) { /* look in frame's local var hashtable */
|
---|
3345 | tablePtr = varFramePtr->varTablePtr;
|
---|
3346 | if (tablePtr == NULL) {
|
---|
3347 | tablePtr = (Tcl_HashTable *) ckalloc(sizeof(Tcl_HashTable));
|
---|
3348 | Tcl_InitHashTable(tablePtr, TCL_STRING_KEYS);
|
---|
3349 | varFramePtr->varTablePtr = tablePtr;
|
---|
3350 | }
|
---|
3351 | hPtr = Tcl_CreateHashEntry(tablePtr, myName, &new);
|
---|
3352 | if (new) {
|
---|
3353 | varPtr = NewVar();
|
---|
3354 | Tcl_SetHashValue(hPtr, varPtr);
|
---|
3355 | varPtr->hPtr = hPtr;
|
---|
3356 | varPtr->nsPtr = varFramePtr->nsPtr;
|
---|
3357 | } else {
|
---|
3358 | varPtr = (Var *) Tcl_GetHashValue(hPtr);
|
---|
3359 | }
|
---|
3360 | }
|
---|
3361 | }
|
---|
3362 |
|
---|
3363 | if (!new) {
|
---|
3364 | /*
|
---|
3365 | * The variable already exists. Make sure this variable "varPtr"
|
---|
3366 | * isn't the same as "otherPtr" (avoid circular links). Also, if
|
---|
3367 | * it's not an upvar then it's an error. If it is an upvar, then
|
---|
3368 | * just disconnect it from the thing it currently refers to.
|
---|
3369 | */
|
---|
3370 |
|
---|
3371 | if (varPtr == otherPtr) {
|
---|
3372 | Tcl_SetResult((Tcl_Interp *) iPtr,
|
---|
3373 | "can't upvar from variable to itself", TCL_STATIC);
|
---|
3374 | return TCL_ERROR;
|
---|
3375 | }
|
---|
3376 | if (TclIsVarLink(varPtr)) {
|
---|
3377 | Var *linkPtr = varPtr->value.linkPtr;
|
---|
3378 | if (linkPtr == otherPtr) {
|
---|
3379 | return TCL_OK;
|
---|
3380 | }
|
---|
3381 | linkPtr->refCount--;
|
---|
3382 | if (TclIsVarUndefined(linkPtr)) {
|
---|
3383 | CleanupVar(linkPtr, (Var *) NULL);
|
---|
3384 | }
|
---|
3385 | } else if (!TclIsVarUndefined(varPtr)) {
|
---|
3386 | Tcl_AppendResult((Tcl_Interp *) iPtr, "variable \"", myName,
|
---|
3387 | "\" already exists", (char *) NULL);
|
---|
3388 | return TCL_ERROR;
|
---|
3389 | } else if (varPtr->tracePtr != NULL) {
|
---|
3390 | Tcl_AppendResult((Tcl_Interp *) iPtr, "variable \"", myName,
|
---|
3391 | "\" has traces: can't use for upvar", (char *) NULL);
|
---|
3392 | return TCL_ERROR;
|
---|
3393 | }
|
---|
3394 | }
|
---|
3395 | TclSetVarLink(varPtr);
|
---|
3396 | TclClearVarUndefined(varPtr);
|
---|
3397 | varPtr->value.linkPtr = otherPtr;
|
---|
3398 | otherPtr->refCount++;
|
---|
3399 | return TCL_OK;
|
---|
3400 | }
|
---|
3401 | |
---|
3402 |
|
---|
3403 | /*
|
---|
3404 | *----------------------------------------------------------------------
|
---|
3405 | *
|
---|
3406 | * Tcl_UpVar --
|
---|
3407 | *
|
---|
3408 | * This procedure links one variable to another, just like
|
---|
3409 | * the "upvar" command.
|
---|
3410 | *
|
---|
3411 | * Results:
|
---|
3412 | * A standard Tcl completion code. If an error occurs then
|
---|
3413 | * an error message is left in interp->result.
|
---|
3414 | *
|
---|
3415 | * Side effects:
|
---|
3416 | * The variable in frameName whose name is given by varName becomes
|
---|
3417 | * accessible under the name localName, so that references to
|
---|
3418 | * localName are redirected to the other variable like a symbolic
|
---|
3419 | * link.
|
---|
3420 | *
|
---|
3421 | *----------------------------------------------------------------------
|
---|
3422 | */
|
---|
3423 |
|
---|
3424 | int
|
---|
3425 | Tcl_UpVar(interp, frameName, varName, localName, flags)
|
---|
3426 | Tcl_Interp *interp; /* Command interpreter in which varName is
|
---|
3427 | * to be looked up. */
|
---|
3428 | char *frameName; /* Name of the frame containing the source
|
---|
3429 | * variable, such as "1" or "#0". */
|
---|
3430 | char *varName; /* Name of a variable in interp to link to.
|
---|
3431 | * May be either a scalar name or an
|
---|
3432 | * element in an array. */
|
---|
3433 | char *localName; /* Name of link variable. */
|
---|
3434 | int flags; /* 0, TCL_GLOBAL_ONLY or TCL_NAMESPACE_ONLY:
|
---|
3435 | * indicates scope of localName. */
|
---|
3436 | {
|
---|
3437 | int result;
|
---|
3438 | CallFrame *framePtr;
|
---|
3439 | register char *p;
|
---|
3440 |
|
---|
3441 | result = TclGetFrame(interp, frameName, &framePtr);
|
---|
3442 | if (result == -1) {
|
---|
3443 | return TCL_ERROR;
|
---|
3444 | }
|
---|
3445 |
|
---|
3446 | /*
|
---|
3447 | * Figure out whether varName is an array reference, then call
|
---|
3448 | * MakeUpvar to do all the real work.
|
---|
3449 | */
|
---|
3450 |
|
---|
3451 | for (p = varName; *p != '\0'; p++) {
|
---|
3452 | if (*p == '(') {
|
---|
3453 | char *openParen = p;
|
---|
3454 | do {
|
---|
3455 | p++;
|
---|
3456 | } while (*p != '\0');
|
---|
3457 | p--;
|
---|
3458 | if (*p != ')') {
|
---|
3459 | goto scalar;
|
---|
3460 | }
|
---|
3461 | *openParen = '\0';
|
---|
3462 | *p = '\0';
|
---|
3463 | result = MakeUpvar((Interp *) interp, framePtr, varName,
|
---|
3464 | openParen+1, 0, localName, flags);
|
---|
3465 | *openParen = '(';
|
---|
3466 | *p = ')';
|
---|
3467 | return result;
|
---|
3468 | }
|
---|
3469 | }
|
---|
3470 |
|
---|
3471 | scalar:
|
---|
3472 | return MakeUpvar((Interp *) interp, framePtr, varName, (char *) NULL,
|
---|
3473 | 0, localName, flags);
|
---|
3474 | }
|
---|
3475 | |
---|
3476 |
|
---|
3477 | /*
|
---|
3478 | *----------------------------------------------------------------------
|
---|
3479 | *
|
---|
3480 | * Tcl_UpVar2 --
|
---|
3481 | *
|
---|
3482 | * This procedure links one variable to another, just like
|
---|
3483 | * the "upvar" command.
|
---|
3484 | *
|
---|
3485 | * Results:
|
---|
3486 | * A standard Tcl completion code. If an error occurs then
|
---|
3487 | * an error message is left in interp->result.
|
---|
3488 | *
|
---|
3489 | * Side effects:
|
---|
3490 | * The variable in frameName whose name is given by part1 and
|
---|
3491 | * part2 becomes accessible under the name localName, so that
|
---|
3492 | * references to localName are redirected to the other variable
|
---|
3493 | * like a symbolic link.
|
---|
3494 | *
|
---|
3495 | *----------------------------------------------------------------------
|
---|
3496 | */
|
---|
3497 |
|
---|
3498 | int
|
---|
3499 | Tcl_UpVar2(interp, frameName, part1, part2, localName, flags)
|
---|
3500 | Tcl_Interp *interp; /* Interpreter containing variables. Used
|
---|
3501 | * for error messages too. */
|
---|
3502 | char *frameName; /* Name of the frame containing the source
|
---|
3503 | * variable, such as "1" or "#0". */
|
---|
3504 | char *part1, *part2; /* Two parts of source variable name to
|
---|
3505 | * link to. */
|
---|
3506 | char *localName; /* Name of link variable. */
|
---|
3507 | int flags; /* 0, TCL_GLOBAL_ONLY or TCL_NAMESPACE_ONLY:
|
---|
3508 | * indicates scope of localName. */
|
---|
3509 | {
|
---|
3510 | int result;
|
---|
3511 | CallFrame *framePtr;
|
---|
3512 |
|
---|
3513 | result = TclGetFrame(interp, frameName, &framePtr);
|
---|
3514 | if (result == -1) {
|
---|
3515 | return TCL_ERROR;
|
---|
3516 | }
|
---|
3517 | return MakeUpvar((Interp *) interp, framePtr, part1, part2, 0,
|
---|
3518 | localName, flags);
|
---|
3519 | }
|
---|
3520 | |
---|
3521 |
|
---|
3522 | /*
|
---|
3523 | *----------------------------------------------------------------------
|
---|
3524 | *
|
---|
3525 | * Tcl_GetVariableFullName --
|
---|
3526 | *
|
---|
3527 | * Given a Tcl_Var token returned by Tcl_FindNamespaceVar, this
|
---|
3528 | * procedure appends to an object the namespace variable's full
|
---|
3529 | * name, qualified by a sequence of parent namespace names.
|
---|
3530 | *
|
---|
3531 | * Results:
|
---|
3532 | * None.
|
---|
3533 | *
|
---|
3534 | * Side effects:
|
---|
3535 | * The variable's fully-qualified name is appended to the string
|
---|
3536 | * representation of objPtr.
|
---|
3537 | *
|
---|
3538 | *----------------------------------------------------------------------
|
---|
3539 | */
|
---|
3540 |
|
---|
3541 | void
|
---|
3542 | Tcl_GetVariableFullName(interp, variable, objPtr)
|
---|
3543 | Tcl_Interp *interp; /* Interpreter containing the variable. */
|
---|
3544 | Tcl_Var variable; /* Token for the variable returned by a
|
---|
3545 | * previous call to Tcl_FindNamespaceVar. */
|
---|
3546 | Tcl_Obj *objPtr; /* Points to the object onto which the
|
---|
3547 | * variable's full name is appended. */
|
---|
3548 | {
|
---|
3549 | Interp *iPtr = (Interp *) interp;
|
---|
3550 | register Var *varPtr = (Var *) variable;
|
---|
3551 | char *name;
|
---|
3552 |
|
---|
3553 | /*
|
---|
3554 | * Add the full name of the containing namespace (if any), followed by
|
---|
3555 | * the "::" separator, then the variable name.
|
---|
3556 | */
|
---|
3557 |
|
---|
3558 | if (varPtr != NULL) {
|
---|
3559 | if (!TclIsVarArrayElement(varPtr)) {
|
---|
3560 | if (varPtr->nsPtr != NULL) {
|
---|
3561 | Tcl_AppendToObj(objPtr, varPtr->nsPtr->fullName, -1);
|
---|
3562 | if (varPtr->nsPtr != iPtr->globalNsPtr) {
|
---|
3563 | Tcl_AppendToObj(objPtr, "::", 2);
|
---|
3564 | }
|
---|
3565 | }
|
---|
3566 | if (varPtr->name != NULL) {
|
---|
3567 | Tcl_AppendToObj(objPtr, varPtr->name, -1);
|
---|
3568 | } else if (varPtr->hPtr != NULL) {
|
---|
3569 | name = Tcl_GetHashKey(varPtr->hPtr->tablePtr, varPtr->hPtr);
|
---|
3570 | Tcl_AppendToObj(objPtr, name, -1);
|
---|
3571 | }
|
---|
3572 | }
|
---|
3573 | }
|
---|
3574 | }
|
---|
3575 | |
---|
3576 |
|
---|
3577 | /*
|
---|
3578 | *----------------------------------------------------------------------
|
---|
3579 | *
|
---|
3580 | * Tcl_GlobalObjCmd --
|
---|
3581 | *
|
---|
3582 | * This object-based procedure is invoked to process the "global" Tcl
|
---|
3583 | * command. See the user documentation for details on what it does.
|
---|
3584 | *
|
---|
3585 | * Results:
|
---|
3586 | * A standard Tcl object result value.
|
---|
3587 | *
|
---|
3588 | * Side effects:
|
---|
3589 | * See the user documentation.
|
---|
3590 | *
|
---|
3591 | *----------------------------------------------------------------------
|
---|
3592 | */
|
---|
3593 |
|
---|
3594 | int
|
---|
3595 | Tcl_GlobalObjCmd(dummy, interp, objc, objv)
|
---|
3596 | ClientData dummy; /* Not used. */
|
---|
3597 | Tcl_Interp *interp; /* Current interpreter. */
|
---|
3598 | int objc; /* Number of arguments. */
|
---|
3599 | Tcl_Obj *CONST objv[]; /* Argument objects. */
|
---|
3600 | {
|
---|
3601 | Interp *iPtr = (Interp *) interp;
|
---|
3602 | register Tcl_Obj *objPtr;
|
---|
3603 | char *varName;
|
---|
3604 | register char *tail;
|
---|
3605 | int result, i;
|
---|
3606 |
|
---|
3607 | if (objc < 2) {
|
---|
3608 | Tcl_WrongNumArgs(interp, 1, objv, "varName ?varName ...?");
|
---|
3609 | return TCL_ERROR;
|
---|
3610 | }
|
---|
3611 |
|
---|
3612 | /*
|
---|
3613 | * If we are not executing inside a Tcl procedure, just return.
|
---|
3614 | */
|
---|
3615 |
|
---|
3616 | if ((iPtr->varFramePtr == NULL)
|
---|
3617 | || !iPtr->varFramePtr->isProcCallFrame) {
|
---|
3618 | return TCL_OK;
|
---|
3619 | }
|
---|
3620 |
|
---|
3621 | for (i = 1; i < objc; i++) {
|
---|
3622 | /*
|
---|
3623 | * Make a local variable linked to its counterpart in the global ::
|
---|
3624 | * namespace.
|
---|
3625 | */
|
---|
3626 |
|
---|
3627 | objPtr = objv[i];
|
---|
3628 | varName = Tcl_GetStringFromObj(objPtr, (int *) NULL);
|
---|
3629 |
|
---|
3630 | /*
|
---|
3631 | * The variable name might have a scope qualifier, but the name for
|
---|
3632 | * the local "link" variable must be the simple name at the tail.
|
---|
3633 | */
|
---|
3634 |
|
---|
3635 | for (tail = varName; *tail != '\0'; tail++) {
|
---|
3636 | /* empty body */
|
---|
3637 | }
|
---|
3638 | while ((tail > varName) && ((*tail != ':') || (*(tail-1) != ':'))) {
|
---|
3639 | tail--;
|
---|
3640 | }
|
---|
3641 | if (*tail == ':') {
|
---|
3642 | tail++;
|
---|
3643 | }
|
---|
3644 |
|
---|
3645 | /*
|
---|
3646 | * Link to the variable "varName" in the global :: namespace.
|
---|
3647 | */
|
---|
3648 |
|
---|
3649 | result = MakeUpvar(iPtr, (CallFrame *) NULL,
|
---|
3650 | varName, (char *) NULL, /*otherFlags*/ TCL_GLOBAL_ONLY,
|
---|
3651 | /*myName*/ tail, /*myFlags*/ 0);
|
---|
3652 | if (result != TCL_OK) {
|
---|
3653 | return result;
|
---|
3654 | }
|
---|
3655 | }
|
---|
3656 | return TCL_OK;
|
---|
3657 | }
|
---|
3658 | |
---|
3659 |
|
---|
3660 | /*
|
---|
3661 | *----------------------------------------------------------------------
|
---|
3662 | *
|
---|
3663 | * Tcl_VariableObjCmd --
|
---|
3664 | *
|
---|
3665 | * Invoked to implement the "variable" command that creates one or more
|
---|
3666 | * global variables. Handles the following syntax:
|
---|
3667 | *
|
---|
3668 | * variable ?name value...? name ?value?
|
---|
3669 | *
|
---|
3670 | * One or more variables can be created. The variables are initialized
|
---|
3671 | * with the specified values. The value for the last variable is
|
---|
3672 | * optional.
|
---|
3673 | *
|
---|
3674 | * If the variable does not exist, it is created and given the optional
|
---|
3675 | * value. If it already exists, it is simply set to the optional
|
---|
3676 | * value. Normally, "name" is an unqualified name, so it is created in
|
---|
3677 | * the current namespace. If it includes namespace qualifiers, it can
|
---|
3678 | * be created in another namespace.
|
---|
3679 | *
|
---|
3680 | * If the variable command is executed inside a Tcl procedure, it
|
---|
3681 | * creates a local variable linked to the newly-created namespace
|
---|
3682 | * variable.
|
---|
3683 | *
|
---|
3684 | * Results:
|
---|
3685 | * Returns TCL_OK if the variable is found or created. Returns
|
---|
3686 | * TCL_ERROR if anything goes wrong.
|
---|
3687 | *
|
---|
3688 | * Side effects:
|
---|
3689 | * If anything goes wrong, this procedure returns an error message
|
---|
3690 | * as the result in the interpreter's result object.
|
---|
3691 | *
|
---|
3692 | *----------------------------------------------------------------------
|
---|
3693 | */
|
---|
3694 |
|
---|
3695 | int
|
---|
3696 | Tcl_VariableObjCmd(dummy, interp, objc, objv)
|
---|
3697 | ClientData dummy; /* Not used. */
|
---|
3698 | Tcl_Interp *interp; /* Current interpreter. */
|
---|
3699 | int objc; /* Number of arguments. */
|
---|
3700 | Tcl_Obj *CONST objv[]; /* Argument objects. */
|
---|
3701 | {
|
---|
3702 | Interp *iPtr = (Interp *) interp;
|
---|
3703 | char *varName, *tail, *cp;
|
---|
3704 | Var *varPtr, *arrayPtr;
|
---|
3705 | Tcl_Obj *varValuePtr;
|
---|
3706 | int i, result;
|
---|
3707 |
|
---|
3708 | for (i = 1; i < objc; i = i+2) {
|
---|
3709 | /*
|
---|
3710 | * Look up each variable in the current namespace context, creating
|
---|
3711 | * it if necessary.
|
---|
3712 | */
|
---|
3713 |
|
---|
3714 | varName = Tcl_GetStringFromObj(objv[i], (int *) NULL);
|
---|
3715 | varPtr = TclLookupVar(interp, varName, (char *) NULL,
|
---|
3716 | (TCL_NAMESPACE_ONLY | TCL_LEAVE_ERR_MSG), "define",
|
---|
3717 | /*createPart1*/ 1, /*createPart2*/ 0, &arrayPtr);
|
---|
3718 | if (varPtr == NULL) {
|
---|
3719 | return TCL_ERROR;
|
---|
3720 | }
|
---|
3721 |
|
---|
3722 | /*
|
---|
3723 | * Mark the variable as a namespace variable and increment its
|
---|
3724 | * reference count so that it will persist until its namespace is
|
---|
3725 | * destroyed or until the variable is unset.
|
---|
3726 | */
|
---|
3727 |
|
---|
3728 | if (!(varPtr->flags & VAR_NAMESPACE_VAR)) {
|
---|
3729 | varPtr->flags |= VAR_NAMESPACE_VAR;
|
---|
3730 | varPtr->refCount++;
|
---|
3731 | }
|
---|
3732 |
|
---|
3733 | /*
|
---|
3734 | * If a value was specified, set the variable to that value.
|
---|
3735 | * Otherwise, if the variable is new, leave it undefined.
|
---|
3736 | * (If the variable already exists and no value was specified,
|
---|
3737 | * leave its value unchanged; just create the local link if
|
---|
3738 | * we're in a Tcl procedure).
|
---|
3739 | */
|
---|
3740 |
|
---|
3741 | if (i+1 < objc) { /* a value was specified */
|
---|
3742 | varValuePtr = Tcl_ObjSetVar2(interp, objv[i], (Tcl_Obj *) NULL,
|
---|
3743 | objv[i+1], (TCL_NAMESPACE_ONLY | TCL_LEAVE_ERR_MSG));
|
---|
3744 | if (varValuePtr == NULL) {
|
---|
3745 | return TCL_ERROR;
|
---|
3746 | }
|
---|
3747 | }
|
---|
3748 |
|
---|
3749 | /*
|
---|
3750 | * If we are executing inside a Tcl procedure, create a local
|
---|
3751 | * variable linked to the new namespace variable "varName".
|
---|
3752 | */
|
---|
3753 |
|
---|
3754 | if ((iPtr->varFramePtr != NULL)
|
---|
3755 | && iPtr->varFramePtr->isProcCallFrame) {
|
---|
3756 | /*
|
---|
3757 | * varName might have a scope qualifier, but the name for the
|
---|
3758 | * local "link" variable must be the simple name at the tail.
|
---|
3759 | *
|
---|
3760 | * Locate tail in one pass: drop any prefix after two *or more*
|
---|
3761 | * consecutive ":" characters).
|
---|
3762 | */
|
---|
3763 |
|
---|
3764 | for (tail = cp = varName; *cp != '\0'; ) {
|
---|
3765 | if (*cp++ == ':') {
|
---|
3766 | while (*cp++ == ':') {
|
---|
3767 | tail = cp;
|
---|
3768 | }
|
---|
3769 | }
|
---|
3770 | }
|
---|
3771 |
|
---|
3772 | /*
|
---|
3773 | * Create a local link "tail" to the variable "varName" in the
|
---|
3774 | * current namespace.
|
---|
3775 | */
|
---|
3776 |
|
---|
3777 | result = MakeUpvar(iPtr, (CallFrame *) NULL,
|
---|
3778 | /*otherP1*/ varName, /*otherP2*/ (char *) NULL,
|
---|
3779 | /*otherFlags*/ TCL_NAMESPACE_ONLY,
|
---|
3780 | /*myName*/ tail, /*myFlags*/ 0);
|
---|
3781 | if (result != TCL_OK) {
|
---|
3782 | return result;
|
---|
3783 | }
|
---|
3784 | }
|
---|
3785 | }
|
---|
3786 | return TCL_OK;
|
---|
3787 | }
|
---|
3788 | |
---|
3789 |
|
---|
3790 | /*
|
---|
3791 | *----------------------------------------------------------------------
|
---|
3792 | *
|
---|
3793 | * Tcl_UpvarObjCmd --
|
---|
3794 | *
|
---|
3795 | * This object-based procedure is invoked to process the "upvar"
|
---|
3796 | * Tcl command. See the user documentation for details on what it does.
|
---|
3797 | *
|
---|
3798 | * Results:
|
---|
3799 | * A standard Tcl object result value.
|
---|
3800 | *
|
---|
3801 | * Side effects:
|
---|
3802 | * See the user documentation.
|
---|
3803 | *
|
---|
3804 | *----------------------------------------------------------------------
|
---|
3805 | */
|
---|
3806 |
|
---|
3807 | /* ARGSUSED */
|
---|
3808 | int
|
---|
3809 | Tcl_UpvarObjCmd(dummy, interp, objc, objv)
|
---|
3810 | ClientData dummy; /* Not used. */
|
---|
3811 | Tcl_Interp *interp; /* Current interpreter. */
|
---|
3812 | int objc; /* Number of arguments. */
|
---|
3813 | Tcl_Obj *CONST objv[]; /* Argument objects. */
|
---|
3814 | {
|
---|
3815 | register Interp *iPtr = (Interp *) interp;
|
---|
3816 | CallFrame *framePtr;
|
---|
3817 | char *frameSpec, *otherVarName, *myVarName;
|
---|
3818 | register char *p;
|
---|
3819 | int result;
|
---|
3820 |
|
---|
3821 | if (objc < 3) {
|
---|
3822 | upvarSyntax:
|
---|
3823 | Tcl_WrongNumArgs(interp, 1, objv,
|
---|
3824 | "?level? otherVar localVar ?otherVar localVar ...?");
|
---|
3825 | return TCL_ERROR;
|
---|
3826 | }
|
---|
3827 |
|
---|
3828 | /*
|
---|
3829 | * Find the call frame containing each of the "other variables" to be
|
---|
3830 | * linked to. FAILS IF objv[1]'s STRING REP CONTAINS NULLS.
|
---|
3831 | */
|
---|
3832 |
|
---|
3833 | frameSpec = Tcl_GetStringFromObj(objv[1], (int *) NULL);
|
---|
3834 | result = TclGetFrame(interp, frameSpec, &framePtr);
|
---|
3835 | if (result == -1) {
|
---|
3836 | return TCL_ERROR;
|
---|
3837 | }
|
---|
3838 | objc -= result+1;
|
---|
3839 | if ((objc & 1) != 0) {
|
---|
3840 | goto upvarSyntax;
|
---|
3841 | }
|
---|
3842 | objv += result+1;
|
---|
3843 |
|
---|
3844 | /*
|
---|
3845 | * Iterate over each (other variable, local variable) pair.
|
---|
3846 | * Divide the other variable name into two parts, then call
|
---|
3847 | * MakeUpvar to do all the work of linking it to the local variable.
|
---|
3848 | */
|
---|
3849 |
|
---|
3850 | for ( ; objc > 0; objc -= 2, objv += 2) {
|
---|
3851 | myVarName = Tcl_GetStringFromObj(objv[1], (int *) NULL);
|
---|
3852 | otherVarName = Tcl_GetStringFromObj(objv[0], (int *) NULL);
|
---|
3853 | for (p = otherVarName; *p != 0; p++) {
|
---|
3854 | if (*p == '(') {
|
---|
3855 | char *openParen = p;
|
---|
3856 |
|
---|
3857 | do {
|
---|
3858 | p++;
|
---|
3859 | } while (*p != '\0');
|
---|
3860 | p--;
|
---|
3861 | if (*p != ')') {
|
---|
3862 | goto scalar;
|
---|
3863 | }
|
---|
3864 | *openParen = '\0';
|
---|
3865 | *p = '\0';
|
---|
3866 | result = MakeUpvar(iPtr, framePtr,
|
---|
3867 | otherVarName, openParen+1, /*otherFlags*/ 0,
|
---|
3868 | myVarName, /*flags*/ 0);
|
---|
3869 | *openParen = '(';
|
---|
3870 | *p = ')';
|
---|
3871 | goto checkResult;
|
---|
3872 | }
|
---|
3873 | }
|
---|
3874 | scalar:
|
---|
3875 | result = MakeUpvar(iPtr, framePtr, otherVarName, (char *) NULL, 0,
|
---|
3876 | myVarName, /*flags*/ 0);
|
---|
3877 |
|
---|
3878 | checkResult:
|
---|
3879 | if (result != TCL_OK) {
|
---|
3880 | return TCL_ERROR;
|
---|
3881 | }
|
---|
3882 | }
|
---|
3883 | return TCL_OK;
|
---|
3884 | }
|
---|
3885 | |
---|
3886 |
|
---|
3887 | /*
|
---|
3888 | *----------------------------------------------------------------------
|
---|
3889 | *
|
---|
3890 | * CallTraces --
|
---|
3891 | *
|
---|
3892 | * This procedure is invoked to find and invoke relevant
|
---|
3893 | * trace procedures associated with a particular operation on
|
---|
3894 | * a variable. This procedure invokes traces both on the
|
---|
3895 | * variable and on its containing array (where relevant).
|
---|
3896 | *
|
---|
3897 | * Results:
|
---|
3898 | * The return value is NULL if no trace procedures were invoked, or
|
---|
3899 | * if all the invoked trace procedures returned successfully.
|
---|
3900 | * The return value is non-NULL if a trace procedure returned an
|
---|
3901 | * error (in this case no more trace procedures were invoked after
|
---|
3902 | * the error was returned). In this case the return value is a
|
---|
3903 | * pointer to a static string describing the error.
|
---|
3904 | *
|
---|
3905 | * Side effects:
|
---|
3906 | * Almost anything can happen, depending on trace; this procedure
|
---|
3907 | * itself doesn't have any side effects.
|
---|
3908 | *
|
---|
3909 | *----------------------------------------------------------------------
|
---|
3910 | */
|
---|
3911 |
|
---|
3912 | static char *
|
---|
3913 | CallTraces(iPtr, arrayPtr, varPtr, part1, part2, flags)
|
---|
3914 | Interp *iPtr; /* Interpreter containing variable. */
|
---|
3915 | register Var *arrayPtr; /* Pointer to array variable that contains
|
---|
3916 | * the variable, or NULL if the variable
|
---|
3917 | * isn't an element of an array. */
|
---|
3918 | Var *varPtr; /* Variable whose traces are to be
|
---|
3919 | * invoked. */
|
---|
3920 | char *part1, *part2; /* Variable's two-part name. */
|
---|
3921 | int flags; /* Flags passed to trace procedures:
|
---|
3922 | * indicates what's happening to variable,
|
---|
3923 | * plus other stuff like TCL_GLOBAL_ONLY,
|
---|
3924 | * TCL_NAMESPACE_ONLY, and
|
---|
3925 | * TCL_INTERP_DESTROYED. May also contain
|
---|
3926 | * TCL_PARSE_PART1, which should not be
|
---|
3927 | * passed through to callbacks. */
|
---|
3928 | {
|
---|
3929 | register VarTrace *tracePtr;
|
---|
3930 | ActiveVarTrace active;
|
---|
3931 | char *result, *openParen, *p;
|
---|
3932 | Tcl_DString nameCopy;
|
---|
3933 | int copiedName;
|
---|
3934 |
|
---|
3935 | /*
|
---|
3936 | * If there are already similar trace procedures active for the
|
---|
3937 | * variable, don't call them again.
|
---|
3938 | */
|
---|
3939 |
|
---|
3940 | if (varPtr->flags & VAR_TRACE_ACTIVE) {
|
---|
3941 | return NULL;
|
---|
3942 | }
|
---|
3943 | varPtr->flags |= VAR_TRACE_ACTIVE;
|
---|
3944 | varPtr->refCount++;
|
---|
3945 |
|
---|
3946 | /*
|
---|
3947 | * If the variable name hasn't been parsed into array name and
|
---|
3948 | * element, do it here. If there really is an array element,
|
---|
3949 | * make a copy of the original name so that NULLs can be
|
---|
3950 | * inserted into it to separate the names (can't modify the name
|
---|
3951 | * string in place, because the string might get used by the
|
---|
3952 | * callbacks we invoke).
|
---|
3953 | */
|
---|
3954 |
|
---|
3955 | copiedName = 0;
|
---|
3956 | if (flags & TCL_PARSE_PART1) {
|
---|
3957 | for (p = part1; ; p++) {
|
---|
3958 | if (*p == 0) {
|
---|
3959 | break;
|
---|
3960 | }
|
---|
3961 | if (*p == '(') {
|
---|
3962 | openParen = p;
|
---|
3963 | do {
|
---|
3964 | p++;
|
---|
3965 | } while (*p != '\0');
|
---|
3966 | p--;
|
---|
3967 | if (*p == ')') {
|
---|
3968 | Tcl_DStringInit(&nameCopy);
|
---|
3969 | Tcl_DStringAppend(&nameCopy, part1, (p-part1));
|
---|
3970 | part2 = Tcl_DStringValue(&nameCopy)
|
---|
3971 | + (openParen + 1 - part1);
|
---|
3972 | part2[-1] = 0;
|
---|
3973 | part1 = Tcl_DStringValue(&nameCopy);
|
---|
3974 | copiedName = 1;
|
---|
3975 | }
|
---|
3976 | break;
|
---|
3977 | }
|
---|
3978 | }
|
---|
3979 | }
|
---|
3980 | flags &= ~TCL_PARSE_PART1;
|
---|
3981 |
|
---|
3982 | /*
|
---|
3983 | * Invoke traces on the array containing the variable, if relevant.
|
---|
3984 | */
|
---|
3985 |
|
---|
3986 | result = NULL;
|
---|
3987 | active.nextPtr = iPtr->activeTracePtr;
|
---|
3988 | iPtr->activeTracePtr = &active;
|
---|
3989 | if (arrayPtr != NULL) {
|
---|
3990 | arrayPtr->refCount++;
|
---|
3991 | active.varPtr = arrayPtr;
|
---|
3992 | for (tracePtr = arrayPtr->tracePtr; tracePtr != NULL;
|
---|
3993 | tracePtr = active.nextTracePtr) {
|
---|
3994 | active.nextTracePtr = tracePtr->nextPtr;
|
---|
3995 | if (!(tracePtr->flags & flags)) {
|
---|
3996 | continue;
|
---|
3997 | }
|
---|
3998 | result = (*tracePtr->traceProc)(tracePtr->clientData,
|
---|
3999 | (Tcl_Interp *) iPtr, part1, part2, flags);
|
---|
4000 | if (result != NULL) {
|
---|
4001 | if (flags & TCL_TRACE_UNSETS) {
|
---|
4002 | result = NULL;
|
---|
4003 | } else {
|
---|
4004 | goto done;
|
---|
4005 | }
|
---|
4006 | }
|
---|
4007 | }
|
---|
4008 | }
|
---|
4009 |
|
---|
4010 | /*
|
---|
4011 | * Invoke traces on the variable itself.
|
---|
4012 | */
|
---|
4013 |
|
---|
4014 | if (flags & TCL_TRACE_UNSETS) {
|
---|
4015 | flags |= TCL_TRACE_DESTROYED;
|
---|
4016 | }
|
---|
4017 | active.varPtr = varPtr;
|
---|
4018 | for (tracePtr = varPtr->tracePtr; tracePtr != NULL;
|
---|
4019 | tracePtr = active.nextTracePtr) {
|
---|
4020 | active.nextTracePtr = tracePtr->nextPtr;
|
---|
4021 | if (!(tracePtr->flags & flags)) {
|
---|
4022 | continue;
|
---|
4023 | }
|
---|
4024 | result = (*tracePtr->traceProc)(tracePtr->clientData,
|
---|
4025 | (Tcl_Interp *) iPtr, part1, part2, flags);
|
---|
4026 | if (result != NULL) {
|
---|
4027 | if (flags & TCL_TRACE_UNSETS) {
|
---|
4028 | result = NULL;
|
---|
4029 | } else {
|
---|
4030 | goto done;
|
---|
4031 | }
|
---|
4032 | }
|
---|
4033 | }
|
---|
4034 |
|
---|
4035 | /*
|
---|
4036 | * Restore the variable's flags, remove the record of our active
|
---|
4037 | * traces, and then return.
|
---|
4038 | */
|
---|
4039 |
|
---|
4040 | done:
|
---|
4041 | if (arrayPtr != NULL) {
|
---|
4042 | arrayPtr->refCount--;
|
---|
4043 | }
|
---|
4044 | if (copiedName) {
|
---|
4045 | Tcl_DStringFree(&nameCopy);
|
---|
4046 | }
|
---|
4047 | varPtr->flags &= ~VAR_TRACE_ACTIVE;
|
---|
4048 | varPtr->refCount--;
|
---|
4049 | iPtr->activeTracePtr = active.nextPtr;
|
---|
4050 | return result;
|
---|
4051 | }
|
---|
4052 | |
---|
4053 |
|
---|
4054 | /*
|
---|
4055 | *----------------------------------------------------------------------
|
---|
4056 | *
|
---|
4057 | * NewVar --
|
---|
4058 | *
|
---|
4059 | * Create a new heap-allocated variable that will eventually be
|
---|
4060 | * entered into a hashtable.
|
---|
4061 | *
|
---|
4062 | * Results:
|
---|
4063 | * The return value is a pointer to the new variable structure. It is
|
---|
4064 | * marked as a scalar variable (and not a link or array variable). Its
|
---|
4065 | * value initially is NULL. The variable is not part of any hash table
|
---|
4066 | * yet. Since it will be in a hashtable and not in a call frame, its
|
---|
4067 | * name field is set NULL. It is initially marked as undefined.
|
---|
4068 | *
|
---|
4069 | * Side effects:
|
---|
4070 | * Storage gets allocated.
|
---|
4071 | *
|
---|
4072 | *----------------------------------------------------------------------
|
---|
4073 | */
|
---|
4074 |
|
---|
4075 | static Var *
|
---|
4076 | NewVar()
|
---|
4077 | {
|
---|
4078 | register Var *varPtr;
|
---|
4079 |
|
---|
4080 | varPtr = (Var *) ckalloc(sizeof(Var));
|
---|
4081 | varPtr->value.objPtr = NULL;
|
---|
4082 | varPtr->name = NULL;
|
---|
4083 | varPtr->nsPtr = NULL;
|
---|
4084 | varPtr->hPtr = NULL;
|
---|
4085 | varPtr->refCount = 0;
|
---|
4086 | varPtr->tracePtr = NULL;
|
---|
4087 | varPtr->searchPtr = NULL;
|
---|
4088 | varPtr->flags = (VAR_SCALAR | VAR_UNDEFINED | VAR_IN_HASHTABLE);
|
---|
4089 | return varPtr;
|
---|
4090 | }
|
---|
4091 | |
---|
4092 |
|
---|
4093 | /*
|
---|
4094 | *----------------------------------------------------------------------
|
---|
4095 | *
|
---|
4096 | * ParseSearchId --
|
---|
4097 | *
|
---|
4098 | * This procedure translates from a string to a pointer to an
|
---|
4099 | * active array search (if there is one that matches the string).
|
---|
4100 | *
|
---|
4101 | * Results:
|
---|
4102 | * The return value is a pointer to the array search indicated
|
---|
4103 | * by string, or NULL if there isn't one. If NULL is returned,
|
---|
4104 | * interp->result contains an error message.
|
---|
4105 | *
|
---|
4106 | * Side effects:
|
---|
4107 | * None.
|
---|
4108 | *
|
---|
4109 | *----------------------------------------------------------------------
|
---|
4110 | */
|
---|
4111 |
|
---|
4112 | static ArraySearch *
|
---|
4113 | ParseSearchId(interp, varPtr, varName, string)
|
---|
4114 | Tcl_Interp *interp; /* Interpreter containing variable. */
|
---|
4115 | Var *varPtr; /* Array variable search is for. */
|
---|
4116 | char *varName; /* Name of array variable that search is
|
---|
4117 | * supposed to be for. */
|
---|
4118 | char *string; /* String containing id of search. Must have
|
---|
4119 | * form "search-num-var" where "num" is a
|
---|
4120 | * decimal number and "var" is a variable
|
---|
4121 | * name. */
|
---|
4122 | {
|
---|
4123 | char *end;
|
---|
4124 | int id;
|
---|
4125 | ArraySearch *searchPtr;
|
---|
4126 |
|
---|
4127 | /*
|
---|
4128 | * Parse the id into the three parts separated by dashes.
|
---|
4129 | */
|
---|
4130 |
|
---|
4131 | if ((string[0] != 's') || (string[1] != '-')) {
|
---|
4132 | syntax:
|
---|
4133 | Tcl_AppendResult(interp, "illegal search identifier \"", string,
|
---|
4134 | "\"", (char *) NULL);
|
---|
4135 | return NULL;
|
---|
4136 | }
|
---|
4137 | id = strtoul(string+2, &end, 10);
|
---|
4138 | if ((end == (string+2)) || (*end != '-')) {
|
---|
4139 | goto syntax;
|
---|
4140 | }
|
---|
4141 | if (strcmp(end+1, varName) != 0) {
|
---|
4142 | Tcl_AppendResult(interp, "search identifier \"", string,
|
---|
4143 | "\" isn't for variable \"", varName, "\"", (char *) NULL);
|
---|
4144 | return NULL;
|
---|
4145 | }
|
---|
4146 |
|
---|
4147 | /*
|
---|
4148 | * Search through the list of active searches on the interpreter
|
---|
4149 | * to see if the desired one exists.
|
---|
4150 | */
|
---|
4151 |
|
---|
4152 | for (searchPtr = varPtr->searchPtr; searchPtr != NULL;
|
---|
4153 | searchPtr = searchPtr->nextPtr) {
|
---|
4154 | if (searchPtr->id == id) {
|
---|
4155 | return searchPtr;
|
---|
4156 | }
|
---|
4157 | }
|
---|
4158 | Tcl_AppendResult(interp, "couldn't find search \"", string, "\"",
|
---|
4159 | (char *) NULL);
|
---|
4160 | return NULL;
|
---|
4161 | }
|
---|
4162 | |
---|
4163 |
|
---|
4164 | /*
|
---|
4165 | *----------------------------------------------------------------------
|
---|
4166 | *
|
---|
4167 | * DeleteSearches --
|
---|
4168 | *
|
---|
4169 | * This procedure is called to free up all of the searches
|
---|
4170 | * associated with an array variable.
|
---|
4171 | *
|
---|
4172 | * Results:
|
---|
4173 | * None.
|
---|
4174 | *
|
---|
4175 | * Side effects:
|
---|
4176 | * Memory is released to the storage allocator.
|
---|
4177 | *
|
---|
4178 | *----------------------------------------------------------------------
|
---|
4179 | */
|
---|
4180 |
|
---|
4181 | static void
|
---|
4182 | DeleteSearches(arrayVarPtr)
|
---|
4183 | register Var *arrayVarPtr; /* Variable whose searches are
|
---|
4184 | * to be deleted. */
|
---|
4185 | {
|
---|
4186 | ArraySearch *searchPtr;
|
---|
4187 |
|
---|
4188 | while (arrayVarPtr->searchPtr != NULL) {
|
---|
4189 | searchPtr = arrayVarPtr->searchPtr;
|
---|
4190 | arrayVarPtr->searchPtr = searchPtr->nextPtr;
|
---|
4191 | ckfree((char *) searchPtr);
|
---|
4192 | }
|
---|
4193 | }
|
---|
4194 | |
---|
4195 |
|
---|
4196 | /*
|
---|
4197 | *----------------------------------------------------------------------
|
---|
4198 | *
|
---|
4199 | * TclDeleteVars --
|
---|
4200 | *
|
---|
4201 | * This procedure is called to recycle all the storage space
|
---|
4202 | * associated with a table of variables. For this procedure
|
---|
4203 | * to work correctly, it must not be possible for any of the
|
---|
4204 | * variables in the table to be accessed from Tcl commands
|
---|
4205 | * (e.g. from trace procedures).
|
---|
4206 | *
|
---|
4207 | * Results:
|
---|
4208 | * None.
|
---|
4209 | *
|
---|
4210 | * Side effects:
|
---|
4211 | * Variables are deleted and trace procedures are invoked, if
|
---|
4212 | * any are declared.
|
---|
4213 | *
|
---|
4214 | *----------------------------------------------------------------------
|
---|
4215 | */
|
---|
4216 |
|
---|
4217 | void
|
---|
4218 | TclDeleteVars(iPtr, tablePtr)
|
---|
4219 | Interp *iPtr; /* Interpreter to which variables belong. */
|
---|
4220 | Tcl_HashTable *tablePtr; /* Hash table containing variables to
|
---|
4221 | * delete. */
|
---|
4222 | {
|
---|
4223 | Tcl_Interp *interp = (Tcl_Interp *) iPtr;
|
---|
4224 | Tcl_HashSearch search;
|
---|
4225 | Tcl_HashEntry *hPtr;
|
---|
4226 | register Var *varPtr;
|
---|
4227 | Var *linkPtr;
|
---|
4228 | int flags;
|
---|
4229 | ActiveVarTrace *activePtr;
|
---|
4230 | Tcl_Obj *objPtr;
|
---|
4231 | Namespace *currNsPtr = (Namespace *) Tcl_GetCurrentNamespace(interp);
|
---|
4232 |
|
---|
4233 | /*
|
---|
4234 | * Determine what flags to pass to the trace callback procedures.
|
---|
4235 | */
|
---|
4236 |
|
---|
4237 | flags = TCL_TRACE_UNSETS;
|
---|
4238 | if (tablePtr == &iPtr->globalNsPtr->varTable) {
|
---|
4239 | flags |= (TCL_INTERP_DESTROYED | TCL_GLOBAL_ONLY);
|
---|
4240 | } else if (tablePtr == &currNsPtr->varTable) {
|
---|
4241 | flags |= TCL_NAMESPACE_ONLY;
|
---|
4242 | }
|
---|
4243 |
|
---|
4244 | for (hPtr = Tcl_FirstHashEntry(tablePtr, &search); hPtr != NULL;
|
---|
4245 | hPtr = Tcl_NextHashEntry(&search)) {
|
---|
4246 | varPtr = (Var *) Tcl_GetHashValue(hPtr);
|
---|
4247 |
|
---|
4248 | /*
|
---|
4249 | * For global/upvar variables referenced in procedures, decrement
|
---|
4250 | * the reference count on the variable referred to, and free
|
---|
4251 | * the referenced variable if it's no longer needed. Don't delete
|
---|
4252 | * the hash entry for the other variable if it's in the same table
|
---|
4253 | * as us: this will happen automatically later on.
|
---|
4254 | */
|
---|
4255 |
|
---|
4256 | if (TclIsVarLink(varPtr)) {
|
---|
4257 | linkPtr = varPtr->value.linkPtr;
|
---|
4258 | linkPtr->refCount--;
|
---|
4259 | if ((linkPtr->refCount == 0) && TclIsVarUndefined(linkPtr)
|
---|
4260 | && (linkPtr->tracePtr == NULL)
|
---|
4261 | && (linkPtr->flags & VAR_IN_HASHTABLE)) {
|
---|
4262 | if (linkPtr->hPtr == NULL) {
|
---|
4263 | ckfree((char *) linkPtr);
|
---|
4264 | } else if (linkPtr->hPtr->tablePtr != tablePtr) {
|
---|
4265 | Tcl_DeleteHashEntry(linkPtr->hPtr);
|
---|
4266 | ckfree((char *) linkPtr);
|
---|
4267 | }
|
---|
4268 | }
|
---|
4269 | }
|
---|
4270 |
|
---|
4271 | /*
|
---|
4272 | * Invoke traces on the variable that is being deleted, then
|
---|
4273 | * free up the variable's space (no need to free the hash entry
|
---|
4274 | * here, unless we're dealing with a global variable: the
|
---|
4275 | * hash entries will be deleted automatically when the whole
|
---|
4276 | * table is deleted). Note that we give CallTraces the variable's
|
---|
4277 | * fully-qualified name so that any called trace procedures can
|
---|
4278 | * refer to these variables being deleted.
|
---|
4279 | */
|
---|
4280 |
|
---|
4281 | if (varPtr->tracePtr != NULL) {
|
---|
4282 | objPtr = Tcl_NewObj();
|
---|
4283 | Tcl_IncrRefCount(objPtr); /* until done with traces */
|
---|
4284 | Tcl_GetVariableFullName(interp, (Tcl_Var) varPtr, objPtr);
|
---|
4285 | (void) CallTraces(iPtr, (Var *) NULL, varPtr,
|
---|
4286 | Tcl_GetStringFromObj(objPtr, (int *) NULL),
|
---|
4287 | (char *) NULL, flags);
|
---|
4288 | Tcl_DecrRefCount(objPtr); /* free no longer needed obj */
|
---|
4289 |
|
---|
4290 | while (varPtr->tracePtr != NULL) {
|
---|
4291 | VarTrace *tracePtr = varPtr->tracePtr;
|
---|
4292 | varPtr->tracePtr = tracePtr->nextPtr;
|
---|
4293 | ckfree((char *) tracePtr);
|
---|
4294 | }
|
---|
4295 | for (activePtr = iPtr->activeTracePtr; activePtr != NULL;
|
---|
4296 | activePtr = activePtr->nextPtr) {
|
---|
4297 | if (activePtr->varPtr == varPtr) {
|
---|
4298 | activePtr->nextTracePtr = NULL;
|
---|
4299 | }
|
---|
4300 | }
|
---|
4301 | }
|
---|
4302 |
|
---|
4303 | if (TclIsVarArray(varPtr)) {
|
---|
4304 | DeleteArray(iPtr, Tcl_GetHashKey(tablePtr, hPtr), varPtr,
|
---|
4305 | flags);
|
---|
4306 | varPtr->value.tablePtr = NULL;
|
---|
4307 | }
|
---|
4308 | if (TclIsVarScalar(varPtr) && (varPtr->value.objPtr != NULL)) {
|
---|
4309 | objPtr = varPtr->value.objPtr;
|
---|
4310 | TclDecrRefCount(objPtr);
|
---|
4311 | varPtr->value.objPtr = NULL;
|
---|
4312 | }
|
---|
4313 | varPtr->hPtr = NULL;
|
---|
4314 | varPtr->tracePtr = NULL;
|
---|
4315 | TclSetVarUndefined(varPtr);
|
---|
4316 | TclSetVarScalar(varPtr);
|
---|
4317 |
|
---|
4318 | /*
|
---|
4319 | * If the variable was a namespace variable, decrement its
|
---|
4320 | * reference count. We are in the process of destroying its
|
---|
4321 | * namespace so that namespace will no longer "refer" to the
|
---|
4322 | * variable.
|
---|
4323 | */
|
---|
4324 |
|
---|
4325 | if (varPtr->flags & VAR_NAMESPACE_VAR) {
|
---|
4326 | varPtr->flags &= ~VAR_NAMESPACE_VAR;
|
---|
4327 | varPtr->refCount--;
|
---|
4328 | }
|
---|
4329 |
|
---|
4330 | /*
|
---|
4331 | * Recycle the variable's memory space if there aren't any upvar's
|
---|
4332 | * pointing to it. If there are upvars to this variable, then the
|
---|
4333 | * variable will get freed when the last upvar goes away.
|
---|
4334 | */
|
---|
4335 |
|
---|
4336 | if (varPtr->refCount == 0) {
|
---|
4337 | ckfree((char *) varPtr); /* this Var must be VAR_IN_HASHTABLE */
|
---|
4338 | }
|
---|
4339 | }
|
---|
4340 | Tcl_DeleteHashTable(tablePtr);
|
---|
4341 | }
|
---|
4342 | |
---|
4343 |
|
---|
4344 | /*
|
---|
4345 | *----------------------------------------------------------------------
|
---|
4346 | *
|
---|
4347 | * TclDeleteCompiledLocalVars --
|
---|
4348 | *
|
---|
4349 | * This procedure is called to recycle storage space associated with
|
---|
4350 | * the compiler-allocated array of local variables in a procedure call
|
---|
4351 | * frame. This procedure resembles TclDeleteVars above except that each
|
---|
4352 | * variable is stored in a call frame and not a hash table. For this
|
---|
4353 | * procedure to work correctly, it must not be possible for any of the
|
---|
4354 | * variable in the table to be accessed from Tcl commands (e.g. from
|
---|
4355 | * trace procedures).
|
---|
4356 | *
|
---|
4357 | * Results:
|
---|
4358 | * None.
|
---|
4359 | *
|
---|
4360 | * Side effects:
|
---|
4361 | * Variables are deleted and trace procedures are invoked, if
|
---|
4362 | * any are declared.
|
---|
4363 | *
|
---|
4364 | *----------------------------------------------------------------------
|
---|
4365 | */
|
---|
4366 |
|
---|
4367 | void
|
---|
4368 | TclDeleteCompiledLocalVars(iPtr, framePtr)
|
---|
4369 | Interp *iPtr; /* Interpreter to which variables belong. */
|
---|
4370 | CallFrame *framePtr; /* Procedure call frame containing
|
---|
4371 | * compiler-assigned local variables to
|
---|
4372 | * delete. */
|
---|
4373 | {
|
---|
4374 | register Var *varPtr;
|
---|
4375 | int flags; /* Flags passed to trace procedures. */
|
---|
4376 | Var *linkPtr;
|
---|
4377 | ActiveVarTrace *activePtr;
|
---|
4378 | int numLocals, i;
|
---|
4379 |
|
---|
4380 | flags = TCL_TRACE_UNSETS;
|
---|
4381 | numLocals = framePtr->numCompiledLocals;
|
---|
4382 | varPtr = framePtr->compiledLocals;
|
---|
4383 | for (i = 0; i < numLocals; i++) {
|
---|
4384 | /*
|
---|
4385 | * For global/upvar variables referenced in procedures, decrement
|
---|
4386 | * the reference count on the variable referred to, and free
|
---|
4387 | * the referenced variable if it's no longer needed. Don't delete
|
---|
4388 | * the hash entry for the other variable if it's in the same table
|
---|
4389 | * as us: this will happen automatically later on.
|
---|
4390 | */
|
---|
4391 |
|
---|
4392 | if (TclIsVarLink(varPtr)) {
|
---|
4393 | linkPtr = varPtr->value.linkPtr;
|
---|
4394 | linkPtr->refCount--;
|
---|
4395 | if ((linkPtr->refCount == 0) && TclIsVarUndefined(linkPtr)
|
---|
4396 | && (linkPtr->tracePtr == NULL)
|
---|
4397 | && (linkPtr->flags & VAR_IN_HASHTABLE)) {
|
---|
4398 | if (linkPtr->hPtr == NULL) {
|
---|
4399 | ckfree((char *) linkPtr);
|
---|
4400 | } else {
|
---|
4401 | Tcl_DeleteHashEntry(linkPtr->hPtr);
|
---|
4402 | ckfree((char *) linkPtr);
|
---|
4403 | }
|
---|
4404 | }
|
---|
4405 | }
|
---|
4406 |
|
---|
4407 | /*
|
---|
4408 | * Invoke traces on the variable that is being deleted. Then delete
|
---|
4409 | * the variable's trace records.
|
---|
4410 | */
|
---|
4411 |
|
---|
4412 | if (varPtr->tracePtr != NULL) {
|
---|
4413 | (void) CallTraces(iPtr, (Var *) NULL, varPtr,
|
---|
4414 | varPtr->name, (char *) NULL, flags);
|
---|
4415 | while (varPtr->tracePtr != NULL) {
|
---|
4416 | VarTrace *tracePtr = varPtr->tracePtr;
|
---|
4417 | varPtr->tracePtr = tracePtr->nextPtr;
|
---|
4418 | ckfree((char *) tracePtr);
|
---|
4419 | }
|
---|
4420 | for (activePtr = iPtr->activeTracePtr; activePtr != NULL;
|
---|
4421 | activePtr = activePtr->nextPtr) {
|
---|
4422 | if (activePtr->varPtr == varPtr) {
|
---|
4423 | activePtr->nextTracePtr = NULL;
|
---|
4424 | }
|
---|
4425 | }
|
---|
4426 | }
|
---|
4427 |
|
---|
4428 | /*
|
---|
4429 | * Now if the variable is an array, delete its element hash table.
|
---|
4430 | * Otherwise, if it's a scalar variable, decrement the ref count
|
---|
4431 | * of its value.
|
---|
4432 | */
|
---|
4433 |
|
---|
4434 | if (TclIsVarArray(varPtr) && (varPtr->value.tablePtr != NULL)) {
|
---|
4435 | DeleteArray(iPtr, varPtr->name, varPtr, flags);
|
---|
4436 | }
|
---|
4437 | if (TclIsVarScalar(varPtr) && (varPtr->value.objPtr != NULL)) {
|
---|
4438 | TclDecrRefCount(varPtr->value.objPtr);
|
---|
4439 | varPtr->value.objPtr = NULL;
|
---|
4440 | }
|
---|
4441 | varPtr->hPtr = NULL;
|
---|
4442 | varPtr->tracePtr = NULL;
|
---|
4443 | TclSetVarUndefined(varPtr);
|
---|
4444 | TclSetVarScalar(varPtr);
|
---|
4445 | varPtr++;
|
---|
4446 | }
|
---|
4447 | }
|
---|
4448 | |
---|
4449 |
|
---|
4450 | /*
|
---|
4451 | *----------------------------------------------------------------------
|
---|
4452 | *
|
---|
4453 | * DeleteArray --
|
---|
4454 | *
|
---|
4455 | * This procedure is called to free up everything in an array
|
---|
4456 | * variable. It's the caller's responsibility to make sure
|
---|
4457 | * that the array is no longer accessible before this procedure
|
---|
4458 | * is called.
|
---|
4459 | *
|
---|
4460 | * Results:
|
---|
4461 | * None.
|
---|
4462 | *
|
---|
4463 | * Side effects:
|
---|
4464 | * All storage associated with varPtr's array elements is deleted
|
---|
4465 | * (including the array's hash table). Deletion trace procedures for
|
---|
4466 | * array elements are invoked, then deleted. Any pending traces for
|
---|
4467 | * array elements are also deleted.
|
---|
4468 | *
|
---|
4469 | *----------------------------------------------------------------------
|
---|
4470 | */
|
---|
4471 |
|
---|
4472 | static void
|
---|
4473 | DeleteArray(iPtr, arrayName, varPtr, flags)
|
---|
4474 | Interp *iPtr; /* Interpreter containing array. */
|
---|
4475 | char *arrayName; /* Name of array (used for trace
|
---|
4476 | * callbacks). */
|
---|
4477 | Var *varPtr; /* Pointer to variable structure. */
|
---|
4478 | int flags; /* Flags to pass to CallTraces:
|
---|
4479 | * TCL_TRACE_UNSETS and sometimes
|
---|
4480 | * TCL_INTERP_DESTROYED,
|
---|
4481 | * TCL_NAMESPACE_ONLY, or
|
---|
4482 | * TCL_GLOBAL_ONLY. */
|
---|
4483 | {
|
---|
4484 | Tcl_HashSearch search;
|
---|
4485 | register Tcl_HashEntry *hPtr;
|
---|
4486 | register Var *elPtr;
|
---|
4487 | ActiveVarTrace *activePtr;
|
---|
4488 | Tcl_Obj *objPtr;
|
---|
4489 |
|
---|
4490 | DeleteSearches(varPtr);
|
---|
4491 | for (hPtr = Tcl_FirstHashEntry(varPtr->value.tablePtr, &search);
|
---|
4492 | hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
|
---|
4493 | elPtr = (Var *) Tcl_GetHashValue(hPtr);
|
---|
4494 | if (TclIsVarScalar(elPtr) && (elPtr->value.objPtr != NULL)) {
|
---|
4495 | objPtr = elPtr->value.objPtr;
|
---|
4496 | TclDecrRefCount(objPtr);
|
---|
4497 | elPtr->value.objPtr = NULL;
|
---|
4498 | }
|
---|
4499 | elPtr->hPtr = NULL;
|
---|
4500 | if (elPtr->tracePtr != NULL) {
|
---|
4501 | elPtr->flags &= ~VAR_TRACE_ACTIVE;
|
---|
4502 | (void) CallTraces(iPtr, (Var *) NULL, elPtr, arrayName,
|
---|
4503 | Tcl_GetHashKey(varPtr->value.tablePtr, hPtr), flags);
|
---|
4504 | while (elPtr->tracePtr != NULL) {
|
---|
4505 | VarTrace *tracePtr = elPtr->tracePtr;
|
---|
4506 | elPtr->tracePtr = tracePtr->nextPtr;
|
---|
4507 | ckfree((char *) tracePtr);
|
---|
4508 | }
|
---|
4509 | for (activePtr = iPtr->activeTracePtr; activePtr != NULL;
|
---|
4510 | activePtr = activePtr->nextPtr) {
|
---|
4511 | if (activePtr->varPtr == elPtr) {
|
---|
4512 | activePtr->nextTracePtr = NULL;
|
---|
4513 | }
|
---|
4514 | }
|
---|
4515 | }
|
---|
4516 | TclSetVarUndefined(elPtr);
|
---|
4517 | TclSetVarScalar(elPtr);
|
---|
4518 | if (elPtr->refCount == 0) {
|
---|
4519 | ckfree((char *) elPtr); /* element Vars are VAR_IN_HASHTABLE */
|
---|
4520 | }
|
---|
4521 | }
|
---|
4522 | Tcl_DeleteHashTable(varPtr->value.tablePtr);
|
---|
4523 | ckfree((char *) varPtr->value.tablePtr);
|
---|
4524 | }
|
---|
4525 | |
---|
4526 |
|
---|
4527 | /*
|
---|
4528 | *----------------------------------------------------------------------
|
---|
4529 | *
|
---|
4530 | * CleanupVar --
|
---|
4531 | *
|
---|
4532 | * This procedure is called when it looks like it may be OK to free up
|
---|
4533 | * a variable's storage. If the variable is in a hashtable, its Var
|
---|
4534 | * structure and hash table entry will be freed along with those of its
|
---|
4535 | * containing array, if any. This procedure is called, for example,
|
---|
4536 | * when a trace on a variable deletes a variable.
|
---|
4537 | *
|
---|
4538 | * Results:
|
---|
4539 | * None.
|
---|
4540 | *
|
---|
4541 | * Side effects:
|
---|
4542 | * If the variable (or its containing array) really is dead and in a
|
---|
4543 | * hashtable, then its Var structure, and possibly its hash table
|
---|
4544 | * entry, is freed up.
|
---|
4545 | *
|
---|
4546 | *----------------------------------------------------------------------
|
---|
4547 | */
|
---|
4548 |
|
---|
4549 | static void
|
---|
4550 | CleanupVar(varPtr, arrayPtr)
|
---|
4551 | Var *varPtr; /* Pointer to variable that may be a
|
---|
4552 | * candidate for being expunged. */
|
---|
4553 | Var *arrayPtr; /* Array that contains the variable, or
|
---|
4554 | * NULL if this variable isn't an array
|
---|
4555 | * element. */
|
---|
4556 | {
|
---|
4557 | if (TclIsVarUndefined(varPtr) && (varPtr->refCount == 0)
|
---|
4558 | && (varPtr->tracePtr == NULL)
|
---|
4559 | && (varPtr->flags & VAR_IN_HASHTABLE)) {
|
---|
4560 | if (varPtr->hPtr != NULL) {
|
---|
4561 | Tcl_DeleteHashEntry(varPtr->hPtr);
|
---|
4562 | }
|
---|
4563 | ckfree((char *) varPtr);
|
---|
4564 | }
|
---|
4565 | if (arrayPtr != NULL) {
|
---|
4566 | if (TclIsVarUndefined(arrayPtr) && (arrayPtr->refCount == 0)
|
---|
4567 | && (arrayPtr->tracePtr == NULL)
|
---|
4568 | && (arrayPtr->flags & VAR_IN_HASHTABLE)) {
|
---|
4569 | if (arrayPtr->hPtr != NULL) {
|
---|
4570 | Tcl_DeleteHashEntry(arrayPtr->hPtr);
|
---|
4571 | }
|
---|
4572 | ckfree((char *) arrayPtr);
|
---|
4573 | }
|
---|
4574 | }
|
---|
4575 | }
|
---|
4576 | /*
|
---|
4577 | *----------------------------------------------------------------------
|
---|
4578 | *
|
---|
4579 | * VarErrMsg --
|
---|
4580 | *
|
---|
4581 | * Generate a reasonable error message describing why a variable
|
---|
4582 | * operation failed.
|
---|
4583 | *
|
---|
4584 | * Results:
|
---|
4585 | * None.
|
---|
4586 | *
|
---|
4587 | * Side effects:
|
---|
4588 | * Interp->result is reset to hold a message identifying the
|
---|
4589 | * variable given by part1 and part2 and describing why the
|
---|
4590 | * variable operation failed.
|
---|
4591 | *
|
---|
4592 | *----------------------------------------------------------------------
|
---|
4593 | */
|
---|
4594 |
|
---|
4595 | static void
|
---|
4596 | VarErrMsg(interp, part1, part2, operation, reason)
|
---|
4597 | Tcl_Interp *interp; /* Interpreter in which to record message. */
|
---|
4598 | char *part1, *part2; /* Variable's two-part name. */
|
---|
4599 | char *operation; /* String describing operation that failed,
|
---|
4600 | * e.g. "read", "set", or "unset". */
|
---|
4601 | char *reason; /* String describing why operation failed. */
|
---|
4602 | {
|
---|
4603 | Tcl_ResetResult(interp);
|
---|
4604 | Tcl_AppendResult(interp, "can't ", operation, " \"", part1,
|
---|
4605 | (char *) NULL);
|
---|
4606 | if (part2 != NULL) {
|
---|
4607 | Tcl_AppendResult(interp, "(", part2, ")", (char *) NULL);
|
---|
4608 | }
|
---|
4609 | Tcl_AppendResult(interp, "\": ", reason, (char *) NULL);
|
---|
4610 | }
|
---|