1
0
mirror of git://projects.qi-hardware.com/wernermisc.git synced 2024-12-01 03:27:32 +02:00
wernermisc/bin/grepchunk

65 lines
864 B
Plaintext
Raw Normal View History

#!/usr/bin/perl
sub usage
{
print STDERR "usage: $0 [-v] pattern [file ...]\n";
exit(1);
}
if ($ARGV[0] eq "-v") {
$invert = 1;
shift @ARGV;
}
&usage if $ARGV[0] =~ /^-/;
$pattern = shift @ARGV;
&usage unless defined $pattern;
sub flush
{
my $mismatch = !($chunk =~ /$pattern/m);
if ($mismatch == $invert) {
print $head;
print $chunk;
}
undef $head;
undef $chunk;
}
$HEAD = 0;
$CHUNK = 1;
$TAIL = 2;
$state = $HEAD;
while (<>) {
$state = $HEAD if /^diff /;
if (/^@@ -\d+,(\d+) \+\d+,(\d+)/) {
$state = $CHUNK;
($in, $out) = ($1, $2);
$chunk = $_;
next;
}
if ($state == $HEAD) {
$head .= $_;
next;
}
next if $state == $TAIL;
$in-- if /^-/;
$out-- if /^\+/;
($in--, $out--) if /^\s/;
die "in $in out $out" if $in < 0 || $out < 0;
if ($in+$out == 0) {
$state = $TAIL;
&flush;
} else {
$chunk .= $_;
}
}
&flush;