initctl: add -c create flag, to force creation at edit

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2021-02-27 19:21:18 +01:00
1 parent 2fd4418371
commit 689e6cd52a
2 files changed
+23 -19

No files matched your search

+8 -1
View File
@@ -48,6 +48,7 @@ struct command {
int (*cb)(char *arg); int (*cb)(char *arg);
}; };
int icreate = 0;
int heading = 1; int heading = 1;
int numeric = 0; int numeric = 0;
int verbose = 0; int verbose = 0;
@@ -645,6 +646,7 @@ static int usage(int rc)
"\n" "\n"
"Options:\n" "Options:\n"
" -b, --batch Batch mode, no screen size probing\n" " -b, --batch Batch mode, no screen size probing\n"
" -c, --create Create missing paths (and files) as needed\n"
" -n, --numeric Show JOB:ID instead of NAME:ID\n" " -n, --numeric Show JOB:ID instead of NAME:ID\n"
" -t, --no-heading Skip table headings\n" " -t, --no-heading Skip table headings\n"
" -v, --verbose Verbose output\n" " -v, --verbose Verbose output\n"
@@ -738,6 +740,7 @@ int main(int argc, char *argv[])
}; };
struct option long_options[] = { struct option long_options[] = {
{ "batch", 0, NULL, 'b' }, { "batch", 0, NULL, 'b' },
{ "create", 0, NULL, 'c' },
{ "help", 0, NULL, 'h' }, { "help", 0, NULL, 'h' },
{ "numeric", 0, NULL, 'n' }, { "numeric", 0, NULL, 'n' },
{ "no-heading", 0, NULL, 't' }, { "no-heading", 0, NULL, 't' },
@@ -748,12 +751,16 @@ int main(int argc, char *argv[])
if (transform(progname(argv[0]))) if (transform(progname(argv[0])))
return reboot_main(argc, argv); return reboot_main(argc, argv);
while ((c = getopt_long(argc, argv, "bh?ntv", long_options, NULL)) != EOF) { while ((c = getopt_long(argc, argv, "bch?ntv", long_options, NULL)) != EOF) {
switch(c) { switch(c) {
case 'b': case 'b':
interactive = 0; interactive = 0;
break; break;
case 'c':
icreate = 1;
break;
case 'h': case 'h':
case '?': case '?':
return usage(0); return usage(0);
+15 -18
View File
@@ -35,6 +35,7 @@
static const char *cwd; static const char *cwd;
static const char *available = FINIT_RCSD "/available"; static const char *available = FINIT_RCSD "/available";
static const char *enabled = FINIT_RCSD "/enabled"; static const char *enabled = FINIT_RCSD "/enabled";
extern int icreate; /* initctl -c */
static void pushd(const char *dir) static void pushd(const char *dir)
{ {
@@ -246,28 +247,19 @@ static int do_edit(char *arg, int creat)
"mg", "mg",
"vi" "vi"
}; };
char corr[40];
char path[256]; char path[256];
char *fn;
if (!arg || !arg[0]) fn = conf(path, sizeof(path), arg, creat);
return serv_list(NULL); if (!fexist(fn)) {
if (!creat) {
if (!strstr(arg, ".conf")) { warnx("Cannot find %s, use create command, or select one of:", arg);
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)) {
if (!creat)
return serv_list(NULL); return serv_list(NULL);
}
/* XXX: fill with template/commented-out examples */ /* XXX: fill with template/commented-out examples */
} } else if (creat)
warnx("the file %s already exists, falling back to edit.", fn);
for (size_t i = 0; i < NELEMS(editor); i++) { for (size_t i = 0; i < NELEMS(editor); i++) {
if (systemf("%s %s 2>/dev/null", editor[i], path)) if (systemf("%s %s 2>/dev/null", editor[i], path))
@@ -280,7 +272,12 @@ static int do_edit(char *arg, int creat)
int serv_edit(char *arg) int serv_edit(char *arg)
{ {
return do_edit(arg, 0); if (!arg || !arg[0]) {
warnx("missing argument to edit, may be one of:");
return serv_list("available");
}
return do_edit(arg, icreate);
} }
int serv_creat(char *arg) int serv_creat(char *arg)