X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;f=cmd.c;h=d2b0b115531d382e90ee2782b0d9143c7ff53baf;hb=ca913e8190a32d6e96dededdb0a6f8d5ce72bf63;hp=293155d62de244a1eb3ffefcd2fc500b2cabc065;hpb=d57be67cb00229acb8564b92c8b7c58eeed8a0cb;p=xonotic%2Fdarkplaces.git diff --git a/cmd.c b/cmd.c index 293155d6..d2b0b115 100644 --- a/cmd.c +++ b/cmd.c @@ -25,9 +25,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. typedef struct cmdalias_s { - struct cmdalias_s *next; - char name[MAX_ALIAS_NAME]; - char *value; + struct cmdalias_s *next; + char name[MAX_ALIAS_NAME]; + char *value; } cmdalias_t; static cmdalias_t *cmd_alias; @@ -36,6 +36,10 @@ static qboolean cmd_wait; static mempool_t *cmd_mempool; +#define CMD_TOKENIZELENGTH 4096 +static char cmd_tokenizebuffer[CMD_TOKENIZELENGTH]; +static int cmd_tokenizebufferpos = 0; + //============================================================================= /* @@ -81,7 +85,7 @@ Cbuf_AddText Adds command text at the end of the buffer ============ */ -void Cbuf_AddText (char *text) +void Cbuf_AddText (const char *text) { int l; @@ -106,7 +110,7 @@ Adds a \n to the text FIXME: actually change the command buffer to do less copying ============ */ -void Cbuf_InsertText (char *text) +void Cbuf_InsertText (const char *text) { char *temp; int templen; @@ -140,10 +144,13 @@ Cbuf_Execute */ void Cbuf_Execute (void) { - int i; - char *text; - char line[1024]; - int quotes; + int i; + char *text; + char line[1024]; + int quotes; + + // LordHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes + cmd_tokenizebufferpos = 0; while (cmd_text.cursize) { @@ -279,7 +286,7 @@ Cmd_Exec_f */ static void Cmd_Exec_f (void) { - char *f; + char *f; if (Cmd_Argc () != 2) { @@ -287,13 +294,13 @@ static void Cmd_Exec_f (void) return; } - f = (char *)COM_LoadFile (Cmd_Argv(1), false); + f = (char *)FS_LoadFile (Cmd_Argv(1), false); if (!f) { Con_Printf ("couldn't exec %s\n",Cmd_Argv(1)); return; } - Con_Printf ("execing %s\n",Cmd_Argv(1)); + Con_DPrintf ("execing %s\n",Cmd_Argv(1)); Cbuf_InsertText (f); Mem_Free(f); @@ -323,10 +330,9 @@ Cmd_Alias_f Creates a new command that executes a command string (possibly ; seperated) =============== */ - static char *CopyString (char *in) { - char *out; + char *out; out = Z_Malloc (strlen(in)+1); strcpy (out, in); @@ -338,7 +344,7 @@ static void Cmd_Alias_f (void) cmdalias_t *a; char cmd[1024]; int i, c; - char *s; + const char *s; if (Cmd_Argc() == 1) { @@ -371,18 +377,18 @@ static void Cmd_Alias_f (void) a->next = cmd_alias; cmd_alias = a; } - strcpy (a->name, s); + strlcpy (a->name, s, sizeof (a->name)); // copy the rest of the command line cmd[0] = 0; // start out with a null string c = Cmd_Argc(); for (i=2 ; i< c ; i++) { - strcat (cmd, Cmd_Argv(i)); + strlcat (cmd, Cmd_Argv(i), sizeof (cmd)); if (i != c) - strcat (cmd, " "); + strlcat (cmd, " ", sizeof (cmd)); } - strcat (cmd, "\n"); + strlcat (cmd, "\n", sizeof (cmd)); a->value = CopyString (cmd); } @@ -397,18 +403,18 @@ static void Cmd_Alias_f (void) typedef struct cmd_function_s { - struct cmd_function_s *next; - char *name; - xcommand_t function; + struct cmd_function_s *next; + const char *name; + xcommand_t function; } cmd_function_t; #define MAX_ARGS 80 static int cmd_argc; -static char *cmd_argv[MAX_ARGS]; -static char *cmd_null_string = ""; -static char *cmd_args = NULL; +static const char *cmd_argv[MAX_ARGS]; +static const char *cmd_null_string = ""; +static const char *cmd_args = NULL; cmd_source_t cmd_source; @@ -426,21 +432,24 @@ Cmd_List */ static void Cmd_List_f (void) { - cmd_function_t *cmd; - char *partial; - int len; - int count; + cmd_function_t *cmd; + const char *partial; + int len, count; - if (Cmd_Argc() > 1) { + if (Cmd_Argc() > 1) + { partial = Cmd_Argv (1); len = strlen(partial); - } else { + } + else + { partial = NULL; len = 0; } count = 0; - for (cmd = cmd_functions; cmd; cmd = cmd->next) { + for (cmd = cmd_functions; cmd; cmd = cmd->next) + { if (partial && strncmp(partial, cmd->name, len)) continue; Con_Printf ("%s\n", cmd->name); @@ -492,7 +501,7 @@ int Cmd_Argc (void) Cmd_Argv ============ */ -char *Cmd_Argv (int arg) +const char *Cmd_Argv (int arg) { if (arg >= cmd_argc ) return cmd_null_string; @@ -504,7 +513,7 @@ char *Cmd_Argv (int arg) Cmd_Args ============ */ -char *Cmd_Args (void) +const char *Cmd_Args (void) { return cmd_args; } @@ -517,27 +526,22 @@ Cmd_TokenizeString Parses the given string into command line tokens. ============ */ -static void Cmd_TokenizeString (char *text) +static void Cmd_TokenizeString (const char *text) { - int i; - -// clear the args from the last string - for (i=0 ; i CMD_TOKENIZELENGTH) + Sys_Error("Cmd_TokenizeString: ran out of %i character buffer space for command arguements\n", CMD_TOKENIZELENGTH); + strcpy (cmd_tokenizebuffer + cmd_tokenizebufferpos, com_token); + cmd_argv[cmd_argc] = cmd_tokenizebuffer + cmd_tokenizebufferpos; + cmd_tokenizebufferpos += l; cmd_argc++; } } @@ -568,12 +575,9 @@ static void Cmd_TokenizeString (char *text) Cmd_AddCommand ============ */ -void Cmd_AddCommand (char *cmd_name, xcommand_t function) +void Cmd_AddCommand (const char *cmd_name, xcommand_t function) { - cmd_function_t *cmd; - -// if (host_initialized) // because hunk allocation would get stomped -// Sys_Error ("Cmd_AddCommand after host_initialized"); + cmd_function_t *cmd; // fail if the command is a variable name if (Cvar_VariableString(cmd_name)[0]) @@ -604,7 +608,7 @@ void Cmd_AddCommand (char *cmd_name, xcommand_t function) Cmd_Exists ============ */ -qboolean Cmd_Exists (char *cmd_name) +qboolean Cmd_Exists (const char *cmd_name) { cmd_function_t *cmd; @@ -616,16 +620,15 @@ qboolean Cmd_Exists (char *cmd_name) } - /* ============ Cmd_CompleteCommand ============ */ -char *Cmd_CompleteCommand (char *partial) +const char *Cmd_CompleteCommand (const char *partial) { - cmd_function_t *cmd; - int len; + cmd_function_t *cmd; + int len; len = strlen(partial); @@ -649,11 +652,10 @@ char *Cmd_CompleteCommand (char *partial) Thanks to taniwha */ -int Cmd_CompleteCountPossible (char *partial) +int Cmd_CompleteCountPossible (const char *partial) { - cmd_function_t *cmd; - int len; - int h; + cmd_function_t *cmd; + int len, h; h = 0; len = strlen(partial); @@ -678,16 +680,16 @@ int Cmd_CompleteCountPossible (char *partial) Thanks to taniwha */ -char **Cmd_CompleteBuildList (char *partial) +const char **Cmd_CompleteBuildList (const char *partial) { - cmd_function_t *cmd; - int len = 0; - int bpos = 0; - int sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (char *); - char **buf; + cmd_function_t *cmd; + int len = 0; + int bpos = 0; + int sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (const char *); + const char **buf; len = strlen(partial); - buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (char *)); + buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *)); // Loop through the alias list and print all matches for (cmd = cmd_functions; cmd; cmd = cmd->next) if (!strncasecmp(partial, cmd->name, len)) @@ -706,10 +708,10 @@ char **Cmd_CompleteBuildList (char *partial) Thanks to taniwha */ -char *Cmd_CompleteAlias (char * partial) +const char *Cmd_CompleteAlias (const char *partial) { - cmdalias_t *alias; - int len; + cmdalias_t *alias; + int len; len = strlen(partial); @@ -733,7 +735,7 @@ char *Cmd_CompleteAlias (char * partial) Thanks to taniwha */ -int Cmd_CompleteAliasCountPossible (char *partial) +int Cmd_CompleteAliasCountPossible (const char *partial) { cmdalias_t *alias; int len; @@ -763,16 +765,16 @@ int Cmd_CompleteAliasCountPossible (char *partial) Thanks to taniwha */ -char **Cmd_CompleteAliasBuildList (char *partial) +const char **Cmd_CompleteAliasBuildList (const char *partial) { - cmdalias_t *alias; - int len = 0; - int bpos = 0; - int sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (char *); - char **buf; + cmdalias_t *alias; + int len = 0; + int bpos = 0; + int sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (const char *); + const char **buf; len = strlen(partial); - buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (char *)); + buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *)); // Loop through the alias list and print all matches for (alias = cmd_alias; alias; alias = alias->next) if (!strncasecmp(partial, alias->name, len)) @@ -790,24 +792,30 @@ A complete command line has been parsed, so try to execute it FIXME: lookupnoadd the token to speed search? ============ */ -void Cmd_ExecuteString (char *text, cmd_source_t src) +void Cmd_ExecuteString (const char *text, cmd_source_t src) { - cmd_function_t *cmd; - cmdalias_t *a; + int oldpos; + cmd_function_t *cmd; + cmdalias_t *a; + oldpos = cmd_tokenizebufferpos; cmd_source = src; Cmd_TokenizeString (text); // execute the command line if (!Cmd_Argc()) + { + cmd_tokenizebufferpos = oldpos; return; // no tokens + } // check functions for (cmd=cmd_functions ; cmd ; cmd=cmd->next) { - if (!Q_strcasecmp (cmd_argv[0],cmd->name)) + if (!strcasecmp (cmd_argv[0],cmd->name)) { cmd->function (); + cmd_tokenizebufferpos = oldpos; return; } } @@ -815,9 +823,10 @@ void Cmd_ExecuteString (char *text, cmd_source_t src) // check alias for (a=cmd_alias ; a ; a=a->next) { - if (!Q_strcasecmp (cmd_argv[0], a->name)) + if (!strcasecmp (cmd_argv[0], a->name)) { Cbuf_InsertText (a->value); + cmd_tokenizebufferpos = oldpos; return; } } @@ -825,6 +834,8 @@ void Cmd_ExecuteString (char *text, cmd_source_t src) // check cvars if (!Cvar_Command ()) Con_Printf ("Unknown command \"%s\"\n", Cmd_Argv(0)); + + cmd_tokenizebufferpos = oldpos; } @@ -847,7 +858,7 @@ void Cmd_ForwardToServer (void) return; // not really connected MSG_WriteByte (&cls.message, clc_stringcmd); - if (Q_strcasecmp(Cmd_Argv(0), "cmd") != 0) + if (strcasecmp(Cmd_Argv(0), "cmd") != 0) { SZ_Print (&cls.message, Cmd_Argv(0)); SZ_Print (&cls.message, " "); @@ -868,7 +879,7 @@ where the given parameter apears, or 0 if not present ================ */ -int Cmd_CheckParm (char *parm) +int Cmd_CheckParm (const char *parm) { int i; @@ -876,7 +887,7 @@ int Cmd_CheckParm (char *parm) Sys_Error ("Cmd_CheckParm: NULL"); for (i = 1; i < Cmd_Argc (); i++) - if (!Q_strcasecmp (parm, Cmd_Argv (i))) + if (!strcasecmp (parm, Cmd_Argv (i))) return i; return 0;