[alt.sources] Add Change Bars to troff output

hutch@fps.com (Jim Hutchison) (11/17/90)

#! /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:  add_cb.doc add_cb.c
#
#	-rw-r--r--  1 hutch        2138 Nov 16 13:39 add_cb.c
#	-rw-r--r--  1 hutch        1068 Nov 16 13:40 add_cb.doc
#
#	34282     3 add_cb.c
#	39821     2 add_cb.doc
#
# Wrapped by hutch@dreamit on Fri Nov 16 13:40:54 1990
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'add_cb.doc' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'add_cb.doc'\"
else
echo shar: Extracting \"'add_cb.doc'\" \(1068 characters\)
sed "s/^X//" >'add_cb.doc' <<'END_OF_FILE'
X
X    add_cb	-- Add change bars to troff output (Public Domain)
X
XAdd_cb adds change bars to documents or other troff source, based on 'diff'
Xoutput.  I've tested it with documents and vgrind output, and found it to
Xwork satisfactorily.
X
XIt does expect diff output to look something like:
X<non-numeric>anything
X#,#a#
X<non-numeric>anything
X#,#a#,#
X<non-numeric>anything
X#,#c#
X<non-numeric>anything
X#,#c#,#
X<non-numeric>anything
X
XWhich is normal for most default /bin/diff output (atleast on an FPS model
X500 or Sun.  If your system is different, or you want to use a different
Xsymbol for change bars, the code is short and easy to modify.  You will want
Xto modify it if you have lines longer than 1024 bytes.
X
XTo compile:
X    cc -o add_cb add_cb.c
X
XUsage examples:
X    diff old.ms new.ms | add_cb new.ms | ditroff -ms
X
X    rcsdiff new.me  | add_cb new.me | ditroff -me
X
X    ( echo .vS ; rcsdiff foo.c | add_cb foo.c ; echo .vE ) |\
X	vgrind -n -s10 -f -t | dtroff -ms
X
XPlease feel free to send me Email if you run across any bugs.
X
X    Jim Hutchison	hutch@rawfish.fps.com
END_OF_FILE
if test 1068 -ne `wc -c <'add_cb.doc'`; then
    echo shar: \"'add_cb.doc'\" unpacked with wrong size!
fi
# end of 'add_cb.doc'
fi
if test -f 'add_cb.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'add_cb.c'\"
else
echo shar: Extracting \"'add_cb.c'\" \(2138 characters\)
sed "s/^X//" >'add_cb.c' <<'END_OF_FILE'
X/* Public Domain Change Bar utility
X**
X** Adds change bars to a file, based on /bin/diff output
X**
X** To compile:
X**    cc -o add_cb add_cb.c
X**
X** Usage examples:
X**    diff old.ms new.ms | add_cb new.ms | ditroff -ms
X**
X**    rcsdiff new.me  | add_cb new.me | ditroff -me
X**
X**    ( echo .vS ; rcsdiff foo.c | add_cb foo.c ; echo .vE ) |\
X**	vgrind -n -s10 -f -t | dtroff -ms
X**
X** Please feel free to send me Email if you run across any bugs.
X**
X**    Jim Hutchison	hutch@rawfish.fps.com
X*/
X#include <stdio.h>
X#include <ctype.h>
X#include <sys/types.h>
X
X#define BEGIN_CHANGE	".mc \\fB\\s18|\\s0\\fR 2"
X#define END_CHANGE	".mc"
X
Xmain(argc, argv)
X    int argc;
X    char **argv;
X{
X    char buf[1024];
X    int a, b;
X    register FILE *fp;
X    register u_int start, end, idx, cnt;
X    register char *bp = buf, *cp;
X
X    if (argc < 2) {
X	fprintf(stderr, "Usage: diff old new | add_cb new > foo\n");
X	fprintf(stderr, "       rcsdiff new  | add_cb new > foo\n");
X	exit(-1);
X    }
X
X    if ((fp = fopen(argv[1], "r")) == NULL)
X	perror(argv[1]), exit(-1);
X
X    start = end = -1;
X    idx = 1;
X    while (!feof(fp)) {
X	while (fgets(bp, sizeof(buf), stdin) != NULL) {
X
X	    /* Look for a line like:
X	    **	#,#a#
X	    **	#,#a#,#
X	    **	#,#c#
X	    **	#,#c#,#
X	    */
X	    if (isdigit(*bp)) {	/* may be a command */
X		for (cp = bp; cp && *cp != 'a' && *cp != 'c'; cp++)
X		    ;
X		cp++;		/* get to number(s) */
X
X		cnt = sscanf(cp, "%d,%d", &a, &b);
X		if (cnt == 1)
X		    start = end = a;
X		else if (cnt == 2) {
X		    start = a;
X		    end = b;
X		}
X		/* end++;		/* step 1 past the change */
X
X		if (cnt > 0) 	/* got a "change" range */
X		    break;
X	    }
X	}
X
X	for (; idx < start && !feof(fp); idx++)
X	    copyline(fp, stdout);
X
X	if (start == idx)
X	    puts(BEGIN_CHANGE);
X	
X	for (; idx <= end && !feof(fp); idx++)
X	    copyline(fp, stdout);
X
X	if (idx >= end) {
X	    puts(END_CHANGE);
X	    end = (u_int)0x7fffffff;
X	}
X    }
X
X    fclose(fp);
X    exit(0);
X}
X
X/* Copy a line from fin to fout
X*/
Xcopyline(fin, fout)
X    register FILE *fin, *fout;
X{
X    register int c;
X
X    while ((c = getc(fin)) != EOF) {
X	putc(c, fout);
X	if (c == '\n')
X	    break;
X    }
X}
END_OF_FILE
if test 2138 -ne `wc -c <'add_cb.c'`; then
    echo shar: \"'add_cb.c'\" unpacked with wrong size!
fi
# end of 'add_cb.c'
fi
echo shar: End of shell archive.
exit 0
--
-
Jim Hutchison		{dcdwest,ucbvax}!ucsd!fps!hutch
Disclaimer:  I am not an official spokesman for FPS computing