[net.sources] diff to ed script

mah@alberta.UUCP (06/08/83)

echo "x - Readme"
cat >Readme <<!FUNKY!STUFF!
#
# This program takes 'diff' output and rearranges it to
# produce 'ed'itor input.  This is useful when only the
# diff output exists and you wish to recreate/create the
# file without manually editing the file.
#
# This is a shell script file. So just execute it.
# (Removing the header of course.)
#
# If there are any problems please mail to:
#
#				...alberta!mah
# or:
#
#  Ken S. Mah
#  Electrical Engineering
#  University of Alberta
#  Edmonton, Alberta
#  CANADA
#
!FUNKY!STUFF!
echo "x - dscript.nr"
cat >dscript.nr <<!FUNKY!STUFF!
.TH DSCRIPT I
.SH NAME
dscript \- changes diff output to 'ed'itor script
.SH SYNOPSIS
.B dscript [file ...]
.SH DESCRIPTION
.I Dscript
changes ordinary 
.I diff
output to 
.I ed
input.  This is useful for large or small 
.I diff 
outputs when it is
not possible to use the
.I diff -e
option to produce a script of 
.I a, c
and
.I d
commands for the editor
.I ed
in which to recreate
.I file2
from
.I file1.
.PP
.I Dscript
take the input from
.I stdin
and outputs on
.I stdout.
.PP
All error message appear on
.I stderr.
.SH DIAGNOSTICS
The problem is vaguely and sometimes accurately described.
.SH "SEE ALSO"
.IR diff (1),
.IR ed(1),
.SH BUGS
Overly large diff output files may have a tendency to choke.(ie >10000 lines)
.SH AUTHOR
Ken S. Mah, Electical Engineering, University of Alberta, CANADA
(alberta!mah)
!FUNKY!STUFF!
echo "x - dscript.c"
cat >dscript.c <<!FUNKY!STUFF!
/*
 * Written by: Ken S. Mah
 * Electrical Engineering
 * University of Alberta 
 * Canada
 * June 8, 1983
 * Version 1.1
 */

/*
 * dscript:
 *
 * This program takes 'diff' output and strips it down 
 * into 'ed' commands suitable for the UNIX editor.
 * It produces code like the 'diff -e'.
 * This program is great if you have massive diff output
 * and you don't want to manually change everything by hand.
 */

#include <stdio.h>
#include <ctype.h>

#define	MAXLINE	10000
char *lineptr[MAXLINE];
char *progname;
char *malloc();
char *tempfile;
char *fgets();
char *strcpy();
char *strncpy();
char *mktemp();
int delete = 0;

main(argc,argv)
char *argv[];
{
	FILE *fp, *fopen();

	progname = argv[0];

	if (argc == 1) {
		filescan(stdin);
	} else
		while(--argc > 0)
			if ((fp = fopen(*++argv, "r")) == NULL) {
				fprintf(stderr,"%s: can't open %s.\n",progname, *argv);
				exit(1);
			} else {
				filescan(fp);
				fclose(fp);
			}
	unlink(tempfile);
	exit(0);
}

filescan(fp)
register FILE *fp;
{
	register int nlines, len ,nlen;
	register FILE *ftemp;
	char line[MAXLINE], *p;

	invert(fp);
	if ((ftemp = fopen(tempfile, "r")) == NULL) {
		fprintf(stderr,"%s: can't open %s.\n",progname, tempfile);
		exit(1);
	}

	nlines = 0;
	while((fgets(line, MAXLINE, ftemp)) != NULL) {
		if((p = malloc((unsigned)(len = strlen(line)))) == NULL)
			error("allocation error");
		else {
			line[len-1] = '\0';	/* zap newline */
			strcpy(p, line);
			if((nlen = scanln(p)) == 0) {
				strcpy(p, &line[2]);
				lineptr[nlines++] = p;
			} else if (nlen > 0) {
				strncpy (p, line, nlen);
				p[nlen]= '\0';
				lineptr[nlines++] = p;
				copytofile(lineptr, nlines);
				nlines = 0;
			}
		}
	}
}

copytofile(lineptr,nlines)
char *lineptr[];
int nlines;
{
	register i;

	if(nlines == 0)
		printf("%s\n",lineptr[nlines]);
	else if (nlines >= 0) {
		while(nlines--)
			printf("%s\n",lineptr[nlines]);
		if(!delete)
			printf(".\n");
		else
			delete = 0;
	} else
		error("copy error");
}

scanln(line)
char *line;
{
	register i, n;

	n = 0;
	for (i=0;line[i] != '\0';i++) {
		switch(line[i]) {
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		case '0':
		case ',':
			n++;
			break;
		case 'a':
		case 'c':
			n++;
			return(n);
		case 'd':
			n++;
			delete++;
			return(n);
		case '>':
			return(0);
		case '-':
		case '<':
			return(-1);
		case ' ':
		case '\t':
		case '\n':
			break;
		default:
			error("Illegal ed command");
		}
	}
}

error(str)
char *str;
{
	fprintf(stderr, "%s: %s.\n", progname, str);
	exit(1);
}

invert(fp)
register FILE *fp;
{
	register int nlines, len;
	register char *p, line[MAXLINE];
	register FILE *ftemps;

	nlines = 0;
	while((fgets(line, MAXLINE, fp)) != NULL) {
		if((p = malloc((unsigned)(len = strlen(line)))) == NULL 
		   || nlines >= MAXLINE)
			error("allocation error");
		else {
			line[len-1] = '\0';
			strcpy(p, line);
			lineptr[nlines++] = p;
		}
	}
	tempfile = mktemp("/tmp/dscrXXXXXX");
	if ((ftemps = fopen(tempfile, "w+")) == NULL) {
		fprintf(stderr,"%s: can't open %s.\n",progname, tempfile);
		exit(1);
	}

	while(nlines--){
		fputs(lineptr[nlines],ftemps);
		fputs("\n",ftemps);
	}
	fclose(ftemps);
}
!FUNKY!STUFF!