Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!cca!decvax!harpo!floyd!vax135!cornell!uw-beaver!ubc-visi!alberta!mah From: mah@alberta.UUCP Newsgroups: net.sources Subject: diff to ed script Message-ID: <345@alberta.UUCP> Date: Wed, 8-Jun-83 16:35:42 EDT Article-I.D.: alberta.345 Posted: Wed Jun 8 16:35:42 1983 Date-Received: Sat, 11-Jun-83 08:30:43 EDT Lines: 263 echo "x - Readme" cat >Readme <dscript.nr <10000 lines) .SH AUTHOR Ken S. Mah, Electical Engineering, University of Alberta, CANADA (alberta!mah) !FUNKY!STUFF! echo "x - dscript.c" cat >dscript.c < #include#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!