BSD 4_3_Net_2 release
[unix-history] / usr / src / sys / vax / inline / main.c
index 476e5eb..d5f79f7 100644 (file)
@@ -1,8 +1,45 @@
-/* Copyright (c) 1984 Regents of the University of California */
+/*-
+ * Copyright (c) 1984, 1986 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1984, 1986 The Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)main.c     1.5     (Berkeley)      %G%";
-#endif not lint
+static char sccsid[] = "@(#)main.c     7.2 (Berkeley) 5/8/91";
+#endif /* not lint */
 
 #include <stdio.h>
 #include <ctype.h>
 
 #include <stdio.h>
 #include <ctype.h>
@@ -11,10 +48,21 @@ static char sccsid[] = "@(#)main.c  1.5     (Berkeley)      %G%";
 /*
  * These are the pattern tables to be loaded
  */
 /*
  * These are the pattern tables to be loaded
  */
-struct pats *inittables[] = {
+struct pats *vax_inittables[] = {
        language_ptab,
        libc_ptab,
        language_ptab,
        libc_ptab,
+       vax_libc_ptab,
        machine_ptab,
        machine_ptab,
+       vax_ptab,
+       0
+};
+
+struct pats *vaxsubset_inittables[] = {
+       language_ptab,
+       libc_ptab,
+       vaxsubset_libc_ptab,
+       machine_ptab,
+       vaxsubset_ptab,
        0
 };
 
        0
 };
 
@@ -27,6 +75,11 @@ struct stats {
        int     lostmodified;   /* mergers inhibited by intervening mod */
        int     savedpush;      /* successful push/pop merger */
 } stats;
        int     lostmodified;   /* mergers inhibited by intervening mod */
        int     savedpush;      /* successful push/pop merger */
 } stats;
+
+extern char *strcpy();
+
+char *whoami;
+int lineno = 0;
 int dflag;
 
 main(argc, argv)
 int dflag;
 
 main(argc, argv)
@@ -40,17 +93,39 @@ main(argc, argv)
        register struct inststoptbl *itp, **ithp;
        int size;
        extern char *index();
        register struct inststoptbl *itp, **ithp;
        int size;
        extern char *index();
+       int subset = 0;
+
+       whoami = argv[0];
+       argc--;
+       argv++;
+       while (argc > 0 && argv[0][0] == '-') {
+               switch(argv[0][1]) {
+
+               case 's':
+                       subset++;
+                       break;
+
+               case 'd':
+                       dflag++;
+                       break;
 
 
-       if (argc > 1 && bcmp(argv[1], "-d", 3) == 0)
-               dflag++, argc--, argv++;
+               default:
+                       break;
+               }
+               argc--, argv++;
+       }
+       if (argc > 0)
+               freopen(argv[0], "r", stdin);
        if (argc > 1)
        if (argc > 1)
-               freopen(argv[1], "r", stdin);
-       if (argc > 2)
-               freopen(argv[2], "w", stdout);
+               freopen(argv[1], "w", stdout);
        /*
         * Set up the hash table for the patterns.
         */
        /*
         * Set up the hash table for the patterns.
         */
