diff options
Diffstat (limited to 'src/shared/pager.c')
-rw-r--r-- | src/shared/pager.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/shared/pager.c b/src/shared/pager.c index 002e3aa373..54790947ba 100644 --- a/src/shared/pager.c +++ b/src/shared/pager.c @@ -150,3 +150,56 @@ void pager_close(void) { bool pager_have(void) { return pager_pid > 0; } + +int show_man_page(const char *desc, bool null_stdio) { + const char *args[4] = { "man", NULL, NULL, NULL }; + char *e = NULL; + pid_t pid; + size_t k; + int r; + siginfo_t status; + + k = strlen(desc); + + if (desc[k-1] == ')') + e = strrchr(desc, '('); + + if (e) { + char *page = NULL, *section = NULL; + + page = strndupa(desc, e - desc); + section = strndupa(e + 1, desc + k - e - 2); + + args[1] = section; + args[2] = page; + } else + args[1] = desc; + + pid = fork(); + if (pid < 0) { + log_error("Failed to fork: %m"); + return -errno; + } + + if (pid == 0) { + /* Child */ + if (null_stdio) { + r = make_null_stdio(); + if (r < 0) { + log_error("Failed to kill stdio: %s", strerror(-r)); + _exit(EXIT_FAILURE); + } + } + + execvp(args[0], (char**) args); + log_error("Failed to execute man: %m"); + _exit(EXIT_FAILURE); + } + + r = wait_for_terminate(pid, &status); + if (r < 0) + return r; + + log_debug("Exit code %i status %i", status.si_code, status.si_status); + return status.si_status; +} |