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
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
))
91 if (send_signal_to_engine(d
->engine
->pid
, SIGINT
))
96 len
= vasprintf(&line
, format
, ap
);
98 line
= Malloc(LINE_MAX
);
99 len
= vsnprintf(line
, LINE_MAX
, format
, ap
);
109 FD_SET(d
->engine
->fd
[ENGINE_OUT_FD
], &fds
);
114 if ((n
= select(d
->engine
->fd
[ENGINE_OUT_FD
] + 1, NULL
, &fds
, NULL
,
116 if (FD_ISSET(d
->engine
->fd
[ENGINE_OUT_FD
], &fds
)) {
117 n
= write(d
->engine
->fd
[ENGINE_OUT_FD
], line
, len
);
123 if (kill(d
->engine
->pid
, 0) == -1) {
124 message(ERROR
, ANYKEY
, "Could not write to engine. "
125 "Process no longer exists.");
126 d
->engine
->status
= ENGINE_OFFLINE
;
130 message(ERROR
, ANYKEY
, "Attempt #%i. write(): %s", ++try,
136 message(NULL
, ANYKEY
, "try #%i: write() error to engine. "
137 "Expected %i, got %i.", ++try, len
, n
);
149 d
->engine
->status
= status
;
155 /* From the Xboard and screen packages. */
156 static int get_pty(char *pty_name
)
161 for (i
= 0; PTY_MAJOR
[i
]; i
++) {
164 for (n
= 0; PTY_MINOR
[n
]; n
++) {
165 sprintf(pty_name
, "%spty%c%c", _PATH_DEV
, PTY_MAJOR
[i
],
168 if ((fd
= open(pty_name
, O_RDWR
| O_NOCTTY
)) == -1)
171 sprintf(pty_name
, "%stty%c%c", _PATH_DEV
, PTY_MAJOR
[i
],
174 if (access(pty_name
, R_OK
| W_OK
) == -1) {
187 static char **parseargs(char *str
)
199 if (!(pptr
= malloc(sizeof(char *))))
202 for (i
= 0, s
= str
; *s
; lastchar
= *s
++) {
203 if ((*s
== '\"' || *s
== '\'') && lastchar
!= '\\') {
204 quote
= (quote
) ? 0 : 1;
208 if (*s
== ' ' && !quote
) {
210 pptr
= realloc(pptr
, (n
+ 2) * sizeof(char *));
211 pptr
[n
++] = strdup(arg
);
216 if ((i
+ 1) == sizeof(arg
))
225 pptr
= realloc(pptr
, (n
+ 2) * sizeof(char *));
226 pptr
[n
++] = strdup(arg
);
233 /* Is this dangerous if pty permissions are wrong? */
234 static pid_t
init_chess_engine(GAME g
, char **args
)
238 struct userdata_s
*d
= g
->data
;
240 char pty
[FILENAME_MAX
];
242 if ((to
[1] = get_pty(pty
)) == -1) {
247 if ((to
[1] = open("/dev/ptmx", O_RDWR
| O_NOCTTY
)) == -1) {
251 if (grantpt(to
[1]) == -1) {
255 if (unlockpt(to
[1]) == -1) {
264 if ((to
[0] = open(pty
, O_RDWR
, 0)) == -1)
266 if ((to
[0] = open(ptsname(to
[1]), O_RDWR
, 0)) == -1)
273 switch ((pid
= fork())) {
277 dup2(to
[0], STDIN_FILENO
);
278 dup2(from
[1], STDOUT_FILENO
);
283 dup2(STDOUT_FILENO
, STDERR_FILENO
);
284 execvp(args
[0], args
);
292 if (kill(pid
, 0) == -1)
298 d
->engine
->fd
[ENGINE_IN_FD
] = from
[0];
299 d
->engine
->fd
[ENGINE_OUT_FD
] = to
[1];
300 fcntl(d
->engine
->fd
[ENGINE_IN_FD
], F_SETFL
, O_NONBLOCK
);
301 fcntl(d
->engine
->fd
[ENGINE_OUT_FD
], F_SETFL
, O_NONBLOCK
);
302 d
->engine
->pid
= pid
;
306 void stop_engine(GAME g
)
308 struct userdata_s
*d
= g
->data
;
311 if (!d
->engine
|| d
->engine
->pid
== -1 || d
->engine
->status
== ENGINE_OFFLINE
)
314 send_to_engine(g
, ENGINE_READY
, "quit\n");
316 if (!send_signal_to_engine(d
->engine
->pid
, 0)) {
317 if (!send_signal_to_engine(d
->engine
->pid
, SIGTERM
))
318 send_signal_to_engine(d
->engine
->pid
, SIGKILL
);
321 waitpid(d
->engine
->pid
, &s
, 0);
325 void set_engine_defaults(GAME g
, char **init
)
332 for (i
= 0; init
[i
]; i
++)
333 add_engine_command(g
, ENGINE_READY
, "%s\n", init
[i
]);
336 int start_chess_engine(GAME g
)
341 struct userdata_s
*d
= g
->data
;
346 args
= parseargs(config
.engine_cmd
);
348 d
->engine
= Calloc(1, sizeof(struct engine_s
));
349 d
->engine
->status
= ENGINE_INITIALIZING
;
350 update_status_window(g
);
353 switch (init_chess_engine(g
, args
)) {
355 /* Pty allocation. */
356 message(ERROR
, ANYKEY
, "Could not allocate PTY");
357 d
->engine
->status
= ENGINE_OFFLINE
;
360 /* Could not execute engine. */
361 message(ERROR
, ANYKEY
, "%s: %s", args
[0], strerror(errno
));
362 d
->engine
->status
= ENGINE_OFFLINE
;
366 set_engine_defaults(g
, config
.einit
);
367 d
->engine
->status
= ENGINE_READY
;
371 for (i
= 0; args
[i
]; i
++)
375 update_status_window(g
);
379 static void parse_xboard_line(GAME g
, char *str
)
381 char m
[MAX_SAN_MOVE_LEN
+ 1] = {0}, *p
= m
;
382 struct userdata_s
*d
= g
->data
;
389 // 1. a2a4 (gnuchess)
393 // gnuchess echos our input move so we don't need the duplicate (above).
394 if (*p
== '.' && *(p
+ 1) == ' ' && *(p
+ 2) != '.')
397 // 1. ... a2a4 (gnuchess/engine move)
398 if (*p
== '.' && *(p
+ 1) == ' ' && *(p
+ 2) == '.') {
399 while (*p
== ' ' || *p
== '.')
403 if (strncmp(str
, "move ", 5) == 0)
406 if (strlen(p
) > MAX_SAN_MOVE_LEN
)
409 // We should now have the real move which may be in SAN or frfr format.
410 if (sscanf(p
, "%[0-9a-hprnqkxPRNBQKO+=#-]%n", m
, &count
) == 1) {
412 * For engine commands (the '|' key). Don't try and validate them.
414 if (count
!= strlen(p
))
417 if (TEST_FLAG(g
->flags
, GF_GAMEOVER
)) {
425 p
= m
+ strlen(m
) - 1;
427 // Test SAN or a2a4 format.
428 if (!isdigit(*p
) && *p
!= 'O' && *p
!= '+' && *p
!= '#' &&
429 ((*(p
- 1) != '=' || !isdigit(*(p
- 1))) &&
430 pgn_piece_to_int(*p
) == -1))
435 if ((n
= pgn_parse_move(g
, d
->b
, &p
, &frfr
)) != E_PGN_OK
) {
436 invalid_move(d
->n
+ 1, n
, m
);
440 pgn_history_add(g
, p
);
441 SET_FLAG(d
->flags
, CF_MODIFIED
);
444 if (TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
)) {
445 update_cursor(g
, g
->hindex
);
447 if (TEST_FLAG(g
->flags
, GF_GAMEOVER
)) {
452 add_engine_command(g
, ENGINE_THINKING
, "go\n");
455 if (g
->side
== g
->turn
)
462 void parse_engine_output(GAME g
, char *str
)
464 char buf
[255], *p
= buf
;
467 if (*str
== '\n' || *str
== '\r') {
471 append_enginebuf(g
, buf
);
472 parse_xboard_line(g
, buf
);
488 void send_engine_command(GAME g
)
490 struct userdata_s
*d
= g
->data
;
491 struct queue_s
**q
= d
->engine
->queue
;
494 if (!d
->engine
|| !d
->engine
->queue
)
500 if (send_to_engine(g
, q
[0]->status
, "%s", q
[0]->line
) == 0) {
501 if (g
== game
[gindex
])
502 update_status_window(g
);
508 for (i
= 0; q
[i
]; i
++) {
518 void add_engine_command(GAME g
, int s
, char *fmt
, ...)
522 struct userdata_s
*d
= g
->data
;
529 q
= d
->engine
->queue
;
532 for (i
= 0; q
[i
]; i
++);
534 q
= Realloc(q
, (i
+ 2) * sizeof(struct queue_s
*));
536 #ifdef HAVE_VASPRINTF
537 vasprintf(&line
, fmt
, ap
);
539 line
= Malloc(LINE_MAX
+ 1);
540 vsnprintf(line
, LINE_MAX
, fmt
, ap
);
543 q
[i
] = Malloc(sizeof(struct queue_s
));
545 q
[i
++]->status
= (s
== -1) ? d
->engine
->status
: s
;
547 d
->engine
->queue
= q
;