1/* chesslib.c - test expectlib */
2
3#include <stdio.h>
4#include "expect.h"
5
6timedout()
7{
8	fprintf(stderr,"timed out\n");
9	exit(-1);
10}
11
12char move[100];
13
14read_first_move(fd)
15int 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
27read_counter_move(fd)
28int 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
38read_move(fd)
39int 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
49send_move(fd)
50int fd;
51{
52	write(fd,move,strlen(move));
53}
54
55main(){
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