ANSIfication; bug report 4.3BSD/bin/223
[unix-history] / usr / src / old / cpp / cpp.c
index cdc90e7..5564bac 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)cpp.c      1.9 %G%";
+static char sccsid[] = "@(#)cpp.c      1.17 %G%";
 #endif lint
 
 #ifdef FLEXNAMES
 #endif lint
 
 #ifdef FLEXNAMES
@@ -8,6 +8,7 @@ static char sccsid[] = "@(#)cpp.c       1.9 %G%";
 #define        NCPS    8
 #endif
 
 #define        NCPS    8
 #endif
 
+# include "sys/param.h"
 # include "stdio.h"
 # include "ctype.h"
 /* C command
 # include "stdio.h"
 # include "ctype.h"
 /* C command
@@ -17,9 +18,8 @@ static char sccsid[] = "@(#)cpp.c     1.9 %G%";
 
 #define STATIC
 
 
 #define STATIC
 
+#define FIRSTOPEN -2
 #define STDIN 0
 #define STDIN 0
-#define STDOUT 1
-#define STDERR 2
 #define READ 0
 #define WRITE 1
 #define SALT '#'
 #define READ 0
 #define WRITE 1
 #define SALT '#'
@@ -35,7 +35,7 @@ char cinit;
 
 /* some code depends on whether characters are sign or zero extended */
 /*     #if '\377' < 0          not used here, old cpp doesn't understand */
 
 /* some code depends on whether characters are sign or zero extended */
 /*     #if '\377' < 0          not used here, old cpp doesn't understand */
-#if pdp11 | vax | mc68000
+#if pdp11 | vax | mc68000 | tahoe
 #define COFF 128
 #else
 #define COFF 0
 #define COFF 128
 #else
 #define COFF 0
@@ -112,9 +112,9 @@ char *ptrtab;
 
 char buffer[NCPS+BUFSIZ+BUFSIZ+NCPS];
 
 
 char buffer[NCPS+BUFSIZ+BUFSIZ+NCPS];
 
-# define SBSIZE 80000          /* std = 12000, wnj aug 1979 */
-char   sbf[SBSIZE];
-char   *savch  = sbf;
+char   *lastcopy;
+
+char *malloc(), *realloc();
 
 # define DROP 0xFE     /* special character not legal ASCII or EBCDIC */
 # define WARN DROP
 
 # define DROP 0xFE     /* special character not legal ASCII or EBCDIC */
 # define WARN DROP
@@ -149,11 +149,12 @@ STATIC    int     lineno[MAXINC];
 STATIC char    *dirs[10];      /* -I and <> directories */
 char *strdex(), *copy(), *subst(), *trmdir();
 struct symtab *stsym();
 STATIC char    *dirs[10];      /* -I and <> directories */
 char *strdex(), *copy(), *subst(), *trmdir();
 struct symtab *stsym();
-STATIC int     fin     = STDIN;
+STATIC int     fin     = FIRSTOPEN;
 STATIC FILE    *fout   = stdout;
 STATIC int     nd      = 1;
 STATIC int     pflag;  /* don't put out lines "# 12 foo.c" */
 int    passcom;        /* don't delete comments */
 STATIC FILE    *fout   = stdout;
 STATIC int     nd      = 1;
 STATIC int     pflag;  /* don't put out lines "# 12 foo.c" */
 int    passcom;        /* don't delete comments */
+int    incomment;      /* True if parsing a comment */
 STATIC int rflag;      /* allow macro recursion */
 STATIC int mflag;      /* generate makefile dependencies */
 STATIC char *infile;   /* name of .o file to build dependencies from */
 STATIC int rflag;      /* allow macro recursion */
 STATIC int mflag;      /* generate makefile dependencies */
 STATIC char *infile;   /* name of .o file to build dependencies from */
@@ -201,6 +202,7 @@ STATIC      struct symtab *varloc;
 STATIC struct symtab *lneloc;
 STATIC struct symtab *ulnloc;
 STATIC struct symtab *uflloc;
 STATIC struct symtab *lneloc;
 STATIC struct symtab *ulnloc;
 STATIC struct symtab *uflloc;
