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