cpp still isn't working right, but this is an interim checkin:

- compiled for GNO
- added stack diagnostics and error checking
- made large auto arrays static, where possible
- added checks for unexpected recursion
This commit is contained in:
gdr 1997-12-02 08:05:53 +00:00
parent 492cb296f2
commit e7bb29a749
14 changed files with 190 additions and 22 deletions

18
usr.bin/cpp/Makefile Normal file
View File

@ -0,0 +1,18 @@
#
# $Id: Makefile,v 1.1 1997/12/02 08:05:52 gdr Exp $
#
PROG = cpp
SRCS = cpp.c eval.c getopt.c hideset.c include.c lex.c macro.c nlist.c \
tokens.c unix.c
STACK = 8192
OPTIMIZE = 0
DEBUG = 25
CFLAGS += -D__STACK_CHECK__
LDLIBS += /src/gno/lib/libc/gno/stack.o /src/gno/lib/libc/gno/stack2.o
# Delivery directory
BINDIR = /usr/bin
.INCLUDE : /src/gno/prog.mk

View File

@ -1,4 +1,8 @@
#ifdef __ORCAC__
segment "cpp_3_____";
#endif
#include <stdio.h>
#undef fputc
#include <stdlib.h>
#include <string.h>
#include <time.h>
@ -17,15 +21,27 @@ int ifdepth;
int ifsatisfied[NIF];
int skipping;
char rcsid[] = "$Revision: 1.1 $ $Date: 1997/10/30 05:51:12 $";
char rcsid[] = "$Revision: 1.2 $ $Date: 1997/12/02 08:05:52 $";
#if defined(__GNO__) && defined(__STACK_CHECK__)
#include <err.h>
#include <gno/gno.h>
static void
printStack (void) {
warnx("stack usage: %d bytes\n", _endStackCheck());
}
#endif
int
main(int argc, char **argv)
{
Tokenrow tr;
time_t t;
char ebuf[BUFSIZ];
STATIC char ebuf[BUFSIZ];
#if defined(__GNO__) && defined(__STACK_CHECK__)
atexit(printStack);
#endif
setbuf(stderr, ebuf);
t = time(NULL);
curtime = ctime(&t);
@ -263,6 +279,11 @@ dofree(void *p)
free(p);
}
#ifdef __ORCAC__ /* required for variadic routines */
#pragma debug 0
#pragma optimize 78
#endif
void
error(enum errtype type, char *string, ...)
{

10
usr.bin/cpp/cpp.desc Normal file
View File

@ -0,0 +1,10 @@
Name: cpp
Version: 2.0 (29 Oct 97)
Shell: GNO
Author: Ported by Devin Reade.
Contact: gdr@eddore.myrias.com
Where: /usr/bin
FTP: ground.isca.uiowa.edu apple2.caltech.edu trenco.myrias.com
The C Preprocessor. This is not invoked by ORCA/C but is rather provided
as a stand-alone preprocessor.

View File

@ -1,4 +1,4 @@
/* $Id: cpp.h,v 1.1 1997/10/30 05:51:12 gdr Exp $ */
/* $Id: cpp.h,v 1.2 1997/12/02 08:05:52 gdr Exp $ */
#define INS 32768 /* input buffer */
#define OBS 4096 /* outbut buffer */
#define NARG 32 /* Max number arguments to a macro */
@ -156,9 +156,25 @@ extern Nlist *kwdefined;
extern Includelist includelist[NINCLUDE];
extern char wd[];
#ifndef __ORCAC__ /* make sure we get our own decls */
extern int creat(char *, int);
extern int open(char *, int);
extern int close(int);
extern int dup2(int, int);
extern int write(int, char *, size_t);
extern int read(int, char *, size_t);
#endif
#ifdef __ORCAC__
# define STATIC static
# ifdef NDEBUG
# define CHECKIN()
# define CHECKOUT()
# else
# include <assert.h>
# define CHECKIN() static int recursing=0; assert(!recursing); recursing++
# define CHECKOUT() --recursing
# endif
#else
# define STATIC
#endif

17
usr.bin/cpp/cpp.rez Normal file
View File

@ -0,0 +1,17 @@
/*
* $Id: cpp.rez,v 1.1 1997/12/02 08:05:52 gdr Exp $
*/
#include "Types.Rez"
resource rVersion (0x1, purgeable3, nocrossbank) {
{ 2, 0, 0, /* version */
release, /* development|alpha|beta|final|release */
0 /* non-final release number */
},
verUS,
"cpp",
"C Preprocessor\n"
"Ported by Devin Reade for GNO v2.0.6. Originally from lcc v4.0."
};

View File

@ -1,3 +1,6 @@
#ifdef __ORCAC__
segment "cpp_3_____";
#endif
#include <stdlib.h>
#include <string.h>
#include "cpp.h"

View File

@ -1,11 +1,14 @@
#ifdef __ORCAC__
segment "cpp_3_____";
#endif
#include <stdio.h>
#include <string.h>
#define EPR fprintf(stderr,
#define ERR(str, chr) if(opterr){EPR "%s%c\n", str, chr);}
int opterr = 1;
int optind = 1;
int optopt;
char *optarg;
char *strchr();
int
getopt (int argc, char *const argv[], const char *opts)

View File

@ -1,3 +1,6 @@
#ifdef __ORCAC__
segment "cpp_3_____";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -40,17 +43,22 @@ int
newhideset(int hs, Nlist *np)
{
int i, len;
Nlist *nhs[HSSIZ+3];
STATIC Nlist *nhs[HSSIZ+3];
Hideset hs1, hs2;
CHECKIN();
len = inserths(nhs, hidesets[hs], np);
for (i=0; i<nhidesets; i++) {
for (hs1=nhs, hs2=hidesets[i]; *hs1==*hs2; hs1++, hs2++)
if (*hs1 == NULL)
if (*hs1 == NULL) {
CHECKOUT();
return i;
}
}
if (len>=HSSIZ)
if (len>=HSSIZ) {
CHECKOUT();
return hs;
}
if (nhidesets >= maxhidesets) {
maxhidesets = 3*maxhidesets/2+1;
hidesets = (Hideset *)realloc(hidesets, (sizeof (Hideset *))*maxhidesets);
@ -60,6 +68,7 @@ newhideset(int hs, Nlist *np)
hs1 = (Hideset)domalloc(len*sizeof(Hideset));
memmove(hs1, nhs, len*sizeof(Hideset));
hidesets[nhidesets] = hs1;
CHECKOUT();
return nhidesets++;
}

View File

@ -1,5 +1,10 @@
#ifdef __ORCAC__
segment "cpp_3_____";
#endif
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "cpp.h"
Includelist includelist[NINCLUDE];
@ -9,10 +14,11 @@ extern char *objname;
void
doinclude(Tokenrow *trp)
{
char fname[256], iname[256];
STATIC char fname[256], iname[256];
Includelist *ip;
int angled, len, fd, i;
CHECKIN();
trp->tp += 1;
if (trp->tp>=trp->lp)
goto syntax;
@ -45,7 +51,7 @@ doinclude(Tokenrow *trp)
goto syntax;
fname[len] = '\0';
if (fname[0]=='/') {
fd = open(fname, 0);
fd = open(fname, O_RDONLY); /* gdr: last arg previously zero */
strcpy(iname, fname);
} else for (fd = -1,i=NINCLUDE-1; i>=0; i--) {
ip = &includelist[i];
@ -60,9 +66,9 @@ doinclude(Tokenrow *trp)
break;
}
if ( Mflag>1 || !angled&&Mflag==1 ) {
write(1,objname,strlen(objname));
write(1,iname,strlen(iname));
write(1,"\n",1);
write(STDOUT_FILENO,objname,strlen(objname));
write(STDOUT_FILENO,iname,strlen(iname));
write(STDOUT_FILENO,"\n",1);
}
if (fd >= 0) {
if (++incdepth > 10)
@ -73,9 +79,11 @@ doinclude(Tokenrow *trp)
trp->tp = trp->bp+2;
error(ERROR, "Could not find include file %r", trp);
}
CHECKOUT();
return;
syntax:
error(ERROR, "Syntax error in #include");
CHECKOUT();
return;
}

View File

@ -1,6 +1,10 @@
#ifdef __ORCAC__
segment "cpp_1_____";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "cpp.h"
/*

View File

@ -1,6 +1,12 @@
#ifdef __ORCAC__
segment "cpp_2_____";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __GNO__
#include <gno/gno.h> /* for _assertStack() */
#endif
#include "cpp.h"
/*
@ -180,8 +186,16 @@ expand(Tokenrow *trp, Nlist *np)
Tokenrow ntr;
int ntokc, narg, i;
Token *tp;
Tokenrow *atr[NARG+1];
int hs;
#ifdef __ORCAC__
Tokenrow **atr;
atr = domalloc(sizeof(Tokenrow *) * (NARG+1));
#define RETURN free(atr);return
#else
Tokenrow *atr[NARG+1];
#define RETURN return;
#endif
copytokenrow(&ntr, np->vp); /* copy macro value */
if (np->ap==NULL) /* parameterless */
@ -190,13 +204,13 @@ expand(Tokenrow *trp, Nlist *np)
ntokc = gatherargs(trp, atr, &narg);
if (narg<0) { /* not actually a call (no '(') */
trp->tp++;
return;
RETURN;
}
if (narg != rowlen(np->ap)) {
error(ERROR, "Disagreement in number of macro arguments");
trp->tp->hideset = newhideset(trp->tp->hideset, np);
trp->tp += ntokc;
return;
RETURN;
}
substargs(np, &ntr, atr); /* put args into replacement */
for (i=0; i<narg; i++) {
@ -218,8 +232,9 @@ expand(Tokenrow *trp, Nlist *np)
insertrow(trp, ntokc, &ntr);
trp->tp -= rowlen(&ntr);
dofree(ntr.bp);
return;
RETURN;
}
#undef RETURN
/*
* Gather an arglist, starting in trp with tp pointing at the macro name.
@ -343,6 +358,10 @@ substargs(Nlist *np, Tokenrow *rtr, Tokenrow **atr)
insertrow(rtr, 1, atr[argno]);
else {
copytokenrow(&tatr, atr[argno]);
#ifdef __ORCAC__
/* make sure we don't overrun the stack */
_assertStack(256, __LINE__, __FILE__);
#endif
expandrow(&tatr, "<macro>");
insertrow(rtr, 1, &tatr);
dofree(tatr.bp);
@ -360,14 +379,15 @@ void
doconcat(Tokenrow *trp)
{
Token *ltp, *ntp;
Tokenrow ntr;
STATIC Tokenrow ntr;
int len;
CHECKIN();
for (trp->tp=trp->bp; trp->tp<trp->lp; trp->tp++) {
if (trp->tp->type==DSHARP1)
trp->tp->type = DSHARP;
else if (trp->tp->type==DSHARP) {
char tt[128];
STATIC char tt[128];
ltp = trp->tp-1;
ntp = trp->tp+1;
if (ltp<trp->bp || ntp>=trp->lp) {
@ -392,6 +412,7 @@ doconcat(Tokenrow *trp)
trp->tp--;
}
}
CHECKOUT();
}
/*
@ -423,10 +444,11 @@ stringify(Tokenrow *vp)
static Token t = { STRING };
static Tokenrow tr = { &t, &t, &t+1, 1 };
Token *tp;
uchar s[STRLEN];
STATIC uchar s[STRLEN];
uchar *sp = s, *cp;
int i, instring;
CHECKIN();
*sp++ = '"';
for (tp = vp->bp; tp < vp->lp; tp++) {
instring = tp->type==STRING || tp->type==CCON;
@ -447,6 +469,7 @@ stringify(Tokenrow *vp)
sp = s;
t.len = strlen((char*)sp);
t.t = newstring(sp, t.len, 0);
CHECKOUT();
return &tr;
}

View File

@ -1,3 +1,6 @@
#ifdef __ORCAC__
segment "cpp_3_____";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

View File

@ -1,6 +1,11 @@
#ifdef __ORCAC__
segment "cpp_3_____";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "cpp.h"
static char wbuf[2*OBS];
@ -306,7 +311,7 @@ puttokens(Tokenrow *trp)
memcpy(wbp, p, len);
wbp += len;
if (wbp >= &wbuf[OBS]) {
write(1, wbuf, OBS);
write(STDOUT_FILENO, wbuf, OBS);
if (wbp > &wbuf[OBS])
memcpy(wbuf, wbuf+OBS, wbp - &wbuf[OBS]);
wbp -= OBS;
@ -321,7 +326,7 @@ void
flushout(void)
{
if (wbp>wbuf) {
write(1, wbuf, wbp-wbuf);
write(STDOUT_FILENO, wbuf, wbp-wbuf);
wbp = wbuf;
}
}
@ -343,9 +348,31 @@ setempty(Tokenrow *trp)
char *
outnum(char *p, int n)
{
#ifdef __appleiigs__
int i, m;
char *q = p;
/* find out how many decimal places there are */
m = n;
i = 1;
while ((m / 10) != 0) {
i++;
m /= 10;
}
p += i;
m = n;
do {
*p = m%10 + '0';
m = m/10;
--p;
--i;
} while (i > 0);
assert(q == p);
#else
if (n>=10)
p = outnum(p, n/10);
*p++ = n%10 + '0';
#endif
return p;
}

View File

@ -1,7 +1,12 @@
#ifdef __ORCAC__
segment "cpp_3_____";
#endif
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "cpp.h"
extern int getopt(int, char *const *, const char *);
@ -88,7 +93,7 @@ setup(int argc, char **argv)
setsource(fp, fd, NULL);
}
#ifndef __ORCAC__
/* memmove is defined here because some vendors don't provide it at
all and others do a terrible job (like calling malloc) */
@ -114,3 +119,4 @@ memmove(void *dp, const void *sp, size_t n)
}
return 0;
}
#endif