plan9port/src/cmd/plumb/plumber.c

142 lines
2.1 KiB
C
Raw Normal View History

2003-11-23 17:58:26 +00:00
#include <u.h>
#include <libc.h>
#include <regexp.h>
#include <thread.h>
#include <plumb.h>
#include <fcall.h>
#include "plumber.h"
int debug;
2003-11-23 17:58:26 +00:00
char *plumbfile;
char *user;
char *home;
char *progname;
Ruleset **rules;
int printerrors=1;
jmp_buf parsejmp;
char *lasterror;
void
makeports(Ruleset *rules[])
{
int i;
for(i=0; rules[i]; i++)
addport(rules[i]->port);
}
void
threadmain(int argc, char *argv[])
{
char buf[512];
2004-12-28 17:34:05 +00:00
int fd;
2003-11-23 17:58:26 +00:00
progname = "plumber";
ARGBEGIN{
case 'd':
debug = 1;
break;
2003-11-23 17:58:26 +00:00
case 'p':
plumbfile = ARGF();
break;
}ARGEND
user = getuser();
2003-11-23 17:58:26 +00:00
home = getenv("home");
if(home == nil)
home = getenv("HOME");
2003-11-23 17:58:26 +00:00
if(user==nil || home==nil)
error("can't initialize $user or $home: %r");
if(plumbfile == nil){
sprint(buf, "%s/lib/plumbing", home);
2004-03-25 23:03:57 +00:00
if(access(buf, 0) >= 0)
plumbfile = estrdup(buf);
else
plumbfile = unsharp("#9/plumb/initial.plumbing");
2003-11-23 17:58:26 +00:00
}
fd = open(plumbfile, OREAD);
if(fd < 0)
error("can't open rules file %s: %r", plumbfile);
if(setjmp(parsejmp))
error("parse error");
rules = readrules(plumbfile, fd);
close(fd);
2004-12-28 23:20:54 +00:00
/*
* Start all processes and threads from other proc
* so we (main pid) can return to user.
*/
2005-01-04 22:17:10 +00:00
printerrors = 0;
makeports(rules);
startfsys();
2004-12-28 23:20:54 +00:00
threadexits(nil);
2003-11-23 17:58:26 +00:00
}
void
error(char *fmt, ...)
{
char buf[512];
va_list args;
va_start(args, fmt);
vseprint(buf, buf+sizeof buf, fmt, args);
va_end(args);
fprint(2, "%s: %s\n", progname, buf);
threadexitsall("error");
}
void
parseerror(char *fmt, ...)
{
char buf[512];
va_list args;
va_start(args, fmt);
vseprint(buf, buf+sizeof buf, fmt, args);
va_end(args);
if(printerrors){
printinputstack();
fprint(2, "%s\n", buf);
}
do; while(popinput());
lasterror = estrdup(buf);
longjmp(parsejmp, 1);
}
void*
emalloc(long n)
{
void *p;
p = malloc(n);
if(p == nil)
error("malloc failed: %r");
memset(p, 0, n);
return p;
}
void*
erealloc(void *p, long n)
{
p = realloc(p, n);
if(p == nil)
error("realloc failed: %r");
return p;
}
char*
estrdup(char *s)
{
char *t;
t = strdup(s);
if(t == nil)
error("estrdup failed: %r");
return t;
}