Tcl Source Code

Changes On Branch amg-array-enum-c-api
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch amg-array-enum-c-api Excluding Merge-Ins

This is equivalent to a diff from 31fbc1f55a to d1f8336a60

2017-04-15
15:15
Check in stash. This code probably does not compile as-is. Changes intended to address possible se... Leaf check-in: d1f8336a60 user: andy tags: amg-array-enum-c-api
2017-01-01
19:10
Update zlib to version 1.2.9. Dll's and *.lib files not updated yet check-in: 4b7084a579 user: jan.nijtmans tags: trunk
2016-12-25
00:39
Add note to array documentation about the general uselessness of -exact matching. It exists because... check-in: aac2dc5db6 user: andy tags: amg-array-enum-c-api
2016-12-24
23:02
Merge trunk. Test var-13.2 now fails due to [af5c35971a]. check-in: 5066c38552 user: andy tags: array-search-unset
2016-12-23
15:49
merge trunk check-in: c56a03aae7 user: jan.nijtmans tags: novem
15:43
Update -DMSTATS functionality, for possible total memory sizes > 2Gb. One more place where use of si... check-in: 31fbc1f55a user: jan.nijtmans tags: trunk
08:27
Eliminate the internal macro/function TclNewIntObj: In all cases TclNewLongObj is just as good. check-in: d12ed0fcc6 user: jan.nijtmans tags: trunk

Added doc/ArraySet.3.



























































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
'\"
'\" Copyright (c) 2016 Andy Goth
'\"
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
.TH Tcl_ArraySet 3 8.7 Tcl "Tcl Library Procedures"
.so man.macros
.BS
'\" Note: do not modify the .SH NAME line immediately below!
.SH NAME
Tcl_ArraySet, Tcl_ArrayUnset, Tcl_ArrayGet, Tcl_ArrayNames, Tcl_ArraySize, Tcl_ArrayExists, Tcl_ArraySearchStart, Tcl_ArraySearchPeek, Tcl_ArraySearchNext, Tcl_ArraySearchDone, Tcl_ArrayStatistics \- manipulate Tcl array variables
.SH SYNOPSIS
.nf
\fB#include <tcl.h>\fR
.sp
int
\fBTcl_ArraySet\fR(\fIinterp, part1Ptr, dictPtr, flags\fR)
.sp
int
\fBTcl_ArrayUnset\fR(\fIinterp, part1Ptr, part2Ptr, flags\fR)
.sp
int
\fBTcl_ArrayGet\fR(\fIinterp, part1Ptr, part2Ptr, dictPtr, flags\fR)
.sp
int
\fBTcl_ArrayNames\fR(\fIinterp, part1Ptr, part2Ptr, listPtr, flags\fR)
.sp
int
\fBTcl_ArraySize\fR(\fIinterp, part1Ptr, part2Ptr, intPtr, flags\fR)
.sp
int
\fBTcl_ArrayExists\fR(\fIinterp, part1Ptr, part2ptr, intPtr, flags\fR)
.sp
Tcl_ArraySearch
\fBTcl_ArraySearchStart\fR(\fIinterp, part1Ptr, part2Ptr, flags\fR)
.sp
Tcl_Obj *
\fBTcl_ArraySearchPeek\fR(\fIsearch\fR)
.sp
Tcl_Obj *
\fBTcl_ArraySearchNext\fR(\fIsearch\fR)
.sp
void
\fBTcl_ArraySearchDone\fR(\fIsearch\fR)
.sp
int
\fBTcl_ArrayStatistics\fR(\fIinterp, part1Ptr, stringPtr, flags\fR)
.SH ARGUMENTS
.AS Tcl_ArraySearch search
.AP Tcl_Interp *interp in
Interpreter containing the variable.  If an error occurs, an error message is
left in the interpreter's result.
.AP Tcl_Obj *part1Ptr in
Points to a Tcl value containing the array variable's name.  The name may
include a series of \fB::\fR namespace qualifiers to specify a variable in a
particular namespace.
.AP Tcl_Obj *part2Ptr in
If non-NULL, points to a Tcl value containing an array element name filter
specification.  The format and interpretation of \fIpart2Ptr\fR are determined
by the \fIflags\fR argument.
.AP int flags in
OR-ed combination of bits providing additional information.  See below for valid
values.
.AP Tcl_Obj *dictPtr in/out
Points to the dictionary value to be read or manipulated.  If \fIdictPtr\fR does
not already point to a dictionary value, an attempt will be made to convert it
to one.  \fBTcl_ArraySet\fR allows \fIdictPtr\fR to be NULL, in which case it
creates an empty array if the array does not already exist.
.AP Tcl_Obj *listPtr in/out
Points to the list value to be manipulated.  If \fIlistPtr\fR does not already
point to a list value, an attempt will be made to convert it to one.
.AP Tcl_Obj *stringPtr in/out
Points to the string value to be manipulated.
.AP int *intPtr out
Points to location where \fBTcl_ArrayExists\fR stores the array existence flag
or \fBTcl_ArraySize\fR stores the number of matching array elements.
.AP Tcl_ArraySearch search in/out
Token for tracking the progress of enumerating array elements.
.BE
.SH DESCRIPTION
.PP
These functions are used to create, modify, enumerate, analyze, read, and delete
Tcl array variables from C code.
.PP
The \fBTcl_ArraySet\fR, \fBTcl_ArrayUnset\fR, \fBTcl_ArrayGet\fR,
\fBTcl_ArrayNames\fR, \fBTcl_ArraySize\fR, \fBTcl_ArrayExists\fR, and
\fBTcl_ArrayStatistics\fR functions return \fBTCL_OK\fR on success and
\fBTCL_ERROR\fR on error.  If an error occurs, an error message is left in the
interpreter's result.  The possible errors are caused by format (invalid list,
dictionary, or regular expression) and traces (array, read, write, or unset).
.PP
Except for \fBTcl_ArraySet\fR, \fBTcl_ArraySearchStart\fR (not listed above),
and \fBTcl_ArrayStatistics\fR, the array functions do not consider it an error
for a namespace or variable to not exist or a variable to be a scalar or array
element instead of an array.  \fBTcl_ArraySet\fR allows the array variable to
not exist, in which case it creates it, but it returns \fBTCL_ERROR\fR in all
the other situations listed in the previous sentence.
.PP
Array functions trigger array traces in the same manner as the \fBarray\fR
command.  After the completion of any array traces, for each accessed array
element, \fBTcl_ArraySet\fR triggers write traces, \fBTcl_ArrayUnset\fR triggers
unset traces, and \fBTcl_ArrayGet\fR triggers read traces.  As discussed above,
an error encountered during the execution of a trace causes the function to
place error information in the interpreter's result and return \fBTCL_ERROR\fR.
.PP
The \fBTcl_ArraySet\fR, \fBTcl_ArrayUnset\fR, \fBTcl_ArrayGet\fR,
\fBTcl_ArrayNames\fR, \fBTcl_ArraySize\fR, \fBTcl_ArrayExists\fR,
\fBTcl_ArraySearchStart\fR, and \fBTcl_ArrayStatistics\fR functions accept a
\fIflags\fR argument to control the scope of the variable lookup and to specify
the interpretation of \fIpart2Ptr\fR.  It consists of an OR-ed combination of
zero or more of the following bits.
.TP
\fBTCL_GLOBAL_ONLY\fR
Under normal circumstances the functions look up variables as follows.  If a
procedure call is active in \fIinterp\fR, the array variable is looked up at the
current level of procedure call.  Otherwise, the array variable is looked up
first in the current namespace, then in the global namespace.  However, if this
bit is set in \fIflags\fR then the array variable is looked up only in the
global namespace even if there is a procedure call active.  If both
\fBTCL_GLOBAL_ONLY\fR and \fBTCL_NAMESPACE_ONLY\fR are given,
\fBTCL_GLOBAL_ONLY\fR is ignored.
.TP
\fBTCL_NAMESPACE_ONLY\fR
If this bit is set in \fIflags\fR then the array variable is looked up only in
the current namespace.  If a procedure is active, its variables are ignored, and
the global namespace is also ignored unless it is the current namespace.
.PP
With the exception of \fBTcl_ArraySet\fR and \fBTcl_ArrayStatistics\fR which do
not support filtering, the \fIflags\fR argument is also OR-ed with zero or one
of the following values to select the type of filtering applied by
\fIpart2Ptr\fR.  Setting more than one filter type may cause \fBTcl_Panic\fR to
be called.  The filter type is ignored if \fIpart2Ptr\fR is NULL, in which case
no filtering is applied and all array elements are matched.
.TP
\fBTCL_MATCH_EXACT\fR
\fIpart2Ptr\fR is an array element name.  The filter matches at most a single
array element whose name is exactly equal to the value of \fIpart2Ptr\fR.  If no
filter type is explicitly specified in \fIflags\fR, \fBTCL_MATCH_EXACT\fR is
used by default.  This differs from the \fBarray\fR commands which default to
\fB\-glob\fR.
.TP
\fBTCL_MATCH_GLOB\fR
\fIpart2Ptr\fR is a glob pattern, and it matches array element names according
to the rules of \fBstring match\fR.  \fIpart2Ptr\fR must match the entire array
element name from beginning to end.  To match substrings, place \fB*\fR at
either end of \fIpart2Ptr\fR.
.TP
\fBTCL_MATCH_REGEXP\fR
\fIpart2Ptr\fR is a regular expression, and it matches array element names
according to the rules of \fBregexp\fR.  Unless anchored by the \fB^\fR and
\fB$\fR constraints, \fIpart2Ptr\fR matches substrings.  Thus, an empty
\fIpart2Ptr\fR matches every possible array element name.  It is an error for
\fIpart2Ptr\fR to not be a valid regular expression, but this error is only
detected and reported when \fIpart1Ptr\fR names a non-empty array.
.PP
The \fBTcl_ArraySet\fR, \fBTcl_ArrayGet\fR, and \fBTcl_ArrayNames\fR functions
accept \fIdictPtr\fR and \fIlistPtr\fR arguments referencing dictionary and list
values, respectively.  If these values are not already dictionary and list
values, an attempt will be made to convert them.  If the conversion fails,
\fBTCL_ERROR\fR will be returned and an error message will be left in the
interpreter's result.
.PP
\fBTcl_ArraySet\fR sets the values of zero or more elements in the array named
by \fIpart1Ptr\fR.  If not NULL, \fIdictPtr\fR must be a valid dictionary, i.e.
it must be a list consisting of an even number of elements alternating between
key and value.  Each key in \fIdictPtr\fR is treated as an element name within
the array, and its associated value is used as the new value for that array
element.  If the variable named by \fIpart1Ptr\fR does not already exist and
\fIdictPtr\fR is empty or NULL, the variable is created as an empty array.  It
is an error for \fIpart1Ptr\fR to name a scalar (non-array) variable or an array
element, or for it to reference a nonexistent namespace.
.PP
\fBTcl_ArrayUnset\fR unsets all elements in the array named by \fIpart1Ptr\fR
that match the filter specified by \fIpart2Ptr\fR and \fIflags\fR.  It is not an
error for \fIpart1Ptr\fR to not name an array or for there to be no matching
elements in the array.  If \fIpart2Ptr\fR is NULL and \fIpart1Ptr\fR names an
array, the entire array is unset, and future calls to \fBTcl_ArrayExists\fR will
report it as not existing.  This is distinct from unsetting each element of the
array (e.g. if \fIpart2Ptr\fR is \fB*\fR and \fIflags\fR is
\fBTCL_MATCH_GLOB\fR), which does not cause future calls to
\fBTcl_ArrayExists\fR to report the array as not existing.
.PP
\fBTcl_ArrayGet\fR loads the contents of the array named by \fIpart1Ptr\fR into
the \fIdictPtr\fR dictionary object.  The array element names and values are
used as the dictionary keys and values, respectively.  If \fIpart2Ptr\fR is not
NULL, \fIpart2Ptr\fR and \fIflags\fR specify a filter used to limit which array
elements are loaded into \fIdictPtr\fR.  If \fIpart1Ptr\fR does not name an
array or if there are no matching elements, \fIdictPtr\fR is not modified.
If \fIdictPtr\fR is not empty prior to calling \fBTcl_ArrayGet\fR, the array
data is merged into \fIdictPtr\fR.  If an array element has the same name as an
existing dictionary key, the array element value replaces the existing
dictionary value.
.PP
\fBTcl_ArrayNames\fR loads the element names of the array named by
\fIpart1Ptr\fR into the \fIlistPtr\fR list object.  If \fIpart2Ptr\fR is not
NULL, \fIpart2Ptr\fR and \fIflags\fR specify a filter used to limit which array
element names are loaded into \fIlistPtr\fR.  If \fIpart1Ptr\fR does not name an
array or if there are no matching elements, \fIlistPtr\fR is not modified.  If
\fIlistPtr\fR is not empty prior to calling \fBTcl_ArrayNames\fR, the array
element names are appended to \fIlistPtr\fR.
.PP
\fBTcl_ArraySize\fR stores the number of elements in the array named by
\fIpart1Ptr\fR into the address \fIintPtr\fR.  If \fIpart2Ptr\fR is not NULL,
\fIpart2Ptr\fR and \fIflags\fR specify a filter used to limit which array
elements count toward the total number of elements.  
.PP
\fBTcl_ArrayExists\fR checks for the existence of an array variable named
\fIpart1Ptr\fR or (if \fIpart2Ptr\fR is not NULL) an array element within
matching the filter specified by \fIpart2Ptr\fR and \fIflags\fR.  If the array
variable or array element exists, the value \fB1\fR is stored into the location
\fIintPtr\fR, even if the array is empty.  If \fIpart1Ptr\fR does not name an
array variable (the variable does not exist, it is a scalar variable or array
element, or it references a nonexistent namespace), or if \fIpart2Ptr\fR is not
NULL and no elements match the filter, the value \fB0\fR is stored into the
location \fIintPtr\fR.
.PP
\fBTcl_ArraySearchStart\fR initiates an array search, i.e. an array element
enumeration yielding one element at a time.  \fIpart1Ptr\fR must name an array.
If \fIpart2Ptr\fR is not NULL, \fIpart2Ptr\fR and \fIflags\fR specify a filter
used to limit which array elements are included in the enumeration.  The return
value is a token used to track the progress of the search, and it is to be
passed to the \fBTcl_ArraySearchPeek\fR, \fBTcl_ArraySearchNext\fR, and
\fBTcl_ArraySearchDone\fR functions.  If there is an error looking up the
variable, executing an array trace, or validating a regular expression filter,
NULL is returned and error information is placed in the interpreter result.
.PP
\fBTcl_ArraySearchPeek\fR and \fBTcl_ArraySearchNext\fR return the next array
element name in the search identified by the \fIsearch\fR argument.  If there
are no remaining element names, NULL is returned.  \fBTcl_ArraySearchPeek\fR
does not advance the enumeration, whereas \fBTcl_ArraySearchNext\fR does, so
\fBTcl_ArraySearchPeek\fR will return the same value each time it is called if
there are no intervening calls to \fBTcl_ArraySearchNext\fR with the same
\fIsearch\fR argument.
.PP
\fBTcl_ArraySearchDone\fR completes the search identified by the \fIsearch\fR
argument and deallocates associated resources.  To avoid memory leaks,
\fBTcl_ArraySearchDone\fR must be called once for each search token returned by
\fBTcl_ArraySearchStart\fR.
.PP
\fBTcl_ArrayStatistics\fR produces statistics about the distribution of data
within the hash table underlying the array named by \fIpart1Ptr\fR.  This
information includes the number of entries in the table, the number of buckets,
and the utilization of the buckets.  The statistics information is appended to
the string value \fIstringPtr\fR.
.SH EXAMPLES
.PP
Common initialization used for subsequent examples:
.PP
.CS
int size, i;
Tcl_Obj *obj, **objPtr;
Tcl_Obj *varNameObj = Tcl_NewStringObj("colorcount", -1);
Tcl_Channel outChan = Tcl_GetChannel(interp, "stdout", NULL);
.CE
.PP
Create an array:
.PP
.CS
obj = Tcl_NewStringObj(
        " red   1"
        " green 5"
        " blue  4"
        " white 9", -1);
