#include "gnugo.h"
#include <ctype.h>
+#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define _XOPEN_SOURCE 500
#if READLINE
#include <readline/history.h>
showterri display territory\n\
"
+#define MASTER_FIFO_NAME "/tmp/sgkgo-fifo-master"
+#define SLAVE_FIFO_NAME "/tmp/sgkgo-fifo-slave"
+
/* ANSI color codes */
#define RESETCOLOR "\x1B[0m"
#define WHITECOLOR "\x1B[41m"
{
int sz;
+ int transmit_fifo_fd, receive_fifo_fd;
+ printf("DEBUG: Attempting to setup connection with other player.\n");
+ if (mknod(MASTER_FIFO_NAME, S_IFIFO | 0666, 0) == 0) {
+ printf("DEBUG: We are the master. Created %s.\n", MASTER_FIFO_NAME);
+ // TODO: Choose black for the master.
+ printf("DEBUG: Waiting for other player to connect.\n");
+ transmit_fifo_fd = open(MASTER_FIFO_NAME, O_WRONLY);
+ printf("DEBUG: Slave connected to our FIFO. Now we connect to the slave's FIFO.\n");
+ receive_fifo_fd = open(SLAVE_FIFO_NAME, O_RDONLY);
+ printf("DEBUG: Bidirectional communication established.\n");
+ } else {
+ printf("DEBUG: Failed to create master FIFO. Perhaps we are the slave?\n");
+ // TODO: Choose white for the slave.
+ if (mknod(SLAVE_FIFO_NAME, S_IFIFO | 0666, 0) == 0) {
+ printf("DEBUG: We are the slave. Created %s.\n", SLAVE_FIFO_NAME);
+ receive_fifo_fd = open(MASTER_FIFO_NAME, O_RDONLY);
+ printf("DEBUG: We connected to master's FIFO. Now we wait for master's connection to our FIFO.\n");
+ transmit_fifo_fd = open(SLAVE_FIFO_NAME, O_WRONLY);
+ printf("DEBUG: Bidirectional communication established.\n");
+ } else {
+ printf("DEBUG: Failed to create slave FIFO. This is an unrecoverable error.\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ // Allow both master and slave to attempt removal. Ignore error code since
+ // this is a best-effort situation for now.
+ unlink(MASTER_FIFO_NAME);
+ unlink(SLAVE_FIFO_NAME);
+while(1){sleep(1);}
+exit(EXIT_SUCCESS);
+
setvbuf(stdout, (char*)NULL, _IONBF, 0); /* No buffering. */
sgftree = *tree;
do_play_twoplayer(gameinfo);
printf("\nThanks! for playing GNU Go.\n\n");
+ close(transmit_fifo_fd);
+ close(receive_fifo_fd);
+
/* main() frees the tree and we might have changed it. */
*tree = sgftree;
}