shithub: moonfish

Download patch

ref: bbf7408073c3175bba723758d3038c4c14a2ba71
parent: c80f7beebc91dfce3d37874c79f698705d751dd9
author: zamfofex <zamfofex@twdb.moe>
date: Sun Jan 21 12:29:58 EST 2024

refactor 'moonfish_fork' (for tools)

--- a/tools/analyse.c
+++ b/tools/analyse.c
@@ -33,7 +33,6 @@
 	FILE *in, *out;
 	char *fen;
 	int ox, oy;
-	int out_fd;
 	int offset;
 	char pv[32];
 };
@@ -384,7 +383,13 @@
 	
 	fancy = data;
 	
-	fds.fd = fancy->out_fd;
+	fds.fd = fileno(fancy->out);
+	if (fds.fd < 0)
+	{
+		perror(fancy->argv0);
+		exit(1);
+	}
+	
 	fds.events = POLLIN;
 	
 	pthread_mutex_lock(fancy->mutex);
@@ -638,7 +643,6 @@
 	static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 	static pthread_mutex_t read_mutex = PTHREAD_MUTEX_INITIALIZER;
 	
-	int in_fd, out_fd;
 	struct moonfish_fancy *fancy;
 	pthread_t thread;
 	struct termios termios;
@@ -646,6 +650,7 @@
 	int ch, ch0;
 	int x1, y1;
 	struct moonfish_move move;
+	int error;
 	
 	if (argc < 3)
 	{
@@ -653,12 +658,6 @@
 		return 1;
 	}
 	
-	if (moonfish_spawn(argv[0], argv + 2, &in_fd, &out_fd))
-	{
-		perror(argv[0]);
-		return 1;
-	}
-	
 	if (tcgetattr(0, &moonfish_termios))
 	{
 		perror(argv[0]);
@@ -701,7 +700,6 @@
 	fancy->argv0 = argv[0];
 	fancy->mutex = &mutex;
 	fancy->read_mutex = &read_mutex;
-	fancy->out_fd = out_fd;
 	fancy->offset = 0;
 	fancy->pv[0] = 0;
 	
@@ -708,34 +706,8 @@
 	fancy->x = 0;
 	fancy->y = 0;
 	
-	fancy->in = fdopen(in_fd, "w");
-	if (fancy->in == NULL)
-	{
-		perror(argv[0]);
-		return 1;
-	}
+	moonfish_spawn(argv[0], argv + 2, &fancy->in, &fancy->out);
 	
-	fancy->out = fdopen(out_fd, "r");
-	if (fancy->out == NULL)
-	{
-		perror(argv[0]);
-		return 1;
-	}
-	
-	errno = 0;
-	if (setvbuf(fancy->in, NULL, _IOLBF, 0))
-	{
-		if (errno) perror(argv[0]);
-		return 1;
-	}
-	
-	errno = 0;
-	if (setvbuf(fancy->out, NULL, _IOLBF, 0))
-	{
-		if (errno) perror(argv[0]);
-		return 1;
-	}
-	
 	fancy->i = 0;
 	fancy->count = 1;
 	
@@ -792,7 +764,12 @@
 	
 	if (scanf("[%d;%dR", &fancy->oy, &fancy->ox) != 2) return 1;
 	
-	pthread_create(&thread, NULL, &moonfish_start, fancy);
+	error = pthread_create(&thread, NULL, &moonfish_start, fancy);
+	if (error != 0)
+	{
+		fprintf(stderr, "%s: %s\n", fancy->argv0, strerror(error));
+		exit(1);
+	}
 	
 	printf("\x1B[?1000h");
 	fflush(stdout);
--- a/tools/lichess.c
+++ b/tools/lichess.c
@@ -657,7 +657,6 @@
 {
 	char request[4096];
 	struct moonfish_game *game;
-	int in_fd, out_fd;
 	FILE *in, *out;
 	br_ssl_client_context ctx;
 	br_sslio_context io_ctx;
@@ -667,40 +666,8 @@
 	
 	game = data;
 	
-	if (moonfish_spawn(game->argv0, game->argv, &in_fd, &out_fd))
-	{
-		perror(game->argv0);
-		exit(1);
-	}
+	moonfish_spawn(game->argv0, game->argv, &in, &out);
 	
-	in = fdopen(in_fd, "w");
-	if (in == NULL)
-	{
-		perror(game->argv0);
-		exit(1);
-	}
-	
-	out = fdopen(out_fd, "r");
-	if (out == NULL)
-	{
-		perror(game->argv0);
-		exit(1);
-	}
-	
-	errno = 0;
-	if (setvbuf(in, NULL, _IOLBF, 0))
-	{
-		if (errno) perror(game->argv0);
-		exit(1);
-	}
-	
-	errno = 0;
-	if (setvbuf(out, NULL, _IOLBF, 0))
-	{
-		if (errno) perror(game->argv0);
-		exit(1);
-	}
-	
 	buffer = malloc(BR_SSL_BUFSIZE_BIDI);
 	
 	snprintf(request, sizeof request, "GET /api/bot/game/stream/%s", game->id);
@@ -749,6 +716,7 @@
 	pthread_t thread;
 	struct moonfish_chess chess;
 	int invalid;
+	int error;
 	
 	root = NULL;
 	
@@ -806,7 +774,12 @@
 			game->argv = argv;
 			game->username = username;
 			
-			pthread_create(&thread, NULL, &moonfish_handle_game, game);
+			error = pthread_create(&thread, NULL, &moonfish_handle_game, game);
+			if (error != 0)
+			{
+				fprintf(stderr, "%s: %s\n", argv0, strerror(error));
+				exit(1);
+			}
 			
 			continue;
 		}
--- a/tools/play.c
+++ b/tools/play.c
@@ -312,7 +312,6 @@
 	static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 	static char names[4096] = "";
 	
-	int in_fd, out_fd;
 	FILE *in, *out;
 	struct moonfish_fancy *fancy;
 	char *arg;
@@ -324,6 +323,7 @@
 	int x1, y1;
 	char *name;
 	struct moonfish_move move;
+	int error;
 	
 	name = names;
 	
@@ -333,11 +333,7 @@
 		return 1;
 	}
 	
