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 {&cmd_serverfromclient},
42 mempool_t *cbuf_mempool;
44 // we only run the +whatever commandline arguments once
45 qbool host_stuffcmdsrun = false;
47 //=============================================================================
49 void Cbuf_Lock(cmd_buf_t *cbuf)
51 Thread_LockMutex(cbuf->lock);
54 void Cbuf_Unlock(cmd_buf_t *cbuf)
56 Thread_UnlockMutex(cbuf->lock);
64 Causes execution of the remainder of the command buffer to be delayed until
65 next frame. This allows commands like:
66 bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
69 static void Cmd_Wait_f (cmd_state_t *cmd)
71 cmd->cbuf->wait = true;
78 Cause a command to be executed after a delay.
81 static cmd_input_t *Cbuf_LinkGet(cmd_buf_t *cbuf, cmd_input_t *existing);
82 static void Cmd_Defer_f (cmd_state_t *cmd)
85 cmd_buf_t *cbuf = cmd->cbuf;
87 if(Cmd_Argc(cmd) == 1)
89 if(List_Is_Empty(&cbuf->deferred))
90 Con_Printf("No commands are pending.\n");
94 List_For_Each(pos, &cbuf->deferred)
96 current = List_Entry(*pos, cmd_input_t, list);
97 Con_Printf("-> In %9.2f: %s\n", current->delay, current->text);
101 else if(Cmd_Argc(cmd) == 2 && !strcasecmp("clear", Cmd_Argv(cmd, 1)))
103 while(!List_Is_Empty(&cbuf->deferred))
104 List_Move_Tail(cbuf->deferred.next, &cbuf->free);
106 else if(Cmd_Argc(cmd) == 3)
108 const char *text = Cmd_Argv(cmd, 2);
109 current = Cbuf_LinkGet(cbuf, NULL);
110 current->length = strlen(text);
111 current->source = cmd;
112 current->delay = atof(Cmd_Argv(cmd, 1));
114 if(current->size < current->length)
116 current->text = (char *)Mem_Realloc(cbuf_mempool, current->text, current->length + 1);
117 current->size = current->length;
120 strlcpy(current->text, text, current->length + 1);
122 List_Move_Tail(¤t->list, &cbuf->deferred);
126 Con_Printf("usage: defer <seconds> <command>\n"
136 Print something to the center of the screen using SCR_Centerprint
139 static void Cmd_Centerprint_f (cmd_state_t *cmd)
141 char msg[MAX_INPUTLINE];
142 unsigned int i, c, p;
146 strlcpy(msg, Cmd_Argv(cmd,1), sizeof(msg));
147 for(i = 2; i < c; ++i)
149 strlcat(msg, " ", sizeof(msg));
150 strlcat(msg, Cmd_Argv(cmd, i), sizeof(msg));
152 c = (unsigned int)strlen(msg);
153 for(p = 0, i = 0; i < c; ++i)
159 else if(msg[i+1] == '\\')
171 SCR_CenterPrint(msg);
176 =============================================================================
180 =============================================================================
183 static cmd_input_t *Cbuf_LinkGet(cmd_buf_t *cbuf, cmd_input_t *existing)
185 cmd_input_t *ret = NULL;
186 if(existing && existing->pending)
188 else if(!List_Is_Empty(&cbuf->free))
190 ret = List_Entry(*cbuf->free.next, cmd_input_t, list);
192 ret->pending = false;
197 static cmd_input_t *Cmd_AllocInputNode(void)
199 cmd_input_t *node = (cmd_input_t *)Mem_Alloc(cbuf_mempool, sizeof(cmd_input_t));
200 node->list.prev = node->list.next = &node->list;
201 node->size = node->length = node->pending = 0;
205 static size_t Cmd_ParseInput (cmd_input_t **output, char **input)
207 size_t pos, cmdsize = 0, start = 0;
208 qbool command = false, lookahead = false;
209 qbool quotes = false, comment = false;
210 qbool escaped = false;
213 * The Quake command-line is super basic. It can be entered in the console
214 * or in config files. A semicolon is used to terminate a command and chain
215 * them together. Otherwise, a newline delineates command input.
217 * In most engines, the Quake command-line is a simple linear text buffer that
218 * is parsed when it executes. In Darkplaces, we use a linked list of command
219 * input and parse the input on the spot.
221 * This was done because Darkplaces allows multiple command interpreters on the
222 * same thread. Previously, each interpreter maintained its own buffer and this
223 * caused problems related to execution order, and maintaining a single simple
224 * buffer for all interpreters makes it non-trivial to keep track of which
225 * command should execute on which interpreter.
228 // Run until command and lookahead are both true, or until we run out of input.
229 for (pos = 0; (*input)[pos]; pos++)
231 // Look for newlines and semicolons. Ignore semicolons in quotes.
232 switch((*input)[pos])
240 if(!comment) // Not a newline so far. Still not a valid command yet.
242 if(!quotes && (*input)[pos] == ';') // Ignore semicolons in quotes.
244 else if (ISCOMMENT((*input), pos)) // Comments
259 switch((*input)[pos])
268 if (!escaped && quotes)
277 if(cmdsize && !command)
280 if(command && lookahead)
289 *output = Cmd_AllocInputNode();
291 if((*output)->pending)
292 offset = (*output)->length;
294 (*output)->length += cmdsize;
296 if((*output)->size < (*output)->length)
298 (*output)->text = (char *)Mem_Realloc(cbuf_mempool, (*output)->text, (*output)->length + 1);
299 (*output)->size = (*output)->length;
302 strlcpy(&(*output)->text[offset], &(*input)[start], cmdsize + 1);
303 (*output)->pending = !lookahead;
306 // Set input to its new position. Can be NULL.
307 *input = &(*input)[pos];
312 // Cloudwalk: Not happy with this, but it works.
313 static void Cbuf_LinkCreate(cmd_state_t *cmd, llist_t *head, cmd_input_t *existing, const char *text)
315 char *in = (char *)&text[0];
316 cmd_buf_t *cbuf = cmd->cbuf;
317 size_t totalsize = 0, newsize = 0;
318 cmd_input_t *current = NULL;
320 // Slide the pointer down until we reach the end
323 current = Cbuf_LinkGet(cbuf, existing);
324 newsize = Cmd_ParseInput(¤t, &in);
329 if(current != existing)
331 current->source = cmd;
332 List_Move_Tail(¤t->list, head);
335 totalsize += newsize;
337 else if (current == existing && !totalsize)
338 current->pending = false;
342 cbuf->size += totalsize;
349 Adds command text at the end of the buffer
352 void Cbuf_AddText (cmd_state_t *cmd, const char *text)
354 size_t l = strlen(text);
355 cmd_buf_t *cbuf = cmd->cbuf;
356 llist_t llist = {&llist, &llist};
360 if (cbuf->maxsize - cbuf->size <= l)
361 Con_Print("Cbuf_AddText: overflow\n");
364 Cbuf_LinkCreate(cmd, &llist, (List_Is_Empty(&cbuf->start) ? NULL : List_Entry(*cbuf->start.prev, cmd_input_t, list)), text);
365 if(!List_Is_Empty(&llist))
366 List_Splice_Tail(&llist, &cbuf->start);
375 Adds command text immediately after the current command
376 FIXME: actually change the command buffer to do less copying
379 void Cbuf_InsertText (cmd_state_t *cmd, const char *text)
381 cmd_buf_t *cbuf = cmd->cbuf;
382 llist_t llist = {&llist, &llist};
383 size_t l = strlen(text);
387 // we need to memmove the existing text and stuff this in before it...
388 if (cbuf->size + l >= cbuf->maxsize)
389 Con_Print("Cbuf_InsertText: overflow\n");
392 Cbuf_LinkCreate(cmd, &llist, List_Entry(*cbuf->start.next, cmd_input_t, list), text);
393 if(!List_Is_Empty(&llist))
394 List_Splice(&llist, &cbuf->start);
402 Cbuf_Execute_Deferred --blub
405 static void Cbuf_Execute_Deferred (cmd_buf_t *cbuf)
408 cmd_input_t *current;
411 if (host.realtime - cbuf->deferred_oldtime < 0 || host.realtime - cbuf->deferred_oldtime > 1800)
412 cbuf->deferred_oldtime = host.realtime;
413 eat = host.realtime - cbuf->deferred_oldtime;
414 if (eat < (1.0 / 120.0))
416 cbuf->deferred_oldtime = host.realtime;
418 List_For_Each(pos, &cbuf->deferred)
420 current = List_Entry(*pos, cmd_input_t, list);
421 current->delay -= eat;
422 if(current->delay <= 0)
424 cbuf->size += current->length;
425 List_Move(pos, &cbuf->start);
426 // We must return and come back next frame or the engine will freeze. Fragile... like glass :3
437 static qbool Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmd_alias_t *alias );
438 void Cbuf_Execute (cmd_buf_t *cbuf)
440 cmd_input_t *current;
441 char preprocessed[MAX_INPUTLINE];
444 // LadyHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
445 cbuf->tokenizebufferpos = 0;
447 while (!List_Is_Empty(&cbuf->start))
450 * Delete the text from the command buffer and move remaining
451 * commands down. This is necessary because commands (exec, alias)
452 * can insert data at the beginning of the text buffer
454 current = List_Entry(*cbuf->start.next, cmd_input_t, list);
456 // Recycle memory so using WASD doesn't cause a malloc and free
457 List_Move_Tail(¤t->list, &cbuf->free);
460 * Assume we're rolling with the current command-line and
461 * always set this false because alias expansion or cbuf insertion
462 * without a newline may set this true, and cause weirdness.
464 current->pending = false;
466 cbuf->size -= current->length;
468 firstchar = current->text;
469 while(*firstchar && ISWHITESPACE(*firstchar))
471 if((strncmp(firstchar, "alias", 5) || !ISWHITESPACE(firstchar[5])) &&
472 (strncmp(firstchar, "bind", 4) || !ISWHITESPACE(firstchar[4])) &&
473 (strncmp(firstchar, "in_bind", 7) || !ISWHITESPACE(firstchar[7])))
475 if(Cmd_PreprocessString(current->source, current->text, preprocessed, sizeof(preprocessed), NULL ))
476 Cmd_ExecuteString(current->source, preprocessed, src_local, false);
480 Cmd_ExecuteString (current->source, current->text, src_local, false);
488 * Skip out while text still remains in
489 * buffer, leaving it for next frame
497 void Cbuf_Frame(cmd_buf_t *cbuf)
499 Cbuf_Execute_Deferred(cbuf);
502 SV_LockThreadMutex();
504 SV_UnlockThreadMutex();
509 ==============================================================================
513 ==============================================================================
520 Adds command line parameters as script statements
521 Commands lead with a +, and continue until a - or another +
522 quake +prog jctest.qp +cmd amlev1
523 quake -nosound +cmd amlev1
526 static void Cmd_StuffCmds_f (cmd_state_t *cmd)
529 // this is for all commandline options combined (and is bounds checked)
530 char build[MAX_INPUTLINE];
532 if (Cmd_Argc (cmd) != 1)
534 Con_Print("stuffcmds : execute command line parameters\n");
538 // no reason to run the commandline arguments twice
539 if (host_stuffcmdsrun)
542 host_stuffcmdsrun = true;
545 for (i = 0;i < sys.argc;i++)
547 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)
550 while (sys.argv[i][j])
551 build[l++] = sys.argv[i][j++];
553 for (;i < sys.argc;i++)
557 if ((sys.argv[i][0] == '+' || sys.argv[i][0] == '-') && (sys.argv[i][1] < '0' || sys.argv[i][1] > '9'))
559 if (l + strlen(sys.argv[i]) + 4 > sizeof(build) - 1)
562 if (strchr(sys.argv[i], ' '))
564 for (j = 0;sys.argv[i][j];j++)
565 build[l++] = sys.argv[i][j];
566 if (strchr(sys.argv[i], ' '))
573 // now terminate the combined string and prepend it to the command buffer
574 // we already reserved space for the terminator
576 Cbuf_InsertText (cmd, build);
579 static void Cmd_Exec(cmd_state_t *cmd, const char *filename)
582 size_t filenameLen = strlen(filename);
584 !strcmp(filename, "default.cfg") ||
585 (filenameLen >= 12 && !strcmp(filename + filenameLen - 12, "/default.cfg"));
587 if (!strcmp(filename, "config.cfg"))
589 filename = CONFIGFILENAME;
590 if (Sys_CheckParm("-noconfig"))
591 return; // don't execute config.cfg
594 f = (char *)FS_LoadFile (filename, tempmempool, false, NULL);
597 Con_Printf("couldn't exec %s\n",filename);
600 Con_Printf("execing %s\n",filename);
602 // if executing default.cfg for the first time, lock the cvar defaults
603 // it may seem backwards to insert this text BEFORE the default.cfg
604 // but Cbuf_InsertText inserts before, so this actually ends up after it.
606 Cbuf_InsertText(cmd, "\ncvar_lockdefaults\n");
608 Cbuf_InsertText (cmd, f);
613 // special defaults for specific games go here, these execute before default.cfg
614 // Nehahra pushable crates malfunction in some levels if this is on
615 // Nehahra NPC AI is confused by blowupfallenzombies
619 Cbuf_InsertText(cmd, "\n"
620 "sv_gameplayfix_blowupfallenzombies 0\n"
621 "sv_gameplayfix_findradiusdistancetobox 0\n"
622 "sv_gameplayfix_grenadebouncedownslopes 0\n"
623 "sv_gameplayfix_slidemoveprojectiles 0\n"
624 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
625 "sv_gameplayfix_setmodelrealbox 0\n"
626 "sv_gameplayfix_droptofloorstartsolid 0\n"
627 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
628 "sv_gameplayfix_noairborncorpse 0\n"
629 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
630 "sv_gameplayfix_easierwaterjump 0\n"
631 "sv_gameplayfix_delayprojectiles 0\n"
632 "sv_gameplayfix_multiplethinksperframe 0\n"
633 "sv_gameplayfix_fixedcheckwatertransition 0\n"
634 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
635 "sv_gameplayfix_swiminbmodels 0\n"
636 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
637 "sys_ticrate 0.01388889\n"
639 "r_shadow_bumpscale_basetexture 0\n"
640 "csqc_polygons_defaultmaterial_nocullface 0\n"
644 Cbuf_InsertText(cmd, "\n"
645 "sv_gameplayfix_blowupfallenzombies 0\n"
646 "sv_gameplayfix_findradiusdistancetobox 0\n"
647 "sv_gameplayfix_grenadebouncedownslopes 0\n"
648 "sv_gameplayfix_slidemoveprojectiles 0\n"
649 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
650 "sv_gameplayfix_setmodelrealbox 0\n"
651 "sv_gameplayfix_droptofloorstartsolid 0\n"
652 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
653 "sv_gameplayfix_noairborncorpse 0\n"
654 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
655 "sv_gameplayfix_easierwaterjump 0\n"
656 "sv_gameplayfix_delayprojectiles 0\n"
657 "sv_gameplayfix_multiplethinksperframe 0\n"
658 "sv_gameplayfix_fixedcheckwatertransition 0\n"
659 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
660 "sv_gameplayfix_swiminbmodels 0\n"
661 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
662 "sys_ticrate 0.01388889\n"
664 "r_shadow_bumpscale_basetexture 0\n"
665 "csqc_polygons_defaultmaterial_nocullface 0\n"
668 // 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.
669 // 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
670 // hipnotic mission pack has issues in their proximity mine sticking code, which causes them to bounce off.
673 Cbuf_InsertText(cmd, "\n"
674 "sv_gameplayfix_blowupfallenzombies 0\n"
675 "sv_gameplayfix_findradiusdistancetobox 0\n"
676 "sv_gameplayfix_grenadebouncedownslopes 0\n"
677 "sv_gameplayfix_slidemoveprojectiles 0\n"
678 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
679 "sv_gameplayfix_setmodelrealbox 0\n"
680 "sv_gameplayfix_droptofloorstartsolid 0\n"
681 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
682 "sv_gameplayfix_noairborncorpse 0\n"
683 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
684 "sv_gameplayfix_easierwaterjump 0\n"
685 "sv_gameplayfix_delayprojectiles 0\n"
686 "sv_gameplayfix_multiplethinksperframe 0\n"
687 "sv_gameplayfix_fixedcheckwatertransition 0\n"
688 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
689 "sv_gameplayfix_swiminbmodels 0\n"
690 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
693 "r_shadow_bumpscale_basetexture 0\n"
694 "csqc_polygons_defaultmaterial_nocullface 0\n"
697 // rogue mission pack has a guardian boss that does not wake up if findradius returns one of the entities around its spawn area
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"
717 "sys_ticrate 0.01388889\n"
719 "r_shadow_bumpscale_basetexture 0\n"
720 "csqc_polygons_defaultmaterial_nocullface 0\n"
724 Cbuf_InsertText(cmd, "\n"
725 "sv_gameplayfix_blowupfallenzombies 0\n"
726 "sv_gameplayfix_findradiusdistancetobox 0\n"
727 "sv_gameplayfix_grenadebouncedownslopes 0\n"
728 "sv_gameplayfix_slidemoveprojectiles 0\n"
729 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
730 "sv_gameplayfix_setmodelrealbox 0\n"
731 "sv_gameplayfix_droptofloorstartsolid 0\n"
732 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
733 "sv_gameplayfix_noairborncorpse 0\n"
734 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
735 "sv_gameplayfix_easierwaterjump 0\n"
736 "sv_gameplayfix_delayprojectiles 0\n"
737 "sv_gameplayfix_multiplethinksperframe 0\n"
738 "sv_gameplayfix_fixedcheckwatertransition 0\n"
739 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
740 "sv_gameplayfix_swiminbmodels 0\n"
741 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
742 "sys_ticrate 0.01388889\n"
744 "r_shadow_bumpscale_basetexture 4\n"
745 "csqc_polygons_defaultmaterial_nocullface 0\n"
749 Cbuf_InsertText(cmd, "\n"
750 "sv_gameplayfix_blowupfallenzombies 1\n"
751 "sv_gameplayfix_findradiusdistancetobox 1\n"
752 "sv_gameplayfix_grenadebouncedownslopes 1\n"
753 "sv_gameplayfix_slidemoveprojectiles 1\n"
754 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
755 "sv_gameplayfix_setmodelrealbox 1\n"
756 "sv_gameplayfix_droptofloorstartsolid 1\n"
757 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
758 "sv_gameplayfix_noairborncorpse 1\n"
759 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
760 "sv_gameplayfix_easierwaterjump 1\n"
761 "sv_gameplayfix_delayprojectiles 1\n"
762 "sv_gameplayfix_multiplethinksperframe 1\n"
763 "sv_gameplayfix_fixedcheckwatertransition 1\n"
764 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
765 "sv_gameplayfix_swiminbmodels 1\n"
766 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
767 "sys_ticrate 0.01388889\n"
768 "sv_gameplayfix_q2airaccelerate 1\n"
769 "sv_gameplayfix_stepmultipletimes 1\n"
770 "csqc_polygons_defaultmaterial_nocullface 1\n"
771 "con_chatsound_team_mask 13\n"
775 case GAME_VORETOURNAMENT:
776 // 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
777 Cbuf_InsertText(cmd, "\n"
778 "csqc_polygons_defaultmaterial_nocullface 1\n"
779 "con_chatsound_team_mask 13\n"
780 "sv_gameplayfix_customstats 1\n"
783 // Steel Storm: Burning Retribution csqc misinterprets CSQC_InputEvent if type is a value other than 0 or 1
784 case GAME_STEELSTORM:
785 Cbuf_InsertText(cmd, "\n"
786 "sv_gameplayfix_blowupfallenzombies 1\n"
787 "sv_gameplayfix_findradiusdistancetobox 1\n"
788 "sv_gameplayfix_grenadebouncedownslopes 1\n"
789 "sv_gameplayfix_slidemoveprojectiles 1\n"
790 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
791 "sv_gameplayfix_setmodelrealbox 1\n"
792 "sv_gameplayfix_droptofloorstartsolid 1\n"
793 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
794 "sv_gameplayfix_noairborncorpse 1\n"
795 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
796 "sv_gameplayfix_easierwaterjump 1\n"
797 "sv_gameplayfix_delayprojectiles 1\n"
798 "sv_gameplayfix_multiplethinksperframe 1\n"
799 "sv_gameplayfix_fixedcheckwatertransition 1\n"
800 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
801 "sv_gameplayfix_swiminbmodels 1\n"
802 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
803 "sys_ticrate 0.01388889\n"
804 "cl_csqc_generatemousemoveevents 0\n"
805 "csqc_polygons_defaultmaterial_nocullface 1\n"
809 Cbuf_InsertText(cmd, "\n"
810 "sv_gameplayfix_blowupfallenzombies 1\n"
811 "sv_gameplayfix_findradiusdistancetobox 1\n"
812 "sv_gameplayfix_grenadebouncedownslopes 1\n"
813 "sv_gameplayfix_slidemoveprojectiles 1\n"
814 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
815 "sv_gameplayfix_setmodelrealbox 1\n"
816 "sv_gameplayfix_droptofloorstartsolid 1\n"
817 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
818 "sv_gameplayfix_noairborncorpse 1\n"
819 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
820 "sv_gameplayfix_easierwaterjump 1\n"
821 "sv_gameplayfix_delayprojectiles 1\n"
822 "sv_gameplayfix_multiplethinksperframe 1\n"
823 "sv_gameplayfix_fixedcheckwatertransition 1\n"
824 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
825 "sv_gameplayfix_swiminbmodels 1\n"
826 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
827 "sys_ticrate 0.01388889\n"
828 "csqc_polygons_defaultmaterial_nocullface 0\n"
840 static void Cmd_Exec_f (cmd_state_t *cmd)
845 if (Cmd_Argc(cmd) != 2)
847 Con_Print("exec <filename> : execute a script file\n");
851 s = FS_Search(Cmd_Argv(cmd, 1), true, true, NULL);
852 if(!s || !s->numfilenames)
854 Con_Printf("couldn't exec %s\n",Cmd_Argv(cmd, 1));
858 for(i = 0; i < s->numfilenames; ++i)
859 Cmd_Exec(cmd, s->filenames[i]);
869 Just prints the rest of the line to the console
872 static void Cmd_Echo_f (cmd_state_t *cmd)
876 for (i=1 ; i<Cmd_Argc(cmd) ; i++)
877 Con_Printf("%s ",Cmd_Argv(cmd, i));
882 // Support Doom3-style Toggle Console Command
887 Toggles a specified console variable amongst the values specified (default is 0 and 1)
890 static void Cmd_Toggle_f(cmd_state_t *cmd)
892 // Acquire Number of Arguments
893 int nNumArgs = Cmd_Argc(cmd);
896 // No Arguments Specified; Print Usage
897 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");
899 { // Correct Arguments Specified
900 // Acquire Potential CVar
901 cvar_t* cvCVar = Cvar_FindVar(cmd->cvars, Cmd_Argv(cmd, 1), cmd->cvars_flagsmask);
908 Cvar_SetValueQuick(cvCVar, 0);
910 Cvar_SetValueQuick(cvCVar, 1);
914 { // 0 and Specified Usage
915 if(cvCVar->integer == atoi(Cmd_Argv(cmd, 2) ) )
916 // CVar is Specified Value; // Reset to 0
917 Cvar_SetValueQuick(cvCVar, 0);
919 if(cvCVar->integer == 0)
920 // CVar is 0; Specify Value
921 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
923 // CVar does not match; Reset to 0
924 Cvar_SetValueQuick(cvCVar, 0);
927 { // Variable Values Specified
931 for(nCnt = 2; nCnt < nNumArgs; nCnt++)
932 { // Cycle through Values
933 if( strcmp(cvCVar->string, Cmd_Argv(cmd, nCnt) ) == 0)
934 { // Current Value Located; Increment to Next
935 if( (nCnt + 1) == nNumArgs)
936 // Max Value Reached; Reset
937 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
940 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, nCnt + 1) );
949 // Value not Found; Reset to Original
950 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
956 Con_Printf("ERROR : CVar '%s' not found\n", Cmd_Argv(cmd, 1) );
965 Creates a new command that executes a command string (possibly ; seperated)
968 static void Cmd_Alias_f (cmd_state_t *cmd)
971 char line[MAX_INPUTLINE];
976 if (Cmd_Argc(cmd) == 1)
978 Con_Print("Current alias commands:\n");
979 for (a = cmd->userdefined->alias ; a ; a=a->next)
980 Con_Printf("%s : %s", a->name, a->value);
984 s = Cmd_Argv(cmd, 1);
985 if (strlen(s) >= MAX_ALIAS_NAME)
987 Con_Print("Alias name is too long\n");
991 // if the alias already exists, reuse it
992 for (a = cmd->userdefined->alias ; a ; a=a->next)
994 if (!strcmp(s, a->name))
1003 cmd_alias_t *prev, *current;
1005 a = (cmd_alias_t *)Z_Malloc (sizeof(cmd_alias_t));
1006 strlcpy (a->name, s, sizeof (a->name));
1007 // insert it at the right alphanumeric position
1008 for( prev = NULL, current = cmd->userdefined->alias ; current && strcmp( current->name, a->name ) < 0 ; prev = current, current = current->next )
1013 cmd->userdefined->alias = a;
1019 // copy the rest of the command line
1020 line[0] = 0; // start out with a null string
1022 for (i=2 ; i < c ; i++)
1025 strlcat (line, " ", sizeof (line));
1026 strlcat (line, Cmd_Argv(cmd, i), sizeof (line));
1028 strlcat (line, "\n", sizeof (line));
1030 alloclen = strlen (line) + 1;
1032 line[alloclen - 2] = '\n'; // to make sure a newline is appended even if too long
1033 a->value = (char *)Z_Malloc (alloclen);
1034 memcpy (a->value, line, alloclen);
1041 Remove existing aliases.
1044 static void Cmd_UnAlias_f (cmd_state_t *cmd)
1050 if(Cmd_Argc(cmd) == 1)
1052 Con_Print("unalias: Usage: unalias alias1 [alias2 ...]\n");
1056 for(i = 1; i < Cmd_Argc(cmd); ++i)
1058 s = Cmd_Argv(cmd, i);
1060 for(a = cmd->userdefined->alias; a; p = a, a = a->next)
1062 if(!strcmp(s, a->name))
1064 if (a->initstate) // we can not remove init aliases
1066 if(a == cmd->userdefined->alias)
1067 cmd->userdefined->alias = a->next;
1076 Con_Printf("unalias: %s alias not found\n", s);
1081 =============================================================================
1085 =============================================================================
1088 static const char *Cmd_GetDirectCvarValue(cmd_state_t *cmd, const char *varname, cmd_alias_t *alias, qbool *is_multiple)
1093 static char vabuf[1024]; // cmd_mutex
1096 *is_multiple = false;
1098 if(!varname || !*varname)
1103 if(!strcmp(varname, "*"))
1106 *is_multiple = true;
1107 return Cmd_Args(cmd);
1109 else if(!strcmp(varname, "#"))
1111 return va(vabuf, sizeof(vabuf), "%d", Cmd_Argc(cmd));
1113 else if(varname[strlen(varname) - 1] == '-')
1115 argno = strtol(varname, &endptr, 10);
1116 if(endptr == varname + strlen(varname) - 1)
1118 // whole string is a number, apart from the -
1119 const char *p = Cmd_Args(cmd);
1120 for(; argno > 1; --argno)
1121 if(!COM_ParseToken_Console(&p))
1126 *is_multiple = true;
1128 // kill pre-argument whitespace
1129 for (;*p && ISWHITESPACE(*p);p++)
1138 argno = strtol(varname, &endptr, 10);
1141 // whole string is a number
1142 // NOTE: we already made sure we don't have an empty cvar name!
1143 if(argno >= 0 && argno < Cmd_Argc(cmd))
1144 return Cmd_Argv(cmd, argno);
1149 if((cvar = Cvar_FindVar(cmd->cvars, varname, cmd->cvars_flagsmask)) && !(cvar->flags & CF_PRIVATE))
1150 return cvar->string;
1155 qbool Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset, qbool putquotes)
1157 qbool quote_quot = !!strchr(quoteset, '"');
1158 qbool quote_backslash = !!strchr(quoteset, '\\');
1159 qbool quote_dollar = !!strchr(quoteset, '$');
1168 *out++ = '"'; --outlen;
1174 if(*in == '"' && quote_quot)
1178 *out++ = '\\'; --outlen;
1179 *out++ = '"'; --outlen;
1181 else if(*in == '\\' && quote_backslash)
1185 *out++ = '\\'; --outlen;
1186 *out++ = '\\'; --outlen;
1188 else if(*in == '$' && quote_dollar)
1192 *out++ = '$'; --outlen;
1193 *out++ = '$'; --outlen;
1199 *out++ = *in; --outlen;
1214 static const char *Cmd_GetCvarValue(cmd_state_t *cmd, const char *var, size_t varlen, cmd_alias_t *alias)
1216 static char varname[MAX_INPUTLINE]; // cmd_mutex
1217 static char varval[MAX_INPUTLINE]; // cmd_mutex
1218 const char *varstr = NULL;
1220 qbool required = false;
1221 qbool optional = false;
1222 static char asis[] = "asis"; // just to suppress const char warnings
1224 if(varlen >= MAX_INPUTLINE)
1225 varlen = MAX_INPUTLINE - 1;
1226 memcpy(varname, var, varlen);
1227 varname[varlen] = 0;
1228 varfunc = strchr(varname, ' ');
1240 Con_Printf(CON_WARN "Warning: Could not expand $ in alias %s\n", alias->name);
1242 Con_Printf(CON_WARN "Warning: Could not expand $\n");
1250 while((p = strchr(varfunc, '?')))
1253 memmove(p, p+1, strlen(p)); // with final NUL
1256 while((p = strchr(varfunc, '!')))
1259 memmove(p, p+1, strlen(p)); // with final NUL
1262 while((p = strchr(varfunc, ' ')))
1264 memmove(p, p+1, strlen(p)); // with final NUL
1266 // if no function is left, NULL it
1271 if(varname[0] == '$')
1272 varstr = Cmd_GetDirectCvarValue(cmd, Cmd_GetDirectCvarValue(cmd, varname + 1, alias, NULL), alias, NULL);
1275 qbool is_multiple = false;
1276 // Exception: $* and $n- don't use the quoted form by default
1277 varstr = Cmd_GetDirectCvarValue(cmd, varname, alias, &is_multiple);
1288 Con_Printf(CON_ERROR "Error: Could not expand $%s in alias %s\n", varname, alias->name);
1290 Con_Printf(CON_ERROR "Error: Could not expand $%s\n", varname);
1300 Con_Printf(CON_WARN "Warning: Could not expand $%s in alias %s\n", varname, alias->name);
1302 Con_Printf(CON_WARN "Warning: Could not expand $%s\n", varname);
1303 dpsnprintf(varval, sizeof(varval), "$%s", varname);
1308 if(!varfunc || !strcmp(varfunc, "q")) // note: quoted form is default, use "asis" to override!
1310 // quote it so it can be used inside double quotes
1311 // we just need to replace " by \", and of course, double backslashes
1312 Cmd_QuoteString(varval, sizeof(varval), varstr, "\"\\", false);
1315 else if(!strcmp(varfunc, "asis"))
1320 Con_Printf("Unknown variable function %s\n", varfunc);
1326 Cmd_PreprocessString
1328 Preprocesses strings and replaces $*, $param#, $cvar accordingly. Also strips comments.
1330 static qbool Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmd_alias_t *alias ) {
1336 // don't crash if there's no room in the outtext buffer
1337 if( maxoutlen == 0 ) {
1340 maxoutlen--; // because of \0
1345 while( *in && outlen < maxoutlen ) {
1347 // this is some kind of expansion, see what comes after the $
1350 // The console does the following preprocessing:
1352 // - $$ is transformed to a single dollar sign.
1353 // - $var or ${var} are expanded to the contents of the named cvar,
1354 // with quotation marks and backslashes quoted so it can safely
1355 // be used inside quotation marks (and it should always be used
1357 // - ${var asis} inserts the cvar value as is, without doing this
1359 // - ${var ?} silently expands to the empty string if
1360 // $var does not exist
1361 // - ${var !} fails expansion and executes nothing if
1362 // $var does not exist
1363 // - prefix the cvar name with a dollar sign to do indirection;
1364 // for example, if $x has the value timelimit, ${$x} will return
1365 // the value of $timelimit
1366 // - when expanding an alias, the special variable name $* refers
1367 // to all alias parameters, and a number refers to that numbered
1368 // alias parameter, where the name of the alias is $0, the first
1369 // parameter is $1 and so on; as a special case, $* inserts all
1370 // parameters, without extra quoting, so one can use $* to just
1371 // pass all parameters around. All parameters starting from $n
1372 // can be referred to as $n- (so $* is equivalent to $1-).
1373 // - ${* q} and ${n- q} force quoting anyway
1375 // Note: when expanding an alias, cvar expansion is done in the SAME step
1376 // as alias expansion so that alias parameters or cvar values containing
1377 // dollar signs have no unwanted bad side effects. However, this needs to
1378 // be accounted for when writing complex aliases. For example,
1379 // alias foo "set x NEW; echo $x"
1380 // actually expands to
1381 // "set x NEW; echo OLD"
1382 // and will print OLD! To work around this, use a second alias:
1383 // alias foo "set x NEW; foo2"
1384 // alias foo2 "echo $x"
1386 // Also note: lines starting with alias are exempt from cvar expansion.
1387 // If you want cvar expansion, write "alias" instead:
1390 // alias foo "echo $x"
1391 // "alias" bar "echo $x"
1394 // foo will print 2, because the variable $x will be expanded when the alias
1395 // gets expanded. bar will print 1, because the variable $x was expanded
1396 // at definition time. foo can be equivalently defined as
1398 // "alias" foo "echo $$x"
1400 // because at definition time, $$ will get replaced to a single $.
1405 } else if(*in == '{') {
1406 varlen = strcspn(in + 1, "}");
1407 if(in[varlen + 1] == '}')
1409 val = Cmd_GetCvarValue(cmd, in + 1, varlen, alias);
1421 varlen = strspn(in, "#*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-");
1422 val = Cmd_GetCvarValue(cmd, in, varlen, alias);
1429 // insert the cvar value
1430 while(*val && outlen < maxoutlen)
1431 outtext[outlen++] = *val++;
1436 // copy the unexpanded text
1437 outtext[outlen++] = '$';
1438 while(eat && outlen < maxoutlen)
1440 outtext[outlen++] = *in++;
1446 outtext[outlen++] = *in++;
1448 outtext[outlen] = 0;
1456 Called for aliases and fills in the alias into the cbuffer
1459 static void Cmd_ExecuteAlias (cmd_state_t *cmd, cmd_alias_t *alias)
1461 static char buffer[ MAX_INPUTLINE ]; // cmd_mutex
1462 static char buffer2[ MAX_INPUTLINE ]; // cmd_mutex
1463 qbool ret = Cmd_PreprocessString( cmd, alias->value, buffer, sizeof(buffer) - 2, alias );
1466 // insert at start of command buffer, so that aliases execute in order
1467 // (fixes bug introduced by Black on 20050705)
1469 // Note: Cbuf_PreprocessString will be called on this string AGAIN! So we
1470 // have to make sure that no second variable expansion takes place, otherwise
1471 // alias parameters containing dollar signs can have bad effects.
1472 Cmd_QuoteString(buffer2, sizeof(buffer2), buffer, "$", false);
1473 Cbuf_InsertText(cmd, buffer2);
1480 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
1481 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
1485 static void Cmd_List_f (cmd_state_t *cmd)
1487 cmd_function_t *func;
1488 const char *partial;
1493 if (Cmd_Argc(cmd) > 1)
1495 partial = Cmd_Argv(cmd, 1);
1496 len = strlen(partial);
1497 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1507 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1509 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1511 Con_Printf("%s : %s\n", func->name, func->description);
1514 for (func = cmd->engine_functions; func; func = func->next)
1516 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1518 Con_Printf("%s : %s\n", func->name, func->description);
1525 Con_Printf("%i Command%s matching \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1527 Con_Printf("%i Command%s beginning with \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1530 Con_Printf("%i Command%s\n\n", count, (count > 1) ? "s" : "");
1533 static void Cmd_Apropos_f(cmd_state_t *cmd)
1535 cmd_function_t *func;
1538 const char *partial;
1543 if (Cmd_Argc(cmd) > 1)
1544 partial = Cmd_Args(cmd);
1547 Con_Printf("usage: %s <string>\n",Cmd_Argv(cmd, 0));
1551 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
1553 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
1556 for (cvar = cmd->cvars->vars; cvar; cvar = cvar->next)
1558 if (matchpattern_with_separator(cvar->name, partial, true, "", false) ||
1559 matchpattern_with_separator(cvar->description, partial, true, "", false))
1561 Con_Printf ("cvar ");
1562 Cvar_PrintHelp(cvar, cvar->name, true);
1565 for (int i = 0; i < cvar->aliasindex; i++)
1567 if (matchpattern_with_separator(cvar->aliases[i], partial, true, "", false))
1569 Con_Printf ("cvar ");
1570 Cvar_PrintHelp(cvar, cvar->aliases[i], true);
1575 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1577 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1578 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1580 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1583 for (func = cmd->engine_functions; func; func = func->next)
1585 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1586 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1588 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1591 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
1593 // procede here a bit differently as an alias value always got a final \n
1594 if (!matchpattern_with_separator(alias->name, partial, true, "", false))
1595 if (!matchpattern_with_separator(alias->value, partial, true, "\n", false)) // when \n is as separator wildcards don't match it
1597 Con_Printf("alias ^5%s^7: %s", alias->name, alias->value); // do not print an extra \n
1600 Con_Printf("%i result%s\n\n", count, (count > 1) ? "s" : "");
1610 cmd_iter_t *cmd_iter;
1612 cbuf_mempool = Mem_AllocPool("Command buffer", 0, NULL);
1613 cbuf = (cmd_buf_t *)Mem_Alloc(cbuf_mempool, sizeof(cmd_buf_t));
1614 cbuf->maxsize = 655360;
1615 cbuf->lock = Thread_CreateMutex();
1619 cbuf->start.prev = cbuf->start.next = &(cbuf->start);
1620 cbuf->deferred.prev = cbuf->deferred.next = &(cbuf->deferred);
1621 cbuf->free.prev = cbuf->free.next = &(cbuf->free);
1623 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
1625 cmd_state_t *cmd = cmd_iter->cmd;
1626 cmd->mempool = Mem_AllocPool("commands", 0, NULL);
1627 // space for commands and script files
1629 cmd->null_string = "";
1631 // client console can see server cvars because the user may start a server
1632 cmd_local.cvars = &cvars_all;
1633 cmd_local.cvars_flagsmask = CF_CLIENT | CF_SERVER;
1634 cmd_local.cmd_flags = CF_SERVER | CF_CLIENT | CF_CLIENT_FROM_SERVER;
1635 cmd_local.auto_flags = CF_SERVER_FROM_CLIENT;
1636 cmd_local.auto_function = CL_ForwardToServer_f; // FIXME: Move this to the client.
1637 cmd_local.userdefined = &cmd_userdefined_all;
1638 // server commands received from clients have no reason to access cvars, cvar expansion seems perilous.
1639 cmd_serverfromclient.cvars = &cvars_null;
1640 cmd_serverfromclient.cvars_flagsmask = 0;
1641 cmd_serverfromclient.cmd_flags = CF_SERVER_FROM_CLIENT | CF_USERINFO;
1642 cmd_serverfromclient.auto_flags = 0;
1643 cmd_serverfromclient.auto_function = NULL;
1644 cmd_serverfromclient.userdefined = &cmd_userdefined_null;
1647 // register our commands
1649 // client-only commands
1650 Cmd_AddCommand(CF_SHARED, "wait", Cmd_Wait_f, "make script execution wait for next rendered frame");
1651 Cmd_AddCommand(CF_CLIENT, "cprint", Cmd_Centerprint_f, "print something at the screen center");
1653 // maintenance commands used for upkeep of cvars and saved configs
1654 Cmd_AddCommand(CF_SHARED, "stuffcmds", Cmd_StuffCmds_f, "execute commandline parameters (must be present in quake.rc script)");
1655 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");
1656 Cmd_AddCommand(CF_SHARED, "cvar_resettodefaults_all", Cvar_ResetToDefaults_All_f, "sets all cvars to their locked default values");
1657 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)");
1658 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)");
1660 // general console commands used in multiple environments
1661 Cmd_AddCommand(CF_SHARED, "exec", Cmd_Exec_f, "execute a script file");
1662 Cmd_AddCommand(CF_SHARED, "echo",Cmd_Echo_f, "print a message to the console (useful in scripts)");
1663 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");
1664 Cmd_AddCommand(CF_SHARED, "unalias",Cmd_UnAlias_f, "remove an alias");
1665 Cmd_AddCommand(CF_SHARED, "set", Cvar_Set_f, "create or change the value of a console variable");
1666 Cmd_AddCommand(CF_SHARED, "seta", Cvar_SetA_f, "create or change the value of a console variable that will be saved to config.cfg");
1667 Cmd_AddCommand(CF_SHARED, "unset", Cvar_Del_f, "delete a cvar (does not work for static ones like _cl_name, or read-only ones)");
1669 #ifdef FILLALLCVARSWITHRUBBISH
1670 Cmd_AddCommand(CF_SHARED, "fillallcvarswithrubbish", Cvar_FillAll_f, "fill all cvars with a specified number of characters to provoke buffer overruns");
1671 #endif /* FILLALLCVARSWITHRUBBISH */
1673 // 2000-01-09 CmdList, CvarList commands By Matthias "Maddes" Buecher
1674 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
1675 Cmd_AddCommand(CF_SHARED, "cmdlist", Cmd_List_f, "lists all console commands beginning with the specified prefix or matching the specified wildcard pattern");
1676 Cmd_AddCommand(CF_SHARED, "cvarlist", Cvar_List_f, "lists all console variables beginning with the specified prefix or matching the specified wildcard pattern");
1677 Cmd_AddCommand(CF_SHARED, "apropos", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1678 Cmd_AddCommand(CF_SHARED, "find", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1680 Cmd_AddCommand(CF_SHARED, "defer", Cmd_Defer_f, "execute a command in the future");
1683 // Support Doom3-style Toggle Command
1684 Cmd_AddCommand(CF_SHARED | CF_CLIENT_FROM_SERVER, "toggle", Cmd_Toggle_f, "toggles a console variable's values (use for more info)");
1692 void Cmd_Shutdown(void)
1694 cmd_iter_t *cmd_iter;
1695 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
1697 cmd_state_t *cmd = cmd_iter->cmd;
1699 if (cmd->cbuf->lock)
1701 // we usually have this locked when we get here from Host_Quit_f
1702 Cbuf_Unlock(cmd->cbuf);
1705 Mem_FreePool(&cmd->mempool);
1714 int Cmd_Argc (cmd_state_t *cmd)
1724 const char *Cmd_Argv(cmd_state_t *cmd, int arg)
1726 if (arg >= cmd->argc )
1727 return cmd->null_string;
1728 return cmd->argv[arg];
1736 const char *Cmd_Args (cmd_state_t *cmd)
1745 Parses the given string into command line tokens.
1748 // AK: This function should only be called from ExcuteString because the current design is a bit of an hack
1749 static void Cmd_TokenizeString (cmd_state_t *cmd, const char *text)
1755 cmd->cmdline = NULL;
1759 // skip whitespace up to a /n
1760 while (*text && ISWHITESPACE(*text) && *text != '\r' && *text != '\n')
1767 if (*text == '\n' || *text == '\r')
1769 // a newline separates commands in the buffer
1770 if (*text == '\r' && text[1] == '\n')
1780 cmd->cmdline = text;
1784 if (!COM_ParseToken_Console(&text))
1787 if (cmd->argc < MAX_ARGS)
1789 l = (int)strlen(com_token) + 1;
1790 if (cmd->cbuf->tokenizebufferpos + l > CMD_TOKENIZELENGTH)
1792 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguments\n", CMD_TOKENIZELENGTH);
1795 memcpy (cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos, com_token, l);
1796 cmd->argv[cmd->argc] = cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos;
1797 cmd->cbuf->tokenizebufferpos += l;
1809 void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const char *description)
1811 cmd_function_t *func;
1812 cmd_function_t *prev, *current;
1814 xcommand_t save = NULL;
1815 qbool auto_add = false;
1818 for (i = 0; i < 2; i++)
1820 cmd = cmd_iter_all[i].cmd;
1821 if ((flags & cmd->cmd_flags) || (flags & cmd->auto_flags))
1823 if((flags & cmd->auto_flags) && cmd->auto_function)
1826 function = cmd->auto_function;
1830 // fail if the command is a variable name
1831 if (Cvar_FindVar(cmd->cvars, cmd_name, ~0))
1833 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
1839 // fail if the command already exists in this interpreter
1840 for (func = cmd->engine_functions; func; func = func->next)
1842 if (!strcmp(cmd_name, func->name))
1844 if(func->autofunc && !auto_add)
1846 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
1851 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1852 func->flags = flags;
1853 func->name = cmd_name;
1854 func->function = function;
1855 func->description = description;
1856 func->next = cmd->engine_functions;
1857 func->autofunc = auto_add;
1859 // insert it at the right alphanumeric position
1860 for (prev = NULL, current = cmd->engine_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1866 cmd->engine_functions = func;
1868 func->next = current;
1872 // mark qcfunc if the function already exists in the qc_functions list
1873 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1875 if (!strcmp(cmd_name, func->name))
1877 func->qcfunc = true; //[515]: csqc
1883 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1884 func->flags = flags;
1885 func->name = cmd_name;
1886 func->function = function;
1887 func->description = description;
1888 func->qcfunc = true; //[515]: csqc
1889 func->next = cmd->userdefined->qc_functions;
1890 func->autofunc = false;
1892 // insert it at the right alphanumeric position
1893 for (prev = NULL, current = cmd->userdefined->qc_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1899 cmd->userdefined->qc_functions = func;
1901 func->next = current;
1917 qbool Cmd_Exists (cmd_state_t *cmd, const char *cmd_name)
1919 cmd_function_t *func;
1921 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1922 if (!strcmp(cmd_name, func->name))
1925 for (func=cmd->engine_functions ; func ; func=func->next)
1926 if (!strcmp (cmd_name,func->name))
1938 const char *Cmd_CompleteCommand (cmd_state_t *cmd, const char *partial)
1940 cmd_function_t *func;
1943 len = strlen(partial);
1949 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1950 if (!strncasecmp(partial, func->name, len))
1953 for (func = cmd->engine_functions; func; func = func->next)
1954 if (!strncasecmp(partial, func->name, len))
1961 Cmd_CompleteCountPossible
1963 New function for tab-completion system
1964 Added by EvilTypeGuy
1965 Thanks to Fett erich@heintz.com
1969 int Cmd_CompleteCountPossible (cmd_state_t *cmd, const char *partial)
1971 cmd_function_t *func;
1976 len = strlen(partial);
1981 // Loop through the command list and count all partial matches
1982 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1983 if (!strncasecmp(partial, func->name, len))
1986 for (func = cmd->engine_functions; func; func = func->next)
1987 if (!strncasecmp(partial, func->name, len))
1994 Cmd_CompleteBuildList
1996 New function for tab-completion system
1997 Added by EvilTypeGuy
1998 Thanks to Fett erich@heintz.com
2002 const char **Cmd_CompleteBuildList (cmd_state_t *cmd, const char *partial)
2004 cmd_function_t *func;
2007 size_t sizeofbuf = (Cmd_CompleteCountPossible (cmd, partial) + 1) * sizeof (const char *);
2010 len = strlen(partial);
2011 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
2012 // Loop through the functions lists and print all matches
2013 for (func = cmd->userdefined->qc_functions; func; func = func->next)
2014 if (!strncasecmp(partial, func->name, len))
2015 buf[bpos++] = func->name;
2016 for (func = cmd->engine_functions; func; func = func->next)
2017 if (!strncasecmp(partial, func->name, len))
2018 buf[bpos++] = func->name;
2024 // written by LadyHavoc
2025 void Cmd_CompleteCommandPrint (cmd_state_t *cmd, const char *partial)
2027 cmd_function_t *func;
2028 size_t len = strlen(partial);
2029 // Loop through the command list and print all matches
2030 for (func = cmd->userdefined->qc_functions; func; func = func->next)
2031 if (!strncasecmp(partial, func->name, len))
2032 Con_Printf("^2%s^7: %s\n", func->name, func->description);
2033 for (func = cmd->engine_functions; func; func = func->next)
2034 if (!strncasecmp(partial, func->name, len))
2035 Con_Printf("^2%s^7: %s\n", func->name, func->description);
2041 New function for tab-completion system
2042 Added by EvilTypeGuy
2043 Thanks to Fett erich@heintz.com
2047 const char *Cmd_CompleteAlias (cmd_state_t *cmd, const char *partial)
2052 len = strlen(partial);
2058 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2059 if (!strncasecmp(partial, alias->name, len))
2065 // written by LadyHavoc
2066 void Cmd_CompleteAliasPrint (cmd_state_t *cmd, const char *partial)
2069 size_t len = strlen(partial);
2070 // Loop through the alias list and print all matches
2071 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2072 if (!strncasecmp(partial, alias->name, len))
2073 Con_Printf("^5%s^7: %s", alias->name, alias->value);
2078 Cmd_CompleteAliasCountPossible
2080 New function for tab-completion system
2081 Added by EvilTypeGuy
2082 Thanks to Fett erich@heintz.com
2086 int Cmd_CompleteAliasCountPossible (cmd_state_t *cmd, const char *partial)
2094 len = strlen(partial);
2099 // Loop through the command list and count all partial matches
2100 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2101 if (!strncasecmp(partial, alias->name, len))
2108 Cmd_CompleteAliasBuildList
2110 New function for tab-completion system
2111 Added by EvilTypeGuy
2112 Thanks to Fett erich@heintz.com
2116 const char **Cmd_CompleteAliasBuildList (cmd_state_t *cmd, const char *partial)
2121 size_t sizeofbuf = (Cmd_CompleteAliasCountPossible (cmd, partial) + 1) * sizeof (const char *);
2124 len = strlen(partial);
2125 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
2126 // Loop through the alias list and print all matches
2127 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2128 if (!strncasecmp(partial, alias->name, len))
2129 buf[bpos++] = alias->name;
2135 // TODO: Make this more generic?
2136 void Cmd_ClearCSQCCommands (cmd_state_t *cmd)
2138 cmd_function_t *func;
2139 cmd_function_t **next = &cmd->userdefined->qc_functions;
2149 extern cvar_t sv_cheats;
2155 A complete command line has been parsed, so try to execute it
2156 FIXME: lookupnoadd the token to speed search?
2159 void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qbool lockmutex)
2162 cmd_function_t *func;
2165 Cbuf_Lock(cmd->cbuf);
2166 oldpos = cmd->cbuf->tokenizebufferpos;
2169 Cmd_TokenizeString (cmd, text);
2171 // execute the command line
2173 goto done; // no tokens
2176 for (func = cmd->userdefined->qc_functions; func; func = func->next)
2178 if (!strcasecmp(cmd->argv[0], func->name))
2182 if((func->flags & CF_CLIENT) && CL_VM_ConsoleCommand(text))
2184 else if((func->flags & CF_SERVER) && SV_VM_ConsoleCommand(text))
2190 for (func = cmd->engine_functions; func; func=func->next)
2192 if (!strcasecmp (cmd->argv[0], func->name))
2198 func->function(cmd);
2200 Con_Printf("Command \"%s\" can not be executed\n", Cmd_Argv(cmd, 0));
2205 if((func->flags & CF_CHEAT) && !sv_cheats.integer)
2206 SV_ClientPrintf("No cheats allowed. The server must have sv_cheats set to 1\n");
2208 func->function(cmd);
2216 // if it's a client command and no command was found, say so.
2217 if (cmd->source == src_client)
2219 Con_Printf("Client \"%s\" tried to execute \"%s\"\n", host_client->name, text);
2224 for (a=cmd->userdefined->alias ; a ; a=a->next)
2226 if (!strcasecmp (cmd->argv[0], a->name))
2228 Cmd_ExecuteAlias(cmd, a);
2234 if (!Cvar_Command(cmd) && host.framecount > 0)
2235 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(cmd, 0));
2237 cmd->cbuf->tokenizebufferpos = oldpos;
2239 Cbuf_Unlock(cmd->cbuf);
2246 Returns the position (1 to argc-1) in the command's argument list
2247 where the given parameter apears, or 0 if not present
2251 int Cmd_CheckParm (cmd_state_t *cmd, const char *parm)
2257 Con_Printf ("Cmd_CheckParm: NULL");
2261 for (i = 1; i < Cmd_Argc (cmd); i++)
2262 if (!strcasecmp (parm, Cmd_Argv(cmd, i)))
2270 void Cmd_SaveInitState(void)
2272 cmd_iter_t *cmd_iter;
2273 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2275 cmd_state_t *cmd = cmd_iter->cmd;
2278 for (f = cmd->userdefined->qc_functions; f; f = f->next)
2279 f->initstate = true;
2280 for (f = cmd->engine_functions; f; f = f->next)
2281 f->initstate = true;
2282 for (a = cmd->userdefined->alias; a; a = a->next)
2284 a->initstate = true;
2285 a->initialvalue = Mem_strdup(zonemempool, a->value);
2288 Cvar_SaveInitState(&cvars_all);
2291 void Cmd_RestoreInitState(void)
2293 cmd_iter_t *cmd_iter;
2294 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2296 cmd_state_t *cmd = cmd_iter->cmd;
2297 cmd_function_t *f, **fp;
2298 cmd_alias_t *a, **ap;
2299 for (fp = &cmd->userdefined->qc_functions; (f = *fp);)
2305 // destroy this command, it didn't exist at init
2306 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2311 for (fp = &cmd->engine_functions; (f = *fp);)
2317 // destroy this command, it didn't exist at init
2318 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2323 for (ap = &cmd->userdefined->alias; (a = *ap);)
2327 // restore this alias, it existed at init
2328 if (strcmp(a->value ? a->value : "", a->initialvalue ? a->initialvalue : ""))
2330 Con_DPrintf("Cmd_RestoreInitState: Restoring alias %s\n", a->name);
2333 a->value = Mem_strdup(zonemempool, a->initialvalue);
2339 // free this alias, it didn't exist at init...
2340 Con_DPrintf("Cmd_RestoreInitState: Destroying alias %s\n", a->name);
2348 Cvar_RestoreInitState(&cvars_all);