-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprog.c
More file actions
185 lines (144 loc) · 2.86 KB
/
prog.c
File metadata and controls
185 lines (144 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "includes.h"
#include "knightcap.h"
#ifdef SUNOS4
#define memmove memcpy
#endif
static int fd_in = -1;
static int fd_out = -1;
static FILE *f_out;
static int child_pid;
extern struct state *state;
void prog_printf(char *format_str, ...)
{
va_list ap;
if (!f_out) return;
va_start(ap, format_str);
vfprintf(f_out,format_str,ap);
va_end(ap);
#if 0
fprintf(stdout, "[");
va_start(ap, format_str);
vfprintf(stdout,format_str,ap);
va_end(ap);
#endif
}
void prog_start(char *prog)
{
int fd1[2], fd2[2];
if (pipe(fd1) || pipe(fd2)) {
perror("pipe");
return;
}
child_pid = fork();
if (child_pid) {
fd_in = fd1[0];
fd_out = fd2[1];
close(fd1[1]);
close(fd2[0]);
f_out = fdopen(fd_out, "w");
setlinebuf(f_out);
return;
}
close(0);
close(1);
if (dup(fd2[0]) != 0 ||
dup(fd1[1]) != 1) {
fprintf(stderr,"Failed to setup pipes\n");
exit(1);
}
close(fd1[0]);
close(fd2[1]);
close(fd1[0]);
close(fd2[1]);
exit(system(prog));
}
int prog_running(void)
{
if (!process_exists(child_pid)) return 0;
return f_out != NULL;
}
void prog_tell_move(Position *p, Move *move)
{
prog_printf("%s\n",short_movestr(p, move));
}
void prog_script(const char *script)
{
FILE *f = fopen(script,"r");
char line[200];
if (!f) return;
while (fgets(line, sizeof(line)-1, f)) {
if (line[strlen(line)-1] == '\n') {
line[strlen(line)-1] = 0;
}
if (line[0] == '&') {
prog_script(&line[1]);
} else {
prog_printf("%s\n", line);
}
}
fclose(f);
}
static int parse_prog_move(char *line,Move *move,int player)
{
int ret, move_num;
char movebuf[100];
Piece promotion;
if ((ret = sscanf(line,"%d. ... %s",&move_num,movebuf)) >= 2 &&
parse_move(movebuf, &state->position, move)) {
return 1;
}
if (parse_ics_move(line, player, move, &promotion)) {
return 1;
}
return 0;
}
int prog_check_move(Move *move, int player)
{
static char line[1000];
static int line_len, print_len;
fd_set set;
struct timeval tval;
int n, found=0;
char *p;
if (fd_in == -1)
return 0;
FD_ZERO(&set);
FD_SET(fd_in, &set);
tval.tv_sec = 0;
tval.tv_usec = 0;
while (!found) {
if (select(fd_in+1, &set, NULL, NULL, &tval) != 1)
break;
n = read(fd_in, line+line_len, sizeof(line) - (line_len+1));
if (n <= 0) break;
line[line_len+n] = 0;
while ((p=strchr(line,'\n'))) {
*p++ = 0;
if (parse_prog_move(line, move, player)) {
found = 1;
}
if (strstr(line,"<12>") == NULL)
lprintf(0,"%s\n", line+print_len);
memmove(line, p, sizeof(line)-(p-line));
print_len = 0;
}
line_len = strlen(line);
if (line_len > 0 && strstr(line,"<12>") == NULL) {
lprintf(0,"%s", line);
print_len = line_len;
}
}
return found;
}
void prog_exit(void)
{
if (f_out) {
prog_printf("quit\n");
close(fd_in);
}
if (child_pid) {
kill(child_pid, SIGINT);
kill(child_pid, SIGINT);
kill(child_pid, SIGTERM);
}
}