4.4BSD snapshot (revision 8.1)
[unix-history] / usr / src / usr.bin / rdist / gram.y
index c9f12c3..36fd079 100644 (file)
@@ -1,30 +1,53 @@
 %{
 %{
+/*
+ * Copyright (c) 1983, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)gram.y      4.1 (Berkeley) 83/09/07";
-#endif
+static char sccsid[] = "@(#)gram.y     8.1 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "defs.h"
 
 
 #include "defs.h"
 
-struct block *last;
+struct cmd *cmds = NULL;
+struct cmd *last_cmd;
+struct namelist *last_n;
+struct subcmd *last_sc;
+
+static char  *makestr __P((char *));
 
 %}
 
 
 %}
 
-%term EQUAL 1
-%term ARROW 2
-%term LP 3
-%term RP 4
-%term NAME 5
-%term INSTALL 6
-%term VERIFY 7
-%term NOTIFY 8
-%term EXCEPT 9
-
-%union
-       {
-       struct block *blk;
-       }
+%term EQUAL    1
+%term LP       2
+%term RP       3
+%term SM       4
+%term ARROW    5
+%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 {
+       int intval;
+       char *string;
+       struct subcmd *subcmd;
+       struct namelist *namel;
+}
 
 
-%type <blk> NAME, INSTALL, VERIFY, NOTIFY, EXCEPT, namelist, names, cmdlist, cmd
+%type <intval> OPTION, options
+%type <string> NAME, STRING
+%type <subcmd> INSTALL, NOTIFY, EXCEPT, PATTERN, SPECIAL, cmdlist, cmd
+%type <namel> namelist, names, opt_namelist
 
 %%
 
 
 %%
 
@@ -33,17 +56,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 = {
-                       docmd($1, $3, $4);
+                       insert(NULL, $1, $3, $4);
+               }
+               | NAME COLON namelist ARROW namelist cmdlist = {
+                       insert($1, $3, $5, $6);
+               }
+               | namelist DCOLON NAME cmdlist = {
+                       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;
@@ -51,49 +82,89 @@ namelist:     NAME = {
                ;
 
 names:           /* VOID */ {
                ;
 
 names:           /* VOID */ {
-                       $$ = last = NULL;
+                       $$ = last_n = NULL;
                }
                | names NAME = {
                }
                | names NAME = {
-                       if (last == NULL)
-                               $$ = last = $2;
+                       if (last_n == NULL)
+                               $$ = last_n = makenl($2);
                        else {
                        else {
-                               last->b_next = $2;
-                               last = $2;
+                               last_n->n_next = makenl($2);
+                               last_n = last_n->n_next;
                                $$ = $1;
                        }
                }
                ;
 
 cmdlist:         /* VOID */ {
                                $$ = $1;
                        }
                }
                ;
 
 cmdlist:         /* VOID */ {
-                       $$ = last = NULL;
+                       $$ = last_sc = NULL;
                }
                | cmdlist cmd = {
                }
                | cmdlist cmd = {
-                       if (last == NULL)
-                               $$ = last = $2;
+                       if (last_sc == NULL)
+                               $$ = last_sc = $2;
                        else {
                        else {
-                               last->b_next = $2;
-                               last = $2;
+                               last_sc->sc_next = $2;
+                               last_sc = $2;
                                $$ = $1;
                        }
                }
                ;
 
                                $$ = $1;
                        }
                }
                ;
 
-cmd:             INSTALL NAME = {
-                       $1->b_name = $2->b_name;
-                       free($2);
+cmd:             INSTALL options opt_namelist SM = {
+                       register struct namelist *nl;
+
+                       $1->sc_options = $2 | options;
+                       if ($3 != NULL) {
+                               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;
                }
                        $$ = $1;
                }
-               | VERIFY NAME = {
-                       $1->b_name = $2->b_name;
-                       free($2);
+               | NOTIFY namelist SM = {
+                       if ($2 != NULL)
+                               $1->sc_args = expand($2, E_VARS);
                        $$ = $1;
                }
                        $$ = $1;
                }
-               | NOTIFY namelist = {
-                       $1->b_args = $2;
+               | EXCEPT namelist SM = {
+                       if ($2 != NULL)
+                               $1->sc_args = expand($2, E_ALL);
                        $$ = $1;
                }
                        $$ = $1;
                }
-               | EXCEPT namelist = {
-                       $1->b_args = expand($2);
+               | 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->sc_args = expand($2, E_ALL);
+                       $1->sc_name = $3;
+                       $$ = $1;
+               }
+               ;
+
+options:         /* VOID */ = {
+                       $$ = 0;
+               }
+               | options OPTION = {
+                       $$ |= $2;
+               }
+               ;
+
+opt_namelist:    /* VOID */ = {
+                       $$ = NULL;
+               }
+               | namelist = {
                        $$ = $1;
                }
                ;
                        $$ = $1;
                }
                ;
@@ -103,90 +174,158 @@ cmd:               INSTALL NAME = {
 int    yylineno = 1;
 extern FILE *fin;
 
 int    yylineno = 1;
 extern FILE *fin;
 
+int
 yylex()
 {
        static char yytext[INMAX];
        register int c;
        register char *cp1, *cp2;
 yylex()
 {
        static char yytext[INMAX];
        register int c;
        register char *cp1, *cp2;
-       register struct block *bp;
+       static char quotechars[] = "[]{}*?$";
        
        
-       for (;;) {
-               switch (c = getc(fin)) {
-               case EOF:  /* end of file */
+again:
+       switch (c = getc(fin)) {
+       case EOF:  /* end of file */
+               return(0);
+
+       case '#':  /* start of comment */
+               while ((c = getc(fin)) != EOF && c != '\n')
+                       ;
+               if (c == EOF)
                        return(0);
                        return(0);
+       case '\n':
+               yylineno++;
+       case ' ':
+       case '\t':  /* skip blanks */
+               goto again;
 
 
-               case '#':  /* start of comment */
-                       while ((c = getc(fin)) != EOF && c != '\n')
-                               ;
-                       if (c == EOF)
-                               return(0);
-               case '\n':
-                       yylineno++;
-               case ' ':
-               case '\t':  /* skip blanks */
-                       continue;
-
-               case '=':  /* EQUAL */
-                       return(EQUAL);
-
-               case '(':  /* LP */
-                       return(LP);
-
-               case ')':  /* RP */
-                       return(RP);
-
-               case '-':  /* -> */
-                       if ((c = getc(fin)) == '>')
-                               return(ARROW);
-                       ungetc(c, fin);
-                       c = '-';
-               }
-               /*
-                * Start of a name.
-                */
+       case '=':  /* EQUAL */
+               return(EQUAL);
+
+       case '(':  /* LP */
+               return(LP);
+
+       case ')':  /* RP */
+               return(RP);
+
+       case ';':  /* SM */
+               return(SM);
+
+       case '-':  /* -> */
+               if ((c = getc(fin)) == '>')
+                       return(ARROW);
+               ungetc(c, fin);
+               c = '-';
+               break;
+
+       case '"':  /* STRING */
                cp1 = yytext;
                cp2 = &yytext[INMAX - 1];
                for (;;) {
                        if (cp1 >= cp2) {
                cp1 = yytext;
                cp2 = &yytext[INMAX - 1];
                for (;;) {
                        if (cp1 >= cp2) {
-                               fatal("input line too long\n");
+                               yyerror("command string too long\n");
                                break;
                        }
                                break;
                        }
-                       *cp1++ = c;
                        c = getc(fin);
                        c = getc(fin);
-                       if (c == EOF || any(c, " \t()=\n")) {
-                               ungetc(c, fin);
+                       if (c == EOF || c == '"')
                                break;
                                break;
+                       if (c == '\\') {
+                               if ((c = getc(fin)) == EOF) {
+                                       *cp1++ = '\\';
+                                       break;
+                               }
+                       }
+                       if (c == '\n') {
+                               yylineno++;
+                               c = ' '; /* can't send '\n' */
                        }
                        }
+                       *cp1++ = c;
                }
                }
+               if (c != '"')
+                       yyerror("missing closing '\"'\n");
                *cp1 = '\0';
                *cp1 = '\0';
-               yylval.blk = bp = ALLOC(block);
-               if (bp == NULL)
-                       fatal("ran out of memory\n");
-               if (!strcmp(yytext, "install"))
-                       c = INSTALL;
-               else if (!strcmp(yytext, "verify"))
-                       c = VERIFY;
-               else if (!strcmp(yytext, "notify"))
-                       c = NOTIFY;
-               else if (!strcmp(yytext, "except"))
-                       c = EXCEPT;
-               else
-                       c = NAME;
-               bp->b_type = c;
-               bp->b_next = bp->b_args = NULL;
-               if (c == NAME) {
-                       c = strlen(yytext) + 1;
-                       bp->b_name = cp1 = (char *) malloc(c);
-                       if (cp1 == NULL)
-                               fatal("ran out of memory\n");
-                       for (cp2 = yytext; *cp1++ = *cp2++; )
-                               ;
-               } else
-                       bp->b_name = NULL;
-               return(bp->b_type);
+               yylval.string = makestr(yytext);
+               return(STRING);
+
+       case ':':  /* : or :: */
+               if ((c = getc(fin)) == ':')
+                       return(DCOLON);
+               ungetc(c, fin);
+               return(COLON);
+       }
+       cp1 = yytext;
+       cp2 = &yytext[INMAX - 1];
+       for (;;) {
+               if (cp1 >= cp2) {
+                       yyerror("input line too long\n");
+                       break;
+               }
+               if (c == '\\') {
+                       if ((c = getc(fin)) != EOF) {
+                               if (any(c, quotechars))
+                                       c |= QUOTE;
+                       } else {
+                               *cp1++ = '\\';
+                               break;
+                       }
+               }
+               *cp1++ = c;
+               c = getc(fin);
+               if (c == EOF || any(c, " \"'\t()=;:\n")) {
+                       ungetc(c, fin);
+                       break;
+               }
+       }
+       *cp1 = '\0';
+       if (yytext[0] == '-' && yytext[2] == '\0') {
+               switch (yytext[1]) {
+               case 'b':
+                       yylval.intval = COMPARE;
+                       return(OPTION);
+
+               case 'R':
+                       yylval.intval = REMOVE;
+                       return(OPTION);
+
+               case 'v':
+                       yylval.intval = VERIFY;
+                       return(OPTION);
+
+               case 'w':
+                       yylval.intval = WHOLE;
+                       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"))
+               c = INSTALL;
+       else if (!strcmp(yytext, "notify"))
+               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 {
+               yylval.string = makestr(yytext);
+               return(NAME);
+       }
+       yylval.subcmd = makesubcmd(c);
+       return(c);
 }
 
 }
 
+int
 any(c, str)
        register int c;
        register char *str;
 any(c, str)
        register int c;
        register char *str;
@@ -197,13 +336,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;
-
+       ++nerrs;
+       fflush(stdout);
        fprintf(stderr, "rdist: line %d: %s\n", yylineno, s);
 }
        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);
+}