Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bf8af22

Browse files
committed
PAGER \z in psql.
1 parent 31a697b commit bf8af22

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

src/bin/psql/psql.c

+43-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.124 1998/01/05 13:56:05 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.125 1998/01/09 19:34:38 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -472,9 +472,24 @@ rightsList(PsqlSettings *pset)
472472
char listbuf[256];
473473
int nColumns;
474474
int i;
475-
475+
int usePipe = 0;
476+
char *pagerenv;
477+
FILE *fout;
476478
PGresult *res;
477479

480+
#ifdef TIOCGWINSZ
481+
if (pset->notty == 0 &&
482+
(ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) == -1 ||
483+
screen_size.ws_col == 0 ||
484+
screen_size.ws_row == 0))
485+
{
486+
#endif
487+
screen_size.ws_row = 24;
488+
screen_size.ws_col = 80;
489+
#ifdef TIOCGWINSZ
490+
}
491+
#endif
492+
478493
listbuf[0] = '\0';
479494
strcat(listbuf, "SELECT relname, relacl ");
480495
strcat(listbuf, "FROM pg_class, pg_user ");
@@ -485,26 +500,43 @@ rightsList(PsqlSettings *pset)
485500
strcat(listbuf, " ORDER BY relname ");
486501
if (!(res = PSQLexec(pset, listbuf)))
487502
return -1;
488-
503+
/* first, print out the attribute names */
489504
nColumns = PQntuples(res);
490505
if (nColumns > 0)
491506
{
507+
if (pset->notty == 0 &&
508+
(pagerenv = getenv("PAGER")) &&
509+
pagerenv[0] != '\0' &&
510+
screen_size.ws_row <= nColumns + 7 &&
511+
(fout = popen(pagerenv, "w")))
512+
{
513+
usePipe = 1;
514+
pqsignal(SIGPIPE, SIG_IGN);
515+
}
516+
else
517+
fout = stdout;
518+
492519
/* Display the information */
493520

494-
printf("\nDatabase = %s\n", PQdb(pset->db));
495-
printf(" +------------------+----------------------------------------------------+\n");
496-
printf(" | Relation | Grant/Revoke Permissions |\n");
497-
printf(" +------------------+----------------------------------------------------+\n");
521+
fprintf(fout,"\nDatabase = %s\n", PQdb(pset->db));
522+
fprintf(fout," +------------------+----------------------------------------------------+\n");
523+
fprintf(fout," | Relation | Grant/Revoke Permissions |\n");
524+
fprintf(fout," +------------------+----------------------------------------------------+\n");
498525

499526
/* next, print out the instances */
500527
for (i = 0; i < PQntuples(res); i++)
501528
{
502-
printf(" | %-16.16s", PQgetvalue(res, i, 0));
503-
printf(" | %-50.50s | ", PQgetvalue(res, i, 1));
504-
printf("\n");
529+
fprintf(fout," | %-16.16s", PQgetvalue(res, i, 0));
530+
fprintf(fout," | %-50.50s | ", PQgetvalue(res, i, 1));
531+
fprintf(fout,"\n");
505532
}
506-
printf(" +------------------+----------------------------------------------------+\n");
533+
fprintf(fout," +------------------+----------------------------------------------------+\n");
507534
PQclear(res);
535+
if (usePipe)
536+
{
537+
pclose(fout);
538+
pqsignal(SIGPIPE, SIG_DFL);
539+
}
508540
return (0);
509541
}
510542
else

0 commit comments

Comments
 (0)