dynamic allocation of string space; ignore #ident (from ks@purdue)
[unix-history] / usr / src / old / cpp / cpp.c
index 4934993..64e9675 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)cpp.c      1.6 %G%";
+static char sccsid[] = "@(#)cpp.c      1.11 %G%";
 #endif lint
 
 #ifdef FLEXNAMES
 #endif lint
 
 #ifdef FLEXNAMES
@@ -23,8 +23,9 @@ static char sccsid[] = "@(#)cpp.c     1.6 %G%";
 #define READ 0
 #define WRITE 1
 #define SALT '#'
 #define READ 0
 #define WRITE 1
 #define SALT '#'
-#ifndef BUFSIZ
-#define BUFSIZ 512
+#if !defined BUFSIZ || BUFSIZ < 8192
+#undef BUFSIZ
+#define BUFSIZ 8192
 #endif
 
 char *pbeg,*pbuf,*pend;
 #endif
 
 char *pbeg,*pbuf,*pend;
@@ -111,9 +112,9 @@ char *ptrtab;
 
 char buffer[NCPS+BUFSIZ+BUFSIZ+NCPS];
 
 
 char buffer[NCPS+BUFSIZ+BUFSIZ+NCPS];
 
-# define SBSIZE 60000          /* 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
@@ -154,6 +155,12 @@ STATIC     int     nd      = 1;
 STATIC int     pflag;  /* don't put out lines "# 12 foo.c" */
 int    passcom;        /* don't delete comments */
 STATIC int rflag;      /* allow macro recursion */
 STATIC int     pflag;  /* don't put out lines "# 12 foo.c" */
 int    passcom;        /* don't delete comments */
 STATIC int rflag;      /* allow macro recursion */
+STATIC int mflag;      /* generate makefile dependencies */
+STATIC char *infile;   /* name of .o file to build dependencies from */
+STATIC         FILE *mout;     /* file to place dependencies on */
+#define START 1
+#define CONT  2
+#define BACK  3
 STATIC int     ifno;
 # define NPREDEF 20
 STATIC char *prespc[NPREDEF];
 STATIC int     ifno;
 # define NPREDEF 20
 STATIC char *prespc[NPREDEF];
@@ -194,10 +201,14 @@ 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;
 
-sayline() {
+sayline(where)
+       int where;
+{
+       if (mflag && where==START) fprintf(mout, "%s: %s\n", infile, fnames[ifno]);
        if (pflag==0) fprintf(fout,"# %d \"%s\"\n", lineno[ifno], fnames[ifno]);
 }
 
        if (pflag==0) fprintf(fout,"# %d \"%s\"\n", lineno[ifno], fnames[ifno]);
 }
 
@@ -249,7 +260,7 @@ sayline() {
 /* 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.
 */
@@ -276,7 +287,7 @@ dump() {
                        }
                        if (c==' ' && stopc==0) pblank=p1-1;
                }
                        }
                        if (c==' ' && stopc==0) pblank=p1-1;
                }
-               if (brk) sayline();
+               if (brk) sayline(CONT);
                *p2=savc; inp=p2; p1=outp; tgpscan=0;
        }
 #endif
                *p2=savc; inp=p2; p1=outp; tgpscan=0;
        }
 #endif
@@ -336,7 +347,7 @@ refill(p) register char *p; {
                                        pperror("missing endif");
                                inp=p; dump(); exit(exfail);
                        }
                                        pperror("missing endif");
                                inp=p; dump(); exit(exfail);
                        }
-                       close(fin); fin=fins[--ifno]; dirs[0]=dirnams[ifno]; sayline();
+                       close(fin); fin=fins[--ifno]; dirs[0]=dirnams[ifno]; sayline(BACK);
                }
        }
 }
                }
        }
 }
@@ -525,9 +536,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 */
@@ -573,7 +584,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 (
@@ -599,11 +610,12 @@ 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));
                dirnams[ifno]=dirs[0]=trmdir(copy(nfil));
-               sayline();
+               sayline(START);
                /* save current contents of buffer */
                while (!eob(p)) p=unfill(p);
                inctop[ifno]=mactop;
                /* save current contents of buffer */
                while (!eob(p)) p=unfill(p);
                inctop[ifno]=mactop;
@@ -621,19 +633,18 @@ 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 */
 
-       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) {
                ppwarn("illegal macro name"); while (*inp!='\n') p=skipbl(p); return(p);
        }
        np=slookup(pin,p,1);
        ++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);
        }
        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;
@@ -663,7 +674,9 @@ 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"); 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 */
@@ -699,12 +712,21 @@ 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;
+       }
+       return(p);
 }
 
 #define fasscan() ptrtab=fastab+COFF
 }
 
 #define fasscan() ptrtab=fastab+COFF
