#!/usr/bin/perl use strict; use File::Temp qw/ :mktemp /; sub usage() { print "usage: cat diff | $0 old new old new old new...\n"; print " or: cat diff | $0 -e 's/old/new/g'\n"; print " -e : execute on old lines\n"; print " -ea: execute on all lines\n"; print " -nc: strip one line comments\n"; exit(1); } my @subs; my @cmds; my $strip_comments; sub filter($) { my $_ = shift(); my $old = 0; if ($_ =~ /^-/) { $old = 1; } # remove the first char s/^[ +-]//; if ($strip_comments) { s/\/\*.*?\*\///g; s/\/\/.*//; } foreach my $cmd (@cmds) { if ($old || $cmd->[0] =~ /^-ea$/) { eval $cmd->[1]; } } foreach my $sub (@subs) { if ($old) { s/$sub->[0]/$sub->[1]/g; } } return $_; } my ($oldfh, $oldfile) = mkstemp("/tmp/oldXXXXX"); my ($newfh, $newfile) = mkstemp("/tmp/newXXXXX"); while (my $param1 = shift()) { if ($param1 =~ /^-nc$/) { $strip_comments = 1; next; } my $param2 = shift; if ($param2 =~ /^$/) { usage(); } if ($param1 =~ /^-e(a|)$/) { push @cmds, [$param1, $param2]; next; } push @subs, [$param1, $param2]; } while (<>) { my $line = $_; if ($line =~ /^(---|\+\+\+)/) { next; } my $output = filter($line); if ($line =~ /^-/) { print $oldfh $output; next; } if ($line =~ /^\+/) { print $newfh $output; next; } print $oldfh $output; print $newfh $output; } system("diff -uw $oldfile $newfile"); unlink($oldfile); unlink($newfile);