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 void Cmd_ForwardToServer (void);
26 #define MAX_ALIAS_NAME 32
28 typedef struct cmdalias_s
30 struct cmdalias_s *next;
31 char name[MAX_ALIAS_NAME];
35 cmdalias_t *cmd_alias;
42 //=============================================================================
48 Causes execution of the remainder of the command buffer to be delayed until
49 next frame. This allows commands like:
50 bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
53 void Cmd_Wait_f (void)
59 =============================================================================
63 =============================================================================
75 SZ_Alloc (&cmd_text, 8192); // space for commands and script files
83 Adds command text at the end of the buffer
86 void Cbuf_AddText (char *text)
92 if (cmd_text.cursize + l >= cmd_text.maxsize)
94 Con_Printf ("Cbuf_AddText: overflow\n");
98 SZ_Write (&cmd_text, text, strlen (text));
106 Adds command text immediately after the current command
107 Adds a \n to the text
108 FIXME: actually change the command buffer to do less copying
111 void Cbuf_InsertText (char *text)
116 // copy off any commands still remaining in the exec buffer
117 templen = cmd_text.cursize;
120 temp = Z_Malloc (templen);
121 memcpy (temp, cmd_text.data, templen);
122 SZ_Clear (&cmd_text);
125 temp = NULL; // shut up compiler
127 // add the entire text of the file
130 // add the copied off data
133 SZ_Write (&cmd_text, temp, templen);
143 void Cbuf_Execute (void)
150 while (cmd_text.cursize)
152 // find a \n or ; line break
153 text = (char *)cmd_text.data;
156 for (i=0 ; i< cmd_text.cursize ; i++)
160 if ( !(quotes&1) && text[i] == ';')
161 break; // don't break if inside a quoted string
167 memcpy (line, text, i);
170 // delete the text from the command buffer and move remaining commands down
171 // this is necessary because commands (exec, alias) can insert data at the
172 // beginning of the text buffer
174 if (i == cmd_text.cursize)
175 cmd_text.cursize = 0;
179 cmd_text.cursize -= i;
180 memcpy (text, text+i, cmd_text.cursize);
183 // execute the command line
184 Cmd_ExecuteString (line, src_command);
187 { // skip out while text still remains in buffer, leaving it
196 ==============================================================================
200 ==============================================================================
207 Adds command line parameters as script statements
208 Commands lead with a +, and continue until a - or another +
209 quake +prog jctest.qp +cmd amlev1
210 quake -nosound +cmd amlev1
213 void Cmd_StuffCmds_f (void)
217 char *text, *build, c;
219 if (Cmd_Argc () != 1)
221 Con_Printf ("stuffcmds : execute command line parameters\n");
225 // build the combined string to parse from
227 for (i=1 ; i<com_argc ; i++)
230 continue; // NEXTSTEP nulls out -NXHost
231 s += strlen (com_argv[i]) + 1;
236 text = Z_Malloc (s+1);
238 for (i=1 ; i<com_argc ; i++)
241 continue; // NEXTSTEP nulls out -NXHost
242 strcat (text,com_argv[i]);
247 // pull out the commands
248 build = Z_Malloc (s+1);
251 for (i=0 ; i<s-1 ; i++)
257 for (j=i ; (text[j] != '+') && (text[j] != '-') && (text[j] != 0) ; j++)
263 strcat (build, text+i);
264 strcat (build, "\n");
271 Cbuf_InsertText (build);
283 void Cmd_Exec_f (void)
287 if (Cmd_Argc () != 2)
289 Con_Printf ("exec <filename> : execute a script file\n");
293 f = (char *)COM_LoadMallocFile (Cmd_Argv(1), false);
296 Con_Printf ("couldn't exec %s\n",Cmd_Argv(1));
299 Con_Printf ("execing %s\n",Cmd_Argv(1));
310 Just prints the rest of the line to the console
313 void Cmd_Echo_f (void)
317 for (i=1 ; i<Cmd_Argc() ; i++)
318 Con_Printf ("%s ",Cmd_Argv(i));
326 Creates a new command that executes a command string (possibly ; seperated)
330 char *CopyString (char *in)
334 out = Z_Malloc (strlen(in)+1);
339 void Cmd_Alias_f (void)
348 Con_Printf ("Current alias commands:\n");
349 for (a = cmd_alias ; a ; a=a->next)
350 Con_Printf ("%s : %s\n", a->name, a->value);
355 if (strlen(s) >= MAX_ALIAS_NAME)
357 Con_Printf ("Alias name is too long\n");
361 // if the alias already exists, reuse it
362 for (a = cmd_alias ; a ; a=a->next)
364 if (!strcmp(s, a->name))
373 a = Z_Malloc (sizeof(cmdalias_t));
379 // copy the rest of the command line
380 cmd[0] = 0; // start out with a null string
382 for (i=2 ; i< c ; i++)
384 strcat (cmd, Cmd_Argv(i));
390 a->value = CopyString (cmd);
394 =============================================================================
398 =============================================================================
401 typedef struct cmd_function_s
403 struct cmd_function_s *next;
412 static char *cmd_argv[MAX_ARGS];
413 static char *cmd_null_string = "";
414 static char *cmd_args = NULL;
416 cmd_source_t cmd_source;
419 static cmd_function_t *cmd_functions; // possible commands to execute
425 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
426 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
430 void Cmd_List_f (void)
437 if (Cmd_Argc() > 1) {
438 partial = Cmd_Argv (1);
439 len = strlen(partial);
446 for (cmd = cmd_functions; cmd; cmd = cmd->next) {
447 if (partial && strncmp(partial, cmd->name, len))
449 Con_Printf ("%s\n", cmd->name);
453 Con_Printf ("%i Command%s", count, (count > 1) ? "s" : "");
455 Con_Printf(" beginning with \"%s\"", partial);
468 // register our commands
470 Cmd_AddCommand ("stuffcmds",Cmd_StuffCmds_f);
471 Cmd_AddCommand ("exec",Cmd_Exec_f);
472 Cmd_AddCommand ("echo",Cmd_Echo_f);
473 Cmd_AddCommand ("alias",Cmd_Alias_f);
474 Cmd_AddCommand ("cmd", Cmd_ForwardToServer);
475 Cmd_AddCommand ("wait", Cmd_Wait_f);
476 Cmd_AddCommand ("cmdlist", Cmd_List_f); // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
477 Cmd_AddCommand ("cvarlist", Cvar_List_f); // 2000-01-09 CmdList, CvarList commands
478 // By Matthias "Maddes" Buecher
496 char *Cmd_Argv (int arg)
498 if (arg >= cmd_argc )
499 return cmd_null_string;
500 return cmd_argv[arg];
508 char *Cmd_Args (void)
518 Parses the given string into command line tokens.
521 void Cmd_TokenizeString (char *text)
525 // clear the args from the last string
526 for (i=0 ; i<cmd_argc ; i++)
527 Z_Free (cmd_argv[i]);
534 // skip whitespace up to a /n
535 while (*text && *text <= ' ' && *text != '\n')
541 { // a newline seperates commands in the buffer
552 text = COM_Parse (text);
556 if (cmd_argc < MAX_ARGS)
558 cmd_argv[cmd_argc] = Z_Malloc (strlen(com_token)+1);
559 strcpy (cmd_argv[cmd_argc], com_token);
572 void Cmd_AddCommand (char *cmd_name, xcommand_t function)
576 if (host_initialized) // because hunk allocation would get stomped
577 Sys_Error ("Cmd_AddCommand after host_initialized");
579 // fail if the command is a variable name
580 if (Cvar_VariableString(cmd_name)[0])
582 Con_Printf ("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
586 // fail if the command already exists
587 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
589 if (!strcmp (cmd_name, cmd->name))
591 Con_Printf ("Cmd_AddCommand: %s already defined\n", cmd_name);
596 cmd = Hunk_AllocName (sizeof(cmd_function_t), "commands");
597 cmd->name = cmd_name;
598 cmd->function = function;
599 cmd->next = cmd_functions;
608 qboolean Cmd_Exists (char *cmd_name)
612 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
614 if (!strcmp (cmd_name,cmd->name))
628 char *Cmd_CompleteCommand (char *partial)
633 len = strlen(partial);
639 for (cmd = cmd_functions; cmd; cmd = cmd->next)
640 if (!strncmp(partial, cmd->name, len))
647 Cmd_CompleteCountPossible
649 New function for tab-completion system
651 Thanks to Fett erich@heintz.com
656 Cmd_CompleteCountPossible (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
686 Cmd_CompleteBuildList (char *partial)
691 int sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (char *);
694 len = strlen(partial);
695 buf = qmalloc(sizeofbuf + sizeof (char *));
696 // Loop through the alias list and print all matches
697 for (cmd = cmd_functions; cmd; cmd = cmd->next)
698 if (!strncasecmp(partial, cmd->name, len))
699 buf[bpos++] = cmd->name;
708 New function for tab-completion system
710 Thanks to Fett erich@heintz.com
715 *Cmd_CompleteAlias (char * partial)
720 len = strlen(partial);
726 for (alias = cmd_alias; alias; alias = alias->next)
727 if (!strncasecmp(partial, alias->name, len))
734 Cmd_CompleteAliasCountPossible
736 New function for tab-completion system
738 Thanks to Fett erich@heintz.com
743 Cmd_CompleteAliasCountPossible (char *partial)
751 len = strlen(partial);
756 // Loop through the command list and count all partial matches
757 for (alias = cmd_alias; alias; alias = alias->next)
758 if (!strncasecmp(partial, alias->name, len))
765 Cmd_CompleteAliasBuildList
767 New function for tab-completion system
769 Thanks to Fett erich@heintz.com
774 Cmd_CompleteAliasBuildList (char *partial)
779 int sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (char *);
782 len = strlen(partial);
783 buf = qmalloc(sizeofbuf + sizeof (char *));
784 // Loop through the alias list and print all matches
785 for (alias = cmd_alias; alias; alias = alias->next)
786 if (!strncasecmp(partial, alias->name, len))
787 buf[bpos++] = alias->name;
797 A complete command line has been parsed, so try to execute it
798 FIXME: lookupnoadd the token to speed search?
801 void Cmd_ExecuteString (char *text, cmd_source_t src)
807 Cmd_TokenizeString (text);
809 // execute the command line
814 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
816 if (!Q_strcasecmp (cmd_argv[0],cmd->name))
824 for (a=cmd_alias ; a ; a=a->next)
826 if (!Q_strcasecmp (cmd_argv[0], a->name))
828 Cbuf_InsertText (a->value);
834 if (!Cvar_Command ())
835 Con_Printf ("Unknown command \"%s\"\n", Cmd_Argv(0));
844 Sends the entire command line over to the server
847 void Cmd_ForwardToServer (void)
849 if (cls.state != ca_connected)
851 Con_Printf ("Can't \"%s\", not connected\n", Cmd_Argv(0));
855 if (cls.demoplayback)
856 return; // not really connected
858 MSG_WriteByte (&cls.message, clc_stringcmd);
859 if (Q_strcasecmp(Cmd_Argv(0), "cmd") != 0)
861 SZ_Print (&cls.message, Cmd_Argv(0));
862 SZ_Print (&cls.message, " ");
865 SZ_Print (&cls.message, Cmd_Args());
867 SZ_Print (&cls.message, "\n");
875 Returns the position (1 to argc-1) in the command's argument list
876 where the given parameter apears, or 0 if not present
880 int Cmd_CheckParm (char *parm)
885 Sys_Error ("Cmd_CheckParm: NULL");
887 for (i = 1; i < Cmd_Argc (); i++)
888 if (!Q_strcasecmp (parm, Cmd_Argv (i)))