File: bltTupleCmd.c

package info (click to toggle)
blt 2.5.3%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 24,864 kB
  • sloc: ansic: 133,724; tcl: 17,680; sh: 2,722; makefile: 799; cpp: 370
file content (3051 lines) | stat: -rw-r--r-- 84,344 bytes parent folder | download | duplicates (9)
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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
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
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
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
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
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
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
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051

/*
 *
 * bltTupleCmd.c --
 *
 * Copyright 1998-1999 Lucent Technologies, Inc.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that the copyright notice and warranty
 * disclaimer appear in supporting documentation, and that the names
 * of Lucent Technologies or any of their entities not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.
 *
 * Lucent Technologies disclaims all warranties with regard to this
 * software, including all implied warranties of merchantability and
 * fitness.  In no event shall Lucent Technologies be liable for any
 * special, indirect or consequential damages or any damages
 * whatsoever resulting from loss of use, data or profits, whether in
 * an action of contract, negligence or other tortuous action, arising
 * out of or in connection with the use or performance of this
 * software.
 *
 *	The "tuple" data object was created by George A. Howlett.
 */

/*
  tuple create t0 t1 t2
  tuple names
  t0 destroy
     -or-
  tuple destroy t0
  tuple copy tuple@node tuple@node -recurse -tags

  tuple move node after|before|into t2@node

  $t apply -recurse $root command arg arg			

  $t attach tuplename				

  $t children $n
  t0 copy node1 node2 node3 node4 node5 destName 
  $t delete $n...				
  $t delete 0 
  $t delete 0 10
  $t delete all
  $t depth $n
  $t dump $root
  $t dumpfile $root fileName
  $t dup $t2		
  $t find $root -name pat -name pattern
  $t firstchild $n
  $t get $n $key
  $t get $n $key(abc)
  $t index $n
  $t insert $parent $switches?
  $t isancestor $n1 $n2
  $t isbefore $n1 $n2
  $t isleaf $n
  $t lastchild $n
  $t move $n1 after|before|into $n2
  $t next $n
  $t nextsibling $n
  $t path $n1 $n2 $n3...
  $t parent $n
  $t previous $n
  $t prevsibling $n
  $t restore $root data -overwrite
  $t root ?$n?

  $t set $n $key $value ?$key $value?
  $t size $n
  $t slink $n $t2@$node				???
  $t sort -recurse $root		

  $t tag delete tag1 tag2 tag3...
  $t tag names
  $t tag nodes $tag
  $t tag set $n tag1 tag2 tag3...
  $t tag unset $n tag1 tag2 tag3...

  $t trace create $n $key how command		
  $t trace delete id1 id2 id3...
  $t trace names
  $t trace info $id

  $t unset $n key1 key2 key3...
  
  $t notify create -oncreate -ondelete -onmove command 
  $t notify create -oncreate -ondelete -onmove -onsort command arg arg arg 
  $t notify delete id1 id2 id3
  $t notify names
  $t notify info id

  for { set n [$t firstchild $node] } { $n >= 0 } { 
        set n [$t nextsibling $n] } {
  }
  foreach n [$t children $node] { 
	  
  }
  set n [$t next $node]
  set n [$t previous $node]

*/

/*
 * Rows as tokens.  
 * 
 */

#include <bltInt.h>

#ifndef NO_TUPLE

#include <bltTuple.h>
#include <bltHash.h>
#include <bltList.h>
#include "bltSwitch.h"
#include <ctype.h>

#define TUPLE_THREAD_KEY "BLT Tuple Command Data"
#define TUPLE_MAGIC ((unsigned int) 0x46170277)

enum TagTypes { TAG_TYPE_NONE, TAG_TYPE_ALL, TAG_TYPE_TAG };

typedef struct {
    Blt_HashTable instTable;	/* Hash table of tuples keyed by address. */
    Tcl_Interp *interp;
} TupleCmdInterpData;

typedef struct {
    Tcl_Interp *interp;
    Blt_TupleTable table;	/* Token representing the tuple
				 * table. */
    Tcl_Command cmdToken;	/* Token for tuple object's Tcl
				 * command. */

    Tcl_Obj *emptyObjPtr;	/* String representing an empty table
				 * value. */
    Blt_HashEntry *hashPtr;
    Blt_HashTable *instTablePtr;

    int traceCounter;		/* Used to generate trace id strings.  */
    Blt_HashTable traceTable;	/* Table of active traces. Maps trace ids
				 * back to their TraceInfo records. */

    int notifyCounter;		/* Used to generate notify id strings. */
    Blt_HashTable notifyTable;	/* Table of event handlers. Maps notify ids
				 * back to their NotifyInfo records. */
} TupleCmd;

typedef struct {
    Blt_TupleTrace trace;
    TupleCmd *cmdPtr;
    Tcl_Obj **objv;
    int objc;
    Blt_HashEntry *hashPtr;
} TraceInfo;

typedef struct {
    Blt_TupleNotifier notifier;
    TupleCmd *cmdPtr;
    Tcl_Obj **objv;
    int objc;
    Blt_HashEntry *hashPtr;
} NotifyInfo;

typedef struct {
    unsigned int column;
    Tcl_Obj *objPtr;
} KeyValue;

typedef struct {
    int mask;
} NotifyData;

static Blt_SwitchSpec notifySwitches[] = 
{
    {BLT_SWITCH_FLAG, "-create", Blt_Offset(NotifyData, mask), 0, 0, 
	TUPLE_NOTIFY_CREATE},
    {BLT_SWITCH_FLAG, "-createrow", Blt_Offset(NotifyData, mask), 0, 0, 
	TUPLE_NOTIFY_CREATE_ROW},
    {BLT_SWITCH_FLAG, "-createcolumn", Blt_Offset(NotifyData, mask), 0, 0, 
	TUPLE_NOTIFY_CREATE_COLUMN},
    {BLT_SWITCH_FLAG, "-delete", Blt_Offset(NotifyData, mask), 0, 0, 
	TUPLE_NOTIFY_DELETE},
    {BLT_SWITCH_FLAG, "-deleterow", Blt_Offset(NotifyData, mask), 0, 0, 
	TUPLE_NOTIFY_DELETE_ROW},
    {BLT_SWITCH_FLAG, "-deletecolumn", Blt_Offset(NotifyData, mask), 0, 0, 
	TUPLE_NOTIFY_DELETE_COLUMN},
    {BLT_SWITCH_FLAG, "-move", Blt_Offset(NotifyData, mask), 0, 0, 
	TUPLE_NOTIFY_MOVE},
    {BLT_SWITCH_FLAG, "-sort", Blt_Offset(NotifyData, mask), 0, 0, 
	TUPLE_NOTIFY_SORT},
    {BLT_SWITCH_FLAG, "-relabel", Blt_Offset(NotifyData, mask), 0, 0, 
	TUPLE_NOTIFY_RELABEL},
    {BLT_SWITCH_FLAG, "-allevents", Blt_Offset(NotifyData, mask), 0, 0, 
	TUPLE_NOTIFY_ALL},
    {BLT_SWITCH_FLAG, "-whenidle", Blt_Offset(NotifyData, mask), 0, 0, 
	TUPLE_NOTIFY_WHENIDLE},
    {BLT_SWITCH_END, NULL, 0, 0}
};

#ifdef notdef

static Blt_SwitchParseProc StringToChild;
#define INSERT_BEFORE	(ClientData)0
#define INSERT_AFTER	(ClientData)1
static Blt_SwitchCustom beforeSwitch =
{
    StringToChild, (Blt_SwitchFreeProc *)NULL, INSERT_BEFORE,
};
static Blt_SwitchCustom afterSwitch =
{
    StringToChild, (Blt_SwitchFreeProc *)NULL, INSERT_AFTER,
};

typedef struct {
    char *label;
    int insertPos;
    int inode;
    char **tags;
    char **dataPairs;
    Blt_TupleNode parent;
} InsertData;

static Blt_SwitchSpec insertSwitches[] = 
{
    {BLT_SWITCH_CUSTOM, "-after", Blt_Offset(InsertData, insertPos), 0, 
	&afterSwitch},
    {BLT_SWITCH_INT_NONNEGATIVE, "-at", Blt_Offset(InsertData, insertPos), 0},
    {BLT_SWITCH_CUSTOM, "-before", Blt_Offset(InsertData, insertPos), 0,
	&beforeSwitch},
    {BLT_SWITCH_LIST, "-data", Blt_Offset(InsertData, dataPairs), 0},
    {BLT_SWITCH_STRING, "-label", Blt_Offset(InsertData, label), 0},
    {BLT_SWITCH_INT_NONNEGATIVE, "-node", Blt_Offset(InsertData, inode), 0},
    {BLT_SWITCH_LIST, "-tags", Blt_Offset(InsertData, tags), 0},
    {BLT_SWITCH_END, NULL, 0, 0}
};

#define PATTERN_EXACT	(1)
#define PATTERN_GLOB	(2)
#define PATTERN_MASK	(0x3)
#define PATTERN_NONE	(0)
#define PATTERN_REGEXP	(3)
#define MATCH_INVERT		(1<<8)
#define MATCH_LEAFONLY		(1<<4)
#define MATCH_NOCASE		(1<<5)
#define MATCH_PATHNAME		(1<<6)

typedef struct {
    TupleCmd *cmdPtr;		/* Tuple to examine. */
    Tcl_Obj *listObjPtr;	/* List to accumulate the indices of 
				 * matching nodes. */
    Tcl_Obj **objv;		/* Command converted into an array of 
				 * Tcl_Obj's. */
    int objc;			/* Number of Tcl_Objs in above array. */

    int nMatches;		/* Current number of matches. */

    unsigned int flags;		/* See flags definitions above. */

    /* Integer options. */
    int maxMatches;		/* If > 0, stop after this many matches. */
    int maxDepth;		/* If > 0, don't descend more than this
				 * many levels. */
    int order;			/* Order of search: Can be either
				 * TUPLE_PREORDER, TUPLE_POSTORDER,
				 * TUPLE_INORDER, TUPLE_BREADTHFIRST. */
    /* String options. */
    Blt_List patternList;	/* List of patterns to compare with labels
				 * or values.  */
    char *addTag;		/* If non-NULL, tag added to selected nodes. */

    char **command;		/* Command split into a Tcl list. */

    Blt_List keyList;		/* List of key name patterns. */
    char *withTag;

} FindData;

static Blt_SwitchParseProc StringToOrder;
static Blt_SwitchCustom orderSwitch =
{
    StringToOrder, (Blt_SwitchFreeProc *)NULL, (ClientData)0,
};

static Blt_SwitchParseProc StringToPattern;
static Blt_SwitchFreeProc FreePatterns;

static Blt_SwitchCustom regexpSwitch =
{
    StringToPattern, FreePatterns, (ClientData)PATTERN_REGEXP,
};
static Blt_SwitchCustom globSwitch =
{
    StringToPattern, FreePatterns, (ClientData)PATTERN_GLOB,
};
static Blt_SwitchCustom exactSwitch =
{
    StringToPattern, FreePatterns, (ClientData)PATTERN_EXACT,
};


static Blt_SwitchSpec findSwitches[] = 
{
    {BLT_SWITCH_STRING, "-addtag", Blt_Offset(FindData, addTag), 0},
    {BLT_SWITCH_INT_NONNEGATIVE, "-count", Blt_Offset(FindData, maxMatches), 0},
    {BLT_SWITCH_INT_NONNEGATIVE, "-depth", Blt_Offset(FindData, maxDepth), 0},
    {BLT_SWITCH_CUSTOM, "-exact", Blt_Offset(FindData, patternList), 0,
        &exactSwitch},
    {BLT_SWITCH_LIST, "-exec", Blt_Offset(FindData, command), 0},
    {BLT_SWITCH_CUSTOM, "-glob", Blt_Offset(FindData, patternList), 0, 
	&globSwitch},
    {BLT_SWITCH_FLAG, "-invert", Blt_Offset(FindData, flags), 0, 0, 
	MATCH_INVERT},
    {BLT_SWITCH_CUSTOM, "-key", Blt_Offset(FindData, keyList), 0, &exactSwitch},
    {BLT_SWITCH_CUSTOM, "-keyexact", Blt_Offset(FindData, keyList), 0, 
	&exactSwitch},
    {BLT_SWITCH_CUSTOM, "-keyglob", Blt_Offset(FindData, keyList), 0, 
	&globSwitch},
    {BLT_SWITCH_CUSTOM, "-keyregexp", Blt_Offset(FindData, keyList), 0, 
	&regexpSwitch},
    {BLT_SWITCH_FLAG, "-leafonly", Blt_Offset(FindData, flags), 0, 0, 
	MATCH_LEAFONLY},
    {BLT_SWITCH_FLAG, "-nocase", Blt_Offset(FindData, flags), 0, 0, 
	MATCH_NOCASE},
    {BLT_SWITCH_CUSTOM, "-order", Blt_Offset(FindData, order), 0, &orderSwitch},
    {BLT_SWITCH_FLAG, "-path", Blt_Offset(FindData, flags), 0, 0, 
	MATCH_PATHNAME},
    {BLT_SWITCH_CUSTOM, "-regexp", Blt_Offset(FindData, patternList), 0, 
	&regexpSwitch},
    {BLT_SWITCH_STRING, "-tag", Blt_Offset(FindData, withTag), 0},
    {BLT_SWITCH_END, NULL, 0, 0}
};

