summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2021-06-13 16:10:59 -0400
committerTavian Barnes <tavianator@tavianator.com>2021-06-13 16:16:31 -0400
commitf0bae86b684e57065da737ed5c8744f3757e9d38 (patch)
treed2941ab2e3eb124c2ce3b4cd581a03287737351b
parentdb67752672877ea0f5360699cb34ad31065ef39e (diff)
downloadbfs-f0bae86b684e57065da737ed5c8744f3757e9d38.tar.xz
parse: More -help pager improvements
If $PAGER is unset, we now try less if it exists, then fall back to more. Colors are only used if less is the used pager, since more on non-coreutils platforms doesn't always handle colors. Finally, less's configuration is given on the command line, which works better if the user has $LESS already set. Fixes https://github.com/tavianator/bfs/issues/76.
-rw-r--r--parse.c63
1 files changed, 24 insertions, 39 deletions
diff --git a/parse.c b/parse.c
index 83004fb..8f34c08 100644
--- a/parse.c
+++ b/parse.c
@@ -2567,13 +2567,23 @@ static struct expr *parse_xdev(struct parser_state *state, int arg1, int arg2) {
*/
static CFILE *launch_pager(pid_t *pid, CFILE *cout) {
char *pager = getenv("PAGER");
- if (!pager || !pager[0]) {
- pager = "more";
+
+ char *exe;
+ if (pager && pager[0]) {
+ exe = bfs_spawn_resolve(pager);
+ } else {
+ exe = bfs_spawn_resolve("less");
+ if (!exe) {
+ exe = bfs_spawn_resolve("more");
+ }
+ }
+ if (!exe) {
+ goto fail;
}
int pipefd[2];
if (pipe(pipefd) != 0) {
- goto fail;
+ goto fail_exe;
}
FILE *file = fdopen(pipefd[1], "w");
@@ -2588,17 +2598,12 @@ static CFILE *launch_pager(pid_t *pid, CFILE *cout) {
}
file = NULL;
ret->close = true;
- ret->colors = cout->colors;
struct bfs_spawn ctx;
if (bfs_spawn_init(&ctx) != 0) {
goto fail_ret;
}
- if (bfs_spawn_setflags(&ctx, BFS_SPAWN_USEPATH) != 0) {
- goto fail_ctx;
- }
-
if (bfs_spawn_addclose(&ctx, fileno(ret->file)) != 0) {
goto fail_ctx;
}
@@ -2610,49 +2615,27 @@ static CFILE *launch_pager(pid_t *pid, CFILE *cout) {
}
char *argv[] = {
- pager,
+ exe,
+ NULL,
NULL,
};
- extern char **environ;
- char **envp = environ;
-
- const char *less = getenv("LESS");
- if (!less || !less[0]) {
- size_t envc;
- for (envc = 0; environ[envc]; ++envc) { }
-
- envp = malloc((envc + 2)*sizeof(*envp));
- if (!envp) {
- goto fail_ctx;
- }
-
- size_t j = 0;
- for (size_t i = 0; i < envc; ++i) {
- if (strncmp(environ[i], "LESS=", 5) != 0) {
- envp[j++] = environ[i];
- }
- }
- envp[j++] = "LESS=FKRX";
- envp[j] = NULL;
+ if (strcmp(xbasename(exe), "less") == 0) {
+ // We know less supports colors, other pagers may not
+ ret->colors = cout->colors;
+ argv[1] = "-FKRX";
}
- *pid = bfs_spawn(pager, &ctx, argv, envp);
+ *pid = bfs_spawn(exe, &ctx, argv, NULL);
if (*pid < 0) {
- goto fail_envp;
+ goto fail_ctx;
}
close(pipefd[0]);
- if (envp != environ) {
- free(envp);
- }
bfs_spawn_destroy(&ctx);
+ free(exe);
return ret;
-fail_envp:
- if (envp != environ) {
- free(envp);
- }
fail_ctx:
bfs_spawn_destroy(&ctx);
fail_ret:
@@ -2668,6 +2651,8 @@ fail_pipe:
if (pipefd[0] >= 0) {
close(pipefd[0]);
}
+fail_exe:
+ free(exe);
fail:
return cout;
}