Index | Thread | Search

From:
Carsten Reith <carsten.reith@t-online.de>
Subject:
ed: behaviour not as described in manpage
To:
tech@openbsd.org
Date:
Mon, 17 Feb 2025 22:38:17 +0100

Download raw body.

Thread
  • Carsten Reith:

    ed: behaviour not as described in manpage

Hi,

if the last delimiter is omitted in a substitution expression, the % is
printed literally and not replaced by the last substitution:

mogli$ printf "a\nb\nc\n" > test.txt
mogli$ ed test.txt 
6
1s/a/hello
hello
2s/b/%
%
,n
1       hello
2       %
3       c

As far as I understand the manpage and POSIX, this should be:

mogli$ printf "a\nb\nc\n" > test.txt
mogli$ ed test.txt                  
6
1s/a/hello
hello
2s/b/%
hello
,n
1       hello
2       hello
3       c

The diff below implements this.

Index: sub.c
===================================================================
RCS file: /cvs/src/bin/ed/sub.c,v
diff -u -p -u -p -r1.18 sub.c
--- sub.c       11 Oct 2016 06:54:05 -0000      1.18
+++ sub.c       17 Feb 2025 21:32:18 -0000
@@ -86,7 +86,8 @@ extract_subst_template(void)
        char c;
	char delimiter = *ibufp++;
	 
-       if (*ibufp == '%' && *(ibufp + 1) == delimiter) {
+       if (*ibufp == '%' && 
+           (*(ibufp + 1) == delimiter || *(ibufp + 1) == '\n')) {
		ibufp++;
		if (!rhbuf)
			 seterrmsg("no previous substitution");