#include <stdlib.h>
#include <stdio.h>

#include "compiler.h"


#define	BUF_SIZE	1000000


static void report(const char *s)
{
	fprintf(stderr, "%s\n", s);
}


static void usage(const char *name)
{
	fprintf(stderr, "usage: %s patch-file [loops]\n", name);
	exit(1);
}


int main(int argc, char **argv)
{
	char buf[BUF_SIZE];
	const char *name;
	FILE *file;
	size_t got;
	int loops = 1;
	int i;

	switch (argc) {
	case 2:
		break;
	case 3:
		loops = atoi(argv[2]);
		break;
	default:
		usage(*argv);
	}

	name = argv[1];
	file = fopen(name, "r");
	if (!file) {
		perror(name);
		exit(1);
	}
	got = fread(buf, 1, sizeof(buf)-1, file);
	if (got < 0) {
		perror(name);
		exit(1);
	}
	buf[got] = 0;
	fclose(file);

	for (i = 0; i != loops; i++)
		if (!patch_compile(buf, report))
			return 1;

	return 0;
}