\fBTcl_ArraySet\fR(interp, varNameObj, obj, 0);
.CE
.PP
Load the full contents of an array into a single object:
.PP
.CS
Tcl_SetStringObj(obj, NULL, 0);
\fBTcl_ArrayGet\fR(interp, varNameObj, NULL, obj, 0);
Tcl_ListObjGetElements(interp, obj, &size, &objPtr);
for (i = 0; i < size; i += 2, objPtr += 2) {
    Tcl_WriteChars(outChan, "Color: ", -1);
    Tcl_WriteObj(outChan, objPtr[0]);
    Tcl_WriteChars(outChan, " Count: ", -1);
    Tcl_WriteObj(outChan, objPtr[1]);
    Tcl_WriteChars(outChan, "\\n", -1);
}
  \fB\(->\fR Color: blue Count: 4
    Color: white Count: 9
    Color: green Count: 5
    Color: red Count: 1
.CE
.PP
Get an array element name list then individually look up each element value:
.PP
.CS
Tcl_SetStringObj(obj, NULL, 0);
\fBTcl_ArrayNames\fR(interp, varNameObj, NULL, obj, 0);
Tcl_ListObjGetElements(interp, obj, &size, &objPtr);
for (i = 0; i < size; ++i, ++objPtr) {
    Tcl_WriteChars(outChan, "Color: ", -1);
    Tcl_WriteObj(outChan, *objPtr);
    Tcl_WriteChars(outChan, " Count: ", -1);
    Tcl_WriteObj(outChan, Tcl_ObjGetVar2(
            interp, varNameObj, *objPtr, 0));
    Tcl_WriteChars(outChan, "\\n", -1);
}
  \fB\(->\fR Color: blue Count: 4
    Color: white Count: 9
    Color: green Count: 5
    Color: red Count: 1
.CE
.PP
Get array hash table statistics:
.PP
.CS
Tcl_SetStringObj(obj, NULL, 0);
\fBTcl_ArrayStatistics\fR(interp, varNameObj, obj, 0);
Tcl_WriteObj(outChan, obj);
Tcl_WriteChars(outChan, "\\n", -1);
  \fB\(->\fR 4 entries in table, 4 buckets
    number of buckets with 0 entries: 1
    number of buckets with 1 entries: 2
    number of buckets with 2 entries: 1
    number of buckets with 3 entries: 0
    number of buckets with 4 entries: 0
    number of buckets with 5 entries: 0
    number of buckets with 6 entries: 0
    number of buckets with 7 entries: 0
    number of buckets with 8 entries: 0
    number of buckets with 9 entries: 0
    number of buckets with 10 or more entries: 0
    average search distance for entry: 1.2
.CE
.SH "SEE ALSO"
array(n), Tcl_NewObj(3), Tcl_NewListObj(3), Tcl_NewDictObj(3),
Tcl_ObjGetVar2(3), Tcl_GetObjResult(3), Tcl_TraceVar2(3)
.SH KEYWORDS
array, dict, dict value, dictionary, get variable, hash table, iteration,
interpreter, set, unset, value, variable

Changes to doc/array.n.

18
19
20
21
22
23
24




































25
26
27
28
29
30
31
.PP
This command performs one of several operations on the
variable given by \fIarrayName\fR.
Unless otherwise specified for individual commands below,
\fIarrayName\fR must be the name of an existing array variable.
The \fIoption\fR argument determines what action is carried
out by the command.




































The legal \fIoptions\fR (which may be abbreviated) are:
.TP
\fBarray anymore \fIarrayName searchId\fR
Returns 1 if there are any more elements left to be processed
in an array search, 0 if all elements have already been
returned.
\fISearchId\fR indicates which search on \fIarrayName\fR to







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
.PP
This command performs one of several operations on the
variable given by \fIarrayName\fR.
Unless otherwise specified for individual commands below,
\fIarrayName\fR must be the name of an existing array variable.
The \fIoption\fR argument determines what action is carried
out by the command.
.PP
Many array commands accept \fImode\fR and \fIpattern\fR arguments.  These
arguments specify a filter to limit which array elements are included in the
operation.  If neither argument is specified, no filtering is applied, and the
entire array is processed.  If only one argument is specified, it is used as the
\fIpattern\fR argument, and \fB\-glob\fR is used as the default value for
\fImode\fR.  If both arguments are specified, the first is used as \fImode\fR
and the second as \fIpattern\fR.  The \fImode\fR argument designates which
matching rules to use to match \fIpattern\fR against the names of the elements
in the array.  The legal \fImode\fR values are:
.TP
\fB\-exact\fR
.
\fIPattern\fR is a literal string that is compared for exact equality against
each array element name.  This mode has limited utility because all uses of the
\fBarray\fR command with \fB-exact\fR matching can be implemented by operating
directly on the array element with standard \fIname\fB(\fIindex\fB)\fR notation.
.TP
\fB\-glob\fR
.
\fIPattern\fR is a glob-style pattern which is matched against each array
element name using the same rules as the \fBstring match\fR command.
\fIPattern\fR must match the entire array element name from beginning to end.
To match substrings, place \fB*\fR at either end of \fIpattern\fR.  This mode is
the default if no \fImode\fR is specified.
.TP
\fB\-regexp\fR
.
\fIPattern\fR is treated as a regular expression and matched against each array
element name using the rules described in the \fBre_syntax\fR reference page.
Unless anchored by the \fB^\fR and \fB$\fR constraints, \fIpattern\fR matches
substrings.  Thus, an empty \fIpattern\fR matches every possible array element
name.  It is an error for \fIpattern\fR to not be a valid regular expression,
but this error condition is only detected and reported when \fIarrayName\fR is a
non-empty array.
.PP
The legal \fIoptions\fR (which may be abbreviated) are:
.TP
\fBarray anymore \fIarrayName searchId\fR
Returns 1 if there are any more elements left to be processed
in an array search, 0 if all elements have already been
returned.
\fISearchId\fR indicates which search on \fIarrayName\fR to
39
40
41
42
43
44
45
46
47
48



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
\fBarray donesearch \fIarrayName searchId\fR
This command terminates an array search and destroys all the
state associated with that search.  \fISearchId\fR indicates
which search on \fIarrayName\fR to destroy, and must have
been the return value from a previous invocation of
\fBarray startsearch\fR.  Returns an empty string.
.TP
\fBarray exists \fIarrayName\fR
Returns 1 if \fIarrayName\fR is an array variable, 0 if there
is no variable by that name or if it is a scalar variable.



.TP
\fBarray get \fIarrayName\fR ?\fIpattern\fR?
Returns a list containing pairs of elements.  The first
element in each pair is the name of an element in \fIarrayName\fR
and the second element of each pair is the value of the
array element.  The order of the pairs is undefined.
If \fIpattern\fR is not specified, then all of the elements of the
array are included in the result.
If \fIpattern\fR is specified, then only those elements whose names
match \fIpattern\fR (using the matching rules of
\fBstring match\fR) are included.
If \fIarrayName\fR is not the name of an array variable, or if
the array contains no elements, then an empty list is returned.
If traces on the array modify the list of elements, the elements
returned are those that exist both before and after the call to
\fBarray get\fR.
.TP
\fBarray names \fIarrayName\fR ?\fImode\fR? ?\fIpattern\fR?
Returns a list containing the names of all of the elements in
the array that match \fIpattern\fR.  \fIMode\fR may be one of
\fB\-exact\fR, \fB\-glob\fR, or \fB\-regexp\fR.  If specified, \fImode\fR
designates which matching rules to use to match \fIpattern\fR against
the names of the elements in the array.  If not specified, \fImode\fR
defaults to \fB\-glob\fR.  See the documentation for \fBstring match\fR
for information on glob style matching, and the documentation for
\fBregexp\fR for information on regexp matching.
If \fIpattern\fR is omitted then the command returns all of
the element names in the array.  If there are no (matching) elements
in the array, or if \fIarrayName\fR is not the name of an array
variable, then an empty string is returned.
.TP
\fBarray nextelement \fIarrayName searchId\fR
Returns the name of the next element in \fIarrayName\fR, or
an empty string if all elements of \fIarrayName\fR have
already been returned in this search.  The \fIsearchId\fR







|


>
>
>

|




|
|
<
<
<








|
<
|
|
<
<
<
<
|







75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95



96
97
98
99
100
101
102
103
104

105
106




107
108
109
110
111
112
113
114
\fBarray donesearch \fIarrayName searchId\fR
This command terminates an array search and destroys all the
state associated with that search.  \fISearchId\fR indicates
which search on \fIarrayName\fR to destroy, and must have
been the return value from a previous invocation of
\fBarray startsearch\fR.  Returns an empty string.
.TP
\fBarray exists \fIarrayName\fR ?\fImode\fR? ?\fIpattern\fR?
Returns 1 if \fIarrayName\fR is an array variable, 0 if there
is no variable by that name or if it is a scalar variable.
If \fIpattern\fR is specified, this command instead checks if one or more
elements of \fIarrayName\fR match the filter defined by \fImode\fR and
\fIpattern\fR, returning 1 or 0 if the match succeeds or fails, respectively.
.TP
\fBarray get \fIarrayName\fR ?\fImode\fR? ?\fIpattern\fR?
Returns a list containing pairs of elements.  The first
element in each pair is the name of an element in \fIarrayName\fR
and the second element of each pair is the value of the
array element.  The order of the pairs is undefined.
The \fImode\fR and \fIpattern\fR arguments can be used to limit which array
elements are included in the result.



If \fIarrayName\fR is not the name of an array variable, or if
the array contains no elements, then an empty list is returned.
If traces on the array modify the list of elements, the elements
returned are those that exist both before and after the call to
\fBarray get\fR.
.TP
\fBarray names \fIarrayName\fR ?\fImode\fR? ?\fIpattern\fR?
Returns a list containing the names of all of the elements in
the array.

The \fImode\fR and \fIpattern\fR arguments can be used to limit which array
element names are included in the result.




If there are no (matching) elements
in the array, or if \fIarrayName\fR is not the name of an array
variable, then an empty string is returned.
.TP
\fBarray nextelement \fIarrayName searchId\fR
Returns the name of the next element in \fIarrayName\fR, or
an empty string if all elements of \fIarrayName\fR have
already been returned in this search.  The \fIsearchId\fR
95
96
97
98
99
100
101
102
103
104


105
106
107
108
109
110
111


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

131
132
133
134
135
136
137
138
Each odd-numbered element in \fIlist\fR is treated as an element
name within \fIarrayName\fR, and the following element in \fIlist\fR
is used as a new value for that array element.
If the variable \fIarrayName\fR does not already exist
and \fIlist\fR is empty,
\fIarrayName\fR is created with an empty array value.
.TP
\fBarray size \fIarrayName\fR
Returns a decimal string giving the number of elements in the
array.


If \fIarrayName\fR is not the name of an array then 0 is returned.
.TP
\fBarray startsearch \fIarrayName\fR
This command initializes an element-by-element search through the
array given by \fIarrayName\fR, such that invocations of the
\fBarray nextelement\fR command will return the names of the
individual elements in the array.


When the search has been completed, the \fBarray donesearch\fR
command should be invoked.
The return value is a
search identifier that must be used in \fBarray nextelement\fR
and \fBarray donesearch\fR commands; it allows multiple
searches to be underway simultaneously for the same array.
It is currently more efficient and easier to use either the \fBarray
get\fR or \fBarray names\fR, together with \fBforeach\fR, to iterate
over all but very large arrays.  See the examples below for how to do
this.
.TP
\fBarray statistics \fIarrayName\fR
Returns statistics about the distribution of data within the hashtable
that represents the array.  This information includes the number of
entries in the table, the number of buckets, and the utilization of
the buckets.
.TP
\fBarray unset \fIarrayName\fR ?\fIpattern\fR?
Unsets all of the elements in the array that match \fIpattern\fR (using the

matching rules of \fBstring match\fR).  If \fIarrayName\fR is not the name
of an array variable or there are no matching elements in the array, no
error will be raised.  If \fIpattern\fR is omitted and \fIarrayName\fR is
an array variable, then the command unsets the entire array.
The command always returns an empty string.
.SH EXAMPLES
.CS
\fBarray set\fR colorcount {







|
|

>
>


|




>
>

















|
|
>
|







126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
Each odd-numbered element in \fIlist\fR is treated as an element
name within \fIarrayName\fR, and the following element in \fIlist\fR
is used as a new value for that array element.
If the variable \fIarrayName\fR does not already exist
and \fIlist\fR is empty,
\fIarrayName\fR is created with an empty array value.
.TP
\fBarray size \fIarrayName\fR ?\fImode\fR? ?\fIpattern\fR?
Returns an integer giving the number of elements in the
array.
The \fImode\fR and \fIpattern\fR arguments can be used to limit which array
elements are counted toward the result.
If \fIarrayName\fR is not the name of an array then 0 is returned.
.TP
\fBarray startsearch \fIarrayName\fR ?\fImode\fR? ?\fIpattern\fR?
This command initializes an element-by-element search through the
array given by \fIarrayName\fR, such that invocations of the
\fBarray nextelement\fR command will return the names of the
individual elements in the array.
The \fImode\fR and \fIpattern\fR arguments can be used to limit which array
element names are returned by future invocations of \fBarray nextelement\fR.
When the search has been completed, the \fBarray donesearch\fR
command should be invoked.
The return value is a
search identifier that must be used in \fBarray nextelement\fR
and \fBarray donesearch\fR commands; it allows multiple
searches to be underway simultaneously for the same array.
It is currently more efficient and easier to use either the \fBarray
get\fR or \fBarray names\fR, together with \fBforeach\fR, to iterate
over all but very large arrays.  See the examples below for how to do
this.
.TP
\fBarray statistics \fIarrayName\fR
Returns statistics about the distribution of data within the hashtable
that represents the array.  This information includes the number of
entries in the table, the number of buckets, and the utilization of
the buckets.
.TP
\fBarray unset \fIarrayName\fR ?\fImode\fR? ?\fIpattern\fR?
Unsets all of the elements in the array that match the filter specified by the
\fImode\fR and \fIpattern\fR arguments.
If \fIarrayName\fR is not the name
of an array variable or there are no matching elements in the array, no
error will be raised.  If \fIpattern\fR is omitted and \fIarrayName\fR is
an array variable, then the command unsets the entire array.
The command always returns an empty string.
.SH EXAMPLES
.CS
\fBarray set\fR colorcount {

Changes to generic/tcl.decls.

2321
2322
2323
2324
2325
2326
2327













































2328
2329
2330
2331
2332
2333
2334
# TIP #400
declare 630 {
    void Tcl_ZlibStreamSetCompressionDictionary(Tcl_ZlibStream zhandle,
	    Tcl_Obj *compressionDictionaryObj)
}

# ----- BASELINE -- FOR -- 8.6.0 ----- #














































##############################################################################

# Define the platform specific public Tcl interface. These functions are only
# available on the designated platform.

interface tclPlat







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
# TIP #400
declare 630 {
    void Tcl_ZlibStreamSetCompressionDictionary(Tcl_ZlibStream zhandle,
	    Tcl_Obj *compressionDictionaryObj)
}

# ----- BASELINE -- FOR -- 8.6.0 ----- #

# TIP #XXX
declare 631 {
    int Tcl_ArraySet(Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *dictPtr,
	int flags)
}
declare 632 {
    int Tcl_ArrayUnset(Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr,
	int flags)
}
declare 633 {
    int Tcl_ArrayGet(Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr,
	Tcl_Obj *dictPtr, int flags)
}
declare 634 {
    int Tcl_ArrayNames(Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr,
	Tcl_Obj *listPtr, int flags)
}
declare 635 {
    int Tcl_ArraySize(Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr,
	int *intPtr, int flags)
}
declare 636 {
    int Tcl_ArrayExists(Tcl_Interp *interp, Tcl_Obj *part1Ptr,
	Tcl_Obj *part2Ptr, int *intPtr, int flags)
}
declare 637 {
    Tcl_ArraySearch Tcl_ArraySearchStart(Tcl_Interp *interp,
	Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr, int flags)
}
declare 638 {
    int Tcl_ArraySearchPeek(Tcl_Interp *interp, Tcl_ArraySearch search,
	Tcl_Obj **keyPtrPtr, Tcl_Obj **valuePtrPtr)
}
declare 639 {
    int Tcl_ArraySearchNext(Tcl_Interp *interp, Tcl_ArraySearch search,
	Tcl_Obj **keyPtrPtr, Tcl_Obj **valuePtrPtr)
}
declare 640 {
    int Tcl_ArraySearchDone(Tcl_Interp *interp, Tcl_ArraySearch search)
}
declare 641 {
   int Tcl_ArrayStatistics(Tcl_Interp *interp, Tcl_Obj *part1Ptr,
         Tcl_Obj *stringPtr, int flags)
}

##############################################################################

# Define the platform specific public Tcl interface. These functions are only
# available on the designated platform.

interface tclPlat

Changes to generic/tcl.h.

528
529
530
531
532
533
534

535
536
537
538
539
540
541
#else
    int errorLineDontUse; /* Don't use in extensions! */
#endif
}
#endif /* TCL_NO_DEPRECATED */
Tcl_Interp;


typedef struct Tcl_AsyncHandler_ *Tcl_AsyncHandler;
typedef struct Tcl_Channel_ *Tcl_Channel;
typedef struct Tcl_ChannelTypeVersion_ *Tcl_ChannelTypeVersion;
typedef struct Tcl_Command_ *Tcl_Command;
typedef struct Tcl_Condition_ *Tcl_Condition;
typedef struct Tcl_Dict_ *Tcl_Dict;
typedef struct Tcl_EncodingState_ *Tcl_EncodingState;







>







528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#else
    int errorLineDontUse; /* Don't use in extensions! */
#endif
}
#endif /* TCL_NO_DEPRECATED */
Tcl_Interp;

typedef struct Tcl_ArraySearch_ *Tcl_ArraySearch;
typedef struct Tcl_AsyncHandler_ *Tcl_AsyncHandler;
typedef struct Tcl_Channel_ *Tcl_Channel;
typedef struct Tcl_ChannelTypeVersion_ *Tcl_ChannelTypeVersion;
typedef struct Tcl_Command_ *Tcl_Command;
typedef struct Tcl_Condition_ *Tcl_Condition;
typedef struct Tcl_Dict_ *Tcl_Dict;
typedef struct Tcl_EncodingState_ *Tcl_EncodingState;
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088

1089
1090
1091
1092
1093
1094
1095
1096
1097
1098

1099
1100
1101
1102
1103


1104
1105
1106


1107
1108
1109
1110
1111
1112
1113
#define TCL_VOLATILE		((Tcl_FreeProc *) 1)
#define TCL_STATIC		((Tcl_FreeProc *) 0)
#define TCL_DYNAMIC		((Tcl_FreeProc *) 3)

/*
 * Flag values passed to variable-related functions.
 * WARNING: these bit choices must not conflict with the bit choice for
 * TCL_CANCEL_UNWIND, above.
 */


#define TCL_GLOBAL_ONLY		 1
#define TCL_NAMESPACE_ONLY	 2
#define TCL_APPEND_VALUE	 4
#define TCL_LIST_ELEMENT	 8
#define TCL_TRACE_READS		 0x10
#define TCL_TRACE_WRITES	 0x20
#define TCL_TRACE_UNSETS	 0x40
#define TCL_TRACE_DESTROYED	 0x80
#define TCL_INTERP_DESTROYED	 0x100
#define TCL_LEAVE_ERR_MSG	 0x200

#define TCL_TRACE_ARRAY		 0x800
#ifndef TCL_REMOVE_OBSOLETE_TRACES
/* Required to support old variable/vdelete/vinfo traces. */
#define TCL_TRACE_OLD_STYLE	 0x1000
#endif


/* Indicate the semantics of the result of a trace. */
#define TCL_TRACE_RESULT_DYNAMIC 0x8000
#define TCL_TRACE_RESULT_OBJECT  0x10000



/*
 * Flag values for ensemble commands.
 */

#define TCL_ENSEMBLE_PREFIX 0x02/* Flag value to say whether to allow
				 * unambiguous prefixes of commands or to







|


>










>





>
>



>
>







1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
#define TCL_VOLATILE		((Tcl_FreeProc *) 1)
#define TCL_STATIC		((Tcl_FreeProc *) 0)
#define TCL_DYNAMIC		((Tcl_FreeProc *) 3)

/*
 * Flag values passed to variable-related functions.
 * WARNING: these bit choices must not conflict with the bit choice for
 * TCL_CANCEL_UNWIND, above, or TCL_VAR_CREATE (0x4000) in tclVar.c.
 */

#define TCL_MATCH_EXACT		 0
#define TCL_GLOBAL_ONLY		 1
#define TCL_NAMESPACE_ONLY	 2
#define TCL_APPEND_VALUE	 4
#define TCL_LIST_ELEMENT	 8
#define TCL_TRACE_READS		 0x10
#define TCL_TRACE_WRITES	 0x20
#define TCL_TRACE_UNSETS	 0x40
#define TCL_TRACE_DESTROYED	 0x80
#define TCL_INTERP_DESTROYED	 0x100
#define TCL_LEAVE_ERR_MSG	 0x200
/* Value 0x400 is reserved for TCL_PARSE_PART1, conditionally defined below. */
#define TCL_TRACE_ARRAY		 0x800
#ifndef TCL_REMOVE_OBSOLETE_TRACES
/* Required to support old variable/vdelete/vinfo traces. */
#define TCL_TRACE_OLD_STYLE	 0x1000
#endif
#define TCL_MATCH_REGEXP	 0x2000
/* Value 0x4000 is reserved for TCL_VAR_CREATE, defined in tclVar.c. */
/* Indicate the semantics of the result of a trace. */
#define TCL_TRACE_RESULT_DYNAMIC 0x8000
#define TCL_TRACE_RESULT_OBJECT  0x10000
#define TCL_MATCH_GLOB		 0x20000
/* Value 0x100000 is reserved for TCL_CANCEL_UNWIND, defined above. */

/*
 * Flag values for ensemble commands.
 */

#define TCL_ENSEMBLE_PREFIX 0x02/* Flag value to say whether to allow
				 * unambiguous prefixes of commands or to

Changes to generic/tclDecls.h.

1812
1813
1814
1815
1816
1817
1818








































1819
1820
1821
1822
1823
1824
1825
/* 629 */
EXTERN int		Tcl_FSUnloadFile(Tcl_Interp *interp,
				Tcl_LoadHandle handlePtr);
/* 630 */
EXTERN void		Tcl_ZlibStreamSetCompressionDictionary(
				Tcl_ZlibStream zhandle,
				Tcl_Obj *compressionDictionaryObj);









































typedef struct {
    const struct TclPlatStubs *tclPlatStubs;
    const struct TclIntStubs *tclIntStubs;
    const struct TclIntPlatStubs *tclIntPlatStubs;
} TclStubHooks;








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
/* 629 */
EXTERN int		Tcl_FSUnloadFile(Tcl_Interp *interp,
				Tcl_LoadHandle handlePtr);
/* 630 */
EXTERN void		Tcl_ZlibStreamSetCompressionDictionary(
				Tcl_ZlibStream zhandle,
				Tcl_Obj *compressionDictionaryObj);
/* 631 */
EXTERN int		Tcl_ArraySet(Tcl_Interp *interp, Tcl_Obj *part1Ptr,
				Tcl_Obj *dictPtr, int flags);
/* 632 */
EXTERN int		Tcl_ArrayUnset(Tcl_Interp *interp, Tcl_Obj *part1Ptr,
				Tcl_Obj *part2Ptr, int flags);
/* 633 */
EXTERN int		Tcl_ArrayGet(Tcl_Interp *interp, Tcl_Obj *part1Ptr,
				Tcl_Obj *part2Ptr, Tcl_Obj *dictPtr,
				int flags);
/* 634 */
EXTERN int		Tcl_ArrayNames(Tcl_Interp *interp, Tcl_Obj *part1Ptr,
				Tcl_Obj *part2Ptr, Tcl_Obj *listPtr,
				int flags);
/* 635 */
EXTERN int		Tcl_ArraySize(Tcl_Interp *interp, Tcl_Obj *part1Ptr,
				Tcl_Obj *part2Ptr, int *intPtr, int flags);
/* 636 */
EXTERN int		Tcl_ArrayExists(Tcl_Interp *interp,
				Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr,
				int *intPtr, int flags);
/* 637 */
EXTERN Tcl_ArraySearch	Tcl_ArraySearchStart(Tcl_Interp *interp,
				Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr,
				int flags);
/* 638 */
EXTERN int		Tcl_ArraySearchPeek(Tcl_Interp *interp,
				Tcl_ArraySearch search, Tcl_Obj **keyPtrPtr,
				Tcl_Obj **valuePtrPtr);
/* 639 */
EXTERN int		Tcl_ArraySearchNext(Tcl_Interp *interp,
				Tcl_ArraySearch search, Tcl_Obj **keyPtrPtr,
				Tcl_Obj **valuePtrPtr);
/* 640 */
EXTERN int		Tcl_ArraySearchDone(Tcl_Interp *interp,
				Tcl_ArraySearch search);
/* 641 */
EXTERN int		Tcl_ArrayStatistics(Tcl_Interp *interp,
				Tcl_Obj *part1Ptr, Tcl_Obj *stringPtr,
				int flags);

typedef struct {
    const struct TclPlatStubs *tclPlatStubs;
    const struct TclIntStubs *tclIntStubs;
    const struct TclIntPlatStubs *tclIntPlatStubs;
} TclStubHooks;

2478
2479
2480
2481
2482
2483
2484











2485
2486
2487
2488
2489
2490
2491
    int (*tcl_CloseEx) (Tcl_Interp *interp, Tcl_Channel chan, int flags); /* 624 */
    int (*tcl_NRExprObj) (Tcl_Interp *interp, Tcl_Obj *objPtr, Tcl_Obj *resultPtr); /* 625 */
    int (*tcl_NRSubstObj) (Tcl_Interp *interp, Tcl_Obj *objPtr, int flags); /* 626 */
    int (*tcl_LoadFile) (Tcl_Interp *interp, Tcl_Obj *pathPtr, const char *const symv[], int flags, void *procPtrs, Tcl_LoadHandle *handlePtr); /* 627 */
    void * (*tcl_FindSymbol) (Tcl_Interp *interp, Tcl_LoadHandle handle, const char *symbol); /* 628 */
    int (*tcl_FSUnloadFile) (Tcl_Interp *interp, Tcl_LoadHandle handlePtr); /* 629 */
    void (*tcl_ZlibStreamSetCompressionDictionary) (Tcl_ZlibStream zhandle, Tcl_Obj *compressionDictionaryObj); /* 630 */











} TclStubs;

extern const TclStubs *tclStubsPtr;

#ifdef __cplusplus
}
#endif







>
>
>
>
>
>
>
>
>
>
>







2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
    int (*tcl_CloseEx) (Tcl_Interp *interp, Tcl_Channel chan, int flags); /* 624 */
    int (*tcl_NRExprObj) (Tcl_Interp *interp, Tcl_Obj *objPtr, Tcl_Obj *resultPtr); /* 625 */
    int (*tcl_NRSubstObj) (Tcl_Interp *interp, Tcl_Obj *objPtr, int flags); /* 626 */
    int (*tcl_LoadFile) (Tcl_Interp *interp, Tcl_Obj *pathPtr, const char *const symv[], int flags, void *procPtrs, Tcl_LoadHandle *handlePtr); /* 627 */
    void * (*tcl_FindSymbol) (Tcl_Interp *interp, Tcl_LoadHandle handle, const char *symbol); /* 628 */
    int (*tcl_FSUnloadFile) (Tcl_Interp *interp, Tcl_LoadHandle handlePtr); /* 629 */
    void (*tcl_ZlibStreamSetCompressionDictionary) (Tcl_ZlibStream zhandle, Tcl_Obj *compressionDictionaryObj); /* 630 */
    int (*tcl_ArraySet) (Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *dictPtr, int flags); /* 631 */
    int (*tcl_ArrayUnset) (Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr, int flags); /* 632 */
    int (*tcl_ArrayGet) (Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr, Tcl_Obj *dictPtr, int flags); /* 633 */
    int (*tcl_ArrayNames) (Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr, Tcl_Obj *listPtr, int flags); /* 634 */
    int (*tcl_ArraySize) (Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr, int *intPtr, int flags); /* 635 */
    int (*tcl_ArrayExists) (Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr, int *intPtr, int flags); /* 636 */
    Tcl_ArraySearch (*tcl_ArraySearchStart) (Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr, int flags); /* 637 */
    int (*tcl_ArraySearchPeek) (Tcl_Interp *interp, Tcl_ArraySearch search, Tcl_Obj **keyPtrPtr, Tcl_Obj **valuePtrPtr); /* 638 */
    int (*tcl_ArraySearchNext) (Tcl_Interp *interp, Tcl_ArraySearch search, Tcl_Obj **keyPtrPtr, Tcl_Obj **valuePtrPtr); /* 639 */
    int (*tcl_ArraySearchDone) (Tcl_Interp *interp, Tcl_ArraySearch search); /* 640 */
    int (*tcl_ArrayStatistics) (Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *stringPtr, int flags); /* 641 */
} TclStubs;

extern const TclStubs *tclStubsPtr;

#ifdef __cplusplus
}
#endif
3770
3771
3772
3773
3774
3775
3776






















3777
3778
3779
3780
3781
3782
3783
	(tclStubsPtr->tcl_LoadFile) /* 627 */
#define Tcl_FindSymbol \
	(tclStubsPtr->tcl_FindSymbol) /* 628 */
#define Tcl_FSUnloadFile \
	(tclStubsPtr->tcl_FSUnloadFile) /* 629 */
#define Tcl_ZlibStreamSetCompressionDictionary \
	(tclStubsPtr->tcl_ZlibStreamSetCompressionDictionary) /* 630 */























#endif /* defined(USE_TCL_STUBS) */

/* !END!: Do not edit above this line. */

#if defined(USE_TCL_STUBS)
#   undef Tcl_CreateInterp







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
	(tclStubsPtr->tcl_LoadFile) /* 627 */
#define Tcl_FindSymbol \
	(tclStubsPtr->tcl_FindSymbol) /* 628 */
#define Tcl_FSUnloadFile \
	(tclStubsPtr->tcl_FSUnloadFile) /* 629 */
#define Tcl_ZlibStreamSetCompressionDictionary \
	(tclStubsPtr->tcl_ZlibStreamSetCompressionDictionary) /* 630 */
#define Tcl_ArraySet \
	(tclStubsPtr->tcl_ArraySet) /* 631 */
#define Tcl_ArrayUnset \
	(tclStubsPtr->tcl_ArrayUnset) /* 632 */
#define Tcl_ArrayGet \
	(tclStubsPtr->tcl_ArrayGet) /* 633 */
#define Tcl_ArrayNames \
	(tclStubsPtr->tcl_ArrayNames) /* 634 */
#define Tcl_ArraySize \
	(tclStubsPtr->tcl_ArraySize) /* 635 */
#define Tcl_ArrayExists \
	(tclStubsPtr->tcl_ArrayExists) /* 636 */
#define Tcl_ArraySearchStart \
	(tclStubsPtr->tcl_ArraySearchStart) /* 637 */
#define Tcl_ArraySearchPeek \
	(tclStubsPtr->tcl_ArraySearchPeek) /* 638 */
#define Tcl_ArraySearchNext \
	(tclStubsPtr->tcl_ArraySearchNext) /* 639 */
#define Tcl_ArraySearchDone \
	(tclStubsPtr->tcl_ArraySearchDone) /* 640 */
#define Tcl_ArrayStatistics \
	(tclStubsPtr->tcl_ArrayStatistics) /* 641 */

#endif /* defined(USE_TCL_STUBS) */

/* !END!: Do not edit above this line. */

#if defined(USE_TCL_STUBS)
#   undef Tcl_CreateInterp

Changes to generic/tclExecute.c.

4030
4031
4032
4033
4034
4035
4036
4037

