Commit 641b658c authored by Bruce Momjian's avatar Bruce Momjian

Page \h output and centralize psql paging code in PageOutput().

parent 30963fc2
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright 2000-2002 by PostgreSQL Global Development Group * Copyright 2000-2002 by PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.83 2002/10/15 02:24:15 tgl Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.84 2002/10/23 19:23:56 momjian Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "command.h" #include "command.h"
...@@ -493,7 +493,8 @@ exec_command(const char *cmd, ...@@ -493,7 +493,8 @@ exec_command(const char *cmd,
/* help */ /* help */
else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0) else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0)
{ {
helpSQL(options_string ? &options_string[strspn(options_string, " \t\n\r")] : NULL); helpSQL(options_string ? &options_string[strspn(options_string, " \t\n\r")] : NULL,
pset.popt.topt.pager);
/* set pointer to end of line */ /* set pointer to end of line */
if (string) if (string)
string += strlen(string); string += strlen(string);
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright 2000 by PostgreSQL Global Development Group * Copyright 2000 by PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.48 2002/10/15 16:44:21 tgl Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.49 2002/10/23 19:23:56 momjian Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
...@@ -515,3 +515,46 @@ SendQuery(const char *query) ...@@ -515,3 +515,46 @@ SendQuery(const char *query)
return success; return success;
} }
/*
* PageOutput
*
* Tests if pager is needed and returns appropriate FILE pointer.
*/
FILE *
PageOutput(int lines, bool pager)
{
/* check whether we need / can / are supposed to use pager */
if (pager
#ifndef WIN32
&&
isatty(fileno(stdin)) &&
isatty(fileno(stdout))
#endif
)
{
const char *pagerprog;
#ifdef TIOCGWINSZ
int result;
struct winsize screen_size;
result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
if (result == -1 || lines > screen_size.ws_row)
{
#endif
pagerprog = getenv("PAGER");
if (!pagerprog)
pagerprog = DEFAULT_PAGER;
#ifndef WIN32
pqsignal(SIGPIPE, SIG_IGN);
#endif
return popen(pagerprog, "w");
#ifdef TIOCGWINSZ
}
#endif
}
return stdout;
}
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright 2000 by PostgreSQL Global Development Group * Copyright 2000 by PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.19 2002/10/15 02:24:16 tgl Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.20 2002/10/23 19:23:56 momjian Exp $
*/ */
#ifndef COMMON_H #ifndef COMMON_H
#define COMMON_H #define COMMON_H
...@@ -37,6 +37,8 @@ extern PGresult *PSQLexec(const char *query, bool ignore_command_ok); ...@@ -37,6 +37,8 @@ extern PGresult *PSQLexec(const char *query, bool ignore_command_ok);
extern bool SendQuery(const char *query); extern bool SendQuery(const char *query);
extern FILE *PageOutput(int lines, bool pager);
/* sprompt.h */ /* sprompt.h */
extern char *simple_prompt(const char *prompt, int maxlen, bool echo); extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
......
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
* *
* Copyright 2000 by PostgreSQL Global Development Group * Copyright 2000 by PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.58 2002/10/18 22:05:36 petere Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.59 2002/10/23 19:23:56 momjian Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "common.h"
#include "print.h" #include "print.h"
#include "help.h" #include "help.h"
...@@ -161,48 +162,11 @@ struct winsize ...@@ -161,48 +162,11 @@ struct winsize
void void
slashUsage(bool pager) slashUsage(bool pager)
{ {
FILE *output, FILE *output;
*pagerfd = NULL;
/* check whether we need / can / are supposed to use pager */
if (pager
#ifndef WIN32
&&
isatty(fileno(stdin)) &&
isatty(fileno(stdout))
#endif
)
{
const char *pagerprog;
#ifdef TIOCGWINSZ output = PageOutput(50, pager);
int result;
struct winsize screen_size;
result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size); /* if you add/remove a line here, change the row count above */
if (result == -1 || 50 > screen_size.ws_row)
{
#endif
pagerprog = getenv("PAGER");
if (!pagerprog)
pagerprog = DEFAULT_PAGER;
pagerfd = popen(pagerprog, "w");
#ifdef TIOCGWINSZ
}
#endif
}
if (pagerfd)
{
output = pagerfd;
#ifndef WIN32
pqsignal(SIGPIPE, SIG_IGN);
#endif
}
else
output = stdout;
/* if you add/remove a line here, change the row test above */
/* /*
* if this " is the start of the string then it ought to end there to * if this " is the start of the string then it ought to end there to
...@@ -262,9 +226,9 @@ slashUsage(bool pager) ...@@ -262,9 +226,9 @@ slashUsage(bool pager)
fprintf(output, _(" \\z [PATTERN] list table access privileges (same as \\dp)\n")); fprintf(output, _(" \\z [PATTERN] list table access privileges (same as \\dp)\n"));
fprintf(output, _(" \\! [COMMAND] execute command in shell or start interactive shell\n")); fprintf(output, _(" \\! [COMMAND] execute command in shell or start interactive shell\n"));
if (pagerfd) if (output != stdout)
{ {
pclose(pagerfd); pclose(output);
#ifndef WIN32 #ifndef WIN32
pqsignal(SIGPIPE, SIG_DFL); pqsignal(SIGPIPE, SIG_DFL);
#endif #endif
...@@ -278,7 +242,7 @@ slashUsage(bool pager) ...@@ -278,7 +242,7 @@ slashUsage(bool pager)
* *
*/ */
void void
helpSQL(const char *topic) helpSQL(const char *topic, bool pager)
{ {
#define VALUE_OR_NULL(a) ((a) ? (a) : "") #define VALUE_OR_NULL(a) ((a) ? (a) : "")
...@@ -286,21 +250,31 @@ helpSQL(const char *topic) ...@@ -286,21 +250,31 @@ helpSQL(const char *topic)
{ {
int i; int i;
int items_per_column = (QL_HELP_COUNT + 2) / 3; int items_per_column = (QL_HELP_COUNT + 2) / 3;
FILE *output;
output = PageOutput(items_per_column, pager);
puts(_("Available help:")); fputs(_("Available help:\n"), output);
for (i = 0; i < items_per_column; i++) for (i = 0; i < items_per_column; i++)
{ {
printf(" %-26s%-26s", fprintf(output, " %-26s%-26s",
VALUE_OR_NULL(QL_HELP[i].cmd), VALUE_OR_NULL(QL_HELP[i].cmd),
VALUE_OR_NULL(QL_HELP[i + items_per_column].cmd)); VALUE_OR_NULL(QL_HELP[i + items_per_column].cmd));
if (i + 2 * items_per_column < QL_HELP_COUNT) if (i + 2 * items_per_column < QL_HELP_COUNT)
printf("%-26s", fprintf(output, "%-26s",
VALUE_OR_NULL(QL_HELP[i + 2 * items_per_column].cmd)); VALUE_OR_NULL(QL_HELP[i + 2 * items_per_column].cmd));
fputc('\n', stdout); fputc('\n', output);
}
/* Only close if we used the pager */
if (output != stdout)
{
pclose(output);
#ifndef WIN32
pqsignal(SIGPIPE, SIG_DFL);
#endif
} }
} }
else else
{ {
int i; int i;
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright 2000 by PostgreSQL Global Development Group * Copyright 2000 by PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/bin/psql/help.h,v 1.9 2002/07/15 01:56:25 momjian Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/help.h,v 1.10 2002/10/23 19:23:57 momjian Exp $
*/ */
#ifndef HELP_H #ifndef HELP_H
#define HELP_H #define HELP_H
...@@ -12,7 +12,7 @@ void usage(void); ...@@ -12,7 +12,7 @@ void usage(void);
void slashUsage(bool pager); void slashUsage(bool pager);
void helpSQL(const char *topic); void helpSQL(const char *topic, bool pager);
void print_copyright(void); void print_copyright(void);
......
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
* *
* Copyright 2000 by PostgreSQL Global Development Group * Copyright 2000 by PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.32 2002/10/03 17:09:42 momjian Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.33 2002/10/23 19:23:57 momjian Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "common.h"
#include "print.h" #include "print.h"
#include <math.h> #include <math.h>
...@@ -970,9 +971,7 @@ printTable(const char *title, ...@@ -970,9 +971,7 @@ printTable(const char *title,
{ {
const char *default_footer[] = {NULL}; const char *default_footer[] = {NULL};
unsigned short int border = opt->border; unsigned short int border = opt->border;
FILE *pagerfd = NULL, FILE *output;
*output;
if (opt->format == PRINT_NOTHING) if (opt->format == PRINT_NOTHING)
return; return;
...@@ -983,25 +982,12 @@ printTable(const char *title, ...@@ -983,25 +982,12 @@ printTable(const char *title,
if (opt->format != PRINT_HTML && border > 2) if (opt->format != PRINT_HTML && border > 2)
border = 2; border = 2;
if (fout == stdout)
/* check whether we need / can / are supposed to use pager */
if (fout == stdout && opt->pager
#ifndef WIN32
&&
isatty(fileno(stdin)) &&
isatty(fileno(stdout))
#endif
)
{ {
const char *pagerprog; int col_count = 0,
row_count = 0,
#ifdef TIOCGWINSZ lines;
unsigned int col_count = 0,
row_count = 0,
lines;
const char *const * ptr; const char *const * ptr;
int result;
struct winsize screen_size;
/* rough estimate of columns and rows */ /* rough estimate of columns and rows */
if (headers) if (headers)
...@@ -1020,31 +1006,11 @@ printTable(const char *title, ...@@ -1020,31 +1006,11 @@ printTable(const char *title,
if (footers && !opt->tuples_only) if (footers && !opt->tuples_only)
for (ptr = footers; *ptr; ptr++) for (ptr = footers; *ptr; ptr++)
lines++; lines++;
output = PageOutput(lines, opt->pager);
result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
if (result == -1 || lines > screen_size.ws_row)
{
#endif
pagerprog = getenv("PAGER");
if (!pagerprog)
pagerprog = DEFAULT_PAGER;
pagerfd = popen(pagerprog, "w");
#ifdef TIOCGWINSZ
}
#endif
}
if (pagerfd)
{
output = pagerfd;
#ifndef WIN32
pqsignal(SIGPIPE, SIG_IGN);
#endif
} }
else else
output = fout; output = fout;
/* print the stuff */ /* print the stuff */
switch (opt->format) switch (opt->format)
...@@ -1077,9 +1043,10 @@ printTable(const char *title, ...@@ -1077,9 +1043,10 @@ printTable(const char *title,
fprintf(stderr, "+ Oops, you shouldn't see this!\n"); fprintf(stderr, "+ Oops, you shouldn't see this!\n");
} }
if (pagerfd) /* Only close if we used the pager */
if (fout == stdout && output != stdout)
{ {
pclose(pagerfd); pclose(output);
#ifndef WIN32 #ifndef WIN32
pqsignal(SIGPIPE, SIG_DFL); pqsignal(SIGPIPE, SIG_DFL);
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment