~pixelherodev/DSM-9

78cc8aad696417ef2816c326afbbc6e5cb37b436 — Noam Preil a month ago d280627
[cli] improvements
1 files changed, 49 insertions(+), 17 deletions(-)

M cli.c
M cli.c => cli.c +49 -17
@@ 9,8 9,9 @@
#include "sm2.h"

struct sm2_instance instance;
unsigned long max_cards;
int html;
ulong max_cards, start;
int html, all;
int offset;

void
parse_arguments(int argc, char **argv)


@@ 19,10 20,38 @@ parse_arguments(int argc, char **argv)
	float dt;
	long count;
	for (i = 1; i < argc; i += 1){
		if (strcmp(argv[i], "-a") == 0){
			all = 1;
			continue;
		}
		if (strcmp(argv[i], "-h") == 0){
			html = 1;
			continue;
		}
		if (strcmp(argv[i], "-s") == 0){
			i += 1;
			if(i == argc)
				sysfatal("Expected argument to -s!");
			start = atol(argv[i]);
			continue;
		}
		if (strcmp(argv[i], "-j") == 0){
			i += 1;
			if(i == argc)
				sysfatal("Expected argument to -j!");
			offset = atol(argv[i]);
			continue;
		}
		if (strcmp(argv[i], "-m") == 0){
			i += 1;
			if(i == argc)
				sysfatal("Expected argument to -m!");
			max_cards = atol(argv[i]);
			// If zero, wrap around to max ulong
			if(max_cards == 0)
				max_cards -= 1;
			continue;
		}
		delta();
		count = sm2_load(&instance, argv[i], 0);
		dt = delta();


@@ 45,7 74,10 @@ parse_arguments(int argc, char **argv)
void
default_options(void)
{
	start = 0;
	max_cards = 12;
	offset = 0;
	all = 0;
	html = 0;
}



@@ 79,29 111,22 @@ render_inner(char *msg, int html)
	if (html == 0){
		execl("/bin/rc", "rc", "-c", "/bin/eqn /tmp/dsm9 | /bin/troff  > /tmp/dsm9-2 && /bin/mv /tmp/dsm9-2 /tmp/dsm9 && exit", nil);
	}
	exits(nil);
}

void
render(char *msg, int html)
{
	if (fork() == 0) {
		// child
	// Render in a child so that we can execl()
	// TODO: switch to libthread
	if(fork() == 0)
		render_inner(msg, html);
	}
	else {
		// parent
		Waitmsg *msg = wait();
		if (msg->msg != nil && msg->msg[0] != 0){
			sysfatal("Rendering failed: %s!",msg->msg);
		}
		free(msg);
	}
	int p = plumbopen("send", OWRITE);
	if (p < 0){
		sysfatal("Failed to open plumber!");
	}
	if (html == 1)
		plumbsendtext(p, "dsm9", "web", "/tmp", "file:/tmp/dsm9.html");
		plumbsendtext(p, "dsm9", "web", "/tmp", "file:///tmp/dsm9.html");
	else
		plumbsendtext(p, "dsm9", "postscript", "/tmp", "/tmp/dsm9");
	close(p);


@@ 145,18 170,25 @@ main(int argc, char **argv)
	default_options();
	ulong count;
	sm2_init(&instance);
	pin(0);
	parse_arguments(argc, argv);
	delta();
	cards = h_find_cards(&instance, nsec() / 1000000000, &count);
	if(all){
		cards = instance.cards;
		count = instance.card_count;
	}else
		cards = h_find_cards(&instance, nsec() / 1000000000 + offset, &count);
	dt = delta();
	fprint(1, "Found %lud due cards in %fs\n", count, dt);
	if(count != 0){	
		delta();
		repetition_sort(cards, count);
		if(!all)
			repetition_sort(cards, count);
		dt = delta();
		fprint(1, "Repetition-sorted %lud cards in %fs\n", count, dt);
		count = count < max_cards ? count : max_cards;
		for(unsigned long i = 0; i < count; i += 1)
		fprint(1, "Testing %lud cards\n", count);
		for(unsigned long i = start; i < count + start; i += 1)
			card_prompt(cards[i]);
	}
}