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

This commit is contained in:
Bruce Momjian 2002-10-23 19:23:57 +00:00
parent 30963fc200
commit 641b658c26
6 changed files with 86 additions and 99 deletions

View File

@ -3,7 +3,7 @@
*
* 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 "command.h"
@ -493,7 +493,8 @@ exec_command(const char *cmd,
/* help */
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 */
if (string)
string += strlen(string);

View File

@ -3,7 +3,7 @@
*
* 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"
@ -515,3 +515,46 @@ SendQuery(const char *query)
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;
}

View File

@ -3,7 +3,7 @@
*
* 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
#define COMMON_H
@ -37,6 +37,8 @@ extern PGresult *PSQLexec(const char *query, bool ignore_command_ok);
extern bool SendQuery(const char *query);
extern FILE *PageOutput(int lines, bool pager);
/* sprompt.h */
extern char *simple_prompt(const char *prompt, int maxlen, bool echo);

View File

@ -3,9 +3,10 @@
*
* 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 "common.h"
#include "print.h"
#include "help.h"
@ -161,48 +162,11 @@ struct winsize
void
slashUsage(bool pager)
{
FILE *output,
*pagerfd = NULL;
FILE *output;
/* check whether we need / can / are supposed to use pager */
if (pager
#ifndef WIN32
&&
isatty(fileno(stdin)) &&
isatty(fileno(stdout))
#endif
)
{
const char *pagerprog;
output = PageOutput(50, pager);
#ifdef TIOCGWINSZ
int result;
struct winsize screen_size;
result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
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 you add/remove a line here, change the row count above */
/*
* if this " is the start of the string then it ought to end there to
@ -262,9 +226,9 @@ slashUsage(bool pager)
fprintf(output, _(" \\z [PATTERN] list table access privileges (same as \\dp)\n"));
fprintf(output, _(" \\! [COMMAND] execute command in shell or start interactive shell\n"));
if (pagerfd)
if (output != stdout)
{
pclose(pagerfd);
pclose(output);
#ifndef WIN32
pqsignal(SIGPIPE, SIG_DFL);
#endif
@ -278,7 +242,7 @@ slashUsage(bool pager)
*
*/
void
helpSQL(const char *topic)
helpSQL(const char *topic, bool pager)
{
#define VALUE_OR_NULL(a) ((a) ? (a) : "")
@ -286,21 +250,31 @@ helpSQL(const char *topic)
{
int i;
int items_per_column = (QL_HELP_COUNT + 2) / 3;
FILE *output;
puts(_("Available help:"));
output = PageOutput(items_per_column, pager);
fputs(_("Available help:\n"), output);
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 + items_per_column].cmd));
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));
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
{
int i;

View File

@ -3,7 +3,7 @@
*
* 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
#define HELP_H
@ -12,7 +12,7 @@ void usage(void);
void slashUsage(bool pager);
void helpSQL(const char *topic);
void helpSQL(const char *topic, bool pager);
void print_copyright(void);

View File

@ -3,9 +3,10 @@
*
* 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 "common.h"
#include "print.h"
#include <math.h>
@ -970,9 +971,7 @@ printTable(const char *title,
{
const char *default_footer[] = {NULL};
unsigned short int border = opt->border;
FILE *pagerfd = NULL,
*output;
FILE *output;
if (opt->format == PRINT_NOTHING)
return;
@ -983,25 +982,12 @@ printTable(const char *title,
if (opt->format != PRINT_HTML && border > 2)
border = 2;
/* check whether we need / can / are supposed to use pager */
if (fout == stdout && opt->pager
#ifndef WIN32
&&
isatty(fileno(stdin)) &&
isatty(fileno(stdout))
#endif
)
if (fout == stdout)
{
const char *pagerprog;
#ifdef TIOCGWINSZ
unsigned int col_count = 0,
row_count = 0,
lines;
int col_count = 0,
row_count = 0,
lines;
const char *const * ptr;
int result;
struct winsize screen_size;
/* rough estimate of columns and rows */
if (headers)
@ -1020,31 +1006,11 @@ printTable(const char *title,
if (footers && !opt->tuples_only)
for (ptr = footers; *ptr; ptr++)
lines++;
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
output = PageOutput(lines, opt->pager);
}
else
output = fout;
/* print the stuff */
switch (opt->format)
@ -1077,9 +1043,10 @@ printTable(const char *title,
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
pqsignal(SIGPIPE, SIG_DFL);
#endif