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