ANSIfication; bug report 4.3BSD/bin/223
[unix-history] / usr / src / old / cpp / cpp.c
index 6bd7688..5564bac 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)cpp.c      1.12 %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.12 %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
@@ -18,6 +19,7 @@ static char sccsid[] = "@(#)cpp.c     1.12 %G%";
 #define STATIC
 
 #define FIRSTOPEN -2
 #define STATIC
 
 #define FIRSTOPEN -2
+#define STDIN 0
 #define READ 0
 #define WRITE 1
 #define SALT '#'
 #define READ 0
 #define WRITE 1
 #define SALT '#'
@@ -33,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
@@ -152,6 +154,7 @@ 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 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 */
@@ -342,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);
@@ -389,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++));
@@ -415,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;
                }
@@ -635,11 +643,15 @@ dodef(p) char *p; {/* process '#define' */
        char *space, *newspace;
        char *formal[MAXFRM]; /* formal[n] is name of nth formal */
        char formtxt[BUFSIZ]; /* space for formal names */
        char *space, *newspace;
        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 */
 
        ++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);
        if (oldval=np->value) free(lastcopy);   /* was previously defined */
        }
        np=slookup(pin,p,1);
        if (oldval=np->value) free(lastcopy);   /* was previously defined */
@@ -673,7 +685,11 @@ dodef(p) char *p; {/* process '#define' */
        /* warn if a redefinition is different from old value.
        */
        space=psav=malloc(BUFSIZ);
        /* warn if a redefinition is different from old value.
        */
        space=psav=malloc(BUFSIZ);
-       if (space==NULL) {pperror("too much defining"); return(p);}
+       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;
        *psav++ = '\0';
        for (;;) {/* accumulate definition until linefeed */
                outp=inp=p; p=cotoken(p); pin=inp;
@@ -724,6 +740,7 @@ dodef(p) char *p; {/* process '#define' */
                 */
                np->value += newspace-space;
        }
                 */
                np->value += newspace-space;
        }
+       passcom=opt_passcom;
        return(p);
 }
 
        return(p);
 }
 
@@ -775,22 +792,47 @@ 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;
                }
        } else if (np==identloc) {/* ident (for Sys 5r3 compat) */
                        continue;
                }
        } else if (np==identloc) {/* ident (for Sys 5r3 compat) */
@@ -927,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;
@@ -1142,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) {
@@ -1198,36 +1242,7 @@ main(argc,argv)
 # if unix
        ysysloc=stsym("unix");
 # endif
 # 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__");