static Blt_SwitchParseProc StringToNode;
static Blt_SwitchCustom nodeSwitch =
{
    StringToNode, (Blt_SwitchFreeProc *)NULL, (ClientData)0,
};

typedef struct {
    TupleCmd *cmdPtr;		/* Tuple to move nodes. */
    Blt_TupleNode node;
    int movePos;
} MoveData;

static Blt_SwitchSpec moveSwitches[] = 
{
    {BLT_SWITCH_CUSTOM, "-after", Blt_Offset(MoveData, node), 0, &nodeSwitch},
    {BLT_SWITCH_INT_NONNEGATIVE, "-at", Blt_Offset(MoveData, movePos), 0},
    {BLT_SWITCH_CUSTOM, "-before", Blt_Offset(MoveData, node), 0, &nodeSwitch},
    {BLT_SWITCH_END, NULL, 0, 0}
};

typedef struct {
    Blt_TupleNode srcNode, destNode;
    Blt_Tuple srcTuple, destTuple;
    TupleCmd *srcPtr, *destPtr;
    unsigned int flags;
    char *label;
} CopyData;

#define COPY_RECURSE	(1<<0)
#define COPY_TAGS	(1<<1)
#define COPY_OVERWRITE	(1<<2)

static Blt_SwitchSpec copySwitches[] = 
{
    {BLT_SWITCH_STRING, "-label", Blt_Offset(CopyData, label), 0},
    {BLT_SWITCH_FLAG, "-recurse", Blt_Offset(CopyData, flags), 0, 0, 
	COPY_RECURSE},
    {BLT_SWITCH_FLAG, "-tags", Blt_Offset(CopyData, flags), 0, 0, 
	COPY_TAGS},
    {BLT_SWITCH_FLAG, "-overwrite", Blt_Offset(CopyData, flags), 0, 0, 
	COPY_OVERWRITE},
    {BLT_SWITCH_END, NULL, 0, 0}
};

typedef struct {
    TupleCmd *cmdPtr;		/* Tuple to examine. */
    Tcl_Obj **preObjv;		/* Command converted into an array of 
				 * Tcl_Obj's. */
    int preObjc;		/* Number of Tcl_Objs in above array. */

    Tcl_Obj **postObjv;		/* Command converted into an array of 
				 * Tcl_Obj's. */
    int postObjc;		/* Number of Tcl_Objs in above array. */

    unsigned int flags;		/* See flags definitions above. */

    int maxDepth;		/* If > 0, don't descend more than this
				 * many levels. */
    /* String options. */
    Blt_List patternList;	/* List of label or value patterns. */
    char **preCmd;		/* Pre-command split into a Tcl list. */
    char **postCmd;		/* Post-command split into a Tcl list. */

    Blt_List keyList;		/* List of key-name patterns. */
    char *withTag;
} ApplyData;

static Blt_SwitchSpec applySwitches[] = 
{
    {BLT_SWITCH_LIST, "-precommand", Blt_Offset(ApplyData, preCmd), 0},
    {BLT_SWITCH_LIST, "-postcommand", Blt_Offset(ApplyData, postCmd), 0},
    {BLT_SWITCH_INT_NONNEGATIVE, "-depth", Blt_Offset(ApplyData, maxDepth), 0},
    {BLT_SWITCH_CUSTOM, "-exact", Blt_Offset(ApplyData, patternList), 0,
	&exactSwitch},
    {BLT_SWITCH_CUSTOM, "-glob", Blt_Offset(ApplyData, patternList), 0, 
	&globSwitch},
    {BLT_SWITCH_FLAG, "-invert", Blt_Offset(ApplyData, flags), 0, 0, 
	MATCH_INVERT},
    {BLT_SWITCH_CUSTOM, "-key", Blt_Offset(ApplyData, keyList), 0, 
	&exactSwitch},
    {BLT_SWITCH_CUSTOM, "-keyexact", Blt_Offset(ApplyData, keyList), 0, 
	&exactSwitch},
    {BLT_SWITCH_CUSTOM, "-keyglob", Blt_Offset(ApplyData, keyList), 0, 
	&globSwitch},
    {BLT_SWITCH_CUSTOM, "-keyregexp", Blt_Offset(ApplyData, keyList), 0, 
	&regexpSwitch},
    {BLT_SWITCH_FLAG, "-leafonly", Blt_Offset(ApplyData, flags), 0, 0, 
	MATCH_LEAFONLY},
    {BLT_SWITCH_FLAG, "-nocase", Blt_Offset(ApplyData, flags), 0, 0, 
	MATCH_NOCASE},
    {BLT_SWITCH_FLAG, "-path", Blt_Offset(ApplyData, flags), 0, 0, 
	MATCH_PATHNAME},
    {BLT_SWITCH_CUSTOM, "-regexp", Blt_Offset(ApplyData, patternList), 0,
	&regexpSwitch},
    {BLT_SWITCH_STRING, "-tag", Blt_Offset(ApplyData, withTag), 0},
    {BLT_SWITCH_END, NULL, 0, 0}
};

typedef struct {
    unsigned int flags;
    Blt_HashTable idTable;
} RestoreData;

#define RESTORE_NO_TAGS		(1<<0)
#define RESTORE_OVERWRITE	(1<<1)

static Blt_SwitchSpec restoreSwitches[] = 
{
    {BLT_SWITCH_FLAG, "-notags", Blt_Offset(RestoreData, flags), 0, 0, 
	RESTORE_NO_TAGS},
    {BLT_SWITCH_FLAG, "-overwrite", Blt_Offset(RestoreData, flags), 0, 0, 
	RESTORE_OVERWRITE},
    {BLT_SWITCH_END, NULL, 0, 0}
};

static Blt_SwitchParseProc StringToFormat;
static Blt_SwitchCustom formatSwitch =
{
    StringToFormat, (Blt_SwitchFreeProc *)NULL, (ClientData)0,
};

typedef struct {
    int sort;			/* If non-zero, sort the tuples.  */
    int withParent;		/* If non-zero, add the parent node id 
				 * to the output of the command.*/
    int withId;			/* If non-zero, echo the node id in the
				 * output of the command. */
} PositionData;

#define POSITION_SORTED		(1<<0)

static Blt_SwitchSpec positionSwitches[] = 
{
    {BLT_SWITCH_FLAG, "-sort", Blt_Offset(PositionData, sort), 0, 0,
       POSITION_SORTED},
    {BLT_SWITCH_CUSTOM, "-format", 0, 0, &formatSwitch},
    {BLT_SWITCH_END, NULL, 0, 0}
};

static Blt_TupleApplyProc MatchTupleProc, SortTupleProc;
static Blt_TupleApplyProc ApplyTupleProc;
static Blt_TupleTraceProc TupleTraceProc;
static Blt_TupleCompareTuplesProc CompareTuples;

static Tcl_ObjCmdProc CompareDictionaryCmd;
static Tcl_ObjCmdProc ExitCmd;
#endif

static Blt_TupleNotifyEventProc TupleEventProc;
static Tcl_CmdDeleteProc TupleInstDeleteProc;
static Tcl_InterpDeleteProc TupleInterpDeleteProc;
static Tcl_ObjCmdProc TupleInstObjCmd;
static Tcl_ObjCmdProc TupleObjCmd;

static int GetTuple(TupleCmd *cmdPtr, Tcl_Obj *objPtr, Blt_Tuple *tuplePtr);


/*
 *----------------------------------------------------------------------
 *
 * NewTupleCmd --
 *
 *	This is a helper routine used by TupleCreateOp.  It create a
 *	new instance of a tuple command.  Memory is allocated for the
 *	command structure and a new Tcl command is created (same as
 *	the instance name).  All tuple commands have hash table
 *	entries in a global (interpreter-specific) registry.
 *	
 * Results:
 *	Returns a pointer to the newly allocated tuple command structure.
 *
 * Side Effects:
 *	Memory is allocated for the structure and a hash table entry is
 *	added.  
 *
 *---------------------------------------------------------------------- 
 */
static TupleCmd *
NewTupleCmd(
    Tcl_Interp *interp, 
    TupleCmdInterpData *interpDataPtr,
    Blt_TupleTable table, 
    CONST char *instName)
{
    int isNew;
    TupleCmd *cmdPtr;
    
    cmdPtr = Blt_Calloc(1, sizeof(TupleCmd));
    assert(cmdPtr);
    cmdPtr->table = table;
    cmdPtr->interp = interp;

    Blt_InitHashTable(&(cmdPtr->traceTable), BLT_STRING_KEYS);
    Blt_InitHashTable(&(cmdPtr->notifyTable), BLT_STRING_KEYS);
    cmdPtr->cmdToken = Tcl_CreateObjCommand(interp, (char *)instName, 
	(Tcl_ObjCmdProc *)TupleInstObjCmd, cmdPtr, TupleInstDeleteProc);
    cmdPtr->instTablePtr = &interpDataPtr->instTable;
    cmdPtr->hashPtr = Blt_CreateHashEntry(cmdPtr->instTablePtr, (char *)cmdPtr,
	&isNew);
    cmdPtr->emptyObjPtr = Tcl_NewStringObj("NA", -1);
    Tcl_IncrRefCount(cmdPtr->emptyObjPtr);
    Blt_SetHashValue(cmdPtr->hashPtr, cmdPtr);
    return cmdPtr;
}


/*
 *----------------------------------------------------------------------
 *
 * GetTupleCmdInterpData --
 *
 *---------------------------------------------------------------------- 
 */
static TupleCmdInterpData *
GetTupleCmdInterpData(Tcl_Interp *interp)
{
    TupleCmdInterpData *dataPtr;
    Tcl_InterpDeleteProc *proc;

    dataPtr = (TupleCmdInterpData *)
	Tcl_GetAssocData(interp, TUPLE_THREAD_KEY, &proc);
    if (dataPtr == NULL) {
	dataPtr = Blt_Malloc(sizeof(TupleCmdInterpData));
	assert(dataPtr);
	dataPtr->interp = interp;
	Tcl_SetAssocData(interp, TUPLE_THREAD_KEY, TupleInterpDeleteProc,
		 dataPtr);
	Blt_InitHashTable(&dataPtr->instTable, BLT_ONE_WORD_KEYS);
    }
    return dataPtr;
}


/*
 * ----------------------------------------------------------------------
 *
 * GenerateName --
 *
 *	Generates an unique tuple command name.  Tuple names are in
 *	the form "tupleN", where N is a non-negative integer. Check 
 *	each name generated to see if it is already a tuple. We want
 *	to recycle names if possible.
 *	
 * Results:
 *	Returns the unique name.  The string itself is stored in the
 *	dynamic string passed into the routine.
 *
 * ----------------------------------------------------------------------
 */
static CONST char *
GenerateName(
    Tcl_Interp *interp,
    CONST char *prefix, 
    CONST char *suffix,
    Tcl_DString *resultPtr)
{

    int n;
    Tcl_Namespace *nsPtr;
    char string[200];
    Tcl_CmdInfo cmdInfo;
    Tcl_DString dString;
    CONST char *instName, *name;

    /* 
     * Parse the command and put back so that it's in a consistent
     * format.  
     *
     *	t1         <current namespace>::t1
     *	n1::t1     <current namespace>::n1::t1
     *	::t1	   ::t1
     *  ::n1::t1   ::n1::t1
     */
    instName = NULL;		/* Suppress compiler warning. */
    for (n = 0; n < INT_MAX; n++) {
	Tcl_DStringInit(&dString);
	Tcl_DStringAppend(&dString, prefix, -1);
	sprintf(string, "tuple%d", n);
	Tcl_DStringAppend(&dString, string, -1);
	Tcl_DStringAppend(&dString, suffix, -1);
	instName = Tcl_DStringValue(&dString);
	if (Blt_ParseQualifiedName(interp, instName, &nsPtr, &name) 
	    != TCL_OK) {
	    Tcl_AppendResult(interp, "can't find namespace in \"", instName, 
		"\"", (char *)NULL);
	    return NULL;
	}
	if (nsPtr == NULL) {
	    nsPtr = Tcl_GetCurrentNamespace(interp);
	}
	instName = Blt_GetQualifiedName(nsPtr, name, resultPtr);
	/* 
	 * Check if the command already exists. 
	 */
	if (Tcl_GetCommandInfo(interp, (char *)instName, &cmdInfo)) {
	    continue;
	}
	if (!Blt_TupleTableExists(interp, instName)) {
	    /* 
	     * We want the name of the tuple command and the underlying
	     * tuple object to be the same. Check that the free command
	     * name isn't an already a tuple object name.  
	     */
	    break;
	}
    }
    return instName;
}


