Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit641b658

Browse files
committed
Page \h output and centralize psql paging code in PageOutput().
1 parent30963fc commit641b658

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
elseif (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
returnsuccess;
517517
}
518+
519+
520+
/*
521+
* PageOutput
522+
*
523+
* Tests if pager is needed and returns appropriate FILE pointer.
524+
*/
525+
FILE*
526+
PageOutput(intlines,boolpager)
527+
{
528+
/* check whether we need / can / are supposed to use pager */
529+
if (pager
530+
#ifndefWIN32
531+
&&
532+
isatty(fileno(stdin))&&
533+
isatty(fileno(stdout))
534+
#endif
535+
)
536+
{
537+
constchar*pagerprog;
538+
539+
#ifdefTIOCGWINSZ
540+
intresult;
541+
structwinsizescreen_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+
#ifndefWIN32
551+
pqsignal(SIGPIPE,SIG_IGN);
552+
#endif
553+
returnpopen(pagerprog,"w");
554+
#ifdefTIOCGWINSZ
555+
}
556+
#endif
557+
}
558+
559+
returnstdout;
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
#ifndefCOMMON_H
99
#defineCOMMON_H
@@ -37,6 +37,8 @@ extern PGresult *PSQLexec(const char *query, bool ignore_command_ok);
3737

3838
externboolSendQuery(constchar*query);
3939

40+
externFILE*PageOutput(intlines,boolpager);
41+
4042
/* sprompt.h */
4143
externchar*simple_prompt(constchar*prompt,intmaxlen,boolecho);
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(boolpager)
163164
{
164-
FILE*output,
165-
*pagerfd=NULL;
166-
167-
/* check whether we need / can / are supposed to use pager */
168-
if (pager
169-
#ifndefWIN32
170-
&&
171-
isatty(fileno(stdin))&&
172-
isatty(fileno(stdout))
173-
#endif
174-
)
175-
{
176-
constchar*pagerprog;
165+
FILE*output;
177166

178-
#ifdefTIOCGWINSZ
179-
intresult;
180-
structwinsizescreen_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-
#ifdefTIOCGWINSZ
191-
}
192-
#endif
193-
}
194-
195-
if (pagerfd)
196-
{
197-
output=pagerfd;
198-
#ifndefWIN32
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
#ifndefWIN32
269233
pqsignal(SIGPIPE,SIG_DFL);
270234
#endif
@@ -278,29 +242,39 @@ slashUsage(bool pager)
278242
*
279243
*/
280244
void
281-
helpSQL(constchar*topic)
245+
helpSQL(constchar*topic,boolpager)
282246
{
283247
#defineVALUE_OR_NULL(a) ((a) ? (a) : "")
284248

285249
if (!topic||strlen(topic)==0)
286250
{
287251
inti;
288252
intitems_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+
#ifndefWIN32
274+
pqsignal(SIGPIPE,SIG_DFL);
275+
#endif
301276
}
302277
}
303-
304278
else
305279
{
306280
inti;

‎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
#ifndefHELP_H
99
#defineHELP_H
@@ -12,7 +12,7 @@ voidusage(void);
1212

1313
voidslashUsage(boolpager);
1414

15-
voidhelpSQL(constchar*topic);
15+
voidhelpSQL(constchar*topic,boolpager);
1616

1717
voidprint_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
constchar*default_footer[]= {NULL};
972973
unsigned shortintborder=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-
#ifndefWIN32
990-
&&
991-
isatty(fileno(stdin))&&
992-
isatty(fileno(stdout))
993-
#endif
994-
)
985+
if (fout==stdout)
995986
{
996-
constchar*pagerprog;
997-
998-
#ifdefTIOCGWINSZ
999-
unsignedintcol_count=0,
1000-
row_count=0,
1001-
lines;
987+
intcol_count=0,
988+
row_count=0,
989+
lines;
1002990
constchar*const*ptr;
1003-
intresult;
1004-
structwinsizescreen_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-
#ifdefTIOCGWINSZ
1033-
}
1034-
#endif
1035-
}
1036-
1037-
if (pagerfd)
1038-
{
1039-
output=pagerfd;
1040-
#ifndefWIN32
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
#ifndefWIN32
10841051
pqsignal(SIGPIPE,SIG_DFL);
10851052
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp