2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // cmd.c -- Quake script command processing module
24 #define MAX_ALIAS_NAME 32
25 // this is the largest script file that can be executed in one step
26 // LordHavoc: inreased this from 8192 to 32768
27 #define CMDBUFSIZE 32768
28 // maximum number of parameters to a command
30 // maximum tokenizable commandline length (counting NUL terminations)
31 #define CMD_TOKENIZELENGTH (MAX_INPUTLINE + 80)
33 typedef struct cmdalias_s
35 struct cmdalias_s *next;
36 char name[MAX_ALIAS_NAME];
40 static cmdalias_t *cmd_alias;
42 static qboolean cmd_wait;
44 static mempool_t *cmd_mempool;
46 static char cmd_tokenizebuffer[CMD_TOKENIZELENGTH];
47 static int cmd_tokenizebufferpos = 0;
49 //=============================================================================
55 Causes execution of the remainder of the command buffer to be delayed until
56 next frame. This allows commands like:
57 bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
60 static void Cmd_Wait_f (void)
66 =============================================================================
70 =============================================================================
73 static sizebuf_t cmd_text;
74 static unsigned char cmd_text_buf[CMDBUFSIZE];
80 Adds command text at the end of the buffer
83 void Cbuf_AddText (const char *text)
87 l = (int)strlen (text);
89 if (cmd_text.cursize + l >= cmd_text.maxsize)
91 Con_Print("Cbuf_AddText: overflow\n");
95 SZ_Write (&cmd_text, (const unsigned char *)text, (int)strlen (text));
103 Adds command text immediately after the current command
104 Adds a \n to the text
105 FIXME: actually change the command buffer to do less copying
108 void Cbuf_InsertText (const char *text)
113 // copy off any commands still remaining in the exec buffer
114 templen = cmd_text.cursize;
117 temp = (char *)Mem_Alloc (tempmempool, templen);
118 memcpy (temp, cmd_text.data, templen);
119 SZ_Clear (&cmd_text);
124 // add the entire text of the file
127 // add the copied off data
130 SZ_Write (&cmd_text, (const unsigned char *)temp, templen);
140 static void Cmd_PreprocessString( const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias );
141 void Cbuf_Execute (void)
145 char line[MAX_INPUTLINE];
146 char preprocessed[MAX_INPUTLINE];
150 // LordHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
151 cmd_tokenizebufferpos = 0;
153 while (cmd_text.cursize)
155 // find a \n or ; line break
156 text = (char *)cmd_text.data;
159 for (i=0 ; i< cmd_text.cursize ; i++)
163 if (text[i] == '\\' && (text[i+1] == '"' || text[i+1] == '\\'))
165 if ( !quotes && text[i] == ';')
166 break; // don't break if inside a quoted string
167 if (text[i] == '\r' || text[i] == '\n')
171 memcpy (line, text, i);
174 // delete the text from the command buffer and move remaining commands down
175 // this is necessary because commands (exec, alias) can insert data at the
176 // beginning of the text buffer
178 if (i == cmd_text.cursize)
179 cmd_text.cursize = 0;
183 cmd_text.cursize -= i;
184 memcpy (cmd_text.data, text+i, cmd_text.cursize);
187 // execute the command line
188 firstchar = line + strspn(line, " \t");
190 (strncmp(firstchar, "alias", 5) || (firstchar[5] != ' ' && firstchar[5] != '\t'))
192 (strncmp(firstchar, "bind", 4) || (firstchar[4] != ' ' && firstchar[4] != '\t'))
194 (strncmp(firstchar, "in_bind", 7) || (firstchar[7] != ' ' && firstchar[7] != '\t'))
197 Cmd_PreprocessString( line, preprocessed, sizeof(preprocessed), NULL );
198 Cmd_ExecuteString (preprocessed, src_command);
202 Cmd_ExecuteString (line, src_command);
206 { // skip out while text still remains in buffer, leaving it
215 ==============================================================================
219 ==============================================================================
226 Adds command line parameters as script statements
227 Commands lead with a +, and continue until a - or another +
228 quake +prog jctest.qp +cmd amlev1
229 quake -nosound +cmd amlev1
232 qboolean host_stuffcmdsrun = false;
233 void Cmd_StuffCmds_f (void)
236 // this is for all commandline options combined (and is bounds checked)
237 char build[MAX_INPUTLINE];
239 if (Cmd_Argc () != 1)
241 Con_Print("stuffcmds : execute command line parameters\n");
245 // no reason to run the commandline arguments twice
246 if (host_stuffcmdsrun)
249 host_stuffcmdsrun = true;
252 for (i = 0;i < com_argc;i++)
254 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)
257 while (com_argv[i][j])
258 build[l++] = com_argv[i][j++];
260 for (;i < com_argc;i++)
264 if ((com_argv[i][0] == '+' || com_argv[i][0] == '-') && (com_argv[i][1] < '0' || com_argv[i][1] > '9'))
266 if (l + strlen(com_argv[i]) + 4 > sizeof(build) - 1)
269 if (strchr(com_argv[i], ' '))
271 for (j = 0;com_argv[i][j];j++)
272 build[l++] = com_argv[i][j];
273 if (strchr(com_argv[i], ' '))
280 // now terminate the combined string and prepend it to the command buffer
281 // we already reserved space for the terminator
283 Cbuf_InsertText (build);
292 static void Cmd_Exec_f (void)
296 if (Cmd_Argc () != 2)
298 Con_Print("exec <filename> : execute a script file\n");
302 f = (char *)FS_LoadFile (Cmd_Argv(1), tempmempool, false, NULL);
305 Con_Printf("couldn't exec %s\n",Cmd_Argv(1));
308 Con_DPrintf("execing %s\n",Cmd_Argv(1));
310 // if executing default.cfg for the first time, lock the cvar defaults
311 // it may seem backwards to insert this text BEFORE the default.cfg
312 // but Cbuf_InsertText inserts before, so this actually ends up after it.
313 if (!strcmp(Cmd_Argv(1), "default.cfg"))
314 Cbuf_InsertText("\ncvar_lockdefaults\n");
316 // insert newline after the text to make sure the last line is terminated (some text editors omit the trailing newline)
317 // (note: insertion order here is backwards from execution order, so this adds it after the text, by calling it before...)
318 Cbuf_InsertText ("\n");
328 Just prints the rest of the line to the console
331 static void Cmd_Echo_f (void)
335 for (i=1 ; i<Cmd_Argc() ; i++)
336 Con_Printf("%s ",Cmd_Argv(i));
341 // Support Doom3-style Toggle Console Command
346 Toggles a specified console variable amongst the values specified (default is 0 and 1)
349 static void Cmd_Toggle_f(void)
351 // Acquire Number of Arguments
352 int nNumArgs = Cmd_Argc();
355 // No Arguments Specified; Print Usage
356 Con_Print("Toggle Console Variable - Usage\n toggle <variable> - toggles between 0 and 1\n toggle <variable> <value> - toggles between 0 and <value>\n toggle <variable> [string 1] [string 2]...[string n] - cycles through all strings\n");
358 { // Correct Arguments Specified
359 // Acquire Potential CVar
360 cvar_t* cvCVar = Cvar_FindVar( Cmd_Argv(1) );
367 Cvar_SetValueQuick(cvCVar, 0);
369 Cvar_SetValueQuick(cvCVar, 1);
373 { // 0 and Specified Usage
374 if(cvCVar->integer == atoi(Cmd_Argv(2) ) )
375 // CVar is Specified Value; // Reset to 0
376 Cvar_SetValueQuick(cvCVar, 0);
378 if(cvCVar->integer == 0)
379 // CVar is 0; Specify Value
380 Cvar_SetQuick(cvCVar, Cmd_Argv(2) );
382 // CVar does not match; Reset to 0
383 Cvar_SetValueQuick(cvCVar, 0);
386 { // Variable Values Specified
390 for(nCnt = 2; nCnt < nNumArgs; nCnt++)
391 { // Cycle through Values
392 if( strcmp(cvCVar->string, Cmd_Argv(nCnt) ) == 0)
393 { // Current Value Located; Increment to Next
394 if( (nCnt + 1) == nNumArgs)
395 // Max Value Reached; Reset
396 Cvar_SetQuick(cvCVar, Cmd_Argv(2) );
399 Cvar_SetQuick(cvCVar, Cmd_Argv(nCnt + 1) );
408 // Value not Found; Reset to Original
409 Cvar_SetQuick(cvCVar, Cmd_Argv(2) );
415 Con_Printf("ERROR : CVar '%s' not found\n", Cmd_Argv(2) );
424 Creates a new command that executes a command string (possibly ; seperated)
427 static void Cmd_Alias_f (void)
430 char cmd[MAX_INPUTLINE];
437 Con_Print("Current alias commands:\n");
438 for (a = cmd_alias ; a ; a=a->next)
439 Con_Printf("%s : %s\n", a->name, a->value);
444 if (strlen(s) >= MAX_ALIAS_NAME)
446 Con_Print("Alias name is too long\n");
450 // if the alias already exists, reuse it
451 for (a = cmd_alias ; a ; a=a->next)
453 if (!strcmp(s, a->name))
462 cmdalias_t *prev, *current;
464 a = (cmdalias_t *)Z_Malloc (sizeof(cmdalias_t));
465 strlcpy (a->name, s, sizeof (a->name));
466 // insert it at the right alphanumeric position
467 for( prev = NULL, current = cmd_alias ; current && strcmp( current->name, a->name ) < 0 ; prev = current, current = current->next )
478 // copy the rest of the command line
479 cmd[0] = 0; // start out with a null string
481 for (i=2 ; i< c ; i++)
483 strlcat (cmd, Cmd_Argv(i), sizeof (cmd));
485 strlcat (cmd, " ", sizeof (cmd));
487 strlcat (cmd, "\n", sizeof (cmd));
489 alloclen = strlen (cmd) + 1;
490 a->value = (char *)Z_Malloc (alloclen);
491 memcpy (a->value, cmd, alloclen);
495 =============================================================================
499 =============================================================================
502 typedef struct cmd_function_s
504 struct cmd_function_s *next;
506 const char *description;
507 xcommand_t consolefunction;
508 xcommand_t clientfunction;
513 static const char *cmd_argv[MAX_ARGS];
514 static const char *cmd_null_string = "";
515 static const char *cmd_args;
516 cmd_source_t cmd_source;
519 static cmd_function_t *cmd_functions; // possible commands to execute
521 static const char *Cmd_GetDirectCvarValue(const char *varname, cmdalias_t *alias, qboolean *is_multiple)
528 *is_multiple = false;
530 if(!varname || !*varname)
535 if(!strcmp(varname, "*"))
541 else if(varname[strlen(varname) - 1] == '-')
543 argno = strtol(varname, &endptr, 10);
544 if(endptr == varname + strlen(varname) - 1)
546 // whole string is a number, apart from the -
547 const char *p = Cmd_Args();
548 for(; argno > 1; --argno)
549 if(!COM_ParseToken_Console(&p))
556 // kill pre-argument whitespace
557 for (;*p && *p <= ' ';p++)
566 argno = strtol(varname, &endptr, 10);
569 // whole string is a number
570 // NOTE: we already made sure we don't have an empty cvar name!
571 if(argno >= 0 && argno < Cmd_Argc())
572 return Cmd_Argv(argno);
577 if((cvar = Cvar_FindVar(varname)) && !(cvar->flags & CVAR_PRIVATE))
583 qboolean Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset)
585 qboolean quote_quot = !!strchr(quoteset, '"');
586 qboolean quote_backslash = !!strchr(quoteset, '\\');
587 qboolean quote_dollar = !!strchr(quoteset, '$');
591 if(*in == '"' && quote_quot)
598 *out++ = '\\'; --outlen;
599 *out++ = '"'; --outlen;
601 else if(*in == '\\' && quote_backslash)
608 *out++ = '\\'; --outlen;
609 *out++ = '\\'; --outlen;
611 else if(*in == '$' && quote_dollar)
618 *out++ = '$'; --outlen;
619 *out++ = '$'; --outlen;
628 *out++ = *in; --outlen;
636 static const char *Cmd_GetCvarValue(const char *var, size_t varlen, cmdalias_t *alias)
638 static char varname[MAX_INPUTLINE];
639 static char varval[MAX_INPUTLINE];
643 if(varlen >= MAX_INPUTLINE)
644 varlen = MAX_INPUTLINE - 1;
645 memcpy(varname, var, varlen);
647 varfunc = strchr(varname, ' ');
663 if(varname[0] == '$')
664 varstr = Cmd_GetDirectCvarValue(Cmd_GetDirectCvarValue(varname + 1, alias, NULL), alias, NULL);
667 qboolean is_multiple = false;
668 // Exception: $* and $n- don't use the quoted form by default
669 varstr = Cmd_GetDirectCvarValue(varname, alias, &is_multiple);
677 Con_Printf("Warning: Could not expand $%s in alias %s\n", varname, alias->name);
679 Con_Printf("Warning: Could not expand $%s\n", varname);
683 if(!varfunc || !strcmp(varfunc, "q")) // note: quoted form is default, use "asis" to override!
685 // quote it so it can be used inside double quotes
686 // we just need to replace " by \", and of course, double backslashes
687 Cmd_QuoteString(varval, sizeof(varval), varstr, "\"\\");
690 else if(!strcmp(varfunc, "asis"))
695 Con_Printf("Unknown variable function %s\n", varfunc);
703 Preprocesses strings and replaces $*, $param#, $cvar accordingly
705 static void Cmd_PreprocessString( const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias ) {
711 // don't crash if there's no room in the outtext buffer
712 if( maxoutlen == 0 ) {
715 maxoutlen--; // because of \0
720 while( *in && outlen < maxoutlen ) {
722 // this is some kind of expansion, see what comes after the $
725 // The console does the following preprocessing:
727 // - $$ is transformed to a single dollar sign.
728 // - $var or ${var} are expanded to the contents of the named cvar,
729 // with quotation marks and backslashes quoted so it can safely
730 // be used inside quotation marks (and it should always be used
732 // - ${var asis} inserts the cvar value as is, without doing this
734 // - prefix the cvar name with a dollar sign to do indirection;
735 // for example, if $x has the value timelimit, ${$x} will return
736 // the value of $timelimit
737 // - when expanding an alias, the special variable name $* refers
738 // to all alias parameters, and a number refers to that numbered
739 // alias parameter, where the name of the alias is $0, the first
740 // parameter is $1 and so on; as a special case, $* inserts all
741 // parameters, without extra quoting, so one can use $* to just
742 // pass all parameters around. All parameters starting from $n
743 // can be referred to as $n- (so $* is equivalent to $1-).
745 // Note: when expanding an alias, cvar expansion is done in the SAME step
746 // as alias expansion so that alias parameters or cvar values containing
747 // dollar signs have no unwanted bad side effects. However, this needs to
748 // be accounted for when writing complex aliases. For example,
749 // alias foo "set x NEW; echo $x"
750 // actually expands to
751 // "set x NEW; echo OLD"
752 // and will print OLD! To work around this, use a second alias:
753 // alias foo "set x NEW; foo2"
754 // alias foo2 "echo $x"
756 // Also note: lines starting with alias are exempt from cvar expansion.
757 // If you want cvar expansion, write "alias" instead:
760 // alias foo "echo $x"
761 // "alias" bar "echo $x"
764 // foo will print 2, because the variable $x will be expanded when the alias
765 // gets expanded. bar will print 1, because the variable $x was expanded
766 // at definition time. foo can be equivalently defined as
768 // "alias" foo "echo $$x"
770 // because at definition time, $$ will get replaced to a single $.
775 } else if(*in == '{') {
776 varlen = strcspn(in + 1, "}");
777 if(in[varlen + 1] == '}')
779 val = Cmd_GetCvarValue(in + 1, varlen, alias);
789 varlen = strspn(in, "*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-");
790 val = Cmd_GetCvarValue(in, varlen, alias);
795 // insert the cvar value
796 while(*val && outlen < maxoutlen)
797 outtext[outlen++] = *val++;
802 // copy the unexpanded text
803 outtext[outlen++] = '$';
804 while(eat && outlen < maxoutlen)
806 outtext[outlen++] = *in++;
811 outtext[outlen++] = *in++;
821 Called for aliases and fills in the alias into the cbuffer
824 static void Cmd_ExecuteAlias (cmdalias_t *alias)
826 static char buffer[ MAX_INPUTLINE + 2 ];
827 static char buffer2[ MAX_INPUTLINE * 2 + 2 ];
828 Cmd_PreprocessString( alias->value, buffer, sizeof(buffer) - 2, alias );
829 // insert at start of command buffer, so that aliases execute in order
830 // (fixes bug introduced by Black on 20050705)
832 // Note: Cbuf_PreprocessString will be called on this string AGAIN! So we
833 // have to make sure that no second variable expansion takes place, otherwise
834 // alias parameters containing dollar signs can have bad effects.
835 Cmd_QuoteString(buffer2, sizeof(buffer2), buffer, "$");
836 Cbuf_InsertText( buffer2 );
843 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
844 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
848 static void Cmd_List_f (void)
856 partial = Cmd_Argv (1);
857 len = (int)strlen(partial);
866 for (cmd = cmd_functions; cmd; cmd = cmd->next)
868 if (partial && strncmp(partial, cmd->name, len))
870 Con_Printf("%s : %s\n", cmd->name, cmd->description);
875 Con_Printf("%i Command%s beginning with \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
877 Con_Printf("%i Command%s\n\n", count, (count > 1) ? "s" : "");
887 cmd_mempool = Mem_AllocPool("commands", 0, NULL);
888 // space for commands and script files
889 cmd_text.data = cmd_text_buf;
890 cmd_text.maxsize = sizeof(cmd_text_buf);
891 cmd_text.cursize = 0;
894 void Cmd_Init_Commands (void)
897 // register our commands
899 Cmd_AddCommand ("stuffcmds",Cmd_StuffCmds_f, "execute commandline parameters (must be present in quake.rc script)");
900 Cmd_AddCommand ("exec",Cmd_Exec_f, "execute a script file");
901 Cmd_AddCommand ("echo",Cmd_Echo_f, "print a message to the console (useful in scripts)");
902 Cmd_AddCommand ("alias",Cmd_Alias_f, "create a script function (parameters are passed in as $1 through $9, and $* for all parameters)");
903 Cmd_AddCommand ("cmd", Cmd_ForwardToServer, "send a console commandline to the server (used by some mods)");
904 Cmd_AddCommand ("wait", Cmd_Wait_f, "make script execution wait for next rendered frame");
905 Cmd_AddCommand ("set", Cvar_Set_f, "create or change the value of a console variable");
906 Cmd_AddCommand ("seta", Cvar_SetA_f, "create or change the value of a console variable that will be saved to config.cfg");
908 // 2000-01-09 CmdList, CvarList commands By Matthias "Maddes" Buecher
909 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
910 Cmd_AddCommand ("cmdlist", Cmd_List_f, "lists all console commands beginning with the specified prefix");
911 Cmd_AddCommand ("cvarlist", Cvar_List_f, "lists all console variables beginning with the specified prefix");
913 Cmd_AddCommand ("cvar_lockdefaults", Cvar_LockDefaults_f, "stores the current values of all cvars into their default values, only used once during startup after parsing default.cfg");
914 Cmd_AddCommand ("cvar_resettodefaults_all", Cvar_ResetToDefaults_All_f, "sets all cvars to their locked default values");
915 Cmd_AddCommand ("cvar_resettodefaults_nosaveonly", Cvar_ResetToDefaults_NoSaveOnly_f, "sets all non-saved cvars to their locked default values (variables that will not be saved to config.cfg)");
916 Cmd_AddCommand ("cvar_resettodefaults_saveonly", Cvar_ResetToDefaults_SaveOnly_f, "sets all saved cvars to their locked default values (variables that will be saved to config.cfg)");
919 // Support Doom3-style Toggle Command
920 Cmd_AddCommand( "toggle", Cmd_Toggle_f, "toggles a console variable's values (use for more info)");
928 void Cmd_Shutdown(void)
930 Mem_FreePool(&cmd_mempool);
948 const char *Cmd_Argv (int arg)
950 if (arg >= cmd_argc )
951 return cmd_null_string;
952 return cmd_argv[arg];
960 const char *Cmd_Args (void)
970 Parses the given string into command line tokens.
973 // AK: This function should only be called from ExcuteString because the current design is a bit of an hack
974 static void Cmd_TokenizeString (const char *text)
983 // skip whitespace up to a /n
984 while (*text && *text <= ' ' && *text != '\r' && *text != '\n')
991 if (*text == '\n' || *text == '\r')
993 // a newline separates commands in the buffer
994 if (*text == '\r' && text[1] == '\n')
1006 if (!COM_ParseToken_Console(&text))
1009 if (cmd_argc < MAX_ARGS)
1011 l = (int)strlen(com_token) + 1;
1012 if (cmd_tokenizebufferpos + l > CMD_TOKENIZELENGTH)
1014 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguements\n", CMD_TOKENIZELENGTH);
1017 memcpy (cmd_tokenizebuffer + cmd_tokenizebufferpos, com_token, l);
1018 cmd_argv[cmd_argc] = cmd_tokenizebuffer + cmd_tokenizebufferpos;
1019 cmd_tokenizebufferpos += l;
1031 void Cmd_AddCommand_WithClientCommand (const char *cmd_name, xcommand_t consolefunction, xcommand_t clientfunction, const char *description)
1033 cmd_function_t *cmd;
1034 cmd_function_t *prev, *current;
1036 // fail if the command is a variable name
1037 if (Cvar_FindVar( cmd_name ))
1039 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
1043 // fail if the command already exists
1044 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1046 if (!strcmp (cmd_name, cmd->name))
1048 if (consolefunction || clientfunction)
1050 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
1055 cmd->csqcfunc = true;
1061 cmd = (cmd_function_t *)Mem_Alloc(cmd_mempool, sizeof(cmd_function_t));
1062 cmd->name = cmd_name;
1063 cmd->consolefunction = consolefunction;
1064 cmd->clientfunction = clientfunction;
1065 cmd->description = description;
1066 if(!consolefunction && !clientfunction) //[515]: csqc
1067 cmd->csqcfunc = true;
1068 cmd->next = cmd_functions;
1070 // insert it at the right alphanumeric position
1071 for( prev = NULL, current = cmd_functions ; current && strcmp( current->name, cmd->name ) < 0 ; prev = current, current = current->next )
1076 cmd_functions = cmd;
1078 cmd->next = current;
1081 void Cmd_AddCommand (const char *cmd_name, xcommand_t function, const char *description)
1083 Cmd_AddCommand_WithClientCommand (cmd_name, function, NULL, description);
1091 qboolean Cmd_Exists (const char *cmd_name)
1093 cmd_function_t *cmd;
1095 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1096 if (!strcmp (cmd_name,cmd->name))
1108 const char *Cmd_CompleteCommand (const char *partial)
1110 cmd_function_t *cmd;
1113 len = strlen(partial);
1119 for (cmd = cmd_functions; cmd; cmd = cmd->next)
1120 if (!strncasecmp(partial, cmd->name, len))
1127 Cmd_CompleteCountPossible
1129 New function for tab-completion system
1130 Added by EvilTypeGuy
1131 Thanks to Fett erich@heintz.com
1135 int Cmd_CompleteCountPossible (const char *partial)
1137 cmd_function_t *cmd;
1142 len = strlen(partial);
1147 // Loop through the command list and count all partial matches
1148 for (cmd = cmd_functions; cmd; cmd = cmd->next)
1149 if (!strncasecmp(partial, cmd->name, len))
1156 Cmd_CompleteBuildList
1158 New function for tab-completion system
1159 Added by EvilTypeGuy
1160 Thanks to Fett erich@heintz.com
1164 const char **Cmd_CompleteBuildList (const char *partial)
1166 cmd_function_t *cmd;
1169 size_t sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (const char *);
1172 len = strlen(partial);
1173 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
1174 // Loop through the alias list and print all matches
1175 for (cmd = cmd_functions; cmd; cmd = cmd->next)
1176 if (!strncasecmp(partial, cmd->name, len))
1177 buf[bpos++] = cmd->name;
1183 // written by LordHavoc
1184 void Cmd_CompleteCommandPrint (const char *partial)
1186 cmd_function_t *cmd;
1187 size_t len = strlen(partial);
1188 // Loop through the command list and print all matches
1189 for (cmd = cmd_functions; cmd; cmd = cmd->next)
1190 if (!strncasecmp(partial, cmd->name, len))
1191 Con_Printf("%s : %s\n", cmd->name, cmd->description);
1197 New function for tab-completion system
1198 Added by EvilTypeGuy
1199 Thanks to Fett erich@heintz.com
1203 const char *Cmd_CompleteAlias (const char *partial)
1208 len = strlen(partial);
1214 for (alias = cmd_alias; alias; alias = alias->next)
1215 if (!strncasecmp(partial, alias->name, len))
1221 // written by LordHavoc
1222 void Cmd_CompleteAliasPrint (const char *partial)
1225 size_t len = strlen(partial);
1226 // Loop through the alias list and print all matches
1227 for (alias = cmd_alias; alias; alias = alias->next)
1228 if (!strncasecmp(partial, alias->name, len))
1229 Con_Printf("%s : %s\n", alias->name, alias->value);
1234 Cmd_CompleteAliasCountPossible
1236 New function for tab-completion system
1237 Added by EvilTypeGuy
1238 Thanks to Fett erich@heintz.com
1242 int Cmd_CompleteAliasCountPossible (const char *partial)
1250 len = strlen(partial);
1255 // Loop through the command list and count all partial matches
1256 for (alias = cmd_alias; alias; alias = alias->next)
1257 if (!strncasecmp(partial, alias->name, len))
1264 Cmd_CompleteAliasBuildList
1266 New function for tab-completion system
1267 Added by EvilTypeGuy
1268 Thanks to Fett erich@heintz.com
1272 const char **Cmd_CompleteAliasBuildList (const char *partial)
1277 size_t sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (const char *);
1280 len = strlen(partial);
1281 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
1282 // Loop through the alias list and print all matches
1283 for (alias = cmd_alias; alias; alias = alias->next)
1284 if (!strncasecmp(partial, alias->name, len))
1285 buf[bpos++] = alias->name;
1291 void Cmd_ClearCsqcFuncs (void)
1293 cmd_function_t *cmd;
1294 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1295 cmd->csqcfunc = false;
1298 qboolean CL_VM_ConsoleCommand (const char *cmd);
1303 A complete command line has been parsed, so try to execute it
1304 FIXME: lookupnoadd the token to speed search?
1307 void Cmd_ExecuteString (const char *text, cmd_source_t src)
1310 cmd_function_t *cmd;
1313 oldpos = cmd_tokenizebufferpos;
1316 Cmd_TokenizeString (text);
1318 // execute the command line
1321 cmd_tokenizebufferpos = oldpos;
1322 return; // no tokens
1326 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1328 if (!strcasecmp (cmd_argv[0],cmd->name))
1330 if (cmd->csqcfunc && CL_VM_ConsoleCommand (text)) //[515]: csqc
1335 if (cmd->consolefunction)
1336 cmd->consolefunction ();
1337 else if (cmd->clientfunction)
1339 if (cls.state == ca_connected)
1341 // forward remote commands to the server for execution
1342 Cmd_ForwardToServer();
1345 Con_Printf("Can not send command \"%s\", not connected.\n", Cmd_Argv(0));
1348 Con_Printf("Command \"%s\" can not be executed\n", Cmd_Argv(0));
1349 cmd_tokenizebufferpos = oldpos;
1352 if (cmd->clientfunction)
1354 cmd->clientfunction ();
1355 cmd_tokenizebufferpos = oldpos;
1364 // if it's a client command and no command was found, say so.
1365 if (cmd_source == src_client)
1367 Con_Printf("player \"%s\" tried to %s\n", host_client->name, text);
1372 for (a=cmd_alias ; a ; a=a->next)
1374 if (!strcasecmp (cmd_argv[0], a->name))
1376 Cmd_ExecuteAlias(a);
1377 cmd_tokenizebufferpos = oldpos;
1383 if (!Cvar_Command () && host_framecount > 0)
1384 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(0));
1386 cmd_tokenizebufferpos = oldpos;
1392 Cmd_ForwardStringToServer
1394 Sends an entire command string over to the server, unprocessed
1397 void Cmd_ForwardStringToServer (const char *s)
1400 if (cls.state != ca_connected)
1402 Con_Printf("Can't \"%s\", not connected\n", s);
1409 // LordHavoc: thanks to Fuh for bringing the pure evil of SZ_Print to my
1410 // attention, it has been eradicated from here, its only (former) use in
1411 // all of darkplaces.
1412 if (cls.protocol == PROTOCOL_QUAKEWORLD)
1413 MSG_WriteByte(&cls.netcon->message, qw_clc_stringcmd);
1415 MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
1416 if ((!strncmp(s, "say ", 4) || !strncmp(s, "say_team ", 9)) && cl_locs_enable.integer)
1418 // say/say_team commands can replace % character codes with status info
1421 if (*s == '%' && s[1])
1423 // handle proquake message macros
1427 case 'l': // current location
1428 CL_Locs_FindLocationName(temp, sizeof(temp), cl.movement_origin);
1430 case 'h': // current health
1431 dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_HEALTH]);
1433 case 'a': // current armor
1434 dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_ARMOR]);
1436 case 'x': // current rockets
1437 dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_ROCKETS]);
1439 case 'c': // current cells
1440 dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_CELLS]);
1442 // silly proquake macros
1443 case 'd': // loc at last death
1444 CL_Locs_FindLocationName(temp, sizeof(temp), cl.lastdeathorigin);
1446 case 't': // current time
1447 dpsnprintf(temp, sizeof(temp), "%.0f:%.0f", floor(cl.time / 60), cl.time - floor(cl.time / 60) * 60);
1449 case 'r': // rocket launcher status ("I have RL", "I need rockets", "I need RL")
1450 if (!(cl.stats[STAT_ITEMS] & IT_ROCKET_LAUNCHER))
1451 dpsnprintf(temp, sizeof(temp), "I need RL");
1452 else if (!cl.stats[STAT_ROCKETS])
1453 dpsnprintf(temp, sizeof(temp), "I need rockets");
1455 dpsnprintf(temp, sizeof(temp), "I have RL");
1457 case 'p': // powerup status (outputs "quad" "pent" and "eyes" according to status)
1458 if (cl.stats[STAT_ITEMS] & IT_QUAD)
1461 strlcat(temp, " ", sizeof(temp));
1462 strlcat(temp, "quad", sizeof(temp));
1464 if (cl.stats[STAT_ITEMS] & IT_INVULNERABILITY)
1467 strlcat(temp, " ", sizeof(temp));
1468 strlcat(temp, "pent", sizeof(temp));
1470 if (cl.stats[STAT_ITEMS] & IT_INVISIBILITY)
1473 strlcat(temp, " ", sizeof(temp));
1474 strlcat(temp, "eyes", sizeof(temp));
1477 case 'w': // weapon status (outputs "SSG:NG:SNG:GL:RL:LG" with the text between : characters omitted if you lack the weapon)
1478 if (cl.stats[STAT_ITEMS] & IT_SUPER_SHOTGUN)
1479 strlcat(temp, "SSG", sizeof(temp));
1480 strlcat(temp, ":", sizeof(temp));
1481 if (cl.stats[STAT_ITEMS] & IT_NAILGUN)
1482 strlcat(temp, "NG", sizeof(temp));
1483 strlcat(temp, ":", sizeof(temp));
1484 if (cl.stats[STAT_ITEMS] & IT_SUPER_NAILGUN)
1485 strlcat(temp, "SNG", sizeof(temp));
1486 strlcat(temp, ":", sizeof(temp));
1487 if (cl.stats[STAT_ITEMS] & IT_GRENADE_LAUNCHER)
1488 strlcat(temp, "GL", sizeof(temp));
1489 strlcat(temp, ":", sizeof(temp));
1490 if (cl.stats[STAT_ITEMS] & IT_ROCKET_LAUNCHER)
1491 strlcat(temp, "RL", sizeof(temp));
1492 strlcat(temp, ":", sizeof(temp));
1493 if (cl.stats[STAT_ITEMS] & IT_LIGHTNING)
1494 strlcat(temp, "LG", sizeof(temp));
1497 // not a recognized macro, print it as-is...
1503 // write the resulting text
1504 SZ_Write(&cls.netcon->message, (unsigned char *)temp, strlen(temp));
1508 MSG_WriteByte(&cls.netcon->message, *s);
1511 MSG_WriteByte(&cls.netcon->message, 0);
1513 else // any other command is passed on as-is
1514 SZ_Write(&cls.netcon->message, (const unsigned char *)s, (int)strlen(s) + 1);
1521 Sends the entire command line over to the server
1524 void Cmd_ForwardToServer (void)
1527 if (!strcasecmp(Cmd_Argv(0), "cmd"))
1529 // we want to strip off "cmd", so just send the args
1530 s = Cmd_Argc() > 1 ? Cmd_Args() : "";
1534 // we need to keep the command name, so send Cmd_Argv(0), a space and then Cmd_Args()
1535 s = va("%s %s", Cmd_Argv(0), Cmd_Argc() > 1 ? Cmd_Args() : "");
1537 // don't send an empty forward message if the user tries "cmd" by itself
1540 Cmd_ForwardStringToServer(s);
1548 Returns the position (1 to argc-1) in the command's argument list
1549 where the given parameter apears, or 0 if not present
1553 int Cmd_CheckParm (const char *parm)
1559 Con_Printf ("Cmd_CheckParm: NULL");
1563 for (i = 1; i < Cmd_Argc (); i++)
1564 if (!strcasecmp (parm, Cmd_Argv (i)))