date and time created 83/08/05 13:34:33 by sam
[unix-history] / usr / src / old / cpp / cpp.c
index 107c17d..4934993 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)cpp.c 1.1 %G%";
+static char sccsid[] = "@(#)cpp.c      1.6 %G%";
 #endif lint
 #endif lint
+
 #ifdef FLEXNAMES
 #define        NCPS    128
 #else
 #ifdef FLEXNAMES
 #define        NCPS    128
 #else
@@ -8,6 +9,7 @@ static char sccsid[] = "@(#)cpp.c 1.1 %G%";
 #endif
 
 # include "stdio.h"
 #endif
 
 # include "stdio.h"
+# include "ctype.h"
 /* C command
 /* written by John F. Reiser
 /* July/August 1978
 /* C command
 /* written by John F. Reiser
 /* July/August 1978
@@ -32,7 +34,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
+#if pdp11 | vax | mc68000
 #define COFF 128
 #else
 #define COFF 0
 #define COFF 128
 #else
 #define COFF 0
@@ -105,9 +107,11 @@ char *ptrtab;
 #define eob(a) ((a)>=pend)
 #define bob(a) (pbeg>=(a))
 
 #define eob(a) ((a)>=pend)
 #define bob(a) (pbeg>=(a))
 
+# define cputc(a,b)    if(!flslvl) putc(a,b)
+
 char buffer[NCPS+BUFSIZ+BUFSIZ+NCPS];
 
 char buffer[NCPS+BUFSIZ+BUFSIZ+NCPS];
 
-# define SBSIZE 48000          /* std = 12000, wnj aug 1979 */
+# define SBSIZE 60000          /* std = 12000, wnj aug 1979 */
 char   sbf[SBSIZE];
 char   *savch  = sbf;
 
 char   sbf[SBSIZE];
 char   *savch  = sbf;
 
@@ -148,7 +152,7 @@ STATIC      int     fin     = STDIN;
 STATIC FILE    *fout   = stdout;
 STATIC int     nd      = 1;
 STATIC int     pflag;  /* don't put out lines "# 12 foo.c" */
 STATIC FILE    *fout   = stdout;
 STATIC int     nd      = 1;
 STATIC int     pflag;  /* don't put out lines "# 12 foo.c" */
-STATIC int     passcom;        /* don't delete comments */
+int    passcom;        /* don't delete comments */
 STATIC int rflag;      /* allow macro recursion */
 STATIC int     ifno;
 # define NPREDEF 20
 STATIC int rflag;      /* allow macro recursion */
 STATIC int     ifno;
 # define NPREDEF 20
@@ -173,7 +177,7 @@ static jmp_buf env;
 extern FILE *_f[];
 # define symsiz 500
 # else
 extern FILE *_f[];
 # define symsiz 500
 # else
-# define symsiz 1000           /* std = 500, wnj aug 1979 */
+# define symsiz 2000           /* std = 500, wnj aug 1979 */
 # endif
 STATIC struct symtab stab[symsiz];
 
 # endif
 STATIC struct symtab stab[symsiz];
 
@@ -328,6 +332,8 @@ refill(p) register char *p; {
                                        pend=np; *np='\0'; if (plvl<0) plvl=0;
                                        return(p);
                                }
                                        pend=np; *np='\0'; if (plvl<0) plvl=0;
                                        return(p);
                                }
+                               if (trulvl || flslvl)
+                                       pperror("missing endif");
                                inp=p; dump(); exit(exfail);
                        }
                        close(fin); fin=fins[--ifno]; dirs[0]=dirnams[ifno]; sayline();
                                inp=p; dump(); exit(exfail);
                        }
                        close(fin); fin=fins[--ifno]; dirs[0]=dirnams[ifno]; sayline();
@@ -383,7 +389,7 @@ again:
                                                if (!passcom) {inp=p; p=refill(p);}
                                                else if ((p-inp)>=BUFSIZ) {/* split long comment */
                                                        inp=p; p=refill(p);     /* last char written is '*' */
                                                if (!passcom) {inp=p; p=refill(p);}
                                                else if ((p-inp)>=BUFSIZ) {/* split long comment */
                                                        inp=p; p=refill(p);     /* last char written is '*' */
-                                                       putc('/',fout); /* terminate first part */
+                                                       cputc('/',fout);        /* terminate first part */
                                                        /* and fake start of 2nd */
                                                        outp=inp=p-=3; *p++='/'; *p++='*'; *p++='*';
                                                } else p=refill(p);
                                                        /* and fake start of 2nd */
                                                        outp=inp=p-=3; *p++='/'; *p++='*'; *p++='*';
                                                } else p=refill(p);
