summaryrefslogtreecommitdiffstats
path: root/client/commands.c
diff options
context:
space:
mode:
authorMartin Mares <mj@ucw.cz>2000-02-27 23:00:19 +0100
committerMartin Mares <mj@ucw.cz>2000-02-27 23:00:19 +0100
commite69e4ed9349ee28262fe74f70e7e52c181d5d098 (patch)
tree33dcb70e608d36460ff7ac0484cc438c7f9a7289 /client/commands.c
parentde30342f97490e3a3626c4a5fbf3352d1d0aa9c8 (diff)
downloadbird-e69e4ed9349ee28262fe74f70e7e52c181d5d098.tar
bird-e69e4ed9349ee28262fe74f70e7e52c181d5d098.zip
Support expansion of command abbreviations.
Client considered finished (modulo bugs).
Diffstat (limited to 'client/commands.c')
-rw-r--r--client/commands.c53
1 files changed, 46 insertions, 7 deletions
diff --git a/client/commands.c b/client/commands.c
index 09c5c34..fdcb6d2 100644
--- a/client/commands.c
+++ b/client/commands.c
@@ -25,8 +25,6 @@ static struct cmd_info command_table[] = {
#include "conf/commands.h"
};
-/* FIXME: There should exist some system of aliases, so that `show' can be abbreviated as `s' etc. */
-
struct cmd_node {
struct cmd_node *sibling, *son, **plastson;
struct cmd_info *cmd, *help;
@@ -53,7 +51,7 @@ cmd_build_tree(void)
while (*c)
{
char *d = c;
- while (*c && *c != ' ')
+ while (*c && !isspace(*c))
c++;
for(new=old->son; new; new=new->sibling)
if (new->len == c-d && !memcmp(new->token, d, c-d))
@@ -70,7 +68,7 @@ cmd_build_tree(void)
memcpy(new->token, d, c-d);
}
old = new;
- while (*c == ' ')
+ while (isspace(*c))
c++;
}
if (cmd->is_real_cmd)
@@ -143,13 +141,13 @@ cmd_help(char *cmd, int len)
n = &cmd_root;
while (cmd < end)
{
- if (*cmd == ' ' || *cmd == '\t')
+ if (isspace(*cmd))
{
cmd++;
continue;
}
z = cmd;
- while (cmd < end && *cmd != ' ' && *cmd != '\t')
+ while (cmd < end && !isspace(*cmd))
cmd++;
m = cmd_find_abbrev(n, z, cmd-z, &ambig);
if (ambig)
@@ -213,7 +211,7 @@ cmd_complete(char *cmd, int len, char *buf, int again)
n = &cmd_root;
while (cmd < fin && n->son)
{
- if (*cmd == ' ' || *cmd == '\t')
+ if (isspace(*cmd))
{
cmd++;
continue;
@@ -262,3 +260,44 @@ cmd_complete(char *cmd, int len, char *buf, int again)
input_stop_list();
return 0;
}
+
+char *
+cmd_expand(char *cmd)
+{
+ struct cmd_node *n, *m;
+ char *c, *b, *args;
+ int ambig;
+
+ args = c = cmd;
+ n = &cmd_root;
+ while (*c)
+ {
+ if (isspace(*c))
+ {
+ c++;
+ continue;
+ }
+ b = c;
+ while (*c && !isspace(*c))
+ c++;
+ m = cmd_find_abbrev(n, b, c-b, &ambig);
+ if (!m)
+ {
+ if (!ambig)
+ break;
+ puts("Ambiguous command, possible expansions are:");
+ cmd_list_ambiguous(n, b, c-b);
+ return NULL;
+ }
+ args = c;
+ n = m;
+ }
+ if (!n->cmd)
+ {
+ puts("No such command.");
+ return NULL;
+ }
+ b = malloc(strlen(n->cmd->command) + strlen(args) + 1);
+ sprintf(b, "%s%s", n->cmd->command, args);
+ return b;
+}