Skip to content

Commit

Permalink
cli: handle missing manpages gracefully
Browse files Browse the repository at this point in the history
Instead of exiting when we can't find a manpage, set the command and continue so
that we can try the json rpc for help.

Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 authored and rustyrussell committed Oct 10, 2018
1 parent cc4357f commit d23a0e8
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions cli/lightning-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>

#define NO_ERROR 0
Expand Down Expand Up @@ -171,9 +172,23 @@ static void add_input(char **cmd, const char *input,
}

static void
exec_man (const char *page) {
execlp("man", "man", page, (char *)NULL);
err(1, "Running man command");
try_exec_man (const char *page) {
int status;

switch (fork()) {
case -1:
err(1, "Forking man command");
case 0:
/* child, run man command. */
execlp("man", "man", page, (char *)NULL);
err(1, "Running man command");
default:
break;
}

wait(&status);
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
exit(0);
}

int main(int argc, char *argv[])
Expand All @@ -192,6 +207,7 @@ int main(int argc, char *argv[])
int parserr;
enum format format = DEFAULT_FORMAT;
enum input input = DEFAULT_INPUT;
char *command = NULL;

err_set_progname(argv[0]);
jsmn_init(&parser);
Expand Down Expand Up @@ -234,9 +250,10 @@ int main(int argc, char *argv[])
/* Launch a manpage if we have a help command with an argument. We do
* not need to have lightningd running in this case. */
if (streq(method, "help") && format == HUMAN && argc >= 3) {
char command[strlen(argv[2]) + sizeof("lightning-")];
snprintf(command, sizeof(command), "lightning-%s", argv[2]);
exec_man(command);
command = argv[2];
char page[strlen(command) + sizeof("lightning-")];
snprintf(page, sizeof(page), "lightning-%s", command);
try_exec_man(page);
}

if (chdir(lightning_dir) != 0)
Expand Down

0 comments on commit d23a0e8

Please sign in to comment.