@@ -394,7 +400,7 @@ again:
                                        if (!passcom) {inp=p; p=refill(p);}
                                        else if ((p-inp)>=BUFSIZ) {/* split long comment */
                                                inp=p; p=refill(p);
                                        if (!passcom) {inp=p; p=refill(p);}
                                        else if ((p-inp)>=BUFSIZ) {/* split long comment */
                                                inp=p; p=refill(p);
-                                               putc('*',fout); putc('/',fout);
+                                               cputc('*',fout); cputc('/',fout);
                                                outp=inp=p-=2; *p++='/'; *p++='*';
                                        } else p=refill(p);
                                } else ++p; /* ignore null byte */
                                                outp=inp=p-=2; *p++='/'; *p++='*';
                                        } else p=refill(p);
                                } else ++p; /* ignore null byte */
@@ -749,7 +755,22 @@ 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);
                        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);
+                       }
                        continue;
                }
        } else if (*++inp=='\n') outp=inp;      /* allows blank line after # */
                        continue;
                }
        } else if (*++inp=='\n') outp=inp;      /* allows blank line after # */
@@ -759,6 +780,21 @@ for (;;) {
 }
 }
 
 }
 }
 
+char *
+savestring(start, finish)
+       register char *start, *finish;
+{
+       char *retbuf;
+       register char *cp;
+
+       retbuf = (char *) calloc(finish - start + 1, sizeof (char));
+       cp = retbuf;
+       while (start < finish)
+               *cp++ = *start++;
+       *cp = 0;
+       return(retbuf);
+}
+
 struct symtab *
 stsym(s) register char *s; {
        char buf[BUFSIZ]; register char *p;
 struct symtab *
 stsym(s) register char *s; {
        char buf[BUFSIZ]; register char *p;
@@ -849,8 +885,10 @@ char *
 subst(p,sp) register char *p; struct symtab *sp; {
        static char match[]="%s: argument mismatch";
        register char *ca,*vp; int params;
 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 */
+       char *actual[MAXFRM]; /* actual[n] is text of nth actual   */
+       char actused[MAXFRM]; /* for newline processing in actuals */
+       char acttxt[BUFSIZ];  /* space for actuals */
+       int  nlines = 0;
 
        if (0==(vp=sp->value)) return(p);
        if ((p-macforw)<=macdam) {
 
        if (0==(vp=sp->value)) return(p);
        if ((p-macforw)<=macdam) {
@@ -889,8 +927,10 @@ subst(p,sp) register char *p; struct symtab *sp; {
                                                pperror("%s: actuals too long",sp->name);
                                }
                                if (pa>= &actual[MAXFRM]) ppwarn(match,sp->name);
                                                pperror("%s: actuals too long",sp->name);
                                }
                                if (pa>= &actual[MAXFRM]) ppwarn(match,sp->name);
-                               else *pa++=ca;
+                               else { actused[pa-actual]=0; *pa++=ca; }
                        }
                        }
+                       nlines = lineno[ifno] - maclin;
+                       lineno[ifno] = maclin; /* don't count newlines here */
                }
                if (params!=0) ppwarn(match,sp->name);
                while (--params>=0) *pa++=""+1; /* null string for missing actuals */
                }
                if (params!=0) ppwarn(match,sp->name);
                while (--params>=0) *pa++=""+1; /* null string for missing actuals */
@@ -905,9 +945,19 @@ subst(p,sp) register char *p; struct symtab *sp; {
                        ca=actual[*--vp-1];
                        while (*--ca) {
                                if (bob(p)) {outp=inp=p; p=unfill(p);}
                        ca=actual[*--vp-1];
                        while (*--ca) {
                                if (bob(p)) {outp=inp=p; p=unfill(p);}
-                               *--p= *ca;
+                               /* Actuals with newlines confuse line numbering */
+                               if (*ca == '\n' && actused[*vp-1])
+                                       if (*(ca-1) == '\\') ca--;
+                                       else *--p = ' ';
+                               else { *--p= *ca; if (*ca == '\n') nlines--; }
                        }
                        }
-               } else break;
+                       actused[*vp-1] = 1;
+               } else {
+                       if (nlines > 0 )
+                               while (nlines-- > 0)
+                                       *--p = '\n';
+                       break;
+               }
        }
        outp=inp=p;
        return(p);
        }
        outp=inp=p;
        return(p);
@@ -1047,7 +1097,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]);
-                                       dirs[0]=dirnams[ifno]=trmdir(copy(argv[i]));
+                                       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,
 # ifndef gcos
 /* too dangerous to have file name in same syntactic position
    be input or output file depending on file redirections,
@@ -1119,6 +1169,12 @@ main(argc,argv)
 # endif
 # if mert
        varloc=stsym ("mert");
 # endif
 # if mert
        varloc=stsym ("mert");
+# endif
+# if mc68000
+       varloc=stsym("mc68000");
+# endif
+# if sun
+       varloc=stsym("sun");
 # endif
        ulnloc=stsym ("__LINE__");
        uflloc=stsym ("__FILE__");
 # endif
        ulnloc=stsym ("__LINE__");
        uflloc=stsym ("__FILE__");