Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Command_Interpreter.cpp
Go to the documentation of this file.
3
4#include <sstream>
5
6namespace joedb
7{
8 ////////////////////////////////////////////////////////////////////////////
9 void Command_Interpreter::after_command
10 ////////////////////////////////////////////////////////////////////////////
11 (
12 std::ostream &out,
13 int64_t line_number,
14 const std::string &line,
15 const Exception *exception
16 ) const
17 {
18 if (exception)
19 {
20 std::ostringstream error;
21 error << exception->what();
22 error << "\nLine " << line_number << ": " << line << '\n';
23
24 if (rethrow)
25 throw Exception(error.str());
26 else
27 out << "Exception caught: " << error.str();
28 }
29 else if (echo)
30 out << "OK: " << line << '\n';
31 }
32
33 ////////////////////////////////////////////////////////////////////////////
35 ////////////////////////////////////////////////////////////////////////////
36 (
37 const std::string &command,
38 std::istream &parameters,
39 std::istream &in,
40 std::ostream &out
41 )
42 {
43 if (command.empty() || command[0] == '#') /////////////////////////////////
44 {
45 }
46 else if (command == "about") //////////////////////////////////////////////
47 {
48 about_joedb(out);
49 }
50 else if (command == "echo") ///////////////////////////////////////////////
51 {
52 std::string parameter;
53 parameters >> parameter;
54
55 if (parameter == "on")
56 set_echo(true);
57 else if (parameter == "off")
58 set_echo(false);
59 }
60 else if (command == "prompt") /////////////////////////////////////////////
61 {
62 std::string parameter;
63 parameters >> parameter;
64
65 if (parameter == "on")
66 set_prompt(true);
67 else if (parameter == "off")
68 set_prompt(false);
69 }
70 else if (command == "help") ///////////////////////////////////////////////
71 {
72 out << R"RRR(
73General commands
74~~~~~~~~~~~~~~~~
75 about
76 help|?
77 quit
78 abort
79 echo on|off
80 prompt on|off
81
82)RRR";
83
84 return Status::ok;
85 }
86 else if (command == "quit") ///////////////////////////////////////////////
87 return Status::quit;
88 else if (command == "abort") //////////////////////////////////////////////
89 return Status::abort;
90 else
91 return Status::not_found;
92
93 return Status::done;
94 }
95
96 ////////////////////////////////////////////////////////////////////////////
98 ////////////////////////////////////////////////////////////////////////////
99 {
100 add_processor(*static_cast<Command_Processor *>(this));
102
103 ////////////////////////////////////////////////////////////////////////////
105 ////////////////////////////////////////////////////////////////////////////
106 {
107 processors.emplace_back(processor);
108 }
109
110 ////////////////////////////////////////////////////////////////////////////
111 void Command_Interpreter::set_parent(const Command_Interpreter *new_parent)
112 ////////////////////////////////////////////////////////////////////////////
113 {
114 parent = new_parent;
116 if (parent)
117 {
118 echo = parent->echo;
119 rethrow = parent->rethrow;
120 prompt = parent->prompt;
121 }
123
124 ////////////////////////////////////////////////////////////////////////////
125 void Command_Interpreter::write_prompt(std::ostream &out) const
126 ////////////////////////////////////////////////////////////////////////////
127 {
128 out << "joedbi";
129 }
130
131 ////////////////////////////////////////////////////////////////////////////
132 void Command_Interpreter::write_whole_prompt(std::ostream &out) const
133 ////////////////////////////////////////////////////////////////////////////
134 {
135 if (parent)
136 {
137 parent->write_prompt(out);
138 out << '/';
139 }
140
141 write_prompt(out);
142 out << "> ";
143 out.flush();
144 }
145
146 ////////////////////////////////////////////////////////////////////////////
147 void Command_Interpreter::main_loop(std::istream &in, std::ostream &out)
148 ////////////////////////////////////////////////////////////////////////////
149 {
150 int64_t line_number = 0;
151 bool abort = false;
152
153 while(true)
154 {
155 if (prompt)
157
158 std::string line;
159 if (!std::getline(in, line))
160 break;
161
162 line_number++;
163 std::istringstream iss(line);
164 std::string command;
165 iss >> command;
166
167 if (command == "?")
168 command = "help";
169
170 try
171 {
172 bool found = false;
173 bool quit = false;
174
175 for (const auto &processor: processors)
176 {
177 const Command_Processor::Status status =
178 processor.get().process_command(command, iss, in, out);
179
181 found = true;
182
184 {
185 quit = true;
186 break;
187 }
188
190 {
191 abort = true;
192 break;
193 }
194
196 break;
197 }
198
199 if (!found)
200 {
201 throw Exception
202 (
203 "Unknown command. For a list of available commands, try \"help\"."
204 );
205 }
206
207 after_command(out, line_number, line, nullptr);
208
209 if (quit || abort)
210 break;
211 }
212 catch (const Exception &e)
213 {
214 after_command(out, line_number, line, &e);
215 }
216 }
217
218 if (abort)
219 throw Exception("aborted");
220 }
221}
void set_parent(const Command_Interpreter *new_parent)
void main_loop(std::istream &in, std::ostream &out)
void write_whole_prompt(std::ostream &out) const
Status process_command(const std::string &command, std::istream &parameters, std::istream &in, std::ostream &out) override
void add_processor(Command_Processor &processor)
virtual void write_prompt(std::ostream &out) const
void about_joedb(std::ostream &out)
Definition Blob.h:7