-       for (tablep = inittables; *tablep; tablep++) {
+       if (subset)
+               tablep = vaxsubset_inittables;
+       else
+               tablep = vax_inittables;
+       for ( ; *tablep; tablep++) {
                for (pp = *tablep; pp->name[0] != '\0'; pp++) {
                        php = &patshdr[hash(pp->name, &size)];
                        pp->size = size;
                for (pp = *tablep; pp->name[0] != '\0'; pp++) {
                        php = &patshdr[hash(pp->name, &size)];
                        pp->size = size;
@@ -73,17 +148,23 @@ main(argc, argv)
        buftail = bufhead = 0;
        bufp = line[0];
        while (fgets(bufp, MAXLINELEN, stdin)) {
        buftail = bufhead = 0;
        bufp = line[0];
        while (fgets(bufp, MAXLINELEN, stdin)) {
+               lineno++;
                lp = index(bufp, LABELCHAR);
                if (lp != NULL) {
                lp = index(bufp, LABELCHAR);
                if (lp != NULL) {
-                       bufp = newline();
-                       if (*++lp == '\n') {
+                       for (cp = bufp; cp < lp; cp++)
+                               if (!isalnum(*cp))
+                                       break;
+                       if (cp == lp) {
+                               bufp = newline();
+                               if (*++lp == '\n') {
+                                       emptyqueue();
+                                       continue;
+                               }
+                               (void) strcpy(bufp, lp);
+                               *lp++ = '\n';
+                               *lp = '\0';
                                emptyqueue();
                                emptyqueue();
-                               continue;
                        }
                        }
-                       strcpy(bufp, lp);
-                       *lp++ = '\n';
-                       *lp = '\0';
-                       emptyqueue();
                }
                for (cp = bufp; isspace(*cp); cp++)
                        /* void */;
                }
                for (cp = bufp; isspace(*cp); cp++)
                        /* void */;
@@ -93,6 +174,10 @@ main(argc, argv)
                }
                for (pp = patshdr[hash(cp, &size)]; pp; pp = pp->next) {
                        if (pp->size == size && bcmp(pp->name, cp, size) == 0) {
                }
                for (pp = patshdr[hash(cp, &size)]; pp; pp = pp->next) {
                        if (pp->size == size && bcmp(pp->name, cp, size) == 0) {
+                               if (argcounterr(pp->args, countargs(bufp), pp->name)) {
+                                       pp = NULL;
+                                       break;
+                               }
                                expand(pp->replace);
                                bufp = line[bufhead];
                                break;
                                expand(pp->replace);
                                bufp = line[bufhead];
                                break;
@@ -105,7 +190,8 @@ main(argc, argv)
        }
        emptyqueue();
        if (dflag)
        }
        emptyqueue();
        if (dflag)
-               fprintf(stderr, "inline: %s %d, %s %d, %s %d, %s %d\n",
+               fprintf(stderr, "%s: %s %d, %s %d, %s %d, %s %d\n",
+                       whoami,
                        "attempts", stats.attempted,
                        "finished", stats.finished,
                        "inhibited", stats.lostmodified,
                        "attempts", stats.attempted,
                        "finished", stats.finished,
                        "inhibited", stats.lostmodified,
@@ -121,7 +207,7 @@ expand(replace)
 {
        register int curptr;
        char *nextreplace, *argv[MAXARGS];
 {
        register int curptr;
        char *nextreplace, *argv[MAXARGS];
-       int argc, argreg, foundarg, mod = 0;
+       int argc, argreg, foundarg, mod = 0, args = 0;
        char parsebuf[BUFSIZ];
 
        stats.attempted++;
        char parsebuf[BUFSIZ];
 
        stats.attempted++;
@@ -131,6 +217,7 @@ expand(replace)
                argreg = nextarg(argc, argv);
                if (argreg == -1)
                        break;
                argreg = nextarg(argc, argv);
                if (argreg == -1)
                        break;
+               args++;
                for (foundarg = 0; curptr != buftail; ) {
                        curptr = PRED(curptr);
                        argc = parseline(line[curptr], argv, parsebuf);
                for (foundarg = 0; curptr != buftail; ) {
                        curptr = PRED(curptr);
                        argc = parseline(line[curptr], argv, parsebuf);
@@ -160,6 +247,7 @@ expand(replace)
                stats.finished++;
        emptyqueue();
        fputs(replace, stdout);
                stats.finished++;
        emptyqueue();
        fputs(replace, stdout);
+       cleanup(args);
 }
 
 /*
 }
 
 /*
@@ -233,6 +321,37 @@ copyline(from, to)
        return (from);
 }
 
        return (from);
 }
 
+/*
+ * Check for a disparity between the number of arguments a function
+ * is called with and the number which we expect to see.
+ * If the error is unrecoverable, return 1, otherwise 0.
+ */
+argcounterr(args, callargs, name)
+       int args, callargs;
+       char *name;
+{
+       register char *cp;
+       char namebuf[MAXLINELEN];
+
+       if (args == callargs)
+               return (0);
+       cp = strcpy(namebuf, name);
+       while (*cp != '\0' && *cp != '\n')
+               ++cp;
+       if (*cp == '\n')
+               *cp = '\0';
+       if (callargs >= 0) {
+               fprintf(stderr,
+               "%s: error: arg count mismatch, %d != %d for '%s' at line %d\n",
+                       whoami, callargs, args, namebuf, lineno);
+               return (1);
+       }
+       fprintf(stderr,
+               "%s: warning: can't verify arg count for '%s' at line %d\n",
+               whoami, namebuf, lineno);
+       return (0);
+}
+
 /*
  * open space for next line in the queue
  */
 /*
  * open space for next line in the queue
  */