From 5543a4d58cb5ac0b052221e063df584f087b6924 Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Tue, 3 Jul 2018 14:19:12 +0300 Subject: [PATCH] src: Propagate to clients the `kill` exit code Fixes #2177 --- src/commands.cc | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index f6ec6ad5..640fea90 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -434,30 +434,44 @@ static void ensure_all_buffers_are_saved() throw runtime_error(message); } +template +void kill(const ParametersParser& parser, Context& context, const ShellContext&) +{ + auto& client_manager = ClientManager::instance(); + + if (not force and client_manager.count() == 1) + ensure_all_buffers_are_saved(); + + const int status = parser.positional_count() > 0 ? str_to_int(parser[0]) : 0; + while (not client_manager.empty()) + client_manager.remove_client(**client_manager.begin(), true, status); + + throw kill_session{}; +} + const CommandDesc kill_cmd = { "kill", nullptr, - "kill current session, quit all clients and server", - no_params, + "kill [exit_code]: terminate the current session, the server and all clients connected" + "; the given integer will be used as exit code by the server and client processes", + { {}, ParameterDesc::Flags::SwitchesAsPositional, 0, 1 }, CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser&, Context&, const ShellContext&){ - ensure_all_buffers_are_saved(); - throw kill_session{}; - } + kill }; const CommandDesc force_kill_cmd = { "kill!", nullptr, - "kill current session, quit all clients and server, do not check for unsaved buffers", - no_params, + "kill [exit_code]: force the termination of the current session, the server and all clients connected" + "; the given integer will be used as exit code by the server and client processes", + { {}, ParameterDesc::Flags::SwitchesAsPositional, 0, 1 }, CommandFlags::None, CommandHelper{}, CommandCompleter{}, - [](const ParametersParser&, Context&, const ShellContext&){ throw kill_session{}; } + kill }; template