summaryrefslogtreecommitdiffstats
path: root/package/busybox/busybox-1.16.1-sed.patch
blob: 0376cc92f0fa1888c086a3115c6a2c157c18a822 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
diff -urpN busybox-1.16.1/editors/sed.c busybox-1.16.1-sed/editors/sed.c
--- busybox-1.16.1/editors/sed.c	2010-03-28 19:43:35.000000000 +0200
+++ busybox-1.16.1-sed/editors/sed.c	2010-05-12 01:46:57.000000000 +0200
@@ -487,7 +487,7 @@ static const char *parse_cmd_args(sed_cm
 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;
 	}
 
@@ -936,7 +938,15 @@ static void process_files(void)
 		/* Skip blocks of commands we didn't match */
 		if (sed_cmd->cmd == '{') {
 			if (sed_cmd->invert ? matched : !matched) {
-				while (sed_cmd->cmd != '}') {
+				unsigned nest_cnt = 0;
+				while (1) {
+					if (sed_cmd->cmd == '{')
+						nest_cnt++;
+					if (sed_cmd->cmd == '}') {
+						nest_cnt--;
+						if (nest_cnt == 0)
+							break;
+					}
 					sed_cmd = sed_cmd->next;
 					if (!sed_cmd)
 						bb_error_msg_and_die("unterminated {");
@@ -1031,7 +1041,7 @@ static void process_files(void)
 		case 'c':
 			/* Only triggers on last line of a matching range. */
 			if (!sed_cmd->in_match)
-				sed_puts(sed_cmd->string, NO_EOL_CHAR);
+				sed_puts(sed_cmd->string, '\n');
 			goto discard_line;
 
 		/* Read file, append contents to output */
diff -urpN busybox-1.16.1/testsuite/sed.tests busybox-1.16.1-sed/testsuite/sed.tests
--- busybox-1.16.1/testsuite/sed.tests	2010-03-20 03:58:07.000000000 +0100
+++ busybox-1.16.1-sed/testsuite/sed.tests	2010-05-12 01:46:57.000000000 +0200
@@ -248,4 +248,28 @@ testing "sed beginning (^) matches only 
 	">/usr</>lib<\n" "" \
 	"/usr/lib\n"
 
+testing "sed c" \
+	"sed 'crepl'" \
+	"repl\nrepl\n" "" \
+	"first\nsecond\n"
+
+testing "sed nested {}s" \
+	"sed '/asd/ { p; /s/ { s/s/c/ }; p; q }'" \
+	"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