33 *
44 * Copyright (c) 2000-2003, PostgreSQL Global Development Group
55 *
6- * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.78 2003/09/10 21:35:55 momjian Exp $
6+ * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.79 2003/09/11 16:22:42 momjian Exp $
77 */
88#include "postgres_fe.h"
99#include "common.h"
@@ -303,20 +303,41 @@ helpSQL(const char *topic, unsigned short int pager)
303303{
304304int i ;
305305bool help_found = false;
306+ FILE * output ;
306307size_t len ;
307-
308+ int nl_count = 0 ;
309+ char * ch ;
310+
308311/* don't care about trailing spaces */
309312len = strlen (topic );
310313while (topic [len - 1 ]== ' ' )
311314len -- ;
312315
316+ /* Count newlines for pager */
317+ for (i = 0 ;QL_HELP [i ].cmd ;i ++ )
318+ {
319+ if (strncasecmp (topic ,QL_HELP [i ].cmd ,len )== 0 ||
320+ strcmp (topic ,"*" )== 0 )
321+ {
322+ nl_count += 5 ;
323+ for (ch = QL_HELP [i ].syntax ;* ch != '\0' ;ch ++ )
324+ if (* ch == '\n' )
325+ nl_count ++ ;
326+ /* If we have an exact match, exit. Fixes \h SELECT */
327+ if (strcasecmp (topic ,QL_HELP [i ].cmd )== 0 )
328+ break ;
329+ }
330+ }
331+
332+ output = PageOutput (nl_count ,pager );
333+
313334for (i = 0 ;QL_HELP [i ].cmd ;i ++ )
314335{
315336if (strncasecmp (topic ,QL_HELP [i ].cmd ,len )== 0 ||
316337strcmp (topic ,"*" )== 0 )
317338{
318339help_found = true;
319- printf ( _ ("Command: %s\n"
340+ fprintf ( output , _ ("Command: %s\n"
320341"Description: %s\n"
321342"Syntax:\n%s\n\n" ),
322343QL_HELP [i ].cmd ,QL_HELP [i ].help ,QL_HELP [i ].syntax );
@@ -327,7 +348,16 @@ helpSQL(const char *topic, unsigned short int pager)
327348}
328349
329350if (!help_found )
330- printf (_ ("No help available for \"%-.*s\".\nTry \\h with no arguments to see available help.\n" ), (int )len ,topic );
351+ fprintf (output ,_ ("No help available for \"%-.*s\".\nTry \\h with no arguments to see available help.\n" ), (int )len ,topic );
352+
353+ /* Only close if we used the pager */
354+ if (output != stdout )
355+ {
356+ pclose (output );
357+ #ifndef WIN32
358+ pqsignal (SIGPIPE ,SIG_DFL );
359+ #endif
360+ }
331361}
332362}
333363