From 60d0ca1075bbfbb34627f3954d1b705a562e18ac Mon Sep 17 00:00:00 2001 From: Andreas Egeberg Date: Wed, 24 Feb 2021 08:53:54 +1300 Subject: [PATCH] Added Edit command Command Edit opens file /etc/finit.d/available/.conf in the editor referenced by Linux environment variable $EDITOR If $EDITOR is not set it will try to open emacs and if emacs is not found it will try to open the file in vim. Signed-off-by: Andreas Egeberg --- src/initctl.c | 4 +++- src/serv.c | 31 +++++++++++++++++++++++++++++++ src/serv.h | 1 + 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/initctl.c b/src/initctl.c index fd12d5dd..db2325ed 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -659,7 +659,8 @@ static int usage(int rc) " disable Disable .conf in /etc/finit.d/[enabled/]\n" " touch Mark .conf in /etc/finit.d/ for reload\n" " reload Reload *.conf in /etc/finit.d/ (activates changes)\n" -// " reload [:ID] Reload (SIGHUP) service by job# or name\n" +// " reload [:ID] Reload (SIGHUP) service by job# or name\n" + " edit Edit .conf in /etc/finit.d/available/\n" "\n" " cond show Show condition status\n" " cond dump Dump all conditions and their status\n" @@ -709,6 +710,7 @@ int main(int argc, char *argv[]) { "enable", serv_enable }, { "disable", serv_disable }, { "touch", serv_touch }, + { "edit", serv_edit }, { "reload", do_reload }, { "cond", do_cond }, diff --git a/src/serv.c b/src/serv.c index 71874810..9e31514b 100644 --- a/src/serv.c +++ b/src/serv.c @@ -27,6 +27,7 @@ #include /* Definition of AT_* constants */ #include #include +#include #include #include "util.h" @@ -199,6 +200,36 @@ int serv_touch(char *arg) return 0; } +int serv_edit(char *arg) +{ + char corr[40]; + char path[256]; + + if (!arg || !arg[0]) + return serv_list(NULL); + + if (!strstr(arg, ".conf")) { + snprintf(corr, sizeof(corr), "%s.conf", arg); + arg = corr; + } + + pushd(FINIT_RCSD); + if (mkdir("available", 0755) && EEXIST != errno) + err(1, "Failed creating %s/available directory", FINIT_RCSD); + + snprintf(path, sizeof(path), "%s/%s", available, arg); + if (!fexist(path)) + return serv_list(NULL); + + if (systemf("[ -x \"$(command -v sensible-editor)\" ] && sensible-editor %s", path) != 0) + if (systemf("[ -x \"$(command -v editor)\" ] && editor %s", path) != 0) + if (systemf("${VISUAL:-${EDITOR:-$(command -v vi)}} %s", path) != 0) + if (systemf("[ -x \"$(command -v mg)\" ] && mg %s", path) != 0) + systemf("[ -x \"$(command -v vi)\" ] && vi %s", path); + + return 0; +} + /** * Local Variables: * indent-tabs-mode: t diff --git a/src/serv.h b/src/serv.h index e52c42c3..f5e41753 100644 --- a/src/serv.h +++ b/src/serv.h @@ -28,6 +28,7 @@ int serv_list (char *arg); int serv_enable (char *arg); int serv_disable (char *arg); int serv_touch (char *arg); +int serv_edit (char *arg); #endif /* FINIT_SERV_H_ */