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 */
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 */
652654void
653655PQdisplayTuples (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 */
769740void
770741PQprintTuples (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 )
0 commit comments