OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [insight/] [expect/] [example/] [chesslib++.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/* testlib.c for c++ - test expectlib */
2
 
3
#include <stdio.h>
4
#include "expect.h"
5
 
6
extern "C" {
7
        extern int write(...);
8
        extern int strlen(...);
9
}
10
 
11
void
12
timedout()
13
{
14
        fprintf(stderr,"timed out\n");
15
        exit(-1);
16
}
17
 
18
char move[100];
19
 
20
void
21
read_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
 
31
void
32
read_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
 
42
void
43
read_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
 
53
void
54
send_move(int fd)
55
{
56
        write(fd,move,strlen(move));
57
}
58
 
59
main(){
60
        int fd1, fd2;
61
 
62
        exp_loguser = 1;
63
        exp_timeout = 3600;
64
 
65
        fd1 = exp_spawnl("chess","chess",(char *)0);
66
 
67
        if (-1 == exp_expectl(fd1,exp_glob,"Chess\r\n",0,exp_end)) exit;
68
 
69
        if (-1 == write(fd1,"first\r",6)) exit;
70
 
71
        read_first_move(fd1);
72
 
73
        fd2 = exp_spawnl("chess","chess",(char *)0);
74
 
75
        if (-1 == exp_expectl(fd2,exp_glob,"Chess\r\n",0,exp_end)) exit;
76
 
77
        for (;;) {
78
                send_move(fd2);
79
                read_counter_move(fd2);
80
 
81
                send_move(fd1);
82
                read_move(fd1);
83
        }
84
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.