@@ -730,13 +752,13 @@ for (;;) {
                if (flslvl==0 && np->value!=0) ++trulvl;
                else ++flslvl;
        } else if (np==eifloc) {/* endif */
                if (flslvl==0 && np->value!=0) ++trulvl;
                else ++flslvl;
        } else if (np==eifloc) {/* endif */
-               if (flslvl) {if (--flslvl==0) sayline();}
+               if (flslvl) {if (--flslvl==0) sayline(CONT);}
                else if (trulvl) --trulvl;
                else pperror("If-less endif",0);
        } else if (np==elsloc) {/* else */
                if (flslvl) {
                        if (--flslvl!=0) ++flslvl;
                else if (trulvl) --trulvl;
                else pperror("If-less endif",0);
        } else if (np==elsloc) {/* else */
                if (flslvl) {
                        if (--flslvl!=0) ++flslvl;
-                       else {++trulvl; sayline();}
+                       else {++trulvl; sayline(CONT);}
                }
                else if (trulvl) {++flslvl; --trulvl;}
                else pperror("If-less else",0);
                }
                else if (trulvl) {++flslvl; --trulvl;}
                else pperror("If-less else",0);
@@ -773,6 +795,8 @@ for (;;) {
                        }
                        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 */
@@ -812,7 +836,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 */
@@ -981,8 +1009,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 *
@@ -1063,6 +1093,7 @@ main(argc,argv)
 # else
                        switch(argv[i][1]) {
 # endif
 # else
                        switch(argv[i][1]) {
 # endif
+                               case 'M': mflag++;
                                case 'P': pflag++;
                                case 'E': continue;
                                case 'R': ++rflag; continue;
                                case 'P': pflag++;
                                case 'E': continue;
                                case 'R': ++rflag; continue;
@@ -1097,6 +1128,7 @@ main(argc,argv)
                                                pperror("No source file %s",argv[i]); exit(8);
                                        }
                                        fnames[ifno]=copy(argv[i]);
                                                pperror("No source file %s",argv[i]); exit(8);
                                        }
                                        fnames[ifno]=copy(argv[i]);
+                                       infile=copy(argv[i]);
                                        dirs[0]=dirnams[ifno]=trmdir(argv[i]);
 # ifndef gcos
 /* too dangerous to have file name in same syntactic position
                                        dirs[0]=dirnams[ifno]=trmdir(argv[i]);
 # ifndef gcos
 /* too dangerous to have file name in same syntactic position
@@ -1105,15 +1137,36 @@ main(argc,argv)
        [i don't see what the problem is.  jfr]
 */
                                } else if (fout==stdout) {
        [i don't see what the problem is.  jfr]
 */
                                } else if (fout==stdout) {
-                                       extern char _sobuf[BUFSIZ];
                                        if (NULL==(fout=fopen(argv[i], "w"))) {
                                                pperror("Can't create %s", argv[i]); exit(8);
                                        if (NULL==(fout=fopen(argv[i], "w"))) {
                                                pperror("Can't create %s", argv[i]); exit(8);
-                                       } else {fclose(stdout); setbuf(fout,_sobuf);}
+                                       } else fclose(stdout);
 # endif
                                } else pperror("extraneous name %s", argv[i]);
                        }
                }
 
 # endif
                                } else pperror("extraneous name %s", argv[i]);
                        }
                }
 
+       if (mflag) {
+               if (infile==(char *)0) {
+                       fprintf(stderr,
+                               "no input file specified with -M flag\n");
+                       exit(8);
+               }
+               tf=(char *)rindex(infile, '.');
+               if (tf==0) {
+                       fprintf(stderr, "missing component name on %s\n",
+                               infile);
+                       exit(8);
+               }
+               tf[1]='o';
+               tf=(char *)rindex(infile, '/');
+               if (tf!=(char *)0)
+                       infile = tf + 1;
+               mout=fout;
+               if (NULL==(fout=fopen("/dev/null", "w"))) {
+                       pperror("Can't open /dev/null");
+                       exit(8);
+               }
+       }
        fins[ifno]=fin;
        exfail = 0;
                /* after user -I files here are the standard include libraries */
        fins[ifno]=fin;
        exfail = 0;
                /* after user -I files here are the standard include libraries */
@@ -1142,6 +1195,7 @@ 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");
        for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; ) macbit[i]=0;
 # if unix
        ysysloc=stsym("unix");
@@ -1191,7 +1245,7 @@ main(argc,argv)
        pbeg=buffer+NCPS; pbuf=pbeg+BUFSIZ; pend=pbuf+BUFSIZ;
 
        trulvl = 0; flslvl = 0;
        pbeg=buffer+NCPS; pbuf=pbeg+BUFSIZ; pend=pbuf+BUFSIZ;
 
        trulvl = 0; flslvl = 0;
-       lineno[0] = 1; sayline();
+       lineno[0] = 1; sayline(START);
        outp=inp=pend;
        control(pend);
        return (exfail);
        outp=inp=pend;
        control(pend);
        return (exfail);