From 47015126a6457cafc71849edb16d2cac80c34c5c Mon Sep 17 00:00:00 2001 From: cloudwalk Date: Wed, 19 Aug 2020 14:24:03 +0000 Subject: [PATCH] cmd: Reduce duplicate code with command lookup git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@12908 d7cf8633-e32d-0410-b094-e92efae38249 --- cmd.c | 73 ++++++++++++++++++++++++++++------------------------------- cmd.h | 2 ++ 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/cmd.c b/cmd.c index f58730bf..6f76df95 100644 --- a/cmd.c +++ b/cmd.c @@ -1949,6 +1949,30 @@ next: } } +static int Cmd_Compare(const char *s1, const char *s2, size_t len, qboolean casesensitive) +{ + if(len) + return (casesensitive ? strncmp(s1, s2, len) : strncasecmp(s1, s2, len)); + else + return (casesensitive ? strcmp(s1, s2) : strcasecmp(s1, s2)); +} + +cmd_function_t *Cmd_GetCommand(cmd_state_t *cmd, const char *partial, size_t len, qboolean casesensitive) +{ + cmd_function_t *func = NULL; + + // check functions + for (func = cmd->userdefined->csqc_functions; func; func = func->next) + if (!Cmd_Compare(partial, func->name, len, casesensitive)) + break; + + for (func=cmd->engine_functions ; func ; func=func->next) + if (!Cmd_Compare(partial, func->name, len, casesensitive)) + break; + + return func; +} + /* ============ Cmd_Exists @@ -1956,20 +1980,11 @@ Cmd_Exists */ qboolean Cmd_Exists (cmd_state_t *cmd, const char *cmd_name) { - cmd_function_t *func; - - for (func = cmd->userdefined->csqc_functions; func; func = func->next) - if (!strcmp(cmd_name, func->name)) - return true; - - for (func=cmd->engine_functions ; func ; func=func->next) - if (!strcmp (cmd_name,func->name)) - return true; - + if(Cmd_GetCommand(cmd, cmd_name, 0, true)) + return true; return false; } - /* ============ Cmd_CompleteCommand @@ -1978,22 +1993,10 @@ Cmd_CompleteCommand const char *Cmd_CompleteCommand (cmd_state_t *cmd, const char *partial) { cmd_function_t *func; - size_t len; - - len = strlen(partial); - - if (!len) - return NULL; - -// check functions - for (func = cmd->userdefined->csqc_functions; func; func = func->next) - if (!strncasecmp(partial, func->name, len)) - return func->name; - - for (func = cmd->engine_functions; func; func = func->next) - if (!strncasecmp(partial, func->name, len)) - return func->name; + func = Cmd_GetCommand(cmd, partial, strlen(partial), false); + if(func) + return func->name; return NULL; } @@ -2213,19 +2216,12 @@ void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qb goto done; // no tokens // check functions - for (func = cmd->userdefined->csqc_functions; func; func = func->next) + func = Cmd_GetCommand(cmd, cmd->argv[0], 0, false); + if(func) { - if (!strcasecmp(cmd->argv[0], func->name)) - { - if (func->csqcfunc && CL_VM_ConsoleCommand(text)) //[515]: csqc - goto done; - break; - } - } - - for (func = cmd->engine_functions; func; func=func->next) - { - if (!strcasecmp (cmd->argv[0], func->name)) + if (func->csqcfunc && CL_VM_ConsoleCommand(text)) //[515]: csqc + goto done; + else { switch (src) { @@ -2245,7 +2241,6 @@ void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qb goto done; } } - break; } } diff --git a/cmd.h b/cmd.h index b1218a73..8a8d32b9 100644 --- a/cmd.h +++ b/cmd.h @@ -222,6 +222,8 @@ void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const // register commands and functions to call for them. // The cmd_name is referenced later, so it should not be in temp memory +cmd_function_t *Cmd_GetCommand(cmd_state_t *cmd, const char *partial, size_t len, qboolean casesensitive); + /// used by the cvar code to check for cvar / command name overlap qboolean Cmd_Exists (cmd_state_t *cmd, const char *cmd_name); -- 2.39.2