home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / tcl / expect / expect-4.7 / example / chesslib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-20  |  1.3 KB  |  83 lines

  1. /* testlib.c - test expectlib */
  2.  
  3. #include <stdio.h>
  4. #include "expect.h"
  5.  
  6. extern exit();
  7.  
  8. timedout()
  9. {
  10.     fprintf(stderr,"timed out\n");
  11.     exit(-1);
  12. }
  13.  
  14. char move[100];
  15.  
  16. read_first_move(fd)
  17. int fd;
  18. {
  19.     if (EXP_TIMEOUT == exp_expectl(fd,
  20.             exp_glob,"first\r\n1.*\r\n",0,
  21.             exp_end)) {
  22.         timedout();
  23.     }
  24.     sscanf(exp_match,"%*s 1. %s",move);
  25. }
  26.  
  27. /* moves and counter-moves are printed out in different formats, sigh... */
  28.  
  29. read_counter_move(fd)
  30. int fd;
  31. {
  32.     switch (exp_expectl(fd,exp_glob,"*...*\r\n",0,exp_end)) {
  33.     case EXP_TIMEOUT: timedout();
  34.     case EXP_EOF: exit(-1);
  35.     }
  36.  
  37.     sscanf(exp_match,"%*s %*s %*s %*s ... %s",move);
  38. }
  39.  
  40. read_move(fd)
  41. int fd;
  42. {
  43.     switch (exp_expectl(fd,exp_glob,"*...*\r\n*.*\r\n",0,exp_end)) {
  44.     case EXP_TIMEOUT: timedout();
  45.     case EXP_EOF: exit(-1);
  46.     }
  47.  
  48.     sscanf(exp_match,"%*s %*s ... %*s %*s %s",move);
  49. }
  50.  
  51. send_move(fd)
  52. int fd;
  53. {
  54.     write(fd,move,strlen(move));
  55. }
  56.  
  57. main(){
  58.     int fd1, fd2;
  59.  
  60.     exp_loguser = 1;
  61.     exp_timeout = 3600;
  62.  
  63.     fd1 = exp_spawnl("chess","chess",(char *)0);
  64.  
  65.     if (-1 == exp_expectl(fd1,exp_glob,"Chess\r\n",0,exp_end)) exit;
  66.  
  67.     if (-1 == write(fd1,"first\r",6)) exit;
  68.  
  69.     read_first_move(fd1);
  70.  
  71.     fd2 = exp_spawnl("chess",0);
  72.  
  73.     if (-1 == exp_expectl(fd2,exp_glob,"Chess\r\n",0,exp_end)) exit;
  74.  
  75.     for (;;) {
  76.         send_move(fd2);
  77.         read_counter_move(fd2);
  78.  
  79.         send_move(fd1);
  80.         read_move(fd1);
  81.     }
  82. }
  83.