hlfw.ca

drawcpu

ref: 4f64a0294ee182f54c5ec341ab13163f43038af0
dir: /main.c/

View raw version
#include "u.h"
#include "lib.h"
#include "kern/dat.h"
#include "kern/fns.h"
#include "user.h"
#include "drawcpu.h"
#include "arg.h"

char *argv0;
char *dbgfile = "./debug.log";
int debug;

void
sizebug(void)
{
	/*
	 * Needed by various parts of the code.
	 * This is a huge bug.
	 */
	assert(sizeof(char)==1);
	assert(sizeof(short)==2);
	assert(sizeof(ushort)==2);
	assert(sizeof(int)==4);
	assert(sizeof(uint)==4);
	assert(sizeof(long)==4);
	assert(sizeof(ulong)==4);
	assert(sizeof(vlong)==8);
	assert(sizeof(uvlong)==8);
}

// TODO: remove libgui, or at least revamp as cpubody goes away
void cpubody(void) {}

char*
estrdup(char *s)
{
	s = strdup(s);
	if(s == nil)
		sysfatal("out of memory");
    return s;
}


static void
usage(void)
{
	fprintf(stderr, "usage: drawcpu [-D]\n");
	exit(1);
}


int
main(int argc, char **argv)
{
	extern ulong kerndate;
	int ifd, ofd,efd, dfd;

	debug = 0;
	kerndate = seconds();
	eve = getuser();
	if(eve == nil)
		eve = "drawcpu";

	ARGBEGIN {
	case 'D':
		debug++;
		break;
	default:
		usage();
	} ARGEND;

	sizebug();
	osinit();
	procinit0();
	printinit();

	chandevreset();
	chandevinit();
	quotefmtinstall();

	if(bind("#c", "/dev", MBEFORE) < 0)
		panic("bind #c: %r");
	if(bind("#e", "/env", MREPL|MCREATE) < 0)
		panic("bind #e: %r");
	if(bind("#I", "/net", MBEFORE) < 0)
		panic("bind #I: %r");
	if(bind("#U", "/root", MREPL|MCREATE) < 0)
		panic("bind #U: %r");
	bind("#C", "/", MAFTER);
	//bind("#F", "/fd", MBEFORE);
	//bind("#P", "/proc", MBEFORE);

	/* We have to be a bit pedantic about the fds, leave 0, 1, 2 alone */
	ifd = open("/dev/null", OREAD);
	ofd = open("/dev/null", OWRITE);
	efd = open("/dev/null", OWRITE);
	if(debug)
		dfd = create("./error.log", OWRITE, 0644);
	else
		dfd = open("/dev/null", OWRITE);

	close(ifd);
	close(ofd);
	close(efd);

	if(bind("/root", "/", MAFTER) < 0)
		panic("bind /root: %r");

	char *cmd[] = {
		"drawcpu",
		"-c",
		". <{n=`{read} && ! ~ $#n 0 && read -c $n} >[2=1]"	
	};

	rc(3, cmd, dfd);
	_exit(0);
}