gram.y revision 1.9 1 1.1 cgd %{
2 1.9 wiz /* $NetBSD: gram.y,v 1.9 2002/06/14 01:18:55 wiz Exp $ */
3 1.5 thorpej
4 1.1 cgd /*
5 1.3 cgd * Copyright (c) 1983, 1993
6 1.3 cgd * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.8 lukem #include <sys/cdefs.h>
38 1.1 cgd #ifndef lint
39 1.5 thorpej #if 0
40 1.5 thorpej static char sccsid[] = "@(#)gram.y 8.1 (Berkeley) 6/9/93";
41 1.5 thorpej #else
42 1.9 wiz __RCSID("$NetBSD: gram.y,v 1.9 2002/06/14 01:18:55 wiz Exp $");
43 1.5 thorpej #endif
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.1 cgd #include "defs.h"
47 1.1 cgd
48 1.1 cgd struct cmd *cmds = NULL;
49 1.1 cgd struct cmd *last_cmd;
50 1.1 cgd struct namelist *last_n;
51 1.1 cgd struct subcmd *last_sc;
52 1.1 cgd
53 1.9 wiz static char *makestr(char *);
54 1.9 wiz void append(char *, struct namelist *, char *, struct subcmd *);
55 1.3 cgd
56 1.1 cgd %}
57 1.1 cgd
58 1.1 cgd %term EQUAL 1
59 1.1 cgd %term LP 2
60 1.1 cgd %term RP 3
61 1.1 cgd %term SM 4
62 1.1 cgd %term ARROW 5
63 1.1 cgd %term COLON 6
64 1.1 cgd %term DCOLON 7
65 1.1 cgd %term NAME 8
66 1.1 cgd %term STRING 9
67 1.1 cgd %term INSTALL 10
68 1.1 cgd %term NOTIFY 11
69 1.1 cgd %term EXCEPT 12
70 1.1 cgd %term PATTERN 13
71 1.1 cgd %term SPECIAL 14
72 1.1 cgd %term OPTION 15
73 1.1 cgd
74 1.1 cgd %union {
75 1.1 cgd int intval;
76 1.1 cgd char *string;
77 1.1 cgd struct subcmd *subcmd;
78 1.1 cgd struct namelist *namel;
79 1.1 cgd }
80 1.1 cgd
81 1.1 cgd %type <intval> OPTION, options
82 1.1 cgd %type <string> NAME, STRING
83 1.1 cgd %type <subcmd> INSTALL, NOTIFY, EXCEPT, PATTERN, SPECIAL, cmdlist, cmd
84 1.1 cgd %type <namel> namelist, names, opt_namelist
85 1.1 cgd
86 1.1 cgd %%
87 1.1 cgd
88 1.1 cgd file: /* VOID */
89 1.1 cgd | file command
90 1.1 cgd ;
91 1.1 cgd
92 1.1 cgd command: NAME EQUAL namelist = {
93 1.1 cgd (void) lookup($1, INSERT, $3);
94 1.1 cgd }
95 1.1 cgd | namelist ARROW namelist cmdlist = {
96 1.1 cgd insert(NULL, $1, $3, $4);
97 1.1 cgd }
98 1.1 cgd | NAME COLON namelist ARROW namelist cmdlist = {
99 1.1 cgd insert($1, $3, $5, $6);
100 1.1 cgd }
101 1.1 cgd | namelist DCOLON NAME cmdlist = {
102 1.1 cgd append(NULL, $1, $3, $4);
103 1.1 cgd }
104 1.1 cgd | NAME COLON namelist DCOLON NAME cmdlist = {
105 1.1 cgd append($1, $3, $5, $6);
106 1.1 cgd }
107 1.1 cgd | error
108 1.1 cgd ;
109 1.1 cgd
110 1.1 cgd namelist: NAME = {
111 1.1 cgd $$ = makenl($1);
112 1.1 cgd }
113 1.1 cgd | LP names RP = {
114 1.1 cgd $$ = $2;
115 1.1 cgd }
116 1.1 cgd ;
117 1.1 cgd
118 1.1 cgd names: /* VOID */ {
119 1.1 cgd $$ = last_n = NULL;
120 1.1 cgd }
121 1.1 cgd | names NAME = {
122 1.1 cgd if (last_n == NULL)
123 1.1 cgd $$ = last_n = makenl($2);
124 1.1 cgd else {
125 1.1 cgd last_n->n_next = makenl($2);
126 1.1 cgd last_n = last_n->n_next;
127 1.1 cgd $$ = $1;
128 1.1 cgd }
129 1.1 cgd }
130 1.1 cgd ;
131 1.1 cgd
132 1.1 cgd cmdlist: /* VOID */ {
133 1.1 cgd $$ = last_sc = NULL;
134 1.1 cgd }
135 1.1 cgd | cmdlist cmd = {
136 1.1 cgd if (last_sc == NULL)
137 1.1 cgd $$ = last_sc = $2;
138 1.1 cgd else {
139 1.1 cgd last_sc->sc_next = $2;
140 1.1 cgd last_sc = $2;
141 1.1 cgd $$ = $1;
142 1.1 cgd }
143 1.1 cgd }
144 1.1 cgd ;
145 1.1 cgd
146 1.1 cgd cmd: INSTALL options opt_namelist SM = {
147 1.8 lukem struct namelist *nl;
148 1.1 cgd
149 1.1 cgd $1->sc_options = $2 | options;
150 1.1 cgd if ($3 != NULL) {
151 1.1 cgd nl = expand($3, E_VARS);
152 1.1 cgd if (nl) {
153 1.1 cgd if (nl->n_next != NULL)
154 1.1 cgd yyerror("only one name allowed\n");
155 1.1 cgd $1->sc_name = nl->n_name;
156 1.1 cgd free(nl);
157 1.1 cgd } else
158 1.1 cgd $1->sc_name = NULL;
159 1.1 cgd }
160 1.1 cgd $$ = $1;
161 1.1 cgd }
162 1.1 cgd | NOTIFY namelist SM = {
163 1.1 cgd if ($2 != NULL)
164 1.1 cgd $1->sc_args = expand($2, E_VARS);
165 1.1 cgd $$ = $1;
166 1.1 cgd }
167 1.1 cgd | EXCEPT namelist SM = {
168 1.1 cgd if ($2 != NULL)
169 1.1 cgd $1->sc_args = expand($2, E_ALL);
170 1.1 cgd $$ = $1;
171 1.1 cgd }
172 1.1 cgd | PATTERN namelist SM = {
173 1.4 thorpej if ($2 != NULL)
174 1.4 thorpej $1->sc_args = expand($2, E_VARS);
175 1.1 cgd $$ = $1;
176 1.1 cgd }
177 1.1 cgd | SPECIAL opt_namelist STRING SM = {
178 1.1 cgd if ($2 != NULL)
179 1.1 cgd $1->sc_args = expand($2, E_ALL);
180 1.1 cgd $1->sc_name = $3;
181 1.1 cgd $$ = $1;
182 1.1 cgd }
183 1.1 cgd ;
184 1.1 cgd
185 1.1 cgd options: /* VOID */ = {
186 1.1 cgd $$ = 0;
187 1.1 cgd }
188 1.1 cgd | options OPTION = {
189 1.1 cgd $$ |= $2;
190 1.1 cgd }
191 1.1 cgd ;
192 1.1 cgd
193 1.1 cgd opt_namelist: /* VOID */ = {
194 1.1 cgd $$ = NULL;
195 1.1 cgd }
196 1.1 cgd | namelist = {
197 1.1 cgd $$ = $1;
198 1.1 cgd }
199 1.1 cgd ;
200 1.1 cgd
201 1.1 cgd %%
202 1.1 cgd
203 1.1 cgd int yylineno = 1;
204 1.1 cgd extern FILE *fin;
205 1.1 cgd
206 1.9 wiz int yylex(void);
207 1.8 lukem
208 1.3 cgd int
209 1.9 wiz yylex(void)
210 1.1 cgd {
211 1.1 cgd static char yytext[INMAX];
212 1.8 lukem int c;
213 1.8 lukem char *cp1, *cp2;
214 1.1 cgd static char quotechars[] = "[]{}*?$";
215 1.1 cgd
216 1.1 cgd again:
217 1.1 cgd switch (c = getc(fin)) {
218 1.1 cgd case EOF: /* end of file */
219 1.1 cgd return(0);
220 1.1 cgd
221 1.1 cgd case '#': /* start of comment */
222 1.1 cgd while ((c = getc(fin)) != EOF && c != '\n')
223 1.1 cgd ;
224 1.1 cgd if (c == EOF)
225 1.1 cgd return(0);
226 1.1 cgd case '\n':
227 1.1 cgd yylineno++;
228 1.1 cgd case ' ':
229 1.1 cgd case '\t': /* skip blanks */
230 1.1 cgd goto again;
231 1.1 cgd
232 1.1 cgd case '=': /* EQUAL */
233 1.1 cgd return(EQUAL);
234 1.1 cgd
235 1.1 cgd case '(': /* LP */
236 1.1 cgd return(LP);
237 1.1 cgd
238 1.1 cgd case ')': /* RP */
239 1.1 cgd return(RP);
240 1.1 cgd
241 1.1 cgd case ';': /* SM */
242 1.1 cgd return(SM);
243 1.1 cgd
244 1.1 cgd case '-': /* -> */
245 1.1 cgd if ((c = getc(fin)) == '>')
246 1.1 cgd return(ARROW);
247 1.1 cgd ungetc(c, fin);
248 1.1 cgd c = '-';
249 1.1 cgd break;
250 1.1 cgd
251 1.1 cgd case '"': /* STRING */
252 1.1 cgd cp1 = yytext;
253 1.1 cgd cp2 = &yytext[INMAX - 1];
254 1.1 cgd for (;;) {
255 1.1 cgd if (cp1 >= cp2) {
256 1.1 cgd yyerror("command string too long\n");
257 1.1 cgd break;
258 1.1 cgd }
259 1.1 cgd c = getc(fin);
260 1.1 cgd if (c == EOF || c == '"')
261 1.1 cgd break;
262 1.1 cgd if (c == '\\') {
263 1.1 cgd if ((c = getc(fin)) == EOF) {
264 1.1 cgd *cp1++ = '\\';
265 1.1 cgd break;
266 1.1 cgd }
267 1.1 cgd }
268 1.1 cgd if (c == '\n') {
269 1.1 cgd yylineno++;
270 1.1 cgd c = ' '; /* can't send '\n' */
271 1.1 cgd }
272 1.1 cgd *cp1++ = c;
273 1.1 cgd }
274 1.1 cgd if (c != '"')
275 1.1 cgd yyerror("missing closing '\"'\n");
276 1.1 cgd *cp1 = '\0';
277 1.1 cgd yylval.string = makestr(yytext);
278 1.1 cgd return(STRING);
279 1.1 cgd
280 1.1 cgd case ':': /* : or :: */
281 1.1 cgd if ((c = getc(fin)) == ':')
282 1.1 cgd return(DCOLON);
283 1.1 cgd ungetc(c, fin);
284 1.1 cgd return(COLON);
285 1.1 cgd }
286 1.1 cgd cp1 = yytext;
287 1.1 cgd cp2 = &yytext[INMAX - 1];
288 1.1 cgd for (;;) {
289 1.1 cgd if (cp1 >= cp2) {
290 1.1 cgd yyerror("input line too long\n");
291 1.1 cgd break;
292 1.1 cgd }
293 1.1 cgd if (c == '\\') {
294 1.1 cgd if ((c = getc(fin)) != EOF) {
295 1.1 cgd if (any(c, quotechars))
296 1.1 cgd c |= QUOTE;
297 1.1 cgd } else {
298 1.1 cgd *cp1++ = '\\';
299 1.1 cgd break;
300 1.1 cgd }
301 1.1 cgd }
302 1.1 cgd *cp1++ = c;
303 1.1 cgd c = getc(fin);
304 1.1 cgd if (c == EOF || any(c, " \"'\t()=;:\n")) {
305 1.1 cgd ungetc(c, fin);
306 1.1 cgd break;
307 1.1 cgd }
308 1.1 cgd }
309 1.1 cgd *cp1 = '\0';
310 1.1 cgd if (yytext[0] == '-' && yytext[2] == '\0') {
311 1.1 cgd switch (yytext[1]) {
312 1.1 cgd case 'b':
313 1.1 cgd yylval.intval = COMPARE;
314 1.1 cgd return(OPTION);
315 1.1 cgd
316 1.1 cgd case 'R':
317 1.1 cgd yylval.intval = REMOVE;
318 1.1 cgd return(OPTION);
319 1.1 cgd
320 1.1 cgd case 'v':
321 1.1 cgd yylval.intval = VERIFY;
322 1.1 cgd return(OPTION);
323 1.1 cgd
324 1.1 cgd case 'w':
325 1.1 cgd yylval.intval = WHOLE;
326 1.1 cgd return(OPTION);
327 1.1 cgd
328 1.1 cgd case 'y':
329 1.1 cgd yylval.intval = YOUNGER;
330 1.1 cgd return(OPTION);
331 1.1 cgd
332 1.1 cgd case 'h':
333 1.1 cgd yylval.intval = FOLLOW;
334 1.1 cgd return(OPTION);
335 1.1 cgd
336 1.1 cgd case 'i':
337 1.1 cgd yylval.intval = IGNLNKS;
338 1.1 cgd return(OPTION);
339 1.1 cgd }
340 1.1 cgd }
341 1.1 cgd if (!strcmp(yytext, "install"))
342 1.1 cgd c = INSTALL;
343 1.1 cgd else if (!strcmp(yytext, "notify"))
344 1.1 cgd c = NOTIFY;
345 1.1 cgd else if (!strcmp(yytext, "except"))
346 1.1 cgd c = EXCEPT;
347 1.1 cgd else if (!strcmp(yytext, "except_pat"))
348 1.1 cgd c = PATTERN;
349 1.1 cgd else if (!strcmp(yytext, "special"))
350 1.1 cgd c = SPECIAL;
351 1.1 cgd else {
352 1.1 cgd yylval.string = makestr(yytext);
353 1.1 cgd return(NAME);
354 1.1 cgd }
355 1.1 cgd yylval.subcmd = makesubcmd(c);
356 1.1 cgd return(c);
357 1.1 cgd }
358 1.1 cgd
359 1.3 cgd int
360 1.9 wiz any(int c, char *str)
361 1.1 cgd {
362 1.1 cgd while (*str)
363 1.1 cgd if (c == *str++)
364 1.1 cgd return(1);
365 1.1 cgd return(0);
366 1.1 cgd }
367 1.1 cgd
368 1.1 cgd /*
369 1.1 cgd * Insert or append ARROW command to list of hosts to be updated.
370 1.1 cgd */
371 1.3 cgd void
372 1.9 wiz insert(char *label, struct namelist *files, struct namelist *hosts,
373 1.9 wiz struct subcmd *subcmds)
374 1.1 cgd {
375 1.8 lukem struct cmd *c, *prev, *nc;
376 1.8 lukem struct namelist *h, *nexth;
377 1.1 cgd
378 1.1 cgd files = expand(files, E_VARS|E_SHELL);
379 1.1 cgd hosts = expand(hosts, E_ALL);
380 1.6 thorpej for (h = hosts; h != NULL; nexth = h->n_next, free(h), h = nexth) {
381 1.1 cgd /*
382 1.1 cgd * Search command list for an update to the same host.
383 1.1 cgd */
384 1.1 cgd for (prev = NULL, c = cmds; c!=NULL; prev = c, c = c->c_next) {
385 1.1 cgd if (strcmp(c->c_name, h->n_name) == 0) {
386 1.1 cgd do {
387 1.1 cgd prev = c;
388 1.1 cgd c = c->c_next;
389 1.1 cgd } while (c != NULL &&
390 1.1 cgd strcmp(c->c_name, h->n_name) == 0);
391 1.1 cgd break;
392 1.1 cgd }
393 1.1 cgd }
394 1.1 cgd /*
395 1.1 cgd * Insert new command to update host.
396 1.1 cgd */
397 1.1 cgd nc = ALLOC(cmd);
398 1.1 cgd if (nc == NULL)
399 1.1 cgd fatal("ran out of memory\n");
400 1.1 cgd nc->c_type = ARROW;
401 1.1 cgd nc->c_name = h->n_name;
402 1.1 cgd nc->c_label = label;
403 1.1 cgd nc->c_files = files;
404 1.1 cgd nc->c_cmds = subcmds;
405 1.1 cgd nc->c_next = c;
406 1.1 cgd if (prev == NULL)
407 1.1 cgd cmds = nc;
408 1.1 cgd else
409 1.1 cgd prev->c_next = nc;
410 1.1 cgd /* update last_cmd if appending nc to cmds */
411 1.1 cgd if (c == NULL)
412 1.1 cgd last_cmd = nc;
413 1.1 cgd }
414 1.1 cgd }
415 1.1 cgd
416 1.1 cgd /*
417 1.1 cgd * Append DCOLON command to the end of the command list since these are always
418 1.1 cgd * executed in the order they appear in the distfile.
419 1.1 cgd */
420 1.3 cgd void
421 1.9 wiz append(char *label, struct namelist *files, char *stamp,
422 1.9 wiz struct subcmd *subcmds)
423 1.1 cgd {
424 1.8 lukem struct cmd *c;
425 1.1 cgd
426 1.1 cgd c = ALLOC(cmd);
427 1.1 cgd if (c == NULL)
428 1.1 cgd fatal("ran out of memory\n");
429 1.1 cgd c->c_type = DCOLON;
430 1.1 cgd c->c_name = stamp;
431 1.1 cgd c->c_label = label;
432 1.1 cgd c->c_files = expand(files, E_ALL);
433 1.1 cgd c->c_cmds = subcmds;
434 1.1 cgd c->c_next = NULL;
435 1.1 cgd if (cmds == NULL)
436 1.1 cgd cmds = last_cmd = c;
437 1.1 cgd else {
438 1.1 cgd last_cmd->c_next = c;
439 1.1 cgd last_cmd = c;
440 1.1 cgd }
441 1.1 cgd }
442 1.1 cgd
443 1.1 cgd /*
444 1.1 cgd * Error printing routine in parser.
445 1.1 cgd */
446 1.3 cgd void
447 1.9 wiz yyerror(char *s)
448 1.1 cgd {
449 1.7 mrg
450 1.3 cgd ++nerrs;
451 1.1 cgd fflush(stdout);
452 1.1 cgd fprintf(stderr, "rdist: line %d: %s\n", yylineno, s);
453 1.1 cgd }
454 1.1 cgd
455 1.1 cgd /*
456 1.1 cgd * Return a copy of the string.
457 1.1 cgd */
458 1.3 cgd static char *
459 1.9 wiz makestr(char *str)
460 1.1 cgd {
461 1.8 lukem char *cp, *s;
462 1.1 cgd
463 1.1 cgd str = cp = malloc(strlen(s = str) + 1);
464 1.1 cgd if (cp == NULL)
465 1.1 cgd fatal("ran out of memory\n");
466 1.8 lukem while ((*cp++ = *s++) != NULL)
467 1.1 cgd ;
468 1.1 cgd return(str);
469 1.1 cgd }
470 1.1 cgd
471 1.1 cgd /*
472 1.1 cgd * Allocate a namelist structure.
473 1.1 cgd */
474 1.1 cgd struct namelist *
475 1.9 wiz makenl(char *name)
476 1.1 cgd {
477 1.8 lukem struct namelist *nl;
478 1.1 cgd
479 1.1 cgd nl = ALLOC(namelist);
480 1.1 cgd if (nl == NULL)
481 1.1 cgd fatal("ran out of memory\n");
482 1.1 cgd nl->n_name = name;
483 1.1 cgd nl->n_next = NULL;
484 1.1 cgd return(nl);
485 1.1 cgd }
486 1.1 cgd
487 1.1 cgd /*
488 1.1 cgd * Make a sub command for lists of variables, commands, etc.
489 1.1 cgd */
490 1.1 cgd struct subcmd *
491 1.9 wiz makesubcmd(int type)
492 1.1 cgd {
493 1.8 lukem struct subcmd *sc;
494 1.1 cgd
495 1.1 cgd sc = ALLOC(subcmd);
496 1.1 cgd if (sc == NULL)
497 1.1 cgd fatal("ran out of memory\n");
498 1.1 cgd sc->sc_type = type;
499 1.1 cgd sc->sc_args = NULL;
500 1.1 cgd sc->sc_next = NULL;
501 1.1 cgd sc->sc_name = NULL;
502 1.1 cgd return(sc);
503 1.1 cgd }
504