33 *
44 * Copyright 2000 by PostgreSQL Global Development Group
55 *
6- * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.35 2002/11/01 15:12:19 tgl Exp $
6+ * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.36 2003/03/18 22: 15:44 petere Exp $
77 */
88#include "postgres_fe.h"
99#include "common.h"
1010#include "print.h"
1111
1212#include <math.h>
1313#include <signal.h>
14+ #include <unistd.h>
15+
16+ #ifndef WIN32
17+ #include <sys/ioctl.h> /* for ioctl() */
18+ #endif
1419
1520#include "pqsignal.h"
1621#include "libpq-fe.h"
1722
18- #include "settings.h"
19-
2023#include "mbprint.h"
2124
2225/*************************/
@@ -195,7 +198,7 @@ static void
195198print_aligned_text (const char * title ,const char * const * headers ,
196199const char * const * cells ,const char * const * footers ,
197200const char * opt_align ,bool opt_barebones ,
198- unsigned shortint opt_border ,
201+ unsigned shortint opt_border ,int encoding ,
199202FILE * fout )
200203{
201204unsignedint col_count = 0 ;
@@ -253,15 +256,15 @@ print_aligned_text(const char *title, const char *const * headers,
253256/* calc column widths */
254257for (i = 0 ;i < col_count ;i ++ )
255258{
256- tmp = pg_wcswidth ((unsignedchar * )headers [i ],strlen (headers [i ]));
259+ tmp = pg_wcswidth ((unsignedchar * )headers [i ],strlen (headers [i ]), encoding );
257260if (tmp > widths [i ])
258261widths [i ]= tmp ;
259262head_w [i ]= tmp ;
260263}
261264
262265for (i = 0 ,ptr = cells ;* ptr ;ptr ++ ,i ++ )
263266{
264- tmp = pg_wcswidth ((unsignedchar * )* ptr ,strlen (* ptr ));
267+ tmp = pg_wcswidth ((unsignedchar * )* ptr ,strlen (* ptr ), encoding );
265268if (tmp > widths [i %col_count ])
266269widths [i %col_count ]= tmp ;
267270cell_w [i ]= tmp ;
@@ -282,7 +285,7 @@ print_aligned_text(const char *title, const char *const * headers,
282285{
283286int tlen ;
284287
285- tlen = pg_wcswidth ((unsignedchar * )title ,strlen (title ));
288+ tlen = pg_wcswidth ((unsignedchar * )title ,strlen (title ), encoding );
286289if (tlen >= (int )total_w )
287290fprintf (fout ,"%s\n" ,title );
288291else
@@ -392,9 +395,9 @@ print_aligned_text(const char *title, const char *const * headers,
392395
393396static void
394397print_aligned_vertical (const char * title ,const char * const * headers ,
395- const char * const * cells ,const char * const * footers ,
398+ const char * const * cells ,const char * const * footers ,
396399bool opt_barebones ,unsigned shortint opt_border ,
397- FILE * fout )
400+ int encoding , FILE * fout )
398401{
399402unsignedint col_count = 0 ;
400403unsignedint record = 1 ;
@@ -425,7 +428,7 @@ print_aligned_vertical(const char *title, const char *const * headers,
425428}
426429for (i = 0 ;i < col_count ;i ++ )
427430{
428- if ((tmp = pg_wcswidth ((unsignedchar * )headers [i ],strlen (headers [i ])))> hwidth )
431+ if ((tmp = pg_wcswidth ((unsignedchar * )headers [i ],strlen (headers [i ]), encoding ))> hwidth )
429432hwidth = tmp ;
430433head_w [i ]= tmp ;
431434}
@@ -447,7 +450,7 @@ print_aligned_vertical(const char *title, const char *const * headers,
447450/* find longest data cell */
448451for (i = 0 ,ptr = cells ;* ptr ;ptr ++ ,i ++ )
449452{
450- if ((tmp = pg_wcswidth ((unsignedchar * )* ptr ,strlen (* ptr )))> dwidth )
453+ if ((tmp = pg_wcswidth ((unsignedchar * )* ptr ,strlen (* ptr ), encoding ))> dwidth )
451454dwidth = tmp ;
452455cell_w [i ]= tmp ;
453456}
@@ -953,14 +956,55 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
953956
954957
955958
956-
957-
958-
959959/********************************/
960960/* Public functions*/
961961/********************************/
962962
963963
964+ /*
965+ * PageOutput
966+ *
967+ * Tests if pager is needed and returns appropriate FILE pointer.
968+ */
969+ FILE *
970+ PageOutput (int lines ,unsigned shortint pager )
971+ {
972+ /* check whether we need / can / are supposed to use pager */
973+ if (pager
974+ #ifndef WIN32
975+ &&
976+ isatty (fileno (stdin ))&&
977+ isatty (fileno (stdout ))
978+ #endif
979+ )
980+ {
981+ const char * pagerprog ;
982+
983+ #ifdef TIOCGWINSZ
984+ int result ;
985+ struct winsize screen_size ;
986+
987+ result = ioctl (fileno (stdout ),TIOCGWINSZ ,& screen_size );
988+ if (result == -1 || lines > screen_size .ws_row || pager > 1 )
989+ {
990+ #endif
991+ pagerprog = getenv ("PAGER" );
992+ if (!pagerprog )
993+ pagerprog = DEFAULT_PAGER ;
994+ #ifndef WIN32
995+ pqsignal (SIGPIPE ,SIG_IGN );
996+ #endif
997+ return popen (pagerprog ,"w" );
998+ #ifdef TIOCGWINSZ
999+ }
1000+ #endif
1001+ }
1002+
1003+ return stdout ;
1004+ }
1005+
1006+
1007+
9641008void
9651009printTable (const char * title ,
9661010const char * const * headers ,
@@ -1023,9 +1067,9 @@ printTable(const char *title,
10231067break ;
10241068case PRINT_ALIGNED :
10251069if (opt -> expanded )
1026- print_aligned_vertical (title ,headers ,cells ,footers ,opt -> tuples_only ,border ,output );
1070+ print_aligned_vertical (title ,headers ,cells ,footers ,opt -> tuples_only ,border ,opt -> encoding , output );
10271071else
1028- print_aligned_text (title ,headers ,cells ,footers ,align ,opt -> tuples_only ,border ,output );
1072+ print_aligned_text (title ,headers ,cells ,footers ,align ,opt -> tuples_only ,border ,opt -> encoding , output );
10291073break ;
10301074case PRINT_HTML :
10311075if (opt -> expanded )
@@ -1077,7 +1121,7 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
10771121}
10781122
10791123for (i = 0 ;i < nfields ;i ++ )
1080- headers [i ]= mbvalidate (PQfname (result ,i ));
1124+ headers [i ]= mbvalidate (PQfname (result ,i ), opt -> topt . encoding );
10811125
10821126/* set cells */
10831127
@@ -1093,7 +1137,7 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
10931137if (PQgetisnull (result ,i /nfields ,i %nfields ))
10941138cells [i ]= opt -> nullPrint ?opt -> nullPrint :"" ;
10951139else
1096- cells [i ]= mbvalidate (PQgetvalue (result ,i /nfields ,i %nfields ));
1140+ cells [i ]= mbvalidate (PQgetvalue (result ,i /nfields ,i %nfields ), opt -> topt . encoding );
10971141}
10981142
10991143/* set footers */