]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cmd.c
Call cvar callbacks after the cvar is set. Potentially fixes heap corruption.
[xonotic/darkplaces.git] / cmd.c
diff --git a/cmd.c b/cmd.c
index ec638b19c1bc8ca8822025a5215ffd1a8dbf5f97..ea0b608eecfab77ade1a7bca311d3ce0ca4e85dc 100644 (file)
--- a/cmd.c
+++ b/cmd.c
@@ -237,11 +237,11 @@ static void Cbuf_Execute_Deferred (cmd_state_t *cmd)
 {
        cmddeferred_t *defcmd, *prev;
        double eat;
-       if (realtime - cmd->deferred_oldrealtime < 0 || realtime - cmd->deferred_oldrealtime > 1800) cmd->deferred_oldrealtime = realtime;
-       eat = realtime - cmd->deferred_oldrealtime;
+       if (host.realtime - cmd->deferred_oldrealtime < 0 || host.realtime - cmd->deferred_oldrealtime > 1800) cmd->deferred_oldrealtime = host.realtime;
+       eat = host.realtime - cmd->deferred_oldrealtime;
        if (eat < (1.0 / 120.0))
                return;
-       cmd->deferred_oldrealtime = realtime;
+       cmd->deferred_oldrealtime = host.realtime;
        prev = NULL;
        defcmd = cmd->deferred_list;
        while(defcmd)
@@ -395,8 +395,6 @@ void Cbuf_Frame(cmd_state_t *cmd)
 ==============================================================================
 */
 
-extern qboolean host_init;
-
 /*
 ===============
 Cmd_StuffCmds_f
@@ -412,9 +410,9 @@ static void Cmd_StuffCmds_f (cmd_state_t *cmd)
        int             i, j, l;
        // this is for all commandline options combined (and is bounds checked)
        char    build[MAX_INPUTLINE];
-
+       
        // come back later so we don't crash
-       if(host_init)
+       if(host.state == host_init)
                return;
 
        if (Cmd_Argc (cmd) != 1)
@@ -430,28 +428,28 @@ static void Cmd_StuffCmds_f (cmd_state_t *cmd)
        host_stuffcmdsrun = true;
        build[0] = 0;
        l = 0;
-       for (i = 0;i < com_argc;i++)
+       for (i = 0;i < sys.argc;i++)
        {
-               if (com_argv[i] && com_argv[i][0] == '+' && (com_argv[i][1] < '0' || com_argv[i][1] > '9') && l + strlen(com_argv[i]) - 1 <= sizeof(build) - 1)
+               if (sys.argv[i] && sys.argv[i][0] == '+' && (sys.argv[i][1] < '0' || sys.argv[i][1] > '9') && l + strlen(sys.argv[i]) - 1 <= sizeof(build) - 1)
                {
                        j = 1;
-                       while (com_argv[i][j])
-                               build[l++] = com_argv[i][j++];
+                       while (sys.argv[i][j])
+                               build[l++] = sys.argv[i][j++];
                        i++;
-                       for (;i < com_argc;i++)
+                       for (;i < sys.argc;i++)
                        {
-                               if (!com_argv[i])
+                               if (!sys.argv[i])
                                        continue;
-                               if ((com_argv[i][0] == '+' || com_argv[i][0] == '-') && (com_argv[i][1] < '0' || com_argv[i][1] > '9'))
+                               if ((sys.argv[i][0] == '+' || sys.argv[i][0] == '-') && (sys.argv[i][1] < '0' || sys.argv[i][1] > '9'))
                                        break;
-                               if (l + strlen(com_argv[i]) + 4 > sizeof(build) - 1)
+                               if (l + strlen(sys.argv[i]) + 4 > sizeof(build) - 1)
                                        break;
                                build[l++] = ' ';
-                               if (strchr(com_argv[i], ' '))
+                               if (strchr(sys.argv[i], ' '))
                                        build[l++] = '\"';
-                               for (j = 0;com_argv[i][j];j++)
-                                       build[l++] = com_argv[i][j];
-                               if (strchr(com_argv[i], ' '))
+                               for (j = 0;sys.argv[i][j];j++)
+                                       build[l++] = sys.argv[i][j];
+                               if (strchr(sys.argv[i], ' '))
                                        build[l++] = '\"';
                        }
                        build[l++] = '\n';
@@ -1435,7 +1433,7 @@ static void Cmd_Apropos_f(cmd_state_t *cmd)
                partial = Cmd_Args(cmd);
        else
        {
-               Con_Printf("usage: apropos <string>\n");
+               Con_Printf("usage: %s <string>\n",Cmd_Argv(cmd, 0));
                return;
        }
 
@@ -1512,7 +1510,7 @@ void Cmd_Init(void)
        // client console can see server cvars because the user may start a server
        cmd_client.cvars = &cvars_all;
        cmd_client.cvars_flagsmask = CVAR_CLIENT | CVAR_SERVER;
-       cmd_client.cmd_flags = CMD_CLIENT | CMD_CLIENT_FROM_SERVER;
+       cmd_client.cmd_flags = CMD_CLIENT | CMD_CLIENT_FROM_SERVER | CMD_SERVER_FROM_CLIENT;
        cmd_client.userdefined = &cmd_userdefined_all;
        // dedicated server console can only see server cvars, there is no client
        cmd_server.cvars = &cvars_all;
@@ -1522,12 +1520,9 @@ void Cmd_Init(void)
        // server commands received from clients have no reason to access cvars, cvar expansion seems perilous.
        cmd_serverfromclient.cvars = &cvars_null;
        cmd_serverfromclient.cvars_flagsmask = 0;
-       cmd_serverfromclient.cmd_flags = CMD_SERVER_FROM_CLIENT;
+       cmd_serverfromclient.cmd_flags = CMD_SERVER_FROM_CLIENT | CMD_USERINFO;
        cmd_serverfromclient.userdefined = &cmd_userdefined_null;
-}
 
-void Cmd_Init_Commands(qboolean dedicated_server)
-{
 //
 // register our commands
 //
@@ -1561,6 +1556,7 @@ void Cmd_Init_Commands(qboolean dedicated_server)
        Cmd_AddCommand(CMD_SHARED, "cmdlist", Cmd_List_f, "lists all console commands beginning with the specified prefix or matching the specified wildcard pattern");
        Cmd_AddCommand(CMD_SHARED, "cvarlist", Cvar_List_f, "lists all console variables beginning with the specified prefix or matching the specified wildcard pattern");
        Cmd_AddCommand(CMD_SHARED, "apropos", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
+       Cmd_AddCommand(CMD_SHARED, "find", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
 
        Cmd_AddCommand(CMD_SHARED, "defer", Cmd_Defer_f, "execute a command in the future");
 
@@ -1694,6 +1690,7 @@ void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const
        cmd_function_t *func;
        cmd_function_t *prev, *current;
        cmd_state_t *cmd;
+       xcommand_t save = NULL;
        int i;
 
        for (i = 0; i < 3; i++)
@@ -1701,6 +1698,11 @@ void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const
                cmd = cmd_iter_all[i].cmd;
                if (flags & cmd->cmd_flags)
                {
+                       if(cmd == &cmd_client && (flags & CMD_SERVER_FROM_CLIENT) && !(flags & CMD_CLIENT))
+                       {
+                               save = function;
+                               function = Cmd_ForwardToServer_f;
+                       }
                        // fail if the command is a variable name
                        if (Cvar_FindVar(cmd->cvars, cmd_name, ~0))
                        {
@@ -1715,14 +1717,8 @@ void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const
                                {
                                        if (!strcmp(cmd_name, func->name))
                                        {
-                                               // Allow overriding forward to server
-                                               if(func->function == Cmd_ForwardToServer_f && (func->flags & 8))
-                                                       break;
-                                               else
-                                               {
-                                                       Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
-                                                       goto nested_continue;
-                                               }
+                                               Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
+                                               goto next;
                                        }
                                }
 
@@ -1775,8 +1771,10 @@ void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const
                                }
                                func->next = current;
                        }
+                       if (save)
+                               function = save;
                }
-nested_continue:
+next:
                continue;
        }
 }
@@ -2072,7 +2070,8 @@ void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qb
                                {
                                        if((func->flags & CMD_CHEAT) && !sv_cheats.integer)
                                                SV_ClientPrintf("No cheats allowed. The server must have sv_cheats set to 1\n");
-                                       func->function(cmd);
+                                       else
+                                               func->function(cmd);
                                        goto done;
                                }
                        }
@@ -2098,7 +2097,7 @@ void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qb
        }
 
 // check cvars
-       if (!Cvar_Command(cmd) && host_framecount > 0)
+       if (!Cvar_Command(cmd) && host.framecount > 0)
                Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(cmd, 0));
 done:
        cmd->tokenizebufferpos = oldpos;