PERFORCE change 144777 for review
Gabor Kovesdan
gabor at FreeBSD.org
Sun Jul 6 12:50:53 UTC 2008
http://perforce.freebsd.org/chv.cgi?CH=144777
Change 144777 by gabor at gabor_server on 2008/07/06 12:50:06
- Readd --exclude and add --include in the same way. Even if these
are not GNU compatible as GNU seems to except a shell pattern, I
consider this a great feature with extended regexps.
Affected files ...
.. //depot/projects/soc2008/gabor_textproc/grep/grep.c#56 edit
.. //depot/projects/soc2008/gabor_textproc/grep/grep.h#32 edit
.. //depot/projects/soc2008/gabor_textproc/grep/util.c#49 edit
Differences ...
==== //depot/projects/soc2008/gabor_textproc/grep/grep.c#56 (text+ko) ====
@@ -70,9 +70,10 @@
/*10*/ "FreeBSD grep 2.5.1\n",
/*11*/ "unknown --binary-files option",
/*12*/ "Binary file %s matches\n",
-/*12*/ "value out of range",
-/*13*/ "unknown -d or --directory option",
-/*14*/ "unknown --color option"
+/*13*/ "value out of range",
+/*14*/ "unknown -d or --directory option",
+/*15*/ "unknown --color option",
+/*16*/ "both --include and --exclude are specified"
};
/* Flags passed to regcomp() and regexec() */
@@ -80,10 +81,17 @@
int eflags = REG_STARTEND;
int matchall; /* shortcut */
+
+/* Searching patterns */
int patterns, pattern_sz;
char **pattern;
regex_t *r_pattern;
+/* Filename exclusion patterns */
+int epatterns, epattern_sz;
+char **epattern;
+regex_t *er_pattern;
+
/* For regex errors */
char re_error[RE_ERROR_BUF + 1];
@@ -112,6 +120,8 @@
int xflag; /* -x: pattern must match entire line */
int lbflag; /* --line-buffered */
int nullflag; /* --null */
+int exclflag; /* --exclude */
+int inclflag; /* --include */
char *label; /* --label */
char *color; /* --color */
unsigned long long mcount; /* count for -m */
@@ -128,7 +138,9 @@
MMAP_OPT,
LINEBUF_OPT,
LABEL_OPT,
- NULL_OPT
+ NULL_OPT,
+ R_EXCLUDE_OPT,
+ R_INCLUDE_OPT
};
/* Housekeeping */
@@ -162,6 +174,8 @@
{"null", no_argument, NULL, NULL_OPT},
{"color", optional_argument, NULL, COLOR_OPT},
{"colour", optional_argument, NULL, COLOR_OPT},
+ {"exclude", required_argument, NULL, R_EXCLUDE_OPT},
+ {"include", required_argument, NULL, R_INCLUDE_OPT},
{"after-context", required_argument, NULL, 'A'},
{"text", no_argument, NULL, 'a'},
{"before-context", required_argument, NULL, 'B'},
@@ -239,6 +253,22 @@
}
static void
+add_epattern(char *pat, size_t len)
+{
+ if (epatterns == epattern_sz) {
+ epattern_sz *= 2;
+ epattern = grep_realloc(epattern, ++epattern_sz * sizeof(*epattern));
+ }
+ if (len > 0 && pat[len - 1] == '\n')
+ --len;
+ /* pat may not be NUL-terminated */
+ epattern[epatterns] = grep_malloc(len + 1);
+ memcpy(epattern[epatterns], pat, len);
+ epattern[epatterns][len] = '\0';
+ ++epatterns;
+}
+
+static void
read_patterns(const char *fn)
{
FILE *f;
@@ -470,7 +500,7 @@
} else if (strcmp("never", optarg) == 0)
color = NULL;
else
- errx(2, getstr(14));
+ errx(2, getstr(15));
break;
case LABEL_OPT:
label = optarg;
@@ -481,6 +511,20 @@
case NULL_OPT:
nullflag = 1;
break;
+ case R_INCLUDE_OPT:
+ if (dirbehave != DIR_RECURSE)
+ usage();
+ inclflag = 1;
+ exclflag = 0;
+ add_epattern(optarg, strlen(optarg));
+ break;
+ case R_EXCLUDE_OPT:
+ if (dirbehave != DIR_RECURSE)
+ usage();
+ inclflag = 0;
+ exclflag = 1;
+ add_epattern(optarg, strlen(optarg));
+ break;
case HELP_OPT:
default:
usage();
@@ -516,6 +560,15 @@
errx(2, "%s", re_error);
}
}
+ er_pattern = grep_calloc(epatterns, sizeof(*er_pattern));
+ for (i = 0; i < epatterns; ++i) {
+ c = regcomp(&er_pattern[i], epattern[i], REG_EXTENDED);
+ if (c != 0) {
+ regerror(c, &er_pattern[i], re_error,
+ RE_ERROR_BUF);
+ errx(2, "%s", re_error);
+ }
+ }
if (lbflag)
setlinebuf(stdout);
==== //depot/projects/soc2008/gabor_textproc/grep/grep.h#32 (text+ko) ====
@@ -82,14 +82,15 @@
/* Command line flags */
extern int Eflag, Fflag, Gflag, Hflag, Jflag, Lflag, Zflag,
bflag, cflag, hflag, iflag, lflag, mflag, nflag, oflag,
- qflag, sflag, vflag, wflag, xflag, nullflag;
+ qflag, sflag, vflag, wflag, xflag;
+extern int nullflag, exclflag, inclflag;
extern unsigned long long Aflag, Bflag, mcount;
extern char *color, *label;
extern int binbehave, devbehave, dirbehave, linkbehave;
-extern int first, prev, matchall, patterns, tail, notfound;
-extern char **pattern;
-extern regex_t *r_pattern;
+extern int first, prev, matchall, patterns, epatterns, tail, notfound;
+extern char **pattern, **epattern;
+extern regex_t *r_pattern, *er_pattern;
/* For regex errors */
#define RE_ERROR_BUF 512
==== //depot/projects/soc2008/gabor_textproc/grep/util.c#49 (text+ko) ====
@@ -62,7 +62,7 @@
{
FTS *fts;
FTSENT *p;
- int c, fts_flags;
+ int i, c, ok, fts_flags;
c = fts_flags = 0;
@@ -92,7 +92,31 @@
p->fts_path);
break;
default:
- c += procfile(p->fts_path);
+ if (exclflag) {
+ regmatch_t pmatch;
+
+ ok = 1;
+ pmatch.rm_so = 0;
+ pmatch.rm_eo = strlen(p->fts_path);
+ for (i = 0; i < epatterns; i++)
+ if (regexec(&er_pattern[i], p->fts_path, 0, &pmatch, eflags) == 0) {
+ ok = 0;
+ break;
+ }
+ } else if (inclflag) {
+ regmatch_t pmatch;
+ ok = 0;
+ pmatch.rm_so = 0;
+ pmatch.rm_eo = strlen(p->fts_path);
+ for (i = 0; i < epatterns; i++)
+ if (regexec(&er_pattern[i], p->fts_path, 0, &pmatch, eflags) == 0) {
+ ok = 1;
+ break;
+ }
+ } else
+ ok = 1;
+ if (ok)
+ c += procfile(p->fts_path);
break;
}
}
More information about the p4-projects
mailing list