shithub: riscv

Download patch

ref: 27f36483d44a1f9fcf9a3ffefe688a0f12ace466
parent: 50791b875552b0dc5748aa67c95776736f6ff4b7
author: Sigrid <ftrvxmtrx@gmail.com>
date: Mon Mar 9 09:21:50 EDT 2020

sam: add $%dot (thanks kvik)

--- a/sys/src/cmd/sam/sam.h
+++ b/sys/src/cmd/sam/sam.h
@@ -400,3 +400,5 @@
 void	outTsv(Hmesg, int, vlong);
 void	outflush(void);
 int needoutflush(void);
+
+Posn	nlcount(File *f, Posn p0, Posn p1);
--- a/sys/src/cmd/sam/shell.c
+++ b/sys/src/cmd/sam/shell.c
@@ -10,6 +10,36 @@
 Buffer	cmdbuf;
 int	cmdbufpos;
 
+static void
+updateenv(File *f)
+{
+	static int fd = -1;
+	int n;
+	char buf[64], *p, *e;
+	
+	if(f == nil){
+		putenv("%", "");
+		putenv("%dot", "");
+		return;
+	}
+
+	p = Strtoc(&f->name);
+	putenv("%", p);
+	free(p);
+	
+	p = buf;
+	e = buf+sizeof(buf);
+	p = seprint(p, e, "%lud", 1+nlcount(f, 0, f->dot.r.p1));
+	p = seprint(p+1, e, "%lud", f->dot.r.p1);
+	p = seprint(p+1, e, "%lud", f->dot.r.p2);
+	n = p - buf;
+	if(fd == -1)
+	if((fd = create("/env/%dot", OWRITE, 0666)) < 0)
+		fprint(2, "updateenv create: %r\n");
+	if(write(fd, buf, n) != n)
+		fprint(2, "updateenv write: %r\n");
+}
+
 int
 plan9(File *f, int type, String *s, int nest)
 {
@@ -93,7 +123,7 @@
 			close(0);	/* so it won't read from terminal */
 			open("/dev/null", 0);
 		}
-		putenv("%", f == nil ? "" : Strtoc(&f->name));
+		updateenv(f);
 		execl(SHPATH, SH, "-c", Strtoc(&plan9cmd), nil);
 		exits("exec");
 	}