From d768cb267b9651faeaed48e77cc398ff0be6a0df Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Thu, 11 Sep 2003 16:22:42 +0000
Subject: [PATCH] seemed like a typo in one of the appendix tables

Robert Treat
---
 doc/src/sgml/datetime.sgml |  4 ++--
 src/bin/psql/help.c        | 38 ++++++++++++++++++++++++++++++++++----
 2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/doc/src/sgml/datetime.sgml b/doc/src/sgml/datetime.sgml
index 38dd80c9b3b..24acb96f1e8 100644
--- a/doc/src/sgml/datetime.sgml
+++ b/doc/src/sgml/datetime.sgml
@@ -1,5 +1,5 @@
 <!--
-$Header: /cvsroot/pgsql/doc/src/sgml/datetime.sgml,v 2.34 2003/08/31 17:32:18 petere Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/datetime.sgml,v 2.35 2003/09/11 16:22:42 momjian Exp $
 -->
 
  <appendix id="datetime-appendix">
@@ -344,7 +344,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datetime.sgml,v 2.34 2003/08/31 17:32:18 pe
        </row>
        <row>
 	<entry><literal>PM</literal></entry>
-	<entry>Time is on or after after 12:00</entry>
+	<entry>Time is on or after 12:00</entry>
        </row>
        <row>
 	<entry><literal>T</literal></entry>
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 219e5136d21..f6e0f3961b0 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2003, PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.78 2003/09/10 21:35:55 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.79 2003/09/11 16:22:42 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "common.h"
@@ -303,20 +303,41 @@ helpSQL(const char *topic, unsigned short int pager)
 	{
 		int			i;
 		bool		help_found = false;
+		FILE	   *output;
 		size_t		len;
-
+		int			nl_count = 0;
+		char		*ch;
+		
 		/* don't care about trailing spaces */
 		len = strlen(topic);
 		while (topic[len - 1] == ' ')
 			len--;
 
+		/* Count newlines for pager */
+		for (i = 0; QL_HELP[i].cmd; i++)
+		{
+			if (strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
+				strcmp(topic, "*") == 0)
+			{
+				nl_count += 5;
+				for (ch = QL_HELP[i].syntax; *ch != '\0'; ch++)
+					if (*ch == '\n')
+						nl_count++;
+				/* If we have an exact match, exit.  Fixes \h SELECT */
+				if (strcasecmp(topic, QL_HELP[i].cmd) == 0)
+					break;
+			}
+		}
+
+		output = PageOutput(nl_count, pager);
+
 		for (i = 0; QL_HELP[i].cmd; i++)
 		{
 			if (strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
 				strcmp(topic, "*") == 0)
 			{
 				help_found = true;
-				printf(_("Command:     %s\n"
+				fprintf(output, _("Command:     %s\n"
 						 "Description: %s\n"
 						 "Syntax:\n%s\n\n"),
 					 QL_HELP[i].cmd, QL_HELP[i].help, QL_HELP[i].syntax);
@@ -327,7 +348,16 @@ helpSQL(const char *topic, unsigned short int pager)
 		}
 
 		if (!help_found)
-			printf(_("No help available for \"%-.*s\".\nTry \\h with no arguments to see available help.\n"), (int) len, topic);
+			fprintf(output, _("No help available for \"%-.*s\".\nTry \\h with no arguments to see available help.\n"), (int) len, topic);
+
+		/* Only close if we used the pager */
+		if (output != stdout)
+		{
+			pclose(output);
+#ifndef WIN32
+			pqsignal(SIGPIPE, SIG_DFL);
+#endif
+		}
 	}
 }
 
-- 
GitLab