summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2016-12-07 12:19:56 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2016-12-07 12:19:56 -0500
commit82eb5c5144732df7774fe1f83c0b079e068c5be9 (patch)
treedf1077956fdc1a327dd63ca28bc67650c570c908
parent6c5d5918b5f7fe5e24680832a5c381dd3ae934f0 (diff)
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
-rw-r--r--doc/src/sgml/ref/psql-ref.sgml5
-rw-r--r--src/bin/psql/print.c6
-rw-r--r--src/interfaces/libpq/fe-print.c3
3 files changed, 11 insertions, 3 deletions
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index fdcc62a4493..7a43a6936b8 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -3450,8 +3450,9 @@ $endif
If the query results do not fit on the screen, they are piped
through this command. Typical values are
<literal>more</literal> or <literal>less</literal>. The default
- is platform-dependent. The use of the pager can be disabled by
- using the <command>\pset</command> command.
+ is platform-dependent. Use of the pager can be disabled by setting
+ <envar>PAGER</envar> to empty, or by using pager-related options of
+ the <command>\pset</command> command.
</para>
</listitem>
</varlistentry>
diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index 6dfd1cbec9a..85ac76b8fe3 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -2205,6 +2205,12 @@ PageOutput(int lines, unsigned short int pager)
pagerprog = getenv("PAGER");
if (!pagerprog)
pagerprog = DEFAULT_PAGER;
+ else
+ {
+ /* if PAGER is empty or all-white-space, don't use pager */
+ if (strspn(pagerprog, " \t\r\n") == strlen(pagerprog))
+ return stdout;
+ }
#ifndef WIN32
pqsignal(SIGPIPE, SIG_IGN);
#endif
diff --git a/src/interfaces/libpq/fe-print.c b/src/interfaces/libpq/fe-print.c
index 2fb4de36634..e0b713e943a 100644
--- a/src/interfaces/libpq/fe-print.c
+++ b/src/interfaces/libpq/fe-print.c
@@ -166,8 +166,9 @@ PQprint(FILE *fout, const PGresult *res, const PQprintOpt *po)
screen_size.ws_col = 80;
#endif
pagerenv = getenv("PAGER");
+ /* if PAGER is unset, empty or all-white-space, don't use pager */
if (pagerenv != NULL &&
- pagerenv[0] != '\0' &&
+ strspn(pagerenv, " \t\r\n") != strlen(pagerenv) &&
!po->html3 &&
((po->expanded &&
nTups * (nFields + 1) >= screen_size.ws_row) ||