From c0b101a1de80297628f51febb62cb16f3c3cff26 Mon Sep 17 00:00:00 2001 From: Pavel Tvrdik Date: Thu, 21 Apr 2016 16:00:29 +0200 Subject: [PATCH] BIRD Client: Refactore cmd_find_common_match() --- client/commands.c | 67 ++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/client/commands.c b/client/commands.c index a5d54f95..26068859 100644 --- a/client/commands.c +++ b/client/commands.c @@ -36,8 +36,8 @@ struct cmd_node { struct cmd_info *cmd; /* Short info */ struct cmd_info *help; /* Detailed info */ signed char prio; /* Priority */ - int len; /* Length of string in token */ u32 flags; /* Mask of (CLI_SF_*) */ + int len; /* Length of string in token */ char token[1]; /* Name of command */ }; @@ -188,6 +188,29 @@ cmd_help(char *cmd, int len) 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, * 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) { struct cmd_node *m; - int best, /* len of common prefix */ - best_prio, i; + int max_common_len; + int best_prio, i; *pcount = 0; - best = -1; + max_common_len = -1; best_prio = -1; 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) { *pcount = 0; - best = -1; + max_common_len = -1; } + if (max_common_len < 0) + best_prio = m->prio; + (*pcount)++; - if (best < 0) - { - /* 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; - } + max_common_len = cmd_merge_match_with_others(max_common_len, m->token, m->len, buf, len); } 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; (*pcount)++; - - 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; - } + max_common_len = cmd_merge_match_with_others(max_common_len, sym->name, sym->len, buf, len); } - return best; + return max_common_len; } int