mirror of
https://gitlab.nic.cz/labs/bird.git
synced 2025-01-08 18:11:54 +00:00
BIRD Client: Refactore cmd_find_common_match()
This commit is contained in:
parent
822e2f1cc7
commit
c0b101a1de
@ -36,8 +36,8 @@ struct cmd_node {
|
|||||||
struct cmd_info *cmd; /* Short info */
|
struct cmd_info *cmd; /* Short info */
|
||||||
struct cmd_info *help; /* Detailed info */
|
struct cmd_info *help; /* Detailed info */
|
||||||
signed char prio; /* Priority */
|
signed char prio; /* Priority */
|
||||||
int len; /* Length of string in token */
|
|
||||||
u32 flags; /* Mask of (CLI_SF_*) */
|
u32 flags; /* Mask of (CLI_SF_*) */
|
||||||
|
int len; /* Length of string in token */
|
||||||
char token[1]; /* Name of command */
|
char token[1]; /* Name of command */
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -188,6 +188,29 @@ cmd_help(char *cmd, int len)
|
|||||||
cmd_display_help(m->help, m->cmd);
|
cmd_display_help(m->help, m->cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return length of common prefix of all matches,
|
||||||
|
* Write common prefix string into buf
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
cmd_merge_match_with_others(int max_common_len, const char *token_name, int token_len, char *buf, int from)
|
||||||
|
{
|
||||||
|
if (max_common_len < 0)
|
||||||
|
{
|
||||||
|
/* For a case that we'll have exactly one match */
|
||||||
|
strcpy(buf, token_name + from);
|
||||||
|
max_common_len = token_len - from;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
while (i < max_common_len && i < token_len - from && buf[i] == token_name[from+i])
|
||||||
|
i++;
|
||||||
|
max_common_len = i;
|
||||||
|
}
|
||||||
|
return max_common_len;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return length of common prefix of all matches,
|
* Return length of common prefix of all matches,
|
||||||
* Write count of all matches into pcount,
|
* Write count of all matches into pcount,
|
||||||
@ -197,11 +220,11 @@ static int
|
|||||||
cmd_find_common_match(struct cmd_node *root, char *cmd, int len, int *pcount, char *buf)
|
cmd_find_common_match(struct cmd_node *root, char *cmd, int len, int *pcount, char *buf)
|
||||||
{
|
{
|
||||||
struct cmd_node *m;
|
struct cmd_node *m;
|
||||||
int best, /* len of common prefix */
|
int max_common_len;
|
||||||
best_prio, i;
|
int best_prio, i;
|
||||||
|
|
||||||
*pcount = 0;
|
*pcount = 0;
|
||||||
best = -1;
|
max_common_len = -1;
|
||||||
best_prio = -1;
|
best_prio = -1;
|
||||||
for(m=root->son; m; m=m->sibling)
|
for(m=root->son; m; m=m->sibling)
|
||||||
{
|
{
|
||||||
@ -214,24 +237,14 @@ cmd_find_common_match(struct cmd_node *root, char *cmd, int len, int *pcount, ch
|
|||||||
if (best_prio < m->prio)
|
if (best_prio < m->prio)
|
||||||
{
|
{
|
||||||
*pcount = 0;
|
*pcount = 0;
|
||||||
best = -1;
|
max_common_len = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (max_common_len < 0)
|
||||||
|
best_prio = m->prio;
|
||||||
|
|
||||||
(*pcount)++;
|
(*pcount)++;
|
||||||
if (best < 0)
|
max_common_len = cmd_merge_match_with_others(max_common_len, m->token, m->len, buf, len);
|
||||||
{
|
|
||||||
/* For a case that we'll have exactly one match */
|
|
||||||
strcpy(buf, m->token + len);
|
|
||||||
best = m->len - len;
|
|
||||||
best_prio = m->prio;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
i = 0;
|
|
||||||
while (i < best && i < m->len - len && buf[i] == m->token[len+i])
|
|
||||||
i++;
|
|
||||||
best = i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
list *syms = cli_get_symbol_list();
|
list *syms = cli_get_symbol_list();
|
||||||
@ -245,22 +258,10 @@ cmd_find_common_match(struct cmd_node *root, char *cmd, int len, int *pcount, ch
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
(*pcount)++;
|
(*pcount)++;
|
||||||
|
max_common_len = cmd_merge_match_with_others(max_common_len, sym->name, sym->len, buf, len);
|
||||||
if (best < 0)
|
|
||||||
{
|
|
||||||
strcpy(buf, sym->name + len);
|
|
||||||
best = sym->len - len; /* for a case that we'll have only one match */
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
i = 0;
|
|
||||||
while (i < best && i < sym->len - len && buf[i] == sym->name[len+i])
|
|
||||||
i++;
|
|
||||||
best = i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return best;
|
return max_common_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
Loading…
Reference in New Issue
Block a user