Fork me on GitHub

source: git/external/tcl/tclVar.c@ 00afcd8

ImprovedOutputFile Timing dual_readout llp 3.3.3pre15
Last change on this file since 00afcd8 was d7d2da3, checked in by pavel <pavel@…>, 12 years ago

move branches/ModularDelphes to trunk

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