+STATIC struct symtab *identloc;        /* Sys 5r3 compatibility */
 STATIC int     trulvl;
 STATIC int     flslvl;
 
 STATIC int     trulvl;
 STATIC int     flslvl;
 
@@ -259,7 +261,7 @@ sayline(where)
 /* buffers which are "live"; the side buffers instack[0 : inctop[ifno]]
 /* are dormant, waiting for end-of-file on the current file.
 /*
 /* buffers which are "live"; the side buffers instack[0 : inctop[ifno]]
 /* are dormant, waiting for end-of-file on the current file.
 /*
-/* space for side buffers is obtained from 'savch' and is never returned.
+/* space for side buffers is obtained from 'malloc' and is never returned.
 /* bufstack[0:fretop-1] holds addresses of side buffers which
 /* are available for use.
 */
 /* bufstack[0:fretop-1] holds addresses of side buffers which
 /* are available for use.
 */
@@ -343,7 +345,10 @@ refill(p) register char *p; {
                                        return(p);
                                }
                                if (trulvl || flslvl)
                                        return(p);
                                }
                                if (trulvl || flslvl)
-                                       pperror("missing endif");
+                                       if (incomment)
+                                               pperror("unterminated comment");
+                                       else
+                                               pperror("missing endif");
                                inp=p; dump(); exit(exfail);
                        }
                        close(fin); fin=fins[--ifno]; dirs[0]=dirnams[ifno]; sayline(BACK);
                                inp=p; dump(); exit(exfail);
                        }
                        close(fin); fin=fins[--ifno]; dirs[0]=dirnams[ifno]; sayline(BACK);
@@ -390,6 +395,7 @@ again:
        } break;
        case '/': for (;;) {
                if (*p++=='*') {/* comment */
        } break;
        case '/': for (;;) {
                if (*p++=='*') {/* comment */
+                       incomment++;
                        if (!passcom) {inp=p-2; dump(); ++flslvl;}
                        for (;;) {
                                while (!iscom(*p++));
                        if (!passcom) {inp=p-2; dump(); ++flslvl;}
                        for (;;) {
                                while (!iscom(*p++));
@@ -416,6 +422,7 @@ again:
                                } else ++p; /* ignore null byte */
                        }
                endcom:
                                } else ++p; /* ignore null byte */
                        }
                endcom:
+                       incomment--;
                        if (!passcom) {outp=inp=p; --flslvl; goto again;}
                        break;
                }
                        if (!passcom) {outp=inp=p; --flslvl; goto again;}
                        break;
                }
@@ -535,9 +542,9 @@ unfill(p) register char *p; {
        }
        if (fretop>0) np=bufstack[--fretop];
        else {
        }
        if (fretop>0) np=bufstack[--fretop];
        else {
-               np=savch; savch+=BUFSIZ;
-               if (savch>=sbf+SBSIZE) {pperror("no space"); exit(exfail);}
-               *savch++='\0';
+               np=malloc(BUFSIZ+1);
+               if (np==NULL) {pperror("no space"); exit(exfail);}
+               np[BUFSIZ]='\0';
        }
        instack[mactop]=np; op=pend-BUFSIZ; if (op<p) op=p;
        for (;;) {while (*np++= *op++); if (eob(op)) break;} /* out with old */
        }
        instack[mactop]=np; op=pend-BUFSIZ; if (op<p) op=p;
        for (;;) {while (*np++= *op++); if (eob(op)) break;} /* out with old */
