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