Date: Fri, 24 Feb 2012 13:05:10 +0000 (UTC) From: Gabor Kovesdan <gabor@FreeBSD.org> To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r232109 - user/gabor/tre-integration/usr.bin/grep Message-ID: <201202241305.q1OD5ABE060031@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: gabor Date: Fri Feb 24 13:05:10 2012 New Revision: 232109 URL: http://svn.freebsd.org/changeset/base/232109 Log: - Use new multi-pattern interface Modified: user/gabor/tre-integration/usr.bin/grep/Makefile user/gabor/tre-integration/usr.bin/grep/grep.c user/gabor/tre-integration/usr.bin/grep/grep.h user/gabor/tre-integration/usr.bin/grep/util.c Modified: user/gabor/tre-integration/usr.bin/grep/Makefile ============================================================================== --- user/gabor/tre-integration/usr.bin/grep/Makefile Fri Feb 24 12:35:17 2012 (r232108) +++ user/gabor/tre-integration/usr.bin/grep/Makefile Fri Feb 24 13:05:10 2012 (r232109) @@ -61,12 +61,6 @@ MLINKS+= grep.1 bzgrep.1 \ CFLAGS+= -DWITHOUT_BZIP2 .endif -.if !defined(WITHOUT_GNU_COMPAT) -CFLAGS+= -I/usr/include/gnu -LDADD+= -lgnuregex -DPADD+= ${LIBGNUREGEX} -.endif - .if !defined(WITHOUT_NLS) .include "${.CURDIR}/nls/Makefile.inc" .else Modified: user/gabor/tre-integration/usr.bin/grep/grep.c ============================================================================== --- user/gabor/tre-integration/usr.bin/grep/grep.c Fri Feb 24 12:35:17 2012 (r232108) +++ user/gabor/tre-integration/usr.bin/grep/grep.c Fri Feb 24 13:05:10 2012 (r232109) @@ -43,6 +43,8 @@ __FBSDID("$FreeBSD$"); #include <limits.h> #include <libgen.h> #include <locale.h> +#include <mregex.h> +#include <regex.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -56,6 +58,8 @@ __FBSDID("$FreeBSD$"); nl_catd catalog; #endif +#define MAX_PATTERNS 256 + /* * Default messags to use when NLS is disabled or no catalogue * is found. @@ -81,9 +85,10 @@ int eflags = REG_STARTEND; bool matchall; /* Searching patterns */ -unsigned int patterns, pattern_sz; -struct pat *pattern; -regex_t *r_pattern; +unsigned int patterns; +char **pats; +size_t *lens; +mregex_t preg; /* Filename exclusion/inclusion patterns */ unsigned int fpatterns, fpattern_sz; @@ -228,28 +233,21 @@ add_pattern(char *pat, size_t len) /* Check if we can do a shortcut */ if (len == 0) { matchall = true; - for (unsigned int i = 0; i < patterns; i++) { - free(pattern[i].pat); - } - pattern = grep_realloc(pattern, sizeof(struct pat)); - pattern[0].pat = NULL; - pattern[0].len = 0; + for (unsigned int i = 0; i < patterns; i++) + free(pats[i]); + pats[0] = grep_strdup(""); patterns = 1; return; } /* Increase size if necessary */ - if (patterns == pattern_sz) { - pattern_sz *= 2; - pattern = grep_realloc(pattern, ++pattern_sz * - sizeof(struct pat)); - } + if (len > 0 && pat[len - 1] == '\n') --len; /* pat may not be NUL-terminated */ - pattern[patterns].pat = grep_malloc(len + 1); - memcpy(pattern[patterns].pat, pat, len); - pattern[patterns].len = len; - pattern[patterns].pat[len] = '\0'; + pats[patterns] = grep_malloc(len + 1); + memcpy(pats[patterns], pat, len); + pats[patterns][len] = '\0'; + lens[patterns] = len; ++patterns; } @@ -327,7 +325,7 @@ main(int argc, char *argv[]) { char **aargv, **eargv, *eopts; char *ep; - const char *pn; + const char *pn, **ptr; unsigned long long l; unsigned int aargc, eargc, i; int c, lastc, needpattern, newarg, prevoptind; @@ -338,6 +336,9 @@ main(int argc, char *argv[]) catalog = catopen("grep", NL_CAT_LOCALE); #endif + pats = grep_malloc(MAX_PATTERNS * sizeof(char *)); + lens = grep_malloc(MAX_PATTERNS * sizeof(size_t)); + /* Check what is the program name of the binary. In this way we can have all the funcionalities in one binary without the need of scripting and using ugly hacks. */ @@ -682,16 +683,13 @@ main(int argc, char *argv[]) usage(); } - r_pattern = grep_calloc(patterns, sizeof(*r_pattern)); - - /* Check if cheating is allowed (always is for fgrep). */ - for (i = 0; i < patterns; ++i) { - c = regcomp(&r_pattern[i], pattern[i].pat, cflags); - if (c != 0) { - regerror(c, &r_pattern[i], re_error, - RE_ERROR_BUF); - errx(2, "%s", re_error); - } + /* Compile patterns. */ + ptr = (const char **)pats; + c = mregncomp(&preg, patterns, ptr, lens, cflags); + if (c != 0) { + // regerror(c, &r_pattern[i], re_error, RE_ERROR_BUF); + // errx(2, "%s", re_error); + errx(2, "%s", "Bad patterns."); } if (lbflag) Modified: user/gabor/tre-integration/usr.bin/grep/grep.h ============================================================================== --- user/gabor/tre-integration/usr.bin/grep/grep.h Fri Feb 24 12:35:17 2012 (r232108) +++ user/gabor/tre-integration/usr.bin/grep/grep.h Fri Feb 24 13:05:10 2012 (r232109) @@ -31,6 +31,7 @@ #include <bzlib.h> #include <limits.h> +#include <mregex.h> #include <regex.h> #include <stdbool.h> #include <stdio.h> @@ -93,16 +94,14 @@ struct str { int line_no; }; -struct pat { - char *pat; - int len; -}; - struct epat { char *pat; int mode; }; +extern char **pats; +extern size_t *lens; + /* Flags passed to regcomp() and regexec() */ extern int cflags, eflags; @@ -122,7 +121,7 @@ extern int tail; extern unsigned int dpatterns, fpatterns, patterns; extern struct pat *pattern; extern struct epat *dpattern, *fpattern; -extern regex_t *er_pattern, *r_pattern; +extern mregex_t preg; /* For regex errors */ #define RE_ERROR_BUF 512 Modified: user/gabor/tre-integration/usr.bin/grep/util.c ============================================================================== --- user/gabor/tre-integration/usr.bin/grep/util.c Fri Feb 24 12:35:17 2012 (r232108) +++ user/gabor/tre-integration/usr.bin/grep/util.c Fri Feb 24 13:05:10 2012 (r232109) @@ -273,7 +273,6 @@ procline(struct str *l, int nottext) regmatch_t matches[MAX_LINE_MATCHES]; regmatch_t pmatch; size_t st = 0; - unsigned int i; int c = 0, m = 0, r = 0; /* Loop to process the whole line */ @@ -281,44 +280,30 @@ procline(struct str *l, int nottext) pmatch.rm_so = st; pmatch.rm_eo = l->len; - /* Loop to compare with all the patterns */ - for (i = 0; i < patterns; i++) { - 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 */ - if (r == 0 && xflag) - if (pmatch.rm_so != 0 || - (size_t)pmatch.rm_eo != l->len) - r = REG_NOMATCH; - if (r == 0) { - if (m == 0) - c++; - if (m < MAX_LINE_MATCHES) - matches[m++] = pmatch; - /* matches - skip further patterns */ - if ((color == NULL && !oflag) || - qflag || lflag) - break; - } + r = mregexec(&preg, l->dat, 1, &pmatch, eflags); + st = (cflags & REG_NOSUB) ? (size_t)l->len : + (size_t)pmatch.rm_eo; + if (r == REG_NOMATCH) + continue; + /* Check for full match */ + if (r == REG_OK && xflag) + if (pmatch.rm_so != 0 || + (size_t)pmatch.rm_eo != l->len) + r = REG_NOMATCH; + if (r == REG_OK) { + if (m == 0) + c++; + if (m < MAX_LINE_MATCHES) + matches[m++] = pmatch; + /* matches - skip further patterns */ + if ((color == NULL && !oflag) || qflag || lflag) + break; } if (vflag) { c = !c; break; } - - /* One pass if we are not recording matches */ - if ((color == NULL && !oflag) || qflag || lflag) - break; - - if (st == (size_t)pmatch.rm_so) - break; /* No matches */ }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201202241305.q1OD5ABE060031>