home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume06 / diffmk.p < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  2.7 KB

  1. Path: wugate!wucs1!uunet!allbery
  2. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  3. Newsgroups: comp.sources.misc
  4. Subject: v06i076: perl diffbar
  5. Message-ID: <51368@uunet.UU.NET>
  6. Date: 15 Mar 89 23:57:46 GMT
  7. Sender: allbery@uunet.UU.NET
  8. Reply-To: Randal L. Schwartz <merlyn@intelob.intel.com>
  9. Lines: 89
  10. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  11.  
  12. Posting-number: Volume 6, Issue 76
  13. Submitted-by: Randal L. Schwartz <merlyn@intelob.intel.com>
  14. Archive-name: diffmk.p
  15.  
  16. Here's a program to emulate the 'diffmk' command for those of us that
  17. don't have it.  It's also a chance for me to flex my perl muscles.
  18.  
  19. Requires perl (2.0?) and BSD-style "diff -D...".  See comments for
  20. usage.
  21.  
  22. #! /bin/sh
  23. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  24. # Contents:  diffmk.p
  25. echo extracting 'diffmk.p'
  26. if test -f 'diffmk.p' -a -z "$1"; then echo Not overwriting 'diffmk.p'; else
  27. sed 's/^X//' << \EOF > 'diffmk.p'
  28. X#!/usr/bin/perl
  29. X# original version by merlyn (Randal L. Schwartz @ Stonehenge)
  30. X# LastEditDate = "Wed Mar 15 14:54:56 1989"
  31. X# requires /usr/bin/diff that understands -D
  32. X
  33. X($myname = $0) =~ s!.*/!!; # save this very early
  34. X
  35. Xsub usage {
  36. X    die join("\n",@_) .
  37. X    "\nusage: $myname [-aA] [-cC] [-dD] old-file new-file >marked-file\n";
  38. X}
  39. X
  40. X# defaults:
  41. X$marka = "+"; # lines that are added
  42. X$markc = "|"; # lines that are changed
  43. X$markd = "*"; # deletions (near where they were deleted)
  44. X
  45. Xwhile ($_ = shift) {
  46. X    $marka = $1, next if /^-a(.+)$/;
  47. X    $markc = $1, next if /^-c(.+)$/;
  48. X    $markd = $1, next if /^-d(.+)$/;
  49. X    do usage("unknown flag: $1") if /^(-.*)$/;
  50. X    unshift (@ARGV,$_), last;
  51. X}
  52. X
  53. Xdo usage("missing old-file") unless $#ARGV > -1;
  54. X
  55. Xdo usage("cannot read old-file '$old': $!") unless -r ($old = shift);
  56. X
  57. Xdo usage("missing new-file") unless $#ARGV > -1;
  58. X
  59. Xdo usage("cannot read new-file '$new': $!") unless -r ($new = shift);
  60. X
  61. Xdo usage("extra args") if $#ARGV > -1;
  62. X
  63. X$zzz = "___A_VERY_UNLIKELY_STRING___"; # separator string
  64. X
  65. Xopen(I,"exec /usr/bin/diff -D$zzz $old $new |") || die "cannot open diff: $!";
  66. X
  67. XMAIN: while (<I>) {
  68. X    if (/^#ifdef $zzz/) {
  69. X        print ".mc $marka\n";
  70. X        print while ($_ = <I>) && !/^#endif $zzz/;
  71. X        print ".mc\n";
  72. X        last MAIN if eof;
  73. X        next MAIN;
  74. X    }
  75. X    if (/^#ifndef $zzz/) {
  76. X        while (<I>) {
  77. X            if (/^#else $zzz/) {
  78. X                print ".mc $markc\n";
  79. X                print while ($_ = <I>) && !/^#endif $zzz/;
  80. X                print ".mc\n";
  81. X                last MAIN if eof;
  82. X                next MAIN;
  83. X            }
  84. X            if (/^#endif $zzz/) {
  85. X                print ".mc $markd\n.mc\n";
  86. X                next MAIN;
  87. X            }
  88. X        }
  89. X    }
  90. X    print;
  91. X}
  92. X
  93. Xclose(I);
  94. X
  95. Xexit 0;
  96. EOF
  97. chars=`wc -c < 'diffmk.p'`
  98. if test $chars !=     1636; then echo 'diffmk.p' is $chars characters, should be     1636 characters!; fi
  99. fi
  100. exit 0
  101.