Debian Codegraph @

xpa/stest.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 /*
6 *
7 * stest -- server test for xpa
8 *
9 */
10 #include <xpap.h>
11
12 #ifndef BUILD_WITH_XT
13 #if HAVE_XT
14 #undef HAVE_XT
15 #endif
16 #endif
17
18 #if HAVE_XT
19 #include <X11/Intrinsic.h>
20 #include <X11/StringDefs.h>
21 #endif
22
23 #define MAX_FPS 10
24
25 extern char *optarg;
26 extern int optind;
27
28 XPA xpa1, xpa1a, xpa2, xpa3;
29 int quiet=0;
30 int dowait=0;
31 int dofill=0;
32 int dosave=1;
33 int doone=0;
34 int domyfree=0;
35 int doatexit=0;
36 int n=0;
37 int please_exit=0;
38 size_t save_bytes=-1;
39 char *save_buf=NULL;
40
41 char *mode="";
42 char name[SZ_LINE];
43 char xclass[SZ_LINE];
44
45 #ifdef ANSI_FUNC
46 void myfree(void *buf)
47 #else
48 void myfree(buf)
49 void *buf;
50 #endif
51 {
52 fprintf(stderr, "myfree calling xfree(%p)\n", buf);
53 xfree(buf);
54 }
55
56 #ifdef ANSI_FUNC
57 int
58 send_cb (void *client_data, void *call_data, char *paramlist,
59 char **buf, size_t *len)
60 #else
61 int send_cb(client_data, call_data, paramlist, buf, len)
62 void *client_data;
63 void *call_data;
64 char *paramlist;
65 char **buf;
66 size_t *len;
67 #endif
68 {
69 char *s = (char *)client_data;
70 XPA xpa = (XPA)call_data;
71 char tbuf[SZ_LINE];
72 char xtemplate[SZ_LINE];
73 char *names[MAX_FPS];
74 char *bufs[MAX_FPS];
75 char *errs[MAX_FPS];
76 size_t lens[MAX_FPS];
77 int sendbuf=0;
78 int got;
79 int i;
80 int ip;
81
82 /* set the free routine */
83 if( domyfree ) XPASetFree(xpa, myfree, NULL);
84
85 /* introduce ourselves */
86 if( !quiet ){
87 fprintf(stdout, "SEND_CB #%d: %s:%s %s (%s)\n",
88 n++, xpa_class(xpa), xpa_name(xpa), xpa_method(xpa), s);
89 }
90
91 /* process special paramlist tokens */
92 if( paramlist && *paramlist ){
93 ip = 0;
94 word(paramlist, xtemplate, &ip);
95 if( !quiet )
96 fprintf(stdout, "\tparamlist: %s\n", paramlist);
97 if( !strncmp(paramlist, "buf", 3) ){
98 sendbuf=1;
99 }
100 else if( !strcmp(paramlist, "free") ){
101 if( !quiet )
102 fprintf(stdout, "Freeing xpa struct\n");
103 XPAFree(xpa);
104 return(0);
105 }
106 else if( !strcmp(paramlist, "exit") ){
107 if( !quiet )
108 fprintf(stdout, "Exiting\n");
109 if( doatexit){
110 please_exit = 1;
111 } else {
112 XPAFree(xpa1);
113 if( !doone ){
114 XPAFree(xpa1a);
115 XPAFree(xpa2);
116 XPAFree(xpa3);
117 }
118 }
119 }
120 else if( !strcmp(paramlist, "Exit") ){
121 if( !quiet )
122 fprintf(stdout, "Exiting immediately\n");
123 exit(0);
124 }
125 else if( !strncmp(paramlist, "error", 5) ){
126 if( !quiet )
127 fprintf(stdout, "\treturning error: %s\n", &paramlist[6]);
128 *len = 0;
129 *buf = NULL;
130 if( strlen(paramlist) > 6 )
131 XPAError(xpa, &paramlist[6]);
132 else
133 XPAError(xpa, "intentional error from client");
134 return(-1);
135 }
136 else if( !strcmp(paramlist, "wait") || !strcmp(paramlist, "version") ){
137 fprintf(stdout, "Press <CR> to continue ...");
138 fgets(tbuf, SZ_LINE, stdin);
139 }
140 else if( !strcmp(paramlist, "poll") ){
141 repoll:
142 fprintf(stdout, "Press 'q' to quit ...");
143 got = XPAPoll (10000, 1);
144 fprintf(stdout, " XPAPoll returns %d ...", got);
145 fgets(tbuf, SZ_LINE, stdin);
146 if( *tbuf != 'q' )
147 goto repoll;
148 }
149 else if( !strncmp(paramlist, "fork ", 5) ){
150 #if HAVE_MINGW32==0
151 if( strlen(paramlist) > 5 ){
152 fprintf(stdout, "fork command: %s\n", &paramlist[5]);
153 /* child forks a command and exits */
154 if(!(fork())){
155 system(&paramlist[5]);
156 /* should call _exit but this tests avoidance of atexit routine */
157 exit(0);
158 }
159 }
160 #else
161 fprintf(stderr, "ERROR: fork() not available in mingw\n");
162 exit(1);
163 #endif
164 }
165 else if( !strcmp(xtemplate, "xpaget") ){
166 word(paramlist, xtemplate, &ip);
167 got = XPAGet(xpa, xtemplate, &(paramlist[ip]), NULL,
168 bufs, lens, names, errs, MAX_FPS);
169 if( !quiet )
170 fprintf(stdout, "XPAGet (%s) returned %d buffer(s)\n", xtemplate, got);
171 for(i=0; i<got; i++){
172 if( !quiet )
173 fprintf(stdout, "\t%d: %s\n", i, names[i]);
174 if( errs[i] == NULL ){
175 if( !quiet && (lens[i] > 0) ){
176 fprintf(stdout, "contents (%lu bytes):\n", (unsigned long)lens[i]);
177 fwrite(bufs[i], sizeof(char), lens[i], stdout);
178 }
179 if( !quiet )
180 fprintf(stdout, "\n");
181 }
182 else{
183 write(fileno(stdout), errs[i], strlen(errs[i]));
184 }
185 if( bufs[i] )
186 xfree(bufs[i]);
187 if( names[i] )
188 xfree(names[i]);
189 if( errs[i] )
190 xfree(errs[i]);
191 }
192 }
193 }
194 else if( dowait ){
195 fprintf(stdout, "Press <CR> to continue ...");
196 fgets(tbuf, SZ_LINE, stdin);
197 }
198
199 /* return information about this xpa */
200 if( !sendbuf ){
201 snprintf(tbuf, SZ_LINE,
202 "class: %s\nname: %s\nmethod: %s\nsendian: %s\ncendian: %s\n",
203 xpa_class(xpa), xpa_name(xpa), xpa_method(xpa),
204 xpa_sendian(xpa), xpa_cendian(xpa));
205
206 if( (xpa->send_mode & XPA_MODE_FILLBUF) ){
207 send(xpa_datafd(xpa), tbuf, strlen(tbuf), 0);
208 *len = 0;
209 *buf = NULL;
210 if( !quiet)
211 fprintf(stdout, "\tcallback writes %d bytes to client\n",
212 (int)strlen(tbuf));
213 }
214 /* return the buffer and let xpa transmit it */
215 else{
216 *len = strlen(tbuf);
217 *buf = (char *)xmalloc(*len);
218 memcpy(*buf, tbuf, *len);
219 if( !quiet)
220 fprintf(stdout, "\tcallback returns %d bytes to xpa handler\n",
221 (int)strlen(tbuf));
222 }
223 }
224 /* return the last buffer we were sent */
225 else{
226 if( (xpa->send_mode & XPA_MODE_FILLBUF) ){
227 send(xpa_datafd(xpa), save_buf, save_bytes, 0);
228 *len = 0;
229 *buf = NULL;
230 if( !quiet)
231 fprintf(stdout, "\tcallback writes %lu bytes to client\n",
232 (unsigned long)save_bytes);
233 }
234 /* return the buffer and let xpa transmit it */
235 else{
236 *len = save_bytes;
237 *buf = (char *)xmalloc(*len);
238 memcpy(*buf, save_buf, *len);
239 if( !quiet)
240 fprintf(stdout, "\tcallback returns %lu bytes to xpa handler\n",
241 (unsigned long)save_bytes);
242 }
243 }
244 if( !quiet ){
245 fprintf(stdout, "SEND_CB complete\n");
246 }
247 else{
248 fprintf(stdout, ".");
249 }
250 fflush(stdout);
251 fflush(stderr);
252 return(0);
253 }
254
255 #ifdef ANSI_FUNC
256 int
257 receive_cb (void *client_data, void *call_data, char *paramlist,
258 char *buf, size_t len)
259 #else
260 int receive_cb(client_data, call_data, paramlist, buf, len)
261 void *client_data;
262 void *call_data;
263 char *paramlist;
264 char *buf;
265 size_t len;
266 #endif
267 {
268 XPA xpa = (XPA)call_data;
269 char *s = (char *)client_data;
270 char tbuf[SZ_LINE];
271 char cbuf[SZ_LINE];
272 char *errs[1];
273 int i;
274 int ip;
275 int got;
276 int xwait;
277
278 /* set the free routine */
279 if( domyfree ) XPASetFree(xpa, myfree, NULL);
280
281 if( !quiet ){
282 fprintf(stdout, "RECEIVE_CB #%d: %s:%s %s (%s)\n",
283 n++, xpa_class(xpa), xpa_name(xpa), xpa_method(xpa), s);
284 }
285 /* process param list */
286 if( paramlist && *paramlist ){
287 if( !quiet )
288 fprintf(stdout, "\tparamlist: %s\n", paramlist);
289 if( !strcmp(paramlist, "free") ){
290 if( !quiet )
291 fprintf(stdout, "Freeing xpa struct\n");
292 XPAFree(xpa);
293 return(0);
294 }
295 else if( !strcmp(paramlist, "exit") ){
296 if( !quiet )
297 fprintf(stdout, "Exiting\n");
298 if( doatexit){
299 please_exit = 1;
300 } else {
301 XPAFree(xpa1);
302 if( !doone ){
303 XPAFree(xpa1a);
304 XPAFree(xpa2);
305 XPAFree(xpa3);
306 }
307 }
308 }
309 else if( !strcmp(paramlist, "Exit") ){
310 if( !quiet )
311 fprintf(stdout, "Exiting immediately\n");
312 exit(0);
313 }
314 else if( !strncmp(paramlist, "error", 5) ){
315 if( !quiet )
316 fprintf(stdout, "Processing error: %s\n", &paramlist[6]);
317 if( strlen(paramlist) > 6 )
318 XPAError(xpa, &paramlist[6]);
319 else
320 XPAError(xpa, "intentional error from client");
321 return(-1);
322 }
323 else if( !strcmp(paramlist, "wait") ){
324 fprintf(stdout, "Press <CR> to continue ...");
325 fgets(tbuf, SZ_LINE, stdin);
326 }
327 else if( !strcmp(paramlist, "poll") ){
328 repoll:
329 fprintf(stdout, "Press 'q' to quit ...");
330 got = XPAPoll (10000, 1);
331 fprintf(stdout, " ... XPAPoll returns %d ...", got);
332 fgets(tbuf, SZ_LINE, stdin);
333 if( *tbuf != 'q' )
334 goto repoll;
335 }
336 else if( !strncmp(paramlist, "fork ", 5) ){
337 #if HAVE_MINGW32==0
338 if( strlen(paramlist) > 5 ){
339 fprintf(stdout, "fork command: %s\n", &paramlist[5]);
340 /* child forks a command and exits */
341 if(!(fork())){
342 system(&paramlist[5]);
343 /* should call _exit but this tests avoidance of atexit routine */
344 exit(0);
345 }
346 }
347 #else
348 fprintf(stderr, "ERROR: fork() not available in mingw\n");
349 exit(1);
350 #endif
351 }
352 else if( !strncmp(paramlist, "xpaset", 6) ){
353 ip = 0;
354 word(paramlist, tbuf, &ip);
355 if( word(paramlist, tbuf, &ip) ){
356 if( !quiet )
357 fprintf(stdout, "calling XPASet(%s, \"%s\")\n",
358 tbuf, &(paramlist[ip]));
359 got = XPASet(NULL, tbuf, &(paramlist[ip]), mode,
360 paramlist, strlen(paramlist), NULL, errs, 1);
361 if( got == 0 ){
362 if( !quiet )
363 fprintf(stdout, "no XPA access points matched template %s\n",
364 tbuf);
365 }
366 else if( errs[0] != NULL ){
367 if( !quiet )
368 fprintf(stdout, "Error on xpaset to %s: %s\n", tbuf, errs[0]);
369 xfree(errs[0]);
370 }
371 else{
372 if( !quiet )
373 fprintf(stdout, "XPASet to %s successful\n", tbuf);
374 }
375 }
376 return(0);
377 }
378 }
379 else if( dowait ){
380 fprintf(stdout, "Press <CR> to continue ...");
381 fgets(tbuf, SZ_LINE, stdin);
382 }
383
384 /* reset save buffer */
385 if( save_buf != NULL ){
386 xfree(save_buf);
387 save_buf = NULL;
388 }
389 save_bytes = 0;
390 xwait = dowait;
391 if( !(xpa->receive_mode & XPA_MODE_FILLBUF) && dofill ){
392 while( (got=recv(xpa_datafd(xpa), tbuf, SZ_LINE, 0)) >0 ){
393 if( xwait >0 ){
394 fprintf(stdout, "got %d bytes ... press <CR> to continue ...", got);
395 fgets(cbuf, SZ_LINE, stdin);
396 xwait--;
397 }
398 i = save_bytes;
399 save_bytes += got;
400 if( dosave ){
401 if( save_buf == NULL )
402 save_buf = (char *)xmalloc(save_bytes);
403 else
404 save_buf = (char *)xrealloc(save_buf, save_bytes);
405 memcpy(&save_buf[i], tbuf, got);
406 }
407 }
408 if( !quiet )
409 fprintf(stdout, "\tcallback read %lu bytes\n",
410 (unsigned long)save_bytes);
411 }
412 else{
413 if( !quiet ){
414 fprintf(stdout, "\tenter callback with buf: %lu bytes\n",
415 (unsigned long)len);
416 }
417 save_bytes = len;
418 save_buf = (char *)xmalloc(len);
419 memcpy(save_buf, buf, len);
420 }
421 if( !quiet ){
422 fprintf(stdout, "RECEIVE_CB complete\n");
423 }
424 else{
425 fprintf(stdout, ".");
426 }
427 fflush(stdout);
428 fflush(stderr);
429 return(0);
430 }
431
432 #ifdef ANSI_FUNC
433 int
434 info_cb (void *client_data, void *call_data, char *paramlist)
435 #else
436 int info_cb(client_data, call_data, paramlist)
437 void *client_data;
438 void *call_data;
439 char *paramlist;
440 #endif
441 {
442 XPA xpa = (XPA)call_data;
443 char *s = (char *)client_data;
444 char xtemplate[SZ_LINE];
445 char *names[MAX_FPS];
446 char *bufs[MAX_FPS];
447 char *errs[MAX_FPS];
448 size_t lens[MAX_FPS];
449 int i;
450 int ip;
451 int got;
452
453 if( !quiet ){
454 fprintf(stdout, "INFO_CB #%d: %s:%s (%s)\n",
455 n++, xpa_class(xpa), xpa_name(xpa), s);
456 }
457
458 if( paramlist && *paramlist ){
459 if( !quiet )
460 fprintf(stdout, "\tparamlist: %s\n", paramlist);
461 ip = 0;
462 word(paramlist, xtemplate, &ip);
463 if( !strcmp(xtemplate, "xpaget") ){
464 word(paramlist, xtemplate, &ip);
465 got = XPAGet(xpa, xtemplate, &(paramlist[ip]), NULL,
466 bufs, lens, names, errs, MAX_FPS);
467 if( !quiet )
468 fprintf(stdout, "XPAGet (%s) returned %d buffer(s)\n", xtemplate, got);
469 for(i=0; i<got; i++){
470 if( !quiet )
471 fprintf(stdout, "\t%d: %s\n", i, names[i]);
472 if( errs[i] == NULL ){
473 if( !quiet && (lens[i] > 0) ){
474 fprintf(stdout, "contents (%lu bytes):\n",
475 (unsigned long)lens[i]);
476 fwrite(bufs[i], sizeof(char), lens[i], stdout);
477 }
478 if( !quiet )
479 fprintf(stdout, "\n");
480 }
481 else{
482 write(fileno(stdout), errs[i], strlen(errs[i]));
483 }
484 if( bufs[i] )
485 xfree(bufs[i]);
486 if( names[i] )
487 xfree(names[i]);
488 if( errs[i] )
489 xfree(errs[i]);
490 }
491 }
492 }
493 if( !quiet ){
494 fprintf(stdout, "INFO_CB complete\n");
495 }
496 else{
497 fprintf(stdout, ".");
498 }
499 fflush(stdout);
500 fflush(stderr);
501 return(0);
502 }
503
504 #ifdef ANSI_FUNC
505 int
506 main (int argc, char **argv)
507 #else
508 main(argc, argv)
509 int argc;
510 char **argv;
511 #endif
512 {
513 #if HAVE_XT
514 Widget top;
515 XtAppContext appcontext;
516 #endif
517 char *proxy=NULL;
518 char tbuf[SZ_LINE];
519 char tbuf2[SZ_LINE];
520 char cmd[SZ_LINE];
521 int c;
522 int got=0;
523 int x=0;
524 int delay=0;
525
526 #if HAVE_MINGW32==0
527 setvbuf(stdout, NULL, _IONBF, 0);
528 setvbuf(stderr, NULL, _IONBF, 0);
529 #endif
530
531 *cmd = '\0';
532 /* process switch arguments */
533 while ((c = getopt(argc, argv, "1ad:f:mqrRp:suwxX")) != -1){
534 switch(c){
535 case '1':
536 doone = 1;
537 break;
538 case 'a':
539 mode = "ack=false";
540 break;
541 case 'd':
542 delay = atoi(optarg);
543 break;
544 case 'f':
545 snprintf(cmd, SZ_LINE, "%s %s &\n", argv[0], optarg);
546 break;
547 case 'm':
548 domyfree = 1;
549 break;
550 case 'q':
551 quiet = 1;
552 break;
553 case 'p':
554 proxy = optarg;
555 break;
556 case 'r':
557 mode = "fillbuf=false";
558 break;
559 case 'R':
560 mode = "fillbuf=false";
561 dofill = 1;
562 dosave = 0;
563 break;
564 case 's':
565 x = 0;
566 break;
567 case 'u':
568 mode = "buf=false";
569 break;
570 case 'w':
571 dowait++;
572 break;
573 case 'x':
574 doatexit=1;
575 break;
576 case 'X':
577 x = 1;
578 break;
579 }
580 }
581
582 if( optind >= argc ){
583 strcpy(name, "xpa");
584 }
585 else{
586 strcpy(name, argv[optind]);
587 }
588 strcpy(xclass, name);
589 cluc(xclass);
590
591 if( doatexit ){
592 #if HAVE_ATEXIT
593 XPAAtExit();
594 #else
595 fprintf(stderr, "ERROR: atexit not available\n");
596 exit(1);
597 #endif
598 }
599
600 if( x ){
601 #if HAVE_XT
602 top = XtAppInitialize(&appcontext, "Server", NULL, 0,
603 &argc, argv, NULL, NULL, 0);
604 XtVaSetValues(top, XtNwidth, (XtArgVal)10, XtNheight, (XtArgVal)10,
605 XtNmappedWhenManaged, (XtArgVal)False, NULL);
606 #else
607 fprintf(stderr, "Xt is not available ...\n");
608 got = 1;
609 goto done;
610 #endif
611 }
612
613 strcpy(tbuf, name);
614 snprintf(tbuf2, SZ_LINE, "help for %s", tbuf);
615 xpa1 = XPANew(xclass, tbuf, tbuf2,
616 send_cb, (void *)"send1", mode,
617 receive_cb, (void *)"receive1", mode);
618 if( xpa1 == NULL ){
619 fprintf(stderr, "ERROR: could not init xpa1\n");
620 }
621 else{
622 fprintf(stdout, "%s using method: %s\n", xpa_name(xpa1), xpa_method(xpa1));
623 }
624
625 if( !doone ){
626 snprintf(tbuf, SZ_LINE, "%s1", name);
627 snprintf(tbuf2, SZ_LINE, "help for %s", tbuf);
628 xpa1a = XPANew(xclass, tbuf, tbuf2,
629 send_cb, (void *)"send1a", mode,
630 receive_cb, (void *)"receive1a", mode);
631 if( xpa1a == NULL ){
632 fprintf(stderr, "ERROR: could not init xpa1a\n");
633 }
634 else{
635 fprintf(stdout, "%s using method: %s\n",
636 xpa_name(xpa1a), xpa_method(xpa1a));
637 }
638
639 snprintf(tbuf, SZ_LINE, "c_%s", name);
640 xpa2 = XPACmdNew(xclass, tbuf);
641 if( xpa2 == NULL ){
642 fprintf(stderr, "ERROR: could not init xpa2\n");
643 }
644 else{
645 XPACmdAdd(xpa2, "cmd2", "and help for cmd2",
646 send_cb, (void *)"cmd2", mode,
647 receive_cb, (void *)"cmd2", mode);
648 XPACmdAdd(xpa2, "cmd1 xx yy", "help for cmd1 xx yy",
649 send_cb, (void *)"cmd1 xx yy", mode,
650 receive_cb, (void *)"cmd1 xx yy", mode);
651 XPACmdAdd(xpa2, "cmd1 xx", "help for cmd1 xx",
652 send_cb, (void *)"cmd1 xx", mode,
653 receive_cb, (void *)"cmd1 xx", mode);
654 XPACmdAdd(xpa2, "cmd1", NULL,
655 send_cb, (void *)"cmd1", mode,
656 receive_cb, (void *)"cmd1", mode);
657 fprintf(stdout, "%s using method: %s\n",
658 xpa_name(xpa2), xpa_method(xpa2));
659 }
660
661 snprintf(tbuf, SZ_LINE, "i_%s", name);
662 xpa3 = XPAInfoNew(xclass, tbuf, info_cb, (void *)"info1", mode);
663 if( xpa3 == NULL ){
664 fprintf(stderr, "ERROR: could not init xpa3\n");
665 }
666 else{
667 fprintf(stdout, "%s using method: %s\n", xpa_name(xpa3), xpa_method(xpa3));
668 }
669 }
670
671 if( *cmd != '\0' ){
672 fprintf(stdout, "starting bkgd process: %s", cmd);
673 system(cmd);
674 }
675
676 /* delay if necessary */
677 if( delay ){
678 fprintf(stdout, "starting delay of %d seconds ...", delay);
679 XPASleep(delay*1000);
680 fprintf(stdout, " done\n");
681 }
682 fflush(stdout);
683 fflush(stderr);
684
685 /* connect to proxy, if necessary */
686 if( proxy ){
687 if( XPARemote(xpa1, proxy, "+", "proxy=true") < 0 ){
688 fprintf(stderr, "ERROR: could not connect to proxy: %s\n", proxy);
689 }
690 }
691
692 if( x ){
693 #if HAVE_XT
694 fprintf(stdout, "\nEntering Xt loop ...\n");
695 XPAXtAddInput(appcontext, NULL);
696 XtRealizeWidget(top);
697 XtAppMainLoop(appcontext);
698 #else
699 fprintf(stderr, "Xt is not available ...\n");
700 got = 1;
701 goto done;
702 #endif
703 }
704 else{
705 fprintf(stdout, "\nEntering select loop ...\n");
706 if( !doatexit ){
707 XPAMainLoop();
708 } else {
709 while( !please_exit )
710 if( !XPAPoll(-1, 100) ) please_exit=1;
711 }
712 goto done;
713 }
714
715 done:
716 if( !doatexit ){
717 XPAFree(xpa1);
718 if( !doone ){
719 XPAFree(xpa1a);
720 XPAFree(xpa2);
721 XPAFree(xpa3);
722 }
723 }
724
725 /* make valgrind happy */
726 XPACleanup();
727 if( save_buf ) xfree(save_buf);
728 exit(got);
729 }