sed: fix a command with multible trailing backslashes
authorDenys Vlasenko <vda.linux@googlemail.com>
Tue, 11 May 2010 23:49:04 +0000 (01:49 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 11 May 2010 23:49:04 +0000 (01:49 +0200)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
editors/sed.c
testsuite/sed.tests
testsuite/testing.sh [changed mode: 0755->0644]

index 30ab8c9fbef34378c8240817bb395c4030207189..4bd6e0168d8fc3366ec8bc9c7baf388ac4b69e23 100644 (file)
@@ -487,7 +487,7 @@ static const char *parse_cmd_args(sed_cmd_t *sed_cmd, const char *cmdstr)
 static void add_cmd(const char *cmdstr)
 {
        sed_cmd_t *sed_cmd;
-       int temp;
+       unsigned len, n;
 
        /* Append this line to any unfinished line from last time. */
        if (G.add_cmd_line) {
@@ -496,12 +496,14 @@ static void add_cmd(const char *cmdstr)
                cmdstr = G.add_cmd_line = tp;
        }
 
-       /* If this line ends with backslash, request next line. */
-       temp = strlen(cmdstr);
-       if (temp && cmdstr[--temp] == '\\') {
+       /* If this line ends with unescaped backslash, request next line. */
+       n = len = strlen(cmdstr);
+       while (n && cmdstr[n-1] == '\\')
+               n--;
+       if ((len - n) & 1) { /* if odd number of trailing backslashes */
                if (!G.add_cmd_line)
                        G.add_cmd_line = xstrdup(cmdstr);
-               G.add_cmd_line[temp] = '\0';
+               G.add_cmd_line[len-1] = '\0';
                return;
        }
 
@@ -560,7 +562,7 @@ static void add_cmd(const char *cmdstr)
                /* last part (mandatory) will be a command */
                if (!*cmdstr)
                        bb_error_msg_and_die("missing command");
-               sed_cmd->cmd = *(cmdstr++);
+               sed_cmd->cmd = *cmdstr++;
                cmdstr = parse_cmd_args(sed_cmd, cmdstr);
 
                /* Add the command to the command array */
index b0de9657ca7bc387f414528f7ee80e86592c5769..5b0750cac0cb880e12ec600a6f55021089537248 100755 (executable)
@@ -258,4 +258,18 @@ testing "sed nested {}s" \
        "qwe\nasd\nacd\nacd\n" "" \
        "qwe\nasd\nzxc\n"
 
+testing "sed a cmd ended by double backslash" \
+       "sed -e '/| one /a \\
+       | three \\\\' -e '/| one-/a \\
+       | three-* \\\\'" \
+'      | one \\
+       | three \\
+       | two \\
+' '' \
+'      | one \\
+       | two \\
+'
+
+# testing "description" "arguments" "result" "infile" "stdin"
+
 exit $FAILCOUNT
old mode 100755 (executable)
new mode 100644 (file)
index 65a0f65..913d7f8
@@ -73,7 +73,7 @@ testing()
 
   if [ $# -ne 5 ]
   then
-    echo "Test $NAME has wrong number of arguments (must be 5) ($# $*)" >&2
+    echo "Test $NAME has wrong number of arguments: $# (must be 5)" >&2
     exit 1
   fi