This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
tcl.c
2773 lines (2529 loc) · 70.7 KB
/
tcl.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
/*
* Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
*/
#include <xpap.h>
#if HAVE_TCL
#include <tcl.h>
/*
*----------------------------------------------------------------------------
*
*
* Private Routines and Data
*
*
*----------------------------------------------------------------------------
*/
/* with Tcl 8.4, some function prototypes have added CONST qualifiers,
which we try to deal with in a backward-compatible way */
#define XCONST84
#if TCL_MAJOR_VERSION >= 8
#if TCL_MINOR_VERSION >= 4
#undef XCONST84
#define XCONST84 CONST
#endif
#endif
#ifdef NULLSTRING
#undef NULLSTRING
#endif
#define NULLSTRING ""
#define TCL_NULLSTR(s) (!s || !*s || !strcmp(s, "{}"))
#define TY_CLIENT 1
#define TY_SERVER 2
#ifndef MAX_XPAS
#define MAX_XPAS 10000
#endif
/*
*
* record struct for client_data for XPATcl[Send,Receive,Info] routines
*
*/
typedef struct xpatclclientdatarec{
Tcl_Interp *interp;
char *callback;
char *client_data;
} *XPATclClientData, XPATclClientDataRec;
/*
*----------------------------------------------------------------------------
*
* Routine: Tcl_GetXPAFromObj
*
* Purpose: convert string to XPA handle
*
* Returns: Tcl error code
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
Tcl_GetXPAFromObj(Tcl_Interp *interp, Tcl_Obj *obj, int flag, XPA *xpa)
#else
static int Tcl_GetXPAFromObj(interp, obj, flag, xpa)
Tcl_Interp *interp;
Tcl_Obj *obj;
int flag;
XPA *xpa;
#endif
{
char *s;
void *lval;
Tcl_Obj *resultPtr;
/* get result pointer */
resultPtr = Tcl_GetObjResult(interp);
if( (s = Tcl_GetStringFromObj(obj, NULL)) == NULL ){
return(TCL_ERROR);
}
if( strncmp(s, "xpa_", 4) || (sscanf(&(s[4]), "%p", &lval) != 1) ){
Tcl_SetStringObj(resultPtr, "XPA$ERROR: invalid xpa handle", -1);
return(TCL_ERROR);
}
*xpa = (XPA)lval;
/* make sure its a valid xpa */
switch(flag){
case TY_CLIENT:
if( !XPAClientValid(*xpa) ){
Tcl_SetStringObj(resultPtr, "XPA$ERROR: invalid xpa client handle", -1);
return(TCL_ERROR);
}
break;
case TY_SERVER:
if( !XPAValid(*xpa) ){
Tcl_SetStringObj(resultPtr, "XPA$ERROR: invalid xpa server handle", -1);
return(TCL_ERROR);
}
break;
}
return(TCL_OK);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPATclHandler
*
* Purpose: common handler for access points written in tcl
* execute the tcl receive command with xpa arguments
* used instead of Tcl_Eval so we can avoid interpreting either
* paramlist or buf
*
*
* Returns: 0 on success, -1 on failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPATclHandler (void *client_data, void *call_data, char *paramlist,
char *buf, int len, int nargs)
#else
static int XPATclHandler(client_data, call_data, paramlist, buf, len, nargs)
void *client_data;
void *call_data;
char *paramlist;
char *buf;
int len;
int nargs;
#endif
{
XPA xpa = (XPA)call_data;
XPATclClientData xptr = (XPATclClientData)client_data;
Tcl_CmdInfo info; /* Info about command procedures */
Tcl_Obj *objv[10]; /* Object vector for arguments */
Tcl_Obj *resultPtr; /* The result object */
int result; /* TCL_OK or TCL_ERROR */
int object;
char tbuf[SZ_LINE];
XCONST84 char *argv[10];
char *s=NULL;
char *t=NULL;
char *cmd;
/* make sure we have a callback */
if( !xptr || !xptr->callback ){
XPAError(xpa, "Invalid tcl command for xpa callback");
return(-1);
}
/* set command name */
cmd = xptr->callback;
/* Map from the command name to a C procedure */
if( !Tcl_GetCommandInfo(xptr->interp, cmd, &info) ){
XPAError(xpa, "Unknown tcl command for xpa callback");
return(-1);
}
/* string-ize some values */
snprintf(tbuf, SZ_LINE, "xpa_%p", xpa);
s = xstrdup(tbuf);
if( nargs > 4 ){
snprintf(tbuf, SZ_LINE, "%d", len);
t = xstrdup(tbuf);
}
/* package up argument values */
object = info.isNativeObjectProc;
if (object) {
/* The object interface is preferred for this command */
objv[0] = Tcl_NewStringObj(cmd, strlen(cmd));
objv[1] = Tcl_NewStringObj(s, strlen(s));
if( (xptr->client_data == NULL) || (*xptr->client_data == '\0') )
objv[2] = Tcl_NewStringObj(NULLSTRING, strlen(NULLSTRING));
else
objv[2] = Tcl_NewStringObj(xptr->client_data, strlen(xptr->client_data));
if( (paramlist == NULL) || (*paramlist == '\0') )
objv[3] = Tcl_NewStringObj(NULLSTRING, strlen(NULLSTRING));
else
objv[3] = Tcl_NewStringObj(paramlist, strlen(paramlist));
if( nargs > 4 ){
if( (buf == NULL) || (*buf == '\0') || (len == 0) )
objv[4] = Tcl_NewStringObj(NULLSTRING, strlen(NULLSTRING));
else
objv[4] = Tcl_NewStringObj(buf, len);
objv[5] = Tcl_NewStringObj(t, strlen(t));
}
} else {
argv[0] = cmd;
argv[1] = s;
argv[2] = xptr->client_data;
argv[3] = paramlist;
if( nargs > 4 ){
argv[4] = buf;
argv[5] = t;
}
}
/* reset before we make C call */
Tcl_ResetResult(xptr->interp);
/*
* Invoke the C procedure.
*/
if (object) {
result = (*info.objProc)(info.objClientData, xptr->interp, nargs, objv);
/* Get the string value from the result object */
resultPtr = Tcl_GetObjResult(xptr->interp);
Tcl_SetResult(xptr->interp, Tcl_GetStringFromObj(resultPtr, NULL),
TCL_VOLATILE);
} else {
result = (*info.proc)(info.clientData, xptr->interp, nargs, argv);
}
/* clean up */
if( s ) xfree(s);
if( t ) xfree(t);
/* translate Tcl status into XPA status */
if( result == TCL_OK ){
return(0);
}
else{
s = (char *)Tcl_GetStringResult(xptr->interp);
if( !strncmp(s, "XPA$ERROR: ", 11) )
s += 11;
XPAError(xpa, s);
return(-1);
}
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPATclReceive
*
* Purpose: receive handler for access points written in tcl
* execute the tcl receive command with xpa arguments
* used instead of Tcl_Eval so we can avoid interpreting either
* paramlist or buf
*
*
* Returns: 0 on success, -1 on failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPATclReceive (void *client_data, void *call_data, char *paramlist,
char *buf, size_t len)
#else
static int XPATclReceive(client_data, call_data, paramlist, buf, len)
void *client_data;
void *call_data;
char *paramlist;
char *buf;
size_t len;
#endif
{
return(XPATclHandler(client_data, call_data, paramlist, buf, len, 6));
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPATclSend
*
* Purpose: send handler for access points written in tcl
*
* Returns: 0 on success, -1 on failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPATclSend (void *client_data, void *call_data, char *paramlist,
char **buf, size_t *len)
#else
static int XPATclSend(client_data, call_data, paramlist, buf, len)
void *client_data;
void *call_data;
char *paramlist;
char **buf;
size_t *len;
#endif
{
return(XPATclHandler(client_data, call_data, paramlist, NULL, 0, 4));
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPATclInfo
*
* Purpose: info handler for access points written in tcl
*
* Returns: 0 on success, -1 on failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPATclInfo (void *client_data, void *call_data, char *paramlist)
#else
static int XPATclInfo(client_data, call_data, paramlist)
void *client_data;
void *call_data;
char *paramlist;
#endif
{
return(XPATclHandler(client_data, call_data, paramlist, NULL, 0, 4));
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPANew_Tcl
*
* Purpose: Tcl binding to XPANew procedure
*
* Tcl call:
*
* xpanew class name help sproc sdata smode rproc rdata rmode
*
* use the empty string to specify NULL arguments
*
* Returns: Tcl error code
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPANew_Tcl(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
#else
static int XPANew_Tcl(clientData, interp, objc, objv)
ClientData clientData;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
#endif
{
char *xclass;
char *name;
char *help;
char *send_cb;
char *sdata;
char *smode;
char *rec_cb;
char *rdata;
char *rmode;
char tbuf[SZ_LINE];
XPA xpa;
SendCb sproc;
ReceiveCb rproc;
XPATclClientData sptr;
XPATclClientData rptr;
Tcl_Obj *resultPtr;
/* make sure argument count is correct */
if( objc != 10 ){
Tcl_WrongNumArgs(interp, 1, objv,
"class name help sproc sdata smode rproc rdata rmode");
return(TCL_ERROR);
}
/* get arguments as strings */
xclass = Tcl_GetStringFromObj(objv[1], NULL);
name = Tcl_GetStringFromObj(objv[2], NULL);
help = Tcl_GetStringFromObj(objv[3], NULL);
send_cb = Tcl_GetStringFromObj(objv[4], NULL);
sdata = Tcl_GetStringFromObj(objv[5], NULL);
smode = Tcl_GetStringFromObj(objv[6], NULL);
rec_cb = Tcl_GetStringFromObj(objv[7], NULL);
rdata = Tcl_GetStringFromObj(objv[8], NULL);
rmode = Tcl_GetStringFromObj(objv[9], NULL);
/* this will hold the result */
resultPtr = Tcl_GetObjResult(interp);
/* set up callback procedures */
if( ((send_cb == NULL) || (*send_cb == '\0') ) ){
sproc = NULL;
sptr = NULL;
}
else{
sproc = XPATclSend;
sptr = (XPATclClientData)xcalloc(1, sizeof(XPATclClientDataRec));
sptr->interp = interp;
sptr->callback = xstrdup(send_cb);
sptr->client_data = xstrdup(sdata);
}
if( ((rec_cb == NULL) || (*rec_cb == '\0') ) ){
rproc = NULL;
rptr = NULL;
}
else{
rproc = XPATclReceive;
rptr = (XPATclClientData)xcalloc(1, sizeof(XPATclClientDataRec));
rptr->interp = interp;
rptr->callback = xstrdup(rec_cb);
rptr->client_data = xstrdup(rdata);
}
/* make sure we have either a send or receive callback */
if( !sproc && !rproc ){
Tcl_SetStringObj(resultPtr,
"XPA$ERROR: xpanew requires send_cb or rec_cb (or both)", -1);
return(TCL_ERROR);
}
/* set up the tcl handler for the xpa access point */
xpa = XPANew(xclass, name, help, sproc, sptr, smode, rproc, rptr, rmode);
if( xpa == NULL ){
Tcl_SetStringObj(resultPtr,
"XPA$ERROR: could not create XPA access point", -1);
return(TCL_ERROR);
}
else{
/* add this xpa to the Tcl event loop */
XPATclAddInput(xpa);
/* return xpa address to tcl in a string */
snprintf(tbuf, SZ_LINE, "xpa_%p", xpa);
Tcl_SetStringObj(resultPtr, tbuf, -1);
return(TCL_OK);
}
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAFree_Tcl
*
* Purpose: Tcl binding to XPAFree procedure
*
* Tcl call:
*
* xpafree xpa
*
* Returns: Tcl error code
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAFree_Tcl(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
#else
static int XPAFree_Tcl(clientData, interp, objc, objv)
ClientData clientData;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
#endif
{
XPA xpa;
XPATclClientData ptr;
/* make sure argument count is correct */
if( objc != 2 ){
Tcl_WrongNumArgs(interp, 1, objv, "xpa");
return(TCL_ERROR);
}
/* get xpa, which is always arg 1 */
if( Tcl_GetXPAFromObj(interp, objv[1], TY_SERVER, &xpa) != TCL_OK){
return(TCL_ERROR);
}
/* reset error/result condition */
Tcl_ResetResult(interp);
/* free the associated tcl record, stored in the client data */
if( xpa->send_data ){
ptr = (XPATclClientData)xpa->send_data;
if( ptr->callback ) xfree( ptr->callback);
if( ptr->client_data ) xfree( ptr->client_data);
xfree(xpa->send_data);
}
if( xpa->receive_data ){
ptr = (XPATclClientData)xpa->receive_data;
if( ptr->callback ) xfree( ptr->callback);
if( ptr->client_data ) xfree( ptr->client_data);
xfree(xpa->receive_data);
}
if( xpa->info_data ){
ptr = (XPATclClientData)xpa->info_data;
if( ptr->callback ) xfree( ptr->callback);
if( ptr->client_data ) xfree( ptr->client_data);
xfree(xpa->info_data);
}
/* call the XPAFree routine */
XPAFree(xpa);
return(TCL_OK);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAInfoNew_Tcl
*
* Purpose: Tcl binding to XPAInfoNew procedure
*
* Tcl call:
*
* xpanew class name help iproc idata imode
*
* use the empty string to specify NULL arguments
*
* Returns: Tcl error code
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAInfoNew_Tcl(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
#else
static int XPAInfoNew_Tcl(clientData, interp, objc, objv)
ClientData clientData;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
#endif
{
char *xclass;
char *name;
char *info_cb;
char *idata;
char *imode;
char tbuf[SZ_LINE];
XPA xpa;
InfoCb iproc;
XPATclClientData iptr;
Tcl_Obj *resultPtr;
/* make sure argument count is correct */
if( objc != 6 ){
Tcl_WrongNumArgs(interp, 1, objv,
"class name iproc idata imode");
return(TCL_ERROR);
}
/* get arguments as strings */
xclass = Tcl_GetStringFromObj(objv[1], NULL);
name = Tcl_GetStringFromObj(objv[2], NULL);
info_cb = Tcl_GetStringFromObj(objv[3], NULL);
idata = Tcl_GetStringFromObj(objv[4], NULL);
imode = Tcl_GetStringFromObj(objv[5], NULL);
/* this will hold the result */
resultPtr = Tcl_GetObjResult(interp);
/* set up callback procedures */
if( info_cb == NULL ){
Tcl_SetStringObj(resultPtr,
"XPA$ERROR: xpainfonew requires info_cb", -1);
return(TCL_ERROR);
}
else{
iproc = XPATclInfo;
iptr = (XPATclClientData)xcalloc(1, sizeof(XPATclClientDataRec));
iptr->interp = interp;
iptr->callback = xstrdup(info_cb);
iptr->client_data = xstrdup(idata);
}
/* set up the tcl handler for the xpa access point */
xpa = XPAInfoNew(xclass, name, iproc, iptr, imode);
if( xpa == NULL ){
Tcl_SetStringObj(resultPtr,
"XPA$ERROR: could not create XPA info access point", -1);
return(TCL_ERROR);
}
else{
/* add this xpa to the Tcl event loop */
XPATclAddInput(xpa);
/* return xpa address to tcl in a string */
snprintf(tbuf, SZ_LINE, "xpa_%p", xpa);
Tcl_SetStringObj(resultPtr, tbuf, -1);
return(TCL_OK);
}
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPACmdNew_Tcl
*
* Purpose: Tcl binding to XPACmdNew procedure
*
* Tcl call:
*
* xpacmdnew class name
*
* Returns: Tcl error code
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPACmdNew_Tcl(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
#else
static int XPACmdNew_Tcl(clientData, interp, objc, objv)
ClientData clientData;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
#endif
{
char *xclass;
char *name;
char tbuf[SZ_LINE];
XPA xpa;
Tcl_Obj *resultPtr;
/* make sure argument count is correct */
if( objc != 3 ){
Tcl_WrongNumArgs(interp, 1, objv, "class name");
return(TCL_ERROR);
}
/* get arguments as strings */
xclass = Tcl_GetStringFromObj(objv[1], NULL);
name = Tcl_GetStringFromObj(objv[2], NULL);
/* this will hold the result */
resultPtr = Tcl_GetObjResult(interp);
/* set up the tcl handler for the xpa access point */
if( (xpa = XPACmdNew(xclass, name)) == NULL ){
Tcl_SetStringObj(resultPtr,
"XPA$ERROR: could not create XPA command access point", -1);
return(TCL_ERROR);
}
else{
/* add this xpa to the Tcl event loop */
XPATclAddInput(xpa);
/* return xpa address to tcl in a string */
snprintf(tbuf, SZ_LINE, "xpa_%p", xpa);
Tcl_SetStringObj(resultPtr, tbuf, -1);
return(TCL_OK);
}
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPACmdAdd_Tcl
*
* Purpose: Tcl binding to XPACmdAdd procedure
*
* Tcl call:
*
* xpacmdadd xpa name help sproc sdata smode rproc rdata rmode
*
* use the empty string to specify NULL arguments
*
* Returns: Tcl error code
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPACmdAdd_Tcl(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
#else
static int XPACmdAdd_Tcl(clientData, interp, objc, objv)
ClientData clientData;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
#endif
{
char *name;
char *help;
char *send_cb;
char *sdata;
char *smode;
char *rec_cb;
char *rdata;
char *rmode;
char tbuf[SZ_LINE];
XPA xpa;
XPACmd xpacmd;
SendCb sproc;
ReceiveCb rproc;
XPATclClientData sptr;
XPATclClientData rptr;
Tcl_Obj *resultPtr;
/* make sure argument count is correct */
if( objc != 10 ){
Tcl_WrongNumArgs(interp, 1, objv,
"class name help sproc sdata smode rproc rdata rmode");
return(TCL_ERROR);
}
/* get xpa, which is always arg 1 */
if( Tcl_GetXPAFromObj(interp, objv[1], TY_SERVER, &xpa) != TCL_OK){
return(TCL_ERROR);
}
name = Tcl_GetStringFromObj(objv[2], NULL);
help = Tcl_GetStringFromObj(objv[3], NULL);
send_cb = Tcl_GetStringFromObj(objv[4], NULL);
sdata = Tcl_GetStringFromObj(objv[5], NULL);
smode = Tcl_GetStringFromObj(objv[6], NULL);
rec_cb = Tcl_GetStringFromObj(objv[7], NULL);
rdata = Tcl_GetStringFromObj(objv[8], NULL);
rmode = Tcl_GetStringFromObj(objv[9], NULL);
/* this will hold the result */
resultPtr = Tcl_GetObjResult(interp);
/* set up callback procedures */
if( ((send_cb == NULL) || (*send_cb == '\0') ) ){
sproc = NULL;
sptr = NULL;
}
else{
sproc = XPATclSend;
sptr = (XPATclClientData)xcalloc(1, sizeof(XPATclClientDataRec));
sptr->interp = interp;
sptr->callback = xstrdup(send_cb);
sptr->client_data = xstrdup(sdata);
}
if( ((rec_cb == NULL) || (*rec_cb == '\0') ) ){
rproc = NULL;
rptr = NULL;
}
else{
rproc = XPATclReceive;
rptr = (XPATclClientData)xcalloc(1, sizeof(XPATclClientDataRec));
rptr->interp = interp;
rptr->callback = xstrdup(rec_cb);
rptr->client_data = xstrdup(rdata);
}
/* make sure we have either a send or receive callback */
if( !sproc && !rproc ){
Tcl_SetStringObj(resultPtr,
"XPA$ERROR: xpacmdadd requires send_cb or rec_cb (or both)", -1);
return(TCL_ERROR);
}
/* set up the tcl handler for the xpa access point */
xpacmd = XPACmdAdd(xpa, name, help, sproc, sptr, smode, rproc, rptr, rmode);
if( xpacmd == NULL ){
Tcl_SetStringObj(resultPtr, "XPA$ERROR: could not create XPA command", -1);
return(TCL_ERROR);
}
else{
/* return xpa address to tcl in a string */
snprintf(tbuf, SZ_LINE, "xpacmd_%p", xpacmd);
Tcl_SetStringObj(resultPtr, tbuf, -1);
return(TCL_OK);
}
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPACmdDel_Tcl
*
* Purpose: Tcl binding to XPACmdDel procedure
*
* Tcl call:
*
* xpacmddel xpa cmd
*
* Returns: Tcl error code
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPACmdDel_Tcl(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
#else
static int XPACmdDel_Tcl(clientData, interp, objc, objv)
ClientData clientData;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
#endif
{
void *lval;
char *s;
XPA xpa;
XPACmd cmd;
Tcl_Obj *resultPtr;
/* make sure argument count is correct */
if( objc != 3 ){
Tcl_WrongNumArgs(interp, 1, objv, "xpa cmd");
return(TCL_ERROR);
}
/* get result pointer */
resultPtr = Tcl_GetObjResult(interp);
/* get xpa, which is always arg 1 */
if( Tcl_GetXPAFromObj(interp, objv[1], TY_SERVER, &xpa) != TCL_OK){
return(TCL_ERROR);
}
/* get xpacmd, which is always arg 2 */
if( (s = Tcl_GetStringFromObj(objv[2], NULL)) == NULL ){
return(TCL_ERROR);
}
if( strncmp(s, "xpacmd_", 7) || (sscanf(&(s[7]), "%p", &lval) != 1) ){
Tcl_SetStringObj(resultPtr, "XPA$ERROR: invalid xpacmd handle", -1);
return(TCL_ERROR);
}
cmd = (XPACmd)lval;
/* reset error/result condition */
Tcl_ResetResult(interp);
/* call the XPACmdDel routine */
if( XPACmdDel(xpa, cmd) == 0 ){
/* free the associated tcl record, stored in the client data */
if( cmd->send_data ) xfree(cmd->send_data);
if( cmd->receive_data ) xfree(cmd->receive_data);
return(TCL_OK);
}
else{
resultPtr = Tcl_GetObjResult(interp);
Tcl_SetStringObj(resultPtr, "XPA$ERROR: could not delete xpa cmd", -1);
return(TCL_ERROR);
}
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPARec_Tcl
*
* Purpose: Tcl binding to retrieve info from the xpa struct
*
* Tcl call:
*
* set val [xparec xpa <option>]
*
* where option can be one of the following:
*
* name
* class
* method
* cmdfd
* datafd
#ifndef HAVE_CYGWIN
* cmdchan
* datachan
#endif
* sendian
* cendian
* version
*
*
* Returns: Tcl error code
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPARec_Tcl(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
#else
static int XPARec_Tcl(clientData, interp, objc, objv)
ClientData clientData;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
#endif
{
#ifndef HAVE_CYGWIN
Tcl_Channel chan;
#endif
static XCONST84 char *subCmds[] = {
"cendian",
"class",
#ifndef HAVE_CYGWIN
"cmdchan",
#endif
"cmdfd",
#ifndef HAVE_CYGWIN
"datachan",
#endif
"datafd",
"method",
"name",
"sendian",
"version",
(char *) NULL};
enum ISubCmdIdx {
ICendianIdx,
IClassIdx,
#ifndef HAVE_CYGWIN
ICmdChanIdx,
#endif
ICmdFdIdx,
#ifndef HAVE_CYGWIN
IDataChanIdx,
#endif
IDataFdIdx,
IMethodIdx,
INameIdx,
ISendianIdx,
IVersionIdx
} index;
XPA xpa;
int result;
char tbuf[SZ_LINE];
/* we always have 3 args: xparec $xpa [option] */
if (objc != 3) {
Tcl_WrongNumArgs(interp, 1, objv, "xpa option");
return TCL_ERROR;
}
/* get sub-command index */
if( (result = Tcl_GetIndexFromObj(interp, objv[2], subCmds, "option", 0,
(int *)&index)) != TCL_OK ){
return result;
}
/* get xpa, which is always arg 1 */
if( (result=Tcl_GetXPAFromObj(interp, objv[1], TY_SERVER, &xpa)) != TCL_OK){
/* valid xpa is required, except for version */
if( index != IVersionIdx )
return(TCL_ERROR);
}
/* process sub-command */
switch (index) {
case ICendianIdx:
Tcl_SetResult(interp, (char *)xpa_cendian(xpa), TCL_VOLATILE);
result = TCL_OK;
break;
case IClassIdx:
Tcl_SetResult(interp, xpa_class(xpa), TCL_VOLATILE);
result = TCL_OK;
break;
#ifndef HAVE_CYGWIN
case ICmdChanIdx:
snprintf(tbuf, SZ_LINE, "sock%d", xpa_cmdfd(xpa));
/* make sure we have a valid OS socket fd */
if( xpa_cmdfd(xpa) < 0 ){
Tcl_SetResult(interp, "none", TCL_STATIC);
result = TCL_OK;
}
/* see if this socket already is defined as a tcl variable */
else if( Tcl_GetChannel(interp, tbuf, NULL) != NULL ){
Tcl_SetResult(interp, tbuf, TCL_VOLATILE);
result = TCL_OK;
}
/* create new tcl variable for this socket */
else{
/* create a tcl channel corresponding to the xpa socket */
if( !(chan = Tcl_MakeTcpClientChannel((ClientData)(uintptr_t)xpa_cmdfd(xpa))) ){
Tcl_SetResult(interp, "XPA$ERROR: could not map XPA cmdfd to tcl",
TCL_STATIC);
result = TCL_ERROR;
}
else{
/* hold a reference to this channel outside tcl */
Tcl_RegisterChannel(NULL, chan);
/* register this channel with tcl */
Tcl_RegisterChannel(interp, chan);
/* make this Tcl channel unbuffered, so user does not have to flush */
Tcl_SetChannelOption(interp, chan, "-buffering", "none");
/* return name so that it can be used by tcl */
Tcl_SetResult(interp, (char *)Tcl_GetChannelName(chan), TCL_VOLATILE);
result = TCL_OK;
}
}
break;
#endif
case ICmdFdIdx:
if( xpa_cmdfd(xpa) < 0 )
strcpy(tbuf, "none");
else
snprintf(tbuf, SZ_LINE, "%d", xpa_cmdfd(xpa));
Tcl_SetResult(interp, tbuf, TCL_VOLATILE);
result = TCL_OK;
break;