[mod.sources] v06i044: Patches to 4.2BSD cpp for #elif, // comments

sources-request@mirror.UUCP (07/09/86)

Submitted by: seismo!gatech!emory!arnold (Arnold D. Robbins {EUCC})
Mod.sources: Volume 6, Issue 44
Archive-name: cpp.patch

[  This had been posted in net.bugs.4bsd, etc., but the Arnold and I
   both thought it important enough to be published in mod.sources;
   indeed, he had originally sent this to mod.sources, but it got
   lost in the changeover from John to me.  -r$ ]

These patches include mcgill-vision!mouse's bug fix (due to a typo).

Basically, with these patches are installed, /lib/cpp gains two new
capabilities: The #elif found in recent versions of System V and in the
draft ANSI standard, and the ability to recognize C++ // comments,
which start with the // delimiter, and go to the end of the line.

#elif is automatic: it is fast becoming a standard C feature, and I
feel that everyone would want it. // comments require that the new -B
option be given, for their recognition to be turned on. Sites with C++
should modify their CC shell script to call /lib/cpp with this option.
(I chose -B as sort of mnemonic for the BCPL programming language, from
which // was re-instituted.)

Credits: Doug Gwyn of BRL implemented #elif for his System V emulation;
however I typed it in to make the "style" consistent; all typos are
mine. I wrote the // processing code. Enjoy.

Unpack this file and feed it to patch while in a cpp source directory.

--------------------CUT HERE--------------------
#!/bin/sh
# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
# Contents:  patchkit
 
echo x - patchkit
sed 's/^XX//' > "patchkit" <<'@//E*O*F patchkit//'
XX*** ./README	Tue May 27 14:07:10 1986
XX--- ../ncpp/README	Tue May 27 15:28:08 1986
XX***************
XX*** 1,6
XX  #
XX  # @(#)README 1.2 8/30/82
XX  #
XX  August 30, 1982
XX  Fixed by Kurt Shoens, UCB
XX  If the "#line n name" occurs, then all future references

XX--- 1,12 -----
XX  #
XX  # @(#)README 1.2 8/30/82
XX  #
XX+ May 27, 1985
XX+ Modified by Arnold Robbins, Emory University Computing Center
XX+ With the -B option, will recognize C++ style start with // and go to
XX+ the end of the line comments. Also added in code from Doug Gwyn of
XX+ the Ballistics Research Laboratory to implement #elif.
XX+ 
XX  August 30, 1982
XX  Fixed by Kurt Shoens, UCB
XX  If the "#line n name" occurs, then all future references
XX*** ./cpp.c	Tue May 27 14:07:09 1986
XX--- ../ncpp/cpp.c	Tue May 27 15:16:12 1986
XX***************
XX*** 153,158
XX  STATIC	int	nd	= 1;
XX  STATIC	int	pflag;	/* don't put out lines "# 12 foo.c" */
XX  int	passcom;	/* don't delete comments */
XX  STATIC	int rflag;	/* allow macro recursion */
XX  STATIC	int	ifno;
XX  # define NPREDEF 20

XX--- 153,159 -----
XX  STATIC	int	nd	= 1;
XX  STATIC	int	pflag;	/* don't put out lines "# 12 foo.c" */
XX  int	passcom;	/* don't delete comments */
XX+ int eolcom;		/* allow // ... \n comments */
XX  STATIC	int rflag;	/* allow macro recursion */
XX  STATIC	int	ifno;
XX  # define NPREDEF 20
XX***************
XX*** 185,190
XX  STATIC	struct symtab *udfloc;
XX  STATIC	struct symtab *incloc;
XX  STATIC	struct symtab *ifloc;
XX  STATIC	struct symtab *elsloc;
XX  STATIC	struct symtab *eifloc;
XX  STATIC	struct symtab *ifdloc;

XX--- 186,192 -----
XX  STATIC	struct symtab *udfloc;
XX  STATIC	struct symtab *incloc;
XX  STATIC	struct symtab *ifloc;
XX+ STATIC	struct symtab *eliloc;		/* DAG -- added */
XX  STATIC	struct symtab *elsloc;
XX  STATIC	struct symtab *eifloc;
XX  STATIC	struct symtab *ifdloc;
XX***************
XX*** 196,201
XX  STATIC	struct symtab *uflloc;
XX  STATIC	int	trulvl;
XX  STATIC	int	flslvl;
XX  
XX  sayline() {
XX  	if (pflag==0) fprintf(fout,"# %d \"%s\"\n", lineno[ifno], fnames[ifno]);

XX--- 198,205 -----
XX  STATIC	struct symtab *uflloc;
XX  STATIC	int	trulvl;
XX  STATIC	int	flslvl;
XX+ #define MAX_IF_NESTING	64		/* DAD -- added (must be at least 6) */
XX+ STATIC	int	ifdone[MAX_IF_NESTING];	/* DAG -- added */
XX  
XX  sayline() {
XX  	if (pflag==0) fprintf(fout,"# %d \"%s\"\n", lineno[ifno], fnames[ifno]);
XX***************
XX*** 379,385
XX  		else {++p; break;}
XX  	} break;
XX  	case '/': for (;;) {
XX! 		if (*p++=='*') {/* comment */
XX  			if (!passcom) {inp=p-2; dump(); ++flslvl;}
XX  			for (;;) {
XX  				while (!iscom(*p++));

XX--- 383,390 -----
XX  		else {++p; break;}
XX  	} break;
XX  	case '/': for (;;) {
XX! 		if (*p=='/' && eolcom) {/* C++ style comment to end of line */
XX! 			p++;
XX  			if (!passcom) {inp=p-2; dump(); ++flslvl;}
XX  			for (;;) {
XX  				while (*p && *p++ != '\n');
XX***************
XX*** 382,387
XX  		if (*p++=='*') {/* comment */
XX  			if (!passcom) {inp=p-2; dump(); ++flslvl;}
XX  			for (;;) {
XX  				while (!iscom(*p++));
XX  				if (p[-1]=='*') for (;;) {
XX  					if (*p++=='/') goto endcom;

XX--- 387,411 -----
XX  			p++;
XX  			if (!passcom) {inp=p-2; dump(); ++flslvl;}
XX  			for (;;) {
XX+ 				while (*p && *p++ != '\n');
XX+ 				if (p[-1]=='\n') {
XX+ 					p--;
XX+ 					goto endcpluscom;
XX+ 				} else if (eob(--p)) {
XX+ 					if (!passcom) {inp=p; p=refill(p);}
XX+ 					else if ((p-inp)>=BUFSIZ) {/* split long comment */
XX+ 						inp=p; p=refill(p);
XX+ 					} else p=refill(p);
XX+ 				} else ++p; /* ignore null byte */
XX+ 			}
XX+ 		endcpluscom:
XX+ 			if (!passcom) {outp=inp=p; --flslvl;}
XX+ 			goto newline;
XX+ 			break;
XX+ 		}
XX+ 		else if (*p++=='*') {/* comment */
XX+ 			if (!passcom) {inp=p-2; dump(); ++flslvl;}
XX+ 			for (;;) {
XX  				while (!iscom(*p++));
XX  				if (p[-1]=='*') for (;;) {
XX  					if (*p++=='/') goto endcom;
XX***************
XX*** 430,435
XX  		}
XX  	} break;
XX  	case '\n': {
XX  		++lineno[ifno]; if (isslo) {state=LF; return(p);}
XX  prevlf:
XX  		state=BEG;

XX--- 454,460 -----
XX  		}
XX  	} break;
XX  	case '\n': {
XX+ newline:
XX  		++lineno[ifno]; if (isslo) {state=LF; return(p);}
XX  prevlf:
XX  		state=BEG;
XX***************
XX*** 710,716
XX  #define fasscan() ptrtab=fastab+COFF
XX  #define sloscan() ptrtab=slotab+COFF
XX  
XX! char *
XX  control(p) register char *p; {/* find and handle preprocessor control lines */
XX  	register struct symtab *np;
XX  for (;;) {

XX--- 735,741 -----
XX  #define fasscan() ptrtab=fastab+COFF
XX  #define sloscan() ptrtab=slotab+COFF
XX  
XX! void		/* DAG -- bug fix (was (char *)) */
XX  control(p) register char *p; {/* find and handle preprocessor control lines */
XX  	register struct symtab *np;
XX  for (;;) {
XX***************
XX*** 723,729
XX  		if (flslvl==0) {p=doincl(p); continue;}
XX  	} else if (np==ifnloc) {/* ifndef */
XX  		++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
XX! 		if (flslvl==0 && np->value==0) ++trulvl;
XX  		else ++flslvl;
XX  	} else if (np==ifdloc) {/* ifdef */
XX  		++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;

XX--- 748,758 -----
XX  		if (flslvl==0) {p=doincl(p); continue;}
XX  	} else if (np==ifnloc) {/* ifndef */
XX  		++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
XX! 		if (flslvl==0)
XX! 			if (ifdone[trulvl] = np->value == 0)
XX! 				++trulvl;
XX! 			else
XX! 				++flslvl;
XX  		else ++flslvl;
XX  	} else if (np==ifdloc) {/* ifdef */
XX  		++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
XX***************
XX*** 727,733
XX  		else ++flslvl;
XX  	} else if (np==ifdloc) {/* ifdef */
XX  		++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
XX! 		if (flslvl==0 && np->value!=0) ++trulvl;
XX  		else ++flslvl;
XX  	} else if (np==eifloc) {/* endif */
XX  		if (flslvl) {if (--flslvl==0) sayline();}

XX--- 756,766 -----
XX  		else ++flslvl;
XX  	} else if (np==ifdloc) {/* ifdef */
XX  		++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
XX! 		if (flslvl==0)
XX! 			if (ifdone[trulvl] = np->value!=0)
XX! 				++trulvl;
XX! 			else
XX! 				++flslvl;
XX  		else ++flslvl;
XX  	} else if (np==eifloc) {/* endif */
XX  		if (flslvl) {if (--flslvl==0) sayline();}
XX***************
XX*** 731,737
XX  		else ++flslvl;
XX  	} else if (np==eifloc) {/* endif */
XX  		if (flslvl) {if (--flslvl==0) sayline();}
XX! 		else if (trulvl) --trulvl;
XX  		else pperror("If-less endif",0);
XX  	} else if (np==elsloc) {/* else */
XX  		if (flslvl) {

XX--- 764,770 -----
XX  		else ++flslvl;
XX  	} else if (np==eifloc) {/* endif */
XX  		if (flslvl) {if (--flslvl==0) sayline();}
XX! 		else if (trulvl) ifdone[--trulvl] = 0;	/* DAG */
XX  		else pperror("If-less endif",0);
XX  	} else if (np==elsloc) {/* else */
XX  		if (flslvl) {
XX***************
XX*** 735,741
XX  		else pperror("If-less endif",0);
XX  	} else if (np==elsloc) {/* else */
XX  		if (flslvl) {
XX! 			if (--flslvl!=0) ++flslvl;
XX  			else {++trulvl; sayline();}
XX  		}
XX  		else if (trulvl) {++flslvl; --trulvl;}

XX--- 768,774 -----
XX  		else pperror("If-less endif",0);
XX  	} else if (np==elsloc) {/* else */
XX  		if (flslvl) {
XX! 			if (--flslvl!=0 || ifdone[trulvl]) ++flslvl;
XX  			else {++trulvl; sayline();}
XX  		}
XX  		else if (trulvl) {++flslvl; --trulvl;}
XX***************
XX*** 747,753
XX  	} else if (np==ifloc) {/* if */
XX  #if tgp
XX  		pperror(" IF not implemented, true assumed", 0);
XX! 		if (flslvl==0) ++trulvl; else ++flslvl;
XX  #else
XX  		newp=p;
XX  		if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;

XX--- 780,786 -----
XX  	} else if (np==ifloc) {/* if */
XX  #if tgp
XX  		pperror(" IF not implemented, true assumed", 0);
XX! 		if (flslvl==0) ifdone[trulvl++] = 1; else ++flslvl;
XX  #else
XX  		newp=p;
XX  		if (flslvl==0)
XX***************
XX*** 750,756
XX  		if (flslvl==0) ++trulvl; else ++flslvl;
XX  #else
XX  		newp=p;
XX! 		if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
XX  		p=newp;
XX  #endif
XX  	} else if (np==lneloc) {/* line */

XX--- 783,796 -----
XX  		if (flslvl==0) ifdone[trulvl++] = 1; else ++flslvl;
XX  #else
XX  		newp=p;
XX! 		if (flslvl==0)
XX! 		{
XX! 			if (ifdone[trulvl] = yyparse())	/* DAG */
XX! 				++trulvl;
XX! 			else
XX! 				++flslvl;
XX! 		}
XX! 		else ++flslvl;
XX  		p=newp;
XX  #endif
XX  	} else if (np==eliloc) {/* elif */	/* DAG -- added */
XX***************
XX*** 753,758
XX  		if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
XX  		p=newp;
XX  #endif
XX  	} else if (np==lneloc) {/* line */
XX  		if (flslvl==0 && pflag==0) {
XX  			char *cp, *cp2, *savestring();

XX--- 793,839 -----
XX  		else ++flslvl;
XX  		p=newp;
XX  #endif
XX+ 	} else if (np==eliloc) {/* elif */	/* DAG -- added */
XX+ #if tgp
XX+ 		pperror ( " ELIF not implemented, true assumed", (char *) 0, (char *) 0);
XX+ 		if (flslvl)
XX+ 		{
XX+ 			if (--flslvl == 0 && !ifdeone[trulvl])
XX+ 			{
XX+ 				ifdone[trulvl++] = 1;
XX+ 				sayline():
XX+ 			}
XX+ 			else
XX+ 				++flslvl;
XX+ 		}
XX+ 		else if (trulvl)
XX+ 		{
XX+ 			++flslvl;
XX+ 			--trulvl;
XX+ 		}
XX+ 		else
XX+ 			pperror ( "If-less elif", (char *) 0, (char *) 0);
XX+ #else
XX+ 		newp = p;
XX+ 		if (flslvl)
XX+ 		{
XX+ 			if (--flslvl == 0 && !ifdone[trulvl] && yyparse())
XX+ 			{
XX+ 				ifdone[trulvl++] = 1;
XX+ 				sayline();
XX+ 			}
XX+ 			else
XX+ 				++flslvl;
XX+ 		}
XX+ 		else if (trulvl)
XX+ 		{
XX+ 			++flslvl;
XX+ 			--trulvl;
XX+ 		}
XX+ 		else
XX+ 			pperror ("If-less elif", (char *) 0, (char *) 0);
XX+ 		p = newp;
XX+ #endif
XX  	} else if (np==lneloc) {/* line */
XX  		if (flslvl==0 && pflag==0) {
XX  			char *cp, *cp2, *savestring();
XX***************
XX*** 1067,1072
XX  				case 'E': continue;
XX  				case 'R': ++rflag; continue;
XX  				case 'C': passcom++; continue;
XX  				case 'D':
XX  					if (predef>prespc+NPREDEF) {
XX  						pperror("too many -D options, ignoring %s",argv[i]);

XX--- 1148,1154 -----
XX  				case 'E': continue;
XX  				case 'R': ++rflag; continue;
XX  				case 'C': passcom++; continue;
XX+ 				case 'B': eolcom++; continue;
XX  				case 'D':
XX  					if (predef>prespc+NPREDEF) {
XX  						pperror("too many -D options, ignoring %s",argv[i]);
XX***************
XX*** 1141,1146
XX  	ifdloc=ppsym("ifdef");
XX  	ifnloc=ppsym("ifndef");
XX  	ifloc=ppsym("if");
XX  	lneloc=ppsym("line");
XX  	for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; ) macbit[i]=0;
XX  # if unix

XX--- 1223,1229 -----
XX  	ifdloc=ppsym("ifdef");
XX  	ifnloc=ppsym("ifndef");
XX  	ifloc=ppsym("if");
XX+ 	eliloc=ppsym("elif");
XX  	lneloc=ppsym("line");
XX  	for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; ) macbit[i]=0;
XX  # if unix

@//E*O*F patchkit//
chmod u=rw,g=rw,o=rw patchkit
 
echo Inspecting for damage in transit...
temp=/tmp/sharin$$; dtemp=/tmp/sharout$$
trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
cat > $temp <<\!!!
     382    1399    9889 patchkit
!!!
wc  patchkit | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
if test -s $dtemp
then echo "Ouch [diff of wc output]:" ; cat $dtemp
else echo "No problems found."
fi
exit 0