1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2006 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
39 #ifdef HAVE_SYS_WAIT_H
55 int send_signal_to_engine(pid_t pid
, int sig
)
57 if (kill(pid
, sig
) == -1)
63 int send_to_engine(GAME
*g
, int status
, const char *format
, ...)
69 struct userdata_s
*d
= g
->data
;
71 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
||
72 TEST_FLAG(d
->flags
, CF_HUMAN
))
75 if (send_signal_to_engine(d
->engine
->pid
, SIGINT
))
80 len
= vasprintf(&line
, format
, ap
);
82 line
= Malloc(LINE_MAX
);
83 len
= vsnprintf(line
, LINE_MAX
, format
, ap
);
93 FD_SET(d
->engine
->fd
[ENGINE_OUT_FD
], &fds
);
98 if ((n
= select(d
->engine
->fd
[ENGINE_OUT_FD
] + 1, NULL
, &fds
, NULL
,
100 if (FD_ISSET(d
->engine
->fd
[ENGINE_OUT_FD
], &fds
)) {
101 n
= write(d
->engine
->fd
[ENGINE_OUT_FD
], line
, len
);
107 if (kill(d
->engine
->pid
, 0) == -1) {
108 message(ERROR
, ANYKEY
, "Could not write to engine. "
109 "Process no longer exists.");
110 d
->engine
->status
= ENGINE_OFFLINE
;
114 message(ERROR
, ANYKEY
, "Attempt #%i. write(): %s", ++try,
120 message(NULL
, ANYKEY
, "try #%i: write() error to engine. "
121 "Expected %i, got %i.", ++try, len
, n
);
133 d
->engine
->status
= status
;
139 /* From the Xboard and screen packages. */
140 static int get_pty(char *pty_name
)
145 for (i
= 0; PTY_MAJOR
[i
]; i
++) {
148 for (n
= 0; PTY_MINOR
[n
]; n
++) {
149 sprintf(pty_name
, "%spty%c%c", _PATH_DEV
, PTY_MAJOR
[i
],
152 if ((fd
= open(pty_name
, O_RDWR
| O_NOCTTY
)) == -1)
155 sprintf(pty_name
, "%stty%c%c", _PATH_DEV
, PTY_MAJOR
[i
],
158 if (access(pty_name
, R_OK
| W_OK
) == -1) {
171 static char **parseargs(char *str
)
183 if (!(pptr
= malloc(sizeof(char *))))
186 for (i
= 0, s
= str
; *s
; lastchar
= *s
++) {
187 if ((*s
== '\"' || *s
== '\'') && lastchar
!= '\\') {
188 quote
= (quote
) ? 0 : 1;
192 if (*s
== ' ' && !quote
) {
194 pptr
= realloc(pptr
, (n
+ 2) * sizeof(char *));
195 pptr
[n
++] = strdup(arg
);
200 if ((i
+ 1) == sizeof(arg
))
209 pptr
= realloc(pptr
, (n
+ 2) * sizeof(char *));
210 pptr
[n
++] = strdup(arg
);
217 /* Is this dangerous if pty permissions are wrong? */
218 static pid_t
init_chess_engine(GAME
*g
, char **args
)
222 struct userdata_s
*d
= g
->data
;
224 char pty
[FILENAME_MAX
];
226 if ((to
[1] = get_pty(pty
)) == -1) {
231 if ((to
[1] = open("/dev/ptmx", O_RDWR
| O_NOCTTY
)) == -1) {
235 if (grantpt(to
[1]) == -1) {
239 if (unlockpt(to
[1]) == -1) {
248 if ((to
[0] = open(pty
, O_RDWR
, 0)) == -1)
250 if ((to
[0] = open(ptsname(to
[1]), O_RDWR
, 0)) == -1)
257 switch ((pid
= fork())) {
261 dup2(to
[0], STDIN_FILENO
);
262 dup2(from
[1], STDOUT_FILENO
);
267 dup2(STDOUT_FILENO
, STDERR_FILENO
);
268 execvp(args
[0], args
);
276 if (kill(pid
, 0) == -1)
282 d
->engine
->fd
[ENGINE_IN_FD
] = from
[0];
283 d
->engine
->fd
[ENGINE_OUT_FD
] = to
[1];
284 fcntl(d
->engine
->fd
[ENGINE_IN_FD
], F_SETFL
, O_NONBLOCK
| O_DIRECT
);
285 fcntl(d
->engine
->fd
[ENGINE_OUT_FD
], F_SETFL
, O_NONBLOCK
| O_DIRECT
);
286 d
->engine
->pid
= pid
;
290 void stop_engine(GAME
*g
)
292 struct userdata_s
*d
= g
->data
;
295 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
)
298 send_to_engine(g
, ENGINE_READY
, "quit\n");
300 if (!send_signal_to_engine(d
->engine
->pid
, 0)) {
301 if (!send_signal_to_engine(d
->engine
->pid
, SIGTERM
))
302 send_signal_to_engine(d
->engine
->pid
, SIGKILL
);
305 waitpid(d
->engine
->pid
, &s
, 0);
308 void set_engine_defaults(GAME
*g
, char **init
)
315 for (i
= 0; init
[i
]; i
++)
316 add_engine_command(g
, ENGINE_READY
, "%s\n", init
[i
]);
319 int start_chess_engine(GAME
*g
)
324 struct userdata_s
*d
= g
->data
;
329 args
= parseargs(config
.engine_cmd
);
331 d
->engine
= Calloc(1, sizeof(struct engine_s
));
332 d
->engine
->status
= ENGINE_INITIALIZING
;
333 update_status_window(*g
);
336 switch (init_chess_engine(g
, args
)) {
338 /* Pty allocation. */
339 message(ERROR
, ANYKEY
, "Could not allocate PTY");
340 d
->engine
->status
= ENGINE_OFFLINE
;
343 /* Could not execute engine. */
344 message(ERROR
, ANYKEY
, "%s: %s", args
[0], strerror(errno
));
345 d
->engine
->status
= ENGINE_OFFLINE
;
349 set_engine_defaults(g
, config
.einit
);
350 d
->engine
->status
= ENGINE_READY
;
354 for (i
= 0; args
[i
]; i
++)
358 update_status_window(*g
);
362 static void parse_xboard_line(GAME
*g
, char *str
)
364 char m
[MAX_SAN_MOVE_LEN
+ 1] = {0}, *p
= m
;
365 struct userdata_s
*d
= g
->data
;
369 // 1. a2a4 (gnuchess)
373 // gnuchess echos our input move so we don't need the duplicate (above).
374 if (*p
== '.' && *(p
+ 1) == ' ' && *(p
+ 2) != '.')
377 // 1. ... a2a4 (gnuchess/engine move)
378 if (*p
== '.' && *(p
+ 1) == ' ' && *(p
+ 2) == '.') {
379 while (*p
== ' ' || *p
== '.')
383 if (strncmp(str
, "move ", 5) == 0)
386 if (strlen(p
) > MAX_SAN_MOVE_LEN
)
389 // We should now have the real move which may be in SAN or frfr format.
390 if (sscanf(p
, "%[0-9a-hprnqkxPRNBQKO+=#-]", m
) == 1) {
391 if (TEST_FLAG(g
->flags
, GF_GAMEOVER
))
397 p
= m
+ strlen(m
) - 1;
399 // Test SAN or a2a4 format.
400 if (!isdigit(*p
) && *p
!= 'O' && *p
!= '+' && *p
!= '#' &&
401 ((*(p
- 1) != '=' || !isdigit(*(p
- 1))) &&
402 pgn_piece_to_int(*p
) == -1))
407 if (pgn_parse_move(g
, d
->b
, &p
) != E_PGN_OK
) {
412 pgn_history_add(g
, p
);
413 SET_FLAG(g
->flags
, GF_MODIFIED
);
416 if (TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
)) {
418 update_cursor(*g
, g
->hindex
);
420 if (TEST_FLAG(g
->flags
, GF_GAMEOVER
))
423 add_engine_command(g
, ENGINE_THINKING
, "go\n");
426 if (g
->side
== g
->turn
)
433 void parse_engine_output(GAME
*g
, char *str
)
435 char buf
[255], *p
= buf
;
438 if (*str
== '\n' || *str
== '\r') {
442 append_enginebuf(buf
);
443 parse_xboard_line(g
, buf
);
459 void send_engine_command(GAME
*g
)
461 struct userdata_s
*d
= g
->data
;
462 struct queue_s
**q
= d
->engine
->queue
;
465 if (!d
->engine
|| !d
->engine
->queue
)
471 if (send_to_engine(g
, q
[0]->status
, "%s", q
[0]->line
) == 0) {
473 update_status_window(*g
);
479 for (i
= 0; q
[i
]; i
++) {
489 void add_engine_command(GAME
*g
, int s
, char *fmt
, ...)
493 struct userdata_s
*d
= g
->data
;
500 q
= d
->engine
->queue
;
503 for (i
= 0; q
[i
]; i
++);
505 q
= Realloc(q
, (i
+ 2) * sizeof(struct queue_s
*));
507 #ifdef HAVE_VASPRINTF
508 vasprintf(&line
, fmt
, ap
);
510 line
= Malloc(LINE_MAX
+ 1);
511 vsnprintf(line
, LINE_MAX
, fmt
, ap
);
514 q
[i
] = Malloc(sizeof(struct queue_s
));
516 q
[i
++]->status
= (s
== -1) ? d
->engine
->status
: s
;
518 d
->engine
->queue
= q
;