summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Momjian2005-07-06 03:14:48 +0000
committerBruce Momjian2005-07-06 03:14:48 +0000
commitc3a69c3b3bf2a4c338a6c6293203e577d2000dd6 (patch)
treee1b3248adc778af2f34c9f381de507c851a9c22e
parent84d630eb24afb6887f9dff1362e1485d3db00017 (diff)
Attached is a patch that enhances the "\h" capability in psql. I often
find myself typing a command and then wanting to get the syntax for it. So I do a ctrl-a and add a \h: but psql does not recognize the command, because I have stuff attached to it (e.g. "alter table foobar"), so I have to scroll over and delete everything except the name of the command itself. This patch gives \h three chances to match: if nothing matches the complete string (current behavior), it tries to match the first two words (e.g. "ALTER TABLE"). If that fails, it tries to match the first word (e.g. "DELETE"). Greg Sabino Mullane
-rw-r--r--src/bin/psql/help.c102
1 files changed, 64 insertions, 38 deletions
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 9c1e6d3a574..1bbe4acc43f 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -3,7 +3,7 @@
*
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.102 2005/06/14 02:57:41 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.103 2005/07/06 03:14:48 momjian Exp $
*/
#include "postgres_fe.h"
#include "common.h"
@@ -305,52 +305,78 @@ helpSQL(const char *topic, unsigned short int pager)
}
else
{
- int i;
+ int i,j,x=0;
bool help_found = false;
FILE *output;
- size_t len;
+ size_t len, wordlen;
int nl_count = 0;
char *ch;
- /* don't care about trailing spaces or semicolons */
+ /* User gets two chances: exact match, then the first word */
+
+ /* First pass : strip trailing spaces and semicolons */
len = strlen(topic);
while (topic[len - 1] == ' ' || topic[len - 1] == ';')
- len--;
-
- /* Count newlines for pager */
- for (i = 0; QL_HELP[i].cmd; i++)
- {
- if (pg_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 (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
- break;
- }
- }
-
- output = PageOutput(nl_count, pager);
+ len--;
- for (i = 0; QL_HELP[i].cmd; i++)
+ for (x=1; x<=3; x++) /* Three chances to guess that word... */
{
- if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
- strcmp(topic, "*") == 0)
- {
- help_found = true;
- 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));
- /* If we have an exact match, exit. Fixes \h SELECT */
- if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
- break;
- }
+ if (x>1) /* Nothing on first pass - try the opening words */
+ {
+ wordlen=j=1;
+ while (topic[j] != ' ' && j++<len)
+ wordlen++;
+ if (x==2)
+ {
+ j++;
+ while (topic[j] != ' ' && j++<=len)
+ wordlen++;
+ }
+ if (wordlen >= len) /* Don't try again if the same word */
+ {
+ output = PageOutput(nl_count, pager);
+ break;
+ }
+ len = wordlen;
+ }
+
+ /* Count newlines for pager */
+ for (i = 0; QL_HELP[i].cmd; i++)
+ {
+ if (pg_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 (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
+ break;
+ }
+ }
+
+ output = PageOutput(nl_count, pager);
+
+ for (i = 0; QL_HELP[i].cmd; i++)
+ {
+ if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
+ strcmp(topic, "*") == 0)
+ {
+ help_found = true;
+ 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));
+ /* If we have an exact match, exit. Fixes \h SELECT */
+ if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
+ break;
+ }
+ }
+ if (help_found) /* Don't keep trying if we got a match */
+ break;
}
if (!help_found)