Changes On Branch b15d607c3a76db13
Not logged in

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

Changes In Branch amg-array-enum-c-api Through [b15d607c3a] Excluding Merge-Ins

This is equivalent to a diff from ad557c91a9 to b15d607c3a

2016-11-25
05:53
Implement Tcl_ArrayNames(), still have a bug to fix though check-in: a7b63341ab user: andy tags: amg-array-enum-c-api
05:11
Implement various array functions in terms of ArrayVar() to avoid repetition of trace code, and rest... check-in: b15d607c3a user: andy tags: amg-array-enum-c-api
04:42
Implement Tcl_ArraySearchDone() check-in: fc39454bd5 user: andy tags: amg-array-enum-c-api
01:39
Remove spurious article in comments check-in: 965949e559 user: andy tags: trunk
2016-11-24
20:55
merge trunk check-in: c1735f1869 user: jan.nijtmans tags: package_files
20:31
merge trunk check-in: 4115262f80 user: jan.nijtmans tags: novem
19:22
Merge trunk check-in: a353ee5093 user: andy tags: amg-array-enum-c-api
19:21
Partially backout [ef4da65408] because AppendPrintfToObjVA() (used indirectly by [tcl::unsupported::... check-in: ad557c91a9 user: andy tags: trunk
13:33
Make compileEpoch "unsigned int", and start counting at 1. check-in: 83f0d245d8 user: jan.nijtmans tags: trunk

Changes to generic/tcl.decls.
2321
2322
2323
2324
2325
2326
2327




















2328
2329
2330
2331
2332
2333
2334
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







# TIP #400
declare 630 {
    void Tcl_ZlibStreamSetCompressionDictionary(Tcl_ZlibStream zhandle,
	    Tcl_Obj *compressionDictionaryObj)
}

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

# TIP #XXX
declare 631 {
    int Tcl_ArraySize(Tcl_Interp *interp, Tcl_Obj *part1Ptr,
	Tcl_Obj *part2Ptr, int flags)
}
declare 632 {
    Tcl_ArraySearch Tcl_ArraySearchStart(Tcl_Interp *interp,
	Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr, int flags)
}
declare 633 {
    Tcl_Obj *Tcl_ArraySearchNext(Tcl_ArraySearch search)
}
declare 634 {
    void Tcl_ArraySearchDone(Tcl_ArraySearch search)
}
declare 635 {
    int Tcl_ArrayNames(Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr,
	Tcl_Obj *listPtr, 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
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;
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
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







+










+





+








/*
 * 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_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
#define TCL_MATCH_GLOB		 0x400
#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
/* 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.
 */
Changes to generic/tclDecls.h.
1812
1813
1814
1815
1816
1817
1818















1819
1820
1821
1822
1823
1824
1825
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







/* 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_ArraySize(Tcl_Interp *interp, Tcl_Obj *part1Ptr,
				Tcl_Obj *part2Ptr, int flags);
/* 632 */
EXTERN Tcl_ArraySearch	Tcl_ArraySearchStart(Tcl_Interp *interp,
				Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr,
				int flags);
/* 633 */
EXTERN Tcl_Obj *	Tcl_ArraySearchNext(Tcl_ArraySearch search);
/* 634 */
EXTERN void		Tcl_ArraySearchDone(Tcl_ArraySearch search);
/* 635 */
EXTERN int		Tcl_ArrayNames(Tcl_Interp *interp, Tcl_Obj *part1Ptr,
				Tcl_Obj *part2Ptr, Tcl_Obj *listPtr,
				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
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511







+
+
+
+
+







    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_ArraySize) (Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr, int flags); /* 631 */
    Tcl_ArraySearch (*tcl_ArraySearchStart) (Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr, int flags); /* 632 */
    Tcl_Obj * (*tcl_ArraySearchNext) (Tcl_ArraySearch search); /* 633 */
    void (*tcl_ArraySearchDone) (Tcl_ArraySearch search); /* 634 */
    int (*tcl_ArrayNames) (Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr, Tcl_Obj *listPtr, int flags); /* 635 */
} TclStubs;

extern const TclStubs *tclStubsPtr;

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










3777
3778
3779
3780
3781
3782
3783
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813







+
+
+
+
+
+
+
+
+
+







	(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_ArraySize \
	(tclStubsPtr->tcl_ArraySize) /* 631 */
#define Tcl_ArraySearchStart \
	(tclStubsPtr->tcl_ArraySearchStart) /* 632 */
#define Tcl_ArraySearchNext \
	(tclStubsPtr->tcl_ArraySearchNext) /* 633 */
#define Tcl_ArraySearchDone \
	(tclStubsPtr->tcl_ArraySearchDone) /* 634 */
#define Tcl_ArrayNames \
	(tclStubsPtr->tcl_ArrayNames) /* 635 */

#endif /* defined(USE_TCL_STUBS) */

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

#if defined(USE_TCL_STUBS)
#   undef Tcl_CreateInterp
Changes to generic/tclStubInit.c.
1412
1413
1414
1415
1416
1417
1418





1419
1420
1421
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426







+
+
+
+
+



    Tcl_CloseEx, /* 624 */
    Tcl_NRExprObj, /* 625 */
    Tcl_NRSubstObj, /* 626 */
    Tcl_LoadFile, /* 627 */
    Tcl_FindSymbol, /* 628 */
    Tcl_FSUnloadFile, /* 629 */
    Tcl_ZlibStreamSetCompressionDictionary, /* 630 */
    Tcl_ArraySize, /* 631 */
    Tcl_ArraySearchStart, /* 632 */
    Tcl_ArraySearchNext, /* 633 */
    Tcl_ArraySearchDone, /* 634 */
    Tcl_ArrayNames, /* 635 */
};

/* !END!: Do not edit above this line. */
Changes to generic/tclVar.c.
44
45
46
47
48
49
50








51

52
53

54
55
56
57
58
59
60
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







+
+
+
+
+
+
+
+
-
+

-
+







static inline Var *	VarHashNextVar(Tcl_HashSearch *searchPtr);
static inline void	CleanupVar(Var *varPtr, Var *arrayPtr);

#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)

/*
 * NOTE: VarHashCreateVar increments the recount of its key argument.
 * 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
 * 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,
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
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







-
+
+
+


-
+
+
+




-
+



-
-
+
+
-
-
-
+
+
-
-
+


-
+
+
+







 * 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.
 * array variable. It is used by Tcl_ArraySize(), Tcl_ArraySearchStart(),
 * Tcl_ArraySearchNext(), Tcl_ArraySearchDone(), Tcl_ArrayNames(), and their
 * respective [array] script interface commands.
 */

typedef struct ArraySearch {
typedef struct Tcl_ArraySearch_ ArraySearch;
struct Tcl_ArraySearch_ {
    Tcl_Interp *interp;		/* Tcl interpreter in which search is run. */
    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
    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
    Var *nextEntry;		/* Non-NULL means this is the next element to
				 * be enumerated (left over from ArrayFirst()
				 * Tcl_FirstHashEntry call or from an "array
				 * anymore" command). NULL means must call
				 * Tcl_NextHashEntry to get value to
				 * or [array anymore]). NULL means must call
				 * ArrayNext() to get value to return. */
				 * return. */
    struct ArraySearch *nextPtr;/* Next in list of all active searches for
    ArraySearch *nextPtr;	/* Next in list of all active searches for
				 * this variable, or NULL if this is the last
				 * one. */
} ArraySearch;
    Tcl_Obj *filterObj;		/* Search filter pattern, or NULL if none. */
    int filterType;		/* TCL_MATCH_EXACT, _GLOB, or _REGEXP. */
};

/*
 * 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
197
198
199
200
201
202
203

204
205
206
207
208
209
210
211
212
213
214
215
216







-
+
+
+
+
+
+







			    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);
static Var *		ArrayVar(Tcl_Interp *interp, Tcl_Obj *varNameObj,
			    int *traceFailPtr, int flags);
static Var *		ArrayFirst(ArraySearch *searchPtr, int *failPtr);
static Var *		ArrayNext(ArraySearch *searchPtr, int *failPtr);
static int		ArraySize(Tcl_Interp *interp, Var *varPtr,
			    Tcl_Obj *filterObj, int filterType);

/*
 * 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,
913
914
915
916
917
918
919
920

921
922
923
924
925
926
927
930
931
932
933
934
935
936

937
938
939
940
941
942
943
944







-
+







 * 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 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
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
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
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







		Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ELEMENT",
			TclGetString(elNamePtr), NULL);
	    }
	}
    }
    return varPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayVar --
 *
 *	This function looks up an existing array variable.
 *
 * 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.
 *	
 *----------------------------------------------------------------------
 */

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, and TCL_LEAVE_ERR_MSG. */
{
    Var *varPtr, *arrayPtr;
    const char *varName;

    /*
     * Locate the array variable.
     */

    varPtr = TclObjLookupVarEx(interp, varNameObj, NULL,
	    flags & ~TCL_LEAVE_ERR_MSG, /*msg*/ NULL, /*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((Interp *)interp, arrayPtr, varPtr, varNameObj,
		NULL, flags | TCL_LEAVE_ERR_MSG | TCL_TRACE_ARRAY,
		/*leaveErrMsg*/ 1, -1) == TCL_ERROR) {
	    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 (!varPtr || !TclIsVarArray(varPtr) || TclIsVarUndefined(varPtr)) {
	varName = TclGetString(varNameObj);
	if (flags & TCL_LEAVE_ERR_MSG) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		    "\"%s\" isn't an array", varName));
	    Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ARRAY", varName, NULL);
	}
	return NULL;
    }

    /*
     * On success, give the caller the address of the variable object.
     */

    return varPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * 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(
    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.nextIndex = tablePtr->table.numBuckets;

	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(searchPtr, failPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * ArrayNext --
 *
 *	Finds the next element of an array for a given search query.
 *
 * 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(
    ArraySearch *searchPtr,	/* Array enumeration state structure. */
    int *failPtr)		/* Set to 1 on error. */
{
    Var *varPtr = searchPtr->nextEntry;
    Tcl_Obj *nameObj;
    int matched;

    /*
     * If there is no cached nextEntry left over from ArrayFirst() or [array
     * anymore], get the next one from the hash table.
     */

    if (varPtr) {
	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(searchPtr->interp, nameObj,
			searchPtr->filterObj);
		if (matched < 0) {
		    if (failPtr) {
			*failPtr = 1;
		    }
		    return NULL;
		} else if (matched) {
		    return varPtr;
		}
	    } else {
		Tcl_Panic("invalid filter type: %u", searchPtr->filterType);
	    }
	}
    }

    return NULL;
}

/*
 *----------------------------------------------------------------------
 *
 * ArraySize --
 *
 *	This function returns the number of elements in an array variable.
 *
 * Results:
 *	The return value is the integer count of array elements. The only
 *	possible error is a regular expression error, in which case -1 is
 *	returned and the error information is loaded into the interp result.
 *
 * Side effects:
 *	None.
 *	
 *----------------------------------------------------------------------
 */

static int
ArraySize(
    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. */
{
    ArraySearch search;
    int fail = 0, size = 0;

    /*
     * Count the number of times ArrayFirst() or ArrayNext() returns non-NULL.
     */

    search.interp = interp;
    search.varPtr = varPtr;
    search.filterObj = filterObj;
    search.filterType = filterType;
    search.nextEntry = NULL;
    varPtr = ArrayFirst(&search, &fail);
    for (; varPtr; varPtr = ArrayNext(&search, &fail)) {
	++size;
    }

    /*
     * Return -1 on error or the number of matching elements on success.
     */

    if (fail) {
	return -1;
    } else {
	return size;
    }
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ArraySize --
 *
 *	This function returns the number of elements in an array variable. It
 *	provides C-level access to [array size] functionality, except this
 *	function does not treat scalar and nonexistent variable as if they were
 *	empty arrays. 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 TCL_MATCH_* being set within flags.
 *
 * Results:
 *	The return value is normally the integer count of array elements whose
 *	names match the given filter. If varNamePtr does not name an array, -1
 *	is returned and an error message is placed in interp's result.
 *
 * Side effects:
 *	None.
 *	
 *----------------------------------------------------------------------
 */

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 flags)			/* OR-ed combination of TCL_GLOBAL_ONLY,
				 * TCL_NAMESPACE_ONLY, and TCL_LEAVE_ERR_MSG,
				 * also at most one of TCL_MATCH_EXACT, _GLOB,
				 * and _REGEXP. */
{
    Var *varPtr = ArrayVar(interp, part1Ptr, NULL, flags);
    return varPtr ? ArraySize(interp, varPtr, part2Ptr, flags & TCL_MATCH) : -1;
}

/*
 *----------------------------------------------------------------------
 *
 * 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 TCL_MATCH_* being set within flags.
 *
 * Results:
 *	A new array search is created, a pointer to which is returned. If the
 *	variable does not exist or is not an array, NULL is returned, and no
 *	search is created.
 *
 * Side effects:
 *	On success, the search is allocated on the heap and will need to be
 *	deallocated by a future call to Tcl_ArraySearchDone().
 *
 *----------------------------------------------------------------------
 */

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,
				 * TCL_NAMESPACE_ONLY, and TCL_LEAVE_ERR_MSG,
				 * also at most one of TCL_MATCH_EXACT, _GLOB,
				 * and _REGEXP. */
{
    Interp *iPtr = (Interp *)interp;
    Var *varPtr = ArrayVar(interp, part1Ptr, NULL, flags);
    Tcl_HashEntry *hPtr;
    int isNew, fail = 0;
    ArraySearch search, *searchPtr;

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

    if (!varPtr) {
	return NULL;
    }

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

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

    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);
    }
    search.name = Tcl_ObjPrintf("s-%d-%s", search.id, TclGetString(part1Ptr));
    Tcl_IncrRefCount(search.name);
    if (part2Ptr) {
	Tcl_IncrRefCount(part2Ptr);
    }
    searchPtr = ckalloc(sizeof(*searchPtr));
    *searchPtr = search;
    Tcl_SetHashValue(hPtr, searchPtr);

    return searchPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * 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 return value is the name of the next array element. If there are no
 *	more array elements, NULL is returned.
 *
 * Side effects:
 *	The search data structure is updated such that successive invocations of
 *	this function will return successive array element names.
 *
 * Limitations:
 *	It is not possible to distinguish between reaching the end of the array
 *	and experiencing a regular expression error. This is unlikely to be an
 *	actual problem because Tcl_ArraySearchStart() already checks for regular
 *	expression errors and returns NULL if found. If the regular expression
 *	engine has a bug whereby a given query can initially succeed yet return
 *	error depending on the string it is matched against, the caller of this
 *	function will perceive it as prematurely hitting the end of the array.
 *
 *----------------------------------------------------------------------
 */

Tcl_Obj *
Tcl_ArraySearchNext(
    Tcl_ArraySearch search)	/* Return value of Tcl_ArraySearchStart(). */
{
    Var *varPtr = ArrayNext(search, NULL);
    return varPtr ? VarHashGetKey(varPtr) : NULL;
}

/*
 *----------------------------------------------------------------------
 *
 * 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:
 *	The search query is completed.
 *
 * Side effects:
 *	Resources associated with the search are deallocated.
 *
 *----------------------------------------------------------------------
 */

void
Tcl_ArraySearchDone(
    Tcl_ArraySearch search)	/* Return value of Tcl_ArraySearchStart(). */
{
    Interp *iPtr = (Interp *)search->interp;
    Var *varPtr = search->varPtr;
    Tcl_HashEntry *hPtr = Tcl_FindHashEntry(&iPtr->varSearches, varPtr);
    ArraySearch *prevPtr;

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

    if (search == Tcl_GetHashValue(hPtr)) {
	if (search->nextPtr) {
	    Tcl_SetHashValue(hPtr, search->nextPtr);
	} else {
	    varPtr->flags &= ~VAR_SEARCH_ACTIVE;
	    Tcl_DeleteHashEntry(hPtr);
	}
    } else {
	for (prevPtr = Tcl_GetHashValue(hPtr);; prevPtr = prevPtr->nextPtr) {
	    if (prevPtr->nextPtr == search) {
		prevPtr->nextPtr = search->nextPtr;
		break;
	    }
	}
    }
    Tcl_DecrRefCount(search->name);
    if (search->filterObj) {
	Tcl_DecrRefCount(search->filterObj);
    }
    ckfree(search);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_ArrayNames --
 *
 *----------------------------------------------------------------------
 */

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,
    int flags)			/* OR-ed combination of TCL_GLOBAL_ONLY and
				 * TCL_NAMESPACE_ONLY, also at most one of
				 * TCL_MATCH_EXACT, _GLOB, and _REGEXP. */
{
    return 0;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetVar --
 *
 *	Return the value of a Tcl variable as a string.
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
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







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







-
-
-
-







-
-
+
+



-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







 * 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) {
    searchPtr = Tcl_ArraySearchStart(interp, objv[1], NULL, TCL_LEAVE_ERR_MSG);
    if (!searchPtr) {
	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;
}

/*
 *----------------------------------------------------------------------
 *
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
3429
3430
3431
3432
3433
3434
3435

3436
3437
3438
3439
3440
3441
3442
3443
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







-

+








-
+
-













-
-
+
+
+


-
-
+
-
-
-
-
-
+
+
+
+
-
-
-
-
+
-
-
-
+
+







    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr;
    Tcl_Obj *varNameObj, *searchObj;
    int gotValue;
    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];

    varPtr = VerifyArray(interp, varNameObj);
    if (!(varPtr = ArrayVar(interp, varNameObj, NULL, TCL_LEAVE_ERR_MSG))) {
    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.
     * 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.
     */

    while (1) {
	if (searchPtr->nextEntry != NULL) {
    if ((searchPtr->nextEntry = ArrayNext(searchPtr, &fail))) {
	    varPtr = VarHashGetValue(searchPtr->nextEntry);
	    if (!TclIsVarUndefined(varPtr)) {
		gotValue = 1;
		break;
	    }
	gotValue = 1;
    } else if (fail) {
	return TCL_ERROR;
    } else {
	}
	searchPtr->nextEntry = Tcl_NextHashEntry(&searchPtr->search);
	if (searchPtr->nextEntry == NULL) {
	    gotValue = 0;
	gotValue = 0;
	    break;
	}
    }
    }

    Tcl_SetObjResult(interp, iPtr->execEnvPtr->constants[gotValue]);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
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
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







-
+









-
+
-














-
+
-
-


-
-
-
+
-
-
+
-
-
-
+
-
-
-
+
-
-
-
-
+
-
-







ArrayNextElementCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Var *varPtr;
    Tcl_Obj *varNameObj, *searchObj;
    Tcl_Obj *varNameObj, *searchObj, *resultObj;
    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 = ArrayVar(interp, varNameObj, NULL, TCL_LEAVE_ERR_MSG))) {
    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
     * exhaustion.
     * pulled a value from the hash enumeration, so we have to check the cache
     * there first.
     */

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

    if ((resultObj = Tcl_ArraySearchNext(searchPtr))) {
	if (hPtr == NULL) {
	    hPtr = Tcl_NextHashEntry(&searchPtr->search);
	Tcl_SetObjResult(interp, resultObj);
	    if (hPtr == NULL) {
		return TCL_OK;
	    }
    }
	} else {
	    searchPtr->nextEntry = NULL;
	}

	varPtr = VarHashGetValue(hPtr);
	if (!TclIsVarUndefined(varPtr)) {
	    Tcl_SetObjResult(interp, VarHashGetKey(varPtr));
	    return TCL_OK;
    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
3558
3559
3560
3561
3562
3563
3564

3565

3566

3567
3568
3569
3570
3571
3572
3573
3574
3575

3576

3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593


















3594
3595
3596
3597
3598
3599
3600
3601







-

-

-
+








-
+
-

















-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+







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;
    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 = ArrayVar(interp, varNameObj, NULL, TCL_LEAVE_ERR_MSG))) {
    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);
    Tcl_ArraySearchDone(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
3617
3618
3619
3620
3621
3622
3623

3624
3625

3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637

3638


3639



3640


3641







3642
3643
3644
3645
3646







3647
3648
3649
3650
3651
3652
3653
3654







-
+

-
+











-
+
-
-
+
-
-
-
+
-
-
+
-
-
-
-
-
-
-
+
+
+


-
-
-
-
-
-
-
+







ArrayExistsCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr, *arrayPtr;
    Var *varPtr;
    Tcl_Obj *arrayNameObj;
    int notArray;
    int traceFail = 0, isArray;

    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,
    varPtr = ArrayVar(interp, arrayNameObj, &traceFail, 0);
	    /*msg*/ 0, /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr);

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

    } else if (traceFail) {
    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;
	}
	return TCL_ERROR;
    } else {
	isArray = 0;
    }

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

    notArray = ((varPtr == NULL) || !TclIsVarArray(varPtr)
	    || TclIsVarUndefined(varPtr));
    Tcl_SetObjResult(interp, iPtr->execEnvPtr->constants[!notArray]);
    Tcl_SetObjResult(interp, iPtr->execEnvPtr->constants[isArray]);
    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
