7
7
*
8
8
*
9
9
* 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 $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
20
20
#include "libpq/pqcomm.h"
21
21
#include "libpq-fe.h"
22
22
#include <signal.h>
23
+ #include <sys/ioctl.h>
24
+
25
+ #ifdef TIOCGWINSZ
26
+ struct winsize screen_size ;
27
+ #endif
23
28
24
29
/* the tuples array in a PGresGroup has to grow to accommodate the tuples */
25
30
/* returned. Each time, we grow by this much: */
@@ -644,10 +649,7 @@ fill (int length, int max, char filler, FILE *fp)
644
649
645
650
/*
646
651
* 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
651
653
*/
652
654
void
653
655
PQdisplayTuples (PGresult * res ,
@@ -660,13 +662,10 @@ PQdisplayTuples(PGresult *res,
660
662
{
661
663
#define DEFAULT_FIELD_SEP " "
662
664
663
- char * pager ;
664
665
int i , j ;
665
666
int nFields ;
666
667
int nTuples ;
667
668
int fLength [MAX_FIELDS ];
668
- int usePipe = 0 ;
669
- int total_line_length = 0 ;
670
669
671
670
if (fieldSep == NULL )
672
671
fieldSep == DEFAULT_FIELD_SEP ;
@@ -691,29 +690,8 @@ PQdisplayTuples(PGresult *res,
691
690
fLength [j ] = PQgetlength (res ,i ,j );
692
691
}
693
692
}
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
- }
715
693
}
716
-
694
+
717
695
if (printHeader ) {
718
696
/* first, print out the attribute names */
719
697
for (i = 0 ; i < nFields ; i ++ ) {
@@ -749,22 +727,15 @@ PQdisplayTuples(PGresult *res,
749
727
(PQntuples (res ) == 1 ) ? "" : "s" );
750
728
751
729
fflush (fp );
752
- if (usePipe ) {
753
- pclose (fp );
754
- signal (SIGPIPE , SIG_DFL );
755
- }
756
730
}
757
731
758
732
759
733
760
734
/*
761
735
* PQprintTuples()
762
736
*
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
+ *
768
739
*/
769
740
void
770
741
PQprintTuples (PGresult * res ,
@@ -833,6 +804,8 @@ PQprintTuples(PGresult *res,
833
804
}
834
805
835
806
/*
807
+ * PQprint()
808
+ *
836
809
* new PQprintTuples routine (proff@suburbia.net)
837
810
* PQprintOpt is a typedef (structure) that containes
838
811
* various flags and options. consult libpq-fe.h for
@@ -860,6 +833,10 @@ PQprint(FILE *fout,
860
833
char * border = NULL ;
861
834
int numFieldName ;
862
835
int fs_len = strlen (po -> fieldSep );
836
+ int total_line_length = 0 ;
837
+ int usePipe = 0 ;
838
+ char * pagerenv ;
839
+
863
840
nTups = PQntuples (res );
864
841
if (!(fieldNames = (char * * )calloc (nFields , sizeof (char * ))))
865
842
{
@@ -876,7 +853,8 @@ PQprint(FILE *fout,
876
853
perror ("calloc" );
877
854
exit (1 );
878
855
}
879
- for (numFieldName = 0 ; po -> fieldName && po -> fieldName [numFieldName ]; numFieldName ++ );
856
+ for (numFieldName = 0 ; po -> fieldName && po -> fieldName [numFieldName ]; numFieldName ++ )
857
+ ;
880
858
for (j = 0 ; j < nFields ; j ++ )
881
859
{
882
860
int len ;
@@ -891,7 +869,48 @@ PQprint(FILE *fout,
891
869
len += fs_len ;
892
870
if (len > fieldMaxLen )
893
871
fieldMaxLen = len ;
872
+ total_line_length += len ;
894
873
}
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
+
895
914
if (!po -> expanded && (po -> align || po -> html3 ))
896
915
{
897
916
if (!(fields = (char * * )calloc (nFields * (nTups + 1 ), sizeof (char * ))))
@@ -1114,6 +1133,10 @@ PQprint(FILE *fout,
1114
1133
free (fieldMax );
1115
1134
free (fieldNotNum );
1116
1135
free (fieldNames );
1136
+ if (usePipe ) {
1137
+ pclose (fout );
1138
+ signal (SIGPIPE , SIG_DFL );
1139
+ }
1117
1140
if (border )
1118
1141
free (border );
1119
1142
if (po -> html3 && !po -> expanded )
0 commit comments