Skip to content

Commit edb519b

Browse files
committed
and now, the long awaited PAGER patches from Bruce...
Submitted by: Bruce Momjian <maillist@candle.pha.pa.us>
1 parent 6e077b0 commit edb519b

File tree

3 files changed

+68
-43
lines changed

3 files changed

+68
-43
lines changed

src/bin/psql/psql.c

Lines changed: 3 additions & 2 deletions
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.8 1996/07/27 02:40:45 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.9 1996/07/27 02:55:11 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -111,7 +111,7 @@ usage(char* progname)
111111
fprintf(stderr,"\t -S single line mode (i.e. query terminated by newline)\n");
112112
fprintf(stderr,"\t -t turn off printing of attribute headers\n");
113113
fprintf(stderr,"\t -T html set html3.0 table command options (cf. -H)\n");
114-
fprintf(stderr,"\t -x turn on expanded output (field names on left)");
114+
fprintf(stderr,"\t -x turn on expanded output (field names on left)\n");
115115
exit(1);
116116
}
117117

@@ -1131,6 +1131,7 @@ main(int argc, char** argv)
11311131
settings.opt.header = 1;
11321132
settings.queryFout = stdout;
11331133
settings.opt.fieldSep=dupstr(DEFAULT_FIELD_SEP);
1134+
settings.opt.pager = 1;
11341135
if (!isatty(0) || !isatty(1))
11351136
settings.quiet = settings.notty = 1;
11361137
else

src/interfaces/libpq/fe-exec.c

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.6 1996/07/25 06:21:11 julian Exp $
10+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.7 1996/07/27 02:55:19 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -20,6 +20,11 @@
2020
#include "libpq/pqcomm.h"
2121
#include "libpq-fe.h"
2222
#include <signal.h>
23+
#include <sys/ioctl.h>
24+
25+
#ifdef TIOCGWINSZ
26+
struct winsize screen_size;
27+
#endif
2328

2429
/* the tuples array in a PGresGroup has to grow to accommodate the tuples */
2530
/* returned. Each time, we grow by this much: */
@@ -644,10 +649,7 @@ fill (int length, int max, char filler, FILE *fp)
644649

