Browse Source

cli: source help from local docs when not installed

Teach `lightning-cli help` to look in the doc directory relative to
lightning-cli if it can't find the manpage in any man paths.

This makes `lightning-cli help` work even if clightning hasn't been installed
yet.

Signed-off-by: William Casarin <jb55@jb55.com>
plugin-timeout-inc
William Casarin 6 years ago
committed by Rusty Russell
parent
commit
41468fb607
  1. 23
      cli/lightning-cli.c

23
cli/lightning-cli.c

@ -11,6 +11,7 @@
#include <common/memleak.h> #include <common/memleak.h>
#include <common/utils.h> #include <common/utils.h>
#include <common/version.h> #include <common/version.h>
#include <libgen.h>
#include <stdio.h> #include <stdio.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
@ -175,7 +176,7 @@ static void add_input(char **cmd, const char *input,
} }
static void static void
try_exec_man (const char *page) { try_exec_man (const char *page, char *relative_to) {
int status; int status;
switch (fork()) { switch (fork()) {
@ -183,7 +184,14 @@ try_exec_man (const char *page) {
err(1, "Forking man command"); err(1, "Forking man command");
case 0: case 0:
/* child, run man command. */ /* child, run man command. */
if (relative_to != NULL) {
page = tal_fmt(page, "%s/../doc/%s.7", relative_to, page);
execlp("man", "man", "-l", page, (char *)NULL);
}
else {
execlp("man", "man", page, (char *)NULL); execlp("man", "man", page, (char *)NULL);
}
err(1, "Running man command"); err(1, "Running man command");
default: default:
break; break;
@ -254,9 +262,16 @@ int main(int argc, char *argv[])
* not need to have lightningd running in this case. */ * not need to have lightningd running in this case. */
if (streq(method, "help") && format == HUMAN && argc >= 3) { if (streq(method, "help") && format == HUMAN && argc >= 3) {
command = argv[2]; command = argv[2];
char page[strlen(command) + sizeof("lightning-")]; char *page = tal_fmt(ctx, "lightning-%s", command);
snprintf(page, sizeof(page), "lightning-%s", command);
try_exec_man(page); try_exec_man(page, NULL);
/* Try to find the page relative to this executable.
* This handles the common scenario where lightning-cli
* was built from source and hasn't been installed yet */
try_exec_man(page, dirname(argv[0]));
tal_free(page);
} }
if (chdir(lightning_dir) != 0) if (chdir(lightning_dir) != 0)

Loading…
Cancel
Save