3669
3670
3671
3672
3673
3674
3675


3676
3677
3678
3679
3680

3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
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







-
-
+




-
+



















-
+
-


-
-
+
+


-
-
-
+
+
-
-

-
+
-
-
+
-
-
-
-
-
-
+
-
-
-







static int
ArrayGetCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr, *arrayPtr, *varPtr2;
    Var *varPtr, *varPtr2;
    Tcl_Obj *varNameObj, *nameObj, *valueObj, *nameLstObj, *tmpResObj;
    Tcl_Obj **nameObjPtr, *patternObj;
    Tcl_HashSearch search;
    const char *pattern;
    int i, count, result;
    int traceFail = 0, 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,
    varPtr = ArrayVar(interp, varNameObj, &traceFail, 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.
     * Report trace failures as errors. If the variable is a scalar or does not
     * exist, treat it like an empty array.
     */

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

	    return TCL_OK;
    /*
     * 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.
     */
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
3843
3844
3845
3846
3847
3848
3849


3850
3851
3852
3853

3854
3855
3856
3857
3858
3859
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







-
-
+



-
+












-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-











-
-
-
+
+


-
-
-
+
+
+
+
+
+







    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;
    Var *varPtr, *varPtr2;
    Tcl_Obj *varNameObj, *nameObj, *resultObj, *patternObj;
    Tcl_HashSearch search;
    const char *pattern = NULL;
    int mode = OPT_GLOB;
    int traceFail = 0, 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,
    varPtr = ArrayVar(interp, varNameObj, &traceFail, 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.
     * Report trace failures as errors. If the variable is a scalar or does not
     * exist, treat it like an empty array.
     */

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

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

    TclNewObj(resultObj);
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
4017
4018
4019
4020
4021
4022
4023

4024

4025
4026
4027
4028
4029
4030







4031
4032
4033
4034
4035



4036



4037

4038
4039
4040
4041
4042
4043
4044







-
+
-






-
-
-
-
-
-
-





-
-
-
+
-
-
-
+
-







static int
ArraySetCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    int traceFail = 0;
    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,
    if (!ArrayVar(interp, objv[1], &traceFail, 0) && traceFail) {
		(TCL_LEAVE_ERR_MSG|TCL_NAMESPACE_ONLY|TCL_GLOBAL_ONLY|
		TCL_TRACE_ARRAY), /* leaveErrMsg */ 1, -1) == TCL_ERROR) {
	    return TCL_ERROR;
	return TCL_ERROR;
	}
    }

    return TclArraySet(interp, objv[1], objv[2]);
}

