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
26 typedef struct cmdalias_s
28 struct cmdalias_s *next;
29 char name[MAX_ALIAS_NAME];
33 static cmdalias_t *cmd_alias;
35 static qboolean cmd_wait;
37 static mempool_t *cmd_mempool;
39 #define CMD_TOKENIZELENGTH 4096
40 static char cmd_tokenizebuffer[CMD_TOKENIZELENGTH];
41 static int cmd_tokenizebufferpos = 0;
43 //=============================================================================
49 Causes execution of the remainder of the command buffer to be delayed until
50 next frame. This allows commands like:
51 bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
54 static void Cmd_Wait_f (void)
60 =============================================================================
64 =============================================================================
67 static sizebuf_t cmd_text;
76 // LordHavoc: inreased this from 8192 to 32768
77 SZ_Alloc (&cmd_text, 32768, "command buffer"); // space for commands and script files
85 Adds command text at the end of the buffer
88 void Cbuf_AddText (const char *text)
94 if (cmd_text.cursize + l >= cmd_text.maxsize)
96 Con_Print("Cbuf_AddText: overflow\n");
100 SZ_Write (&cmd_text, text, strlen (text));
108 Adds command text immediately after the current command
109 Adds a \n to the text
110 FIXME: actually change the command buffer to do less copying
113 void Cbuf_InsertText (const char *text)
118 // copy off any commands still remaining in the exec buffer
119 templen = cmd_text.cursize;
122 temp = Z_Malloc (templen);
123 memcpy (temp, cmd_text.data, templen);
124 SZ_Clear (&cmd_text);
127 temp = NULL; // shut up compiler
129 // add the entire text of the file
132 // add the copied off data
135 SZ_Write (&cmd_text, temp, templen);
145 void Cbuf_Execute (void)
152 // LordHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
153 cmd_tokenizebufferpos = 0;
155 while (cmd_text.cursize)
157 // find a \n or ; line break
158 text = (char *)cmd_text.data;
161 while (*text && *text <= ' ')
163 for (i=0 ; i< cmd_text.cursize ; i++)
167 if ( !(quotes&1) && text[i] == ';')
168 break; // don't break if inside a quoted string
169 if (text[i] == '\r' || text[i] == '\n')
173 memcpy (line, text, i);
176 // delete the text from the command buffer and move remaining commands down
177 // this is necessary because commands (exec, alias) can insert data at the
178 // beginning of the text buffer
180 if (i == cmd_text.cursize)
181 cmd_text.cursize = 0;
185 cmd_text.cursize -= i;
186 memcpy (text, text+i, cmd_text.cursize);
189 // execute the command line
190 Cmd_ExecuteString (line, src_command);
193 { // skip out while text still remains in buffer, leaving it
202 ==============================================================================
206 ==============================================================================
213 Adds command line parameters as script statements
214 Commands lead with a +, and continue until a - or another +
215 quake +prog jctest.qp +cmd amlev1
216 quake -nosound +cmd amlev1
219 void Cmd_StuffCmds_f (void)
223 char *text, *build, c;
225 if (Cmd_Argc () != 1)
227 Con_Print("stuffcmds : execute command line parameters\n");
231 // build the combined string to parse from
233 for (i=1 ; i<com_argc ; i++)
236 continue; // NEXTSTEP nulls out -NXHost
237 s += strlen (com_argv[i]) + 1;
242 text = Z_Malloc (s+1);
244 for (i=1 ; i<com_argc ; i++)
247 continue; // NEXTSTEP nulls out -NXHost
248 strcat (text,com_argv[i]);
253 // pull out the commands
254 build = Z_Malloc (s+1);
257 for (i=0 ; i<s-1 ; i++)
263 for (j=i ; (text[j] != '+') && (text[j] != '-') && (text[j] != 0) ; j++)
269 strcat (build, text+i);
270 strcat (build, "\n");
277 Cbuf_InsertText (build);
289 static void Cmd_Exec_f (void)
293 if (Cmd_Argc () != 2)
295 Con_Print("exec <filename> : execute a script file\n");
299 f = (char *)FS_LoadFile (Cmd_Argv(1), false);
302 Con_Printf("couldn't exec %s\n",Cmd_Argv(1));
305 Con_DPrintf("execing %s\n",Cmd_Argv(1));
316 Just prints the rest of the line to the console
319 static void Cmd_Echo_f (void)
323 for (i=1 ; i<Cmd_Argc() ; i++)
324 Con_Printf("%s ",Cmd_Argv(i));
332 Creates a new command that executes a command string (possibly ; seperated)
335 static char *CopyString (char *in)
339 out = Z_Malloc (strlen(in)+1);
344 static void Cmd_Alias_f (void)
353 Con_Print("Current alias commands:\n");
354 for (a = cmd_alias ; a ; a=a->next)
355 Con_Printf("%s : %s\n", a->name, a->value);
360 if (strlen(s) >= MAX_ALIAS_NAME)
362 Con_Print("Alias name is too long\n");
366 // if the alias already exists, reuse it
367 for (a = cmd_alias ; a ; a=a->next)
369 if (!strcmp(s, a->name))
378 a = Z_Malloc (sizeof(cmdalias_t));
382 strlcpy (a->name, s, sizeof (a->name));
384 // copy the rest of the command line
385 cmd[0] = 0; // start out with a null string
387 for (i=2 ; i< c ; i++)
389 strlcat (cmd, Cmd_Argv(i), sizeof (cmd));
391 strlcat (cmd, " ", sizeof (cmd));
393 strlcat (cmd, "\n", sizeof (cmd));
395 a->value = CopyString (cmd);
399 =============================================================================
403 =============================================================================
406 typedef struct cmd_function_s
408 struct cmd_function_s *next;
417 static const char *cmd_argv[MAX_ARGS];
418 static const char *cmd_null_string = "";
419 static const char *cmd_args = NULL;
421 cmd_source_t cmd_source;
424 static cmd_function_t *cmd_functions; // possible commands to execute
430 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
431 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
435 static void Cmd_List_f (void)
443 partial = Cmd_Argv (1);
444 len = strlen(partial);
453 for (cmd = cmd_functions; cmd; cmd = cmd->next)
455 if (partial && strncmp(partial, cmd->name, len))
457 Con_Printf("%s\n", cmd->name);
461 Con_Printf("%i Command%s", count, (count > 1) ? "s" : "");
463 Con_Printf(" beginning with \"%s\"", partial);
475 cmd_mempool = Mem_AllocPool("commands");
478 // register our commands
480 Cmd_AddCommand ("stuffcmds",Cmd_StuffCmds_f);
481 Cmd_AddCommand ("exec",Cmd_Exec_f);
482 Cmd_AddCommand ("echo",Cmd_Echo_f);
483 Cmd_AddCommand ("alias",Cmd_Alias_f);
484 Cmd_AddCommand ("cmd", Cmd_ForwardToServer);
485 Cmd_AddCommand ("wait", Cmd_Wait_f);
486 Cmd_AddCommand ("cmdlist", Cmd_List_f); // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
487 Cmd_AddCommand ("cvarlist", Cvar_List_f); // 2000-01-09 CmdList, CvarList commands
488 // By Matthias "Maddes" Buecher
506 const char *Cmd_Argv (int arg)
508 if (arg >= cmd_argc )
509 return cmd_null_string;
510 return cmd_argv[arg];
518 const char *Cmd_Args (void)
528 Parses the given string into command line tokens.
531 static void Cmd_TokenizeString (const char *text)
540 // skip whitespace up to a /n
541 while (*text && *text <= ' ' && *text != '\n')
546 // a newline seperates commands in the buffer
557 if (!COM_ParseTokenConsole(&text))
560 if (cmd_argc < MAX_ARGS)
562 l = strlen(com_token) + 1;
563 if (cmd_tokenizebufferpos + l > CMD_TOKENIZELENGTH)
564 Sys_Error("Cmd_TokenizeString: ran out of %i character buffer space for command arguements\n", CMD_TOKENIZELENGTH);
565 strcpy (cmd_tokenizebuffer + cmd_tokenizebufferpos, com_token);
566 cmd_argv[cmd_argc] = cmd_tokenizebuffer + cmd_tokenizebufferpos;
567 cmd_tokenizebufferpos += l;
580 void Cmd_AddCommand (const char *cmd_name, xcommand_t function)
584 // fail if the command is a variable name
585 if (Cvar_VariableString(cmd_name)[0])
587 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
591 // fail if the command already exists
592 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
594 if (!strcmp (cmd_name, cmd->name))
596 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
601 cmd = Mem_Alloc(cmd_mempool, sizeof(cmd_function_t));
602 cmd->name = cmd_name;
603 cmd->function = function;
604 cmd->next = cmd_functions;
613 qboolean Cmd_Exists (const char *cmd_name)
617 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
618 if (!strcmp (cmd_name,cmd->name))
630 const char *Cmd_CompleteCommand (const char *partial)
635 len = strlen(partial);
641 for (cmd = cmd_functions; cmd; cmd = cmd->next)
642 if (!strncmp(partial, cmd->name, len))
649 Cmd_CompleteCountPossible
651 New function for tab-completion system
653 Thanks to Fett erich@heintz.com
657 int Cmd_CompleteCountPossible (const char *partial)
663 len = strlen(partial);
668 // Loop through the command list and count all partial matches
669 for (cmd = cmd_functions; cmd; cmd = cmd->next)
670 if (!strncasecmp(partial, cmd->name, len))
677 Cmd_CompleteBuildList
679 New function for tab-completion system
681 Thanks to Fett erich@heintz.com
685 const char **Cmd_CompleteBuildList (const char *partial)
690 int sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (const char *);
693 len = strlen(partial);
694 buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
695 // Loop through the alias list and print all matches
696 for (cmd = cmd_functions; cmd; cmd = cmd->next)
697 if (!strncasecmp(partial, cmd->name, len))
698 buf[bpos++] = cmd->name;
707 New function for tab-completion system
709 Thanks to Fett erich@heintz.com
713 const char *Cmd_CompleteAlias (const char *partial)
718 len = strlen(partial);
724 for (alias = cmd_alias; alias; alias = alias->next)
725 if (!strncasecmp(partial, alias->name, len))
732 Cmd_CompleteAliasCountPossible
734 New function for tab-completion system
736 Thanks to Fett erich@heintz.com
740 int Cmd_CompleteAliasCountPossible (const char *partial)
748 len = strlen(partial);
753 // Loop through the command list and count all partial matches
754 for (alias = cmd_alias; alias; alias = alias->next)
755 if (!strncasecmp(partial, alias->name, len))
762 Cmd_CompleteAliasBuildList
764 New function for tab-completion system
766 Thanks to Fett erich@heintz.com
770 const char **Cmd_CompleteAliasBuildList (const char *partial)
775 int sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (const char *);
778 len = strlen(partial);
779 buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
780 // Loop through the alias list and print all matches
781 for (alias = cmd_alias; alias; alias = alias->next)
782 if (!strncasecmp(partial, alias->name, len))
783 buf[bpos++] = alias->name;
793 A complete command line has been parsed, so try to execute it
794 FIXME: lookupnoadd the token to speed search?
797 void Cmd_ExecuteString (const char *text, cmd_source_t src)
803 oldpos = cmd_tokenizebufferpos;
805 Cmd_TokenizeString (text);
807 // execute the command line
810 cmd_tokenizebufferpos = oldpos;
814 // check functions (only after host_initialized)
815 if (host_initialized || !strcasecmp(cmd_argv[0], "exec"))
817 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
819 if (!strcasecmp (cmd_argv[0],cmd->name))
822 cmd_tokenizebufferpos = oldpos;
828 // check alias (only after host_initialized)
829 if (host_initialized)
831 for (a=cmd_alias ; a ; a=a->next)
833 if (!strcasecmp (cmd_argv[0], a->name))
835 Cbuf_InsertText (a->value);
836 cmd_tokenizebufferpos = oldpos;
842 // check cvars (always)
843 if (!Cvar_Command () && host_initialized)
844 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(0));
846 cmd_tokenizebufferpos = oldpos;
854 Sends the entire command line over to the server
857 void Cmd_ForwardToServer (void)
860 if (cls.state != ca_connected)
862 Con_Printf("Can't \"%s\", not connected\n", Cmd_Argv(0));
866 if (cls.demoplayback)
867 return; // not really connected
869 // LordHavoc: thanks to Fuh for bringing the pure evil of SZ_Print to my
870 // attention, it has been eradicated from here, its only (former) use in
871 // all of darkplaces.
872 if (strcasecmp(Cmd_Argv(0), "cmd") != 0)
873 s = va("%s %s", Cmd_Argv(0), Cmd_Argc() > 1 ? Cmd_Args() : "\n");
875 s = Cmd_Argc() > 1 ? Cmd_Args() : "\n";
876 MSG_WriteByte(&cls.message, clc_stringcmd);
877 SZ_Write(&cls.message, s, strlen(s) + 1);
885 Returns the position (1 to argc-1) in the command's argument list
886 where the given parameter apears, or 0 if not present
890 int Cmd_CheckParm (const char *parm)
895 Sys_Error ("Cmd_CheckParm: NULL");
897 for (i = 1; i < Cmd_Argc (); i++)
898 if (!strcasecmp (parm, Cmd_Argv (i)))