77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.11 1996/07/3106:05:46 scrappy Exp $
10+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.12 1996/07/3118:40:09 scrappy Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -829,21 +829,22 @@ PQprint(FILE *fout,
829829nFields = PQnfields (res );
830830
831831if (nFields > 0 ) {/* only print tuples with at least 1 field. */
832- int i ,j ;
833- int nTups ;
834- int * fieldMax = NULL ;/* keep -Wall happy */
835- unsignedchar * fieldNotNum = NULL ;/* keep -Wall happy */
836- char * * fields = NULL ;/*keep -Wall happy */
832+ int i ,j ;
833+ int nTups ;
834+ int * fieldMax = NULL ;/* in case we don't use them */
835+ unsignedchar * fieldNotNum = NULL ;
836+ char * border = NULL ;
837+ char * * fields = NULL ;
837838char * * fieldNames ;
838839int fieldMaxLen = 0 ;
839- char * border = NULL ;
840- int numFieldName ;
840+ int numFieldName ;
841841int fs_len = strlen (po -> fieldSep );
842- int total_line_length = 0 ;
843- int usePipe = 0 ;
844- char * pagerenv ;
842+ int total_line_length = 0 ;
843+ int usePipe = 0 ;
844+ char * pagerenv ;
845+ char buf [8192 * 2 + 1 ];
845846
846- nTups = PQntuples (res );
847+ nTups = PQntuples (res );
847848if (!(fieldNames = (char * * )calloc (nFields ,sizeof (char * ))))
848849{
849850perror ("calloc" );
@@ -882,7 +883,10 @@ PQprint(FILE *fout,
882883
883884if (fout == NULL )
884885fout = stdout ;
885- if (po -> pager && fout == stdout && isatty (fileno (stdout ))) {
886+ if (po -> pager && fout == stdout &&
887+ isatty (fileno (stdin ))&&
888+ isatty (fileno (stdout )))
889+ {
886890/* try to pipe to the pager program if possible */
887891#ifdef TIOCGWINSZ
888892if (ioctl (fileno (stdout ),TIOCGWINSZ ,& screen_size )== -1 ||
@@ -907,8 +911,7 @@ PQprint(FILE *fout,
907911screen_size .ws_row -
908912(po -> header != 0 )*
909913(total_line_length /screen_size .ws_col + 1 )* 2
910- /*- 1 */ /* newline at end of tuple list */
911- /*- (quiet == 0)*/
914+ - (po -> header != 0 )* 2 /* row count and newline */
912915)))
913916{
914917fout = popen (pagerenv ,"w" );
@@ -927,7 +930,8 @@ PQprint(FILE *fout,
927930perror ("calloc" );
928931exit (1 );
929932}
930- }else
933+ }
934+ else
931935if (po -> header && !po -> html3 )
932936 {
933937if (po -> expanded )
@@ -936,7 +940,8 @@ PQprint(FILE *fout,
936940fprintf (fout ,"%-*s%s Value\n" ,fieldMaxLen - fs_len ,"Field" ,po -> fieldSep );
937941else
938942fprintf (fout ,"%s%sValue\n" ,"Field" ,po -> fieldSep );
939- }else
943+ }
944+ else
940945{
941946int len = 0 ;
942947for (j = 0 ;j < nFields ;j ++ )
@@ -959,16 +964,17 @@ PQprint(FILE *fout,
959964else
960965fprintf (fout ,"<centre><h2>Query retrieved %d tuples * %d fields</h2></centre>\n" ,nTups ,nFields );
961966}
962- for (i = 0 ;i < nTups ;i ++ ) {
963- char buf [ 8192 * 2 + 1 ];
967+ for (i = 0 ;i < nTups ;i ++ )
968+ {
964969if (po -> expanded )
965970 {
966971if (po -> html3 )
967972fprintf (fout ,"<table %s><caption align=high>%d</caption>\n" ,po -> tableOpt ?po -> tableOpt :"" ,i );
968973else
969974fprintf (fout ,"-- RECORD %d --\n" ,i );
970975 }
971- for (j = 0 ;j < nFields ;j ++ ) {
976+ for (j = 0 ;j < nFields ;j ++ )
977+ {
972978char * pval ,* p ,* o ;
973979int plen ;
974980if ((plen = PQgetlength (res ,i ,j ))< 1 || !(pval = PQgetvalue (res ,i ,j ))|| !* pval )
@@ -996,7 +1002,8 @@ PQprint(FILE *fout,
9961002exit (1 );
9971003}
9981004strcpy (fields [i * nFields + j ],buf );
999- }else
1005+ }
1006+ else
10001007{
10011008if (po -> expanded )
10021009{
@@ -1089,7 +1096,8 @@ PQprint(FILE *fout,
10891096{
10901097fprintf (fout ,"<th align=%s>%s</th>" ,fieldNotNum [j ]?"left" :"right" ,
10911098fieldNames [j ]);
1092- }else
1099+ }
1100+ else
10931101{
10941102int n = strlen (s );
10951103if (n > fieldMax [j ])
@@ -1139,10 +1147,14 @@ PQprint(FILE *fout,
11391147}
11401148free (fields );
11411149}
1150+ if (po -> header && !po -> html3 )
1151+ fprintf (fout ,"(%d row%s)\n\n" ,PQntuples (res ),
1152+ (PQntuples (res )== 1 ) ?"" :"s" );
11421153free (fieldMax );
11431154free (fieldNotNum );
11441155free (fieldNames );
1145- if (usePipe ) {
1156+ if (usePipe )
1157+ {
11461158pclose (fout );
11471159signal (SIGPIPE ,SIG_DFL );
11481160}