4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
	part2Ptr = OBJ_AT_TOS;
	arrayPtr = LOCAL(opnd);
	while (TclIsVarLink(arrayPtr)) {
	    arrayPtr = arrayPtr->value.linkPtr;
	}
	TRACE(("%s %u \"%.30s\" => ",
		(flags ? "normal" : "noerr"), opnd, O2S(part2Ptr)));
	if (TclIsVarArray(arrayPtr) && !UnsetTraced(arrayPtr)) {

	    varPtr = VarHashFindVar(arrayPtr->value.tablePtr, part2Ptr);
	    if (varPtr && TclIsVarDirectUnsettable(varPtr)) {
		/*
		 * No nasty traces and element exists, so we can proceed to
		 * unset it. Might still not exist though...
		 */

		if (!TclIsVarUndefined(varPtr)) {
		    TclDecrRefCount(varPtr->value.objPtr);
		    TclSetVarUndefined(varPtr);
		    TclClearVarNamespaceVar(varPtr);
		    TclCleanupVar(varPtr, arrayPtr);







|
>



|
|







4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
	part2Ptr = OBJ_AT_TOS;
	arrayPtr = LOCAL(opnd);
	while (TclIsVarLink(arrayPtr)) {
	    arrayPtr = arrayPtr->value.linkPtr;
	}
	TRACE(("%s %u \"%.30s\" => ",
		(flags ? "normal" : "noerr"), opnd, O2S(part2Ptr)));
	if (TclIsVarArray(arrayPtr) && !UnsetTraced(arrayPtr)
		&& !TclIsVarArraySearched(arrayPtr)) {
	    varPtr = VarHashFindVar(arrayPtr->value.tablePtr, part2Ptr);
	    if (varPtr && TclIsVarDirectUnsettable(varPtr)) {
		/*
		 * No nasty traces or searchesw and element exists, so we can
		 * proceed to unset it. Might still not exist though...
		 */

		if (!TclIsVarUndefined(varPtr)) {
		    TclDecrRefCount(varPtr->value.objPtr);
		    TclSetVarUndefined(varPtr);
		    TclClearVarNamespaceVar(varPtr);
		    TclCleanupVar(varPtr, arrayPtr);

Changes to generic/tclInt.h.

800
801
802
803
804
805
806

807
808
809
810
811
812
813
 * MODULE_SCOPE int	TclIsVarLink(Var *varPtr);
 * MODULE_SCOPE int	TclIsVarArray(Var *varPtr);
 * MODULE_SCOPE int	TclIsVarUndefined(Var *varPtr);
 * MODULE_SCOPE int	TclIsVarArrayElement(Var *varPtr);
 * MODULE_SCOPE int	TclIsVarTemporary(Var *varPtr);
 * MODULE_SCOPE int	TclIsVarArgument(Var *varPtr);
 * MODULE_SCOPE int	TclIsVarResolved(Var *varPtr);

 */

#define TclIsVarScalar(varPtr) \
    !((varPtr)->flags & (VAR_ARRAY|VAR_LINK))

#define TclIsVarLink(varPtr) \
    ((varPtr)->flags & VAR_LINK)







>







800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
 * MODULE_SCOPE int	TclIsVarLink(Var *varPtr);
 * MODULE_SCOPE int	TclIsVarArray(Var *varPtr);
 * MODULE_SCOPE int	TclIsVarUndefined(Var *varPtr);
 * MODULE_SCOPE int	TclIsVarArrayElement(Var *varPtr);
 * MODULE_SCOPE int	TclIsVarTemporary(Var *varPtr);
 * MODULE_SCOPE int	TclIsVarArgument(Var *varPtr);
 * MODULE_SCOPE int	TclIsVarResolved(Var *varPtr);
 * MODULE_SCOPE int	TclIsVarArraySearched(Var *varPtr);
 */

#define TclIsVarScalar(varPtr) \
    !((varPtr)->flags & (VAR_ARRAY|VAR_LINK))

#define TclIsVarLink(varPtr) \
    ((varPtr)->flags & VAR_LINK)
841
842
843
844
845
846
847



848
849
850
851
852
853
854

#define TclIsVarInHash(varPtr) \
    ((varPtr)->flags & VAR_IN_HASHTABLE)

#define TclIsVarDeadHash(varPtr) \
    ((varPtr)->flags & VAR_DEAD_HASH)




#define TclGetVarNsPtr(varPtr) \
    (TclIsVarInHash(varPtr) \
	? ((TclVarHashTable *) ((((VarInHash *) (varPtr))->entry.tablePtr)))->nsPtr \
	: NULL)

#define VarHashRefCount(varPtr) \
    ((VarInHash *) (varPtr))->refCount







>
>
>







842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858

#define TclIsVarInHash(varPtr) \
    ((varPtr)->flags & VAR_IN_HASHTABLE)

#define TclIsVarDeadHash(varPtr) \
    ((varPtr)->flags & VAR_DEAD_HASH)

#define TclIsVarArraySearched(varPtr) \
    ((varPtr)->flags & VAR_SEARCH_ACTIVE)

#define TclGetVarNsPtr(varPtr) \
    (TclIsVarInHash(varPtr) \
	? ((TclVarHashTable *) ((((VarInHash *) (varPtr))->entry.tablePtr)))->nsPtr \
	: NULL)

#define VarHashRefCount(varPtr) \
    ((VarInHash *) (varPtr))->refCount

Changes to generic/tclStubInit.c.

1412
1413
1414
1415
1416
1417
1418











1419
1420
1421
    Tcl_CloseEx, /* 624 */
    Tcl_NRExprObj, /* 625 */
    Tcl_NRSubstObj, /* 626 */
    Tcl_LoadFile, /* 627 */
    Tcl_FindSymbol, /* 628 */
    Tcl_FSUnloadFile, /* 629 */
    Tcl_ZlibStreamSetCompressionDictionary, /* 630 */











};

/* !END!: Do not edit above this line. */







>
>
>
>
>
>
>
>
>
>
>



1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
    Tcl_CloseEx, /* 624 */
    Tcl_NRExprObj, /* 625 */
    Tcl_NRSubstObj, /* 626 */
    Tcl_LoadFile, /* 627 */
    Tcl_FindSymbol, /* 628 */
    Tcl_FSUnloadFile, /* 629 */
    Tcl_ZlibStreamSetCompressionDictionary, /* 630 */
    Tcl_ArraySet, /* 631 */
    Tcl_ArrayUnset, /* 632 */
    Tcl_ArrayGet, /* 633 */
    Tcl_ArrayNames, /* 634 */
    Tcl_ArraySize, /* 635 */
    Tcl_ArrayExists, /* 636 */
    Tcl_ArraySearchStart, /* 637 */
    Tcl_ArraySearchPeek, /* 638 */
    Tcl_ArraySearchNext, /* 639 */
    Tcl_ArraySearchDone, /* 640 */
    Tcl_ArrayStatistics, /* 641 */
};

/* !END!: Do not edit above this line. */

Changes to generic/tclVar.c.

38
39
40
41
42
43
44
45
46
47
48
49
50














51
52
53
54
55
56
57
58
59
60
};

static inline Var *	VarHashCreateVar(TclVarHashTable *tablePtr,
			    Tcl_Obj *key, int *newPtr);
static inline Var *	VarHashFirstVar(TclVarHashTable *tablePtr,
			    Tcl_HashSearch *searchPtr);
static inline Var *	VarHashNextVar(Tcl_HashSearch *searchPtr);
static inline void	CleanupVar(Var *varPtr, Var *arrayPtr);

#define VarHashGetValue(hPtr) \
    ((Var *) ((char *)hPtr - TclOffset(VarInHash, entry)))

/*














 * NOTE: VarHashCreateVar increments the recount of its key argument.
 * All callers that will call Tcl_DecrRefCount on that argument must
 * call Tcl_IncrRefCount on it before passing it in.  This requirement
 * can bubble up to callers of callers .... etc.
 */

static inline Var *
VarHashCreateVar(
    TclVarHashTable *tablePtr,
    Tcl_Obj *key,







<





>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

|







38
39
40
41
42
43
44

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
};

static inline Var *	VarHashCreateVar(TclVarHashTable *tablePtr,
			    Tcl_Obj *key, int *newPtr);
static inline Var *	VarHashFirstVar(TclVarHashTable *tablePtr,
			    Tcl_HashSearch *searchPtr);
static inline Var *	VarHashNextVar(Tcl_HashSearch *searchPtr);


#define VarHashGetValue(hPtr) \
    ((Var *) ((char *)hPtr - TclOffset(VarInHash, entry)))

/*
 * Bit mask matching any of the bits used to select a match filter. AND this
 * mask against a flags value to obtain a value which can be compared against
 * each of the available match modes using the "==" equality operator.
 */

#define TCL_MATCH (TCL_MATCH_EXACT | TCL_MATCH_GLOB | TCL_MATCH_REGEXP)

/*
 * ArrayVar()-specific flag to enable array variable creation.
 */

#define TCL_VAR_CREATE 0x4000

/*
 * NOTE: VarHashCreateVar increments the refcount of its key argument.
 * All callers that will call Tcl_DecrRefCount on that argument must
 * call Tcl_IncrRefCount on it before passing it in. This requirement
 * can bubble up to callers of callers .... etc.
 */

static inline Var *
VarHashCreateVar(
    TclVarHashTable *tablePtr,
    Tcl_Obj *key,
139
140
141
142
143
144
145
146



























147
148

149
150
151

152





153
154
155

156
157
158
159
160
161
162
163
164
165
166
167
168


169

170
171
172
173
174
175
176
/*
 * A test to see if we are in a call frame that has local variables. This is
 * true if we are inside a procedure body.
 */

#define HasLocalVars(framePtr) ((framePtr)->isProcCallFrame & FRAME_IS_PROC)

/*



























 * The following structure describes an enumerative search in progress on an
 * array variable; this are invoked with options to the "array" command.

 */

typedef struct ArraySearch {

    Tcl_Obj *name;		/* Name of this search */





    int id;			/* Integer id used to distinguish among
				 * multiple concurrent searches for the same
				 * array. */

    struct Var *varPtr;		/* Pointer to array variable that's being
				 * searched. */
    Tcl_HashSearch search;	/* Info kept by the hash module about progress
				 * through the array. */
    Tcl_HashEntry *nextEntry;	/* Non-null means this is the next element to
				 * be enumerated (it's leftover from the
				 * Tcl_FirstHashEntry call or from an "array
				 * anymore" command). NULL means must call
				 * Tcl_NextHashEntry to get value to
				 * return. */
    struct ArraySearch *nextPtr;/* Next in list of all active searches for
				 * this variable, or NULL if this is the last
				 * one. */


} ArraySearch;


/*
 * Forward references to functions defined later in this file:
 */

static void		AppendLocals(Tcl_Interp *interp, Tcl_Obj *listPtr,
			    Tcl_Obj *patternPtr, int includeLinks);








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|
>


|
>
|
>
>
>
>
>



>
|



|
|
<
|
|
<
|


>
>
|
>







152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

210
211

212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * A test to see if we are in a call frame that has local variables. This is
 * true if we are inside a procedure body.
 */

#define HasLocalVars(framePtr) ((framePtr)->isProcCallFrame & FRAME_IS_PROC)

/*
 * Array search flags.
 */

enum {
    KEEP_ON_ABORT_BIT,
    SEARCH_ABORTED_BIT,

    /*
     * If set, the search structure is not automatically freed when the search
     * terminates early due to an array element being added or removed or the
     * array itself being deleted. In this case, the search structure is kept
     * until the next time a search operation is performed, at which time it is
     * deallocated and an error is reported.
     */

    KEEP_ON_ABORT = 1 << KEEP_ON_ABORT_BIT,

    /*
     * If KEEP_ON_ABORT is set and the search has terminated early, in lieu of
     * immediately freeing the search structure, this bit is set so that it will
     * be freed the next time a search operation is performed.
     */

    SEARCH_ABORTED = 1 << SEARCH_ABORTED_BIT,
};

/*
 * The following structure describes an enumerative search in progress on an
 * array variable. It is used by various Tcl_Array*() functions and their
 * respective [array] script interface commands.
 */

typedef struct Tcl_ArraySearch_ ArraySearch;
struct Tcl_ArraySearch_ {
    Tcl_Obj *name;		/* Name of this search. NULL if this search was
				 * created by Tcl_ArraySearchStart() rather than
				 * the [array startsearch] command. This is done
				 * to prevent C-initiated searches from being
				 * accessed via Tcl commands since they have
				 * different memory management semantics. */
    int id;			/* Integer id used to distinguish among
				 * multiple concurrent searches for the same
				 * array. */
    Tcl_Obj *varNameObj;	/* Name of the array variable. */
    Var *varPtr;		/* Pointer to array variable that's being
				 * searched. */
    Tcl_HashSearch search;	/* Info kept by the hash module about progress
				 * through the array. */
    Var *nextEntry;		/* Non-NULL means this is the next element to
				 * be enumerated (left over from ArrayFirst()

				 * or [array anymore]). NULL means must call
				 * ArrayNext() to get value to return. */

    ArraySearch *nextPtr;	/* Next in list of all active searches for
				 * this variable, or NULL if this is the last
				 * one. */
    Tcl_Obj *filterObj;		/* Search filter pattern, or NULL if none. */
    int filterType;		/* TCL_MATCH_EXACT, _GLOB, or _REGEXP. */
    int flags;			/* Search status flags as defined above. */
};

/*
 * Forward references to functions defined later in this file:
 */

static void		AppendLocals(Tcl_Interp *interp, Tcl_Obj *listPtr,
			    Tcl_Obj *patternPtr, int includeLinks);
185
186
187
188
189
190
191
192

















193
194
195
196
197
198
199
			    const char *otherP2, const int otherFlags,
			    Tcl_Obj *myNamePtr, int myFlags, int index);
static ArraySearch *	ParseSearchId(Tcl_Interp *interp, const Var *varPtr,
			    Tcl_Obj *varNamePtr, Tcl_Obj *handleObj);
static void		UnsetVarStruct(Var *varPtr, Var *arrayPtr,
			    Interp *iPtr, Tcl_Obj *part1Ptr,
			    Tcl_Obj *part2Ptr, int flags, int index);
static Var *		VerifyArray(Tcl_Interp *interp, Tcl_Obj *varNameObj);


















/*
 * Functions defined in this file that may be exported in the future for use
 * by the bytecode compiler and engine or to the public interface.
 */

MODULE_SCOPE Var *	TclLookupSimpleVar(Tcl_Interp *interp,







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
			    const char *otherP2, const int otherFlags,
			    Tcl_Obj *myNamePtr, int myFlags, int index);
static ArraySearch *	ParseSearchId(Tcl_Interp *interp, const Var *varPtr,
			    Tcl_Obj *varNamePtr, Tcl_Obj *handleObj);
static void		UnsetVarStruct(Var *varPtr, Var *arrayPtr,
			    Interp *iPtr, Tcl_Obj *part1Ptr,
			    Tcl_Obj *part2Ptr, int flags, int index);
static Var *		ArrayVar(Tcl_Interp *interp, Tcl_Obj *varNameObj,
			    int *traceFailPtr, int flags);
static int		ArrayVarTrace(Tcl_Interp *interp, Var *varPtr,
			    Tcl_Obj *varNameObj);
static Var *		ArrayFirst(Tcl_Interp *interp, ArraySearch *searchPtr,
			    int *failPtr);
static Var *		ArrayNext(Tcl_Interp *interp, ArraySearch *searchPtr,
			    int *failPtr);
static void		ArrayDone(Tcl_Interp *interp, ArraySearch *searchPtr);
static int  		ArrayAborted(Tcl_Interp *interp,
			    ArraySearch *searchPtr, int dealloc);
static void		ArraySearchFree(ArraySearch *searchPtr);
static int		ArrayNames(Tcl_Interp *interp, Var *varPtr,
			    Tcl_Obj *filterObj, int filterType,
			    Tcl_Obj *listObj);
static int		ArrayArgs(Tcl_Interp *interp, int objc,
			    Tcl_Obj *const objv[], Tcl_Obj **varNameObjPtr,
			    Tcl_Obj **patternObjPtr, int *patternTypePtr);

/*
 * Functions defined in this file that may be exported in the future for use
 * by the bytecode compiler and engine or to the public interface.
 */

MODULE_SCOPE Var *	TclLookupSimpleVar(Tcl_Interp *interp,
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
    FreeLocalVarName, DupLocalVarName, NULL, NULL
};

static const Tcl_ObjType tclParsedVarNameType = {
    "parsedVarName",
    FreeParsedVarName, DupParsedVarName, NULL, NULL
};


Var *
TclVarHashCreateVar(
    TclVarHashTable *tablePtr,
    const char *key,
    int *newPtr)
{







<







292
293
294
295
296
297
298

299
300
301
302
303
304
305
    FreeLocalVarName, DupLocalVarName, NULL, NULL
};

static const Tcl_ObjType tclParsedVarNameType = {
    "parsedVarName",
    FreeParsedVarName, DupParsedVarName, NULL, NULL
};


Var *
TclVarHashCreateVar(
    TclVarHashTable *tablePtr,
    const char *key,
    int *newPtr)
{
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
 *	If the variable (or its containing array) really is dead and in a
 *	hashtable, then its Var structure, and possibly its hash table entry,
 *	is freed up.
 *
 *----------------------------------------------------------------------
 */

static inline void
CleanupVar(
    Var *varPtr,		/* Pointer to variable that may be a candidate
				 * for being expunged. */
    Var *arrayPtr)		/* Array that contains the variable, or NULL
				 * if this variable isn't an array element. */
{
    if (TclIsVarUndefined(varPtr) && TclIsVarInHash(varPtr)
	    && !TclIsVarTraced(varPtr)







|
|







332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
 *	If the variable (or its containing array) really is dead and in a
 *	hashtable, then its Var structure, and possibly its hash table entry,
 *	is freed up.
 *
 *----------------------------------------------------------------------
 */

void
TclCleanupVar(
    Var *varPtr,		/* Pointer to variable that may be a candidate
				 * for being expunged. */
    Var *arrayPtr)		/* Array that contains the variable, or NULL
				 * if this variable isn't an array element. */
{
    if (TclIsVarUndefined(varPtr) && TclIsVarInHash(varPtr)
	    && !TclIsVarTraced(varPtr)
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
	if (VarHashRefCount(arrayPtr) == 0) {
	    ckfree(arrayPtr);
	} else {
	    VarHashDeleteEntry(arrayPtr);
	}
    }
}

void
TclCleanupVar(
    Var *varPtr,		/* Pointer to variable that may be a candidate
				 * for being expunged. */
    Var *arrayPtr)		/* Array that contains the variable, or NULL
				 * if this variable isn't an array element. */
{
    CleanupVar(varPtr, arrayPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * TclLookupVar --
 *
 *	This function is used to locate a variable given its name(s). It has







<
<
<
<
<
<
<
<
<
<







358
359
360
361
362
363
364










365
366
367
368
369
370
371
	if (VarHashRefCount(arrayPtr) == 0) {
	    ckfree(arrayPtr);
	} else {
	    VarHashDeleteEntry(arrayPtr);
	}
    }
}











/*
 *----------------------------------------------------------------------
 *
 * TclLookupVar --
 *
 *	This function is used to locate a variable given its name(s). It has
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
 * TclLookupArrayElement --
 *
 *	This function is used to locate a variable which is in an array's
 *	hashtable given a pointer to the array's Var structure and the
 *	element's name.
 *
 * Results:
 *	The return value is a pointer to the variable structure , or NULL if
 *	the variable couldn't be found.
 *
 *	If arrayPtr points to a variable that isn't an array and createPart1
 *	is 1, the corresponding variable will be converted to an array.
 *	Otherwise, NULL is returned and an error message is left in the
 *	interp's result if TCL_LEAVE_ERR_MSG is set in flags.
 *







|







968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
 * TclLookupArrayElement --
 *
 *	This function is used to locate a variable which is in an array's
 *	hashtable given a pointer to the array's Var structure and the
 *	element's name.
 *
 * Results:
 *	The return value is a pointer to the variable structure, or NULL if
 *	the variable couldn't be found.
 *
 *	If arrayPtr points to a variable that isn't an array and createPart1
 *	is 1, the corresponding variable will be converted to an array.
 *	Otherwise, NULL is returned and an error message is left in the
 *	interp's result if TCL_LEAVE_ERR_MSG is set in flags.
 *
1040
1041
1042
1043
1044
1045
1046

















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































1047
1048
1049
1050
1051
1052
1053
		Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ELEMENT",
			TclGetString(elNamePtr), NULL);
	    }
	}
    }
    return varPtr;
}


















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetVar --
 *
 *	Return the value of a Tcl variable as a string.







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
		Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ELEMENT",
			TclGetString(elNamePtr), NULL);
	    }
	}
    }
    return varPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayVar --
 *
 *	This function looks up or creates an array variable. The TCL_VAR_CREATE
 *	flag is used to enable creation mode.
 *
 * Results:
 *	If successful, the requested variable is returned. On failure, NULL is
 *	returned, and error information is placed in the interpreter result. If
 *	the error occurred within an array trace and traceFailPtr is not NULL,
 *	*traceFailPtr is set to 1. Non-trace errors are inhibited if flags does
 *	not contain TCL_LEAVE_ERR_MSG.
 *
 * Side effects:
 *	Array traces, if any, are executed. The variable is created if it does
 *	not exist and create mode is enabled.
 *	
 *----------------------------------------------------------------------
 */

static Var *
ArrayVar(
    Tcl_Interp *interp,		/* Command interpreter in which varNamePtr is to
				 * be looked up. */
    Tcl_Obj *varNameObj,	/* Name of array variable in interp. */
    int *traceFailPtr,		/* Unless NULL, set to 1 on trace failure. */
    int flags)			/* OR-ed combination of TCL_GLOBAL_ONLY,
				 * TCL_NAMESPACE_ONLY, TCL_LEAVE_ERR_MSG, and
				 * TCL_VAR_CREATE. */
{
    Var *varPtr, *arrayPtr;
    const char *varName;
    int create = !!(flags & TCL_VAR_CREATE);

    /*
     * Strip TCL_VAR_CREATE from flags because no other function recognizes it.
     */

    flags &= ~TCL_VAR_CREATE;

    /*
     * Locate the array variable. Unless in create mode, inhibit the normal
     * variable lookup error messages in favor of the custom messages that will
     * be generated below.
     */

    varPtr = TclObjLookupVarEx(interp, varNameObj, NULL,
	    create ? flags : flags & ~TCL_LEAVE_ERR_MSG, "set",
	    create, create, &arrayPtr);

    /*
     * In create mode with TCL_LEAVE_ERR_MSG set, keep any error messages that
     * were generated. Furthermore, if the variable turned out be an array
     * element, delete it and proceed to the common error routine below.
     */

    if (create) {
	if (!varPtr && (flags & TCL_LEAVE_ERR_MSG)) {
	    return NULL;
	} else if (arrayPtr) {
	    TclCleanupVar(varPtr, arrayPtr);
	    varPtr = NULL;
	}
    }

    if (varPtr) {
	/*
	 * Special array trace used to keep the env array in sync for array
	 * names, array get, etc.
	 */

	if (ArrayVarTrace(interp, varPtr, varNameObj) != TCL_OK) {
	    if (traceFailPtr) {
		*traceFailPtr = 1;
	    }
	    return NULL;
	}

	/*
	 * Verify that it is indeed an array variable. This test comes after the
	 * traces - the variable may actually become an array as an effect of
	 * said traces.
	 */

	if (!create && (!TclIsVarArray(varPtr) || TclIsVarUndefined(varPtr))) {
	    varPtr = NULL;
	}
    }

    /*
     * Common error generation routine. This handles everything but creation
     * errors (e.g. bad namespace) and traces (handled above).
     */

    if (!varPtr && (flags & TCL_LEAVE_ERR_MSG)) {
	varName = TclGetString(varNameObj);
	if (create) {
	    TclObjVarErrMsg(interp, varNameObj, NULL, "set", needArray, -1);
	    Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "VARNAME", varName, NULL);
	} else {
	    Tcl_SetObjResult(interp,
		    Tcl_ObjPrintf("\"%s\" isn't an array", varName));
	    Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ARRAY", varName, NULL);
	}
    }

    return varPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayVarTrace --
 *
 *	Calls array traces on an array variable.
 *
 * Results:
 *	Returns TCL_OK if the array variable's array traces complete without
 *	error or if the array variable has no array traces. On error, returns
 *	TCL_ERROR and places error information in the interpreter result.
 *
 * Side effects:
 *	Array traces, if any, are executed.
 *
 *----------------------------------------------------------------------
 */

int
ArrayVarTrace(
    Tcl_Interp *interp,		/* Interpreter containing the variable. */
    Var *varPtr,		/* Array variable structure pointer. */
    Tcl_Obj *varNameObj)	/* Name of array variable. */
{
    if ((varPtr->flags & VAR_TRACED_ARRAY)
     && (TclIsVarArray(varPtr) || TclIsVarUndefined(varPtr))
     && TclObjCallVarTraces((Interp *)interp, NULL, varPtr, varNameObj,
	    NULL, TCL_LEAVE_ERR_MSG | TCL_TRACE_ARRAY,
	    /*leaveErrMsg*/ 1, -1) != TCL_OK) {
	return TCL_ERROR;
    } else {
	return TCL_OK;
    }
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayFirst --
 *
 *	Finds the first element of an array. If a filter is specified, only
 *	elements matching the filter are found.
 *
 * Preconditions:
 *	The interp, varPtr, filterObj, and filterType fields of *searchPtr must
 *	have been initialized.
 *
 * Results:
 *	The first array element is returned, or NULL if there are no matching
 *	elements or on error, in which case *failPtr is set to 1.
 *
 * Side effects:
 *	*searchPtr is updated to track the progress of the enumeration. On
 *	error, detailed error information is placed into the interpreter result.
 *
 *----------------------------------------------------------------------
 */

static Var *
ArrayFirst(
    Tcl_Interp *interp,		/* Command interpreter in which the array
				 * variable is located. */
    ArraySearch *searchPtr,	/* Array enumeration state structure. */
    int *failPtr)		/* Set to 1 on error. */
{
    TclVarHashTable *tablePtr = searchPtr->varPtr->value.tablePtr;
    Var *varPtr;

    /*
     * Exact matches and trivial glob matches can be completed immediately since
     * they will only ever match one or zero elements. No need to iterate, just
     * do a direct lookup, then fast-forward to the end of the hash table.
     */

    if (searchPtr->filterObj
     && (searchPtr->filterType == TCL_MATCH_EXACT
      || (searchPtr->filterType == TCL_MATCH_GLOB
       && TclMatchIsTrivial(TclGetString(searchPtr->filterObj))))) {
	varPtr = VarHashFindVar(tablePtr, searchPtr->filterObj);
	searchPtr->search.tablePtr = &tablePtr->table;
	searchPtr->search.nextIndex = tablePtr->table.numBuckets;
	searchPtr->search.nextEntryPtr = NULL;
	searchPtr->nextEntry = NULL;

	if (!varPtr || TclIsVarUndefined(varPtr)) {
	    return NULL;
	} else {
	    return varPtr;
	}
    }

    /*
     * For all other match types, find the first item (which may or may not
     * match the filter) then chain to ArrayNext() to get the real first item.
     */

    searchPtr->nextEntry = VarHashFirstVar(tablePtr, &searchPtr->search);
    return ArrayNext(interp, searchPtr, failPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayNext --
 *
 *	Finds the next element of an array for a given search query.
 *
 *	To peek at the next element without consuming it, copy the returned
 *	element to searchPtr->nextEntry. This causes the next invocation of
 *	ArrayNext() to return the same element again.
 *
 * Preconditions:
 *	ArrayFirst() must have been called on searchPtr.
 *
 * Results:
 *	The next array element is returned, or NULL if there are no matching
 *	elements remaining or on error, in which case *failPtr is set to 1 if
 *	failPtr is not NULL.
 *
 * Side effects:
 *	*searchPtr is updated to track the progress of the enumeration. On
 *	error, detailed error information is placed into the interpreter result.
 *
 *----------------------------------------------------------------------
 */

static Var *
ArrayNext(
    Tcl_Interp *interp,		/* Command interpreter in which the array
				 * variable is located. */
    ArraySearch *searchPtr,	/* Array enumeration state structure. */
    int *failPtr)		/* Set to 1 on error. */
{
    Var *varPtr;
    Tcl_Obj *nameObj;
    int matched;

    /*
     * Use the cached nextEntry left over from ArrayFirst() or [array anymore],
     * or else get the next one from the hash table.
     */

    if (searchPtr->nextEntry) {
	varPtr = searchPtr->nextEntry;
	searchPtr->nextEntry = NULL;
    } else {
	varPtr = VarHashNextVar(&searchPtr->search);
    }

    /*
     * Iterate through the hash table until an element matches the filter or the
     * end is reached.
     */

    for (; varPtr; varPtr = VarHashNextVar(&searchPtr->search)) {
	if (!TclIsVarUndefined(varPtr)) {
	    /*
	     * If no filter, accept each defined element regardless of name.
	     */

	    if (!searchPtr->filterObj) {
		return varPtr;
	    }

	    /*
	     * Conditionally accept elements whose names match the filter.
	     */

	    nameObj = VarHashGetKey(varPtr);
	    if (searchPtr->filterType == TCL_MATCH_GLOB) {
		if (Tcl_StringMatch(TclGetString(nameObj),
			TclGetString(searchPtr->filterObj))) {
		    return varPtr;
		}
	    } else if (searchPtr->filterType == TCL_MATCH_REGEXP) {
		matched = Tcl_RegExpMatchObj(interp, nameObj,
			searchPtr->filterObj);
		if (matched < 0) {
		    if (failPtr) {
			*failPtr = 1;
		    }
		    return NULL;
		} else if (matched) {
		    return varPtr;
		}
	    } else if (searchPtr->filterType == TCL_MATCH_EXACT) {
		if (strcmp(TclGetString(nameObj),
			TclGetString(searchPtr->filterObj)) == 0) {
		    return varPtr;
		}
	    } else {
		Tcl_Panic("invalid filter type: %u", searchPtr->filterType);
	    }
	}
    }

    return NULL;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayDone --
 *
 *	Terminates and cleans up an array search query.
 *
 * Preconditions:
 *	The search argument must be the return value of Tcl_ArraySearchStart()
 *	and must not have been passed to Tcl_ArraySearchDone().
 *
 * Results:
 *	The search query is completed.
 *
 * Side effects:
 *	Resources associated with the search are deallocated.
 *
 *----------------------------------------------------------------------
 */

void
ArrayDone(
    Tcl_Interp *interp,		/* Command interpreter in which the array
				 * variable is located. */
    ArraySearch *searchPtr)	/* Array enumeration state structure. */
{
    ArraySearch *prevPtr;
    Tcl_HashEntry *hPtr = Tcl_FindHashEntry(
	    &((Interp *)interp)->varSearches, searchPtr->varPtr);

    /*
     * Unhook the search from the list of searches associated with the
     * variable.
     */

    if (searchPtr == Tcl_GetHashValue(hPtr)) {
	if (searchPtr->nextPtr) {
	    Tcl_SetHashValue(hPtr, searchPtr->nextPtr);
	} else {
	    searchPtr->varPtr->flags &= ~VAR_SEARCH_ACTIVE;
	    Tcl_DeleteHashEntry(hPtr);
	}
    } else {
	for (prevPtr = Tcl_GetHashValue(hPtr);; prevPtr = prevPtr->nextPtr) {
	    if (prevPtr->nextPtr == searchPtr) {
		prevPtr->nextPtr = searchPtr->nextPtr;
		break;
	    }
	}
    }

    /*
     * Deallocate the search object.
     */

    ArraySearchFree(searchPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayAborted --
 *
 *	Checks if an array search was aborted due to array elements being added
 *	or removed or the array being unsed. Optionally deallocates it if so.
 *
 * Results:
 *	TCL_OK is returned if the array search did not abort. TCL_ERROR is
 *	returned if the array search did abort, and a message to that effect is
 *	placed in the interpreter result. If the dealloc argument is nonzero,
 *	the array search is also deallocated if it aborted.
 *
 * Side effects:
 *	Memory used by the search may be released to the storage allocator.
 *
 *----------------------------------------------------------------------
 */

int
ArrayAborted(
    Tcl_Interp *interp,		/* Command interpreter in which the array
				 * variable is located. */
    ArraySearch *searchPtr,	/* Array enumeration state structure. */
    int dealloc)		/* If nonzero, deallocate aborted searches. */
{
    if (searchPtr->flags & SEARCH_ABORTED) {
	if (dealloc) {
	    ArraySearchFree(searchPtr);
	}
	Tcl_SetResult(interp, "search aborted due to array change", TCL_STATIC);
	Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ARRAYSEARCH", "n/a", NULL);
	return TCL_ERROR;
    }

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ArraySearchFree --
 *
 *	Deallocates an array search structure.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Memory used by the search is released to the storage allocator.
 *
 *----------------------------------------------------------------------
 */

void
ArraySearchFree(
    ArraySearch *searchPtr)	/* Array enumeration state structure. */
{
    if (searchPtr->name) {
	Tcl_DecrRefCount(searchPtr->name);
    }
    Tcl_DecrRefCount(searchPtr->varNameObj);
    if (searchPtr->filterObj) {
	Tcl_DecrRefCount(searchPtr->filterObj);
    }
    ckfree(searchPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayNames --
 *
 *	Obtains a list of array element names, optionally limited by a filter.
 *
 * Results:
 *	Normally, TCL_OK is returned, and the list of matching array element
 *	names is appended to listObj. On error, TCL_ERROR is returned, and the
 *	error information is placed in the interpreter's result.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

static int
ArrayNames(
    Tcl_Interp *interp,		/* Interpreter, used to report regexp errors. */
    Var *varPtr,		/* Array variable. */
    Tcl_Obj *filterObj,		/* Element filter or NULL to accept all. */
    int filterType,		/* TCL_MATCH_EXACT, _GLOB, or _REGEXP. */
    Tcl_Obj *listObj)		/* List to which array names are appended. */
{
    ArraySearch search;
    int fail = 0, oldLen, newLen;

    /*
     * Ensure output object is a list. Also get its length in case there is
     * trouble and changes need to be rolled back. Such a failure should never
     * occur because it requires a regular expression to initially succeed then
     * return error on a subsequent evaluation, but handle it anyway because
     * it's easy to do.
     */

    if (Tcl_ListObjLength(interp, listObj, &oldLen) != TCL_OK) {
	return TCL_ERROR;
    }

    /*
     * Begin the search.
     */

    search.varPtr = varPtr;
    search.filterObj = filterObj;
    search.filterType = filterType;
    search.nextEntry = NULL;
    search.flags = 0;
    varPtr = ArrayFirst(interp, &search, &fail);

    /*
     * Enumerate the array.
     */

    for (; varPtr; varPtr = ArrayNext(interp, &search, &fail)) {
	Tcl_ListObjAppendElement(interp, listObj, VarHashGetKey(varPtr));
    }

    /*
     * On failure, roll back changes to output list.
     */

    if (fail) {
	Tcl_ListObjLength(interp, listObj, &newLen);
	Tcl_ListObjReplace(interp, listObj, oldLen, newLen - oldLen, 0, NULL);
	return TCL_ERROR;
    }

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayArgs --
 *
 *	Common argument parser for numerous [array] commands, namely:
 *	startsearch, exists, get, names, size, and unset.
 *
 * Results:
 *	Arguments are parsed, results are written to caller variables, and
 *	TCL_OK is returned. On failure, TCL_ERROR is returned, and error
 *	information is logged to the interpreter.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

static int
ArrayArgs(
    Tcl_Interp *interp,		/* Interpreter into which errors are logged. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[],	/* Argument vector. */
    Tcl_Obj **varNameObjPtr,	/* Location to write variable name. */
    Tcl_Obj **filterObjPtr,	/* Location to write filter pattern. */
    int *filterTypePtr)		/* Location to write filter type code. */
{
    static const struct {
	const char *name;
	int type;
    } options[] = {
	{"-exact"   , TCL_MATCH_EXACT	},
	{"-glob"    , TCL_MATCH_GLOB	},
	{"-regexp"  , TCL_MATCH_REGEXP	},
	{NULL	    , 0			},
    };
    enum {OPT_EXACT, OPT_GLOB, OPT_REGEXP} mode = OPT_GLOB;

    if (objc < 2 || objc > 4) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName ?mode? ?pattern?");
	return TCL_ERROR;
    }

    if (objc == 4 && Tcl_GetIndexFromObjStruct(interp, objv[2], options,
	    sizeof(*options), "option", 0, (int *)&mode) != TCL_OK) {
	return TCL_ERROR;
    }

    *varNameObjPtr = objv[1];
    *filterObjPtr = objc > 2 ? objv[objc - 1] : NULL;
    *filterTypePtr = options[mode].type;
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ArraySet --
 *
 *	Set the elements of an array. If there are no elements to set, create
 *	an empty array.
 *
 * Results:
 *	A standard Tcl result object.
 *
 * Side effects:
 *	A variable will be created if one does not already exist.
 *	Callers must Incr part1Ptr if they plan to Decr it.
 *	Array and variable set traces are executed.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_ArraySet(
    Tcl_Interp *interp,		/* Current interpreter. */
    Tcl_Obj *part1Ptr,		/* The array name. */
    Tcl_Obj *dictPtr,		/* The array elements list or dict. If this is
				 * NULL, create an empty array. */
    int flags)			/* OR-ed combination of TCL_GLOBAL_ONLY and
				 * TCL_NAMESPACE_ONLY. */
{
    Var *varPtr;
    int result, i;

    if (!(varPtr = ArrayVar(interp, part1Ptr, NULL,
	    flags | TCL_LEAVE_ERR_MSG | TCL_VAR_CREATE))) {
	return TCL_ERROR;
    }

    if (dictPtr == NULL) {
	goto ensureArray;
    }

    /*
     * Install the contents of the dictionary or list into the array.
     */

    if (dictPtr->typePtr == &tclDictType) {
	Tcl_Obj *keyPtr, *valuePtr;
	Tcl_DictSearch search;
	int done;

	if (Tcl_DictObjSize(interp, dictPtr, &done) != TCL_OK) {
	    return TCL_ERROR;
	}
	if (done == 0) {
	    /*
	     * Empty, so we'll just force the array to be properly existing
	     * instead.
	     */

	    goto ensureArray;
	}

	/*
	 * Don't need to look at result of Tcl_DictObjFirst as we've just
	 * successfully used a dictionary operation on the same object.
	 */

	for (Tcl_DictObjFirst(interp, dictPtr, &search,
		&keyPtr, &valuePtr, &done) ; !done ;
		Tcl_DictObjNext(&search, &keyPtr, &valuePtr, &done)) {
	    /*
	     * At this point, it would be nice if the key was directly usable
	     * by the array. This isn't the case though.
	     */

	    Var *elemVarPtr = TclLookupArrayElement(interp, part1Ptr,
		    keyPtr, flags | TCL_LEAVE_ERR_MSG, "set", 1, 1, varPtr, -1);

	    if ((elemVarPtr == NULL) ||
		    (TclPtrSetVar(interp, elemVarPtr, varPtr, part1Ptr,
		    keyPtr, valuePtr, flags | TCL_LEAVE_ERR_MSG, -1) == NULL)) {
		Tcl_DictObjDone(&search);
		return TCL_ERROR;
	    }
	}
	return TCL_OK;
    } else {
	/*
	 * Not a dictionary, so assume (and convert to, for backward-
	 * -compatibility reasons) a list.
	 */

	int elemLen;
	Tcl_Obj **elemPtrs, *copyListObj;

	result = TclListObjGetElements(interp, dictPtr,
		&elemLen, &elemPtrs);
	if (result != TCL_OK) {
	    return result;
	}
	if (elemLen & 1) {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj(
		    "list must have an even number of elements", -1));
	    Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "FORMAT", NULL);
	    return TCL_ERROR;
	}
	if (elemLen == 0) {
	    goto ensureArray;
	}

	/*
	 * We needn't worry about traces invalidating arrayPtr: should that be
	 * the case, TclPtrSetVar will return NULL so that we break out of the
	 * loop and return an error.
	 */

	copyListObj = TclListObjCopy(NULL, dictPtr);
	for (i=0 ; i<elemLen ; i+=2) {
	    Var *elemVarPtr = TclLookupArrayElement(interp, part1Ptr,
		    elemPtrs[i], flags | TCL_LEAVE_ERR_MSG, "set",
		    1, 1, varPtr, -1);

	    if ((elemVarPtr == NULL) ||
		    (TclPtrSetVar(interp, elemVarPtr, varPtr, part1Ptr,
		    elemPtrs[i],elemPtrs[i + 1],
		    flags | TCL_LEAVE_ERR_MSG, -1) == NULL)) {
		result = TCL_ERROR;
		break;
	    }
	}
	Tcl_DecrRefCount(copyListObj);
	return result;
    }

    /*
     * The list is empty make sure we have an array, or create one if
     * necessary.
     */

  ensureArray:
    if (varPtr != NULL) {
	if (TclIsVarArray(varPtr)) {
	    /*
	     * Already an array, done.
	     */

	    return TCL_OK;
	}
	if (TclIsVarArrayElement(varPtr) || !TclIsVarUndefined(varPtr)) {
	    /*
	     * Either an array element, or a scalar: lose!
	     */

	    TclObjVarErrMsg(interp, part1Ptr, NULL, "array set", needArray, -1);
	    Tcl_SetErrorCode(interp, "TCL", "WRITE", "ARRAY", NULL);
	    return TCL_ERROR;
	}
    }
    TclSetVarArray(varPtr);
    varPtr->value.tablePtr = ckalloc(sizeof(TclVarHashTable));
    TclInitVarHashTable(varPtr->value.tablePtr, TclGetVarNsPtr(varPtr));
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ArrayUnset --
 *
 *	Unsets array elements, optionally limited by a filter. It is not an
 *	error for the filter to not match any elements or for the variable to
 *	not exist or not be an array.
 *
 * Results:
 *	The requested array elements are unset, and TCL_OK is returned. On trace
 *	or filter match error, TCL_ERROR is returned and error information is
 *	placed in the interpreter result.
 *
 * Side effects:
 *	Array and unset traces are executed.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_ArrayUnset(
    Tcl_Interp *interp,		/* Interpreter containing the variable. */
    Tcl_Obj *part1Ptr,		/* Name of the array variable. */
    Tcl_Obj *part2Ptr,		/* Element filter or NULL to unset all. */
    int flags)			/* OR-ed combination of TCL_GLOBAL_ONLY,
				 * TCL_NAMESPACE_ONLY, and at most one of
				 * TCL_MATCH_EXACT, _GLOB, and _REGEXP. */
{
    ArraySearch search;
    int fail = 0, filterType = flags & TCL_MATCH;
    Var *varPtr, *elemPtr, *protectedElemPtr;

    /*
     * Locate the array variable. This has the side effect of executing any
     * array traces. If the variable does not exist, exit successfully.
     */

    if (!(varPtr = ArrayVar(interp, part1Ptr, &fail, 0))) {
	return fail ? TCL_ERROR : TCL_OK;
    }

    /*
     * When no filter is given, unset the whole array.
     */

    if (!part2Ptr) {
	return TclObjUnsetVar2(interp, part1Ptr, NULL, flags);
    }

    /*
     * With an exact match or trivial pattern, unset the single element.
     */

    if (filterType == TCL_MATCH_EXACT || (filterType == TCL_MATCH_GLOB
     && TclMatchIsTrivial(TclGetString(part2Ptr)))) {
	elemPtr = VarHashFindVar(varPtr->value.tablePtr, part2Ptr);
	if (!elemPtr || TclIsVarUndefined(elemPtr)) {
	    return TCL_OK;
	} else {
	    return TclPtrUnsetVar(interp, elemPtr, varPtr,
		    part1Ptr, part2Ptr, 0, -1);
	}
    }

    /*
     * Prepare to iterate through all elements of the array.
     */

    search.varPtr = varPtr;
    search.filterObj = part2Ptr;
    search.filterType = filterType;
    search.flags = 0;
    elemPtr = ArrayFirst(interp, &search, &fail);

    /*
     * Non-trivial case (well, deeply tricky really). We peek inside the hash
     * iterator in order to allow us to guarantee that the following element
     * in the array will not be scrubbed until we have dealt with it. This
     * stops the overall iterator from ending up pointing into deallocated
     * memory. [Bug 2939073]
     */

    protectedElemPtr = NULL;
    for (; elemPtr; elemPtr = ArrayNext(interp, &search, &fail)) {
	/*
	 * Drop the extra ref immediately. We don't need to free it at this
	 * point though; we'll be unsetting it if necessary soon.
	 */

	if (protectedElemPtr == elemPtr) {
	    VarHashRefCount(protectedElemPtr)--;
	}

	/*
	 * Peek ahead at the next item in the search and guard it against being
	 * deallocated in the scenario where the unset trace on the current
	 * element causes the next element to be unset as well.
	 *
	 * Curiosity: each hash table bucket is a linked list to which new items
	 * are prepended, so "next" usually means the latest element in the same
	 * bucket that was set before the current element.
	 */

	if ((search.nextEntry = ArrayNext(interp, &search, &fail))) {
	    VarHashRefCount(protectedElemPtr)++;
	}
	protectedElemPtr = search.nextEntry;

	/*
	 * If the variable is undefined, clean it out as it has been hit by
	 * something else (i.e., an unset trace).
	 */

	if (TclIsVarUndefined(elemPtr)) {
	    TclCleanupVar(elemPtr, varPtr);
	} else if (TclPtrUnsetVar(interp, elemPtr, varPtr, part1Ptr,
		VarHashGetKey(elemPtr), 0, -1) != TCL_OK) {
	    /*
	     * If we incremented a refcount, we must decrement it here as we
	     * will not be coming back properly due to the error.
	     */

	    if (protectedElemPtr) {
		VarHashRefCount(protectedElemPtr)--;
		TclCleanupVar(protectedElemPtr, varPtr);
	    }
	    return TCL_ERROR;
	}
    }

    return fail ? TCL_ERROR : TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ArrayGet --
 *
 *	This function loads the contents of an array into a dict. It is not an
 *	error for the variable to not exist or not be an array; in this case,
 *	the dict is not modified, as if the variable were an empty array.
 *
 * Results:
 *	The array is loaded into the specified dict. The array element names and
 *	values become the dict keys and values, respectively. If a filter is
 *	supplied, only the elements whose names match the filter are loaded.
 *
 *	If the output dict isn't initially empty, the new keys and values take
 *	precedence over its initial contents. This can be used to merge multiple
 *	arrays, or multiple collections of elements from a single array obtained
 *	by different filters, into a single dict. There is a mild performance
 *	penalty when the output dict isn't initially empty due to the need to
 *	maintain a rollback dict in case an array trace or read trace unsets the
 *	array during the execution of this function.
 *
 *	The return value is TCL_OK on success and TCL_ERROR on trace or filter
 *	match error.
 *
 * Side effects:
 *	Array and variable read traces, if any, are executed.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_ArrayGet(
    Tcl_Interp *interp,		/* Interpreter in which to look up variable. */
    Tcl_Obj *part1Ptr,		/* Name of array variable. */
    Tcl_Obj *part2Ptr,		/* Element filter or NULL to read all. */
    Tcl_Obj *dictPtr,		/* Dict object to load array data into. */
    int flags)			/* OR-ed combination of TCL_GLOBAL_ONLY and
				 * TCL_NAMESPACE_ONLY, and at most one of
				 * TCL_MATCH_EXACT, _GLOB, and _REGEXP. */
{
    Tcl_Obj *nameListObj, *rollbackObj, **nameObjPtr, *valueObj;
    Var *varPtr;
    int fail = 0, filterType = flags & TCL_MATCH, i, count, result;

    /*
     * Locate the array variable. Report trace failures as errors. If the
     * variable is a scalar or does not exist, treat it like an empty array.
     */

    if (!(varPtr = ArrayVar(interp, part1Ptr, &fail, flags))) {
	return fail ? TCL_ERROR : TCL_OK;
    }

    /*
     * Confirm the output structure forms a valid dict. It's also important to
     * check whether it's initially empty or not.
     */

    if (Tcl_DictObjSize(interp, dictPtr, &count) != TCL_OK) {
	return TCL_ERROR;
    }

    /*
     * Get the list of element names matching the filter.
     */

    TclNewObj(nameListObj);
    Tcl_IncrRefCount(nameListObj);
    if (ArrayNames(interp, varPtr, part2Ptr, filterType,
	    nameListObj) != TCL_OK) {
	Tcl_DecrRefCount(nameListObj);
	return TCL_ERROR;
    }

    /*
     * If the output dict wasn't initially empty, prepare to undo any changes
     * made to it in case an error occurs.
     */

    if (count) {
	TclNewObj(rollbackObj);
	Tcl_IncrRefCount(rollbackObj);
    } else {
	rollbackObj = NULL;
    }

    /*
     * Make sure the Var structure of the array is not removed by a trace while
     * we're working.
     */

    if (TclIsVarInHash(varPtr)) {
	VarHashRefCount(varPtr)++;
    }

    /*
     * Load the array keys and values into the output dict.
     */

    Tcl_ListObjGetElements(interp, nameListObj, &count, &nameObjPtr);
    result = TCL_OK;
    for (i = 0; i < count; ++i, ++nameObjPtr) {
	/*
	 * If rollback is enabled, before loading each new value, remember the
	 * old value already present in the output dict.
	 */

	if (rollbackObj) {
	    Tcl_DictObjGet(interp, dictPtr, *nameObjPtr, &valueObj);
	    if (valueObj) {
		Tcl_DictObjPut(interp, rollbackObj, *nameObjPtr, valueObj);
	    }
	}

	/*
	 * Try to get the array element value, but beware of traces.
	 */

	valueObj = Tcl_ObjGetVar2(interp, part1Ptr, *nameObjPtr,
		TCL_LEAVE_ERR_MSG);

	if (valueObj) {
	    /*
	     * If the array element was found, load it into the output dict, an
	     * operation which cannot fail since the output was already
	     * confirmed to be a valid dict.
	     */

	    Tcl_DictObjPut(interp, dictPtr, *nameObjPtr, valueObj);
	} else if (!TclIsVarArray(varPtr)) {
	    /*
	     * On error (specifically, if a trace deleted the array), restore
	     * the output dict to the way it was when this function was called.
	     *
	     * If the element was unset by a trace, proceed as if it never
	     * existed in the first place, provided the array still exists.
	     */

	    if (rollbackObj) {
		count = i;
		nameObjPtr -= i;
		for (i = 0; i < count; ++i, ++nameObjPtr) {
		    Tcl_DictObjGet(interp, rollbackObj, *nameObjPtr, &valueObj);
		    if (valueObj) {
			Tcl_DictObjPut(interp, dictPtr, *nameObjPtr, valueObj);
		    } else {
			Tcl_DictObjRemove(interp, dictPtr, *nameObjPtr);
		    }
		}
	    } else {
		Tcl_SetStringObj(dictPtr, NULL, 0);
	    }

	    result = TCL_ERROR;
	    break;
	}
    }

    /*
     * Clean up internal data structures, then report success or failure.
     */

    if (TclIsVarInHash(varPtr)) {
	VarHashRefCount(varPtr)--;
    }
    if (rollbackObj) {
	Tcl_DecrRefCount(rollbackObj);
    }
    Tcl_DecrRefCount(nameListObj);

    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ArrayNames --
 *
 *	Obtains a list of array element names, optionally limited by a filter.
 *
 * Results:
 *	Normally, TCL_OK is returned, and the list of matching array element
 *	names is appended to listPtr. On error, TCL_ERROR is returned, and the
 *	error information is placed in the interpreter's result. If part1Ptr
 *	does not name an array, TCL_OK is returned and listPtr is unmodified.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_ArrayNames(
    Tcl_Interp *interp,		/* Command interpreter in which part1Ptr is to
				 * be looked up. */
    Tcl_Obj *part1Ptr,		/* Name of array variable in interp. */
    Tcl_Obj *part2Ptr,		/* Element filter or NULL to accept all. */
    Tcl_Obj *listPtr,		/* List to which array names are appended. */
    int flags)			/* OR-ed combination of TCL_GLOBAL_ONLY and
				 * TCL_NAMESPACE_ONLY, and at most one of
				 * TCL_MATCH_EXACT, _GLOB, and _REGEXP. */
{
    int traceFail = 0;
    Var *varPtr = ArrayVar(interp, part1Ptr, &traceFail, flags);

    if (varPtr) {
	return ArrayNames(interp, varPtr, part2Ptr, flags & TCL_MATCH, listPtr);
    } else if (traceFail) {
	return TCL_ERROR;
    } else {
	return TCL_OK;
    }
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ArraySize --
 *
 *	This function reports the number of elements in an array variable. It
 *	provides C-level access to [array size] functionality. If part2Ptr is
 *	not NULL, only array elements whose names match part2Ptr are counted
 *	toward the return value. The interpretation of part2Ptr is controlled by
 *	the TCL_MATCH_* bits within flags.
 *
 * Results:
 *	The return value is normally TCL_OK; in this case *intPtr will be set to
 *	the integer count of array elements whose names match the given filter.
 *	If varNamePtr does not name an array, TCL_OK is returned and *intPtr is
 *	set to 0. If an array trace error occurs, or if there is an error in the
 *	filter (e.g. bad regular expression), TCL_ERROR is returned and an error
 *	message is left in the interpreter's result.
 *
 * Side effects:
 *	Array traces, if any, are executed.
 *	
 *----------------------------------------------------------------------
 */

int
Tcl_ArraySize(
    Tcl_Interp *interp,		/* Command interpreter in which part1Ptr is to
				 * be looked up. */
    Tcl_Obj *part1Ptr,		/* Name of array variable in interp. */
    Tcl_Obj *part2Ptr,		/* Element filter or NULL to accept all. */
    int *intPtr,		/* Location to which size is written. */
    int flags)			/* OR-ed combination of TCL_GLOBAL_ONLY and
				 * TCL_NAMESPACE_ONLY, and at most one of
				 * TCL_MATCH_EXACT, _GLOB, and _REGEXP. */
{
    Var *varPtr;
    ArraySearch search;
    int fail = 0, size = 0;

    if ((varPtr = ArrayVar(interp, part1Ptr, &fail, flags))) {
	/*
	 * Determine the size by counting the number of times ArrayFirst() or
	 * ArrayNext() returns non-NULL.
	 */

	search.varPtr = varPtr;
	search.filterObj = part2Ptr;
	search.filterType = flags & TCL_MATCH;
	search.flags = 0;
	for (varPtr = ArrayFirst(interp, &search, &fail); varPtr;
		varPtr = ArrayNext(interp, &search, &fail)) {
	    ++size;
	}
    }
	
    if (fail) {
	return TCL_ERROR;
    }

    *intPtr = size;
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ArrayExists --
 *
 *	This function checks if an array or array element exists. If part2Ptr is
 *	NULL, the existence of the array is checked. If part2Ptr is not NULL,
 *	this function instead checks if at least one array element name matches
 *	the filter specified by part2Ptr and the TCL_MATCH_* bits within flags.
 *
 * Results:
 *	*intPtr is set to 1 or 0 if the array or array element does or does not
 *	exist, and TCL_OK is returned. *intPtr is also set to 0 if a variable
 *	with the given name exists but is not an array, as well as in event of
 *	lookup error such as nonexistent namespace. If an array trace error
 *	occurs, TCL_ERROR is returned and *intPtr is not modified.
 *
 * Side effects:
 *	Array traces, if any, are executed.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_ArrayExists(
    Tcl_Interp *interp,		/* Interpreter in which to look up variable. */
    Tcl_Obj *part1Ptr,		/* Name of array variable in interp. */
    Tcl_Obj *part2Ptr,		/* Element to check or NULL to check array. */
    int *intPtr,		/* Set to 1 if array exists, 0 if not. */
    int flags)			/* OR-ed combination of TCL_GLOBAL_ONLY and
				 * TCL_NAMESPACE_ONLY, and at most one of
				 * TCL_MATCH_EXACT, _GLOB, and _REGEXP. */
{
    ArraySearch search;
    int fail = 0;
    Var *varPtr = ArrayVar(interp, part1Ptr, &fail, flags);

    /*
     * If the array lookup succeeded and a filter was specified, attempt to find
     * the first element which matches the filter.
     */

    if (varPtr && part2Ptr) {
	search.varPtr = varPtr;
	search.filterObj = part2Ptr;
	search.filterType = flags & TCL_MATCH;
	search.flags = 0;
	varPtr = ArrayFirst(interp, &search, &fail);
    }

    if (varPtr) {
	*intPtr = 1;
    } else if (fail) {
	return TCL_ERROR;
    } else {
	*intPtr = 0;
    }

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ArraySearchStart --
 *
 *	This function initiates an array search, i.e. step-by-step array element
 *	enumeration. It provides C-level access to [array startsearch]. The
 *	returned value is used to obtain one array element name at a time. If
 *	part2Ptr is not NULL, only array elements whose names match part2Ptr are
 *	returned by future calls to Tcl_ArraySearchNext(). The interpretation of
 *	part2Ptr is controlled by the TCL_MATCH_* bits within flags.
 *
 * Important note:
 *	Unlike all the other Tcl_Array*() functions, this function reports array
 *	existence errors rather than treating nonexistent and scalar variables
 *	as if they were empty arrays.
 *
 * Results:
 *	A new array search is created and returned. If part1Ptr does not name an
 *	array, if there is a problem with the filter, or if an array trace error
 *	occurred, no search is created, NULL is returned, and error information
 *	is placed in the interpreter.
 *
 * Side effects:
 *	On success, the search is allocated on the heap. To avoid a memory leak,
 *	the search must be deallocated by Tcl_ArraySearchDone() when complete.
 *	Array traces, if any, are executed.
 *
 *----------------------------------------------------------------------
 */

Tcl_ArraySearch
Tcl_ArraySearchStart(
    Tcl_Interp *interp,		/* Command interpreter in which part1Ptr is to
				 * be looked up. */
    Tcl_Obj *part1Ptr,		/* Name of array variable in interp. */
    Tcl_Obj *part2Ptr,		/* Element filter or NULL to accept all. */
    int flags)			/* OR-ed combination of TCL_GLOBAL_ONLY and
                                 * TCL_NAMESPACE_ONLY, and at most one of
                                 * TCL_MATCH_EXACT, _GLOB, and _REGEXP. */
{
    Interp *iPtr = (Interp *)interp;
    Var *varPtr = ArrayVar(interp, part1Ptr, NULL, flags | TCL_LEAVE_ERR_MSG);
    Tcl_HashEntry *hPtr;
    int isNew, fail = 0;
    ArraySearch search, *searchPtr;

    /*
     * Handle the possible error cases before performing any allocations.
     */

    if (!varPtr) {
	return NULL;
    }

    search.nextEntry = ArrayFirst(interp, &search, &fail);
    if (!search.nextEntry && fail) {
	return NULL;
    }
    search.name = NULL;
    search.varNameObj = part1Ptr;
    search.varPtr = varPtr;
    search.filterObj = part2Ptr;
    search.filterType = flags & TCL_MATCH;
    search.flags = KEEP_ON_ABORT;

    /*
     * Make a new array search.
     */

    hPtr = Tcl_CreateHashEntry(&iPtr->varSearches, varPtr, &isNew);
    if (isNew) {
	search.id = 1;
	varPtr->flags |= VAR_SEARCH_ACTIVE;
	search.nextPtr = NULL;
    } else {
	search.id = ((ArraySearch *)Tcl_GetHashValue(hPtr))->id + 1;
	search.nextPtr = Tcl_GetHashValue(hPtr);
    }
    Tcl_IncrRefCount(search.varNameObj);
    if (part2Ptr) {
	Tcl_IncrRefCount(part2Ptr);
    }
    searchPtr = ckalloc(sizeof(*searchPtr));
    *searchPtr = search;
    Tcl_SetHashValue(hPtr, searchPtr);

    return searchPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ArraySearchPeek --
 *
 *	Finds the next element of an array for a given search query. Unlike
 *	Tcl_ArraySearchNext(), the only side effect is array traces, so the
 *	search query state is not advanced and the element is not consumed.
 *
 * Preconditions, results:
 *	Same as Tcl_ArraySearchNext().
 *
 * Side effects:
 *	Array traces, if any, are executed.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_ArraySearchPeek(
    Tcl_Interp *interp,		/* Command interpreter in which the array
				 * variable is located. */
    Tcl_ArraySearch search,	/* Prior return from Tcl_ArraySearchStart(). */
    Tcl_Obj **keyPtrPtr,	/* Location to which pointer to next array
				 * element name is written. NULL is written when
				 * the end of the array has been encountered. */
    Tcl_Obj **valuePtrPtr)	/* If not NULL, location to which pointer to
				 * next array element value is written. NULL is
				 * written when at the end of the array. */
{
    Tcl_Obj *keyObj, *valueObj;

    /*
     * Report aborted searches.
     */

    if (ArrayAborted(interp, search, 0) != TCL_OK) {
	return TCL_ERROR;
    }

    /*
     * Execute array traces and report any errors that may arise.
     */

    if (ArrayVarTrace(interp, search->varPtr, search->varNameObj) != TCL_OK) {
	return TCL_ERROR;
    }

    /*
     * Get the next array element, but push it back into the nextEntry buffer so
     * that it will be reused when ArrayNext() is called again.
     */

    if ((search->nextEntry = ArrayNext(interp, search, NULL))) {
	/*
	 * If not at the end of the array, get the element name and (if an
	 * output location was given) element value. Report any errors that may
	 * occur due to reading the element value if it is requested.
	 */

	keyObj = VarHashGetKey(search->nextEntry);
	if (valuePtrPtr) {
	    if ((valueObj = Tcl_ObjGetVar2(interp, search->varNameObj, keyObj,
		    TCL_LEAVE_ERR_MSG))) {
		*valuePtrPtr = valueObj;
	    } else {
		return TCL_ERROR;
	    }
	}
	*keyPtrPtr = keyObj;
    } else {
	/*
	 * At the end of the array, store NULL for the element name and (if an
	 * output location was given) element value.
	 */

	*keyPtrPtr = NULL;
	if (valuePtrPtr) {
	    *valuePtrPtr = NULL;
	}
    }

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ArraySearchNext --
 *
 *	Finds the next element of an array for a given search query.
 *
 * Preconditions:
 *	The search argument must be the return value of Tcl_ArraySearchStart()
 *	and must not have been passed to Tcl_ArraySearchDone().
 *
 * Results:
 *	The next array element name is written to *keyPtrPtr. If valuePtrPtr is
 *	not NULL, the array element value is written to *valuePtrPtr. If there
 *	are no more array elements, NULL is written instead. If an array trace
 *	or variable read trace error occurs, TCL_ERROR is returned and error
 *	information is placed in the interpreter result.
 *
 * Side effects:
 *	The search data structure is updated such that successive invocations of
 *	this function will return successive array element names.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_ArraySearchNext(
    Tcl_Interp *interp,		/* Command interpreter in which the array
				 * variable is located. */
    Tcl_ArraySearch search,	/* Prior return from Tcl_ArraySearchStart(). */
    Tcl_Obj **keyPtrPtr,	/* Location to which pointer to next array
				 * element name is written. NULL is written when
				 * the end of the array has been encountered. */
    Tcl_Obj **valuePtrPtr)	/* If not NULL, location to which pointer to
				 * next array element value is written. NULL is
				 * written when at the end of the array. */
{
    /*
     * Let Tcl_ArraySearchPeek() do all the work, then clear the search
     * structure's nextEntry buffer so that the search will advance.
     */

    if (Tcl_ArraySearchPeek(interp, search, keyPtrPtr, valuePtrPtr) != TCL_OK) {
	return TCL_ERROR;
    }

    search->nextEntry = NULL;
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ArraySearchDone --
 *
 *	Terminates and cleans up an array search query.
 *
 * Preconditions:
 *	The search argument must be the return value of Tcl_ArraySearchStart()
 *	and must not have been passed to Tcl_ArraySearchDone().
 *
 * Results:
 *	Normally, the search query is completed, and TCL_OK is returned. If an
 *	array trace has an error, TCL_ERROR is returned and error information is
 *	placed in the interpreter result.
 *
 * Side effects:
 *	Resources associated with the search are deallocated. Array traces, if
 *	any, are executed.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_ArraySearchDone(
    Tcl_Interp *interp,		/* Command interpreter in which the array
				 * variable is located. */
    Tcl_ArraySearch search)	/* Prior return from Tcl_ArraySearchStart(). */
{
    /*
     * Report and deallocate aborted searches.
     */

    if (ArrayAborted(interp, search, 1) != TCL_OK) {
	return TCL_ERROR;
    }

    /*
     * Execute array traces and report any errors that may arise.
     */

    if (ArrayVarTrace(interp, search->varPtr, search->varNameObj) != TCL_OK) {
	return TCL_ERROR;
    }

    ArrayDone(interp, search);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ArrayStatistics --
 *
 *	Returns statistics about the distribution of data within the hash table
 *	that represents the array. This information includes the number of
 *	entries in the table, the number of buckets, and the utilization of the
 *	buckets.
 *
 * Results:
 *	Normally, TCL_OK is returned, and the statistics information is appended
 *	to stringPtr. If part1Ptr does not name an array, or if an array trace
 *	error occurs, TCL_ERROR is returned and error information is left in the
 *	interpreter result.
 *
 * Side effects:
 *	Array traces, if any, are executed.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_ArrayStatistics(
    Tcl_Interp *interp,		/* Interpreter containing the variable. */
    Tcl_Obj *part1Ptr,		/* Name of the array variable. */
    Tcl_Obj *stringPtr,		/* String to which statistics is appended. */
    int flags)			/* OR-ed combination of TCL_GLOBAL_ONLY and
				 * TCL_NAMESPACE_ONLY. */
{
    Var *varPtr = ArrayVar(interp, part1Ptr, NULL, flags | TCL_LEAVE_ERR_MSG);
    char *stats;

    if (!varPtr) {
	return TCL_ERROR;
    }

    stats = Tcl_HashStats((Tcl_HashTable *)varPtr->value.tablePtr);
    Tcl_AppendToObj(stringPtr, stats, -1);
    ckfree(stats);

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetVar --
 *
 *	Return the value of a Tcl variable as a string.
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
     * Finally, if the variable is truly not in use then free up its Var
     * structure and remove it from its hash table, if any. The ref count of
     * its value object, if any, was decremented above.
     */

    if (TclIsVarInHash(varPtr)) {
	VarHashRefCount(varPtr)--;
	CleanupVar(varPtr, arrayPtr);
    }
    return result;
}

/*
 *----------------------------------------------------------------------
 *







|







3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
     * Finally, if the variable is truly not in use then free up its Var
     * structure and remove it from its hash table, if any. The ref count of
     * its value object, if any, was decremented above.
     */

    if (TclIsVarInHash(varPtr)) {
	VarHashRefCount(varPtr)--;
	TclCleanupVar(varPtr, arrayPtr);
    }
    return result;
}

/*
 *----------------------------------------------------------------------
 *
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
	 * referenced variable if it's no longer needed.
	 */

	Var *linkPtr = dummyVar.value.linkPtr;

	if (TclIsVarInHash(linkPtr)) {
	    VarHashRefCount(linkPtr)--;
	    CleanupVar(linkPtr, NULL);
	}
    }

    /*
     * If the variable was a namespace variable, decrement its reference
     * count.
     */







|







3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
	 * referenced variable if it's no longer needed.
	 */

	Var *linkPtr = dummyVar.value.linkPtr;

	if (TclIsVarInHash(linkPtr)) {
	    VarHashRefCount(linkPtr)--;
	    TclCleanupVar(linkPtr, NULL);
	}
    }

    /*
     * If the variable was a namespace variable, decrement its reference
     * count.
     */
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908

2909
2910
2911

2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927

2928


2929

2930
2931
2932

2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
    Tcl_SetObjResult(interp, newValuePtr);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TclArraySet --
 *
 *	Set the elements of an array. If there are no elements to set, create
 *	an empty array. This routine is used by the Tcl_ArrayObjCmd and by the
 *	TclSetupEnv routine.
 *
 * Results:
 *	A standard Tcl result object.
 *
 * Side effects:
 *	A variable will be created if one does not already exist.
 *	Callers must Incr arrayNameObj if they pland to Decr it.
 *
 *----------------------------------------------------------------------
 */

int
TclArraySet(
    Tcl_Interp *interp,		/* Current interpreter. */
    Tcl_Obj *arrayNameObj,	/* The array name. */
    Tcl_Obj *arrayElemObj)	/* The array elements list or dict. If this is
				 * NULL, create an empty array. */
{
    Var *varPtr, *arrayPtr;
    int result, i;

    varPtr = TclObjLookupVarEx(interp, arrayNameObj, NULL,
	    /*flags*/ TCL_LEAVE_ERR_MSG, /*msg*/ "set", /*createPart1*/ 1,
	    /*createPart2*/ 1, &arrayPtr);
    if (varPtr == NULL) {
	return TCL_ERROR;
    }
    if (arrayPtr) {
	CleanupVar(varPtr, arrayPtr);
	TclObjVarErrMsg(interp, arrayNameObj, NULL, "set", needArray, -1);
	Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "VARNAME",
		TclGetString(arrayNameObj), NULL);
	return TCL_ERROR;
    }

    if (arrayElemObj == NULL) {
	goto ensureArray;
    }

    /*
     * Install the contents of the dictionary or list into the array.
     */

    if (arrayElemObj->typePtr == &tclDictType) {
	Tcl_Obj *keyPtr, *valuePtr;
	Tcl_DictSearch search;
	int done;

	if (Tcl_DictObjSize(interp, arrayElemObj, &done) != TCL_OK) {
	    return TCL_ERROR;
	}
	if (done == 0) {
	    /*
	     * Empty, so we'll just force the array to be properly existing
	     * instead.
	     */

	    goto ensureArray;
	}

	/*
	 * Don't need to look at result of Tcl_DictObjFirst as we've just
	 * successfully used a dictionary operation on the same object.
	 */

	for (Tcl_DictObjFirst(interp, arrayElemObj, &search,
		&keyPtr, &valuePtr, &done) ; !done ;
		Tcl_DictObjNext(&search, &keyPtr, &valuePtr, &done)) {
	    /*
	     * At this point, it would be nice if the key was directly usable
	     * by the array. This isn't the case though.
	     */

	    Var *elemVarPtr = TclLookupArrayElement(interp, arrayNameObj,
		    keyPtr, TCL_LEAVE_ERR_MSG, "set", 1, 1, varPtr, -1);

	    if ((elemVarPtr == NULL) ||
		    (TclPtrSetVar(interp, elemVarPtr, varPtr, arrayNameObj,
		    keyPtr, valuePtr, TCL_LEAVE_ERR_MSG, -1) == NULL)) {
		Tcl_DictObjDone(&search);
		return TCL_ERROR;
	    }
	}
	return TCL_OK;
    } else {
	/*
	 * Not a dictionary, so assume (and convert to, for backward-
	 * -compatibility reasons) a list.
	 */

	int elemLen;
	Tcl_Obj **elemPtrs, *copyListObj;

	result = TclListObjGetElements(interp, arrayElemObj,
		&elemLen, &elemPtrs);
	if (result != TCL_OK) {
	    return result;
	}
	if (elemLen & 1) {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj(
		    "list must have an even number of elements", -1));
	    Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "FORMAT", NULL);
	    return TCL_ERROR;
	}
	if (elemLen == 0) {
	    goto ensureArray;
	}

	/*
	 * We needn't worry about traces invalidating arrayPtr: should that be
	 * the case, TclPtrSetVar will return NULL so that we break out of the
	 * loop and return an error.
	 */

	copyListObj = TclListObjCopy(NULL, arrayElemObj);
	for (i=0 ; i<elemLen ; i+=2) {
	    Var *elemVarPtr = TclLookupArrayElement(interp, arrayNameObj,
		    elemPtrs[i], TCL_LEAVE_ERR_MSG, "set", 1, 1, varPtr, -1);

	    if ((elemVarPtr == NULL) ||
		    (TclPtrSetVar(interp, elemVarPtr, varPtr, arrayNameObj,
		    elemPtrs[i],elemPtrs[i+1],TCL_LEAVE_ERR_MSG,-1) == NULL)){
		result = TCL_ERROR;
		break;
	    }
	}
	Tcl_DecrRefCount(copyListObj);
	return result;
    }

    /*
     * The list is empty make sure we have an array, or create one if
     * necessary.
     */

  ensureArray:
    if (varPtr != NULL) {
	if (TclIsVarArray(varPtr)) {
	    /*
	     * Already an array, done.
	     */

	    return TCL_OK;
	}
	if (TclIsVarArrayElement(varPtr) || !TclIsVarUndefined(varPtr)) {
	    /*
	     * Either an array element, or a scalar: lose!
	     */

	    TclObjVarErrMsg(interp, arrayNameObj, NULL, "array set",
		    needArray, -1);
	    Tcl_SetErrorCode(interp, "TCL", "WRITE", "ARRAY", NULL);
	    return TCL_ERROR;
	}
    }
    TclSetVarArray(varPtr);
    varPtr->value.tablePtr = ckalloc(sizeof(TclVarHashTable));
    TclInitVarHashTable(varPtr->value.tablePtr, TclGetVarNsPtr(varPtr));
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayStartSearchCmd --
 *
 *	This object-based function is invoked to process the "array
 *	startsearch" Tcl command. See the user documentation for details on
 *	what it does.
 *
 * Results:
 *	A standard Tcl result object.
 *
 * Side effects:
 *	See the user documentation.
 *
 *----------------------------------------------------------------------
 */

	/* ARGSUSED */

static Var *
VerifyArray(
    Tcl_Interp *interp,
    Tcl_Obj *varNameObj)
{
    Interp *iPtr = (Interp *) interp;
    const char *varName = TclGetString(varNameObj);
    Var *arrayPtr;

    /*
     * Locate the array variable.
     */

    Var *varPtr = TclObjLookupVarEx(interp, varNameObj, NULL, /*flags*/ 0,
	    /*msg*/ 0, /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr);

    /*
     * Special array trace used to keep the env array in sync for array names,
     * array get, etc.
     */

    if (varPtr && (varPtr->flags & VAR_TRACED_ARRAY)
	    && (TclIsVarArray(varPtr) || TclIsVarUndefined(varPtr))) {
	if (TclObjCallVarTraces(iPtr, arrayPtr, varPtr, varNameObj, NULL,
		(TCL_LEAVE_ERR_MSG|TCL_NAMESPACE_ONLY|TCL_GLOBAL_ONLY|
		TCL_TRACE_ARRAY), /* leaveErrMsg */ 1, -1) == TCL_ERROR) {
	    return NULL;
	}
    }

    /*
     * Verify that it is indeed an array variable. This test comes after the
     * traces - the variable may actually become an array as an effect of said
     * traces.
     */

    if ((varPtr == NULL) || !TclIsVarArray(varPtr) || TclIsVarUndefined(varPtr)) {
	Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		"\"%s\" isn't an array", varName));
	Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ARRAY", varName, NULL);
	return NULL;
    }

    return varPtr;
}

static int
ArrayStartSearchCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr;
    Tcl_HashEntry *hPtr;
    int isNew;
    ArraySearch *searchPtr;


    if (objc != 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName");

	return TCL_ERROR;
    }

    varPtr = VerifyArray(interp, objv[1]);
    if (varPtr == NULL) {
	return TCL_ERROR;
    }

    /*
     * Make a new array search with a free name.
     */

    searchPtr = ckalloc(sizeof(ArraySearch));
    hPtr = Tcl_CreateHashEntry(&iPtr->varSearches, varPtr, &isNew);
    if (isNew) {
	searchPtr->id = 1;

	varPtr->flags |= VAR_SEARCH_ACTIVE;


	searchPtr->nextPtr = NULL;

    } else {
	searchPtr->id = ((ArraySearch *) Tcl_GetHashValue(hPtr))->id + 1;
	searchPtr->nextPtr = Tcl_GetHashValue(hPtr);

    }
    searchPtr->varPtr = varPtr;
    searchPtr->nextEntry = VarHashFirstEntry(varPtr->value.tablePtr,
	    &searchPtr->search);
    Tcl_SetHashValue(hPtr, searchPtr);
    searchPtr->name = Tcl_ObjPrintf("s-%d-%s", searchPtr->id, TclGetString(objv[1]));
    Tcl_IncrRefCount(searchPtr->name);
    Tcl_SetObjResult(interp, searchPtr->name);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayAnyMoreCmd --







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
















<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







<
|
<
<

>

<
|
>



|
|




|


|
|
<
|
>
|
>
>
|
>
|
<
<
>
|
|
|
<
<
<
<
|







4234
4235
4236
4237
4238
4239
4240









































































































































































4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256















































4257
4258
4259
4260
4261
4262
4263

4264


4265
4266
4267

4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283

4284
4285
4286
4287
4288
4289
4290
4291


4292
4293
4294
4295




4296
4297
4298
4299
4300
4301
4302
4303
    Tcl_SetObjResult(interp, newValuePtr);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *









































































































































































 * ArrayStartSearchCmd --
 *
 *	This object-based function is invoked to process the "array
 *	startsearch" Tcl command. See the user documentation for details on
 *	what it does.
 *
 * Results:
 *	A standard Tcl result object.
 *
 * Side effects:
 *	See the user documentation.
 *
 *----------------------------------------------------------------------
 */

	/* ARGSUSED */















































static int
ArrayStartSearchCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{

    Tcl_Obj *varNameObj, *filterObj, *tokenObj;


    ArraySearch *searchPtr;
    int filterType;


    if (ArrayArgs(interp, objc, objv, &varNameObj,
	    &filterObj, &filterType) != TCL_OK) {
	return TCL_ERROR;
    }

    if (!(searchPtr = Tcl_ArraySearchStart(interp, varNameObj, filterObj,
	    TCL_LEAVE_ERR_MSG | filterType))) {
	return TCL_ERROR;
    }

    /*
     * Give the search a name so it can be looked up by other array commands.
     */

    tokenObj = Tcl_ObjPrintf("s-%d-%s", searchPtr->id,
	    TclGetString(varNameObj));

    searchPtr->name = tokenObj;
    Tcl_IncrRefCount(tokenObj);

    /*
     * Clear the KEEP_ON_ABORT flag which was set by Tcl_ArraySearchStart() so
     * the search structure will automatically be deallocated should the search
     * terminate early due to array elements being added or removed or the array
     * itself being unset.


     */

    searchPtr->flags = 0;





    Tcl_SetObjResult(interp, tokenObj);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayAnyMoreCmd --
2965
2966
2967
2968
2969
2970
2971
2972
2973

2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998

2999
3000
3001
3002
3003
3004
3005

3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr;
    Tcl_Obj *varNameObj, *searchObj;
    int gotValue;
    ArraySearch *searchPtr;


    if (objc != 3) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName searchId");
	return TCL_ERROR;
    }
    varNameObj = objv[1];
    searchObj = objv[2];

    varPtr = VerifyArray(interp, varNameObj);
    if (varPtr == NULL) {
	return TCL_ERROR;
    }

    /*
     * Get the search.
     */

    searchPtr = ParseSearchId(interp, varPtr, varNameObj, searchObj);
    if (searchPtr == NULL) {
	return TCL_ERROR;
    }

    /*
     * Scan forward to find if there are any further elements in the array
     * that are defined.

     */

    while (1) {
	if (searchPtr->nextEntry != NULL) {
	    varPtr = VarHashGetValue(searchPtr->nextEntry);
	    if (!TclIsVarUndefined(varPtr)) {
		gotValue = 1;

		break;
	    }
	}
	searchPtr->nextEntry = Tcl_NextHashEntry(&searchPtr->search);
	if (searchPtr->nextEntry == NULL) {
	    gotValue = 0;
	    break;
	}
    }
    Tcl_SetObjResult(interp, iPtr->execEnvPtr->constants[gotValue]);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *







<

>








|
<













|
|
>


<
|
<
<
|
>
|
|
<
<
<
|
<
|
|







4321
4322
4323
4324
4325
4326
4327

4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338

4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356

4357


4358
4359
4360
4361



4362

4363
4364
4365
4366
4367
4368
4369
4370
4371
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr;
    Tcl_Obj *varNameObj, *searchObj;

    ArraySearch *searchPtr;
    int gotValue, fail = 0;

    if (objc != 3) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName searchId");
	return TCL_ERROR;
    }
    varNameObj = objv[1];
    searchObj = objv[2];

    if (!(varPtr = ArrayVar(interp, varNameObj, NULL, TCL_LEAVE_ERR_MSG))) {

	return TCL_ERROR;
    }

    /*
     * Get the search.
     */

    searchPtr = ParseSearchId(interp, varPtr, varNameObj, searchObj);
    if (searchPtr == NULL) {
	return TCL_ERROR;
    }

    /*
     * Scan forward to find if there are any further matching elements in the
     * array. Put the found element (if any) into searchPtr->nextEntry so that
     * it is not consumed and is available for the next call.
     */


    if ((searchPtr->nextEntry = ArrayNext(interp, searchPtr, &fail))) {


	gotValue = 1;
    } else if (fail) {
	return TCL_ERROR;
    } else {



	gotValue = 0;

    }

    Tcl_SetObjResult(interp, iPtr->execEnvPtr->constants[gotValue]);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
static int
ArrayNextElementCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Var *varPtr;
    Tcl_Obj *varNameObj, *searchObj;
    ArraySearch *searchPtr;

    if (objc != 3) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName searchId");
	return TCL_ERROR;
    }
    varNameObj = objv[1];
    searchObj = objv[2];

    varPtr = VerifyArray(interp, varNameObj);
    if (varPtr == NULL) {
	return TCL_ERROR;
    }

    /*
     * Get the search.
     */

    searchPtr = ParseSearchId(interp, varPtr, varNameObj, searchObj);
    if (searchPtr == NULL) {
	return TCL_ERROR;
    }

    /*
     * Get the next element from the search, or the empty string on
     * exhaustion. Note that the [array anymore] command may well have already
     * pulled a value from the hash enumeration, so we have to check the cache
     * there first.
     */

    while (1) {
	Tcl_HashEntry *hPtr = searchPtr->nextEntry;

	if (hPtr == NULL) {
	    hPtr = Tcl_NextHashEntry(&searchPtr->search);
	    if (hPtr == NULL) {
		return TCL_OK;
	    }
	} else {
	    searchPtr->nextEntry = NULL;
	}
	varPtr = VarHashGetValue(hPtr);
	if (!TclIsVarUndefined(varPtr)) {
	    Tcl_SetObjResult(interp, VarHashGetKey(varPtr));
	    return TCL_OK;
	}
    }
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayDoneSearchCmd --
 *







|










|
<







|
<




|
<
<
<


<
|
|
<
<
<
<
|
<
<
|
<
<
<
|
<
<







4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406

4407
4408
4409
4410
4411
4412
4413
4414

4415
4416
4417
4418
4419



4420
4421

4422
4423




4424


4425



4426


4427
4428
4429
4430
4431
4432
4433
static int
ArrayNextElementCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Var *varPtr, *elemPtr;
    Tcl_Obj *varNameObj, *searchObj;
    ArraySearch *searchPtr;

    if (objc != 3) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName searchId");
	return TCL_ERROR;
    }
    varNameObj = objv[1];
    searchObj = objv[2];

    if (!(varPtr = ArrayVar(interp, varNameObj, NULL, TCL_LEAVE_ERR_MSG))) {

	return TCL_ERROR;
    }

    /*
     * Get the search.
     */

    if (!(searchPtr = ParseSearchId(interp, varPtr, varNameObj, searchObj))) {

	return TCL_ERROR;
    }

    /*
     * Get the next element from the search, or the empty string on exhaustion.



     */


    if ((elemPtr = ArrayNext(interp, searchPtr, NULL))) {
	Tcl_SetObjResult(interp, VarHashGetKey(elemPtr));




    }






    return TCL_OK;


}

/*
 *----------------------------------------------------------------------
 *
 * ArrayDoneSearchCmd --
 *
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
static int
ArrayDoneSearchCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr;
    Tcl_HashEntry *hPtr;
    Tcl_Obj *varNameObj, *searchObj;
    ArraySearch *searchPtr, *prevPtr;

    if (objc != 3) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName searchId");
	return TCL_ERROR;
    }
    varNameObj = objv[1];
    searchObj = objv[2];

    varPtr = VerifyArray(interp, varNameObj);
    if (varPtr == NULL) {
	return TCL_ERROR;
    }

    /*
     * Get the search.
     */

    searchPtr = ParseSearchId(interp, varPtr, varNameObj, searchObj);
    if (searchPtr == NULL) {
	return TCL_ERROR;
    }

    /*
     * Unhook the search from the list of searches associated with the
     * variable.
     */

    hPtr = Tcl_FindHashEntry(&iPtr->varSearches, varPtr);
    if (searchPtr == Tcl_GetHashValue(hPtr)) {
	if (searchPtr->nextPtr) {
	    Tcl_SetHashValue(hPtr, searchPtr->nextPtr);
	} else {
	    varPtr->flags &= ~VAR_SEARCH_ACTIVE;
	    Tcl_DeleteHashEntry(hPtr);
	}
    } else {
	for (prevPtr=Tcl_GetHashValue(hPtr) ;; prevPtr=prevPtr->nextPtr) {
	    if (prevPtr->nextPtr == searchPtr) {
		prevPtr->nextPtr = searchPtr->nextPtr;
		break;
	    }
	}
    }
    Tcl_DecrRefCount(searchPtr->name);
    ckfree(searchPtr);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayExistsCmd --







<

<

|








|
<







|
<








<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|







4448
4449
4450
4451
4452
4453
4454

4455

4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466

4467
4468
4469
4470
4471
4472
4473
4474

4475
4476
4477
4478
4479
4480
4481
4482

















4483
4484
4485
4486
4487
4488
4489
4490
static int
ArrayDoneSearchCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{

    Var *varPtr;

    Tcl_Obj *varNameObj, *searchObj;
    ArraySearch *searchPtr;

    if (objc != 3) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName searchId");
	return TCL_ERROR;
    }
    varNameObj = objv[1];
    searchObj = objv[2];

    if (!(varPtr = ArrayVar(interp, varNameObj, NULL, TCL_LEAVE_ERR_MSG))) {

	return TCL_ERROR;
    }

    /*
     * Get the search.
     */

    if (!(searchPtr = ParseSearchId(interp, varPtr, varNameObj, searchObj))) {

	return TCL_ERROR;
    }

    /*
     * Unhook the search from the list of searches associated with the
     * variable.
     */


















    ArrayDone(interp, searchPtr);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayExistsCmd --
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206

3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
ArrayExistsCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr, *arrayPtr;
    Tcl_Obj *arrayNameObj;
    int notArray;

    if (objc != 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName");

	return TCL_ERROR;
    }
    arrayNameObj = objv[1];

    /*
     * Locate the array variable.
     */

    varPtr = TclObjLookupVarEx(interp, arrayNameObj, NULL, /*flags*/ 0,
	    /*msg*/ 0, /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr);

    /*
     * Special array trace used to keep the env array in sync for array names,
     * array get, etc.
     */

    if (varPtr && (varPtr->flags & VAR_TRACED_ARRAY)
	    && (TclIsVarArray(varPtr) || TclIsVarUndefined(varPtr))) {
	if (TclObjCallVarTraces(iPtr, arrayPtr, varPtr, arrayNameObj, NULL,
		(TCL_LEAVE_ERR_MSG|TCL_NAMESPACE_ONLY|TCL_GLOBAL_ONLY|
		TCL_TRACE_ARRAY), /* leaveErrMsg */ 1, -1) == TCL_ERROR) {
	    return TCL_ERROR;
	}
    }

    /*
     * Check whether we've actually got an array variable.
     */

    notArray = ((varPtr == NULL) || !TclIsVarArray(varPtr)
	    || TclIsVarUndefined(varPtr));
    Tcl_SetObjResult(interp, iPtr->execEnvPtr->constants[!notArray]);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayGetCmd --







<
|
|

<
|
>


<

<
<
<
|
<
<
|
<
<
<
<
<
<
<
<
<
<
|
|
|
<
<
<
<
<
<
<
|







4506
4507
4508
4509
4510
4511
4512

4513
4514
4515

4516
4517
4518
4519

4520



4521


4522










4523
4524
4525







4526
4527
4528
4529
4530
4531
4532
4533
ArrayExistsCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;

    Tcl_Obj *varNameObj, *filterObj;
    int exists, filterType;


    if (ArrayArgs(interp, objc, objv, &varNameObj,
	    &filterObj, &filterType) != TCL_OK) {
	return TCL_ERROR;
    }





    if (Tcl_ArrayExists(interp, varNameObj, filterObj, &exists,


	    filterType) != TCL_OK) {










	return TCL_ERROR;
    }








    Tcl_SetObjResult(interp, iPtr->execEnvPtr->constants[exists]);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayGetCmd --
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
static int
ArrayGetCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr, *arrayPtr, *varPtr2;
    Tcl_Obj *varNameObj, *nameObj, *valueObj, *nameLstObj, *tmpResObj;
    Tcl_Obj **nameObjPtr, *patternObj;
    Tcl_HashSearch search;
    const char *pattern;
    int i, count, result;

    switch (objc) {
    case 2:
	varNameObj = objv[1];
	patternObj = NULL;
	break;
    case 3:
	varNameObj = objv[1];
	patternObj = objv[2];
	break;
    default:
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName ?pattern?");
	return TCL_ERROR;
    }

    /*
     * Locate the array variable.
     */

    varPtr = TclObjLookupVarEx(interp, varNameObj, NULL, /*flags*/ 0,
	    /*msg*/ 0, /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr);

    /*
     * Special array trace used to keep the env array in sync for array names,
     * array get, etc.
     */

    if (varPtr && (varPtr->flags & VAR_TRACED_ARRAY)
	    && (TclIsVarArray(varPtr) || TclIsVarUndefined(varPtr))) {
	if (TclObjCallVarTraces(iPtr, arrayPtr, varPtr, varNameObj, NULL,
		(TCL_LEAVE_ERR_MSG|TCL_NAMESPACE_ONLY|TCL_GLOBAL_ONLY|
		TCL_TRACE_ARRAY), /* leaveErrMsg */ 1, -1) == TCL_ERROR) {
	    return TCL_ERROR;
	}
    }

    /*
     * Verify that it is indeed an array variable. This test comes after the
     * traces - the variable may actually become an array as an effect of said
     * traces. If not an array, it's an empty result.
     */

    if ((varPtr == NULL) || !TclIsVarArray(varPtr)
	    || TclIsVarUndefined(varPtr)) {
	return TCL_OK;
    }

    pattern = (patternObj ? TclGetString(patternObj) : NULL);

    /*
     * Store the array names in a new object.
     */

    TclNewObj(nameLstObj);
    Tcl_IncrRefCount(nameLstObj);
    if ((patternObj != NULL) && TclMatchIsTrivial(pattern)) {
	varPtr2 = VarHashFindVar(varPtr->value.tablePtr, patternObj);
	if (varPtr2 == NULL) {
	    goto searchDone;
	}
	if (TclIsVarUndefined(varPtr2)) {
	    goto searchDone;
	}
	result = Tcl_ListObjAppendElement(interp, nameLstObj,
		VarHashGetKey(varPtr2));
	if (result != TCL_OK) {
	    TclDecrRefCount(nameLstObj);
	    return result;
	}
	goto searchDone;
    }

    for (varPtr2 = VarHashFirstVar(varPtr->value.tablePtr, &search);
	    varPtr2; varPtr2 = VarHashNextVar(&search)) {
	if (TclIsVarUndefined(varPtr2)) {
	    continue;
	}
	nameObj = VarHashGetKey(varPtr2);
	if (patternObj && !Tcl_StringMatch(TclGetString(nameObj), pattern)) {
	    continue;		/* Element name doesn't match pattern. */
	}

	result = Tcl_ListObjAppendElement(interp, nameLstObj, nameObj);
	if (result != TCL_OK) {
	    TclDecrRefCount(nameLstObj);
	    return result;
	}
    }

    /*
     * Make sure the Var structure of the array is not removed by a trace
     * while we're working.
     */

  searchDone:
    if (TclIsVarInHash(varPtr)) {
	VarHashRefCount(varPtr)++;
    }

    /*
     * Get the array values corresponding to each element name.
     */

    TclNewObj(tmpResObj);
    result = Tcl_ListObjGetElements(interp, nameLstObj, &count, &nameObjPtr);
    if (result != TCL_OK) {
	goto errorInArrayGet;
    }

    for (i=0 ; i<count ; i++) {
	nameObj = *nameObjPtr++;
	valueObj = Tcl_ObjGetVar2(interp, varNameObj, nameObj,
		TCL_LEAVE_ERR_MSG);
	if (valueObj == NULL) {
	    /*
	     * Some trace played a trick on us; we need to diagnose to adapt
	     * our behaviour: was the array element unset, or did the
	     * modification modify the complete array?
	     */

	    if (TclIsVarArray(varPtr)) {
		/*
		 * The array itself looks OK, the variable was undefined:
		 * forget it.
		 */

		continue;
	    }
	    result = TCL_ERROR;
	    goto errorInArrayGet;
	}
	result = Tcl_DictObjPut(interp, tmpResObj, nameObj, valueObj);
	if (result != TCL_OK) {
	    goto errorInArrayGet;
	}
    }
    if (TclIsVarInHash(varPtr)) {
	VarHashRefCount(varPtr)--;
    }
    Tcl_SetObjResult(interp, tmpResObj);
    TclDecrRefCount(nameLstObj);
    return TCL_OK;

  errorInArrayGet:
    if (TclIsVarInHash(varPtr)) {
	VarHashRefCount(varPtr)--;
    }
    TclDecrRefCount(nameLstObj);
    TclDecrRefCount(tmpResObj);	/* Free unneeded temp result. */
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayNamesCmd --
 *







<
<
|
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
|
|
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

<
<
<
<
<
<
<
<
<
|
<
<
|
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
|
<
<
<
|
<
<
<
<
|
|

<
<
<
<
<
<
<
<







4548
4549
4550
4551
4552
4553
4554


4555




4556












4557
4558



4559













4560
4561





































4562









4563


4564



4565
















4566


4567



















4568

4569



4570




4571
4572
4573








4574
4575
4576
4577
4578
4579
4580
static int
ArrayGetCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{


    Tcl_Obj *varNameObj, *filterObj, *resultObj;




    int filterType;













    if (ArrayArgs(interp, objc, objv, &varNameObj,



	    &filterObj, &filterType) != TCL_OK) {













	return TCL_ERROR;
    }















































    TclNewObj(resultObj);


    Tcl_IncrRefCount(resultObj);



    if (Tcl_ArrayGet(interp, varNameObj, filterObj, resultObj,
















	    filterType) != TCL_OK) {


	Tcl_DecrRefCount(resultObj);



















	return TCL_ERROR;

    }








    Tcl_SetObjResult(interp, resultObj);
    Tcl_DecrRefCount(resultObj);
    return TCL_OK;








}

/*
 *----------------------------------------------------------------------
 *
 * ArrayNamesCmd --
 *
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567

3568
3569
3570
3571
3572
3573
3574
static int
ArrayNamesCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    static const char *const options[] = {
	"-exact", "-glob", "-regexp", NULL
    };
    enum options { OPT_EXACT, OPT_GLOB, OPT_REGEXP };
    Interp *iPtr = (Interp *) interp;
    Var *varPtr, *arrayPtr, *varPtr2;
    Tcl_Obj *varNameObj, *nameObj, *resultObj, *patternObj;
    Tcl_HashSearch search;
    const char *pattern = NULL;
    int mode = OPT_GLOB;

    if ((objc < 2) || (objc > 4)) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName ?mode? ?pattern?");
	return TCL_ERROR;
    }
    varNameObj = objv[1];
    patternObj = (objc > 2 ? objv[objc-1] : NULL);

    /*
     * Locate the array variable.
     */

    varPtr = TclObjLookupVarEx(interp, varNameObj, NULL, /*flags*/ 0,
	    /*msg*/ 0, /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr);

    /*
     * Special array trace used to keep the env array in sync for array names,
     * array get, etc.
     */

    if (varPtr && (varPtr->flags & VAR_TRACED_ARRAY)
	    && (TclIsVarArray(varPtr) || TclIsVarUndefined(varPtr))) {
	if (TclObjCallVarTraces(iPtr, arrayPtr, varPtr, varNameObj, NULL,
		(TCL_LEAVE_ERR_MSG|TCL_NAMESPACE_ONLY|TCL_GLOBAL_ONLY|
		TCL_TRACE_ARRAY), /* leaveErrMsg */ 1, -1) == TCL_ERROR) {
	    return TCL_ERROR;
	}
    }

    /*
     * Finish parsing the arguments.
     */

    if ((objc == 4) && Tcl_GetIndexFromObj(interp, objv[2], options, "option",
	    0, &mode) != TCL_OK) {
	return TCL_ERROR;
    }

    /*
     * Verify that it is indeed an array variable. This test comes after the
     * traces - the variable may actually become an array as an effect of said
     * traces. If not an array, the result is empty.
     */

    if ((varPtr == NULL) || !TclIsVarArray(varPtr)
	    || TclIsVarUndefined(varPtr)) {
	return TCL_OK;
    }

    /*
     * Check for the trivial cases where we can use a direct lookup.
     */

    TclNewObj(resultObj);
    if (patternObj) {
	pattern = TclGetString(patternObj);
    }
    if ((mode==OPT_GLOB && patternObj && TclMatchIsTrivial(pattern))
	    || (mode==OPT_EXACT)) {
	varPtr2 = VarHashFindVar(varPtr->value.tablePtr, patternObj);
	if ((varPtr2 != NULL) && !TclIsVarUndefined(varPtr2)) {
	    /*
	     * This can't fail; lappending to an empty object always works.
	     */

	    Tcl_ListObjAppendElement(NULL, resultObj, VarHashGetKey(varPtr2));
	}
	Tcl_SetObjResult(interp, resultObj);
	return TCL_OK;
    }

    /*
     * Must scan the array to select the elements.
     */

    for (varPtr2=VarHashFirstVar(varPtr->value.tablePtr, &search);
	    varPtr2!=NULL ; varPtr2=VarHashNextVar(&search)) {
	if (TclIsVarUndefined(varPtr2)) {
	    continue;
	}
	nameObj = VarHashGetKey(varPtr2);
	if (patternObj) {
	    const char *name = TclGetString(nameObj);
	    int matched = 0;

	    switch ((enum options) mode) {
	    case OPT_EXACT:
		Tcl_Panic("exact matching shouldn't get here");
	    case OPT_GLOB:
		matched = Tcl_StringMatch(name, pattern);
		break;
	    case OPT_REGEXP:
		matched = Tcl_RegExpMatchObj(interp, nameObj, patternObj);
		if (matched < 0) {
		    TclDecrRefCount(resultObj);
		    return TCL_ERROR;
		}
		break;
	    }
	    if (matched == 0) {
		continue;
	    }
	}

	Tcl_ListObjAppendElement(NULL, resultObj, nameObj);
    }
    Tcl_SetObjResult(interp, resultObj);

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TclFindArrayPtrElements --







<
<
<
<
<
<
|
<
<
<
|
<
<
<
|
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
|
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
<
|
<
<
<
<
<
<
<

>







4594
4595
4596
4597
4598
4599
4600






4601



4602



4603


4604


























4605
4606
4607















4608
4609










4610





4611



4612



















4613
4614
4615

4616







4617
4618
4619
4620
4621
4622
4623
4624
4625
static int
ArrayNamesCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{






    Tcl_Obj *varNameObj, *filterObj, *resultObj;



    int filterType;






    if (ArrayArgs(interp, objc, objv, &varNameObj,


























	    &filterObj, &filterType) != TCL_OK) {
	return TCL_ERROR;
    }
















    TclNewObj(resultObj);










    Tcl_IncrRefCount(resultObj);





    if (Tcl_ArrayNames(interp, varNameObj, filterObj, resultObj,



	    filterType) != TCL_OK) {



















	Tcl_DecrRefCount(resultObj);
	return TCL_ERROR;
    }









    Tcl_SetObjResult(interp, resultObj);
    Tcl_DecrRefCount(resultObj);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TclFindArrayPtrElements --
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
static int
ArraySetCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr, *arrayPtr;

    if (objc != 3) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName list");
	return TCL_ERROR;
    }

    /*
     * Locate the array variable.
     */

    varPtr = TclObjLookupVarEx(interp, objv[1], NULL, /*flags*/ 0,
	    /*msg*/ 0, /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr);

    /*
     * Special array trace used to keep the env array in sync for array names,
     * array get, etc.
     */

    if (varPtr && (varPtr->flags & VAR_TRACED_ARRAY)
	    && (TclIsVarArray(varPtr) || TclIsVarUndefined(varPtr))) {
	if (TclObjCallVarTraces(iPtr, arrayPtr, varPtr, objv[1], NULL,
		(TCL_LEAVE_ERR_MSG|TCL_NAMESPACE_ONLY|TCL_GLOBAL_ONLY|
		TCL_TRACE_ARRAY), /* leaveErrMsg */ 1, -1) == TCL_ERROR) {
	    return TCL_ERROR;
	}
    }

    return TclArraySet(interp, objv[1], objv[2]);
}

/*
 *----------------------------------------------------------------------
 *
 * ArraySizeCmd --
 *







<
<
<





<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|







4687
4688
4689
4690
4691
4692
4693



4694
4695
4696
4697
4698





















4699
4700
4701
4702
4703
4704
4705
4706
static int
ArraySetCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{



    if (objc != 3) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName list");
	return TCL_ERROR;
    }






















    return Tcl_ArraySet(interp, objv[1], objv[2], 0);
}

/*
 *----------------------------------------------------------------------
 *
 * ArraySizeCmd --
 *
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708

3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
static int
ArraySizeCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr, *arrayPtr;
    Tcl_Obj *varNameObj;
    Tcl_HashSearch search;
    Var *varPtr2;
    int size = 0;

    if (objc != 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName");

	return TCL_ERROR;
    }
    varNameObj = objv[1];

    /*
     * Locate the array variable.
     */

    varPtr = TclObjLookupVarEx(interp, varNameObj, NULL, /*flags*/ 0,
	    /*msg*/ 0, /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr);

    /*
     * Special array trace used to keep the env array in sync for array names,
     * array get, etc.
     */

    if (varPtr && (varPtr->flags & VAR_TRACED_ARRAY)
	    && (TclIsVarArray(varPtr) || TclIsVarUndefined(varPtr))) {
	if (TclObjCallVarTraces(iPtr, arrayPtr, varPtr, varNameObj, NULL,
		(TCL_LEAVE_ERR_MSG|TCL_NAMESPACE_ONLY|TCL_GLOBAL_ONLY|
		TCL_TRACE_ARRAY), /* leaveErrMsg */ 1, -1) == TCL_ERROR) {
	    return TCL_ERROR;
	}
    }

    /*
     * Verify that it is indeed an array variable. This test comes after the
     * traces - the variable may actually become an array as an effect of said
     * traces. We can only iterate over the array if it exists...
     */

    if (varPtr && TclIsVarArray(varPtr) && !TclIsVarUndefined(varPtr)) {
	/*
	 * Must iterate in order to get chance to check for present but
	 * "undefined" entries.
	 */

	for (varPtr2=VarHashFirstVar(varPtr->value.tablePtr, &search);
		varPtr2!=NULL ; varPtr2=VarHashNextVar(&search)) {
	    if (!TclIsVarUndefined(varPtr2)) {
		size++;
	    }
	}
    }

    Tcl_SetObjResult(interp, Tcl_NewIntObj(size));
    return TCL_OK;
}

/*







<
<
|
<
<
|

<
|
>


<

<
<
<
|
<
<
|
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







4720
4721
4722
4723
4724
4725
4726


4727


4728
4729

4730
4731
4732
4733

4734



4735


4736










4737





















4738
4739
4740
4741
4742
4743
4744
static int
ArraySizeCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{


    Tcl_Obj *varNameObj, *filterObj;


    int filterType, size;


    if (ArrayArgs(interp, objc, objv, &varNameObj,
	    &filterObj, &filterType) != TCL_OK) {
	return TCL_ERROR;
    }





    if (Tcl_ArraySize(interp, varNameObj, filterObj, &size,


	    filterType) != TCL_OK) {










	return TCL_ERROR;





















    }

    Tcl_SetObjResult(interp, Tcl_NewIntObj(size));
    return TCL_OK;
}

/*
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
static int
ArrayStatsCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr, *arrayPtr;
    Tcl_Obj *varNameObj;
    char *stats;

    if (objc != 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName");
	return TCL_ERROR;
    }
    varNameObj = objv[1];

    /*
     * Locate the array variable.
     */

    varPtr = TclObjLookupVarEx(interp, varNameObj, NULL, /*flags*/ 0,
	    /*msg*/ 0, /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr);

    /*
     * Special array trace used to keep the env array in sync for array names,
     * array get, etc.
     */

    if (varPtr && (varPtr->flags & VAR_TRACED_ARRAY)
	    && (TclIsVarArray(varPtr) || TclIsVarUndefined(varPtr))) {
	if (TclObjCallVarTraces(iPtr, arrayPtr, varPtr, varNameObj, NULL,
		(TCL_LEAVE_ERR_MSG|TCL_NAMESPACE_ONLY|TCL_GLOBAL_ONLY|
		TCL_TRACE_ARRAY), /* leaveErrMsg */ 1, -1) == TCL_ERROR) {
	    return TCL_ERROR;
	}
    }

    /*
     * Verify that it is indeed an array variable. This test comes after the
     * traces - the variable may actually become an array as an effect of said
     * traces.
     */

    if ((varPtr == NULL) || !TclIsVarArray(varPtr)
	    || TclIsVarUndefined(varPtr)) {
	Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		"\"%s\" isn't an array", TclGetString(varNameObj)));
	Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ARRAY",
		TclGetString(varNameObj), NULL);
	return TCL_ERROR;
    }

    stats = Tcl_HashStats((Tcl_HashTable *) varPtr->value.tablePtr);
    if (stats == NULL) {
	Tcl_SetObjResult(interp, Tcl_NewStringObj(
		"error reading array statistics", -1));
	return TCL_ERROR;
    }
    Tcl_SetObjResult(interp, Tcl_NewStringObj(stats, -1));
    ckfree(stats);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayUnsetCmd --







<
<
|
<





<

<
<
<
|
<
<
|
<
<
<
<
|
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<



<
<
|
<
<
<
|
<







4763
4764
4765
4766
4767
4768
4769


4770

4771
4772
4773
4774
4775

4776



4777


4778




4779








4780












4781
4782
4783


4784



4785

4786
4787
4788
4789
4790
4791
4792
static int
ArrayStatsCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{


    Tcl_Obj *resultObj;


    if (objc != 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName");
	return TCL_ERROR;
    }





    TclNewObj(resultObj);


    Tcl_IncrRefCount(resultObj);




    if (Tcl_ArrayStatistics(interp, objv[1], resultObj, 0) != TCL_OK) {








	Tcl_DecrRefCount(resultObj);












	return TCL_ERROR;
    }



    Tcl_SetObjResult(interp, resultObj);



    Tcl_DecrRefCount(resultObj);

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayUnsetCmd --
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
static int
ArrayUnsetCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr, *arrayPtr, *varPtr2, *protectedVarPtr;
    Tcl_Obj *varNameObj, *patternObj, *nameObj;
    Tcl_HashSearch search;
    const char *pattern;
    const int unsetFlags = 0;	/* Should this be TCL_LEAVE_ERR_MSG? */

    switch (objc) {
    case 2:
	varNameObj = objv[1];
	patternObj = NULL;
	break;
    case 3:
	varNameObj = objv[1];
	patternObj = objv[2];
	break;
    default:
	Tcl_WrongNumArgs(interp, 1, objv, "arrayName ?pattern?");
	return TCL_ERROR;
    }

    /*
     * Locate the array variable
     */

    varPtr = TclObjLookupVarEx(interp, varNameObj, NULL, /*flags*/ 0,
	    /*msg*/ 0, /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr);

    /*
     * Special array trace used to keep the env array in sync for array names,
     * array get, etc.
     */

    if (varPtr && (varPtr->flags & VAR_TRACED_ARRAY)
	    && (TclIsVarArray(varPtr) || TclIsVarUndefined(varPtr))) {
	if (TclObjCallVarTraces(iPtr, arrayPtr, varPtr, varNameObj, NULL,
		(TCL_LEAVE_ERR_MSG|TCL_NAMESPACE_ONLY|TCL_GLOBAL_ONLY|
		TCL_TRACE_ARRAY), /* leaveErrMsg */ 1, -1) == TCL_ERROR) {
	    return TCL_ERROR;
	}
    }

    /*
     * Verify that it is indeed an array variable. This test comes after the
     * traces - the variable may actually become an array as an effect of said
     * traces.
     */

    if ((varPtr == NULL) || !TclIsVarArray(varPtr)
	    || TclIsVarUndefined(varPtr)) {
	return TCL_OK;
    }

    if (!patternObj) {
	/*
	 * When no pattern is given, just unset the whole array.
	 */

	return TclObjUnsetVar2(interp, varNameObj, NULL, 0);
    }

    /*
     * With a trivial pattern, we can just unset.
     */

    pattern = TclGetString(patternObj);
    if (TclMatchIsTrivial(pattern)) {
	varPtr2 = VarHashFindVar(varPtr->value.tablePtr, patternObj);
	if (!varPtr2 || TclIsVarUndefined(varPtr2)) {
	    return TCL_OK;
	}
	return TclPtrUnsetVar(interp, varPtr2, varPtr, varNameObj, patternObj,
		unsetFlags, -1);
    }

    /*
     * Non-trivial case (well, deeply tricky really). We peek inside the hash
     * iterator in order to allow us to guarantee that the following element
     * in the array will not be scrubbed until we have dealt with it. This
     * stops the overall iterator from ending up pointing into deallocated
     * memory. [Bug 2939073]
     */

    protectedVarPtr = NULL;
    for (varPtr2=VarHashFirstVar(varPtr->value.tablePtr, &search);
	    varPtr2!=NULL ; varPtr2=VarHashNextVar(&search)) {
	/*
	 * Drop the extra ref immediately. We don't need to free it at this
	 * point though; we'll be unsetting it if necessary soon.
	 */

	if (varPtr2 == protectedVarPtr) {
	    VarHashRefCount(varPtr2)--;
	}

	/*
	 * Guard the next (peeked) item in the search chain by incrementing
	 * its refcount. This guarantees that the hash table iterator won't be
	 * dangling on the next time through the loop.
	 */

	if (search.nextEntryPtr != NULL) {
	    protectedVarPtr = VarHashGetValue(search.nextEntryPtr);
	    VarHashRefCount(protectedVarPtr)++;
	} else {
	    protectedVarPtr = NULL;
	}

	/*
	 * If the variable is undefined, clean it out as it has been hit by
	 * something else (i.e., an unset trace).
	 */

	if (TclIsVarUndefined(varPtr2)) {
	    CleanupVar(varPtr2, varPtr);
	    continue;
	}

	nameObj = VarHashGetKey(varPtr2);
	if (Tcl_StringMatch(TclGetString(nameObj), pattern)
		&& TclPtrUnsetVar(interp, varPtr2, varPtr, varNameObj,
			nameObj, unsetFlags, -1) != TCL_OK) {
	    /*
	     * If we incremented a refcount, we must decrement it here as we
	     * will not be coming back properly due to the error.
	     */

	    if (protectedVarPtr) {
		VarHashRefCount(protectedVarPtr)--;
		CleanupVar(protectedVarPtr, varPtr);
	    }
	    return TCL_ERROR;
	}
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TclInitArrayCmd --
 *







<
<
|
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
|
|
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







4807
4808
4809
4810
4811
4812
4813


4814



4815












4816
4817



4818













4819
4820
4821

















4822












































































4823
4824
4825
4826
4827
4828
4829
static int
ArrayUnsetCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{


    Tcl_Obj *varNameObj, *filterObj;



    int filterType;













    if (ArrayArgs(interp, objc, objv, &varNameObj,



	    &filterObj, &filterType) != TCL_OK) {













	return TCL_ERROR;
    }


















    return Tcl_ArrayUnset(interp, varNameObj, filterObj, filterType);












































































}

/*
 *----------------------------------------------------------------------
 *
 * TclInitArrayCmd --
 *
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
	linkPtr = varPtr->value.linkPtr;
	if (linkPtr == otherPtr) {
	    return TCL_OK;
	}
	if (TclIsVarInHash(linkPtr)) {
	    VarHashRefCount(linkPtr)--;
	    if (TclIsVarUndefined(linkPtr)) {
		CleanupVar(linkPtr, NULL);
	    }
	}
    }
    TclSetVarLink(varPtr);
    varPtr->value.linkPtr = otherPtr;
    if (TclIsVarInHash(otherPtr)) {
	VarHashRefCount(otherPtr)++;







|







5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
	linkPtr = varPtr->value.linkPtr;
	if (linkPtr == otherPtr) {
	    return TCL_OK;
	}
	if (TclIsVarInHash(linkPtr)) {
	    VarHashRefCount(linkPtr)--;
	    if (TclIsVarUndefined(linkPtr)) {
		TclCleanupVar(linkPtr, NULL);
	    }
	}
    }
    TclSetVarLink(varPtr);
    varPtr->value.linkPtr = otherPtr;
    if (TclIsVarInHash(otherPtr)) {
	VarHashRefCount(otherPtr)++;
4846
4847
4848
4849
4850
4851
4852

4853
4854
4855
4856
4857
4858
4859
4860
	    if (searchPtr->name == handleObj) {
		return searchPtr;
	    }
	}
	/* Fallback: do string compares. */
	for (searchPtr = Tcl_GetHashValue(hPtr); searchPtr != NULL;
		searchPtr = searchPtr->nextPtr) {

	    if (strcmp(TclGetString(searchPtr->name), handle) == 0) {
		return searchPtr;
	    }
	}
    }
    if ((handle[0] != 's') || (handle[1] != '-')
	    || (strtoul(handle + 2, &end, 10), end == (handle + 2))
	    || (*end != '-')) {







>
|







5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
	    if (searchPtr->name == handleObj) {
		return searchPtr;
	    }
	}
	/* Fallback: do string compares. */
	for (searchPtr = Tcl_GetHashValue(hPtr); searchPtr != NULL;
		searchPtr = searchPtr->nextPtr) {
	    if (searchPtr->name
		    && strcmp(TclGetString(searchPtr->name), handle) == 0) {
		return searchPtr;
	    }
	}
    }
    if ((handle[0] != 's') || (handle[1] != '-')
	    || (strtoul(handle + 2, &end, 10), end == (handle + 2))
	    || (*end != '-')) {
4873
4874
4875
4876
4877
4878
4879
4880
4881


4882
4883
4884
4885
4886
4887
4888
}

/*
 *----------------------------------------------------------------------
 *
 * DeleteSearches --
 *
 *	This function is called to free up all of the searches associated
 *	with an array variable.


 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Memory is released to the storage allocator.
 *







|
|
>
>







5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
}

/*
 *----------------------------------------------------------------------
 *
 * DeleteSearches --
 *
 *	This function is called to free up all of the searches associated with
 *	an array variable. Any searches with the KEEP_ON_ABORT flag set will not
 *	immediately be freed but will have the SEARCH_ABORTED flag set so they
 *	will be freed the next time a search operation is performed.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Memory is released to the storage allocator.
 *
4899
4900
4901
4902
4903
4904
4905
4906


4907

4908
4909
4910
4911
4912
4913
4914
    Tcl_HashEntry *sPtr;

    if (arrayVarPtr->flags & VAR_SEARCH_ACTIVE) {
	sPtr = Tcl_FindHashEntry(&iPtr->varSearches, arrayVarPtr);
	for (searchPtr = Tcl_GetHashValue(sPtr); searchPtr != NULL;
		searchPtr = nextPtr) {
	    nextPtr = searchPtr->nextPtr;
	    Tcl_DecrRefCount(searchPtr->name);


	    ckfree(searchPtr);

	}
	arrayVarPtr->flags &= ~VAR_SEARCH_ACTIVE;
	Tcl_DeleteHashEntry(sPtr);
    }
}

/*







|
>
>
|
>







5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
    Tcl_HashEntry *sPtr;

    if (arrayVarPtr->flags & VAR_SEARCH_ACTIVE) {
	sPtr = Tcl_FindHashEntry(&iPtr->varSearches, arrayVarPtr);
	for (searchPtr = Tcl_GetHashValue(sPtr); searchPtr != NULL;
		searchPtr = nextPtr) {
	    nextPtr = searchPtr->nextPtr;
	    if (searchPtr->flags & KEEP_ON_ABORT) {
		searchPtr->flags |= SEARCH_ABORTED;
	    } else {
		ArraySearchFree(searchPtr);
	    }
	}
	arrayVarPtr->flags &= ~VAR_SEARCH_ACTIVE;
	Tcl_DeleteHashEntry(sPtr);
    }
}

/*

Added tests/array.test.

















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
# Commands covered:  array
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 2016 Andy Goth
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {[lsearch [namespace children] ::tcltest] < 0} {
    package require tcltest 2
}
::tcltest::loadTestedCommands
catch {package require -exact Tcltest [info patchlevel]}
set namespaces [namespace children]
set procs [info procs]
set vars [info vars]

# test set   chapter title
# ---------  -----------------------
# array-1.*  subcommand dispatch
# array-2.*  common argument parsing
# array-3.*  array set
# array-4.*  array unset
# array-5.*  array statistics
# array-6.*  array exists|size|names|get
# array-7.*  array anymore|donesearch|nextelement|startsearch

# test set   [array] subcommand  arrayName             mode and filter
# ---------  ------------------  --------------------  ---------------
# array-7.*  anymore             array required        not allowed
# array-7.*  donesearch          array required        not allowed
# array-6.*  exists              anything              optional
# array-6.*  get                 anything              optional
# array-6.*  names               anything              optional
# array-7.*  nextelement         array required        not allowed
# array-3.*  set                 array or nonexistent  not allowed
# array-6.*  size                anything              optional
# array-7.*  startsearch         array required        optional
# array-5.*  statistics          array required        not allowed
# array-4.*  unset               anything              optional
# array-1.*  (unique abbrev)     (see above)           (see above)
# array-1.*  (ambiguous abbrev)  (error)               (error)
# array-1.*  (invalid)           (error)               (error)

# oxfordJoin --
# and --
# or --
# Joins a list by commas, a conjunction, or both, using Oxford comma rules,
# matching Tcl's internal algorithm for displaying lists in error messages.
proc oxfordJoin {conjunction list} {
    if {[llength $list] > 1} {
        lset list end "$conjunction [lindex $list end]"
    }
    if {[llength $list] < 3} {
        join $list " "
    } else {
        join $list ", "
    }
}
interp alias {} and {} oxfordJoin and
interp alias {} or {} oxfordJoin or

# samples --
# Produces a list of sample arguments, given a list of parameters.
# Customizations can be applied via $args.
proc samples {params args} {
    set map [dict merge {
        arrayName   a
        searchId    s-1-a
        ?mode?      -exact
        ?pattern?   hello
        list        {hello world}
    } $args]
    lmap param $params {dict get $map $param}
}

# test --
# Wrapper around [::tcltest::test] with additional features:
#
# - Single-argument block design gives a cleaner visual presentation.
# - All script execution is performed in a new stack frame.
# - Script variables are shared across scripts via a temporary namespace.
# - All definition values (except scripts) are [subst]'ed.
#
# The $def argument is a dict which defines the test.  Its possible keys are:
#
# - name | desc
#   Test name and description.
#
# - {scalar name} | {array name}
#   The name component of the key is the name of a scalar or array variable, and
#   the value is the initial scalar value or array dictionary value.  These
#   variables are shared across the setup, body, and cleanup scripts.
#
# - link
#   Uninitialized variables shared across the setup, body, and cleanup scripts.
#
# - setup | body | cleanup
#   The scripts are modified to include variable initialization, linkage, and
#   finalization and to be executed inside new stack frames.
#
# - constraints | result | output | errorOutput | returnCodes | match
#   See tcltest(n) for the purpose of these keys.
proc test {def} {
    # Perform uplevel substitutions, and process scalar and array arguments.
    set scalars {}
    set arrays {}
    dict for {key val} $def {
        if {$key ni {setup body cleanup}} {
            dict set def $key [set val [uplevel [list subst $val]]]
        }
        if {[llength $key] == 2} {
            if {[lindex $key 0] eq "scalar"} {
                dict set scalars [lindex $key 1] $val
                dict unset def $key
            } elseif {[lindex $key 0] eq "array"} {
                dict set arrays [lindex $key 1] $val
                dict unset def $key
            }
        }
    }

    # Augment scripts with variable initialization, linkage, and finalization.
    dict lappend def link {*}[dict keys $scalars] {*}[dict keys $arrays]
    if {[llength [dict get $def link]]} {
        # Ensure all three scripts exist, even if empty.
        foreach key {setup body cleanup} {
            dict append def $key
        }

        # Build variable initialization, linkage, and finalization snippets.
        set initial [list namespace eval ::TestVars [join [list\
            [list variable {*}$scalars]\
            {*}[lmap {var val} $arrays {list variable $var}]\
            {*}[lmap {var val} $arrays {list array set $var $val}]] \n]]
        set linkage [join [lmap var [dict get $def link]\
            {list variable ::TestVars::$var}] \n]
        set final [list namespace delete ::TestVars]

        # Update scripts.
        dict set def setup $initial\n$linkage\n[dict get $def setup]
        dict set def body $linkage\n[dict get $def body]
        dict set def cleanup $linkage\n[dict get $def cleanup]\n$final
    }
    dict unset def link

    # Convert scripts to zero-argument lambda invocations.
    foreach key {setup body cleanup} {
        if {[dict exists $def $key]} {
            dict set def $key [list apply [list {} [dict get $def $key]]]
        }
    }

    # Assemble the Tcltest command.
    set command [list ::tcltest::test [dict get $def name] [dict get $def desc]]
    dict unset def name
    dict unset def desc
    foreach key {constraints setup body cleanup result output errorOutput
            returnCodes match} {
        if {[dict exists $def $key]} {
            lappend command -$key [dict get $def $key]
            dict unset def $key
        }
    }

    # Complain if there are any invalid test definition keys.
    if {[dict size $def]} {
        error "bad test definition key(s): [and [dict keys $def]]"
    }

    # Run the Tcltest command.
    tailcall {*}$command
}

# Formal parameters for each array subcommand.
set params {
    anymore     {arrayName searchId}
    donesearch  {arrayName searchId}
    exists      {arrayName ?mode? ?pattern?}
    get         {arrayName ?mode? ?pattern?}
    names       {arrayName ?mode? ?pattern?}
    nextelement {arrayName searchId}
    set         {arrayName list}
    size        {arrayName ?mode? ?pattern?}
    startsearch {arrayName ?mode? ?pattern?}
    statistics  {arrayName}
    unset       {arrayName ?mode? ?pattern?}
}

# List of array subcommands.
set commands [lsort [dict keys $params]]

# Ambiguous and unambiguous abbreviations of array subcommands.
foreach cmd $commands {
    for {set i 0} {$i < [string length $cmd] - 1} {incr i} {
        set abbrev [string range $cmd 0 $i]
        if {$abbrev in $commands
         || [llength [lsearch -all $commands $abbrev*]] == 1} {
            dict lappend abbrevs $cmd $abbrev
        } else {
            dict set ambig $abbrev {}
        }
    }
}
set ambig [lsort [dict keys $ambig]]

# List of valid array filter mode options.
set modes [lsort {-exact -glob -regexp}]

######################## array-1.*: subcommand dispatch ########################
test {
    name array-1.1
    desc {no subcommand}
    body {array}
    returnCodes error
    result {wrong # args: should be "array subcommand ?arg ...?"}
}
test {
    name array-1.2
    desc {empty subcommand}
    body {array {}}
    returnCodes error
    result {unknown or ambiguous subcommand "": must be [or $commands]}
}
test {
    name array-1.3
    desc {invalid subcommand}
    body {array gorp}
    returnCodes error
    result {unknown or ambiguous subcommand "gorp": must be [or $commands]}
}
foreach cmd $ambig {
    test {
        name array-1.4.$cmd
        desc {ambiguous subcommand: \[array $cmd\]}
        {scalar cmd} $cmd
        body {array $cmd}
        returnCodes error
        result {unknown or ambiguous subcommand "$cmd": must be [or $commands]}
    }
}
foreach cmd $commands {
    test {
        name array-1.5.$cmd
        desc {formal parameter lists: \[array $cmd\]}
        {scalar cmd} $cmd
        body {array $cmd}
        returnCodes error
        result {wrong # args: should be "array $cmd [dict get $params $cmd]"}
    }
}

###################### array-2.*: common argument parsing ######################
foreach {cmd    code    resultPattern} {
    anymore     error   "\"%VAR%\" %MSG%"
    donesearch  error   "\"%VAR%\" %MSG%"
    exists      ok      0
    get         ok      {}
    names       ok      {}
    nextelement error   "\"%VAR%\" %MSG%"
    set         error   "can't set \"%VAR%%ELEM%\": %MSG%"
    size        ok      0
    startsearch error   "\"%VAR%\" %MSG%"
    statistics  error   "\"%VAR%\" %MSG%"
    unset       ok      {}
} {
    test {
        name array-2.1.$cmd
        desc {too many arguments: \[array $cmd\]}
        {scalar cmd} $cmd
        {scalar args} {[lmap param [dict get $params $cmd] {samples $param}]}
        body {array $cmd {*}$args extra}
        returnCodes error
        result {wrong # args: should be "array $cmd [dict get $params $cmd]"}
    }
    if {"?mode?" in [dict get $params $cmd]} {
        test {
            name array-2.2.$cmd
            desc {ambiguous mode: \[array $cmd\]"}
            {scalar cmd} $cmd
            {array a} {}
            body {array $cmd a {} {}}
            returnCodes error
            result {ambiguous option "": must be [or $modes]}
        }
        test {
            name array-2.3.$cmd
            desc {invalid mode: \[array $cmd\]}
            {scalar cmd} $cmd
            {array a} {}
            body {array $cmd a INVALID {}}
            returnCodes error
            result {bad option "INVALID": must be [or $modes]}
        }
        test {
            name array-2.4.$cmd
            desc {invalid regexp: \[array $cmd\]}
            {scalar cmd} $cmd
            {array a} {e 1}
            body {array $cmd a -regexp *}
            returnCodes error
            result {couldn't compile regular expression pattern:\
                quantifier operand invalid}
        }
    }
    test {
        name array-2.5.$cmd
        desc {array trace error during variable lookup: \[array $cmd\]}
        {scalar cmd} $cmd
        {scalar args} {[lmap param [dict get $params $cmd] {samples $param}]}
        link a
        setup {trace add variable a array {apply {{args} {error $args}}}}
        body {array $cmd {*}$args}
        returnCodes error
        result {can't trace array "a": a {} array}
    }
    if {$cmd eq "set"} {
        set nonArray "variable isn't array"
        set nonNamespace "parent namespace doesn't exist"
    } else {
        set nonArray "isn't an array"
        set nonNamespace "isn't an array"
    }
    foreach {
        desc                                skip    setup
        name        msg             var     elem    extra
    } {
        "nonexistent array"                 set     {}
        array-2.6   nonArray        a       {}      {}

        "element of proc-slot-only array"   set     {}
        array-2.7   nonArray        a       {}      {set a(hello) 123}

        "scalar variable"                   {}      {{scalar a} {}}
        array-2.8   nonArray        a       (hello) {}

        "element of empty array"            {}      {{array a} {}}
        array-2.9   nonArray        a(x)    {}      {}

        "element of nonexistent array"      {}      {}
        array-2.10  nonArray        a(x)    {}      {}

        "element of scalar variable"        {}      {{scalar a} {}}
        array-2.11  nonArray        a(x)    {}      {}

        "existing element of array"         {}      {{scalar a} {x 123}}
        array-2.12  nonArray        a(x)    {}      {}

        "nonexistent element of array"      {}      {{array a} {}}
        array-2.13  nonArray        a(x)    {}      {}

        "bad namespace"                     {}      {}
        array-2.14  nonNamespace    ::X::a  {}      {}
    } {
        if {$cmd ni $skip} {
            set map [list %VAR% $var %MSG% [set $msg] %ELEM% $elem]
            test [string map [list %SETUP% $setup %EXTRA% $extra] {
                name $name.$cmd
                desc {$desc: \[array $cmd\]}
                {scalar cmd} $cmd
                {scalar args} {[samples [dict get $params $cmd] arrayName $var]}
                %SETUP%
                body {set result [array $cmd {*}$args]; %EXTRA%; return $result}
                returnCodes $code
                result {[string map $map $resultPattern]}
            }]
        }
    }
}

############################# array-3.*: array set #############################
test {
    name array-3.1
    desc {empty}
    body {array set a {}; list [array exists a] [array get a]}
    result {1 {}}
}
test {
    name array-3.2
    desc {one array element}
    body {array set a {e 1}; array get a}
    result {e 1}
}
test {
    name array-3.3
    desc {missing value}
    body {array set a e}
    returnCodes error
    result {list must have an even number of elements}
}
test {
    name array-3.4
    desc {duplicate key}
    body {array set a {e 0 e 1}; array get a}
    result {e 1}
}
test {
    name array-3.5
    desc {invalid list}
    body {array set a \{\}x}
    returnCodes error
    result {list element in braces followed by "x" instead of space}
}
test {
    name array-3.6
    desc {invalid list part 2}
    body {array set a \"\"x}
    returnCodes error
    result {list element in quotes followed by "x" instead of space}
}
test {
    name array-3.7
    desc {invalid list part 3}
    body {array set a \{}
    returnCodes error
    result {unmatched open brace in list}
}
test {
    name array-3.8
    desc {invalid list part 4}
    body {array set a \"}
    returnCodes error
    result {unmatched open quote in list}
}
test {
    name array-3.9
    desc {hash order}
    body {array set a {f 2 e 1}; array get a}
    result {e 1 f 2}
}
test {
    name array-3.10
    desc {adding elements}
    body {array set a {f 2}; array set a {e 1}; array get a}
    result {e 1 f 2}
}
test {
    name array-3.11
    desc {adding elements, hash order}
    body {array set a {e 1}; array set a {f 2}; array get a}
    result {e 1 f 2}
}
test {
    name array-3.12
    desc {replacing elements}
    body {array set a {e 1}; array set a {e 2}; array get a}
    result {e 2}
}
test {
    name array-3.13
    desc {adding and replacing elements}
    body {array set a {e 1}; array set a {f 3 e 2}; array get a}
    result {e 2 f 3}
}
test {
    name array-3.14
    desc {former scalar}
    {scalar a} xxx
    body {unset a; array set a {e 1}; array get a}
    result {e 1}
}
test {
    name array-3.15
    desc {weird names}
    body {array set a {{ a  b } 1 ) 2 ( 3 )( 4 () 5 {} 6}; array get a}
    result {{} 6 ( 3 () 5 )( 4 ) 2 { a  b } 1}
}

############################ array-4.*: array unset ############################
test {
    name array-4.1
    desc {unset empty array}
    {array a} {}
    body {array unset a; info exists a}
    result 0
}
test {
    name array-4.2
    desc {unset non-empty array}
    {array a} {e 1}
    body {array unset a; info exists a}
    result 0
}
test {
    name array-4.3
    desc {unset scalar}
    {scalar a} x
    body {array unset a; return $a}
    result x
}
test {
    name array-4.4
    desc {unset all elements of empty array}
    {array a} {}
    body {array unset a *; list [info exists a] [array size a]}
    result {1 0}
}
test {
    name array-4.5
    desc {unset all elements of non-empty array}
    {array a} {e 1}
    body {array unset a *; list [info exists a] [array size a]}
    result {1 0}
}
test {
    name array-4.6
    desc {unset all elements of scalar array}
    {scalar a} x
    body {array unset a *; return $a}
    result x
}
test {
    name array-4.7
    desc {unset single existing element using -exact}
    {array a} {f 2 e 1}
    body {array unset a -exact e; array get a}
    result {f 2}
}
test {
    name array-4.8
    desc {unset single nonexistent element using -exact}
    {array a} {f 2 e 1}
    body {array unset a -exact d; array get a}
    result {e 1 f 2}
}
test {
    name array-4.9
    desc {unset single existing element using default mode}
    {array a} {f 2 e 1}
    body {array unset a e; array get a}
    result {f 2}
}
test {
    name array-4.10
    desc {unset single nonexistent element using default mode}
    {array a} {f 2 e 1}
    body {array unset a d; array get a}
    result {e 1 f 2}
}
test {
    name array-4.11
    desc {unset single existing element using -glob}
    {array a} {f 2 e 1}
    body {array unset a -glob {[e]}; array get a}
    result {f 2}
}
test {
    name array-4.12
    desc {unset single nonexistent element using -glob}
    {array a} {f 2 e 1}
    body {array unset a -glob {[d]}; array get a}
    result {e 1 f 2}
}
test {
    name array-4.13
    desc {unset single existing element using -regexp}
    {array a} {f 2 e 1}
    body {array unset a -regexp {^[e]}; array get a}
    result {f 2}
}
test {
    name array-4.14
    desc {unset single nonexistent element using -regexp}
    {array a} {f 2 e 1}
    body {array unset a -regexp {^[d]}; array get a}
    result {e 1 f 2}
}
test {
    name array-4.15
    desc {confirm unset -exact does not match substrings}
    {array a} {abc 1}
    body {array unset a -exact b; array get a}
    result {abc 1}
}
test {
    name array-4.16
    desc {confirm unset -glob does not match substrings}
    {array a} {abc 1}
    body {array unset a -glob b; array get a}
    result {abc 1}
}
test {
    name array-4.17
    desc {confirm unset -regexp does match substrings}
    {array a} {abc 1}
    body {array unset a -regexp b; array get a}
    result {}
}

######################### array-5.*: array statistics ##########################
# Note: array-5.3 expected results obtained from Tcl version 8.5.7
test {
    name array-5.1
    desc {empty array}
    {array a} {}
    body {array statistics a}
    result
{0 entries in table, 4 buckets
number of buckets with 0 entries: 4
number of buckets with 1 entries: 0
number of buckets with 2 entries: 0
number of buckets with 3 entries: 0
number of buckets with 4 entries: 0
number of buckets with 5 entries: 0
number of buckets with 6 entries: 0
number of buckets with 7 entries: 0
number of buckets with 8 entries: 0
number of buckets with 9 entries: 0
number of buckets with 10 or more entries: 0
average search distance for entry: 0.0}
}
test {
    name array-5.2
    desc {single-element array}
    {array a} {e 1}
    body {array statistics a}
    result
{1 entries in table, 4 buckets
number of buckets with 0 entries: 3
number of buckets with 1 entries: 1
number of buckets with 2 entries: 0
number of buckets with 3 entries: 0
number of buckets with 4 entries: 0
number of buckets with 5 entries: 0
number of buckets with 6 entries: 0
number of buckets with 7 entries: 0
number of buckets with 8 entries: 0
number of buckets with 9 entries: 0
number of buckets with 10 or more entries: 0
average search distance for entry: 1.0}
}
test {
    name array-5.3
    desc {thousand-element array}
    link a
    setup {for {set i 0} {$i < 1000} {incr i} {set a($i) $i}}
    body {array statistics a}
    result
{1000 entries in table, 1024 buckets
number of buckets with 0 entries: 285
number of buckets with 1 entries: 520
number of buckets with 2 entries: 180
number of buckets with 3 entries: 36
number of buckets with 4 entries: 3
number of buckets with 5 entries: 0
number of buckets with 6 entries: 0
number of buckets with 7 entries: 0
number of buckets with 8 entries: 0
number of buckets with 9 entries: 0
number of buckets with 10 or more entries: 0
average search distance for entry: 1.3}
}
test {
    name array-5.4
    desc {collision attack}
    link a
    setup {
        for {set i 16} {$i < 29} {incr i} {
            set a([binary format cc $i [expr {-$i * 9}]]) $i
        }
    }
    body {array statistics a}
    result
{13 entries in table, 16 buckets
number of buckets with 0 entries: 15
number of buckets with 1 entries: 0
number of buckets with 2 entries: 0
number of buckets with 3 entries: 0
number of buckets with 4 entries: 0
number of buckets with 5 entries: 0
number of buckets with 6 entries: 0
number of buckets with 7 entries: 0
number of buckets with 8 entries: 0
number of buckets with 9 entries: 0
number of buckets with 10 or more entries: 1
average search distance for entry: 7.0}
}

################### array-6.*: array exists|size|names|get #####################
foreach {
    desc
    name        dict        args            exists  size    names   get
} {
    "empty array"
    array-6.1   {}          {}              1       0       {}      {}

    "non-empty array"
    array-6.2   {e 1}       {}              1       1       {e}     {e 1}

    "nonexistent element using default mode"
    array-6.3   {e 1}       {[d]}           0       0       {}      {}

    "existing element using default mode"
    array-6.4   {e 1}       {[e]}           1       1       {e}     {e 1}

    "multiple elements using default mode"
    array-6.5   {e 1 f 2}   {[ef]}          1       2       {e f}   {e 1 f 2}

    "nonexistent element using -glob"
    array-6.6   {e 1}       {-glob [d]}     0       0       {}      {}

    "existing element using -glob"
    array-6.7   {e 1}       {-glob [e]}     1       1       {e}     {e 1}

    "multiple elements using -glob"
    array-6.8   {e 1 f 2}   {-glob [ef]}    1       2       {e f}   {e 1 f 2}

    "nonexistent element using -exact"
    array-6.9   {e 1}       {-exact d}      0       0       {}      {}

    "existing element using -exact"
    array-6.10  {e 1}       {-exact e}      1       1       {e}     {e 1}

    "nonexistent element using -regexp"
    array-6.11  {e 1}       {-regexp ^[d]}  0       0       {}      {}

    "existing element using -regexp"
    array-6.12  {e 1}       {-regexp ^[e]}  1       1       {e}     {e 1}

    "multiple elements using -regexp"
    array-6.13  {e 1 f 2}   {-regexp ^[ef]} 1       2       {e f}   {e 1 f 2}
} {
    foreach cmd {exists size names get} {
        test [string map [list %RESULT% [set $cmd]] {
            name $name.$cmd
            desc {$desc: \[array $cmd\]}
            {scalar cmd} $cmd
            {scalar args} $args
            {array a} $dict
            body {array $cmd a {*}$args}
            result {%RESULT%}
        }]
    }
}
foreach {
    cmd     small   large
} {
    exists  1       1
    size    1       2
    names   {e}     {e f}
    get     {e 1}   {e 1 f 2}
} {
    test {
        name array-6.14.$cmd
        desc {increasing array size: \[array $cmd\]}
        {scalar cmd} $cmd
        {array a} {e 1}
        body {list [array $cmd a][set a(f) 2; list] [array $cmd a]}
        result {[list $small $large]}
    }
    test {
        name array-6.15.$cmd
        desc {decreasing array size: \[array $cmd\]}
        {scalar cmd} $cmd
        {array a} {e 1 f 2}
        body {list [array $cmd a][unset a(f)] [array $cmd a]}
        result {[list $large $small]}
    }
}

######### array-7.*: array anymore|donesearch|nextelement|startsearch ##########
foreach cmd {anymore donesearch nextelement} {
    test {
        name array-7.1.$cmd
        desc {nonexistent search token}
        {scalar cmd} $cmd
        {array a} {}
        body {array $cmd a s-1-a}
        returnCodes error
        result {couldn't find search "s-1-a"}
    }
}
foreach {
    desc
    name        dict        args            result
} {
    "no filter, empty array"
    array-7.2   {}          {}              {}

    "no filter, single element"
    array-7.3   {e 1}       {}              {e}

    "no filter, two elements"
    array-7.4   {f 2 e 1}   {}              {e f}

    "default filter, matches nothing"
    array-7.5   {f 2 e 1}   {[g]}           {}

    "default filter, matches one item"
    array-7.6   {f 2 e 1}   {[f]}           {f}

    "default filter, matches two items"
    array-7.7   {f 2 e 1}   {[ef]}          {e f}

    "-glob filter, matches nothing"
    array-7.8   {f 2 e 1}   {-glob [g]}     {}

    "-glob filter, matches one item"
    array-7.9   {f 2 e 1}   {-glob [f]}     {f}

    "-glob filter, matches two items"
    array-7.10  {f 2 e 1}   {-glob [ef]}    {e f}

    "-exact filter, matches nothing"
    array-7.11  {f 2 e 1}   {-exact g}      {}

    "-exact filter, matches one item"
    array-7.12  {f 2 e 1}   {-exact f}      {f}

    "-regexp filter, matches nothing"
    array-7.13  {f 2 e 1}   {-regexp ^[g]}  {}

    "-regexp filter, matches one item"
    array-7.14  {f 2 e 1}   {-regexp ^[f]}  {f}

    "-regexp filter, matches two items"
    array-7.15  {f 2 e 1}   {-regexp ^[ef]} {e f}
} {
    test {
        name $name
        desc $desc
        {scalar args} $args
        {array a} $dict
        body {
            set result {}
            set s [array startsearch a {*}$args]
            while {[array anymore a $s]} {
                lappend result [array nextelement a $s]
            }
            array donesearch a $s
            return $result
        }
        result $result
    }
}
test {
    name array-7.16
    desc {unset visited element during search, bug 46a2410650, s/a var-13.2}
    {array a} {aa 11 bb 22 cc 33 dd 44 ee 55 ff 66}
    body {
        set s [array startsearch a]
        unset a([array nextelement a $s])
        array anymore a $s
    }
    returnCodes error
    result {couldn't find search "s-1-a"}
}
test {
    name array-7.17
    desc {unset future element during search, bug 46a2410650, s/a var-13.3}
    {array a} {aa 11 bb 22 cc 33 dd 44 ee 55 ff 66}
    body {
        set result {}
        set s [array startsearch a]
        unset a(ee)
        array anymore a $s
    }
    returnCodes error
    result {couldn't find search "s-1-a"}
}

# Cleanup.
foreach namespace [namespace children] {
    if {$namespace ni $namespaces} {
        namespace delete $namespace
    }
}
foreach proc [info procs] {
    if {$proc ni $procs} {
        rename $proc {}
    }
}
foreach var [info vars] {
    if {$var ne "vars" && $var ni $vars} {
        unset $var
    }
}
unset -nocomplain var vars
::tcltest::cleanupTests
return

# vim: set sts=4 sw=4 tw=80 et ft=tcl:
# Local Variables:
# mode: tcl
# End:

Changes to tests/set-old.test.

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
        }
        set a(x) 123
    }
    list [catch {foo 1} msg] $msg
} {1 {"a" isn't an array}}
test set-old-8.11 {array command, exists option} {
    list [catch {array exists a b} msg] $msg
} {1 {wrong # args: should be "array exists arrayName"}}
test set-old-8.12 {array command, exists option} {
    catch {unset a}
    array exists a
} {0}
test set-old-8.13 {array command, exists option} {
    catch {unset a}
    set a(0) 1
    array exists a
} {1}
test set-old-8.14 {array command, exists option, array doesn't exist yet but has compiler-allocated procedure slot} {
    proc foo {x} {
        if {$x==1} {
            return [array exists a]
        }
        set a(x) 123
    }
    list [catch {foo 1} msg] $msg
} {0 0}
test set-old-8.15 {array command, get option} {
    list [catch {array get} msg] $msg
} {1 {wrong # args: should be "array get arrayName ?pattern?"}}
test set-old-8.16 {array command, get option} {
    list [catch {array get a b c} msg] $msg
} {1 {wrong # args: should be "array get arrayName ?pattern?"}}
test set-old-8.17 {array command, get option} {
    catch {unset a}
    array get a
} {}
test set-old-8.18 {array command, get option} {
    catch {unset a}
    set a(22) 3







|




















|


|







365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
        }
        set a(x) 123
    }
    list [catch {foo 1} msg] $msg
} {1 {"a" isn't an array}}
test set-old-8.11 {array command, exists option} {
    list [catch {array exists a b} msg] $msg
} {0 0}
test set-old-8.12 {array command, exists option} {
    catch {unset a}
    array exists a
} {0}
test set-old-8.13 {array command, exists option} {
    catch {unset a}
    set a(0) 1
    array exists a
} {1}
test set-old-8.14 {array command, exists option, array doesn't exist yet but has compiler-allocated procedure slot} {
    proc foo {x} {
        if {$x==1} {
            return [array exists a]
        }
        set a(x) 123
    }
    list [catch {foo 1} msg] $msg
} {0 0}
test set-old-8.15 {array command, get option} {
    list [catch {array get} msg] $msg
} {1 {wrong # args: should be "array get arrayName ?mode? ?pattern?"}}
test set-old-8.16 {array command, get option} {
    list [catch {array get a b c} msg] $msg
} {1 {bad option "b": must be -exact, -glob, or -regexp}}
test set-old-8.17 {array command, get option} {
    catch {unset a}
    array get a
} {}
test set-old-8.18 {array command, get option} {
    catch {unset a}
    set a(22) 3
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
} {1 {can't set "bogusnamespace::var(0)": parent namespace doesn't exist}}
test set-old-8.39 {array command, size option} {
    catch {unset a}
    array size a
} {0}
test set-old-8.40 {array command, size option} {
    list [catch {array size a 4} msg] $msg
} {1 {wrong # args: should be "array size arrayName"}}
test set-old-8.41 {array command, size option} {
    catch {unset a}
    array size a
} {0}
test set-old-8.42 {array command, size option} {
    catch {unset a}
    set a(22) 3; set a(Textual_name) 44; set "a(name with spaces)" xxx







|







556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
} {1 {can't set "bogusnamespace::var(0)": parent namespace doesn't exist}}
test set-old-8.39 {array command, size option} {
    catch {unset a}
    array size a
} {0}
test set-old-8.40 {array command, size option} {
    list [catch {array size a 4} msg] $msg
} {0 0}
test set-old-8.41 {array command, size option} {
    catch {unset a}
    array size a
} {0}
test set-old-8.42 {array command, size option} {
    catch {unset a}
    set a(22) 3; set a(Textual_name) 44; set "a(name with spaces)" xxx
588
589
590
591
592
593
594

595
596
597
598
599
600
601
602
603
            return [array size a]
        }
        set a(x) 123
    }
    list [catch {foo 1} msg] $msg
} {0 0}
test set-old-8.46 {array command, startsearch option} {

    list [catch {array startsearch a b} msg] $msg
} {1 {wrong # args: should be "array startsearch arrayName"}}
test set-old-8.47 {array command, startsearch option} {
    catch {unset a}
    list [catch {array startsearch a} msg] $msg
} {1 {"a" isn't an array}}
test set-old-8.48 {array command, startsearch option, array doesn't exist yet but has compiler-allocated procedure slot} {
    catch {rename p ""}
    proc p {x} {







>

|







588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
            return [array size a]
        }
        set a(x) 123
    }
    list [catch {foo 1} msg] $msg
} {0 0}
test set-old-8.46 {array command, startsearch option} {
    catch {unset a}
    list [catch {array startsearch a b} msg] $msg
} {1 {"a" isn't an array}}
test set-old-8.47 {array command, startsearch option} {
    catch {unset a}
    list [catch {array startsearch a} msg] $msg
} {1 {"a" isn't an array}}
test set-old-8.48 {array command, startsearch option, array doesn't exist yet but has compiler-allocated procedure slot} {
    catch {rename p ""}
    proc p {x} {
805
806
807
808
809
810
811
812
813


814
815
816
817
818
819
820
821
822
    trace var a(b) r {}
    set x [array startsearch a]
    lsort [list [array next a $x] [array next a $x]]
} {{} a}

test set-old-10.1 {array enumeration errors} {
    list [catch {array start} msg] $msg
} {1 {wrong # args: should be "array startsearch arrayName"}}
test set-old-10.2 {array enumeration errors} {


    list [catch {array start a b} msg] $msg
} {1 {wrong # args: should be "array startsearch arrayName"}}
test set-old-10.3 {array enumeration errors} {
    catch {unset a}
    list [catch {array start a} msg] $msg
} {1 {"a" isn't an array}}
test set-old-10.4 {array enumeration errors} {
    catch {unset a}
    set a(a) 1







|
|
>
>
|
|







806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
    trace var a(b) r {}
    set x [array startsearch a]
    lsort [list [array next a $x] [array next a $x]]
} {{} a}

test set-old-10.1 {array enumeration errors} {
    list [catch {array start} msg] $msg
} {1 {wrong # args: should be "array startsearch arrayName ?mode? ?pattern?"}}
test set-old-10.2 {array command, startsearch option} {
    # Note: appears to be almost identical to set-old-8.47
    catch {unset a}
    list [catch {array start a} msg] $msg
} {1 {"a" isn't an array}}
test set-old-10.3 {array enumeration errors} {
    catch {unset a}
    list [catch {array start a} msg] $msg
} {1 {"a" isn't an array}}
test set-old-10.4 {array enumeration errors} {
    catch {unset a}
    set a(a) 1

Changes to tests/var.test.

742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
    array exists a
} -result 0
test var-11.3 {array unset errors} -setup {
    catch {unset a}
} -returnCodes error -body {
    array set a { 1,1 a 1,2 b }
    array unset a pattern too
} -result {wrong # args: should be "array unset arrayName ?pattern?"}

test var-12.1 {TclFindCompiledLocals, {} array name} {
    namespace eval n {
	proc p {} {
	    variable {}
	    set (0) 0
	    set (1) 1







|







742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
    array exists a
} -result 0
test var-11.3 {array unset errors} -setup {
    catch {unset a}
} -returnCodes error -body {
    array set a { 1,1 a 1,2 b }
    array unset a pattern too
} -result {bad option "pattern": must be -exact, -glob, or -regexp}

test var-12.1 {TclFindCompiledLocals, {} array name} {
    namespace eval n {
	proc p {} {
	    variable {}
	    set (0) 0
	    set (1) 1
773
774
775
776
777
778
779


















780
781
782
783
784
785
786
    namespace eval :: {
	set t(1) 1
	trace variable t(1) u foo
	unset t
    }
    set x "If you see this, it worked"
} -result "If you see this, it worked"



















test var-14.1 {array names syntax} -body {
    array names foo bar baz snafu
} -returnCodes 1 -match glob -result *
test var-14.2 {array names -glob} -body {
    array names tcl_platform -glob os
} -result os







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
    namespace eval :: {
	set t(1) 1
	trace variable t(1) u foo
	unset t
    }
    set x "If you see this, it worked"
} -result "If you see this, it worked"

test var-13.2 {unset array with search, bug 46a2410650} -body {
    apply {{} {
	array set a {aa 11 bb 22 cc 33 dd 44 ee 55 ff 66}
	set s [array startsearch a]
	unset a([array nextelement a $s])
	array nextelement a $s
    }}
} -returnCodes error -result {couldn't find search "s-1-a"}

test var-13.3 {unset array with search, SIGSEGV, bug 46a2410650} -body {
    apply {{} {
	array set a {aa 11 bb 22 cc 33 dd 44 ee 55 ff 66}
	set s [array startsearch a]
        unset a(ff)
	array nextelement a $s
    }}
} -returnCodes error -result {couldn't find search "s-1-a"}

test var-14.1 {array names syntax} -body {
    array names foo bar baz snafu
} -returnCodes 1 -match glob -result *
test var-14.2 {array names -glob} -body {
    array names tcl_platform -glob os
} -result os