Debian Codegraph @

xpa/command.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 #include <xpap.h>
6
7 /*
8 *----------------------------------------------------------------------------
9 *
10 *
11 * Private Routines and Data
12 *
13 *
14 *----------------------------------------------------------------------------
15 */
16
17 /* this is the static xpa struct that holds the reserved commands */
18 static XPA rxpa=NULL;
19
20 /*
21 *----------------------------------------------------------------------------
22 *
23 * Routine: XPACmdParseNames
24 *
25 * Purpose: massage a name string, changing multiple sequential spaces
26 * into a single space
27 *
28 * Returns: new name, with spaces massaged (also number of spacess)
29 *
30 *----------------------------------------------------------------------------
31 */
32 #ifdef ANSI_FUNC
33 static char *
34 XPACmdParseNames(char *lbuf, int *ntokens)
35 #else
36 static char *XPACmdParseNames(lbuf, ntokens)
37 char *lbuf;
38 int *ntokens;
39 #endif
40 {
41 char tbuf[SZ_LINE];
42 int lp=0;
43 char *buf;
44
45 /* can't be larger than the original string */
46 buf = (char *)xmalloc(strlen(lbuf)+1);
47 *buf = '\0';
48 *ntokens = 0;
49
50 /* pick off each word, separating by a single space */
51 while( word(lbuf, tbuf, &lp)){
52 if( *buf != '\0' )
53 strcat(buf, " ");
54 strcat(buf, tbuf);
55 *ntokens += 1;
56 }
57
58 /* make the string the right size */
59 buf = (char *)xrealloc(buf, strlen(buf)+1);
60 return(buf);
61 }
62
63 /*
64 *----------------------------------------------------------------------------
65 *
66 * Routine: XPAReceiveNSConnect
67 *
68 * Purpose: reset and re-establish connection to name server
69 *
70 * Returns: xpa callback error codes
71 *
72 *----------------------------------------------------------------------------
73 */
74 #ifdef ANSI_FUNC
75 static int
76 XPAReceiveNSConnect (void *client_data, void *call_data, char *paramlist,
77 char *buf, size_t len)
78 #else
79 static int XPAReceiveNSConnect(client_data, call_data, paramlist, buf, len)
80 void *client_data;
81 void *call_data;
82 char *paramlist;
83 char *buf;
84 size_t len;
85 #endif
86 {
87 XPA xpa = (XPA)call_data;
88 XPA txpa;
89 char tbuf[SZ_LINE];
90 int doall=0;
91 int lp=0;
92
93 if( paramlist && *paramlist ){
94 if( word(paramlist, tbuf, &lp) && !strcmp(tbuf, "-all") ){
95 doall = 1;
96 }
97 }
98 if( doall ){
99 for(txpa=XPAListHead(); txpa!=NULL; txpa=txpa->next){
100 XPANSAdd(txpa, NULL, NULL);
101 }
102 }
103 else{
104 XPANSAdd(xpa, NULL, NULL);
105 }
106 return(0);
107 }
108
109 /*
110 *----------------------------------------------------------------------------
111 *
112 * Routine: XPAReceiveNSDisconnect
113 *
114 * Purpose: break connection to name server
115 *
116 * Returns: xpa callback error codes
117 *
118 *----------------------------------------------------------------------------
119 */
120 #ifdef ANSI_FUNC
121 static int
122 XPAReceiveNSDisconnect (void *client_data, void *call_data, char *paramlist,
123 char *buf, size_t len)
124 #else
125 static int XPAReceiveNSDisconnect(client_data, call_data, paramlist, buf, len)
126 void *client_data;
127 void *call_data;
128 char *paramlist;
129 char *buf;
130 size_t len;
131 #endif
132 {
133 XPA xpa = (XPA)call_data;
134 XPA txpa;
135 NS ns, tns;
136 char tbuf[SZ_LINE];
137 int doall=0;
138 int lp=0;
139
140 if( paramlist && *paramlist ){
141 if( word(paramlist, tbuf, &lp) && !strcmp(tbuf, "-all") ){
142 doall = 1;
143 }
144 }
145 if( doall ){
146 for(txpa=XPAListHead(); txpa!=NULL; txpa=txpa->next){
147 for(ns=txpa->nshead; ns!= NULL; ){
148 tns = ns->next;
149 XPANSClose(txpa, ns);
150 ns = tns;
151 }
152 }
153 }
154 else{
155 for(ns=xpa->nshead; ns!= NULL; ){
156 tns = ns->next;
157 XPANSClose(xpa, ns);
158 ns = tns;
159 }
160 }
161 return(0);
162 }
163
164 /*
165 *----------------------------------------------------------------------------
166 *
167 * Routine: XPAReceiveEnv
168 *
169 * Purpose: set an environment variable
170 *
171 * Returns: xpa callback error codes
172 *
173 *----------------------------------------------------------------------------
174 */
175 #ifdef ANSI_FUNC
176 static int
177 XPAReceiveEnv (void *client_data, void *call_data, char *paramlist,
178 char *buf, size_t len)
179 #else
180 static int XPAReceiveEnv(client_data, call_data, paramlist, buf, len)
181 void *client_data;
182 void *call_data;
183 char *paramlist;
184 char *buf;
185 size_t len;
186 #endif
187 {
188 XPA xpa = (XPA)call_data;
189 char name[SZ_LINE];
190 char value[SZ_LINE];
191 char *tbuf;
192 int lp=0;
193
194 if( word(paramlist, name, &lp) ){
195 if( word(paramlist, value, &lp) ){
196 tbuf = (char *)xmalloc(strlen(name)+1+strlen(value)+1);
197 snprintf(tbuf, SZ_LINE, "%s=%s", name, value);
198 putenv(tbuf);
199 return(0);
200 }
201 else{
202 if( strchr(name, '=') != NULL ){
203 tbuf = xstrdup(name);
204 putenv(tbuf);
205 return(0);
206 }
207 else{
208 XPAError(xpa, "XPA setenv requires name and value pair\n");
209 return(-1);
210 }
211 }
212 }
213 else{
214 XPAError(xpa, "XPA setenv requires name and value pair\n");
215 return(-1);
216 }
217 }
218
219 /*
220 *----------------------------------------------------------------------------
221 *
222 * Routine: XPASendEnv
223 *
224 * Purpose: return an environment variable to client
225 *
226 * Returns: xpa callback error codes
227 *
228 *----------------------------------------------------------------------------
229 */
230 #ifdef ANSI_FUNC
231 static int
232 XPASendEnv (void *client_data, void *call_data, char *paramlist,
233 char **buf, size_t *len)
234 #else
235 static int XPASendEnv(client_data, call_data, paramlist, buf, len)
236 void *client_data;
237 void *call_data;
238 char *paramlist;
239 char **buf;
240 size_t *len;
241 #endif
242 {
243 int tlen;
244 char *tbuf;
245 char *ebuf;
246
247 if( (ebuf = (char *)getenv(paramlist)) != NULL ){
248 tlen = strlen(ebuf)+2;
249 tbuf = (char *)xmalloc(tlen);
250 snprintf(tbuf, tlen, "%s\n", ebuf);
251 *buf = tbuf;
252 *len = strlen(tbuf);
253 }
254 else{
255 *buf = xstrdup("\n");
256 *len = 1;
257 }
258 return(0);
259 }
260
261 /*
262 *----------------------------------------------------------------------------
263 *
264 * Routine: XPAReceiveReserved
265 *
266 * Purpose: execute reserved command
267 *
268 * Returns: xpa callback error codes
269 *
270 *----------------------------------------------------------------------------
271 */
272 #ifdef ANSI_FUNC
273 static int
274 XPAReceiveReserved (void *client_data, void *call_data, char *paramlist,
275 char *buf, size_t len)
276 #else
277 static int XPAReceiveReserved(client_data, call_data, paramlist, buf, len)
278 void *client_data;
279 void *call_data;
280 char *paramlist;
281 char *buf;
282 size_t len;
283 #endif
284 {
285 char *cmd = (char *)client_data;
286 XPA xpa = (XPA)call_data;
287
288 if( !strcmp(cmd, "end") ){
289 xpa->comm->status |= XPA_STATUS_ENDBUF;
290 return(0);
291 }
292 else if( !strcmp(cmd, "exec") ){
293 xpa->comm->status |= XPA_STATUS_READBUF;
294 return(0);
295 }
296 else{
297 return(-1);
298 }
299 }
300
301 /*
302 *----------------------------------------------------------------------------
303 *
304 * Routine: XPASendHelp
305 *
306 * Purpose: send help strings
307 *
308 * Returns: xpa callback error codes
309 *
310 *----------------------------------------------------------------------------
311 */
312 #ifdef ANSI_FUNC
313 static int
314 XPASendHelp (void *client_data, void *call_data, char *paramlist,
315 char **buf, size_t *len)
316 #else
317 static int XPASendHelp(client_data, call_data, paramlist, buf, len)
318 void *client_data;
319 void *call_data;
320 char *paramlist;
321 char **buf;
322 size_t *len;
323 #endif
324 {
325 XPA xpa = (XPA)call_data;
326 XPACmd cmd;
327 int lp=0;
328 int slen;
329 char tbuf[SZ_LINE];
330 char lbuf[SZ_LINE];
331 char *sbuf;
332
333 if( !paramlist || !*paramlist ){
334 if( xpa->version != NULL ){
335 snprintf(lbuf, SZ_LINE, "XPA version: %s\n", xpa->version);
336 send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0);
337 }
338 }
339 if( xpa->commands == NULL ){
340 if( xpa->help != NULL ){
341 slen = strlen(xpa->help)+SZ_LINE;
342 sbuf = (char *)xmalloc(slen);
343 snprintf(sbuf, slen, "%s\n", xpa->help);
344 send(xpa_datafd(xpa), sbuf, strlen(sbuf), 0);
345 xfree(sbuf);
346 }
347 else{
348 strcpy(lbuf, "\n");
349 send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0);
350 }
351 }
352 else{
353 if( paramlist && *paramlist ){
354 while( word(paramlist, tbuf, &lp) ){
355 for(cmd=xpa->commands; cmd!=NULL; cmd=cmd->next){
356 if( !strcmp(tbuf, cmd->name) ){
357 if( cmd->help != NULL ){
358 slen = strlen(cmd->name)+strlen(cmd->help)+SZ_LINE;
359 sbuf = (char *)xmalloc(slen);
360 snprintf(sbuf, slen, "%s:\t%s\n", cmd->name, cmd->help);
361 send(xpa_datafd(xpa), sbuf, strlen(sbuf), 0);
362 xfree(sbuf);
363 }
364 else{
365 snprintf(lbuf, SZ_LINE, "%s:\t(no help available)\n", cmd->name);
366 send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0);
367 }
368 }
369 }
370 }
371 }
372 else{
373 for(cmd=xpa->commands; cmd!=NULL; cmd=cmd->next){
374 if( cmd->help != NULL ){
375 slen = strlen(cmd->name)+strlen(cmd->help)+SZ_LINE;
376 sbuf = (char *)xmalloc(slen);
377 snprintf(sbuf, slen, "%s:\t%s\n", cmd->name, cmd->help);
378 send(xpa_datafd(xpa), sbuf, strlen(sbuf), 0);
379 xfree(sbuf);
380 }
381 else{
382 snprintf(lbuf, SZ_LINE, "%s:\t(no help available)\n", cmd->name);
383 send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0);
384 }
385 }
386 }
387 }
388 return(0);
389 }
390
391 /*
392 *----------------------------------------------------------------------------
393 *
394 * Routine: XPASendVersion
395 *
396 * Purpose: send XPA version string
397 *
398 * Returns: xpa callback error codes
399 *
400 *----------------------------------------------------------------------------
401 */
402 #ifdef ANSI_FUNC
403 static int
404 XPASendVersion (void *client_data, void *call_data, char *paramlist,
405 char **buf, size_t *len)
406 #else
407 static int XPASendVersion(client_data, call_data, paramlist, buf, len)
408 void *client_data;
409 void *call_data;
410 char *paramlist;
411 char **buf;
412 size_t *len;
413 #endif
414 {
415 XPA xpa = (XPA)call_data;
416 char lbuf[SZ_LINE];
417
418 if( xpa->version != NULL )
419 snprintf(lbuf, SZ_LINE, "%s\n", xpa->version);
420 else
421 strcpy(lbuf, "\n");
422 send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0);
423 return(0);
424 }
425
426 /*
427 *----------------------------------------------------------------------------
428 *
429 *
430 * Semi-Public Routines and Data
431 *
432 * These routines are used by XPAHandler and XPANew
433 *
434 *
435 *----------------------------------------------------------------------------
436 */
437
438 /*
439 *----------------------------------------------------------------------------
440 *
441 * Routine: XPAInitReserved
442 *
443 * Purpose: add the reserved commands to the reserved xpa struct
444 *
445 * Results: none
446 *
447 *----------------------------------------------------------------------------
448 */
449 #ifdef ANSI_FUNC
450 void
451 XPAInitReserved (void)
452 #else
453 void XPAInitReserved()
454 #endif
455 {
456 if( !rxpa ){
457 if( (rxpa = (XPA)xcalloc(1, sizeof(struct xparec))) == NULL )
458 return;
459 /* XPACmdAdd requires that the callbacks be defined explicitly in rxpa */
460 rxpa->send_callback = XPASendCommands;
461 rxpa->receive_callback = XPAReceiveCommands;
462 /* add reserved commands */
463 XPACmdAdd(rxpa, "-acl",
464 "\tget (set) the access control list\n\t\t options: host type acl",
465 XPASendAcl, NULL, NULL, XPAReceiveAcl, NULL, "fillbuf=false");
466 XPACmdAdd(rxpa, "-env",
467 "\tget (set) an environment variable\n\t\t options: name (value)",
468 XPASendEnv, NULL, NULL, XPAReceiveEnv, NULL, NULL);
469 XPACmdAdd(rxpa, "-exec",
470 "\texecute commands from buffer\n\t\t options: none",
471 NULL, NULL, NULL, XPAReceiveReserved, (void *)"exec", NULL);
472 XPACmdAdd(rxpa, "-help",
473 "\treturn help string for specified XPA\n\t\t options: cmd name (commands only)",
474 XPASendHelp, NULL, NULL, NULL, NULL, NULL);
475 XPACmdAdd(rxpa, "-ltimeout",
476 "\tget (set) long timeout\n\t\t options: seconds|reset",
477 XPASendLTimeout, NULL, NULL, XPAReceiveLTimeout, NULL, NULL);
478 XPACmdAdd(rxpa, "-nsconnect",
479 "\tre-establish name server connection to this XPA\n\t\t options: -all",
480 NULL, NULL, NULL, XPAReceiveNSConnect, NULL, NULL);
481 XPACmdAdd(rxpa, "-nsdisconnect",
482 "\tbreak name server connection to this XPA\n\t\t options: -all",
483 NULL, NULL, NULL, XPAReceiveNSDisconnect, NULL, NULL);
484 XPACmdAdd(rxpa, "-remote",
485 "\tconnect to remote name service with specified acl \n\t\t options: host:port +|-|acl -proxy",
486 XPASendRemote, NULL, NULL, XPAReceiveRemote, NULL, "fillbuf=false");
487 XPACmdAdd(rxpa, "-clipboard",
488 "\tset/get clipboard information \n\t\t options: [cmd] name",
489 XPASendClipboard, NULL, NULL, XPAReceiveClipboard, NULL, NULL);
490 XPACmdAdd(rxpa, "-stimeout",
491 "\tget (set) short timeout\n\t\t options: seconds|reset",
492 XPASendSTimeout, NULL, NULL, XPAReceiveSTimeout, NULL, NULL);
493 XPACmdAdd(rxpa, "-version",
494 "\treturn XPA version string\n\t\t options: none",
495 XPASendVersion, NULL, NULL, NULL, NULL, NULL);
496 }
497 }
498
499 #ifdef ANSI_FUNC
500 void
501 XPAFreeReserved (void)
502 #else
503 void XPAFreeReserved()
504 #endif
505 {
506 XPACmd cmd, tcmd;
507 if( !rxpa ) return;
508 /* free reserved commands */
509 for(cmd=rxpa->commands; cmd!=NULL; ){
510 tcmd = cmd->next;
511 XPACmdDel(rxpa, cmd);
512 cmd = tcmd;
513 }
514 xfree(rxpa);
515 rxpa = NULL;
516 }
517
518 /*
519 *----------------------------------------------------------------------------
520 *
521 * Routine: XPACmdLookupReserved
522 *
523 * Purpose: lookup a reserved command name
524 *
525 * Results: cmd struct or null
526 *
527 *----------------------------------------------------------------------------
528 */
529 #ifdef ANSI_FUNC
530 XPACmd
531 XPACmdLookupReserved (XPA xpa, char *lbuf, int *lp)
532 #else
533 XPACmd XPACmdLookupReserved(xpa, lbuf, lp)
534 XPA xpa;
535 char *lbuf;
536 int *lp;
537 #endif
538 {
539 XPACmd cmd;
540 int lp2=0;
541 char *lptr;
542 char name[SZ_LINE];
543
544 /* make sure we have something to work with */
545 if( (rxpa==NULL) || (lbuf==NULL) || (lbuf[*lp]=='\0') )
546 return(NULL);
547
548 /* this is where we start parsing */
549 lptr = &(lbuf[*lp]);
550
551 /* to simplify life, we assume reserved words are 1 token */
552 if( !word(lptr, name, &lp2) )
553 return(NULL);
554
555 /* look for reserved keywords that have callbacks */
556 for(cmd=rxpa->commands; cmd!=NULL; cmd=cmd->next){
557 if( !strcmp(name, cmd->name) ){
558 *lp += lp2;
559 return(cmd);
560 }
561 }
562
563 /* nothing, nowhere */
564 return(NULL);
565 }
566
567 /*
568 *----------------------------------------------------------------------------
569 *
570 * Routine: XPACmdLookup
571 *
572 * Purpose: lookup a user-defined command name
573 *
574 * Results: cmd struct or null
575 *
576 *----------------------------------------------------------------------------
577 */
578 #ifdef ANSI_FUNC
579 XPACmd
580 XPACmdLookup (XPA xpa, char *lbuf, int *lp)
581 #else
582 XPACmd XPACmdLookup(xpa, lbuf, lp)
583 XPA xpa;
584 char *lbuf;
585 int *lp;
586 #endif
587 {
588 XPACmd cmd;
589 int i;
590 int lp2;
591 int len, tlen;
592 char *lptr;
593 char tbuf[SZ_LINE];
594 char name[SZ_LINE];
595
596 /* make sure we have something to work with */
597 if( (xpa==NULL) || (lbuf==NULL) || (lbuf[*lp]=='\0') )
598 return(NULL);
599
600 /* this is where we start parsing */
601 lptr = &(lbuf[*lp]);
602
603 /* look up commands for this name */
604 for(cmd=xpa->commands; cmd!=NULL; cmd=cmd->next){
605 /* make up a name with the required number of tokens for this command */
606 *name = '\0';
607 lp2 = 0;
608 tlen = 0;
609 for(i=0; i<cmd->ntokens; i++){
610 if( word(lptr, tbuf, &lp2)){
611 len = strlen(tbuf)+1;
612 if( (tlen+len) <= (SZ_LINE-1) ){
613 if( *name != '\0' )
614 strcat(name, " ");
615 strcat(name, tbuf);
616 tlen += len;
617 }
618 /* not enough room */
619 else{
620 *name = '\0';
621 break;
622 }
623 }
624 }
625 /* we now have the name, see if its what we want */
626 if( *name && !strcmp(cmd->name, name) ){
627 *lp += lp2;
628 return(cmd);
629 }
630 }
631
632 /* did not find the command in this xpa -- now look through reserved */
633 return(XPACmdLookupReserved(xpa, lbuf, lp));
634 }
635
636 /*
637 *----------------------------------------------------------------------------
638 *
639 * Routine: XPAReceiveCommands
640 *
641 * Purpose: process a list of commands from xpaset
642 *
643 * Results: number of commands processed
644 *
645 *----------------------------------------------------------------------------
646 */
647 #ifdef ANSI_FUNC
648 int
649 XPAReceiveCommands (void *client_data, void *call_data, char *paramlist,
650 char *buf, size_t len)
651 #else
652 int XPAReceiveCommands(client_data, call_data, paramlist, buf, len)
653 void *client_data;
654 void *call_data;
655 char *paramlist;
656 char *buf;
657 size_t len;
658 #endif
659 {
660 XPA xpa = (XPA)call_data;
661 XPACmd cmd;
662 int lp;
663 int savelp;
664 int plen;
665 int bgot;
666 int got=0;
667 int gotbuf=0;
668 int freebuf=1;
669 char lbuf[SZ_LINE];
670 char tbuf[SZ_LINE];
671 char tbuf1[SZ_LINE];
672
673 /* use ";" as a command separator (as well as \n) */
674 newdtable(";");
675
676 /* if we already have buf, there will be no need to get it */
677 if( buf )
678 gotbuf++;
679
680 /* if we have no paramlist, we read from the socket */
681 if( (xpa_datafd(xpa) >=0) && (!paramlist || (*paramlist == '\0')) ){
682 xpa->comm->status |= XPA_STATUS_READBUF;
683 XPAGets(xpa, xpa_datafd(xpa), lbuf, SZ_LINE, XPALongTimeout());
684 FPRINTF((stderr, "%sXPAReceiveCommands: read %s\n", _sp, lbuf));
685 }
686 else{
687 xpa->comm->status &= ~XPA_STATUS_READBUF;
688 nowhite(paramlist, lbuf);
689 }
690
691 /* we must have something to start with */
692 if( *lbuf == '\0' ){
693 XPAError(xpa, xpaMessbuf[XPA_RTN_NOCMD2]);
694 got = -1;
695 goto done;
696 }
697
698 /* This look either executes the one set of commands in paramlist,
699 or reads one line at a time from the socket and executes commands.
700 In the latter case, each callback can read the socket as well */
701 while( 1 ){
702 FPRINTF((stderr, "%sXPAReceiveCommands: top of loop: %s\n", _sp, lbuf));
703 lp = 0;
704 while( lbuf[lp] != '\0' ){
705 if( (cmd = XPACmdLookup(xpa, lbuf, &lp)) == NULL ){
706 XPAError(xpa, xpaMessbuf[XPA_RTN_UNCMD]);
707 got = -1;
708 goto done;
709 }
710 /* reserved commands can only be called from the same host as the server,
711 or from local (unix) sockets */
712 if( (cmd->xpa == rxpa) &&
713 strcmp(cmd->name, "-help") && strcmp(cmd->name, "-version") ){
714 if( XPAMtype() == XPA_INET ){
715 if( (!xpa->comm || !LOCALIP(xpa->comm->cmdip)) ){
716 FPRINTF((stderr, "%sXPAReceiveCommands: rejecting reserved: %s\n",
717 _sp, cmd->name));
718 XPAError(xpa, xpaMessbuf[XPA_RTN_NOAUTH]);
719 got = -1;
720 goto done;
721 }
722 }
723 }
724 FPRINTF((stderr, "%sXPAReceiveCommands: cmd->name: %s\n",
725 _sp, cmd->name));
726 *tbuf = '\0';
727 if( (lastdelim() != ';') && (lastdelim() != '\n') ){
728 /* skip white space between command and params */
729 while( isspace((int)lbuf[lp]) )
730 lp++;
731 /* here is where the params start */
732 savelp = lp;
733 /* look for command delimiter -- the end of the params */
734 while( word(lbuf, tbuf1, &lp) ){
735 if( (lastdelim() == ';') || (lastdelim() == '\n') ){
736 break;
737 }
738 /* make sure a command-ending delim is not next */
739 while( isspace((int)lbuf[lp]) )
740 lp++;
741 if( (lbuf[lp] == ';') || (lbuf[lp] == '\n') ){
742 break;
743 }
744 }
745 /* get length of parameter list */
746 plen = lp - savelp;
747 /* but skip final delim */
748 if( (plen>0) && ((lastdelim() == ';')||(lastdelim() == '\n')) )
749 plen--;
750 /* copy the params up to the command delimiter */
751 if( plen > 0 ){
752 strncpy(tbuf, &(lbuf[savelp]), plen);
753 tbuf[plen] = '\0';
754 }
755 }
756 /* execute the associated XPA callback */
757 if( cmd->receive_callback != NULL ){
758 /* get buf now, if its needed */
759 if( !gotbuf && (xpa_datafd(xpa) >= 0) &&
760 (cmd->receive_mode & XPA_MODE_FILLBUF) ){
761 /* read buf -- this buf will stay around for all commands */
762 FPRINTF((stderr, "%sXPAReceiveCommands: at XPAGetBuf\n", _sp));
763 bgot = XPAGetBuf(xpa, xpa_datafd(xpa), &buf, &len, -1);
764 /* close the data channel */
765 XPACloseData(xpa, xpa->comm);
766 if( bgot >= 0 ){
767 /* got the buffer */
768 gotbuf++;
769 }
770 /* error getting buf */
771 else{
772 XPAError(xpa, xpaMessbuf[XPA_RTN_NODATA]);
773 got = -1;
774 goto done;
775 }
776 }
777 got = (cmd->receive_callback)(cmd->receive_data, call_data,
778 tbuf, buf, len);
779 /* if we don't want to free the buffer, mark it for saving */
780 if( (buf != NULL) && !(cmd->receive_mode & XPA_MODE_FREEBUF) ){
781 freebuf=0;
782 }
783 if( got != 0 ){
784 goto done;
785 }
786 }
787 else{
788 XPAError(xpa, xpaMessbuf[XPA_RTN_NOREC]);
789 got = -1;
790 goto done;
791 }
792 }
793 /* conditions for exiting the command loop: */
794 /* if we processed the END command, we are done */
795 if( xpa->comm->status & XPA_STATUS_ENDBUF )
796 break;
797 /* if we are not reading the data buffer, we are done */
798 if( !(xpa->comm->status & XPA_STATUS_READBUF) )
799 break;
800 /* if we got EOF or error reading the data buffer, we are done */
801 if( XPAGets(xpa, xpa_datafd(xpa), lbuf, SZ_LINE, XPALongTimeout()) <=0 )
802 break;
803 }
804
805 done:
806 /* if no one wants buf, free it now */
807 if( freebuf )
808 xfree(buf);
809 /* restore last delimiter table */
810 freedtable();
811 /* return error code */
812 return(got);
813 }
814
815 /*
816 *----------------------------------------------------------------------------
817 *
818 * Routine: XPASendCommands
819 *
820 * Purpose: process a list of commands from xpaget
821 *
822 * Results: number of commands processed (currently 0 or 1)
823 *
824 *----------------------------------------------------------------------------
825 */
826 #ifdef ANSI_FUNC
827 int
828 XPASendCommands (void *client_data, void *call_data, char *paramlist,
829 char **buf, size_t *len)
830 #else
831 int XPASendCommands(client_data, call_data, paramlist, buf, len)
832 void *client_data;
833 void *call_data;
834 char *paramlist;
835 char **buf;
836 size_t *len;
837 #endif
838 {
839 XPA xpa = (XPA)call_data;
840 XPACmd cmd;
841 char tbuf[SZ_LINE];
842 int lp=0;
843 int got=0;
844
845 /* return help as default */
846 if( *paramlist == '\0' ){
847 paramlist = "-help";
848 }
849
850 /* lookup the command and execute */
851 if( (cmd = XPACmdLookup(xpa, paramlist, &lp)) != NULL ){
852 /* reserved commands can only be called from the same host as the server,
853 or from local (unix) sockets */
854 if( (cmd->xpa == rxpa) &&
855 strcmp(cmd->name, "-help") && strcmp(cmd->name, "-version") ){
856 if( XPAMtype() == XPA_INET ){
857 if( (!xpa->comm || !LOCALIP(xpa->comm->cmdip)) ){
858 FPRINTF((stderr, "%sXPAReceiveCommands: rejecting reserved: %s\n",
859 _sp, cmd->name));
860 XPAError(xpa, xpaMessbuf[XPA_RTN_NOAUTH]);
861 got = -1;
862 goto done;
863 }
864 }
865 }
866 /* execute the associated XPA send callback,
867 using the remaining command string as an argument */
868 strcpy(tbuf, &paramlist[lp]);
869 nocr(tbuf);
870 if( !strcmp(tbuf, "-help") ){
871 if( cmd->help != NULL )
872 snprintf(tbuf, SZ_LINE, "%s\n", cmd->help);
873 else
874 snprintf(tbuf, SZ_LINE, "\n");
875 send(xpa_datafd(xpa), tbuf, strlen(tbuf), 0);
876 got = 0;
877 }
878 else if( cmd->send_callback != NULL ){
879 got = (cmd->send_callback)(cmd->send_data, call_data,
880 &paramlist[lp], buf, len);
881 }
882 else{
883 XPAError(xpa, xpaMessbuf[XPA_RTN_NOSEND]);
884 got = -1;
885 goto done;
886 }
887 }
888 else{
889 XPAError(xpa, xpaMessbuf[XPA_RTN_UNCMD]);
890 got = -1;
891 goto done;
892 }
893
894 done:
895 /* return error code */
896 return(got);
897 }
898
899 /*
900 *----------------------------------------------------------------------------
901 *
902 *
903 * Public Routines and Data
904 *
905 *
906 *----------------------------------------------------------------------------
907 */
908
909 /*
910 *----------------------------------------------------------------------------
911 *
912 * Routine: XPACmdNew
913 *
914 * Purpose: define a named command access point
915 *
916 * Results: XPA struct or NULL
917 *
918 *----------------------------------------------------------------------------
919 */
920 #ifdef ANSI_FUNC
921 XPA
922 XPACmdNew (char *xclass, char *name)
923 #else
924 XPA XPACmdNew(xclass, name)
925 char *xclass;
926 char *name;
927 #endif
928 {
929 XPA xpa;
930 char tbuf[SZ_LINE];
931
932 /* we need a name */
933 if( (name == NULL) || (*name == '\0') )
934 return(NULL);
935
936 /* we need some valid class */
937 if( (xclass == NULL) || (*xclass == '\0') )
938 xclass = "*";
939
940 /* help string */
941 snprintf(tbuf, SZ_LINE, "XPA commands for %s:%s\n\t\t options: see -help",
942 xclass, name);
943
944 /* create a new XPA with command callbacks */
945 xpa = XPANew(xclass, name, tbuf,
946 XPASendCommands, NULL, NULL,
947 XPAReceiveCommands, NULL, "fillbuf=false");
948
949 /* return the good news */
950 return(xpa);
951 }
952
953 /*
954 *----------------------------------------------------------------------------
955 *
956 * Routine: XPACmdAdd
957 *
958 * Purpose: add a command to a named command access point
959 *
960 * Results: 0 on success, -1 for failure
961 *
962 *----------------------------------------------------------------------------
963 */
964 #ifdef ANSI_FUNC
965 XPACmd
966 XPACmdAdd (XPA xpa, char *name, char *help,
967 SendCb send_callback, void *send_data, char *send_mode,
968 ReceiveCb rec_callback, void *rec_data, char *rec_mode)
969 #else
970 XPACmd XPACmdAdd(xpa, name, help,
971 send_callback, send_data, send_mode,
972 rec_callback, rec_data, rec_mode)
973 XPA xpa;
974 char *name;
975 char *help;
976 SendCb send_callback;
977 void *send_data;
978 char *send_mode;
979 ReceiveCb rec_callback;
980 void *rec_data;
981 char *rec_mode;
982 #endif
983 {
984 XPACmd xnew;
985 XPACmd cur;
986 XPACmd prev;
987
988 /* make sure we have a valid command record */
989 if( !xpa ||
990 (xpa->send_callback != XPASendCommands) ||
991 (xpa->receive_callback != XPAReceiveCommands) ){
992 return(NULL);
993 }
994
995 /* we need either a send or a receive or both */
996 if( (send_callback == NULL) && (rec_callback == NULL ) ){
997 return(NULL);
998 }
999
1000 /* limit the size of the cmd name designation */
1001 if( strlen(name) > XPA_NAMELEN ){
1002 return(NULL);
1003 }
1004
1005 /* allocate space for a new record */
1006 xnew = (XPACmd)xcalloc(1, sizeof(struct xpacmdrec));
1007
1008 /* backlink to xpa */
1009 xnew->xpa = xpa;
1010
1011 /* fill in the blanks */
1012 xnew->name = XPACmdParseNames(name, &(xnew->ntokens));
1013 xnew->help = xstrdup(help);
1014
1015 /* receive callback */
1016 xnew->send_callback = send_callback;
1017 xnew->send_data = send_data;
1018 xnew->send_mode = XPA_DEF_MODE_SEND;
1019 XPAMode(send_mode, &(xnew->send_mode), "freebuf", XPA_MODE_FREEBUF,1);
1020 /* acl is a global mode */
1021 XPAMode(send_mode, &(xpa->send_mode), "acl", XPA_MODE_ACL, 1);
1022
1023 /* receive callback */
1024 xnew->receive_callback = rec_callback;
1025 xnew->receive_data = rec_data;
1026 /* process the mode string */
1027 xnew->receive_mode = XPA_DEF_MODE_REC;
1028 XPAMode(rec_mode, &(xnew->receive_mode), "usebuf", XPA_MODE_BUF,1);
1029 XPAMode(rec_mode, &(xnew->receive_mode), "fillbuf", XPA_MODE_FILLBUF,1);
1030 XPAMode(rec_mode, &(xnew->receive_mode), "freebuf", XPA_MODE_FREEBUF,1);
1031 /* acl is a global mode */
1032 XPAMode(rec_mode, &(xpa->receive_mode), "acl", XPA_MODE_ACL, 1);
1033
1034 /* enter into list, in alphabetical order */
1035 if( xpa->commands == NULL ){
1036 xpa->commands = xnew;
1037 return(xnew);
1038 }
1039 else{
1040 for(prev=NULL, cur=xpa->commands; cur!=NULL; prev=cur, cur=cur->next){
1041 /* if new name is an existing name, add it before */
1042 if( strcmp(xnew->name, cur->name) ==0 )
1043 goto addname;
1044 /* if existing name is a subset of new name, add it before */
1045 else if( !strncmp(xnew->name, cur->name, strlen(cur->name)) )
1046 goto addname;
1047 /* if new name is a subset of existing name, add it after */
1048 else if( !strncmp(xnew->name, cur->name, strlen(xnew->name)) )
1049 continue;
1050 /* if new name is lexically greater than existing name, add it after */
1051 else if( strcmp(xnew->name, cur->name) >0 )
1052 continue;
1053 addname:
1054 /* add the new name here and return */
1055 if( prev == NULL ){
1056 xpa->commands = xnew;
1057 xnew->next = cur;
1058 return(xnew);
1059 }
1060 else{
1061 prev->next = xnew;
1062 xnew->next = cur;
1063 return(xnew);
1064 }
1065 }
1066 /* if we are still here, we did not find a place to insert, i.e.,
1067 we are at the end of the list */
1068 prev->next = xnew;
1069 return(xnew);
1070 }
1071 }
1072
1073 /*
1074 *----------------------------------------------------------------------------
1075 *
1076 * Routine: XPACmdDel
1077 *
1078 * Purpose: free a named command access point
1079 *
1080 * Results: none
1081 *
1082 *----------------------------------------------------------------------------
1083 */
1084 #ifdef ANSI_FUNC
1085 int
1086 XPACmdDel (XPA xpa, XPACmd cmd)
1087 #else
1088 int XPACmdDel(xpa, cmd)
1089 XPA xpa;
1090 XPACmd cmd;
1091 #endif
1092 {
1093 XPACmd cur;
1094 int got=0;
1095
1096 /* gotta have something to free */
1097 if( cmd == NULL )
1098 return(-1);
1099
1100 /* remove from list of xpa's commands */
1101 if( xpa && xpa->commands ){
1102 if( xpa->commands == cmd ){
1103 xpa->commands = cmd->next;
1104 got++;
1105 }
1106 else{
1107 for(cur=xpa->commands; cur!=NULL; cur=cur->next){
1108 if( cur->next == cmd ){
1109 cur->next = cmd->next;
1110 got++;
1111 break;
1112 }
1113 }
1114 }
1115 }
1116
1117 /* free up space */
1118 if( got ){
1119 if( cmd->name )
1120 xfree(cmd->name);
1121 if( cmd->help )
1122 xfree(cmd->help);
1123 xfree(cmd);
1124 return(0);
1125 }
1126 else{
1127 return(-1);
1128 }
1129 }
1130
1131 /*
1132 *----------------------------------------------------------------------------
1133 *
1134 * Routine: XPACmdInternalReceive
1135 *
1136 * Purpose: internal execute of the receive callback for this command
1137 *
1138 * Results: number of commands processed
1139 *
1140 *----------------------------------------------------------------------------
1141 */
1142 #ifdef ANSI_FUNC
1143 int
1144 XPACmdInternalReceive (void *client_data, void *call_data,
1145 char *paramlist, char *buf, size_t len)
1146 #else
1147 int XPACmdInternalReceive(client_data, call_data, paramlist, buf, len)
1148 void *client_data;
1149 void *call_data;
1150 char *paramlist;
1151 char *buf;
1152 size_t len;
1153 #endif
1154 {
1155 XPA xpa = (XPA)call_data;
1156
1157 /* make sure we have something */
1158 if( xpa == NULL )
1159 return(-1);
1160 /* make the call */
1161 return(XPAReceiveCommands(client_data, xpa, paramlist, buf, len));
1162 }
1163
1164 /*
1165 *----------------------------------------------------------------------------
1166 *
1167 * Routine: XPACmdInternalSend
1168 *
1169 * Purpose: internal execute of the send callback for this command
1170 *
1171 * Results: number of commands processed (currently 0 or 1)
1172 *
1173 *----------------------------------------------------------------------------
1174 */
1175 #ifdef ANSI_FUNC
1176 int
1177 XPACmdInternalSend (void *client_data, void *call_data,
1178 char *paramlist, char **buf, size_t *len)
1179 #else
1180 int XPACmdInternalSend(client_data, call_data, paramlist, buf, len)
1181 void *client_data;
1182 void *call_data;
1183 char *paramlist;
1184 char **buf;
1185 size_t *len;
1186 #endif
1187 {
1188 XPA xpa = (XPA)call_data;
1189
1190 /* make sure we have something */
1191 if( xpa == NULL )
1192 return(-1);
1193
1194 /* make the call */
1195 return(XPASendCommands(client_data, xpa, paramlist, buf, len));
1196 }
1197
1198 /*
1199 *----------------------------------------------------------------------------
1200 *
1201 * Routine: XPAGetReserved
1202 *
1203 * Purpose: return xpa handle for reserved xpa commands
1204 *
1205 * Return: xpa handle
1206 *
1207 *----------------------------------------------------------------------------
1208 */
1209 #ifdef ANSI_FUNC
1210 XPA
1211 XPAGetReserved (void)
1212 #else
1213 XPA XPAGetReserved()
1214 #endif
1215 {
1216 return(rxpa);
1217 }