BSD 4_4 release
[unix-history] / usr / src / usr.bin / rdist / gram.y
index 323a3ec..7f40f87 100644 (file)
@@ -1,12 +1,49 @@
 %{
 %{
+/*
+ * Copyright (c) 1983, 1993
+ *     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
 #ifndef lint
-static char *sccsid = "@(#)gram.y      4.7 (Berkeley) 83/11/29";
-#endif
+static char sccsid[] = "@(#)gram.y     8.1 (Berkeley) 6/9/93";
+#endif /* not lint */
 
 #include "defs.h"
 
 
 #include "defs.h"
 
-struct block *lastn;
-struct block *lastc;
+struct cmd *cmds = NULL;
+struct cmd *last_cmd;
+struct namelist *last_n;
+struct subcmd *last_sc;
+
+static char  *makestr __P((char *));
 
 %}
 
 
 %}
 
@@ -15,25 +52,28 @@ struct      block *lastc;
 %term RP       3
 %term SM       4
 %term ARROW    5
 %term RP       3
 %term SM       4
 %term ARROW    5
-%term DCOLON   6
-%term NAME     7
-%term STRING   8
-%term INSTALL  9
-%term NOTIFY   10
-%term EXCEPT   11
-%term SPECIAL  12
-%term OPTION   13
+%term COLON    6
+%term DCOLON   7
+%term NAME     8
+%term STRING   9
+%term INSTALL  10
+%term NOTIFY   11
+%term EXCEPT   12
+%term PATTERN  13
+%term SPECIAL  14
+%term OPTION   15
 
 %union {
 
 %union {
-       struct block *blk;
        int intval;
        char *string;
        int intval;
        char *string;
+       struct subcmd *subcmd;
+       struct namelist *namel;
 }
 
 }
 
-%type <blk> NAME, INSTALL, NOTIFY, EXCEPT, SPECIAL
-%type <blk> namelist, names, opt_name, opt_namelist, cmdlist, cmd
 %type <intval> OPTION, options
 %type <intval> OPTION, options
-%type <string> STRING
+%type <string> NAME, STRING
+%type <subcmd> INSTALL, NOTIFY, EXCEPT, PATTERN, SPECIAL, cmdlist, cmd
+%type <namel> namelist, names, opt_namelist
 
 %%
 
 
 %%
 
