8000 Handle empty or all-blank PAGER setting more sanely in psql. · phan-pivotal/postgres@1ec5cc0 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 1ec5cc0

< 8000 span role="tooltip" aria-label="Browse the repository at this point in the history" id=":R1abab:" class="Tooltip__TooltipBase-sc-17tf59c-0 jjwhNb d-none d-md-flex tooltipped-sw">Browse files
committed
Handle empty or all-blank PAGER setting more sanely in psql.
If the PAGER environment variable is set but contains an empty string, psql would pass it to "sh" which would silently exit, causing whatever query output we were printing to vanish entirely. This is quite mystifying; it took a long time for us to figure out that this was the cause of Joseph Brenner's trouble report. Rather than allowing that to happen, we should treat this as another way to specify "no pager". (We could alternatively treat it as selecting the default pager, but it seems more likely that the former is what the user meant to achieve by setting PAGER this way.) Nonempty, but all-white-space, PAGER values have the same behavior, and it's pretty easy to test for that, so let's handle that case the same way. Most other cases of faulty PAGER values will result in the shell printing some kind of complaint to stderr, which should be enough to diagnose the problem, so we don't need to work harder than this. (Note that there's been an intentional decision not to be very chatty about apparent failure returns from the pager process, since that may happen if, eg, the user quits the pager with control-C or some such. I'd just as soon not start splitting hairs about which exit codes might merit making our own report.) libpq's old PQprint() function was already on board with ignoring empty PAGER values, but for consistency, make it ignore all-white-space values as well. It's been like this a long time, so back-patch to all supported branches. Discussion: https://postgr.es/m/CAFfgvXWLOE2novHzYjmQK8-J6TmHz42G8f3X0SORM44+stUGmw@mail.gmail.com
1 parent d83c942 commit 1ec5cc0

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3354,8 +3354,9 @@ $endif
33543354
If the query results do not fit on the screen, they are piped
33553355
through this command. Typical values are
33563356
<literal>more</literal> or <literal>less</literal>. The default
3357-
is platform-dependent. The use of the pager can be disabled by
3358-
using the <command>\pset</command> command.
3357+
is platform-dependent. Use of the pager can be disabled by setting
3358+
<envar>PAGER</envar> to empty, or by using pager-related options of
3359+
the <command>\pset</command> command.
33593360
</para>
33603361
</listitem>
33613362
</varlistentry>

src/bin/psql/print.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,12 @@ PageOutput(int lines, unsigned short int pager)
20752075
pagerprog = getenv("PAGER");
20762076
if (!pagerprog)
20772077
pagerprog = DEFAULT_PAGER;
2078+
else
2079+
{
2080+
/* if PAGER is empty or all-white-space, don't use pager */
2081+
if (strspn(pagerprog, " \t\r\n") == strlen(pagerprog))
2082+
return stdout;
2083+
}
20782084
#ifndef WIN32
20792085
pqsignal(SIGPIPE, SIG_IGN);
20802086
#endif

src/interfaces/libpq/fe-print.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ PQprint(FILE *fout, const PGresult *res, const PQprintOpt *po)
167167
screen_size.ws_col = 80;
168168
#endif
169169
pagerenv = getenv("PAGER");
170+
/* if PAGER is unset, empty or all-white-space, don't use pager */
170171
if (pagerenv != NULL &&
171-
pagerenv[0] != '\0' &&
172+
strspn(pagerenv, " \t\r\n") != strlen(pagerenv) &&
172173
!po->html3 &&
173174
((po->expanded &&
174175
nTups * (nFields + 1) >= screen_size.ws_row) ||

0 commit comments

Comments
 (0)
0