-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathotcl.c
2344 lines (1974 loc) · 67 KB
/
otcl.c
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
/* -*- Mode: c++ -*-
*
* $Id: otcl.c,v 1.25 2011/09/18 20:58:40 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.