home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume15 / ultrix-modem / sendex.c < prev    next >
C/C++ Source or Header  |  1988-05-24  |  2KB  |  99 lines

  1. /*
  2.  * Copyright (c) Dave Settle, Mar 1987
  3.  * Permission is granted to do anything with this program, except remove
  4.  * this copyright notice, or sell it for profit.
  5.  *
  6.  *
  7.  * sendex.c: contains the send-expect routines, beefed-up a little to
  8.  * make them more useful.
  9.  * Both "send" and "expect" now have a function, rather than
  10.  * a file descriptor. This function gets called as follows:
  11.  *
  12.  *    (int) (*rdfunc)();        [send]
  13.  *    (*wtfunc)((char *)s);        [expect]
  14.  *
  15.  */
  16.  
  17. #include <setjmp.h>
  18. #include <signal.h>
  19. #include <stdio.h>
  20. /*
  21.  * send: write the string out. Show the chars written if necessary.
  22.  */
  23. send(s, func)
  24. char *s;
  25. void (*func)();
  26. {
  27. #ifdef    DEBUG
  28.     printf("send(");
  29.     sshow(s);
  30.     printf(")\n");
  31. #endif
  32.     (*func)(s);
  33.     sleep(1);
  34. }
  35. /*
  36.  * expect: expect a string. Return 0 on success, 1 on timeout.
  37.  * chars are threaded on the needle as they match the expect string
  38.  * if one fails to match, all chars are unthreaded. 
  39.  * expect succeeds if all chars on expect string are threaded.
  40.  */
  41. jmp_buf env;
  42.  
  43. timeout(){
  44.     longjmp(env, 1);
  45. }
  46.  
  47. expect(s, t, func)
  48. char *s;
  49. int (*func)();
  50. {
  51.     char *needle = s, c;
  52.     int (*handler)();
  53.     alarm(0);
  54.     handler = signal(SIGALRM, timeout);
  55.     if(setjmp(env)) {
  56.         printf("Timeout expecting %s\n", s);
  57.         return(1);        /* fail - timeout */
  58.     }
  59.     alarm(t);                /* if zero, no timeout */
  60. #ifdef    DEBUG
  61.     printf("\nexpect(%s)\n", s);
  62. #endif
  63.     while(*needle) {
  64.         c = (*func)();
  65. #ifdef DEBUG
  66.         show(c);
  67. #endif
  68.         if(*needle != c) needle = s;
  69.         if(*needle == c) needle++;
  70.     }
  71. #ifdef    DEBUG
  72.     printf("got it\n");
  73. #endif
  74.     alarm(0);
  75.     signal(SIGALRM, handler);
  76.     return(0);
  77. }
  78. #ifdef    DEBUG
  79. /*
  80.  * sshow: show string
  81.  */
  82. sshow(s)
  83. char *s;
  84. {
  85.     while(*s) show(*s++);
  86. }
  87. show(c)
  88. char c;
  89. {
  90.     if((c < 31)) {
  91.         printf("^%c", c + '@');
  92.         if(c == '\n') putchar('\n');
  93.     }
  94.     else putchar(c);
  95.     fflush(stdout);
  96. }
  97. #endif
  98.  
  99.