From: wendt@CS.ColoState.EDU (alan l wendt)
Subject: Very small program development environment for Icon
Date: Thu, 17 Dec 1992 18:35:34 GMT
Organization: Colorado State University, Computer Science Department



Aplogies for posting C source to this Icon group.  This is a modified
version of mcc, which allows you to fix syntax  errors quickly in
Icon programs.  It runs icont and stores the error output.  It then pops
you back into an editor positioned at the first syntax error.

In order to use it with icont, you must define the following
environment variables:

COMPILER=icont
ERRORPATTERN='File %f; Line %l # %m'

Then just "mcc foo.icn" will put you into a icont-edit loop
until you abort out.

Alan Wendt

--------------------------------------------------------------------------------
# to unbundle, sh this file
echo unbundling Makefile 1>&2
cat >Makefile <<'AlBeRtEiNsTeIn'
#  If you're on 4.2 or earlier, use the first line; else the second.
CFLAGS= -g  -DBSD
#CFLAGS=-O

mcc:	mcc.o regex.o
	cc $(CFLAGS) -o mcc mcc.o regex.o

all:	mcc mcc.1

install:	all
	@echo copy mcc and mcc.1 to the appropriate directories.
AlBeRtEiNsTeIn
echo unbundling mcc.c 1>&2
cat >mcc.c <<'AlBeRtEiNsTeIn'
/*  Program: mcc
 *  By:      Brent Callaghan
 *  Date:    July 1984
 *
 *  Function: Runs the C compiler, passing all command line 
 *	    arguments. If the compiler returns a non-zero
 *	    result, the syntax errors are merged with the
 *	    source and the user's editor is invoked. The
 *	    cursor is placed on the first line in error.
 *	    Exit from the editor re-invokes the C compiler.
 *	    This loop continues until the C compiler exits
 *	    to the linker, the source file is not changed,
 *	    or the user kills mcc with a keyboard interrupt
 *	    after exiting the editor.
 *
 *	    Environment variables EDITOR and COMPILER may
 *	    be used to set an alternative editor or compiler.
 *
 *		      ~~~  PUBLIC DOMAIN  ~~~
 *	    
 *	   This program may be freely used and distributed
 *	   but I would rather you did not sell it.
 *
 ************************************************************
 */

 /* bugs: 1. only edits the first source file with errors.		*/
 /*       2. copies the temporary back onto the original even if	*/
 /*          the original is newer					*/

#if 0
Mcc (Merge C Compiler) behaves just like a C for an error free
compile.  However, if the compiler finds syntax errors, it merges the
error messages with the source and invokes your editor (default is vi)
on the merged file.  On exiting the editor it strips the merged
messages and if the source file was modified it re-runs the compiler
for another try.

This posting contains a shell script and a C program with identical
functionality.  The C program is noticeably faster.

Made in New Zealand -->  Brent Callaghan  @ Sun Microsystems
			 uucp: sun!bcallaghan
			 phone: (415) 691 6188
#endif

#include <stdio.h>
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>

#ifdef BSD
#include <strings.h>
#define strchr  index
#define strrchr rindex
#else
#include <string.h>
#endif

extern char * getenv();
static char *errname  = "/tmp/errXXXXXX";
static char mergename[128];
static char *srcname;
static char *editor, *edname, *compiler;
static int pid, viedit, firsterr;
static int chksum1, chksum2;
static int	filevar, linevar, msgvar;
extern char *re_comp();
static char	errpat[1024], errpato[1024];
static char	*errdfa;
extern char	*braslist[], *braelist[];

/*
 * Form 16 bit checksum of source line
 */
int 
checksum(sum, line)
    register int sum;
    register char *line;
{
    while (*line++) {
	if (sum & 1)
	    sum = (sum >> 1) + 0x8000;
	else
	    sum >>= 1;

	sum = (sum + *line) & 0xFFFF;
    }
    return sum;
}

