/[dtapublic]/projs/trunk/shared_source/c_tcl_base_7_5_w_mods/tclvar.c
ViewVC logotype

Contents of /projs/trunk/shared_source/c_tcl_base_7_5_w_mods/tclvar.c

Parent Directory Parent Directory | Revision Log Revision Log


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

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25