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];
149 // LordHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
150 cmd_tokenizebufferpos = 0;
152 while (cmd_text.cursize)
154 // find a \n or ; line break
155 text = (char *)cmd_text.data;
158 for (i=0 ; i< cmd_text.cursize ; i++)
162 if (text[i] == '\\' && text[i+1] == '"')
164 if ( !quotes && text[i] == ';')
165 break; // don't break if inside a quoted string
166 if (text[i] == '\r' || text[i] == '\n')
170 memcpy (line, text, i);
173 // delete the text from the command buffer and move remaining commands down
174 // this is necessary because commands (exec, alias) can insert data at the
175 // beginning of the text buffer
177 if (i == cmd_text.cursize)
178 cmd_text.cursize = 0;
182 cmd_text.cursize -= i;
183 memcpy (cmd_text.data, text+i, cmd_text.cursize);
186 // execute the command line
187 Cmd_PreprocessString( line, preprocessed, sizeof(preprocessed), NULL );
188 Cmd_ExecuteString (preprocessed, src_command);
191 { // skip out while text still remains in buffer, leaving it
200 ==============================================================================
204 ==============================================================================
211 Adds command line parameters as script statements
212 Commands lead with a +, and continue until a - or another +
213 quake +prog jctest.qp +cmd amlev1
214 quake -nosound +cmd amlev1
217 qboolean host_stuffcmdsrun = false;
218 void Cmd_StuffCmds_f (void)
221 // this is for all commandline options combined (and is bounds checked)
222 char build[MAX_INPUTLINE];
224 if (Cmd_Argc () != 1)
226 Con_Print("stuffcmds : execute command line parameters\n");
230 // no reason to run the commandline arguments twice
231 if (host_stuffcmdsrun)
234 host_stuffcmdsrun = true;
237 for (i = 0;i < com_argc;i++)
239 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)
242 while (com_argv[i][j])
243 build[l++] = com_argv[i][j++];
245 for (;i < com_argc;i++)
249 if ((com_argv[i][0] == '+' || com_argv[i][0] == '-') && (com_argv[i][1] < '0' || com_argv[i][1] > '9'))
251 if (l + strlen(com_argv[i]) + 4 > sizeof(build) - 1)
254 if (strchr(com_argv[i], ' '))
256 for (j = 0;com_argv[i][j];j++)
257 build[l++] = com_argv[i][j];
258 if (strchr(com_argv[i], ' '))
265 // now terminate the combined string and prepend it to the command buffer
266 // we already reserved space for the terminator
268 Cbuf_InsertText (build);
277 static void Cmd_Exec_f (void)
281 if (Cmd_Argc () != 2)
283 Con_Print("exec <filename> : execute a script file\n");
287 f = (char *)FS_LoadFile (Cmd_Argv(1), tempmempool, false, NULL);
290 Con_Printf("couldn't exec %s\n",Cmd_Argv(1));
293 Con_DPrintf("execing %s\n",Cmd_Argv(1));
295 // if executing default.cfg for the first time, lock the cvar defaults
296 // it may seem backwards to insert this text BEFORE the default.cfg
297 // but Cbuf_InsertText inserts before, so this actually ends up after it.
298 if (!strcmp(Cmd_Argv(1), "default.cfg"))
299 Cbuf_InsertText("\ncvar_lockdefaults\n");
301 // insert newline after the text to make sure the last line is terminated (some text editors omit the trailing newline)
302 // (note: insertion order here is backwards from execution order, so this adds it after the text, by calling it before...)
303 Cbuf_InsertText ("\n");
313 Just prints the rest of the line to the console
316 static void Cmd_Echo_f (void)
320 for (i=1 ; i<Cmd_Argc() ; i++)
321 Con_Printf("%s ",Cmd_Argv(i));
326 // Support Doom3-style Toggle Console Command
331 Toggles a specified console variable amongst the values specified (default is 0 and 1)
334 static void Cmd_Toggle_f(void)
336 // Acquire Number of Arguments
337 int nNumArgs = Cmd_Argc();
340 // No Arguments Specified; Print Usage
341 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");
343 { // Correct Arguments Specified
344 // Acquire Potential CVar
345 cvar_t* cvCVar = Cvar_FindVar( Cmd_Argv(1) );
352 Cvar_SetValueQuick(cvCVar, 0);
354 Cvar_SetValueQuick(cvCVar, 1);
358 { // 0 and Specified Usage
359 if(cvCVar->integer == atoi(Cmd_Argv(2) ) )
360 // CVar is Specified Value; // Reset to 0
361 Cvar_SetValueQuick(cvCVar, 0);
363 if(cvCVar->integer == 0)
364 // CVar is 0; Specify Value
365 Cvar_SetQuick(cvCVar, Cmd_Argv(2) );
367 // CVar does not match; Reset to 0
368 Cvar_SetValueQuick(cvCVar, 0);
371 { // Variable Values Specified
375 for(nCnt = 2; nCnt < nNumArgs; nCnt++)
376 { // Cycle through Values
377 if( strcmp(cvCVar->string, Cmd_Argv(nCnt) ) == 0)
378 { // Current Value Located; Increment to Next
379 if( (nCnt + 1) == nNumArgs)
380 // Max Value Reached; Reset
381 Cvar_SetQuick(cvCVar, Cmd_Argv(2) );
384 Cvar_SetQuick(cvCVar, Cmd_Argv(nCnt + 1) );
393 // Value not Found; Reset to Original
394 Cvar_SetQuick(cvCVar, Cmd_Argv(2) );
400 Con_Printf("ERROR : CVar '%s' not found\n", Cmd_Argv(2) );
409 Creates a new command that executes a command string (possibly ; seperated)
412 static void Cmd_Alias_f (void)
415 char cmd[MAX_INPUTLINE];
422 Con_Print("Current alias commands:\n");
423 for (a = cmd_alias ; a ; a=a->next)
424 Con_Printf("%s : %s\n", a->name, a->value);
429 if (strlen(s) >= MAX_ALIAS_NAME)
431 Con_Print("Alias name is too long\n");
435 // if the alias already exists, reuse it
436 for (a = cmd_alias ; a ; a=a->next)
438 if (!strcmp(s, a->name))
447 cmdalias_t *prev, *current;
449 a = (cmdalias_t *)Z_Malloc (sizeof(cmdalias_t));
450 strlcpy (a->name, s, sizeof (a->name));
451 // insert it at the right alphanumeric position
452 for( prev = NULL, current = cmd_alias ; current && strcmp( current->name, a->name ) < 0 ; prev = current, current = current->next )
463 // copy the rest of the command line
464 cmd[0] = 0; // start out with a null string
466 for (i=2 ; i< c ; i++)
468 strlcat (cmd, Cmd_Argv(i), sizeof (cmd));
470 strlcat (cmd, " ", sizeof (cmd));
472 strlcat (cmd, "\n", sizeof (cmd));
474 alloclen = strlen (cmd) + 1;
475 a->value = (char *)Z_Malloc (alloclen);
476 memcpy (a->value, cmd, alloclen);
480 =============================================================================
484 =============================================================================
487 typedef struct cmd_function_s
489 struct cmd_function_s *next;
491 const char *description;
492 xcommand_t consolefunction;
493 xcommand_t clientfunction;
498 static const char *cmd_argv[MAX_ARGS];
499 static const char *cmd_null_string = "";
500 static const char *cmd_args;
501 cmd_source_t cmd_source;
504 static cmd_function_t *cmd_functions; // possible commands to execute
509 Preprocesses strings and replaces $*, $param#, $cvar accordingly
511 static void Cmd_PreprocessString( const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias ) {
516 // don't crash if there's no room in the outtext buffer
517 if( maxoutlen == 0 ) {
520 maxoutlen--; // because of \0
526 while( *in && outlen < maxoutlen ) {
527 if( *in == '$' && !inquote ) {
528 // this is some kind of expansion, see what comes after the $
530 // replacements that can always be used:
531 // $$ is replaced with $, to allow escaping $
532 // $<cvarname> is replaced with the contents of the cvar
534 // the following can be used in aliases only:
535 // $* is replaced with all formal parameters (including name of the alias - this probably is not desirable)
536 // $0 is replaced with the name of this alias
537 // $<number> is replaced with an argument to this alias (or copied as-is if no such parameter exists), can be multiple digits
539 outtext[outlen++] = *in++;
540 } else if( *in == '*' && alias ) {
541 const char *linein = Cmd_Args();
543 // include all parameters
545 while( *linein && outlen < maxoutlen ) {
546 outtext[outlen++] = *linein++;
551 } else if( '0' <= *in && *in <= '9' && alias ) {
555 argnum = strtol( in, &nexttoken, 10 );
557 if( 0 <= argnum && argnum < Cmd_Argc() ) {
558 const char *param = Cmd_Argv( argnum );
559 while( *param && outlen < maxoutlen ) {
560 outtext[outlen++] = *param++;
563 } else if( argnum >= Cmd_Argc() ) {
564 Con_Printf( "Warning: Not enough parameters passed to alias '%s', at least %i expected:\n %s\n", alias->name, argnum, alias->value );
565 outtext[outlen++] = '$';
569 const char *tempin = in;
571 COM_ParseToken_Console( &tempin );
572 // don't expand rcon_password or similar cvars (CVAR_PRIVATE flag)
573 if ((cvar = Cvar_FindVar(&com_token[0])) && !(cvar->flags & CVAR_PRIVATE)) {
574 const char *cvarcontent = cvar->string;
575 while( *cvarcontent && outlen < maxoutlen ) {
576 outtext[outlen++] = *cvarcontent++;
581 Con_Printf( "Warning: could not find cvar %s when expanding alias %s\n %s\n", com_token, alias->name, alias->value );
583 Con_Printf( "Warning: could not find cvar %s\n", com_token );
585 outtext[outlen++] = '$';
592 outtext[outlen++] = *in++;
602 Called for aliases and fills in the alias into the cbuffer
605 static void Cmd_ExecuteAlias (cmdalias_t *alias)
607 static char buffer[ MAX_INPUTLINE + 2 ];
608 Cmd_PreprocessString( alias->value, buffer, sizeof(buffer) - 2, alias );
609 // insert at start of command buffer, so that aliases execute in order
610 // (fixes bug introduced by Black on 20050705)
611 Cbuf_InsertText( buffer );
618 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
619 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
623 static void Cmd_List_f (void)
631 partial = Cmd_Argv (1);
632 len = (int)strlen(partial);
641 for (cmd = cmd_functions; cmd; cmd = cmd->next)
643 if (partial && strncmp(partial, cmd->name, len))
645 Con_Printf("%s : %s\n", cmd->name, cmd->description);
650 Con_Printf("%i Command%s beginning with \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
652 Con_Printf("%i Command%s\n\n", count, (count > 1) ? "s" : "");
662 cmd_mempool = Mem_AllocPool("commands", 0, NULL);
663 // space for commands and script files
664 cmd_text.data = cmd_text_buf;
665 cmd_text.maxsize = sizeof(cmd_text_buf);
666 cmd_text.cursize = 0;
669 void Cmd_Init_Commands (void)
672 // register our commands
674 Cmd_AddCommand ("stuffcmds",Cmd_StuffCmds_f, "execute commandline parameters (must be present in quake.rc script)");
675 Cmd_AddCommand ("exec",Cmd_Exec_f, "execute a script file");
676 Cmd_AddCommand ("echo",Cmd_Echo_f, "print a message to the console (useful in scripts)");
677 Cmd_AddCommand ("alias",Cmd_Alias_f, "create a script function (parameters are passed in as $1 through $9, and $* for all parameters)");
678 Cmd_AddCommand ("cmd", Cmd_ForwardToServer, "send a console commandline to the server (used by some mods)");
679 Cmd_AddCommand ("wait", Cmd_Wait_f, "make script execution wait for next rendered frame");
680 Cmd_AddCommand ("set", Cvar_Set_f, "create or change the value of a console variable");
681 Cmd_AddCommand ("seta", Cvar_SetA_f, "create or change the value of a console variable that will be saved to config.cfg");
683 // 2000-01-09 CmdList, CvarList commands By Matthias "Maddes" Buecher
684 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
685 Cmd_AddCommand ("cmdlist", Cmd_List_f, "lists all console commands beginning with the specified prefix");
686 Cmd_AddCommand ("cvarlist", Cvar_List_f, "lists all console variables beginning with the specified prefix");
688 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");
689 Cmd_AddCommand ("cvar_resettodefaults_all", Cvar_ResetToDefaults_All_f, "sets all cvars to their locked default values");
690 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)");
691 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)");
694 // Support Doom3-style Toggle Command
695 Cmd_AddCommand( "toggle", Cmd_Toggle_f, "toggles a console variable's values (use for more info)");
703 void Cmd_Shutdown(void)
705 Mem_FreePool(&cmd_mempool);
723 const char *Cmd_Argv (int arg)
725 if (arg >= cmd_argc )
726 return cmd_null_string;
727 return cmd_argv[arg];
735 const char *Cmd_Args (void)
745 Parses the given string into command line tokens.
748 // AK: This function should only be called from ExcuteString because the current design is a bit of an hack
749 static void Cmd_TokenizeString (const char *text)
758 // skip whitespace up to a /n
759 while (*text && *text <= ' ' && *text != '\r' && *text != '\n')
766 if (*text == '\n' || *text == '\r')
768 // a newline separates commands in the buffer
769 if (*text == '\r' && text[1] == '\n')
781 if (!COM_ParseToken_Console(&text))
784 if (cmd_argc < MAX_ARGS)
786 l = (int)strlen(com_token) + 1;
787 if (cmd_tokenizebufferpos + l > CMD_TOKENIZELENGTH)
789 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguements\n", CMD_TOKENIZELENGTH);
792 memcpy (cmd_tokenizebuffer + cmd_tokenizebufferpos, com_token, l);
793 cmd_argv[cmd_argc] = cmd_tokenizebuffer + cmd_tokenizebufferpos;
794 cmd_tokenizebufferpos += l;
806 void Cmd_AddCommand_WithClientCommand (const char *cmd_name, xcommand_t consolefunction, xcommand_t clientfunction, const char *description)
809 cmd_function_t *prev, *current;
811 // fail if the command is a variable name
812 if (Cvar_FindVar( cmd_name ))
814 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
818 // fail if the command already exists
819 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
821 if (!strcmp (cmd_name, cmd->name))
823 if (consolefunction || clientfunction)
825 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
830 cmd->csqcfunc = true;
836 cmd = (cmd_function_t *)Mem_Alloc(cmd_mempool, sizeof(cmd_function_t));
837 cmd->name = cmd_name;
838 cmd->consolefunction = consolefunction;
839 cmd->clientfunction = clientfunction;
840 cmd->description = description;
841 if(!consolefunction && !clientfunction) //[515]: csqc
842 cmd->csqcfunc = true;
843 cmd->next = cmd_functions;
845 // insert it at the right alphanumeric position
846 for( prev = NULL, current = cmd_functions ; current && strcmp( current->name, cmd->name ) < 0 ; prev = current, current = current->next )
856 void Cmd_AddCommand (const char *cmd_name, xcommand_t function, const char *description)
858 Cmd_AddCommand_WithClientCommand (cmd_name, function, NULL, description);
866 qboolean Cmd_Exists (const char *cmd_name)
870 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
871 if (!strcmp (cmd_name,cmd->name))
883 const char *Cmd_CompleteCommand (const char *partial)
888 len = strlen(partial);
894 for (cmd = cmd_functions; cmd; cmd = cmd->next)
895 if (!strncasecmp(partial, cmd->name, len))
902 Cmd_CompleteCountPossible
904 New function for tab-completion system
906 Thanks to Fett erich@heintz.com
910 int Cmd_CompleteCountPossible (const char *partial)
917 len = strlen(partial);
922 // Loop through the command list and count all partial matches
923 for (cmd = cmd_functions; cmd; cmd = cmd->next)
924 if (!strncasecmp(partial, cmd->name, len))
931 Cmd_CompleteBuildList
933 New function for tab-completion system
935 Thanks to Fett erich@heintz.com
939 const char **Cmd_CompleteBuildList (const char *partial)
944 size_t sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (const char *);
947 len = strlen(partial);
948 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
949 // Loop through the alias list and print all matches
950 for (cmd = cmd_functions; cmd; cmd = cmd->next)
951 if (!strncasecmp(partial, cmd->name, len))
952 buf[bpos++] = cmd->name;
958 // written by LordHavoc
959 void Cmd_CompleteCommandPrint (const char *partial)
962 size_t len = strlen(partial);
963 // Loop through the command list and print all matches
964 for (cmd = cmd_functions; cmd; cmd = cmd->next)
965 if (!strncasecmp(partial, cmd->name, len))
966 Con_Printf("%s : %s\n", cmd->name, cmd->description);
972 New function for tab-completion system
974 Thanks to Fett erich@heintz.com
978 const char *Cmd_CompleteAlias (const char *partial)
983 len = strlen(partial);
989 for (alias = cmd_alias; alias; alias = alias->next)
990 if (!strncasecmp(partial, alias->name, len))
996 // written by LordHavoc
997 void Cmd_CompleteAliasPrint (const char *partial)
1000 size_t len = strlen(partial);
1001 // Loop through the alias list and print all matches
1002 for (alias = cmd_alias; alias; alias = alias->next)
1003 if (!strncasecmp(partial, alias->name, len))
1004 Con_Printf("%s : %s\n", alias->name, alias->value);
1009 Cmd_CompleteAliasCountPossible
1011 New function for tab-completion system
1012 Added by EvilTypeGuy
1013 Thanks to Fett erich@heintz.com
1017 int Cmd_CompleteAliasCountPossible (const char *partial)
1025 len = strlen(partial);
1030 // Loop through the command list and count all partial matches
1031 for (alias = cmd_alias; alias; alias = alias->next)
1032 if (!strncasecmp(partial, alias->name, len))
1039 Cmd_CompleteAliasBuildList
1041 New function for tab-completion system
1042 Added by EvilTypeGuy
1043 Thanks to Fett erich@heintz.com
1047 const char **Cmd_CompleteAliasBuildList (const char *partial)
1052 size_t sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (const char *);
1055 len = strlen(partial);
1056 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
1057 // Loop through the alias list and print all matches
1058 for (alias = cmd_alias; alias; alias = alias->next)
1059 if (!strncasecmp(partial, alias->name, len))
1060 buf[bpos++] = alias->name;
1066 void Cmd_ClearCsqcFuncs (void)
1068 cmd_function_t *cmd;
1069 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1070 cmd->csqcfunc = false;
1073 qboolean CL_VM_ConsoleCommand (const char *cmd);
1078 A complete command line has been parsed, so try to execute it
1079 FIXME: lookupnoadd the token to speed search?
1082 void Cmd_ExecuteString (const char *text, cmd_source_t src)
1085 cmd_function_t *cmd;
1088 oldpos = cmd_tokenizebufferpos;
1091 Cmd_TokenizeString (text);
1093 // execute the command line
1096 cmd_tokenizebufferpos = oldpos;
1097 return; // no tokens
1101 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1103 if (!strcasecmp (cmd_argv[0],cmd->name))
1105 if (cmd->csqcfunc && CL_VM_ConsoleCommand (text)) //[515]: csqc
1110 if (cmd->consolefunction)
1111 cmd->consolefunction ();
1112 else if (cmd->clientfunction)
1114 if (cls.state == ca_connected)
1116 // forward remote commands to the server for execution
1117 Cmd_ForwardToServer();
1120 Con_Printf("Can not send command \"%s\", not connected.\n", Cmd_Argv(0));
1123 Con_Printf("Command \"%s\" can not be executed\n", Cmd_Argv(0));
1124 cmd_tokenizebufferpos = oldpos;
1127 if (cmd->clientfunction)
1129 cmd->clientfunction ();
1130 cmd_tokenizebufferpos = oldpos;
1139 // if it's a client command and no command was found, say so.
1140 if (cmd_source == src_client)
1142 Con_Printf("player \"%s\" tried to %s\n", host_client->name, text);
1147 for (a=cmd_alias ; a ; a=a->next)
1149 if (!strcasecmp (cmd_argv[0], a->name))
1151 Cmd_ExecuteAlias(a);
1152 cmd_tokenizebufferpos = oldpos;
1158 if (!Cvar_Command () && host_framecount > 0)
1159 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(0));
1161 cmd_tokenizebufferpos = oldpos;
1167 Cmd_ForwardStringToServer
1169 Sends an entire command string over to the server, unprocessed
1172 void Cmd_ForwardStringToServer (const char *s)
1175 if (cls.state != ca_connected)
1177 Con_Printf("Can't \"%s\", not connected\n", s);
1184 // LordHavoc: thanks to Fuh for bringing the pure evil of SZ_Print to my
1185 // attention, it has been eradicated from here, its only (former) use in
1186 // all of darkplaces.
1187 if (cls.protocol == PROTOCOL_QUAKEWORLD)
1188 MSG_WriteByte(&cls.netcon->message, qw_clc_stringcmd);
1190 MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
1191 if ((!strncmp(s, "say ", 4) || !strncmp(s, "say_team ", 9)) && cl_locs_enable.integer)
1193 // say/say_team commands can replace % character codes with status info
1196 if (*s == '%' && s[1])
1198 // handle proquake message macros
1202 case 'l': // current location
1203 CL_Locs_FindLocationName(temp, sizeof(temp), cl.movement_origin);
1205 case 'h': // current health
1206 dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_HEALTH]);
1208 case 'a': // current armor
1209 dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_ARMOR]);
1211 case 'x': // current rockets
1212 dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_ROCKETS]);
1214 case 'c': // current cells
1215 dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_CELLS]);
1217 // silly proquake macros
1218 case 'd': // loc at last death
1219 CL_Locs_FindLocationName(temp, sizeof(temp), cl.lastdeathorigin);
1221 case 't': // current time
1222 dpsnprintf(temp, sizeof(temp), "%.0f:%.0f", floor(cl.time / 60), cl.time - floor(cl.time / 60) * 60);
1224 case 'r': // rocket launcher status ("I have RL", "I need rockets", "I need RL")
1225 if (!(cl.stats[STAT_ITEMS] & IT_ROCKET_LAUNCHER))
1226 dpsnprintf(temp, sizeof(temp), "I need RL");
1227 else if (!cl.stats[STAT_ROCKETS])
1228 dpsnprintf(temp, sizeof(temp), "I need rockets");
1230 dpsnprintf(temp, sizeof(temp), "I have RL");
1232 case 'p': // powerup status (outputs "quad" "pent" and "eyes" according to status)
1233 if (cl.stats[STAT_ITEMS] & IT_QUAD)
1236 strlcat(temp, " ", sizeof(temp));
1237 strlcat(temp, "quad", sizeof(temp));
1239 if (cl.stats[STAT_ITEMS] & IT_INVULNERABILITY)
1242 strlcat(temp, " ", sizeof(temp));
1243 strlcat(temp, "pent", sizeof(temp));
1245 if (cl.stats[STAT_ITEMS] & IT_INVISIBILITY)
1248 strlcat(temp, " ", sizeof(temp));
1249 strlcat(temp, "eyes", sizeof(temp));
1252 case 'w': // weapon status (outputs "SSG:NG:SNG:GL:RL:LG" with the text between : characters omitted if you lack the weapon)
1253 if (cl.stats[STAT_ITEMS] & IT_SUPER_SHOTGUN)
1254 strlcat(temp, "SSG", sizeof(temp));
1255 strlcat(temp, ":", sizeof(temp));
1256 if (cl.stats[STAT_ITEMS] & IT_NAILGUN)
1257 strlcat(temp, "NG", sizeof(temp));
1258 strlcat(temp, ":", sizeof(temp));
1259 if (cl.stats[STAT_ITEMS] & IT_SUPER_NAILGUN)
1260 strlcat(temp, "SNG", sizeof(temp));
1261 strlcat(temp, ":", sizeof(temp));
1262 if (cl.stats[STAT_ITEMS] & IT_GRENADE_LAUNCHER)
1263 strlcat(temp, "GL", sizeof(temp));
1264 strlcat(temp, ":", sizeof(temp));
1265 if (cl.stats[STAT_ITEMS] & IT_ROCKET_LAUNCHER)
1266 strlcat(temp, "RL", sizeof(temp));
1267 strlcat(temp, ":", sizeof(temp));
1268 if (cl.stats[STAT_ITEMS] & IT_LIGHTNING)
1269 strlcat(temp, "LG", sizeof(temp));
1272 // not a recognized macro, print it as-is...
1278 // write the resulting text
1279 SZ_Write(&cls.netcon->message, (unsigned char *)temp, strlen(temp));
1283 MSG_WriteByte(&cls.netcon->message, *s);
1286 MSG_WriteByte(&cls.netcon->message, 0);
1288 else // any other command is passed on as-is
1289 SZ_Write(&cls.netcon->message, (const unsigned char *)s, (int)strlen(s) + 1);
1296 Sends the entire command line over to the server
1299 void Cmd_ForwardToServer (void)
1302 if (!strcasecmp(Cmd_Argv(0), "cmd"))
1304 // we want to strip off "cmd", so just send the args
1305 s = Cmd_Argc() > 1 ? Cmd_Args() : "";
1309 // we need to keep the command name, so send Cmd_Argv(0), a space and then Cmd_Args()
1310 s = va("%s %s", Cmd_Argv(0), Cmd_Argc() > 1 ? Cmd_Args() : "");
1312 // don't send an empty forward message if the user tries "cmd" by itself
1315 Cmd_ForwardStringToServer(s);
1323 Returns the position (1 to argc-1) in the command's argument list
1324 where the given parameter apears, or 0 if not present
1328 int Cmd_CheckParm (const char *parm)
1334 Con_Printf ("Cmd_CheckParm: NULL");
1338 for (i = 1; i < Cmd_Argc (); i++)
1339 if (!strcasecmp (parm, Cmd_Argv (i)))