Revision: 420
http://vcs.pcre.org/viewvc?view=rev&revision=420
Author: ph10
Date: 2009-08-12 18:32:27 +0100 (Wed, 12 Aug 2009)
Log Message:
-----------
Fix infelicities to do with --count in pcregrep.
Modified Paths:
--------------
code/trunk/ChangeLog
code/trunk/RunGrepTest
code/trunk/doc/pcregrep.1
code/trunk/pcregrep.c
code/trunk/testdata/grepoutput
Modified: code/trunk/ChangeLog
===================================================================
--- code/trunk/ChangeLog 2009-08-12 10:45:33 UTC (rev 419)
+++ code/trunk/ChangeLog 2009-08-12 17:32:27 UTC (rev 420)
@@ -12,8 +12,16 @@
2. Changed the call to open a subject file in pcregrep from fopen(pathname,
"r") to fopen(pathname, "rb"), which fixed a problem with some of the tests
in a Windows environment.
+
+3. The pcregrep --count option prints the count for each file even when it is
+ zero, as does GNU grep. However, pcregrep was also printing all files when
+ --files-with-matches was added. Now, when both options are given, it prints
+ counts only for those files that have at least one match. (GNU grep just
+ prints the file name in this circumstance, but including the count seems
+ more useful - otherwise, why use --count?) Also ensured that the
+ combination -clh just lists non-zero counts, with no names.
+
-
Version 7.9 11-Apr-09
---------------------
Modified: code/trunk/RunGrepTest
===================================================================
--- code/trunk/RunGrepTest 2009-08-12 10:45:33 UTC (rev 419)
+++ code/trunk/RunGrepTest 2009-08-12 17:32:27 UTC (rev 420)
@@ -217,6 +217,12 @@
echo "---------------------------- Test 54 -----------------------------" >>testtry
(cd $srcdir; $valgrind $pcregrep -f./testdata/greplist --color=always ./testdata/grepinputx) >>testtry
+echo "---------------------------- Test 55 -----------------------------" >>testtry
+(cd $srcdir; $valgrind $pcregrep -c lazy ./testdata/grepinput*) >>testtry
+
+echo "---------------------------- Test 56 -----------------------------" >>testtry
+(cd $srcdir; $valgrind $pcregrep -c -l lazy ./testdata/grepinput*) >>testtry
+
# Now compare the results.
$cf $srcdir/testdata/grepoutput testtry
Modified: code/trunk/doc/pcregrep.1
===================================================================
--- code/trunk/doc/pcregrep.1 2009-08-12 10:45:33 UTC (rev 419)
+++ code/trunk/doc/pcregrep.1 2009-08-12 17:32:27 UTC (rev 420)
@@ -88,6 +88,11 @@
.
.SH OPTIONS
.rs
+.sp
+The order in which some of the options appear can affect the output. For
+example, both the \fB-h\fP and \fB-l\fP options affect the printing of file
+names. Whichever comes later in the command line will be the one that takes
+effect.
.TP 10
\fB--\fP
This terminate the list of options. It is useful if the next item on the
@@ -115,10 +120,13 @@
This is equivalent to setting both \fB-A\fP and \fB-B\fP to the same value.
.TP
\fB-c\fP, \fB--count\fP
-Do not output individual lines; instead just output a count of the number of
-lines that would otherwise have been output. If several files are given, a
-count is output for each of them. In this mode, the \fB-A\fP, \fB-B\fP, and
-\fB-C\fP options are ignored.
+Do not output individual lines from the files that are being scanned; instead
+output the number of lines that would otherwise have been shown. If no lines
+are selected, the number zero is output. If several files are are being
+scanned, a count is output for each of them. However, if the
+\fB--files-with-matches\fP option is also used, only those files whose counts
+are greater than zero are listed. When \fB-c\fP is used, the \fB-A\fP,
+\fB-B\fP, and \fB-C\fP options are ignored.
.TP
\fB--colour\fP, \fB--color\fP
If this option is given without any data, it is equivalent to "--colour=auto".
@@ -263,8 +271,11 @@
\fB-l\fP, \fB--files-with-matches\fP
Instead of outputting lines from the files, just output the names of the files
containing lines that would have been output. Each file name is output
-once, on a separate line. Searching stops as soon as a matching line is found
-in a file.
+once, on a separate line. Searching normally stops as soon as a matching line
+is found in a file. However, if the \fB-c\fP (count) option is also used,
+matching continues in order to obtain the correct count, and those files that
+have at least one match are listed along with their counts. Using this option
+with \fB-c\fP is a way of suppressing the listing of files with no matches.
.TP
\fB--label\fP=\fIname\fP
This option supplies a name to be used for the standard input when file names
@@ -399,7 +410,9 @@
as in the GNU \fBgrep\fP program. Any long option of the form
\fB--xxx-regexp\fP (GNU terminology) is also available as \fB--xxx-regex\fP
(PCRE terminology). However, the \fB--locale\fP, \fB-M\fP, \fB--multiline\fP,
-\fB-u\fP, and \fB--utf-8\fP options are specific to \fBpcregrep\fP.
+\fB-u\fP, and \fB--utf-8\fP options are specific to \fBpcregrep\fP. If both the
+\fB-c\fP and \fB-l\fP options are given, GNU grep lists only file names,
+without counts, but \fBpcregrep\fP gives the counts.
.
.
.SH "OPTIONS WITH DATA"
@@ -472,6 +485,6 @@
.rs
.sp
.nf
-Last updated: 01 March 2009
+Last updated: 12 August 2009
Copyright (c) 1997-2009 University of Cambridge.
.fi
Modified: code/trunk/pcregrep.c
===================================================================
--- code/trunk/pcregrep.c 2009-08-12 10:45:33 UTC (rev 419)
+++ code/trunk/pcregrep.c 2009-08-12 17:32:27 UTC (rev 420)
@@ -83,7 +83,7 @@
output. The order is important; it is assumed that a file name is wanted for
all values greater than FN_DEFAULT. */
-enum { FN_NONE, FN_DEFAULT, FN_ONLY, FN_NOMATCH_ONLY, FN_FORCE };
+enum { FN_NONE, FN_DEFAULT, FN_MATCH_ONLY, FN_NOMATCH_ONLY, FN_FORCE };
/* File reading styles */
@@ -165,6 +165,7 @@
static BOOL line_offsets = FALSE;
static BOOL multiline = FALSE;
static BOOL number = FALSE;
+static BOOL omit_zero_count = FALSE;
static BOOL only_matching = FALSE;
static BOOL quiet = FALSE;
static BOOL silent = FALSE;
@@ -1061,7 +1062,7 @@
/* If all we want is a file name, there is no need to scan any more lines
in the file. */
- else if (filenames == FN_ONLY)
+ else if (filenames == FN_MATCH_ONLY)
{
fprintf(stdout, "%s\n", printname);
return 0;
@@ -1365,8 +1366,12 @@
if (count_only)
{
- if (printname != NULL) fprintf(stdout, "%s:", printname);
- fprintf(stdout, "%d\n", count);
+ if (count > 0 || !omit_zero_count)
+ {
+ if (printname != NULL && filenames != FN_NONE)
+ fprintf(stdout, "%s:", printname);
+ fprintf(stdout, "%d\n", count);
+ }
}
return rc;
@@ -1686,7 +1691,7 @@
case 'H': filenames = FN_FORCE; break;
case 'h': filenames = FN_NONE; break;
case 'i': options |= PCRE_CASELESS; break;
- case 'l': filenames = FN_ONLY; break;
+ case 'l': omit_zero_count = TRUE; filenames = FN_MATCH_ONLY; break;
case 'L': filenames = FN_NOMATCH_ONLY; break;
case 'M': multiline = TRUE; options |= PCRE_MULTILINE|PCRE_FIRSTLINE; break;
case 'n': number = TRUE; break;
Modified: code/trunk/testdata/grepoutput
===================================================================
--- code/trunk/testdata/grepoutput 2009-08-12 10:45:33 UTC (rev 419)
+++ code/trunk/testdata/grepoutput 2009-08-12 17:32:27 UTC (rev 420)
@@ -423,3 +423,11 @@
Here is the ?[1;31mpattern?[00m again.
That time it was on a ?[1;31mline by itself?[00m.
This line contains ?[1;31mpattern?[00m not on a ?[1;31mline by itself?[00m.
+---------------------------- Test 55 -----------------------------
+./testdata/grepinput:456
+./testdata/grepinput8:0
+./testdata/grepinputv:1
+./testdata/grepinputx:0
+---------------------------- Test 56 -----------------------------
+./testdata/grepinput:456
+./testdata/grepinputv:1