File: otcl.c

package info (click to toggle)
otcl 1.14~RC2+dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 792 kB
  • ctags: 182
  • sloc: sh: 2,825; ansic: 1,949; tcl: 825; makefile: 114; perl: 49
file content (2342 lines) | stat: -rw-r--r-- 68,574 bytes parent folder | download
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
/* -*- Mode: c++ -*-
 *
 *  $Id: otcl.c,v 1.24 2009/12/30 19:56:17 tom_henderson Exp $
 *  
 *  Copyright 1993 Massachusetts Institute of Technology
 * 
 *  Permission to use, copy, modify, distribute, and sell this software and its
 *  documentation for any purpose is hereby granted without fee, provided that
 *  the above copyright notice appear in all copies and that both that
 *  copyright notice and this permission notice appear in supporting
 *  documentation, and that the name of M.I.T. not be used in advertising or
 *  publicity pertaining to distribution of the software without specific,
 *  written prior permission.  M.I.T. makes no representations about the
 *  suitability of this software for any purpose.  It is provided "as is"
 *  without express or implied warranty.
 * 
 */

#include <stdlib.h>
#include <string.h>
#include <tclInt.h>
#include <otcl.h> 


/*
 * compatibility definitions to bridge 7.x -> 7.5
 */

#if TCL_MAJOR_VERSION < 7
  #error Tcl distribution is TOO OLD
#elif TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION <= 3
  typedef char* Tcl_Command;
  static char* Tcl_GetCommandName(Tcl_Interp* in, Tcl_Command id) {
    return id;
  }

  static int Tcl_UpVar(Tcl_Interp* in, char* lvl, char* l, char* g, int flg){
    char* args[4];
    args[0] = "uplevel"; args[1] = "1";
    args[2]=l; args[3]=g;
    return Tcl_UpvarCmd(0, in, 4, args);
  }

  #define Tcl_CreateCommand(A,B,C,D,E)     \
    strcpy((char*)ckalloc(strlen(B)+1), B);\
    Tcl_CreateCommand(A,B,C,D,E)
#endif

#if TCL_MAJOR_VERSION <= 7
#define TclIsVarUndefined(varPtr) \
    ((varPtr)->flags == VAR_UNDEFINED)
#endif

#if TCL_MAJOR_VERSION < 8
#define ObjVarTablePtr(OBJ) (&(OBJ)->variables.varTable)
#define compat_Tcl_AddObjErrorInfo(a,b,c) Tcl_AddErrorInfo(a,b)
#else

#if TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION < 5
#define ObjVarTablePtr(OBJ) ((OBJ)->variables.varTablePtr)
#else
#define TCL_VERSION_8_5_OR_HIGHER
#define ObjVarTablePtr(OBJ) (&(OBJ)->variables.varTablePtr->table)
#endif

#define compat_Tcl_AddObjErrorInfo(a,b,c) Tcl_AddObjErrorInfo(a,b,c)
#endif

#ifdef TCL_VERSION_8_5_OR_HIGHER
#define Tcl_VarHashInitialize(varTablePtr)  varTablePtr = NULL;
#define Tcl_IsVarHashInitialized(varTablePtr)  varTablePtr == NULL
#define Tcl_VarHashGetKey(table, hPtr) TclGetString(hPtr->key.objPtr)
#define Tcl_VarHashGetValue(hPtr) ((Var *) ((char *)hPtr - TclOffset(VarInHash, entry)))
#define Tcl_CmdInfoGetProc(co) (Proc *)(co->isNativeObjectProc) ? co->objClientData : co->clientData;
#else
#define Tcl_VarHashInitialize(varTablePtr) \
	varTablePtr = (Tcl_HashTable *)ckalloc(sizeof(Tcl_HashTable)); \
	if (varTablePtr != NULL) Tcl_InitHashTable(varTablePtr, TCL_STRING_KEYS);
#define Tcl_IsVarHashInitialized(varTablePtr)  varTablePtr != NULL
#define Tcl_VarHashGetKey(table, hPtr) Tcl_GetHashKey(table, hPtr)
#define Tcl_VarHashGetValue(hPtr) (Var*)Tcl_GetHashValue(hp);
#define Tcl_CmdInfoGetProc(co) (Proc*)co->clientData;
#endif

/*
 * object and class internals
 */


typedef struct OTclObject {
  Tcl_Command id;
  Tcl_Interp* teardown;
  struct OTclClass* cl;
  struct OTclClass* type;
  Tcl_HashTable* procs;
  CallFrame variables;
} OTclObject;


typedef struct OTclClass {
  struct OTclObject object;
  struct OTclClasses* super;
  struct OTclClasses* sub;
  int color;
  struct OTclClasses* order;
  struct OTclClass* parent;
  Tcl_HashTable instprocs;
  Tcl_HashTable instances;
  Tcl_HashTable* objectdata;
} OTclClass;


typedef struct OTclClasses {
  struct OTclClass* cl;
  struct OTclClasses* next;
} OTclClasses;


/*
 * definitions of the main otcl objects
 */
static Tcl_HashTable* theObjects = 0;
static Tcl_HashTable* theClasses = 0;
static Tcl_CmdProc* ProcInterpId = 0;

/*
 * error return functions
 */


static int
OTclErrMsg(Tcl_Interp *in, char* msg, Tcl_FreeProc* type) {
  Tcl_SetResult(in, msg, type);
  return TCL_ERROR;
}

static int
OTclErrArgCnt(Tcl_Interp *in, CONST84 char *cmdname, char *arglist) {
  Tcl_ResetResult(in);
  Tcl_AppendResult(in, "wrong # args: should be {", cmdname, 0);
  if (arglist != 0) Tcl_AppendResult(in, " ", arglist, 0);
  Tcl_AppendResult(in, "}", 0);
  return TCL_ERROR;
}


static int
OTclErrBadVal(Tcl_Interp *in, char *expected, CONST84 char *value) {
  Tcl_ResetResult(in);
  Tcl_AppendResult(in, "expected ", expected, " but got", 0);
  Tcl_AppendElement(in, value);
  return TCL_ERROR;
}


static int
OTclErrType(Tcl_Interp *in, CONST84 char* nm, char* wt) {
  Tcl_ResetResult(in);
  Tcl_AppendResult(in,"type check failed: ",nm," is not of type ",wt,0);
  return TCL_ERROR;
}


/*
 * precedence ordering functions
 */


enum colors { WHITE, GRAY, BLACK };

static int
TopoSort(OTclClass* cl, OTclClass* base, OTclClasses* (*next)(OTclClass*)) {
  OTclClasses* sl = (*next)(cl);
  OTclClasses* pl;

  /*
   * careful to reset the color of unreported classes to
   * white in case we unwind with error, and on final exit
   * reset color of reported classes to white
   */

  cl->color = GRAY;
  for (; sl != 0; sl = sl->next) {
    OTclClass* sc = sl->cl;
    if (sc->color==GRAY) { cl->color = WHITE; return 0; }
    if (sc->color==WHITE && !TopoSort(sc, base, next)) {
      cl->color=WHITE;
      if (cl == base) {
	OTclClasses* pc = cl->order;
	while (pc != 0) { pc->cl->color = WHITE; pc = pc->next; }
      }
      return 0;
    }
  }
  cl->color = BLACK;
  pl = (OTclClasses*)ckalloc(sizeof(OTclClasses));
  pl->cl = cl;
  pl->next = base->order;
  base->order = pl;
  if (cl == base) {
    OTclClasses* pc = cl->order;
    while (pc != 0) { pc->cl->color = WHITE; pc = pc->next; }
  }
  return 1;
}


static void
RC(OTclClasses* sl) {
  while (sl != 0) {
    OTclClasses* n = sl->next;
    ckfree((char*)sl); sl = n;
  }
} 


static OTclClasses* Super(OTclClass* cl) { return cl->super; }

static OTclClasses*
ComputePrecedence(OTclClass* cl) {
  if (!cl->order) {
    int ok = TopoSort(cl, cl, Super);
    if (!ok) { RC(cl->order); cl->order = 0; }
  }
  return cl->order;
}


static OTclClasses* Sub(OTclClass* cl) { return cl->sub; }

static OTclClasses*
ComputeDependents(OTclClass* cl) {
  if (!cl->order) {
    int ok = TopoSort(cl, cl, Sub);
    if (!ok) { RC(cl->order); cl->order = 0; }
  }
  return cl->order;
}


static void
FlushPrecedences(OTclClass* cl) {
  OTclClasses* pc;
  RC(cl->order); cl->order = 0;
  pc = ComputeDependents(cl);

  /*
   * ordering doesn't matter here - we're just using toposort
   * to find all lower classes so we can flush their caches
   */

  if (pc) pc = pc->next;
  while (pc != 0) {
    RC(pc->cl->order); pc->cl->order = 0;
    pc = pc->next;
  }  
  RC(cl->order); cl->order = 0;
}


static void
AddInstance(OTclObject* obj, OTclClass* cl) {
  obj->cl = cl;
  if (cl != 0) {
    int nw;
    (void) Tcl_CreateHashEntry(&cl->instances, (char*)obj, &nw);
  }
}


static int
RemoveInstance(OTclObject* obj, OTclClass* cl) {
  if (cl != 0) {
    Tcl_HashEntry *hPtr = Tcl_FindHashEntry(&cl->instances, (char*)obj);
    if (hPtr) { Tcl_DeleteHashEntry(hPtr); return 1; }
  }
  return 0;
}


/*
 * superclass/subclass list maintenance
 */


static void
AS(OTclClass* cl, OTclClass* s, OTclClasses** sl) {
  OTclClasses* l = *sl;
  while (l &&  l->cl != s) l = l->next;
  if (!l) {
    OTclClasses* sc = (OTclClasses*)ckalloc(sizeof(OTclClasses));
    sc->cl = s; sc->next = *sl; *sl = sc;
  }
}

static void
AddSuper(OTclClass* cl, OTclClass* super) {
  if (cl && super) {
    
    /*
     * keep corresponding sub in step with super
     */ 

    AS(cl, super, &cl->super);
    AS(super, cl, &super->sub);
  }
}


static int
RS(OTclClass* cl, OTclClass* s, OTclClasses** sl) {
  OTclClasses* l = *sl;
  if (!l) return 0;
  if (l->cl == s) {
    *sl = l->next;
    ckfree((char*)l);
    return 1;
  }
  while (l->next && l->next->cl != s) l = l->next;
  if (l->next) {
    OTclClasses* n = l->next->next;
    ckfree((char*)(l->next));
    l->next = n;
    return 1;
  }
  return 0;
}


static int
RemoveSuper(OTclClass* cl, OTclClass* super) {

  /*
   * keep corresponding sub in step with super
   */ 

  int sp = RS(cl, super, &cl->super);
  int sb = RS(super, cl, &super->sub);
  return (sp && sb);
}


/*
 * internal type checking
 */


