Simplify and clean up find_dll_command()

The only external change here is that it now always sets *cnt to 0 when it returns NULL

Previously, it could set it to -1 in some cases, in particular when dll_commands is NULL.
This led to a bug where /HELP would show the wrong number of matching commands, because it
added the return value of find_dll_command() to the number of matching internal commands.

Also fixes a set-but-unused-variable warning.
This commit is contained in:
Kevin Easton
2017-11-27 17:18:15 +11:00
parent 860e7e28fb
commit 0ba25f02a5
2 changed files with 17 additions and 29 deletions

View File

@@ -1,5 +1,7 @@
[Changes 1.2.2] [Changes 1.2.2]
* Show correct count of matching commands in /HELP. (caf)
* Simplify rsindex() and strsearch() exported functions. (caf) * Simplify rsindex() and strsearch() exported functions. (caf)
* scr-bx now lists the detached screens if there is more than one that matches * scr-bx now lists the detached screens if there is more than one that matches

View File

@@ -933,44 +933,30 @@ const IrcCommand *BX_find_command(const char *com, int *cnt)
#ifdef WANT_DLL #ifdef WANT_DLL
IrcCommandDll *find_dll_command(const char *com, int *cnt) IrcCommandDll *find_dll_command(const char *com, int *cnt)
{ {
int len = 0; const size_t len = com ? strlen(com) : 0;
IrcCommandDll *first_match = NULL;
*cnt = 0;
if (com && (len = strlen(com)) && dll_commands) if (len)
{ {
int min, IrcCommandDll *cmd;
max;
IrcCommandDll *old, *old_next = NULL;
*cnt = 0; for (cmd = dll_commands; cmd; cmd = cmd->next)
min = 1;
max = 0;
for (old = dll_commands; old; old = old->next)
max++;
old = dll_commands;
while (1)
{ {
if (!my_strnicmp(com, old->name, len)) if (!my_strnicmp(com, cmd->name, len))
{ {
if (!old_next) if (!first_match)
old_next = old; first_match = cmd;
(*cnt)++; (*cnt)++;
} }
if (old->next == NULL)
{
if (old_next && strlen(old_next->name) == len)
*cnt *= -1;
return (old_next);
}
else
old = old->next;
} }
if (first_match && strlen(first_match->name) == len)
*cnt *= -1;
} }
else
{ return first_match;
*cnt = -1;
return (NULL);
}
} }
#endif #endif