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
/
Copy pathcommand.c
1217 lines (1144 loc) · 29.7 KB
/
command.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>
/*
*----------------------------------------------------------------------------
*
*
* Private Routines and Data
*
*
*----------------------------------------------------------------------------
*/
/* this is the static xpa struct that holds the reserved commands */
static XPA rxpa=NULL;
/*
*----------------------------------------------------------------------------
*
* Routine: XPACmdParseNames
*
* Purpose: massage a name string, changing multiple sequential spaces
* into a single space
*
* Returns: new name, with spaces massaged (also number of spacess)
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static char *
XPACmdParseNames(char *lbuf, int *ntokens)
#else
static char *XPACmdParseNames(lbuf, ntokens)
char *lbuf;
int *ntokens;
#endif
{
char tbuf[SZ_LINE];
int lp=0;
char *buf;
/* can't be larger than the original string */
buf = (char *)xmalloc(strlen(lbuf)+1);
*buf = '\0';
*ntokens = 0;
/* pick off each word, separating by a single space */
while( word(lbuf, tbuf, &lp)){
if( *buf != '\0' )
strcat(buf, " ");
strcat(buf, tbuf);
*ntokens += 1;
}
/* make the string the right size */
buf = (char *)xrealloc(buf, strlen(buf)+1);
return(buf);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAReceiveNSConnect
*
* Purpose: reset and re-establish connection to name server
*
* Returns: xpa callback error codes
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAReceiveNSConnect (void *client_data, void *call_data, char *paramlist,
char *buf, size_t len)
#else
static int XPAReceiveNSConnect(client_data, call_data, paramlist, buf, len)
void *client_data;
void *call_data;
char *paramlist;
char *buf;
size_t len;
#endif
{
XPA xpa = (XPA)call_data;
XPA txpa;
char tbuf[SZ_LINE];
int doall=0;
int lp=0;
if( paramlist && *paramlist ){
if( word(paramlist, tbuf, &lp) && !strcmp(tbuf, "-all") ){
doall = 1;
}
}
if( doall ){
for(txpa=XPAListHead(); txpa!=NULL; txpa=txpa->next){
XPANSAdd(txpa, NULL, NULL);
}
}
else{
XPANSAdd(xpa, NULL, NULL);
}
return(0);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAReceiveNSDisconnect
*
* Purpose: break connection to name server
*
* Returns: xpa callback error codes
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAReceiveNSDisconnect (void *client_data, void *call_data, char *paramlist,
char *buf, size_t len)
#else
static int XPAReceiveNSDisconnect(client_data, call_data, paramlist, buf, len)
void *client_data;
void *call_data;
char *paramlist;
char *buf;
size_t len;
#endif
{
XPA xpa = (XPA)call_data;
XPA txpa;
NS ns, tns;
char tbuf[SZ_LINE];
int doall=0;
int lp=0;
if( paramlist && *paramlist ){
if( word(paramlist, tbuf, &lp) && !strcmp(tbuf, "-all") ){
doall = 1;
}
}
if( doall ){
for(txpa=XPAListHead(); txpa!=NULL; txpa=txpa->next){
for(ns=txpa->nshead; ns!= NULL; ){
tns = ns->next;
XPANSClose(txpa, ns);
ns = tns;
}
}
}
else{
for(ns=xpa->nshead; ns!= NULL; ){
tns = ns->next;
XPANSClose(xpa, ns);
ns = tns;
}
}
return(0);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAReceiveEnv
*
* Purpose: set an environment variable
*
* Returns: xpa callback error codes
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAReceiveEnv (void *client_data, void *call_data, char *paramlist,
char *buf, size_t len)
#else
static int XPAReceiveEnv(client_data, call_data, paramlist, buf, len)
void *client_data;
void *call_data;
char *paramlist;
char *buf;
size_t len;
#endif
{
XPA xpa = (XPA)call_data;
char name[SZ_LINE];
char value[SZ_LINE];
char *tbuf;
int lp=0;
if( word(paramlist, name, &lp) ){
if( word(paramlist, value, &lp) ){
tbuf = (char *)xmalloc(strlen(name)+1+strlen(value)+1);
snprintf(tbuf, SZ_LINE, "%s=%s", name, value);
putenv(tbuf);
return(0);
}
else{
if( strchr(name, '=') != NULL ){
tbuf = xstrdup(name);
putenv(tbuf);
return(0);
}
else{
XPAError(xpa, "XPA setenv requires name and value pair\n");
return(-1);
}
}
}
else{
XPAError(xpa, "XPA setenv requires name and value pair\n");
return(-1);
}
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPASendEnv
*
* Purpose: return an environment variable to client
*
* Returns: xpa callback error codes
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPASendEnv (void *client_data, void *call_data, char *paramlist,
char **buf, size_t *len)
#else
static int XPASendEnv(client_data, call_data, paramlist, buf, len)
void *client_data;
void *call_data;
char *paramlist;
char **buf;
size_t *len;
#endif
{
int tlen;
char *tbuf;
char *ebuf;
if( (ebuf = (char *)getenv(paramlist)) != NULL ){
tlen = strlen(ebuf)+2;
tbuf = (char *)xmalloc(tlen);
snprintf(tbuf, tlen, "%s\n", ebuf);
*buf = tbuf;
*len = strlen(tbuf);
}
else{
*buf = xstrdup("\n");
*len = 1;
}
return(0);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAReceiveReserved
*
* Purpose: execute reserved command
*
* Returns: xpa callback error codes
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAReceiveReserved (void *client_data, void *call_data, char *paramlist,
char *buf, size_t len)
#else
static int XPAReceiveReserved(client_data, call_data, paramlist, buf, len)
void *client_data;
void *call_data;
char *paramlist;
char *buf;
size_t len;
#endif
{
char *cmd = (char *)client_data;
XPA xpa = (XPA)call_data;
if( !strcmp(cmd, "end") ){
xpa->comm->status |= XPA_STATUS_ENDBUF;
return(0);
}
else if( !strcmp(cmd, "exec") ){
xpa->comm->status |= XPA_STATUS_READBUF;
return(0);
}
else{
return(-1);
}
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPASendHelp
*
* Purpose: send help strings
*
* Returns: xpa callback error codes
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPASendHelp (void *client_data, void *call_data, char *paramlist,
char **buf, size_t *len)
#else
static int XPASendHelp(client_data, call_data, paramlist, buf, len)
void *client_data;
void *call_data;
char *paramlist;
char **buf;
size_t *len;
#endif
{
XPA xpa = (XPA)call_data;
XPACmd cmd;
int lp=0;
int slen;
char tbuf[SZ_LINE];
char lbuf[SZ_LINE];
char *sbuf;
if( !paramlist || !*paramlist ){
if( xpa->version != NULL ){
snprintf(lbuf, SZ_LINE, "XPA version: %s\n", xpa->version);
send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0);
}
}
if( xpa->commands == NULL ){
if( xpa->help != NULL ){
slen = strlen(xpa->help)+SZ_LINE;
sbuf = (char *)xmalloc(slen);
snprintf(sbuf, slen, "%s\n", xpa->help);
send(xpa_datafd(xpa), sbuf, strlen(sbuf), 0);
xfree(sbuf);
}
else{
strcpy(lbuf, "\n");
send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0);
}
}
else{
if( paramlist && *paramlist ){
while( word(paramlist, tbuf, &lp) ){
for(cmd=xpa->commands; cmd!=NULL; cmd=cmd->next){
if( !strcmp(tbuf, cmd->name) ){
if( cmd->help != NULL ){
slen = strlen(cmd->name)+strlen(cmd->help)+SZ_LINE;
sbuf = (char *)xmalloc(slen);
snprintf(sbuf, slen, "%s:\t%s\n", cmd->name, cmd->help);
send(xpa_datafd(xpa), sbuf, strlen(sbuf), 0);
xfree(sbuf);
}
else{
snprintf(lbuf, SZ_LINE, "%s:\t(no help available)\n", cmd->name);
send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0);
}
}
}
}
}
else{
for(cmd=xpa->commands; cmd!=NULL; cmd=cmd->next){
if( cmd->help != NULL ){
slen = strlen(cmd->name)+strlen(cmd->help)+SZ_LINE;
sbuf = (char *)xmalloc(slen);
snprintf(sbuf, slen, "%s:\t%s\n", cmd->name, cmd->help);
send(xpa_datafd(xpa), sbuf, strlen(sbuf), 0);
xfree(sbuf);
}
else{
snprintf(lbuf, SZ_LINE, "%s:\t(no help available)\n", cmd->name);
send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0);
}
}
}
}
return(0);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPASendVersion
*
* Purpose: send XPA version string
*
* Returns: xpa callback error codes
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPASendVersion (void *client_data, void *call_data, char *paramlist,
char **buf, size_t *len)
#else
static int XPASendVersion(client_data, call_data, paramlist, buf, len)
void *client_data;
void *call_data;
char *paramlist;
char **buf;
size_t *len;
#endif
{
XPA xpa = (XPA)call_data;
char lbuf[SZ_LINE];
if( xpa->version != NULL )
snprintf(lbuf, SZ_LINE, "%s\n", xpa->version);
else
strcpy(lbuf, "\n");
send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0);
return(0);
}
/*
*----------------------------------------------------------------------------
*
*
* Semi-Public Routines and Data
*
* These routines are used by XPAHandler and XPANew
*
*
*----------------------------------------------------------------------------
*/
/*
*----------------------------------------------------------------------------
*
* Routine: XPAInitReserved
*
* Purpose: add the reserved commands to the reserved xpa struct
*
* Results: none
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
void
XPAInitReserved (void)
#else
void XPAInitReserved()
#endif
{
if( !rxpa ){
if( (rxpa = (XPA)xcalloc(1, sizeof(struct xparec))) == NULL )
return;
/* XPACmdAdd requires that the callbacks be defined explicitly in rxpa */
rxpa->send_callback = XPASendCommands;
rxpa->receive_callback = XPAReceiveCommands;
/* add reserved commands */
XPACmdAdd(rxpa, "-acl",
"\tget (set) the access control list\n\t\t options: host type acl",
XPASendAcl, NULL, NULL, XPAReceiveAcl, NULL, "fillbuf=false");
XPACmdAdd(rxpa, "-env",
"\tget (set) an environment variable\n\t\t options: name (value)",
XPASendEnv, NULL, NULL, XPAReceiveEnv, NULL, NULL);
XPACmdAdd(rxpa, "-exec",
"\texecute commands from buffer\n\t\t options: none",
NULL, NULL, NULL, XPAReceiveReserved, (void *)"exec", NULL);
XPACmdAdd(rxpa, "-help",
"\treturn help string for specified XPA\n\t\t options: cmd name (commands only)",
XPASendHelp, NULL, NULL, NULL, NULL, NULL);
XPACmdAdd(rxpa, "-ltimeout",
"\tget (set) long timeout\n\t\t options: seconds|reset",
XPASendLTimeout, NULL, NULL, XPAReceiveLTimeout, NULL, NULL);
XPACmdAdd(rxpa, "-nsconnect",
"\tre-establish name server connection to this XPA\n\t\t options: -all",
NULL, NULL, NULL, XPAReceiveNSConnect, NULL, NULL);
XPACmdAdd(rxpa, "-nsdisconnect",
"\tbreak name server connection to this XPA\n\t\t options: -all",
NULL, NULL, NULL, XPAReceiveNSDisconnect, NULL, NULL);
XPACmdAdd(rxpa, "-remote",
"\tconnect to remote name service with specified acl \n\t\t options: host:port +|-|acl -proxy",
XPASendRemote, NULL, NULL, XPAReceiveRemote, NULL, "fillbuf=false");
XPACmdAdd(rxpa, "-clipboard",
"\tset/get clipboard information \n\t\t options: [cmd] name",
XPASendClipboard, NULL, NULL, XPAReceiveClipboard, NULL, NULL);
XPACmdAdd(rxpa, "-stimeout",
"\tget (set) short timeout\n\t\t options: seconds|reset",
XPASendSTimeout, NULL, NULL, XPAReceiveSTimeout, NULL, NULL);
XPACmdAdd(rxpa, "-version",
"\treturn XPA version string\n\t\t options: none",
XPASendVersion, NULL, NULL, NULL, NULL, NULL);
}
}
#ifdef ANSI_FUNC
void
XPAFreeReserved (void)
#else
void XPAFreeReserved()
#endif
{
XPACmd cmd, tcmd;
if( !rxpa ) return;
/* free reserved commands */
for(cmd=rxpa->commands; cmd!=NULL; ){
tcmd = cmd->next;
XPACmdDel(rxpa, cmd);
cmd = tcmd;
}
xfree(rxpa);
rxpa = NULL;
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPACmdLookupReserved
*
* Purpose: lookup a reserved command name
*
* Results: cmd struct or null
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
XPACmd
XPACmdLookupReserved (XPA xpa, char *lbuf, int *lp)
#else
XPACmd XPACmdLookupReserved(xpa, lbuf, lp)
XPA xpa;
char *lbuf;
int *lp;
#endif
{
XPACmd cmd;
int lp2=0;
char *lptr;
char name[SZ_LINE];
/* make sure we have something to work with */
if( (rxpa==NULL) || (lbuf==NULL) || (lbuf[*lp]=='\0') )
return(NULL);
/* this is where we start parsing */
lptr = &(lbuf[*lp]);
/* to simplify life, we assume reserved words are 1 token */
if( !word(lptr, name, &lp2) )
return(NULL);
/* look for reserved keywords that have callbacks */
for(cmd=rxpa->commands; cmd!=NULL; cmd=cmd->next){
if( !strcmp(name, cmd->name) ){
*lp += lp2;
return(cmd);
}
}
/* nothing, nowhere */
return(NULL);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPACmdLookup
*
* Purpose: lookup a user-defined command name
*
* Results: cmd struct or null
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
XPACmd
XPACmdLookup (XPA xpa, char *lbuf, int *lp)
#else
XPACmd XPACmdLookup(xpa, lbuf, lp)
XPA xpa;
char *lbuf;
int *lp;
#endif
{
XPACmd cmd;
int i;
int lp2;
int len, tlen;
char *lptr;
char tbuf[SZ_LINE];
char name[SZ_LINE];
/* make sure we have something to work with */
if( (xpa==NULL) || (lbuf==NULL) || (lbuf[*lp]=='\0') )
return(NULL);
/* this is where we start parsing */
lptr = &(lbuf[*lp]);
/* look up commands for this name */
for(cmd=xpa->commands; cmd!=NULL; cmd=cmd->next){
/* make up a name with the required number of tokens for this command */
*name = '\0';
lp2 = 0;
tlen = 0;
for(i=0; i<cmd->ntokens; i++){
if( word(lptr, tbuf, &lp2)){
len = strlen(tbuf)+1;
if( (tlen+len) <= (SZ_LINE-1) ){
if( *name != '\0' )
strcat(name, " ");
strcat(name, tbuf);
tlen += len;
}
/* not enough room */
else{
*name = '\0';
break;
}
}
}
/* we now have the name, see if its what we want */
if( *name && !strcmp(cmd->name, name) ){
*lp += lp2;
return(cmd);
}
}
/* did not find the command in this xpa -- now look through reserved */
return(XPACmdLookupReserved(xpa, lbuf, lp));
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAReceiveCommands
*
* Purpose: process a list of commands from xpaset
*
* Results: number of commands processed
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
XPAReceiveCommands (void *client_data, void *call_data, char *paramlist,
char *buf, size_t len)
#else
int XPAReceiveCommands(client_data, call_data, paramlist, buf, len)
void *client_data;
void *call_data;
char *paramlist;
char *buf;
size_t len;
#endif
{
XPA xpa = (XPA)call_data;
XPACmd cmd;
int lp;
int savelp;
int plen;
int bgot;
int got=0;
int gotbuf=0;
int freebuf=1;
char lbuf[SZ_LINE];
char tbuf[SZ_LINE];
char tbuf1[SZ_LINE];
/* use ";" as a command separator (as well as \n) */
newdtable(";");
/* if we already have buf, there will be no need to get it */
if( buf )
gotbuf++;
/* if we have no paramlist, we read from the socket */
if( (xpa_datafd(xpa) >=0) && (!paramlist || (*paramlist == '\0')) ){
xpa->comm->status |= XPA_STATUS_READBUF;
XPAGets(xpa, xpa_datafd(xpa), lbuf, SZ_LINE, XPALongTimeout());
FPRINTF((stderr, "%sXPAReceiveCommands: read %s\n", _sp, lbuf));
}
else{
xpa->comm->status &= ~XPA_STATUS_READBUF;
nowhite(paramlist, lbuf);
}
/* we must have something to start with */
if( *lbuf == '\0' ){
XPAError(xpa, xpaMessbuf[XPA_RTN_NOCMD2]);
got = -1;
goto done;
}
/* This look either executes the one set of commands in paramlist,
or reads one line at a time from the socket and executes commands.
In the latter case, each callback can read the socket as well */
while( 1 ){
FPRINTF((stderr, "%sXPAReceiveCommands: top of loop: %s\n", _sp, lbuf));
lp = 0;
while( lbuf[lp] != '\0' ){
if( (cmd = XPACmdLookup(xpa, lbuf, &lp)) == NULL ){
XPAError(xpa, xpaMessbuf[XPA_RTN_UNCMD]);
got = -1;
goto done;
}
/* reserved commands can only be called from the same host as the server,
or from local (unix) sockets */
if( (cmd->xpa == rxpa) &&
strcmp(cmd->name, "-help") && strcmp(cmd->name, "-version") ){
if( XPAMtype() == XPA_INET ){
if( (!xpa->comm || !LOCALIP(xpa->comm->cmdip)) ){
FPRINTF((stderr, "%sXPAReceiveCommands: rejecting reserved: %s\n",
_sp, cmd->name));
XPAError(xpa, xpaMessbuf[XPA_RTN_NOAUTH]);
got = -1;
goto done;
}
}
}
FPRINTF((stderr, "%sXPAReceiveCommands: cmd->name: %s\n",
_sp, cmd->name));
*tbuf = '\0';
if( (lastdelim() != ';') && (lastdelim() != '\n') ){
/* skip white space between command and params */
while( isspace((int)lbuf[lp]) )
lp++;
/* here is where the params start */
savelp = lp;
/* look for command delimiter -- the end of the params */
while( word(lbuf, tbuf1, &lp) ){
if( (lastdelim() == ';') || (lastdelim() == '\n') ){
break;
}
/* make sure a command-ending delim is not next */
while( isspace((int)lbuf[lp]) )
lp++;
if( (lbuf[lp] == ';') || (lbuf[lp] == '\n') ){
break;
}
}
/* get length of parameter list */
plen = lp - savelp;
/* but skip final delim */
if( (plen>0) && ((lastdelim() == ';')||(lastdelim() == '\n')) )
plen--;
/* copy the params up to the command delimiter */
if( plen > 0 ){
strncpy(tbuf, &(lbuf[savelp]), plen);
tbuf[plen] = '\0';
}
}
/* execute the associated XPA callback */
if( cmd->receive_callback != NULL ){
/* get buf now, if its needed */
if( !gotbuf && (xpa_datafd(xpa) >= 0) &&
(cmd->receive_mode & XPA_MODE_FILLBUF) ){
/* read buf -- this buf will stay around for all commands */
FPRINTF((stderr, "%sXPAReceiveCommands: at XPAGetBuf\n", _sp));
bgot = XPAGetBuf(xpa, xpa_datafd(xpa), &buf, &len, -1);
/* close the data channel */
XPACloseData(xpa, xpa->comm);
if( bgot >= 0 ){
/* got the buffer */
gotbuf++;
}
/* error getting buf */
else{
XPAError(xpa, xpaMessbuf[XPA_RTN_NODATA]);
got = -1;
goto done;
}
}
got = (cmd->receive_callback)(cmd->receive_data, call_data,
tbuf, buf, len);
/* if we don't want to free the buffer, mark it for saving */
if( (buf != NULL) && !(cmd->receive_mode & XPA_MODE_FREEBUF) ){
freebuf=0;
}
if( got != 0 ){
goto done;
}
}
else{
XPAError(xpa, xpaMessbuf[XPA_RTN_NOREC]);
got = -1;
goto done;
}
}
/* conditions for exiting the command loop: */
/* if we processed the END command, we are done */
if( xpa->comm->status & XPA_STATUS_ENDBUF )
break;
/* if we are not reading the data buffer, we are done */
if( !(xpa->comm->status & XPA_STATUS_READBUF) )
break;
/* if we got EOF or error reading the data buffer, we are done */
if( XPAGets(xpa, xpa_datafd(xpa), lbuf, SZ_LINE, XPALongTimeout()) <=0 )
break;
}
done:
/* if no one wants buf, free it now */
if( freebuf )
xfree(buf);
/* restore last delimiter table */
freedtable();
/* return error code */
return(got);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPASendCommands
*
* Purpose: process a list of commands from xpaget
*
* Results: number of commands processed (currently 0 or 1)
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
XPASendCommands (void *client_data, void *call_data, char *paramlist,
char **buf, size_t *len)
#else
int XPASendCommands(client_data, call_data, paramlist, buf, len)
void *client_data;
void *call_data;
char *paramlist;
char **buf;
size_t *len;
#endif
{
XPA xpa = (XPA)call_data;
XPACmd cmd;
char tbuf[SZ_LINE];
int lp=0;
int got=0;
/* return help as default */
if( *paramlist == '\0' ){
paramlist = "-help";
}
/* lookup the command and execute */
if( (cmd = XPACmdLookup(xpa, paramlist, &lp)) != NULL ){
/* reserved commands can only be called from the same host as the server,
or from local (unix) sockets */
if( (cmd->xpa == rxpa) &&
strcmp(cmd->name, "-help") && strcmp(cmd->name, "-version") ){
if( XPAMtype() == XPA_INET ){
if( (!xpa->comm || !LOCALIP(xpa->comm->cmdip)) ){
FPRINTF((stderr, "%sXPAReceiveCommands: rejecting reserved: %s\n",
_sp, cmd->name));
XPAError(xpa, xpaMessbuf[XPA_RTN_NOAUTH]);
got = -1;
goto done;
}
}
}
/* execute the associated XPA send callback,
using the remaining command string as an argument */
strcpy(tbuf, ¶mlist[lp]);
nocr(tbuf);
if( !strcmp(tbuf, "-help") ){
if( cmd->help != NULL )
snprintf(tbuf, SZ_LINE, "%s\n", cmd->help);
else
snprintf(tbuf, SZ_LINE, "\n");
send(xpa_datafd(xpa), tbuf, strlen(tbuf), 0);
got = 0;
}
else if( cmd->send_callback != NULL ){
got = (cmd->send_callback)(cmd->send_data, call_data,
¶mlist[lp], buf, len);
}
else{
XPAError(xpa, xpaMessbuf[XPA_RTN_NOSEND]);
got = -1;
goto done;
}
}
else{
XPAError(xpa, xpaMessbuf[XPA_RTN_UNCMD]);
got = -1;
goto done;
}
done:
/* return error code */
return(got);
}
/*
*----------------------------------------------------------------------------
*
*
* Public Routines and Data
*
*
*----------------------------------------------------------------------------
*/
/*
*----------------------------------------------------------------------------
*
* Routine: XPACmdNew
*
* Purpose: define a named command access point
*
* Results: XPA struct or NULL
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
XPA
XPACmdNew (char *xclass, char *name)
#else
XPA XPACmdNew(xclass, name)
char *xclass;
char *name;
#endif
{
XPA xpa;
char tbuf[SZ_LINE];
/* we need a name */
if( (name == NULL) || (*name == '\0') )
return(NULL);
/* we need some valid class */
if( (xclass == NULL) || (*xclass == '\0') )
xclass = "*";
/* help string */
snprintf(tbuf, SZ_LINE, "XPA commands for %s:%s\n\t\t options: see -help",
xclass, name);
/* create a new XPA with command callbacks */
xpa = XPANew(xclass, name, tbuf,
XPASendCommands, NULL, NULL,
XPAReceiveCommands, NULL, "fillbuf=false");
/* return the good news */
return(xpa);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPACmdAdd
*
* Purpose: add a command to a named command access point
*
* Results: 0 on success, -1 for failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
XPACmd
XPACmdAdd (XPA xpa, char *name, char *help,
SendCb send_callback, void *send_data, char *send_mode,
ReceiveCb rec_callback, void *rec_data, char *rec_mode)
#else
XPACmd XPACmdAdd(xpa, name, help,
send_callback, send_data, send_mode,
rec_callback, rec_data, rec_mode)
XPA xpa;
char *name;
char *help;
SendCb send_callback;
void *send_data;
char *send_mode;
ReceiveCb rec_callback;
void *rec_data;
char *rec_mode;
#endif
{
XPACmd xnew;
XPACmd cur;
XPACmd prev;
/* make sure we have a valid command record */
if( !xpa ||
(xpa->send_callback != XPASendCommands) ||
(xpa->receive_callback != XPAReceiveCommands) ){
return(NULL);
}
/* we need either a send or a receive or both */
if( (send_callback == NULL) && (rec_callback == NULL ) ){
return(NULL);
}
/* limit the size of the cmd name designation */