]> git.tdb.fi Git - gldbg.git/blob - source/commandinterpreter.cpp
71e8ee7a001b7eafc14b1ec4f014ff47a48bac84
[gldbg.git] / source / commandinterpreter.cpp
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <msp/core/except.h>
9 #include <msp/io/file.h>
10 #include <msp/io/print.h>
11 #include <msp/strings/lexicalcast.h>
12 #include "commandinterpreter.h"
13 #include "gldbg.h"
14 #include "tracer.h"
15
16 using namespace std;
17 using namespace Msp;
18
19 CommandInterpreter::CommandInterpreter(GlDbg &d):
20         gldbg(d)
21 {
22         commands["run"] = &CommandInterpreter::cmd_run;
23         commands["continue"] = &CommandInterpreter::cmd_continue;
24         commands["c"] = &CommandInterpreter::cmd_continue;
25         commands["kill"] = &CommandInterpreter::cmd_kill;
26         commands["signal"] = &CommandInterpreter::cmd_signal;
27         commands["exit"] = &CommandInterpreter::cmd_exit;
28         commands["quit"] = &CommandInterpreter::cmd_exit;
29         commands["trace"] = &CommandInterpreter::cmd_trace;
30 }
31
32 void CommandInterpreter::execute(const string &cmd)
33 {
34         unsigned space = cmd.find(' ');
35         string name = cmd.substr(0, space);
36         CommandMap::const_iterator i = commands.lower_bound(name);
37         if(i==commands.end() || i->first.compare(0, name.size(), name))
38                 throw KeyError("Unknown command", name);
39         if(i->first!=name)
40         {
41                 CommandMap::const_iterator j = i;
42                 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
43                         throw KeyError("Ambiguous command", name);
44         }
45
46         string args;
47         if(space!=string::npos)
48                 args = cmd.substr(space+1);
49
50         (this->*(i->second))(args);
51 }
52
53
54 void CommandInterpreter::cmd_run(const string &)
55 {
56         gldbg.launch();
57 }
58
59 void CommandInterpreter::cmd_continue(const string &)
60 {
61         gldbg.get_process().resume();
62 }
63
64 void CommandInterpreter::cmd_signal(const string &args)
65 {
66         gldbg.get_process().resume(lexical_cast<unsigned>(args));
67 }
68
69 void CommandInterpreter::cmd_kill(const string &)
70 {
71         gldbg.get_process().kill();
72 }
73
74 void CommandInterpreter::cmd_exit(const string &)
75 {
76         gldbg.quit();
77 }
78
79 void CommandInterpreter::cmd_trace(const string &args)
80 {
81         Tracer &tracer = gldbg.get_tracer();
82         if(args[0]=='>')
83         {
84                 string fn = args.substr(1);
85                 if(fn=="-")
86                 {
87                         tracer.set_output(IO::cout);
88                         IO::print("Tracing to stdout\n");
89                 }
90                 else
91                 {
92                         tracer.set_output(new IO::File(fn, IO::M_WRITE));
93                         IO::print("Tracing to %s\n", fn);
94                 }
95         }
96         else
97         {
98                 if(args=="on")
99                 {
100                         tracer.enable();
101                         IO::print("Tracing enabled\n");
102                 }
103                 else if(args=="off")
104                 {
105                         tracer.disable();
106                         IO::print("Tracing disabled\n");
107                 }
108                 else if(args=="end")
109                 {
110                         tracer.set_output(0);
111                         IO::print("Tracing terminated\n");
112                 }
113         }
114 }