awgn/cgrep

file arguments ignored when stdin is null

ilovezfs opened this issue · 7 comments

iMac-TMP:~ joe$ cgrep -V
Cgrep 6.6.18. Usage: cgrep [OPTION] [PATTERN] files...
iMac-TMP:~ joe$ cat test.rb 
# puts test comment.
puts "test literal."
iMac-TMP:~ joe$ cgrep --count --comment puts test.rb 
test.rb:1
iMac-TMP:~ joe$ cgrep --count --comment puts test.rb </dev/null
iMac-TMP:~ joe$

So this makes it impossible to test cgrep in a non-PTY without resorting to something like

iMac-TMP:~ joe$ cat test.rb | cgrep --count --comment --language-force=ruby puts
<STDIN>:1
iMac-TMP:~ joe$

Note that standard grep does not have this problem:

iMac-TMP:~ joe$ grep --count puts test.rb </dev/null
2
awgn commented

Cgrep is not intended as a drop-in replacement of grep.
For instance, filename in the output format is enabled by default, even in combination with --count (that causes the annoying "STDIN").
The option -h can be used to suppress the filename in the output format. Unfortunately, due to a bug, the option did not work along with --count.
Now the bug is fixed. Thanks for reporting.

@awgn I've just tested HEAD @ 327588e. The bug persists. It still fail when stdin is null. There's no -h involved. And --count doesn't need to be specified either.

bash-3.2$ cat test.rb                 
# puts test comment.
puts "test literal."
bash-3.2$ cgrep test test.rb </dev/null
bash-3.2$ 

Basically, it's prioritizing the null stdin over the explicit file argument.

The expected output would be

test.rb:1:1:# puts test comment.
test.rb:2:7:puts "test literal."

which would be the same as you get without the </dev/null.

bash-3.2$ cgrep test test.rb           
test.rb:1:1:# puts test comment.
test.rb:2:7:puts "test literal."
bash-3.2$ 
awgn commented

This is another issue that I need to fix. Since I'm getting ready for the year-end party, I'm not sure to be able to fix it immediately.

@awgn no worries. It's a very old issue. I noticed we were using /usr/bin/script in the Homebrew formula's test block to work around this. That workaround had been there since October 2015.

awgn commented

This issue should be fixed! Now cgrep takes the first item as pattern and the rest as list of files to search in, regardless if stdin has been redirected or not. Happy New Year!

@awgn Much better! It works now! Thank you :)