@@ -42,20 +82,25 @@ file:                 /* VOID */
                ;
 
 command:         NAME EQUAL namelist = {
                ;
 
 command:         NAME EQUAL namelist = {
-                       $1->b_args = $3;
-                       (void) lookup($1->b_name, $1, 1);
+                       (void) lookup($1, INSERT, $3);
                }
                | namelist ARROW namelist cmdlist = {
                }
                | namelist ARROW namelist cmdlist = {
-                       dohcmds($1, $3, $4);
+                       insert(NULL, $1, $3, $4);
+               }
+               | NAME COLON namelist ARROW namelist cmdlist = {
+                       insert($1, $3, $5, $6);
                }
                | namelist DCOLON NAME cmdlist = {
                }
                | namelist DCOLON NAME cmdlist = {
-                       dofcmds($1, $3, $4);
+                       append(NULL, $1, $3, $4);
+               }
+               | NAME COLON namelist DCOLON NAME cmdlist = {
+                       append($1, $3, $5, $6);
                }
                | error
                ;
 
 namelist:        NAME = {
                }
                | error
                ;
 
 namelist:        NAME = {
-                       $$ = $1;
+                       $$ = makenl($1);
                }
                | LP names RP = {
                        $$ = $2;
                }
                | LP names RP = {
                        $$ = $2;
@@ -63,57 +108,73 @@ namelist:    NAME = {
                ;
 
 names:           /* VOID */ {
                ;
 
 names:           /* VOID */ {
-                       $$ = lastn = NULL;
+                       $$ = last_n = NULL;
                }
                | names NAME = {
                }
                | names NAME = {
-                       if (lastn == NULL)
-                               $$ = lastn = $2;
+                       if (last_n == NULL)
+                               $$ = last_n = makenl($2);
                        else {
                        else {
-                               lastn->b_next = $2;
-                               lastn = $2;
+                               last_n->n_next = makenl($2);
+                               last_n = last_n->n_next;
                                $$ = $1;
                        }
                }
                ;
 
 cmdlist:         /* VOID */ {
                                $$ = $1;
                        }
                }
                ;
 
 cmdlist:         /* VOID */ {
-                       $$ = lastc = NULL;
+                       $$ = last_sc = NULL;
                }
                | cmdlist cmd = {
                }
                | cmdlist cmd = {
-                       if (lastc == NULL)
-                               $$ = lastc = $2;
+                       if (last_sc == NULL)
+                               $$ = last_sc = $2;
                        else {
                        else {
-                               lastc->b_next = $2;
-                               lastc = $2;
+                               last_sc->sc_next = $2;
+                               last_sc = $2;
                                $$ = $1;
                        }
                }
                ;
 
                                $$ = $1;
                        }
                }
                ;
 
-cmd:             INSTALL options opt_name SM = {
-                       register struct block *b;
+cmd:             INSTALL options opt_namelist SM = {
+                       register struct namelist *nl;
 
 
-                       $1->b_options = $2 | options;
+                       $1->sc_options = $2 | options;
                        if ($3 != NULL) {
                        if ($3 != NULL) {
-                               b = expand($3, E_VARS|E_SHELL);
-                               if (b->b_next != NULL)
-                                       yyerror("only one name allowed\n");
-                               $1->b_name = b->b_name;
+                               nl = expand($3, E_VARS);
+                               if (nl) {
+                                       if (nl->n_next != NULL)
+                                           yyerror("only one name allowed\n");
+                                       $1->sc_name = nl->n_name;
+                                       free(nl);
+                               } else
+                                       $1->sc_name = NULL;
                        }
                        $$ = $1;
                }
                | NOTIFY namelist SM = {
                        }
                        $$ = $1;
                }
                | NOTIFY namelist SM = {
-                       $1->b_args = expand($2, E_VARS);
+                       if ($2 != NULL)
+                               $1->sc_args = expand($2, E_VARS);
                        $$ = $1;
                }
                | EXCEPT namelist SM = {
                        $$ = $1;
                }
                | EXCEPT namelist SM = {
-                       $1->b_args = $2;
+                       if ($2 != NULL)
+                               $1->sc_args = expand($2, E_ALL);
+                       $$ = $1;
+               }
+               | PATTERN namelist SM = {
+                       struct namelist *nl;
+                       char *cp, *re_comp();
+
+                       for (nl = $2; nl != NULL; nl = nl->n_next)
+                               if ((cp = re_comp(nl->n_name)) != NULL)
+                                       yyerror(cp);
+                       $1->sc_args = expand($2, E_VARS);
                        $$ = $1;
                }
                | SPECIAL opt_namelist STRING SM = {
                        if ($2 != NULL)
                        $$ = $1;
                }
                | SPECIAL opt_namelist STRING SM = {
                        if ($2 != NULL)
-                               $1->b_args = expand($2, E_ALL);
-                       $1->b_name = $3;
+                               $1->sc_args = expand($2, E_ALL);
+                       $1->sc_name = $3;
                        $$ = $1;
                }
                ;
                        $$ = $1;
                }
                ;
@@ -126,14 +187,6 @@ options:     /* VOID */ = {
                }
                ;
 
                }
                ;
 
-opt_name:        /* VOID */ = {
-                       $$ = NULL;
-               }
-               | NAME = {
-                       $$ = $1;
-               }
-               ;
-
 opt_namelist:    /* VOID */ = {
                        $$ = NULL;
                }
 opt_namelist:    /* VOID */ = {
                        $$ = NULL;
                }
@@ -147,6 +200,7 @@ opt_namelist:         /* VOID */ = {
 int    yylineno = 1;
 extern FILE *fin;
 
 int    yylineno = 1;
 extern FILE *fin;
 
+int
 yylex()
 {
        static char yytext[INMAX];
 yylex()
 {
        static char yytext[INMAX];
@@ -206,26 +260,23 @@ again:
                                        break;
                                }
                        }
                                        break;
                                }
                        }
-                       if (c == '\n')
+                       if (c == '\n') {
+                               yylineno++;
                                c = ' '; /* can't send '\n' */
                                c = ' '; /* can't send '\n' */
+                       }
                        *cp1++ = c;
                }
                if (c != '"')
                        yyerror("missing closing '\"'\n");
                        *cp1++ = c;
                }
                if (c != '"')
                        yyerror("missing closing '\"'\n");
