// idbg - IJVM debugger.
// Copyright 2000 Tom Rothamel <tom-idbg@onegeek.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  

#include <malloc.h>
#include "idbg.h"

FILE *ijvmf = NULL;
VM *vm = NULL;
int pp_quick = 0;
int pp_full = 0;
int pp_disasm = 1;

void reinit() {
	if (vm) delete vm;
	vm = new VM;

	rewind(ijvmf);
	vm->load(ijvmf);

	curses_erase();
}

void quit(int rv) {
	exit(rv);
}

int main(int argc, char **argv) {
	char *in;
	char *oldin = NULL;
	int optind = 1;
	FILE *idebugf;
	char prompt[128];
	
	ijvmf = fopen(argv[optind], "r");
	if (!ijvmf) {
		oprintf("Couldn't open ijvm file '%s.'\n", argv[optind]);
		exit(-1);
	}

	if (argc > optind+1) {
		idebugf = fopen(argv[optind + 1], "r");
		if (!idebugf) {
			oprintf("Couldn't open debugging symbols file '%s'.\n", argv[optind + 1]);
			exit(-1);
		}

		read_symbols(idebugf);
		fclose(idebugf);
	}

	curses_init();
	reinit();

	curses_hide();
	if (pp_quick) quick_prompt();
	if (pp_full) full_prompt();
	if (pp_disasm) disasm(vm->regs->pc, 1);

	snprintf(prompt, 128, "idbg> ");
	
	while (in = readline(prompt)) {
		if (!*in) {
			in = oldin;
			if (!in) continue;
		} else {
			if (oldin) free(oldin);
			oldin = strdup(in);
		}

		add_history(in);

		oreset();		
		parse_string(in);
		
		curses_hide();
		snprintf(prompt, 128, "idbg (%s)> ", oldin);

		oprintf("\n");
		if (pp_quick) quick_prompt();
		if (pp_full) full_prompt();
		if (pp_disasm) disasm(vm->regs->pc, 1);

	}
}

	

