[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] sed: fix KNOWN BUG [ sed n (flushes pattern space, terminates early) ]
From:       Dengke Du <dengke.du () windriver ! com>
Date:       2016-08-31 9:32:02
Message-ID: 1472635922-249151-1-git-send-email-dengke.du () windriver ! com
[Download RAW message or body]

When use sed sub-command 'n', the GNU sed doesn't print extra character
because n flushed the pattern space.

In busybox, sed would print extra character when n hits the EOF.

Because in "case 'n'", when the next_line is null, it wouldn't execute the
break, it would go the "case 'q'" and goto the discard_commands, in
discard_commands it output the old pattern space that contain the last
character, so we see it print extra character.
------------------------------------------------------------------------
case 'n':
	if (!G.be_quiet)
        	sed_puts(pattern_space, last_gets_char);
        if (next_line) {
                free(pattern_space);
                pattern_space = next_line;
                last_gets_char = next_gets_char;
                next_line = get_next_line(&next_gets_char, &last_puts_char, last_gets_char);
                substituted = 0;
                linenum++;
                break;
        } else               # add by myself
		goto again;  # add by myself
        /* fall through */

/* Quit.  End of script, end of input. */
case 'q':
	/* Exit the outer while loop */
        free(next_line);
        next_line = NULL;
        goto discard_commands;
------------------------------------------------------------------------

So in order to comply with GNU sed, in case 'n', when the next_line is null,
we add "else" at the end of the second "if":

	goto again;

to avoid print the old pattern space which have been printed.

Signed-off-by: Dengke Du <dengke.du@windriver.com>
---
 editors/sed.c       | 3 ++-
 testsuite/sed.tests | 6 +-----
 2 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/editors/sed.c b/editors/sed.c
index 7bbf820..2e4a805 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -1282,7 +1282,8 @@ static void process_files(void)
 				substituted = 0;
 				linenum++;
 				break;
-			}
+			} else
+				goto again;
 			/* fall through */
 
 		/* Quit.  End of script, end of input. */
diff --git a/testsuite/sed.tests b/testsuite/sed.tests
index 34479e5..96ff7a5 100755
--- a/testsuite/sed.tests
+++ b/testsuite/sed.tests
@@ -73,13 +73,9 @@ testing "sed t (test/branch clears test bit)" "sed -e 's/a/b/;:loop;t loop'" \
 testing "sed T (!test/branch)" "sed -e 's/a/1/;T notone;p;: notone;p'" \
 	"1\n1\n1\nb\nb\nc\nc\n" "" "a\nb\nc\n"
 
-test x"$SKIP_KNOWN_BUGS" = x"" && {
-# Normal sed end-of-script doesn't print "c" because n flushed the pattern
-# space.  If n hits EOF, pattern space is empty when script ends.
-# Query: how does this interact with no newline at EOF?
 testing "sed n (flushes pattern space, terminates early)" "sed -e 'n;p'" \
 	"a\nb\nb\nc\n" "" "a\nb\nc\n"
-}
+
 # non-GNU sed: N does _not_ flush pattern space, therefore c is eaten @ script end
 # GNU sed: N flushes pattern space, therefore c is printed too @ script end
 testing "sed N (flushes pattern space (GNU behavior))" "sed -e 'N;p'" \
-- 
2.8.1

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic