Index: src/bin/psql/command.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/command.c,v
retrieving revision 1.83
diff -c -c -r1.83 command.c
*** src/bin/psql/command.c	15 Oct 2002 02:24:15 -0000	1.83
--- src/bin/psql/command.c	23 Oct 2002 19:15:10 -0000
***************
*** 493,499 ****
  	/* help */
  	else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0)
  	{
! 		helpSQL(options_string ? &options_string[strspn(options_string, " \t\n\r")] : NULL);
  		/* set pointer to end of line */
  		if (string)
  			string += strlen(string);
--- 493,500 ----
  	/* help */
  	else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0)
  	{
! 		helpSQL(options_string ? &options_string[strspn(options_string, " \t\n\r")] : NULL,
! 				pset.popt.topt.pager);
  		/* set pointer to end of line */
  		if (string)
  			string += strlen(string);
Index: src/bin/psql/common.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/common.c,v
retrieving revision 1.48
diff -c -c -r1.48 common.c
*** src/bin/psql/common.c	15 Oct 2002 16:44:21 -0000	1.48
--- src/bin/psql/common.c	23 Oct 2002 19:15:11 -0000
***************
*** 515,517 ****
--- 515,560 ----
  
  	return success;
  }
+ 
+ 
+ /*
+  * PageOutput
+  *
+  * Tests if pager is needed and returns appropriate FILE pointer.
+  */
+ FILE *
+ PageOutput(int lines, bool pager)
+ {
+ 	/* check whether we need / can / are supposed to use pager */
+ 	if (pager
+ #ifndef WIN32
+ 		&&
+ 		isatty(fileno(stdin)) &&
+ 		isatty(fileno(stdout))
+ #endif
+ 		)
+ 	{
+ 		const char *pagerprog;
+ 
+ #ifdef TIOCGWINSZ
+ 		int			result;
+ 		struct winsize screen_size;
+ 
+ 		result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
+ 		if (result == -1 || lines > screen_size.ws_row)
+ 		{
+ #endif
+ 			pagerprog = getenv("PAGER");
+ 			if (!pagerprog)
+ 				pagerprog = DEFAULT_PAGER;
+ #ifndef WIN32
+ 			pqsignal(SIGPIPE, SIG_IGN);
+ #endif
+ 			return popen(pagerprog, "w");
+ #ifdef TIOCGWINSZ
+ 		}
+ #endif
+ 	}
+ 
+ 	return stdout;
+ }
Index: src/bin/psql/common.h
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/common.h,v
retrieving revision 1.19
diff -c -c -r1.19 common.h
*** src/bin/psql/common.h	15 Oct 2002 02:24:16 -0000	1.19
--- src/bin/psql/common.h	23 Oct 2002 19:15:11 -0000
***************
*** 37,42 ****
--- 37,44 ----
  
  extern bool SendQuery(const char *query);
  
+ extern FILE *PageOutput(int lines, bool pager);
+ 
  /* sprompt.h */
  extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
  
Index: src/bin/psql/help.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/help.c,v
retrieving revision 1.58
diff -c -c -r1.58 help.c
*** src/bin/psql/help.c	18 Oct 2002 22:05:36 -0000	1.58
--- src/bin/psql/help.c	23 Oct 2002 19:15:13 -0000
***************
*** 6,11 ****
--- 6,12 ----
   * $Header: /cvsroot/pgsql-server/src/bin/psql/help.c,v 1.58 2002/10/18 22:05:36 petere Exp $
   */
  #include "postgres_fe.h"
+ #include "common.h"
  #include "print.h"
  #include "help.h"
  
***************
*** 161,208 ****
  void
  slashUsage(bool pager)
  {
! 	FILE	   *output,
! 			   *pagerfd = NULL;
! 
! 	/* check whether we need / can / are supposed to use pager */
! 	if (pager
! #ifndef WIN32
! 		&&
! 		isatty(fileno(stdin)) &&
! 		isatty(fileno(stdout))
! #endif
! 		)
! 	{
! 		const char *pagerprog;
! 
! #ifdef TIOCGWINSZ
! 		int			result;
! 		struct winsize screen_size;
! 
! 		result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
! 		if (result == -1 || 50 > screen_size.ws_row)
! 		{
! #endif
! 			pagerprog = getenv("PAGER");
! 			if (!pagerprog)
! 				pagerprog = DEFAULT_PAGER;
! 			pagerfd = popen(pagerprog, "w");
! #ifdef TIOCGWINSZ
! 		}
! #endif
! 	}
  
! 	if (pagerfd)
! 	{
! 		output = pagerfd;
! #ifndef WIN32
! 		pqsignal(SIGPIPE, SIG_IGN);
! #endif
! 	}
! 	else
! 		output = stdout;
  
! 	/* if you add/remove a line here, change the row test above */
  
  	/*
  	 * if this " is the start of the string then it ought to end there to
--- 162,172 ----
  void
  slashUsage(bool pager)
  {
! 	FILE	   *output;
  
! 	output = PageOutput(50, pager);
  
! 	/* if you add/remove a line here, change the row count above */
  
  	/*
  	 * if this " is the start of the string then it ought to end there to
***************
*** 262,270 ****
  	fprintf(output, _(" \\z [PATTERN]   list table access privileges (same as \\dp)\n"));
  	fprintf(output, _(" \\! [COMMAND]   execute command in shell or start interactive shell\n"));
  
! 	if (pagerfd)
  	{
! 		pclose(pagerfd);
  #ifndef WIN32
  		pqsignal(SIGPIPE, SIG_DFL);
  #endif
--- 226,234 ----
  	fprintf(output, _(" \\z [PATTERN]   list table access privileges (same as \\dp)\n"));
  	fprintf(output, _(" \\! [COMMAND]   execute command in shell or start interactive shell\n"));
  
! 	if (output != stdout)
  	{
! 		pclose(output);
  #ifndef WIN32
  		pqsignal(SIGPIPE, SIG_DFL);
  #endif
***************
*** 278,284 ****
   *
   */
  void
! helpSQL(const char *topic)
  {
  #define VALUE_OR_NULL(a) ((a) ? (a) : "")
  
--- 242,248 ----
   *
   */
  void
! helpSQL(const char *topic, bool pager)
  {
  #define VALUE_OR_NULL(a) ((a) ? (a) : "")
  
***************
*** 286,306 ****
  	{
  		int			i;
  		int			items_per_column = (QL_HELP_COUNT + 2) / 3;
  
! 		puts(_("Available help:"));
  
  		for (i = 0; i < items_per_column; i++)
  		{
! 			printf("  %-26s%-26s",
  				   VALUE_OR_NULL(QL_HELP[i].cmd),
  				   VALUE_OR_NULL(QL_HELP[i + items_per_column].cmd));
  			if (i + 2 * items_per_column < QL_HELP_COUNT)
! 				printf("%-26s",
  				   VALUE_OR_NULL(QL_HELP[i + 2 * items_per_column].cmd));
! 			fputc('\n', stdout);
  		}
  	}
- 
  	else
  	{
  		int			i;
--- 250,280 ----
  	{
  		int			i;
  		int			items_per_column = (QL_HELP_COUNT + 2) / 3;
+ 		FILE		*output;
+ 
+ 		output = PageOutput(items_per_column, pager);
  
! 		fputs(_("Available help:\n"), output);
  
  		for (i = 0; i < items_per_column; i++)
  		{
! 			fprintf(output, "  %-26s%-26s",
  				   VALUE_OR_NULL(QL_HELP[i].cmd),
  				   VALUE_OR_NULL(QL_HELP[i + items_per_column].cmd));
  			if (i + 2 * items_per_column < QL_HELP_COUNT)
! 				fprintf(output, "%-26s",
  				   VALUE_OR_NULL(QL_HELP[i + 2 * items_per_column].cmd));
! 			fputc('\n', output);
! 		}
! 		/* Only close if we used the pager */
! 		if (output != stdout)
! 		{
! 			pclose(output);
! #ifndef WIN32
! 			pqsignal(SIGPIPE, SIG_DFL);
! #endif
  		}
  	}
  	else
  	{
  		int			i;
Index: src/bin/psql/help.h
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/help.h,v
retrieving revision 1.9
diff -c -c -r1.9 help.h
*** src/bin/psql/help.h	15 Jul 2002 01:56:25 -0000	1.9
--- src/bin/psql/help.h	23 Oct 2002 19:15:13 -0000
***************
*** 12,18 ****
  
  void		slashUsage(bool pager);
  
! void		helpSQL(const char *topic);
  
  void		print_copyright(void);
  
--- 12,18 ----
  
  void		slashUsage(bool pager);
  
! void		helpSQL(const char *topic, bool pager);
  
  void		print_copyright(void);
  
Index: src/bin/psql/print.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/print.c,v
retrieving revision 1.32
diff -c -c -r1.32 print.c
*** src/bin/psql/print.c	3 Oct 2002 17:09:42 -0000	1.32
--- src/bin/psql/print.c	23 Oct 2002 19:15:15 -0000
***************
*** 6,11 ****
--- 6,12 ----
   * $Header: /cvsroot/pgsql-server/src/bin/psql/print.c,v 1.32 2002/10/03 17:09:42 momjian Exp $
   */
  #include "postgres_fe.h"
+ #include "common.h"
  #include "print.h"
  
  #include <math.h>
***************
*** 970,978 ****
  {
  	const char *default_footer[] = {NULL};
  	unsigned short int border = opt->border;
! 	FILE	   *pagerfd = NULL,
! 			   *output;
! 
  
  	if (opt->format == PRINT_NOTHING)
  		return;
--- 971,977 ----
  {
  	const char *default_footer[] = {NULL};
  	unsigned short int border = opt->border;
! 	FILE		*output;
  
  	if (opt->format == PRINT_NOTHING)
  		return;
***************
*** 983,1007 ****
  	if (opt->format != PRINT_HTML && border > 2)
  		border = 2;
  
! 
! 	/* check whether we need / can / are supposed to use pager */
! 	if (fout == stdout && opt->pager
! #ifndef WIN32
! 		&&
! 		isatty(fileno(stdin)) &&
! 		isatty(fileno(stdout))
! #endif
! 		)
  	{
! 		const char *pagerprog;
! 
! #ifdef TIOCGWINSZ
! 		unsigned int col_count = 0,
! 					row_count = 0,
! 					lines;
  		const char *const * ptr;
- 		int			result;
- 		struct winsize screen_size;
  
  		/* rough estimate of columns and rows */
  		if (headers)
--- 982,993 ----
  	if (opt->format != PRINT_HTML && border > 2)
  		border = 2;
  
! 	if (fout == stdout)
  	{
! 		int col_count = 0,
! 			row_count = 0,
! 			lines;
  		const char *const * ptr;
  
  		/* rough estimate of columns and rows */
  		if (headers)
***************
*** 1020,1050 ****
  		if (footers && !opt->tuples_only)
  			for (ptr = footers; *ptr; ptr++)
  				lines++;
! 
! 		result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
! 		if (result == -1 || lines > screen_size.ws_row)
! 		{
! #endif
! 			pagerprog = getenv("PAGER");
! 			if (!pagerprog)
! 				pagerprog = DEFAULT_PAGER;
! 			pagerfd = popen(pagerprog, "w");
! #ifdef TIOCGWINSZ
! 		}
! #endif
! 	}
! 
! 	if (pagerfd)
! 	{
! 		output = pagerfd;
! #ifndef WIN32
! 		pqsignal(SIGPIPE, SIG_IGN);
! #endif
  	}
  	else
  		output = fout;
  
- 
  	/* print the stuff */
  
  	switch (opt->format)
--- 1006,1016 ----
  		if (footers && !opt->tuples_only)
  			for (ptr = footers; *ptr; ptr++)
  				lines++;
! 		output = PageOutput(lines, opt->pager);
  	}
  	else
  		output = fout;
  
  	/* print the stuff */
  
  	switch (opt->format)
***************
*** 1077,1085 ****
  			fprintf(stderr, "+ Oops, you shouldn't see this!\n");
  	}
  
! 	if (pagerfd)
  	{
! 		pclose(pagerfd);
  #ifndef WIN32
  		pqsignal(SIGPIPE, SIG_DFL);
  #endif
--- 1043,1052 ----
  			fprintf(stderr, "+ Oops, you shouldn't see this!\n");
  	}
  
! 	/* Only close if we used the pager */
! 	if (fout == stdout && output != stdout)
  	{
! 		pclose(output);
  #ifndef WIN32
  		pqsignal(SIGPIPE, SIG_DFL);
  #endif