/*
 *----------------------------------------------------------------------
 *
 * GetTupleCmd --
 *
 *	Find the tuple command associated with the Tcl command "string".
 *	
 *	We have to perform multiple lookups to get this right.  
 *
 *	The first step is to generate a canonical command name.  If an
 *	unqualified command name (i.e. no namespace qualifier) is
 *	given, we should search first the current namespace and then
 *	the global one.  Most Tcl commands (like Tcl_GetCmdInfo) look
 *	only at the global namespace.
 *
 *	Next check if the string is 
 *		a) a Tcl command and 
 *		b) really is a command for a tuple object.  
 *	Tcl_GetCommandInfo will get us the objClientData field that 
 *	should be a cmdPtr.  We verify that by searching our hashtable 
 *	of cmdPtr addresses.
 *
 * Results:
 *	A pointer to the tuple command.  If no associated tuple command
 *	can be found, NULL is returned.  It's up to the calling routines
 *	to generate an error message.
 *
 *---------------------------------------------------------------------- 
 */
static TupleCmd *
GetTupleCmd(
    TupleCmdInterpData *interpDataPtr, 
    Tcl_Interp *interp, 
    CONST char *string)
{
    CONST char *name;
    Tcl_Namespace *nsPtr;
    Tcl_CmdInfo cmdInfo;
    Blt_HashEntry *hPtr;
    Tcl_DString dString;
    char *qualName;
    int result;

    /* Put apart the tuple name and put is back together in a standard
     * format. */
    if (Blt_ParseQualifiedName(interp, string, &nsPtr, &name) != TCL_OK) {
	return NULL;		/* No such parent namespace. */
    }
    if (nsPtr == NULL) {
	nsPtr = Tcl_GetCurrentNamespace(interp);
    }
    /* Rebuild the fully qualified name. */
    qualName = Blt_GetQualifiedName(nsPtr, name, &dString);
    result = Tcl_GetCommandInfo(interp, qualName, &cmdInfo);
    Tcl_DStringFree(&dString);

    if (!result) {
	return NULL;
    }
    hPtr = Blt_FindHashEntry(&interpDataPtr->instTable, 
		(char *)(cmdInfo.objClientData));
    if (hPtr == NULL) {
	return NULL;
    }
    return Blt_GetHashValue(hPtr);
}


/*
 *----------------------------------------------------------------------
 *
 * GetTraceFlags --
 *
 *	Parses a string representation of the trace bit flags and
 *	returns the mask.
 *
 * Results:
 *	The trace mask is returned.
 *
 *---------------------------------------------------------------------- 
 */
static int
GetTraceFlags(char *string)
{
    register char *p;
    unsigned int flags;

    flags = 0;
    for (p = string; *p != '\0'; p++) {
	switch (toupper(*p)) {
	case 'R':
	    flags |= TUPLE_TRACE_READ;
	    break;
	case 'W':
	    flags |= TUPLE_TRACE_WRITE;
	    break;
	case 'U':
	    flags |= TUPLE_TRACE_UNSET;
	    break;
	case 'C':
	    flags |= TUPLE_TRACE_CREATE;
	    break;
	default:
	    return -1;
	}
    }
    return flags;
}

/*
 *----------------------------------------------------------------------
 *
 * PrintTraceFlags --
 *
 *	Generates a string representation of the trace bit flags.
 *	It's assumed that the provided string is at least 5 bytes.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	The bitflag information is written to the provided string.
 *
 *---------------------------------------------------------------------- 
 */
static void
PrintTraceFlags(unsigned int flags, char *string)
{
    register char *p;

    p = string;
    if (flags & TUPLE_TRACE_READ) {
	*p++ = 'r';
    } 
    if (flags & TUPLE_TRACE_WRITE) {
	*p++ = 'w';
    } 
    if (flags & TUPLE_TRACE_UNSET) {
	*p++ = 'u';
    } 
    if (flags & TUPLE_TRACE_CREATE) {
	*p++ = 'c';
    } 
    *p = '\0';
}

/*
 *----------------------------------------------------------------------
 *
 * PrintTuple --
 *
 *	Generates a string representation of a tuple in the provided
 *	dynamic string (the dstring is assumed to be previously
 *	initialized).
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	The tuple information is appended to the dynamic string.
 *
 *---------------------------------------------------------------------- 
 */
static void
PrintTuple(
    TupleCmd *cmdPtr, 
    Blt_Tuple tuple, 
    Tcl_DString *resultPtr)
{
    /* Tuple index */
    Tcl_DStringAppendElement(resultPtr, Blt_Itoa(Blt_TupleRowIndex(tuple)));

    /* Key-value pairs */
    {
	int column, nColumns;
	CONST char *key;
	Tcl_Obj *valueObjPtr;
	
	Tcl_DStringStartSublist(resultPtr);
	nColumns = Blt_TupleTableWidth(cmdPtr->table);
	for (column = 0; column < nColumns; column++) {
	    key = Blt_TupleGetColumnKey(cmdPtr->table, column);
	    if (Blt_TupleGetValue((Tcl_Interp *)NULL, cmdPtr->table, tuple, 
			key, &valueObjPtr) == TCL_OK) {
		Tcl_DStringAppendElement(resultPtr, key);
		if (valueObjPtr != NULL) {
		    Tcl_DStringAppendElement(resultPtr, 
			Tcl_GetString(valueObjPtr));
		} else {
		    Tcl_DStringAppendElement(resultPtr, "NA");
		}
	    }
	}	    
	Tcl_DStringEndSublist(resultPtr);
    }

    
    /* Tags */
    {				
	Blt_HashEntry *hPtr;
	Blt_HashSearch cursor;
	Blt_TupleTagEntry *tPtr;

	Tcl_DStringStartSublist(resultPtr);
	for (hPtr = Blt_TupleFirstTag(cmdPtr->table, &cursor); hPtr != NULL; 
	     hPtr = Blt_NextHashEntry(&cursor)) {
	    tPtr = Blt_GetHashValue(hPtr);
	    Tcl_DStringAppendElement(resultPtr, tPtr->tagName);
	}
	Tcl_DStringEndSublist(resultPtr);
    }
    Tcl_DStringAppend(resultPtr, "\n", -1);
}

/*
 *----------------------------------------------------------------------
 *
 * UnsetValues --
 *
 *	This is a helper routine used by UnsetOp.  It unsets one or
 *	more key value pairs for a given tuple.
 *
 * Results:
 *	A standard Tcl result.  If an error occurred when unsetting
 *	the new value TCL_ERROR is returned and an error message is
 *	left in the interpreter result.
 *
 *---------------------------------------------------------------------- 
 */
