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
25 cmd_state_t *cmd_local;
26 cmd_state_t *cmd_serverfromclient;
28 cmd_userdefined_t cmd_userdefined_all;
29 cmd_userdefined_t cmd_userdefined_null;
31 typedef struct cmd_iter_s {
36 static cmd_iter_t *cmd_iter_all;
38 mempool_t *cbuf_mempool;
40 // we only run the +whatever commandline arguments once
41 qbool host_stuffcmdsrun = false;
43 //=============================================================================
45 void Cbuf_Lock(cmd_buf_t *cbuf)
47 Thread_LockMutex(cbuf->lock);
50 void Cbuf_Unlock(cmd_buf_t *cbuf)
52 Thread_UnlockMutex(cbuf->lock);
60 Causes execution of the remainder of the command buffer to be delayed until
61 next frame. This allows commands like:
62 bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
65 static void Cmd_Wait_f (cmd_state_t *cmd)
67 cmd->cbuf->wait = true;
74 Cause a command to be executed after a delay.
77 static cmd_input_t *Cbuf_LinkGet(cmd_buf_t *cbuf, cmd_input_t *existing);
78 static void Cmd_Defer_f (cmd_state_t *cmd)
81 cmd_buf_t *cbuf = cmd->cbuf;
83 if(Cmd_Argc(cmd) == 1)
85 if(List_Is_Empty(&cbuf->deferred))
86 Con_Printf("No commands are pending.\n");
89 List_For_Each_Entry(current, &cbuf->deferred, cmd_input_t, list)
90 Con_Printf("-> In %9.2f: %s\n", current->delay, current->text);
93 else if(Cmd_Argc(cmd) == 2 && !strcasecmp("clear", Cmd_Argv(cmd, 1)))
95 while(!List_Is_Empty(&cbuf->deferred))
96 List_Move_Tail(cbuf->deferred.next, &cbuf->free);
98 else if(Cmd_Argc(cmd) == 3)
100 const char *text = Cmd_Argv(cmd, 2);
101 current = Cbuf_LinkGet(cbuf, NULL);
102 current->length = strlen(text);
103 current->source = cmd;
104 current->delay = atof(Cmd_Argv(cmd, 1));
106 if(current->size < current->length)
108 current->text = (char *)Mem_Realloc(cbuf_mempool, current->text, current->length + 1);
109 current->size = current->length;
112 strlcpy(current->text, text, current->length + 1);
114 List_Move_Tail(¤t->list, &cbuf->deferred);
118 Con_Printf("usage: defer <seconds> <command>\n"
128 Print something to the center of the screen using SCR_Centerprint
131 static void Cmd_Centerprint_f (cmd_state_t *cmd)
133 char msg[MAX_INPUTLINE];
134 unsigned int i, c, p;
138 strlcpy(msg, Cmd_Argv(cmd,1), sizeof(msg));
139 for(i = 2; i < c; ++i)
141 strlcat(msg, " ", sizeof(msg));
142 strlcat(msg, Cmd_Argv(cmd, i), sizeof(msg));
144 c = (unsigned int)strlen(msg);
145 for(p = 0, i = 0; i < c; ++i)
151 else if(msg[i+1] == '\\')
163 SCR_CenterPrint(msg);
168 =============================================================================
172 =============================================================================
175 static cmd_input_t *Cbuf_LinkGet(cmd_buf_t *cbuf, cmd_input_t *existing)
177 cmd_input_t *ret = NULL;
178 if(existing && existing->pending)
180 else if(!List_Is_Empty(&cbuf->free))
182 ret = List_Entry(cbuf->free.next, cmd_input_t, list);
184 ret->pending = false;
189 static cmd_input_t *Cmd_AllocInputNode(void)
191 cmd_input_t *node = (cmd_input_t *)Mem_Alloc(cbuf_mempool, sizeof(cmd_input_t));
192 node->list.prev = node->list.next = &node->list;
193 node->size = node->length = node->pending = 0;
198 // Cloudwalk FIXME: The entire design of this thing is overly complicated.
199 // We could very much safely have one input node per line whether or not
200 // the command was terminated. We don't need to split up input nodes per command
202 static size_t Cmd_ParseInput (cmd_input_t **output, char **input)
204 size_t pos, cmdsize = 0, start = 0;
205 qbool command = false, lookahead = false;
206 qbool quotes = false, comment = false;
207 qbool escaped = false;
210 * The Quake command-line is super basic. It can be entered in the console
211 * or in config files. A semicolon is used to terminate a command and chain
212 * them together. Otherwise, a newline delineates command input.
214 * In most engines, the Quake command-line is a simple linear text buffer that
215 * is parsed when it executes. In Darkplaces, we use a linked list of command
216 * input and parse the input on the spot.
218 * This was done because Darkplaces allows multiple command interpreters on the
219 * same thread. Previously, each interpreter maintained its own buffer and this
220 * caused problems related to execution order, and maintaining a single simple
221 * buffer for all interpreters makes it non-trivial to keep track of which
222 * command should execute on which interpreter.
225 // Run until command and lookahead are both true, or until we run out of input.
226 for (pos = 0; (*input)[pos]; pos++)
228 // Look for newlines and semicolons. Ignore semicolons in quotes.
229 switch((*input)[pos])
237 if(!comment) // Not a newline so far. Still not a valid command yet.
239 if(!quotes && (*input)[pos] == ';') // Ignore semicolons in quotes.
241 else if (ISCOMMENT((*input), pos)) // Comments
256 switch((*input)[pos])
265 if (!escaped && quotes)
274 if(cmdsize && !command)
277 if(command && lookahead)
286 *output = Cmd_AllocInputNode();
288 // Append, since this input line hasn't closed yet.
289 if((*output)->pending)
290 offset = (*output)->length;
292 (*output)->length += cmdsize;
294 if((*output)->size < (*output)->length)
296 (*output)->text = (char *)Mem_Realloc(cbuf_mempool, (*output)->text, (*output)->length + 1);
297 (*output)->size = (*output)->length;
300 strlcpy(&(*output)->text[offset], &(*input)[start], cmdsize + 1);
303 * If we were still looking ahead by the time we broke from the loop, the command input
304 * hasn't terminated yet and we're still expecting more, so keep this node open for appending later.
306 (*output)->pending = !lookahead;
309 // Set input to its new position. Can be NULL.
310 *input = &(*input)[pos];
315 // Cloudwalk: Not happy with this, but it works.
316 static void Cbuf_LinkCreate(cmd_state_t *cmd, llist_t *head, cmd_input_t *existing, const char *text)
318 char *in = (char *)&text[0];
319 cmd_buf_t *cbuf = cmd->cbuf;
320 size_t totalsize = 0, newsize = 0;
321 cmd_input_t *current = NULL;
323 // Slide the pointer down until we reach the end
326 // Check if the current node is still accepting input (input line hasn't terminated)
327 current = Cbuf_LinkGet(cbuf, existing);
328 newsize = Cmd_ParseInput(¤t, &in);
333 // current will match existing if the input line hasn't terminated yet
334 if(current != existing)
336 current->source = cmd;
337 List_Move_Tail(¤t->list, head);
340 totalsize += newsize;
342 else if (current == existing && !totalsize)
343 current->pending = false;
347 cbuf->size += totalsize;
354 Adds command text at the end of the buffer
357 void Cbuf_AddText (cmd_state_t *cmd, const char *text)
359 size_t l = strlen(text);
360 cmd_buf_t *cbuf = cmd->cbuf;
361 llist_t llist = {&llist, &llist};
365 if (cbuf->maxsize - cbuf->size <= l)
366 Con_Print("Cbuf_AddText: overflow\n");
369 Cbuf_LinkCreate(cmd, &llist, (List_Is_Empty(&cbuf->start) ? NULL : List_Entry(cbuf->start.prev, cmd_input_t, list)), text);
370 if(!List_Is_Empty(&llist))
371 List_Splice_Tail(&llist, &cbuf->start);
380 Adds command text immediately after the current command
381 FIXME: actually change the command buffer to do less copying
384 void Cbuf_InsertText (cmd_state_t *cmd, const char *text)
386 cmd_buf_t *cbuf = cmd->cbuf;
387 llist_t llist = {&llist, &llist};
388 size_t l = strlen(text);
392 // we need to memmove the existing text and stuff this in before it...
393 if (cbuf->size + l >= cbuf->maxsize)
394 Con_Print("Cbuf_InsertText: overflow\n");
397 Cbuf_LinkCreate(cmd, &llist, List_Entry(cbuf->start.next, cmd_input_t, list), text);
398 if(!List_Is_Empty(&llist))
399 List_Splice(&llist, &cbuf->start);
407 Cbuf_Execute_Deferred --blub
410 static void Cbuf_Execute_Deferred (cmd_buf_t *cbuf)
412 cmd_input_t *current;
415 if (host.realtime - cbuf->deferred_oldtime < 0 || host.realtime - cbuf->deferred_oldtime > 1800)
416 cbuf->deferred_oldtime = host.realtime;
417 eat = host.realtime - cbuf->deferred_oldtime;
418 if (eat < (1.0 / 120.0))
420 cbuf->deferred_oldtime = host.realtime;
422 List_For_Each_Entry(current, &cbuf->deferred, cmd_input_t, list)
424 current->delay -= eat;
425 if(current->delay <= 0)
427 cbuf->size += current->length;
428 List_Move(¤t->list, &cbuf->start);
429 // We must return and come back next frame or the engine will freeze. Fragile... like glass :3
440 static qbool Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmd_alias_t *alias );
441 void Cbuf_Execute (cmd_buf_t *cbuf)
443 cmd_input_t *current;
444 char preprocessed[MAX_INPUTLINE];
447 // LadyHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
448 cbuf->tokenizebufferpos = 0;
450 while (!List_Is_Empty(&cbuf->start))
453 * Delete the text from the command buffer and move remaining
454 * commands down. This is necessary because commands (exec, alias)
455 * can insert data at the beginning of the text buffer
457 current = List_Entry(cbuf->start.next, cmd_input_t, list);
459 // Recycle memory so using WASD doesn't cause a malloc and free
460 List_Move_Tail(¤t->list, &cbuf->free);
463 * Assume we're rolling with the current command-line and
464 * always set this false because alias expansion or cbuf insertion
465 * without a newline may set this true, and cause weirdness.
467 current->pending = false;
469 cbuf->size -= current->length;
471 firstchar = current->text;
472 while(*firstchar && ISWHITESPACE(*firstchar))
474 if((strncmp(firstchar, "alias", 5) || !ISWHITESPACE(firstchar[5])) &&
475 (strncmp(firstchar, "bind", 4) || !ISWHITESPACE(firstchar[4])) &&
476 (strncmp(firstchar, "in_bind", 7) || !ISWHITESPACE(firstchar[7])))
478 if(Cmd_PreprocessString(current->source, current->text, preprocessed, sizeof(preprocessed), NULL ))
479 Cmd_ExecuteString(current->source, preprocessed, src_local, false);
483 Cmd_ExecuteString (current->source, current->text, src_local, false);
491 * Skip out while text still remains in
492 * buffer, leaving it for next frame
504 Add them exactly as if they had been typed at the console
507 static void Cbuf_Frame_Input(void)
511 while ((line = Sys_ConsoleInput()))
512 Cbuf_AddText(cmd_local, line);
515 void Cbuf_Frame(cmd_buf_t *cbuf)
517 // check for commands typed to the host
520 // R_TimeReport("preconsole");
522 // execute commands queued with the defer command
523 Cbuf_Execute_Deferred(cbuf);
526 SV_LockThreadMutex();
528 SV_UnlockThreadMutex();
531 // R_TimeReport("console");
535 ==============================================================================
539 ==============================================================================
546 Adds command line parameters as script statements
547 Commands lead with a +, and continue until a - or another +
548 quake +prog jctest.qp +cmd amlev1
549 quake -nosound +cmd amlev1
552 static void Cmd_StuffCmds_f (cmd_state_t *cmd)
555 // this is for all commandline options combined (and is bounds checked)
556 char build[MAX_INPUTLINE];
558 if (Cmd_Argc (cmd) != 1)
560 Con_Print("stuffcmds : execute command line parameters\n");
564 // no reason to run the commandline arguments twice
565 if (host_stuffcmdsrun)
568 host_stuffcmdsrun = true;
571 for (i = 0;i < sys.argc;i++)
573 if (sys.argv[i] && sys.argv[i][0] == '+' && (sys.argv[i][1] < '0' || sys.argv[i][1] > '9') && l + strlen(sys.argv[i]) - 1 <= sizeof(build) - 1)
576 while (sys.argv[i][j])
577 build[l++] = sys.argv[i][j++];
579 for (;i < sys.argc;i++)
583 if ((sys.argv[i][0] == '+' || sys.argv[i][0] == '-') && (sys.argv[i][1] < '0' || sys.argv[i][1] > '9'))
585 if (l + strlen(sys.argv[i]) + 4 > sizeof(build) - 1)
588 if (strchr(sys.argv[i], ' '))
590 for (j = 0;sys.argv[i][j];j++)
591 build[l++] = sys.argv[i][j];
592 if (strchr(sys.argv[i], ' '))
599 // now terminate the combined string and prepend it to the command buffer
600 // we already reserved space for the terminator
602 Cbuf_InsertText (cmd, build);
605 static void Cmd_Exec(cmd_state_t *cmd, const char *filename)
608 size_t filenameLen = strlen(filename);
610 !strcmp(filename, "default.cfg") ||
611 (filenameLen >= 12 && !strcmp(filename + filenameLen - 12, "/default.cfg"));
613 if (!strcmp(filename, "config.cfg"))
615 filename = CONFIGFILENAME;
616 if (Sys_CheckParm("-noconfig"))
617 return; // don't execute config.cfg
620 f = (char *)FS_LoadFile (filename, tempmempool, false, NULL);
623 Con_Printf("couldn't exec %s\n",filename);
626 Con_Printf("execing %s\n",filename);
628 // if executing default.cfg for the first time, lock the cvar defaults
629 // it may seem backwards to insert this text BEFORE the default.cfg
630 // but Cbuf_InsertText inserts before, so this actually ends up after it.
632 Cbuf_InsertText(cmd, "\ncvar_lockdefaults\n");
634 Cbuf_InsertText (cmd, f);
639 // special defaults for specific games go here, these execute before default.cfg
640 // Nehahra pushable crates malfunction in some levels if this is on
641 // Nehahra NPC AI is confused by blowupfallenzombies
645 Cbuf_InsertText(cmd, "\n"
646 "sv_gameplayfix_blowupfallenzombies 0\n"
647 "sv_gameplayfix_findradiusdistancetobox 0\n"
648 "sv_gameplayfix_grenadebouncedownslopes 0\n"
649 "sv_gameplayfix_slidemoveprojectiles 0\n"
650 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
651 "sv_gameplayfix_setmodelrealbox 0\n"
652 "sv_gameplayfix_droptofloorstartsolid 0\n"
653 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
654 "sv_gameplayfix_noairborncorpse 0\n"
655 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
656 "sv_gameplayfix_easierwaterjump 0\n"
657 "sv_gameplayfix_delayprojectiles 0\n"
658 "sv_gameplayfix_multiplethinksperframe 0\n"
659 "sv_gameplayfix_fixedcheckwatertransition 0\n"
660 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
661 "sv_gameplayfix_swiminbmodels 0\n"
662 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
663 "sys_ticrate 0.01388889\n"
665 "r_shadow_bumpscale_basetexture 0\n"
666 "csqc_polygons_defaultmaterial_nocullface 0\n"
670 Cbuf_InsertText(cmd, "\n"
671 "sv_gameplayfix_blowupfallenzombies 0\n"
672 "sv_gameplayfix_findradiusdistancetobox 0\n"
673 "sv_gameplayfix_grenadebouncedownslopes 0\n"
674 "sv_gameplayfix_slidemoveprojectiles 0\n"
675 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
676 "sv_gameplayfix_setmodelrealbox 0\n"
677 "sv_gameplayfix_droptofloorstartsolid 0\n"
678 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
679 "sv_gameplayfix_noairborncorpse 0\n"
680 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
681 "sv_gameplayfix_easierwaterjump 0\n"
682 "sv_gameplayfix_delayprojectiles 0\n"
683 "sv_gameplayfix_multiplethinksperframe 0\n"
684 "sv_gameplayfix_fixedcheckwatertransition 0\n"
685 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
686 "sv_gameplayfix_swiminbmodels 0\n"
687 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
688 "sys_ticrate 0.01388889\n"
690 "r_shadow_bumpscale_basetexture 0\n"
691 "csqc_polygons_defaultmaterial_nocullface 0\n"
694 // hipnotic mission pack has issues in their 'friendly monster' ai, which seem to attempt to attack themselves for some reason when findradius() returns non-solid entities.
695 // hipnotic mission pack has issues with bobbing water entities 'jittering' between different heights on alternate frames at the default 0.0138889 ticrate, 0.02 avoids this issue
696 // hipnotic mission pack has issues in their proximity mine sticking code, which causes them to bounce off.
699 Cbuf_InsertText(cmd, "\n"
700 "sv_gameplayfix_blowupfallenzombies 0\n"
701 "sv_gameplayfix_findradiusdistancetobox 0\n"
702 "sv_gameplayfix_grenadebouncedownslopes 0\n"
703 "sv_gameplayfix_slidemoveprojectiles 0\n"
704 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
705 "sv_gameplayfix_setmodelrealbox 0\n"
706 "sv_gameplayfix_droptofloorstartsolid 0\n"
707 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
708 "sv_gameplayfix_noairborncorpse 0\n"
709 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
710 "sv_gameplayfix_easierwaterjump 0\n"
711 "sv_gameplayfix_delayprojectiles 0\n"
712 "sv_gameplayfix_multiplethinksperframe 0\n"
713 "sv_gameplayfix_fixedcheckwatertransition 0\n"
714 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
715 "sv_gameplayfix_swiminbmodels 0\n"
716 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
719 "r_shadow_bumpscale_basetexture 0\n"
720 "csqc_polygons_defaultmaterial_nocullface 0\n"
723 // rogue mission pack has a guardian boss that does not wake up if findradius returns one of the entities around its spawn area
725 Cbuf_InsertText(cmd, "\n"
726 "sv_gameplayfix_blowupfallenzombies 0\n"
727 "sv_gameplayfix_findradiusdistancetobox 0\n"
728 "sv_gameplayfix_grenadebouncedownslopes 0\n"
729 "sv_gameplayfix_slidemoveprojectiles 0\n"
730 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
731 "sv_gameplayfix_setmodelrealbox 0\n"
732 "sv_gameplayfix_droptofloorstartsolid 0\n"
733 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
734 "sv_gameplayfix_noairborncorpse 0\n"
735 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
736 "sv_gameplayfix_easierwaterjump 0\n"
737 "sv_gameplayfix_delayprojectiles 0\n"
738 "sv_gameplayfix_multiplethinksperframe 0\n"
739 "sv_gameplayfix_fixedcheckwatertransition 0\n"
740 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
741 "sv_gameplayfix_swiminbmodels 0\n"
742 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
743 "sys_ticrate 0.01388889\n"
745 "r_shadow_bumpscale_basetexture 0\n"
746 "csqc_polygons_defaultmaterial_nocullface 0\n"
750 Cbuf_InsertText(cmd, "\n"
751 "sv_gameplayfix_blowupfallenzombies 0\n"
752 "sv_gameplayfix_findradiusdistancetobox 0\n"
753 "sv_gameplayfix_grenadebouncedownslopes 0\n"
754 "sv_gameplayfix_slidemoveprojectiles 0\n"
755 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
756 "sv_gameplayfix_setmodelrealbox 0\n"
757 "sv_gameplayfix_droptofloorstartsolid 0\n"
758 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
759 "sv_gameplayfix_noairborncorpse 0\n"
760 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
761 "sv_gameplayfix_easierwaterjump 0\n"
762 "sv_gameplayfix_delayprojectiles 0\n"
763 "sv_gameplayfix_multiplethinksperframe 0\n"
764 "sv_gameplayfix_fixedcheckwatertransition 0\n"
765 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
766 "sv_gameplayfix_swiminbmodels 0\n"
767 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
768 "sys_ticrate 0.01388889\n"
770 "r_shadow_bumpscale_basetexture 4\n"
771 "csqc_polygons_defaultmaterial_nocullface 0\n"
775 Cbuf_InsertText(cmd, "\n"
776 "sv_gameplayfix_blowupfallenzombies 1\n"
777 "sv_gameplayfix_findradiusdistancetobox 1\n"
778 "sv_gameplayfix_grenadebouncedownslopes 1\n"
779 "sv_gameplayfix_slidemoveprojectiles 1\n"
780 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
781 "sv_gameplayfix_setmodelrealbox 1\n"
782 "sv_gameplayfix_droptofloorstartsolid 1\n"
783 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
784 "sv_gameplayfix_noairborncorpse 1\n"
785 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
786 "sv_gameplayfix_easierwaterjump 1\n"
787 "sv_gameplayfix_delayprojectiles 1\n"
788 "sv_gameplayfix_multiplethinksperframe 1\n"
789 "sv_gameplayfix_fixedcheckwatertransition 1\n"
790 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
791 "sv_gameplayfix_swiminbmodels 1\n"
792 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
793 "sys_ticrate 0.01388889\n"
794 "sv_gameplayfix_q2airaccelerate 1\n"
795 "sv_gameplayfix_stepmultipletimes 1\n"
796 "csqc_polygons_defaultmaterial_nocullface 1\n"
797 "con_chatsound_team_mask 13\n"
801 case GAME_VORETOURNAMENT:
802 // compatibility for versions prior to 2020-05-25, this can be overridden in newer versions to get the default behavior and be consistent with FTEQW engine
803 Cbuf_InsertText(cmd, "\n"
804 "csqc_polygons_defaultmaterial_nocullface 1\n"
805 "con_chatsound_team_mask 13\n"
806 "sv_gameplayfix_customstats 1\n"
809 // Steel Storm: Burning Retribution csqc misinterprets CSQC_InputEvent if type is a value other than 0 or 1
810 case GAME_STEELSTORM:
811 Cbuf_InsertText(cmd, "\n"
812 "sv_gameplayfix_blowupfallenzombies 1\n"
813 "sv_gameplayfix_findradiusdistancetobox 1\n"
814 "sv_gameplayfix_grenadebouncedownslopes 1\n"
815 "sv_gameplayfix_slidemoveprojectiles 1\n"
816 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
817 "sv_gameplayfix_setmodelrealbox 1\n"
818 "sv_gameplayfix_droptofloorstartsolid 1\n"
819 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
820 "sv_gameplayfix_noairborncorpse 1\n"
821 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
822 "sv_gameplayfix_easierwaterjump 1\n"
823 "sv_gameplayfix_delayprojectiles 1\n"
824 "sv_gameplayfix_multiplethinksperframe 1\n"
825 "sv_gameplayfix_fixedcheckwatertransition 1\n"
826 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
827 "sv_gameplayfix_swiminbmodels 1\n"
828 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
829 "sys_ticrate 0.01388889\n"
830 "cl_csqc_generatemousemoveevents 0\n"
831 "csqc_polygons_defaultmaterial_nocullface 1\n"
835 Cbuf_InsertText(cmd, "\n"
836 "sv_gameplayfix_blowupfallenzombies 1\n"
837 "sv_gameplayfix_findradiusdistancetobox 1\n"
838 "sv_gameplayfix_grenadebouncedownslopes 1\n"
839 "sv_gameplayfix_slidemoveprojectiles 1\n"
840 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
841 "sv_gameplayfix_setmodelrealbox 1\n"
842 "sv_gameplayfix_droptofloorstartsolid 1\n"
843 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
844 "sv_gameplayfix_noairborncorpse 1\n"
845 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
846 "sv_gameplayfix_easierwaterjump 1\n"
847 "sv_gameplayfix_delayprojectiles 1\n"
848 "sv_gameplayfix_multiplethinksperframe 1\n"
849 "sv_gameplayfix_fixedcheckwatertransition 1\n"
850 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
851 "sv_gameplayfix_swiminbmodels 1\n"
852 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
853 "sys_ticrate 0.01388889\n"
854 "csqc_polygons_defaultmaterial_nocullface 0\n"
866 static void Cmd_Exec_f (cmd_state_t *cmd)
871 if (Cmd_Argc(cmd) != 2)
873 Con_Print("exec <filename> : execute a script file\n");
877 s = FS_Search(Cmd_Argv(cmd, 1), true, true, NULL);
878 if(!s || !s->numfilenames)
880 Con_Printf("couldn't exec %s\n",Cmd_Argv(cmd, 1));
884 for(i = 0; i < s->numfilenames; ++i)
885 Cmd_Exec(cmd, s->filenames[i]);
895 Just prints the rest of the line to the console
898 static void Cmd_Echo_f (cmd_state_t *cmd)
902 for (i=1 ; i<Cmd_Argc(cmd) ; i++)
903 Con_Printf("%s ",Cmd_Argv(cmd, i));
908 // Support Doom3-style Toggle Console Command
913 Toggles a specified console variable amongst the values specified (default is 0 and 1)
916 static void Cmd_Toggle_f(cmd_state_t *cmd)
918 // Acquire Number of Arguments
919 int nNumArgs = Cmd_Argc(cmd);
922 // No Arguments Specified; Print Usage
923 Con_Print("Toggle Console Variable - Usage\n toggle <variable> - toggles between 0 and 1\n toggle <variable> <value> - toggles between 0 and <value>\n toggle <variable> [string 1] [string 2]...[string n] - cycles through all strings\n");
925 { // Correct Arguments Specified
926 // Acquire Potential CVar
927 cvar_t* cvCVar = Cvar_FindVar(cmd->cvars, Cmd_Argv(cmd, 1), cmd->cvars_flagsmask);
934 Cvar_SetValueQuick(cvCVar, 0);
936 Cvar_SetValueQuick(cvCVar, 1);
940 { // 0 and Specified Usage
941 if(cvCVar->integer == atoi(Cmd_Argv(cmd, 2) ) )
942 // CVar is Specified Value; // Reset to 0
943 Cvar_SetValueQuick(cvCVar, 0);
945 if(cvCVar->integer == 0)
946 // CVar is 0; Specify Value
947 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
949 // CVar does not match; Reset to 0
950 Cvar_SetValueQuick(cvCVar, 0);
953 { // Variable Values Specified
957 for(nCnt = 2; nCnt < nNumArgs; nCnt++)
958 { // Cycle through Values
959 if( strcmp(cvCVar->string, Cmd_Argv(cmd, nCnt) ) == 0)
960 { // Current Value Located; Increment to Next
961 if( (nCnt + 1) == nNumArgs)
962 // Max Value Reached; Reset
963 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
966 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, nCnt + 1) );
975 // Value not Found; Reset to Original
976 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
982 Con_Printf("ERROR : CVar '%s' not found\n", Cmd_Argv(cmd, 1) );
991 Creates a new command that executes a command string (possibly ; seperated)
994 static void Cmd_Alias_f (cmd_state_t *cmd)
997 char line[MAX_INPUTLINE];
1002 if (Cmd_Argc(cmd) == 1)
1004 Con_Print("Current alias commands:\n");
1005 for (a = cmd->userdefined->alias ; a ; a=a->next)
1006 Con_Printf("%s : %s", a->name, a->value);
1010 s = Cmd_Argv(cmd, 1);
1011 if (strlen(s) >= MAX_ALIAS_NAME)
1013 Con_Print("Alias name is too long\n");
1017 // if the alias already exists, reuse it
1018 for (a = cmd->userdefined->alias ; a ; a=a->next)
1020 if (!strcmp(s, a->name))
1029 cmd_alias_t *prev, *current;
1031 a = (cmd_alias_t *)Z_Malloc (sizeof(cmd_alias_t));
1032 strlcpy (a->name, s, sizeof (a->name));
1033 // insert it at the right alphanumeric position
1034 for( prev = NULL, current = cmd->userdefined->alias ; current && strcmp( current->name, a->name ) < 0 ; prev = current, current = current->next )
1039 cmd->userdefined->alias = a;
1045 // copy the rest of the command line
1046 line[0] = 0; // start out with a null string
1048 for (i=2 ; i < c ; i++)
1051 strlcat (line, " ", sizeof (line));
1052 strlcat (line, Cmd_Argv(cmd, i), sizeof (line));
1054 strlcat (line, "\n", sizeof (line));
1056 alloclen = strlen (line) + 1;
1058 line[alloclen - 2] = '\n'; // to make sure a newline is appended even if too long
1059 a->value = (char *)Z_Malloc (alloclen);
1060 memcpy (a->value, line, alloclen);
1067 Remove existing aliases.
1070 static void Cmd_UnAlias_f (cmd_state_t *cmd)
1076 if(Cmd_Argc(cmd) == 1)
1078 Con_Print("unalias: Usage: unalias alias1 [alias2 ...]\n");
1082 for(i = 1; i < Cmd_Argc(cmd); ++i)
1084 s = Cmd_Argv(cmd, i);
1086 for(a = cmd->userdefined->alias; a; p = a, a = a->next)
1088 if(!strcmp(s, a->name))
1090 if (a->initstate) // we can not remove init aliases
1092 if(a == cmd->userdefined->alias)
1093 cmd->userdefined->alias = a->next;
1102 Con_Printf("unalias: %s alias not found\n", s);
1107 =============================================================================
1111 =============================================================================
1114 static const char *Cmd_GetDirectCvarValue(cmd_state_t *cmd, const char *varname, cmd_alias_t *alias, qbool *is_multiple)
1119 static char vabuf[1024]; // cmd_mutex
1122 *is_multiple = false;
1124 if(!varname || !*varname)
1129 if(!strcmp(varname, "*"))
1132 *is_multiple = true;
1133 return Cmd_Args(cmd);
1135 else if(!strcmp(varname, "#"))
1137 return va(vabuf, sizeof(vabuf), "%d", Cmd_Argc(cmd));
1139 else if(varname[strlen(varname) - 1] == '-')
1141 argno = strtol(varname, &endptr, 10);
1142 if(endptr == varname + strlen(varname) - 1)
1144 // whole string is a number, apart from the -
1145 const char *p = Cmd_Args(cmd);
1146 for(; argno > 1; --argno)
1147 if(!COM_ParseToken_Console(&p))
1152 *is_multiple = true;
1154 // kill pre-argument whitespace
1155 for (;*p && ISWHITESPACE(*p);p++)
1164 argno = strtol(varname, &endptr, 10);
1167 // whole string is a number
1168 // NOTE: we already made sure we don't have an empty cvar name!
1169 if(argno >= 0 && argno < Cmd_Argc(cmd))
1170 return Cmd_Argv(cmd, argno);
1175 if((cvar = Cvar_FindVar(cmd->cvars, varname, cmd->cvars_flagsmask)) && !(cvar->flags & CF_PRIVATE))
1176 return cvar->string;
1181 qbool Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset, qbool putquotes)
1183 qbool quote_quot = !!strchr(quoteset, '"');
1184 qbool quote_backslash = !!strchr(quoteset, '\\');
1185 qbool quote_dollar = !!strchr(quoteset, '$');
1194 *out++ = '"'; --outlen;
1200 if(*in == '"' && quote_quot)
1204 *out++ = '\\'; --outlen;
1205 *out++ = '"'; --outlen;
1207 else if(*in == '\\' && quote_backslash)
1211 *out++ = '\\'; --outlen;
1212 *out++ = '\\'; --outlen;
1214 else if(*in == '$' && quote_dollar)
1218 *out++ = '$'; --outlen;
1219 *out++ = '$'; --outlen;
1225 *out++ = *in; --outlen;
1240 static const char *Cmd_GetCvarValue(cmd_state_t *cmd, const char *var, size_t varlen, cmd_alias_t *alias)
1242 static char varname[MAX_INPUTLINE]; // cmd_mutex
1243 static char varval[MAX_INPUTLINE]; // cmd_mutex
1244 const char *varstr = NULL;
1246 qbool required = false;
1247 qbool optional = false;
1248 static char asis[] = "asis"; // just to suppress const char warnings
1250 if(varlen >= MAX_INPUTLINE)
1251 varlen = MAX_INPUTLINE - 1;
1252 memcpy(varname, var, varlen);
1253 varname[varlen] = 0;
1254 varfunc = strchr(varname, ' ');
1266 Con_Printf(CON_WARN "Warning: Could not expand $ in alias %s\n", alias->name);
1268 Con_Printf(CON_WARN "Warning: Could not expand $\n");
1276 while((p = strchr(varfunc, '?')))
1279 memmove(p, p+1, strlen(p)); // with final NUL
1282 while((p = strchr(varfunc, '!')))
1285 memmove(p, p+1, strlen(p)); // with final NUL
1288 while((p = strchr(varfunc, ' ')))
1290 memmove(p, p+1, strlen(p)); // with final NUL
1292 // if no function is left, NULL it
1297 if(varname[0] == '$')
1298 varstr = Cmd_GetDirectCvarValue(cmd, Cmd_GetDirectCvarValue(cmd, varname + 1, alias, NULL), alias, NULL);
1301 qbool is_multiple = false;
1302 // Exception: $* and $n- don't use the quoted form by default
1303 varstr = Cmd_GetDirectCvarValue(cmd, varname, alias, &is_multiple);
1314 Con_Printf(CON_ERROR "Error: Could not expand $%s in alias %s\n", varname, alias->name);
1316 Con_Printf(CON_ERROR "Error: Could not expand $%s\n", varname);
1326 Con_Printf(CON_WARN "Warning: Could not expand $%s in alias %s\n", varname, alias->name);
1328 Con_Printf(CON_WARN "Warning: Could not expand $%s\n", varname);
1329 dpsnprintf(varval, sizeof(varval), "$%s", varname);
1334 if(!varfunc || !strcmp(varfunc, "q")) // note: quoted form is default, use "asis" to override!
1336 // quote it so it can be used inside double quotes
1337 // we just need to replace " by \", and of course, double backslashes
1338 Cmd_QuoteString(varval, sizeof(varval), varstr, "\"\\", false);
1341 else if(!strcmp(varfunc, "asis"))
1346 Con_Printf("Unknown variable function %s\n", varfunc);
1352 Cmd_PreprocessString
1354 Preprocesses strings and replaces $*, $param#, $cvar accordingly. Also strips comments.
1356 static qbool Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmd_alias_t *alias ) {
1362 // don't crash if there's no room in the outtext buffer
1363 if( maxoutlen == 0 ) {
1366 maxoutlen--; // because of \0
1371 while( *in && outlen < maxoutlen ) {
1373 // this is some kind of expansion, see what comes after the $
1376 // The console does the following preprocessing:
1378 // - $$ is transformed to a single dollar sign.
1379 // - $var or ${var} are expanded to the contents of the named cvar,
1380 // with quotation marks and backslashes quoted so it can safely
1381 // be used inside quotation marks (and it should always be used
1383 // - ${var asis} inserts the cvar value as is, without doing this
1385 // - ${var ?} silently expands to the empty string if
1386 // $var does not exist
1387 // - ${var !} fails expansion and executes nothing if
1388 // $var does not exist
1389 // - prefix the cvar name with a dollar sign to do indirection;
1390 // for example, if $x has the value timelimit, ${$x} will return
1391 // the value of $timelimit
1392 // - when expanding an alias, the special variable name $* refers
1393 // to all alias parameters, and a number refers to that numbered
1394 // alias parameter, where the name of the alias is $0, the first
1395 // parameter is $1 and so on; as a special case, $* inserts all
1396 // parameters, without extra quoting, so one can use $* to just
1397 // pass all parameters around. All parameters starting from $n
1398 // can be referred to as $n- (so $* is equivalent to $1-).
1399 // - ${* q} and ${n- q} force quoting anyway
1401 // Note: when expanding an alias, cvar expansion is done in the SAME step
1402 // as alias expansion so that alias parameters or cvar values containing
1403 // dollar signs have no unwanted bad side effects. However, this needs to
1404 // be accounted for when writing complex aliases. For example,
1405 // alias foo "set x NEW; echo $x"
1406 // actually expands to
1407 // "set x NEW; echo OLD"
1408 // and will print OLD! To work around this, use a second alias:
1409 // alias foo "set x NEW; foo2"
1410 // alias foo2 "echo $x"
1412 // Also note: lines starting with alias are exempt from cvar expansion.
1413 // If you want cvar expansion, write "alias" instead:
1416 // alias foo "echo $x"
1417 // "alias" bar "echo $x"
1420 // foo will print 2, because the variable $x will be expanded when the alias
1421 // gets expanded. bar will print 1, because the variable $x was expanded
1422 // at definition time. foo can be equivalently defined as
1424 // "alias" foo "echo $$x"
1426 // because at definition time, $$ will get replaced to a single $.
1431 } else if(*in == '{') {
1432 varlen = strcspn(in + 1, "}");
1433 if(in[varlen + 1] == '}')
1435 val = Cmd_GetCvarValue(cmd, in + 1, varlen, alias);
1447 varlen = strspn(in, "#*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-");
1448 val = Cmd_GetCvarValue(cmd, in, varlen, alias);
1455 // insert the cvar value
1456 while(*val && outlen < maxoutlen)
1457 outtext[outlen++] = *val++;
1462 // copy the unexpanded text
1463 outtext[outlen++] = '$';
1464 while(eat && outlen < maxoutlen)
1466 outtext[outlen++] = *in++;
1472 outtext[outlen++] = *in++;
1474 outtext[outlen] = 0;
1482 Called for aliases and fills in the alias into the cbuffer
1485 static void Cmd_ExecuteAlias (cmd_state_t *cmd, cmd_alias_t *alias)
1487 static char buffer[ MAX_INPUTLINE ]; // cmd_mutex
1488 static char buffer2[ MAX_INPUTLINE ]; // cmd_mutex
1489 qbool ret = Cmd_PreprocessString( cmd, alias->value, buffer, sizeof(buffer) - 2, alias );
1492 // insert at start of command buffer, so that aliases execute in order
1493 // (fixes bug introduced by Black on 20050705)
1495 // Note: Cbuf_PreprocessString will be called on this string AGAIN! So we
1496 // have to make sure that no second variable expansion takes place, otherwise
1497 // alias parameters containing dollar signs can have bad effects.
1498 Cmd_QuoteString(buffer2, sizeof(buffer2), buffer, "$", false);
1499 Cbuf_InsertText(cmd, buffer2);
1506 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
1507 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
1511 static void Cmd_List_f (cmd_state_t *cmd)
1513 cmd_function_t *func;
1514 const char *partial;
1519 if (Cmd_Argc(cmd) > 1)
1521 partial = Cmd_Argv(cmd, 1);
1522 len = strlen(partial);
1523 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1533 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1535 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1537 Con_Printf("%s : %s\n", func->name, func->description);
1540 for (func = cmd->engine_functions; func; func = func->next)
1542 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1544 Con_Printf("%s : %s\n", func->name, func->description);
1551 Con_Printf("%i Command%s matching \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1553 Con_Printf("%i Command%s beginning with \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1556 Con_Printf("%i Command%s\n\n", count, (count > 1) ? "s" : "");
1559 static void Cmd_Apropos_f(cmd_state_t *cmd)
1561 cmd_function_t *func;
1564 const char *partial;
1569 if (Cmd_Argc(cmd) > 1)
1570 partial = Cmd_Args(cmd);
1573 Con_Printf("usage: %s <string>\n",Cmd_Argv(cmd, 0));
1577 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
1579 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
1582 for (cvar = cmd->cvars->vars; cvar; cvar = cvar->next)
1584 if (matchpattern_with_separator(cvar->name, partial, true, "", false) ||
1585 matchpattern_with_separator(cvar->description, partial, true, "", false))
1587 Con_Printf ("cvar ");
1588 Cvar_PrintHelp(cvar, cvar->name, true);
1591 for (char **cvar_alias = cvar->aliases; cvar_alias && *cvar_alias; cvar_alias++)
1593 if (matchpattern_with_separator(*cvar_alias, partial, true, "", false))
1595 Con_Printf ("cvar ");
1596 Cvar_PrintHelp(cvar, *cvar_alias, true);
1601 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1603 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1604 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1606 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1609 for (func = cmd->engine_functions; func; func = func->next)
1611 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1612 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1614 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1617 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
1619 // procede here a bit differently as an alias value always got a final \n
1620 if (!matchpattern_with_separator(alias->name, partial, true, "", false))
1621 if (!matchpattern_with_separator(alias->value, partial, true, "\n", false)) // when \n is as separator wildcards don't match it
1623 Con_Printf("alias ^5%s^7: %s", alias->name, alias->value); // do not print an extra \n
1626 Con_Printf("%i result%s\n\n", count, (count > 1) ? "s" : "");
1629 static cmd_state_t *Cmd_AddInterpreter(cmd_buf_t *cbuf, cvar_state_t *cvars, int cvars_flagsmask, int cmds_flagsmask, cmd_userdefined_t *userdefined)
1631 cmd_state_t *cmd = (cmd_state_t *)Mem_Alloc(tempmempool, sizeof(cmd_state_t));
1633 cmd->mempool = Mem_AllocPool("commands", 0, NULL);
1634 // space for commands and script files
1636 cmd->null_string = "";
1639 cmd->cvars_flagsmask = cvars_flagsmask;
1640 cmd->cmd_flags = cmds_flagsmask;
1641 cmd->userdefined = userdefined;
1654 cbuf_mempool = Mem_AllocPool("Command buffer", 0, NULL);
1655 cbuf = (cmd_buf_t *)Mem_Alloc(cbuf_mempool, sizeof(cmd_buf_t));
1656 cbuf->maxsize = 655360;
1657 cbuf->lock = Thread_CreateMutex();
1661 cbuf->start.prev = cbuf->start.next = &(cbuf->start);
1662 cbuf->deferred.prev = cbuf->deferred.next = &(cbuf->deferred);
1663 cbuf->free.prev = cbuf->free.next = &(cbuf->free);
1665 // FIXME: Get rid of cmd_iter_all eventually. This is just a hack to reduce the amount of work to make the interpreters dynamic.
1666 cmd_iter_all = (cmd_iter_t *)Mem_Alloc(tempmempool, sizeof(cmd_iter_t) * 3);
1669 cmd_iter_all[0].cmd = cmd_local = Cmd_AddInterpreter(cbuf, &cvars_all, CF_CLIENT | CF_SERVER, CF_CLIENT | CF_CLIENT_FROM_SERVER | CF_SERVER_FROM_CLIENT, &cmd_userdefined_all);
1670 cmd_local->Handle = Cmd_CL_Callback;
1671 cmd_local->NotFound = NULL;
1673 // server commands received from clients have no reason to access cvars, cvar expansion seems perilous.
1674 cmd_iter_all[1].cmd = cmd_serverfromclient = Cmd_AddInterpreter(cbuf, &cvars_null, 0, CF_SERVER_FROM_CLIENT | CF_USERINFO, &cmd_userdefined_null);
1675 cmd_serverfromclient->Handle = Cmd_SV_Callback;
1676 cmd_serverfromclient->NotFound = Cmd_SV_NotFound;
1678 cmd_iter_all[2].cmd = NULL;
1680 // register our commands
1682 // client-only commands
1683 Cmd_AddCommand(CF_SHARED, "wait", Cmd_Wait_f, "make script execution wait for next rendered frame");
1684 Cmd_AddCommand(CF_CLIENT, "cprint", Cmd_Centerprint_f, "print something at the screen center");
1686 // maintenance commands used for upkeep of cvars and saved configs
1687 Cmd_AddCommand(CF_SHARED, "stuffcmds", Cmd_StuffCmds_f, "execute commandline parameters (must be present in quake.rc script)");
1688 Cmd_AddCommand(CF_SHARED, "cvar_lockdefaults", Cvar_LockDefaults_f, "stores the current values of all cvars into their default values, only used once during startup after parsing default.cfg");
1689 Cmd_AddCommand(CF_SHARED, "cvar_resettodefaults_all", Cvar_ResetToDefaults_All_f, "sets all cvars to their locked default values");
1690 Cmd_AddCommand(CF_SHARED, "cvar_resettodefaults_nosaveonly", Cvar_ResetToDefaults_NoSaveOnly_f, "sets all non-saved cvars to their locked default values (variables that will not be saved to config.cfg)");
1691 Cmd_AddCommand(CF_SHARED, "cvar_resettodefaults_saveonly", Cvar_ResetToDefaults_SaveOnly_f, "sets all saved cvars to their locked default values (variables that will be saved to config.cfg)");
1693 // general console commands used in multiple environments
1694 Cmd_AddCommand(CF_SHARED, "exec", Cmd_Exec_f, "execute a script file");
1695 Cmd_AddCommand(CF_SHARED, "echo",Cmd_Echo_f, "print a message to the console (useful in scripts)");
1696 Cmd_AddCommand(CF_SHARED, "alias",Cmd_Alias_f, "create a script function (parameters are passed in as $X (being X a number), $* for all parameters, $X- for all parameters starting from $X). Without arguments show the list of all alias");
1697 Cmd_AddCommand(CF_SHARED, "unalias",Cmd_UnAlias_f, "remove an alias");
1698 Cmd_AddCommand(CF_SHARED, "set", Cvar_Set_f, "create or change the value of a console variable");
1699 Cmd_AddCommand(CF_SHARED, "seta", Cvar_SetA_f, "create or change the value of a console variable that will be saved to config.cfg");
1700 Cmd_AddCommand(CF_SHARED, "unset", Cvar_Del_f, "delete a cvar (does not work for static ones like _cl_name, or read-only ones)");
1702 #ifdef FILLALLCVARSWITHRUBBISH
1703 Cmd_AddCommand(CF_SHARED, "fillallcvarswithrubbish", Cvar_FillAll_f, "fill all cvars with a specified number of characters to provoke buffer overruns");
1704 #endif /* FILLALLCVARSWITHRUBBISH */
1706 // 2000-01-09 CmdList, CvarList commands By Matthias "Maddes" Buecher
1707 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
1708 Cmd_AddCommand(CF_SHARED, "cmdlist", Cmd_List_f, "lists all console commands beginning with the specified prefix or matching the specified wildcard pattern");
1709 Cmd_AddCommand(CF_SHARED, "cvarlist", Cvar_List_f, "lists all console variables beginning with the specified prefix or matching the specified wildcard pattern");
1710 Cmd_AddCommand(CF_SHARED, "apropos", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1711 Cmd_AddCommand(CF_SHARED, "find", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1713 Cmd_AddCommand(CF_SHARED, "defer", Cmd_Defer_f, "execute a command in the future");
1716 // Support Doom3-style Toggle Command
1717 Cmd_AddCommand(CF_SHARED | CF_CLIENT_FROM_SERVER, "toggle", Cmd_Toggle_f, "toggles a console variable's values (use for more info)");
1725 void Cmd_Shutdown(void)
1727 cmd_iter_t *cmd_iter;
1728 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
1730 cmd_state_t *cmd = cmd_iter->cmd;
1732 if (cmd->cbuf->lock)
1734 // we usually have this locked when we get here from Host_Quit_f
1735 Cbuf_Unlock(cmd->cbuf);
1738 Mem_FreePool(&cmd->mempool);
1747 int Cmd_Argc (cmd_state_t *cmd)
1757 const char *Cmd_Argv(cmd_state_t *cmd, int arg)
1759 if (arg >= cmd->argc )
1760 return cmd->null_string;
1761 return cmd->argv[arg];
1769 const char *Cmd_Args (cmd_state_t *cmd)
1778 Parses the given string into command line tokens.
1781 // AK: This function should only be called from ExcuteString because the current design is a bit of an hack
1782 static void Cmd_TokenizeString (cmd_state_t *cmd, const char *text)
1788 cmd->cmdline = NULL;
1792 // skip whitespace up to a /n
1793 while (*text && ISWHITESPACE(*text) && *text != '\r' && *text != '\n')
1800 if (*text == '\n' || *text == '\r')
1802 // a newline separates commands in the buffer
1803 if (*text == '\r' && text[1] == '\n')
1813 cmd->cmdline = text;
1817 if (!COM_ParseToken_Console(&text))
1820 if (cmd->argc < MAX_ARGS)
1822 l = (int)strlen(com_token) + 1;
1823 if (cmd->cbuf->tokenizebufferpos + l > CMD_TOKENIZELENGTH)
1825 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguments\n", CMD_TOKENIZELENGTH);
1828 memcpy (cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos, com_token, l);
1829 cmd->argv[cmd->argc] = cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos;
1830 cmd->cbuf->tokenizebufferpos += l;
1842 void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const char *description)
1844 cmd_function_t *func;
1845 cmd_function_t *prev, *current;
1849 for (i = 0; i < 2; i++)
1851 cmd = cmd_iter_all[i].cmd;
1852 if (flags & cmd->cmd_flags)
1854 // fail if the command is a variable name
1855 if (Cvar_FindVar(cmd->cvars, cmd_name, ~0))
1857 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
1863 // fail if the command already exists in this interpreter
1864 for (func = cmd->engine_functions; func; func = func->next)
1866 if (!strcmp(cmd_name, func->name))
1868 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
1873 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1874 func->flags = flags;
1875 func->name = cmd_name;
1876 func->function = function;
1877 func->description = description;
1878 func->next = cmd->engine_functions;
1880 // insert it at the right alphanumeric position
1881 for (prev = NULL, current = cmd->engine_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1887 cmd->engine_functions = func;
1889 func->next = current;
1893 // mark qcfunc if the function already exists in the qc_functions list
1894 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1896 if (!strcmp(cmd_name, func->name))
1898 func->qcfunc = true; //[515]: csqc
1904 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1905 func->flags = flags;
1906 func->name = cmd_name;
1907 func->function = function;
1908 func->description = description;
1909 func->qcfunc = true; //[515]: csqc
1910 func->next = cmd->userdefined->qc_functions;
1912 // insert it at the right alphanumeric position
1913 for (prev = NULL, current = cmd->userdefined->qc_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1919 cmd->userdefined->qc_functions = func;
1921 func->next = current;
1932 qbool Cmd_Exists (cmd_state_t *cmd, const char *cmd_name)
1934 cmd_function_t *func;
1936 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1937 if (!strcmp(cmd_name, func->name))
1940 for (func=cmd->engine_functions ; func ; func=func->next)
1941 if (!strcmp (cmd_name,func->name))
1953 const char *Cmd_CompleteCommand (cmd_state_t *cmd, const char *partial)
1955 cmd_function_t *func;
1958 len = strlen(partial);
1964 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1965 if (!strncasecmp(partial, func->name, len))
1968 for (func = cmd->engine_functions; func; func = func->next)
1969 if (!strncasecmp(partial, func->name, len))
1976 Cmd_CompleteCountPossible
1978 New function for tab-completion system
1979 Added by EvilTypeGuy
1980 Thanks to Fett erich@heintz.com
1984 int Cmd_CompleteCountPossible (cmd_state_t *cmd, const char *partial)
1986 cmd_function_t *func;
1991 len = strlen(partial);
1996 // Loop through the command list and count all partial matches
1997 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1998 if (!strncasecmp(partial, func->name, len))
2001 for (func = cmd->engine_functions; func; func = func->next)
2002 if (!strncasecmp(partial, func->name, len))
2009 Cmd_CompleteBuildList
2011 New function for tab-completion system
2012 Added by EvilTypeGuy
2013 Thanks to Fett erich@heintz.com
2017 const char **Cmd_CompleteBuildList (cmd_state_t *cmd, const char *partial)
2019 cmd_function_t *func;
2022 size_t sizeofbuf = (Cmd_CompleteCountPossible (cmd, partial) + 1) * sizeof (const char *);
2025 len = strlen(partial);
2026 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
2027 // Loop through the functions lists and print all matches
2028 for (func = cmd->userdefined->qc_functions; func; func = func->next)
2029 if (!strncasecmp(partial, func->name, len))
2030 buf[bpos++] = func->name;
2031 for (func = cmd->engine_functions; func; func = func->next)
2032 if (!strncasecmp(partial, func->name, len))
2033 buf[bpos++] = func->name;
2039 // written by LadyHavoc
2040 void Cmd_CompleteCommandPrint (cmd_state_t *cmd, const char *partial)
2042 cmd_function_t *func;
2043 size_t len = strlen(partial);
2044 // Loop through the command list and print all matches
2045 for (func = cmd->userdefined->qc_functions; func; func = func->next)
2046 if (!strncasecmp(partial, func->name, len))
2047 Con_Printf("^2%s^7: %s\n", func->name, func->description);
2048 for (func = cmd->engine_functions; func; func = func->next)
2049 if (!strncasecmp(partial, func->name, len))
2050 Con_Printf("^2%s^7: %s\n", func->name, func->description);
2056 New function for tab-completion system
2057 Added by EvilTypeGuy
2058 Thanks to Fett erich@heintz.com
2062 const char *Cmd_CompleteAlias (cmd_state_t *cmd, const char *partial)
2067 len = strlen(partial);
2073 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2074 if (!strncasecmp(partial, alias->name, len))
2080 // written by LadyHavoc
2081 void Cmd_CompleteAliasPrint (cmd_state_t *cmd, const char *partial)
2084 size_t len = strlen(partial);
2085 // Loop through the alias list and print all matches
2086 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2087 if (!strncasecmp(partial, alias->name, len))
2088 Con_Printf("^5%s^7: %s", alias->name, alias->value);
2093 Cmd_CompleteAliasCountPossible
2095 New function for tab-completion system
2096 Added by EvilTypeGuy
2097 Thanks to Fett erich@heintz.com
2101 int Cmd_CompleteAliasCountPossible (cmd_state_t *cmd, const char *partial)
2109 len = strlen(partial);
2114 // Loop through the command list and count all partial matches
2115 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2116 if (!strncasecmp(partial, alias->name, len))
2123 Cmd_CompleteAliasBuildList
2125 New function for tab-completion system
2126 Added by EvilTypeGuy
2127 Thanks to Fett erich@heintz.com
2131 const char **Cmd_CompleteAliasBuildList (cmd_state_t *cmd, const char *partial)
2136 size_t sizeofbuf = (Cmd_CompleteAliasCountPossible (cmd, partial) + 1) * sizeof (const char *);
2139 len = strlen(partial);
2140 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
2141 // Loop through the alias list and print all matches
2142 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2143 if (!strncasecmp(partial, alias->name, len))
2144 buf[bpos++] = alias->name;
2150 // TODO: Make this more generic?
2151 void Cmd_ClearCSQCCommands (cmd_state_t *cmd)
2153 cmd_function_t *func;
2154 cmd_function_t **next = &cmd->userdefined->qc_functions;
2164 extern cvar_t sv_cheats;
2167 * Cloudwalk FIXME: This idea sounded great in my head but...
2168 * How do we handle commands that can be received by the client,
2169 * but which the server can also execute locally?
2171 * If we create a callback where the engine will forward to server
2172 * but try to execute the command locally if it's dedicated,
2173 * we're back to intermixing client and server code which I'm
2174 * trying to avoid. There's no other way I can think of to
2175 * implement that behavior that doesn't involve an #ifdef, or
2176 * making a mess of hooks.
2178 qbool Cmd_Callback(cmd_state_t *cmd, cmd_function_t *func, const char *text, cmd_source_t src)
2181 func->function(cmd);
2183 Con_Printf("Command \"%s\" can not be executed\n", Cmd_Argv(cmd, 0));
2187 qbool Cmd_CL_Callback(cmd_state_t *cmd, cmd_function_t *func, const char *text, cmd_source_t src)
2189 // TODO: Assign these functions to QC commands directly?
2192 if(((func->flags & CF_CLIENT) && CL_VM_ConsoleCommand(text)) ||
2193 ((func->flags & CF_SERVER) && SV_VM_ConsoleCommand(text)))
2196 if (func->flags & CF_SERVER_FROM_CLIENT)
2198 if(host_isclient.integer)
2200 CL_ForwardToServer_f(cmd);
2203 else if(!(func->flags & CF_SERVER))
2205 Con_Printf("Cannot execute client commands from a dedicated server console.\n");
2209 return Cmd_Callback(cmd, func, text, src);
2212 qbool Cmd_SV_Callback(cmd_state_t *cmd, cmd_function_t *func, const char *text, cmd_source_t src)
2214 if(func->qcfunc && (func->flags & CF_SERVER))
2215 return SV_VM_ConsoleCommand(text);
2216 else if (src == src_client)
2218 if((func->flags & CF_CHEAT) && !sv_cheats.integer)
2219 SV_ClientPrintf("No cheats allowed. The server must have sv_cheats set to 1\n");
2221 func->function(cmd);
2227 qbool Cmd_SV_NotFound(cmd_state_t *cmd, cmd_function_t *func, const char *text, cmd_source_t src)
2229 if (cmd->source == src_client)
2231 Con_Printf("Client \"%s\" tried to execute \"%s\"\n", host_client->name, text);
2240 A complete command line has been parsed, so try to execute it
2241 FIXME: lookupnoadd the token to speed search?
2244 void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qbool lockmutex)
2247 cmd_function_t *func;
2250 Cbuf_Lock(cmd->cbuf);
2251 oldpos = cmd->cbuf->tokenizebufferpos;
2254 Cmd_TokenizeString (cmd, text);
2256 // execute the command line
2258 goto done; // no tokens
2261 for (func = cmd->userdefined->qc_functions; func; func = func->next)
2263 if (!strcasecmp(cmd->argv[0], func->name))
2265 if(cmd->Handle(cmd, func, text, src))
2270 for (func = cmd->engine_functions; func; func=func->next)
2272 if (!strcasecmp (cmd->argv[0], func->name))
2274 if(cmd->Handle(cmd, func, text, src))
2279 // if it's a client command and no command was found, say so.
2282 if(cmd->NotFound(cmd, func, text, src))
2287 for (a=cmd->userdefined->alias ; a ; a=a->next)
2289 if (!strcasecmp (cmd->argv[0], a->name))
2291 Cmd_ExecuteAlias(cmd, a);
2297 if (!Cvar_Command(cmd) && host.framecount > 0)
2298 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(cmd, 0));
2300 cmd->cbuf->tokenizebufferpos = oldpos;
2302 Cbuf_Unlock(cmd->cbuf);
2309 Returns the position (1 to argc-1) in the command's argument list
2310 where the given parameter apears, or 0 if not present
2314 int Cmd_CheckParm (cmd_state_t *cmd, const char *parm)
2320 Con_Printf ("Cmd_CheckParm: NULL");
2324 for (i = 1; i < Cmd_Argc (cmd); i++)
2325 if (!strcasecmp (parm, Cmd_Argv(cmd, i)))
2333 void Cmd_SaveInitState(void)
2335 cmd_iter_t *cmd_iter;
2336 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2338 cmd_state_t *cmd = cmd_iter->cmd;
2341 for (f = cmd->userdefined->qc_functions; f; f = f->next)
2342 f->initstate = true;
2343 for (f = cmd->engine_functions; f; f = f->next)
2344 f->initstate = true;
2345 for (a = cmd->userdefined->alias; a; a = a->next)
2347 a->initstate = true;
2348 a->initialvalue = Mem_strdup(zonemempool, a->value);
2351 Cvar_SaveInitState(&cvars_all);
2354 void Cmd_RestoreInitState(void)
2356 cmd_iter_t *cmd_iter;
2357 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2359 cmd_state_t *cmd = cmd_iter->cmd;
2360 cmd_function_t *f, **fp;
2361 cmd_alias_t *a, **ap;
2362 for (fp = &cmd->userdefined->qc_functions; (f = *fp);)
2368 // destroy this command, it didn't exist at init
2369 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2374 for (fp = &cmd->engine_functions; (f = *fp);)
2380 // destroy this command, it didn't exist at init
2381 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2386 for (ap = &cmd->userdefined->alias; (a = *ap);)
2390 // restore this alias, it existed at init
2391 if (strcmp(a->value ? a->value : "", a->initialvalue ? a->initialvalue : ""))
2393 Con_DPrintf("Cmd_RestoreInitState: Restoring alias %s\n", a->name);
2396 a->value = Mem_strdup(zonemempool, a->initialvalue);
2402 // free this alias, it didn't exist at init...
2403 Con_DPrintf("Cmd_RestoreInitState: Destroying alias %s\n", a->name);
2411 Cvar_RestoreInitState(&cvars_all);
2414 void Cmd_NoOperation_f(cmd_state_t *cmd)