/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* This is GNU Go, a Go program. Contact gnugo@gnu.org, or see *
* http://www.gnu.org/software/gnugo/ for more information. *
* Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, *
* 2008 and 2009 by the Free Software Foundation. *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation - version 3 *
* or (at your option) any later version. *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License in file COPYING for more details. *
* You should have received a copy of the GNU General Public *
* License along with this program; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02111, USA. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* This program uses two pipes to communicate with the
* GNU Go client. To an external client it appears to
* be a gtp engine. It accomplishes this by intercepting
* gtp commands and passing them on to GNU Go.
* This program in and of itself is not useful but it
* can be the basis of useful programs. Example: hack
* in gmp.c and get a gtp / gmp mediator.
* Pipe a: client --> gnugo
* Pipe b: gnugo --> client
void error(const char *msg
);
#define TELL_GNUGO(x) if (verbose) fprintf(stderr, "%s", x); \
if (fprintf(to_gnugo_stream, "%s", x) < 0) \
error ("can't write command in to_gnugo_stream"); \
#define ASK_GNUGO(x) if (!fgets(x, 128, from_gnugo_stream)) \
error("can't get response");
char gnugo_line
[128], client_line
[128];
FILE *to_gnugo_stream
, *from_gnugo_stream
;
error("can't open pipe a");
error("can't open pipe b");
error("fork failed (try chopsticks)");
/* Attach pipe a to stdin */
if (dup2(pfd_a
[0], 0) == -1)
error("dup pfd_a[0] failed");
/* attach pipe b to stdout" */
if (dup2(pfd_b
[1], 1) == -1)
error("dup pfd_b[1] failed");
execlp("gnugo", "gnugo", "--mode", "gtp", "--quiet", NULL
);
/* We use stderr to communicate with the client since stdout is needed. */
/* Attach pipe a to to_gnugo_stream */
to_gnugo_stream
= fdopen(pfd_a
[1], "w");
/* Attach pipe b to from_gnugo_stream */
from_gnugo_stream
= fdopen(pfd_b
[0], "r");
if (!fgets(client_line
, 128, stdin
)
|| !strncmp(client_line
, "quit", 4)) {
if (!strncmp(client_line
, "genmove_black", 13)) {
const char delimiters
[] = " \t\r\n";
TELL_GNUGO("top_moves_black\n");
token
= strtok(gnugo_line
, delimiters
);
token
= strtok(NULL
, delimiters
);
length
= strlen(gnugo_line
);
length
= strlen(gnugo_line
);
fprintf(stderr
, "vanilla: %s\n", msg
);