#include "sam.h"

Rune	samname[] = { '~', '~', 's', 'a', 'm', '~', '~', '\0' };

Rune left0[]  = { '{', '[', '(', '<', 0xAB, '\0' };
Rune right0[] = { '}', ']', ')', '>', 0xBB, '\0' };
Rune delim1[] = { '\n', '\0' };
Rune delim2[] = { '"', '\'', '\0' };

Rune *left[]= {
	left0,
	delim1,
	delim2,
	0
};
Rune *right[]= {
	right0,
	delim1,
	delim2,
	0
};

char	RSAM[] = "sam";
char	SAMTERM[] = "/bin/aux/samterm";
char	HOME[] = "home";
char	TMPDIR[] = "/tmp";
char	SH[] = "rc";
char	SHPATH[] = "/bin/rc";
char	RX[] = "rx";
char	RXPATH[] = "/bin/rx";
char	SAMSAVECMD[] = "/bin/rc\n/sys/lib/samsave";

Channel *wchan;

void
dprint(char *z, ...)
{
	char buf[BLOCKSIZE];
	va_list arg;

	va_start(arg, z);
	doprint(buf, &buf[BLOCKSIZE], z, arg);
	va_end(arg);
	termwrite(buf);
}

void
print_ss(char *s, String *a, String *b)
{
	dprint("?warning: %s: `%.*S' and `%.*S'\n", s, a->n, a->s, b->n, b->s);
}

void
print_s(char *s, String *a)
{
	dprint("?warning: %s `%.*S'\n", s, a->n, a->s);
}

char*
getuser(void)
{
	static char user[64];
	int fd;

	if(user[0] == 0){
		fd = open("/dev/user", 0);
		if(fd<0 || read(fd, user, sizeof user-1)<=0)
			strcpy(user, "none");
		close(fd);
	}
	return user;
}

int
statfile(char *name, ulong *dev, uvlong *id, long *time, long *length, long *appendonly)
{
	Dir *dirb;

	dirb = dirstat(name);
	if(dirb == nil)
		return -1;
	if(dev)
		*dev = dirb->type|(dirb->dev<<16);
	if(id)
		*id = dirb->qid.path;
	if(time)
		*time = dirb->mtime;
	if(length)
		*length = dirb->length;
	if(appendonly)
		*appendonly = dirb->mode & DMAPPEND;
	free(dirb);
	return 1;
}

int
statfd(int fd, ulong *dev, uvlong *id, long *time, long *length, long *appendonly)
{
	Dir *dirb;

	dirb = dirfstat(fd);
	if(dirb == nil)
		return -1;
	if(dev)
		*dev = dirb->type|(dirb->dev<<16);
	if(id)
		*id = dirb->qid.path;
	if(time)
		*time = dirb->mtime;
	if(length)
		*length = dirb->length;
	if(appendonly)
		*appendonly = dirb->mode & DMAPPEND;
	free(dirb);
	return 1;
}

void
notifyf(void *a, char *s)
{
	USED(a);
	if(bpipeok && strcmp(s, "sys: write on closed pipe") == 0)
//		noted(NCONT);
	if(strcmp(s, "interrupt") == 0)
//		noted(NCONT);
	panicking = 1;
	rescue();
//	noted(NDFLT);
}

int
newtmp(int num)
{
	int i, fd;
	static char	tempnam[30];

	i = getpid();
	do
		snprint(tempnam, sizeof tempnam, "%s/%d%.4s%dsam", TMPDIR, num, getuser(), i++);
	while(access(tempnam, 0) == 0);
	fd = create(tempnam, ORDWR|OCEXEC|ORCLOSE, 0000);
	if(fd < 0){
		remove(tempnam);
		fd = create(tempnam, ORDWR|OCEXEC|ORCLOSE, 0000);
	}
	return fd;
}

int
waitfor(int pid)
{
	int msg;
	Waitmsg *w;

	while((w = recvp(wchan)) != nil){
		if(w->pid != pid){
			free(w);
			continue;
		}
		msg = (w->msg[0] != '\0');
		free(w);
		return msg;
	}
	return -1;
}

void
samerr(char *buf)
{
	sprint(buf, "%s/sam.err", TMPDIR);
}

void*
emalloc(ulong n)
{
	void *p;

	p = malloc(n);
	if(p == 0)
		panic("malloc fails");
	memset(p, 0, n);
	return p;
}

void*
erealloc(void *p, ulong n)
{
	p = realloc(p, n);
	if(p == 0)
		panic("realloc fails");
	return p;
}

void
sysinit(void)
{
	wchan = threadwaitchan();
}

int	remotefd0 = 0;
int	remotefd1 = 1;

static void
procjump(void *arg)
{
	int i;
	struct { char **argv; int fd[3]; Channel *pidc; } *a;

	a = arg;
	rfork(RFFDG);
	for(i=0; i<3; i++)
		if(a->fd[i] != i)
			dup(a->fd[i], i);
	procexec(a->pidc, a->argv[0], a->argv);
}

static int
proc(int dofork, char **argv, int fd0, int fd1, int fd2)
{
	int pid;
	struct { char **argv; int fd[3]; Channel *pidc; } a;

	a.argv = argv;
	a.fd[0] = fd0;
	a.fd[1] = fd1;
	a.fd[2] = fd2;

	if(dofork){
		a.pidc = chancreate(sizeof(ulong), 0);
		proccreate(procjump, &a, 8192);
		pid = recvul(a.pidc);
		chanfree(a.pidc);
		if(pid == -1)
			return 0;
		return pid;
	}else{
		a.pidc = nil;
		procjump(&a);
		threadexits(nil);
		return -1;	/* not reached */
	}
}

void
bootterm(char *machine, char **argv, char **end)
{
	int ph2t[2], pt2h[2];
	char *ae;

	argv[0] = samterm;
	ae = *end;
	*end = 0;

	if(machine) {
		if(proc(0, argv, 0, 1, 2) == 0)
			panic("proc failed");
		threadexits(0);
	}

	if(pipe(ph2t)==-1 || pipe(pt2h)==-1)
		panic("pipe");

	if(proc(1, argv, ph2t[0], pt2h[1], 2)==0)
		panic("proc failed");
	*end = ae;
	dup(pt2h[0], 0);
	dup(ph2t[1], 1);
	close(ph2t[0]);
	close(ph2t[1]);
	close(pt2h[0]);
	close(pt2h[1]);
}

void
connectto(char *machine)
{
	int p1[2], p2[2];
	static char *argv[6];
	char buf[1];

	if(pipe(p1)<0 || pipe(p2)<0){
		dprint("can't pipe\n");
		threadexits("pipe");
	}
	argv[0] = RXPATH;
	argv[1] = machine;
	argv[2] = rsamname;
	argv[3] = "-R";
	argv[4] = 0;

	if(proc(1, argv, p2[0], p1[1], 2)==0)
		panic("proc failed");

	dup(p1[0], 0);
	dup(p2[1], 1);
	close(p1[0]);
	close(p1[1]);
	close(p2[0]);
	close(p2[1]);

//	if(read(0, buf, 1) != 1 || buf[0] != 'o') {
//		exits(0); // RX will display an error message
//	}
}

void
startup(char *machine, int Rflag, char **argv, char **end)
{
	if(machine)
		connectto(machine);
	if(!Rflag)
		bootterm(machine, argv, end);
	downloaded = 1;
	outTs(Hversion, VERSION);
}
