From b1d62a307da88a2ee80679cfd2d129bcefe301d4 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 19 Sep 2013 20:09:53 +0100 Subject: [PATCH] Add a -d command line option for running Kakoune as a headless server --- README.asciidoc | 4 ++++ src/main.cc | 31 ++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index e6b657e5..ad575682 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -42,6 +42,10 @@ Just running *kak* launch a new kak session with a client on local terminal. * +-n+: ignore kakrc file * +-s +: set the session name, by default it will be the pid 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 -------------- diff --git a/src/main.cc b/src/main.cc index b9e8e5a0..dba1ac48 100644 --- a/src/main.cc +++ b/src/main.cc @@ -179,14 +179,15 @@ int kakoune(memoryview params) ParametersParser parser(params, { { "c", true }, { "e", true }, { "n", false }, - { "s", true } }); + { "s", true }, + { "d", false } }); String init_command; if (parser.has_option("e")) init_command = parser.option_value("e"); if (parser.has_option("c")) { - for (auto opt : { "n", "s" }) + for (auto opt : { "n", "s", "d" }) { if (parser.has_option(opt)) { @@ -205,13 +206,32 @@ int kakoune(memoryview params) } catch (peer_disconnected&) { - fputs("disconnected from server", stderr); + fputs("disconnected from server\n", stderr); return -1; } return 0; } 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; GlobalOptions global_options; GlobalHooks global_hooks; @@ -276,9 +296,10 @@ int kakoune(memoryview params) else new Buffer("*scratch*", Buffer::Flags::None); - create_local_client(init_command); + if (not daemon) + 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(); {