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 cbuf_cmd_t *Cbuf_LinkGet(cbuf_t *cbuf, cbuf_cmd_t *existing);
84 static cbuf_cmd_t *Cbuf_LinkPop(cbuf_cmd_t **list);
85 static void Cbuf_LinkAdd(cbuf_cmd_t *add, cbuf_cmd_t **list);
86 static void Cmd_Defer_f (cmd_state_t *cmd)
89 cbuf_t *cbuf = cmd->cbuf;
91 if(Cmd_Argc(cmd) == 1)
93 current = cbuf->deferred;
95 Con_Printf("No commands are pending.\n");
96 else if (current == cbuf->deferred)
100 while(current != cbuf->deferred)
103 Con_Printf("-> In %9.2f: %s\n", current->delay, current->text);
104 current = current->next;
108 else if(Cmd_Argc(cmd) == 2 && !strcasecmp("clear", Cmd_Argv(cmd, 1)))
110 while(cbuf->deferred)
112 current = Cbuf_LinkPop(&cbuf->deferred);
113 current->prev = current->next = current;
114 Cbuf_LinkAdd(current, &cbuf->free);
117 else if(Cmd_Argc(cmd) == 3)
119 const char *text = Cmd_Argv(cmd, 2);
120 size_t len = strlen(text);
121 current = Cbuf_LinkGet(cbuf, NULL);
123 current->delay = atof(Cmd_Argv(cmd, 1));
124 memcpy(current->text, text, len+1);
125 current->source = cmd;
127 current->prev = current->next = current;
129 Cbuf_LinkInsert(current, &cbuf->deferred);
134 Con_Printf("usage: defer <seconds> <command>\n"
144 Print something to the center of the screen using SCR_Centerprint
147 static void Cmd_Centerprint_f (cmd_state_t *cmd)
149 char msg[MAX_INPUTLINE];
150 unsigned int i, c, p;
154 strlcpy(msg, Cmd_Argv(cmd,1), sizeof(msg));
155 for(i = 2; i < c; ++i)
157 strlcat(msg, " ", sizeof(msg));
158 strlcat(msg, Cmd_Argv(cmd, i), sizeof(msg));
160 c = (unsigned int)strlen(msg);
161 for(p = 0, i = 0; i < c; ++i)
167 else if(msg[i+1] == '\\')
179 SCR_CenterPrint(msg);
184 =============================================================================
188 =============================================================================
191 static void Cbuf_LinkAdd(cbuf_cmd_t *add, cbuf_cmd_t **list)
197 cbuf_cmd_t *temp = add->prev;
198 add->prev->next = *list;
199 add->prev = (*list)->prev;
200 (*list)->prev->next = add;
201 (*list)->prev = temp;
205 static void Cbuf_LinkInsert(cbuf_cmd_t *insert, cbuf_cmd_t **list)
207 // Same algorithm, but backwards
209 Cbuf_LinkAdd(*list, &insert);
213 static cbuf_cmd_t *Cbuf_LinkPop(cbuf_cmd_t **list)
215 cbuf_cmd_t *node = *list;
217 (*list)->prev = node->prev;
218 (*list)->prev->next = *list;
228 Parses Quake console command-line
229 Returns size of parsed command-line
232 static size_t Cbuf_ParseText(char **in)
235 qboolean quotes = false;
236 qboolean comment = false; // Does not imply end because we might be starting the line with a comment.
237 qboolean escaped = false;
238 qboolean end = false; // Reached the end of a valid command
239 char *offset = NULL; // Non-NULL if valid command. Used by the caller to know where to start copying.
240 size_t cmdsize = 0; // Non-zero if valid command. Basically bytes to copy for the caller.
243 * Allow escapes in quotes. Ignore newlines and
244 * comments. Return 0 if input consists solely
245 * of either of those, and ignore blank input.
252 if(!quotes && (*in)[i+1] == '/' && (i == 0 || ISWHITESPACE((*in)[i-1])))
257 // Use bit magic to indicate an incomplete (pending) command.
285 if (!escaped && quotes)
295 offset = (char *)&(*in)[i];
310 static cbuf_cmd_t *Cbuf_LinkGet(cbuf_t *cbuf, cbuf_cmd_t *existing)
312 cbuf_cmd_t *ret = NULL;
313 if(existing && existing->pending)
318 ret = Cbuf_LinkPop(&cbuf->free);
320 ret = (cbuf_cmd_t *)Z_Malloc(sizeof(cbuf_cmd_t));
322 ret->pending = false;
329 // Cloudwalk: Not happy with this, but it works.
330 static cbuf_cmd_t *Cbuf_LinkCreate(cmd_state_t *cmd, cbuf_cmd_t *existing, const char *text)
332 char *in = (char *)&text[0];
333 cbuf_t *cbuf = cmd->cbuf;
334 size_t totalsize = 0, newsize = 0;
335 cbuf_cmd_t *current = NULL, *head = NULL;
337 // Slide the pointer down until we reach the end
341 * FIXME: Upon reaching a terminator, we make a redundant
342 * call just to say "it's the end of the input stream".
344 newsize = Cbuf_ParseText(&in);
350 current = Cbuf_LinkGet(cbuf, existing);
352 if(!current->pending)
354 current->source = cmd;
355 current->prev = current->next = current;
356 Cbuf_LinkAdd(current, &head);
359 if(newsize & (1<<17))
360 current->pending = true;
361 totalsize += (newsize &= ~(1<<17));
362 strlcpy(¤t->text[current->size], in, newsize + 1);
363 current->size += newsize;
365 else if (existing && !totalsize)
366 existing->pending = false;
371 cbuf->size += totalsize;
380 Adds command text at the end of the buffer
383 void Cbuf_AddText (cmd_state_t *cmd, const char *text)
385 size_t l = strlen(text);
386 cbuf_t *cbuf = cmd->cbuf;
387 cbuf_cmd_t *add = NULL;
391 if (cbuf->maxsize - cbuf->size <= l)
392 Con_Print("Cbuf_AddText: overflow\n");
395 if(!(add = Cbuf_LinkCreate(cmd, cbuf->start ? cbuf->start->prev : NULL, text)))
398 Cbuf_LinkAdd(add, &cbuf->start);
407 Adds command text immediately after the current command
408 FIXME: actually change the command buffer to do less copying
411 void Cbuf_InsertText (cmd_state_t *cmd, const char *text)
413 cbuf_t *cbuf = cmd->cbuf;
414 cbuf_cmd_t *insert = NULL;
415 size_t l = strlen(text);
419 // we need to memmove the existing text and stuff this in before it...
420 if (cbuf->size + l >= (size_t)cbuf->maxsize)
421 Con_Print("Cbuf_InsertText: overflow\n");
424 if(!(insert = Cbuf_LinkCreate(cmd, cbuf->start, text)))
427 Cbuf_LinkInsert(insert, &cbuf->start);
435 Cbuf_Execute_Deferred --blub
438 static void Cbuf_Execute_Deferred (cbuf_t *cbuf)
443 if (host.realtime - cbuf->deferred_oldtime < 0 || host.realtime - cbuf->deferred_oldtime > 1800)
444 cbuf->deferred_oldtime = host.realtime;
445 eat = host.realtime - cbuf->deferred_oldtime;
446 if (eat < (1.0 / 120.0))
448 cbuf->deferred_oldtime = host.realtime;
452 cbuf->deferred->delay -= eat;
453 if(cbuf->deferred->delay <= 0)
455 current = Cbuf_LinkPop(&cbuf->deferred);
456 Cbuf_AddText(current->source, current->text);
457 Cbuf_AddText(current->source, ";\n");
459 current->prev = current->next = current;
460 Cbuf_LinkAdd(current, &cbuf->free);
470 static qboolean Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias );
471 void Cbuf_Execute (cbuf_t *cbuf)
474 char preprocessed[MAX_INPUTLINE];
477 // LadyHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
478 cbuf->tokenizebufferpos = 0;
483 * Delete the text from the command buffer and move remaining
484 * commands down. This is necessary because commands (exec, alias)
485 * can insert data at the beginning of the text buffer
487 current = Cbuf_LinkPop(&cbuf->start);
490 * Assume we're rolling with the current command-line and
491 * always set this false because alias expansion or cbuf insertion
492 * without a newline may set this true, and cause weirdness.
494 current->pending = false;
496 cbuf->size -= current->size;
498 // Infinite loop if aliases expand without this
502 firstchar = current->text;
503 while(*firstchar && ISWHITESPACE(*firstchar))
505 if((strncmp(firstchar, "alias", 5) || !ISWHITESPACE(firstchar[5])) &&
506 (strncmp(firstchar, "bind", 4) || !ISWHITESPACE(firstchar[4])) &&
507 (strncmp(firstchar, "in_bind", 7) || !ISWHITESPACE(firstchar[7])))
509 if(Cmd_PreprocessString(current->source, current->text, preprocessed, sizeof(preprocessed), NULL ))
510 Cmd_ExecuteString(current->source, preprocessed, src_local, false);
514 Cmd_ExecuteString (current->source, current->text, src_local, false);
517 // Recycle memory so using WASD doesn't cause a malloc and free
518 current->prev = current->next = current;
520 Cbuf_LinkAdd(current, &cbuf->free);
527 * Skip out while text still remains in
528 * buffer, leaving it for next frame
536 void Cbuf_Frame(cbuf_t *cbuf)
538 Cbuf_Execute_Deferred(cbuf);
541 SV_LockThreadMutex();
543 SV_UnlockThreadMutex();
548 ==============================================================================
552 ==============================================================================
559 Adds command line parameters as script statements
560 Commands lead with a +, and continue until a - or another +
561 quake +prog jctest.qp +cmd amlev1
562 quake -nosound +cmd amlev1
565 static void Cmd_StuffCmds_f (cmd_state_t *cmd)
568 // this is for all commandline options combined (and is bounds checked)
569 char build[MAX_INPUTLINE];
571 // come back later so we don't crash
572 if(host.state == host_init)
575 if (Cmd_Argc (cmd) != 1)
577 Con_Print("stuffcmds : execute command line parameters\n");
581 // no reason to run the commandline arguments twice
582 if (host_stuffcmdsrun)
585 host_stuffcmdsrun = true;
588 for (i = 0;i < sys.argc;i++)
590 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)
593 while (sys.argv[i][j])
594 build[l++] = sys.argv[i][j++];
596 for (;i < sys.argc;i++)
600 if ((sys.argv[i][0] == '+' || sys.argv[i][0] == '-') && (sys.argv[i][1] < '0' || sys.argv[i][1] > '9'))
602 if (l + strlen(sys.argv[i]) + 4 > sizeof(build) - 1)
605 if (strchr(sys.argv[i], ' '))
607 for (j = 0;sys.argv[i][j];j++)
608 build[l++] = sys.argv[i][j];
609 if (strchr(sys.argv[i], ' '))
616 // now terminate the combined string and prepend it to the command buffer
617 // we already reserved space for the terminator
619 Cbuf_InsertText (cmd, build);
622 static void Cmd_Exec(cmd_state_t *cmd, const char *filename)
625 size_t filenameLen = strlen(filename);
626 qboolean isdefaultcfg =
627 !strcmp(filename, "default.cfg") ||
628 (filenameLen >= 12 && !strcmp(filename + filenameLen - 12, "/default.cfg"));
630 if (!strcmp(filename, "config.cfg"))
632 filename = CONFIGFILENAME;
633 if (COM_CheckParm("-noconfig"))
634 return; // don't execute config.cfg
637 f = (char *)FS_LoadFile (filename, tempmempool, false, NULL);
640 Con_Printf("couldn't exec %s\n",filename);
643 Con_Printf("execing %s\n",filename);
645 // if executing default.cfg for the first time, lock the cvar defaults
646 // it may seem backwards to insert this text BEFORE the default.cfg
647 // but Cbuf_InsertText inserts before, so this actually ends up after it.
649 Cbuf_InsertText(cmd, "\ncvar_lockdefaults\n");
651 Cbuf_InsertText (cmd, f);
656 // special defaults for specific games go here, these execute before default.cfg
657 // Nehahra pushable crates malfunction in some levels if this is on
658 // Nehahra NPC AI is confused by blowupfallenzombies
662 Cbuf_InsertText(cmd, "\n"
663 "sv_gameplayfix_blowupfallenzombies 0\n"
664 "sv_gameplayfix_findradiusdistancetobox 0\n"
665 "sv_gameplayfix_grenadebouncedownslopes 0\n"
666 "sv_gameplayfix_slidemoveprojectiles 0\n"
667 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
668 "sv_gameplayfix_setmodelrealbox 0\n"
669 "sv_gameplayfix_droptofloorstartsolid 0\n"
670 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
671 "sv_gameplayfix_noairborncorpse 0\n"
672 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
673 "sv_gameplayfix_easierwaterjump 0\n"
674 "sv_gameplayfix_delayprojectiles 0\n"
675 "sv_gameplayfix_multiplethinksperframe 0\n"
676 "sv_gameplayfix_fixedcheckwatertransition 0\n"
677 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
678 "sv_gameplayfix_swiminbmodels 0\n"
679 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
680 "sys_ticrate 0.01388889\n"
682 "r_shadow_bumpscale_basetexture 0\n"
683 "csqc_polygons_defaultmaterial_nocullface 0\n"
687 Cbuf_InsertText(cmd, "\n"
688 "sv_gameplayfix_blowupfallenzombies 0\n"
689 "sv_gameplayfix_findradiusdistancetobox 0\n"
690 "sv_gameplayfix_grenadebouncedownslopes 0\n"
691 "sv_gameplayfix_slidemoveprojectiles 0\n"
692 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
693 "sv_gameplayfix_setmodelrealbox 0\n"
694 "sv_gameplayfix_droptofloorstartsolid 0\n"
695 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
696 "sv_gameplayfix_noairborncorpse 0\n"
697 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
698 "sv_gameplayfix_easierwaterjump 0\n"
699 "sv_gameplayfix_delayprojectiles 0\n"
700 "sv_gameplayfix_multiplethinksperframe 0\n"
701 "sv_gameplayfix_fixedcheckwatertransition 0\n"
702 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
703 "sv_gameplayfix_swiminbmodels 0\n"
704 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
705 "sys_ticrate 0.01388889\n"
707 "r_shadow_bumpscale_basetexture 0\n"
708 "csqc_polygons_defaultmaterial_nocullface 0\n"
711 // 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.
712 // 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
713 // hipnotic mission pack has issues in their proximity mine sticking code, which causes them to bounce off.
716 Cbuf_InsertText(cmd, "\n"
717 "sv_gameplayfix_blowupfallenzombies 0\n"
718 "sv_gameplayfix_findradiusdistancetobox 0\n"
719 "sv_gameplayfix_grenadebouncedownslopes 0\n"
720 "sv_gameplayfix_slidemoveprojectiles 0\n"
721 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
722 "sv_gameplayfix_setmodelrealbox 0\n"
723 "sv_gameplayfix_droptofloorstartsolid 0\n"
724 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
725 "sv_gameplayfix_noairborncorpse 0\n"
726 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
727 "sv_gameplayfix_easierwaterjump 0\n"
728 "sv_gameplayfix_delayprojectiles 0\n"
729 "sv_gameplayfix_multiplethinksperframe 0\n"
730 "sv_gameplayfix_fixedcheckwatertransition 0\n"
731 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
732 "sv_gameplayfix_swiminbmodels 0\n"
733 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
736 "r_shadow_bumpscale_basetexture 0\n"
737 "csqc_polygons_defaultmaterial_nocullface 0\n"
740 // rogue mission pack has a guardian boss that does not wake up if findradius returns one of the entities around its spawn area
742 Cbuf_InsertText(cmd, "\n"
743 "sv_gameplayfix_blowupfallenzombies 0\n"
744 "sv_gameplayfix_findradiusdistancetobox 0\n"
745 "sv_gameplayfix_grenadebouncedownslopes 0\n"
746 "sv_gameplayfix_slidemoveprojectiles 0\n"
747 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
748 "sv_gameplayfix_setmodelrealbox 0\n"
749 "sv_gameplayfix_droptofloorstartsolid 0\n"
750 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
751 "sv_gameplayfix_noairborncorpse 0\n"
752 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
753 "sv_gameplayfix_easierwaterjump 0\n"
754 "sv_gameplayfix_delayprojectiles 0\n"
755 "sv_gameplayfix_multiplethinksperframe 0\n"
756 "sv_gameplayfix_fixedcheckwatertransition 0\n"
757 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
758 "sv_gameplayfix_swiminbmodels 0\n"
759 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
760 "sys_ticrate 0.01388889\n"
762 "r_shadow_bumpscale_basetexture 0\n"
763 "csqc_polygons_defaultmaterial_nocullface 0\n"
767 Cbuf_InsertText(cmd, "\n"
768 "sv_gameplayfix_blowupfallenzombies 0\n"
769 "sv_gameplayfix_findradiusdistancetobox 0\n"
770 "sv_gameplayfix_grenadebouncedownslopes 0\n"
771 "sv_gameplayfix_slidemoveprojectiles 0\n"
772 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
773 "sv_gameplayfix_setmodelrealbox 0\n"
774 "sv_gameplayfix_droptofloorstartsolid 0\n"
775 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
776 "sv_gameplayfix_noairborncorpse 0\n"
777 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
778 "sv_gameplayfix_easierwaterjump 0\n"
779 "sv_gameplayfix_delayprojectiles 0\n"
780 "sv_gameplayfix_multiplethinksperframe 0\n"
781 "sv_gameplayfix_fixedcheckwatertransition 0\n"
782 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
783 "sv_gameplayfix_swiminbmodels 0\n"
784 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
785 "sys_ticrate 0.01388889\n"
787 "r_shadow_bumpscale_basetexture 4\n"
788 "csqc_polygons_defaultmaterial_nocullface 0\n"
792 Cbuf_InsertText(cmd, "\n"
793 "sv_gameplayfix_blowupfallenzombies 1\n"
794 "sv_gameplayfix_findradiusdistancetobox 1\n"
795 "sv_gameplayfix_grenadebouncedownslopes 1\n"
796 "sv_gameplayfix_slidemoveprojectiles 1\n"
797 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
798 "sv_gameplayfix_setmodelrealbox 1\n"
799 "sv_gameplayfix_droptofloorstartsolid 1\n"
800 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
801 "sv_gameplayfix_noairborncorpse 1\n"
802 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
803 "sv_gameplayfix_easierwaterjump 1\n"
804 "sv_gameplayfix_delayprojectiles 1\n"
805 "sv_gameplayfix_multiplethinksperframe 1\n"
806 "sv_gameplayfix_fixedcheckwatertransition 1\n"
807 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
808 "sv_gameplayfix_swiminbmodels 1\n"
809 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
810 "sys_ticrate 0.01388889\n"
811 "sv_gameplayfix_q2airaccelerate 1\n"
812 "sv_gameplayfix_stepmultipletimes 1\n"
813 "csqc_polygons_defaultmaterial_nocullface 1\n"
814 "con_chatsound_team_mask 13\n"
818 case GAME_VORETOURNAMENT:
819 // 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
820 Cbuf_InsertText(cmd, "\n"
821 "csqc_polygons_defaultmaterial_nocullface 1\n"
822 "con_chatsound_team_mask 13\n"
823 "sv_gameplayfix_customstats 1\n"
826 // Steel Storm: Burning Retribution csqc misinterprets CSQC_InputEvent if type is a value other than 0 or 1
827 case GAME_STEELSTORM:
828 Cbuf_InsertText(cmd, "\n"
829 "sv_gameplayfix_blowupfallenzombies 1\n"
830 "sv_gameplayfix_findradiusdistancetobox 1\n"
831 "sv_gameplayfix_grenadebouncedownslopes 1\n"
832 "sv_gameplayfix_slidemoveprojectiles 1\n"
833 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
834 "sv_gameplayfix_setmodelrealbox 1\n"
835 "sv_gameplayfix_droptofloorstartsolid 1\n"
836 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
837 "sv_gameplayfix_noairborncorpse 1\n"
838 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
839 "sv_gameplayfix_easierwaterjump 1\n"
840 "sv_gameplayfix_delayprojectiles 1\n"
841 "sv_gameplayfix_multiplethinksperframe 1\n"
842 "sv_gameplayfix_fixedcheckwatertransition 1\n"
843 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
844 "sv_gameplayfix_swiminbmodels 1\n"
845 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
846 "sys_ticrate 0.01388889\n"
847 "cl_csqc_generatemousemoveevents 0\n"
848 "csqc_polygons_defaultmaterial_nocullface 1\n"
852 Cbuf_InsertText(cmd, "\n"
853 "sv_gameplayfix_blowupfallenzombies 1\n"
854 "sv_gameplayfix_findradiusdistancetobox 1\n"
855 "sv_gameplayfix_grenadebouncedownslopes 1\n"
856 "sv_gameplayfix_slidemoveprojectiles 1\n"
857 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
858 "sv_gameplayfix_setmodelrealbox 1\n"
859 "sv_gameplayfix_droptofloorstartsolid 1\n"
860 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
861 "sv_gameplayfix_noairborncorpse 1\n"
862 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
863 "sv_gameplayfix_easierwaterjump 1\n"
864 "sv_gameplayfix_delayprojectiles 1\n"
865 "sv_gameplayfix_multiplethinksperframe 1\n"
866 "sv_gameplayfix_fixedcheckwatertransition 1\n"
867 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
868 "sv_gameplayfix_swiminbmodels 1\n"
869 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
870 "sys_ticrate 0.01388889\n"
871 "csqc_polygons_defaultmaterial_nocullface 0\n"
883 static void Cmd_Exec_f (cmd_state_t *cmd)
888 if (Cmd_Argc(cmd) != 2)
890 Con_Print("exec <filename> : execute a script file\n");
894 s = FS_Search(Cmd_Argv(cmd, 1), true, true, NULL);
895 if(!s || !s->numfilenames)
897 Con_Printf("couldn't exec %s\n",Cmd_Argv(cmd, 1));
901 for(i = 0; i < s->numfilenames; ++i)
902 Cmd_Exec(cmd, s->filenames[i]);
912 Just prints the rest of the line to the console
915 static void Cmd_Echo_f (cmd_state_t *cmd)
919 for (i=1 ; i<Cmd_Argc(cmd) ; i++)
920 Con_Printf("%s ",Cmd_Argv(cmd, i));
925 // Support Doom3-style Toggle Console Command
930 Toggles a specified console variable amongst the values specified (default is 0 and 1)
933 static void Cmd_Toggle_f(cmd_state_t *cmd)
935 // Acquire Number of Arguments
936 int nNumArgs = Cmd_Argc(cmd);
939 // No Arguments Specified; Print Usage
940 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");
942 { // Correct Arguments Specified
943 // Acquire Potential CVar
944 cvar_t* cvCVar = Cvar_FindVar(cmd->cvars, Cmd_Argv(cmd, 1), cmd->cvars_flagsmask);
951 Cvar_SetValueQuick(cvCVar, 0);
953 Cvar_SetValueQuick(cvCVar, 1);
957 { // 0 and Specified Usage
958 if(cvCVar->integer == atoi(Cmd_Argv(cmd, 2) ) )
959 // CVar is Specified Value; // Reset to 0
960 Cvar_SetValueQuick(cvCVar, 0);
962 if(cvCVar->integer == 0)
963 // CVar is 0; Specify Value
964 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
966 // CVar does not match; Reset to 0
967 Cvar_SetValueQuick(cvCVar, 0);
970 { // Variable Values Specified
974 for(nCnt = 2; nCnt < nNumArgs; nCnt++)
975 { // Cycle through Values
976 if( strcmp(cvCVar->string, Cmd_Argv(cmd, nCnt) ) == 0)
977 { // Current Value Located; Increment to Next
978 if( (nCnt + 1) == nNumArgs)
979 // Max Value Reached; Reset
980 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
983 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, nCnt + 1) );
992 // Value not Found; Reset to Original
993 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
999 Con_Printf("ERROR : CVar '%s' not found\n", Cmd_Argv(cmd, 1) );
1008 Creates a new command that executes a command string (possibly ; seperated)
1011 static void Cmd_Alias_f (cmd_state_t *cmd)
1014 char line[MAX_INPUTLINE];
1019 if (Cmd_Argc(cmd) == 1)
1021 Con_Print("Current alias commands:\n");
1022 for (a = cmd->userdefined->alias ; a ; a=a->next)
1023 Con_Printf("%s : %s", a->name, a->value);
1027 s = Cmd_Argv(cmd, 1);
1028 if (strlen(s) >= MAX_ALIAS_NAME)
1030 Con_Print("Alias name is too long\n");
1034 // if the alias already exists, reuse it
1035 for (a = cmd->userdefined->alias ; a ; a=a->next)
1037 if (!strcmp(s, a->name))
1046 cmdalias_t *prev, *current;
1048 a = (cmdalias_t *)Z_Malloc (sizeof(cmdalias_t));
1049 strlcpy (a->name, s, sizeof (a->name));
1050 // insert it at the right alphanumeric position
1051 for( prev = NULL, current = cmd->userdefined->alias ; current && strcmp( current->name, a->name ) < 0 ; prev = current, current = current->next )
1056 cmd->userdefined->alias = a;
1062 // copy the rest of the command line
1063 line[0] = 0; // start out with a null string
1065 for (i=2 ; i < c ; i++)
1068 strlcat (line, " ", sizeof (line));
1069 strlcat (line, Cmd_Argv(cmd, i), sizeof (line));
1071 strlcat (line, "\n", sizeof (line));
1073 alloclen = strlen (line) + 1;
1075 line[alloclen - 2] = '\n'; // to make sure a newline is appended even if too long
1076 a->value = (char *)Z_Malloc (alloclen);
1077 memcpy (a->value, line, alloclen);
1084 Remove existing aliases.
1087 static void Cmd_UnAlias_f (cmd_state_t *cmd)
1093 if(Cmd_Argc(cmd) == 1)
1095 Con_Print("unalias: Usage: unalias alias1 [alias2 ...]\n");
1099 for(i = 1; i < Cmd_Argc(cmd); ++i)
1101 s = Cmd_Argv(cmd, i);
1103 for(a = cmd->userdefined->alias; a; p = a, a = a->next)
1105 if(!strcmp(s, a->name))
1107 if (a->initstate) // we can not remove init aliases
1109 if(a == cmd->userdefined->alias)
1110 cmd->userdefined->alias = a->next;
1119 Con_Printf("unalias: %s alias not found\n", s);
1124 =============================================================================
1128 =============================================================================
1131 static const char *Cmd_GetDirectCvarValue(cmd_state_t *cmd, const char *varname, cmdalias_t *alias, qboolean *is_multiple)
1136 static char vabuf[1024]; // cmd_mutex
1139 *is_multiple = false;
1141 if(!varname || !*varname)
1146 if(!strcmp(varname, "*"))
1149 *is_multiple = true;
1150 return Cmd_Args(cmd);
1152 else if(!strcmp(varname, "#"))
1154 return va(vabuf, sizeof(vabuf), "%d", Cmd_Argc(cmd));
1156 else if(varname[strlen(varname) - 1] == '-')
1158 argno = strtol(varname, &endptr, 10);
1159 if(endptr == varname + strlen(varname) - 1)
1161 // whole string is a number, apart from the -
1162 const char *p = Cmd_Args(cmd);
1163 for(; argno > 1; --argno)
1164 if(!COM_ParseToken_Console(&p))
1169 *is_multiple = true;
1171 // kill pre-argument whitespace
1172 for (;*p && ISWHITESPACE(*p);p++)
1181 argno = strtol(varname, &endptr, 10);
1184 // whole string is a number
1185 // NOTE: we already made sure we don't have an empty cvar name!
1186 if(argno >= 0 && argno < Cmd_Argc(cmd))
1187 return Cmd_Argv(cmd, argno);
1192 if((cvar = Cvar_FindVar(cmd->cvars, varname, cmd->cvars_flagsmask)) && !(cvar->flags & CVAR_PRIVATE))
1193 return cvar->string;
1198 qboolean Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset, qboolean putquotes)
1200 qboolean quote_quot = !!strchr(quoteset, '"');
1201 qboolean quote_backslash = !!strchr(quoteset, '\\');
1202 qboolean quote_dollar = !!strchr(quoteset, '$');
1211 *out++ = '"'; --outlen;
1217 if(*in == '"' && quote_quot)
1221 *out++ = '\\'; --outlen;
1222 *out++ = '"'; --outlen;
1224 else if(*in == '\\' && quote_backslash)
1228 *out++ = '\\'; --outlen;
1229 *out++ = '\\'; --outlen;
1231 else if(*in == '$' && quote_dollar)
1235 *out++ = '$'; --outlen;
1236 *out++ = '$'; --outlen;
1242 *out++ = *in; --outlen;
1257 static const char *Cmd_GetCvarValue(cmd_state_t *cmd, const char *var, size_t varlen, cmdalias_t *alias)
1259 static char varname[MAX_INPUTLINE]; // cmd_mutex
1260 static char varval[MAX_INPUTLINE]; // cmd_mutex
1261 const char *varstr = NULL;
1263 qboolean required = false;
1264 qboolean optional = false;
1265 static char asis[] = "asis"; // just to suppress const char warnings
1267 if(varlen >= MAX_INPUTLINE)
1268 varlen = MAX_INPUTLINE - 1;
1269 memcpy(varname, var, varlen);
1270 varname[varlen] = 0;
1271 varfunc = strchr(varname, ' ');
1283 Con_Printf(CON_WARN "Warning: Could not expand $ in alias %s\n", alias->name);
1285 Con_Printf(CON_WARN "Warning: Could not expand $\n");
1293 while((p = strchr(varfunc, '?')))
1296 memmove(p, p+1, strlen(p)); // with final NUL
1299 while((p = strchr(varfunc, '!')))
1302 memmove(p, p+1, strlen(p)); // with final NUL
1305 while((p = strchr(varfunc, ' ')))
1307 memmove(p, p+1, strlen(p)); // with final NUL
1309 // if no function is left, NULL it
1314 if(varname[0] == '$')
1315 varstr = Cmd_GetDirectCvarValue(cmd, Cmd_GetDirectCvarValue(cmd, varname + 1, alias, NULL), alias, NULL);
1318 qboolean is_multiple = false;
1319 // Exception: $* and $n- don't use the quoted form by default
1320 varstr = Cmd_GetDirectCvarValue(cmd, varname, alias, &is_multiple);
1331 Con_Printf(CON_ERROR "Error: Could not expand $%s in alias %s\n", varname, alias->name);
1333 Con_Printf(CON_ERROR "Error: Could not expand $%s\n", varname);
1343 Con_Printf(CON_WARN "Warning: Could not expand $%s in alias %s\n", varname, alias->name);
1345 Con_Printf(CON_WARN "Warning: Could not expand $%s\n", varname);
1346 dpsnprintf(varval, sizeof(varval), "$%s", varname);
1351 if(!varfunc || !strcmp(varfunc, "q")) // note: quoted form is default, use "asis" to override!
1353 // quote it so it can be used inside double quotes
1354 // we just need to replace " by \", and of course, double backslashes
1355 Cmd_QuoteString(varval, sizeof(varval), varstr, "\"\\", false);
1358 else if(!strcmp(varfunc, "asis"))
1363 Con_Printf("Unknown variable function %s\n", varfunc);
1369 Cmd_PreprocessString
1371 Preprocesses strings and replaces $*, $param#, $cvar accordingly. Also strips comments.
1373 static qboolean Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias ) {
1379 // don't crash if there's no room in the outtext buffer
1380 if( maxoutlen == 0 ) {
1383 maxoutlen--; // because of \0
1388 while( *in && outlen < maxoutlen ) {
1390 // this is some kind of expansion, see what comes after the $
1393 // The console does the following preprocessing:
1395 // - $$ is transformed to a single dollar sign.
1396 // - $var or ${var} are expanded to the contents of the named cvar,
1397 // with quotation marks and backslashes quoted so it can safely
1398 // be used inside quotation marks (and it should always be used
1400 // - ${var asis} inserts the cvar value as is, without doing this
1402 // - ${var ?} silently expands to the empty string if
1403 // $var does not exist
1404 // - ${var !} fails expansion and executes nothing if
1405 // $var does not exist
1406 // - prefix the cvar name with a dollar sign to do indirection;
1407 // for example, if $x has the value timelimit, ${$x} will return
1408 // the value of $timelimit
1409 // - when expanding an alias, the special variable name $* refers
1410 // to all alias parameters, and a number refers to that numbered
1411 // alias parameter, where the name of the alias is $0, the first
1412 // parameter is $1 and so on; as a special case, $* inserts all
1413 // parameters, without extra quoting, so one can use $* to just
1414 // pass all parameters around. All parameters starting from $n
1415 // can be referred to as $n- (so $* is equivalent to $1-).
1416 // - ${* q} and ${n- q} force quoting anyway
1418 // Note: when expanding an alias, cvar expansion is done in the SAME step
1419 // as alias expansion so that alias parameters or cvar values containing
1420 // dollar signs have no unwanted bad side effects. However, this needs to
1421 // be accounted for when writing complex aliases. For example,
1422 // alias foo "set x NEW; echo $x"
1423 // actually expands to
1424 // "set x NEW; echo OLD"
1425 // and will print OLD! To work around this, use a second alias:
1426 // alias foo "set x NEW; foo2"
1427 // alias foo2 "echo $x"
1429 // Also note: lines starting with alias are exempt from cvar expansion.
1430 // If you want cvar expansion, write "alias" instead:
1433 // alias foo "echo $x"
1434 // "alias" bar "echo $x"
1437 // foo will print 2, because the variable $x will be expanded when the alias
1438 // gets expanded. bar will print 1, because the variable $x was expanded
1439 // at definition time. foo can be equivalently defined as
1441 // "alias" foo "echo $$x"
1443 // because at definition time, $$ will get replaced to a single $.
1448 } else if(*in == '{') {
1449 varlen = strcspn(in + 1, "}");
1450 if(in[varlen + 1] == '}')
1452 val = Cmd_GetCvarValue(cmd, in + 1, varlen, alias);
1464 varlen = strspn(in, "#*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-");
1465 val = Cmd_GetCvarValue(cmd, in, varlen, alias);
1472 // insert the cvar value
1473 while(*val && outlen < maxoutlen)
1474 outtext[outlen++] = *val++;
1479 // copy the unexpanded text
1480 outtext[outlen++] = '$';
1481 while(eat && outlen < maxoutlen)
1483 outtext[outlen++] = *in++;
1489 outtext[outlen++] = *in++;
1491 outtext[outlen] = 0;
1499 Called for aliases and fills in the alias into the cbuffer
1502 static void Cmd_ExecuteAlias (cmd_state_t *cmd, cmdalias_t *alias)
1504 static char buffer[ MAX_INPUTLINE ]; // cmd_mutex
1505 static char buffer2[ MAX_INPUTLINE ]; // cmd_mutex
1506 qboolean ret = Cmd_PreprocessString( cmd, alias->value, buffer, sizeof(buffer) - 2, alias );
1509 // insert at start of command buffer, so that aliases execute in order
1510 // (fixes bug introduced by Black on 20050705)
1512 // Note: Cbuf_PreprocessString will be called on this string AGAIN! So we
1513 // have to make sure that no second variable expansion takes place, otherwise
1514 // alias parameters containing dollar signs can have bad effects.
1515 Cmd_QuoteString(buffer2, sizeof(buffer2), buffer, "$", false);
1516 Cbuf_InsertText(cmd, buffer2);
1523 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
1524 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
1528 static void Cmd_List_f (cmd_state_t *cmd)
1530 cmd_function_t *func;
1531 const char *partial;
1536 if (Cmd_Argc(cmd) > 1)
1538 partial = Cmd_Argv(cmd, 1);
1539 len = strlen(partial);
1540 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1550 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1552 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1554 Con_Printf("%s : %s\n", func->name, func->description);
1557 for (func = cmd->engine_functions; func; func = func->next)
1559 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1561 Con_Printf("%s : %s\n", func->name, func->description);
1568 Con_Printf("%i Command%s matching \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1570 Con_Printf("%i Command%s beginning with \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1573 Con_Printf("%i Command%s\n\n", count, (count > 1) ? "s" : "");
1576 static void Cmd_Apropos_f(cmd_state_t *cmd)
1578 cmd_function_t *func;
1581 const char *partial;
1586 if (Cmd_Argc(cmd) > 1)
1587 partial = Cmd_Args(cmd);
1590 Con_Printf("usage: %s <string>\n",Cmd_Argv(cmd, 0));
1594 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
1596 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
1599 for (cvar = cmd->cvars->vars; cvar; cvar = cvar->next)
1601 if (matchpattern_with_separator(cvar->name, partial, true, "", false) ||
1602 matchpattern_with_separator(cvar->description, partial, true, "", false))
1604 Con_Printf ("cvar ");
1605 Cvar_PrintHelp(cvar, cvar->name, true);
1608 for (int i = 0; i < cvar->aliasindex; i++)
1610 if (matchpattern_with_separator(cvar->aliases[i], partial, true, "", false))
1612 Con_Printf ("cvar ");
1613 Cvar_PrintHelp(cvar, cvar->aliases[i], true);
1618 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1620 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1621 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1623 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1626 for (func = cmd->engine_functions; func; func = func->next)
1628 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1629 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1631 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1634 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
1636 // procede here a bit differently as an alias value always got a final \n
1637 if (!matchpattern_with_separator(alias->name, partial, true, "", false))
1638 if (!matchpattern_with_separator(alias->value, partial, true, "\n", false)) // when \n is as separator wildcards don't match it
1640 Con_Printf("alias ^5%s^7: %s", alias->name, alias->value); // do not print an extra \n
1643 Con_Printf("%i result%s\n\n", count, (count > 1) ? "s" : "");
1653 cmd_iter_t *cmd_iter;
1654 cbuf_t *cbuf = (cbuf_t *)Z_Malloc(sizeof(cbuf_t));
1655 cbuf->maxsize = 655360;
1656 cbuf->lock = Thread_CreateMutex();
1660 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
1662 cmd_state_t *cmd = cmd_iter->cmd;
1663 cmd->mempool = Mem_AllocPool("commands", 0, NULL);
1664 // space for commands and script files
1666 cmd->null_string = "";
1668 // client console can see server cvars because the user may start a server
1669 cmd_client.cvars = &cvars_all;
1670 cmd_client.cvars_flagsmask = CVAR_CLIENT | CVAR_SERVER;
1671 cmd_client.cmd_flags = CMD_CLIENT | CMD_CLIENT_FROM_SERVER;
1672 cmd_client.auto_flags = CMD_SERVER_FROM_CLIENT;
1673 cmd_client.auto_function = CL_ForwardToServer_f; // FIXME: Move this to the client.
1674 cmd_client.userdefined = &cmd_userdefined_all;
1675 // dedicated server console can only see server cvars, there is no client
1676 cmd_server.cvars = &cvars_all;
1677 cmd_server.cvars_flagsmask = CVAR_SERVER;
1678 cmd_server.cmd_flags = CMD_SERVER;
1679 cmd_server.auto_flags = 0;
1680 cmd_server.auto_function = NULL;
1681 cmd_server.userdefined = &cmd_userdefined_all;
1682 // server commands received from clients have no reason to access cvars, cvar expansion seems perilous.
1683 cmd_serverfromclient.cvars = &cvars_null;
1684 cmd_serverfromclient.cvars_flagsmask = 0;
1685 cmd_serverfromclient.cmd_flags = CMD_SERVER_FROM_CLIENT | CMD_USERINFO;
1686 cmd_serverfromclient.auto_flags = 0;
1687 cmd_serverfromclient.auto_function = NULL;
1688 cmd_serverfromclient.userdefined = &cmd_userdefined_null;
1691 // register our commands
1693 // client-only commands
1694 Cmd_AddCommand(CMD_SHARED, "wait", Cmd_Wait_f, "make script execution wait for next rendered frame");
1695 Cmd_AddCommand(CMD_CLIENT, "cprint", Cmd_Centerprint_f, "print something at the screen center");
1697 // maintenance commands used for upkeep of cvars and saved configs
1698 Cmd_AddCommand(CMD_SHARED, "stuffcmds", Cmd_StuffCmds_f, "execute commandline parameters (must be present in quake.rc script)");
1699 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");
1700 Cmd_AddCommand(CMD_SHARED, "cvar_resettodefaults_all", Cvar_ResetToDefaults_All_f, "sets all cvars to their locked default values");
1701 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)");
1702 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)");
1704 // general console commands used in multiple environments
1705 Cmd_AddCommand(CMD_SHARED, "exec", Cmd_Exec_f, "execute a script file");
1706 Cmd_AddCommand(CMD_SHARED, "echo",Cmd_Echo_f, "print a message to the console (useful in scripts)");
1707 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");
1708 Cmd_AddCommand(CMD_SHARED, "unalias",Cmd_UnAlias_f, "remove an alias");
1709 Cmd_AddCommand(CMD_SHARED, "set", Cvar_Set_f, "create or change the value of a console variable");
1710 Cmd_AddCommand(CMD_SHARED, "seta", Cvar_SetA_f, "create or change the value of a console variable that will be saved to config.cfg");
1711 Cmd_AddCommand(CMD_SHARED, "unset", Cvar_Del_f, "delete a cvar (does not work for static ones like _cl_name, or read-only ones)");
1713 #ifdef FILLALLCVARSWITHRUBBISH
1714 Cmd_AddCommand(CMD_SHARED, "fillallcvarswithrubbish", Cvar_FillAll_f, "fill all cvars with a specified number of characters to provoke buffer overruns");
1715 #endif /* FILLALLCVARSWITHRUBBISH */
1717 // 2000-01-09 CmdList, CvarList commands By Matthias "Maddes" Buecher
1718 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
1719 Cmd_AddCommand(CMD_SHARED, "cmdlist", Cmd_List_f, "lists all console commands beginning with the specified prefix or matching the specified wildcard pattern");
1720 Cmd_AddCommand(CMD_SHARED, "cvarlist", Cvar_List_f, "lists all console variables beginning with the specified prefix or matching the specified wildcard pattern");
1721 Cmd_AddCommand(CMD_SHARED, "apropos", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1722 Cmd_AddCommand(CMD_SHARED, "find", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1724 Cmd_AddCommand(CMD_SHARED, "defer", Cmd_Defer_f, "execute a command in the future");
1727 // Support Doom3-style Toggle Command
1728 Cmd_AddCommand(CMD_SHARED | CMD_CLIENT_FROM_SERVER, "toggle", Cmd_Toggle_f, "toggles a console variable's values (use for more info)");
1736 void Cmd_Shutdown(void)
1738 cmd_iter_t *cmd_iter;
1739 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
1741 cmd_state_t *cmd = cmd_iter->cmd;
1743 if (cmd->cbuf->lock)
1745 // we usually have this locked when we get here from Host_Quit_f
1746 Cbuf_Unlock(cmd->cbuf);
1749 Mem_FreePool(&cmd->mempool);
1758 int Cmd_Argc (cmd_state_t *cmd)
1768 const char *Cmd_Argv(cmd_state_t *cmd, int arg)
1770 if (arg >= cmd->argc )
1771 return cmd->null_string;
1772 return cmd->argv[arg];
1780 const char *Cmd_Args (cmd_state_t *cmd)
1789 Parses the given string into command line tokens.
1792 // AK: This function should only be called from ExcuteString because the current design is a bit of an hack
1793 static void Cmd_TokenizeString (cmd_state_t *cmd, const char *text)
1802 // skip whitespace up to a /n
1803 while (*text && ISWHITESPACE(*text) && *text != '\r' && *text != '\n')
1810 if (*text == '\n' || *text == '\r')
1812 // a newline separates commands in the buffer
1813 if (*text == '\r' && text[1] == '\n')
1825 if (!COM_ParseToken_Console(&text))
1828 if (cmd->argc < MAX_ARGS)
1830 l = (int)strlen(com_token) + 1;
1831 if (cmd->cbuf->tokenizebufferpos + l > CMD_TOKENIZELENGTH)
1833 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguments\n", CMD_TOKENIZELENGTH);
1836 memcpy (cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos, com_token, l);
1837 cmd->argv[cmd->argc] = cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos;
1838 cmd->cbuf->tokenizebufferpos += l;
1850 void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const char *description)
1852 cmd_function_t *func;
1853 cmd_function_t *prev, *current;
1855 xcommand_t save = NULL;
1856 qboolean auto_add = false;
1859 for (i = 0; i < 3; i++)
1861 cmd = cmd_iter_all[i].cmd;
1862 if ((flags & cmd->cmd_flags) || (flags & cmd->auto_flags))
1864 if((flags & cmd->auto_flags) && cmd->auto_function)
1867 function = cmd->auto_function;
1871 // fail if the command is a variable name
1872 if (Cvar_FindVar(cmd->cvars, cmd_name, ~0))
1874 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
1880 // fail if the command already exists in this interpreter
1881 for (func = cmd->engine_functions; func; func = func->next)
1883 if (!strcmp(cmd_name, func->name))
1885 if(func->autofunc && !auto_add)
1887 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
1892 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1893 func->flags = flags;
1894 func->name = cmd_name;
1895 func->function = function;
1896 func->description = description;
1897 func->next = cmd->engine_functions;
1898 func->autofunc = auto_add;
1900 // insert it at the right alphanumeric position
1901 for (prev = NULL, current = cmd->engine_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1907 cmd->engine_functions = func;
1909 func->next = current;
1913 // mark csqcfunc if the function already exists in the csqc_functions list
1914 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1916 if (!strcmp(cmd_name, func->name))
1918 func->csqcfunc = true; //[515]: csqc
1924 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1925 func->name = cmd_name;
1926 func->function = function;
1927 func->description = description;
1928 func->csqcfunc = true; //[515]: csqc
1929 func->next = cmd->userdefined->csqc_functions;
1930 func->autofunc = false;
1932 // insert it at the right alphanumeric position
1933 for (prev = NULL, current = cmd->userdefined->csqc_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1939 cmd->userdefined->csqc_functions = func;
1941 func->next = current;
1952 static int Cmd_Compare(const char *s1, const char *s2, size_t len, qboolean casesensitive)
1955 return (casesensitive ? strncmp(s1, s2, len) : strncasecmp(s1, s2, len));
1957 return (casesensitive ? strcmp(s1, s2) : strcasecmp(s1, s2));
1960 cmd_function_t *Cmd_GetCommand(cmd_state_t *cmd, const char *partial, size_t len, qboolean casesensitive)
1962 cmd_function_t *func = NULL;
1965 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1966 if (!Cmd_Compare(partial, func->name, len, casesensitive))
1969 for (func=cmd->engine_functions ; func ; func=func->next)
1970 if (!Cmd_Compare(partial, func->name, len, casesensitive))
1981 qboolean Cmd_Exists (cmd_state_t *cmd, const char *cmd_name)
1983 if(Cmd_GetCommand(cmd, cmd_name, 0, true))
1993 const char *Cmd_CompleteCommand (cmd_state_t *cmd, const char *partial)
1995 cmd_function_t *func;
1997 func = Cmd_GetCommand(cmd, partial, strlen(partial), false);
2004 Cmd_CompleteCountPossible
2006 New function for tab-completion system
2007 Added by EvilTypeGuy
2008 Thanks to Fett erich@heintz.com
2012 int Cmd_CompleteCountPossible (cmd_state_t *cmd, const char *partial)
2014 cmd_function_t *func;
2019 len = strlen(partial);
2024 // Loop through the command list and count all partial matches
2025 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
2026 if (!strncasecmp(partial, func->name, len))
2029 for (func = cmd->engine_functions; func; func = func->next)
2030 if (!strncasecmp(partial, func->name, len))
2037 Cmd_CompleteBuildList
2039 New function for tab-completion system
2040 Added by EvilTypeGuy
2041 Thanks to Fett erich@heintz.com
2045 const char **Cmd_CompleteBuildList (cmd_state_t *cmd, const char *partial)
2047 cmd_function_t *func;
2050 size_t sizeofbuf = (Cmd_CompleteCountPossible (cmd, partial) + 1) * sizeof (const char *);
2053 len = strlen(partial);
2054 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
2055 // Loop through the functions lists and print all matches
2056 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
2057 if (!strncasecmp(partial, func->name, len))
2058 buf[bpos++] = func->name;
2059 for (func = cmd->engine_functions; func; func = func->next)
2060 if (!strncasecmp(partial, func->name, len))
2061 buf[bpos++] = func->name;
2067 // written by LadyHavoc
2068 void Cmd_CompleteCommandPrint (cmd_state_t *cmd, const char *partial)
2070 cmd_function_t *func;
2071 size_t len = strlen(partial);
2072 // Loop through the command list and print all matches
2073 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
2074 if (!strncasecmp(partial, func->name, len))
2075 Con_Printf("^2%s^7: %s\n", func->name, func->description);
2076 for (func = cmd->engine_functions; func; func = func->next)
2077 if (!strncasecmp(partial, func->name, len))
2078 Con_Printf("^2%s^7: %s\n", func->name, func->description);
2084 New function for tab-completion system
2085 Added by EvilTypeGuy
2086 Thanks to Fett erich@heintz.com
2090 const char *Cmd_CompleteAlias (cmd_state_t *cmd, const char *partial)
2095 len = strlen(partial);
2101 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2102 if (!strncasecmp(partial, alias->name, len))
2108 // written by LadyHavoc
2109 void Cmd_CompleteAliasPrint (cmd_state_t *cmd, const char *partial)
2112 size_t len = strlen(partial);
2113 // Loop through the alias list and print all matches
2114 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2115 if (!strncasecmp(partial, alias->name, len))
2116 Con_Printf("^5%s^7: %s", alias->name, alias->value);
2121 Cmd_CompleteAliasCountPossible
2123 New function for tab-completion system
2124 Added by EvilTypeGuy
2125 Thanks to Fett erich@heintz.com
2129 int Cmd_CompleteAliasCountPossible (cmd_state_t *cmd, const char *partial)
2137 len = strlen(partial);
2142 // Loop through the command list and count all partial matches
2143 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2144 if (!strncasecmp(partial, alias->name, len))
2151 Cmd_CompleteAliasBuildList
2153 New function for tab-completion system
2154 Added by EvilTypeGuy
2155 Thanks to Fett erich@heintz.com
2159 const char **Cmd_CompleteAliasBuildList (cmd_state_t *cmd, const char *partial)
2164 size_t sizeofbuf = (Cmd_CompleteAliasCountPossible (cmd, partial) + 1) * sizeof (const char *);
2167 len = strlen(partial);
2168 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
2169 // Loop through the alias list and print all matches
2170 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2171 if (!strncasecmp(partial, alias->name, len))
2172 buf[bpos++] = alias->name;
2178 // TODO: Make this more generic?
2179 void Cmd_ClearCSQCCommands (cmd_state_t *cmd)
2181 cmd_function_t *func;
2182 cmd_function_t **next = &cmd->userdefined->csqc_functions;
2192 extern cvar_t sv_cheats;
2198 A complete command line has been parsed, so try to execute it
2199 FIXME: lookupnoadd the token to speed search?
2202 void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qboolean lockmutex)
2205 cmd_function_t *func;
2208 Cbuf_Lock(cmd->cbuf);
2209 oldpos = cmd->cbuf->tokenizebufferpos;
2212 Cmd_TokenizeString (cmd, text);
2214 // execute the command line
2216 goto done; // no tokens
2219 func = Cmd_GetCommand(cmd, cmd->argv[0], 0, false);
2222 if (func->csqcfunc && CL_VM_ConsoleCommand(text)) //[515]: csqc
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);
2247 // if it's a client command and no command was found, say so.
2248 if (cmd->source == src_client)
2250 Con_Printf("Client \"%s\" tried to execute \"%s\"\n", host_client->name, text);
2255 for (a=cmd->userdefined->alias ; a ; a=a->next)
2257 if (!strcasecmp (cmd->argv[0], a->name))
2259 Cmd_ExecuteAlias(cmd, a);
2265 if (!Cvar_Command(cmd) && host.framecount > 0)
2266 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(cmd, 0));
2268 cmd->cbuf->tokenizebufferpos = oldpos;
2270 Cbuf_Unlock(cmd->cbuf);
2277 Returns the position (1 to argc-1) in the command's argument list
2278 where the given parameter apears, or 0 if not present
2282 int Cmd_CheckParm (cmd_state_t *cmd, const char *parm)
2288 Con_Printf ("Cmd_CheckParm: NULL");
2292 for (i = 1; i < Cmd_Argc (cmd); i++)
2293 if (!strcasecmp (parm, Cmd_Argv(cmd, i)))
2301 void Cmd_SaveInitState(void)
2303 cmd_iter_t *cmd_iter;
2304 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2306 cmd_state_t *cmd = cmd_iter->cmd;
2309 for (f = cmd->userdefined->csqc_functions; f; f = f->next)
2310 f->initstate = true;
2311 for (f = cmd->engine_functions; f; f = f->next)
2312 f->initstate = true;
2313 for (a = cmd->userdefined->alias; a; a = a->next)
2315 a->initstate = true;
2316 a->initialvalue = Mem_strdup(zonemempool, a->value);
2319 Cvar_SaveInitState(&cvars_all);
2322 void Cmd_RestoreInitState(void)
2324 cmd_iter_t *cmd_iter;
2325 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2327 cmd_state_t *cmd = cmd_iter->cmd;
2328 cmd_function_t *f, **fp;
2329 cmdalias_t *a, **ap;
2330 for (fp = &cmd->userdefined->csqc_functions; (f = *fp);)
2336 // destroy this command, it didn't exist at init
2337 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2342 for (fp = &cmd->engine_functions; (f = *fp);)
2348 // destroy this command, it didn't exist at init
2349 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2354 for (ap = &cmd->userdefined->alias; (a = *ap);)
2358 // restore this alias, it existed at init
2359 if (strcmp(a->value ? a->value : "", a->initialvalue ? a->initialvalue : ""))
2361 Con_DPrintf("Cmd_RestoreInitState: Restoring alias %s\n", a->name);
2364 a->value = Mem_strdup(zonemempool, a->initialvalue);
2370 // free this alias, it didn't exist at init...
2371 Con_DPrintf("Cmd_RestoreInitState: Destroying alias %s\n", a->name);
2379 Cvar_RestoreInitState(&cvars_all);