static OTclClass*
InObject(Tcl_Interp* in) {
  Tcl_HashEntry* hp = Tcl_FindHashEntry(theObjects, (char*)in);
  if (hp != 0) return (OTclClass*)Tcl_GetHashValue(hp);
  return 0;
}


static OTclClass*
InClass(Tcl_Interp* in) {
  Tcl_HashEntry* hp = Tcl_FindHashEntry(theClasses, (char*)in);
  if (hp != 0) return (OTclClass*)Tcl_GetHashValue(hp);
  return 0;
}


static int
IsType(OTclObject* obj, OTclClass* type) {
  OTclClass* t = obj ? obj->type : 0;
  while (t && t!=type) t = t->parent;
  return (t != 0);
}


/*
 * methods lookup and dispatch
 */


static int
LookupMethod(Tcl_HashTable* methods, CONST84 char* nm, Tcl_CmdProc** pr,
	     ClientData* cd, Tcl_CmdDeleteProc** dp) 
{
  Tcl_HashEntry *hPtr = Tcl_FindHashEntry(methods, nm);
  if (hPtr != 0) {
    Tcl_CmdInfo* co = (Tcl_CmdInfo*)Tcl_GetHashValue(hPtr);
    if (pr != 0) *pr = co->proc;
    if (cd != 0) *cd = co->clientData;
    if (dp != 0) *dp = co->deleteProc;
    return 1;
  }
  return 0;
}


static OTclClass*
SearchCMethod(OTclClasses* pl, CONST84 char* nm, Tcl_CmdProc** pr,
	      ClientData* cd, Tcl_CmdDeleteProc** dp)
{ 
  while (pl != 0) {
    Tcl_HashTable* cm = &pl->cl->instprocs;
    if (LookupMethod(cm, nm, pr, cd, 0) != 0) break;
    pl = pl->next;
  }
  return pl ? pl->cl : 0;
}


#define OTCLSMALLARGS 8

static int 
OTclDispatch(ClientData cd, Tcl_Interp* in, int argc, CONST84 char* argv[]) {
  OTclObject* self = (OTclObject*)cd;
  Tcl_CmdProc* proc = 0;
  ClientData cp = 0;
  OTclClass* cl = 0;

  if (argc < 2) return OTclErrArgCnt(in, argv[0], "message ?args...?");

  /*
   * try for local methods first, then up the class heirarchy
   */ 

  if (!self->procs || !LookupMethod(self->procs, argv[1], &proc, &cp, 0))
    cl = SearchCMethod(ComputePrecedence(self->cl),argv[1],&proc,&cp,0);

  if (proc) {
    CONST84 char* sargs[OTCLSMALLARGS];
    CONST84 char** args = sargs;
    int result;
    int i;
    
    /*
     * permute args to be:  self self class method <rest>
     * and, if method has no clientdata, pass an object pointer.
     */ 

    cp = (cp != 0) ? cp : cd;
    if (argc+2 > OTCLSMALLARGS)
      args = (CONST84 char**)ckalloc((argc+2)*sizeof(char*));
    args[0] = argv[0]; 
    args[1] = argv[0];
    args[2] = cl ? (char *) Tcl_GetCommandName(in, cl->object.id) : "";
    for (i = 1; i < argc; i++) args[i+2] = argv[i];
	
    /*
    printf("%d ", argc);
    for (i = 0; i < argc; i++)
      printf("%s ", argv[i]);
    printf("\n");
    */
    /*
    for (i = 0; i < argc + 2; i++)
      printf("%s ", args[i]);
    printf("\n");
    */

    result = (*proc)(cp, in, argc+2, (const char **) args);
    /* this adds to the stack trace */
    if (result == TCL_ERROR) {
	    char msg[150];
	    /* old_args2 is because args[2] was getting
	     * clobbered sometimes => seg fault.
	     * ---johnh
	     */
	    CONST84 char *old_args2 = cl ? (char *) Tcl_GetCommandName(in, cl->object.id) : argv[0];
	    sprintf(msg, "\n    (%.40s %.40s line %d)",
		    old_args2, argv[1], in->errorLine);
	    compat_Tcl_AddObjErrorInfo(in, msg, -1);
    }
    if (argc+2 > OTCLSMALLARGS) { ckfree((char*)args); args = 0; }
    return result;
  }

  /*
   * back off and try unknown
   */

  if (!self->procs || !LookupMethod(self->procs, "unknown", &proc, &cp, 0))
    cl = SearchCMethod(ComputePrecedence(self->cl),"unknown",&proc,&cp,0);
  
  if (proc) {
    CONST84 char* sargs[OTCLSMALLARGS];
    CONST84 char** args = sargs;
    int result;
    int i;
    
    /*
     * permute args to be:  self self class method <rest>
     * and, if method has no clientdata, pass an object pointer.
     */ 

    cp = (cp != 0) ? cp : cd;
    if (argc+3 > OTCLSMALLARGS)
      args = (CONST84 char**)ckalloc((argc+3)*sizeof(char*));
    args[0] = argv[0]; 
    args[1] = argv[0];
    args[2] = cl ? (char *) Tcl_GetCommandName(in, cl->object.id) : "";
    args[3] = "unknown";
    for (i = 1; i < argc; i++) args[i+3] = argv[i];
    result = (*proc)(cp, in, argc+3, (const char **) args);
    if (result == TCL_ERROR) {
	    char msg[100];
	    sprintf(msg, "\n    (%.30s unknown line %d)",
		    cl ? args[2] : argv[0], in->errorLine);
	    compat_Tcl_AddObjErrorInfo(in, msg, -1);
    }
    if (argc+3 > OTCLSMALLARGS) { ckfree((char*)args); args = 0; }
    return result;
  }  

  /*
   * and if that fails too, error out
   */

  Tcl_ResetResult(in);
  Tcl_AppendResult(in, argv[0], ": unable to dispatch method ", argv[1], 0);
  return TCL_ERROR;
}


/*
 * autoloading
 */


static void
AutoLoaderDP(ClientData cd) {
  ckfree((char*)cd);
}

