NetBSD/usr.bin/rdist/gram.y

518 lines
9.7 KiB
Plaintext
Raw Normal View History

1993-03-21 12:45:37 +03:00
%{
/* $NetBSD: gram.y,v 1.13 2009/04/13 04:35:36 lukem Exp $ */
1996-07-12 04:46:18 +04:00
1993-03-21 12:45:37 +03:00
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* 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. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* 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.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1996-07-12 04:46:18 +04:00
#if 0
static char sccsid[] = "@(#)gram.y 8.1 (Berkeley) 6/9/93";
#else
__RCSID("$NetBSD: gram.y,v 1.13 2009/04/13 04:35:36 lukem Exp $");
1996-07-12 04:46:18 +04:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include "defs.h"
struct cmd *cmds = NULL;
struct cmd *last_cmd;
struct namelist *last_n;
struct subcmd *last_sc;
static char *makestr(char *);
void append(char *, struct namelist *, char *, struct subcmd *);
1993-03-21 12:45:37 +03:00
%}
%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 <intval> OPTION, options
%type <string> NAME, STRING
%type <subcmd> INSTALL, NOTIFY, EXCEPT, PATTERN, SPECIAL, cmdlist, cmd
%type <namel> namelist, names, opt_namelist
%%
file: /* VOID */
| file command
;
command: NAME EQUAL namelist = {
(void) lookup($1, INSERT, $3);
}
| namelist ARROW namelist cmdlist = {
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 = {
$$ = makenl($1);
}
| LP names RP = {
$$ = $2;
}
;
names: /* VOID */ {
$$ = last_n = NULL;
}
| names NAME = {
if (last_n == NULL)
$$ = last_n = makenl($2);
else {
last_n->n_next = makenl($2);
last_n = last_n->n_next;
$$ = $1;
}
}
;
cmdlist: /* VOID */ {
$$ = last_sc = NULL;
}
| cmdlist cmd = {
if (last_sc == NULL)
$$ = last_sc = $2;
else {
last_sc->sc_next = $2;
last_sc = $2;
$$ = $1;
}
}
;
cmd: INSTALL options opt_namelist SM = {
struct namelist *nl;
1993-03-21 12:45:37 +03:00
$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;
}
| NOTIFY namelist SM = {
if ($2 != NULL)
$1->sc_args = expand($2, E_VARS);
$$ = $1;
}
| EXCEPT namelist SM = {
if ($2 != NULL)
$1->sc_args = expand($2, E_ALL);
$$ = $1;
}
| PATTERN namelist SM = {
if ($2 != NULL)
$1->sc_args = expand($2, E_VARS);
1993-03-21 12:45:37 +03:00
$$ = $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;
}
;
%%
int yylineno = 1;
extern FILE *fin;
int yylex(void);
int
yylex(void)
1993-03-21 12:45:37 +03:00
{
static char yytext[INMAX];
int c;
char *cp1, *cp2;
1993-03-21 12:45:37 +03:00
static char quotechars[] = "[]{}*?$";
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);
case '\n':
yylineno++;
case ' ':
case '\t': /* skip blanks */
goto again;
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) {
yyerror("command string too long\n");
break;
}
c = getc(fin);
if (c == EOF || c == '"')
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';
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(int c, const char *str)
1993-03-21 12:45:37 +03:00
{
while (*str)
if (c == *str++)
return(1);
return(0);
}
/*
* Insert or append ARROW command to list of hosts to be updated.
*/
void
insert(char *label, struct namelist *files, struct namelist *hosts,
struct subcmd *subcmds)
1993-03-21 12:45:37 +03:00
{
struct cmd *c, *prev, *nc;
struct namelist *h, *nexth;
1993-03-21 12:45:37 +03:00
files = expand(files, E_VARS|E_SHELL);
hosts = expand(hosts, E_ALL);
for (h = hosts; h != NULL; nexth = h->n_next, free(h), h = nexth) {
1993-03-21 12:45:37 +03:00
/*
* 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(char *label, struct namelist *files, char *stamp,
struct subcmd *subcmds)
1993-03-21 12:45:37 +03:00
{
struct cmd *c;
1993-03-21 12:45:37 +03:00
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.
*/
void
yyerror(const char *s)
1993-03-21 12:45:37 +03:00
{
1997-10-18 18:34:39 +04:00
++nerrs;
1993-03-21 12:45:37 +03:00
fflush(stdout);
fprintf(stderr, "rdist: line %d: %s\n", yylineno, s);
}
/*
* Return a copy of the string.
*/
static char *
makestr(char *str)
1993-03-21 12:45:37 +03:00
{
char *cp, *s;
1993-03-21 12:45:37 +03:00
str = cp = malloc(strlen(s = str) + 1);
if (cp == NULL)
fatal("ran out of memory\n");
2003-10-21 06:23:13 +04:00
while ((*cp++ = *s++) != 0)
1993-03-21 12:45:37 +03:00
;
return(str);
}
/*
* Allocate a namelist structure.
*/
struct namelist *
makenl(char *name)
1993-03-21 12:45:37 +03:00
{
struct namelist *nl;
1993-03-21 12:45:37 +03:00
nl = ALLOC(namelist);
if (nl == NULL)
fatal("ran out of memory\n");
nl->n_name = name;
nl->n_next = NULL;
return(nl);
}
void
freenl(struct namelist *nl)
{
if (nl == NULL)
return;
freenl(nl->n_next);
free(nl);
}
void
freesubcmd(struct subcmd *cmd)
{
if (cmd == NULL)
return;
freesubcmd(cmd->sc_next);
free(cmd);
}
1993-03-21 12:45:37 +03:00
/*
* Make a sub command for lists of variables, commands, etc.
*/
struct subcmd *
makesubcmd(int type)
1993-03-21 12:45:37 +03:00
{
struct subcmd *sc;
1993-03-21 12:45:37 +03:00
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);
}