#!/usr/bin/perl -w # based on diffmk by merlyn (Randal L. Schwartz @ Stonehenge) # requires /usr/bin/diff that understands -D use strict; use subs qw/usage/; my $myname; my $new; my $old; my $fh; my $delimiter = "___A_VERY_UNLIKELY_STRING___"; # separator string ($myname = $0) =~ s!.*/!!; # save this very early # # Command-line. # usage("missing old-file") unless $#ARGV > -1; usage("cannot read old-file '$old': $!") unless -r ($old = shift); usage("missing new-file") unless $#ARGV > -1; usage("cannot read new-file '$new': $!") unless -r ($new = shift); usage("extra args") if $#ARGV > -1; # # Real work starts here. # print <<'EndHeader';
EndHeader
open($fh, "exec /usr/bin/diff -D$delimiter $old $new |") ||
die "cannot open diff: $!";
while (<$fh>) {
if (m!^#ifdef $delimiter!) {
print "\n";
while (<$fh>) {
last if m!^#endif /\* $delimiter \*/!;
print;
}
print "\n";
}
if (m!^#ifndef $delimiter!) {
# store lines until #endif read.
# an "#else" line means changes; otherwise, deletions.
my @a = ();
my $elseflg = 0;
my $k = 0;
while (<$fh>) {
last if m!^#endif .* $delimiter!;
push (@a, $_);
$elseflg = $k if m!^#else /\* $delimiter \*/!;
$k++;
}
if ($elseflg) {
print "\n";
$k = $elseflg+1;
while ($k <= $#a) {
print "$a[$k]";
$k++;
}
print "\n";
}
else {
print "\n";
foreach (@a) { print; }
print "\n";
}
}
next if m!^#endif!;
print;
}
print <<'EndFooter';
EndFooter
close($fh);
exit(0);
sub usage {
my ($msg) = @_;
die "usage: $myname old-file new-file > marked-file\n$msg\n";
}