static int
AutoLoader(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {

  /*
   * cd is a script to evaluate; object context reconstructed from argv
   */

  OTclObject* obj = OTclGetObject(in, argv[1]);
  OTclClass* cl = argv[2][0] ? OTclGetClass(in, argv[2]) : 0;
  CONST84 char* clname = cl ? argv[2] : "{}"; 
  Tcl_CmdProc* proc = 0;
  ClientData cp = 0;

  if (Tcl_Eval(in, (char*)cd) != TCL_OK) {
    Tcl_AppendResult(in, " during autoloading (object=", argv[1],
		     ", class=", clname, ", proc=", argv[3],")", 0);
    return TCL_ERROR;
  }

  /*
   * the above eval should have displaced this procedure from the object,
   * so check by looking at our old spot in the table, and if successful
   * continue dispatch with the right clientdata.
   */ 

  if (cl)
    (void) LookupMethod(&cl->instprocs, argv[3], &proc, &cp, 0);
  else if (obj->procs)
    (void) LookupMethod(obj->procs, argv[3], &proc, &cp, 0);

  if (proc && proc != (Tcl_CmdProc *) AutoLoader) {
    ClientData cdata = (cp != 0) ? cp : (ClientData)obj;
    return (*proc)(cdata, in, argc, (const char **) argv);
  }
  
  Tcl_ResetResult(in);
  Tcl_AppendResult(in, "no new proc during autoloading (object=", argv[1],
		   ", class=", clname, ", proc=", argv[3],")", 0);
  return TCL_ERROR;
}


int
MakeAuto(Tcl_CmdInfo* proc, CONST84 char* loader) {
  proc->proc = (Tcl_CmdProc *) AutoLoader;
  proc->deleteProc = AutoLoaderDP;
  proc->clientData = (ClientData)strcpy(ckalloc(strlen(loader)+1), loader);
  return (proc->clientData != 0);
}


/*
 * creating, installing, listing and removing procs
 */


static void
AddMethod(Tcl_HashTable* methods, CONST84 char* nm, Tcl_CmdProc* pr,
	  ClientData cd, ClientData ocd, Tcl_CmdDeleteProc* dp, ClientData dd)
{ 
  int nw = 0;
  Tcl_HashEntry *hPtr = Tcl_CreateHashEntry(methods, nm, &nw);
  Tcl_CmdInfo* co = (Tcl_CmdInfo*)ckalloc(sizeof(Tcl_CmdInfo));
  co->proc = pr;
  co->clientData = cd;
#ifdef TCL_VERSION_8_5_OR_HIGHER
  co->objClientData = ocd;
  co->isNativeObjectProc = (ocd != NULL) ? 1 : 0;
#endif
  co->deleteProc = dp;
  co->deleteData = dd;
  Tcl_SetHashValue(hPtr, (ClientData)co);
}


static int
RemoveMethod(Tcl_HashTable* methods, CONST84 char* nm, ClientData cd) { 
  Tcl_HashEntry *hPtr = Tcl_FindHashEntry(methods, nm);
  if (hPtr != 0) {
    Tcl_CmdInfo* co = (Tcl_CmdInfo*)Tcl_GetHashValue(hPtr);
#ifdef TCL_VERSION_8_5_OR_HIGHER
#else
    if (co->deleteProc != 0) (*co->deleteProc)(co->deleteData);
#endif
    ckfree((char*)co);
    Tcl_DeleteHashEntry(hPtr);
    return 1;
  }
  return 0;
}

#if TCL_MAJOR_VERSION >= 8
typedef struct {
	Tcl_Interp* interp;	
	int procUid;
} OTclDeleteProcData;


static int s_ProcUid=0; 
static const char s_otclProcPrefix[] = "::otcl::p";
static char s_otclProcName[sizeof(s_otclProcPrefix) + 8];

const char* GetProcName(int index)
{
	sprintf(s_otclProcName, "%s%d", s_otclProcPrefix, index);
	return s_otclProcName;
}

static void
OTclDeleteProc(ClientData cd)
{
  OTclDeleteProcData* pdpd = (OTclDeleteProcData*)cd;
  /* cleanup, ignore any errors */
  Tcl_Command cmd;
  cmd = Tcl_FindCommand(pdpd->interp, (char*)GetProcName(pdpd->procUid),
			(Tcl_Namespace*)NULL, 0);
  if (cmd) 
	  Tcl_DeleteCommandFromToken(pdpd->interp, cmd);
  ckfree((char*)pdpd);
}
#endif  


int
MakeProc(Tcl_CmdInfo* proc, Tcl_Interp* in, int argc, CONST84 char* argv[]) {    
  CONST84 char* name = argv[1];
  CONST84 char* oargs = argv[2];
  CONST84 char* nargs = (CONST84 char*)ckalloc(strlen("self class proc ")+strlen(argv[2])+1);
  int ok = 0;
  CONST84 char* id;

#if TCL_MAJOR_VERSION >= 8
  Tcl_Obj **objv;
  int i;
  id= (char*)GetProcName(++s_ProcUid);
#else
  id= "__OTclProc__";
#endif

  /*
   * add the standard method args automatically
   */

  argv[1] = id;
  (void)strcpy((char *)nargs, "self class proc ");
  if (argv[2][0] != 0) (void) strcat((char *)nargs, argv[2]);
  argv[2] = nargs;

#if TCL_MAJOR_VERSION >= 8
  objv = (Tcl_Obj **)ckalloc(argc * sizeof(Tcl_Obj *));
  for (i = 0; i < argc; i++) {
    objv[i] = Tcl_NewStringObj(argv[i], -1); /* let strlen() decide length */
    Tcl_IncrRefCount(objv[i]);
  }

  /*
   * use standard Tcl_ProcCmd to digest, and fish result out of interp
   */
  if (Tcl_ProcObjCmd(0, in, argc, objv) == TCL_OK) {
    if (Tcl_GetCommandInfo(in, id, proc) && proc->proc == ProcInterpId) {
      OTclDeleteProcData* pData =
        (OTclDeleteProcData*)(ckalloc(sizeof(OTclDeleteProcData)));

      pData->procUid = s_ProcUid;
      pData->interp = in;

      /* set the delete procedure to be OTclDeleteProc, which will
       * remove the procedure, the deleteProc will be called in, for example,
       * RemoveMethod, note that we are changing a copy of proc, the original
       * proc structure still has the right deleteProc */
      proc->deleteProc = OTclDeleteProc;
      proc->deleteData = (ClientData)pData;
      ok = 1;
    }
  }

  for (i = 0; i < argc; i++)
    Tcl_DecrRefCount(objv[i]);
  ckfree((char *)objv);
  
#else /* TCL_MAJOR_VERSION < 8 */
  
  if (Tcl_ProcCmd(0, in, argc, argv) == TCL_OK) {
    if (Tcl_GetCommandInfo(in, id, proc) && proc->proc == ProcInterpId) {
      Tcl_CmdDeleteProc* dp = proc->deleteProc;
      proc->deleteProc = 0;
      if (Tcl_SetCommandInfo(in, id, proc))
      	(void)Tcl_DeleteCommand(in, id);
      proc->deleteProc = dp;
      ok = 1;
    }
  }

#endif /* TCL_MAJOR_VERSION < 8 */

  ckfree((char*)nargs);
  argv[1] = name;
  argv[2] = oargs;
  
  return ok;
}

static void
ListKeys(Tcl_Interp* in, Tcl_HashTable* table, CONST84 char* pattern, int isVarHash) {
  Tcl_HashSearch hSrch;
  Tcl_HashEntry* hPtr = table ? Tcl_FirstHashEntry(table, &hSrch) : 0;
  Tcl_ResetResult(in);
  for (; hPtr != 0; hPtr = Tcl_NextHashEntry(&hSrch)) {
    char* key = (isVarHash) ? Tcl_VarHashGetKey(table, hPtr) : Tcl_GetHashKey(table, hPtr);
    if (!pattern || Tcl_StringMatch(key, pattern))
      Tcl_AppendElement(in, key);
  }
}


static void
ListInstanceKeys(Tcl_Interp* in, Tcl_HashTable* table, CONST84 char* pattern) {
  Tcl_HashSearch hSrch;
  Tcl_HashEntry* hPtr = table ? Tcl_FirstHashEntry(table, &hSrch) : 0;
  Tcl_ResetResult(in);
  for (; hPtr != 0; hPtr = Tcl_NextHashEntry(&hSrch)) {
    OTclObject* obj = (OTclObject*)Tcl_GetHashKey(table, hPtr);
    CONST84 char* name = (char *) Tcl_GetCommandName(in, obj->id);
    if (!pattern || Tcl_StringMatch(name, pattern))
      Tcl_AppendElement(in, name);
  }
}


static void
ListProcKeys(Tcl_Interp* in, Tcl_HashTable* table, CONST84 char* pattern) {
  Tcl_HashSearch hSrch;
  Tcl_HashEntry* hPtr = table ? Tcl_FirstHashEntry(table, &hSrch) : 0;
  Tcl_ResetResult(in);
  for (; hPtr != 0; hPtr = Tcl_NextHashEntry(&hSrch)) {
    CONST84 char* key = Tcl_GetHashKey(table, hPtr);
    Tcl_CmdProc* proc = ((Tcl_CmdInfo*)Tcl_GetHashValue(hPtr))->proc;
    if (pattern && !Tcl_StringMatch(key, pattern)) continue;
    
    /*
     * also counts anything to be autoloaded as a proc
     */

    if (proc!=(Tcl_CmdProc *) AutoLoader && proc!=ProcInterpId) continue; 
    Tcl_AppendElement(in, key);
  }
}


static Proc*
FindProc(Tcl_HashTable* table, CONST84 char* name) {
  Tcl_HashEntry* hPtr = table ? Tcl_FindHashEntry(table, name) : 0;
  if (hPtr) {
    Tcl_CmdInfo* co = (Tcl_CmdInfo*)Tcl_GetHashValue(hPtr);
    if (co->proc == ProcInterpId)
      return Tcl_CmdInfoGetProc(co);
  }
  return 0;
}
 

static int
ListProcArgs(Tcl_Interp* in, Tcl_HashTable* table, CONST84 char* name) {
  Proc* proc = FindProc(table, name);
  if (proc) {
#if TCL_MAJOR_VERSION == 7
    Arg* args = proc->argPtr;
#else
    CompiledLocal* args = proc->firstLocalPtr;
#endif
    int i = 0;

    /*
     * skip over hidden self, class, proc args 
     */

    for (; args!=0 && i<3; args = args->nextPtr, i++) ;
    Tcl_ResetResult(in);
    while (args != 0) {
#if TCL_MAJOR_VERSION >= 8
/*#if TCL_RELEASE_SERIAL >= 3*/
#if ((TCL_MINOR_VERSION == 0) && (TCL_RELEASE_SERIAL >= 3)) || (TCL_MINOR_VERSION > 0)
	    if (TclIsVarArgument(args))
#else
	    if (args->isArg)
#endif
#endif
		    Tcl_AppendElement(in, args->name);
	    args = args->nextPtr;
    }
    return TCL_OK;
  }
  return OTclErrBadVal(in, "a tcl method name", name);
}


static int
ListProcDefault(Tcl_Interp* in, Tcl_HashTable* table,
		CONST84 char* name, CONST84 char* arg, CONST84 char* var)
{

  /*
   * code snarfed from tcl info default
   */

  Proc* proc = FindProc(table, name);
  if (proc) {
#if TCL_MAJOR_VERSION < 8
    Arg *ap;
    for (ap = proc->argPtr; ap != 0; ap = ap->nextPtr) {
      if (strcmp(arg, ap->name) != 0) continue;
      if (ap->defValue != 0) {
	if (Tcl_SetVar(in, var, ap->defValue, 0) == 0) {
#else 
    CompiledLocal *ap;
    for (ap = proc->firstLocalPtr; ap != 0; ap = ap->nextPtr) {
      if (strcmp(arg, ap->name) != 0) continue;
      if (ap->defValuePtr != 0) {
	if (Tcl_SetVar(in, 
		       var, 
#if TCL_MINOR_VERSION == 0
		       TclGetStringFromObj(ap->defValuePtr, 
					   (int *) NULL),
#else
		       TclGetString(ap->defValuePtr),
#endif
		       0) == NULL) {
#endif
	  Tcl_ResetResult(in);
	  Tcl_AppendResult(in, "couldn't store default value in variable \"",
			   var, "\"", (char *) 0);
	  return TCL_ERROR;
	}
	Tcl_SetResult(in, "1", TCL_STATIC);
      } else {
	if (Tcl_SetVar(in, var, "", 0) == 0) {
	  Tcl_AppendResult(in, "couldn't store default value in variable \"",
			   var, "\"", (char *) 0);
	  return TCL_ERROR;
	}
	Tcl_SetResult(in, "0", TCL_STATIC);
      }
      return TCL_OK;
    }
    Tcl_ResetResult(in);
    Tcl_AppendResult(in, "procedure \"", name,
		     "\" doesn't have an argument \"", arg, "\"", (char *) 0);
    return TCL_ERROR;
  }
  return OTclErrBadVal(in, "a tcl method name", name);
}


static int
ListProcBody(Tcl_Interp* in, Tcl_HashTable* table, CONST84 char* name) {
  Proc* proc = FindProc(table, name);
  if (proc) {
    Tcl_ResetResult(in);
#if TCL_MAJOR_VERSION< 8
    Tcl_AppendResult(in, proc->command, 0);
#else 
    Tcl_AppendResult(in, 
#if TCL_MINOR_VERSION == 0
		     TclGetStringFromObj(proc->bodyPtr, (int *)NULL),
#else
		     TclGetString(proc->bodyPtr),
#endif
		     0);
#endif
    return TCL_OK;
  }
  return OTclErrBadVal(in, "a tcl method name", name);
}
 
/*
 * object creation
 */

static void
PrimitiveOInit(void* mem, Tcl_Interp* in, CONST84 char* name, OTclClass* cl) {
  OTclObject* obj = (OTclObject*)mem;

  obj->teardown = in;
  AddInstance(obj, cl);
  obj->type = InObject(in);
  obj->procs = 0;
  
  /*
   * fake callframe needed to interface to tcl variable
   * manipulations. looks like one below global 
   */

  obj->variables.level = 1;
#if TCL_MAJOR_VERSION < 8
  obj->variables.argc = 0;
  obj->variables.argv = 0;
#else
  obj->variables.numCompiledLocals = 0;
  obj->variables.compiledLocals = 0;
#endif
  obj->variables.callerPtr = 0;
  obj->variables.callerVarPtr = 0;

#if TCL_MAJOR_VERSION >= 8
  /* we need to deal with new members in CallFrame in Tcl8.0 */
  obj->variables.isProcCallFrame = 1;
  /* XXX: is it correct to assign global namespace here? */
  obj->variables.nsPtr = ((Interp *)in)->globalNsPtr; 
  obj->variables.objc = 0;
  obj->variables.objv = NULL; /* we don't want byte codes for now */
  obj->variables.procPtr = (Proc *) ckalloc(sizeof(Proc));
  obj->variables.procPtr->iPtr = (Interp *)in;
  obj->variables.procPtr->refCount = 1;
  /* XXX it correct to assign global namespace here? */
  obj->variables.procPtr->cmdPtr = NULL;
  obj->variables.procPtr->bodyPtr = NULL;
  obj->variables.procPtr->numArgs  = 0;	/* actual argument count is set below. */
  obj->variables.procPtr->numCompiledLocals = 0;
  obj->variables.procPtr->firstLocalPtr = NULL;
  obj->variables.procPtr->lastLocalPtr = NULL;
#ifdef TCL_VERSION_8_5_OR_HIGHER
  obj->variables.clientData = NULL;
  obj->variables.localCachePtr = NULL; 
#endif
#endif
}


static void PrimitiveODestroyNoFree(ClientData cd);

static void 
PrimitiveODestroy(ClientData cd) {
  PrimitiveODestroyNoFree(cd);   
  ckfree((char*)cd);
}

static void
PrimitiveODestroyNoFree(ClientData cd) {
  OTclObject* obj = (OTclObject*)cd;
  Tcl_HashSearch hs;
  Tcl_HashEntry* hp;
  Tcl_HashSearch hs2;
  Tcl_HashEntry* hp2;
  Tcl_Interp* in;

  /*
   * check and latch against recurrent calls with obj->teardown
   */

  if (!obj || !obj->teardown) return;
  in = obj->teardown; obj->teardown = 0;

  /*
   * call and latch user destroy with obj->id if we haven't
   */

  if (obj->id) {
    CONST84 char* args[2] = { "", "destroy" };
    Tcl_CmdInfo info;

    /*
     * but under 7.4p1 it is too late, so check with info
     */

    args[0] = (char *) Tcl_GetCommandName(in, obj->id);
    if (Tcl_GetCommandInfo(in, args[0], &info))
      (void) OTclDispatch(cd, in, 2, args);
    obj->id = 0;
  }

  /*
   * resume the primitive teardown for procs and variables.
   * variables unset here were lost from user destroy, and
   * any trace error messages will be swallowed.
   */
  if (obj->variables.varTablePtr == NULL) goto done;
  hp = Tcl_FirstHashEntry(ObjVarTablePtr(obj), &hs);
  while (hp != 0) {
    for (;;) {
      Var* vp = Tcl_VarHashGetValue(hp);
      if (!TclIsVarUndefined(vp)) break;
      hp = Tcl_NextHashEntry(&hs);
      if (hp == 0)
	      goto done;
    }
    if (hp != 0) {
      char* name = Tcl_VarHashGetKey(ObjVarTablePtr(obj), hp);
      (void)OTclUnsetInstVar(obj, in, name, TCL_LEAVE_ERR_MSG);
    }
    hp = Tcl_FirstHashEntry(ObjVarTablePtr(obj), &hs);
  }
done:
#ifdef TCL_VERSION_8_5_OR_HIGHER
#else
  hp = Tcl_FirstHashEntry(ObjVarTablePtr(obj), &hs);
  while (hp != 0) {
   /*
    * We delete the hash table below so disassociate
    * each remaining (undefined) var from its hash table entry.
    * (Otherwise, tcl will later try to delete
    * the already-freed hash table entry.)
    */
    Var* vp = (Var*)Tcl_GetHashValue(hp);
    vp->hPtr = 0;
    hp = Tcl_NextHashEntry(&hs);
  }
#endif
  if (obj->variables.varTablePtr != NULL) 
  	Tcl_DeleteHashTable(ObjVarTablePtr(obj));
  hp2 = obj->procs ? Tcl_FirstHashEntry(obj->procs, &hs2) : 0; 
  for (; hp2 != 0; hp2 = Tcl_NextHashEntry(&hs2)) {
    Tcl_CmdInfo* co = (Tcl_CmdInfo*)Tcl_GetHashValue(hp2);
    ClientData cdest = cd;
    if (co->clientData != 0) cdest = co->clientData;
    if (co->deleteProc != 0) (*co->deleteProc)(co->deleteData);
    ckfree((char*)co);
  }
  if (obj->procs) {
    Tcl_DeleteHashTable(obj->procs); ckfree((char*)(obj->procs));
  }

  (void)RemoveInstance(obj, obj->cl);

#if TCL_MAJOR_VERSION >= 8
  ckfree((char*)(obj->variables.procPtr));
  ckfree((char*)(obj->variables.varTablePtr));
#endif
}


static OTclObject*
PrimitiveOCreate(Tcl_Interp* in, CONST84 char* name, OTclClass* cl) {
  OTclObject* obj = (OTclObject*)ckalloc(sizeof(OTclObject)); 
#if TCL_MAJOR_VERSION < 8
  if (obj != 0) {
    PrimitiveOInit(obj, in, name, cl);
    obj->id = Tcl_CreateCommand(in, name, OTclDispatch, (ClientData)obj,
				PrimitiveODestroy);
  }
#else
  Tcl_VarHashInitialize(obj->variables.varTablePtr);
  if (obj != 0)
    if (Tcl_IsVarHashInitialized(obj->variables.varTablePtr)) {
      PrimitiveOInit(obj, in, name, cl);
      obj->id = Tcl_CreateCommand(in, name, (Tcl_CmdProc *) OTclDispatch, 
				  (ClientData)obj, PrimitiveODestroy);
    } else {
      ckfree((char *)obj);
      obj = NULL;
    }
#endif
  return obj;
}

static void
PrimitiveCInit(void* mem, Tcl_Interp* in, CONST84 char* name, OTclClass* class) {
  OTclObject* obj = (OTclObject*)mem;
  OTclClass* cl = (OTclClass*)mem;

  obj->type = InClass(in);
  cl->super = 0;
  cl->sub = 0;
  AddSuper(cl, InObject(in));
  cl->parent = InObject(in);
  cl->color = WHITE;
  cl->order = 0;
  Tcl_InitHashTable(&cl->instprocs, TCL_STRING_KEYS);
  Tcl_InitHashTable(&cl->instances, TCL_ONE_WORD_KEYS);
  cl->objectdata = 0;
}


static void
PrimitiveCDestroy(ClientData cd) {
  OTclClass* cl = (OTclClass*)cd;
  OTclObject* obj = (OTclObject*)cd;
  Tcl_HashSearch hSrch;
  Tcl_HashEntry* hPtr;
  Tcl_Interp* in;

  /*
   * check and latch against recurrent calls with obj->teardown
   */
  
  if (!obj || !obj->teardown) return;
  in = obj->teardown; obj->teardown = 0;

  /*
   * call and latch user destroy with obj->id if we haven't
   */

  if (obj->id) {
    CONST84 char* args[2] = { "", "destroy" };
    Tcl_CmdInfo info;

    /*
     * but under 7.4p1 it is too late, so check with info
     */

    args[0] = (char *) Tcl_GetCommandName(in, obj->id);
    if (Tcl_GetCommandInfo(in, args[0], &info))
      (void) OTclDispatch(cd, in, 2, args);
    obj->id = 0;
  }

  /*
   * resume the primitive teardown for instances and instprocs
   */ 

  hPtr = Tcl_FirstHashEntry(&cl->instances, &hSrch);
  while (hPtr) {
    /*
     * allow circularity for meta-classes
     */
    OTclObject* inst;
    for (;;) {
      inst = (OTclObject*)Tcl_GetHashKey(&cl->instances, hPtr);
      if (inst != (OTclObject*)cl) {
        CONST84 char* name = (char *) Tcl_GetCommandName(inst->teardown, inst->id);
	(void)Tcl_DeleteCommand(inst->teardown, name);
	break;
      }
      hPtr = Tcl_NextHashEntry(&hSrch);
      if (hPtr == 0)
	goto done;
    }
    hPtr = Tcl_FirstHashEntry(&cl->instances, &hSrch);
  }
done:
  hPtr = Tcl_FirstHashEntry(&cl->instprocs, &hSrch); 
  for (; hPtr != 0; hPtr = Tcl_NextHashEntry(&hSrch)) {
    /* for version 8 the instprocs are registered, so no need to delete them (?) */
    Tcl_CmdInfo* co = (Tcl_CmdInfo*)Tcl_GetHashValue(hPtr);
    ClientData cdest = cd;
    if (co->clientData != 0) cdest = co->clientData;
    if (co->deleteProc != 0) (*co->deleteProc)(co->deleteData);
    ckfree((char*)co);
  }
  Tcl_DeleteHashTable(&cl->instprocs);

  if (cl->objectdata) {
    Tcl_DeleteHashTable(cl->objectdata);
    ckfree((char*)(cl->objectdata)); cl->objectdata = 0;
  }

  /*
   * flush all caches, unlink superclasses
   */ 

  FlushPrecedences(cl);
  while (cl->super) (void)RemoveSuper(cl, cl->super->cl);
  while (cl->sub) (void)RemoveSuper(cl->sub->cl, cl);

  /*
   * handoff the primitive teardown
   */

  obj->teardown = in;  
  /* don't want to free the memory since we need to 
   * delete the hash table later, because we want the 
   * PrimitiveODestory to destory the hash entries first */
  PrimitiveODestroyNoFree(cd);
  Tcl_DeleteHashTable(&cl->instances);
  ckfree((char*)cd);
}


static OTclClass*
PrimitiveCCreate(Tcl_Interp* in, CONST84 char* name, OTclClass* class){
  OTclClass* cl = (OTclClass*)ckalloc(sizeof(OTclClass));
#if TCL_MAJOR_VERSION < 8
  if (cl != 0) {
    OTclObject* obj = (OTclObject*)cl;
    PrimitiveOInit(obj, in, name, class);
    PrimitiveCInit(cl, in, name, class);
    obj->id = Tcl_CreateCommand(in, name, OTclDispatch, (ClientData)cl,
				PrimitiveCDestroy);
  }
#else
  Tcl_VarHashInitialize(cl->object.variables.varTablePtr);
  if (cl != 0) 
    if (Tcl_IsVarHashInitialized(cl->object.variables.varTablePtr)) {
      OTclObject* obj = &cl->object;
      PrimitiveOInit(obj, in, name, class);
      PrimitiveCInit(cl, in, name, class);
      obj->id = Tcl_CreateCommand(in, name, (Tcl_CmdProc *) OTclDispatch, 
				  (ClientData)cl, PrimitiveCDestroy);
    } else {
      ckfree((char *)cl);
      cl = NULL;
    }
#endif
  return cl;
}


/*
 * object method implementations
 */


static int
OTclOAllocMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclClass* cl = OTclAsClass(in, cd);
  OTclObject* newobj;
  int i;

  if (!cl) return OTclErrType(in, argv[0], "Class");
  if (argc < 5) return OTclErrArgCnt(in, argv[0], "alloc <obj> ?args?");
  newobj = PrimitiveOCreate(in, argv[4], cl);
  if (newobj == 0) return OTclErrMsg(in,"Object alloc failed", TCL_STATIC);
  
  /*
   * this alloc doesn't process any extra args, so return them all
   */

  Tcl_ResetResult(in);
  for (i = 5; i < argc; i++) Tcl_AppendElement(in, argv[i]);
  return TCL_OK;
}


static int
OTclOInitMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclObject* obj = OTclAsObject(in, cd);
  int i;

  if (!obj) return OTclErrType(in, argv[0], "Object");
  if (argc < 4) return OTclErrArgCnt(in, argv[0], "init ?args?");
  if (argc & 1) return OTclErrMsg(in, "uneven number of args", TCL_STATIC);

  for (i=4; i<argc; i+=2) {
    int result;
    CONST84 char* args[3];
    args[0] = argv[0];
    args[1] = argv[i]; if (args[1][0] == '-') args[1]++;
    args[2] = argv[i+1];
    result = OTclDispatch(cd, in, 3, args);
    if (result != TCL_OK) {
      Tcl_AppendResult(in, " during {", args[0], "} {",
		       args[1], "} {", args[2], "}", 0);
      return result;
    }
  }
  Tcl_ResetResult(in);
  return TCL_OK;
}


static int
OTclODestroyMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclObject* obj = OTclAsObject(in, cd);
  Tcl_HashSearch hs;
  Tcl_HashEntry* hp;
  Tcl_Command oid;
  int result = TCL_OK;

  if (!obj) return OTclErrType(in, argv[0], "Object");
  if (argc != 4) return OTclErrArgCnt(in, argv[0], "destroy");

  /*
   * unset variables here, while it may not be too late
   * to deal with trace error messages
   */

  if (obj->variables.varTablePtr == NULL) goto done;
  hp = Tcl_FirstHashEntry(ObjVarTablePtr(obj), &hs);
  while (hp != 0) {
    for (;;) {
      Var* vp = Tcl_VarHashGetValue(hp);
      if (!TclIsVarUndefined(vp)) break;
      hp = Tcl_NextHashEntry(&hs);
      if (hp == 0)
        goto done;
    }
    if (hp != 0) {
	  CONST84 char* name = Tcl_VarHashGetKey(ObjVarTablePtr(obj), hp);
      result = OTclUnsetInstVar(obj, in, name, TCL_LEAVE_ERR_MSG);
      if (result != TCL_OK) break;
    }
    hp = Tcl_FirstHashEntry(ObjVarTablePtr(obj), &hs);
  }
  if (hp != 0) return TCL_ERROR;
done:
  /*
   * latch, and call delete command if not already in progress
   */

  oid = obj->id; obj->id = 0;
  if (obj->teardown != 0) {
    CONST84 char* name = (char *) Tcl_GetCommandName(in, oid);
    return (Tcl_DeleteCommand(in, name) == 0) ? TCL_OK : TCL_ERROR;
  }
  
  Tcl_ResetResult(in);
  return TCL_OK;
}


static int
OTclOClassMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclObject* obj = OTclAsObject(in, cd);
  OTclClass* cl;

  if (!obj) return OTclErrType(in, argv[0], "Object");
  if (argc != 5) return OTclErrArgCnt(in, argv[0], "class <class>");

  /*
   * allow a change to any class; type system enforces safety later
   */

  cl = OTclGetClass(in, argv[4]);
  if (!cl) return OTclErrBadVal(in, "a class", argv[4]);
  (void)RemoveInstance(obj, obj->cl);
  AddInstance(obj, cl);
  return TCL_OK;
}


