aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2021-02-21 11:27:16 -0600
committerRob Landley <rob@landley.net>2021-02-21 11:28:01 -0600
commit6817e114bb320f197727740d7905a5cb2849bdfa (patch)
treed5826416844b1e135b32595626e624769393c433
parent47b9f6a12d471086ceb11c8e2893450074e50543 (diff)
downloadtoybox-6817e114bb320f197727740d7905a5cb2849bdfa.tar.gz
Teach -o to print ranges that produce zero length matches.
And fix one test for NUL that should be a length test for -z support
-rwxr-xr-xtests/grep.test3
-rw-r--r--toys/posix/grep.c14
2 files changed, 11 insertions, 6 deletions
diff --git a/tests/grep.test b/tests/grep.test
index c6171112..215491c2 100755
--- a/tests/grep.test
+++ b/tests/grep.test
@@ -199,3 +199,6 @@ testing "-Fix" "grep -Fix h input" "H\nh\n" \
"missing\nH\nthis is HELLO\nthis is WORLD\nh\nmissing" ""
testing "-f /dev/null" "grep -f /dev/null" "" "" "hello\n"
testing "-z with \n in pattern" "grep -f input" "hi\nthere\n" "i\nt" "hi\nthere"
+
+testing "print zero length match" "grep '[0-9]*'" "abc\n" "" "abc\n"
+testing "-o skip zero length match" "grep -o '[0-9]*'" "1234\n" "" "a1234b"
diff --git a/toys/posix/grep.c b/toys/posix/grep.c
index 2ddb6b93..cd8928a1 100644
--- a/toys/posix/grep.c
+++ b/toys/posix/grep.c
@@ -218,8 +218,12 @@ static void do_grep(int fd, char *name)
}
}
- if (!rc && FLAG(x))
- if (mm->rm_so || line[mm->rm_eo]) rc = 1;
+ if (!rc && FLAG(o) && !mm->rm_eo && ulen>start-line) {
+ start++;
+ continue;
+ }
+
+ if (!rc && FLAG(x) && (mm->rm_so || ulen-(start-line)!=mm->rm_eo)) rc = 1;
if (!rc && FLAG(w)) {
char c = 0;
@@ -240,10 +244,8 @@ static void do_grep(int fd, char *name)
if (FLAG(v)) {
if (FLAG(o)) {
- if (rc) {
- mm->rm_so = 0;
- mm->rm_eo = ulen-(start-line);
- } else if (!mm->rm_so) {
+ if (rc) mm->rm_eo = ulen-(start-line);
+ else if (!mm->rm_so) {
start += mm->rm_eo;
continue;
} else mm->rm_eo = mm->rm_so;