12 void Command_Interpreter::after_command
17 const std::string_view line,
18 const Exception *exception
23 std::ostringstream error;
24 error << exception->what();
25 error <<
"\nLine " << line_number <<
": " << line <<
'\n';
28 throw Exception(error.str());
30 out <<
"Exception caught: " << error.str();
33 out <<
"OK: " << line <<
'\n';
40 const std::string_view command,
41 std::istream ¶meters,
46 if (command.empty() || command[0] ==
'#')
49 else if (command ==
"about")
53 else if (command ==
"echo")
55 std::string parameter;
56 parameters >> parameter;
58 if (parameter ==
"on")
60 else if (parameter ==
"off")
63 else if (command ==
"prompt")
65 std::string parameter;
66 parameters >> parameter;
68 if (parameter ==
"on")
70 else if (parameter ==
"off")
73 else if (command ==
"help")
90 else if (command ==
"include")
92 const std::string file_name =
read_string(parameters);
93 std::ifstream ifs(file_name);
100 Recursion(
int &depth_init): depth(depth_init) {++depth;}
101 ~Recursion() {--depth;}
103 recursion(recursion_depth);
105 constexpr int max_recursion_depth = 42;
107 if (recursion_depth > max_recursion_depth)
108 throw Exception(
"Reached max recursion depth");
113 throw Exception(
"error opening " + file_name);
115 else if (command ==
"quit")
117 else if (command ==
"abort")
118 return Status::abort;
120 return Status::not_found;
136 processors.emplace_back(processor);
148 rethrow = parent->rethrow;
149 prompt = parent->prompt;
157 out << prompt_string;
168 parent->write_prompt(out);
181 int64_t line_number = 0;
190 if (!std::getline(in, line))
192 while (!line.empty() && isspace(line.back()))
195 last_line_empty = line.empty();
198 std::istringstream iss(line);
210 for (
const auto &processor: processors)
238 "Unknown command. For a list of available commands, try \"help\"."
242 after_command(out, line_number, line,
nullptr);
247 catch (
const Exception &e)
249 after_command(out, line_number, line, &e);
254 throw Exception(
"aborted");
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
void add_processor(Command_Processor &processor)
virtual void write_prompt(std::ostream &out) const
Status process_command(std::string_view command, std::istream ¶meters, std::istream &in, std::ostream &out) override
virtual Status process_command(std::string_view command, std::istream ¶meters, std::istream &in, std::ostream &out)=0
void about_joedb(std::ostream &out)
std::string read_string(std::istream &in)