static int OTclCInfoMethod(ClientData, Tcl_Interp*, int, CONST84 char*[]);

static int
OTclOInfoMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclObject* obj = OTclAsObject(in, cd);

  if (!obj) return OTclErrType(in, argv[0], "Object");
  if (argc < 5) return OTclErrArgCnt(in,argv[0],"info <opt> ?args?");
  
  if (!strcmp(argv[4], "class")) {
    if (argc > 6) return OTclErrArgCnt(in,argv[0],"info class ?class?");
    if (argc == 5) {
      Tcl_SetResult(in, (char *)Tcl_GetCommandName(in, obj->cl->object.id),
		    TCL_VOLATILE);
    } else {
      int result;
      CONST84 char* saved = argv[4];
      argv[4] = "superclass";
      result = OTclCInfoMethod((ClientData)obj->cl, in, argc, argv);
      argv[4] = saved;
      return result;
    }
  } else if (!strcmp(argv[4], "commands")) {
    if (argc > 6) return OTclErrArgCnt(in,argv[0],"info commands ?pat?");
    ListKeys(in, obj->procs, (argc == 6) ? argv[5] : 0, 0);
  } else if (!strcmp(argv[4], "procs")) {
    if (argc > 6) return OTclErrArgCnt(in,argv[0],"info procs ?pat?");
    ListProcKeys(in, obj->procs, (argc == 6) ? argv[5] : 0);
  } else if (!strcmp(argv[4], "args")) {
    if (argc != 6) return OTclErrArgCnt(in,argv[0],"info args <proc>");
    return ListProcArgs(in, obj->procs, argv[5]);
  } else if (!strcmp(argv[4], "default")) {
    if (argc != 8)
      return OTclErrArgCnt(in,argv[0],"info default <proc> <arg> <var>");
    return ListProcDefault(in, obj->procs, argv[5], argv[6], argv[7]);
  } else if (!strcmp(argv[4], "body")) {
    if (argc != 6) return OTclErrArgCnt(in,argv[0],"info body <proc>");
    return ListProcBody(in, obj->procs, argv[5]);
  } else if (!strcmp(argv[4], "vars")) {
    if (argc > 6) return OTclErrArgCnt(in,argv[0],"info vars ?pat?");
    ListKeys(in, ObjVarTablePtr(obj), (argc == 6) ? argv[5] : 0, 1);
  } else {
    return OTclErrBadVal(in, "an info option", argv[4]);
  }
  return TCL_OK;
}


