shithub: moonfish

Download patch

ref: 681e6e4b247ac29fad2fade5ba59cc154a140ddc
parent: 19498499a153f6b7508bad8b300aecf3021d2ee0
author: zamfofex <zamfofex@twdb.moe>
date: Sun Mar 2 05:32:08 EST 2025

port more tools to 9front

--- a/mkfile
+++ b/mkfile
@@ -3,8 +3,21 @@
 
 </$objtype/mkfile
 
-moonfish: chess.$O search.$O main.$O
+moonfish:
 	$LD $LDFLAGS -o $target $prereq
+lichess:
+	$LD $LDFLAGS -o $target $prereq
+chat:
+	$LD $LDFLAGS -o $target $prereq
 
 %.$O: %.c moonfish.h
-	$CC $CFLAGS -I/sys/include/npe -Dmoonfish_plan9 $stem.c
+	$CC $CFLAGS -I/sys/include/npe -Dmoonfish_plan9 -o $stem.$O $stem.c
+
+moonfish: chess.$O search.$O main.$O
+lichess: chess.$O tools/lichess.$O tools/utils.$O tools/https.$O
+chat: chess.$O tools/chat.$O tools/utils.$O tools/https.$O
+
+tools/lichess.$O tools/chat.$O: tools/tools.h tools/https.h
+
+tools/utils.$O: tools/tools.h
+tools/https.$O: tools/https.h
--- a/tools/chat.c
+++ b/tools/chat.c
@@ -43,11 +43,13 @@
 		
 		length = 0;
 		while (line[length] != ' ' && line[length] != 0) length++;
-		(*command)[*count] = strndup(line, length);
+		(*command)[*count] = malloc(length + 1);
 		if ((*command)[*count] == NULL) {
-			perror("strndup");
+			perror("malloc");
 			exit(1);
 		}
+		(*command)[*count][0] = 0;
+		strncat((*command)[*count], line, length);
 		line += length;
 		
 		while (*line == ' ') line++;
--- a/tools/https.c
+++ b/tools/https.c
@@ -5,6 +5,70 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#ifdef moonfish_plan9
+
+#include <libsec.h>
+
+#define TLS_WANT_POLLIN (-2)
+#define TLS_WANT_POLLOUT (-2)
+
+struct tls {
+	int fd;
+};
+
+static struct tls *tls_client(void)
+{
+	struct tls *tls;
+	tls = malloc(sizeof *tls);
+	return tls;
+}
+
+static int tls_connect(struct tls *tls, char *host, char *port)
+{
+	TLSconn conn = {0};
+	int fd;
+	
+	fd = dial(netmkaddr(host, "tcp", port), NULL, NULL, NULL);
+	if (fd < 0) return -1;
+	
+	tls->fd = tlsClient(fd, &conn);
+	if (tls->fd < 0) return -1;
+	free(conn.cert);
+	free(conn.sessionID);
+	return 0;
+}
+
+static int tls_close(struct tls *tls)
+{
+	return close(tls->fd);
+}
+
+static void tls_free(struct tls *tls)
+{
+	free(tls);
+}
+
+static ssize_t tls_read(struct tls *tls, void *buffer, size_t count)
+{
+	ssize_t n;
+	n = read(tls->fd, buffer, count);
+	if (n < 0) return 0;
+	return n;
+}
+
+static ssize_t tls_write(struct tls *tls, void *buffer, size_t count)
+{
+	return write(tls->fd, buffer, count);
+}
+
+static char *tls_error(struct tls *tls)
+{
+	(void) tls;
+	return "TLS error";
+}
+
+#endif
+
 #include "https.h"
 
 int moonfish_read(struct tls *tls, void *data0, size_t length)