/*
 *----------------------------------------------------------------------
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
4061
4062
4063
4064
4065
4066
4067






4068
4069
4070
4071
4072
4073
4074

4075
4076



4077



4078


4079

4080
4081









4082





4083

4084





4085





4086
4087
4088

4089
4090
4091
4092
4093
4094
4095
4096







-
-
-
-
-
-
+
+





-


-
-
-
+
-
-
-
+
-
-
+
-


-
-
-
-
-
-
-
-
-
+
-
-
-
-
-

-
+
-
-
-
-
-
+
-
-
-
-
-
+
+
+
-
+







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;
    Var *varPtr;
    int traceFail = 0, size;

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

    /*
     * Locate the array variable.
     */

     * Unlike Tcl_ArraySize(), the [array size] command treats nonexistent and
    varPtr = TclObjLookupVarEx(interp, varNameObj, NULL, /*flags*/ 0,
	    /*msg*/ 0, /*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr);

     * non-array variables as having zero size. The only errors [array size] can
    /*
     * Special array trace used to keep the env array in sync for array names,
     * report are argument count (handled above) and array trace (handled here).
     * 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;
	}
    }

    varPtr = ArrayVar(interp, objv[1], &traceFail, 0);
    /*
     * 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)) {
    if (varPtr) {
	/*
	 * Must iterate in order to get chance to check for present but
	 * "undefined" entries.
	 */

	size = ArraySize(interp, varPtr, NULL, 0);
	for (varPtr2=VarHashFirstVar(varPtr->value.tablePtr, &search);
		varPtr2!=NULL ; varPtr2=VarHashNextVar(&search)) {
	    if (!TclIsVarUndefined(varPtr2)) {
		size++;
	    }
    } else if (!traceFail) {
	size = 0;
    } else {
	}
	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
4115
4116
4117
4118
4119
4120
4121


4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136



4137





4138





















4139
4140
4141
4142
4143
4144
4145







-
-
+


+











-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







static int
ArrayStatsCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr, *arrayPtr;
    Var *varPtr;
    Tcl_Obj *varNameObj;
    char *stats;
    int traceFail = 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);

    varPtr = ArrayVar(interp, varNameObj, &traceFail, TCL_LEAVE_ERR_MSG);
    /*
     * Special array trace used to keep the env array in sync for array names,
     * array get, etc.
     */

    if (!varPtr) {
    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));
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
4171
4172
4173
4174
4175
4176
4177


4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202

4203

4204



4205


4206





4207

4208


4209






4210



4211
4212
4213
4214
4215
4216
4217







-
-
+



+




















-
+
-

-
-
-
+
-
-
+
-
-
-
-
-

-
+
-
-
+
-
-
-
-
-
-
+
-
-
-







static int
ArrayUnsetCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Interp *iPtr = (Interp *) interp;
    Var *varPtr, *arrayPtr, *varPtr2, *protectedVarPtr;
    Var *varPtr, *varPtr2, *protectedVarPtr;
    Tcl_Obj *varNameObj, *patternObj, *nameObj;
    Tcl_HashSearch search;
    const char *pattern;
    int traceFail = 0;
    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,
    varPtr = ArrayVar(interp, varNameObj, &traceFail, 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) {
     */

	if (traceFail) {
    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;
	}
	} else {
    }

	    return TCL_OK;
    /*
     * 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.
	 */