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 in->fd = emalloc(sizeof(Biobuf));
107 if(Binit(in->fd, fd, OREAD) < 0)
108 parseerror("can't initialize Bio for rules file: %r");
137 return Bgetc(input->fd);
138 if(input->s < input->end)
139 return *(input->s)++;
159 if(c<0 || c=='\0' || c=='\n')
168 lookup(char *s, char *tab[])
172 for(i=0; tab[i]!=nil; i++)
173 if(strcmp(s, tab[i])==0)
179 lookupvariable(char *s, int n)
183 for(i=0; i<nvars; i++)
184 if(n==strlen(vars[i].name) && memcmp(s, vars[i].name, n)==0)
190 variable(char *s, int n)
194 var = lookupvariable(s, n);
201 setvariable(char *s, int n, char *val, char *qval)
205 var = lookupvariable(s, n);
210 vars = erealloc(vars, (nvars+1)*sizeof(Var));
212 var->name = emalloc(n+1);
213 memmove(var->name, s, n);
215 var->value = estrdup(val);
216 var->qvalue = estrdup(qval);
228 filename(Exec *e, char *name)
230 static char *buf; /* rock to hold value so we don't leak the strings */
233 /* if name is defined, used it */
234 if(name!=nil && name[0]!='\0'){
236 return cleanname(buf);
238 /* if data is an absolute file name, or wdir is empty, use it */
239 if(e->msg->data[0]=='/' || e->msg->wdir==nil || e->msg->wdir[0]=='\0'){
240 buf = estrdup(e->msg->data);
241 return cleanname(buf);
243 buf = emalloc(strlen(e->msg->wdir)+1+strlen(e->msg->data)+1);
244 sprint(buf, "%s/%s", e->msg->wdir, e->msg->data);
245 return cleanname(buf);
249 dollar(Exec *e, char *s, int *namelen)
256 if(e!=nil && '0'<=s[0] && s[0]<='9')
257 return nonnil(e->match[s[0]-'0']);
259 for(t=s; isalnum(*t); t++)
266 if(memcmp(s, "src", 3) == 0)
267 return nonnil(e->msg->src);
268 if(memcmp(s, "dst", 3) == 0)
269 return nonnil(e->msg->dst);
270 if(memcmp(s, "dir", 3) == 0)
271 return filename(e, e->dir);
274 if(memcmp(s, "attr", 4) == 0){
276 abuf = plumbpackattr(e->msg->attr);
279 if(memcmp(s, "data", 4) == 0)
280 return nonnil(e->msg->data);
281 if(memcmp(s, "file", 4) == 0)
282 return filename(e, e->file);
283 if(memcmp(s, "type", 4) == 0)
284 return nonnil(e->msg->type);
285 if(memcmp(s, "wdir", 4) == 0)
286 return nonnil(e->msg->wdir);
290 return variable(s, n);
293 /* expand one blank-terminated string, processing quotes and $ signs */
295 expand(Exec *e, char *s, char **ends)
298 int namelen, quoting;
301 ep = ebuf+sizeof ebuf-1;
303 while(p<ep && *s!='\0' && (quoting || (*s!=' ' && *s!='\t'))){
315 if(quoting || *s!='$'){
320 val = dollar(e, s, &namelen);
325 if(ep-p < strlen(val))
326 return "string-too-long";
342 parseerror("%s", msg);
350 r->qarg = estrdup(expand(nil, r->arg, nil));
359 if(r->verb==VClient || r->verb==VStart || r->verb==VTo)
360 parseerror("%s not valid verb for object %s", verbs[r->verb], objects[r->obj]);
361 if(r->obj!=OAttr && (r->verb==VAdd || r->verb==VDelete))
362 parseerror("%s not valid verb for object %s", verbs[r->verb], objects[r->obj]);
363 if(r->verb == VMatches){
364 r->regex = regcomp(r->qarg);
369 if(r->verb!=VClient && r->verb!=VStart && r->verb!=VTo)
370 parseerror("%s not valid verb for object %s", verbs[r->verb], objects[r->obj]);
383 for(var=p; isalnum(*p); p++)
386 while(*p==' ' || *p=='\t')
390 while(*p==' ' || *p=='\t')
392 qval = expand(nil, p, nil);
393 setvariable(var, n, p, qval);
400 char *t, *args[3], buf[128];
403 if(strncmp(s, "include", 7) != 0)
405 /* either an include or an error */
406 n = tokenize(s, args, nelem(args));
409 if(strcmp(args[0], "include") != 0)
411 if(args[1][0] == '#')
413 if(n>2 && args[2][0] != '#')
417 if(fd<0 && t[0]!='/' && strncmp(t, "./", 2)!=0 && strncmp(t, "../", 3)!=0){
418 snprint(buf, sizeof buf, "/sys/lib/plumb/%s", t);
423 parseerror("can't open %s for inclusion", t);
424 pushinput(t, fd, nil);
428 parseerror("malformed include statement");
443 * if input is from string, and bytes remain (input->end is within string),
444 * morerules() will pop input and save remaining data. otherwise pop
445 * the stack here, and if there's more input, keep reading.
447 if((input!=nil && input->end==nil) && popinput())
454 for(p=line; *p==' ' || *p=='\t'; p++)
456 if(*p=='\0' || *p=='#') /* empty or comment line */
465 rp = emalloc(sizeof(Rule));
468 for(word=p; *p!=' ' && *p!='\t'; p++)
470 parseerror("malformed rule");
472 rp->obj = lookup(word, objects);
474 if(strcmp(word, "kind") == 0) /* backwards compatibility */
477 parseerror("unknown object %s", word);
481 while(*p==' ' || *p=='\t')
483 for(word=p; *p!=' ' && *p!='\t'; p++)
485 parseerror("malformed rule");
487 rp->verb = lookup(word, verbs);
489 parseerror("unknown verb %s", word);
492 while(*p==' ' || *p=='\t')
495 parseerror("malformed rule");
496 rp->arg = estrdup(p);
519 freeruleset(Ruleset *rs)
534 int eof, inrule, i, ncmd;
538 rs = emalloc(sizeof(Ruleset));
539 rs->pat = emalloc(sizeof(Rule*));
540 rs->act = emalloc(sizeof(Rule*));
562 rs->pat = erealloc(rs->pat, (rs->npat+1)*sizeof(Rule*));
563 rs->pat[rs->npat-1] = r;
564 rs->pat[rs->npat] = nil;
568 rs->act = erealloc(rs->act, (rs->nact+1)*sizeof(Rule*));
569 rs->act[rs->nact-1] = r;
570 rs->act[rs->nact] = nil;
572 if(rs->npat>0 && rs->port != nil) /* npat==0 implies port declaration */
573 parseerror("too many ports");
574 if(lookup(r->qarg, badports) >= 0)
575 parseerror("illegal port name %s", r->qarg);
577 rs->port = estrdup(r->qarg);
579 ncmd++; /* start or client rule */
585 parseerror("ruleset has more than one client or start action");
587 if(rs->npat>0 && rs->nact>0)
589 if(rs->npat==0 && rs->nact==0){
593 if(rs->nact==0 || rs->port==nil){
595 parseerror("ruleset must have patterns and actions");
600 for(i=0; i<rs->nact; i++)
601 if(rs->act[i]->verb != VTo){
603 parseerror("ruleset must have actions");
606 for(i=0; i<rs->nact; i++)
607 addport(rs->act[i]->qarg);
613 readrules(char *name, int fd)
615 Ruleset *rs, **rules;
619 pushinput(name, fd, nil);
620 rules = emalloc(sizeof(Ruleset*));
621 for(n=0; (rs=readruleset())!=nil; n++){
622 rules = erealloc(rules, (n+2)*sizeof(Ruleset*));
632 concat(char *s, char *t)
639 s = erealloc(s, strlen(s)+strlen(t)+1);
650 s = emalloc(strlen(objects[r->obj])+1+strlen(verbs[r->verb])+1+strlen(r->arg)+1+1);
651 sprint(s, "%s\t%s\t%s\n", objects[r->obj], verbs[r->verb], r->arg);
660 s = emalloc(strlen(v->name)+1+strlen(v->value)+2+1);
661 sprint(s, "%s=%s\n\n", v->name, v->value);
666 printrule(Ruleset *r)
672 for(i=0; i<r->npat; i++)
673 s = concat(s, printpat(r->pat[i]));
674 for(i=0; i<r->nact; i++)
675 s = concat(s, printpat(r->act[i]));
681 printport(char *port)
686 s = concat(s, "plumb to ");
699 for(i=0; i<nvars; i++)
700 s = concat(s, printvar(&vars[i]));
701 for(i=0; i<nports; i++)
702 s = concat(s, printport(ports[i]));
704 for(i=0; rules[i]; i++)
705 s = concat(s, printrule(rules[i]));
710 stringof(char *s, int n)
720 morerules(uchar *text, int done)
724 uchar *otext, *s, *endofrule;
726 pushinput("<rules input>", -1, text);
728 input->end = text+strlen((char*)text);
731 * Help user by sending any full rules to parser so any parse errors will
732 * occur on write rather than close. A heuristic will do: blank line ends rule.
735 for(s=text; *s!='\0'; s++)
736 if(*s=='\n' && *++s=='\n')
740 input->end = endofrule;
742 for(n=0; rules[n]; n++)
744 while((rs=readruleset()) != nil){
745 rules = erealloc(rules, (n+2)*sizeof(Ruleset*));
751 text = (uchar*)estrdup("");
753 text = (uchar*)estrdup((char*)input->end);
760 writerules(char *s, int n)
768 if(setjmp(parsejmp) == 0){
769 tmp = stringof(s, n);
770 text = (uchar*)concat((char*)text, tmp);
772 text = morerules(text, s==nil);