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 = Mem_Alloc (tempmempool, templen);
123 memcpy (temp, cmd_text.data, templen);
124 SZ_Clear (&cmd_text);
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 for (i=0 ; i< cmd_text.cursize ; i++)
165 if ( !(quotes&1) && text[i] == ';')
166 break; // don't break if inside a quoted string
167 if (text[i] == '\r' || text[i] == '\n')
171 memcpy (line, text, i);
174 // delete the text from the command buffer and move remaining commands down
175 // this is necessary because commands (exec, alias) can insert data at the
176 // beginning of the text buffer
178 if (i == cmd_text.cursize)
179 cmd_text.cursize = 0;
183 cmd_text.cursize -= i;
184 memcpy (cmd_text.data, text+i, cmd_text.cursize);
187 // execute the command line
188 Cmd_ExecuteString (line, src_command);
191 { // skip out while text still remains in buffer, leaving it
200 ==============================================================================
204 ==============================================================================
211 Adds command line parameters as script statements
212 Commands lead with a +, and continue until a - or another +
213 quake +prog jctest.qp +cmd amlev1
214 quake -nosound +cmd amlev1
217 void Cmd_StuffCmds_f (void)
220 // this is per command, and bounds checked (no buffer overflows)
223 if (Cmd_Argc () != 1)
225 Con_Print("stuffcmds : execute command line parameters\n");
229 for (i = 0;i < com_argc;i++)
231 if (com_argv[i] && com_argv[i][0] == '+' && (com_argv[i][1] < '0' || com_argv[i][1] > '9'))
235 while (com_argv[i][j])
236 build[l++] = com_argv[i][j++];
238 for (;i < com_argc;i++)
242 if ((com_argv[i][0] == '+' || com_argv[i][0] == '-') && (com_argv[i][1] < '0' || com_argv[i][1] > '9'))
244 if (l + strlen(com_argv[i]) + 5 > sizeof(build))
248 for (j = 0;com_argv[i][j];j++)
249 build[l++] = com_argv[i][j];
254 Cbuf_InsertText (build);
266 static void Cmd_Exec_f (void)
270 if (Cmd_Argc () != 2)
272 Con_Print("exec <filename> : execute a script file\n");
276 f = (char *)FS_LoadFile (Cmd_Argv(1), tempmempool, false);
279 Con_Printf("couldn't exec %s\n",Cmd_Argv(1));
282 Con_DPrintf("execing %s\n",Cmd_Argv(1));
293 Just prints the rest of the line to the console
296 static void Cmd_Echo_f (void)
300 for (i=1 ; i<Cmd_Argc() ; i++)
301 Con_Printf("%s ",Cmd_Argv(i));
309 Creates a new command that executes a command string (possibly ; seperated)
312 static void Cmd_Alias_f (void)
321 Con_Print("Current alias commands:\n");
322 for (a = cmd_alias ; a ; a=a->next)
323 Con_Printf("%s : %s\n", a->name, a->value);
328 if (strlen(s) >= MAX_ALIAS_NAME)
330 Con_Print("Alias name is too long\n");
334 // if the alias already exists, reuse it
335 for (a = cmd_alias ; a ; a=a->next)
337 if (!strcmp(s, a->name))
346 a = Z_Malloc (sizeof(cmdalias_t));
350 strlcpy (a->name, s, sizeof (a->name));
352 // copy the rest of the command line
353 cmd[0] = 0; // start out with a null string
355 for (i=2 ; i< c ; i++)
357 strlcat (cmd, Cmd_Argv(i), sizeof (cmd));
359 strlcat (cmd, " ", sizeof (cmd));
361 strlcat (cmd, "\n", sizeof (cmd));
363 a->value = Z_Malloc (strlen (cmd) + 1);
364 strcpy (a->value, cmd);
368 =============================================================================
372 =============================================================================
375 typedef struct cmd_function_s
377 struct cmd_function_s *next;
386 static const char *cmd_argv[MAX_ARGS];
387 static const char *cmd_null_string = "";
388 static const char *cmd_args = NULL;
390 cmd_source_t cmd_source;
393 static cmd_function_t *cmd_functions; // possible commands to execute
399 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
400 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
404 static void Cmd_List_f (void)
412 partial = Cmd_Argv (1);
413 len = strlen(partial);
422 for (cmd = cmd_functions; cmd; cmd = cmd->next)
424 if (partial && strncmp(partial, cmd->name, len))
426 Con_Printf("%s\n", cmd->name);
430 Con_Printf("%i Command%s", count, (count > 1) ? "s" : "");
432 Con_Printf(" beginning with \"%s\"", partial);
444 cmd_mempool = Mem_AllocPool("commands", 0, NULL);
447 // register our commands
449 Cmd_AddCommand ("stuffcmds",Cmd_StuffCmds_f);
450 Cmd_AddCommand ("exec",Cmd_Exec_f);
451 Cmd_AddCommand ("echo",Cmd_Echo_f);
452 Cmd_AddCommand ("alias",Cmd_Alias_f);
453 Cmd_AddCommand ("cmd", Cmd_ForwardToServer);
454 Cmd_AddCommand ("wait", Cmd_Wait_f);
455 Cmd_AddCommand ("cmdlist", Cmd_List_f); // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
456 Cmd_AddCommand ("cvarlist", Cvar_List_f); // 2000-01-09 CmdList, CvarList commands
457 // By Matthias "Maddes" Buecher
458 Cmd_AddCommand ("set", Cvar_Set_f);
459 Cmd_AddCommand ("seta", Cvar_SetA_f);
477 const char *Cmd_Argv (int arg)
479 if (arg >= cmd_argc )
480 return cmd_null_string;
481 return cmd_argv[arg];
489 const char *Cmd_Args (void)
499 Parses the given string into command line tokens.
502 static void Cmd_TokenizeString (const char *text)
511 // skip whitespace up to a /n
512 while (*text && *text <= ' ' && *text != '\n')
517 // a newline seperates commands in the buffer
528 if (!COM_ParseTokenConsole(&text))
531 if (cmd_argc < MAX_ARGS)
533 l = strlen(com_token) + 1;
534 if (cmd_tokenizebufferpos + l > CMD_TOKENIZELENGTH)
535 Sys_Error("Cmd_TokenizeString: ran out of %i character buffer space for command arguements\n", CMD_TOKENIZELENGTH);
536 strcpy (cmd_tokenizebuffer + cmd_tokenizebufferpos, com_token);
537 cmd_argv[cmd_argc] = cmd_tokenizebuffer + cmd_tokenizebufferpos;
538 cmd_tokenizebufferpos += l;
551 void Cmd_AddCommand (const char *cmd_name, xcommand_t function)
555 // fail if the command is a variable name
556 if (Cvar_VariableString(cmd_name)[0])
558 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
562 // fail if the command already exists
563 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
565 if (!strcmp (cmd_name, cmd->name))
567 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
572 cmd = Mem_Alloc(cmd_mempool, sizeof(cmd_function_t));
573 cmd->name = cmd_name;
574 cmd->function = function;
575 cmd->next = cmd_functions;
584 qboolean Cmd_Exists (const char *cmd_name)
588 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
589 if (!strcmp (cmd_name,cmd->name))
601 const char *Cmd_CompleteCommand (const char *partial)
606 len = strlen(partial);
612 for (cmd = cmd_functions; cmd; cmd = cmd->next)
613 if (!strncmp(partial, cmd->name, len))
620 Cmd_CompleteCountPossible
622 New function for tab-completion system
624 Thanks to Fett erich@heintz.com
628 int Cmd_CompleteCountPossible (const char *partial)
634 len = strlen(partial);
639 // Loop through the command list and count all partial matches
640 for (cmd = cmd_functions; cmd; cmd = cmd->next)
641 if (!strncasecmp(partial, cmd->name, len))
648 Cmd_CompleteBuildList
650 New function for tab-completion system
652 Thanks to Fett erich@heintz.com
656 const char **Cmd_CompleteBuildList (const char *partial)
661 int sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (const char *);
664 len = strlen(partial);
665 buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
666 // Loop through the alias list and print all matches
667 for (cmd = cmd_functions; cmd; cmd = cmd->next)
668 if (!strncasecmp(partial, cmd->name, len))
669 buf[bpos++] = cmd->name;
678 New function for tab-completion system
680 Thanks to Fett erich@heintz.com
684 const char *Cmd_CompleteAlias (const char *partial)
689 len = strlen(partial);
695 for (alias = cmd_alias; alias; alias = alias->next)
696 if (!strncasecmp(partial, alias->name, len))
703 Cmd_CompleteAliasCountPossible
705 New function for tab-completion system
707 Thanks to Fett erich@heintz.com
711 int Cmd_CompleteAliasCountPossible (const char *partial)
719 len = strlen(partial);
724 // Loop through the command list and count all partial matches
725 for (alias = cmd_alias; alias; alias = alias->next)
726 if (!strncasecmp(partial, alias->name, len))
733 Cmd_CompleteAliasBuildList
735 New function for tab-completion system
737 Thanks to Fett erich@heintz.com
741 const char **Cmd_CompleteAliasBuildList (const char *partial)
746 int sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (const char *);
749 len = strlen(partial);
750 buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
751 // Loop through the alias list and print all matches
752 for (alias = cmd_alias; alias; alias = alias->next)
753 if (!strncasecmp(partial, alias->name, len))
754 buf[bpos++] = alias->name;
764 A complete command line has been parsed, so try to execute it
765 FIXME: lookupnoadd the token to speed search?
768 void Cmd_ExecuteString (const char *text, cmd_source_t src)
774 oldpos = cmd_tokenizebufferpos;
776 Cmd_TokenizeString (text);
778 // execute the command line
781 cmd_tokenizebufferpos = oldpos;
785 // check functions (only after host_initialized)
786 if (host_initialized || !strcasecmp(cmd_argv[0], "exec") || !strcasecmp(cmd_argv[0], "set") || !strcasecmp(cmd_argv[0], "seta"))
788 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
790 if (!strcasecmp (cmd_argv[0],cmd->name))
793 cmd_tokenizebufferpos = oldpos;
799 // check alias (only after host_initialized)
800 if (host_initialized)
802 for (a=cmd_alias ; a ; a=a->next)
804 if (!strcasecmp (cmd_argv[0], a->name))
806 Cbuf_InsertText (a->value);
807 cmd_tokenizebufferpos = oldpos;
813 // check cvars (always)
814 if (!Cvar_Command () && host_initialized)
815 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(0));
817 cmd_tokenizebufferpos = oldpos;
825 Sends the entire command line over to the server
828 void Cmd_ForwardToServer (void)
831 if (cls.state != ca_connected)
833 Con_Printf("Can't \"%s\", not connected\n", Cmd_Argv(0));
837 if (cls.demoplayback)
838 return; // not really connected
840 // LordHavoc: thanks to Fuh for bringing the pure evil of SZ_Print to my
841 // attention, it has been eradicated from here, its only (former) use in
842 // all of darkplaces.
843 if (strcasecmp(Cmd_Argv(0), "cmd") != 0)
844 s = va("%s %s", Cmd_Argv(0), Cmd_Argc() > 1 ? Cmd_Args() : "\n");
846 s = Cmd_Argc() > 1 ? Cmd_Args() : "\n";
847 MSG_WriteByte(&cls.message, clc_stringcmd);
848 SZ_Write(&cls.message, s, strlen(s) + 1);
856 Returns the position (1 to argc-1) in the command's argument list
857 where the given parameter apears, or 0 if not present
861 int Cmd_CheckParm (const char *parm)
866 Sys_Error ("Cmd_CheckParm: NULL");
868 for (i = 1; i < Cmd_Argc (); i++)
869 if (!strcasecmp (parm, Cmd_Argv (i)))