static int
OTclOProcMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclObject* obj = OTclAsObject(in, cd);
  Tcl_CmdInfo proc;
  int op;

  if (!obj) return OTclErrType(in, argv[0], "Object");
  if (argc != 7) return OTclErrArgCnt(in,argv[0],"proc name args body");

  /*
   * if the args list is "auto", the body is a script to load the proc
   */

  if (!strcmp("auto", argv[5])) op = MakeAuto(&proc, argv[6]);
  else if (argv[5][0]==0 && argv[6][0]==0) op = -1;
  else op = MakeProc(&proc,in, argc-3, argv+3);

  if (!obj->procs) {
    obj->procs = (Tcl_HashTable*)ckalloc(sizeof(Tcl_HashTable));
    Tcl_InitHashTable(obj->procs, TCL_STRING_KEYS);
  }
  (void)RemoveMethod(obj->procs, argv[4], (ClientData)obj);
  if (op == 1) AddMethod(obj->procs, argv[4], proc.proc,
#ifdef TCL_VERSION_8_5_OR_HIGHER
			 proc.clientData, proc.objClientData, proc.deleteProc, proc.deleteData);
#else
			 proc.clientData, NULL, proc.deleteProc, proc.deleteData);
#endif
  
  return (op != 0) ? TCL_OK : TCL_ERROR;
}


static int
OTclONextMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclObject* obj = OTclAsObject(in, cd);
  CONST84 char* class = (char *) Tcl_GetVar(in, "class",0);
  CONST84 char* method = (char *) Tcl_GetVar(in, "proc",0);

  if (!obj) return OTclErrType(in, argv[0], "Object");
  if (argc < 4) return OTclErrArgCnt(in, argv[0], "next ?args?");
  if (!method||!class) return OTclErrMsg(in,"no executing proc", TCL_STATIC);

  argv[2] = class;
  argv[3] = method;
  return OTclNextMethod(obj, in, argc, argv);
}


static int
OTclOSetMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclObject* obj = OTclAsObject(in, cd);
  CONST84 char* result;

  if (!obj) return OTclErrType(in, argv[0], "Object");
  if (argc<5 || argc>6) return OTclErrArgCnt(in, argv[0], "set var ?value?");
  if (argc == 6)
    result = OTclSetInstVar(obj, in, argv[4], argv[5], TCL_LEAVE_ERR_MSG);
  else
    result = OTclGetInstVar(obj, in, argv[4], TCL_LEAVE_ERR_MSG);
  if (result != 0) Tcl_SetResult(in, (char *)result, TCL_VOLATILE); 
  return (result != 0) ? TCL_OK : TCL_ERROR;
}


static int
OTclOUnsetMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclObject* obj = OTclAsObject(in, cd);
  int result = TCL_ERROR;
  int i;

  if (!obj) return OTclErrType(in, argv[0], "Object");
  if (argc < 5) return OTclErrArgCnt(in, argv[0], "unset ?vars?");

  for (i=4; i<argc; i++) {
    result = OTclUnsetInstVar(obj, in, argv[i], TCL_LEAVE_ERR_MSG);
    if (result != TCL_OK) break;
  }
  return result;
}

/*
 * This (fairly low-level) routine is exported to allow tclcl
 * to avoid generating/evaling tcl code to do its instvar.
 */
int
OTclOInstVarOne(OTclObject* obj, Tcl_Interp *in, char *frameName, CONST84 char *varName, CONST84 char *localName, int flags)
{
  Interp* iPtr = (Interp*)in;
  int result = TCL_ERROR;

  /*
   * Fake things as if the caller's stack frame is just over
   * the object, then use UpVar to suck the object's variable
   * into the caller.
   *
   * Patched for global instvar by Orion Hodson <O.Hodson@cs.ucl.ac.uk>
   */
  if (iPtr->varFramePtr) {
  	CallFrame* saved = iPtr->varFramePtr->callerVarPtr;
  	int level = iPtr->varFramePtr->level;

 	iPtr->varFramePtr->callerVarPtr = &obj->variables;
  	iPtr->varFramePtr->level = obj->variables.level+1;
  	result = Tcl_UpVar(in, frameName, varName, localName, flags);
  	iPtr->varFramePtr->callerVarPtr = saved;
  	iPtr->varFramePtr->level = level;
  } else {
  	Tcl_SetResult(in, "no instvar in global :: scope", TCL_STATIC);
  }
  return result;
}

static int
OTclOInstVarMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[])
{
  OTclObject* obj = OTclAsObject(in, cd);
  int i;
  int result = TCL_ERROR;

  if (!obj) return OTclErrType(in, argv[0], "Object");
  if (argc < 5) return OTclErrArgCnt(in, argv[0], "instvar ?vars?");

  for (i=4; i<argc; i++) {
    int ac;
    CONST84 char **av;
    result = Tcl_SplitList(in, argv[i], &ac, (const char ***) &av);
    if (result != TCL_OK) break;
    if (ac == 1) {
      result = OTclOInstVarOne(obj, in, "1", av[0], av[0], 0);
    } else if (ac == 2) {
      result = OTclOInstVarOne(obj, in, "1", av[0], av[1], 0);
    } else {
      result = TCL_ERROR;
      Tcl_ResetResult(in);
      Tcl_AppendResult(in, "expected ?inst/local? or ?inst? ?local? but got ",
		       argv[i]);
    }
    ckfree((char*)av);
    if (result != TCL_OK) break;
  }
  return result;
}


/*
 * class method implementations
 */


static int
OTclCAllocMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclClass* cl = OTclAsClass(in, cd);
  OTclClass* newcl;
  int i;

  if (!cl) return OTclErrType(in, argv[0], "Class");
  if (argc < 5) return OTclErrArgCnt(in, argv[0], "alloc <cl> ?args?");
  newcl = PrimitiveCCreate(in, argv[4], cl);
  if (newcl == 0) return OTclErrMsg(in,"Class alloc failed", TCL_STATIC);

  /*
   * this alloc doesn't process any extra args, so return them all
   */

  Tcl_ResetResult(in);
  for (i = 5; i < argc; i++) Tcl_AppendElement(in, argv[i]);
  return TCL_OK;
}


