Blob
1 #include <u.h>2 #include <libc.h>3 #include <regexp.h>4 #include <thread.h>5 #include <plumb.h>6 #include <fcall.h>7 #include "plumber.h"9 int debug;10 char *plumbfile;11 char *user;12 char *home;13 char *progname;14 Ruleset **rules;15 int printerrors=1;16 jmp_buf parsejmp;17 char *lasterror;19 void20 makeports(Ruleset *rules[])21 {22 int i;24 for(i=0; rules[i]; i++)25 addport(rules[i]->port);26 }28 void29 threadmain(int argc, char *argv[])30 {31 char buf[512];32 int fd;34 progname = "plumber";36 ARGBEGIN{37 case 'd':38 debug = 1;39 break;40 case 'p':41 plumbfile = ARGF();42 break;43 }ARGEND45 user = getuser();46 home = getenv("HOME");47 if(user==nil || home==nil)48 error("can't initialize $user or $home: %r");49 if(plumbfile == nil){50 sprint(buf, "%s/lib/plumbing", home);51 if(access(buf, 0) >= 0)52 plumbfile = estrdup(buf);53 else54 plumbfile = unsharp("#9/plumb/initial.plumbing");55 }57 fd = open(plumbfile, OREAD);58 if(fd < 0)59 error("can't open rules file %s: %r", plumbfile);60 if(setjmp(parsejmp))61 error("parse error");63 rules = readrules(plumbfile, fd);64 close(fd);66 /*67 * Start all processes and threads from other proc68 * so we (main pid) can return to user.69 */70 printerrors = 0;71 makeports(rules);72 startfsys();73 threadexits(nil);74 }76 void77 error(char *fmt, ...)78 {79 char buf[512];80 va_list args;82 va_start(args, fmt);83 vseprint(buf, buf+sizeof buf, fmt, args);84 va_end(args);86 fprint(2, "%s: %s\n", progname, buf);87 threadexitsall("error");88 }90 void91 parseerror(char *fmt, ...)92 {93 char buf[512];94 va_list args;96 va_start(args, fmt);97 vseprint(buf, buf+sizeof buf, fmt, args);98 va_end(args);100 if(printerrors){101 printinputstack();102 fprint(2, "%s\n", buf);103 }104 do; while(popinput());105 lasterror = estrdup(buf);106 longjmp(parsejmp, 1);107 }109 void*110 emalloc(long n)111 {112 void *p;114 p = malloc(n);115 if(p == nil)116 error("malloc failed: %r");117 memset(p, 0, n);118 return p;119 }121 void*122 erealloc(void *p, long n)123 {124 p = realloc(p, n);125 if(p == nil)126 error("realloc failed: %r");127 return p;128 }130 char*131 estrdup(char *s)132 {133 char *t;135 t = strdup(s);136 if(t == nil)137 error("estrdup failed: %r");138 return t;139 }