int 
runc(argv, errname)
    char **argv;
    char *errname;
{
    int status;

    switch (pid = fork()) {
    case 0:			/* child */
	(void) freopen(errname, "w", stderr);
	execvp(compiler, argv);
	perror("Couldn't exec compiler");
	exit (1);

    case -1:			/* Error */
	perror("Couldn't fork compiler");
	exit (1);

    default:			/* Parent */
	while (wait(&status) != pid);	/* wait for compile to finish */
	break;
    }
    return ((status >> 8) & 0xFF);
}

void 
listerrs(errname)
    char *errname;
{
    FILE *errfile;
    char errline[BUFSIZ + 1];

    if ((errfile = fopen(errname, "r")) == NULL)
	return;
    while (fgets(errline, BUFSIZ, errfile) != NULL)
	(void) fputs(errline, stderr);
    (void) fclose(errfile);
    (void) unlink(errname);
}

void 
edit(mergename)
    char *mergename;
{
    int status;
    char sfirsterr[6];

    switch (pid = fork()) {
    case 0:			/* Child */
	if (viedit) {
	    (void) sprintf(sfirsterr, "+%d", firsterr);
	    (void) printf(" vi %s %s\n", sfirsterr, mergename);
	    execlp(editor, "vi", sfirsterr, mergename, NULL);
	} else {
	    (void) printf(" %s %s\n", edname, mergename);
	    execlp(editor, edname, mergename, NULL);
	}
	perror("Couldn't exec editor");
	listerrs(errname);
	exit (1);

    case -1:			/* Error */
	perror("Couldn't fork editor");
	listerrs(errname);
	exit (1);

    default:			/* Parent */
	while (wait(&status) != pid);	/* wait for editor to finish */
	break;
    }
}

int 
errinfo(errfile, srcname, errmsg)
    FILE *errfile;
    char *srcname, *errmsg;
{
    static char errline[BUFSIZ + 1];
    char slineno[8];
    char *p1;

    for (;;) {
	/*	get next line from error message file	*/
	if (fgets(errline, BUFSIZ, errfile) == NULL)
	    return 0;

	errline[strlen(errline) - 1] = '\0';	/* trim newline */
	p1 = errline;

	/*	does it match the pattern?	*/
	if (re_exec(p1, errdfa)) {
	    sprintf(srcname, "%.*s", braelist[filevar] - braslist[filevar],
					    braslist[filevar]);
	    sprintf(slineno, "%.*s", braelist[linevar] - braslist[linevar],
					    braslist[linevar]);
	    sprintf(errmsg, "%.*s", braelist[msgvar] - braslist[msgvar],
					    braslist[msgvar]);
	    return atoi(slineno);
	    }
	}
}

char *merge(errname, mergename)
    char *errname, *mergename;
{
    FILE *errfile, *srcfile, *mergefile;
    int eof = 0, slineno, elineno, elines;
    static char firstname[128];
    char srcline[BUFSIZ + 1];
    char srcname[128], errmsg[80];

    if ((errfile = fopen(errname, "r")) == NULL) {
	perror(errname);
	exit (1);
    }
    if ((firsterr = errinfo(errfile, srcname, errmsg)) == 0)
	return NULL;

    if (access(srcname, 2) < 0)	/* writeable ? */
	return NULL;

    if ((srcfile = fopen(srcname, "r")) == NULL) {
	perror(srcname);
	exit (1);
    }
    if (*mergename == '\0') {
	char *p = strrchr(srcname, '/');
	if (p == NULL)
	    p = srcname;
	else
	    p++;
	(void) sprintf(mergename, "/tmp/%d.%s", getpid(), p);
    }
    if ((mergefile = fopen(mergename, "w")) == NULL) {
	perror(mergename);
	exit (1);
    }
    slineno = 0;
    elineno = firsterr;
    elines = 0;
    (void) strcpy(firstname, srcname);
    chksum1 = 0;

    if (!viedit) {
	    (void) fprintf(mergefile, ">>>><<<< (%d)\n", firsterr + 1);
	    elines++;
    }
    while (!eof) {
	if (!(eof = (fgets(srcline, BUFSIZ, srcfile) == NULL))) {
	    chksum1 = checksum(chksum1, srcline);
	    (void) fputs(srcline, mergefile);
	}
	slineno++;
	while (slineno == elineno) {
	    elines++;
	    (void) fprintf(mergefile, ">>>> %s <<<<", errmsg);
	    if ((elineno = errinfo(errfile, srcname, errmsg)) == 0
		|| strcmp(firstname, srcname) != 0)
		(void) fprintf(mergefile, " (last error)\n");
	    else
		(void) fprintf(mergefile, " (%d)\n", elineno + elines);
	}
    }
    (void) fclose(errfile);
    (void) fclose(srcfile);
    (void) fclose(mergefile);
    return (firstname);
}

