PATCH: add -n option to proc grep

Ben Elliston <[email protected]>
Newsgroups gmane.comp.sysutils.dejagnu.general
Message-ID <[email protected]>
This patch adds a '-n' option to the DejaGnu grep procedure. It is not
only more familiar to grep users, but it moves the option processing
for any "dash options" to the front of the argument list, consistent
with the Tcl built-ins.

Any comments before I commit it?

Cheers, Ben


2018-12-04  Ben Elliston  <[email protected]>

	* lib/utils.exp (grep): Handle -n.
	* doc/dejagnu.texi (grep procedure): Document it.
	* testsuite/runtest.all/utils.test: Add a test case.

diff --git a/doc/dejagnu.texi b/doc/dejagnu.texi
index 7273e90..979dc53 100644
--- a/doc/dejagnu.texi
+++ b/doc/dejagnu.texi
@@ -4776,17 +4776,21 @@ The executable program or shell script to look for.
 @subsubheading grep Procedure
 @findex grep
 
-Search the file called @file{filename} (a fully specified path) for
-lines that contain a match for regular expression @emph{regexp}. The
-result is a list of all the lines that match.  If no lines match, the
-result is an empty string.  Specify @emph{regexp} using the standard
-regular expression style used by the Unix utility program grep.
+Search a named file for lines that contain a match for a regular
+expression. The result is a list of all the lines that match.  If no
+lines match, the result is an empty string. All of the Tcl regular
+expression syntax is supported.
 
 @quotation
-@t{@b{grep} @i{filename} @i{regexp} @b{line}}
+@t{@b{grep} @i{-n} @i{filename} @i{regexp} @b{line}}
 @end quotation
 
 @table @asis
+@item @code{-n}
+The @code{-n} option prefixes matched lines in the result with the line
+number, just like GNU @code{grep} does. This option should be used in
+preference to the @code{line} keyword documented below.
+
 @item @code{filename}
 The file to search.
 
@@ -4796,8 +4800,7 @@ utility) to search for.
 
 @item @code{line}
 Use the optional keyword @code{line} to prefix matched lines in the
-result with the line number.  This is an option flag -- type it just as
-shown.
+result with the line number. This usage is depreciated.
 @end table
 
 @node prune procedure, runtest_file_p procedure, grep procedure, Utility Procedures
diff --git a/lib/utils.exp b/lib/utils.exp
index 0bc759f..30f61fa 100644
--- a/lib/utils.exp
+++ b/lib/utils.exp
@@ -201,27 +201,30 @@ proc which { file } {
 
 # Looks for occurrences of a string in a file.
 #     return:list of lines that matched or empty string if none match.
-#     args:  first arg is the filename,
-#            second is the pattern,
-#            third are any options.
-#     Options: line  - puts line numbers of match in list
-#
+#     args:  first arg is optional (e.g. -n)
+#            second is the filename,
+#            third is the pattern,
+#            fourth is any keyword options (e.g. line)
+#     options:
+#         -n  - include line numbers like grep(1)
+#       line  - synonum for -n
+
 proc grep { args } {
+    set options ""
+    if { [lindex $args 0] == "-n" } {
+	append options "line "
+	set args [lrange $args 1 end]
+    }
 
     set file [lindex $args 0]
     set pattern [lindex $args 1]
 
     verbose "Grepping $file for the pattern \"$pattern\"" 3
 
-    set argc [llength $args]
-    if { $argc > 2 } {
-	for { set i 2 } { $i < $argc } { incr i } {
-	    append options [lindex $args $i]
-	    append options " "
-	}
-    } else {
-	set options ""
+    if { [llength $args] > 2 } {
+	append options [join [lrange $args 2 end]]
     }
+    set options [lsort -unique $options]
 
     set i 0
     set fd [open $file r]
diff --git a/testsuite/runtest.all/utils.test b/testsuite/runtest.all/utils.test
index 95181a0..c73156f 100644
--- a/testsuite/runtest.all/utils.test
+++ b/testsuite/runtest.all/utils.test
@@ -158,7 +158,7 @@ if {[llength [grep ${srcdir}/runtest.all/utils.test "^# Test grep!"]] == 1} {
   fail "grep, no options"
 }
 
-# Test grep with line option..
+# Test grep with line option.
 set result [grep ${srcdir}/runtest.all/utils.test "^# Test grep!" line]
 if {[llength $result] == 1 && [regexp {^\d+ # Test grep!} [lindex $result 0]]} {
   pass "grep, line option"
@@ -166,5 +166,13 @@ if {[llength $result] == 1 && [regexp {^\d+ # Test grep!} [lindex $result 0]]} {
   fail "grep, line option"
 }
 
+# Test grep with -n option.
+set result [grep -n ${srcdir}/runtest.all/utils.test "^# Test grep!" line]
+if {[llength $result] == 1 && [regexp {^\d+ # Test grep!} [lindex $result 0]]} {
+  pass "grep, -n option"
+} else {
+  fail "grep, -n option"
+}
+
 # diff file_1 file_2
 # runtest_file_p

_______________________________________________
DejaGnu mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/dejagnu
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEEFS8+rdM1/2KWERKFZ9rcPj90NkkFAlwGdugACgkQZ9rcPj90
NknaABAAqxJsLA4CIVsl6vND9bB0fC5lU7Dt7m0HfCLM05a0xQFYdeS/03NTszq2
8DgwJqyQNNHnAHX3otfM9DCa+6P1N6GF2Pd0An/Q3jcX8D9C6T4288hZM8Jogmak
/j0YKvGvpEuelwdBr7o340TvTiKIjWFVkKpiMsoHV+hT1FTMKenYNJz86uai05T0
02XHnrbWzHuRQr3B0INsy1uHej4jDiW0EnPMrxoh+2gTyrYISfBAJ0Ys/PW82EMT
BVApKNlQEa+aUFEi83j8mlNb+k6aoAL064mLf/SyLII2+3mSh6q0VuRVGeoU9Ngu
SQPIBicMrCofgEIP2xDlUeS8QeQSEiLMM07JSnhgfUZydpx0Jg6ihX/RGYFnqrMW
CcHKWam2htCHeEplIKzYp81OGtboKLTv0qxuYDyjhxOujK1y7EBrMkD+hwEhMmMR
sJfhocugBlcRYfsIW0P9NUMNFL3ZVmHhmQplBkIprvU2LpN+kWKrFuMhZOlNdAqX
U0SYLpQzRYt8uFqJJlbkmJkzaeWkkYLJkDSLTk0Z2+QVOye5z7zaPWzD/jnHHPv+
4XAB05zzrmpdmJZVTczlHU9TJCqjanC5OW7JdToFHjU7Sc7zTB61gk60QhlfqYTu
5bOm2wtFGo6uAJes7QumDb/aurf+eIeomCN+pkqYzgdyqfMi2Ok=
=86fu
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.