Debian Codegraph @

pTk/mTk/tclGeneric/tclVar.c

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