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");
136 return Bgetc(input->fd);
137 if(input->s < input->end)
138 return *(input->s)++;
146 static char *s /*, *incl*/;
158 if(c<0 || c=='\0' || c=='\n')
167 lookup(char *s, char *tab[])
171 for(i=0; tab[i]!=nil; i++)
172 if(strcmp(s, tab[i])==0)
178 lookupvariable(char *s, int n)
182 for(i=0; i<nvars; i++)
183 if(n==strlen(vars[i].name) && memcmp(s, vars[i].name, n)==0)
189 variable(char *s, int n)
193 var = lookupvariable(s, n);
200 setvariable(char *s, int n, char *val, char *qval)
204 var = lookupvariable(s, n);
209 vars = erealloc(vars, (nvars+1)*sizeof(Var));
211 var->name = emalloc(n+1);
212 memmove(var->name, s, n);
214 var->value = estrdup(val);
215 var->qvalue = estrdup(qval);
227 filename(Exec *e, char *name)
229 static char *buf; /* rock to hold value so we don't leak the strings */
232 /* if name is defined, used it */
233 if(name!=nil && name[0]!='\0'){
235 return cleanname(buf);
237 /* if data is an absolute file name, or wdir is empty, use it */
238 if(e->msg->data[0]=='/' || e->msg->wdir==nil || e->msg->wdir[0]=='\0'){
239 buf = estrdup(e->msg->data);
240 return cleanname(buf);
242 buf = emalloc(strlen(e->msg->wdir)+1+strlen(e->msg->data)+1);
243 sprint(buf, "%s/%s", e->msg->wdir, e->msg->data);
244 return cleanname(buf);
248 dollar(Exec *e, char *s, int *namelen)
255 if(e!=nil && '0'<=s[0] && s[0]<='9')
256 return nonnil(e->match[s[0]-'0']);
258 for(t=s; isalnum(*t); t++)
265 if(memcmp(s, "src", 3) == 0)
266 return nonnil(e->msg->src);
267 if(memcmp(s, "dst", 3) == 0)
268 return nonnil(e->msg->dst);
269 if(memcmp(s, "dir", 3) == 0)
270 return filename(e, e->dir);
273 if(memcmp(s, "attr", 4) == 0){
275 abuf = plumbpackattr(e->msg->attr);
278 if(memcmp(s, "data", 4) == 0)
279 return nonnil(e->msg->data);
280 if(memcmp(s, "file", 4) == 0)
281 return filename(e, e->file);
282 if(memcmp(s, "type", 4) == 0)
283 return nonnil(e->msg->type);
284 if(memcmp(s, "wdir", 3) == 0)
285 return nonnil(e->msg->wdir);
289 return variable(s, n);
292 /* expand one blank-terminated string, processing quotes and $ signs */
294 expand(Exec *e, char *s, char **ends)
297 int namelen, quoting;
300 ep = ebuf+sizeof ebuf-1;
302 while(p<ep && *s!='\0' && (quoting || (*s!=' ' && *s!='\t'))){
314 if(quoting || *s!='$'){
319 val = dollar(e, s, &namelen);
324 if(ep-p < strlen(val))
325 return "string-too-long";
341 parseerror("%s", msg);
349 r->qarg = estrdup(expand(nil, r->arg, nil));
358 if(r->verb==VClient || r->verb==VStart || r->verb==VTo)
359 parseerror("%s not valid verb for object %s", verbs[r->verb], objects[r->obj]);
360 if(r->obj!=OAttr && (r->verb==VAdd || r->verb==VDelete))
361 parseerror("%s not valid verb for object %s", verbs[r->verb], objects[r->obj]);
362 if(r->verb == VMatches){
363 r->regex = regcomp(r->qarg);
368 if(r->verb!=VClient && r->verb!=VStart && r->verb!=VTo)
369 parseerror("%s not valid verb for object %s", verbs[r->verb], objects[r->obj]);
382 for(var=p; isalnum(*p); p++)
385 while(*p==' ' || *p=='\t')
389 while(*p==' ' || *p=='\t')
391 qval = expand(nil, p, nil);
392 setvariable(var, n, p, qval);
399 char *t, *args[3], buf[128];
402 if(strncmp(s, "include", 7) != 0)
404 /* either an include or an error */
405 n = tokenize(s, args, nelem(args));
408 if(strcmp(args[0], "include") != 0)
410 if(args[1][0] == '#')
412 if(n>2 && args[2][0] != '#')
416 if(fd<0 && t[0]!='/' && strncmp(t, "./", 2)!=0 && strncmp(t, "../", 3)!=0){
417 snprint(buf, sizeof buf, "#9/plumb/%s", t);
422 parseerror("can't open %s for inclusion", t);
423 pushinput(t, fd, nil);
427 parseerror("malformed include statement");
442 * if input is from string, and bytes remain (input->end is within string),
443 * morerules() will pop input and save remaining data. otherwise pop
444 * the stack here, and if there's more input, keep reading.
446 if((input!=nil && input->end==nil) && popinput())
453 for(p=line; *p==' ' || *p=='\t'; p++)
455 if(*p=='\0' || *p=='#') /* empty or comment line */
464 rp = emalloc(sizeof(Rule));
467 for(word=p; *p!=' ' && *p!='\t'; p++)
469 parseerror("malformed rule");
471 rp->obj = lookup(word, objects);
473 if(strcmp(word, "kind") == 0) /* backwards compatibility */
476 parseerror("unknown object %s", word);
480 while(*p==' ' || *p=='\t')
482 for(word=p; *p!=' ' && *p!='\t'; p++)
484 parseerror("malformed rule");
486 rp->verb = lookup(word, verbs);
488 parseerror("unknown verb %s", word);
491 while(*p==' ' || *p=='\t')
494 parseerror("malformed rule");
495 rp->arg = estrdup(p);
518 freeruleset(Ruleset *rs)
533 int eof, inrule, i, ncmd;
536 plan9root = get9root();
538 setvariable("plan9", 5, plan9root, plan9root);
542 rs = emalloc(sizeof(Ruleset));
543 rs->pat = emalloc(sizeof(Rule*));
544 rs->act = emalloc(sizeof(Rule*));
566 rs->pat = erealloc(rs->pat, (rs->npat+1)*sizeof(Rule*));
567 rs->pat[rs->npat-1] = r;
568 rs->pat[rs->npat] = nil;
572 rs->act = erealloc(rs->act, (rs->nact+1)*sizeof(Rule*));
573 rs->act[rs->nact-1] = r;
574 rs->act[rs->nact] = nil;
576 if(rs->npat>0 && rs->port != nil) /* npat==0 implies port declaration */
577 parseerror("too many ports");
578 if(lookup(r->qarg, badports) >= 0)
579 parseerror("illegal port name %s", r->qarg);
580 rs->port = estrdup(r->qarg);
582 ncmd++; /* start or client rule */
588 parseerror("ruleset has more than one client or start action");
590 if(rs->npat>0 && rs->nact>0)
592 if(rs->npat==0 && rs->nact==0){
596 if(rs->nact==0 || rs->port==nil){
598 parseerror("ruleset must have patterns and actions");
603 for(i=0; i<rs->nact; i++)
604 if(rs->act[i]->verb != VTo){
606 parseerror("ruleset must have actions");
609 for(i=0; i<rs->nact; i++)
610 addport(rs->act[i]->qarg);
616 readrules(char *name, int fd)
618 Ruleset *rs, **rules;
622 pushinput(name, fd, nil);
623 rules = emalloc(sizeof(Ruleset*));
624 for(n=0; (rs=readruleset())!=nil; n++){
625 rules = erealloc(rules, (n+2)*sizeof(Ruleset*));
635 concat(char *s, char *t)
642 s = erealloc(s, strlen(s)+strlen(t)+1);
653 s = emalloc(strlen(objects[r->obj])+1+strlen(verbs[r->verb])+1+strlen(r->arg)+1+1);
654 sprint(s, "%s\t%s\t%s\n", objects[r->obj], verbs[r->verb], r->arg);
663 s = emalloc(strlen(v->name)+1+strlen(v->value)+2+1);
664 sprint(s, "%s=%s\n\n", v->name, v->value);
669 printrule(Ruleset *r)
675 for(i=0; i<r->npat; i++)
676 s = concat(s, printpat(r->pat[i]));
677 for(i=0; i<r->nact; i++)
678 s = concat(s, printpat(r->act[i]));
684 printport(char *port)
689 s = concat(s, "plumb to ");
702 for(i=0; i<nvars; i++)
703 s = concat(s, printvar(&vars[i]));
704 for(i=0; i<nports; i++)
705 s = concat(s, printport(ports[i]));
707 for(i=0; rules[i]; i++)
708 s = concat(s, printrule(rules[i]));
713 stringof(char *s, int n)
723 morerules(uchar *text, int done)
727 uchar *otext, *s, *endofrule;
729 pushinput("<rules input>", -1, text);
731 input->end = text+strlen((char*)text);
734 * Help user by sending any full rules to parser so any parse errors will
735 * occur on write rather than close. A heuristic will do: blank line ends rule.
738 for(s=text; *s!='\0'; s++)
739 if(*s=='\n' && *++s=='\n')
743 input->end = endofrule;
745 for(n=0; rules[n]; n++)
747 while((rs=readruleset()) != nil){
748 rules = erealloc(rules, (n+2)*sizeof(Ruleset*));
749 fprint(2, "initialize rules %d %d\n", n, n+1);
755 text = (uchar*)estrdup("");
757 text = (uchar*)estrdup((char*)input->end);
764 writerules(char *s, int n)
772 if(setjmp(parsejmp) == 0){
773 tmp = stringof(s, n);
774 text = (uchar*)concat((char*)text, tmp);
776 text = morerules(text, s==nil);