
#include	"tron.h"
#include	"time.h"

#define	CTHRESH	120
#define	NCMD	  7

char	*cmess[] = {
	"init",
	"gett",
	"chgq",
	"cron",
	"echo",
	"logi",
	0
};

gcmd(t)
long	t;
{
	short	si, so, pv[2], pid, i;
	struct tm	*tp, *localtime();
	char ebuf[32], *err;

	t -= CTHRESH;
	tp = localtime(&t);
	sprintf(ebuf, "%02d%02d%02d%02d", tp->tm_mon, tp->tm_mday, tp->tm_hour,
		tp->tm_min);
	si = dup(0);
	so = dup(1);
	close(0);
	close(1);
	if(pipe(pv) < 0){
		err = "Pipe";
elp:
		dup(si);
		dup(so);
		close(si);
		close(so);
		fprintf(stdout, "**TRON: %s creat error for 'lc' - Time: %s",
			err, ctime(&t));
		return(1);
	}
	if((pid = fork()) == -1){
		err = "Fork";
		close(0);
		close(1);
		goto elp;
	}
	signal(2,1);
	signal(3,1);
	if(pid == 0){
		close(0);
		dup(si);
		signal(2,0);
		signal(3,0);
		execl("/staff/john/bin/lc", "lc", "-g", "0", "-g", "2", "-",
			ebuf, 0);
		fprintf(stdout, "*TRON: lc - %s has failed\n", ebuf);
		fflush(stdout);
		exit(1);
	}
	close(1);
	dup(so);
	close(so);
	so = 0;
	while((so = fgets(ebuf, 63, stdin)) && strncmp(ebuf, "Log", 3));
	if(so == NULL){
		fprintf(stdout, "*TRON: Not enough lines from 'lc' - %s",ctime(&t));
		goto finish;
	}
	for(so = 0; so < NCMD && fgets(ebuf, 63, stdin); so++){
		for(i = 0; cmess[i]; i++)
			if(strncmp(cmess[i], ebuf+14, 4) == 0)break;
		if(cmess[i])continue;
		fprintf(stdout, "%s", ebuf);
	}
	if(so != NCMD)
		fprintf(stdout, "*TRON: Not enough commands at %s", ctime(&t));
finish:
	fprintf(stdout, "\n");
	kill(pid, 9);
	while((so = wait(0)) != pid && so != -1);
	close(0);
	dup(si);
	close(si);
	return(0);
}