/*
 * Strip out merged error messages and compute checksum
 */
void 
unmerge(mergename, srcname)
    char *mergename, *srcname;
{
    FILE *mergefile, *srcfile;
    char *p, srcline[BUFSIZ + 1];
    struct stat merge, src;

    if (stat(mergename, &merge) != 0) {
	perror(mergename);
	exit(1);
	}

    if (stat(srcname, &src) != 0) {
	perror(srcname);
	exit(1);
	}

    if (merge.st_mtime < src.st_mtime) {	/* src is newer, don't */
	return;					/* crong it from tmp   */
	}

    if ((mergefile = fopen(mergename, "r")) == NULL) {
	perror(mergename);
	exit (1);
    }
    if ((srcfile = fopen(srcname, "w")) == NULL) {
	perror(srcname);
	exit (1);
    }
    chksum2 = 0;
    while (fgets(srcline, BUFSIZ, mergefile) != NULL) {
	for (p = srcline; isspace(*p); p++);
	if (strncmp(p, ">>>>", 4) != 0) {
	    chksum2 = checksum(chksum2, srcline);
	    (void) fputs(srcline, srcfile);
	}
    }

    (void) fclose(mergefile);
    (void) fclose(srcfile);
}

void 
quit()
{
    (void) kill(pid, SIGTERM);
    (void) unlink(errname);
    (void) unlink(mergename);
    exit (1);
}

main(argc, argv)
    int argc;
    char *argv[];
{
    char	*p, *s, *d;
    int i, status, nvars;

    if ((editor = getenv("EDITOR")) == NULL)
	editor = "vi";
    edname = (edname = strrchr(editor, '/')) == NULL ? editor : edname + 1;
    viedit = strcmp(edname, "vi") == 0;

    if ((compiler = getenv("COMPILER")) == NULL)
	compiler = "cc";
    argv[0] = compiler;

    (void) mktemp(errname);

    signal(SIGINT, quit);
    signal(SIGTERM, quit);
    signal(SIGHUP, quit);

    if (p = getenv("ERRORPATTERN"))
	strcpy(errpat, p);

    nvars = 0;

    /*  preprocess pattern to turn %f and %l into \(.*\) so that regex    */
    /*  will remember the file and line numbers.                          */
    for (s = errpat, d = errpato; *s;) {
	if (*s == '%' && s[1] == 'f') {
	    *d++ = '\\'; *d++ = '('; *d++ = '.'; *d++ = '*';
	    *d++ = '\\'; *d++ = ')';
	    filevar = nvars++;
	    s += 2;
	    }
	else if (*s == '%' && s[1] == 'm') {
	    *d++ = '\\'; *d++ = '('; *d++ = '.'; *d++ = '*';
	    *d++ = '\\'; *d++ = ')';
	    msgvar = nvars++;
	    s += 2;
	    }
	else if (*s == '%' && s[1] == 'l') {
	    *d++ = '\\'; *d++ = '(';
	    *d++ = '['; *d++ = '0'; *d++ = '-'; *d++ = '9'; *d++ = ']';
	    *d++ = '*';
	    *d++ = '\\'; *d++ = ')';
	    s += 2;
	    linevar = nvars++;
	    }
	else *d++ = *s++;
	}

    errdfa = re_comp(errpato);

    while (status = runc(argv, errname)) {
	if ((srcname = merge(errname, mergename)) == NULL) {
	    listerrs(errname);
	    exit (status);	/* couldn't merge */
	}
	edit(mergename);
	(void) unlink(errname);

	signal(SIGINT,  SIG_IGN);
	signal(SIGTERM, SIG_IGN);
	signal(SIGHUP,  SIG_IGN);

	unmerge(mergename, srcname);
	(void) unlink(mergename);

	signal(SIGINT,  quit);
	signal(SIGTERM, quit);
	signal(SIGHUP,  quit);

	if (chksum1 == chksum2)	/* file unchanged ? */
	    break;

	putchar(' ');
	for (i = 0; i < argc; i++)
	    (void) printf("%s ", argv[i]);
	putchar('\n');
    }
    listerrs(errname);
    (void) unlink(errname);
    exit (status);
}

