aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Elliston <bje@gnu.org>2018-12-05 10:48:26 +1100
committerBen Elliston <bje@gnu.org>2018-12-05 10:48:26 +1100
commitd4a10385ee206e7ed40872b70040ce3b66a60e59 (patch)
tree527ce8a12ae68705b962beec623db965a85c8861
parent0d91a5aa03df4045cd87893e63156fd1469ecf77 (diff)
downloaddejagnu-d4a10385ee206e7ed40872b70040ce3b66a60e59.zip
dejagnu-d4a10385ee206e7ed40872b70040ce3b66a60e59.tar.gz
dejagnu-d4a10385ee206e7ed40872b70040ce3b66a60e59.tar.bz2
* lib/utils.exp (grep): Handle -n.
* doc/dejagnu.texi (grep procedure): Document it. * testsuite/runtest.all/utils.test: Add a test case. * NEWS: Add an item.
-rw-r--r--ChangeLog7
-rw-r--r--NEWS2
-rw-r--r--doc/dejagnu.texi21
-rw-r--r--lib/utils.exp29
-rw-r--r--testsuite/runtest.all/utils.test10
5 files changed, 45 insertions, 24 deletions
diff --git a/ChangeLog b/ChangeLog
index 9ca4a74..0835237 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
2018-12-05 Ben Elliston <bje@gnu.org>
+ * lib/utils.exp (grep): Handle -n.
+ * doc/dejagnu.texi (grep procedure): Document it.
+ * testsuite/runtest.all/utils.test: Add a test case.
+ * NEWS: Add an item.
+
+2018-12-05 Ben Elliston <bje@gnu.org>
+
* testsuite/runtest.all/utils.test: Test diff.
2018-12-04 Ben Elliston <bje@gnu.org>
diff --git a/NEWS b/NEWS
index aed6a9b..35466ce 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,8 @@ Changes since 1.6.2:
the default of reading "site.exp". See the manual for details.
3. A utility procedure relative_filename has been added. This procedure
computes a relative file name to a given destination from a given base.
+4. The utility procedure 'grep' now accepts a '-n' option that
+ includes line numbers in the output, consistent with GNU grep.
Changes since 1.6.1:
diff --git a/doc/dejagnu.texi b/doc/dejagnu.texi
index 7273e90..3d4b4df 100644
--- a/doc/dejagnu.texi
+++ b/doc/dejagnu.texi
@@ -2427,7 +2427,7 @@ Is @i{board} remote? Return a non-zero value, if so.
@t{@b{is_remote} @{@i{ board }@}}
@end quotation
-Note that this procedure is now depreciated. Use @code{isremote} instead.
+Note that this procedure is now deprecated. Use @code{isremote} instead.
@node is3way procedure, ishost procedure, is_remote procedure, Core Internal Procedures
@subsubheading is3way Procedure
@@ -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 deprecated.
@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 7051b3f..6277e4e 100644
--- a/testsuite/runtest.all/utils.test
+++ b/testsuite/runtest.all/utils.test
@@ -166,8 +166,14 @@ if {[llength $result] == 1 && [regexp {^\d+ # Test grep!} [lindex $result 0]]} {
fail "grep, line option"
}
-# diff file_1 file_2
-# runtest_file_p
+# Test grep with -n option.
+set result [grep -n ${srcdir}/runtest.all/utils.test "^# Test grep!"]
+if {[llength $result] == 1 && [regexp {^\d+ # Test grep!} [lindex $result 0]]} {
+ pass "grep, -n option"
+} else {
+ fail "grep, -n option"
+}
+
# Test diff proc.
# Setup.