1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2007 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
24 #include <sys/types.h>
54 #ifdef HAVE_SYS_WAIT_H
71 int send_signal_to_engine(pid_t pid
, int sig
)
73 if (kill(pid
, sig
) == -1)
79 int send_to_engine(GAME g
, int status
, const char *format
, ...)
85 struct userdata_s
*d
= g
->data
;
87 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
||
88 TEST_FLAG(d
->flags
, CF_HUMAN
))
93 len
= vasprintf(&line
, format
, ap
);
95 line
= Malloc(LINE_MAX
);
96 len
= vsnprintf(line
, LINE_MAX
, format
, ap
);
106 FD_SET(d
->engine
->fd
[ENGINE_OUT_FD
], &fds
);
111 if ((n
= select(d
->engine
->fd
[ENGINE_OUT_FD
] + 1, NULL
, &fds
, NULL
,
113 if (FD_ISSET(d
->engine
->fd
[ENGINE_OUT_FD
], &fds
)) {
114 n
= write(d
->engine
->fd
[ENGINE_OUT_FD
], line
, len
);
120 if (kill(d
->engine
->pid
, 0) == -1) {
121 message(ERROR
, ANYKEY
, "Could not write to engine. "
122 "Process no longer exists.");
123 d
->engine
->status
= ENGINE_OFFLINE
;
127 message(ERROR
, ANYKEY
, "Attempt #%i. write(): %s", ++try,
133 message(NULL
, ANYKEY
, "try #%i: write() error to engine. "
134 "Expected %i, got %i.", ++try, len
, n
);
146 d
->engine
->status
= status
;
152 /* From the Xboard and screen packages. */
153 static int get_pty(char *pty_name
)
158 for (i
= 0; PTY_MAJOR
[i
]; i
++) {
161 for (n
= 0; PTY_MINOR
[n
]; n
++) {
162 sprintf(pty_name
, "%spty%c%c", _PATH_DEV
, PTY_MAJOR
[i
],
165 if ((fd
= open(pty_name
, O_RDWR
| O_NOCTTY
)) == -1)
168 sprintf(pty_name
, "%stty%c%c", _PATH_DEV
, PTY_MAJOR
[i
],
171 if (access(pty_name
, R_OK
| W_OK
) == -1) {
184 static char **parseargs(char *str
)
196 if (!(pptr
= malloc(sizeof(char *))))
199 for (i
= 0, s
= str
; *s
; lastchar
= *s
++) {
200 if ((*s
== '\"' || *s
== '\'') && lastchar
!= '\\') {
201 quote
= (quote
) ? 0 : 1;
205 if (*s
== ' ' && !quote
) {
207 pptr
= realloc(pptr
, (n
+ 2) * sizeof(char *));
208 pptr
[n
++] = strdup(arg
);
213 if ((i
+ 1) == sizeof(arg
))
222 pptr
= realloc(pptr
, (n
+ 2) * sizeof(char *));
223 pptr
[n
++] = strdup(arg
);
230 int init_chess_engine(GAME g
)
232 struct userdata_s
*d
= g
->data
;
235 if (start_chess_engine(g
) > 0) {
240 x
= pgn_tag_find(g
->tag
, "FEN");
241 w
= pgn_tag_find(g
->tag
, "SetUp");
243 if ((w
>= 0 && x
>= 0 && atoi(g
->tag
[w
]->value
) == 1) ||
245 add_engine_command(g
, ENGINE_READY
, "setboard %s\n", g
->tag
[x
]->value
);
247 add_engine_command(g
, ENGINE_READY
, "setboard %s\n",
248 pgn_game_to_fen(g
, d
->b
));
253 /* Is this dangerous if pty permissions are wrong? */
254 static pid_t
exec_chess_engine(GAME g
, char **args
)
258 struct userdata_s
*d
= g
->data
;
260 char pty
[FILENAME_MAX
];
262 if ((to
[1] = get_pty(pty
)) == -1) {
267 if ((to
[1] = open("/dev/ptmx", O_RDWR
| O_NOCTTY
)) == -1) {
271 if (grantpt(to
[1]) == -1) {
275 if (unlockpt(to
[1]) == -1) {
284 if ((to
[0] = open(pty
, O_RDWR
, 0)) == -1)
286 if ((to
[0] = open(ptsname(to
[1]), O_RDWR
, 0)) == -1)
293 switch ((pid
= fork())) {
297 dup2(to
[0], STDIN_FILENO
);
298 dup2(from
[1], STDOUT_FILENO
);
303 dup2(STDOUT_FILENO
, STDERR_FILENO
);
304 execvp(args
[0], args
);
311 * FIXME shouldn't block here. When theres a high load average, the engine
312 * process will fail to start. Better to handle this in game_loop() and
313 * give up after a timer expires.
317 if (send_signal_to_engine(pid
, 0))
323 d
->engine
->fd
[ENGINE_IN_FD
] = from
[0];
324 d
->engine
->fd
[ENGINE_OUT_FD
] = to
[1];
325 fcntl(d
->engine
->fd
[ENGINE_IN_FD
], F_SETFL
, O_NONBLOCK
);
326 fcntl(d
->engine
->fd
[ENGINE_OUT_FD
], F_SETFL
, O_NONBLOCK
);
327 d
->engine
->pid
= pid
;
331 void stop_engine(GAME g
)
333 struct userdata_s
*d
= g
->data
;
336 if (!d
->engine
|| d
->engine
->pid
== -1 || d
->engine
->status
== ENGINE_OFFLINE
)
339 send_to_engine(g
, ENGINE_READY
, "quit\n");
341 if (!send_signal_to_engine(d
->engine
->pid
, 0)) {
342 if (!send_signal_to_engine(d
->engine
->pid
, SIGTERM
))
343 send_signal_to_engine(d
->engine
->pid
, SIGKILL
);
346 waitpid(d
->engine
->pid
, &s
, 0);
348 d
->engine
->status
= ENGINE_OFFLINE
;
351 void set_engine_defaults(GAME g
, char **init
)
358 for (i
= 0; init
[i
]; i
++)
359 add_engine_command(g
, ENGINE_READY
, "%s\n", init
[i
]);
362 int start_chess_engine(GAME g
)
367 struct userdata_s
*d
= g
->data
;
372 args
= parseargs(config
.engine_cmd
);
374 d
->engine
= Calloc(1, sizeof(struct engine_s
));
375 d
->engine
->status
= ENGINE_INITIALIZING
;
376 update_status_window(g
);
380 switch (exec_chess_engine(g
, args
)) {
382 /* Pty allocation. */
383 message(ERROR
, ANYKEY
, "Could not allocate PTY");
384 d
->engine
->status
= ENGINE_OFFLINE
;
387 /* Could not execute engine. */
388 message(ERROR
, ANYKEY
, "%s: %s", args
[0], strerror(errno
));
389 d
->engine
->status
= ENGINE_OFFLINE
;
393 set_engine_defaults(g
, config
.einit
);
394 d
->engine
->status
= ENGINE_READY
;
398 for (i
= 0; args
[i
]; i
++)
405 static void parse_xboard_line(GAME g
, char *str
)
407 char m
[MAX_SAN_MOVE_LEN
+ 1] = {0}, *p
= m
;
408 struct userdata_s
*d
= g
->data
;
415 // 1. a2a4 (gnuchess)
419 // gnuchess echos our input move so we don't need the duplicate (above).
420 if (*p
== '.' && *(p
+ 1) == ' ' && *(p
+ 2) != '.')
423 // 1. ... a2a4 (gnuchess/engine move)
424 if (*p
== '.' && *(p
+ 1) == ' ' && *(p
+ 2) == '.') {
425 while (*p
== ' ' || *p
== '.')
429 if (strncmp(str
, "move ", 5) == 0)
432 if (strlen(p
) > MAX_SAN_MOVE_LEN
)
435 // We should now have the real move which may be in SAN or frfr format.
436 if (sscanf(p
, "%[0-9a-hprnqkxPRNBQKO+=#-]%n", m
, &count
) == 1) {
438 * For engine commands (the '|' key). Don't try and validate them.
440 if (count
!= strlen(p
))
443 if (TEST_FLAG(g
->flags
, GF_GAMEOVER
)) {
451 p
= m
+ strlen(m
) - 1;
453 // Test SAN or a2a4 format.
454 if (!isdigit(*p
) && *p
!= 'O' && *p
!= '+' && *p
!= '#' &&
455 ((*(p
- 1) != '=' || !isdigit(*(p
- 1))) &&
456 pgn_piece_to_int(*p
) == -1))
461 if ((n
= pgn_parse_move(g
, d
->b
, &p
, &frfr
)) != E_PGN_OK
) {
462 invalid_move(d
->n
+ 1, n
, m
);
466 pgn_history_add(g
, p
);
467 SET_FLAG(d
->flags
, CF_MODIFIED
);
470 if (TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
)) {
471 update_cursor(g
, g
->hindex
);
473 if (TEST_FLAG(g
->flags
, GF_GAMEOVER
)) {
478 add_engine_command(g
, ENGINE_THINKING
, "go\n");
482 if (g
->side
== g
->turn
)
485 d
->engine
->status
= ENGINE_THINKING
;
491 void parse_engine_output(GAME g
, char *str
)
493 char buf
[255], *p
= buf
;
496 if (*str
== '\n' || *str
== '\r') {
500 append_enginebuf(g
, buf
);
501 parse_xboard_line(g
, buf
);
519 void send_engine_command(GAME g
)
521 struct userdata_s
*d
= g
->data
;
522 struct queue_s
**q
= d
->engine
->queue
;
525 if (!d
->engine
|| !d
->engine
->queue
)
531 if (send_to_engine(g
, q
[0]->status
, "%s", q
[0]->line
))
537 for (i
= 0; q
[i
]; i
++) {
547 void add_engine_command(GAME g
, int s
, char *fmt
, ...)
551 struct userdata_s
*d
= g
->data
;
555 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
) {
556 if (init_chess_engine(g
))
560 q
= d
->engine
->queue
;
563 for (i
= 0; q
[i
]; i
++);
565 q
= Realloc(q
, (i
+ 2) * sizeof(struct queue_s
*));
567 #ifdef HAVE_VASPRINTF
568 vasprintf(&line
, fmt
, ap
);
570 line
= Malloc(LINE_MAX
+ 1);
571 vsnprintf(line
, LINE_MAX
, fmt
, ap
);
574 q
[i
] = Malloc(sizeof(struct queue_s
));
576 q
[i
++]->status
= (s
== -1) ? d
->engine
->status
: s
;
578 d
->engine
->queue
= q
;