-               *cp1++ = '\0';
-               yylval.string = cp2 = malloc(cp1 - yytext);
-               if (cp2 == NULL)
-                       fatal("ran out of memory\n");
-               cp1 = yytext;
-               while (*cp2++ = *cp1++)
-                       ;
+               *cp1 = '\0';
+               yylval.string = makestr(yytext);
                return(STRING);
 
                return(STRING);
 
-       case ':':  /* :: */
+       case ':':  /* : or :: */
                if ((c = getc(fin)) == ':')
                        return(DCOLON);
                ungetc(c, fin);
                if ((c = getc(fin)) == ':')
                        return(DCOLON);
                ungetc(c, fin);
-               c = ':';
+               return(COLON);
        }
        cp1 = yytext;
        cp2 = &yytext[INMAX - 1];
        }
        cp1 = yytext;
        cp2 = &yytext[INMAX - 1];
@@ -245,7 +296,7 @@ again:
                }
                *cp1++ = c;
                c = getc(fin);
                }
                *cp1++ = c;
                c = getc(fin);
-               if (c == EOF || any(c, " \t()=;\n")) {
+               if (c == EOF || any(c, " \"'\t()=;:\n")) {
                        ungetc(c, fin);
                        break;
                }
                        ungetc(c, fin);
                        break;
                }
@@ -272,6 +323,14 @@ again:
                case 'y':
                        yylval.intval = YOUNGER;
                        return(OPTION);
                case 'y':
                        yylval.intval = YOUNGER;
                        return(OPTION);
+
+               case 'h':
+                       yylval.intval = FOLLOW;
+                       return(OPTION);
+
+               case 'i':
+                       yylval.intval = IGNLNKS;
+                       return(OPTION);
                }
        }
        if (!strcmp(yytext, "install"))
                }
        }
        if (!strcmp(yytext, "install"))
@@ -280,14 +339,19 @@ again:
                c = NOTIFY;
        else if (!strcmp(yytext, "except"))
                c = EXCEPT;
                c = NOTIFY;
        else if (!strcmp(yytext, "except"))
                c = EXCEPT;
+       else if (!strcmp(yytext, "except_pat"))
+               c = PATTERN;
        else if (!strcmp(yytext, "special"))
                c = SPECIAL;
        else if (!strcmp(yytext, "special"))
                c = SPECIAL;
-       else
-               c = NAME;
-       yylval.blk = makeblock(c, yytext);
+       else {
+               yylval.string = makestr(yytext);
+               return(NAME);
+       }
+       yylval.subcmd = makesubcmd(c);
        return(c);
 }
 
        return(c);
 }
 
+int
 any(c, str)
        register int c;
        register char *str;
 any(c, str)
        register int c;
        register char *str;
@@ -298,15 +362,147 @@ any(c, str)
        return(0);
 }
 
        return(0);
 }
 
