Revision: 324
http://vcs.pcre.org/viewvc?view=rev&revision=324
Author: ph10
Date: 2008-03-07 19:48:32 +0000 (Fri, 07 Mar 2008)
Log Message:
-----------
Fix bugs with --include and --exclude in pcregrep.
Modified Paths:
--------------
code/trunk/ChangeLog
code/trunk/doc/pcregrep.1
code/trunk/pcregrep.c
Modified: code/trunk/ChangeLog
===================================================================
--- code/trunk/ChangeLog 2008-03-05 17:23:42 UTC (rev 323)
+++ code/trunk/ChangeLog 2008-03-07 19:48:32 UTC (rev 324)
@@ -14,8 +14,20 @@
3. Remove a line of dead code, identified by coverity and reported by Nuno
Lopes.
+
+4. Fixed two related pcregrep bugs involving -r with --include or --exclude:
+ (1) The include/exclude patterns were being applied to the whole pathnames
+ of files, instead of just to the final components.
+
+ (2) If there was more than one level of directory, the subdirectories were
+ skipped unless they satisfied the include/exclude conditions. This is
+ inconsistent with GNU grep (and could even be seen as contrary to the
+ pcregrep specification - which I improved to make it absolutely clear).
+ The action now is always to scan all levels of directory, and just
+ apply the include/exclude patterns to regular files.
+
Version 7.6 28-Jan-08
---------------------
Modified: code/trunk/doc/pcregrep.1
===================================================================
--- code/trunk/doc/pcregrep.1 2008-03-05 17:23:42 UTC (rev 323)
+++ code/trunk/doc/pcregrep.1 2008-03-07 19:48:32 UTC (rev 324)
@@ -157,10 +157,12 @@
.TP
\fB--exclude\fP=\fIpattern\fP
When \fBpcregrep\fP is searching the files in a directory as a consequence of
-the \fB-r\fP (recursive search) option, any files whose names match the pattern
-are excluded. The pattern is a PCRE regular expression. If a file name matches
-both \fB--include\fP and \fB--exclude\fP, it is excluded. There is no short
-form for this option.
+the \fB-r\fP (recursive search) option, any regular files whose names match the
+pattern are excluded (subdirectories are not excluded; they are searched
+recursively). The pattern is a PCRE regular expression, and is matched against
+the final component of the file name (not the entire path). If a file name
+matches both \fB--include\fP and \fB--exclude\fP, it is excluded. There is no
+short form for this option.
.TP
\fB-F\fP, \fB--fixed-strings\fP
Interpret each pattern as a list of fixed strings, separated by newlines,
@@ -212,10 +214,12 @@
.TP
\fB--include\fP=\fIpattern\fP
When \fBpcregrep\fP is searching the files in a directory as a consequence of
-the \fB-r\fP (recursive search) option, only those files whose names match the
-pattern are included. The pattern is a PCRE regular expression. If a file name
-matches both \fB--include\fP and \fB--exclude\fP, it is excluded. There is no
-short form for this option.
+the \fB-r\fP (recursive search) option, only those regular files whose names
+match the pattern are included (but subdirectories are always included and
+searched recursively). The pattern is a PCRE regular expression, and is matched
+against the final component of the file name (not the entire path). If a file
+name matches both \fB--include\fP and \fB--exclude\fP, it is excluded. There is
+no short form for this option.
.TP
\fB-L\fP, \fB--files-without-match\fP
Instead of outputting lines from the files, just output the names of the files
@@ -434,6 +438,6 @@
.rs
.sp
.nf
-Last updated: 17 December 2007
-Copyright (c) 1997-2007 University of Cambridge.
+Last updated: 07 March 2008
+Copyright (c) 1997-2008 University of Cambridge.
.fi
Modified: code/trunk/pcregrep.c
===================================================================
--- code/trunk/pcregrep.c 2008-03-05 17:23:42 UTC (rev 323)
+++ code/trunk/pcregrep.c 2008-03-07 19:48:32 UTC (rev 324)
@@ -1383,18 +1383,21 @@
while ((nextfile = readdirectory(dir)) != NULL)
{
- int frc, blen;
+ int frc, nflen;
sprintf(buffer, "%.512s%c%.128s", pathname, sep, nextfile);
- blen = strlen(buffer);
+ nflen = strlen(nextfile);
+
+ if (!isdirectory(buffer))
+ {
+ if (exclude_compiled != NULL &&
+ pcre_exec(exclude_compiled, NULL, nextfile, nflen, 0, 0, NULL, 0) >= 0)
+ continue;
+
+ if (include_compiled != NULL &&
+ pcre_exec(include_compiled, NULL, nextfile, nflen, 0, 0, NULL, 0) < 0)
+ continue;
+ }
- if (exclude_compiled != NULL &&
- pcre_exec(exclude_compiled, NULL, buffer, blen, 0, 0, NULL, 0) >= 0)
- continue;
-
- if (include_compiled != NULL &&
- pcre_exec(include_compiled, NULL, buffer, blen, 0, 0, NULL, 0) < 0)
- continue;
-
frc = grep_or_recurse(buffer, dir_recurse, FALSE);
if (frc > 1) rc = frc;
else if (frc == 0 && rc == 1) rc = 0;