char *alloc(siz)
    {
    char *calloc();
    char *r;
    r = calloc(1, siz);
    if (!r)
        {
        fprintf(stderr, "no memory\n");
        exit(0);
        }
    return r;
    }
AlBeRtEiNsTeIn
echo unbundling regex.c 1>&2
cat >regex.c <<'AlBeRtEiNsTeIn'
/* @(#)regex.c  4.1 (Berkeley) 12/21/80 */
#

/*
 * routines to do regular expression matching
 *
 * Entry points:
 *
 *      re_comp(s)
 *              char *s;
 *       ... returns 0 if the string s was compiled successfully,
 *                   a pointer to an error message otherwise.
 *           If passed 0 or a null string returns without changing
 *           the currently compiled re (see note 11 below).
 *
 *      re_exec(s)
 *              char *s;
 *       ... returns 1 if the string s matches the last compiled regular
 *                     expression, 
 *                   0 if the string s failed to match the last compiled
 *                     regular expression, and
 *                  -1 if the compiled regular expression was invalid 
 *                     (indicating an internal error).
 *
 * The strings passed to both re_comp and re_exec may have trailing or
 * embedded newline characters; they are terminated by nulls.
 *
 * The identity of the author of these routines is lost in antiquity;
 * this is essentially the same as the re code in the original V6 ed.
 *
 * The regular expressions recognized are described below. This description
 * is essentially the same as that for ed.
 *
 *      A regular expression specifies a set of strings of characters.
 *      A member of this set of strings is said to be matched by
 *      the regular expression.  In the following specification for
 *      regular expressions the word `character' means any character but NUL.
 *
 *      1.  Any character except a special character matches itself.
 *          Special characters are the regular expression delimiter plus
 *          \ [ . and sometimes ^ * $.
 *      2.  A . matches any character.
 *      3.  A \ followed by any character except a digit or ( )
 *          matches that character.
 *      4.  A nonempty string s bracketed [s] (or [^s]) matches any
 *          character in (or not in) s. In s, \ has no special meaning,
 *          and ] may only appear as the first letter. A substring 
 *          a-b, with a and b in ascending ASCII order, stands for
 *          the inclusive range of ASCII characters.
 *      5.  A regular expression of form 1-4 followed by * matches a
 *          sequence of 0 or more matches of the regular expression.
 *      6.  A regular expression, x, of form 1-8, bracketed \(x\)
 *          matches what x matches.
 *      7.  A \ followed by a digit n matches a copy of the string that the
 *          bracketed regular expression beginning with the nth \( matched.
 *      8.  A regular expression of form 1-8, x, followed by a regular
 *          expression of form 1-7, y matches a match for x followed by
 *          a match for y, with the x match being as long as possible
 *          while still permitting a y match.
 *      9.  A regular expression of form 1-8 preceded by ^ (or followed
 *          by $), is constrained to matches that begin at the left
 *          (or end at the right) end of a line.
 *      10. A regular expression of form 1-9 picks out the longest among
 *          the leftmost matches in a line.
 *      11. An empty regular expression stands for a copy of the last
 *          regular expression encountered.
 */

