diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2013-09-19 20:09:53 +0100 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2013-09-19 20:09:53 +0100 |
| commit | b1d62a307da88a2ee80679cfd2d129bcefe301d4 (patch) | |
| tree | dfd26949ba8a774104a399287126b47c69361e33 /src | |
| parent | 03fed5f6e279406971a21c816c427c1d01994144 (diff) | |
Add a -d command line option for running Kakoune as a headless server
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cc | 31 |
1 files changed, 26 insertions, 5 deletions
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(); { |
