shithub: purgatorio

Download patch

ref: 21947578303cdb0fcb4b0cdfe80aa9dab5f6b447
parent: 254b07675e1a5348f2a8723f55e6b5b1b18a4721
author: henesy <devnull@localhost>
date: Thu Jun 3 10:40:07 EDT 2021

make 9p.zone the default grid and rework how public grids are mounted ;; introduce engridden.b ;; fix hd(1) and tl(1) quoting

--- /dev/null
+++ b/appl/cmd/engridden.b
@@ -1,0 +1,129 @@
+implement Engridden;
+
+include "sys.m";
+	sys: Sys;
+include "draw.m";
+include "string.m";
+include "env.m";
+include "arg.m";
+include "bufio.m";
+
+stderr: ref Sys->FD;
+
+Engridden: module {
+	init:	fn(nil: ref Draw->Context, argv: list of string);
+};
+
+
+# Mount ourselves into the public grid
+# $pubgridreg sets the public grid registry dialstr
+init(nil: ref Draw->Context, argv: list of string)
+{
+	sys = load Sys Sys->PATH;
+	str := load String String->PATH;
+	env := load Env Env->PATH;
+	arg := load Arg Arg->PATH;
+	bio := load Bufio Bufio->PATH;
+	Iobuf: import bio;
+
+	reg := env->getenv("pubgridreg");
+	mtpt := "/mnt/registry";
+	ndir := "/n";
+
+	arg->init(argv);
+	arg->setusage("engridden [-r tcp!registry] [-m /mnt/registry] [-n /n]");
+	while((c := arg->opt()) != 0)
+	case c {
+		'm' => mtpt = arg->earg();
+		'r' => reg = arg->earg();
+		'n' => ndir = arg->earg();
+		* => arg->usage();
+	}
+	argv = arg->argv();
+
+	if(reg == ""){
+		sys->print("err: $pubgridreg or '-r' must be provided to mount a registry\n");
+		exit;
+	}
+	
+	sys->print("Mounting public grid registry %s…\n", reg);
+
+	if(dialmount(reg, mtpt) < 0)
+		exit;
+
+	buf := bio->open(mtpt + "/index", Bufio->OREAD);
+	if(buf == nil){
+		sys->print("err: open failed → %r\n");
+		exit;
+	}
+
+Read:
+	for(;;){
+		line := buf.gets('\n');
+		if(line == "")
+			break Read;
+
+		(toks, err) := str->qtokenize(line);
+		if(err != nil){
+			sys->print("fail: can't parse line → %s\n", line);
+			continue Read;
+		}
+
+		if(len toks < 2){
+			sys->print("fail: not enough tokens in line → %s\n", line);
+			continue Read;
+		}
+
+		dialstr := hd toks;
+		name := "";
+		toks = tl toks;
+
+		# Modeled after 9p.zone's registry
+		for(; toks != nil; toks = tl toks){
+			case hd toks {
+			"description" =>
+				toks = tl toks;
+				name = hd toks;
+
+			* => 
+				;
+			}
+		}
+
+		if(dialstr == ""){
+			sys->print("fail: no dialstring in line → %s\n", line);
+			continue Read;
+		}
+
+		if(name == ""){
+			sys->print("fail: could not find 'description' value in line → %s\n", line);
+			continue Read;
+		}
+		at := ndir + "/" + name;
+
+		sys->print("Mounting %s at %s…\n", dialstr, at);
+
+		if(dialmount(dialstr, at) < 0)
+			continue Read;
+	}
+
+	sys->print("Done.\n");
+
+	exit;
+}
+
+dialmount(dialstr, mtpt: string): int {
+	(ok, conn) := sys->dial(dialstr, nil);
+	if(ok < 0){
+		sys->print("err: dial failed for %s → %r\n", dialstr);
+		return ok;
+	}
+
+	ok = sys->mount(conn.dfd, nil, mtpt, Sys->MREPL|Sys->MCREATE, nil);
+	if(ok < 0){
+		sys->print("err: mount failed for %s → %r\n", mtpt);
+		return ok;
+	}
+
+	return 0;
+}
\ No newline at end of file
--- a/appl/cmd/hd.b
+++ b/appl/cmd/hd.b
@@ -25,7 +25,10 @@
 
 	argv = tl argv;
 
-	out := str->quoted(hd str->unquoted(str->quoted(argv)) :: nil);
+	out := hd argv;
+	(toks, err) := str->qtokenize(out);
+	if(err != "" || len toks > 1)
+		out = str->quoted(out :: nil);
 	sys->print("%s\n", out);
 
 	exit;
--- a/appl/cmd/mkfile
+++ b/appl/cmd/mkfile
@@ -64,6 +64,7 @@
 	echo.dis\
 	ed.dis\
 	emuinit.dis\
+	engridden.dis\
 	env.dis\
 	export.dis\
 	fc.dis\
--- a/appl/cmd/tl.b
+++ b/appl/cmd/tl.b
@@ -20,13 +20,25 @@
 	stderr = sys->fildes(2);
 	str := load String String->PATH;
 
-	if(len argv < 2)
+	if(len argv < 3)
 		usage();
 
 	argv = tl argv;
 
-	out := str->quoted(tl str->unquoted(str->quoted(argv)));
-	sys->print("%s\n", out);
+	out := tl argv;
+	s := "";
+	for(l := out; l != nil; l = tl l){
+		w := hd l;
+		(toks, nil) := str->qtokenize(w);
+		if(len toks > 1)
+			w = str->quoted(w :: nil);
+		s += w + " ";
+	}
+	(nil, err) := str->qtokenize(s);
+	if(err != "")
+		s = str->quoted(out);
+
+	sys->print("%s\n", s);
 
 	exit;
 }
--- a/dis/gridstart
+++ b/dis/gridstart
@@ -2,7 +2,7 @@
 # Connect to public grid services
 load std
 
-run /dis/engridden
+engridden
 
 echo '/n/griddisk
 /fonts/vga/unicode.font
--- a/lib/sh/profile
+++ b/lib/sh/profile
@@ -3,6 +3,7 @@
 
 user = `{cat /dev/user}
 home = /usr/^$user
+pubgridreg = tcp!registry.9p.zone!registry
 
 bind /locale/US_Central /locale/timezone