dejagnu
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

PATCH: add -n option to proc grep


From: Ben Elliston
Subject: PATCH: add -n option to proc grep
Date: Tue, 4 Dec 2018 23:45:29 +1100
User-agent: NeoMutt/20170609 (1.8.3)

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  <address@hidden>

        * 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
address@hidden@b{grep} @i{filename} @i{regexp} @b{line}}
address@hidden@b{grep} @i{-n} @i{filename} @i{regexp} @b{line}}
 @end quotation
 
 @table @asis
address@hidden @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

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]