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 // LordHavoc: inreased this from 8192 to 32768
68 static sizebuf_t cmd_text;
69 static qbyte cmd_text_buf[32768];
78 // space for commands and script files
79 cmd_text.data = cmd_text_buf;
80 cmd_text.maxsize = sizeof(cmd_text_buf);
89 void Cbuf_Shutdown (void)
97 Adds command text at the end of the buffer
100 void Cbuf_AddText (const char *text)
106 if (cmd_text.cursize + l >= cmd_text.maxsize)
108 Con_Print("Cbuf_AddText: overflow\n");
112 SZ_Write (&cmd_text, text, strlen (text));
120 Adds command text immediately after the current command
121 Adds a \n to the text
122 FIXME: actually change the command buffer to do less copying
125 void Cbuf_InsertText (const char *text)
130 // copy off any commands still remaining in the exec buffer
131 templen = cmd_text.cursize;
134 temp = Mem_Alloc (tempmempool, templen);
135 memcpy (temp, cmd_text.data, templen);
136 SZ_Clear (&cmd_text);
141 // add the entire text of the file
144 // add the copied off data
147 SZ_Write (&cmd_text, temp, templen);
157 void Cbuf_Execute (void)
164 // LordHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
165 cmd_tokenizebufferpos = 0;
167 while (cmd_text.cursize)
169 // find a \n or ; line break
170 text = (char *)cmd_text.data;
173 for (i=0 ; i< cmd_text.cursize ; i++)
177 if ( !(quotes&1) && text[i] == ';')
178 break; // don't break if inside a quoted string
179 if (text[i] == '\r' || text[i] == '\n')
183 memcpy (line, text, i);
186 // delete the text from the command buffer and move remaining commands down
187 // this is necessary because commands (exec, alias) can insert data at the
188 // beginning of the text buffer
190 if (i == cmd_text.cursize)
191 cmd_text.cursize = 0;
195 cmd_text.cursize -= i;
196 memcpy (cmd_text.data, text+i, cmd_text.cursize);
199 // execute the command line
200 Cmd_ExecuteString (line, src_command);
203 { // skip out while text still remains in buffer, leaving it
212 ==============================================================================
216 ==============================================================================
223 Adds command line parameters as script statements
224 Commands lead with a +, and continue until a - or another +
225 quake +prog jctest.qp +cmd amlev1
226 quake -nosound +cmd amlev1
229 void Cmd_StuffCmds_f (void)
232 // this is per command, and bounds checked (no buffer overflows)
235 if (Cmd_Argc () != 1)
237 Con_Print("stuffcmds : execute command line parameters\n");
241 for (i = 0;i < com_argc;i++)
243 if (com_argv[i] && com_argv[i][0] == '+' && (com_argv[i][1] < '0' || com_argv[i][1] > '9'))
247 while (com_argv[i][j])
248 build[l++] = com_argv[i][j++];
250 for (;i < com_argc;i++)
254 if ((com_argv[i][0] == '+' || com_argv[i][0] == '-') && (com_argv[i][1] < '0' || com_argv[i][1] > '9'))
256 if (l + strlen(com_argv[i]) + 5 > sizeof(build))
260 for (j = 0;com_argv[i][j];j++)
261 build[l++] = com_argv[i][j];
266 Cbuf_InsertText (build);
278 static void Cmd_Exec_f (void)
282 if (Cmd_Argc () != 2)
284 Con_Print("exec <filename> : execute a script file\n");
288 f = (char *)FS_LoadFile (Cmd_Argv(1), tempmempool, false);
291 Con_Printf("couldn't exec %s\n",Cmd_Argv(1));
294 Con_DPrintf("execing %s\n",Cmd_Argv(1));
305 Just prints the rest of the line to the console
308 static void Cmd_Echo_f (void)
312 for (i=1 ; i<Cmd_Argc() ; i++)
313 Con_Printf("%s ",Cmd_Argv(i));
321 Creates a new command that executes a command string (possibly ; seperated)
324 static void Cmd_Alias_f (void)
333 Con_Print("Current alias commands:\n");
334 for (a = cmd_alias ; a ; a=a->next)
335 Con_Printf("%s : %s\n", a->name, a->value);
340 if (strlen(s) >= MAX_ALIAS_NAME)
342 Con_Print("Alias name is too long\n");
346 // if the alias already exists, reuse it
347 for (a = cmd_alias ; a ; a=a->next)
349 if (!strcmp(s, a->name))
358 a = Z_Malloc (sizeof(cmdalias_t));
362 strlcpy (a->name, s, sizeof (a->name));
364 // copy the rest of the command line
365 cmd[0] = 0; // start out with a null string
367 for (i=2 ; i< c ; i++)
369 strlcat (cmd, Cmd_Argv(i), sizeof (cmd));
371 strlcat (cmd, " ", sizeof (cmd));
373 strlcat (cmd, "\n", sizeof (cmd));
375 a->value = Z_Malloc (strlen (cmd) + 1);
376 strcpy (a->value, cmd);
380 =============================================================================
384 =============================================================================
387 typedef struct cmd_function_s
389 struct cmd_function_s *next;
398 static const char *cmd_argv[MAX_ARGS];
399 static const char *cmd_null_string = "";
400 static const char *cmd_args = NULL;
402 cmd_source_t cmd_source;
405 static cmd_function_t *cmd_functions; // possible commands to execute
411 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
412 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
416 static void Cmd_List_f (void)
424 partial = Cmd_Argv (1);
425 len = strlen(partial);
434 for (cmd = cmd_functions; cmd; cmd = cmd->next)
436 if (partial && strncmp(partial, cmd->name, len))
438 Con_Printf("%s\n", cmd->name);
442 Con_Printf("%i Command%s", count, (count > 1) ? "s" : "");
444 Con_Printf(" beginning with \"%s\"", partial);
456 cmd_mempool = Mem_AllocPool("commands", 0, NULL);
459 // register our commands
461 Cmd_AddCommand ("stuffcmds",Cmd_StuffCmds_f);
462 Cmd_AddCommand ("exec",Cmd_Exec_f);
463 Cmd_AddCommand ("echo",Cmd_Echo_f);
464 Cmd_AddCommand ("alias",Cmd_Alias_f);
465 Cmd_AddCommand ("cmd", Cmd_ForwardToServer);
466 Cmd_AddCommand ("wait", Cmd_Wait_f);
467 Cmd_AddCommand ("cmdlist", Cmd_List_f); // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
468 Cmd_AddCommand ("cvarlist", Cvar_List_f); // 2000-01-09 CmdList, CvarList commands
469 // By Matthias "Maddes" Buecher
470 Cmd_AddCommand ("set", Cvar_Set_f);
471 Cmd_AddCommand ("seta", Cvar_SetA_f);
479 void Cmd_Shutdown(void)
481 Mem_FreePool(&cmd_mempool);
499 const char *Cmd_Argv (int arg)
501 if (arg >= cmd_argc )
502 return cmd_null_string;
503 return cmd_argv[arg];
511 const char *Cmd_Args (void)
521 Parses the given string into command line tokens.
524 static void Cmd_TokenizeString (const char *text)
533 // skip whitespace up to a /n
534 while (*text && *text <= ' ' && *text != '\r' && *text != '\n')
541 if (*text == '\n' || *text == '\r')
543 // a newline seperates commands in the buffer
544 if (*text == '\r' && text[1] == '\n')
556 if (!COM_ParseTokenConsole(&text))
559 if (cmd_argc < MAX_ARGS)
561 l = strlen(com_token) + 1;
562 if (cmd_tokenizebufferpos + l > CMD_TOKENIZELENGTH)
563 Sys_Error("Cmd_TokenizeString: ran out of %i character buffer space for command arguements\n", CMD_TOKENIZELENGTH);
564 strcpy (cmd_tokenizebuffer + cmd_tokenizebufferpos, com_token);
565 cmd_argv[cmd_argc] = cmd_tokenizebuffer + cmd_tokenizebufferpos;
566 cmd_tokenizebufferpos += l;
579 void Cmd_AddCommand (const char *cmd_name, xcommand_t function)
583 // fail if the command is a variable name
584 if (Cvar_VariableString(cmd_name)[0])
586 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
590 // fail if the command already exists
591 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
593 if (!strcmp (cmd_name, cmd->name))
595 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
600 cmd = Mem_Alloc(cmd_mempool, sizeof(cmd_function_t));
601 cmd->name = cmd_name;
602 cmd->function = function;
603 cmd->next = cmd_functions;
612 qboolean Cmd_Exists (const char *cmd_name)
616 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
617 if (!strcmp (cmd_name,cmd->name))
629 const char *Cmd_CompleteCommand (const char *partial)
634 len = strlen(partial);
640 for (cmd = cmd_functions; cmd; cmd = cmd->next)
641 if (!strncmp(partial, cmd->name, len))
648 Cmd_CompleteCountPossible
650 New function for tab-completion system
652 Thanks to Fett erich@heintz.com
656 int Cmd_CompleteCountPossible (const char *partial)
662 len = strlen(partial);
667 // Loop through the command list and count all partial matches
668 for (cmd = cmd_functions; cmd; cmd = cmd->next)
669 if (!strncasecmp(partial, cmd->name, len))
676 Cmd_CompleteBuildList
678 New function for tab-completion system
680 Thanks to Fett erich@heintz.com
684 const char **Cmd_CompleteBuildList (const char *partial)
689 int sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (const char *);
692 len = strlen(partial);
693 buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
694 // Loop through the alias list and print all matches
695 for (cmd = cmd_functions; cmd; cmd = cmd->next)
696 if (!strncasecmp(partial, cmd->name, len))
697 buf[bpos++] = cmd->name;
706 New function for tab-completion system
708 Thanks to Fett erich@heintz.com
712 const char *Cmd_CompleteAlias (const char *partial)
717 len = strlen(partial);
723 for (alias = cmd_alias; alias; alias = alias->next)
724 if (!strncasecmp(partial, alias->name, len))
731 Cmd_CompleteAliasCountPossible
733 New function for tab-completion system
735 Thanks to Fett erich@heintz.com
739 int Cmd_CompleteAliasCountPossible (const char *partial)
747 len = strlen(partial);
752 // Loop through the command list and count all partial matches
753 for (alias = cmd_alias; alias; alias = alias->next)
754 if (!strncasecmp(partial, alias->name, len))
761 Cmd_CompleteAliasBuildList
763 New function for tab-completion system
765 Thanks to Fett erich@heintz.com
769 const char **Cmd_CompleteAliasBuildList (const char *partial)
774 int sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (const char *);
777 len = strlen(partial);
778 buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
779 // Loop through the alias list and print all matches
780 for (alias = cmd_alias; alias; alias = alias->next)
781 if (!strncasecmp(partial, alias->name, len))
782 buf[bpos++] = alias->name;
792 A complete command line has been parsed, so try to execute it
793 FIXME: lookupnoadd the token to speed search?
796 void Cmd_ExecuteString (const char *text, cmd_source_t src)
802 oldpos = cmd_tokenizebufferpos;
804 Cmd_TokenizeString (text);
806 // execute the command line
809 cmd_tokenizebufferpos = oldpos;
813 // check functions (only after host_initialized)
814 if (host_initialized || !strcasecmp(cmd_argv[0], "exec") || !strcasecmp(cmd_argv[0], "set") || !strcasecmp(cmd_argv[0], "seta"))
816 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
818 if (!strcasecmp (cmd_argv[0],cmd->name))
821 cmd_tokenizebufferpos = oldpos;
827 // check alias (only after host_initialized)
828 if (host_initialized)
830 for (a=cmd_alias ; a ; a=a->next)
832 if (!strcasecmp (cmd_argv[0], a->name))
834 Cbuf_InsertText (a->value);
835 cmd_tokenizebufferpos = oldpos;
841 // check cvars (always)
842 if (!Cvar_Command () && host_initialized)
843 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(0));
845 cmd_tokenizebufferpos = oldpos;
851 Cmd_ForwardStringToServer
853 Sends an entire command string over to the server, unprocessed
856 void Cmd_ForwardStringToServer (const char *s)
858 if (cls.state != ca_connected)
860 Con_Printf("Can't \"%s\", not connected\n", s);
864 if (cls.demoplayback)
865 return; // not really connected
867 // LordHavoc: thanks to Fuh for bringing the pure evil of SZ_Print to my
868 // attention, it has been eradicated from here, its only (former) use in
869 // all of darkplaces.
870 MSG_WriteByte(&cls.message, clc_stringcmd);
871 SZ_Write(&cls.message, s, strlen(s) + 1);
878 Sends the entire command line over to the server
881 void Cmd_ForwardToServer (void)
884 if (strcasecmp(Cmd_Argv(0), "cmd"))
885 s = va("%s %s", Cmd_Argv(0), Cmd_Argc() > 1 ? Cmd_Args() : "");
887 s = Cmd_Argc() > 1 ? Cmd_Args() : "";
888 Cmd_ForwardStringToServer(s);
896 Returns the position (1 to argc-1) in the command's argument list
897 where the given parameter apears, or 0 if not present
901 int Cmd_CheckParm (const char *parm)
906 Sys_Error ("Cmd_CheckParm: NULL");
908 for (i = 1; i < Cmd_Argc (); i++)
909 if (!strcasecmp (parm, Cmd_Argv (i)))