Re: [pcre-dev] pcregrep --include does not recurse

Top Page
Delete this message
Author: Philip Hazel
Date:  
To: Mike Huffman
CC: pcre-dev
Subject: Re: [pcre-dev] pcregrep --include does not recurse
On Sun, 3 Feb 2008, Mike Huffman wrote:

> pcregrep 7.6 built with gcc (GCC) 4.1.2 20061115 (prerelease) (Debian
> 4.1.1- 21), tested on Linux; MS Visual C++ 6.0 (sp5), tested on Windows XP:
> the --include option does not recurse into sub- directories with -r or
> -d recurse, while --exclude works as expected.


It turns out that that's not exactly right: there were two bugs:

(1) It was applying the include/exclude pattern to the entire path, not
just the final component.

(2) It was applying the include/exclude pattern to subdirectory names,
which is not compatible with GNU grep (and not really compatible with
the pcregrep documentation, which is a bit obscure - I will improve it.)

I have fixed both bugs. Patch below.

> In any event, removing the tests for include/exclude from the loop and
> inserting them BELOW the loop in the else if clause (with applicable
> changes) as shown below seems to have solved the problem:


That fix would also change the general behaviour because it would apply
the include/exclude patterns to top-level files as well as to files
found in directories. This is contrary to the specification of pcregrep,
and I think also to the specification of GNU grep. So I don't think it
is correct.

Thanks for the report. The patch is committed and will be in the next
release. I have realized that it would also be useful to implement
--include_dir and --exclude_dir to control the inclusion of
subdirectories when recursion, and I plan to do that.

Philip

--
Philip Hazel



*** pcre-7.6/pcregrep.c  Sun Jan 20 19:56:47 2008
--- pcregrep.c       Fri Mar  7 19:35:35 2008
***************
*** 1383,1399 ****


      while ((nextfile = readdirectory(dir)) != NULL)
        {
!       int frc, blen;
        sprintf(buffer, "%.512s%c%.128s", pathname, sep, nextfile);
!       blen = strlen(buffer);
! 
!       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;
--- 1383,1402 ----


      while ((nextfile = readdirectory(dir)) != NULL)
        {
!       int frc, nflen;
        sprintf(buffer, "%.512s%c%.128s", pathname, sep, nextfile);
!       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;
!         }   


        frc = grep_or_recurse(buffer, dir_recurse, FALSE);
        if (frc > 1) rc = frc;