shithub: riscv

Download patch

ref: 758496ecaa42b5f6c17c0bd1e0f43189e50e0745
parent: 476a47b15c152e72aa8a6d9f9eb363931cbfd50f
author: ftrvxmtrx <ftrvxmtrx@gmail.com>
date: Sat Dec 20 20:28:14 EST 2014

sam: '^' and '_' cmds; same as '<' and '|' except that stdout goes to the command window

--- a/sys/man/1/sam
+++ b/sys/man/1/sam
@@ -496,11 +496,20 @@
 Send the range to the standard input of the
 Plan 9 command.
 .TP
+.BI ^ " Plan 9-command
+Send the standard output of the Plan 9 command
+to the command window.
+.TP
 .BI | " Plan 9-command
 Send the range to the standard input, and replace it by
 the standard output, of the
 Plan 9 command.
 .TP
+.BI _ " Plan 9-command
+Send the range to the standard input, and send the
+standard output of the Plan 9 command to the command
+window.
+.TP
 .BI \*a! " Plan 9-command
 Run the
 Plan 9 command.
@@ -515,6 +524,8 @@
 In any of
 .BR < ,
 .BR > ,
+.BR ^ ,
+.BR _ ,
 .B |
 or
 .BR ! ,
--- a/sys/src/cmd/sam/cmd.c
+++ b/sys/src/cmd/sam/cmd.c
@@ -35,6 +35,8 @@
 	'>',	0,	0,	0,	0,	aDot,	0,	linex,	plan9_cmd,
 	'<',	0,	0,	0,	0,	aDot,	0,	linex,	plan9_cmd,
 	'|',	0,	0,	0,	0,	aDot,	0,	linex,	plan9_cmd,
+	'^',	0,	0,	0,	0,	aDot,	0,	linex,	plan9_cmd,
+	'_',	0,	0,	0,	0,	aDot,	0,	linex,	plan9_cmd,
 	'=',	0,	0,	0,	0,	aDot,	0,	linex,	eq_cmd,
 	'c'|0x100,0,	0,	0,	0,	aNo,	0,	wordx,	cd_cmd,
 	0,	0,	0,	0,	0,	0,	0,	0,
@@ -76,7 +78,13 @@
 
     Again:
 	nbuf = 0;
-	if(downloaded){
+	if(cmdbufpos > cmdbuf.nc && cmdbuf.nc > 0){
+		cmdbufpos = 0;
+		bufreset(&cmdbuf);
+	}
+	if(cmdbufpos < cmdbuf.nc && cmdbuf.nc > 0)
+		bufread(&cmdbuf, cmdbufpos++, &r, 1);
+	else if(downloaded){
 		while(termoutp == terminp){
 			cmdupdate();
 			if(patset)
--- a/sys/src/cmd/sam/sam.h
+++ b/sys/src/cmd/sam/sam.h
@@ -361,6 +361,8 @@
 extern Address	addr;
 extern Buffer	snarfbuf;
 extern Buffer	plan9buf;
+extern Buffer	cmdbuf;
+extern int	cmdbufpos;
 extern List	file;
 extern List	tempfile;
 extern File	*cmd;
--- a/sys/src/cmd/sam/shell.c
+++ b/sys/src/cmd/sam/shell.c
@@ -7,6 +7,8 @@
 String	plan9cmd;	/* null terminated */
 Buffer	plan9buf;
 void	checkerrs(void);
+Buffer	cmdbuf;
+int	cmdbufpos;
 
 int
 plan9(File *f, int type, String *s, int nest)
@@ -28,7 +30,7 @@
 	}
 	if(type!='!' && pipe(pipe1)==-1)
 		error(Epipe);
-	if(type=='|')
+	if(type=='|' || type=='_')
 		snarf(f, addr.r.p1, addr.r.p2, &plan9buf, 1);
 	if((pid=fork()) == 0){
 		if(downloaded){	/* also put nasty fd's into errfile */
@@ -48,14 +50,14 @@
 			}
 		}
 		if(type != '!') {
-			if(type=='<' || type=='|')
-				dup(pipe1[1], 1);
-			else if(type == '>')
+			if(type == '>')
 				dup(pipe1[0], 0);
+			else
+				dup(pipe1[1], 1);
 			close(pipe1[0]);
 			close(pipe1[1]);
 		}
-		if(type == '|'){
+		if(type == '|' || type == '_'){
 			if(pipe(pipe2) == -1)
 				exits("pipe");
 			if((pid = fork())==0){
@@ -87,7 +89,7 @@
 			close(pipe2[0]);
 			close(pipe2[1]);
 		}
-		if(type=='<'){
+		if(type=='<' || type=='^'){
 			close(0);	/* so it won't read from terminal */
 			open("/dev/null", 0);
 		}
@@ -115,9 +117,14 @@
 		writeio(f);
 		bpipeok = 0;
 		closeio((Posn)-1);
+	}else if(type == '^' || type == '_'){
+		int nulls;
+		close(pipe1[1]);
+		bufload(&cmdbuf, cmdbufpos, pipe1[0], &nulls);
+		close(pipe1[0]);
 	}
 	retmsg = waitfor(pid);
-	if(type=='|' || type=='<')
+	if(type=='|' || type=='<' || type=='_' || type=='^')
 		if(retmsg[0]!=0)
 			warn_s(Wbadstatus, retmsg);
 	if(downloaded)