@@ -583,7 +590,7 @@ doincl(p) register char *p; {
        if (ifno+1 >=MAXINC) {
                pperror("Unreasonable include nesting",0); return(p);
        }
        if (ifno+1 >=MAXINC) {
                pperror("Unreasonable include nesting",0); return(p);
        }
-       if((nfil=savch)>sbf+SBSIZE-BUFSIZ) {pperror("no space"); exit(exfail);}
+       if((nfil=malloc(BUFSIZ))==NULL) {pperror("no space"); exit(exfail);}
        filok=0;
        for (dirp=dirs+inctype; *dirp; ++dirp) {
                if (
        filok=0;
        for (dirp=dirs+inctype; *dirp; ++dirp) {
                if (
@@ -609,9 +616,10 @@ doincl(p) register char *p; {
                        filok=1; fin=fins[++ifno]; break;
                }
        }
                        filok=1; fin=fins[++ifno]; break;
                }
        }
-       if (filok==0) pperror("Can't find include file %s",filname);
+       if(filok==0){pperror("Can't find include file %s",filname);free(nfil);}
        else {
        else {
-               lineno[ifno]=1; fnames[ifno]=cp=nfil; while (*cp++); savch=cp;
+               nfil=realloc(nfil,strlen(nfil)+1);
+               lineno[ifno]=1; fnames[ifno]=nfil;
                dirnams[ifno]=dirs[0]=trmdir(copy(nfil));
                sayline(START);
                /* save current contents of buffer */
                dirnams[ifno]=dirs[0]=trmdir(copy(nfil));
                sayline(START);
                /* save current contents of buffer */
@@ -631,19 +639,22 @@ char *
 dodef(p) char *p; {/* process '#define' */
        register char *pin,*psav,*cf;
        char **pf,**qf; int b,c,params; struct symtab *np;
 dodef(p) char *p; {/* process '#define' */
        register char *pin,*psav,*cf;
        char **pf,**qf; int b,c,params; struct symtab *np;
-       char *oldval,*oldsavch;
+       char *oldval;
+       char *space, *newspace;
        char *formal[MAXFRM]; /* formal[n] is name of nth formal */
        char formtxt[BUFSIZ]; /* space for formal names */
        char *formal[MAXFRM]; /* formal[n] is name of nth formal */
        char formtxt[BUFSIZ]; /* space for formal names */
+       int opt_passcom=passcom;
+
+       passcom=0;      /* don't put comments in macro expansions */
 
 
-       if (savch>sbf+SBSIZE-BUFSIZ) {pperror("too much defining"); return(p);}
-       oldsavch=savch; /* to reclaim space if redefinition */
        ++flslvl; /* prevent macro expansion during 'define' */
        p=skipbl(p); pin=inp;
        if ((toktyp+COFF)[*pin]!=IDENT) {
        ++flslvl; /* prevent macro expansion during 'define' */
        p=skipbl(p); pin=inp;
        if ((toktyp+COFF)[*pin]!=IDENT) {
-               ppwarn("illegal macro name"); while (*inp!='\n') p=skipbl(p); return(p);
+               ppwarn("illegal macro name"); while (*inp!='\n') p=skipbl(p);
+               passcom=opt_passcom; return(p);
        }
        np=slookup(pin,p,1);
        }
        np=slookup(pin,p,1);
-       if (oldval=np->value) savch=oldsavch;   /* was previously defined */
+       if (oldval=np->value) free(lastcopy);   /* was previously defined */
        b=1; cf=pin;
        while (cf<p) {/* update macbit */
                c= *cf++; xmac1(c,b,|=); b=(b+b)&0xFF;
        b=1; cf=pin;
        while (cf<p) {/* update macbit */
                c= *cf++; xmac1(c,b,|=); b=(b+b)&0xFF;
@@ -673,7 +684,13 @@ dodef(p) char *p; {/* process '#define' */
        /* remember beginning of macro body, so that we can
        /* warn if a redefinition is different from old value.
        */
        /* remember beginning of macro body, so that we can
        /* warn if a redefinition is different from old value.
        */
-       oldsavch=psav=savch;
+       space=psav=malloc(BUFSIZ);
+       if (space==NULL) {
+               pperror("too much defining");
+               passcom=opt_passcom;
+               return(p);
+       }
+       *psav++ = '\0';
        for (;;) {/* accumulate definition until linefeed */
                outp=inp=p; p=cotoken(p); pin=inp;
                if (*pin=='\\' && pin[1]=='\n') {putc('\n',fout); continue;}    /* ignore escaped lf */
        for (;;) {/* accumulate definition until linefeed */
                outp=inp=p; p=cotoken(p); pin=inp;
                if (*pin=='\\' && pin[1]=='\n') {putc('\n',fout); continue;}    /* ignore escaped lf */
@@ -709,12 +726,22 @@ dodef(p) char *p; {/* process '#define' */
        if ((cf=oldval)!=NULL) {/* redefinition */
                --cf;   /* skip no. of params, which may be zero */
                while (*--cf);  /* go back to the beginning */
        if ((cf=oldval)!=NULL) {/* redefinition */
                --cf;   /* skip no. of params, which may be zero */
                while (*--cf);  /* go back to the beginning */
-               if (0!=strcmp(++cf,oldsavch)) {/* redefinition different from old */
+               if (0!=strcmp(++cf,space+1)) {/* redefinition different from old */
                        --lineno[ifno]; ppwarn("%s redefined",np->name); ++lineno[ifno];
                        np->value=psav-1;
                        --lineno[ifno]; ppwarn("%s redefined",np->name); ++lineno[ifno];
                        np->value=psav-1;
-               } else psav=oldsavch; /* identical redef.; reclaim space */
+               } else free(space); /* identical redef.; reclaim space */
        } else np->value=psav-1;
        } else np->value=psav-1;
-       --flslvl; inp=pin; savch=psav; return(p);
+       --flslvl; inp=pin;
+       if (np->value == psav-1) {
+               newspace = realloc(space, psav-space);
+               if (newspace==NULL) {pperror("no space"); exit(exfail);}
+               /*
+                * Adjust pointer in case this moved.
+                */
+               np->value += newspace-space;
+       }
+       passcom=opt_passcom;
+       return(p);
 }
 
 #define fasscan() ptrtab=fastab+COFF
 }
 
 #define fasscan() ptrtab=fastab+COFF
@@ -765,24 +792,51 @@ for (;;) {
 #endif
        } else if (np==lneloc) {/* line */
                if (flslvl==0 && pflag==0) {
 #endif
        } else if (np==lneloc) {/* line */
                if (flslvl==0 && pflag==0) {
-                       char *cp, *cp2, *savestring();
-                       outp=inp=p; *--outp='#'; while (*inp!='\n') p=cotoken(p);
-                       cp = outp + 1;
-                       while (isspace(*cp) && cp < inp)
-                               cp++;
-                       while (isdigit(*cp) && cp < inp)
-                               cp++;
-                       while (*cp != '"' && cp < inp)
-                               cp++;
-                       if (cp < inp) {
-                               cp++;
-                               cp2 = cp;
-                               while (*cp2 != '"' && cp2 < inp)
-                                       cp2++;
-                               fnames[ifno] = savestring(cp, cp2);
+                       char *savestring();
+                       char filename[BUFSIZ], *cp = filename;
+                       outp=inp=p; *--outp='#';
+                       /* Find the line number.. */
+                       do {
+                               p = cotoken(p);
+                       } while (!isnum(*inp) && *inp != '\n');
+                       if (isnum(*inp))
+                               lineno[ifno] = atoi(inp)-1;
+                       /* Skip over the blank token */
+                       inp = p;
+                       if (*inp != '\n') {
+                               p = cotoken(p); inp = p;
                        }
                        }
+                       /* Add a quote if missing..  */
+                       if (*inp != '\n') {
+                               p = cotoken(p);
+                               /* Add a quote if missing..  */
+                               if (*inp == '"')
+                                       inp++;
+                               else {
+                                       dump();
+                                       *--outp = '"';
+                               }
+                               while (*inp != '\n') {
+                                       while (inp < p && *inp != '"' &&
+                                               cp < filename+sizeof(filename))
+                                               *cp++ = *inp++;
+                                       if (*inp == '"')
+                                               break;
+                                       inp = p; p = cotoken(p);
+                               }
+                               fnames[ifno] = savestring(filename, cp);
+                               /* Add a quote if missing..  */
+                               if (*inp != '"') {
+                                       dump();
+                                       *--outp = '"';
+                               }
+                       }
+                       while (*inp != '\n')
+                               p = cotoken(p);
                        continue;
                }
                        continue;
                }
+       } else if (np==identloc) {/* ident (for Sys 5r3 compat) */
+               while(*inp!='\n') p=cotoken(p);
        } else if (*++inp=='\n') outp=inp;      /* allows blank line after # */
        else pperror("undefined control",0);
        /* flush to lf */
        } else if (*++inp=='\n') outp=inp;      /* allows blank line after # */
        else pperror("undefined control",0);
        /* flush to lf */
@@ -822,7 +876,11 @@ stsym(s) register char *s; {
 struct symtab *
 ppsym(s) char *s; {/* kluge */
        register struct symtab *sp;
 struct symtab *
 ppsym(s) char *s; {/* kluge */
        register struct symtab *sp;
-       cinit=SALT; *savch++=SALT; sp=stsym(s); --sp->name; cinit=0; return(sp);
+       register char *name;
+
+       cinit=SALT; sp=stsym(s); name = malloc(strlen(sp->name)+1+1);
+       name[0] = '#'; strcpy(name+1, sp->name); sp->name = name;
+       cinit=0; return(sp);
 }
 
 /* VARARGS1 */
 }
 
 /* VARARGS1 */
@@ -911,10 +969,10 @@ subst(p,sp) register char *p; struct symtab *sp; {
        dump();
        if (sp==ulnloc) {
                vp=acttxt; *vp++='\0';
        dump();
        if (sp==ulnloc) {
                vp=acttxt; *vp++='\0';
-               sprintf(vp,"%d",lineno[ifno]); while (*vp++);
+               (void)sprintf(vp,"%d",lineno[ifno]); while (*vp++);
        } else if (sp==uflloc) {
                vp=acttxt; *vp++='\0';
        } else if (sp==uflloc) {
                vp=acttxt; *vp++='\0';
-               sprintf(vp,"\"%s\"",fnames[ifno]); while (*vp++);
+               (void)sprintf(vp,"\"%s\"",fnames[ifno]); while (*vp++);
        }
        if (0!=(params= *--vp&0xFF)) {/* definition calls for params */
                register char **pa;
        }
        if (0!=(params= *--vp&0xFF)) {/* definition calls for params */
                register char **pa;
@@ -991,8 +1049,10 @@ STATIC char *
 copy(s) register char *s; {
        register char *old;
 
 copy(s) register char *s; {
        register char *old;
 
-       old = savch; while (*savch++ = *s++);
-       return(old);
+       old = malloc(strlen(s)+1);
+       if (old==NULL) {pperror("no space"); exit(exfail);}
+       strcpy(old, s);
+       return(lastcopy=old);
 }
 
 char *
 }
 
 char *
@@ -1103,7 +1163,7 @@ main(argc,argv)
                                        continue;
                                }
                        default:
                                        continue;
                                }
                        default:
-                               if (fin==STDIN) {
+                               if (fin==FIRSTOPEN) {
                                        if (0>(fin=open(argv[i], READ))) {
                                                pperror("No source file %s",argv[i]); exit(8);
                                        }
                                        if (0>(fin=open(argv[i], READ))) {
                                                pperror("No source file %s",argv[i]); exit(8);
                                        }
@@ -1124,6 +1184,8 @@ main(argc,argv)
                                } else pperror("extraneous name %s", argv[i]);
                        }
                }
                                } else pperror("extraneous name %s", argv[i]);
                        }
                }
+       if (fin == FIRSTOPEN)
+               fin = STDIN;
 
        if (mflag) {
                if (infile==(char *)0) {
 
        if (mflag) {
                if (infile==(char *)0) {
@@ -1175,40 +1237,12 @@ main(argc,argv)
        ifnloc=ppsym("ifndef");
        ifloc=ppsym("if");
        lneloc=ppsym("line");
        ifnloc=ppsym("ifndef");
        ifloc=ppsym("if");
        lneloc=ppsym("line");
+       identloc=ppsym("ident");        /* Sys 5r3 compatibility */
        for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; ) macbit[i]=0;
 # if unix
        ysysloc=stsym("unix");
 # endif
        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 mc68000
-       varloc=stsym("mc68000");
-# endif
-# if sun
-       varloc=stsym("sun");
-# endif
+       ysysloc=stsym(MACHINE);
        ulnloc=stsym ("__LINE__");
        uflloc=stsym ("__FILE__");
 
        ulnloc=stsym ("__LINE__");
        uflloc=stsym ("__FILE__");