[comp.sources.misc] v06i076: perl diffbar

allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (03/16/89)

Posting-number: Volume 6, Issue 76
Submitted-by: Randal L. Schwartz <merlyn@intelob.intel.com>
Archive-name: diffmk.p

Here's a program to emulate the 'diffmk' command for those of us that
don't have it.  It's also a chance for me to flex my perl muscles.

Requires perl (2.0?) and BSD-style "diff -D...".  See comments for
usage.

#! /bin/sh
# This file was wrapped with "dummyshar".  "sh" this file to extract.
# Contents:  diffmk.p
echo extracting 'diffmk.p'
if test -f 'diffmk.p' -a -z "$1"; then echo Not overwriting 'diffmk.p'; else
sed 's/^X//' << \EOF > 'diffmk.p'
X#!/usr/bin/perl
X# original version by merlyn (Randal L. Schwartz @ Stonehenge)
X# LastEditDate = "Wed Mar 15 14:54:56 1989"
X# requires /usr/bin/diff that understands -D
X
X($myname = $0) =~ s!.*/!!; # save this very early
X
Xsub usage {
X	die join("\n",@_) .
X	"\nusage: $myname [-aA] [-cC] [-dD] old-file new-file >marked-file\n";
X}
X
X# defaults:
X$marka = "+"; # lines that are added
X$markc = "|"; # lines that are changed
X$markd = "*"; # deletions (near where they were deleted)
X
Xwhile ($_ = shift) {
X	$marka = $1, next if /^-a(.+)$/;
X	$markc = $1, next if /^-c(.+)$/;
X	$markd = $1, next if /^-d(.+)$/;
X	do usage("unknown flag: $1") if /^(-.*)$/;
X	unshift (@ARGV,$_), last;
X}
X
Xdo usage("missing old-file") unless $#ARGV > -1;
X
Xdo usage("cannot read old-file '$old': $!") unless -r ($old = shift);
X
Xdo usage("missing new-file") unless $#ARGV > -1;
X
Xdo usage("cannot read new-file '$new': $!") unless -r ($new = shift);
X
Xdo usage("extra args") if $#ARGV > -1;
X
X$zzz = "___A_VERY_UNLIKELY_STRING___"; # separator string
X
Xopen(I,"exec /usr/bin/diff -D$zzz $old $new |") || die "cannot open diff: $!";
X
XMAIN: while (<I>) {
X	if (/^#ifdef $zzz/) {
X		print ".mc $marka\n";
X		print while ($_ = <I>) && !/^#endif $zzz/;
X		print ".mc\n";
X		last MAIN if eof;
X		next MAIN;
X	}
X	if (/^#ifndef $zzz/) {
X		while (<I>) {
X			if (/^#else $zzz/) {
X				print ".mc $markc\n";
X				print while ($_ = <I>) && !/^#endif $zzz/;
X				print ".mc\n";
X				last MAIN if eof;
X				next MAIN;
X			}
X			if (/^#endif $zzz/) {
X				print ".mc $markd\n.mc\n";
X				next MAIN;
X			}
X		}
X	}
X	print;
X}
X
Xclose(I);
X
Xexit 0;
EOF
chars=`wc -c < 'diffmk.p'`
if test $chars !=     1636; then echo 'diffmk.p' is $chars characters, should be     1636 characters!; fi
fi
exit 0