+/*
+ * Insert or append ARROW command to list of hosts to be updated.
+ */
+void
+insert(label, files, hosts, subcmds)
+       char *label;
+       struct namelist *files, *hosts;
+       struct subcmd *subcmds;
+{
+       register struct cmd *c, *prev, *nc;
+       register struct namelist *h;
+
+       files = expand(files, E_VARS|E_SHELL);
+       hosts = expand(hosts, E_ALL);
+       for (h = hosts; h != NULL; free(h), h = h->n_next) {
+               /*
+                * Search command list for an update to the same host.
+                */
+               for (prev = NULL, c = cmds; c!=NULL; prev = c, c = c->c_next) {
+                       if (strcmp(c->c_name, h->n_name) == 0) {
+                               do {
+                                       prev = c;
+                                       c = c->c_next;
+                               } while (c != NULL &&
+                                       strcmp(c->c_name, h->n_name) == 0);
+                               break;
+                       }
+               }
+               /*
+                * Insert new command to update host.
+                */
+               nc = ALLOC(cmd);
+               if (nc == NULL)
+                       fatal("ran out of memory\n");
+               nc->c_type = ARROW;
+               nc->c_name = h->n_name;
+               nc->c_label = label;
+               nc->c_files = files;
+               nc->c_cmds = subcmds;
+               nc->c_next = c;
+               if (prev == NULL)
+                       cmds = nc;
+               else
+                       prev->c_next = nc;
+               /* update last_cmd if appending nc to cmds */
+               if (c == NULL)
+                       last_cmd = nc;
+       }
+}
+
+/*
+ * Append DCOLON command to the end of the command list since these are always
+ * executed in the order they appear in the distfile.
+ */
+void
+append(label, files, stamp, subcmds)
+       char *label;
+       struct namelist *files;
+       char *stamp;
+       struct subcmd *subcmds;
+{
+       register struct cmd *c;
+
+       c = ALLOC(cmd);
+       if (c == NULL)
+               fatal("ran out of memory\n");
+       c->c_type = DCOLON;
+       c->c_name = stamp;
+       c->c_label = label;
+       c->c_files = expand(files, E_ALL);
+       c->c_cmds = subcmds;
+       c->c_next = NULL;
+       if (cmds == NULL)
+               cmds = last_cmd = c;
+       else {
+               last_cmd->c_next = c;
+               last_cmd = c;
+       }
+}
+
 /*
  * Error printing routine in parser.
  */
 /*
  * Error printing routine in parser.
  */
+void
 yyerror(s)
        char *s;
 {
 yyerror(s)
        char *s;
 {
-       extern int yychar;
-
-       errs++;
+       ++nerrs;
        fflush(stdout);
        fprintf(stderr, "rdist: line %d: %s\n", yylineno, s);
 }
        fflush(stdout);
        fprintf(stderr, "rdist: line %d: %s\n", yylineno, s);
 }
+
+/*
+ * Return a copy of the string.
+ */
+static char *
+makestr(str)
+       char *str;
+{
+       register char *cp, *s;
+
+       str = cp = malloc(strlen(s = str) + 1);
+       if (cp == NULL)
+               fatal("ran out of memory\n");
+       while (*cp++ = *s++)
+               ;
+       return(str);
+}
+
+/*
+ * Allocate a namelist structure.
+ */
+struct namelist *
+makenl(name)
+       char *name;
+{
+       register struct namelist *nl;
+
+       nl = ALLOC(namelist);
+       if (nl == NULL)
+               fatal("ran out of memory\n");
+       nl->n_name = name;
+       nl->n_next = NULL;
+       return(nl);
+}
+
+/*
+ * Make a sub command for lists of variables, commands, etc.
+ */
+struct subcmd *
+makesubcmd(type)
+       int type;
+{
+       register struct subcmd *sc;
+
+       sc = ALLOC(subcmd);
+       if (sc == NULL)
+               fatal("ran out of memory\n");
+       sc->sc_type = type;
+       sc->sc_args = NULL;
+       sc->sc_next = NULL;
+       sc->sc_name = NULL;
+       return(sc);
+}