Skip to content

Commit 641b658

Browse files
committed
Page \h output and centralize psql paging code in PageOutput().
1 parent 30963fc commit 641b658

File tree

6 files changed

+86
-99
lines changed

6 files changed

+86
-99
lines changed

src/bin/psql/command.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000-2002 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.83 2002/10/15 02:24:15 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.84 2002/10/23 19:23:56 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -493,7 +493,8 @@ exec_command(const char *cmd,
493493
/* help */
494494
else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0)
495495
{
496-
helpSQL(options_string ? &options_string[strspn(options_string, " \t\n\r")] : NULL);
496+
helpSQL(options_string ? &options_string[strspn(options_string, " \t\n\r")] : NULL,
497+
pset.popt.topt.pager);
497498
/* set pointer to end of line */
498499
if (string)
499500
string += strlen(string);

src/bin/psql/common.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.48 2002/10/15 16:44:21 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.49 2002/10/23 19:23:56 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99

@@ -515,3 +515,46 @@ SendQuery(const char *query)
515515

516516
return success;
517517
}
518+
519+
520+
/*
521+
* PageOutput
522+
*
523+
* Tests if pager is needed and returns appropriate FILE pointer.
524+
*/
525+
FILE *
526+
PageOutput(int lines, bool pager)
527+
{
528+
/* check whether we need / can / are supposed to use pager */
529+
if (pager
530+
#ifndef WIN32
531+
&&
532+
isatty(fileno(stdin)) &&
533+
isatty(fileno(stdout))
534+
#endif
535+
)
536+
{
537+
const char *pagerprog;
538+
539+
#ifdef TIOCGWINSZ
540+
int result;
541+
struct winsize screen_size;
542+
543+
result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
544+
if (result == -1 || lines > screen_size.ws_row)
545+
{
546+
#endif
547+
pagerprog = getenv("PAGER");
548+
if (!pagerprog)
549+
pagerprog = DEFAULT_PAGER;
550+
#ifndef WIN32
551+
pqsignal(SIGPIPE, SIG_IGN);
552+
#endif
553+
return popen(pagerprog, "w");
554+
#ifdef TIOCGWINSZ
555+
}
556+
#endif
557+
}
558+
559+
return stdout;
560+
}

src/bin/psql/common.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.19 2002/10/15 02:24:16 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.20 2002/10/23 19:23:56 momjian Exp $
77
*/
88
#ifndef COMMON_H
99
#define COMMON_H
@@ -37,6 +37,8 @@ extern PGresult *PSQLexec(const char *query, bool ignore_command_ok);
3737

3838
extern bool SendQuery(const char *query);
3939

40+
extern FILE *PageOutput(int lines, bool pager);
41+
4042
/* sprompt.h */
4143
extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
4244

src/bin/psql/help.c

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.58 2002/10/18 22:05:36 petere Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.59 2002/10/23 19:23:56 momjian Exp $
77
*/
88
#include "postgres_fe.h"
9+
#include "common.h"
910
#include "print.h"
1011
#include "help.h"
1112

