adjust spacing, add const, remove unnecessary variable, bad malloc check

This commit is contained in:
John Jones 2018-03-07 02:55:59 -05:00
parent c225488cd4
commit b3053d0421

View file

@ -119,27 +119,33 @@ void cli::run()
*/ */
static char *my_rl_complete(char *token, int *match) static char *my_rl_complete(char *token, int *match)
{ {
int matchlen = 0; bool have_one = false;
int count = 0;
std::string method_name; std::string method_name;
auto& cmd = cli_commands(); auto& cmd = cli_commands();
int partlen = strlen (token); /* Part of token */ int partlen = strlen (token); /* Part of token */
for (const std::string it : cmd) for (const std::string it : cmd)
{ {
if (it.compare(0, partlen, token) == 0) if (it.compare(0, partlen, token) == 0)
{ {
method_name = it; if (have_one) {
matchlen = partlen; // we can only have 1, but we found a second
count ++; return NULL;
}
else
{
method_name = it;
have_one = true;
}
} }
} }
if (count == 1) if (have_one)
{ {
*match = 1; *match = 1;
method_name += " "; method_name += " ";
return strdup (method_name.c_str() + matchlen); return strdup (method_name.c_str() + partlen);
} }
return NULL; return NULL;
@ -148,27 +154,35 @@ static char *my_rl_complete(char *token, int *match)
/*** /***
* @brief return an array of matching commands * @brief return an array of matching commands
* @param token the incoming text * @param token the incoming text
* @param av the resultant array of possible matches * @param array the resultant array of possible matches
* @returns the number of matches * @returns the number of matches
*/ */
static int cli_completion(char *token, char ***av) static int cli_completion(char *token, char ***array)
{ {
int num, total = 0;
char **copy;
auto& cmd = cli_commands(); auto& cmd = cli_commands();
num = cmd.size(); int num_commands = cmd.size();
copy = (char **) malloc (num * sizeof(char *)); char **copy = (char **) malloc (num_commands * sizeof(char *));
for (auto it : cmd) { if (copy == NULL)
if (!strncmp (it.c_str(), token, strlen (token))) { {
copy[total] = strdup ( it.c_str() ); // possible out of memory
total ++; return 0;
}
int total_matches = 0;
int partlen = strlen(token);
for (const std::string it : cmd)
{
if ( it.compare(0, partlen, token) == 0)
{
copy[total_matches] = strdup ( it.c_str() );
++total_matches;
} }
} }
*av = copy; *array = copy;
return total; return total_matches;
} }
/*** /***