sed: make N command behave as in GNU sed
authorDenys Vlasenko <vda.linux@googlemail.com>
Mon, 16 Aug 2010 14:26:33 +0000 (16:26 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Mon, 16 Aug 2010 14:26:33 +0000 (16:26 +0200)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
editors/sed.c
testsuite/sed.tests

index 7d6e7e79f2f23fbc42c088bfc4829f3674d38955..6cf54afe9a82f68ddcbe723c389160a1a7ef5cff 100644 (file)
@@ -992,6 +992,8 @@ static void process_files(void)
                }
 
                /* actual sedding */
+               //bb_error_msg("pattern_space:'%s' next_line:'%s' cmd:%c",
+               //pattern_space, next_line, sed_cmd->cmd);
                switch (sed_cmd->cmd) {
 
                /* Print line number */
@@ -1118,10 +1120,16 @@ static void process_files(void)
                {
                        int len;
                        /* If no next line, jump to end of script and exit. */
+                       /* http://www.gnu.org/software/sed/manual/sed.html:
+                        * "Most versions of sed exit without printing anything
+                        * when the N command is issued on the last line of
+                        * a file. GNU sed prints pattern space before exiting
+                        * unless of course the -n command switch has been
+                        * specified. This choice is by design."
+                        */
                        if (next_line == NULL) {
-                               free(next_line);
-                               next_line = NULL;
-                               goto discard_line;
+                               //goto discard_line;
+                               goto discard_commands; /* GNU behavior */
                        }
                        /* Append next_line, read new next_line. */
                        len = strlen(pattern_space);
index 880fc211876d5b395b45d98f9a386a0e1f012732..61551e31ce8fc551ac2d5263e10ccbc527e10f4d 100755 (executable)
@@ -80,10 +80,18 @@ test x"$SKIP_KNOWN_BUGS" = x"" && {
 # 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"
-# N does _not_ flush pattern space, therefore c is still in there @ script end.
-testing "sed N (doesn't flush pattern space when terminating)" "sed -e 'N;p'" \
-       "a\nb\na\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'" \
+       "a\nb\na\nb\nc\n" "" "a\nb\nc\n"
+
+testing "sed N test2" "sed ':a;N;s/\n/ /;ta'" \
+       "a b c\n" "" "a\nb\nc\n"
+
+testing "sed N test3" "sed 'N;s/\n/ /'" \
+       "a b\nc\n" "" "a\nb\nc\n"
+
 testing "sed address match newline" 'sed "/b/N;/b\\nc/i woo"' \
        "a\nwoo\nb\nc\nd\n" "" "a\nb\nc\nd\n"