@@ -169,7 +233,12 @@
 
 void moonfish_close(struct tls *tls)
 {
-	if (tls_close(tls)) {
+	int result;
+	
+	do result = tls_close(tls);
+	while (result == TLS_WANT_POLLIN || result == TLS_WANT_POLLOUT);
+	
+	if (result) {
 		fprintf(stderr, "%s\n", tls_error(tls));
 		exit(1);
 	}
--- a/tools/https.h
+++ b/tools/https.h
@@ -1,7 +1,12 @@
 /* moonfish is licensed under the AGPL (v3 or later) */
 /* copyright 2025 zamfofex */
 
+#ifndef moonfish_plan9
 #include <tls.h>
+#else
+struct tls;
+#pragma incomplete struct tls
+#endif
 
 int moonfish_read(struct tls *tls, void *data, size_t length);
 int moonfish_write(struct tls *tls, void *data, size_t length);
--- a/tools/lichess.c
+++ b/tools/lichess.c
@@ -4,11 +4,62 @@
 #include <string.h>
 #include <stdlib.h>
 #include <pthread.h>
-#include <signal.h>
+
+#ifndef moonfish_plan9
+
 #include <sys/wait.h>
+#include <signal.h>
 
 #include <cjson/cJSON.h>
 
+#else
+
+#include <json.h>
+
+#define valuestring s
+#define valueint n
+
+typedef JSON cJSON;
+
+static void cJSON_Delete(cJSON *json)
+{
+	jsonfree(json);
+}
+
+static cJSON *cJSON_GetObjectItem(cJSON *json, char *name)
+{
+	return jsonbyname(json, name);
+}
+
+static int cJSON_IsNull(cJSON *json)
+{
+	return json->t == JSONNull;
+}
+
+static int cJSON_IsNumber(cJSON *json)
+{
+	return json->t == JSONNumber;
+}
+
+static int cJSON_IsObject(cJSON *json)
+{
+	return json->t == JSONObject;
+}
+
+static int cJSON_IsString(cJSON *json)
+{
+	return json->t == JSONString;
+}
+
+static cJSON *cJSON_ParseWithOpts(char *string, const char **end, int check_null)
+{
+	(void) check_null;
+	*end = string + strlen(string);
+	return jsonparse(string);
+}
+
+#endif
+
 #include "../moonfish.h"
 #include "tools.h"
 #include "https.h"
@@ -31,7 +82,7 @@
 	exit(1);
 }
 
-static pthread_mutex_t moonfish_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t moonfish_mutex;
 
 static void moonfish_handle_game_events(struct tls *tls, struct moonfish_game *game, FILE *in, FILE *out)
 {
@@ -472,15 +523,18 @@
 	return username;
 }
 
+#ifndef moonfish_plan9
+
 static void moonfish_signal(int signal)
 {
 	(void) signal;
-	
 	for (;;) {
 		if (waitpid(-1, NULL, WNOHANG) <= 0) break;
 	}
 }
 
+#endif
+
 int main(int argc, char **argv)
 {
 	static struct moonfish_command cmd = {
@@ -513,8 +567,10 @@
 	int command_count;
 	struct tls *tls;
 	char *username;
-	struct sigaction action;
 	char *value;
+#ifndef moonfish_plan9
+	struct sigaction action;
+#endif
 	
 	command = moonfish_args(&cmd, argc, argv);
 	command_count = argc - (command - argv);
@@ -553,6 +609,13 @@
 		}
 	}
 	
+	if (pthread_mutex_init(&moonfish_mutex, NULL)) {
+		fprintf(stderr, "could not initialise mutex\n");
+		return 1;
+	}
+	
+#ifndef moonfish_plan9
+	
 	action.sa_handler = &moonfish_signal;
 	sigemptyset(&action.sa_mask);
 	action.sa_flags = 0;
@@ -561,6 +624,8 @@
 		perror("sigaction");
 		return 1;
 	}
+	
+#endif
 	
 	tls = moonfish_connect(cmd.args[0].value, cmd.args[1].value);
 	moonfish_request(tls, cmd.args[0].value, "GET /api/stream/event", token, NULL, 0);
--- a/tools/utils.c
+++ b/tools/utils.c
@@ -8,6 +8,32 @@
 #include <limits.h>
 #include <fcntl.h>
 
+#ifdef moonfish_plan9
+
+#define execvp exec
+#define dup2 dup
+#define fork() rfork(RFFDG | RFREND | RFPROC | RFNOWAIT | RFENVG)
+#define O_WRONLY OWRITE
+
+/* note: the following assumes '_SC_OPEN_MAX' only */
+#define sysconf(which) 2048
+
+static char *strtok_r(char *src, char *delim, char **ptr)
+{
+	char *r;
+	size_t n;
+	if (src != NULL) *ptr = src;
+	*ptr += strspn(*ptr, delim);
+	if (**ptr == 0) return NULL;
+	r = *ptr;
+	n = strcspn(r, delim);
+	r[n] = 0;
+	*ptr += n + 1;
+	return r;
+}
+
+#endif
+
 #include "../moonfish.h"
 #include "tools.h"
 
@@ -17,14 +43,10 @@
 	int pid;
 	long int count, i;
 	
-	if (pipe(p1) < 0) {
+	if (pipe(p1) < 0 || pipe(p2) < 0) {
 		perror("pipe");
 		exit(1);
 	}
-	if (pipe(p2) < 0) {
-		perror("pipe");
-		exit(1);
-	}
 	
 	pid = fork();
 	if (pid < 0) {
@@ -82,15 +104,13 @@
 		exit(1);
 	}
 	
-	errno = 0;
-	if (setvbuf(*in, NULL, _IOLBF, 0)) {
-		if (errno != 0) perror("setvbuf");
+	if (setvbuf(*in, NULL, _IONBF, 0)) {
+		perror("setvbuf");
 		exit(1);
 	}
 	
-	errno = 0;
-	if (setvbuf(*out, NULL, _IOLBF, 0)) {
-		if (errno != 0) perror("setvbuf");
+	if (setvbuf(*out, NULL, _IONBF, 0)) {
+		perror("setvbuf");
 		exit(1);
 	}
 }
@@ -109,7 +129,10 @@
 	for (;;) {
 		
 		line = moonfish_next(file);
-		if (line == NULL) exit(1);
+		if (line == NULL) {
+			fprintf(stderr, "file closed unexpectedly\n");
+			exit(1);
+		}
 		
 		arg = strtok_r(line, "\r\n\t ", &buffer);
 		if (arg == NULL) continue;
--