sed: in '/regexp/cmd1;//cmd2', cmd2 should use the same regexp as cmd1
authorDenys Vlasenko <vda.linux@googlemail.com>
Wed, 23 Aug 2017 15:56:03 +0000 (17:56 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Wed, 23 Aug 2017 15:56:03 +0000 (17:56 +0200)
function                                             old     new   delta
get_address                                          165     211     +46

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
editors/sed.c
testsuite/sed.tests

index 3fc40ceef86f44a8dc5c9cb595c794bb970689ca..7ed9937d23ce81891c4e060a33b13aeef94239cd 100644 (file)
@@ -350,10 +350,16 @@ static int get_address(const char *my_str, int *linenum, regex_t ** regex)
                if (*my_str == '\\')
                        delimiter = *++pos;
                next = index_of_next_unescaped_regexp_delim(delimiter, ++pos);
-               temp = copy_parsing_escapes(pos, next);
-               *regex = xzalloc(sizeof(regex_t));
-               xregcomp(*regex, temp, G.regex_type);
-               free(temp);
+               if (next != 0) {
+                       temp = copy_parsing_escapes(pos, next);
+                       G.previous_regex_ptr = *regex = xzalloc(sizeof(regex_t));
+                       xregcomp(*regex, temp, G.regex_type);
+                       free(temp);
+               } else {
+                       *regex = G.previous_regex_ptr;
+                       if (!G.previous_regex_ptr)
+                               bb_error_msg_and_die("no previous regexp");
+               }
                /* Move position to next character after last delimiter */
                pos += (next+1);
        }
index 05c00a99b8a441a7a6a5e24637743bdcba9df757..675cb4f10f73859bc0e3c6b822f38d41ad7bc2d3 100755 (executable)
@@ -387,6 +387,12 @@ testing "sed 's///w FILE'" \
        "" \
        "123\nqwe\nasd\n"
 
+testing "sed uses previous regexp" \
+       "sed '/w/p;//q'" \
+       "q\nw\nw\n" \
+       "" \
+       "q\nw\ne\nr\n"
+
 # testing "description" "commands" "result" "infile" "stdin"
 
 exit $FAILCOUNT