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 qboolean host_stuffcmdsrun = false;
48 //=============================================================================
50 void Cbuf_Lock(cbuf_t *cbuf)
52 Thread_LockMutex(cbuf->lock);
55 void Cbuf_Unlock(cbuf_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 void Cbuf_LinkInsert(cbuf_cmd_t *insert, cbuf_cmd_t **list);
83 static void Cmd_Defer_f (cmd_state_t *cmd)
86 cbuf_t *cbuf = cmd->cbuf;
88 if(Cmd_Argc(cmd) == 1)
90 current = cbuf->deferred;
92 Con_Printf("No commands are pending.\n");
93 else if (current->next == current)
97 while(current->next != current)
100 Con_Printf("-> In %9.2f: %s\n", current->delay, current->text);
101 current = current->next;
105 else if(Cmd_Argc(cmd) == 2 && !strcasecmp("clear", Cmd_Argv(cmd, 1)))
107 while(cbuf->deferred)
109 current = cbuf->deferred;
110 cbuf->deferred = current->next;
114 else if(Cmd_Argc(cmd) == 3)
116 const char *text = Cmd_Argv(cmd, 2);
117 size_t len = strlen(text);
118 current = (cbuf_cmd_t *)Z_Malloc(sizeof(cbuf_cmd_t));
120 current->delay = atof(Cmd_Argv(cmd, 1));
121 memcpy(current->text, text, len+1);
122 current->source = cmd;
124 current->prev = current->next = current;
126 Cbuf_LinkInsert(current, &cbuf->deferred);
131 Con_Printf("usage: defer <seconds> <command>\n"
141 Print something to the center of the screen using SCR_Centerprint
144 static void Cmd_Centerprint_f (cmd_state_t *cmd)
146 char msg[MAX_INPUTLINE];
147 unsigned int i, c, p;
151 strlcpy(msg, Cmd_Argv(cmd,1), sizeof(msg));
152 for(i = 2; i < c; ++i)
154 strlcat(msg, " ", sizeof(msg));
155 strlcat(msg, Cmd_Argv(cmd, i), sizeof(msg));
157 c = (unsigned int)strlen(msg);
158 for(p = 0, i = 0; i < c; ++i)
164 else if(msg[i+1] == '\\')
176 SCR_CenterPrint(msg);
181 =============================================================================
185 =============================================================================
188 static void Cbuf_LinkAdd(cbuf_cmd_t *add, cbuf_cmd_t **list)
194 cbuf_cmd_t *temp = add->prev;
195 add->prev->next = *list;
196 add->prev = (*list)->prev;
197 (*list)->prev->next = add;
198 (*list)->prev = temp;
202 static void Cbuf_LinkInsert(cbuf_cmd_t *insert, cbuf_cmd_t **list)
204 // Same algorithm, but backwards
206 Cbuf_LinkAdd(*list, &insert);
210 static cbuf_cmd_t *Cbuf_LinkPop(cbuf_cmd_t **list)
212 cbuf_cmd_t *node = *list;
214 (*list)->prev = node->prev;
215 (*list)->prev->next = *list;
225 Parses Quake console command-line
226 Returns size of parsed command-line
229 static size_t Cbuf_ParseText(char **in)
232 qboolean quotes = false;
233 qboolean comment = false; // Does not imply end because we might be starting the line with a comment.
234 qboolean escaped = false;
235 qboolean end = false; // Reached the end of a valid command
236 char *offset = NULL; // Non-NULL if valid command. Used by the caller to know where to start copying.
237 size_t cmdsize = 0; // Non-zero if valid command. Basically bytes to copy for the caller.
240 * Allow escapes in quotes. Ignore newlines and
241 * comments. Return 0 if input consists solely
242 * of either of those, and ignore blank input.
249 if(!quotes && (*in)[i+1] == '/' && (i == 0 || ISWHITESPACE((*in)[i-1])))
254 // Use bit magic to indicate an incomplete (pending) command.
282 if (!escaped && quotes)
292 offset = (char *)&(*in)[i];
307 static cbuf_cmd_t *Cbuf_LinkGet(cbuf_t *cbuf, cbuf_cmd_t *existing)
309 cbuf_cmd_t *ret = NULL;
310 if(existing && existing->pending)
315 ret = Cbuf_LinkPop(&cbuf->free);
317 ret = (cbuf_cmd_t *)Z_Malloc(sizeof(cbuf_cmd_t));
319 ret->pending = false;
326 // Cloudwalk: Not happy with this, but it works.
327 static cbuf_cmd_t *Cbuf_LinkCreate(cmd_state_t *cmd, cbuf_cmd_t *existing, const char *text)
329 char *in = (char *)&text[0];
330 cbuf_t *cbuf = cmd->cbuf;
331 size_t totalsize = 0, newsize = 0;
332 cbuf_cmd_t *current = NULL, *head = NULL;
334 // Slide the pointer down until we reach the end
338 * FIXME: Upon reaching a terminator, we make a redundant
339 * call just to say "it's the end of the input stream".
341 newsize = Cbuf_ParseText(&in);
347 current = Cbuf_LinkGet(cbuf, existing);
349 if(!current->pending)
351 current->source = cmd;
352 current->prev = current->next = current;
353 Cbuf_LinkAdd(current, &head);
356 if(newsize & (1<<17))
357 current->pending = true;
358 totalsize += (newsize &= ~(1<<17));
359 strlcpy(¤t->text[current->size], in, newsize + 1);
360 current->size += newsize;
362 else if (existing && !totalsize)
363 existing->pending = false;
368 cbuf->size += totalsize;
377 Adds command text at the end of the buffer
380 void Cbuf_AddText (cmd_state_t *cmd, const char *text)
382 size_t l = strlen(text);
383 cbuf_t *cbuf = cmd->cbuf;
384 cbuf_cmd_t *add = NULL;
388 if (cbuf->maxsize - cbuf->size <= l)
389 Con_Print("Cbuf_AddText: overflow\n");
392 if(!(add = Cbuf_LinkCreate(cmd, cbuf->start ? cbuf->start->prev : NULL, text)))
395 Cbuf_LinkAdd(add, &cbuf->start);
404 Adds command text immediately after the current command
405 FIXME: actually change the command buffer to do less copying
408 void Cbuf_InsertText (cmd_state_t *cmd, const char *text)
410 cbuf_t *cbuf = cmd->cbuf;
411 cbuf_cmd_t *insert = NULL;
412 size_t l = strlen(text);
416 // we need to memmove the existing text and stuff this in before it...
417 if (cbuf->size + l >= (size_t)cbuf->maxsize)
418 Con_Print("Cbuf_InsertText: overflow\n");
421 if(!(insert = Cbuf_LinkCreate(cmd, cbuf->start, text)))
424 Cbuf_LinkInsert(insert, &cbuf->start);
432 Cbuf_Execute_Deferred --blub
435 static void Cbuf_Execute_Deferred (cbuf_t *cbuf)
440 if (host.realtime - cbuf->deferred_oldtime < 0 || host.realtime - cbuf->deferred_oldtime > 1800)
441 cbuf->deferred_oldtime = host.realtime;
442 eat = host.realtime - cbuf->deferred_oldtime;
443 if (eat < (1.0 / 120.0))
445 cbuf->deferred_oldtime = host.realtime;
449 current = cbuf->deferred;
450 current->delay -= eat;
451 if(current->delay <= 0)
453 Cbuf_AddText(current->source, current->text);
454 Cbuf_AddText(current->source, ";\n");
456 current = Cbuf_LinkPop(&cbuf->deferred);
466 static qboolean Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias );
467 void Cbuf_Execute (cbuf_t *cbuf)
470 char preprocessed[MAX_INPUTLINE];
473 // LadyHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
474 cbuf->tokenizebufferpos = 0;
479 * Delete the text from the command buffer and move remaining
480 * commands down. This is necessary because commands (exec, alias)
481 * can insert data at the beginning of the text buffer
483 current = Cbuf_LinkPop(&cbuf->start);
486 * Assume we're rolling with the current command-line and
487 * always set this false because alias expansion or cbuf insertion
488 * without a newline may set this true, and cause weirdness.
490 current->pending = false;
492 cbuf->size -= current->size;
494 // Infinite loop if aliases expand without this
498 firstchar = current->text;
499 while(*firstchar && ISWHITESPACE(*firstchar))
501 if((strncmp(firstchar, "alias", 5) || !ISWHITESPACE(firstchar[5])) &&
502 (strncmp(firstchar, "bind", 4) || !ISWHITESPACE(firstchar[4])) &&
503 (strncmp(firstchar, "in_bind", 7) || !ISWHITESPACE(firstchar[7])))
505 if(Cmd_PreprocessString(current->source, current->text, preprocessed, sizeof(preprocessed), NULL ))
506 Cmd_ExecuteString(current->source, preprocessed, src_command, false);
510 Cmd_ExecuteString (current->source, current->text, src_command, false);
513 // Recycle memory so using WASD doesn't cause a malloc and free
514 current->prev = current->next = current;
516 Cbuf_LinkAdd(current, &cbuf->free);
523 * Skip out while text still remains in
524 * buffer, leaving it for next frame
532 void Cbuf_Frame(cbuf_t *cbuf)
534 Cbuf_Execute_Deferred(cbuf);
537 SV_LockThreadMutex();
539 SV_UnlockThreadMutex();
544 ==============================================================================
548 ==============================================================================
555 Adds command line parameters as script statements
556 Commands lead with a +, and continue until a - or another +
557 quake +prog jctest.qp +cmd amlev1
558 quake -nosound +cmd amlev1
561 static void Cmd_StuffCmds_f (cmd_state_t *cmd)
564 // this is for all commandline options combined (and is bounds checked)
565 char build[MAX_INPUTLINE];
567 // come back later so we don't crash
568 if(host.state == host_init)
571 if (Cmd_Argc (cmd) != 1)
573 Con_Print("stuffcmds : execute command line parameters\n");
577 // no reason to run the commandline arguments twice
578 if (host_stuffcmdsrun)
581 host_stuffcmdsrun = true;
584 for (i = 0;i < sys.argc;i++)
586 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)
589 while (sys.argv[i][j])
590 build[l++] = sys.argv[i][j++];
592 for (;i < sys.argc;i++)
596 if ((sys.argv[i][0] == '+' || sys.argv[i][0] == '-') && (sys.argv[i][1] < '0' || sys.argv[i][1] > '9'))
598 if (l + strlen(sys.argv[i]) + 4 > sizeof(build) - 1)
601 if (strchr(sys.argv[i], ' '))
603 for (j = 0;sys.argv[i][j];j++)
604 build[l++] = sys.argv[i][j];
605 if (strchr(sys.argv[i], ' '))
612 // now terminate the combined string and prepend it to the command buffer
613 // we already reserved space for the terminator
615 Cbuf_InsertText (cmd, build);
618 static void Cmd_Exec(cmd_state_t *cmd, const char *filename)
621 size_t filenameLen = strlen(filename);
622 qboolean isdefaultcfg =
623 !strcmp(filename, "default.cfg") ||
624 (filenameLen >= 12 && !strcmp(filename + filenameLen - 12, "/default.cfg"));
626 if (!strcmp(filename, "config.cfg"))
628 filename = CONFIGFILENAME;
629 if (COM_CheckParm("-noconfig"))
630 return; // don't execute config.cfg
633 f = (char *)FS_LoadFile (filename, tempmempool, false, NULL);
636 Con_Printf("couldn't exec %s\n",filename);
639 Con_Printf("execing %s\n",filename);
641 // if executing default.cfg for the first time, lock the cvar defaults
642 // it may seem backwards to insert this text BEFORE the default.cfg
643 // but Cbuf_InsertText inserts before, so this actually ends up after it.
645 Cbuf_InsertText(cmd, "\ncvar_lockdefaults\n");
647 Cbuf_InsertText (cmd, f);
652 // special defaults for specific games go here, these execute before default.cfg
653 // Nehahra pushable crates malfunction in some levels if this is on
654 // Nehahra NPC AI is confused by blowupfallenzombies
658 Cbuf_InsertText(cmd, "\n"
659 "sv_gameplayfix_blowupfallenzombies 0\n"
660 "sv_gameplayfix_findradiusdistancetobox 0\n"
661 "sv_gameplayfix_grenadebouncedownslopes 0\n"
662 "sv_gameplayfix_slidemoveprojectiles 0\n"
663 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
664 "sv_gameplayfix_setmodelrealbox 0\n"
665 "sv_gameplayfix_droptofloorstartsolid 0\n"
666 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
667 "sv_gameplayfix_noairborncorpse 0\n"
668 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
669 "sv_gameplayfix_easierwaterjump 0\n"
670 "sv_gameplayfix_delayprojectiles 0\n"
671 "sv_gameplayfix_multiplethinksperframe 0\n"
672 "sv_gameplayfix_fixedcheckwatertransition 0\n"
673 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
674 "sv_gameplayfix_swiminbmodels 0\n"
675 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
676 "sys_ticrate 0.01388889\n"
678 "r_shadow_bumpscale_basetexture 0\n"
679 "csqc_polygons_defaultmaterial_nocullface 0\n"
683 Cbuf_InsertText(cmd, "\n"
684 "sv_gameplayfix_blowupfallenzombies 0\n"
685 "sv_gameplayfix_findradiusdistancetobox 0\n"
686 "sv_gameplayfix_grenadebouncedownslopes 0\n"
687 "sv_gameplayfix_slidemoveprojectiles 0\n"
688 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
689 "sv_gameplayfix_setmodelrealbox 0\n"
690 "sv_gameplayfix_droptofloorstartsolid 0\n"
691 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
692 "sv_gameplayfix_noairborncorpse 0\n"
693 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
694 "sv_gameplayfix_easierwaterjump 0\n"
695 "sv_gameplayfix_delayprojectiles 0\n"
696 "sv_gameplayfix_multiplethinksperframe 0\n"
697 "sv_gameplayfix_fixedcheckwatertransition 0\n"
698 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
699 "sv_gameplayfix_swiminbmodels 0\n"
700 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
701 "sys_ticrate 0.01388889\n"
703 "r_shadow_bumpscale_basetexture 0\n"
704 "csqc_polygons_defaultmaterial_nocullface 0\n"
707 // 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.
708 // 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
709 // hipnotic mission pack has issues in their proximity mine sticking code, which causes them to bounce off.
712 Cbuf_InsertText(cmd, "\n"
713 "sv_gameplayfix_blowupfallenzombies 0\n"
714 "sv_gameplayfix_findradiusdistancetobox 0\n"
715 "sv_gameplayfix_grenadebouncedownslopes 0\n"
716 "sv_gameplayfix_slidemoveprojectiles 0\n"
717 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
718 "sv_gameplayfix_setmodelrealbox 0\n"
719 "sv_gameplayfix_droptofloorstartsolid 0\n"
720 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
721 "sv_gameplayfix_noairborncorpse 0\n"
722 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
723 "sv_gameplayfix_easierwaterjump 0\n"
724 "sv_gameplayfix_delayprojectiles 0\n"
725 "sv_gameplayfix_multiplethinksperframe 0\n"
726 "sv_gameplayfix_fixedcheckwatertransition 0\n"
727 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
728 "sv_gameplayfix_swiminbmodels 0\n"
729 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
732 "r_shadow_bumpscale_basetexture 0\n"
733 "csqc_polygons_defaultmaterial_nocullface 0\n"
736 // rogue mission pack has a guardian boss that does not wake up if findradius returns one of the entities around its spawn area
738 Cbuf_InsertText(cmd, "\n"
739 "sv_gameplayfix_blowupfallenzombies 0\n"
740 "sv_gameplayfix_findradiusdistancetobox 0\n"
741 "sv_gameplayfix_grenadebouncedownslopes 0\n"
742 "sv_gameplayfix_slidemoveprojectiles 0\n"
743 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
744 "sv_gameplayfix_setmodelrealbox 0\n"
745 "sv_gameplayfix_droptofloorstartsolid 0\n"
746 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
747 "sv_gameplayfix_noairborncorpse 0\n"
748 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
749 "sv_gameplayfix_easierwaterjump 0\n"
750 "sv_gameplayfix_delayprojectiles 0\n"
751 "sv_gameplayfix_multiplethinksperframe 0\n"
752 "sv_gameplayfix_fixedcheckwatertransition 0\n"
753 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
754 "sv_gameplayfix_swiminbmodels 0\n"
755 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
756 "sys_ticrate 0.01388889\n"
758 "r_shadow_bumpscale_basetexture 0\n"
759 "csqc_polygons_defaultmaterial_nocullface 0\n"
763 Cbuf_InsertText(cmd, "\n"
764 "sv_gameplayfix_blowupfallenzombies 0\n"
765 "sv_gameplayfix_findradiusdistancetobox 0\n"
766 "sv_gameplayfix_grenadebouncedownslopes 0\n"
767 "sv_gameplayfix_slidemoveprojectiles 0\n"
768 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
769 "sv_gameplayfix_setmodelrealbox 0\n"
770 "sv_gameplayfix_droptofloorstartsolid 0\n"
771 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
772 "sv_gameplayfix_noairborncorpse 0\n"
773 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
774 "sv_gameplayfix_easierwaterjump 0\n"
775 "sv_gameplayfix_delayprojectiles 0\n"
776 "sv_gameplayfix_multiplethinksperframe 0\n"
777 "sv_gameplayfix_fixedcheckwatertransition 0\n"
778 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
779 "sv_gameplayfix_swiminbmodels 0\n"
780 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
781 "sys_ticrate 0.01388889\n"
783 "r_shadow_bumpscale_basetexture 4\n"
784 "csqc_polygons_defaultmaterial_nocullface 0\n"
788 Cbuf_InsertText(cmd, "\n"
789 "sv_gameplayfix_blowupfallenzombies 1\n"
790 "sv_gameplayfix_findradiusdistancetobox 1\n"
791 "sv_gameplayfix_grenadebouncedownslopes 1\n"
792 "sv_gameplayfix_slidemoveprojectiles 1\n"
793 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
794 "sv_gameplayfix_setmodelrealbox 1\n"
795 "sv_gameplayfix_droptofloorstartsolid 1\n"
796 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
797 "sv_gameplayfix_noairborncorpse 1\n"
798 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
799 "sv_gameplayfix_easierwaterjump 1\n"
800 "sv_gameplayfix_delayprojectiles 1\n"
801 "sv_gameplayfix_multiplethinksperframe 1\n"
802 "sv_gameplayfix_fixedcheckwatertransition 1\n"
803 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
804 "sv_gameplayfix_swiminbmodels 1\n"
805 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
806 "sys_ticrate 0.01388889\n"
807 "sv_gameplayfix_q2airaccelerate 1\n"
808 "sv_gameplayfix_stepmultipletimes 1\n"
809 "csqc_polygons_defaultmaterial_nocullface 1\n"
810 "con_chatsound_team_mask 13\n"
814 case GAME_VORETOURNAMENT:
815 // 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
816 Cbuf_InsertText(cmd, "\n"
817 "csqc_polygons_defaultmaterial_nocullface 1\n"
818 "con_chatsound_team_mask 13\n"
819 "sv_gameplayfix_customstats 1\n"
822 // Steel Storm: Burning Retribution csqc misinterprets CSQC_InputEvent if type is a value other than 0 or 1
823 case GAME_STEELSTORM:
824 Cbuf_InsertText(cmd, "\n"
825 "sv_gameplayfix_blowupfallenzombies 1\n"
826 "sv_gameplayfix_findradiusdistancetobox 1\n"
827 "sv_gameplayfix_grenadebouncedownslopes 1\n"
828 "sv_gameplayfix_slidemoveprojectiles 1\n"
829 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
830 "sv_gameplayfix_setmodelrealbox 1\n"
831 "sv_gameplayfix_droptofloorstartsolid 1\n"
832 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
833 "sv_gameplayfix_noairborncorpse 1\n"
834 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
835 "sv_gameplayfix_easierwaterjump 1\n"
836 "sv_gameplayfix_delayprojectiles 1\n"
837 "sv_gameplayfix_multiplethinksperframe 1\n"
838 "sv_gameplayfix_fixedcheckwatertransition 1\n"
839 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
840 "sv_gameplayfix_swiminbmodels 1\n"
841 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
842 "sys_ticrate 0.01388889\n"
843 "cl_csqc_generatemousemoveevents 0\n"
844 "csqc_polygons_defaultmaterial_nocullface 1\n"
848 Cbuf_InsertText(cmd, "\n"
849 "sv_gameplayfix_blowupfallenzombies 1\n"
850 "sv_gameplayfix_findradiusdistancetobox 1\n"
851 "sv_gameplayfix_grenadebouncedownslopes 1\n"
852 "sv_gameplayfix_slidemoveprojectiles 1\n"
853 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
854 "sv_gameplayfix_setmodelrealbox 1\n"
855 "sv_gameplayfix_droptofloorstartsolid 1\n"
856 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
857 "sv_gameplayfix_noairborncorpse 1\n"
858 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
859 "sv_gameplayfix_easierwaterjump 1\n"
860 "sv_gameplayfix_delayprojectiles 1\n"
861 "sv_gameplayfix_multiplethinksperframe 1\n"
862 "sv_gameplayfix_fixedcheckwatertransition 1\n"
863 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
864 "sv_gameplayfix_swiminbmodels 1\n"
865 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
866 "sys_ticrate 0.01388889\n"
867 "csqc_polygons_defaultmaterial_nocullface 0\n"
879 static void Cmd_Exec_f (cmd_state_t *cmd)
884 if (Cmd_Argc(cmd) != 2)
886 Con_Print("exec <filename> : execute a script file\n");
890 s = FS_Search(Cmd_Argv(cmd, 1), true, true, NULL);
891 if(!s || !s->numfilenames)
893 Con_Printf("couldn't exec %s\n",Cmd_Argv(cmd, 1));
897 for(i = 0; i < s->numfilenames; ++i)
898 Cmd_Exec(cmd, s->filenames[i]);
908 Just prints the rest of the line to the console
911 static void Cmd_Echo_f (cmd_state_t *cmd)
915 for (i=1 ; i<Cmd_Argc(cmd) ; i++)
916 Con_Printf("%s ",Cmd_Argv(cmd, i));
921 // Support Doom3-style Toggle Console Command
926 Toggles a specified console variable amongst the values specified (default is 0 and 1)
929 static void Cmd_Toggle_f(cmd_state_t *cmd)
931 // Acquire Number of Arguments
932 int nNumArgs = Cmd_Argc(cmd);
935 // No Arguments Specified; Print Usage
936 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");
938 { // Correct Arguments Specified
939 // Acquire Potential CVar
940 cvar_t* cvCVar = Cvar_FindVar(cmd->cvars, Cmd_Argv(cmd, 1), cmd->cvars_flagsmask);
947 Cvar_SetValueQuick(cvCVar, 0);
949 Cvar_SetValueQuick(cvCVar, 1);
953 { // 0 and Specified Usage
954 if(cvCVar->integer == atoi(Cmd_Argv(cmd, 2) ) )
955 // CVar is Specified Value; // Reset to 0
956 Cvar_SetValueQuick(cvCVar, 0);
958 if(cvCVar->integer == 0)
959 // CVar is 0; Specify Value
960 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
962 // CVar does not match; Reset to 0
963 Cvar_SetValueQuick(cvCVar, 0);
966 { // Variable Values Specified
970 for(nCnt = 2; nCnt < nNumArgs; nCnt++)
971 { // Cycle through Values
972 if( strcmp(cvCVar->string, Cmd_Argv(cmd, nCnt) ) == 0)
973 { // Current Value Located; Increment to Next
974 if( (nCnt + 1) == nNumArgs)
975 // Max Value Reached; Reset
976 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
979 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, nCnt + 1) );
988 // Value not Found; Reset to Original
989 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
995 Con_Printf("ERROR : CVar '%s' not found\n", Cmd_Argv(cmd, 1) );
1004 Creates a new command that executes a command string (possibly ; seperated)
1007 static void Cmd_Alias_f (cmd_state_t *cmd)
1010 char line[MAX_INPUTLINE];
1015 if (Cmd_Argc(cmd) == 1)
1017 Con_Print("Current alias commands:\n");
1018 for (a = cmd->userdefined->alias ; a ; a=a->next)
1019 Con_Printf("%s : %s", a->name, a->value);
1023 s = Cmd_Argv(cmd, 1);
1024 if (strlen(s) >= MAX_ALIAS_NAME)
1026 Con_Print("Alias name is too long\n");
1030 // if the alias already exists, reuse it
1031 for (a = cmd->userdefined->alias ; a ; a=a->next)
1033 if (!strcmp(s, a->name))
1042 cmdalias_t *prev, *current;
1044 a = (cmdalias_t *)Z_Malloc (sizeof(cmdalias_t));
1045 strlcpy (a->name, s, sizeof (a->name));
1046 // insert it at the right alphanumeric position
1047 for( prev = NULL, current = cmd->userdefined->alias ; current && strcmp( current->name, a->name ) < 0 ; prev = current, current = current->next )
1052 cmd->userdefined->alias = a;
1058 // copy the rest of the command line
1059 line[0] = 0; // start out with a null string
1061 for (i=2 ; i < c ; i++)
1064 strlcat (line, " ", sizeof (line));
1065 strlcat (line, Cmd_Argv(cmd, i), sizeof (line));
1067 strlcat (line, "\n", sizeof (line));
1069 alloclen = strlen (line) + 1;
1071 line[alloclen - 2] = '\n'; // to make sure a newline is appended even if too long
1072 a->value = (char *)Z_Malloc (alloclen);
1073 memcpy (a->value, line, alloclen);
1080 Remove existing aliases.
1083 static void Cmd_UnAlias_f (cmd_state_t *cmd)
1089 if(Cmd_Argc(cmd) == 1)
1091 Con_Print("unalias: Usage: unalias alias1 [alias2 ...]\n");
1095 for(i = 1; i < Cmd_Argc(cmd); ++i)
1097 s = Cmd_Argv(cmd, i);
1099 for(a = cmd->userdefined->alias; a; p = a, a = a->next)
1101 if(!strcmp(s, a->name))
1103 if (a->initstate) // we can not remove init aliases
1105 if(a == cmd->userdefined->alias)
1106 cmd->userdefined->alias = a->next;
1115 Con_Printf("unalias: %s alias not found\n", s);
1120 =============================================================================
1124 =============================================================================
1127 static const char *Cmd_GetDirectCvarValue(cmd_state_t *cmd, const char *varname, cmdalias_t *alias, qboolean *is_multiple)
1132 static char vabuf[1024]; // cmd_mutex
1135 *is_multiple = false;
1137 if(!varname || !*varname)
1142 if(!strcmp(varname, "*"))
1145 *is_multiple = true;
1146 return Cmd_Args(cmd);
1148 else if(!strcmp(varname, "#"))
1150 return va(vabuf, sizeof(vabuf), "%d", Cmd_Argc(cmd));
1152 else if(varname[strlen(varname) - 1] == '-')
1154 argno = strtol(varname, &endptr, 10);
1155 if(endptr == varname + strlen(varname) - 1)
1157 // whole string is a number, apart from the -
1158 const char *p = Cmd_Args(cmd);
1159 for(; argno > 1; --argno)
1160 if(!COM_ParseToken_Console(&p))
1165 *is_multiple = true;
1167 // kill pre-argument whitespace
1168 for (;*p && ISWHITESPACE(*p);p++)
1177 argno = strtol(varname, &endptr, 10);
1180 // whole string is a number
1181 // NOTE: we already made sure we don't have an empty cvar name!
1182 if(argno >= 0 && argno < Cmd_Argc(cmd))
1183 return Cmd_Argv(cmd, argno);
1188 if((cvar = Cvar_FindVar(cmd->cvars, varname, cmd->cvars_flagsmask)) && !(cvar->flags & CVAR_PRIVATE))
1189 return cvar->string;
1194 qboolean Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset, qboolean putquotes)
1196 qboolean quote_quot = !!strchr(quoteset, '"');
1197 qboolean quote_backslash = !!strchr(quoteset, '\\');
1198 qboolean quote_dollar = !!strchr(quoteset, '$');
1207 *out++ = '"'; --outlen;
1213 if(*in == '"' && quote_quot)
1217 *out++ = '\\'; --outlen;
1218 *out++ = '"'; --outlen;
1220 else if(*in == '\\' && quote_backslash)
1224 *out++ = '\\'; --outlen;
1225 *out++ = '\\'; --outlen;
1227 else if(*in == '$' && quote_dollar)
1231 *out++ = '$'; --outlen;
1232 *out++ = '$'; --outlen;
1238 *out++ = *in; --outlen;
1253 static const char *Cmd_GetCvarValue(cmd_state_t *cmd, const char *var, size_t varlen, cmdalias_t *alias)
1255 static char varname[MAX_INPUTLINE]; // cmd_mutex
1256 static char varval[MAX_INPUTLINE]; // cmd_mutex
1257 const char *varstr = NULL;
1259 qboolean required = false;
1260 qboolean optional = false;
1261 static char asis[] = "asis"; // just to suppress const char warnings
1263 if(varlen >= MAX_INPUTLINE)
1264 varlen = MAX_INPUTLINE - 1;
1265 memcpy(varname, var, varlen);
1266 varname[varlen] = 0;
1267 varfunc = strchr(varname, ' ');
1279 Con_Printf(CON_WARN "Warning: Could not expand $ in alias %s\n", alias->name);
1281 Con_Printf(CON_WARN "Warning: Could not expand $\n");
1289 while((p = strchr(varfunc, '?')))
1292 memmove(p, p+1, strlen(p)); // with final NUL
1295 while((p = strchr(varfunc, '!')))
1298 memmove(p, p+1, strlen(p)); // with final NUL
1301 while((p = strchr(varfunc, ' ')))
1303 memmove(p, p+1, strlen(p)); // with final NUL
1305 // if no function is left, NULL it
1310 if(varname[0] == '$')
1311 varstr = Cmd_GetDirectCvarValue(cmd, Cmd_GetDirectCvarValue(cmd, varname + 1, alias, NULL), alias, NULL);
1314 qboolean is_multiple = false;
1315 // Exception: $* and $n- don't use the quoted form by default
1316 varstr = Cmd_GetDirectCvarValue(cmd, varname, alias, &is_multiple);
1327 Con_Printf(CON_ERROR "Error: Could not expand $%s in alias %s\n", varname, alias->name);
1329 Con_Printf(CON_ERROR "Error: Could not expand $%s\n", varname);
1339 Con_Printf(CON_WARN "Warning: Could not expand $%s in alias %s\n", varname, alias->name);
1341 Con_Printf(CON_WARN "Warning: Could not expand $%s\n", varname);
1342 dpsnprintf(varval, sizeof(varval), "$%s", varname);
1347 if(!varfunc || !strcmp(varfunc, "q")) // note: quoted form is default, use "asis" to override!
1349 // quote it so it can be used inside double quotes
1350 // we just need to replace " by \", and of course, double backslashes
1351 Cmd_QuoteString(varval, sizeof(varval), varstr, "\"\\", false);
1354 else if(!strcmp(varfunc, "asis"))
1359 Con_Printf("Unknown variable function %s\n", varfunc);
1365 Cmd_PreprocessString
1367 Preprocesses strings and replaces $*, $param#, $cvar accordingly. Also strips comments.
1369 static qboolean Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias ) {
1375 // don't crash if there's no room in the outtext buffer
1376 if( maxoutlen == 0 ) {
1379 maxoutlen--; // because of \0
1384 while( *in && outlen < maxoutlen ) {
1386 // this is some kind of expansion, see what comes after the $
1389 // The console does the following preprocessing:
1391 // - $$ is transformed to a single dollar sign.
1392 // - $var or ${var} are expanded to the contents of the named cvar,
1393 // with quotation marks and backslashes quoted so it can safely
1394 // be used inside quotation marks (and it should always be used
1396 // - ${var asis} inserts the cvar value as is, without doing this
1398 // - ${var ?} silently expands to the empty string if
1399 // $var does not exist
1400 // - ${var !} fails expansion and executes nothing if
1401 // $var does not exist
1402 // - prefix the cvar name with a dollar sign to do indirection;
1403 // for example, if $x has the value timelimit, ${$x} will return
1404 // the value of $timelimit
1405 // - when expanding an alias, the special variable name $* refers
1406 // to all alias parameters, and a number refers to that numbered
1407 // alias parameter, where the name of the alias is $0, the first
1408 // parameter is $1 and so on; as a special case, $* inserts all
1409 // parameters, without extra quoting, so one can use $* to just
1410 // pass all parameters around. All parameters starting from $n
1411 // can be referred to as $n- (so $* is equivalent to $1-).
1412 // - ${* q} and ${n- q} force quoting anyway
1414 // Note: when expanding an alias, cvar expansion is done in the SAME step
1415 // as alias expansion so that alias parameters or cvar values containing
1416 // dollar signs have no unwanted bad side effects. However, this needs to
1417 // be accounted for when writing complex aliases. For example,
1418 // alias foo "set x NEW; echo $x"
1419 // actually expands to
1420 // "set x NEW; echo OLD"
1421 // and will print OLD! To work around this, use a second alias:
1422 // alias foo "set x NEW; foo2"
1423 // alias foo2 "echo $x"
1425 // Also note: lines starting with alias are exempt from cvar expansion.
1426 // If you want cvar expansion, write "alias" instead:
1429 // alias foo "echo $x"
1430 // "alias" bar "echo $x"
1433 // foo will print 2, because the variable $x will be expanded when the alias
1434 // gets expanded. bar will print 1, because the variable $x was expanded
1435 // at definition time. foo can be equivalently defined as
1437 // "alias" foo "echo $$x"
1439 // because at definition time, $$ will get replaced to a single $.
1444 } else if(*in == '{') {
1445 varlen = strcspn(in + 1, "}");
1446 if(in[varlen + 1] == '}')
1448 val = Cmd_GetCvarValue(cmd, in + 1, varlen, alias);
1460 varlen = strspn(in, "#*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-");
1461 val = Cmd_GetCvarValue(cmd, in, varlen, alias);
1468 // insert the cvar value
1469 while(*val && outlen < maxoutlen)
1470 outtext[outlen++] = *val++;
1475 // copy the unexpanded text
1476 outtext[outlen++] = '$';
1477 while(eat && outlen < maxoutlen)
1479 outtext[outlen++] = *in++;
1485 outtext[outlen++] = *in++;
1487 outtext[outlen] = 0;
1495 Called for aliases and fills in the alias into the cbuffer
1498 static void Cmd_ExecuteAlias (cmd_state_t *cmd, cmdalias_t *alias)
1500 static char buffer[ MAX_INPUTLINE ]; // cmd_mutex
1501 static char buffer2[ MAX_INPUTLINE ]; // cmd_mutex
1502 qboolean ret = Cmd_PreprocessString( cmd, alias->value, buffer, sizeof(buffer) - 2, alias );
1505 // insert at start of command buffer, so that aliases execute in order
1506 // (fixes bug introduced by Black on 20050705)
1508 // Note: Cbuf_PreprocessString will be called on this string AGAIN! So we
1509 // have to make sure that no second variable expansion takes place, otherwise
1510 // alias parameters containing dollar signs can have bad effects.
1511 Cmd_QuoteString(buffer2, sizeof(buffer2), buffer, "$", false);
1512 Cbuf_InsertText(cmd, buffer2);
1519 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
1520 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
1524 static void Cmd_List_f (cmd_state_t *cmd)
1526 cmd_function_t *func;
1527 const char *partial;
1532 if (Cmd_Argc(cmd) > 1)
1534 partial = Cmd_Argv(cmd, 1);
1535 len = strlen(partial);
1536 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1546 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1548 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1550 Con_Printf("%s : %s\n", func->name, func->description);
1553 for (func = cmd->engine_functions; func; func = func->next)
1555 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1557 Con_Printf("%s : %s\n", func->name, func->description);
1564 Con_Printf("%i Command%s matching \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1566 Con_Printf("%i Command%s beginning with \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1569 Con_Printf("%i Command%s\n\n", count, (count > 1) ? "s" : "");
1572 static void Cmd_Apropos_f(cmd_state_t *cmd)
1574 cmd_function_t *func;
1577 const char *partial;
1582 if (Cmd_Argc(cmd) > 1)
1583 partial = Cmd_Args(cmd);
1586 Con_Printf("usage: %s <string>\n",Cmd_Argv(cmd, 0));
1590 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
1592 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
1595 for (cvar = cmd->cvars->vars; cvar; cvar = cvar->next)
1597 if (matchpattern_with_separator(cvar->name, partial, true, "", false) ||
1598 matchpattern_with_separator(cvar->description, partial, true, "", false))
1600 Con_Printf ("cvar ");
1601 Cvar_PrintHelp(cvar, cvar->name, true);
1604 for (int i = 0; i < cvar->aliasindex; i++)
1606 if (matchpattern_with_separator(cvar->aliases[i], partial, true, "", false))
1608 Con_Printf ("cvar ");
1609 Cvar_PrintHelp(cvar, cvar->aliases[i], true);
1614 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1616 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1617 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1619 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1622 for (func = cmd->engine_functions; func; func = func->next)
1624 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1625 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1627 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1630 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
1632 // procede here a bit differently as an alias value always got a final \n
1633 if (!matchpattern_with_separator(alias->name, partial, true, "", false))
1634 if (!matchpattern_with_separator(alias->value, partial, true, "\n", false)) // when \n is as separator wildcards don't match it
1636 Con_Printf("alias ^5%s^7: %s", alias->name, alias->value); // do not print an extra \n
1639 Con_Printf("%i result%s\n\n", count, (count > 1) ? "s" : "");
1649 cmd_iter_t *cmd_iter;
1650 cbuf_t *cbuf = (cbuf_t *)Z_Malloc(sizeof(cbuf_t));
1651 cbuf->maxsize = 655360;
1652 cbuf->lock = Thread_CreateMutex();
1656 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
1658 cmd_state_t *cmd = cmd_iter->cmd;
1659 cmd->mempool = Mem_AllocPool("commands", 0, NULL);
1660 // space for commands and script files
1662 cmd->null_string = "";
1664 // client console can see server cvars because the user may start a server
1665 cmd_client.cvars = &cvars_all;
1666 cmd_client.cvars_flagsmask = CVAR_CLIENT | CVAR_SERVER;
1667 cmd_client.cmd_flags = CMD_CLIENT | CMD_CLIENT_FROM_SERVER;
1668 cmd_client.auto_flags = CMD_SERVER_FROM_CLIENT;
1669 cmd_client.auto_function = CL_ForwardToServer_f; // FIXME: Move this to the client.
1670 cmd_client.userdefined = &cmd_userdefined_all;
1671 // dedicated server console can only see server cvars, there is no client
1672 cmd_server.cvars = &cvars_all;
1673 cmd_server.cvars_flagsmask = CVAR_SERVER;
1674 cmd_server.cmd_flags = CMD_SERVER;
1675 cmd_server.auto_flags = 0;
1676 cmd_server.auto_function = NULL;
1677 cmd_server.userdefined = &cmd_userdefined_all;
1678 // server commands received from clients have no reason to access cvars, cvar expansion seems perilous.
1679 cmd_serverfromclient.cvars = &cvars_null;
1680 cmd_serverfromclient.cvars_flagsmask = 0;
1681 cmd_serverfromclient.cmd_flags = CMD_SERVER_FROM_CLIENT | CMD_USERINFO;
1682 cmd_serverfromclient.auto_flags = 0;
1683 cmd_serverfromclient.auto_function = NULL;
1684 cmd_serverfromclient.userdefined = &cmd_userdefined_null;
1687 // register our commands
1689 // client-only commands
1690 Cmd_AddCommand(CMD_SHARED, "wait", Cmd_Wait_f, "make script execution wait for next rendered frame");
1691 Cmd_AddCommand(CMD_CLIENT, "cprint", Cmd_Centerprint_f, "print something at the screen center");
1693 // maintenance commands used for upkeep of cvars and saved configs
1694 Cmd_AddCommand(CMD_SHARED, "stuffcmds", Cmd_StuffCmds_f, "execute commandline parameters (must be present in quake.rc script)");
1695 Cmd_AddCommand(CMD_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");
1696 Cmd_AddCommand(CMD_SHARED, "cvar_resettodefaults_all", Cvar_ResetToDefaults_All_f, "sets all cvars to their locked default values");
1697 Cmd_AddCommand(CMD_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)");
1698 Cmd_AddCommand(CMD_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)");
1700 // general console commands used in multiple environments
1701 Cmd_AddCommand(CMD_SHARED, "exec", Cmd_Exec_f, "execute a script file");
1702 Cmd_AddCommand(CMD_SHARED, "echo",Cmd_Echo_f, "print a message to the console (useful in scripts)");
1703 Cmd_AddCommand(CMD_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");
1704 Cmd_AddCommand(CMD_SHARED, "unalias",Cmd_UnAlias_f, "remove an alias");
1705 Cmd_AddCommand(CMD_SHARED, "set", Cvar_Set_f, "create or change the value of a console variable");
1706 Cmd_AddCommand(CMD_SHARED, "seta", Cvar_SetA_f, "create or change the value of a console variable that will be saved to config.cfg");
1707 Cmd_AddCommand(CMD_SHARED, "unset", Cvar_Del_f, "delete a cvar (does not work for static ones like _cl_name, or read-only ones)");
1709 #ifdef FILLALLCVARSWITHRUBBISH
1710 Cmd_AddCommand(CMD_SHARED, "fillallcvarswithrubbish", Cvar_FillAll_f, "fill all cvars with a specified number of characters to provoke buffer overruns");
1711 #endif /* FILLALLCVARSWITHRUBBISH */
1713 // 2000-01-09 CmdList, CvarList commands By Matthias "Maddes" Buecher
1714 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
1715 Cmd_AddCommand(CMD_SHARED, "cmdlist", Cmd_List_f, "lists all console commands beginning with the specified prefix or matching the specified wildcard pattern");
1716 Cmd_AddCommand(CMD_SHARED, "cvarlist", Cvar_List_f, "lists all console variables beginning with the specified prefix or matching the specified wildcard pattern");
1717 Cmd_AddCommand(CMD_SHARED, "apropos", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1718 Cmd_AddCommand(CMD_SHARED, "find", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1720 Cmd_AddCommand(CMD_SHARED, "defer", Cmd_Defer_f, "execute a command in the future");
1723 // Support Doom3-style Toggle Command
1724 Cmd_AddCommand(CMD_SHARED | CMD_CLIENT_FROM_SERVER, "toggle", Cmd_Toggle_f, "toggles a console variable's values (use for more info)");
1732 void Cmd_Shutdown(void)
1734 cmd_iter_t *cmd_iter;
1735 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
1737 cmd_state_t *cmd = cmd_iter->cmd;
1739 if (cmd->cbuf->lock)
1741 // we usually have this locked when we get here from Host_Quit_f
1742 Cbuf_Unlock(cmd->cbuf);
1745 Mem_FreePool(&cmd->mempool);
1754 int Cmd_Argc (cmd_state_t *cmd)
1764 const char *Cmd_Argv(cmd_state_t *cmd, int arg)
1766 if (arg >= cmd->argc )
1767 return cmd->null_string;
1768 return cmd->argv[arg];
1776 const char *Cmd_Args (cmd_state_t *cmd)
1785 Parses the given string into command line tokens.
1788 // AK: This function should only be called from ExcuteString because the current design is a bit of an hack
1789 static void Cmd_TokenizeString (cmd_state_t *cmd, const char *text)
1798 // skip whitespace up to a /n
1799 while (*text && ISWHITESPACE(*text) && *text != '\r' && *text != '\n')
1806 if (*text == '\n' || *text == '\r')
1808 // a newline separates commands in the buffer
1809 if (*text == '\r' && text[1] == '\n')
1821 if (!COM_ParseToken_Console(&text))
1824 if (cmd->argc < MAX_ARGS)
1826 l = (int)strlen(com_token) + 1;
1827 if (cmd->cbuf->tokenizebufferpos + l > CMD_TOKENIZELENGTH)
1829 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguments\n", CMD_TOKENIZELENGTH);
1832 memcpy (cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos, com_token, l);
1833 cmd->argv[cmd->argc] = cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos;
1834 cmd->cbuf->tokenizebufferpos += l;
1846 void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const char *description)
1848 cmd_function_t *func;
1849 cmd_function_t *prev, *current;
1851 xcommand_t save = NULL;
1852 qboolean auto_add = false;
1855 for (i = 0; i < 3; i++)
1857 cmd = cmd_iter_all[i].cmd;
1858 if ((flags & cmd->cmd_flags) || (flags & cmd->auto_flags))
1860 if((flags & cmd->auto_flags) && cmd->auto_function)
1863 function = cmd->auto_function;
1867 // fail if the command is a variable name
1868 if (Cvar_FindVar(cmd->cvars, cmd_name, ~0))
1870 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
1876 // fail if the command already exists in this interpreter
1877 for (func = cmd->engine_functions; func; func = func->next)
1879 if (!strcmp(cmd_name, func->name))
1881 if(func->autofunc && !auto_add)
1883 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
1888 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1889 func->flags = flags;
1890 func->name = cmd_name;
1891 func->function = function;
1892 func->description = description;
1893 func->next = cmd->engine_functions;
1894 func->autofunc = auto_add;
1896 // insert it at the right alphanumeric position
1897 for (prev = NULL, current = cmd->engine_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1903 cmd->engine_functions = func;
1905 func->next = current;
1909 // mark csqcfunc if the function already exists in the csqc_functions list
1910 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1912 if (!strcmp(cmd_name, func->name))
1914 func->csqcfunc = true; //[515]: csqc
1920 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1921 func->name = cmd_name;
1922 func->function = function;
1923 func->description = description;
1924 func->csqcfunc = true; //[515]: csqc
1925 func->next = cmd->userdefined->csqc_functions;
1926 func->autofunc = false;
1928 // insert it at the right alphanumeric position
1929 for (prev = NULL, current = cmd->userdefined->csqc_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1935 cmd->userdefined->csqc_functions = func;
1937 func->next = current;
1953 qboolean Cmd_Exists (cmd_state_t *cmd, const char *cmd_name)
1955 cmd_function_t *func;
1957 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1958 if (!strcmp(cmd_name, func->name))
1961 for (func=cmd->engine_functions ; func ; func=func->next)
1962 if (!strcmp (cmd_name,func->name))
1974 const char *Cmd_CompleteCommand (cmd_state_t *cmd, const char *partial)
1976 cmd_function_t *func;
1979 len = strlen(partial);
1985 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1986 if (!strncasecmp(partial, func->name, len))
1989 for (func = cmd->engine_functions; func; func = func->next)
1990 if (!strncasecmp(partial, func->name, len))
1997 Cmd_CompleteCountPossible
1999 New function for tab-completion system
2000 Added by EvilTypeGuy
2001 Thanks to Fett erich@heintz.com
2005 int Cmd_CompleteCountPossible (cmd_state_t *cmd, const char *partial)
2007 cmd_function_t *func;
2012 len = strlen(partial);
2017 // Loop through the command list and count all partial matches
2018 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
2019 if (!strncasecmp(partial, func->name, len))
2022 for (func = cmd->engine_functions; func; func = func->next)
2023 if (!strncasecmp(partial, func->name, len))
2030 Cmd_CompleteBuildList
2032 New function for tab-completion system
2033 Added by EvilTypeGuy
2034 Thanks to Fett erich@heintz.com
2038 const char **Cmd_CompleteBuildList (cmd_state_t *cmd, const char *partial)
2040 cmd_function_t *func;
2043 size_t sizeofbuf = (Cmd_CompleteCountPossible (cmd, partial) + 1) * sizeof (const char *);
2046 len = strlen(partial);
2047 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
2048 // Loop through the functions lists and print all matches
2049 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
2050 if (!strncasecmp(partial, func->name, len))
2051 buf[bpos++] = func->name;
2052 for (func = cmd->engine_functions; func; func = func->next)
2053 if (!strncasecmp(partial, func->name, len))
2054 buf[bpos++] = func->name;
2060 // written by LadyHavoc
2061 void Cmd_CompleteCommandPrint (cmd_state_t *cmd, const char *partial)
2063 cmd_function_t *func;
2064 size_t len = strlen(partial);
2065 // Loop through the command list and print all matches
2066 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
2067 if (!strncasecmp(partial, func->name, len))
2068 Con_Printf("^2%s^7: %s\n", func->name, func->description);
2069 for (func = cmd->engine_functions; func; func = func->next)
2070 if (!strncasecmp(partial, func->name, len))
2071 Con_Printf("^2%s^7: %s\n", func->name, func->description);
2077 New function for tab-completion system
2078 Added by EvilTypeGuy
2079 Thanks to Fett erich@heintz.com
2083 const char *Cmd_CompleteAlias (cmd_state_t *cmd, const char *partial)
2088 len = strlen(partial);
2094 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2095 if (!strncasecmp(partial, alias->name, len))
2101 // written by LadyHavoc
2102 void Cmd_CompleteAliasPrint (cmd_state_t *cmd, const char *partial)
2105 size_t len = strlen(partial);
2106 // Loop through the alias list and print all matches
2107 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2108 if (!strncasecmp(partial, alias->name, len))
2109 Con_Printf("^5%s^7: %s", alias->name, alias->value);
2114 Cmd_CompleteAliasCountPossible
2116 New function for tab-completion system
2117 Added by EvilTypeGuy
2118 Thanks to Fett erich@heintz.com
2122 int Cmd_CompleteAliasCountPossible (cmd_state_t *cmd, const char *partial)
2130 len = strlen(partial);
2135 // Loop through the command list and count all partial matches
2136 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2137 if (!strncasecmp(partial, alias->name, len))
2144 Cmd_CompleteAliasBuildList
2146 New function for tab-completion system
2147 Added by EvilTypeGuy
2148 Thanks to Fett erich@heintz.com
2152 const char **Cmd_CompleteAliasBuildList (cmd_state_t *cmd, const char *partial)
2157 size_t sizeofbuf = (Cmd_CompleteAliasCountPossible (cmd, partial) + 1) * sizeof (const char *);
2160 len = strlen(partial);
2161 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
2162 // Loop through the alias list and print all matches
2163 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2164 if (!strncasecmp(partial, alias->name, len))
2165 buf[bpos++] = alias->name;
2171 // TODO: Make this more generic?
2172 void Cmd_ClearCSQCCommands (cmd_state_t *cmd)
2174 cmd_function_t *func;
2175 cmd_function_t **next = &cmd->userdefined->csqc_functions;
2185 extern cvar_t sv_cheats;
2191 A complete command line has been parsed, so try to execute it
2192 FIXME: lookupnoadd the token to speed search?
2195 void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qboolean lockmutex)
2198 cmd_function_t *func;
2201 Cbuf_Lock(cmd->cbuf);
2202 oldpos = cmd->cbuf->tokenizebufferpos;
2205 Cmd_TokenizeString (cmd, text);
2207 // execute the command line
2209 goto done; // no tokens
2212 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
2214 if (!strcasecmp(cmd->argv[0], func->name))
2216 if (func->csqcfunc && CL_VM_ConsoleCommand(text)) //[515]: csqc
2222 for (func = cmd->engine_functions; func; func=func->next)
2224 if (!strcasecmp (cmd->argv[0], func->name))
2230 func->function(cmd);
2232 Con_Printf("Command \"%s\" can not be executed\n", Cmd_Argv(cmd, 0));
2237 if((func->flags & CMD_CHEAT) && !sv_cheats.integer)
2238 SV_ClientPrintf("No cheats allowed. The server must have sv_cheats set to 1\n");
2240 func->function(cmd);
2248 // if it's a client command and no command was found, say so.
2249 if (cmd->source == src_client)
2251 Con_Printf("Client \"%s\" tried to execute \"%s\"\n", host_client->name, text);
2256 for (a=cmd->userdefined->alias ; a ; a=a->next)
2258 if (!strcasecmp (cmd->argv[0], a->name))
2260 Cmd_ExecuteAlias(cmd, a);
2266 if (!Cvar_Command(cmd) && host.framecount > 0)
2267 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(cmd, 0));
2269 cmd->cbuf->tokenizebufferpos = oldpos;
2271 Cbuf_Unlock(cmd->cbuf);
2278 Returns the position (1 to argc-1) in the command's argument list
2279 where the given parameter apears, or 0 if not present
2283 int Cmd_CheckParm (cmd_state_t *cmd, const char *parm)
2289 Con_Printf ("Cmd_CheckParm: NULL");
2293 for (i = 1; i < Cmd_Argc (cmd); i++)
2294 if (!strcasecmp (parm, Cmd_Argv(cmd, i)))
2302 void Cmd_SaveInitState(void)
2304 cmd_iter_t *cmd_iter;
2305 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2307 cmd_state_t *cmd = cmd_iter->cmd;
2310 for (f = cmd->userdefined->csqc_functions; f; f = f->next)
2311 f->initstate = true;
2312 for (f = cmd->engine_functions; f; f = f->next)
2313 f->initstate = true;
2314 for (a = cmd->userdefined->alias; a; a = a->next)
2316 a->initstate = true;
2317 a->initialvalue = Mem_strdup(zonemempool, a->value);
2320 Cvar_SaveInitState(&cvars_all);
2323 void Cmd_RestoreInitState(void)
2325 cmd_iter_t *cmd_iter;
2326 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2328 cmd_state_t *cmd = cmd_iter->cmd;
2329 cmd_function_t *f, **fp;
2330 cmdalias_t *a, **ap;
2331 for (fp = &cmd->userdefined->csqc_functions; (f = *fp);)
2337 // destroy this command, it didn't exist at init
2338 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2343 for (fp = &cmd->engine_functions; (f = *fp);)
2349 // destroy this command, it didn't exist at init
2350 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2355 for (ap = &cmd->userdefined->alias; (a = *ap);)
2359 // restore this alias, it existed at init
2360 if (strcmp(a->value ? a->value : "", a->initialvalue ? a->initialvalue : ""))
2362 Con_DPrintf("Cmd_RestoreInitState: Restoring alias %s\n", a->name);
2365 a->value = Mem_strdup(zonemempool, a->initialvalue);
2371 // free this alias, it didn't exist at init...
2372 Con_DPrintf("Cmd_RestoreInitState: Destroying alias %s\n", a->name);
2380 Cvar_RestoreInitState(&cvars_all);