code: drawterm

Download patch

ref: bcf1eb425dd4c90a3bfcd004f6aee3854259da78
parent: f9ae0c837bf8351037689f1985c1a52c1570ba30
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Feb 18 10:06:16 EST 2024

Add -9 flag for 9p-mode (just exportfs on standard input/output)

With the -9 flag, we can run drawterm on a remote system
over ssh, and have it export the remotes namespace.

Thru the /cmd interface (see os(1)), one can launch native
programs on the remote host and access the remote file system.

--- a/cpu.c
+++ b/cpu.c
@@ -27,6 +27,7 @@
 static int	norcpu;
 static int	nokbd;
 static int	nogfx;
+static int	nineflag;
 
 static char	*ealgs = "rc4_256 sha1";
 
@@ -176,7 +177,7 @@
 	atexit(rcpuexit);
 
 	/* Begin serving the namespace */
-	exportfs(fd);
+	exportfs(fd, fd);
 }
 
 void
@@ -236,13 +237,13 @@
 	write(fd, "OK", 2);
 
 	/* Begin serving the gnot namespace */
-	exportfs(fd);
+	exportfs(fd, fd);
 }
 
 void
 usage(void)
 {
-	fprint(2, "usage: %s [-GBO] "
+	fprint(2, "usage: %s [-9GBO] "
 		"[-h host] [-u user] [-a authserver] [-s secstore] "
 		"[-e 'crypt hash'] [-k keypattern] "
 		"[-p] [-t timeout] "
@@ -262,12 +263,13 @@
 	char *s;
 
 	user = getenv("USER");
-	if((pass = getenv("PASS")) != nil)
-		remove("/env/PASS");
 	host = getenv("cpu");
 	authserver = getenv("auth");
 
 	ARGBEGIN{
+	case '9':
+		nineflag = 1;
+		/* wet floor */
 	case 'G':
 		nogfx = 1;
 		/* wet floor */
@@ -320,8 +322,8 @@
 		break;
 	case 'g':
 		/* X11 geometry string
-			[=][<width>{xX}<height>][{+-}<xoffset>{+-}<yoffset>]
-			*/
+		 *	[=][<width>{xX}<height>][{+-}<xoffset>{+-}<yoffset>]
+		 */
 		geometry = EARGF(usage());
 		break;
 	default:
@@ -330,6 +332,14 @@
 
 	if(argc != 0)
 		usage();
+
+	if(nineflag){
+		exportfs(lfdfd(0), lfdfd(1));
+		return;
+	}
+
+	if((pass = getenv("PASS")) != nil)
+		remove("/env/PASS");
 
 	if(!nogfx)
 		guimain();
--- a/drawterm.1
+++ b/drawterm.1
@@ -6,7 +6,7 @@
 .SH SYNOPSIS
 .B drawterm
 [
-.B -GBO
+.B -9GBO
 ] [
 .B -h
 .I host
@@ -49,6 +49,12 @@
 .PP
 The options are:
 .PD
+
+.TP
+.B -9
+Start drawterm in 9P mode;
+exporting the namespace over 9P on standard input and output.
+Graphics and keyboard input are disabled.
 
 .TP
 .B -G
--- a/drawterm.h
+++ b/drawterm.h
@@ -2,7 +2,7 @@
 extern char *secstore;
 extern char *secstorefetch(char *addr, char *owner, char *passwd);
 extern char *authserver;
-extern int exportfs(int);
+extern int exportfs(int, int);
 extern int dialfactotum(void);
 extern char *getuser(void);
 extern void cpumain(int, char**);
--- a/exportfs/exportfs.c
+++ b/exportfs/exportfs.c
@@ -9,7 +9,6 @@
 #define Extern
 #include "exportfs.h"
 
-/* #define QIDPATH	((1LL<<48)-1) */
 #define QIDPATH	((((vlong)1)<<48)-1)
 vlong newqid = 0;
 
@@ -21,15 +20,16 @@
 int	qidcnt;
 int	qfreecnt;
 int	ncollision;
-int	netfd;
+int	netfd[2];
 
 int
-exportfs(int fd)
+exportfs(int rfd, int wfd)
 {
 	char buf[ERRMAX], ebuf[ERRMAX];
 	Fsrpc *r;
 	int i, n;
 
+
 	fcalls[Tversion] = Xversion;
 	fcalls[Tauth] = Xauth;
 	fcalls[Tflush] = Xflush;
@@ -45,22 +45,18 @@
 	fcalls[Twstat] = Xwstat;
 
 	srvfd = -1;
-	netfd = fd;
-	//dbg = 1;
+	netfd[0] = rfd;
+	netfd[1] = wfd;
 
 	strcpy(buf, "this is buf");
 	strcpy(ebuf, "this is ebuf");
 	DEBUG(DFD, "exportfs: started\n");
 
-//	rfork(RFNOTEG);
-
-	messagesize = iounit(netfd);
+	messagesize = iounit(rfd);
 	if(messagesize == 0)
 		messagesize = IOUNIT+IOHDRSZ;
 
 	Workq = emallocz(sizeof(Fsrpc)*Nr_workbufs);
-//	for(i=0; i<Nr_workbufs; i++)
-//		Workq[i].buf = emallocz(messagesize);
 	fhash = emallocz(sizeof(Fid*)*FHASHSIZE);
 
 	fmtinstall('F', fcallfmt);
@@ -78,7 +74,7 @@
 			fatal("Out of service buffers");
 			
 		DEBUG(DFD, "read9p...");
-		n = read9pmsg(netfd, r->buf, messagesize);
+		n = read9pmsg(netfd[0], r->buf, messagesize);
 		if(n <= 0)
 			fatal(nil);
 
@@ -122,7 +118,7 @@
 	if(data == nil)
 		fatal(Enomem);
 	n = convS2M(t, data, messagesize);
-	if((m=write(netfd, data, n))!=n){
+	if((m=write(netfd[1], data, n))!=n){
 		iprint("wrote %d got %d (%r)\n", n, m);
 		fatal("write");
 	}
--- a/exportfs/exportsrv.c
+++ b/exportfs/exportsrv.c
@@ -15,7 +15,7 @@
 char Eversion[] = "Bad 9P2000 version";
 
 void*
-emallocz(ulong n)
+emallocz(uint n)
 {
 	void *v;
 
--- a/kern/devlfd-win32.c
+++ b/kern/devlfd-win32.c
@@ -23,6 +23,26 @@
 	return c;
 }
 
+/*
+ * only good for stdin/stdout/stderr
+ */
+int
+lfdfd(int fd)
+{
+	HANDLE h;
+
+	switch(fd){
+	case 0: h = GetStdHandle(STD_INPUT_HANDLE); break;
+	case 1: h = GetStdHandle(STD_OUTPUT_HANDLE); break;
+	case 2: h = GetStdHandle(STD_ERROR_HANDLE); break;
+	default:
+		return -1;
+	}
+	if(h == INVALID_HANDLE_VALUE)
+		return -1;
+	return newfd(lfdchan((void*)h));
+}
+
 static Chan*
 lfdattach(char *x)
 {