shithub: fsgen

Download patch

ref: 7c4e47bececffe6e1a62adb2fc93ff2422a9c274
parent: 67090e5d7dcfd71b9f41e40bb9fcc8c3c7846b2e
author: sirjofri <sirjofri@sirjofri.de>
date: Mon Dec 15 06:03:57 EST 2025

correct file function handler output

--- a/code.c
+++ b/code.c
@@ -22,10 +22,25 @@
 	}
 }
 
+char *invalid = "/{}";
+
+static void
+pathtostring(char *buf, int max, char *path)
+{
+	char *s;
+	strncpy(buf, path, max);
+	buf[max-1] = 0;
+	for (s = buf; *s; s++)
+		if (strchr(invalid, *s))
+			*s = '_';
+}
+
 void
 printread(VFile *file)
 {
-	print("static void\nfsread_%s(Req *r", file->path);
+	char buf[64];
+	pathtostring(buf, sizeof(buf), file->path);
+	print("static void\nfsread_%s(Req *r", buf);
 	printvars(file);
 	print(")\n");
 }
@@ -33,7 +48,9 @@
 void
 printwrite(VFile *file)
 {
-	print("static void\nfswrite_%s(Req *r", file->path);
+	char buf[64];
+	pathtostring(buf, sizeof(buf), file->path);
+	print("static void\nfswrite_%s(Req *r", buf);
 	printvars(file);
 	print(")\n");
 }
@@ -41,5 +58,54 @@
 void
 printls(VFile *file)
 {
-	print("static void\nfsls_%s(...)\n", file->path);
-}
\ No newline at end of file
+	char buf[64];
+	pathtostring(buf, sizeof(buf), file->path);
+	print("static void\nfsls_%s(...)\n", buf);
+}
+
+void
+printpre()
+{
+	print("#line 0 \"preamble.inc\"\n"
+#include "preamble.cinc"
+	);
+}
+
+extern char* file;
+
+void
+printfs()
+{
+	char buf[64];
+	char *s;
+	
+	strcpy(buf, file);
+	s = strrchr(buf, '.');
+	if (strcmp(s, ".fs") == 0)
+		*s = 0;
+	for (s = buf; *s; s++)
+		if (*s == '.')
+			*s = '_';
+	s = strrchr(buf, '/');
+	if (s)
+		s++;
+	else
+		s = buf;
+	
+	print("\n#line 0 \"fshandler.inc\"\n"
+#include "fshandler.cinc"
+	);
+	
+	print("\n#line 0 \"fsgen/code.c\"");
+	print("\n"
+	"Srv fs = {\n"
+	"	.read = fsread,\n"
+	"	.walk1 = fswalk,\n"
+	"};\n\n"
+	"Srv*\n"
+	"getfs_%s()\n", s);
+	
+	print("#line 0 \"fsfunc.inc\"\n"
+#include "fsfunc.cinc"
+	);
+}
--- a/fns.h
+++ b/fns.h
@@ -5,3 +5,5 @@
 void printread(VFile*);
 void printwrite(VFile*);
 void printls(VFile*);
+void printfs(void);
+void printpre(void);
--- /dev/null
+++ b/fsfunc.inc
@@ -1,0 +1,3 @@
+{
+	return &fs;
+}
--- /dev/null
+++ b/fshandler.inc
@@ -1,0 +1,11 @@
+static void
+fsread(Req *r)
+{
+	respond(r, nil);
+}
+
+static void
+fswrite(Req *r)
+{
+	respond(r, nil);
+}
--- a/main.c
+++ b/main.c
@@ -150,8 +150,11 @@
 		sysfatal("open: %r");
 	
 	vfileinit();
+	printpre();
 	
 	process(bin);
 	
 	foreachfile(p, nil);
+	
+	printfs();
 }
--- a/mkfile
+++ b/mkfile
@@ -7,5 +7,11 @@
 	code.$O\
 
 HFILES=dat.h fns.h
+CLEANFILES=fsfunc.cinc fshandler.cinc preamble.cinc
 
 </sys/src/cmd/mkone
+
+code.$O: fsfunc.cinc fshandler.cinc preamble.cinc
+
+%.cinc: %.inc
+	sed 's/^/"/;s/$/\\n"/;' $stem.inc > $target
--- /dev/null
+++ b/preamble.inc
@@ -1,0 +1,5 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <fcall.h>
+#include <9p.h>
--