summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorJosé Fonseca <jfonseca@vmware.com>2014-12-30 17:45:08 +0000
committerJosé Fonseca <jfonseca@vmware.com>2014-12-30 17:45:08 +0000
commit057489b3b631f7213d32835812daf338574d371a (patch)
treea7af2fcc8c559cc29ad3bd5e5e3fd55345764bf3 /cli
parente91f845d55073b3e353f8b6dc57ec52e5ebde662 (diff)
cli: Cleanup pager code.
And silence a gcc warning.
Diffstat (limited to 'cli')
-rw-r--r--cli/cli_pager.cpp47
1 files changed, 28 insertions, 19 deletions
diff --git a/cli/cli_pager.cpp b/cli/cli_pager.cpp
index 579b19f3..4a3b451b 100644
--- a/cli/cli_pager.cpp
+++ b/cli/cli_pager.cpp
@@ -64,8 +64,8 @@ static pid_t pid = -1;
static void
on_exit(void)
{
- fflush(stdout);
- fflush(stderr);
+ fflush(stdout);
+ fflush(stderr);
close(STDOUT_FILENO);
close(STDERR_FILENO);
waitpid(pid, NULL, 0);
@@ -78,6 +78,7 @@ on_exit(void)
static void
on_signal(int sig)
{
+ fprintf(stderr, "on_signal\n");
on_exit();
signal(sig, SIG_DFL);
raise(sig);
@@ -93,37 +94,45 @@ pipepager(void) {
return;
}
- enum {
- READ_FD = 0,
- WRITE_FD = 1
- };
+ union {
+ int pipe[2];
+ struct {
+ int read;
+ int write;
+ };
+ } fd;
- int parentToChild[2];
int ret;
const char *pager;
- ret = pipe(parentToChild);
+ ret = pipe(fd.pipe);
assert(ret == 0);
+ if (ret != 0) {
+ return;
+ }
pid = fork();
switch (pid) {
case -1:
// failed to fork
+ close(fd.read);
+ close(fd.write);
return;
case 0:
// child
- ret = dup2(parentToChild[READ_FD], STDIN_FILENO);
- assert(ret != -1);
- ret = close(parentToChild[WRITE_FD]);
- assert(ret == 0);
+ close(fd.write);
+
+ dup2(fd.read, STDIN_FILENO);
pager = getenv("PAGER");
if (!pager) {
pager = "less";
}
- setenv("LESS", "FRXn", 0);
+ if (!getenv("PAGER")) {
+ putenv((char *)"LESS=FRXn");
+ }
execlp(pager, pager, NULL);
@@ -132,13 +141,13 @@ pipepager(void) {
default:
// parent
- ret = close(parentToChild[READ_FD]);
- assert(ret == 0);
+ close(fd.read);
- dup2(parentToChild[WRITE_FD], STDOUT_FILENO);
- if (isatty(STDERR_FILENO))
- dup2(parentToChild[WRITE_FD], STDERR_FILENO);
- close(parentToChild[WRITE_FD]);
+ dup2(fd.write, STDOUT_FILENO);
+ if (isatty(STDERR_FILENO)) {
+ dup2(fd.write, STDERR_FILENO);
+ }
+ close(fd.write);
// Ensure we wait for the pager before terminating
signal(SIGINT, on_signal);