forsyth20121205-174063a80cc13 years agoHistory
825 lines · 10.1 KB · C
RawDownload
#include "cc.h" #define INCREMENT 	8#define DEVNULL	"/dev/null" static int indent = 0;static int fd = -1;static int nf = 0;static int mylineno = 1; typedef struct Com{	int lno;	char *s;	Node *n;	int tba;	struct Com *nxt;} Com; Com *hdc, *curc; typedef struct File{	char *s;	char *f;	char *m;	int b;	int loc;	int in;	int tg;	Node *n;	Com *c;	struct File *nxt;} File; typedef struct Create{	char *s;	struct Create *nxt;} Create; File *fs;Create *cs; static void genmsg(void);static int isloc(void); static voidaddcreate(char *s){	Create *c; 	if(strcmp(s, DEVNULL) == 0)		return;	c = (Create*)malloc(sizeof(Create));	c->s = malloc(strlen(s)+1);	strcpy(c->s, s);	c->nxt = cs;	cs = c;} static intcreated(char *s){	Create *c; 	for(c = cs; c != nil; c = c->nxt)		if(strcmp(s, c->s) == 0)			return 1;	return 0;} intdolog(void){	if(justcode)		return 0;	return domod || !doinc || inmain;} static char*curf(void){	File *f; 	for(f = fs; f != nil; f = f->nxt)		if(f->f != nil)			return f->s;	return nil;} static char*curm(void){	File *f; 	for(f = fs; f != nil; f = f->nxt)		if(f->f != nil)			return f->m;	return nil;} voidsetmod(Sym *s){	if(domod && s->mod == nil && ism() && !(doloc && !isloc()))		s->mod = curm();} char *outmod(char *buf, int up){	char *s, *t; 	s = curf();	if(s == nil)		return "";	t = strchr(s, '.');	if(t != nil)		*t = '\0';	strcpy(buf, s);	if(t != nil)		*t = '.';	if(up == 1 || (up < 0 && ism()))		buf[0] = toupper(buf[0]);	return buf;} intism(void){	return !isb();} intisb(void){	File *f; 	for(f = fs; f != nil; f = f->nxt)		if(f->f != nil)			return f->b;	return 0;} static intisloc(void){	File *f; 	for(f = fs; f != nil; f = f->nxt)		if(f->f != nil)			return f->loc;	return 0;} static File*pushf(void){	static File zfile;	File *f; 	f = (File*)malloc(sizeof(File));	*f = zfile;	f->s = nil;	f->f = nil;	f->m = nil;	f->nxt = fs;	fs = f;	return f;} static voidpopf(void){	File *f; 	f = fs;	fs = fs->nxt;	if(f->s != nil)		free(f->s);	free(f);} static voidsetf(File *f, char *s){	int n;	char *t; 	if(s != nil){		t = strrchr(s, '/');		f->loc = t == nil;		if(t != nil)			s = t+1;		n = strlen(s);		f->s = malloc(n+1);		strcpy(f->s, s);		s = f->s;		if(n > 2 && s[n-2] == '.'){			f->m = malloc(n-1);			strncpy(f->m, s, n-2);			if(s[n-1] == 'h')				s[n-1] = 'm';			else if(s[n-1] == 'c'){				s[n-1] = 'b';				f->b = 1;			}			else				s = nil;		}		else			s = nil;		if(s == nil){			free(f->s);			if(f->m != nil)				free(f->m);			f->s = nil;			f->m = nil;		}	}	f->f = f->s;	if(f->s != nil && nf > 0){		if(doinc || doloc && !f->loc)			f->f = DEVNULL;		else if(!domod)			f->f = nil;	}	} voidoutpush0(char *s, Node *n){	File *f; 	f = pushf();	setf(f, s);	if(f->f != nil){		nf++;		f->tg = taggen;		taggen = 0;		f->n = n;		f->c = hdc;		hdc = nil;	}	} voidoutpop0(int lno){	File *f; 	USED(lno);	f = fs;	if(f->f != nil){		nf--;		taggen = f->tg;		f->n->left = (void*)hdc;		hdc = f->c;	}	popf();} voidoutpush2(char *s, Node *n){	File *f; 	f = pushf();	setf(f, s);	if(f->f != nil){		if(fd >= 0){			newsec(0);			close(fd);			close(1);			fd = -1;		}		if(created(f->f))			f->f = DEVNULL;	/* don't overwrite original if included again */		fd = create(f->f, OWRITE, 0664);		if(fd >= 0)			addcreate(f->f);		mydup(fd, 1);		nf++;		f->tg = taggen;		taggen = 0;		f->c = hdc;		if(n != Z)			hdc = (void*)n->left;		else			hdc = nil;		f->in = indent;		indent = 0;		genmsg();		pgen(f->b);	}	} voidoutpop2(int lno){	File *f, *g; 	f = fs;	if(f->f != nil){		if(fd >= 0){			newsec(0);			output(lno, 1);			epgen(f->b);			close(fd);			close(1);			fd = -1;		}		for(g = fs->nxt; g != nil; g = g->nxt){			if(g->f != nil){				fd = open(g->f, OWRITE);				seek(fd, 0, 2);				mydup(fd, 1);				break;			}		}		nf--;		taggen = f->tg;		hdc = f->c;		indent = f->in;	}	popf();} static voidxprint(char *s){	if(nerrors == 0)		print(s);} static int tot = 0; static voiddoindent(int d){	int i; 	for(i = 0; i < d/8; i++)		xprint("\t");	for(i = 0; i < d%8; i++)		xprint(" ");} voidincind(void){	indent += INCREMENT;} void decind(void){	indent -= INCREMENT;} intzeroind(void){	int i = indent; 	indent = 0;	return i;} voidrestoreind(int i){	indent = i;} voidnewline0(void){	xprint("\n");	tot = 0;	mylineno++;} voidnewline(void){	if(!outcom(1)){		xprint("\n");		mylineno++;	}	tot = 0;} static void lprint(char *s){	if(tot == 0) {		doindent(indent);		tot += indent;	}	xprint(s);	tot += strlen(s);} voidprline(char *s){	xprint(s);	xprint("\n");	mylineno++;} void prdelim(char *s){	if(*s == '%'){		if(*++s == '=')			lprint("%%=");		else			lprint("%%");		return;	}	lprint(s);} void prkeywd(char *kw){	lprint(kw);} voidprid(char *s){	lprint(s);} static voidpriddol(char *s, int dol){	char *t;	char buf[128]; 	if(dol){		t = strchr(s, '$');		if(t != nil)			*t = '_';		lprint(s);		if(t != nil){			strcpy(buf, s);			while(slookup(buf)->type != T){				strcat(buf, "x");				lprint("x");			}			*t = '$';		}	}	else		lprint(s);} voidprsym(Sym *s, int mod){	char buf[128];	int c; 	if(mod && s->mod && strcmp(s->mod, curm()) != 0 && (!s->limbo || s->class == CEXTERN)){		c = isconsym(s);		if(c >= 0){			if(c){				s->mod[0] = toupper(s->mod[0]);				lprint(s->mod);				s->mod[0] = tolower(s->mod[0]);			}			else				lprint(s->mod);			lprint("->");			usemod(s, !c);		}	}	if(s->lname)		prid(s->lname);	else{		priddol(s->name, s->class == CSTATIC);		if(s->lkw){			strcpy(buf, s->name);			for(;;){				strcat(buf, "x");				lprint("x");				s = slookup(buf);				if(s->type == T)					break;			}		}	}} intarrow(Sym *s){	if(s->mod && strcmp(s->mod, curm()) != 0)

Lines 1–500 of 825.