645650
/*
646651
* PQdisplayTuples()
647-
*
648-
* a better version of PQprintTuples()
649-
* that can optionally do padding of fields with spaces and use different
650-
* field separators
652+
* kept for backward compatibility
651653
*/
652654
void
653655
PQdisplayTuples(PGresult *res,
@@ -660,13 +662,10 @@ PQdisplayTuples(PGresult *res,
660662
{
661663
#define DEFAULT_FIELD_SEP " "
662664

663-
char *pager;
664665
int i, j;
665666
int nFields;
666667
int nTuples;
667668
int fLength[MAX_FIELDS];
668-
int usePipe = 0;
669-
int total_line_length = 0;
670669

671670
if (fieldSep == NULL)
672671
fieldSep == DEFAULT_FIELD_SEP;
@@ -691,29 +690,8 @@ PQdisplayTuples(PGresult *res,
691690
fLength[j] = PQgetlength(res,i,j);
692691
}
693692
}
694-
for (j=0 ; j < nFields; j++)
695-
total_line_length += fLength[j];
696-
total_line_length += nFields * strlen(fieldSep) + 2; /* delimiters */
697-
}
698-
699-
/* Use the pager only if the number of tuples is big enough */
700-
pager=getenv("PAGER");
701-
if ((nTuples > 20)
702-
&& (fp == stdout)
703-
&& (pager != NULL)
704-
&& isatty(fileno(stdout))
705-
&& (nTuples * (total_line_length / 80 + 1) >= 24
706-
- (printHeader != 0) * (total_line_length / 80 + 1) * 2
707-
- 1 /* newline at end of tuple list */ - (quiet == 0))) {
708-
fp = popen(pager, "w");
709-
if (fp) {
710-
usePipe = 1;
711-
signal(SIGPIPE, SIG_IGN);
712-
} else {
713-
fp = stdout;
714-
}
715693
}
716-
694+
717695
if (printHeader) {
718696
/* first, print out the attribute names */
719697
for (i=0; i < nFields; i++) {
@@ -749,22 +727,15 @@ PQdisplayTuples(PGresult *res,
749727
(PQntuples(res) == 1) ? "" : "s");
750728

751729
fflush(fp);
752-
if (usePipe) {
753-
pclose(fp);
754-
signal(SIGPIPE, SIG_DFL);
755-
}
756730
}
757731

758732

759733

760734
/*
761735
* PQprintTuples()
762736
*
763-
* This is the routine that prints out the tuples that
764-
* are returned from the backend.
765-
* Right now all columns are of fixed length,
766-
* this should be changed to allow wrap around for
767-
* tuples values that are wider.
737+
* kept for backward compatibility
738+
*
768739
*/
769740
void
770741
PQprintTuples(PGresult *res,
@@ -833,6 +804,8 @@ PQprintTuples(PGresult *res,
833804
}
834805

835806
/*
807+
* PQprint()
808+
*
836809
* new PQprintTuples routine (proff@suburbia.net)
837810
* PQprintOpt is a typedef (structure) that containes
838811
* various flags and options. consult libpq-fe.h for
@@ -860,6 +833,10 @@ PQprint(FILE *fout,
860833
char *border=NULL;
861834
int numFieldName;
862835
int fs_len=strlen(po->fieldSep);
836+
int total_line_length = 0;
837+
int usePipe = 0;
838+
char *pagerenv;
839+
863840
nTups = PQntuples(res);
864841
if (!(fieldNames=(char **)calloc(nFields, sizeof (char *))))
865842
{
@@ -876,7 +853,8 @@ PQprint(FILE *fout,
876853
perror("calloc");
877854
exit(1);
878855
}
879-
for (numFieldName=0; po->fieldName && po->fieldName[numFieldName]; numFieldName++);
856+
for (numFieldName=0; po->fieldName && po->fieldName[numFieldName]; numFieldName++)
857+
;
880858
for (j=0; j < nFields; j++)
881859
{
882860
int len;
@@ -891,7 +869,48 @@ PQprint(FILE *fout,
891869
len+=fs_len;
892870
if (len>fieldMaxLen)
893871
fieldMaxLen=len;
872+
total_line_length += len;
894873
}
874+
875+
total_line_length += nFields * strlen(po->fieldSep) + 1;
876+
877+
if (fout == NULL)
878+
fout = stdout;
879+
if (po->pager && fout == stdout && isatty(fileno(stdout))) {
880+
/* try to pipe to the pager program if possible */
881+
#ifdef TIOCGWINSZ
882+
if (ioctl(fileno(stdout),TIOCGWINSZ,&screen_size) == -1)
883+
{
884+
#endif
885+
screen_size.ws_row = 24;
886+
screen_size.ws_col = 80;
887+
#ifdef TIOCGWINSZ
888+
}
889+
#endif
890+
pagerenv=getenv("PAGER");
891+
if (pagerenv != NULL &&
892+
!po->html3 &&
893+
((po->expanded &&
894+
nTups * (nFields+1) >= screen_size.ws_row) ||
895+
(!po->expanded &&
896+
nTups * (total_line_length / screen_size.ws_col + 1) *
897+
( 1 + (po->standard != 0)) >=
898+
screen_size.ws_row -
899+
(po->header != 0) *
900+
(total_line_length / screen_size.ws_col + 1) * 2
901+
/*- 1 */ /* newline at end of tuple list */
902+
/*- (quiet == 0)*/
903+
)))
904+
{
905+
fout = popen(pagerenv, "w");
906+
if (fout) {
907+
usePipe = 1;
908+
signal(SIGPIPE, SIG_IGN);
909+
} else
910+
fout = stdout;
911+
}
912+
}
913+
895914
if (!po->expanded && (po->align || po->html3))
896915
{
897916
if (!(fields=(char **)calloc(nFields*(nTups+1), sizeof(char *))))
@@ -1114,6 +1133,10 @@ PQprint(FILE *fout,
11141133
free(fieldMax);
11151134
free(fieldNotNum);
11161135
free(fieldNames);
1136+
if (usePipe) {
1137+
pclose(fout);
1138+
signal(SIGPIPE, SIG_DFL);
1139+
}
11171140
if (border)
11181141
free(border);
11191142
if (po->html3 && !po->expanded)

src/interfaces/libpq/libpq-fe.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: libpq-fe.h,v 1.3 1996/07/23 03:35:14 scrappy Exp $
9+
* $Id: libpq-fe.h,v 1.4 1996/07/27 02:55:23 scrappy Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -133,6 +133,7 @@ struct _PQprintOpt {
133133
bool standard; /* old brain dead format */
134134
bool html3; /* output html tables */
135135
bool expanded; /* expand tables */
136+
bool pager; /* use pager for output if needed */
136137
char *fieldSep; /* field separator */
137138
char *tableOpt; /* insert to HTML <table ...> */
138139
char *caption; /* HTML <caption> */

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