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_client;
26 cmd_state_t cmd_server;
27 cmd_state_t cmd_serverfromclient;
29 cmd_userdefined_t cmd_userdefined_all;
30 cmd_userdefined_t cmd_userdefined_null;
32 typedef struct cmd_iter_s {
37 static cmd_iter_t cmd_iter_all[] = {
40 {&cmd_serverfromclient},
45 // we only run the +whatever commandline arguments once
46 qbool host_stuffcmdsrun = false;
48 //=============================================================================
50 void Cbuf_Lock(cmd_buf_t *cbuf)
52 Thread_LockMutex(cbuf->lock);
55 void Cbuf_Unlock(cmd_buf_t *cbuf)
57 Thread_UnlockMutex(cbuf->lock);
65 Causes execution of the remainder of the command buffer to be delayed until
66 next frame. This allows commands like:
67 bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
70 static void Cmd_Wait_f (cmd_state_t *cmd)
72 cmd->cbuf->wait = true;
79 Cause a command to be executed after a delay.
82 static cmd_input_t *Cbuf_LinkGet(cmd_buf_t *cbuf, cmd_input_t *existing);
83 static void Cmd_Defer_f (cmd_state_t *cmd)
86 cmd_buf_t *cbuf = cmd->cbuf;
88 if(Cmd_Argc(cmd) == 1)
90 if(List_IsEmpty(&cbuf->deferred))
91 Con_Printf("No commands are pending.\n");
95 List_ForEach(pos, &cbuf->deferred)
97 current = List_Container(*pos, cmd_input_t, list);
98 Con_Printf("-> In %9.2f: %s\n", current->delay, current->text);
102 else if(Cmd_Argc(cmd) == 2 && !strcasecmp("clear", Cmd_Argv(cmd, 1)))
104 while(!List_IsEmpty(&cbuf->deferred))
105 List_Move_Tail(cbuf->deferred.next, &cbuf->free);
107 else if(Cmd_Argc(cmd) == 3)
109 const char *text = Cmd_Argv(cmd, 2);
110 current = Cbuf_LinkGet(cbuf, NULL);
111 current->size = strlen(text);
112 current->source = cmd;
113 current->delay = atof(Cmd_Argv(cmd, 1));
115 memcpy(current->text, text, current->size + 1);
117 List_Move_Tail(¤t->list, &cbuf->deferred);
122 Con_Printf("usage: defer <seconds> <command>\n"
132 Print something to the center of the screen using SCR_Centerprint
135 static void Cmd_Centerprint_f (cmd_state_t *cmd)
137 char msg[MAX_INPUTLINE];
138 unsigned int i, c, p;
142 strlcpy(msg, Cmd_Argv(cmd,1), sizeof(msg));
143 for(i = 2; i < c; ++i)
145 strlcat(msg, " ", sizeof(msg));
146 strlcat(msg, Cmd_Argv(cmd, i), sizeof(msg));
148 c = (unsigned int)strlen(msg);
149 for(p = 0, i = 0; i < c; ++i)
155 else if(msg[i+1] == '\\')
167 SCR_CenterPrint(msg);
172 =============================================================================
176 =============================================================================
179 static cmd_input_t *Cbuf_LinkGet(cmd_buf_t *cbuf, cmd_input_t *existing)
181 cmd_input_t *ret = NULL;
182 if(existing && existing->pending)
184 else if(!List_IsEmpty(&cbuf->free))
186 ret = List_Container(*cbuf->free.next, cmd_input_t, list);
188 ret->pending = false;
193 static cmd_input_t *Cmd_AllocInputNode(void)
195 cmd_input_t *node = (cmd_input_t *)Z_Malloc(sizeof(cmd_input_t));
196 node->list.prev = node->list.next = &node->list;
197 node->size = node->length = node->pending = 0;
201 static size_t Cmd_ParseInput (cmd_input_t **output, char **input)
203 size_t pos, cmdsize = 0, start = 0;
204 qbool command = false, lookahead = false;
205 qbool quotes = false, comment = false;
206 qbool escaped = false;
209 * The Quake command-line is super basic. It can be entered in the console
210 * or in config files. A semicolon is used to terminate a command and chain
211 * them together. Otherwise, a newline delineates command input.
213 * In most engines, the Quake command-line is a simple linear text buffer that
214 * is parsed when it executes. In Darkplaces, we use a linked list of command
215 * input and parse the input on the spot.
217 * This was done because Darkplaces allows multiple command interpreters on the
218 * same thread. Previously, each interpreter maintained its own buffer and this
219 * caused problems related to execution order, and maintaining a single simple
220 * buffer for all interpreters makes it non-trivial to keep track of which
221 * command should execute on which interpreter.
224 // Run until command and lookahead are both true, or until we run out of input.
225 for (pos = 0; (*input)[pos]; pos++)
227 // Look for newlines and semicolons. Ignore semicolons in quotes.
228 switch((*input)[pos])
236 if(!comment) // Not a newline so far. Still not a valid command yet.
238 if(!quotes && (*input)[pos] == ';') // Ignore semicolons in quotes.
240 else if (ISCOMMENT((*input), pos)) // Comments
255 switch((*input)[pos])
264 if (!escaped && quotes)
273 if(cmdsize && !command)
276 if(command && lookahead)
285 *output = Cmd_AllocInputNode();
287 if((*output)->pending)
288 offset = (*output)->length;
290 (*output)->length += cmdsize;
292 if((*output)->size < (*output)->length + 1)
294 (*output)->text = (char *)Mem_Realloc(tempmempool, (*output)->text, (*output)->size + cmdsize + 1);
295 (*output)->size = (*output)->length + 1;
298 strlcpy(&(*output)->text[offset], &(*input)[start], cmdsize + 1);
299 (*output)->pending = !lookahead;
302 // Set input to its new position. Can be NULL.
303 *input = &(*input)[pos];
308 // Cloudwalk: Not happy with this, but it works.
309 static void Cbuf_LinkCreate(cmd_state_t *cmd, llist_t *head, cmd_input_t *existing, const char *text)
311 char *in = (char *)&text[0];
312 cmd_buf_t *cbuf = cmd->cbuf;
313 size_t totalsize = 0, newsize = 0;
314 cmd_input_t *current = NULL;
316 // Slide the pointer down until we reach the end
319 current = Cbuf_LinkGet(cbuf, existing);
320 newsize = Cmd_ParseInput(¤t, &in);
325 if(current != existing)
327 current->source = cmd;
328 List_Move_Tail(¤t->list, head);
331 totalsize += newsize;
333 else if (current == existing && !totalsize)
334 current->pending = false;
338 cbuf->size += totalsize;
345 Adds command text at the end of the buffer
348 void Cbuf_AddText (cmd_state_t *cmd, const char *text)
350 size_t l = strlen(text);
351 cmd_buf_t *cbuf = cmd->cbuf;
352 llist_t llist = {&llist, &llist};
356 if (cbuf->maxsize - cbuf->size <= l)
357 Con_Print("Cbuf_AddText: overflow\n");
360 Cbuf_LinkCreate(cmd, &llist, (List_IsEmpty(&cbuf->start) ? NULL : List_Container(*cbuf->start.prev, cmd_input_t, list)), text);
361 if(!List_IsEmpty(&llist))
362 List_Splice_Tail(&llist, &cbuf->start);
371 Adds command text immediately after the current command
372 FIXME: actually change the command buffer to do less copying
375 void Cbuf_InsertText (cmd_state_t *cmd, const char *text)
377 cmd_buf_t *cbuf = cmd->cbuf;
378 llist_t llist = {&llist, &llist};
379 size_t l = strlen(text);
383 // we need to memmove the existing text and stuff this in before it...
384 if (cbuf->size + l >= (size_t)cbuf->maxsize)
385 Con_Print("Cbuf_InsertText: overflow\n");
388 Cbuf_LinkCreate(cmd, &llist, List_Container(*cbuf->start.next, cmd_input_t, list), text);
389 if(!List_IsEmpty(&llist))
390 List_Splice(&llist, &cbuf->start);
398 Cbuf_Execute_Deferred --blub
401 static void Cbuf_Execute_Deferred (cmd_buf_t *cbuf)
404 cmd_input_t *current;
407 if (host.realtime - cbuf->deferred_oldtime < 0 || host.realtime - cbuf->deferred_oldtime > 1800)
408 cbuf->deferred_oldtime = host.realtime;
409 eat = host.realtime - cbuf->deferred_oldtime;
410 if (eat < (1.0 / 120.0))
412 cbuf->deferred_oldtime = host.realtime;
414 List_ForEach(pos, &cbuf->deferred)
416 current = List_Container(*pos, cmd_input_t, list);
417 current->delay -= eat;
418 if(current->delay <= 0)
420 cbuf->size += current->size;
421 List_Move(pos, &cbuf->start);
422 // We must return and come back next frame or the engine will freeze. Fragile... like glass :3
433 static qbool Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmd_alias_t *alias );
434 void Cbuf_Execute (cmd_buf_t *cbuf)
436 cmd_input_t *current;
437 char preprocessed[MAX_INPUTLINE];
440 // LadyHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
441 cbuf->tokenizebufferpos = 0;
443 while (!List_IsEmpty(&cbuf->start))
446 * Delete the text from the command buffer and move remaining
447 * commands down. This is necessary because commands (exec, alias)
448 * can insert data at the beginning of the text buffer
450 current = List_Container(*cbuf->start.next, cmd_input_t, list);
452 // Recycle memory so using WASD doesn't cause a malloc and free
453 List_Move_Tail(¤t->list, &cbuf->free);
456 * Assume we're rolling with the current command-line and
457 * always set this false because alias expansion or cbuf insertion
458 * without a newline may set this true, and cause weirdness.
460 current->pending = false;
462 cbuf->size -= current->length;
464 firstchar = current->text;
465 while(*firstchar && ISWHITESPACE(*firstchar))
467 if((strncmp(firstchar, "alias", 5) || !ISWHITESPACE(firstchar[5])) &&
468 (strncmp(firstchar, "bind", 4) || !ISWHITESPACE(firstchar[4])) &&
469 (strncmp(firstchar, "in_bind", 7) || !ISWHITESPACE(firstchar[7])))
471 if(Cmd_PreprocessString(current->source, current->text, preprocessed, sizeof(preprocessed), NULL ))
472 Cmd_ExecuteString(current->source, preprocessed, src_local, false);
476 Cmd_ExecuteString (current->source, current->text, src_local, false);
484 * Skip out while text still remains in
485 * buffer, leaving it for next frame
493 void Cbuf_Frame(cmd_buf_t *cbuf)
495 Cbuf_Execute_Deferred(cbuf);
498 SV_LockThreadMutex();
500 SV_UnlockThreadMutex();
505 ==============================================================================
509 ==============================================================================
516 Adds command line parameters as script statements
517 Commands lead with a +, and continue until a - or another +
518 quake +prog jctest.qp +cmd amlev1
519 quake -nosound +cmd amlev1
522 static void Cmd_StuffCmds_f (cmd_state_t *cmd)
525 // this is for all commandline options combined (and is bounds checked)
526 char build[MAX_INPUTLINE];
528 if (Cmd_Argc (cmd) != 1)
530 Con_Print("stuffcmds : execute command line parameters\n");
534 // no reason to run the commandline arguments twice
535 if (host_stuffcmdsrun)
538 host_stuffcmdsrun = true;
541 for (i = 0;i < sys.argc;i++)
543 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)
546 while (sys.argv[i][j])
547 build[l++] = sys.argv[i][j++];
549 for (;i < sys.argc;i++)
553 if ((sys.argv[i][0] == '+' || sys.argv[i][0] == '-') && (sys.argv[i][1] < '0' || sys.argv[i][1] > '9'))
555 if (l + strlen(sys.argv[i]) + 4 > sizeof(build) - 1)
558 if (strchr(sys.argv[i], ' '))
560 for (j = 0;sys.argv[i][j];j++)
561 build[l++] = sys.argv[i][j];
562 if (strchr(sys.argv[i], ' '))
569 // now terminate the combined string and prepend it to the command buffer
570 // we already reserved space for the terminator
572 Cbuf_InsertText (cmd, build);
575 static void Cmd_Exec(cmd_state_t *cmd, const char *filename)
578 size_t filenameLen = strlen(filename);
580 !strcmp(filename, "default.cfg") ||
581 (filenameLen >= 12 && !strcmp(filename + filenameLen - 12, "/default.cfg"));
583 if (!strcmp(filename, "config.cfg"))
585 filename = CONFIGFILENAME;
586 if (Sys_CheckParm("-noconfig"))
587 return; // don't execute config.cfg
590 f = (char *)FS_LoadFile (filename, tempmempool, false, NULL);
593 Con_Printf("couldn't exec %s\n",filename);
596 Con_Printf("execing %s\n",filename);
598 // if executing default.cfg for the first time, lock the cvar defaults
599 // it may seem backwards to insert this text BEFORE the default.cfg
600 // but Cbuf_InsertText inserts before, so this actually ends up after it.
602 Cbuf_InsertText(cmd, "\ncvar_lockdefaults\n");
604 Cbuf_InsertText (cmd, f);
609 // special defaults for specific games go here, these execute before default.cfg
610 // Nehahra pushable crates malfunction in some levels if this is on
611 // Nehahra NPC AI is confused by blowupfallenzombies
615 Cbuf_InsertText(cmd, "\n"
616 "sv_gameplayfix_blowupfallenzombies 0\n"
617 "sv_gameplayfix_findradiusdistancetobox 0\n"
618 "sv_gameplayfix_grenadebouncedownslopes 0\n"
619 "sv_gameplayfix_slidemoveprojectiles 0\n"
620 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
621 "sv_gameplayfix_setmodelrealbox 0\n"
622 "sv_gameplayfix_droptofloorstartsolid 0\n"
623 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
624 "sv_gameplayfix_noairborncorpse 0\n"
625 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
626 "sv_gameplayfix_easierwaterjump 0\n"
627 "sv_gameplayfix_delayprojectiles 0\n"
628 "sv_gameplayfix_multiplethinksperframe 0\n"
629 "sv_gameplayfix_fixedcheckwatertransition 0\n"
630 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
631 "sv_gameplayfix_swiminbmodels 0\n"
632 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
633 "sys_ticrate 0.01388889\n"
635 "r_shadow_bumpscale_basetexture 0\n"
636 "csqc_polygons_defaultmaterial_nocullface 0\n"
640 Cbuf_InsertText(cmd, "\n"
641 "sv_gameplayfix_blowupfallenzombies 0\n"
642 "sv_gameplayfix_findradiusdistancetobox 0\n"
643 "sv_gameplayfix_grenadebouncedownslopes 0\n"
644 "sv_gameplayfix_slidemoveprojectiles 0\n"
645 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
646 "sv_gameplayfix_setmodelrealbox 0\n"
647 "sv_gameplayfix_droptofloorstartsolid 0\n"
648 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
649 "sv_gameplayfix_noairborncorpse 0\n"
650 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
651 "sv_gameplayfix_easierwaterjump 0\n"
652 "sv_gameplayfix_delayprojectiles 0\n"
653 "sv_gameplayfix_multiplethinksperframe 0\n"
654 "sv_gameplayfix_fixedcheckwatertransition 0\n"
655 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
656 "sv_gameplayfix_swiminbmodels 0\n"
657 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
658 "sys_ticrate 0.01388889\n"
660 "r_shadow_bumpscale_basetexture 0\n"
661 "csqc_polygons_defaultmaterial_nocullface 0\n"
664 // 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.
665 // 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
666 // hipnotic mission pack has issues in their proximity mine sticking code, which causes them to bounce off.
669 Cbuf_InsertText(cmd, "\n"
670 "sv_gameplayfix_blowupfallenzombies 0\n"
671 "sv_gameplayfix_findradiusdistancetobox 0\n"
672 "sv_gameplayfix_grenadebouncedownslopes 0\n"
673 "sv_gameplayfix_slidemoveprojectiles 0\n"
674 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
675 "sv_gameplayfix_setmodelrealbox 0\n"
676 "sv_gameplayfix_droptofloorstartsolid 0\n"
677 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
678 "sv_gameplayfix_noairborncorpse 0\n"
679 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
680 "sv_gameplayfix_easierwaterjump 0\n"
681 "sv_gameplayfix_delayprojectiles 0\n"
682 "sv_gameplayfix_multiplethinksperframe 0\n"
683 "sv_gameplayfix_fixedcheckwatertransition 0\n"
684 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
685 "sv_gameplayfix_swiminbmodels 0\n"
686 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
689 "r_shadow_bumpscale_basetexture 0\n"
690 "csqc_polygons_defaultmaterial_nocullface 0\n"
693 // rogue mission pack has a guardian boss that does not wake up if findradius returns one of the entities around its spawn area
695 Cbuf_InsertText(cmd, "\n"
696 "sv_gameplayfix_blowupfallenzombies 0\n"
697 "sv_gameplayfix_findradiusdistancetobox 0\n"
698 "sv_gameplayfix_grenadebouncedownslopes 0\n"
699 "sv_gameplayfix_slidemoveprojectiles 0\n"
700 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
701 "sv_gameplayfix_setmodelrealbox 0\n"
702 "sv_gameplayfix_droptofloorstartsolid 0\n"
703 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
704 "sv_gameplayfix_noairborncorpse 0\n"
705 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
706 "sv_gameplayfix_easierwaterjump 0\n"
707 "sv_gameplayfix_delayprojectiles 0\n"
708 "sv_gameplayfix_multiplethinksperframe 0\n"
709 "sv_gameplayfix_fixedcheckwatertransition 0\n"
710 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
711 "sv_gameplayfix_swiminbmodels 0\n"
712 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
713 "sys_ticrate 0.01388889\n"
715 "r_shadow_bumpscale_basetexture 0\n"
716 "csqc_polygons_defaultmaterial_nocullface 0\n"
720 Cbuf_InsertText(cmd, "\n"
721 "sv_gameplayfix_blowupfallenzombies 0\n"
722 "sv_gameplayfix_findradiusdistancetobox 0\n"
723 "sv_gameplayfix_grenadebouncedownslopes 0\n"
724 "sv_gameplayfix_slidemoveprojectiles 0\n"
725 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
726 "sv_gameplayfix_setmodelrealbox 0\n"
727 "sv_gameplayfix_droptofloorstartsolid 0\n"
728 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
729 "sv_gameplayfix_noairborncorpse 0\n"
730 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
731 "sv_gameplayfix_easierwaterjump 0\n"
732 "sv_gameplayfix_delayprojectiles 0\n"
733 "sv_gameplayfix_multiplethinksperframe 0\n"
734 "sv_gameplayfix_fixedcheckwatertransition 0\n"
735 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
736 "sv_gameplayfix_swiminbmodels 0\n"
737 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
738 "sys_ticrate 0.01388889\n"
740 "r_shadow_bumpscale_basetexture 4\n"
741 "csqc_polygons_defaultmaterial_nocullface 0\n"
745 Cbuf_InsertText(cmd, "\n"
746 "sv_gameplayfix_blowupfallenzombies 1\n"
747 "sv_gameplayfix_findradiusdistancetobox 1\n"
748 "sv_gameplayfix_grenadebouncedownslopes 1\n"
749 "sv_gameplayfix_slidemoveprojectiles 1\n"
750 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
751 "sv_gameplayfix_setmodelrealbox 1\n"
752 "sv_gameplayfix_droptofloorstartsolid 1\n"
753 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
754 "sv_gameplayfix_noairborncorpse 1\n"
755 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
756 "sv_gameplayfix_easierwaterjump 1\n"
757 "sv_gameplayfix_delayprojectiles 1\n"
758 "sv_gameplayfix_multiplethinksperframe 1\n"
759 "sv_gameplayfix_fixedcheckwatertransition 1\n"
760 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
761 "sv_gameplayfix_swiminbmodels 1\n"
762 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
763 "sys_ticrate 0.01388889\n"
764 "sv_gameplayfix_q2airaccelerate 1\n"
765 "sv_gameplayfix_stepmultipletimes 1\n"
766 "csqc_polygons_defaultmaterial_nocullface 1\n"
767 "con_chatsound_team_mask 13\n"
771 case GAME_VORETOURNAMENT:
772 // 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
773 Cbuf_InsertText(cmd, "\n"
774 "csqc_polygons_defaultmaterial_nocullface 1\n"
775 "con_chatsound_team_mask 13\n"
776 "sv_gameplayfix_customstats 1\n"
779 // Steel Storm: Burning Retribution csqc misinterprets CSQC_InputEvent if type is a value other than 0 or 1
780 case GAME_STEELSTORM:
781 Cbuf_InsertText(cmd, "\n"
782 "sv_gameplayfix_blowupfallenzombies 1\n"
783 "sv_gameplayfix_findradiusdistancetobox 1\n"
784 "sv_gameplayfix_grenadebouncedownslopes 1\n"
785 "sv_gameplayfix_slidemoveprojectiles 1\n"
786 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
787 "sv_gameplayfix_setmodelrealbox 1\n"
788 "sv_gameplayfix_droptofloorstartsolid 1\n"
789 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
790 "sv_gameplayfix_noairborncorpse 1\n"
791 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
792 "sv_gameplayfix_easierwaterjump 1\n"
793 "sv_gameplayfix_delayprojectiles 1\n"
794 "sv_gameplayfix_multiplethinksperframe 1\n"
795 "sv_gameplayfix_fixedcheckwatertransition 1\n"
796 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
797 "sv_gameplayfix_swiminbmodels 1\n"
798 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
799 "sys_ticrate 0.01388889\n"
800 "cl_csqc_generatemousemoveevents 0\n"
801 "csqc_polygons_defaultmaterial_nocullface 1\n"
805 Cbuf_InsertText(cmd, "\n"
806 "sv_gameplayfix_blowupfallenzombies 1\n"
807 "sv_gameplayfix_findradiusdistancetobox 1\n"
808 "sv_gameplayfix_grenadebouncedownslopes 1\n"
809 "sv_gameplayfix_slidemoveprojectiles 1\n"
810 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
811 "sv_gameplayfix_setmodelrealbox 1\n"
812 "sv_gameplayfix_droptofloorstartsolid 1\n"
813 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
814 "sv_gameplayfix_noairborncorpse 1\n"
815 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
816 "sv_gameplayfix_easierwaterjump 1\n"
817 "sv_gameplayfix_delayprojectiles 1\n"
818 "sv_gameplayfix_multiplethinksperframe 1\n"
819 "sv_gameplayfix_fixedcheckwatertransition 1\n"
820 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
821 "sv_gameplayfix_swiminbmodels 1\n"
822 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
823 "sys_ticrate 0.01388889\n"
824 "csqc_polygons_defaultmaterial_nocullface 0\n"
836 static void Cmd_Exec_f (cmd_state_t *cmd)
841 if (Cmd_Argc(cmd) != 2)
843 Con_Print("exec <filename> : execute a script file\n");
847 s = FS_Search(Cmd_Argv(cmd, 1), true, true, NULL);
848 if(!s || !s->numfilenames)
850 Con_Printf("couldn't exec %s\n",Cmd_Argv(cmd, 1));
854 for(i = 0; i < s->numfilenames; ++i)
855 Cmd_Exec(cmd, s->filenames[i]);
865 Just prints the rest of the line to the console
868 static void Cmd_Echo_f (cmd_state_t *cmd)
872 for (i=1 ; i<Cmd_Argc(cmd) ; i++)
873 Con_Printf("%s ",Cmd_Argv(cmd, i));
878 // Support Doom3-style Toggle Console Command
883 Toggles a specified console variable amongst the values specified (default is 0 and 1)
886 static void Cmd_Toggle_f(cmd_state_t *cmd)
888 // Acquire Number of Arguments
889 int nNumArgs = Cmd_Argc(cmd);
892 // No Arguments Specified; Print Usage
893 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");
895 { // Correct Arguments Specified
896 // Acquire Potential CVar
897 cvar_t* cvCVar = Cvar_FindVar(cmd->cvars, Cmd_Argv(cmd, 1), cmd->cvars_flagsmask);
904 Cvar_SetValueQuick(cvCVar, 0);
906 Cvar_SetValueQuick(cvCVar, 1);
910 { // 0 and Specified Usage
911 if(cvCVar->integer == atoi(Cmd_Argv(cmd, 2) ) )
912 // CVar is Specified Value; // Reset to 0
913 Cvar_SetValueQuick(cvCVar, 0);
915 if(cvCVar->integer == 0)
916 // CVar is 0; Specify Value
917 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
919 // CVar does not match; Reset to 0
920 Cvar_SetValueQuick(cvCVar, 0);
923 { // Variable Values Specified
927 for(nCnt = 2; nCnt < nNumArgs; nCnt++)
928 { // Cycle through Values
929 if( strcmp(cvCVar->string, Cmd_Argv(cmd, nCnt) ) == 0)
930 { // Current Value Located; Increment to Next
931 if( (nCnt + 1) == nNumArgs)
932 // Max Value Reached; Reset
933 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
936 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, nCnt + 1) );
945 // Value not Found; Reset to Original
946 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
952 Con_Printf("ERROR : CVar '%s' not found\n", Cmd_Argv(cmd, 1) );
961 Creates a new command that executes a command string (possibly ; seperated)
964 static void Cmd_Alias_f (cmd_state_t *cmd)
967 char line[MAX_INPUTLINE];
972 if (Cmd_Argc(cmd) == 1)
974 Con_Print("Current alias commands:\n");
975 for (a = cmd->userdefined->alias ; a ; a=a->next)
976 Con_Printf("%s : %s", a->name, a->value);
980 s = Cmd_Argv(cmd, 1);
981 if (strlen(s) >= MAX_ALIAS_NAME)
983 Con_Print("Alias name is too long\n");
987 // if the alias already exists, reuse it
988 for (a = cmd->userdefined->alias ; a ; a=a->next)
990 if (!strcmp(s, a->name))
999 cmd_alias_t *prev, *current;
1001 a = (cmd_alias_t *)Z_Malloc (sizeof(cmd_alias_t));
1002 strlcpy (a->name, s, sizeof (a->name));
1003 // insert it at the right alphanumeric position
1004 for( prev = NULL, current = cmd->userdefined->alias ; current && strcmp( current->name, a->name ) < 0 ; prev = current, current = current->next )
1009 cmd->userdefined->alias = a;
1015 // copy the rest of the command line
1016 line[0] = 0; // start out with a null string
1018 for (i=2 ; i < c ; i++)
1021 strlcat (line, " ", sizeof (line));
1022 strlcat (line, Cmd_Argv(cmd, i), sizeof (line));
1024 strlcat (line, "\n", sizeof (line));
1026 alloclen = strlen (line) + 1;
1028 line[alloclen - 2] = '\n'; // to make sure a newline is appended even if too long
1029 a->value = (char *)Z_Malloc (alloclen);
1030 memcpy (a->value, line, alloclen);
1037 Remove existing aliases.
1040 static void Cmd_UnAlias_f (cmd_state_t *cmd)
1046 if(Cmd_Argc(cmd) == 1)
1048 Con_Print("unalias: Usage: unalias alias1 [alias2 ...]\n");
1052 for(i = 1; i < Cmd_Argc(cmd); ++i)
1054 s = Cmd_Argv(cmd, i);
1056 for(a = cmd->userdefined->alias; a; p = a, a = a->next)
1058 if(!strcmp(s, a->name))
1060 if (a->initstate) // we can not remove init aliases
1062 if(a == cmd->userdefined->alias)
1063 cmd->userdefined->alias = a->next;
1072 Con_Printf("unalias: %s alias not found\n", s);
1077 =============================================================================
1081 =============================================================================
1084 static const char *Cmd_GetDirectCvarValue(cmd_state_t *cmd, const char *varname, cmd_alias_t *alias, qbool *is_multiple)
1089 static char vabuf[1024]; // cmd_mutex
1092 *is_multiple = false;
1094 if(!varname || !*varname)
1099 if(!strcmp(varname, "*"))
1102 *is_multiple = true;
1103 return Cmd_Args(cmd);
1105 else if(!strcmp(varname, "#"))
1107 return va(vabuf, sizeof(vabuf), "%d", Cmd_Argc(cmd));
1109 else if(varname[strlen(varname) - 1] == '-')
1111 argno = strtol(varname, &endptr, 10);
1112 if(endptr == varname + strlen(varname) - 1)
1114 // whole string is a number, apart from the -
1115 const char *p = Cmd_Args(cmd);
1116 for(; argno > 1; --argno)
1117 if(!COM_ParseToken_Console(&p))
1122 *is_multiple = true;
1124 // kill pre-argument whitespace
1125 for (;*p && ISWHITESPACE(*p);p++)
1134 argno = strtol(varname, &endptr, 10);
1137 // whole string is a number
1138 // NOTE: we already made sure we don't have an empty cvar name!
1139 if(argno >= 0 && argno < Cmd_Argc(cmd))
1140 return Cmd_Argv(cmd, argno);
1145 if((cvar = Cvar_FindVar(cmd->cvars, varname, cmd->cvars_flagsmask)) && !(cvar->flags & CF_PRIVATE))
1146 return cvar->string;
1151 qbool Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset, qbool putquotes)
1153 qbool quote_quot = !!strchr(quoteset, '"');
1154 qbool quote_backslash = !!strchr(quoteset, '\\');
1155 qbool quote_dollar = !!strchr(quoteset, '$');
1164 *out++ = '"'; --outlen;
1170 if(*in == '"' && quote_quot)
1174 *out++ = '\\'; --outlen;
1175 *out++ = '"'; --outlen;
1177 else if(*in == '\\' && quote_backslash)
1181 *out++ = '\\'; --outlen;
1182 *out++ = '\\'; --outlen;
1184 else if(*in == '$' && quote_dollar)
1188 *out++ = '$'; --outlen;
1189 *out++ = '$'; --outlen;
1195 *out++ = *in; --outlen;
1210 static const char *Cmd_GetCvarValue(cmd_state_t *cmd, const char *var, size_t varlen, cmd_alias_t *alias)
1212 static char varname[MAX_INPUTLINE]; // cmd_mutex
1213 static char varval[MAX_INPUTLINE]; // cmd_mutex
1214 const char *varstr = NULL;
1216 qbool required = false;
1217 qbool optional = false;
1218 static char asis[] = "asis"; // just to suppress const char warnings
1220 if(varlen >= MAX_INPUTLINE)
1221 varlen = MAX_INPUTLINE - 1;
1222 memcpy(varname, var, varlen);
1223 varname[varlen] = 0;
1224 varfunc = strchr(varname, ' ');
1236 Con_Printf(CON_WARN "Warning: Could not expand $ in alias %s\n", alias->name);
1238 Con_Printf(CON_WARN "Warning: Could not expand $\n");
1246 while((p = strchr(varfunc, '?')))
1249 memmove(p, p+1, strlen(p)); // with final NUL
1252 while((p = strchr(varfunc, '!')))
1255 memmove(p, p+1, strlen(p)); // with final NUL
1258 while((p = strchr(varfunc, ' ')))
1260 memmove(p, p+1, strlen(p)); // with final NUL
1262 // if no function is left, NULL it
1267 if(varname[0] == '$')
1268 varstr = Cmd_GetDirectCvarValue(cmd, Cmd_GetDirectCvarValue(cmd, varname + 1, alias, NULL), alias, NULL);
1271 qbool is_multiple = false;
1272 // Exception: $* and $n- don't use the quoted form by default
1273 varstr = Cmd_GetDirectCvarValue(cmd, varname, alias, &is_multiple);
1284 Con_Printf(CON_ERROR "Error: Could not expand $%s in alias %s\n", varname, alias->name);
1286 Con_Printf(CON_ERROR "Error: Could not expand $%s\n", varname);
1296 Con_Printf(CON_WARN "Warning: Could not expand $%s in alias %s\n", varname, alias->name);
1298 Con_Printf(CON_WARN "Warning: Could not expand $%s\n", varname);
1299 dpsnprintf(varval, sizeof(varval), "$%s", varname);
1304 if(!varfunc || !strcmp(varfunc, "q")) // note: quoted form is default, use "asis" to override!
1306 // quote it so it can be used inside double quotes
1307 // we just need to replace " by \", and of course, double backslashes
1308 Cmd_QuoteString(varval, sizeof(varval), varstr, "\"\\", false);
1311 else if(!strcmp(varfunc, "asis"))
1316 Con_Printf("Unknown variable function %s\n", varfunc);
1322 Cmd_PreprocessString
1324 Preprocesses strings and replaces $*, $param#, $cvar accordingly. Also strips comments.
1326 static qbool Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmd_alias_t *alias ) {
1332 // don't crash if there's no room in the outtext buffer
1333 if( maxoutlen == 0 ) {
1336 maxoutlen--; // because of \0
1341 while( *in && outlen < maxoutlen ) {
1343 // this is some kind of expansion, see what comes after the $
1346 // The console does the following preprocessing:
1348 // - $$ is transformed to a single dollar sign.
1349 // - $var or ${var} are expanded to the contents of the named cvar,
1350 // with quotation marks and backslashes quoted so it can safely
1351 // be used inside quotation marks (and it should always be used
1353 // - ${var asis} inserts the cvar value as is, without doing this
1355 // - ${var ?} silently expands to the empty string if
1356 // $var does not exist
1357 // - ${var !} fails expansion and executes nothing if
1358 // $var does not exist
1359 // - prefix the cvar name with a dollar sign to do indirection;
1360 // for example, if $x has the value timelimit, ${$x} will return
1361 // the value of $timelimit
1362 // - when expanding an alias, the special variable name $* refers
1363 // to all alias parameters, and a number refers to that numbered
1364 // alias parameter, where the name of the alias is $0, the first
1365 // parameter is $1 and so on; as a special case, $* inserts all
1366 // parameters, without extra quoting, so one can use $* to just
1367 // pass all parameters around. All parameters starting from $n
1368 // can be referred to as $n- (so $* is equivalent to $1-).
1369 // - ${* q} and ${n- q} force quoting anyway
1371 // Note: when expanding an alias, cvar expansion is done in the SAME step
1372 // as alias expansion so that alias parameters or cvar values containing
1373 // dollar signs have no unwanted bad side effects. However, this needs to
1374 // be accounted for when writing complex aliases. For example,
1375 // alias foo "set x NEW; echo $x"
1376 // actually expands to
1377 // "set x NEW; echo OLD"
1378 // and will print OLD! To work around this, use a second alias:
1379 // alias foo "set x NEW; foo2"
1380 // alias foo2 "echo $x"
1382 // Also note: lines starting with alias are exempt from cvar expansion.
1383 // If you want cvar expansion, write "alias" instead:
1386 // alias foo "echo $x"
1387 // "alias" bar "echo $x"
1390 // foo will print 2, because the variable $x will be expanded when the alias
1391 // gets expanded. bar will print 1, because the variable $x was expanded
1392 // at definition time. foo can be equivalently defined as
1394 // "alias" foo "echo $$x"
1396 // because at definition time, $$ will get replaced to a single $.
1401 } else if(*in == '{') {
1402 varlen = strcspn(in + 1, "}");
1403 if(in[varlen + 1] == '}')
1405 val = Cmd_GetCvarValue(cmd, in + 1, varlen, alias);
1417 varlen = strspn(in, "#*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-");
1418 val = Cmd_GetCvarValue(cmd, in, varlen, alias);
1425 // insert the cvar value
1426 while(*val && outlen < maxoutlen)
1427 outtext[outlen++] = *val++;
1432 // copy the unexpanded text
1433 outtext[outlen++] = '$';
1434 while(eat && outlen < maxoutlen)
1436 outtext[outlen++] = *in++;
1442 outtext[outlen++] = *in++;
1444 outtext[outlen] = 0;
1452 Called for aliases and fills in the alias into the cbuffer
1455 static void Cmd_ExecuteAlias (cmd_state_t *cmd, cmd_alias_t *alias)
1457 static char buffer[ MAX_INPUTLINE ]; // cmd_mutex
1458 static char buffer2[ MAX_INPUTLINE ]; // cmd_mutex
1459 qbool ret = Cmd_PreprocessString( cmd, alias->value, buffer, sizeof(buffer) - 2, alias );
1462 // insert at start of command buffer, so that aliases execute in order
1463 // (fixes bug introduced by Black on 20050705)
1465 // Note: Cbuf_PreprocessString will be called on this string AGAIN! So we
1466 // have to make sure that no second variable expansion takes place, otherwise
1467 // alias parameters containing dollar signs can have bad effects.
1468 Cmd_QuoteString(buffer2, sizeof(buffer2), buffer, "$", false);
1469 Cbuf_InsertText(cmd, buffer2);
1476 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
1477 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
1481 static void Cmd_List_f (cmd_state_t *cmd)
1483 cmd_function_t *func;
1484 const char *partial;
1489 if (Cmd_Argc(cmd) > 1)
1491 partial = Cmd_Argv(cmd, 1);
1492 len = strlen(partial);
1493 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1503 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1505 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1507 Con_Printf("%s : %s\n", func->name, func->description);
1510 for (func = cmd->engine_functions; func; func = func->next)
1512 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1514 Con_Printf("%s : %s\n", func->name, func->description);
1521 Con_Printf("%i Command%s matching \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1523 Con_Printf("%i Command%s beginning with \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1526 Con_Printf("%i Command%s\n\n", count, (count > 1) ? "s" : "");
1529 static void Cmd_Apropos_f(cmd_state_t *cmd)
1531 cmd_function_t *func;
1534 const char *partial;
1539 if (Cmd_Argc(cmd) > 1)
1540 partial = Cmd_Args(cmd);
1543 Con_Printf("usage: %s <string>\n",Cmd_Argv(cmd, 0));
1547 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
1549 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
1552 for (cvar = cmd->cvars->vars; cvar; cvar = cvar->next)
1554 if (matchpattern_with_separator(cvar->name, partial, true, "", false) ||
1555 matchpattern_with_separator(cvar->description, partial, true, "", false))
1557 Con_Printf ("cvar ");
1558 Cvar_PrintHelp(cvar, cvar->name, true);
1561 for (int i = 0; i < cvar->aliasindex; i++)
1563 if (matchpattern_with_separator(cvar->aliases[i], partial, true, "", false))
1565 Con_Printf ("cvar ");
1566 Cvar_PrintHelp(cvar, cvar->aliases[i], true);
1571 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1573 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1574 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1576 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1579 for (func = cmd->engine_functions; func; func = func->next)
1581 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1582 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1584 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1587 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
1589 // procede here a bit differently as an alias value always got a final \n
1590 if (!matchpattern_with_separator(alias->name, partial, true, "", false))
1591 if (!matchpattern_with_separator(alias->value, partial, true, "\n", false)) // when \n is as separator wildcards don't match it
1593 Con_Printf("alias ^5%s^7: %s", alias->name, alias->value); // do not print an extra \n
1596 Con_Printf("%i result%s\n\n", count, (count > 1) ? "s" : "");
1606 cmd_iter_t *cmd_iter;
1607 cmd_buf_t *cbuf = (cmd_buf_t *)Z_Malloc(sizeof(cmd_buf_t));
1608 cbuf->maxsize = 655360;
1609 cbuf->lock = Thread_CreateMutex();
1613 cbuf->start.prev = cbuf->start.next = &(cbuf->start);
1614 cbuf->deferred.prev = cbuf->deferred.next = &(cbuf->deferred);
1615 cbuf->free.prev = cbuf->free.next = &(cbuf->free);
1617 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
1619 cmd_state_t *cmd = cmd_iter->cmd;
1620 cmd->mempool = Mem_AllocPool("commands", 0, NULL);
1621 // space for commands and script files
1623 cmd->null_string = "";
1625 // client console can see server cvars because the user may start a server
1626 cmd_client.cvars = &cvars_all;
1627 cmd_client.cvars_flagsmask = CF_CLIENT | CF_SERVER;
1628 cmd_client.cmd_flags = CF_CLIENT | CF_CLIENT_FROM_SERVER;
1629 cmd_client.auto_flags = CF_SERVER_FROM_CLIENT;
1630 cmd_client.auto_function = CL_ForwardToServer_f; // FIXME: Move this to the client.
1631 cmd_client.userdefined = &cmd_userdefined_all;
1632 // dedicated server console can only see server cvars, there is no client
1633 cmd_server.cvars = &cvars_all;
1634 cmd_server.cvars_flagsmask = CF_SERVER;
1635 cmd_server.cmd_flags = CF_SERVER;
1636 cmd_server.auto_flags = 0;
1637 cmd_server.auto_function = NULL;
1638 cmd_server.userdefined = &cmd_userdefined_all;
1639 // server commands received from clients have no reason to access cvars, cvar expansion seems perilous.
1640 cmd_serverfromclient.cvars = &cvars_null;
1641 cmd_serverfromclient.cvars_flagsmask = 0;
1642 cmd_serverfromclient.cmd_flags = CF_SERVER_FROM_CLIENT | CF_USERINFO;
1643 cmd_serverfromclient.auto_flags = 0;
1644 cmd_serverfromclient.auto_function = NULL;
1645 cmd_serverfromclient.userdefined = &cmd_userdefined_null;
1648 // register our commands
1650 // client-only commands
1651 Cmd_AddCommand(CF_SHARED, "wait", Cmd_Wait_f, "make script execution wait for next rendered frame");
1652 Cmd_AddCommand(CF_CLIENT, "cprint", Cmd_Centerprint_f, "print something at the screen center");
1654 // maintenance commands used for upkeep of cvars and saved configs
1655 Cmd_AddCommand(CF_SHARED, "stuffcmds", Cmd_StuffCmds_f, "execute commandline parameters (must be present in quake.rc script)");
1656 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");
1657 Cmd_AddCommand(CF_SHARED, "cvar_resettodefaults_all", Cvar_ResetToDefaults_All_f, "sets all cvars to their locked default values");
1658 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)");
1659 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)");
1661 // general console commands used in multiple environments
1662 Cmd_AddCommand(CF_SHARED, "exec", Cmd_Exec_f, "execute a script file");
1663 Cmd_AddCommand(CF_SHARED, "echo",Cmd_Echo_f, "print a message to the console (useful in scripts)");
1664 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");
1665 Cmd_AddCommand(CF_SHARED, "unalias",Cmd_UnAlias_f, "remove an alias");
1666 Cmd_AddCommand(CF_SHARED, "set", Cvar_Set_f, "create or change the value of a console variable");
1667 Cmd_AddCommand(CF_SHARED, "seta", Cvar_SetA_f, "create or change the value of a console variable that will be saved to config.cfg");
1668 Cmd_AddCommand(CF_SHARED, "unset", Cvar_Del_f, "delete a cvar (does not work for static ones like _cl_name, or read-only ones)");
1670 #ifdef FILLALLCVARSWITHRUBBISH
1671 Cmd_AddCommand(CF_SHARED, "fillallcvarswithrubbish", Cvar_FillAll_f, "fill all cvars with a specified number of characters to provoke buffer overruns");
1672 #endif /* FILLALLCVARSWITHRUBBISH */
1674 // 2000-01-09 CmdList, CvarList commands By Matthias "Maddes" Buecher
1675 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
1676 Cmd_AddCommand(CF_SHARED, "cmdlist", Cmd_List_f, "lists all console commands beginning with the specified prefix or matching the specified wildcard pattern");
1677 Cmd_AddCommand(CF_SHARED, "cvarlist", Cvar_List_f, "lists all console variables beginning with the specified prefix or matching the specified wildcard pattern");
1678 Cmd_AddCommand(CF_SHARED, "apropos", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1679 Cmd_AddCommand(CF_SHARED, "find", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1681 Cmd_AddCommand(CF_SHARED, "defer", Cmd_Defer_f, "execute a command in the future");
1684 // Support Doom3-style Toggle Command
1685 Cmd_AddCommand(CF_SHARED | CF_CLIENT_FROM_SERVER, "toggle", Cmd_Toggle_f, "toggles a console variable's values (use for more info)");
1693 void Cmd_Shutdown(void)
1695 cmd_iter_t *cmd_iter;
1696 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
1698 cmd_state_t *cmd = cmd_iter->cmd;
1700 if (cmd->cbuf->lock)
1702 // we usually have this locked when we get here from Host_Quit_f
1703 Cbuf_Unlock(cmd->cbuf);
1706 Mem_FreePool(&cmd->mempool);
1715 int Cmd_Argc (cmd_state_t *cmd)
1725 const char *Cmd_Argv(cmd_state_t *cmd, int arg)
1727 if (arg >= cmd->argc )
1728 return cmd->null_string;
1729 return cmd->argv[arg];
1737 const char *Cmd_Args (cmd_state_t *cmd)
1746 Parses the given string into command line tokens.
1749 // AK: This function should only be called from ExcuteString because the current design is a bit of an hack
1750 static void Cmd_TokenizeString (cmd_state_t *cmd, const char *text)
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')
1782 if (!COM_ParseToken_Console(&text))
1785 if (cmd->argc < MAX_ARGS)
1787 l = (int)strlen(com_token) + 1;
1788 if (cmd->cbuf->tokenizebufferpos + l > CMD_TOKENIZELENGTH)
1790 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguments\n", CMD_TOKENIZELENGTH);
1793 memcpy (cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos, com_token, l);
1794 cmd->argv[cmd->argc] = cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos;
1795 cmd->cbuf->tokenizebufferpos += l;
1807 void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const char *description)
1809 cmd_function_t *func;
1810 cmd_function_t *prev, *current;
1812 xcommand_t save = NULL;
1813 qbool auto_add = false;
1816 for (i = 0; i < 3; i++)
1818 cmd = cmd_iter_all[i].cmd;
1819 if ((flags & cmd->cmd_flags) || (flags & cmd->auto_flags))
1821 if((flags & cmd->auto_flags) && cmd->auto_function)
1824 function = cmd->auto_function;
1828 // fail if the command is a variable name
1829 if (Cvar_FindVar(cmd->cvars, cmd_name, ~0))
1831 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
1837 // fail if the command already exists in this interpreter
1838 for (func = cmd->engine_functions; func; func = func->next)
1840 if (!strcmp(cmd_name, func->name))
1842 if(func->autofunc && !auto_add)
1844 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
1849 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1850 func->flags = flags;
1851 func->name = cmd_name;
1852 func->function = function;
1853 func->description = description;
1854 func->next = cmd->engine_functions;
1855 func->autofunc = auto_add;
1857 // insert it at the right alphanumeric position
1858 for (prev = NULL, current = cmd->engine_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1864 cmd->engine_functions = func;
1866 func->next = current;
1870 // mark csqcfunc if the function already exists in the csqc_functions list
1871 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1873 if (!strcmp(cmd_name, func->name))
1875 func->csqcfunc = true; //[515]: csqc
1881 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1882 func->name = cmd_name;
1883 func->function = function;
1884 func->description = description;
1885 func->csqcfunc = true; //[515]: csqc
1886 func->next = cmd->userdefined->csqc_functions;
1887 func->autofunc = false;
1889 // insert it at the right alphanumeric position
1890 for (prev = NULL, current = cmd->userdefined->csqc_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1896 cmd->userdefined->csqc_functions = func;
1898 func->next = current;
1914 qbool Cmd_Exists (cmd_state_t *cmd, const char *cmd_name)
1916 cmd_function_t *func;
1918 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1919 if (!strcmp(cmd_name, func->name))
1922 for (func=cmd->engine_functions ; func ; func=func->next)
1923 if (!strcmp (cmd_name,func->name))
1935 const char *Cmd_CompleteCommand (cmd_state_t *cmd, const char *partial)
1937 cmd_function_t *func;
1940 len = strlen(partial);
1946 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1947 if (!strncasecmp(partial, func->name, len))
1950 for (func = cmd->engine_functions; func; func = func->next)
1951 if (!strncasecmp(partial, func->name, len))
1958 Cmd_CompleteCountPossible
1960 New function for tab-completion system
1961 Added by EvilTypeGuy
1962 Thanks to Fett erich@heintz.com
1966 int Cmd_CompleteCountPossible (cmd_state_t *cmd, const char *partial)
1968 cmd_function_t *func;
1973 len = strlen(partial);
1978 // Loop through the command list and count all partial matches
1979 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1980 if (!strncasecmp(partial, func->name, len))
1983 for (func = cmd->engine_functions; func; func = func->next)
1984 if (!strncasecmp(partial, func->name, len))
1991 Cmd_CompleteBuildList
1993 New function for tab-completion system
1994 Added by EvilTypeGuy
1995 Thanks to Fett erich@heintz.com
1999 const char **Cmd_CompleteBuildList (cmd_state_t *cmd, const char *partial)
2001 cmd_function_t *func;
2004 size_t sizeofbuf = (Cmd_CompleteCountPossible (cmd, partial) + 1) * sizeof (const char *);
2007 len = strlen(partial);
2008 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
2009 // Loop through the functions lists and print all matches
2010 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
2011 if (!strncasecmp(partial, func->name, len))
2012 buf[bpos++] = func->name;
2013 for (func = cmd->engine_functions; func; func = func->next)
2014 if (!strncasecmp(partial, func->name, len))
2015 buf[bpos++] = func->name;
2021 // written by LadyHavoc
2022 void Cmd_CompleteCommandPrint (cmd_state_t *cmd, const char *partial)
2024 cmd_function_t *func;
2025 size_t len = strlen(partial);
2026 // Loop through the command list and print all matches
2027 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
2028 if (!strncasecmp(partial, func->name, len))
2029 Con_Printf("^2%s^7: %s\n", func->name, func->description);
2030 for (func = cmd->engine_functions; func; func = func->next)
2031 if (!strncasecmp(partial, func->name, len))
2032 Con_Printf("^2%s^7: %s\n", func->name, func->description);
2038 New function for tab-completion system
2039 Added by EvilTypeGuy
2040 Thanks to Fett erich@heintz.com
2044 const char *Cmd_CompleteAlias (cmd_state_t *cmd, const char *partial)
2049 len = strlen(partial);
2055 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2056 if (!strncasecmp(partial, alias->name, len))
2062 // written by LadyHavoc
2063 void Cmd_CompleteAliasPrint (cmd_state_t *cmd, const char *partial)
2066 size_t len = strlen(partial);
2067 // Loop through the alias list and print all matches
2068 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2069 if (!strncasecmp(partial, alias->name, len))
2070 Con_Printf("^5%s^7: %s", alias->name, alias->value);
2075 Cmd_CompleteAliasCountPossible
2077 New function for tab-completion system
2078 Added by EvilTypeGuy
2079 Thanks to Fett erich@heintz.com
2083 int Cmd_CompleteAliasCountPossible (cmd_state_t *cmd, const char *partial)
2091 len = strlen(partial);
2096 // Loop through the command list and count all partial matches
2097 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2098 if (!strncasecmp(partial, alias->name, len))
2105 Cmd_CompleteAliasBuildList
2107 New function for tab-completion system
2108 Added by EvilTypeGuy
2109 Thanks to Fett erich@heintz.com
2113 const char **Cmd_CompleteAliasBuildList (cmd_state_t *cmd, const char *partial)
2118 size_t sizeofbuf = (Cmd_CompleteAliasCountPossible (cmd, partial) + 1) * sizeof (const char *);
2121 len = strlen(partial);
2122 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
2123 // Loop through the alias list and print all matches
2124 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2125 if (!strncasecmp(partial, alias->name, len))
2126 buf[bpos++] = alias->name;
2132 // TODO: Make this more generic?
2133 void Cmd_ClearCSQCCommands (cmd_state_t *cmd)
2135 cmd_function_t *func;
2136 cmd_function_t **next = &cmd->userdefined->csqc_functions;
2146 extern cvar_t sv_cheats;
2152 A complete command line has been parsed, so try to execute it
2153 FIXME: lookupnoadd the token to speed search?
2156 void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qbool lockmutex)
2159 cmd_function_t *func;
2162 Cbuf_Lock(cmd->cbuf);
2163 oldpos = cmd->cbuf->tokenizebufferpos;
2166 Cmd_TokenizeString (cmd, text);
2168 // execute the command line
2170 goto done; // no tokens
2173 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
2175 if (!strcasecmp(cmd->argv[0], func->name))
2177 if (func->csqcfunc && CL_VM_ConsoleCommand(text)) //[515]: csqc
2183 for (func = cmd->engine_functions; func; func=func->next)
2185 if (!strcasecmp (cmd->argv[0], func->name))
2191 func->function(cmd);
2193 Con_Printf("Command \"%s\" can not be executed\n", Cmd_Argv(cmd, 0));
2198 if((func->flags & CF_CHEAT) && !sv_cheats.integer)
2199 SV_ClientPrintf("No cheats allowed. The server must have sv_cheats set to 1\n");
2201 func->function(cmd);
2209 // if it's a client command and no command was found, say so.
2210 if (cmd->source == src_client)
2212 Con_Printf("Client \"%s\" tried to execute \"%s\"\n", host_client->name, text);
2217 for (a=cmd->userdefined->alias ; a ; a=a->next)
2219 if (!strcasecmp (cmd->argv[0], a->name))
2221 Cmd_ExecuteAlias(cmd, a);
2227 if (!Cvar_Command(cmd) && host.framecount > 0)
2228 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(cmd, 0));
2230 cmd->cbuf->tokenizebufferpos = oldpos;
2232 Cbuf_Unlock(cmd->cbuf);
2239 Returns the position (1 to argc-1) in the command's argument list
2240 where the given parameter apears, or 0 if not present
2244 int Cmd_CheckParm (cmd_state_t *cmd, const char *parm)
2250 Con_Printf ("Cmd_CheckParm: NULL");
2254 for (i = 1; i < Cmd_Argc (cmd); i++)
2255 if (!strcasecmp (parm, Cmd_Argv(cmd, i)))
2263 void Cmd_SaveInitState(void)
2265 cmd_iter_t *cmd_iter;
2266 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2268 cmd_state_t *cmd = cmd_iter->cmd;
2271 for (f = cmd->userdefined->csqc_functions; f; f = f->next)
2272 f->initstate = true;
2273 for (f = cmd->engine_functions; f; f = f->next)
2274 f->initstate = true;
2275 for (a = cmd->userdefined->alias; a; a = a->next)
2277 a->initstate = true;
2278 a->initialvalue = Mem_strdup(zonemempool, a->value);
2281 Cvar_SaveInitState(&cvars_all);
2284 void Cmd_RestoreInitState(void)
2286 cmd_iter_t *cmd_iter;
2287 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2289 cmd_state_t *cmd = cmd_iter->cmd;
2290 cmd_function_t *f, **fp;
2291 cmd_alias_t *a, **ap;
2292 for (fp = &cmd->userdefined->csqc_functions; (f = *fp);)
2298 // destroy this command, it didn't exist at init
2299 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2304 for (fp = &cmd->engine_functions; (f = *fp);)
2310 // destroy this command, it didn't exist at init
2311 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2316 for (ap = &cmd->userdefined->alias; (a = *ap);)
2320 // restore this alias, it existed at init
2321 if (strcmp(a->value ? a->value : "", a->initialvalue ? a->initialvalue : ""))
2323 Con_DPrintf("Cmd_RestoreInitState: Restoring alias %s\n", a->name);
2326 a->value = Mem_strdup(zonemempool, a->initialvalue);
2332 // free this alias, it didn't exist at init...
2333 Con_DPrintf("Cmd_RestoreInitState: Destroying alias %s\n", a->name);
2341 Cvar_RestoreInitState(&cvars_all);