factor out shell command execution

This commit is contained in:
Martin Michelsen
2025-01-06 00:11:53 -08:00
parent d51f7a0fe7
commit 0be056adce
3 changed files with 18 additions and 11 deletions
+15 -10
View File
@@ -17,7 +17,7 @@ using namespace std;
struct CommandArgs {
shared_ptr<ServerState> s;
shared_ptr<ServerShell> shell;
ServerShell* shell;
string command;
string args;
string session_name;
@@ -107,17 +107,10 @@ void ServerShell::thread_fn() {
}
phosg::strip_trailing_whitespace(command);
phosg::strip_leading_whitespace(command);
try {
// Find the entry in the command table and run the command
size_t command_end = phosg::skip_non_whitespace(command, 0);
size_t args_begin = phosg::skip_whitespace(command, command_end);
CommandArgs args;
args.s = this->state;
args.shell = this->shared_from_this();
args.command = command.substr(0, command_end);
args.args = command.substr(args_begin);
CommandDefinition::dispatch(args);
this->execute_command(command);
} catch (const exit_shell&) {
event_base_loopexit(this->state->base.get(), nullptr);
return;
@@ -127,6 +120,18 @@ void ServerShell::thread_fn() {
}
}
void ServerShell::execute_command(const string& command) {
// Find the entry in the command table and run the command
size_t command_end = phosg::skip_non_whitespace(command, 0);
size_t args_begin = phosg::skip_whitespace(command, command_end);
CommandArgs args;
args.s = this->state;
args.shell = this;
args.command = command.substr(0, command_end);
args.args = command.substr(args_begin);
CommandDefinition::dispatch(args);
}
shared_ptr<ProxyServer::LinkedSession> ServerShell::get_proxy_session(const string& name) {
if (!this->state->proxy_server.get()) {
throw runtime_error("the proxy server is disabled");