static int
UnsetValues(
    TupleCmd *cmdPtr, 
    Blt_Tuple tuple, 
    int objc, 
    Tcl_Obj *CONST *objv)
{
    register int i;

    for (i = 0; i < objc; i ++) {
	if (Blt_TupleUnsetValue(cmdPtr->interp, cmdPtr->table, tuple, 
		Tcl_GetString(objv[i])) != TCL_OK) {
	    return TCL_ERROR;
	}
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * DeleteNotifier --
 *
 *	This is a helper routine used to delete notifiers.  It
 *	releases the Tcl_Objs used in the notification callback
 *	command and the actual tuple notifier.  Memory for the
 *	notifier is also freed.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	Memory is deallocated and the notitifer is no longer 
 *	active.
 *
 *---------------------------------------------------------------------- 
 */
static void
DeleteNotifier(NotifyInfo *notifyPtr)
{
    int i;

    for (i = 0; i < notifyPtr->objc; i++) {
	Tcl_DecrRefCount(notifyPtr->objv[i]);
    }
    Blt_TupleDeleteNotifier(notifyPtr->notifier);
    Blt_Free(notifyPtr->objv);
    Blt_Free(notifyPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * DeleteTrace --
 *
 *	This is a helper routine used to delete traces.  It releases
 *	the Tcl_Objs used in the trace callback command and the actual
 *	tuple trace.  Memory for the trace is also freed.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	Memory is deallocated and the trace is no longer active.
 *
 *---------------------------------------------------------------------- 
 */
static void
DeleteTrace(TraceInfo *tracePtr)
{
    int i;

    for(i = 0; i < tracePtr->objc; i++) {
	Tcl_DecrRefCount(tracePtr->objv[i]);
    }
    Blt_TupleDeleteTrace(tracePtr->trace);
    Blt_Free(tracePtr->objv);
    Blt_Free(tracePtr);
}

/*
 *----------------------------------------------------------------------
 *
 * TupleTraceProc --
 *
 *---------------------------------------------------------------------- 
 */
/*ARGSUSED*/
static int
TupleTraceProc(
    ClientData clientData,
    Tcl_Interp *interp,
    unsigned int row,		/* Tuple that has just been updated. */
    unsigned int column,	/* Field that's updated. */
    unsigned int flags)
{
    TraceInfo *tracePtr = clientData; 
    char string[5];
    CONST char *key;
    int result;
    int i;

    i = tracePtr->objc;		/* Add extra command arguments
				 * starting at this index. */
    tracePtr->objv[i] = Tcl_NewIntObj(row);
    key = Blt_TupleGetColumnKey(tracePtr->cmdPtr->table, column);
    tracePtr->objv[i+1] = Tcl_NewStringObj(key, -1);
    PrintTraceFlags(flags, string);
    tracePtr->objv[i+2] = Tcl_NewStringObj(string, -1);
    Tcl_IncrRefCount(tracePtr->objv[i]);
    Tcl_IncrRefCount(tracePtr->objv[i+1]);
    Tcl_IncrRefCount(tracePtr->objv[i+2]);
    result = Tcl_EvalObjv(interp, tracePtr->objc + 3, tracePtr->objv, 0);
    Tcl_DecrRefCount(tracePtr->objv[i+2]);
    Tcl_DecrRefCount(tracePtr->objv[i+1]);
    Tcl_DecrRefCount(tracePtr->objv[i]);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * TupleEventProc --
 *
 *---------------------------------------------------------------------- 
 */
static int
TupleEventProc(ClientData clientData, Blt_TupleNotifyEvent *eventPtr)
{
    NotifyInfo *notifyPtr = clientData; 
    int result;
    int i;
    char *string;
    
    switch (eventPtr->type) {
    case TUPLE_NOTIFY_CREATE_ROW:
	string = "-createrow";
	break;
	
    case TUPLE_NOTIFY_CREATE_COLUMN:
	string = "-createcolumn";
	break;

    case TUPLE_NOTIFY_DELETE_ROW:
	string = "-deleterow";
	break;

    case TUPLE_NOTIFY_DELETE_COLUMN:
	string = "-deletecolumn";
	break;
	
    case TUPLE_NOTIFY_MOVE:
	string = "-move";
	break;
	
    case TUPLE_NOTIFY_SORT:
	string = "-sort";
	break;
	
    case TUPLE_NOTIFY_RELABEL:
	string = "-relabel";
	break;
	
    default:
	/* empty */
	string = "???";
	break;
    }	
    
    i = notifyPtr->objc;
    notifyPtr->objv[i] = Tcl_NewStringObj(string, -1);
    notifyPtr->objv[i+1] = Tcl_NewIntObj(eventPtr->row);
    Tcl_IncrRefCount(notifyPtr->objv[i]);
    Tcl_IncrRefCount(notifyPtr->objv[i+1]);
    result = Tcl_EvalObjv(notifyPtr->cmdPtr->interp, notifyPtr->objc + 3, 
			  notifyPtr->objv, 0);
    Tcl_DecrRefCount(notifyPtr->objv[i+1]);
    Tcl_DecrRefCount(notifyPtr->objv[i]);
    if (result != TCL_OK) {
	Tcl_BackgroundError(notifyPtr->cmdPtr->interp);
	return TCL_ERROR;
    }
    Tcl_ResetResult(notifyPtr->cmdPtr->interp);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * GetTuple --
 *
 *---------------------------------------------------------------------- 
 */
static int 
GetTuple(TupleCmd *cmdPtr, Tcl_Obj *objPtr, Blt_Tuple *tuplePtr)
{
    char c;
    Blt_Tuple tuple;
    char *string;

    string = Tcl_GetString(objPtr);
    c = string[0];

    if (isdigit(UCHAR(c))) {
	int row;

	if (Tcl_GetIntFromObj(cmdPtr->interp, objPtr, &row) != TCL_OK) {
	    return TCL_ERROR;
	}
	tuple = Blt_TupleGetTupleByIndex(cmdPtr->table, row);
	if (tuple != NULL) {
	    *tuplePtr = tuple;
	    return TCL_OK;
	}
    }  else if (cmdPtr != NULL) {
	if (strcmp(string, "all") == 0) {
	    if (Blt_TupleTableLength(cmdPtr->table) > 1) {
		Tcl_AppendResult(cmdPtr->interp, 
			"more than one node tagged as \"", string, "\"", 
			(char *)NULL);
		return TCL_ERROR;
	    }
	    tuple = Blt_TupleGetTupleByIndex(cmdPtr->table, 0);
	} else {
	    Blt_HashTable *tablePtr;
	    Blt_HashSearch cursor;
	    Blt_HashEntry *hPtr;
	    int result;

	    tuple = NULL;
	    result = TCL_ERROR;
	    tablePtr = Blt_TupleTagHashTable(cmdPtr->table, string);
	    if (tablePtr == NULL) {
		Tcl_AppendResult(cmdPtr->interp, "can't find tag or index \"", 
			string, "\" in ", Blt_TupleTableName(cmdPtr->table), 
			(char *)NULL);
	    } else if (tablePtr->numEntries > 1) {
		Tcl_AppendResult(cmdPtr->interp, 
			"more than one node tagged as \"", string, "\"", 
			(char *)NULL);
	    } else if (tablePtr->numEntries > 0) {
		hPtr = Blt_FirstHashEntry(tablePtr, &cursor);
		tuple = Blt_GetHashValue(hPtr);
		result = TCL_OK;
	    }
	    if (result == TCL_ERROR) {
		return TCL_ERROR;
	    }
	}
	if (tuple != NULL) {
	    *tuplePtr = tuple;
	    return TCL_OK;
	}
    }
    Tcl_AppendResult(cmdPtr->interp, "can't find tag or index \"", string, 
		"\" in ", Blt_TupleTableName(cmdPtr->table), (char *)NULL);
    return TCL_ERROR;
}

/*
 *----------------------------------------------------------------------
 *
 * AttachOp --
 *
 *---------------------------------------------------------------------- 
 */
static int
AttachOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    if (objc == 3) {
	CONST char *objName;
	CONST char *name;
	Blt_TupleTable table;
	Tcl_Namespace *nsPtr;
	Tcl_DString dString;
	int result;

	objName = Tcl_GetString(objv[2]);
	if (Blt_ParseQualifiedName(interp, objName, &nsPtr, &name) 
	    != TCL_OK) {
	    Tcl_AppendResult(interp, "can't find namespace in \"", objName, 
			     "\"", (char *)NULL);
	    return TCL_ERROR;
	}
	if (nsPtr == NULL) {
	    nsPtr = Tcl_GetCurrentNamespace(interp);
	}
	objName = Blt_GetQualifiedName(nsPtr, name, &dString);
	result = Blt_TupleGetTable(interp, objName, &table);
	Tcl_DStringFree(&dString);
	if (result != TCL_OK) {
	    return TCL_ERROR;
	}
	if (cmdPtr->table != NULL) {
	    Blt_HashEntry *hPtr;
	    Blt_HashSearch cursor;
	    TraceInfo *tracePtr;
	    NotifyInfo *notifyPtr;
	    
	    Blt_TupleReleaseTable(cmdPtr->table);
	    /* Dump the current set of tags, traces, and notifiers. */
	    for (hPtr = Blt_FirstHashEntry(&cmdPtr->traceTable, &cursor); 
		 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
		tracePtr = Blt_GetHashValue(hPtr);
		DeleteTrace(tracePtr);
	    }
	    Blt_DeleteHashTable(&cmdPtr->traceTable);
	    Blt_InitHashTable(&cmdPtr->traceTable, TCL_STRING_KEYS);
	    for (hPtr = Blt_FirstHashEntry(&cmdPtr->notifyTable, &cursor); 
		hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
		notifyPtr = Blt_GetHashValue(hPtr);
		DeleteNotifier(notifyPtr);
	    }
	    Blt_DeleteHashTable(&cmdPtr->notifyTable);
	    Blt_InitHashTable(&cmdPtr->notifyTable, TCL_STRING_KEYS);
	}
	cmdPtr->table = table;
    }
    Tcl_SetResult(interp, (char *)Blt_TupleTableName(cmdPtr->table), 
	TCL_VOLATILE);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ColumnOp --
 *
 *	Parses the given command line and calls one of several
 *	column-specific operations.
 *	
 * Results:
 *	Returns a standard Tcl result.  It is the result of 
 *	operation called.
 *
 *---------------------------------------------------------------------- 
 */
static Blt_OpSpec columnOps[] =
{
#ifdef notdef
    {"create", 1, (Blt_Op)ColumnCreateOp, 4, 0, "name ?switches?",},
    {"delete", 1, (Blt_Op)ColumnDeleteOp, 3, 0, "name...",},
    {"get", 1, (Blt_Op)ColumnGetOp, 4, 0, "name...",},
    {"info", 1, (Blt_Op)ColumnInfoOp, 4, 4, "name",},
    {"names", 1, (Blt_Op)ColumnNamesOp, 3, 3, "",},
    {"set", 1, (Blt_Op)ColumnSetOp, 4, 0, "name values ?name values?...",},
    {"type", 1, (Blt_Op)ColumnTypeOp, 4, 0, "?newType?",},
    {"unset", 1, (Blt_Op)ColumnUnsetOp, 4, 0, "name...",},
    {"type", 1, (Blt_Op)ColumnTypeOp, 4, 0, "?newType?",},
#endif
};

static int nColumnOps = sizeof(columnOps) / sizeof(Blt_OpSpec);

static int
ColumnOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    Blt_Op proc;
    int result;

    proc = Blt_GetOpFromObj(interp, nColumnOps, columnOps, BLT_OP_ARG2, objc, 
	objv, 0);
    if (proc == NULL) {
	return TCL_ERROR;
    }
    result = (*proc) (cmdPtr, interp, objc, objv);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * DeleteOp --
 *
 *		d0 delete tagOrIdx ?tagOrIdx...?
 *
 *	Deletes one or more tuples from the table.  Tuples may be
 *	specified by their index or a tag.
 *	
 *---------------------------------------------------------------------- 
 */
static int
DeleteOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    Blt_Tuple tuple;
    Blt_TupleTagSearch cursor;
    int i;

    Blt_TupleStartDeleteRows(cmdPtr->table);
    for (i = 2; i < objc; i++) {
	/* A tag may designate more than one row. */
	tuple = Blt_TupleFirstTagged(interp, cmdPtr->table, objv[i], &cursor);
	if (tuple == NULL) {
	    Blt_TupleEndDeleteRows(cmdPtr->table);
	    return TCL_ERROR;
	}
	for (/* empty */; tuple != NULL; tuple = Blt_TupleNextTagged(&cursor)) {
	    Blt_TupleDeleteTuple(cmdPtr->table, tuple);
	}
    }
    Blt_TupleEndDeleteRows(cmdPtr->table);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ExistsOp --
 *
 *---------------------------------------------------------------------- 
 */
static int
ExistsOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    Blt_Tuple tuple;
    int bool;
    
    bool = TRUE;
    if (GetTuple(cmdPtr, objv[2], &tuple) != TCL_OK) {
	bool = FALSE;
    } else if (objc == 4) { 
	Tcl_Obj *valueObjPtr;
	char *key;
	
	key = Tcl_GetString(objv[3]);
	if (Blt_TupleGetValue((Tcl_Interp *)NULL, cmdPtr->table, tuple, 
			     key, &valueObjPtr) != TCL_OK) {
	    bool = FALSE;
	}
	bool = (valueObjPtr != NULL);
    } 
    Tcl_SetObjResult(interp, Tcl_NewBooleanObj(bool));
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * GetOp --
 *
 *	Retrieves the value from a given tuple for a designated
 *	column.  It's an error if the column key or row tuple is
 *	invalid.  If no key argument is provided then, all key-value
 *	pairs in the tuple are returned.  If a value is empty (the
 *	Tcl_Obj value is NULL), then the tuple command's "empty"
 *	string representation will be used.
 *	
 *	A third argument may be provided as the default return value.
 *	If no value exists for the given key, then this value is
 *	returned.
 * 
 * Results:
 *	A standard Tcl result. If the tag or index is invalid, 
 *	TCL_ERROR is returned and an error message is left in the 
 *	interpreter result.
 *	
 *
 *---------------------------------------------------------------------- 
 */
static int
GetOp(
    TupleCmd *cmdPtr, 
    Tcl_Interp *interp, 
    int objc, 
    Tcl_Obj *CONST *objv)
{
    Blt_Tuple tuple;

    if (GetTuple(cmdPtr, objv[2], &tuple) != TCL_OK) {
	return TCL_ERROR;
    }
    if (objc == 3) {
	int row, column;
	Tcl_Obj *valueObjPtr, *listObjPtr;

	/* Return all the key-value pairs in the tuple. Ignore columns
	 * with empty values. */
	listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
	row = Blt_TupleRowIndex(tuple);
	for (column = 0; column < Blt_TupleTableWidth(cmdPtr->table); column++){
	    if (Blt_TupleGetValueByIndex((Tcl_Interp *)NULL, cmdPtr->table, 
			 row, column, &valueObjPtr) == TCL_OK) {
		if (valueObjPtr != NULL) {
		    Tcl_Obj *objPtr;
		    CONST char *key;

		    key = Blt_TupleGetColumnKey(cmdPtr->table, column);
		    objPtr = Tcl_NewStringObj(key, -1);
		    Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
		    Tcl_ListObjAppendElement(interp, listObjPtr, valueObjPtr);
		}
	    }
	}	    
	Tcl_SetObjResult(interp, listObjPtr);
	return TCL_OK;
    } else {
	Tcl_Obj *valueObjPtr;
	char *key;
	int row, column;

	row = Blt_TupleRowIndex(tuple);
	
	key = Tcl_GetString(objv[3]); 
	if (Blt_TupleGetColumnIndex(interp, cmdPtr->table, key, &column) 
	    != TCL_OK) {
	    return TCL_ERROR;
	}
	if (Blt_TupleGetValueByIndex((Tcl_Interp *)NULL, cmdPtr->table, row, 
		column, &valueObjPtr) != TCL_OK) {
	    return TCL_ERROR;
	}
	if (valueObjPtr == NULL) {
	    if (objc == 4) {
		Tcl_AppendResult(interp, "tuple "\", Tcl_GetString(objv[2]), 
			 "\" in table \"", Tcl_GetString(objv[0]), 
			 "\" is empty.", (char *)NULL);
		return TCL_ERROR;
	    }
	    valueObjPtr = objv[4];
	} 
	Tcl_SetObjResult(interp, valueObjPtr);
    }
    return TCL_OK;
}


/*
 *----------------------------------------------------------------------
 *
 * NotifyCreateOp --
 *
 *	Creates a notifier for this instance.  Notifiers represent
 *	a bitmask of events and a command prefix to be invoked 
 *	when a matching event occurs.  
 *
 *	The command prefix is parsed and saved in an array of
 *	Tcl_Objs. Extra slots are allocated for the 
 *
 * Results:
 *	A standard Tcl result.  The name of the new notifier is
 *	returned in the interpreter result.  Otherwise, if it failed
 *	to parse a switch, then TCL_ERROR is returned and an error
 *	message is left in the interpreter result.
 *
 * Example:
 *	tuple0 notify create ?flags? command arg
 *
 *---------------------------------------------------------------------- 
 */
static int
NotifyCreateOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    NotifyInfo *notifyPtr;
    NotifyData data;
    char *string;
    char idString[200];
    int isNew, nArgs;
    Blt_HashEntry *hPtr;
    int count;
    register int i;

    count = 0;
    for (i = 3; i < objc; i++) {
	string = Tcl_GetString(objv[i]);
	if (string[0] != '-') {
	    break;
	}
	count++;
    }
    data.mask = 0;
    /* Process switches  */
    if (Blt_ProcessObjSwitches(interp, notifySwitches, count, objv + 3, 
	     (char *)&data, 0) < 0) {
	return TCL_ERROR;
    }
    notifyPtr = Blt_Malloc(sizeof(NotifyInfo));

    nArgs = objc - i;

    /* Stash away the command in structure and pass that to the notifier. */
    notifyPtr->objv = Blt_Malloc((nArgs + 2) * sizeof(Tcl_Obj *));
    for (count = 0; i < objc; i++, count++) {
	Tcl_IncrRefCount(objv[i]);
	notifyPtr->objv[count] = objv[i];
    }
    notifyPtr->objc = nArgs + 2;
    notifyPtr->cmdPtr = cmdPtr;
    if (data.mask == 0) {
	data.mask = TUPLE_NOTIFY_ALL;
    }
    sprintf(idString, "notify%d", cmdPtr->notifyCounter++);
    hPtr = Blt_CreateHashEntry(&cmdPtr->notifyTable, idString, &isNew);
    Blt_SetHashValue(hPtr, notifyPtr);
    notifyPtr->notifier = Blt_TupleCreateNotifier(cmdPtr->table, 
	 data.mask, TupleEventProc, notifyPtr);
    Tcl_SetStringObj(Tcl_GetObjResult(interp), idString, -1);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * NotifyDeleteOp --
 *
 *	Deletes one or more notifiers.  
 *
 * Results:
 *	A standard Tcl result.  If a name given doesn't represent
 *	a notifier, then TCL_ERROR is returned and an error message
 *	is left in the interpreter result.
 *
 *---------------------------------------------------------------------- 
 */
static int
NotifyDeleteOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    NotifyInfo *notifyPtr;
    Blt_HashEntry *hPtr;

    register int i;
    char *string;

    for (i = 3; i < objc; i++) {
	string = Tcl_GetString(objv[i]);
	hPtr = Blt_FindHashEntry(&(cmdPtr->notifyTable), string);
	if (hPtr == NULL) {
	    Tcl_AppendResult(interp, "unknown notify name \"", string, "\"", 
			     (char *)NULL);
	    return TCL_ERROR;
	}
	notifyPtr = Blt_GetHashValue(hPtr);
	Blt_DeleteHashEntry(&cmdPtr->notifyTable, hPtr);
	DeleteNotifier(notifyPtr);
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * NotifyInfoOp --
 *
 *	Returns the details for a given notifier.  The name
 *	of the notifier is passed as an argument.  The details
 *	are both the event mask and command prefix.
 *
 * Results:
 *	A standard Tcl result.  If the name given doesn't represent
 *	a notifier, then TCL_ERROR is returned and an error message
 *	is left in the interpreter result.
 *
 *---------------------------------------------------------------------- 
 */
/*ARGSUSED*/
static int
NotifyInfoOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,			/* Not used. */
    Tcl_Obj *CONST *objv)
{
    NotifyInfo *notifyPtr;
    struct Blt_TupleNotifierStruct *nPtr;
    Blt_HashEntry *hPtr;
    Tcl_DString dString;
    char *string;
    int i;

    string = Tcl_GetString(objv[3]);
    hPtr = Blt_FindHashEntry(&(cmdPtr->notifyTable), string);
    if (hPtr == NULL) {
	Tcl_AppendResult(interp, "unknown notify name \"", string, "\"", 
			 (char *)NULL);
	return TCL_ERROR;
    }
    notifyPtr = Blt_GetHashValue(hPtr);

    Tcl_DStringInit(&dString);
    Tcl_DStringAppendElement(&dString, string);	/* Copy notify Id */
    Tcl_DStringStartSublist(&dString);
    nPtr = notifyPtr->notifier;
    if (nPtr->mask & TUPLE_NOTIFY_CREATE) {
	Tcl_DStringAppendElement(&dString, "-create");
    }
    if (nPtr->mask & TUPLE_NOTIFY_DELETE) {
	Tcl_DStringAppendElement(&dString, "-delete");
    }
    if (nPtr->mask & TUPLE_NOTIFY_MOVE) {
	Tcl_DStringAppendElement(&dString, "-move");
    }
    if (nPtr->mask & TUPLE_NOTIFY_SORT) {
	Tcl_DStringAppendElement(&dString, "-sort");
    }
    if (nPtr->mask & TUPLE_NOTIFY_RELABEL) {
	Tcl_DStringAppendElement(&dString, "-relabel");
    }
    if (nPtr->mask & TUPLE_NOTIFY_WHENIDLE) {
	Tcl_DStringAppendElement(&dString, "-whenidle");
    }
    Tcl_DStringEndSublist(&dString);
    Tcl_DStringStartSublist(&dString);
    for (i = 0; i < notifyPtr->objc; i++) {
	Tcl_DStringAppendElement(&dString, Tcl_GetString(notifyPtr->objv[i]));
    }
    Tcl_DStringEndSublist(&dString);
    Tcl_DStringResult(interp, &dString);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * NotifyNamesOp --
 *
 *	Returns the names of all the notifiers in use by this
 *	instance.  Notifiers issues by other instances or object
 *	clients are not reported.  
 *
 * Results:
 *	Always TCL_OK.  A list of notifier names is left in the
 *	interpreter result.
 *
 *---------------------------------------------------------------------- 
 */
/*ARGSUSED*/
static int
NotifyNamesOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,			/* Not used. */
    Tcl_Obj *CONST *objv)
{
    Blt_HashEntry *hPtr;
    Blt_HashSearch cursor;
    Tcl_Obj *objPtr, *listObjPtr;
    char *idString;

    listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
    for (hPtr = Blt_FirstHashEntry(&(cmdPtr->notifyTable), &cursor);
	 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
	idString = Blt_GetHashKey(&(cmdPtr->notifyTable), hPtr);
	objPtr = Tcl_NewStringObj(idString, -1);
	Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
    }
    Tcl_SetObjResult(interp, listObjPtr);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * NotifyOp --
 *
 *	Parses the given command line and calls one of several
 *	notifier-specific operations.
 *	
 * Results:
 *	Returns a standard Tcl result.  It is the result of 
 *	operation called.
 *
 *---------------------------------------------------------------------- 
 */
static Blt_OpSpec notifyOps[] =
{
    {"create", 1, (Blt_Op)NotifyCreateOp, 4, 0, "?flags? command",},
    {"delete", 1, (Blt_Op)NotifyDeleteOp, 3, 0, "notifyId...",},
    {"info", 1, (Blt_Op)NotifyInfoOp, 4, 4, "notifyId",},
    {"names", 1, (Blt_Op)NotifyNamesOp, 3, 3, "",},
};

static int nNotifyOps = sizeof(notifyOps) / sizeof(Blt_OpSpec);

static int
NotifyOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    Blt_Op proc;
    int result;

    proc = Blt_GetOpFromObj(interp, nNotifyOps, notifyOps, BLT_OP_ARG2, objc, 
	objv, 0);
    if (proc == NULL) {
	return TCL_ERROR;
    }
    result = (*proc) (cmdPtr, interp, objc, objv);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * ColumnOp --
 *
 *	Parses the given command line and calls one of several
 *	column-specific operations.
 *	
 * Results:
 *	Returns a standard Tcl result.  It is the result of 
 *	operation called.
 *
 *---------------------------------------------------------------------- 
 */
static Blt_OpSpec columnOps[] =
{
#ifdef notdef
    {"create", 1, (Blt_Op)ColumnCreateOp, 4, 0, "name ?switches?",},
    {"cut",    1, (Blt_Op)ColumnDeleteOp, 3, 0, "name...",},
    {"dup",    1, (Blt_Op)ColumnGetOp,    4, 0, "name...",},
    {"pack",   1, (Blt_Op)ColumnNamesOp,  3, 3, "",},
    {"paste",  1, (Blt_Op)ColumnInfoOp,   4, 4, "name",},
    {"range",  1, (Blt_Op)ColumnSetOp,    4, 0, "name values ?name values?..",},
    {"type",   1, (Blt_Op)ColumnTypeOp,   4, 0, "?newType?",},
    {"unset",  1, (Blt_Op)ColumnUnsetOp,  4, 0, "name...",},
    {"type",   1, (Blt_Op)ColumnTypeOp,   4, 0, "?newType?",},
#endif
};

static int nColumnOps = sizeof(columnOps) / sizeof(Blt_OpSpec);

static int
SelectOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    Blt_Op proc;
    int result;

    proc = Blt_GetOpFromObj(interp, nColumnOps, columnOps, BLT_OP_ARG2, objc, 
	objv, 0);
    if (proc == NULL) {
	return TCL_ERROR;
    }
    result = (*proc) (cmdPtr, interp, objc, objv);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * SetOp --
 *
 *	Sets one or more key-value pairs for tuples.  One or more
 *	tuples may be set.  If any of the columns (keys) given don't
 *	already exist, the columns will be automatically created.  The
 *	same holds true for rows.  If a row index is beyond the end of
 *	the table (tags are always in range), new rows are allocated.
 * 
 * Results:
 *	A standard Tcl result. If the tag or index is invalid, 
 *	TCL_ERROR is returned and an error message is left in the 
 *	interpreter result.
 *	
 *---------------------------------------------------------------------- 
 */
static int
SetOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    Blt_Tuple tuple;
    register int i;
    int isNew;
    char *string;

    string = Tcl_GetString(objv[2]);
    if (isdigit(UCHAR(*string))) {
	int row, extra;
	CONST char *key;
	int column;

	if (Tcl_GetIntFromObj(interp, objv[2], &row) != TCL_OK) {
	    return TCL_ERROR;
	}
	/* Extend the array of tuples if necessary */
	extra = row - Blt_TupleTableLength(cmdPtr->table);
	if ((extra > 0) && 
	    (Blt_TupleExtendRows(cmdPtr->table, extra) != TCL_OK)) {
	    return TCL_ERROR;
	}
	/* Add the values, creating new keys if necessary. */
	for (i = 3; i < objc; i++) {
	    key = Tcl_GetString(objv[i]);
	    column = Blt_TupleAddColumn(cmdPtr->table, key, &isNew);
	    i++;
	    if (i == objc) {
		Tcl_AppendResult(cmdPtr->interp, "missing value for field \"", 
			key, "\"", (char *)NULL);
		return TCL_ERROR;
	    }
	    if (Blt_TupleSetValue(interp, cmdPtr->table, tuple, key, objv[i]) 
		!= TCL_OK) {
		return TCL_ERROR;
	    }
	}
	return TCL_OK;
    } else {
	Blt_TupleTagSearch cursor;
	KeyValue *values;
	CONST char *key;
	int n;

	tuple = Blt_TupleFirstTagged(interp, cmdPtr->table, objv[2], &cursor);
	if (tuple == NULL) {
	    return TCL_ERROR;
	}
	values = Blt_Malloc(sizeof(KeyValue) * objc - 3);
	/* Create new keys if necessary. */
	n = 0;
	for (i = 3; i < objc; i++) {
	    key = Tcl_GetString(objv[i]);
	    values[n].column = Blt_TupleAddColumn(cmdPtr->table, key, &isNew);
	    i++;
	    if (i == objc) {
		Tcl_AppendResult(cmdPtr->interp, "missing value for field \"", 
			key, "\"", (char *)NULL);
		goto error;
	    }
	    values[n].objPtr = objv[i];
	    n++;
	}
	for (/* empty */; tuple != NULL; tuple = Blt_TupleNextTagged(&cursor)) {
	    for (i = 0; i < n; i++) {
		if (Blt_TupleSetValueByIndex(cmdPtr->interp, cmdPtr->table, 
			Blt_TupleRowIndex(tuple), values[i].column, 
			values[i].objPtr) != TCL_OK) {
		    goto error;
		}
	    }
	}
	Blt_Free(values);
	return TCL_OK;
    error:
	Blt_Free(values);
	return TCL_ERROR;
    }
}

/*
 *----------------------------------------------------------------------
 *
 * TagAddOp --
 *
 *	Adds a tag to one or more tuples. A tuple may already have the
 *	tag. Tag names can not start with a digit.  It's also an error
 *	to try to add the reserved tag "all".
 *  
 * Results:
 *	A standard Tcl result.  If a tag isn't valid then TCL_ERROR is
 *	returned and an error message is left in the interpreter
 *	result.
 *
 *---------------------------------------------------------------------- 
 */
static int
TagAddOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    Blt_Tuple tuple;
    register int i;
    char *tagName;
    Blt_TupleTagSearch cursor;

    tagName = Tcl_GetString(objv[3]);
    if (isdigit(UCHAR(tagName[0]))) {
	Tcl_AppendResult(interp, "bad tag \"", tagName, 
		 "\": can't start with a digit", (char *)NULL);
	return TCL_ERROR;
    }
    if (strcmp(tagName, "all") == 0) {
	Tcl_AppendResult(cmdPtr->interp, "can't add reserved tag \"", tagName, 
		"\"", (char *)NULL);
	return TCL_ERROR;
    }
    for (i = 4; i < objc; i++) {
	tuple = Blt_TupleFirstTagged(interp, cmdPtr->table, objv[i], &cursor);
	if (tuple == NULL) {
	    return TCL_ERROR;
	}
	for (/* empty */; tuple != NULL; tuple = Blt_TupleNextTagged(&cursor)) {
	    Blt_TupleAddTag(cmdPtr->table, tuple, tagName);
	}
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TagDeleteOp --
 *
 *	Deletes a tag from one or more tuples. It's an error to try to
 *	remove the tag "all".  It's not an error is the tuple doesn't
 *	currently have the tag.
 *  
 * Results:
 *	A standard Tcl result.  If a tag isn't valid then TCL_ERROR is
 *	returned and an error message is left in the interpreter
 *	result.
 *
 *---------------------------------------------------------------------- 
 */
static int
TagDeleteOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    char *tagName;
    Blt_Tuple tuple;
    int i;
    Blt_TupleTagSearch cursor;

    tagName = Tcl_GetString(objv[3]);
    if (strcmp(tagName, "all") == 0) {
	Tcl_AppendResult(interp, "can't delete reserved tag \"", tagName, "\"", 
			 (char *)NULL);
        return TCL_ERROR;
    }
      
    for (i = 4; i < objc; i++) {
	tuple = Blt_TupleFirstTagged(interp, cmdPtr->table, objv[i], &cursor);
	if (tuple == NULL) {
	    return TCL_ERROR;
	}
	for (/* empty */; tuple != NULL; tuple = Blt_TupleNextTagged(&cursor)) {
	    Blt_TupleRemoveTag(cmdPtr->table, tuple, tagName);
	}
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TagDumpOp --
 *
 *	Reports information about the tuples tagged with the given
 *	tags.  
 *  
 * Results:
 *	A standard Tcl result.  If a tag isn't valid then TCL_ERROR is
 *	returned and an error message is left in the interpreter
 *	result.
 *
 *---------------------------------------------------------------------- 
 */
static int
TagDumpOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    Tcl_DString dString;
    Blt_TupleTagSearch cursor;
    Blt_Tuple tuple;
    register int i;

    Tcl_DStringInit(&dString);
    for (i = 3; i < objc; i++) {
	tuple = Blt_TupleFirstTagged(interp, cmdPtr->table, objv[i], &cursor);
	if (tuple == NULL) {
	    Tcl_DStringFree(&dString);
	    return TCL_ERROR;
	}
	for (/* empty */; tuple != NULL; tuple = Blt_TupleNextTagged(&cursor)) {
	    PrintTuple(cmdPtr, tuple, &dString);
	}
    }
    Tcl_DStringResult(interp, &dString);
    Tcl_DStringFree(&dString);
    return TCL_OK;
}


/*
 *----------------------------------------------------------------------
 *
 * TagForgetOp --
 *
 *	Deletes one or more tags.  The tuples currently associated
 *	with the tag remain otherwise intact.  It's not an error if
 *	the tag doesn't currently exist.
 *  
 * Results:
 *	Always TCL_OK.
 *
 *---------------------------------------------------------------------- 
 */
/*ARGSUSED*/
static int
TagForgetOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,			/* Not used. */
    Tcl_Obj *CONST *objv)
{
    register int i;

    for (i = 3; i < objc; i++) {
	Blt_TupleForgetTag(cmdPtr->table, Tcl_GetString(objv[i]));
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TagNamesOp --
 *
 *	Returns a list of the unique tags for the given tuples.  If no
 *	tuples are given, then all tags are reported.
 *  
 * Results:
 *	A standard Tcl result.  If a tag or index of a tuple isn't
 *	valid then TCL_ERROR is returned and an error message is left
 *	in the interpreter result.
 *
 *---------------------------------------------------------------------- 
 */
static int
TagNamesOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    Blt_HashSearch cursor;
    Blt_TupleTagEntry *tPtr;
    Tcl_Obj *listObjPtr, *objPtr;

    listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
    objPtr = Tcl_NewStringObj("all", -1);
    Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
    if (objc == 3) {
	Blt_HashEntry *hPtr;

	for (hPtr = Blt_TupleFirstTag(cmdPtr->table, &cursor); hPtr != NULL; 
	     hPtr = Blt_NextHashEntry(&cursor)) {
	    tPtr = Blt_GetHashValue(hPtr);
	    objPtr = Tcl_NewStringObj(tPtr->tagName, -1);
	    Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
	}
    } else {
	register int i;
	Blt_HashEntry *hPtr;
	Blt_HashTable uniqTable;
	int isNew;
	Blt_Tuple tuple;

	Blt_InitHashTable(&uniqTable, BLT_STRING_KEYS);
	for (i = 3; i < objc; i++) {
	    if (GetTuple(cmdPtr, objv[i], &tuple) != TCL_OK) {
		goto error;
	    }
	    for (hPtr = Blt_TupleFirstTag(cmdPtr->table, &cursor); hPtr != NULL;
		 hPtr = Blt_NextHashEntry(&cursor)) {
		tPtr = Blt_GetHashValue(hPtr);
		if (Blt_TupleHasTag(cmdPtr->table, tuple, tPtr->tagName)) {
		    Blt_CreateHashEntry(&uniqTable, tPtr->tagName, &isNew);
		}
	    }
	}
	for (hPtr = Blt_FirstHashEntry(&uniqTable, &cursor); hPtr != NULL;
	     hPtr = Blt_NextHashEntry(&cursor)) {
	    objPtr = Tcl_NewStringObj(Blt_GetHashKey(&uniqTable, hPtr), -1);
	    Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
	}
	Blt_DeleteHashTable(&uniqTable);
    }
    Tcl_SetObjResult(interp, listObjPtr);
    return TCL_OK;
 error:
    Tcl_DecrRefCount(listObjPtr);
    return TCL_ERROR;
}


/*
 *----------------------------------------------------------------------
 *
 * TagRangeOp --
 *
 *	Adds a tag to a range of tuples. A tuple may already have the
 *	tag. Tag names can not start with a digit.  It's also an error
 *	to try to add the reserved tag "all".
 *  
 * Results:
 *	A standard Tcl result.  If a tag isn't valid then TCL_ERROR is
 *	returned and an error message is left in the interpreter
 *	result.
 *
 *---------------------------------------------------------------------- 
 */
static int
TagRangeOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    char *tagName;
    Blt_Tuple first, last;

    tagName = Tcl_GetString(objv[3]);
    if (isdigit(UCHAR(tagName[0]))) {
	Tcl_AppendResult(interp, "bad tag \"", tagName, 
		 "\": can't start with a digit", (char *)NULL);
	return TCL_ERROR;
    }
    if (strcmp(tagName, "all") == 0) {
	Tcl_AppendResult(cmdPtr->interp, "can't add reserved tag \"", tagName, 
		"\"", (char *)NULL);
	return TCL_ERROR;
    }
    if ((GetTuple(cmdPtr, objv[4], &first) != TCL_OK) ||
	(GetTuple(cmdPtr, objv[5], &last) != TCL_OK)) {
	return TCL_ERROR;
    }
    if (Blt_TupleRowIndex(first) <= Blt_TupleRowIndex(last)) {
	Blt_Tuple tuple;

	do {
	    Blt_TupleAddTag(cmdPtr->table, tuple, tagName);
	    tuple = Blt_TupleNextTuple(cmdPtr->table, tuple);
	} while(tuple != last);
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TagTuplesOp --
 *
 *	Returns a list of unique tuple indices associated with the
 *	tags given.
 *  
 * Results:
 *	A standard Tcl result.  If a tag isn't valid then TCL_ERROR is
 *	returned and an error message is left in the interpreter
 *	result.
 *
 *---------------------------------------------------------------------- 
 */
static int
TagTuplesOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    Blt_HashEntry *hPtr;
    Blt_HashSearch cursor;
    Blt_HashTable uniqTable;
    Blt_Tuple tuple;
    Tcl_Obj *listObjPtr;
    Tcl_Obj *objPtr;
    char *tagName;
    int isNew;
    register int i;
	
    Blt_InitHashTable(&uniqTable, BLT_ONE_WORD_KEYS);
    for (i = 3; i < objc; i++) {
	tagName = Tcl_GetString(objv[i]);
	if (strcmp(tagName, "all") == 0) {
	    break;
	} else {
	    Blt_HashTable *tablePtr;
	    
	    tablePtr = Blt_TupleTagHashTable(cmdPtr->table, tagName);
	    if (tablePtr != NULL) {
		for (hPtr = Blt_FirstHashEntry(tablePtr, &cursor); 
		     hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
		    tuple = Blt_GetHashValue(hPtr);
		    Blt_CreateHashEntry(&uniqTable, (char *)tuple, &isNew);
		}
		continue;
	    }
	}
	Tcl_AppendResult(interp, "can't find a tag \"", tagName, "\"",
			 (char *)NULL);
	goto error;
    }
    listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
    for (hPtr = Blt_FirstHashEntry(&uniqTable, &cursor); hPtr != NULL; 
	 hPtr = Blt_NextHashEntry(&cursor)) {
	tuple = (Blt_Tuple)Blt_GetHashKey(&uniqTable, hPtr);
	objPtr = Tcl_NewIntObj(Blt_TupleRowIndex(tuple));
	Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
    }
    Tcl_SetObjResult(interp, listObjPtr);
    Blt_DeleteHashTable(&uniqTable);
    return TCL_OK;

 error:
    Blt_DeleteHashTable(&uniqTable);
    return TCL_ERROR;
}

/*
 *----------------------------------------------------------------------
 *
 * TagOp --
 *
 * 	This procedure is invoked to process tag operations.
 *
 * Results:
 *	A standard Tcl result.
 *
 * Side effects:
 *	See the user documentation.
 *
 *---------------------------------------------------------------------- 
 */
static Blt_OpSpec tagOps[] = {
    {"add", 1, (Blt_Op)TagAddOp, 5, 0, "tag tagOrIdx...",},
    {"delete", 2, (Blt_Op)TagDeleteOp, 5, 0, "tag tagOrIdx...",},
    {"dump", 2, (Blt_Op)TagDumpOp, 4, 0, "tag...",},
    {"forget", 1, (Blt_Op)TagForgetOp, 4, 0, "tag...",},
    {"names", 1, (Blt_Op)TagNamesOp, 3, 0, "?tagOrIdx...?",},
    {"range", 1, (Blt_Op)TagRangeOp, 6, 6, "tag first last",},
    {"tuples", 1, (Blt_Op)TagTuplesOp, 4, 0, "tag ?tag...?",},
};

static int nTagOps = sizeof(tagOps) / sizeof(Blt_OpSpec);

static int
TagOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    Blt_Op proc;
    int result;

    proc = Blt_GetOpFromObj(interp, nTagOps, tagOps, BLT_OP_ARG2, objc, objv, 
	0);
    if (proc == NULL) {
	return TCL_ERROR;
    }
    result = (*proc) (cmdPtr, interp, objc, objv);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * TraceCreateOp --
 *
 *	Creates a trace for this instance.  Traces represent
 *	list of keys, a bitmask of trace flags, and a command prefix 
 *	to be invoked when a matching trace event occurs.  
 *
 *	The command prefix is parsed and saved in an array of
 *	Tcl_Objs. The qualified name of the instance is saved
 *	also.
 *
 * Results:
 *	A standard Tcl result.  The name of the new trace is
 *	returned in the interpreter result.  Otherwise, if it failed
 *	to parse a switch, then TCL_ERROR is returned and an error
 *	message is left in the interpreter result.
 *
 *---------------------------------------------------------------------- 
 */
/*ARGSUSED*/
static int
TraceCreateOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,			/* Not used. */
    Tcl_Obj *CONST *objv)
{
    Blt_Tuple tuple;
    TraceInfo *tracePtr;
    char *keyList, *tagName, *string;
    char idString[200];
    int flags, isNew;
    struct Blt_TupleTraceStruct *tPtr;
    Tcl_Obj **args;
    int nArgs;
    int i;

    tagName = Tcl_GetString(objv[3]);
    if (isdigit(UCHAR(*tagName))) {
	if (GetTuple(cmdPtr, objv[3], &tuple) != TCL_OK) {
	    return TCL_ERROR;
	}
	tagName = NULL;
    } else {
	tagName = Blt_Strdup(tagName);
	tuple = NULL;
    }
    keyList = Tcl_GetString(objv[4]);
    string = Tcl_GetString(objv[5]);
    flags = GetTraceFlags(string);
    if (flags < 0) {
	Tcl_AppendResult(interp, "unknown flag in \"", string, "\"", 
		     (char *)NULL);
	return TCL_ERROR;
    }
    if (Tcl_ListObjGetElements(interp, objv[6], &nArgs, &args) != TCL_OK) {
	return TCL_ERROR;
    }
    tPtr = Blt_TupleCreateTrace(cmdPtr->table, tuple, keyList, tagName, 
	      flags, TupleTraceProc, tracePtr);
    if (tPtr == NULL) {
	return TCL_ERROR;
    }
    /* Stash away the command in structure and pass that to the trace. */
    tracePtr = Blt_Malloc(sizeof(TraceInfo));
    tracePtr->cmdPtr = cmdPtr;
    tracePtr->trace = tPtr;
    sprintf(idString, "trace%d", cmdPtr->traceCounter++);
    tracePtr->hashPtr = Blt_CreateHashEntry(&cmdPtr->traceTable, idString, 
	&isNew);
    Blt_SetHashValue(tracePtr->hashPtr, tracePtr);
    Tcl_SetStringObj(Tcl_GetObjResult(interp), idString, -1);

    tracePtr->objv = Blt_Calloc(nArgs + 1 + 3 + 1, sizeof(Tcl_Obj *));
    for(i = 0; i < nArgs; i++) {
	tracePtr->objv[i] = args[i];
	Tcl_IncrRefCount(tracePtr->objv[i]);
    }
    {
	Tcl_DString dString;
	char *qualName;

	Tcl_DStringInit(&dString);
	qualName = Blt_GetQualifiedName(
		Blt_GetCommandNamespace(interp, cmdPtr->cmdToken), 
		Tcl_GetCommandName(interp, cmdPtr->cmdToken), &dString);
	tracePtr->objv[i] = Tcl_NewStringObj(qualName, -1);
	Tcl_IncrRefCount(tracePtr->objv[i]);
	Tcl_DStringFree(&dString);
	tracePtr->objc = nArgs + 1;
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TraceDeleteOp --
 *
 *	Deletes one or more traces.  
 *
 * Results:
 *	A standard Tcl result.  If a name given doesn't represent
 *	a trace, then TCL_ERROR is returned and an error message
 *	is left in the interpreter result.
 *
 *---------------------------------------------------------------------- 
 */
static int
TraceDeleteOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    TraceInfo *tracePtr;
    Blt_HashEntry *hPtr;
    register int i;
    char *name;

    for (i = 3; i < objc; i++) {
	name = Tcl_GetString(objv[i]);
	hPtr = Blt_FindHashEntry(&cmdPtr->traceTable, name);
	if (hPtr == NULL) {
	    Tcl_AppendResult(interp, "unknown trace \"", name, "\"", 
			     (char *)NULL);
	    return TCL_ERROR;
	}
	tracePtr = Blt_GetHashValue(hPtr);
	Blt_DeleteHashEntry(&cmdPtr->traceTable, hPtr); 
	DeleteTrace(tracePtr);
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TraceInfoOp --
 *
 *	Returns the details for a given trace.  The name of the trace
 *	is passed as an argument.  The details are returned as a list
 *	of key-value pairs: trace name, tag, row index, keys, flags,
 *	and the command prefix.
 *
 * Results:
 *	A standard Tcl result.  If the name given doesn't represent
 *	a trace, then TCL_ERROR is returned and an error message
 *	is left in the interpreter result.
 *
 *---------------------------------------------------------------------- 
 */
/*ARGSUSED*/
static int
TraceInfoOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,			/* Not used. */
    Tcl_Obj *CONST *objv)
{
    TraceInfo *tracePtr;
    struct Blt_TupleTraceStruct *tPtr;
    Blt_HashEntry *hPtr;
    Tcl_DString dString;
    char string[5];
    char *withTag;
    char *idString;
    int i;
    CONST char **p;

    idString = Tcl_GetString(objv[3]);
    hPtr = Blt_FindHashEntry(&cmdPtr->traceTable, idString);
    if (hPtr == NULL) {
	Tcl_AppendResult(interp, "unknown trace \"", idString, "\"", 
		(char *)NULL);
	return TCL_ERROR;
    }
    Tcl_DStringInit(&dString);
    tPtr = tracePtr->trace;
    tracePtr = Blt_GetHashValue(hPtr);
    Tcl_DStringAppendElement(&dString, "id");
    Tcl_DStringAppendElement(&dString, idString);

    Tcl_DStringAppendElement(&dString, "tag");
    withTag = tPtr->withTag;
    if (withTag == NULL) {
	withTag = "";
    }
    Tcl_DStringAppendElement(&dString, withTag);
    Tcl_DStringAppendElement(&dString, "row");
    Tcl_DStringAppendElement(&dString, 
		Blt_Itoa(Blt_TupleRowIndex(tPtr->tuple)));

    Tcl_DStringAppendElement(&dString, "keys");
    Tcl_DStringStartSublist(&dString);
    for (p = tPtr->keys; *p != NULL; p++) {
	Tcl_DStringAppendElement(&dString, *p);
    }
    Tcl_DStringEndSublist(&dString);
    Tcl_DStringAppendElement(&dString, "flags");
    PrintTraceFlags(tPtr->mask, string);
    Tcl_DStringAppendElement(&dString, string);
    Tcl_DStringAppendElement(&dString, "command");
    Tcl_DStringStartSublist(&dString);
    for (i = 0; i < tracePtr->objc; i++) {
	Tcl_DStringAppendElement(&dString, Tcl_GetString(tracePtr->objv[i]));
    }
    Tcl_DStringEndSublist(&dString);
    Tcl_DStringResult(interp, &dString);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TraceNamesOp --
 *
 *	Returns the names of all the traces in use by this instance.
 *	Traces created by other instances or object clients are not
 *	reported.
 *
 * Results:
 *	Always TCL_OK.  A list of trace names is left in the
 *	interpreter result.
 *
 *---------------------------------------------------------------------- 
 */
/*ARGSUSED*/
static int
TraceNamesOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,			/* Not used. */
    Tcl_Obj *CONST *objv)
{
    Blt_HashEntry *hPtr;
    Blt_HashSearch cursor;

    for (hPtr = Blt_FirstHashEntry(&cmdPtr->traceTable, &cursor);
	 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
	Tcl_AppendElement(interp, Blt_GetHashKey(&cmdPtr->traceTable, hPtr));
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TraceOp --
 *
 * 	This procedure is invoked to process trace operations.
 *
 * Results:
 *	A standard Tcl result.
 *
 * Side effects:
 *	See the user documentation.
 *
 *---------------------------------------------------------------------- 
 */
static Blt_OpSpec traceOps[] =
{
    {"create", 1, (Blt_Op)TraceCreateOp, 7, 7, "tagOrIdx keyList how command",},
    {"delete", 1, (Blt_Op)TraceDeleteOp, 3, 0, "traceId...",},
    {"info", 1, (Blt_Op)TraceInfoOp, 4, 4, "traceId",},
    {"names", 1, (Blt_Op)TraceNamesOp, 3, 3, "",},
};

static int nTraceOps = sizeof(traceOps) / sizeof(Blt_OpSpec);

static int
TraceOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    Blt_Op proc;
    int result;

    proc = Blt_GetOpFromObj(interp, nTraceOps, traceOps, BLT_OP_ARG2, objc, 
	objv, 0);
    if (proc == NULL) {
	return TCL_ERROR;
    }
    result = (*proc) (cmdPtr, interp, objc, objv);
    return result;
}


/*
 *----------------------------------------------------------------------
 *
 * UnsetOp --
 *
 *	Unsets one or more values.  One or more tuples may be unset
 *	(using tags).  It's not an error if one of the key names
 *	(columns) doesn't exist.  The same holds true for rows.  If a
 *	row index is beyond the end of the table (tags are always in
 *	range), it is also ignored.
 * 
 * Results:
 *	A standard Tcl result. If the tag or index is invalid, 
 *	TCL_ERROR is returned and an error message is left in the 
 *	interpreter result.
 *	
 *---------------------------------------------------------------------- 
 */
static int
UnsetOp(
    TupleCmd *cmdPtr,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    Blt_Tuple tuple;
    char *string;
	
    string = Tcl_GetString(objv[2]);
    if (isdigit(UCHAR(*string))) {
	if (GetTuple(cmdPtr, objv[2], &tuple) != TCL_OK) {
	    return TCL_ERROR;
	}
	if (UnsetValues(cmdPtr, tuple, objc - 3, objv + 3) != TCL_OK) {
	    return TCL_ERROR;
	}
    } else {
	Blt_TupleTagSearch cursor;

	tuple = Blt_TupleFirstTagged(interp, cmdPtr->table, objv[2], &cursor);
	if (tuple == NULL) {
	    return TCL_ERROR;
	}
	for (/* empty */; tuple != NULL; tuple = Blt_TupleNextTagged(&cursor)) {
	    if (UnsetValues(cmdPtr, tuple, objc - 3, objv + 3) != TCL_OK) {
		return TCL_ERROR;
	    }
	}
    }
    return TCL_OK;
}

/*
 * --------------------------------------------------------------
 *
 * TupleInstObjCmd --
 *
 * 	This procedure is invoked to process commands on behalf of
 *	the instance of the tuple-object.
 *
 * Results:
 *	A standard Tcl result.
 *
 * Side effects:
 *	See the user documentation.
 *
 * --------------------------------------------------------------
 */
static Blt_OpSpec tupleOps[] =
{
    {"attach", 2, (Blt_Op)AttachOp, 2, 3, "?tupleCmd?",},
    {"delete", 2, (Blt_Op)DeleteOp, 3, 0, "tagOrIdx ?tagOrIdx...?",},
    {"exists", 1, (Blt_Op)ExistsOp, 3, 4, "tagOrIdx ?key?",},
    {"get", 1, (Blt_Op)GetOp, 3, 5, "tagOrIdx ?key? ?defaultValue?",},
    {"notify", 2, (Blt_Op)NotifyOp, 2, 0, "args...",},
    {"set", 3, (Blt_Op)SetOp, 3, 0, "tagOrIdx ?key value...?",},
    {"tag", 2, (Blt_Op)TagOp, 3, 0, "args...",},
    {"trace", 2, (Blt_Op)TraceOp, 2, 0, "args...",},
    {"unset", 3, (Blt_Op)UnsetOp, 3, 0, "tagOrIdx ?key...?",},
#ifdef notdef
    {"-ancestor", 2, (Blt_Op)AncestorOp, 4, 4, "node1 node2",},
    {"-apply", 1, (Blt_Op)ApplyOp, 3, 0, "first last ?switches?",},
    {"column", 2, (Blt_Op)ColumnOp, 2, 0, "args...", },
    {"copy", 2, (Blt_Op)CopyOp, 4, 0, 
	"srcNode ?destTuple? destNode ?switches?",},
    {"dump", 4, (Blt_Op)DumpOp, 3, 3, "first ?last?",},
    {"dumpfile", 5, (Blt_Op)DumpfileOp, 4, 4, "first ?last? fileName",},
    {"find", 4, (Blt_Op)FindOp, 3, 0, "node ?switches?",},
    {"index", 3, (Blt_Op)IndexOp, 3, 3, "name",},
    {"insert", 3, (Blt_Op)InsertOp, 3, 0, "parent ?switches?",},
    {"-is", 2, (Blt_Op)IsOp, 2, 0, "oper args...",},
    {"keys", 1, (Blt_Op)KeysOp, 3, 0, "node ?node...?",},
    {"-label", 3, (Blt_Op)LabelOp, 3, 4, "node ?newLabel?",},
    {"move", 1, (Blt_Op)MoveOp, 4, 0, "node newParent ?switches?",},
    {"-next", 4, (Blt_Op)NextOp, 3, 3, "node",},
    {"-position", 2, (Blt_Op)PositionOp, 3, 0, "?switches? node...",},
    {"-previous", 5, (Blt_Op)PreviousOp, 3, 3, "node",},
    {"range", 2, (Blt_Op)ChildrenOp, 4, 4, "first last",},
    {"restore", 7, (Blt_Op)RestoreOp, 4, 4, "node dataString",},
    {"restorefile", 8, (Blt_Op)RestorefileOp, 4, 4, "node fileName",},
    {"width", 2, (Blt_Op)SizeOp, 3, 3, "?newWidth?",},
    {"length", 2, (Blt_Op)SizeOp, 3, 3, "?newLength?",},
    {"sort", 2, (Blt_Op)SortOp, 3, 0, "?flags...?",},
    {"type", 2, (Blt_Op)TypeOp, 4, 4, "key",},
#endif
};

static int nTupleOps = sizeof(tupleOps) / sizeof(Blt_OpSpec);

static int
TupleInstObjCmd(
    ClientData clientData,	/* Pointer to the command structure. */
    Tcl_Interp *interp,		/* Interpreter to report errors back to. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *CONST *objv)	/* Vector of argument strings. */
{
    Blt_Op proc;
    TupleCmd *cmdPtr = clientData;
    int result;

    proc = Blt_GetOpFromObj(interp, nTupleOps, tupleOps, BLT_OP_ARG1, objc, 
	objv, BLT_OP_LINEAR_SEARCH);
    if (proc == NULL) {
	return TCL_ERROR;
    }
    Tcl_Preserve(cmdPtr);
    result = (*proc) (cmdPtr, interp, objc, objv);
    Tcl_Release(cmdPtr);
    return result;
}

/*
 * ----------------------------------------------------------------------
 *
 * TupleInstDeleteProc --
 *
 *	Deletes the command associated with the tuple.  This is
 *	called only when the command associated with the tuple is
 *	destroyed.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	The tuple object is released and bookkeeping data for traces
 *	and notifiers are freed.
 *
 * ----------------------------------------------------------------------
 */
static void
TupleInstDeleteProc(ClientData clientData)
{
    TupleCmd *cmdPtr = clientData;
    Blt_HashEntry *hPtr;
    Blt_HashSearch cursor;
    TraceInfo *tracePtr;
    NotifyInfo *notifyPtr;

    if (cmdPtr->hashPtr != NULL) {
	Blt_DeleteHashEntry(cmdPtr->instTablePtr, cmdPtr->hashPtr);
    }
    for (hPtr = Blt_FirstHashEntry(&cmdPtr->traceTable, &cursor); hPtr != NULL;
	 hPtr = Blt_NextHashEntry(&cursor)) {
	tracePtr = Blt_GetHashValue(hPtr);
	DeleteTrace(tracePtr);
    }
    Blt_DeleteHashTable(&cmdPtr->traceTable);
    for (hPtr = Blt_FirstHashEntry(&cmdPtr->notifyTable, &cursor); hPtr != NULL;
	 hPtr = Blt_NextHashEntry(&cursor)) {
	notifyPtr = Blt_GetHashValue(hPtr);
	DeleteNotifier(notifyPtr);
    }
    Tcl_DecrRefCount(cmdPtr->emptyObjPtr);
    Blt_DeleteHashTable(&cmdPtr->notifyTable);
    Blt_TupleReleaseTable(cmdPtr->table);
    Blt_Free(cmdPtr);
}
/*
 *----------------------------------------------------------------------
 *
 * TupleCreateOp --
 *
 *	Creates a new instance of a tuple object.  This routine
 *	ensures that instance and object names are the same.  For
 *	example, you can't create an instance with the name of an
 *	object that already exists.  And because each instance has a
 *	Tcl command associated with it that is used to access the
 *	object, we also check more generally that is it also not the
 *	name of an existing Tcl command.  
 *
 *	Instance names as namespace-qualified.  If no namespace is
 *	designated, it is assumed that instance is to be created in
 *	the current namespace (not the global namespace).
 *	
 * Results:
 *	A standard Tcl result.  If the instance is successfully created,
 *	the namespace-qualified name of the instance is returned. If not,
 *	then TCL_ERROR is returned and an error message is left in the 
 *	interpreter result.
 *
 *---------------------------------------------------------------------- 
 */
/*ARGSUSED*/
static int
TupleCreateOp(
    ClientData clientData,	/* Interpreter-specific data. */
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    TupleCmdInterpData *interpDataPtr = clientData;
    CONST char *instName;
    Tcl_DString dString;
    Blt_TupleTable table;

    instName = NULL;
    if (objc == 3) {
	instName = Tcl_GetString(objv[2]);
    }
    Tcl_DStringInit(&dString);
    if (instName == NULL) {
	instName = GenerateName(interp, "", "", &dString);
    } else {
	char *p;

	p = strstr(instName, "#auto");
	if (p != NULL) {
	    *p = '\0';
	    instName = GenerateName(interp, instName, p + 5, &dString);
	    *p = '#';
	} else {
	    CONST char *name;
	    Tcl_CmdInfo cmdInfo;
	    Tcl_Namespace *nsPtr;

	    nsPtr = NULL;
	    /* 
	     * Parse the command and put back so that it's in a consistent
	     * format.  
	     *
	     *	t1         <current namespace>::t1
	     *	n1::t1     <current namespace>::n1::t1
	     *	::t1	   ::t1
	     *  ::n1::t1   ::n1::t1
	     */
	    if (Blt_ParseQualifiedName(interp, instName, &nsPtr, &name) 
		!= TCL_OK) {
		Tcl_AppendResult(interp, "can't find namespace in \"", instName,
			 "\"", (char *)NULL);
		return TCL_ERROR;
	    }
	    if (nsPtr == NULL) {
		nsPtr = Tcl_GetCurrentNamespace(interp);
	    }
	    instName = Blt_GetQualifiedName(nsPtr, name, &dString);
	    /* 
	     * Check if the command already exists. 
	     */
	    if (Tcl_GetCommandInfo(interp, (char *)instName, &cmdInfo)) {
		Tcl_AppendResult(interp, "a command \"", instName,
				 "\" already exists", (char *)NULL);
		goto error;
	    }
	    if (Blt_TupleTableExists(interp, instName)) {
		Tcl_AppendResult(interp, "a tuple \"", instName, 
			"\" already exists", (char *)NULL);
		goto error;
	    }
	} 
    } 
    if (instName == NULL) {
	goto error;
    }
    if (Blt_TupleCreateTable(interp, instName, &table) == TCL_OK) {
	TupleCmd *cmdPtr;

	cmdPtr = NewTupleCmd(interp, interpDataPtr, table, instName);
	Tcl_SetResult(interp, (char *)instName, TCL_VOLATILE);
	Tcl_DStringFree(&dString);
	return TCL_OK;
    }
 error:
    Tcl_DStringFree(&dString);
    return TCL_ERROR;
}

/*
 *----------------------------------------------------------------------
 *
 * TupleDestroyOp --
 *
 *	Deletes one or more instances of tuple objects.  The deletion
 *	process is done by deleting the Tcl command associated with
 *	the object.  
 *	
 * Results:
 *	A standard Tcl result.  If one of the names given doesn't
 *	represent an instance, TCL_ERROR is returned and an error
 *	message is left in the interpreter result.
 *
 *---------------------------------------------------------------------- 
 */
static int
TupleDestroyOp(
    ClientData clientData,	/* Interpreter-specific data. */
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    TupleCmdInterpData *interpDataPtr = clientData;
    TupleCmd *cmdPtr;
    char *string;
    register int i;

    for (i = 2; i < objc; i++) {
	string = Tcl_GetString(objv[i]);
	cmdPtr = GetTupleCmd(interpDataPtr, interp, string);
	if (cmdPtr == NULL) {
	    Tcl_AppendResult(interp, "can't find a tuple named \"", string,
			     "\"", (char *)NULL);
	    return TCL_ERROR;
	}
	Tcl_DeleteCommandFromToken(interp, cmdPtr->cmdToken);
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TupleNamesOp --
 *
 *	Returns the names of all the tuple-object instances matching a
 *	given pattern.  If no pattern argument is provided, then all
 *	object names are returned.  The names returned are namespace
 *	qualified.
 *	
 * Results:
 *	Always returns TCL_OK.  The names of the matching objects is
 *	returned via the interpreter result.
 *
 *---------------------------------------------------------------------- 
 */
/*ARGSUSED*/
static int
TupleNamesOp(
    ClientData clientData,	/* Interpreter-specific data. */
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    TupleCmdInterpData *interpDataPtr = clientData;
    TupleCmd *cmdPtr;
    Blt_HashEntry *hPtr;
    Blt_HashSearch cursor;
    Tcl_Obj *objPtr, *listObjPtr;
    Tcl_DString dString;
    char *qualName;

    Tcl_DStringInit(&dString);
    listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
    for (hPtr = Blt_FirstHashEntry(&interpDataPtr->instTable, &cursor);
	 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
	cmdPtr = Blt_GetHashValue(hPtr);
	qualName = Blt_GetQualifiedName(
		Blt_GetCommandNamespace(interp, cmdPtr->cmdToken), 
		Tcl_GetCommandName(interp, cmdPtr->cmdToken), &dString);
	if (objc == 3) {
	    if (!Tcl_StringMatch(qualName, Tcl_GetString(objv[2]))) {
		continue;
	    }
	}
	objPtr = Tcl_NewStringObj(qualName, -1);
	Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
    }
    Tcl_SetObjResult(interp, listObjPtr);
    Tcl_DStringFree(&dString);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TupleObjCmd --
 *
 *---------------------------------------------------------------------- 
 */
static Blt_OpSpec tupleCmdOps[] =
{
    {"create", 1, (Blt_Op)TupleCreateOp, 2, 3, "?name?",},
    {"destroy", 1, (Blt_Op)TupleDestroyOp, 3, 0, "name...",},
    {"names", 1, (Blt_Op)TupleNamesOp, 2, 3, "?pattern?...",},
};

static int nCmdOps = sizeof(tupleCmdOps) / sizeof(Blt_OpSpec);

/*ARGSUSED*/
static int
TupleObjCmd(
    ClientData clientData,	/* Interpreter-specific data. */
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST *objv)
{
    Blt_Op proc;

    proc = Blt_GetOpFromObj(interp, nCmdOps, tupleCmdOps, BLT_OP_ARG1, objc, 
	objv, 0);
    if (proc == NULL) {
	return TCL_ERROR;
    }
    return (*proc) (clientData, interp, objc, objv);
}

/*
 * -----------------------------------------------------------------------
 *
 * TupleInterpDeleteProc --
 *
 *	This is called when the interpreter hosting the "tuple" command
 *	is deleted.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Removes the hash table managing all tuple names.
 *
 * ------------------------------------------------------------------------
 */
/* ARGSUSED */
static void
TupleInterpDeleteProc(
    ClientData clientData,	/* Interpreter-specific data. */
    Tcl_Interp *interp)
{
    TupleCmdInterpData *interpDataPtr = clientData;

    /* All tuple instances should already have been destroyed when
     * their respective Tcl commands were deleted. */
    Blt_DeleteHashTable(&interpDataPtr->instTable);
    Tcl_DeleteAssocData(interp, TUPLE_THREAD_KEY);
    Blt_Free(interpDataPtr);
}


/*
 * -----------------------------------------------------------------------
 *
 * Blt_TupleInit --
 *
 *	This procedure is invoked to initialize the "tuple" command.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Creates the new command and adds a new entry into a global Tcl
 *	associative array.
 *
 * ------------------------------------------------------------------------
 */
int
Blt_TupleInit(Tcl_Interp *interp)
{
    TupleCmdInterpData *interpDataPtr;	/* Interpreter-specific data. */
    static Blt_ObjCmdSpec cmdSpec = { 
	"tuple", TupleObjCmd, 
    };
    interpDataPtr = GetTupleCmdInterpData(interp);
    cmdSpec.clientData = interpDataPtr;
    if (Blt_InitObjCmd(interp, "blt", &cmdSpec) == NULL) {
	return TCL_ERROR;
    }
    return TCL_OK;
}

int
Blt_TupleCmdGetToken(
    Tcl_Interp *interp,
    CONST char *string,
    Blt_TupleTable  *tablePtr)
{
    TupleCmdInterpData *interpDataPtr;
    TupleCmd *cmdPtr;

    interpDataPtr = GetTupleCmdInterpData(interp);
    cmdPtr = GetTupleCmd(interpDataPtr, interp, string);
    if (cmdPtr == NULL) {
	Tcl_AppendResult(interp, "can't find a tuple object associated with \"",
		 string, "\"", (char *)NULL);
	return TCL_ERROR;
    }
    *tablePtr = cmdPtr->table;
    return TCL_OK;
}

/* Dump tuple to dbm */
/* Convert node data to datablock */

#endif /* NO_TUPLE */