[comp.text] v18i018: Generate changebars in ?roff docs using patch

rsalz@uunet.uu.net (Rich Salz) (03/14/89)

Submitted-by: rap@ardent.com (Rob Peck)
Posting-number: Volume 18, Issue 18
Archive-name: changebar

[  This is the best of the several of these that I've seen.  -r$ ]

a program for automatically adding change-bars to nroff/troff
documents.  See the CONTENTS file and the accompanying man-pages
for more information.  NOTE: REQUIRES Larry Wall's 'patch' program.

Rob Peck
Ardent Computer
							 
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  CONTENTS README changebar changebar.1 chbar.lex
#   delete.note
# Wrapped by rsalz@papaya.bbn.com on Mon Mar 13 18:20:02 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'CONTENTS' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'CONTENTS'\"
else
echo shar: Extracting \"'CONTENTS'\" \(1565 characters\)
sed "s/^X//" >'CONTENTS' <<'END_OF_FILE'
XThe shell script 'changebar' uses Larry Wall's 'patch' program
Xto automatically install "change-bars" into an nroff/troff document.
XThe output of a context diff is modified by a lex-generated c program
Xto incorporate the changebars as though they are part of the differences
Xbetween the two files in the first place.  Then when patch runs, it
Xadds the change bars to the text.  After the patch processing, one more
Xpost-processing operation is performed to add line feeds to the final
Xtext, replacing ^G characters that were installed so that patch would
Xbe able to run entirely unmodified.
X
XMy thanks to Larry Wall for creating 'patch' in the first place.
XThis program was prompted by a request by some of my engineers who
Xwanted to be able to "use SCCS to edit to my heart's content, then
Xwhen finally ready to reissue the document, have the change bars
Xinstalled automatically."  It seems to work, so here it is.  If
Xanyone makes any changes, I would like to hear about them; hope it
Xhelps a few folks.
X
XReleased to the public domain, unsupported, do with it as you wish.
X
XThe files contained here are:
X	changebar - a shell script
X	chbar.lex - the lex script that compiles into a
X		    context-diff examiner/modifier as a
X		    preprocessing step before patch is used.
X	chbar.1   - the man page that explains chbar.lex
X	changebar.1 - the man page for the changebar script.
X		      The FILES item will have to be modified
X		      depending on where on your own system
X		      you install things.
X
XRob Peck	Ardent Computer, 880 W. Maude Ave, Sunnyvale, Ca. 94086
X
END_OF_FILE
if test 1565 -ne `wc -c <'CONTENTS'`; then
    echo shar: \"'CONTENTS'\" unpacked with wrong size!
fi
# end of 'CONTENTS'
fi
if test -f 'README' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'README'\"
else
echo shar: Extracting \"'README'\" \(631 characters\)
sed "s/^X//" >'README' <<'END_OF_FILE'
XChangebar only works with files in the current directory.  For example,
X
X	changebar foo1 foo2
X
Xworks fine, but:
X
X	changebar /tmp/foo1 /otherpath/foo2
X
Xdoes not work.  I believe that patch is having a problem with paths.
X
XSo this is just a quick warning to users that this limitation exists.
XI do not plan to fix it, however a user who wanted to fix the situation
Xcould, I suppose, copy each file to a known name in /tmp, for example,
Xthen pushd to /tmp, do the changebar work there, then delete the temp
Xfiles, copy the .chbar result to the original directory and finally
Xget rid of any other temp files that we created.
X
XRob Peck
END_OF_FILE
if test 631 -ne `wc -c <'README'`; then
    echo shar: \"'README'\" unpacked with wrong size!
fi
# end of 'README'
fi
if test -f 'changebar' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'changebar'\"
else
echo shar: Extracting \"'changebar'\" \(1422 characters\)
sed "s/^X//" >'changebar' <<'END_OF_FILE'
X#!/bin/sh
X#
X#  Script to automatically add changebars to troff docs.
X#
Xif [ $# -lt 1 ]; then
X#
X#  If user does not enter parameters, show usage info.
X#
X	echo
X	echo "	changebar --> Usage:   changebar OLDFILE NEWFILE"
X	echo
X	echo "	Produces NEWFILE.chbar that is NEWFILE with changebars,"
X	echo "	leaves original files intact."
X	echo
Xelse
X#  Run a context diff on the two files
X#
X	diff -c $2 $1 > $1.diff
X#
X#  Install change bars in the context diff,
X#  for ADDITIONS and MODIFICATIONS only.
X#  DELETIONS not marked -- reverse the
X#  sequence of the file names and run it
X#  again to get an original file with
X#  deletions and modifications marked.
X#
X	chbar < $1.diff > $1.patch
X#
X#  Run the patch program to install the
X#  now-modified context diffs.
X#
X	patch -R < $1.patch
X#
X#  Remove the intermediate files;
X#  assumes that all of the patches worked.
X#
X	rm $1.diff $1.patch
X#
X#  Translate ^G to \n, installed for
X#  convenience to make patch work unmodified.
X#  Name the filtered version as $2.chbar.
X#
X	tr '\007' '\012' < $1 > $2.chbar
X#
X#  Make the patched (intermediate) file removable
X#
X	chmod 666 $1
X#
X#  Patch renamed original file;
X#  restore the original name
X#
X	mv $1.orig $1
X#
X#  Now tell the user what we did.
X#
X	echo 
X	echo "All of the hunks should have succeeded"
X	echo
X	echo "There is now a new file named $2.chbar that is exactly"
X	echo "like your file named $2, but it has changebars installed."
X	echo
Xfi
END_OF_FILE
if test 1422 -ne `wc -c <'changebar'`; then
    echo shar: \"'changebar'\" unpacked with wrong size!
fi
# end of 'changebar'
fi
if test -f 'changebar.1' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'changebar.1'\"
else
echo shar: Extracting \"'changebar.1'\" \(1356 characters\)
sed "s/^X//" >'changebar.1' <<'END_OF_FILE'
X.TH CHBAR 1L "October 7, 1988" "" "User Contributed Software"
X.SH NAME
Xchbar \- modify a context diff to add change bars 
X.br
X.nf
X        before running patch
X.fi
X.SH SYNOPSIS
X.B changebar OLDFILE NEWFILE
X.SH FUNCTION
XInstalls ".mc |^G" as part of the first line of a changed-line
Xregion and is part of the first line of a region of added-lines
Xas indicated by a context diff 
X.br
X.sp
X.nf
X	(diff -c oldfile newfile).
X.fi
X.sp
X.PP
XAlso installs "^G.mc" as part of the last line of a changed-line
Xregion, and as part of the last line of an added-line region.
XThis fools the patch program into believing that these characters
Xare part of the patch to be installed to create the new file.
XFollowing running patch, a filter must be run to change the
XControl-G characters into line feeds;  results in placing
Xthe ".mc |" on a separate line ahead of the changed region
Xand the ".mc" on a separate line just following the end of
Xthe changed region, thus causing the region to be marked.
X.SH LIMITATIONS
XSee LIMITATIONS in the man-page that describes ``changebar''.
X.SH AUTHOR
XRob Peck \- Ardent Computer
X.SH FILES
X/usr/local/lib/chbar.lex
X.br
X/usr/local/bin/chbar
X.br
X/usr/local/lib/PATCH.src (source directory)
X.br
X/usr/local/bin/patch (by Larry Wall) \- version 2.0
X.br
X/usr/local/bin/changebar (a shell script)
X.SH BUGS
XNone known at this time.
X.SH DATE
X10/7/88
END_OF_FILE
if test 1356 -ne `wc -c <'changebar.1'`; then
    echo shar: \"'changebar.1'\" unpacked with wrong size!
fi
# end of 'changebar.1'
fi
if test -f 'chbar.lex' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'chbar.lex'\"
else
echo shar: Extracting \"'chbar.lex'\" \(3615 characters\)
sed "s/^X//" >'chbar.lex' <<'END_OF_FILE'
X/* Compile this program using lex and cc, name the object file 'chbar'
X * and install it somewhere that the system can get to it using the
X * 'changebar' shell script.
X */
X%{
Xint stat=0;
Xint bangstat=0;
Xchar txtbuf[256]=0;
X%}
X%%
X^[^-\*\n\!].*$     {
X                /* If already counting lines that have either a '- '
X                 * or a '! ' or a '*...' in the first columns, and encounter
X                 * a line that does NOT have that, put the closing
X                 * ^G.mc at the tail of the previous line and disable
X                 * the insertion of the .mc till the first line with
X                 * either of the two markers happens again.
X                 */
X                    if(stat == 1) {
X                        printf("%s\007.mc\n",txtbuf);
X                        stat = 0;
X                    }
X                    puts(yytext);
X                }
X
X^---\ .*$       {   puts(yytext);  /* If in a '---' block in a context diff,
X                                * then disable entry of .mc's in text lines.
X                                */
X                    bangstat = 0;
X                }
X
X^\*\*\*\ .*$    {   puts(yytext);  /* If in a '***' block in a context diff,
X                                * then enable entry of .mc's in text lines.
X                                */
X                    bangstat = 1;
X                }
X
X^\*\*\*.*$	{   puts(yytext);
X		}
X
X^\-\ .*$        {
X                    /* If find a line that begins "- <anything>", and it
X                     * is the first line of its kind, then mark it.
X                     */
X                if(stat == 0) {
X                    sprintf(txtbuf,"- .mc \\s+2\\(br\\s-2\007%s",&yytext[2]);
X                    stat = 1;
X                    }
X                else {
X                    puts(txtbuf);
X                    sprintf(txtbuf,"%s",yytext);
X                    }
X                }
X
X
X
X
X
X
X
X
X^\!\ .*$        {
X                    /* If find a line that begins "! <anything>", and it
X                     * is the first line of its kind, then mark it.
X                     */
X                  if(bangstat==1) 
X                  {
X                        if(stat == 0) {
X                          sprintf(txtbuf,"! .mc \\s+2\\(br\\s-2\007%s",
X                                      &yytext[2]);
X                          stat = 1;
X                        }
X                        else {
X                          puts(txtbuf);
X                          sprintf(txtbuf,"%s",yytext);
X                          }
X                  }
X                  else {
X                    puts(yytext);
X                  }
X                }
X\n  		{
X                   ;
X		   /* If there is nothing but a newline, do nothing, because
X                    * we are inserting our own newlines as needed.
X                    */
X		}
X%%
Xyywrap()
X{
X    if (stat == 1)
X        printf("%s\007.mc\n",txtbuf);
X
X    return (1);
X}
X/*
X    This LEX file adds change bars to the output of a 'diff -c'
X    to indicate to the reader where sections have been added
X    (no handling so far for lines that have been deleted).
X
X    This allows the user to edit under SCCS to your heart's content,
X    then SCCS-extract the old version and the new version...
X    then:
X
X    a. diff -c oldfile newfile > filediffs
X    b. addchgbars < filediffs > filemods
X    c. patch < filemods
X    
X    # oldfile is renamed to oldfile.orig
X    # oldfile becomes newfile, but with change bars installed
X
X    d. tr '\007' '\012' oldfile > file.to.print
X
X    # change bars have ^G installed, has to be changed into a ^J;
X    # makes patch program work unmodified.
X
X*/
END_OF_FILE
if test 3615 -ne `wc -c <'chbar.lex'`; then
    echo shar: \"'chbar.lex'\" unpacked with wrong size!
fi
# end of 'chbar.lex'
fi
if test -f 'delete.note' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'delete.note'\"
else
echo shar: Extracting \"'delete.note'\" \(634 characters\)
sed "s/^X//" >'delete.note' <<'END_OF_FILE'
XMARKING DELETIONS TO THE TEXT
X
XThere is no EASY way I can find to appropriately mark DELETIONS to
Xa file with change bars.  Either do deletions manually, or run this
Xprogram in reverse:
X
X	changebar NEW OLD
X
XThis will create a file named OLD.chbar, which will be the exact
Xequivalent of the OLD file, but with change bars marking the deleted
Xtext.  You could therefore print an NEW.chbar (equal to the NEW file
Xwith change bars indicating added text) and OLD.chbar (equal to the
XOLD file with deleted text highlighted) for your users and they'd
Xhave a complete picture showing what you did to the files between
Xissues of the document.
END_OF_FILE
if test 634 -ne `wc -c <'delete.note'`; then
    echo shar: \"'delete.note'\" unpacked with wrong size!
fi
# end of 'delete.note'
fi
echo shar: End of shell archive.
exit 0
-- 
Please send comp.sources.unix-related mail to rsalz@uunet.uu.net.