10 typedef struct Input Input;
11 typedef struct Var Var;
15 char *file; /* name of file */
16 Biobuf *fd; /* input buffer, if from real file */
17 uchar *s; /* input string, if from /mnt/plumb/rules */
18 uchar *end; /* end of input string */
20 Input *next; /* file to read after EOF on this one */
35 static char ebuf[4096];
74 printinputstackrev(Input *in)
78 printinputstackrev(in->next);
79 fprint(2, "%s:%d: ", in->file, in->lineno);
85 printinputstackrev(input);
89 pushinput(char *name, int fd, uchar *str)
95 for(in=input; in; in=in->next)
96 if(depth++ >= 10) /* prevent deep C stack in plumber and bad include structure */
97 parseerror("include stack too deep; max 10");
99 in = emalloc(sizeof(Input));
100 in->file = estrdup(name);
106 if((in->fd = Bfdopen(fd, OREAD)) == nil)
107 parseerror("can't initialize Bio for rules file: %r");
134 return Bgetc(input->fd);
135 if(input->s < input->end)
136 return *(input->s)++;
156 if(c<0 || c=='\0' || c=='\n')
165 lookup(char *s, char *tab[])
169 for(i=0; tab[i]!=nil; i++)
170 if(strcmp(s, tab[i])==0)
176 lookupvariable(char *s, int n)
180 for(i=0; i<nvars; i++)
181 if(n==strlen(vars[i].name) && memcmp(s, vars[i].name, n)==0)
187 variable(char *s, int n)
191 var = lookupvariable(s, n);
198 setvariable(char *s, int n, char *val, char *qval)
202 var = lookupvariable(s, n);
207 vars = erealloc(vars, (nvars+1)*sizeof(Var));
209 var->name = emalloc(n+1);
210 memmove(var->name, s, n);
212 var->value = estrdup(val);
213 var->qvalue = estrdup(qval);
225 filename(Exec *e, char *name)
227 static char *buf; /* rock to hold value so we don't leak the strings */
230 /* if name is defined, used it */
231 if(name!=nil && name[0]!='\0'){
233 return cleanname(buf);
235 /* if data is an absolute file name, or wdir is empty, use it */
236 if(e->msg->data[0]=='/' || e->msg->wdir==nil || e->msg->wdir[0]=='\0'){
237 buf = estrdup(e->msg->data);
238 return cleanname(buf);
240 buf = emalloc(strlen(e->msg->wdir)+1+strlen(e->msg->data)+1);
241 sprint(buf, "%s/%s", e->msg->wdir, e->msg->data);
242 return cleanname(buf);
246 dollar(Exec *e, char *s, int *namelen)
253 if(e!=nil && '0'<=s[0] && s[0]<='9')
254 return nonnil(e->match[s[0]-'0']);
256 for(t=s; isalnum(*t); t++)
263 if(memcmp(s, "src", 3) == 0)
264 return nonnil(e->msg->src);
265 if(memcmp(s, "dst", 3) == 0)
266 return nonnil(e->msg->dst);
267 if(memcmp(s, "dir", 3) == 0)
268 return filename(e, e->dir);
271 if(memcmp(s, "attr", 4) == 0){
273 abuf = plumbpackattr(e->msg->attr);
276 if(memcmp(s, "data", 4) == 0)
277 return nonnil(e->msg->data);
278 if(memcmp(s, "file", 4) == 0)
279 return filename(e, e->file);
280 if(memcmp(s, "type", 4) == 0)
281 return nonnil(e->msg->type);
282 if(memcmp(s, "wdir", 4) == 0)
283 return nonnil(e->msg->wdir);
287 return variable(s, n);
290 /* expand one blank-terminated string, processing quotes and $ signs */
292 expand(Exec *e, char *s, char **ends)
295 int namelen, quoting;
298 ep = ebuf+sizeof ebuf-1;
300 while(p<ep && *s!='\0' && (quoting || (*s!=' ' && *s!='\t'))){
312 if(quoting || *s!='$'){
317 val = dollar(e, s, &namelen);
322 if(ep-p < strlen(val))
323 return "string-too-long";
339 parseerror("%s", msg);
347 r->qarg = estrdup(expand(nil, r->arg, nil));
356 if(r->verb==VClient || r->verb==VStart || r->verb==VTo)
357 parseerror("%s not valid verb for object %s", verbs[r->verb], objects[r->obj]);
358 if(r->obj!=OAttr && (r->verb==VAdd || r->verb==VDelete))
359 parseerror("%s not valid verb for object %s", verbs[r->verb], objects[r->obj]);
360 if(r->verb == VMatches){
361 r->regex = regcomp(r->qarg);
366 if(r->verb!=VClient && r->verb!=VStart && r->verb!=VTo)
367 parseerror("%s not valid verb for object %s", verbs[r->verb], objects[r->obj]);
380 for(var=p; isalnum(*p); p++)
383 while(*p==' ' || *p=='\t')
387 while(*p==' ' || *p=='\t')
389 qval = expand(nil, p, nil);
390 setvariable(var, n, p, qval);
397 char *t, *args[3], buf[128];
400 if(strncmp(s, "include", 7) != 0)
402 /* either an include or an error */
403 n = tokenize(s, args, nelem(args));
406 if(strcmp(args[0], "include") != 0)
408 if(args[1][0] == '#')
410 if(n>2 && args[2][0] != '#')
414 if(fd<0 && t[0]!='/' && strncmp(t, "./", 2)!=0 && strncmp(t, "../", 3)!=0){
415 snprint(buf, sizeof buf, "/sys/lib/plumb/%s", t);
420 parseerror("can't open %s for inclusion", t);
421 pushinput(t, fd, nil);
425 parseerror("malformed include statement");
450 * if input is from string, and bytes remain (input->end is within string),
451 * morerules() will pop input and save remaining data. otherwise pop
452 * the stack here, and if there's more input, keep reading.
454 if((input!=nil && input->end==nil) && popinput())
461 for(p=line; *p==' ' || *p=='\t'; p++)
463 if(*p=='\0' || *p=='#') /* empty or comment line */
472 rp = emalloc(sizeof(Rule));
474 memmove(ojmp, parsejmp, sizeof(jmp_buf));
475 if(setjmp(parsejmp)){
481 for(word=p; *p!=' ' && *p!='\t'; p++)
483 parseerror("malformed rule");
485 rp->obj = lookup(word, objects);
487 if(strcmp(word, "kind") == 0) /* backwards compatibility */
490 parseerror("unknown object %s", word);
494 while(*p==' ' || *p=='\t')
496 for(word=p; *p!=' ' && *p!='\t'; p++)
498 parseerror("malformed rule");
500 rp->verb = lookup(word, verbs);
502 parseerror("unknown verb %s", word);
505 while(*p==' ' || *p=='\t')
508 parseerror("malformed rule");
509 rp->arg = estrdup(p);
513 memmove(parsejmp, ojmp, sizeof(jmp_buf));
526 freeruleset(Ruleset *rs)
541 int eof, inrule, i, ncmd;
545 rs = emalloc(sizeof(Ruleset));
546 rs->pat = emalloc(sizeof(Rule*));
547 rs->act = emalloc(sizeof(Rule*));
569 rs->pat = erealloc(rs->pat, (rs->npat+1)*sizeof(Rule*));
570 rs->pat[rs->npat-1] = r;
571 rs->pat[rs->npat] = nil;
575 rs->act = erealloc(rs->act, (rs->nact+1)*sizeof(Rule*));
576 rs->act[rs->nact-1] = r;
577 rs->act[rs->nact] = nil;
579 if(rs->npat>0 && rs->port != nil) /* npat==0 implies port declaration */
580 parseerror("too many ports");
581 if(lookup(r->qarg, badports) >= 0)
582 parseerror("illegal port name %s", r->qarg);
584 rs->port = estrdup(r->qarg);
586 ncmd++; /* start or client rule */
592 parseerror("ruleset has more than one client or start action");
594 if(rs->npat>0 && rs->nact>0)
596 if(rs->npat==0 && rs->nact==0){
600 if(rs->nact==0 || rs->port==nil){
602 parseerror("ruleset must have patterns and actions");
607 for(i=0; i<rs->nact; i++)
608 if(rs->act[i]->verb != VTo){
610 parseerror("ruleset must have actions");
613 for(i=0; i<rs->nact; i++)
614 addport(rs->act[i]->qarg);
620 readrules(char *name, int fd)
622 Ruleset *rs, **rules;
626 pushinput(name, fd, nil);
627 rules = emalloc(sizeof(Ruleset*));
628 for(n=0; (rs=readruleset())!=nil; n++){
629 rules = erealloc(rules, (n+2)*sizeof(Ruleset*));
639 concat(char *s, char *t)
646 s = erealloc(s, strlen(s)+strlen(t)+1);
657 s = emalloc(strlen(objects[r->obj])+1+strlen(verbs[r->verb])+1+strlen(r->arg)+1+1);
658 sprint(s, "%s\t%s\t%s\n", objects[r->obj], verbs[r->verb], r->arg);
667 s = emalloc(strlen(v->name)+1+strlen(v->value)+2+1);
668 sprint(s, "%s=%s\n\n", v->name, v->value);
673 printrule(Ruleset *r)
679 for(i=0; i<r->npat; i++)
680 s = concat(s, printpat(r->pat[i]));
681 for(i=0; i<r->nact; i++)
682 s = concat(s, printpat(r->act[i]));
688 printport(char *port)
693 s = concat(s, "plumb to ");
706 for(i=0; i<nvars; i++)
707 s = concat(s, printvar(&vars[i]));
708 for(i=0; i<nports; i++)
709 s = concat(s, printport(ports[i]));
711 for(i=0; rules[i]; i++)
712 s = concat(s, printrule(rules[i]));
717 stringof(char *s, int n)
727 morerules(uchar *text, int done)
731 uchar *otext, *s, *endofrule;
733 pushinput("<rules input>", -1, text);
735 input->end = text+strlen((char*)text);
738 * Help user by sending any full rules to parser so any parse errors will
739 * occur on write rather than close. A heuristic will do: blank line ends rule.
742 for(s=text; *s!='\0'; s++)
743 if(*s=='\n' && *++s=='\n')
745 if(endofrule == nil){
749 input->end = endofrule;
751 for(n=0; rules[n]; n++)
753 while((rs=readruleset()) != nil){
754 rules = erealloc(rules, (n+2)*sizeof(Ruleset*));
760 text = (uchar*)estrdup("");
762 text = (uchar*)estrdup((char*)input->end);
769 writerules(char *s, int n)
777 if(setjmp(parsejmp) == 0){
778 tmp = stringof(s, n);
779 text = (uchar*)concat((char*)text, tmp);
781 text = morerules(text, s==nil);