Skip to content

Commit

Permalink
vty_cli_cmds: group code together and make command search non-destruc…
Browse files Browse the repository at this point in the history
…tive

Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Nov 2, 2010
1 parent f7c5fae commit dc28be2
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions vty_cli_cmds.c
Expand Up @@ -56,7 +56,7 @@ static int knet_cmd_help(struct knet_vty *vty)
return 0;
}

static char *get_first_word(struct knet_vty *vty)
static int get_first_word(struct knet_vty *vty, char **cmd, int *len)
{
int start = 0, idx;

Expand All @@ -66,15 +66,18 @@ static char *get_first_word(struct knet_vty *vty)
}
start = idx;
if (start == vty->line_idx)
return NULL;
return -1;

*cmd = &vty->line[start];

for (idx = start; idx < vty->line_idx; idx++) {
if (vty->line[idx] == ' ')
break;
}
vty->line[idx] = 0;

return &vty->line[start];
*len = idx - start;

return 0;
}

/*
Expand All @@ -91,8 +94,11 @@ static int find_command(struct knet_vty *vty)
return -1;
}

cmd = get_first_word(vty);
len = strlen(cmd);
if (get_first_word(vty, &cmd, &len) < 0) {
knet_vty_write(vty,
"CLI error. Unable to determine command%s", telnet_newline);
return -1;
}

idx = 0;
found = 0;
Expand Down Expand Up @@ -120,6 +126,24 @@ static int find_command(struct knet_vty *vty)
return -1;
}

void knet_vty_execute_cmd(struct knet_vty *vty)
{
void *func;
int cmdidx;
func = NULL;

cmdidx = find_command(vty);
if (cmdidx < 0)
return;

if (knet_vty_nodes[vty->node].cmds[cmdidx].func != NULL) {
knet_vty_nodes[vty->node].cmds[cmdidx].func(vty);
} else { /* this will eventually disappear */
knet_vty_write(vty, "no fn associated to this command%s", telnet_newline);
}
return;
}

void knet_vty_help(struct knet_vty *vty)
{
int idx = 0;
Expand All @@ -138,21 +162,3 @@ void knet_vty_help(struct knet_vty *vty)
idx++;
}
}

void knet_vty_execute_cmd(struct knet_vty *vty)
{
void *func;
int cmdidx;
func = NULL;

cmdidx = find_command(vty);
if (cmdidx < 0)
return;

if (knet_vty_nodes[vty->node].cmds[cmdidx].func != NULL) {
knet_vty_nodes[vty->node].cmds[cmdidx].func(vty);
} else { /* this will eventually disappear */
knet_vty_write(vty, "no fn associated to this command%s", telnet_newline);
}
return;
}

0 comments on commit dc28be2

Please sign in to comment.