shithub: riscv

Download patch

ref: 7ade57b678f0799ab0598632403da11fd7271b05
parent: 20b945a382fabd55f8bc54eaa796badaee8d6fd0
author: aiju <devnull@localhost>
date: Fri May 5 04:22:13 EDT 2017

sed: add -u flag that flushes output buffers before reading in further input

--- a/sys/man/1/sed
+++ b/sys/man/1/sed
@@ -10,6 +10,9 @@
 .B -g
 ]
 [
+.B -u
+]
+[
 .B -e
 .I script
 ]
@@ -44,6 +47,11 @@
 .B -g
 causes all substitutions to be global, as if suffixed
 .BR g .
+If
+.B -u
+is specified,
+.I sed
+flushes its output buffers before reading in further input.
 .PP
 A script consists of editing commands, one per line,
 of the following form:
@@ -411,3 +419,8 @@
 characters beyond a line on which a
 .L q
 command is executed.
+.PP
+.B -u
+does not work as expected if
+.B $
+addressing is used.
--- a/sys/src/cmd/sed.c
+++ b/sys/src/cmd/sed.c
@@ -96,6 +96,8 @@
 SedCom *pend = pspace+MAXCMDS;		/* End of command storage */
 SedCom *rep = pspace;			/* Current fill point */
 
+int	dollars;			/* Number of dollar addresses */
+
 Reprog	*lastre;			/* Last regular expression */
 Resub	subexp[MAXSUB];			/* sub-patterns of pattern match*/
 
@@ -136,6 +138,7 @@
 
 int	nflag;				/* Command line flags */
 int	gflag;
+int	uflag;
 
 int	dolflag;			/* Set when at true EOF */
 int	sflag;				/* Set when substitution done */
@@ -233,6 +236,9 @@
 	case 'n':
 		nflag++;
 		continue;
+	case 'u':
+		uflag++;
+		continue;
 	default:
 		quit("Unknown flag: %c", ARGC());
 	} ARGEND
@@ -681,6 +687,16 @@
 	quit(CGMES, L"r.e.-using", linebuf);
 }
 
+int
+flushout(Biobufhdr *bp, void *v, long n)
+{
+	int i;
+	
+	for(i = 0; i < nfiles; i++)
+		Bflush(fcode[i]);
+	return read(bp->fid, v, n);
+}
+
 void
 newfile(enum PTYPE type, char *name)
 {
@@ -690,6 +706,7 @@
 		if ((prog.bp = Bopen(name, OREAD)) == 0)
 			quit("Cannot open pattern-file: %s\n", name);
 		Blethal(prog.bp, nil);
+		if(uflag) Biofn(prog.bp, flushout);
 	}
 	prog.type = type;
 }
@@ -748,9 +765,10 @@
 	int c;
 	long lno;
 
-	if((c = *cp++) == '$')
+	if((c = *cp++) == '$'){
 		ap->type = A_DOL;
-	else if(c == '/') {
+		dollars++;
+	}else if(c == '/') {
 		seof = c;
 		if (ap->rp = compile())
 			ap->type = A_RE;
@@ -1341,6 +1359,7 @@
 			if((fi = Bopen(buf, OREAD)) == 0)
 				continue;
 			Blethal(fi, nil);
+			if(uflag) Biofn(fi, flushout);
 			while((c = Bgetc(fi)) >= 0)
 				Bputc(&fout, c);
 			Bterm(fi);
@@ -1389,7 +1408,7 @@
 		p = addr;
 		for (c = (peekc? peekc: Bgetrune(f)); c >= 0; c = Bgetrune(f)) {
 			if (c == '\n') {
-				if ((peekc = Bgetrune(f)) < 0 && fhead == nil)
+				if (dollars != 0 && (peekc = Bgetrune(f)) < 0 && fhead == nil)
 					dolflag = 1;
 				*p = '\0';
 				return p;
@@ -1445,6 +1464,7 @@
 		f = &stdin;
 	}
 	Blethal(f, nil);
+	if(uflag) Biofn(f, flushout);
 	fhead = fhead->next;
 	return 1;
 }