@@ -161,48 +162,11 @@ struct winsize
161162
void
162163
slashUsage(bool pager)
163164
{
164-
FILE *output,
165-
*pagerfd = NULL;
166-
167-
/* check whether we need / can / are supposed to use pager */
168-
if (pager
169-
#ifndef WIN32
170-
&&
171-
isatty(fileno(stdin)) &&
172-
isatty(fileno(stdout))
173-
#endif
174-
)
175-
{
176-
const char *pagerprog;
165+
FILE *output;
177166

178-
#ifdef TIOCGWINSZ
179-
int result;
180-
struct winsize screen_size;
167+
output = PageOutput(50, pager);
181168

182-
result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
183-
if (result == -1 || 50 > screen_size.ws_row)
184-
{
185-
#endif
186-
pagerprog = getenv("PAGER");
187-
if (!pagerprog)
188-
pagerprog = DEFAULT_PAGER;
189-
pagerfd = popen(pagerprog, "w");
190-
#ifdef TIOCGWINSZ
191-
}
192-
#endif
193-
}
194-
195-
if (pagerfd)
196-
{
197-
output = pagerfd;
198-
#ifndef WIN32
199-
pqsignal(SIGPIPE, SIG_IGN);
200-
#endif
201-
}
202-
else
203-
output = stdout;
204-
205-
/* if you add/remove a line here, change the row test above */
169+
/* if you add/remove a line here, change the row count above */
206170

207171
/*
208172
* if this " is the start of the string then it ought to end there to
@@ -262,9 +226,9 @@ slashUsage(bool pager)
262226
fprintf(output, _(" \\z [PATTERN] list table access privileges (same as \\dp)\n"));
263227
fprintf(output, _(" \\! [COMMAND] execute command in shell or start interactive shell\n"));
264228

265-
if (pagerfd)
229+
if (output != stdout)
266230
{
267-
pclose(pagerfd);
231+
pclose(output);
268232
#ifndef WIN32
269233
pqsignal(SIGPIPE, SIG_DFL);
270234
#endif
@@ -278,29 +242,39 @@ slashUsage(bool pager)
278242
*
279243
*/
280244
void
281-
helpSQL(const char *topic)
245+
helpSQL(const char *topic, bool pager)
282246
{
283247
#define VALUE_OR_NULL(a) ((a) ? (a) : "")
284248

285249
if (!topic || strlen(topic) == 0)
286250
{
287251
int i;
288252
int items_per_column = (QL_HELP_COUNT + 2) / 3;
253+
FILE *output;
254+
255+
output = PageOutput(items_per_column, pager);
289256

290-
puts(_("Available help:"));
257+
fputs(_("Available help:\n"), output);
291258

292259
for (i = 0; i < items_per_column; i++)
293260
{
294-
printf(" %-26s%-26s",
261+
fprintf(output, " %-26s%-26s",
295262
VALUE_OR_NULL(QL_HELP[i].cmd),
296263
VALUE_OR_NULL(QL_HELP[i + items_per_column].cmd));
297264
if (i + 2 * items_per_column < QL_HELP_COUNT)
298-
printf("%-26s",
265+
fprintf(output, "%-26s",
299266
VALUE_OR_NULL(QL_HELP[i + 2 * items_per_column].cmd));
300-
fputc('\n', stdout);
267+
fputc('\n', output);
268+
}
269+
/* Only close if we used the pager */
270+
if (output != stdout)
271+
{
272+
pclose(output);
273+
#ifndef WIN32
274+
pqsignal(SIGPIPE, SIG_DFL);
275+
#endif
301276
}
302277
}
303-
304278
else
305279
{
306280
int i;

src/bin/psql/help.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/help.h,v 1.9 2002/07/15 01:56:25 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/help.h,v 1.10 2002/10/23 19:23:57 momjian Exp $
77
*/
88
#ifndef HELP_H
99
#define HELP_H
@@ -12,7 +12,7 @@ void usage(void);
1212

1313
void slashUsage(bool pager);
1414

15-
void helpSQL(const char *topic);
15+
void helpSQL(const char *topic, bool pager);
1616

1717
void print_copyright(void);
1818

src/bin/psql/print.c

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.32 2002/10/03 17:09:42 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.33 2002/10/23 19:23:57 momjian Exp $
77
*/
88
#include "postgres_fe.h"
9+
#include "common.h"
910
#include "print.h"
1011

1112
#include <math.h>
@@ -970,9 +971,7 @@ printTable(const char *title,
970971
{
971972
const char *default_footer[] = {NULL};
972973
unsigned short int border = opt->border;
973-
FILE *pagerfd = NULL,
974-
*output;
975-
974+
FILE *output;
976975

977976
if (opt->format == PRINT_NOTHING)
978977
return;
@@ -983,25 +982,12 @@ printTable(const char *title,
983982
if (opt->format != PRINT_HTML && border > 2)
984983
border = 2;
985984

986-
987-
/* check whether we need / can / are supposed to use pager */
988-
if (fout == stdout && opt->pager
989-
#ifndef WIN32
990-
&&
991-
isatty(fileno(stdin)) &&
992-
isatty(fileno(stdout))
993-
#endif
994-
)
985+
if (fout == stdout)
995986
{
996-
const char *pagerprog;
997-
998-
#ifdef TIOCGWINSZ
999-
unsigned int col_count = 0,
1000-
row_count = 0,
1001-
lines;
987+
int col_count = 0,
988+
row_count = 0,
989+
lines;
1002990
const char *const * ptr;
1003-
int result;
1004-
struct winsize screen_size;
1005991

1006992
/* rough estimate of columns and rows */
1007993
if (headers)
@@ -1020,31 +1006,11 @@ printTable(const char *title,
10201006
if (footers && !opt->tuples_only)
10211007
for (ptr = footers; *ptr; ptr++)
10221008
lines++;
1023-
1024-
result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
1025-
if (result == -1 || lines > screen_size.ws_row)
1026-
{
1027-
#endif
1028-
pagerprog = getenv("PAGER");
1029-
if (!pagerprog)
1030-
pagerprog = DEFAULT_PAGER;
1031-
pagerfd = popen(pagerprog, "w");
1032-
#ifdef TIOCGWINSZ
1033-
}
1034-
#endif
1035-
}
1036-
1037-
if (pagerfd)
1038-
{
1039-
output = pagerfd;
1040-
#ifndef WIN32
1041-
pqsignal(SIGPIPE, SIG_IGN);
1042-
#endif
1009+
output = PageOutput(lines, opt->pager);
10431010
}
10441011
else
10451012
output = fout;
10461013

1047-
10481014
/* print the stuff */
10491015

10501016
switch (opt->format)
@@ -1077,9 +1043,10 @@ printTable(const char *title,
10771043
fprintf(stderr, "+ Oops, you shouldn't see this!\n");
10781044
}
10791045

1080-
if (pagerfd)
1046+
/* Only close if we used the pager */
1047+
if (fout == stdout && output != stdout)
10811048
{
1082-
pclose(pagerfd);
1049+
pclose(output);
10831050
#ifndef WIN32
10841051
pqsignal(SIGPIPE, SIG_DFL);
10851052
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy