FreeBSD Bugzilla – Attachment 179171 Details for
Bug 195763
bsdgrep, empty matches and -o
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Proposed patch to address line matching issues
grep-zerolen.patch (text/plain), 3.56 KB, created by
Kyle Evans
on 2017-01-21 02:43:10 UTC
(
hide
)
Description:
Proposed patch to address line matching issues
Filename:
MIME Type:
Creator:
Kyle Evans
Created:
2017-01-21 02:43:10 UTC
Size:
3.56 KB
patch
obsolete
>diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c >index 5583c803abc..76cebbf84f8 100644 >--- a/usr.bin/grep/util.c >+++ b/usr.bin/grep/util.c >@@ -52,6 +52,10 @@ __FBSDID("$FreeBSD$"); > #include "fastmatch.h" > #include "grep.h" > >+#ifndef MAX >+#define MAX(a,b) ((a > b) ? (a) : (b)) >+#endif >+ > static int linesqueued; > static int procline(struct str *l, int); > >@@ -264,6 +284,12 @@ procfile(const char *fn) > } > > #define iswword(x) (iswalnum((x)) || (x) == L'_') >+#define overlaps(a,b) ( \ >+ ((a.rm_so) >= (b.rm_so) && (a.rm_so) <= (b.rm_eo-1)) || \ >+ ((a.rm_eo-1) >= (b.rm_so) && (a.rm_eo) <= (b.rm_eo)) || \ >+ ((a.rm_so) < (b.rm_so) && (a.rm_eo-1) >= (b.rm_so)) || \ >+ ((a.rm_so) < (b.rm_eo-1) && (a.rm_eo) >= (b.rm_eo)) \ >+ ) > > /* > * Processes a line comparing it with the specified patterns. Each pattern >@@ -277,17 +303,17 @@ procline(struct str *l, int nottext) > { > regmatch_t matches[MAX_LINE_MATCHES]; > regmatch_t pmatch; >- size_t st = 0; >+ size_t st = 0, nst = 0; > unsigned int i; >- int c = 0, m = 0, r = 0; >+ int c = 0, m = 0, r = 0, lastmatches = 0; > > /* Loop to process the whole line */ > while (st <= l->len) { >- pmatch.rm_so = st; >- pmatch.rm_eo = l->len; >- >+ lastmatches = 0; > /* Loop to compare with all the patterns */ > for (i = 0; i < patterns; i++) { >+ pmatch.rm_so = st; >+ pmatch.rm_eo = l->len; > if (fg_pattern[i].pattern) > r = fastexec(&fg_pattern[i], > l->dat, 1, &pmatch, eflags); >@@ -295,9 +321,6 @@ procline(struct str *l, int nottext) > r = regexec(&r_pattern[i], l->dat, 1, > &pmatch, eflags); > r = (r == 0) ? 0 : REG_NOMATCH; >- st = (cflags & REG_NOSUB) >- ? (size_t)l->len >- : (size_t)pmatch.rm_eo; > if (r == REG_NOMATCH) > continue; > /* Check for full match */ >@@ -324,10 +347,29 @@ procline(struct str *l, int nottext) > r = REG_NOMATCH; > } > if (r == 0) { >+ lastmatches ++; >+ /* Skip over zero-length matches */ >+ if (pmatch.rm_so == pmatch.rm_eo) >+ continue; > if (m == 0) > c++; >- if (m < MAX_LINE_MATCHES) >- matches[m++] = pmatch; >+ >+ if (m < MAX_LINE_MATCHES) { >+ /* Conflicting matches? */ >+ if (m > 0 && overlaps(pmatch, matches[m-1])) { >+ /* Replace previous match if the new one is earlier and/or longer */ >+ if (pmatch.rm_so < matches[m-1].rm_so || >+ (pmatch.rm_eo - pmatch.rm_so) >= (matches[m-1].rm_eo - matches[m-1].rm_so)) { >+ matches[m-1] = pmatch; >+ nst = pmatch.rm_eo; >+ } >+ } else { >+ /* Advance as normal if not */ >+ matches[m++] = pmatch; >+ nst = pmatch.rm_eo; >+ } >+ } >+ > /* matches - skip further patterns */ > if ((color == NULL && !oflag) || > qflag || lflag) >@@ -335,6 +377,13 @@ procline(struct str *l, int nottext) > } > } > >+ /* If we didn't have any matches or REG_NOSUB set */ >+ if (lastmatches == 0 || (cflags & REG_NOSUB)) >+ nst = l->len; >+ >+ /* Advance st based on previous matches */ >+ st = nst; >+ > if (vflag) { > c = !c; > break; >@@ -344,8 +393,10 @@ procline(struct str *l, int nottext) > if (!wflag && ((color == NULL && !oflag) || qflag || lflag || Lflag)) > break; > >- if (st == (size_t)pmatch.rm_so) >+ if (lastmatches == 0) > break; /* No matches */ >+ else if (st == (size_t)pmatch.rm_so) >+ st ++; /* Zero-length match -- advance one more */ > } > > >@@ -444,6 +495,10 @@ printline(struct str *line, int sep, regmatch_t *matches, int m) > size_t a = 0; > int i, n = 0; > >+ /* If matchall, everything matches but don't actually print for -o */ >+ if (oflag && matchall) >+ return; >+ > if (!hflag) { > if (!nullflag) { > fputs(line->file, stdout);
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 195763
:
179126
|
179171
|
179173
|
179175
|
179227