aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtests/sed.test6
-rw-r--r--toys/posix/sed.c21
2 files changed, 18 insertions, 9 deletions
diff --git a/tests/sed.test b/tests/sed.test
index eff2306a..c62f9c4d 100755
--- a/tests/sed.test
+++ b/tests/sed.test
@@ -131,10 +131,13 @@ hello'" "merp\nhello\n" "" "merp"
testing "" "sed -e '/x/c\' -e 'y'" 'y\n' '' 'x\n'
testing "" "sed -e 's/a[([]*b/X/'" 'X' '' 'a[(b'
+testing "" "sed 'y/a\\bc/de\f/'" "db\f" "" "abc"
+testing "sed [a-a] (for perl)" "sed '"'s/\([^a-zA-Z0-9.:_\-\/]\)/\\\1/g'"'" \
+ 'he\ llo' "" "he llo"
# You have to match the first line of a range in order to activate
# the range, numeric and ascii work the same way
-testing "skip start of range" "sed -e n -e '1,2s/b/c/'" "a\nb\n" "" "a\nb\n"
+testing "sed skip start of range" "sed -e n -e '1,2s/b/c/'" "a\nb\n" "" "a\nb\n"
#echo meep | sed/sed -e '1a\' -e 'huh'
#echo blah | sed/sed -f <(echo -e "1a\\\\\nboom")
@@ -146,4 +149,5 @@ testing "sed bonus backslashes" \
"hello\nl x\nab\nc\n" "" "hello\n"
# -i with $ last line test
+
exit $FAILCOUNT
diff --git a/toys/posix/sed.c b/toys/posix/sed.c
index 013dc6b8..9b5e6664 100644
--- a/toys/posix/sed.c
+++ b/toys/posix/sed.c
@@ -701,7 +701,7 @@ static void do_sed(int fd, char *name)
// returns processed copy of string (0 if error), *pstr advances to next
// unused char. if delim (or *delim) is 0 uses/saves starting char as delimiter
// if regxex, ignore delimiter in [ranges]
-static char *unescape_delimited_string(char **pstr, char *delim, int regex)
+static char *unescape_delimited_string(char **pstr, char *delim)
{
char *to, *from, mode = 0, d;
@@ -720,9 +720,14 @@ static char *unescape_delimited_string(char **pstr, char *delim, int regex)
// delimiter in regex character range doesn't count
if (!mode && *from == '[') {
mode = '[';
- if (from[1] == ']') *(to++) = *(from++);
+ if (from[1]=='-' || from[1]==']') *(to++) = *(from++);
} else if (mode && *from == ']') mode = 0;
- else if (*from == '\\') {
+ // Length 1 range (X-X with same X) is "undefined" and makes regcomp err,
+ // but the perl build does it, so we need to filter it out.
+ else if (mode && *from == '-' && from[-1] == from[1]) {
+ from+=2;
+ continue;
+ } else if (*from == '\\') {
if (!from[1]) return 0;
// Check escaped end delimiter before printf style escapes.
@@ -735,7 +740,7 @@ static char *unescape_delimited_string(char **pstr, char *delim, int regex)
*(to++) = c;
from+=2;
continue;
- } else *(to++) = *(from++);
+ } else if (!mode) *(to++) = *(from++);
}
}
*(to++) = *(from++);
@@ -802,7 +807,7 @@ static void jewel_of_judgement(char **pline, long len)
} else if (*line == '/' || *line == '\\') {
char *s = line;
- if (!(s = unescape_delimited_string(&line, 0, 1))) goto brand;
+ if (!(s = unescape_delimited_string(&line, 0))) goto brand;
if (!*s) corwin->rmatch[i] = 0;
else {
xregcomp((void *)reg, s, (toys.optflags & FLAG_r)*REG_EXTENDED);
@@ -844,7 +849,7 @@ static void jewel_of_judgement(char **pline, long len)
// get pattern (just record, we parse it later)
corwin->arg2 = reg - (char *)corwin;
- if (!(TT.remember = unescape_delimited_string(&line, &delim, 1)))
+ if (!(TT.remember = unescape_delimited_string(&line, &delim)))
goto brand;
reg += sizeof(regex_t);
@@ -940,13 +945,13 @@ writenow:
char *s, delim = 0;
int len;
- if (!(s = unescape_delimited_string(&line, &delim, 0))) goto brand;
+ if (!(s = unescape_delimited_string(&line, &delim))) goto brand;
corwin->arg1 = reg-(char *)corwin;
len = strlen(s);
reg = extend_string((void *)&corwin, s, reg-(char *)corwin, len);
free(s);
corwin->arg2 = reg-(char *)corwin;
- if (!(s = unescape_delimited_string(&line, &delim, 0))) goto brand;
+ if (!(s = unescape_delimited_string(&line, &delim))) goto brand;
if (len != strlen(s)) goto brand;
reg = extend_string((void *)&corwin, s, reg-(char*)corwin, len);
free(s);