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 void Cbuf_Shutdown (void)
94 Adds command text at the end of the buffer
97 void Cbuf_AddText (const char *text)
103 if (cmd_text.cursize + l >= cmd_text.maxsize)
105 Con_Print("Cbuf_AddText: overflow\n");
109 SZ_Write (&cmd_text, text, strlen (text));
117 Adds command text immediately after the current command
118 Adds a \n to the text
119 FIXME: actually change the command buffer to do less copying
122 void Cbuf_InsertText (const char *text)
127 // copy off any commands still remaining in the exec buffer
128 templen = cmd_text.cursize;
131 temp = Mem_Alloc (tempmempool, templen);
132 memcpy (temp, cmd_text.data, templen);
133 SZ_Clear (&cmd_text);
138 // add the entire text of the file
141 // add the copied off data
144 SZ_Write (&cmd_text, temp, templen);
154 void Cbuf_Execute (void)
161 // LordHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
162 cmd_tokenizebufferpos = 0;
164 while (cmd_text.cursize)
166 // find a \n or ; line break
167 text = (char *)cmd_text.data;
170 for (i=0 ; i< cmd_text.cursize ; i++)
174 if ( !(quotes&1) && text[i] == ';')
175 break; // don't break if inside a quoted string
176 if (text[i] == '\r' || text[i] == '\n')
180 memcpy (line, text, i);
183 // delete the text from the command buffer and move remaining commands down
184 // this is necessary because commands (exec, alias) can insert data at the
185 // beginning of the text buffer
187 if (i == cmd_text.cursize)
188 cmd_text.cursize = 0;
192 cmd_text.cursize -= i;
193 memcpy (cmd_text.data, text+i, cmd_text.cursize);
196 // execute the command line
197 Cmd_ExecuteString (line, src_command);
200 { // skip out while text still remains in buffer, leaving it
209 ==============================================================================
213 ==============================================================================
220 Adds command line parameters as script statements
221 Commands lead with a +, and continue until a - or another +
222 quake +prog jctest.qp +cmd amlev1
223 quake -nosound +cmd amlev1
226 void Cmd_StuffCmds_f (void)
229 // this is per command, and bounds checked (no buffer overflows)
232 if (Cmd_Argc () != 1)
234 Con_Print("stuffcmds : execute command line parameters\n");
238 for (i = 0;i < com_argc;i++)
240 if (com_argv[i] && com_argv[i][0] == '+' && (com_argv[i][1] < '0' || com_argv[i][1] > '9'))
244 while (com_argv[i][j])
245 build[l++] = com_argv[i][j++];
247 for (;i < com_argc;i++)
251 if ((com_argv[i][0] == '+' || com_argv[i][0] == '-') && (com_argv[i][1] < '0' || com_argv[i][1] > '9'))
253 if (l + strlen(com_argv[i]) + 5 > sizeof(build))
257 for (j = 0;com_argv[i][j];j++)
258 build[l++] = com_argv[i][j];
263 Cbuf_InsertText (build);
275 static void Cmd_Exec_f (void)
279 if (Cmd_Argc () != 2)
281 Con_Print("exec <filename> : execute a script file\n");
285 f = (char *)FS_LoadFile (Cmd_Argv(1), tempmempool, false);
288 Con_Printf("couldn't exec %s\n",Cmd_Argv(1));
291 Con_DPrintf("execing %s\n",Cmd_Argv(1));
302 Just prints the rest of the line to the console
305 static void Cmd_Echo_f (void)
309 for (i=1 ; i<Cmd_Argc() ; i++)
310 Con_Printf("%s ",Cmd_Argv(i));
318 Creates a new command that executes a command string (possibly ; seperated)
321 static void Cmd_Alias_f (void)
330 Con_Print("Current alias commands:\n");
331 for (a = cmd_alias ; a ; a=a->next)
332 Con_Printf("%s : %s\n", a->name, a->value);
337 if (strlen(s) >= MAX_ALIAS_NAME)
339 Con_Print("Alias name is too long\n");
343 // if the alias already exists, reuse it
344 for (a = cmd_alias ; a ; a=a->next)
346 if (!strcmp(s, a->name))
355 a = Z_Malloc (sizeof(cmdalias_t));
359 strlcpy (a->name, s, sizeof (a->name));
361 // copy the rest of the command line
362 cmd[0] = 0; // start out with a null string
364 for (i=2 ; i< c ; i++)
366 strlcat (cmd, Cmd_Argv(i), sizeof (cmd));
368 strlcat (cmd, " ", sizeof (cmd));
370 strlcat (cmd, "\n", sizeof (cmd));
372 a->value = Z_Malloc (strlen (cmd) + 1);
373 strcpy (a->value, cmd);
377 =============================================================================
381 =============================================================================
384 typedef struct cmd_function_s
386 struct cmd_function_s *next;
395 static const char *cmd_argv[MAX_ARGS];
396 static const char *cmd_null_string = "";
397 static const char *cmd_args = NULL;
399 cmd_source_t cmd_source;
402 static cmd_function_t *cmd_functions; // possible commands to execute
408 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
409 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
413 static void Cmd_List_f (void)
421 partial = Cmd_Argv (1);
422 len = strlen(partial);
431 for (cmd = cmd_functions; cmd; cmd = cmd->next)
433 if (partial && strncmp(partial, cmd->name, len))
435 Con_Printf("%s\n", cmd->name);
439 Con_Printf("%i Command%s", count, (count > 1) ? "s" : "");
441 Con_Printf(" beginning with \"%s\"", partial);
453 cmd_mempool = Mem_AllocPool("commands", 0, NULL);
456 // register our commands
458 Cmd_AddCommand ("stuffcmds",Cmd_StuffCmds_f);
459 Cmd_AddCommand ("exec",Cmd_Exec_f);
460 Cmd_AddCommand ("echo",Cmd_Echo_f);
461 Cmd_AddCommand ("alias",Cmd_Alias_f);
462 Cmd_AddCommand ("cmd", Cmd_ForwardToServer);
463 Cmd_AddCommand ("wait", Cmd_Wait_f);
464 Cmd_AddCommand ("cmdlist", Cmd_List_f); // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
465 Cmd_AddCommand ("cvarlist", Cvar_List_f); // 2000-01-09 CmdList, CvarList commands
466 // By Matthias "Maddes" Buecher
467 Cmd_AddCommand ("set", Cvar_Set_f);
468 Cmd_AddCommand ("seta", Cvar_SetA_f);
476 void Cmd_Shutdown(void)
478 Mem_FreePool(&cmd_mempool);
496 const char *Cmd_Argv (int arg)
498 if (arg >= cmd_argc )
499 return cmd_null_string;
500 return cmd_argv[arg];
508 const char *Cmd_Args (void)
518 Parses the given string into command line tokens.
521 static void Cmd_TokenizeString (const char *text)
530 // skip whitespace up to a /n
531 while (*text && *text <= ' ' && *text != '\n')
536 // a newline seperates commands in the buffer
547 if (!COM_ParseTokenConsole(&text))
550 if (cmd_argc < MAX_ARGS)
552 l = strlen(com_token) + 1;
553 if (cmd_tokenizebufferpos + l > CMD_TOKENIZELENGTH)
554 Sys_Error("Cmd_TokenizeString: ran out of %i character buffer space for command arguements\n", CMD_TOKENIZELENGTH);
555 strcpy (cmd_tokenizebuffer + cmd_tokenizebufferpos, com_token);
556 cmd_argv[cmd_argc] = cmd_tokenizebuffer + cmd_tokenizebufferpos;
557 cmd_tokenizebufferpos += l;
570 void Cmd_AddCommand (const char *cmd_name, xcommand_t function)
574 // fail if the command is a variable name
575 if (Cvar_VariableString(cmd_name)[0])
577 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
581 // fail if the command already exists
582 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
584 if (!strcmp (cmd_name, cmd->name))
586 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
591 cmd = Mem_Alloc(cmd_mempool, sizeof(cmd_function_t));
592 cmd->name = cmd_name;
593 cmd->function = function;
594 cmd->next = cmd_functions;
603 qboolean Cmd_Exists (const char *cmd_name)
607 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
608 if (!strcmp (cmd_name,cmd->name))
620 const char *Cmd_CompleteCommand (const char *partial)
625 len = strlen(partial);
631 for (cmd = cmd_functions; cmd; cmd = cmd->next)
632 if (!strncmp(partial, cmd->name, len))
639 Cmd_CompleteCountPossible
641 New function for tab-completion system
643 Thanks to Fett erich@heintz.com
647 int Cmd_CompleteCountPossible (const char *partial)
653 len = strlen(partial);
658 // Loop through the command list and count all partial matches
659 for (cmd = cmd_functions; cmd; cmd = cmd->next)
660 if (!strncasecmp(partial, cmd->name, len))
667 Cmd_CompleteBuildList
669 New function for tab-completion system
671 Thanks to Fett erich@heintz.com
675 const char **Cmd_CompleteBuildList (const char *partial)
680 int sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (const char *);
683 len = strlen(partial);
684 buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
685 // Loop through the alias list and print all matches
686 for (cmd = cmd_functions; cmd; cmd = cmd->next)
687 if (!strncasecmp(partial, cmd->name, len))
688 buf[bpos++] = cmd->name;
697 New function for tab-completion system
699 Thanks to Fett erich@heintz.com
703 const char *Cmd_CompleteAlias (const char *partial)
708 len = strlen(partial);
714 for (alias = cmd_alias; alias; alias = alias->next)
715 if (!strncasecmp(partial, alias->name, len))
722 Cmd_CompleteAliasCountPossible
724 New function for tab-completion system
726 Thanks to Fett erich@heintz.com
730 int Cmd_CompleteAliasCountPossible (const char *partial)
738 len = strlen(partial);
743 // Loop through the command list and count all partial matches
744 for (alias = cmd_alias; alias; alias = alias->next)
745 if (!strncasecmp(partial, alias->name, len))
752 Cmd_CompleteAliasBuildList
754 New function for tab-completion system
756 Thanks to Fett erich@heintz.com
760 const char **Cmd_CompleteAliasBuildList (const char *partial)
765 int sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (const char *);
768 len = strlen(partial);
769 buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
770 // Loop through the alias list and print all matches
771 for (alias = cmd_alias; alias; alias = alias->next)
772 if (!strncasecmp(partial, alias->name, len))
773 buf[bpos++] = alias->name;
783 A complete command line has been parsed, so try to execute it
784 FIXME: lookupnoadd the token to speed search?
787 void Cmd_ExecuteString (const char *text, cmd_source_t src)
793 oldpos = cmd_tokenizebufferpos;
795 Cmd_TokenizeString (text);
797 // execute the command line
800 cmd_tokenizebufferpos = oldpos;
804 // check functions (only after host_initialized)
805 if (host_initialized || !strcasecmp(cmd_argv[0], "exec") || !strcasecmp(cmd_argv[0], "set") || !strcasecmp(cmd_argv[0], "seta"))
807 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
809 if (!strcasecmp (cmd_argv[0],cmd->name))
812 cmd_tokenizebufferpos = oldpos;
818 // check alias (only after host_initialized)
819 if (host_initialized)
821 for (a=cmd_alias ; a ; a=a->next)
823 if (!strcasecmp (cmd_argv[0], a->name))
825 Cbuf_InsertText (a->value);
826 cmd_tokenizebufferpos = oldpos;
832 // check cvars (always)
833 if (!Cvar_Command () && host_initialized)
834 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(0));
836 cmd_tokenizebufferpos = oldpos;
842 Cmd_ForwardStringToServer
844 Sends an entire command string over to the server, unprocessed
847 void Cmd_ForwardStringToServer (const char *s)
849 if (cls.state != ca_connected)
851 Con_Printf("Can't \"%s\", not connected\n", s);
855 if (cls.demoplayback)
856 return; // not really connected
858 // LordHavoc: thanks to Fuh for bringing the pure evil of SZ_Print to my
859 // attention, it has been eradicated from here, its only (former) use in
860 // all of darkplaces.
861 MSG_WriteByte(&cls.message, clc_stringcmd);
862 SZ_Write(&cls.message, s, strlen(s) + 1);
869 Sends the entire command line over to the server
872 void Cmd_ForwardToServer (void)
875 if (strcasecmp(Cmd_Argv(0), "cmd"))
876 s = va("%s %s", Cmd_Argv(0), Cmd_Argc() > 1 ? Cmd_Args() : "");
878 s = Cmd_Argc() > 1 ? Cmd_Args() : "";
879 Cmd_ForwardStringToServer(s);
887 Returns the position (1 to argc-1) in the command's argument list
888 where the given parameter apears, or 0 if not present
892 int Cmd_CheckParm (const char *parm)
897 Sys_Error ("Cmd_CheckParm: NULL");
899 for (i = 1; i < Cmd_Argc (); i++)
900 if (!strcasecmp (parm, Cmd_Argv (i)))