/*
 * constants for re's
 */
#define CBRA    1
#define CCHR    2
#define CDOT    4
#define CCL     6
#define NCCL    8
#define CEOF    11
#define CKET    12
#define CBACK   18

#define CSTAR   01

#define ESIZE   512
#define NBRA    9

static char *names[] = { "?", "\\(", "char", "char*", ".",
                ".*", "[", "[*", "[^", "[^*",
                "?", "$", "\\)", "?", "?", "?", "?", "?", "\\" };

char    expbuf[ESIZE], *braslist[NBRA], *braelist[NBRA];
static  char    circf = 1;

/*
 * compile the regular expression argument into a dfa
 */
char *
re_comp(sp)
        register char   *sp;
{
	char *alloc();
        register int    c;
        register char   *ep = expbuf;
        int     cclcnt, numbra = 0;
        char    *lastep = 0;
        char    bracket[NBRA];
        char    *bracketp = &bracket[0];
        static  char    *retoolong = "Regular expression too long";

#define comerr(msg) {expbuf[0] = 0; numbra = 0; printf(msg); return(0); }

        if (sp == 0 || *sp == '\0') {
                if (*ep == 0)
                        comerr("No previous regular expression\n");
                return(0);
        }
        /*      if (*sp == '^') {
         *		circf = 1;
         *		sp++;
         * 		}
         *	else circf = 0;
	 */
        for (;;) {
                if (ep >= &expbuf[ESIZE])
                        comerr(retoolong);
                if ((c = *sp++) == '\0') {
                        if (bracketp != bracket)
                                comerr("unmatched \\(");
                        *ep++ = CEOF;
                        *ep++ = 0;
			sp = alloc(ep - expbuf);
			for (numbra = 0; numbra < ep - expbuf; numbra++)
			    {
			    sp[numbra] = expbuf[numbra];
			    }
			return sp;
			}
                if (c != '*')
                        lastep = ep;
                switch (c) {

                case '.':
                        *ep++ = CDOT;
                        continue;

                case '*':
                        if (lastep == 0 || *lastep == CBRA || *lastep == CKET)
                                goto defchar;
                        *lastep |= CSTAR;
                        continue;

                case '[':
                        *ep++ = CCL;
                        *ep++ = 0;
                        cclcnt = 1;
                        if ((c = *sp++) == '^') {
                                c = *sp++;
                                ep[-2] = NCCL;
                        }
                        do {
                                if (c == '\0')
                                        comerr("missing ]");
                                if (c == '-' && ep [-1] != 0) {
                                        if ((c = *sp++) == ']') {
                                                *ep++ = '-';
                                                cclcnt++;
                                                break;
                                        }
                                        while (ep[-1] < c) {
                                                *ep = ep[-1] + 1;
                                                ep++;
                                                cclcnt++;
                                                if (ep >= &expbuf[ESIZE])
                                                        comerr(retoolong);
                                        }
                                }
                                *ep++ = c;
                                cclcnt++;
                                if (ep >= &expbuf[ESIZE])
                                        comerr(retoolong);
                        } while ((c = *sp++) != ']');
                        lastep[1] = cclcnt;
                        continue;

                case '\\':
                        if ((c = *sp++) == '(') {
                                if (numbra >= NBRA)
                                        comerr("too many \\(\\) pairs");
                                *bracketp++ = numbra;
                                *ep++ = CBRA;
                                *ep++ = numbra;
				numbra++;
                                continue;
                        }
                        if (c == ')') {
                                if (bracketp <= bracket)
                                        comerr("unmatched \\)");
                                *ep++ = CKET;
                                *ep++ = *--bracketp;
                                continue;
                        }
                        if (c >= '1' && c < ('1' + NBRA)) {
                                *ep++ = CBACK;
                                *ep++ = c - '1';
                                continue;
                        }
                        *ep++ = CCHR;
                        *ep++ = c;
                        continue;

                defchar:
                default:
                        *ep++ = CCHR;
                        *ep++ = c;
                }
        }
}