-	if (moonfish_spawn(argv[0], argv + 2, &in_fd, &out_fd))
-	{
-		perror(argv[0]);
-		return 1;
-	}
+	moonfish_spawn(argv[0], argv + 2, &in, &out);
 	
 	if (tcgetattr(0, &moonfish_termios))
 	{
@@ -401,34 +397,6 @@
 	fancy->our_time *= 60;
 	fancy->their_time = fancy->our_time;
 	
-	in = fdopen(in_fd, "w");
-	if (in == NULL)
-	{
-		perror(argv[0]);
-		return 1;
-	}
-	
-	out = fdopen(out_fd, "r");
-	if (out == NULL)
-	{
-		perror(argv[0]);
-		return 1;
-	}
-	
-	errno = 0;
-	if (setvbuf(in, NULL, _IOLBF, 0))
-	{
-		if (errno) perror(argv[0]);
-		return 1;
-	}
-	
-	errno = 0;
-	if (setvbuf(out, NULL, _IOLBF, 0))
-	{
-		if (errno) perror(argv[0]);
-		return 1;
-	}
-	
 	fprintf(in, "uci\n");
 	
 	for (;;)
@@ -452,7 +420,6 @@
 		if (strcmp(arg, "name")) continue;
 		arg = strtok(NULL, "\r\n");
 		
-		while (strchr("\r\n\t ", *arg) != NULL) { }
 		if (*arg != 0) fancy->their_name = strdup(arg);
 	}
 	
@@ -485,7 +452,12 @@
 	
 	moonfish_chess(&fancy->chess);
 	
-	pthread_create(&thread, NULL, &moonfish_start, fancy);
+	error = pthread_create(&thread, NULL, &moonfish_start, fancy);
+	if (error != 0)
+	{
+		fprintf(stderr, "%s: %s\n", fancy->argv0, strerror(error));
+		exit(1);
+	}
 	
 	if (fancy->white != fancy->chess.white)
 	{
--- a/tools/tools.h
+++ b/tools/tools.h
@@ -6,7 +6,7 @@
 
 #include <stdio.h>
 
-int moonfish_spawn(char *argv0, char **argv, int *in, int *out);
+void moonfish_spawn(char *argv0, char **argv, FILE **in, FILE **out);
 char *moonfish_next(FILE *file);
 char *moonfish_wait(FILE *file, char *name);
 
--- a/tools/utils.c
+++ b/tools/utils.c
@@ -9,7 +9,7 @@
 
 #include "tools.h"
 
-int moonfish_spawn(char *argv0, char **argv, int *in, int *out)
+static int moonfish_fork(char *argv0, char **argv, int *in_fd, int *out_fd)
 {
 	int p1[2], p2[2];
 	int pid, fd;
@@ -22,8 +22,8 @@
 	
 	if (pid)
 	{
-		*in = p1[1];
-		*out = p2[0];
+		*in_fd = p1[1];
+		*out_fd = p2[0];
 		close(p1[0]);
 		close(p2[1]);
 		return 0;
@@ -49,6 +49,45 @@
 	fprintf(stderr, "%s: %s: %s\n", argv0, argv[0], strerror(errno));
 	
 	exit(1);
+}
+
+void moonfish_spawn(char *argv0, char **argv, FILE **in, FILE **out)
+{
+	int in_fd, out_fd;
+	
+	if (moonfish_fork(argv0, argv, &in_fd, &out_fd))
+	{
+		perror(argv0);
+		exit(1);
+	}
+	
+	*in = fdopen(in_fd, "w");
+	if (*in == NULL)
+	{
+		perror(argv0);
+		exit(1);
+	}
+	
+	*out = fdopen(out_fd, "r");
+	if (*out == NULL)
+	{
+		perror(argv0);
+		exit(1);
+	}
+	
+	errno = 0;
+	if (setvbuf(*in, NULL, _IOLBF, 0))
+	{
+		if (errno) perror(argv0);
+		exit(1);
+	}
+	
+	errno = 0;
+	if (setvbuf(*out, NULL, _IOLBF, 0))
+	{
+		if (errno) perror(argv0);
+		exit(1);
+	}
 }
 
 char *moonfish_next(FILE *file)
--