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