diff --git a/src/ServerShell.cc b/src/ServerShell.cc index ea4d327f..fd31f9d4 100644 --- a/src/ServerShell.cc +++ b/src/ServerShell.cc @@ -17,7 +17,7 @@ using namespace std; struct CommandArgs { shared_ptr s; - shared_ptr 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 ServerShell::get_proxy_session(const string& name) { if (!this->state->proxy_server.get()) { throw runtime_error("the proxy server is disabled"); diff --git a/src/ServerShell.hh b/src/ServerShell.hh index 457ceda0..dd57d045 100644 --- a/src/ServerShell.hh +++ b/src/ServerShell.hh @@ -25,6 +25,8 @@ public: std::shared_ptr get_proxy_session(const std::string& name); + void execute_command(const std::string& command); + protected: std::shared_ptr state; std::thread th; diff --git a/src/ServerState.hh b/src/ServerState.hh index 11238adf..cca04e6b 100644 --- a/src/ServerState.hh +++ b/src/ServerState.hh @@ -383,7 +383,7 @@ struct ServerState : public std::enable_shared_from_this { std::vector parse_port_configuration(const phosg::JSON& json) const; template - inline void call_on_event_thread(std::function&& fn) { + inline T call_on_event_thread(std::function&& fn) { return ::call_on_event_thread(this->base, std::move(fn)); } inline void forward_to_event_thread(std::function&& fn) {