summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2013-09-19 20:09:53 +0100
committerMaxime Coste <frrrwww@gmail.com>2013-09-19 20:09:53 +0100
commitb1d62a307da88a2ee80679cfd2d129bcefe301d4 (patch)
treedfd26949ba8a774104a399287126b47c69361e33
parent03fed5f6e279406971a21c816c427c1d01994144 (diff)
Add a -d command line option for running Kakoune as a headless server
-rw-r--r--README.asciidoc4
-rw-r--r--src/main.cc31
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 <session>+: 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<String> 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<String> 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<String> 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();
{