// 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 "idbg.h"
#include <signal.h>

int got_sigint = 0;

int execute(int n) {
	got_sigint = 0;
	Breakpoint *b = NULL;
	
	while (n && !vm->err && !got_sigint) {
		if (n > 0) n--;

		if (!vm->decode(vm->regs->pc)) break;
		curses_show();
		if (!vm->execute()) break;

		b = lookup_breakpoint(vm->regs->pc);
		if (b) break;
	}

	if (vm->err) oprintf("%s\n", vm->err);
	if (b) oprintf("Stopped at breakpoint.\n");
	if (got_sigint) oprintf("Interrupted by user.\n");

	if (got_sigint) return 0;
	if (vm->err) return 0;
	if (b) return 0;

	return 1;	
}

void run() {
	reinit();
	execute(-1);
}

void go() {
	execute(-1);
}

/// next
///
/// The next command cause idbg to advance to the next statement, skipping
/// over intermediate invocations of subroutines via IVIRTUAL.
void next() {
	int depth = vm->depth;

	while (1) {
		if (!execute(1)) break;
		if (vm->depth == depth) break;
	}
}
