diff options
author | Elliott Hughes <enh@google.com> | 2024-01-30 23:49:17 +0000 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2024-01-30 23:49:25 +0000 |
commit | 6c57d80e3fe8b6957406a9dc211caf26576bfbd1 (patch) | |
tree | 64818168536af27954b2374930d85ebf27347e32 | |
parent | e6fa96120955a4827ba53b008f8b160e3509f061 (diff) | |
parent | 6d9a752ac585d4ffc83c00e90985826150ff8298 (diff) | |
download | toybox-6c57d80e3fe8b6957406a9dc211caf26576bfbd1.tar.gz |
Upgrade toybox to 6d9a752ac585d4ffc83c00e90985826150ff8298
This project was upgraded with external_updater.
Usage: tools/external_updater/updater.sh update toybox
For more info, check https://cs.android.com/android/platform/superproject/+/main:tools/external_updater/README.md
Test: TreeHugger
Change-Id: Ia563ef0ba8d9621859c195acd4e920f1f47529b7
-rw-r--r-- | METADATA | 4 | ||||
-rwxr-xr-x | tests/grep.test | 30 | ||||
-rw-r--r-- | toys/posix/grep.c | 45 |
3 files changed, 47 insertions, 32 deletions
@@ -9,12 +9,12 @@ third_party { last_upgrade_date { year: 2024 month: 1 - day: 29 + day: 30 } homepage: "https://landley.net/toybox/" identifier { type: "Git" value: "https://github.com/landley/toybox" - version: "a60c1d84faab4fd3da41d6cd2a14792d7cf5c572" + version: "6d9a752ac585d4ffc83c00e90985826150ff8298" } } diff --git a/tests/grep.test b/tests/grep.test index c7fab92d..74a598ee 100755 --- a/tests/grep.test +++ b/tests/grep.test @@ -9,14 +9,18 @@ #testing "name" "command" "result" "infile" "stdin" +testcmd 'simple' 'two' 'two\n' '' 'one\ntwo\nthree\n' +testcmd 'negative' 'a' '' '' '\n' +testcmd 'empty' "''" '\n' '' '\n' + testcmd "-c" "-c 123 input" "3\n" "123\ncount 123\n123\nfasdfasdf" "" -echo -e "this is test" > foo -echo -e "this is test2" > foo2 -echo -e "this is foo3" > foo3 -testcmd "-l" "-l test foo foo2 foo3" "foo\nfoo2\n" "" "" -testcmd "-L" "-L test foo foo2 foo3" "foo3\n" "" "" -rm foo foo2 foo3 +echo -e "this is test" > file +echo -e "this is test2" > file2 +echo -e "this is number3" > file3 +testcmd "-l" "-l test file file2 file3" "file\nfile2\n" "" "" +testcmd "-L" "-L test file file2 file3" "file3\n" "" "" +rm file file2 file3 testcmd "-q" "-q test input && echo yes" "yes\n" "this is a test\n" "" testcmd "-E" "-E '[0-9]' input" "1234123asdfas123123\n1\n" \ @@ -97,7 +101,21 @@ testcmd "-Fxv -e subset" "-Fxv -e bbswitch-dkms -e dkms" "" "" "bbswitch-dkms\n" testcmd "-e blah -e ''" "-e blah -e '' input" "one one one\n" "one one one\n" "" testcmd "-w ''" "-w '' input" "" "one one one\n" "" testcmd "-w '' 2" "-w '' input" "one two\n" "one two\n" "" +#testcmd "-w '' 3" "-w ''" "one two\n" "one two\n" "" +testcmd "'$' is ''" "'\$'" 'potato\n' '' 'potato\n' +testcmd "'$' is '' 2" "'x*\$'" 'potato\n' '' 'potato\n' +testcmd "-w '$'" "-w '\$'" '' '' 'abc abc\n' +testcmd "-w '$' 2" "-w '\$'" 'abc \n' '' 'abc \n' +testcmd "'^' is ''" "'^'" 'potato\n' '' 'potato\n' +testcmd "'^' is '' 2" "'^x*'" 'potato\n' '' 'potato\n' +testcmd "-w '^'" "-w '^'" '' '' 'abc abc\n' +testcmd "-w '^' 2" "-w '^'" ' abc\n' '' ' abc\n' testcmd "-w \\1" "-wo '\\(x\\)\\1'" "xx\n" "" "xx" +testcmd '' "-nw ''" '1:\n3: \n4:a \n5: a\n7:a a\n' '' \ + '\na\n \na \n a\na a\na a' +testcmd '' "-nw '^'" '1:\n3: \n5: a\n' '' '\na\n \na \n a\na a\na a' +testcmd '' "-nw '\$'" '1:\n3: \n4:a \n' '' '\na\n \na \n a\na a\na a' +testcmd '' "-nw '^\$'" '1:\n' '' '\na\n \na \n a\na a\na a' testcmd "-o ''" "-o '' input" "" "one one one\n" "" testcmd "backref" '-e "a\(b\)" -e "b\(c\)\1"' "bcc\nab\n" "" "bcc\nbcb\nab\n" diff --git a/toys/posix/grep.c b/toys/posix/grep.c index 74af7b69..0d252b71 100644 --- a/toys/posix/grep.c +++ b/toys/posix/grep.c @@ -100,6 +100,16 @@ static void outline(char *line, char dash, char *name, long lcount, long bcount, } } +static int matchw(char *line, char *start, long so, long eo) +{ + if (FLAG(w)) { + if (so+(start-line)) if (isalnum(start[so-1]) || start[so-1]=='_') return 0; + if (isalnum(start[eo]) || start[eo]=='_') return 0; + } + + return 1; +} + // Show matches in one file static void do_grep(int fd, char *name) { @@ -163,7 +173,7 @@ static void do_grep(int fd, char *name) rc = 1; // Handle "fixed" (literal) matches (if any) - if (TT.e && *start) for (ss = start; ss-line<ulen; ss++) { + if (TT.e) for (ss = start; ss-line<=ulen; ss++) { ii = FLAG(i) ? toupper(*ss) : *ss; for (seek = TT.fixed[ii]; seek; seek = seek->next) { if (*(pp = seek->arg)=='^' && !FLAG(F)) { @@ -182,6 +192,7 @@ static void do_grep(int fd, char *name) } if (pp[ii] && (pp[ii]!='$' || pp[ii+1] || ss[ii])) continue; mm->rm_eo = (mm->rm_so = ss-start)+ii; + if (!matchw(line, start, mm->rm_so, mm->rm_eo)) continue; rc = 0; goto got; @@ -189,8 +200,6 @@ static void do_grep(int fd, char *name) if (FLAG(x)) break; } - // Empty pattern always matches - if (rc && *TT.fixed && !FLAG(o)) rc = 0; got: // Handle regex matches (if any) for (shoe = (void *)TT.reg; shoe; shoe = shoe->next) { @@ -203,6 +212,7 @@ got: &shoe->m, start==line ? 0 : REG_NOTBOL); } + if (!matchw(line, start, shoe->m.rm_so, shoe->m.rm_eo)) continue; // If we got a match, is it a _better_ match? if (!shoe->rc && (rc || shoe->m.rm_so < mm->rm_so || (shoe->m.rm_so == mm->rm_so && shoe->m.rm_eo >= mm->rm_eo))) @@ -219,23 +229,6 @@ got: if (!rc && FLAG(x) && (mm->rm_so || ulen-(start-line)!=mm->rm_eo)) rc = 1; - if (!rc && FLAG(w)) { - char c = 0; - - if ((start+mm->rm_so)!=line) { - c = start[mm->rm_so-1]; - if (!isalnum(c) && c != '_') c = 0; - } - if (!c) { - c = start[mm->rm_eo]; - if (!isalnum(c) && c != '_') c = 0; - } - if (c) { - move = mm->rm_so+1; - continue; - } - } - if (FLAG(v)) { if (FLAG(o)) { if (rc) mm->rm_eo = ulen-(start-line); @@ -417,7 +410,7 @@ static void parse_regex(void) } else if (*s>127 || strchr(special+4, *s)) break; } - // Add entry to fast path (literal-ish match) or slow path (regexec) + // Leave entry in fast path (literal-ish match) or move to slow path (regex) if (!*s || FLAG(F)) last = &((*last)->next); else { struct reg *shoe; @@ -447,13 +440,17 @@ static void parse_regex(void) } // Sort each fast path pattern set by length so first hit is longest match - if (TT.e) for (key = 0; key<256; key++) { - if (!TT.fixed[key]) continue; + // Zero length matches aren't sorted, instead appended to every list. + if (TT.e) for (key = 1; key<256; key++) { + if (!TT.fixed[key]) { + TT.fixed[key] = *TT.fixed; + continue; + } for (len = 0, al = TT.fixed[key]; al; al = al->next) len++; last = xmalloc(len*sizeof(void *)); for (len = 0, al = TT.fixed[key]; al; al = al->next) last[len++] = al; qsort(last, len, sizeof(void *), (void *)lensort); - for (ii = 0; ii<len; ii++) last[ii]->next = ii ? last[ii-1] : 0; + for (ii = 0; ii<len; ii++) last[ii]->next = ii ? last[ii-1] : *TT.fixed; TT.fixed[key] = last[len-1]; free(last); } |