Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try less and more when pager is not found (on macOS?) #714

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions src/sipp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,15 @@ static char* wrap(const char* in, int offset, int size)
/* If stdout is a TTY, wrap stdout in a call to PAGER (generally less(1)).
* Returns a pid_t you'll have to pass to end_pager(). */
static pid_t begin_pager() {
char pager[15] = "/usr/bin/pager";
char const *pager_options[4] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::vector<std::string>? Iteration is easier, and you won't need a copy, you can pass &opt[0].

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷

I guess we should do more c++. Be my guest 😬

I assume we won't need a copy because it is a copy already.

"/usr/bin/pager",
"/usr/bin/less",
"/usr/bin/more",
nullptr
};
char const *const *pptr;
char const *ptr;
char argv0_buf[PATH_MAX + 1];
char *argv[2] = {nullptr, nullptr};

int stdout_fd = fileno(stdout);
Expand All @@ -767,17 +775,33 @@ static pid_t begin_pager() {
return 0;
}

/* Get pager first, so we can bail if it's not there */
argv[0] = getenv("PAGER");
if (!argv[0]) {
argv[0] = pager; /* missing PAGER */
} else if (!*argv[0]) {
return 0; /* blank PAGER */
/* Get pager first, so we can bail if it's not there. Prefer env */
if ((ptr = getenv("PAGER"))) {
if (!ptr[0]) {
return 0; /* blank PAGER */
}
pager_options[0] = ptr;
pager_options[1] = nullptr;
}

/* Should use euidaccess(3) instead of access(), but that requires
* _GNU_SOURCE */
for (pptr = pager_options; *pptr; ++pptr) {
if (access(*pptr, X_OK) == 0) {
/* Copy the RO-location to something writable so argv can be
* char* const* when passed to execve. */
argv0_buf[0] = argv0_buf[sizeof(argv0_buf) - 1] = '\0';
strncat(argv0_buf, *pptr, sizeof(argv0_buf) - 1);
argv[0] = argv0_buf;
break;
}
}

/* Should use euidaccess(3), but requires _GNU_SOURCE */
if (access(argv[0], X_OK) < 0) {
perror(argv[0]);
/* Nothing found? Silently ignore */
if (!argv[0]) {
if (pager_options[1] == nullptr) {
perror(pager_options[0]); /* env supplied PAGER not found */
}
return 0;
}

Expand Down