1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2013 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
28 #include <sys/types.h>
48 #elif defined(HAVE_CURSES_H)
56 #ifdef HAVE_SYS_WAIT_H
73 int send_signal_to_engine(pid_t pid
, int sig
)
75 if (kill(pid
, sig
) == -1)
81 int send_to_engine(GAME g
, int status
, const char *format
, ...)
87 struct userdata_s
*d
= g
->data
;
89 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
||
90 TEST_FLAG(d
->flags
, CF_HUMAN
))
95 len
= vasprintf(&line
, format
, ap
);
97 line
= Malloc(LINE_MAX
);
98 len
= vsnprintf(line
, LINE_MAX
, format
, ap
);
108 FD_SET(d
->engine
->fd
[ENGINE_OUT_FD
], &fds
);
113 if ((n
= select(d
->engine
->fd
[ENGINE_OUT_FD
] + 1, NULL
, &fds
, NULL
,
115 if (FD_ISSET(d
->engine
->fd
[ENGINE_OUT_FD
], &fds
)) {
116 n
= write(d
->engine
->fd
[ENGINE_OUT_FD
], line
, len
);
122 if (kill(d
->engine
->pid
, 0) == -1) {
123 message(_("[ ERROR ]"), _("[ press any key to continue ]"), "Could not write to engine. "
124 "Process no longer exists.");
125 d
->engine
->status
= ENGINE_OFFLINE
;
129 message(_("[ ERROR ]"), _("[ press any key to continue ]"), "Attempt #%i. write(): %s", ++try,
135 message(NULL
, _("[ press any key to continue ]"), "try #%i: write() error to engine. "
136 "Expected %i, got %i.", ++try, len
, n
);
148 d
->engine
->status
= status
;
154 /* From the Xboard and screen packages. */
155 static int get_pty(char *pty_name
)
160 for (i
= 0; PTY_MAJOR
[i
]; i
++) {
163 for (n
= 0; PTY_MINOR
[n
]; n
++) {
164 sprintf(pty_name
, "%spty%c%c", _PATH_DEV
, PTY_MAJOR
[i
],
167 if ((fd
= open(pty_name
, O_RDWR
| O_NOCTTY
)) == -1)
170 sprintf(pty_name
, "%stty%c%c", _PATH_DEV
, PTY_MAJOR
[i
],
173 if (access(pty_name
, R_OK
| W_OK
) == -1) {
186 static char **parseargs(char *str
)
198 if (!(pptr
= malloc(sizeof(char *))))
201 for (i
= 0, s
= str
; *s
; lastchar
= *s
++) {
202 if ((*s
== '\"' || *s
== '\'') && lastchar
!= '\\') {
203 quote
= (quote
) ? 0 : 1;
207 if (*s
== ' ' && !quote
) {
209 pptr
= realloc(pptr
, (n
+ 2) * sizeof(char *));
210 pptr
[n
++] = strdup(arg
);
215 if ((i
+ 1) == sizeof(arg
))
224 pptr
= realloc(pptr
, (n
+ 2) * sizeof(char *));
225 pptr
[n
++] = strdup(arg
);
232 int init_chess_engine(GAME g
)
234 struct userdata_s
*d
= g
->data
;
236 if (start_chess_engine(g
) > 0) {
241 add_engine_command(g
, ENGINE_READY
, "setboard %s\n",
242 pgn_game_to_fen(g
, d
->b
));
246 /* Is this dangerous if pty permissions are wrong? */
247 static pid_t
exec_chess_engine(GAME g
, char **args
)
251 struct userdata_s
*d
= g
->data
;
253 char pty
[FILENAME_MAX
];
255 if ((to
[1] = get_pty(pty
)) == -1) {
260 if ((to
[1] = open("/dev/ptmx", O_RDWR
| O_NOCTTY
)) == -1) {
264 if (grantpt(to
[1]) == -1) {
268 if (unlockpt(to
[1]) == -1) {
277 if ((to
[0] = open(pty
, O_RDWR
, 0)) == -1)
279 if ((to
[0] = open(ptsname(to
[1]), O_RDWR
, 0)) == -1)
286 switch ((pid
= fork())) {
290 dup2(to
[0], STDIN_FILENO
);
291 dup2(from
[1], STDOUT_FILENO
);
296 dup2(STDOUT_FILENO
, STDERR_FILENO
);
297 execvp(args
[0], args
);
304 * FIXME shouldn't block here. When theres a high load average, the engine
305 * process will fail to start. Better to handle this in game_loop() and
306 * give up after a timer expires.
310 if (send_signal_to_engine(pid
, 0))
316 d
->engine
->fd
[ENGINE_IN_FD
] = from
[0];
317 d
->engine
->fd
[ENGINE_OUT_FD
] = to
[1];
318 fcntl(d
->engine
->fd
[ENGINE_IN_FD
], F_SETFL
, O_NONBLOCK
);
319 fcntl(d
->engine
->fd
[ENGINE_OUT_FD
], F_SETFL
, O_NONBLOCK
);
320 d
->engine
->pid
= pid
;
324 void stop_engine(GAME g
)
326 struct userdata_s
*d
= g
->data
;
329 if (!d
->engine
|| d
->engine
->pid
== -1 || d
->engine
->status
== ENGINE_OFFLINE
)
332 send_to_engine(g
, ENGINE_READY
, "quit\n");
334 if (!send_signal_to_engine(d
->engine
->pid
, 0)) {
335 if (!send_signal_to_engine(d
->engine
->pid
, SIGTERM
))
336 send_signal_to_engine(d
->engine
->pid
, SIGKILL
);
339 waitpid(d
->engine
->pid
, &s
, 0);
341 d
->engine
->status
= ENGINE_OFFLINE
;
344 void set_engine_defaults(GAME g
, char **init
)
351 for (i
= 0; init
[i
]; i
++)
352 add_engine_command(g
, ENGINE_READY
, "%s\n", init
[i
]);
355 int start_chess_engine(GAME g
)
360 struct userdata_s
*d
= g
->data
;
362 if (d
->engine
&& d
->engine
->status
!= ENGINE_OFFLINE
)
365 args
= parseargs(config
.engine_cmd
);
368 d
->engine
= Calloc(1, sizeof(struct engine_s
));
370 d
->engine
->status
= ENGINE_INITIALIZING
;
371 update_status_window(g
);
375 switch (exec_chess_engine(g
, args
)) {
377 /* Pty allocation. */
378 message(_("[ ERROR ]"), _("[ press any key to continue ]"), "Could not allocate PTY");
379 d
->engine
->status
= ENGINE_OFFLINE
;
382 /* Could not execute engine. */
383 message(_("[ ERROR ]"), _("[ press any key to continue ]"), "%s: %s", args
[0], strerror(errno
));
384 d
->engine
->status
= ENGINE_OFFLINE
;
388 d
->engine
->status
= ENGINE_READY
;
389 set_engine_defaults(g
, config
.einit
);
393 for (i
= 0; args
[i
]; i
++)
400 static void parse_xboard_line(GAME g
, char *str
)
402 char m
[MAX_SAN_MOVE_LEN
+ 1] = {0}, *p
= m
;
403 struct userdata_s
*d
= g
->data
;
410 // 1. a2a4 (gnuchess)
414 // gnuchess echos our input move so we don't need the duplicate (above).
415 if (*p
== '.' && *(p
+ 1) == ' ' && *(p
+ 2) != '.')
418 // 1. ... a2a4 (gnuchess/engine move)
419 if (*p
== '.' && *(p
+ 1) == ' ' && *(p
+ 2) == '.') {
420 while (*p
== ' ' || *p
== '.')
424 if (strncmp(str
, "move ", 5) == 0)
427 if (strlen(p
) > MAX_SAN_MOVE_LEN
)
430 // We should now have the real move which may be in SAN or frfr format.
431 if (sscanf(p
, "%[0-9a-hprnqkxPRNBQKO+=#-]%n", m
, &count
) == 1) {
433 * For engine commands (the '|' key). Don't try and validate them.
435 if (count
!= strlen(p
))
438 if (TEST_FLAG(g
->flags
, GF_GAMEOVER
)) {
446 p
= m
+ strlen(m
) - 1;
448 // Test SAN or a2a4 format.
449 if (!isdigit(*p
) && *p
!= 'O' && *p
!= '+' && *p
!= '#' &&
450 ((*(p
- 1) != '=' || !isdigit(*(p
- 1))) &&
451 pgn_piece_to_int(*p
) == -1))
456 if ((n
= pgn_parse_move(g
, d
->b
, &p
, &frfr
)) != E_PGN_OK
) {
457 invalid_move(d
->n
+ 1, n
, m
);
462 pgn_history_add(g
, d
->b
, p
);
463 SET_FLAG(d
->flags
, CF_MODIFIED
);
466 if (TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
)) {
467 update_cursor(g
, g
->hindex
);
469 if (TEST_FLAG(g
->flags
, GF_GAMEOVER
)) {
474 add_engine_command(g
, ENGINE_THINKING
, "go\n");
478 if (g
->side
== g
->turn
)
481 d
->engine
->status
= ENGINE_THINKING
;
487 void parse_engine_output(GAME g
, char *str
)
489 char buf
[255], *p
= buf
;
492 if (*str
== '\n' || *str
== '\r') {
496 append_enginebuf(g
, buf
);
497 parse_xboard_line(g
, buf
);
515 void send_engine_command(GAME g
)
517 struct userdata_s
*d
= g
->data
;
518 struct queue_s
**q
= d
->engine
->queue
;
521 if (!d
->engine
|| !d
->engine
->queue
)
527 if (send_to_engine(g
, q
[0]->status
, "%s", q
[0]->line
))
533 for (i
= 0; q
[i
]; i
++) {
543 void add_engine_command(GAME g
, int s
, char *fmt
, ...)
547 struct userdata_s
*d
= g
->data
;
551 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
) {
552 if (init_chess_engine(g
))
556 q
= d
->engine
->queue
;
559 for (i
= 0; q
[i
]; i
++);
561 q
= Realloc(q
, (i
+ 2) * sizeof(struct queue_s
*));
563 #ifdef HAVE_VASPRINTF
564 vasprintf(&line
, fmt
, ap
);
566 line
= Malloc(LINE_MAX
+ 1);
567 vsnprintf(line
, LINE_MAX
, fmt
, ap
);
570 q
[i
] = Malloc(sizeof(struct queue_s
));
572 q
[i
++]->status
= (s
== -1) ? d
->engine
->status
: s
;
574 d
->engine
->queue
= q
;