cpp.c

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 1,281 行 · 第 1/3 页

C
1,281
字号
yyerror(s,a) char *s, *a; {	pperror(s,a);}ppwarn(s,x) char *s, *x; {	int fail = exfail;	exfail = -1;	pperror(s,x);	exfail = fail;}struct symtab *lookup(namep, enterf)char *namep;{	register char *np, *snp;	register int c, i; int around;	register struct symtab *sp;	/* namep had better not be too long (currently, <=8 chars) */	np=namep; around=0; i=cinit;	while (c= *np++) i += i+c; c=i;	/* c=i for register usage on pdp11 */	c %= symsiz; if (c<0) c += symsiz;	sp = &stab[c];	while (snp=sp->name) {		np = namep;		while (*snp++ == *np) if (*np++ == '\0') {				if (enterf==DROP) {sp->name[0]= DROP; sp->value=0;}				return(lastsym=sp);			}		if (--sp < &stab[0])			if (around) {pperror("too many defines", 0); exit(exfail);}			else {++around; sp = &stab[symsiz-1];}	}	if (enterf==1) sp->name=namep;	return(lastsym=sp);}struct symtab *slookup(p1,p2,enterf) register char *p1,*p2; int enterf;{	register char *p3; char c2,c3; struct symtab *np;	         c2= *p2; *p2='\0';	/* mark end of token */	if ((p2-p1)>8) p3=p1+8; else p3=p2;			 c3= *p3; *p3='\0';	/* truncate to 8 chars or less */	if (enterf==1) p1=copy(p1);	np=lookup(p1,enterf); *p3=c3; *p2=c2;	if (np->value!=0 && flslvl==0) newp=subst(p2,np);	else newp=0;	return(np);}char *subst(p,sp) register char *p; struct symtab *sp; {	static char match[]="%s: argument mismatch";	register char *ca,*vp; int params;	char *actual[MAXFRM]; /* actual[n] is text of nth actual */	char acttxt[BUFSIZ]; /* space for actuals */	if (0==(vp=sp->value)) return(p);	if ((p-macforw)<=macdam) {		if (++maclvl>symsiz && !rflag) {			pperror("%s: macro recursion",sp->name); return(p);		}	} else maclvl=0;	/* level decreased */	macforw=p; macdam=0;	/* new target for decrease in level */	macnam=sp->name;	ref(macnam, lineno[ifno]);	/* CXREF */	dump();	if (sp==ulnloc) {		vp=acttxt; *vp++='\0';		sprintf(vp,"%d",lineno[ifno]); while (*vp++);	} else if (sp==uflloc) {		vp=acttxt; *vp++='\0';		sprintf(vp,"\"%s\"",fnames[ifno]); while (*vp++);	}	if (0!=(params= *--vp&0xFF)) {/* definition calls for params */		register char **pa;		ca=acttxt; pa=actual;		if (params==0xFF) params=1;	/* #define foo() ... */		sloscan(); ++flslvl; /* no expansion during search for actuals */		plvl= -1;		maclin=lineno[ifno]; macfil=fnames[ifno];		do p=skipbl(p); while (*inp=='\n');	/* skip \n too */		if (*inp=='(') {			for (plvl=1; plvl!=0; ) {				*ca++='\0';				for (;;) {					outp=inp=p; p=cotoken(p);					if (*inp=='(') ++plvl;					if (*inp==')' && --plvl==0) {--params; break;}					if (plvl==1 && *inp==',') {--params; break;}					while (inp<p) *ca++= *inp++;					if (ca> &acttxt[BUFSIZ])						pperror("%s: actuals too long",sp->name);				}				if (pa>= &actual[MAXFRM]) ppwarn(match,sp->name);				else *pa++=ca;			}		}		if (maclin!=lineno[ifno]) {/* embedded linefeeds in macro call */			int i,j=lineno[ifno];			for (i=sizeof(int)/sizeof(char); --i>=0; ) {				if (bob(p)) {outp=inp=p; p=unfill(p);}				*--p=j; j>>=8;			}			if (bob(p)) {outp=inp=p; p=unfill(p);}			*--p= warnc;		}		if (params!=0) ppwarn(match,sp->name);		while (--params>=0) *pa++=""+1;	/* null string for missing actuals */		--flslvl; fasscan();	}	for (;;) {/* push definition onto front of input stack */		while (!iswarn(*--vp)) {			if (bob(p)) {outp=inp=p; p=unfill(p);}			*--p= *vp;		}		if (*vp==warnc) {/* insert actual param */			ca=actual[*--vp-1];			while (*--ca) {				if (bob(p)) {outp=inp=p; p=unfill(p);}				*--p= *ca;			}		} else break;	}	outp=inp=p;	return(p);}char *trmdir(s) register char *s; {	register char *p = s;	while (*p++); --p; while (p>s && *--p!='/');# if unix	if (p==s) *p++='.';# endif	*p='\0';	return(s);}STATIC char *copy(s) register char *s; {	register char *old;	old = savch; while (*savch++ = *s++);	return(old);}char *strdex(s,c) char *s,c; {	while (*s) if (*s++==c) return(--s);	return(0);}yywrap(){ return(1); }main(argc,argv)	char *argv[];{	register int i,c;	register char *p;	char *tf,**cp2;# if gcos	if (setjmp(env)) return (exfail);# endif	p="_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";		i=0;		while (c= *p++) {			(fastab+COFF)[c] |= IB|NB|SB; (toktyp+COFF)[c]=IDENT;#if scw2			/* 53 == 63-10; digits rarely appear in identifiers,			/* and can never be the first char of an identifier.			/* 11 == 53*53/sizeof(macbit) .			*/			++i; (t21+COFF)[c]=(53*i)/11; (t22+COFF)[c]=i%11;#endif		}	p="0123456789.";		while (c= *p++) {(fastab+COFF)[c] |= NB|SB; (toktyp+COFF)[c]=NUMBR;}# if gcos	p="\n\"'`/\\";# else	p="\n\"'/\\";# endif		while (c= *p++) (fastab+COFF)[c] |= SB;# if gcos	p="\n\"'`\\";# else	p="\n\"'\\";# endif		while (c= *p++) (fastab+COFF)[c] |= QB;	p="*\n"; while (c= *p++) (fastab+COFF)[c] |= CB;	(fastab+COFF)[warnc] |= WB|SB;	(fastab+COFF)['\0'] |= CB|QB|SB|WB;	for (i=ALFSIZ; --i>=0; ) slotab[i]=fastab[i]|SB;	p=" \t\013\f\r";	/* note no \n;	\v not legal for vertical tab? */		while (c= *p++) (toktyp+COFF)[c]=BLANK;#if scw2	for ((t23+COFF)[i=ALFSIZ+7-COFF]=1; --i>=-COFF; )		if (((t23+COFF)[i]=(t23+COFF+1)[i]<<1)==0) (t23+COFF)[i]=1;#endif# if unix	fnames[ifno=0] = ""; dirnams[0]=dirs[0]=".";# endif# if ibm	fnames[ifno=0] = "";# endif# if gcos	if (inquire(stdin, _TTY)) freopen("*src", "rt", stdin);# endif# if gimpel || gcos	fnames[ifno=0] = (char *)inquire(stdin, _FILENAME);	dirnams[0] = dirs[0] = trmdir(copy(fnames[0]));# endif	for(i=1; i<argc; i++)		{		switch(argv[i][0])			{			case '-':# if gcos			switch(toupper(argv[i][1])) { /* case-independent on GCOS */# else			switch(argv[i][1]) {# endif				case 'P': pflag++;				case 'E': continue;				case 'R': ++rflag; continue;				case 'C': passcom++; continue;				/* this case for CXREF */				case 'F':					if ((outfp = fopen(argv[++i],"w")) == NULL) {						fprintf(stderr,"Can't open %s\n", argv[i]);						exit(1);					}					continue;				case 'D':					if (predef>prespc+NPREDEF) {						pperror("too many -D options, ignoring %s",argv[i]);						continue;					}					/* ignore plain "-D" (no argument) */					if (*(argv[i]+2)) *predef++ = argv[i]+2;					continue;				case 'U':					if (prund>punspc+NPREDEF) {						pperror("too many -U options, ignoring %s",argv[i]);						continue;					}					*prund++ = argv[i]+2;					continue;				case 'I':					if (nd>8) pperror("excessive -I file (%s) ignored",argv[i]);					else dirs[nd++] = argv[i]+2;					continue;				case '\0': continue;				default: 					pperror("unknown flag %s", argv[i]);					continue;				}			default:				if (fin==STDIN) {					if (0>(fin=open(argv[i], READ))) {						pperror("No source file %s",argv[i]); exit(8);					}					fnames[ifno]=copy(argv[i]);					fprintf(outfp, "\"%s\"\n", fnames[ifno]);	/* CXREF */					dirs[0]=dirnams[ifno]=trmdir(argv[i]);# ifndef gcos/* too dangerous to have file name in same syntactic position   be input or output file depending on file redirections,   so force output to stdout, willy-nilly	[i don't see what the problem is.  jfr]*/				} else if (fout==stdout) {					if (NULL==(fout=fopen(argv[i], "w"))) {						pperror("Can't create %s", argv[i]); exit(8);					} else fclose(stdout); # endif				} else pperror("extraneous name %s", argv[i]);			}		}	fins[ifno]=fin;	exfail = 0;		/* after user -I files here are the standard include libraries */# if unix	dirs[nd++] = "/usr/include";# endif# if gcos	dirs[nd++] = "cc/include";# endif# if ibm# ifndef gimpel	dirs[nd++] = "BTL$CLIB";# endif# endif# ifdef gimpel	dirs[nd++] = intss() ?  "SYS3.C." : "" ;# endif# ifdef compool	dirs[nd++] = "/compool";# endif	dirs[nd++] = 0;	defloc=ppsym("define");	udfloc=ppsym("undef");	incloc=ppsym("include");	elsloc=ppsym("else");	eifloc=ppsym("endif");	ifdloc=ppsym("ifdef");	ifnloc=ppsym("ifndef");	ifloc=ppsym("if");	lneloc=ppsym("line");	for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; ) macbit[i]=0;# if unix	ysysloc=stsym("unix");# endif# if gcos	ysysloc=stsym ("gcos");# endif# if ibm	ysysloc=stsym ("ibm");# endif# if pdp11	varloc=stsym("pdp11");# endif# if vax	varloc=stsym("vax");# endif# if interdata	varloc=stsym ("interdata");# endif# if tss	varloc=stsym ("tss");# endif# if os	varloc=stsym ("os");# endif# if mert	varloc=stsym ("mert");# endif# if TS	varloc=stsym ("TS");# endif# if RT	varloc=stsym ("RT");# endif# if PWB	varloc=stsym ("PWB");# endif# if RES	varloc=stsym ("RES");# endif	ulnloc=stsym ("__LINE__");	uflloc=stsym ("__FILE__");	tf=fnames[ifno]; fnames[ifno]="command line"; lineno[ifno]=1;	cp2=prespc;	while (cp2<predef) stsym(*cp2++);	cp2=punspc;	while (cp2<prund) {		if (p=strdex(*cp2, '=')) *p++='\0';		lookup(*cp2++, DROP);	}	fnames[ifno]=tf;	pbeg=buffer+8; pbuf=pbeg+BUFSIZ; pend=pbuf+BUFSIZ;	trulvl = 0; flslvl = 0;	lineno[0] = 1; sayline();	outp=inp=pend;	ready = 1;	/* CXREF */	control(pend);	return (exfail);}/* CXREF */ref( name, line )	char *name;	int line;{	fprintf(outfp, "R%.8s\t%05d\n", name, line);}def( name, line )	char *name;	int line;{	if (ready)		fprintf(outfp, "D%.8s\t%05d\n", name, line);}newf( name, line )	char *name;	int line;{	fprintf(outfp, "F%.8s\t%05d\n", name, line);}char *xcopy( ptr1, ptr2 )	register char *ptr1, *ptr2;{	static char name[10];	char *saveptr, ch;	register char *ptr3 = name;	/* locate end of name; save character there */	if ((ptr2 - ptr1) > 8) {		saveptr = ptr1 + 8;		ch = *saveptr;		*saveptr = '\0';	}	else {		ch = *ptr2;		*ptr2 = '\0';		saveptr = ptr2;	}	while (*ptr3++ = *ptr1++) ;	/* copy name */	*saveptr = ch;	/* replace character */	return( name );}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?