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 //=============================================================================
45 Causes execution of the remainder of the command buffer to be delayed until
46 next frame. This allows commands like:
47 bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
50 static void Cmd_Wait_f (void)
56 =============================================================================
60 =============================================================================
63 static sizebuf_t cmd_text;
72 // LordHavoc: inreased this from 8192 to 32768
73 SZ_Alloc (&cmd_text, 32768, "command buffer"); // space for commands and script files
81 Adds command text at the end of the buffer
84 void Cbuf_AddText (char *text)
90 if (cmd_text.cursize + l >= cmd_text.maxsize)
92 Con_Printf ("Cbuf_AddText: overflow\n");
96 SZ_Write (&cmd_text, text, strlen (text));
104 Adds command text immediately after the current command
105 Adds a \n to the text
106 FIXME: actually change the command buffer to do less copying
109 void Cbuf_InsertText (char *text)
114 // copy off any commands still remaining in the exec buffer
115 templen = cmd_text.cursize;
118 temp = Z_Malloc (templen);
119 memcpy (temp, cmd_text.data, templen);
120 SZ_Clear (&cmd_text);
123 temp = NULL; // shut up compiler
125 // add the entire text of the file
128 // add the copied off data
131 SZ_Write (&cmd_text, temp, templen);
141 void Cbuf_Execute (void)
148 while (cmd_text.cursize)
150 // find a \n or ; line break
151 text = (char *)cmd_text.data;
154 for (i=0 ; i< cmd_text.cursize ; i++)
158 if ( !(quotes&1) && text[i] == ';')
159 break; // don't break if inside a quoted string
164 memcpy (line, text, i);
167 // delete the text from the command buffer and move remaining commands down
168 // this is necessary because commands (exec, alias) can insert data at the
169 // beginning of the text buffer
171 if (i == cmd_text.cursize)
172 cmd_text.cursize = 0;
176 cmd_text.cursize -= i;
177 memcpy (text, text+i, cmd_text.cursize);
180 // execute the command line
181 Cmd_ExecuteString (line, src_command);
184 { // skip out while text still remains in buffer, leaving it
193 ==============================================================================
197 ==============================================================================
204 Adds command line parameters as script statements
205 Commands lead with a +, and continue until a - or another +
206 quake +prog jctest.qp +cmd amlev1
207 quake -nosound +cmd amlev1
210 void Cmd_StuffCmds_f (void)
214 char *text, *build, c;
216 if (Cmd_Argc () != 1)
218 Con_Printf ("stuffcmds : execute command line parameters\n");
222 // build the combined string to parse from
224 for (i=1 ; i<com_argc ; i++)
227 continue; // NEXTSTEP nulls out -NXHost
228 s += strlen (com_argv[i]) + 1;
233 text = Z_Malloc (s+1);
235 for (i=1 ; i<com_argc ; i++)
238 continue; // NEXTSTEP nulls out -NXHost
239 strcat (text,com_argv[i]);
244 // pull out the commands
245 build = Z_Malloc (s+1);
248 for (i=0 ; i<s-1 ; i++)
254 for (j=i ; (text[j] != '+') && (text[j] != '-') && (text[j] != 0) ; j++)
260 strcat (build, text+i);
261 strcat (build, "\n");
268 Cbuf_InsertText (build);
280 static void Cmd_Exec_f (void)
284 if (Cmd_Argc () != 2)
286 Con_Printf ("exec <filename> : execute a script file\n");
290 f = (char *)COM_LoadFile (Cmd_Argv(1), false);
293 Con_Printf ("couldn't exec %s\n",Cmd_Argv(1));
296 Con_Printf ("execing %s\n",Cmd_Argv(1));
307 Just prints the rest of the line to the console
310 static void Cmd_Echo_f (void)
314 for (i=1 ; i<Cmd_Argc() ; i++)
315 Con_Printf ("%s ",Cmd_Argv(i));
323 Creates a new command that executes a command string (possibly ; seperated)
327 static char *CopyString (char *in)
331 out = Z_Malloc (strlen(in)+1);
336 static void Cmd_Alias_f (void)
345 Con_Printf ("Current alias commands:\n");
346 for (a = cmd_alias ; a ; a=a->next)
347 Con_Printf ("%s : %s\n", a->name, a->value);
352 if (strlen(s) >= MAX_ALIAS_NAME)
354 Con_Printf ("Alias name is too long\n");
358 // if the alias already exists, reuse it
359 for (a = cmd_alias ; a ; a=a->next)
361 if (!strcmp(s, a->name))
370 a = Z_Malloc (sizeof(cmdalias_t));
376 // copy the rest of the command line
377 cmd[0] = 0; // start out with a null string
379 for (i=2 ; i< c ; i++)
381 strcat (cmd, Cmd_Argv(i));
387 a->value = CopyString (cmd);
391 =============================================================================
395 =============================================================================
398 typedef struct cmd_function_s
400 struct cmd_function_s *next;
409 static char *cmd_argv[MAX_ARGS];
410 static char *cmd_null_string = "";
411 static char *cmd_args = NULL;
413 cmd_source_t cmd_source;
416 static cmd_function_t *cmd_functions; // possible commands to execute
422 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
423 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
427 static void Cmd_List_f (void)
434 if (Cmd_Argc() > 1) {
435 partial = Cmd_Argv (1);
436 len = strlen(partial);
443 for (cmd = cmd_functions; cmd; cmd = cmd->next) {
444 if (partial && strncmp(partial, cmd->name, len))
446 Con_Printf ("%s\n", cmd->name);
450 Con_Printf ("%i Command%s", count, (count > 1) ? "s" : "");
452 Con_Printf(" beginning with \"%s\"", partial);
464 cmd_mempool = Mem_AllocPool("commands");
467 // register our commands
469 Cmd_AddCommand ("stuffcmds",Cmd_StuffCmds_f);
470 Cmd_AddCommand ("exec",Cmd_Exec_f);
471 Cmd_AddCommand ("echo",Cmd_Echo_f);
472 Cmd_AddCommand ("alias",Cmd_Alias_f);
473 Cmd_AddCommand ("cmd", Cmd_ForwardToServer);
474 Cmd_AddCommand ("wait", Cmd_Wait_f);
475 Cmd_AddCommand ("cmdlist", Cmd_List_f); // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
476 Cmd_AddCommand ("cvarlist", Cvar_List_f); // 2000-01-09 CmdList, CvarList commands
477 // By Matthias "Maddes" Buecher
495 char *Cmd_Argv (int arg)
497 if (arg >= cmd_argc )
498 return cmd_null_string;
499 return cmd_argv[arg];
507 char *Cmd_Args (void)
517 Parses the given string into command line tokens.
520 static void Cmd_TokenizeString (char *text)
524 // clear the args from the last string
525 for (i=0 ; i<cmd_argc ; i++)
526 Z_Free (cmd_argv[i]);
533 // skip whitespace up to a /n
534 while (*text && *text <= ' ' && *text != '\n')
540 { // a newline seperates commands in the buffer
551 text = COM_Parse (text);
555 if (cmd_argc < MAX_ARGS)
557 cmd_argv[cmd_argc] = Z_Malloc (strlen(com_token)+1);
558 strcpy (cmd_argv[cmd_argc], com_token);
571 void Cmd_AddCommand (char *cmd_name, xcommand_t function)
575 // if (host_initialized) // because hunk allocation would get stomped
576 // Sys_Error ("Cmd_AddCommand after host_initialized");
578 // fail if the command is a variable name
579 if (Cvar_VariableString(cmd_name)[0])
581 Con_Printf ("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
585 // fail if the command already exists
586 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
588 if (!strcmp (cmd_name, cmd->name))
590 Con_Printf ("Cmd_AddCommand: %s already defined\n", cmd_name);
595 cmd = Mem_Alloc(cmd_mempool, sizeof(cmd_function_t));
596 cmd->name = cmd_name;
597 cmd->function = function;
598 cmd->next = cmd_functions;
607 qboolean Cmd_Exists (char *cmd_name)
611 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
612 if (!strcmp (cmd_name,cmd->name))
625 char *Cmd_CompleteCommand (char *partial)
630 len = strlen(partial);
636 for (cmd = cmd_functions; cmd; cmd = cmd->next)
637 if (!strncmp(partial, cmd->name, len))
644 Cmd_CompleteCountPossible
646 New function for tab-completion system
648 Thanks to Fett erich@heintz.com
652 int Cmd_CompleteCountPossible (char *partial)
659 len = strlen(partial);
664 // Loop through the command list and count all partial matches
665 for (cmd = cmd_functions; cmd; cmd = cmd->next)
666 if (!strncasecmp(partial, cmd->name, len))
673 Cmd_CompleteBuildList
675 New function for tab-completion system
677 Thanks to Fett erich@heintz.com
681 char **Cmd_CompleteBuildList (char *partial)
686 int sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (char *);
689 len = strlen(partial);
690 buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (char *));
691 // Loop through the alias list and print all matches
692 for (cmd = cmd_functions; cmd; cmd = cmd->next)
693 if (!strncasecmp(partial, cmd->name, len))
694 buf[bpos++] = cmd->name;
703 New function for tab-completion system
705 Thanks to Fett erich@heintz.com
709 char *Cmd_CompleteAlias (char * partial)
714 len = strlen(partial);
720 for (alias = cmd_alias; alias; alias = alias->next)
721 if (!strncasecmp(partial, alias->name, len))
728 Cmd_CompleteAliasCountPossible
730 New function for tab-completion system
732 Thanks to Fett erich@heintz.com
736 int Cmd_CompleteAliasCountPossible (char *partial)
744 len = strlen(partial);
749 // Loop through the command list and count all partial matches
750 for (alias = cmd_alias; alias; alias = alias->next)
751 if (!strncasecmp(partial, alias->name, len))
758 Cmd_CompleteAliasBuildList
760 New function for tab-completion system
762 Thanks to Fett erich@heintz.com
766 char **Cmd_CompleteAliasBuildList (char *partial)
771 int sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (char *);
774 len = strlen(partial);
775 buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (char *));
776 // Loop through the alias list and print all matches
777 for (alias = cmd_alias; alias; alias = alias->next)
778 if (!strncasecmp(partial, alias->name, len))
779 buf[bpos++] = alias->name;
789 A complete command line has been parsed, so try to execute it
790 FIXME: lookupnoadd the token to speed search?
793 void Cmd_ExecuteString (char *text, cmd_source_t src)
799 Cmd_TokenizeString (text);
801 // execute the command line
806 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
808 if (!Q_strcasecmp (cmd_argv[0],cmd->name))
816 for (a=cmd_alias ; a ; a=a->next)
818 if (!Q_strcasecmp (cmd_argv[0], a->name))
820 Cbuf_InsertText (a->value);
826 if (!Cvar_Command ())
827 Con_Printf ("Unknown command \"%s\"\n", Cmd_Argv(0));
835 Sends the entire command line over to the server
838 void Cmd_ForwardToServer (void)
840 if (cls.state != ca_connected)
842 Con_Printf ("Can't \"%s\", not connected\n", Cmd_Argv(0));
846 if (cls.demoplayback)
847 return; // not really connected
849 MSG_WriteByte (&cls.message, clc_stringcmd);
850 if (Q_strcasecmp(Cmd_Argv(0), "cmd") != 0)
852 SZ_Print (&cls.message, Cmd_Argv(0));
853 SZ_Print (&cls.message, " ");
856 SZ_Print (&cls.message, Cmd_Args());
858 SZ_Print (&cls.message, "\n");
866 Returns the position (1 to argc-1) in the command's argument list
867 where the given parameter apears, or 0 if not present
871 int Cmd_CheckParm (char *parm)
876 Sys_Error ("Cmd_CheckParm: NULL");
878 for (i = 1; i < Cmd_Argc (); i++)
879 if (!Q_strcasecmp (parm, Cmd_Argv (i)))