+/* $Id$
+
+This file is part of gldbg
+Copyright © 2009 Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#include <msp/core/except.h>
+#include <msp/io/file.h>
+#include <msp/io/print.h>
+#include <msp/strings/lexicalcast.h>
+#include "commandinterpreter.h"
+#include "gldbg.h"
+#include "tracer.h"
+
+using namespace std;
+using namespace Msp;
+
+CommandInterpreter::CommandInterpreter(GlDbg &d):
+ gldbg(d)
+{
+ commands["run"] = &CommandInterpreter::cmd_run;
+ commands["continue"] = &CommandInterpreter::cmd_continue;
+ commands["c"] = &CommandInterpreter::cmd_continue;
+ commands["kill"] = &CommandInterpreter::cmd_kill;
+ commands["signal"] = &CommandInterpreter::cmd_signal;
+ commands["exit"] = &CommandInterpreter::cmd_exit;
+ commands["quit"] = &CommandInterpreter::cmd_exit;
+ commands["trace"] = &CommandInterpreter::cmd_trace;
+}
+
+void CommandInterpreter::execute(const string &cmd)
+{
+ unsigned space = cmd.find(' ');
+ string name = cmd.substr(0, space);
+ CommandMap::const_iterator i = commands.lower_bound(name);
+ if(i==commands.end() || i->first.compare(0, name.size(), name))
+ throw KeyError("Unknown command", name);
+ if(i->first!=name)
+ {
+ CommandMap::const_iterator j = i;
+ if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
+ throw KeyError("Ambiguous command", name);
+ }
+
+ string args;
+ if(space!=string::npos)
+ args = cmd.substr(space+1);
+
+ (this->*(i->second))(args);
+}
+
+
+void CommandInterpreter::cmd_run(const string &)
+{
+ gldbg.launch();
+}
+
+void CommandInterpreter::cmd_continue(const string &)
+{
+ gldbg.get_process().resume();
+}
+
+void CommandInterpreter::cmd_signal(const string &args)
+{
+ gldbg.get_process().resume(lexical_cast<unsigned>(args));
+}
+
+void CommandInterpreter::cmd_kill(const string &)
+{
+ gldbg.get_process().kill();
+}
+
+void CommandInterpreter::cmd_exit(const string &)
+{
+ gldbg.quit();
+}
+
+void CommandInterpreter::cmd_trace(const string &args)
+{
+ Tracer &tracer = gldbg.get_tracer();
+ if(args[0]=='>')
+ {
+ string fn = args.substr(1);
+ if(fn=="-")
+ {
+ tracer.set_output(IO::cout);
+ IO::print("Tracing to stdout\n");
+ }
+ else
+ {
+ tracer.set_output(new IO::File(fn, IO::M_WRITE));
+ IO::print("Tracing to %s\n", fn);
+ }
+ }
+ else
+ {
+ if(args=="on")
+ {
+ tracer.enable();
+ IO::print("Tracing enabled\n");
+ }
+ else if(args=="off")
+ {
+ tracer.disable();
+ IO::print("Tracing disabled\n");
+ }
+ else if(args=="end")
+ {
+ tracer.set_output(0);
+ IO::print("Tracing terminated\n");
+ }
+ }
+}