static int
OTclCCreateMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclClass* cl = OTclAsClass(in, cd);
  OTclObject* obj;
  Tcl_CmdProc* proc = 0;
  ClientData cp = 0;
  OTclClasses* pl;
  CONST84 char* args[4];
  int result;
  int i;

  if (!cl) return OTclErrType(in, argv[0], "Class");
  if (argc < 5) return OTclErrArgCnt(in, argv[0], "create <obj> ?args?");
  for (pl = ComputePrecedence(cl); pl != 0; pl = pl->next) {
    Tcl_HashTable* procs = pl->cl->object.procs;
    if (procs && LookupMethod(procs,"alloc",&proc,&cp,0)) break;
  }
  if (!pl) return OTclErrMsg(in, "no reachable alloc", TCL_STATIC);
  
  for (i=0; i<4; i++) args[i] = argv[i];
  argv[0] = (char *) Tcl_GetCommandName(in, pl->cl->object.id);
  argv[1] = argv[0];
  argv[2] = "";
  argv[3] = "alloc";
  cp = (cp != 0) ? cp : (ClientData)pl->cl;
  result = (*proc)(cp, in, argc, (const char **) argv);
  for (i=0; i<4; i++) argv[i] = args[i];
  if (result != TCL_OK) return result;

  obj = OTclGetObject(in, argv[4]);
  if (obj == 0) 
	  return OTclErrMsg(in, "couldn't find result of alloc", TCL_STATIC);
  (void)RemoveInstance(obj, obj->cl);
  AddInstance(obj, cl);

  result = Tcl_VarEval(in, argv[4], " init ", in->result, 0);
  if (result != TCL_OK) return result;
  Tcl_SetResult(in, (char *)argv[4], TCL_VOLATILE);
  return TCL_OK;
}


static int
OTclCSuperClassMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclClass* cl = OTclAsClass(in, cd);
  OTclClasses* osl = 0;
  int ac = 0;
  CONST84 char** av = 0;
  OTclClass** scl = 0;
  int reversed = 0;
  int i, j;

  if (!cl) return OTclErrType(in, argv[0], "Class");
  if (argc != 5) return OTclErrArgCnt(in, argv[0], "superclass <classes>");
  if (Tcl_SplitList(in, argv[4], &ac, (const char ***) &av) != TCL_OK)
    return TCL_ERROR;

  scl = (OTclClass**)ckalloc(ac*sizeof(OTclClass*));
  for (i = 0; i < ac; i++) {
    scl[i] = OTclGetClass(in, av[i]);
    if (!scl[i]) {

      /*
       * try to force autoloading if we can't resolve a class name
       */

      int loaded = 0;
      char* args = (char*)ckalloc(strlen("auto_load ")+strlen(av[i])+1);
      (void)strcpy(args, "auto_load ");
      (void) strcat(args, av[i]);
      if (Tcl_Eval(in, args) == TCL_OK) {
	scl[i] = OTclGetClass(in, av[i]);
	loaded = (scl[i] != 0);
      }
      ckfree(args);
      if (!loaded) {
	ckfree((char*)av);
	ckfree((char*)scl);
	return OTclErrBadVal(in, "a list of classes", argv[4]);
      }
    }
  }

  /*
   * check that superclasses don't precede their classes
   */

  for (i = 0; i < ac; i++) {
    if (reversed != 0) break;
    for (j = i+1; j < ac; j++) {
      OTclClasses* dl = ComputePrecedence(scl[j]);
      if (reversed != 0) break;
      while (dl != 0) {
	if (dl->cl == scl[i]) break;
	dl = dl->next;
      }
      if (dl != 0) reversed = 1;
    }
  }
  
  if (reversed != 0) {
    ckfree((char*)av);
    ckfree((char*)scl);
    return OTclErrBadVal(in, "classes in dependence order", argv[4]);
  }
  
  while (cl->super != 0) {
    
    /*
     * build up an old superclass list in case we need to revert
     */ 

    OTclClass* sc = cl->super->cl;
    OTclClasses* l = osl;
    osl = (OTclClasses*)ckalloc(sizeof(OTclClasses));
    osl->cl = sc;
    osl->next = l;
    (void)RemoveSuper(cl, cl->super->cl);
  }
  for (i = 0; i < ac; i++)
    AddSuper(cl, scl[i]);
  ckfree((char*)av);
  ckfree((char*)scl);
  FlushPrecedences(cl);
  
  if (!ComputePrecedence(cl)) {

    /*
     * cycle in the superclass graph, backtrack
     */ 

    OTclClasses* l;
    while (cl->super != 0) (void)RemoveSuper(cl, cl->super->cl);
    for (l = osl; l != 0; l = l->next) AddSuper(cl, l->cl);
    RC(osl);
    return OTclErrBadVal(in, "a cycle-free graph", argv[4]);
  }
  RC(osl);
  Tcl_ResetResult(in);
  return TCL_OK;
}


static int
OTclCInfoMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclClass* cl = OTclAsClass(in, cd);

  if (!cl) return OTclErrType(in, argv[0], "Class");
  if (argc < 5) return OTclErrArgCnt(in,argv[0],"info <opt> ?args?");

  if (!strcmp(argv[4], "superclass")) {
    if (argc > 6) return OTclErrArgCnt(in,argv[0],"info superclass ?class?");
    if (argc == 5) {
      OTclClasses* sl = cl->super;
      OTclClasses* sc = 0;
      
      /*
       * reverse the list to obtain presentation order
       */ 
      
      Tcl_ResetResult(in);
      while (sc != sl) {
	OTclClasses* nl = sl;
	while (nl->next != sc) nl = nl->next;
	Tcl_AppendElement(in, Tcl_GetCommandName(in, nl->cl->object.id));
	sc = nl;
      }
    } else {
      OTclClass* isc = OTclGetClass(in, argv[5]);
      OTclClasses* pl;
      if (isc == 0) return OTclErrBadVal(in, "a class", argv[5]);
      pl = ComputePrecedence(cl);
      
      /*
       * search precedence to see if we're related or not
       */

      while (pl != 0) {
	if (pl->cl == isc) {
	  Tcl_SetResult(in, "1", TCL_STATIC);
	  break;
	}
	pl = pl->next;
      }
      if (pl == 0) Tcl_SetResult(in, "0", TCL_STATIC);
    }
  } else if (!strcmp(argv[4], "subclass")) {
    if (argc > 6) return OTclErrArgCnt(in,argv[0],"info subclass ?class?");
    if (argc == 5) {
      OTclClasses* sl = cl->sub;
      OTclClasses* sc = 0;
      
      /*
       * order unimportant
       */ 
      
      Tcl_ResetResult(in);
      for (sc = sl; sc != 0; sc = sc->next)
	Tcl_AppendElement(in, Tcl_GetCommandName(in, sc->cl->object.id));
    } else {
      OTclClass* isc = OTclGetClass(in, argv[5]);
      OTclClasses* pl;
      OTclClasses* saved;
      if (isc == 0) return OTclErrBadVal(in, "a class", argv[5]);
      saved = cl->order; cl->order = 0;
      pl = ComputeDependents(cl);
      
      /*
       * search precedence to see if we're related or not
       */

      while (pl != 0) {
	if (pl->cl == isc) {
	  Tcl_SetResult(in, "1", TCL_STATIC);
	  break;
	}
	pl = pl->next;
      }
      if (pl == 0) Tcl_SetResult(in, "0", TCL_STATIC);
      RC(cl->order); cl->order = saved;
    }
  } else if (!strcmp(argv[4], "heritage")) {
    OTclClasses* pl = ComputePrecedence(cl);
    CONST84 char* pattern = (argc == 6) ? argv[5] : 0;
    if (argc > 6) return OTclErrArgCnt(in,argv[0],"info heritage ?pat?");
    if (pl) pl = pl->next;
    Tcl_ResetResult(in);
    for (; pl != 0; pl = pl->next) {
      CONST84 char* name = (char *) Tcl_GetCommandName(in, pl->cl->object.id);
      if (pattern && !Tcl_StringMatch(name, pattern)) continue;
      Tcl_AppendElement(in, name);
    }
  } else if (!strcmp(argv[4], "instances")) {
    if (argc > 6) return OTclErrArgCnt(in,argv[0],"info instances ?pat?");
    ListInstanceKeys(in, &cl->instances, (argc == 6) ? argv[5] : 0);
  } else if (!strcmp(argv[4], "instcommands")) {
    if (argc > 6) return OTclErrArgCnt(in,argv[0],"info instcommands ?pat?");
    ListKeys(in, &cl->instprocs, (argc == 6) ? argv[5] : 0, 0);
  } else if (!strcmp(argv[4], "instprocs")) {
    if (argc > 6) return OTclErrArgCnt(in,argv[0],"info instprocs ?pat?");
    ListProcKeys(in, &cl->instprocs, (argc == 6) ? argv[5] : 0);
  } else if (!strcmp(argv[4], "instargs")) {
    if (argc != 6) return OTclErrArgCnt(in,argv[0],"info instargs <instproc>");
    return ListProcArgs(in, &cl->instprocs, argv[5]);
  } else if (!strcmp(argv[4], "instdefault")) {
    if (argc != 8)
      return OTclErrArgCnt(in,argv[0],
			   "info instdefault <instproc> <arg> <var>");
    return ListProcDefault(in, &cl->instprocs, argv[5], argv[6], argv[7]);
  } else if (!strcmp(argv[4], "instbody")) {
    if (argc != 6) return OTclErrArgCnt(in,argv[0],"info instbody <instproc>");
    return ListProcBody(in, &cl->instprocs, argv[5]);
  } else {
    return OTclOInfoMethod(cd, in, argc, argv);
  }
  return TCL_OK;
}


static int
OTclCInstProcMethod(ClientData cd, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclClass* cl = OTclAsClass(in, cd);
  Tcl_CmdInfo proc;
  int op;

  if (!cl) return OTclErrType(in, argv[0], "Class");
  if (argc != 7) return OTclErrArgCnt(in,argv[0],"instproc name args body");
  
  /*
   * if the args list is "auto", the body is a script to load the proc
   */

  if (!strcmp("auto", argv[5])) op = MakeAuto(&proc, argv[6]);
  else if (argv[5][0]==0 && argv[6][0]==0) op = -1;
  else op = MakeProc(&proc,in, argc-3, argv+3);

  (void)RemoveMethod(&cl->instprocs, argv[4], (ClientData)cl);
  if (op == 1) AddMethod(&cl->instprocs, argv[4], proc.proc,
#ifdef TCL_VERSION_8_5_OR_HIGHER
			 proc.clientData, proc.objClientData, proc.deleteProc, proc.deleteData);
#else
			 proc.clientData, NULL, proc.deleteProc, proc.deleteData);
#endif			
  
  return (op != 0) ? TCL_OK : TCL_ERROR;
}

/*
 * C interface routines for manipulating objects and classes
 */