/* 
 * match the argument string against the compiled re
 */
int
re_exec(subject, pattern, fromline)
        register char   *subject;
        register char   *pattern;
{
        register int    c;
        int     rv;

        for (c = 0; c < NBRA; c++) {
                braslist[c] = 0;
                braelist[c] = 0;
        }
        if (circf) {
                c = (advance(subject, pattern));
		/* printf("return %d from %d\n", c, fromline); */
		return c;
		}
        /*
         * fast check for first character
         */
        if (*pattern == CCHR) {
                c = pattern[1];
                do {
                        if (*subject != c)
                                continue;
                        if (rv = advance(subject, pattern)) {
				/* printf("return %d from %d\n", rv, fromline); */
                                return(rv);
				}
                } while (*subject++);
		/* printf("return 0 from %d\n", fromline); */
                return(0);
        }
        /*
         * regular algorithm
         */
        do
                if (rv = advance(subject, pattern)) {
			/* printf("return %d from %d\n", rv, fromline); */
                        return(rv);
			}
        while (*subject++);
	/* printf("return 0 from %d\n", fromline); */
        return(0);
}

/* 
 * try to match the next thing in the dfa
 */
static  int
advance(lp, ep)
        register char   *lp, *ep;
{
        register char   *curlp;
        int     ct, i;
        int     rv;

        for (;;)
                switch (*ep++) {

                case CCHR:
                        if (*ep++ == *lp++)
                                continue;
                        return(0);

                case CDOT:
                        if (*lp++)
                                continue;
                        return(0);

                case CEOF:
                        if (*lp == 0) return(1);
                        return(0);

                case CCL:
                        if (cclass(ep, *lp++, 1)) {
                                ep += *ep;
                                continue;
                        }
                        return(0);

                case NCCL:
                        if (cclass(ep, *lp++, 0)) {
                                ep += *ep;
                                continue;
                        }
                        return(0);

                case CBRA:
                        braslist[*ep++] = lp;
                        continue;

                case CKET:
                        braelist[*ep++] = lp;
                        continue;

                case CBACK:
                        if (braelist[i = *ep++] == 0)
                                return(-1);
                        if (backref(i, lp)) {
                                lp += braelist[i] - braslist[i];
                                continue;
                        }
                        return(0);

                case CBACK|CSTAR:
                        if (braelist[i = *ep++] == 0)
                                return(-1);
                        curlp = lp;
                        ct = braelist[i] - braslist[i];
                        while (backref(i, lp))
                                lp += ct;
                        while (lp >= curlp) {
                                if (rv = advance(lp, ep))
                                        return(rv);
                                lp -= ct;
                        }
                        continue;

                case CDOT|CSTAR:
                        curlp = lp;
                        while (*lp++)
                                ;
                        goto star;

                case CCHR|CSTAR:
                        curlp = lp;
                        while (*lp++ == *ep)
                                ;
                        ep++;
                        goto star;

                case CCL|CSTAR:
                case NCCL|CSTAR:
                        curlp = lp;
                        while (cclass(ep, *lp++, ep[-1] == (CCL|CSTAR)))
                                ;
                        ep += *ep;
                        goto star;

                star:
                        do {
                                lp--;
                                if (rv = advance(lp, ep))
                                        return(rv);
                        } while (lp > curlp);
                        return(0);

                default:
			printf("oops %d\n", *(ep - 1));
                        return(-1);
                }
}

backref(i, lp)
        register int    i;
        register char   *lp;
{
        register char   *bp;

        bp = braslist[i];
        while (*bp++ == *lp++)
                if (bp >= braelist[i])
                        return(1);
        return(0);
}

int
cclass(set, c, af)
        register char   *set, c;
        int     af;
{
        register int    n;

        if (c == 0)
                return(0);
        n = *set++;
        while (--n)
                if (*set++ == c)
                        return(af);
        return(! af);
}
AlBeRtEiNsTeIn
Newsgroups: comp.lang.icon