Add a -d command line option for running Kakoune as a headless server

This commit is contained in:
Maxime Coste 2013-09-19 20:09:53 +01:00
parent 03fed5f6e2
commit b1d62a307d
2 changed files with 30 additions and 5 deletions

View File

@ -42,6 +42,10 @@ Just running *kak* launch a new kak session with a client on local terminal.
* +-n+: ignore kakrc file * +-n+: ignore kakrc file
* +-s <session>+: set the session name, by default it will be the pid * +-s <session>+: set the session name, by default it will be the pid
of the initial kak process. of the initial kak process.
* +-d+: run Kakoune in daemon mode, without user interface. This requires
the session name to be specified with -s. In this mode, the Kakoune
server will keep running even if there is no connected client, and
will quit when receiving SIGTERM.
Basic Movement Basic Movement
-------------- --------------

View File

@ -179,14 +179,15 @@ int kakoune(memoryview<String> params)
ParametersParser parser(params, { { "c", true }, ParametersParser parser(params, { { "c", true },
{ "e", true }, { "e", true },
{ "n", false }, { "n", false },
{ "s", true } }); { "s", true },
{ "d", false } });
String init_command; String init_command;
if (parser.has_option("e")) if (parser.has_option("e"))
init_command = parser.option_value("e"); init_command = parser.option_value("e");
if (parser.has_option("c")) if (parser.has_option("c"))
{ {
for (auto opt : { "n", "s" }) for (auto opt : { "n", "s", "d" })
{ {
if (parser.has_option(opt)) if (parser.has_option(opt))
{ {
@ -205,13 +206,32 @@ int kakoune(memoryview<String> params)
} }
catch (peer_disconnected&) catch (peer_disconnected&)
{ {
fputs("disconnected from server", stderr); fputs("disconnected from server\n", stderr);
return -1; return -1;
} }
return 0; return 0;
} }
else else
{ {
const bool daemon = parser.has_option("d");
static bool terminate = false;
if (daemon)
{
if (not parser.has_option("s"))
{
fputs("-d needs a session name to be specified with -s", stderr);
return -1;
}
if (pid_t child = fork())
{
printf("Kakoune forked to background, for session '%s'\n"
"send SIGTERM to process %d for closing the session\n",
parser.option_value("s").c_str(), child);
exit(0);
}
signal(SIGTERM, [](int) { terminate = true; });
}
EventManager event_manager; EventManager event_manager;
GlobalOptions global_options; GlobalOptions global_options;
GlobalHooks global_hooks; GlobalHooks global_hooks;
@ -276,9 +296,10 @@ int kakoune(memoryview<String> params)
else else
new Buffer("*scratch*", Buffer::Flags::None); new Buffer("*scratch*", Buffer::Flags::None);
if (not daemon)
create_local_client(init_command); create_local_client(init_command);
while (not client_manager.empty()) while (not terminate and (not client_manager.empty() or daemon))
event_manager.handle_next_events(); event_manager.handle_next_events();
{ {