extern OTclObject*
OTclAsObject(Tcl_Interp* in, ClientData cd) {
  OTclObject* obj = (OTclObject*)cd;
  return IsType(obj, InObject(in)) ? obj : 0;
}


extern OTclClass*
OTclAsClass(Tcl_Interp* in, ClientData cd) {
  OTclClass* cl = (OTclClass*)cd;
  return IsType((OTclObject*)cl, InClass(in)) ? cl : 0;
}


extern OTclObject*
OTclGetObject(Tcl_Interp* in, CONST84 char* name) {
  Tcl_CmdInfo info;
  OTclObject* obj = 0;
  if (Tcl_GetCommandInfo(in, name, &info))
    if (info.proc == (Tcl_CmdProc *) OTclDispatch)
      obj = OTclAsObject(in, info.clientData);
  return obj;
}


extern OTclClass*
OTclGetClass(Tcl_Interp* in, CONST84 char* name) {
  Tcl_CmdInfo info;
  OTclClass* cl = 0;
  if (Tcl_GetCommandInfo(in, name, &info))
    if (info.proc == (Tcl_CmdProc *) OTclDispatch)
      cl = OTclAsClass(in, info.clientData);
  return cl;
}


extern OTclObject*
OTclCreateObject(Tcl_Interp* in, CONST84 char* name, OTclClass* cl) {
  CONST84 char* args[3];
  args[0] = (char *) Tcl_GetCommandName(in, cl->object.id);
  args[1] = "create";
  args[2] = name;
  if (OTclDispatch((ClientData)cl,in,3,args) != TCL_OK) return 0;
  return OTclGetObject(in, name);
}


extern OTclClass*
OTclCreateClass(Tcl_Interp* in, CONST84 char* name, OTclClass* cl){
  CONST84 char* args[3];
  args[0] = (char *) Tcl_GetCommandName(in, cl->object.id);
  args[1] = "create";
  args[2] = name;
  if (OTclDispatch((ClientData)cl,in,3,args) != TCL_OK) return 0;
  return OTclGetClass(in, name);
}


extern int
OTclDeleteObject(Tcl_Interp* in, OTclObject* obj) {
  CONST84 char* args[2];
  args[0] = (char *) Tcl_GetCommandName(in, obj->id);
  args[1] = "destroy";
  return OTclDispatch((ClientData)obj, in, 2, args);
}


extern int
OTclDeleteClass(Tcl_Interp* in, OTclClass* cl) {
  CONST84 char* args[2];
  args[0] = (char *) Tcl_GetCommandName(in, cl->object.id);
  args[1] = "destroy";
  return OTclDispatch((ClientData)cl, in, 2, args);
}


extern void
OTclAddPMethod(OTclObject* obj, char* nm, Tcl_CmdProc* proc,
	       ClientData cd, Tcl_CmdDeleteProc* dp)
{
  if (obj->procs)
    (void)RemoveMethod(obj->procs, nm, (ClientData)obj);
  else {
    obj->procs = (Tcl_HashTable*)ckalloc(sizeof(Tcl_HashTable));
    Tcl_InitHashTable(obj->procs, TCL_STRING_KEYS);
  }
  AddMethod(obj->procs, nm, proc, cd, NULL, dp, cd);
}


extern void
OTclAddIMethod(OTclClass* cl, char* nm, Tcl_CmdProc* proc,
	       ClientData cd, Tcl_CmdDeleteProc* dp)
{
  (void)RemoveMethod(&cl->instprocs, nm, (ClientData)cl);
  AddMethod(&cl->instprocs, nm, proc, cd, NULL, dp, cd);
}


extern int
OTclRemovePMethod(OTclObject* obj, char* nm) {
  if (obj->procs) return RemoveMethod(obj->procs, nm, (ClientData)obj);
  else return 0;
}


extern int
OTclRemoveIMethod(OTclClass* cl, char* nm) {
  return RemoveMethod(&cl->instprocs, nm, (ClientData)cl);
}


extern int
OTclNextMethod(OTclObject* obj, Tcl_Interp* in, int argc, CONST84 char*argv[]) {
  OTclClass* cl = 0;
  OTclClass* ncl;
  OTclClasses* pl;
  Tcl_CmdProc* proc = 0;
  ClientData cp = 0;
  CONST84 char* class = argv[2];
  int result = TCL_OK;

  if (class[0]){
    cl = OTclGetClass(in, class);
    if (!cl) return OTclErrBadVal(in, "a class", class);
  }  

  /*
   * if we are already in the precedence ordering, then advance
   * past our last point; otherwise (if cl==0) start from the start
   */

  pl = ComputePrecedence(obj->cl);
  while (pl && cl) {
    if (pl->cl == cl) cl = 0;
    pl = pl->next;
  }

  /*
   * search for a further class method and patch args before launching.
   * if no further method, return without error.
   */

  ncl = SearchCMethod(pl, argv[3], &proc, &cp, 0);  
  if (proc != 0) {
    cp = (cp != 0) ? cp : (ClientData)obj;
    argv[2] = (char *) Tcl_GetCommandName(in, ncl->object.id);
    result = (*proc)(cp, in, argc, (const char **) argv);
    argv[2] = class;
  }
  return result;
}


extern CONST84_RETURN char*
OTclSetInstVar(OTclObject* obj,Tcl_Interp* in, 
	       CONST84 char* name, CONST84 char* value,int flgs){
  Interp* iPtr = (Interp*)in;
  CallFrame* saved = iPtr->varFramePtr;
  CONST84 char* result;

  iPtr->varFramePtr = &obj->variables;
  result = (char *) Tcl_SetVar(in, name, value, flgs);
  iPtr->varFramePtr = saved;
  return result;
}


extern CONST84_RETURN char*
OTclGetInstVar(OTclObject* obj, Tcl_Interp* in, CONST84 char* name, int flgs){
  Interp* iPtr = (Interp*)in;
  CallFrame* saved = iPtr->varFramePtr;
  CONST84 char* result;

  iPtr->varFramePtr = &obj->variables;
  result = (char *) Tcl_GetVar(in, name, flgs);
  iPtr->varFramePtr = saved;
  return result;
}


extern int
OTclUnsetInstVar(OTclObject* obj, Tcl_Interp* in, CONST84 char* name, int flgs) {
  Interp* iPtr = (Interp*)in;
  CallFrame* saved = iPtr->varFramePtr;
  int result;

  iPtr->varFramePtr = &obj->variables;
  result = Tcl_UnsetVar(in, name, flgs);
  iPtr->varFramePtr = saved;
  return result;
}


extern void
OTclSetObjectData(OTclObject* obj, OTclClass* cl, ClientData data) {
  Tcl_HashEntry *hPtr;
  int nw;

  if (!cl->objectdata) {
    cl->objectdata = (Tcl_HashTable*)ckalloc(sizeof(Tcl_HashTable));
    Tcl_InitHashTable(cl->objectdata, TCL_ONE_WORD_KEYS);
  }
  hPtr = Tcl_CreateHashEntry(cl->objectdata, (char*)obj, &nw);
  Tcl_SetHashValue(hPtr, data);
}


extern int
OTclGetObjectData(OTclObject* obj, OTclClass* cl, ClientData* data) {
  Tcl_HashEntry *hPtr;

  if (!cl->objectdata) return 0;
  hPtr = Tcl_FindHashEntry(cl->objectdata, (char*)obj);
  if (data) *data = hPtr ? Tcl_GetHashValue(hPtr) : 0;
  return (hPtr != 0);
}


extern int
OTclUnsetObjectData(OTclObject* obj, OTclClass* cl) {
  Tcl_HashEntry *hPtr;

  if (!cl->objectdata) return 0;
  hPtr = Tcl_FindHashEntry(cl->objectdata, (char*)obj);
  if (hPtr) Tcl_DeleteHashEntry(hPtr);
  return (hPtr != 0);
}


/*
 * Tcl extension initialization routine
 */

#define MAXTCLPROC 4096

extern int
Otcl_Init(Tcl_Interp* in) {
  OTclClass* theobj = 0;
  OTclClass* thecls = 0;
  Tcl_HashEntry* hp1;
  Tcl_HashEntry* hp2;
  int nw1;
  int nw2;
  char tm[MAXTCLPROC];
#if TCL_MAJOR_VERSION >= 8
  Tcl_Namespace *namespacePtr;
#endif
  
  /*
   * discover Tcl's hidden proc interpreter
   */
  
  if (ProcInterpId == 0) {
    char* args[4];
#if TCL_MAJOR_VERSION >= 8
    int i;
    int res = 0;
    Tcl_Obj* objv[4];
#endif

    args[0]="proc"; args[1]="_fake_"; args[2]=""; args[3]="return";

#if TCL_MAJOR_VERSION < 8
    if (Tcl_ProcCmd(0, in, 4, args) == TCL_OK) {
      Tcl_CmdInfo info;
      if (Tcl_GetCommandInfo(in, args[1], &info)) {
	ProcInterpId = info.proc;
	(void)Tcl_DeleteCommand(in, args[1]);
      } else return OTclErrMsg(in, "proc failed", TCL_STATIC);
    } else return TCL_ERROR;
#else /*TCL_MAJOR_VERSION >= 8*/
    for (i = 0; i < 4; i++) {
      objv[i] = Tcl_NewStringObj(args[i], -1);
      Tcl_IncrRefCount(objv[i]);
    }
    if (Tcl_ProcObjCmd(0, in, 4, objv) == TCL_OK) {
      Tcl_CmdInfo info;
      if (Tcl_GetCommandInfo(in, args[1], &info)) {
	ProcInterpId = info.proc;
	(void)Tcl_DeleteCommand(in, args[1]);
      } else 
	res = 1;
    } else 
      res = 2;
    for (i = 0; i < 4; i++)
      Tcl_DecrRefCount(objv[i]);
    switch (res) {
    case 1: return OTclErrMsg(in, "proc failed", TCL_STATIC);
    case 2: return TCL_ERROR;
    }
#endif  /*TCL_MAJOR_VERSION >= 8*/
  }
  /*
   * bootstrap the tables of base objects and classes
   */
  
  if (theObjects == 0) {
    theObjects = (Tcl_HashTable*)ckalloc(sizeof(Tcl_HashTable));
    if (!theObjects) return OTclErrMsg(in, "Object table failed", TCL_STATIC);
    Tcl_InitHashTable(theObjects, TCL_ONE_WORD_KEYS);
  }

  if (theClasses == 0) {
    theClasses = (Tcl_HashTable*)ckalloc(sizeof(Tcl_HashTable));
    if (!theClasses) return OTclErrMsg(in, "Class table failed", TCL_STATIC);
    Tcl_InitHashTable(theClasses, TCL_ONE_WORD_KEYS);
  }
    
  hp1 = Tcl_CreateHashEntry(theObjects, (char*)in, &nw1);
  if (nw1) theobj = PrimitiveCCreate(in, "Object", 0);
  hp2 = Tcl_CreateHashEntry(theClasses, (char*)in, &nw2);
  if (nw2) thecls = PrimitiveCCreate(in, "Class", 0);

  if (!nw1 && !nw2) {
    Tcl_SetResult(in, "0", TCL_STATIC);
    return TCL_OK;
  } else if (!theobj || !thecls) {
    if (theobj) PrimitiveCDestroy((ClientData)theobj);
    if (thecls) PrimitiveCDestroy((ClientData)thecls);
    return OTclErrMsg(in, "Object/Class failed", TCL_STATIC);
  }

  Tcl_SetHashValue(hp1, (char*)theobj);
  Tcl_SetHashValue(hp2, (char*)thecls);
    
  theobj->object.type = thecls;
  theobj->parent = 0;
  thecls->object.type = thecls;
  thecls->parent = theobj;

  AddInstance((OTclObject*)theobj, thecls);
  AddInstance((OTclObject*)thecls, thecls);
  AddSuper(thecls, theobj);

#if TCL_MAJOR_VERSION >= 8
  /* create the otcl namespace of otcl instprocs and procs */
  namespacePtr = Tcl_CreateNamespace(in, "otcl", (ClientData) NULL,
				     (Tcl_NamespaceDeleteProc *) NULL);  
  if (namespacePtr==NULL)
    return OTclErrMsg(in, "creation of name space failed", TCL_STATIC);
#endif
  
  /*
   * and fill them with functionality
   */
  
  OTclAddPMethod((OTclObject*)theobj, "alloc", (Tcl_CmdProc *) OTclOAllocMethod, 0, 0);
  OTclAddIMethod(theobj, "init", (Tcl_CmdProc *) OTclOInitMethod, 0, 0);
  OTclAddIMethod(theobj, "destroy", (Tcl_CmdProc *) OTclODestroyMethod, 0, 0);
  OTclAddIMethod(theobj, "class", (Tcl_CmdProc *) OTclOClassMethod, 0, 0);
  OTclAddIMethod(theobj, "info", (Tcl_CmdProc *) OTclOInfoMethod, 0, 0);
  OTclAddIMethod(theobj, "proc", (Tcl_CmdProc *) OTclOProcMethod, 0, 0);
  OTclAddIMethod(theobj, "next", (Tcl_CmdProc *) OTclONextMethod, 0, 0);
  OTclAddIMethod(theobj, "set", (Tcl_CmdProc *) OTclOSetMethod, 0, 0);
  OTclAddIMethod(theobj, "unset", (Tcl_CmdProc *) OTclOUnsetMethod, 0, 0);
  OTclAddIMethod(theobj, "instvar", (Tcl_CmdProc *) OTclOInstVarMethod, 0, 0);

  OTclAddPMethod((OTclObject*)thecls, "alloc", (Tcl_CmdProc *) OTclCAllocMethod, 0, 0);
  OTclAddIMethod(thecls, "create", (Tcl_CmdProc *) OTclCCreateMethod, 0, 0);
  OTclAddIMethod(thecls, "superclass", (Tcl_CmdProc *) OTclCSuperClassMethod, 0, 0);
  OTclAddIMethod(thecls, "info", (Tcl_CmdProc *) OTclCInfoMethod, 0, 0);
  OTclAddIMethod(thecls, "instproc", (Tcl_CmdProc *) OTclCInstProcMethod, 0, 0);

  /*
   * with some methods and library procs in tcl - they could go in a
   * otcl.tcl file, but they're embedded here with Tcl_Eval to avoid
   * the need to carry around a separate library.
   */

  (void)strcpy(tm, "Object instproc array {opt ary args} {             \n");
  (void)strcat(tm, "  $self instvar $ary                               \n");
  (void)strcat(tm, "  eval array [list $opt] [list $ary] $args         \n");
  (void)strcat(tm, "}                                                  \n");
  if (Tcl_Eval(in, tm) != TCL_OK) return TCL_ERROR;
  
  (void)strcpy(tm, "Class instproc unknown {m args} {                  \n");
  (void)strcat(tm, "  if {$m == {create}} then {                       \n");
  (void)strcat(tm, "    error \"$self: unable to dispatch $m\"         \n");
  (void)strcat(tm, "  }                                                \n");
  (void)strcat(tm, "  eval [list $self] create [list $m] $args         \n");
  (void)strcat(tm, "}                                                  \n");
  if (Tcl_Eval(in, tm) != TCL_OK) return TCL_ERROR;
  
  (void)strcpy(tm, "proc otcl_load {obj file} {                        \n");
  (void)strcat(tm, "   global auto_index                               \n");
  (void)strcat(tm, "   source $file                                    \n");
  (void)strcat(tm, "   foreach i [array names auto_index             \\\n");
  (void)strcat(tm, "       [list $obj *proc *]] {                      \n");
  (void)strcat(tm, "     set type [lindex $i 1]                        \n");
  (void)strcat(tm, "     set meth [lindex $i 2]                        \n");
  (void)strcat(tm, "     if {[$obj info ${type}s $meth] == {}} then {  \n");
  (void)strcat(tm, "       $obj $type $meth {auto} $auto_index($i)     \n");
  (void)strcat(tm, "     }                                             \n");
  (void)strcat(tm, "   }                                               \n");
  (void)strcat(tm, " }                                                 \n");
  if (Tcl_Eval(in, tm) != TCL_OK) return TCL_ERROR;

  (void)strcpy(tm, "proc otcl_mkindex {meta dir args} {                \n");
  (void)strcat(tm, "  set sp {[ 	]+}                            \n");
  (void)strcat(tm, "  set st {^[ 	]*}                            \n");
  (void)strcat(tm, "  set wd {([^ 	]+)}                           \n");
  (void)strcat(tm, "  foreach creator $meta {                          \n");
  (void)strcat(tm, "    lappend cp \"$st$creator${sp}create$sp$wd\"    \n");
  (void)strcat(tm, "    lappend ap \"$st$creator$sp$wd\"               \n");
  (void)strcat(tm, "  }                                                \n");
  (void)strcat(tm, "  foreach method {proc instproc} {                 \n");
  (void)strcat(tm, "    lappend mp \"$st$wd${sp}($method)$sp$wd\"      \n");
  (void)strcat(tm, "  }                                                \n");
  (void)strcat(tm, "  foreach cl [concat Class [Class info heritage]] {\n");
  (void)strcat(tm, "    eval lappend meths [$cl info instcommands]     \n");
  (void)strcat(tm, "  }                                                \n");
  (void)strcat(tm, "  set old [pwd]                                    \n");
  (void)strcat(tm, "  cd $dir                                          \n");
  (void)strcat(tm, "  append idx \"# Tcl autoload index file, \"       \n");
  (void)strcat(tm, "  append idx \"version 2.0\\n\"                    \n");
  (void)strcat(tm, "  append idx \"# otcl additions generated with \"  \n");
  (void)strcat(tm, "  append idx \"\\\"otcl_mkindex [list $meta] \"    \n");
  (void)strcat(tm, "  append idx \"[list $dir] $args\\\"\\n\"          \n");
  (void)strcat(tm, "  set oc 0                                         \n");
  (void)strcat(tm, "  set mc 0                                         \n");
  (void)strcat(tm, "  foreach file [eval glob -nocomplain -- $args] {  \n");
  (void)strcat(tm, "    if {[catch {set f [open $file]} msg]} then {   \n");
  (void)strcat(tm, "      catch {close $f}                             \n");
  (void)strcat(tm, "      cd $old                                      \n");
  (void)strcat(tm, "      error $msg                                   \n");
  (void)strcat(tm, "    }                                              \n");
  (void)strcat(tm, "    while {[gets $f line] >= 0} {                  \n");
  (void)strcat(tm, "      foreach c $cp {                              \n");
  (void)strcat(tm, "	    if {[regexp $c $line x obj]==1 &&          \n");
  (void)strcat(tm, "	        [string index $obj 0]!={$}} then {     \n");
  (void)strcat(tm, "	      incr oc                                  \n");
  (void)strcat(tm, "	      append idx \"set auto_index($obj) \"     \n");
  (void)strcat(tm, "	      append idx \"\\\"otcl_load $obj \"       \n");
  (void)strcat(tm, "          append idx \"\\$dir/$file\\\"\\n\"       \n");
  (void)strcat(tm, "	    }                                          \n");
  (void)strcat(tm, "	  }                                            \n");
  (void)strcat(tm, "      foreach a $ap {                              \n");
  (void)strcat(tm, "	    if {[regexp $a $line x obj]==1 &&          \n");
  (void)strcat(tm, "	        [string index $obj 0]!={$} &&          \n");
  (void)strcat(tm, "	        [lsearch -exact $meths $obj]==-1} {    \n");
  (void)strcat(tm, "	      incr oc                                  \n");
  (void)strcat(tm, "	      append idx \"set auto_index($obj) \"     \n");
  (void)strcat(tm, "	      append idx \"\\\"otcl_load $obj \"       \n");
  (void)strcat(tm, "          append idx \"\\$dir/$file\\\"\\n\"       \n");
  (void)strcat(tm, "	    }                                          \n");
  (void)strcat(tm, "	  }                                            \n");
  (void)strcat(tm, "      foreach m $mp {                              \n");
  (void)strcat(tm, "	    if {[regexp $m $line x obj ty pr]==1 &&    \n");
  (void)strcat(tm, "	        [string index $obj 0]!={$} &&          \n");
  (void)strcat(tm, "	        [string index $pr 0]!={$}} then {      \n");
  (void)strcat(tm, "	        incr mc                                \n");
  (void)strcat(tm, "	        append idx \"set \\{auto_index($obj \" \n");
  (void)strcat(tm, "	        append idx \"$ty $pr)\\} \\\"source \" \n");
  (void)strcat(tm, "	        append idx \"\\$dir/$file\\\"\\n\"     \n");
  (void)strcat(tm, "	    }                                          \n");
  (void)strcat(tm, "      }                                            \n");
  (void)strcat(tm, "    }                                              \n");
  (void)strcat(tm, "    close $f                                       \n");
  (void)strcat(tm, "  }                                                \n");
  (void)strcat(tm, "  set t [open tclIndex a+]                         \n");
  (void)strcat(tm, "  puts $t $idx nonewline                           \n");
  (void)strcat(tm, "  close $t                                         \n");
  (void)strcat(tm, "  cd $old                                          \n");
  (void)strcat(tm, "  return \"$oc objects, $mc methods\"              \n");
  (void)strcat(tm, "}                                                  \n");
  if (Tcl_Eval(in, tm) != TCL_OK) return TCL_ERROR;

  Tcl_SetResult(in, "1", TCL_STATIC);
  return TCL_OK;
}

/*
 * Otcl strangness:  why isn't c listed?
 *    dash> otclsh
 *    % Class Foo
 *    Foo
 *    % Foo instproc a a {}
 *    % Foo instproc b {} { }
 *    % Foo instproc c {} {}
 *    % Foo info instprocs
 *    a b
 * -johnh, 30-Jun-98
 */