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 cbuf_cmd_t *Cbuf_LinkGet(cbuf_t *cbuf, cbuf_cmd_t *existing);
83 static void Cmd_Defer_f (cmd_state_t *cmd)
86 cbuf_t *cbuf = cmd->cbuf;
88 if(Cmd_Argc(cmd) == 1)
90 if(List_IsEmpty(&cbuf->deferred))
91 Con_Printf("No commands are pending.\n");
95 List_ForEach(pos, &cbuf->deferred)
97 current = List_Container(*pos, cbuf_cmd_t, list);
98 Con_Printf("-> In %9.2f: %s\n", current->delay, current->text);
102 else if(Cmd_Argc(cmd) == 2 && !strcasecmp("clear", Cmd_Argv(cmd, 1)))
104 while(!List_IsEmpty(&cbuf->deferred))
105 List_Move_Tail(cbuf->deferred.next, &cbuf->free);
107 else if(Cmd_Argc(cmd) == 3)
109 const char *text = Cmd_Argv(cmd, 2);
110 current = Cbuf_LinkGet(cbuf, NULL);
111 current->size = strlen(text);
112 current->source = cmd;
113 current->delay = atof(Cmd_Argv(cmd, 1));
115 memcpy(current->text, text, current->size + 1);
117 List_Move_Tail(¤t->list, &cbuf->deferred);
122 Con_Printf("usage: defer <seconds> <command>\n"
132 Print something to the center of the screen using SCR_Centerprint
135 static void Cmd_Centerprint_f (cmd_state_t *cmd)
137 char msg[MAX_INPUTLINE];
138 unsigned int i, c, p;
142 strlcpy(msg, Cmd_Argv(cmd,1), sizeof(msg));
143 for(i = 2; i < c; ++i)
145 strlcat(msg, " ", sizeof(msg));
146 strlcat(msg, Cmd_Argv(cmd, i), sizeof(msg));
148 c = (unsigned int)strlen(msg);
149 for(p = 0, i = 0; i < c; ++i)
155 else if(msg[i+1] == '\\')
167 SCR_CenterPrint(msg);
172 =============================================================================
176 =============================================================================
183 Parses Quake console command-line
184 Returns size of parsed command-line
187 static size_t Cbuf_ParseText(char **in)
190 qboolean quotes = false;
191 qboolean comment = false; // Does not imply end because we might be starting the line with a comment.
192 qboolean escaped = false;
193 qboolean end = false; // Reached the end of a valid command
194 char *offset = NULL; // Non-NULL if valid command. Used by the caller to know where to start copying.
195 size_t cmdsize = 0; // Non-zero if valid command. Basically bytes to copy for the caller.
198 * Allow escapes in quotes. Ignore newlines and
199 * comments. Return 0 if input consists solely
200 * of either of those, and ignore blank input.
207 if(!quotes && (*in)[i+1] == '/' && (i == 0 || ISWHITESPACE((*in)[i-1])))
212 // Use bit magic to indicate an incomplete (pending) command.
240 if (!escaped && quotes)
250 offset = (char *)&(*in)[i];
265 static cbuf_cmd_t *Cbuf_LinkGet(cbuf_t *cbuf, cbuf_cmd_t *existing)
267 cbuf_cmd_t *ret = NULL;
268 if(existing && existing->pending)
272 if(!List_IsEmpty(&cbuf->free))
273 ret = List_Container(*cbuf->free.next, cbuf_cmd_t, list);
276 ret = (cbuf_cmd_t *)Z_Malloc(sizeof(cbuf_cmd_t));
277 ret->list.next = ret->list.prev = &ret->list;
280 ret->pending = false;
287 // Cloudwalk: Not happy with this, but it works.
288 static void Cbuf_LinkCreate(cmd_state_t *cmd, llist_t *head, cbuf_cmd_t *existing, const char *text)
290 char *in = (char *)&text[0];
291 cbuf_t *cbuf = cmd->cbuf;
292 size_t totalsize = 0, newsize = 0;
293 cbuf_cmd_t *current = NULL;
295 // Slide the pointer down until we reach the end
299 * FIXME: Upon reaching a terminator, we make a redundant
300 * call just to say "it's the end of the input stream".
302 newsize = Cbuf_ParseText(&in);
308 current = Cbuf_LinkGet(cbuf, existing);
310 if(!current->pending)
312 current->source = cmd;
313 List_Move_Tail(¤t->list, head);
316 if(newsize & (1<<17))
317 current->pending = true;
318 totalsize += (newsize &= ~(1<<17));
319 strlcpy(¤t->text[current->size], in, newsize + 1);
320 current->size += newsize;
322 else if (existing && !totalsize)
323 existing->pending = false;
328 cbuf->size += totalsize;
335 Adds command text at the end of the buffer
338 void Cbuf_AddText (cmd_state_t *cmd, const char *text)
340 size_t l = strlen(text);
341 cbuf_t *cbuf = cmd->cbuf;
342 llist_t llist = {&llist, &llist};
346 if (cbuf->maxsize - cbuf->size <= l)
347 Con_Print("Cbuf_AddText: overflow\n");
350 Cbuf_LinkCreate(cmd, &llist, (List_IsEmpty(&cbuf->start) ? NULL : List_Container(*cbuf->start.prev, cbuf_cmd_t, list)), text);
351 if(!List_IsEmpty(&llist))
352 List_Splice_Tail(&llist, &cbuf->start);
361 Adds command text immediately after the current command
362 FIXME: actually change the command buffer to do less copying
365 void Cbuf_InsertText (cmd_state_t *cmd, const char *text)
367 cbuf_t *cbuf = cmd->cbuf;
368 llist_t llist = {&llist, &llist};
369 size_t l = strlen(text);
373 // we need to memmove the existing text and stuff this in before it...
374 if (cbuf->size + l >= (size_t)cbuf->maxsize)
375 Con_Print("Cbuf_InsertText: overflow\n");
378 Cbuf_LinkCreate(cmd, &llist, List_Container(*cbuf->start.next, cbuf_cmd_t, list), text);
379 List_Splice(&llist, &cbuf->start);
387 Cbuf_Execute_Deferred --blub
390 static void Cbuf_Execute_Deferred (cbuf_t *cbuf)
396 if (host.realtime - cbuf->deferred_oldtime < 0 || host.realtime - cbuf->deferred_oldtime > 1800)
397 cbuf->deferred_oldtime = host.realtime;
398 eat = host.realtime - cbuf->deferred_oldtime;
399 if (eat < (1.0 / 120.0))
401 cbuf->deferred_oldtime = host.realtime;
403 List_ForEach(pos, &cbuf->deferred)
405 current = List_Container(*pos, cbuf_cmd_t, list);
406 current->delay -= eat;
407 if(current->delay <= 0)
409 cbuf->size += current->size;
410 List_Move(pos, &cbuf->start);
411 // We must return and come back next frame or the engine will freeze. Fragile... like glass :3
422 static qboolean Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias );
423 void Cbuf_Execute (cbuf_t *cbuf)
426 char preprocessed[MAX_INPUTLINE];
429 // LadyHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
430 cbuf->tokenizebufferpos = 0;
432 while (!List_IsEmpty(&cbuf->start))
435 * Delete the text from the command buffer and move remaining
436 * commands down. This is necessary because commands (exec, alias)
437 * can insert data at the beginning of the text buffer
439 current = List_Container(*cbuf->start.next, cbuf_cmd_t, list);
442 * Assume we're rolling with the current command-line and
443 * always set this false because alias expansion or cbuf insertion
444 * without a newline may set this true, and cause weirdness.
446 current->pending = false;
448 cbuf->size -= current->size;
450 firstchar = current->text;
451 while(*firstchar && ISWHITESPACE(*firstchar))
453 if((strncmp(firstchar, "alias", 5) || !ISWHITESPACE(firstchar[5])) &&
454 (strncmp(firstchar, "bind", 4) || !ISWHITESPACE(firstchar[4])) &&
455 (strncmp(firstchar, "in_bind", 7) || !ISWHITESPACE(firstchar[7])))
457 if(Cmd_PreprocessString(current->source, current->text, preprocessed, sizeof(preprocessed), NULL ))
458 Cmd_ExecuteString(current->source, preprocessed, src_local, false);
462 Cmd_ExecuteString (current->source, current->text, src_local, false);
465 // Recycle memory so using WASD doesn't cause a malloc and free
466 List_Move_Tail(¤t->list, &cbuf->free);
473 * Skip out while text still remains in
474 * buffer, leaving it for next frame
482 void Cbuf_Frame(cbuf_t *cbuf)
484 Cbuf_Execute_Deferred(cbuf);
487 SV_LockThreadMutex();
489 SV_UnlockThreadMutex();
494 ==============================================================================
498 ==============================================================================
505 Adds command line parameters as script statements
506 Commands lead with a +, and continue until a - or another +
507 quake +prog jctest.qp +cmd amlev1
508 quake -nosound +cmd amlev1
511 static void Cmd_StuffCmds_f (cmd_state_t *cmd)
514 // this is for all commandline options combined (and is bounds checked)
515 char build[MAX_INPUTLINE];
517 // come back later so we don't crash
518 if(host.state == host_init)
521 if (Cmd_Argc (cmd) != 1)
523 Con_Print("stuffcmds : execute command line parameters\n");
527 // no reason to run the commandline arguments twice
528 if (host_stuffcmdsrun)
531 host_stuffcmdsrun = true;
534 for (i = 0;i < sys.argc;i++)
536 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)
539 while (sys.argv[i][j])
540 build[l++] = sys.argv[i][j++];
542 for (;i < sys.argc;i++)
546 if ((sys.argv[i][0] == '+' || sys.argv[i][0] == '-') && (sys.argv[i][1] < '0' || sys.argv[i][1] > '9'))
548 if (l + strlen(sys.argv[i]) + 4 > sizeof(build) - 1)
551 if (strchr(sys.argv[i], ' '))
553 for (j = 0;sys.argv[i][j];j++)
554 build[l++] = sys.argv[i][j];
555 if (strchr(sys.argv[i], ' '))
562 // now terminate the combined string and prepend it to the command buffer
563 // we already reserved space for the terminator
565 Cbuf_InsertText (cmd, build);
568 static void Cmd_Exec(cmd_state_t *cmd, const char *filename)
571 size_t filenameLen = strlen(filename);
572 qboolean isdefaultcfg =
573 !strcmp(filename, "default.cfg") ||
574 (filenameLen >= 12 && !strcmp(filename + filenameLen - 12, "/default.cfg"));
576 if (!strcmp(filename, "config.cfg"))
578 filename = CONFIGFILENAME;
579 if (Sys_CheckParm("-noconfig"))
580 return; // don't execute config.cfg
583 f = (char *)FS_LoadFile (filename, tempmempool, false, NULL);
586 Con_Printf("couldn't exec %s\n",filename);
589 Con_Printf("execing %s\n",filename);
591 // if executing default.cfg for the first time, lock the cvar defaults
592 // it may seem backwards to insert this text BEFORE the default.cfg
593 // but Cbuf_InsertText inserts before, so this actually ends up after it.
595 Cbuf_InsertText(cmd, "\ncvar_lockdefaults\n");
597 Cbuf_InsertText (cmd, f);
602 // special defaults for specific games go here, these execute before default.cfg
603 // Nehahra pushable crates malfunction in some levels if this is on
604 // Nehahra NPC AI is confused by blowupfallenzombies
608 Cbuf_InsertText(cmd, "\n"
609 "sv_gameplayfix_blowupfallenzombies 0\n"
610 "sv_gameplayfix_findradiusdistancetobox 0\n"
611 "sv_gameplayfix_grenadebouncedownslopes 0\n"
612 "sv_gameplayfix_slidemoveprojectiles 0\n"
613 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
614 "sv_gameplayfix_setmodelrealbox 0\n"
615 "sv_gameplayfix_droptofloorstartsolid 0\n"
616 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
617 "sv_gameplayfix_noairborncorpse 0\n"
618 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
619 "sv_gameplayfix_easierwaterjump 0\n"
620 "sv_gameplayfix_delayprojectiles 0\n"
621 "sv_gameplayfix_multiplethinksperframe 0\n"
622 "sv_gameplayfix_fixedcheckwatertransition 0\n"
623 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
624 "sv_gameplayfix_swiminbmodels 0\n"
625 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
626 "sys_ticrate 0.01388889\n"
628 "r_shadow_bumpscale_basetexture 0\n"
629 "csqc_polygons_defaultmaterial_nocullface 0\n"
633 Cbuf_InsertText(cmd, "\n"
634 "sv_gameplayfix_blowupfallenzombies 0\n"
635 "sv_gameplayfix_findradiusdistancetobox 0\n"
636 "sv_gameplayfix_grenadebouncedownslopes 0\n"
637 "sv_gameplayfix_slidemoveprojectiles 0\n"
638 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
639 "sv_gameplayfix_setmodelrealbox 0\n"
640 "sv_gameplayfix_droptofloorstartsolid 0\n"
641 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
642 "sv_gameplayfix_noairborncorpse 0\n"
643 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
644 "sv_gameplayfix_easierwaterjump 0\n"
645 "sv_gameplayfix_delayprojectiles 0\n"
646 "sv_gameplayfix_multiplethinksperframe 0\n"
647 "sv_gameplayfix_fixedcheckwatertransition 0\n"
648 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
649 "sv_gameplayfix_swiminbmodels 0\n"
650 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
651 "sys_ticrate 0.01388889\n"
653 "r_shadow_bumpscale_basetexture 0\n"
654 "csqc_polygons_defaultmaterial_nocullface 0\n"
657 // 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.
658 // 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
659 // hipnotic mission pack has issues in their proximity mine sticking code, which causes them to bounce off.
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"
682 "r_shadow_bumpscale_basetexture 0\n"
683 "csqc_polygons_defaultmaterial_nocullface 0\n"
686 // rogue mission pack has a guardian boss that does not wake up if findradius returns one of the entities around its spawn area
688 Cbuf_InsertText(cmd, "\n"
689 "sv_gameplayfix_blowupfallenzombies 0\n"
690 "sv_gameplayfix_findradiusdistancetobox 0\n"
691 "sv_gameplayfix_grenadebouncedownslopes 0\n"
692 "sv_gameplayfix_slidemoveprojectiles 0\n"
693 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
694 "sv_gameplayfix_setmodelrealbox 0\n"
695 "sv_gameplayfix_droptofloorstartsolid 0\n"
696 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
697 "sv_gameplayfix_noairborncorpse 0\n"
698 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
699 "sv_gameplayfix_easierwaterjump 0\n"
700 "sv_gameplayfix_delayprojectiles 0\n"
701 "sv_gameplayfix_multiplethinksperframe 0\n"
702 "sv_gameplayfix_fixedcheckwatertransition 0\n"
703 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
704 "sv_gameplayfix_swiminbmodels 0\n"
705 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
706 "sys_ticrate 0.01388889\n"
708 "r_shadow_bumpscale_basetexture 0\n"
709 "csqc_polygons_defaultmaterial_nocullface 0\n"
713 Cbuf_InsertText(cmd, "\n"
714 "sv_gameplayfix_blowupfallenzombies 0\n"
715 "sv_gameplayfix_findradiusdistancetobox 0\n"
716 "sv_gameplayfix_grenadebouncedownslopes 0\n"
717 "sv_gameplayfix_slidemoveprojectiles 0\n"
718 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
719 "sv_gameplayfix_setmodelrealbox 0\n"
720 "sv_gameplayfix_droptofloorstartsolid 0\n"
721 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
722 "sv_gameplayfix_noairborncorpse 0\n"
723 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
724 "sv_gameplayfix_easierwaterjump 0\n"
725 "sv_gameplayfix_delayprojectiles 0\n"
726 "sv_gameplayfix_multiplethinksperframe 0\n"
727 "sv_gameplayfix_fixedcheckwatertransition 0\n"
728 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
729 "sv_gameplayfix_swiminbmodels 0\n"
730 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
731 "sys_ticrate 0.01388889\n"
733 "r_shadow_bumpscale_basetexture 4\n"
734 "csqc_polygons_defaultmaterial_nocullface 0\n"
738 Cbuf_InsertText(cmd, "\n"
739 "sv_gameplayfix_blowupfallenzombies 1\n"
740 "sv_gameplayfix_findradiusdistancetobox 1\n"
741 "sv_gameplayfix_grenadebouncedownslopes 1\n"
742 "sv_gameplayfix_slidemoveprojectiles 1\n"
743 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
744 "sv_gameplayfix_setmodelrealbox 1\n"
745 "sv_gameplayfix_droptofloorstartsolid 1\n"
746 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
747 "sv_gameplayfix_noairborncorpse 1\n"
748 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
749 "sv_gameplayfix_easierwaterjump 1\n"
750 "sv_gameplayfix_delayprojectiles 1\n"
751 "sv_gameplayfix_multiplethinksperframe 1\n"
752 "sv_gameplayfix_fixedcheckwatertransition 1\n"
753 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
754 "sv_gameplayfix_swiminbmodels 1\n"
755 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
756 "sys_ticrate 0.01388889\n"
757 "sv_gameplayfix_q2airaccelerate 1\n"
758 "sv_gameplayfix_stepmultipletimes 1\n"
759 "csqc_polygons_defaultmaterial_nocullface 1\n"
760 "con_chatsound_team_mask 13\n"
764 case GAME_VORETOURNAMENT:
765 // 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
766 Cbuf_InsertText(cmd, "\n"
767 "csqc_polygons_defaultmaterial_nocullface 1\n"
768 "con_chatsound_team_mask 13\n"
769 "sv_gameplayfix_customstats 1\n"
772 // Steel Storm: Burning Retribution csqc misinterprets CSQC_InputEvent if type is a value other than 0 or 1
773 case GAME_STEELSTORM:
774 Cbuf_InsertText(cmd, "\n"
775 "sv_gameplayfix_blowupfallenzombies 1\n"
776 "sv_gameplayfix_findradiusdistancetobox 1\n"
777 "sv_gameplayfix_grenadebouncedownslopes 1\n"
778 "sv_gameplayfix_slidemoveprojectiles 1\n"
779 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
780 "sv_gameplayfix_setmodelrealbox 1\n"
781 "sv_gameplayfix_droptofloorstartsolid 1\n"
782 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
783 "sv_gameplayfix_noairborncorpse 1\n"
784 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
785 "sv_gameplayfix_easierwaterjump 1\n"
786 "sv_gameplayfix_delayprojectiles 1\n"
787 "sv_gameplayfix_multiplethinksperframe 1\n"
788 "sv_gameplayfix_fixedcheckwatertransition 1\n"
789 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
790 "sv_gameplayfix_swiminbmodels 1\n"
791 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
792 "sys_ticrate 0.01388889\n"
793 "cl_csqc_generatemousemoveevents 0\n"
794 "csqc_polygons_defaultmaterial_nocullface 1\n"
798 Cbuf_InsertText(cmd, "\n"
799 "sv_gameplayfix_blowupfallenzombies 1\n"
800 "sv_gameplayfix_findradiusdistancetobox 1\n"
801 "sv_gameplayfix_grenadebouncedownslopes 1\n"
802 "sv_gameplayfix_slidemoveprojectiles 1\n"
803 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
804 "sv_gameplayfix_setmodelrealbox 1\n"
805 "sv_gameplayfix_droptofloorstartsolid 1\n"
806 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
807 "sv_gameplayfix_noairborncorpse 1\n"
808 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
809 "sv_gameplayfix_easierwaterjump 1\n"
810 "sv_gameplayfix_delayprojectiles 1\n"
811 "sv_gameplayfix_multiplethinksperframe 1\n"
812 "sv_gameplayfix_fixedcheckwatertransition 1\n"
813 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
814 "sv_gameplayfix_swiminbmodels 1\n"
815 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
816 "sys_ticrate 0.01388889\n"
817 "csqc_polygons_defaultmaterial_nocullface 0\n"
829 static void Cmd_Exec_f (cmd_state_t *cmd)
834 if (Cmd_Argc(cmd) != 2)
836 Con_Print("exec <filename> : execute a script file\n");
840 s = FS_Search(Cmd_Argv(cmd, 1), true, true, NULL);
841 if(!s || !s->numfilenames)
843 Con_Printf("couldn't exec %s\n",Cmd_Argv(cmd, 1));
847 for(i = 0; i < s->numfilenames; ++i)
848 Cmd_Exec(cmd, s->filenames[i]);
858 Just prints the rest of the line to the console
861 static void Cmd_Echo_f (cmd_state_t *cmd)
865 for (i=1 ; i<Cmd_Argc(cmd) ; i++)
866 Con_Printf("%s ",Cmd_Argv(cmd, i));
871 // Support Doom3-style Toggle Console Command
876 Toggles a specified console variable amongst the values specified (default is 0 and 1)
879 static void Cmd_Toggle_f(cmd_state_t *cmd)
881 // Acquire Number of Arguments
882 int nNumArgs = Cmd_Argc(cmd);
885 // No Arguments Specified; Print Usage
886 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");
888 { // Correct Arguments Specified
889 // Acquire Potential CVar
890 cvar_t* cvCVar = Cvar_FindVar(cmd->cvars, Cmd_Argv(cmd, 1), cmd->cvars_flagsmask);
897 Cvar_SetValueQuick(cvCVar, 0);
899 Cvar_SetValueQuick(cvCVar, 1);
903 { // 0 and Specified Usage
904 if(cvCVar->integer == atoi(Cmd_Argv(cmd, 2) ) )
905 // CVar is Specified Value; // Reset to 0
906 Cvar_SetValueQuick(cvCVar, 0);
908 if(cvCVar->integer == 0)
909 // CVar is 0; Specify Value
910 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
912 // CVar does not match; Reset to 0
913 Cvar_SetValueQuick(cvCVar, 0);
916 { // Variable Values Specified
920 for(nCnt = 2; nCnt < nNumArgs; nCnt++)
921 { // Cycle through Values
922 if( strcmp(cvCVar->string, Cmd_Argv(cmd, nCnt) ) == 0)
923 { // Current Value Located; Increment to Next
924 if( (nCnt + 1) == nNumArgs)
925 // Max Value Reached; Reset
926 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
929 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, nCnt + 1) );
938 // Value not Found; Reset to Original
939 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
945 Con_Printf("ERROR : CVar '%s' not found\n", Cmd_Argv(cmd, 1) );
954 Creates a new command that executes a command string (possibly ; seperated)
957 static void Cmd_Alias_f (cmd_state_t *cmd)
960 char line[MAX_INPUTLINE];
965 if (Cmd_Argc(cmd) == 1)
967 Con_Print("Current alias commands:\n");
968 for (a = cmd->userdefined->alias ; a ; a=a->next)
969 Con_Printf("%s : %s", a->name, a->value);
973 s = Cmd_Argv(cmd, 1);
974 if (strlen(s) >= MAX_ALIAS_NAME)
976 Con_Print("Alias name is too long\n");
980 // if the alias already exists, reuse it
981 for (a = cmd->userdefined->alias ; a ; a=a->next)
983 if (!strcmp(s, a->name))
992 cmdalias_t *prev, *current;
994 a = (cmdalias_t *)Z_Malloc (sizeof(cmdalias_t));
995 strlcpy (a->name, s, sizeof (a->name));
996 // insert it at the right alphanumeric position
997 for( prev = NULL, current = cmd->userdefined->alias ; current && strcmp( current->name, a->name ) < 0 ; prev = current, current = current->next )
1002 cmd->userdefined->alias = a;
1008 // copy the rest of the command line
1009 line[0] = 0; // start out with a null string
1011 for (i=2 ; i < c ; i++)
1014 strlcat (line, " ", sizeof (line));
1015 strlcat (line, Cmd_Argv(cmd, i), sizeof (line));
1017 strlcat (line, "\n", sizeof (line));
1019 alloclen = strlen (line) + 1;
1021 line[alloclen - 2] = '\n'; // to make sure a newline is appended even if too long
1022 a->value = (char *)Z_Malloc (alloclen);
1023 memcpy (a->value, line, alloclen);
1030 Remove existing aliases.
1033 static void Cmd_UnAlias_f (cmd_state_t *cmd)
1039 if(Cmd_Argc(cmd) == 1)
1041 Con_Print("unalias: Usage: unalias alias1 [alias2 ...]\n");
1045 for(i = 1; i < Cmd_Argc(cmd); ++i)
1047 s = Cmd_Argv(cmd, i);
1049 for(a = cmd->userdefined->alias; a; p = a, a = a->next)
1051 if(!strcmp(s, a->name))
1053 if (a->initstate) // we can not remove init aliases
1055 if(a == cmd->userdefined->alias)
1056 cmd->userdefined->alias = a->next;
1065 Con_Printf("unalias: %s alias not found\n", s);
1070 =============================================================================
1074 =============================================================================
1077 static const char *Cmd_GetDirectCvarValue(cmd_state_t *cmd, const char *varname, cmdalias_t *alias, qboolean *is_multiple)
1082 static char vabuf[1024]; // cmd_mutex
1085 *is_multiple = false;
1087 if(!varname || !*varname)
1092 if(!strcmp(varname, "*"))
1095 *is_multiple = true;
1096 return Cmd_Args(cmd);
1098 else if(!strcmp(varname, "#"))
1100 return va(vabuf, sizeof(vabuf), "%d", Cmd_Argc(cmd));
1102 else if(varname[strlen(varname) - 1] == '-')
1104 argno = strtol(varname, &endptr, 10);
1105 if(endptr == varname + strlen(varname) - 1)
1107 // whole string is a number, apart from the -
1108 const char *p = Cmd_Args(cmd);
1109 for(; argno > 1; --argno)
1110 if(!COM_ParseToken_Console(&p))
1115 *is_multiple = true;
1117 // kill pre-argument whitespace
1118 for (;*p && ISWHITESPACE(*p);p++)
1127 argno = strtol(varname, &endptr, 10);
1130 // whole string is a number
1131 // NOTE: we already made sure we don't have an empty cvar name!
1132 if(argno >= 0 && argno < Cmd_Argc(cmd))
1133 return Cmd_Argv(cmd, argno);
1138 if((cvar = Cvar_FindVar(cmd->cvars, varname, cmd->cvars_flagsmask)) && !(cvar->flags & CVAR_PRIVATE))
1139 return cvar->string;
1144 qboolean Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset, qboolean putquotes)
1146 qboolean quote_quot = !!strchr(quoteset, '"');
1147 qboolean quote_backslash = !!strchr(quoteset, '\\');
1148 qboolean quote_dollar = !!strchr(quoteset, '$');
1157 *out++ = '"'; --outlen;
1163 if(*in == '"' && quote_quot)
1167 *out++ = '\\'; --outlen;
1168 *out++ = '"'; --outlen;
1170 else if(*in == '\\' && quote_backslash)
1174 *out++ = '\\'; --outlen;
1175 *out++ = '\\'; --outlen;
1177 else if(*in == '$' && quote_dollar)
1181 *out++ = '$'; --outlen;
1182 *out++ = '$'; --outlen;
1188 *out++ = *in; --outlen;
1203 static const char *Cmd_GetCvarValue(cmd_state_t *cmd, const char *var, size_t varlen, cmdalias_t *alias)
1205 static char varname[MAX_INPUTLINE]; // cmd_mutex
1206 static char varval[MAX_INPUTLINE]; // cmd_mutex
1207 const char *varstr = NULL;
1209 qboolean required = false;
1210 qboolean optional = false;
1211 static char asis[] = "asis"; // just to suppress const char warnings
1213 if(varlen >= MAX_INPUTLINE)
1214 varlen = MAX_INPUTLINE - 1;
1215 memcpy(varname, var, varlen);
1216 varname[varlen] = 0;
1217 varfunc = strchr(varname, ' ');
1229 Con_Printf(CON_WARN "Warning: Could not expand $ in alias %s\n", alias->name);
1231 Con_Printf(CON_WARN "Warning: Could not expand $\n");
1239 while((p = strchr(varfunc, '?')))
1242 memmove(p, p+1, strlen(p)); // with final NUL
1245 while((p = strchr(varfunc, '!')))
1248 memmove(p, p+1, strlen(p)); // with final NUL
1251 while((p = strchr(varfunc, ' ')))
1253 memmove(p, p+1, strlen(p)); // with final NUL
1255 // if no function is left, NULL it
1260 if(varname[0] == '$')
1261 varstr = Cmd_GetDirectCvarValue(cmd, Cmd_GetDirectCvarValue(cmd, varname + 1, alias, NULL), alias, NULL);
1264 qboolean is_multiple = false;
1265 // Exception: $* and $n- don't use the quoted form by default
1266 varstr = Cmd_GetDirectCvarValue(cmd, varname, alias, &is_multiple);
1277 Con_Printf(CON_ERROR "Error: Could not expand $%s in alias %s\n", varname, alias->name);
1279 Con_Printf(CON_ERROR "Error: Could not expand $%s\n", varname);
1289 Con_Printf(CON_WARN "Warning: Could not expand $%s in alias %s\n", varname, alias->name);
1291 Con_Printf(CON_WARN "Warning: Could not expand $%s\n", varname);
1292 dpsnprintf(varval, sizeof(varval), "$%s", varname);
1297 if(!varfunc || !strcmp(varfunc, "q")) // note: quoted form is default, use "asis" to override!
1299 // quote it so it can be used inside double quotes
1300 // we just need to replace " by \", and of course, double backslashes
1301 Cmd_QuoteString(varval, sizeof(varval), varstr, "\"\\", false);
1304 else if(!strcmp(varfunc, "asis"))
1309 Con_Printf("Unknown variable function %s\n", varfunc);
1315 Cmd_PreprocessString
1317 Preprocesses strings and replaces $*, $param#, $cvar accordingly. Also strips comments.
1319 static qboolean Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias ) {
1325 // don't crash if there's no room in the outtext buffer
1326 if( maxoutlen == 0 ) {
1329 maxoutlen--; // because of \0
1334 while( *in && outlen < maxoutlen ) {
1336 // this is some kind of expansion, see what comes after the $
1339 // The console does the following preprocessing:
1341 // - $$ is transformed to a single dollar sign.
1342 // - $var or ${var} are expanded to the contents of the named cvar,
1343 // with quotation marks and backslashes quoted so it can safely
1344 // be used inside quotation marks (and it should always be used
1346 // - ${var asis} inserts the cvar value as is, without doing this
1348 // - ${var ?} silently expands to the empty string if
1349 // $var does not exist
1350 // - ${var !} fails expansion and executes nothing if
1351 // $var does not exist
1352 // - prefix the cvar name with a dollar sign to do indirection;
1353 // for example, if $x has the value timelimit, ${$x} will return
1354 // the value of $timelimit
1355 // - when expanding an alias, the special variable name $* refers
1356 // to all alias parameters, and a number refers to that numbered
1357 // alias parameter, where the name of the alias is $0, the first
1358 // parameter is $1 and so on; as a special case, $* inserts all
1359 // parameters, without extra quoting, so one can use $* to just
1360 // pass all parameters around. All parameters starting from $n
1361 // can be referred to as $n- (so $* is equivalent to $1-).
1362 // - ${* q} and ${n- q} force quoting anyway
1364 // Note: when expanding an alias, cvar expansion is done in the SAME step
1365 // as alias expansion so that alias parameters or cvar values containing
1366 // dollar signs have no unwanted bad side effects. However, this needs to
1367 // be accounted for when writing complex aliases. For example,
1368 // alias foo "set x NEW; echo $x"
1369 // actually expands to
1370 // "set x NEW; echo OLD"
1371 // and will print OLD! To work around this, use a second alias:
1372 // alias foo "set x NEW; foo2"
1373 // alias foo2 "echo $x"
1375 // Also note: lines starting with alias are exempt from cvar expansion.
1376 // If you want cvar expansion, write "alias" instead:
1379 // alias foo "echo $x"
1380 // "alias" bar "echo $x"
1383 // foo will print 2, because the variable $x will be expanded when the alias
1384 // gets expanded. bar will print 1, because the variable $x was expanded
1385 // at definition time. foo can be equivalently defined as
1387 // "alias" foo "echo $$x"
1389 // because at definition time, $$ will get replaced to a single $.
1394 } else if(*in == '{') {
1395 varlen = strcspn(in + 1, "}");
1396 if(in[varlen + 1] == '}')
1398 val = Cmd_GetCvarValue(cmd, in + 1, varlen, alias);
1410 varlen = strspn(in, "#*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-");
1411 val = Cmd_GetCvarValue(cmd, in, varlen, alias);
1418 // insert the cvar value
1419 while(*val && outlen < maxoutlen)
1420 outtext[outlen++] = *val++;
1425 // copy the unexpanded text
1426 outtext[outlen++] = '$';
1427 while(eat && outlen < maxoutlen)
1429 outtext[outlen++] = *in++;
1435 outtext[outlen++] = *in++;
1437 outtext[outlen] = 0;
1445 Called for aliases and fills in the alias into the cbuffer
1448 static void Cmd_ExecuteAlias (cmd_state_t *cmd, cmdalias_t *alias)
1450 static char buffer[ MAX_INPUTLINE ]; // cmd_mutex
1451 static char buffer2[ MAX_INPUTLINE ]; // cmd_mutex
1452 qboolean ret = Cmd_PreprocessString( cmd, alias->value, buffer, sizeof(buffer) - 2, alias );
1455 // insert at start of command buffer, so that aliases execute in order
1456 // (fixes bug introduced by Black on 20050705)
1458 // Note: Cbuf_PreprocessString will be called on this string AGAIN! So we
1459 // have to make sure that no second variable expansion takes place, otherwise
1460 // alias parameters containing dollar signs can have bad effects.
1461 Cmd_QuoteString(buffer2, sizeof(buffer2), buffer, "$", false);
1462 Cbuf_InsertText(cmd, buffer2);
1469 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
1470 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
1474 static void Cmd_List_f (cmd_state_t *cmd)
1476 cmd_function_t *func;
1477 const char *partial;
1482 if (Cmd_Argc(cmd) > 1)
1484 partial = Cmd_Argv(cmd, 1);
1485 len = strlen(partial);
1486 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1496 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1498 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1500 Con_Printf("%s : %s\n", func->name, func->description);
1503 for (func = cmd->engine_functions; func; func = func->next)
1505 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1507 Con_Printf("%s : %s\n", func->name, func->description);
1514 Con_Printf("%i Command%s matching \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1516 Con_Printf("%i Command%s beginning with \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1519 Con_Printf("%i Command%s\n\n", count, (count > 1) ? "s" : "");
1522 static void Cmd_Apropos_f(cmd_state_t *cmd)
1524 cmd_function_t *func;
1527 const char *partial;
1532 if (Cmd_Argc(cmd) > 1)
1533 partial = Cmd_Args(cmd);
1536 Con_Printf("usage: %s <string>\n",Cmd_Argv(cmd, 0));
1540 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
1542 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
1545 for (cvar = cmd->cvars->vars; cvar; cvar = cvar->next)
1547 if (matchpattern_with_separator(cvar->name, partial, true, "", false) ||
1548 matchpattern_with_separator(cvar->description, partial, true, "", false))
1550 Con_Printf ("cvar ");
1551 Cvar_PrintHelp(cvar, cvar->name, true);
1554 for (int i = 0; i < cvar->aliasindex; i++)
1556 if (matchpattern_with_separator(cvar->aliases[i], partial, true, "", false))
1558 Con_Printf ("cvar ");
1559 Cvar_PrintHelp(cvar, cvar->aliases[i], true);
1564 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1566 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1567 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1569 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1572 for (func = cmd->engine_functions; func; func = func->next)
1574 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1575 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1577 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1580 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
1582 // procede here a bit differently as an alias value always got a final \n
1583 if (!matchpattern_with_separator(alias->name, partial, true, "", false))
1584 if (!matchpattern_with_separator(alias->value, partial, true, "\n", false)) // when \n is as separator wildcards don't match it
1586 Con_Printf("alias ^5%s^7: %s", alias->name, alias->value); // do not print an extra \n
1589 Con_Printf("%i result%s\n\n", count, (count > 1) ? "s" : "");
1599 cmd_iter_t *cmd_iter;
1600 cbuf_t *cbuf = (cbuf_t *)Z_Malloc(sizeof(cbuf_t));
1601 cbuf->maxsize = 655360;
1602 cbuf->lock = Thread_CreateMutex();
1606 cbuf->start.prev = cbuf->start.next = &(cbuf->start);
1607 cbuf->deferred.prev = cbuf->deferred.next = &(cbuf->deferred);
1608 cbuf->free.prev = cbuf->free.next = &(cbuf->free);
1610 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
1612 cmd_state_t *cmd = cmd_iter->cmd;
1613 cmd->mempool = Mem_AllocPool("commands", 0, NULL);
1614 // space for commands and script files
1616 cmd->null_string = "";
1618 // client console can see server cvars because the user may start a server
1619 cmd_client.cvars = &cvars_all;
1620 cmd_client.cvars_flagsmask = CVAR_CLIENT | CVAR_SERVER;
1621 cmd_client.cmd_flags = CMD_CLIENT | CMD_CLIENT_FROM_SERVER;
1622 cmd_client.auto_flags = CMD_SERVER_FROM_CLIENT;
1623 cmd_client.auto_function = CL_ForwardToServer_f; // FIXME: Move this to the client.
1624 cmd_client.userdefined = &cmd_userdefined_all;
1625 // dedicated server console can only see server cvars, there is no client
1626 cmd_server.cvars = &cvars_all;
1627 cmd_server.cvars_flagsmask = CVAR_SERVER;
1628 cmd_server.cmd_flags = CMD_SERVER;
1629 cmd_server.auto_flags = 0;
1630 cmd_server.auto_function = NULL;
1631 cmd_server.userdefined = &cmd_userdefined_all;
1632 // server commands received from clients have no reason to access cvars, cvar expansion seems perilous.
1633 cmd_serverfromclient.cvars = &cvars_null;
1634 cmd_serverfromclient.cvars_flagsmask = 0;
1635 cmd_serverfromclient.cmd_flags = CMD_SERVER_FROM_CLIENT | CMD_USERINFO;
1636 cmd_serverfromclient.auto_flags = 0;
1637 cmd_serverfromclient.auto_function = NULL;
1638 cmd_serverfromclient.userdefined = &cmd_userdefined_null;
1641 // register our commands
1643 // client-only commands
1644 Cmd_AddCommand(CMD_SHARED, "wait", Cmd_Wait_f, "make script execution wait for next rendered frame");
1645 Cmd_AddCommand(CMD_CLIENT, "cprint", Cmd_Centerprint_f, "print something at the screen center");
1647 // maintenance commands used for upkeep of cvars and saved configs
1648 Cmd_AddCommand(CMD_SHARED, "stuffcmds", Cmd_StuffCmds_f, "execute commandline parameters (must be present in quake.rc script)");
1649 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");
1650 Cmd_AddCommand(CMD_SHARED, "cvar_resettodefaults_all", Cvar_ResetToDefaults_All_f, "sets all cvars to their locked default values");
1651 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)");
1652 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)");
1654 // general console commands used in multiple environments
1655 Cmd_AddCommand(CMD_SHARED, "exec", Cmd_Exec_f, "execute a script file");
1656 Cmd_AddCommand(CMD_SHARED, "echo",Cmd_Echo_f, "print a message to the console (useful in scripts)");
1657 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");
1658 Cmd_AddCommand(CMD_SHARED, "unalias",Cmd_UnAlias_f, "remove an alias");
1659 Cmd_AddCommand(CMD_SHARED, "set", Cvar_Set_f, "create or change the value of a console variable");
1660 Cmd_AddCommand(CMD_SHARED, "seta", Cvar_SetA_f, "create or change the value of a console variable that will be saved to config.cfg");
1661 Cmd_AddCommand(CMD_SHARED, "unset", Cvar_Del_f, "delete a cvar (does not work for static ones like _cl_name, or read-only ones)");
1663 #ifdef FILLALLCVARSWITHRUBBISH
1664 Cmd_AddCommand(CMD_SHARED, "fillallcvarswithrubbish", Cvar_FillAll_f, "fill all cvars with a specified number of characters to provoke buffer overruns");
1665 #endif /* FILLALLCVARSWITHRUBBISH */
1667 // 2000-01-09 CmdList, CvarList commands By Matthias "Maddes" Buecher
1668 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
1669 Cmd_AddCommand(CMD_SHARED, "cmdlist", Cmd_List_f, "lists all console commands beginning with the specified prefix or matching the specified wildcard pattern");
1670 Cmd_AddCommand(CMD_SHARED, "cvarlist", Cvar_List_f, "lists all console variables beginning with the specified prefix or matching the specified wildcard pattern");
1671 Cmd_AddCommand(CMD_SHARED, "apropos", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1672 Cmd_AddCommand(CMD_SHARED, "find", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1674 Cmd_AddCommand(CMD_SHARED, "defer", Cmd_Defer_f, "execute a command in the future");
1677 // Support Doom3-style Toggle Command
1678 Cmd_AddCommand(CMD_SHARED | CMD_CLIENT_FROM_SERVER, "toggle", Cmd_Toggle_f, "toggles a console variable's values (use for more info)");
1686 void Cmd_Shutdown(void)
1688 cmd_iter_t *cmd_iter;
1689 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
1691 cmd_state_t *cmd = cmd_iter->cmd;
1693 if (cmd->cbuf->lock)
1695 // we usually have this locked when we get here from Host_Quit_f
1696 Cbuf_Unlock(cmd->cbuf);
1699 Mem_FreePool(&cmd->mempool);
1708 int Cmd_Argc (cmd_state_t *cmd)
1718 const char *Cmd_Argv(cmd_state_t *cmd, int arg)
1720 if (arg >= cmd->argc )
1721 return cmd->null_string;
1722 return cmd->argv[arg];
1730 const char *Cmd_Args (cmd_state_t *cmd)
1739 Parses the given string into command line tokens.
1742 // AK: This function should only be called from ExcuteString because the current design is a bit of an hack
1743 static void Cmd_TokenizeString (cmd_state_t *cmd, const char *text)
1752 // skip whitespace up to a /n
1753 while (*text && ISWHITESPACE(*text) && *text != '\r' && *text != '\n')
1760 if (*text == '\n' || *text == '\r')
1762 // a newline separates commands in the buffer
1763 if (*text == '\r' && text[1] == '\n')
1775 if (!COM_ParseToken_Console(&text))
1778 if (cmd->argc < MAX_ARGS)
1780 l = (int)strlen(com_token) + 1;
1781 if (cmd->cbuf->tokenizebufferpos + l > CMD_TOKENIZELENGTH)
1783 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguments\n", CMD_TOKENIZELENGTH);
1786 memcpy (cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos, com_token, l);
1787 cmd->argv[cmd->argc] = cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos;
1788 cmd->cbuf->tokenizebufferpos += l;
1800 void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const char *description)
1802 cmd_function_t *func;
1803 cmd_function_t *prev, *current;
1805 xcommand_t save = NULL;
1806 qboolean auto_add = false;
1809 for (i = 0; i < 3; i++)
1811 cmd = cmd_iter_all[i].cmd;
1812 if ((flags & cmd->cmd_flags) || (flags & cmd->auto_flags))
1814 if((flags & cmd->auto_flags) && cmd->auto_function)
1817 function = cmd->auto_function;
1821 // fail if the command is a variable name
1822 if (Cvar_FindVar(cmd->cvars, cmd_name, ~0))
1824 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
1830 // fail if the command already exists in this interpreter
1831 for (func = cmd->engine_functions; func; func = func->next)
1833 if (!strcmp(cmd_name, func->name))
1835 if(func->autofunc && !auto_add)
1837 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
1842 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1843 func->flags = flags;
1844 func->name = cmd_name;
1845 func->function = function;
1846 func->description = description;
1847 func->next = cmd->engine_functions;
1848 func->autofunc = auto_add;
1850 // insert it at the right alphanumeric position
1851 for (prev = NULL, current = cmd->engine_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1857 cmd->engine_functions = func;
1859 func->next = current;
1863 // mark csqcfunc if the function already exists in the csqc_functions list
1864 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1866 if (!strcmp(cmd_name, func->name))
1868 func->csqcfunc = true; //[515]: csqc
1874 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1875 func->name = cmd_name;
1876 func->function = function;
1877 func->description = description;
1878 func->csqcfunc = true; //[515]: csqc
1879 func->next = cmd->userdefined->csqc_functions;
1880 func->autofunc = false;
1882 // insert it at the right alphanumeric position
1883 for (prev = NULL, current = cmd->userdefined->csqc_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1889 cmd->userdefined->csqc_functions = func;
1891 func->next = current;
1902 static int Cmd_Compare(const char *s1, const char *s2, size_t len, qboolean casesensitive)
1905 return (casesensitive ? strncmp(s1, s2, len) : strncasecmp(s1, s2, len));
1907 return (casesensitive ? strcmp(s1, s2) : strcasecmp(s1, s2));
1910 cmd_function_t *Cmd_GetCommand(cmd_state_t *cmd, const char *partial, size_t len, qboolean casesensitive)
1912 cmd_function_t *func = NULL;
1915 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1916 if (!Cmd_Compare(partial, func->name, len, casesensitive))
1919 for (func=cmd->engine_functions ; func ; func=func->next)
1920 if (!Cmd_Compare(partial, func->name, len, casesensitive))
1931 qboolean Cmd_Exists (cmd_state_t *cmd, const char *cmd_name)
1933 if(Cmd_GetCommand(cmd, cmd_name, 0, true))
1943 const char *Cmd_CompleteCommand (cmd_state_t *cmd, const char *partial)
1945 cmd_function_t *func;
1947 func = Cmd_GetCommand(cmd, partial, strlen(partial), false);
1954 Cmd_CompleteCountPossible
1956 New function for tab-completion system
1957 Added by EvilTypeGuy
1958 Thanks to Fett erich@heintz.com
1962 int Cmd_CompleteCountPossible (cmd_state_t *cmd, const char *partial)
1964 cmd_function_t *func;
1969 len = strlen(partial);
1974 // Loop through the command list and count all partial matches
1975 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
1976 if (!strncasecmp(partial, func->name, len))
1979 for (func = cmd->engine_functions; func; func = func->next)
1980 if (!strncasecmp(partial, func->name, len))
1987 Cmd_CompleteBuildList
1989 New function for tab-completion system
1990 Added by EvilTypeGuy
1991 Thanks to Fett erich@heintz.com
1995 const char **Cmd_CompleteBuildList (cmd_state_t *cmd, const char *partial)
1997 cmd_function_t *func;
2000 size_t sizeofbuf = (Cmd_CompleteCountPossible (cmd, partial) + 1) * sizeof (const char *);
2003 len = strlen(partial);
2004 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
2005 // Loop through the functions lists and print all matches
2006 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
2007 if (!strncasecmp(partial, func->name, len))
2008 buf[bpos++] = func->name;
2009 for (func = cmd->engine_functions; func; func = func->next)
2010 if (!strncasecmp(partial, func->name, len))
2011 buf[bpos++] = func->name;
2017 // written by LadyHavoc
2018 void Cmd_CompleteCommandPrint (cmd_state_t *cmd, const char *partial)
2020 cmd_function_t *func;
2021 size_t len = strlen(partial);
2022 // Loop through the command list and print all matches
2023 for (func = cmd->userdefined->csqc_functions; func; func = func->next)
2024 if (!strncasecmp(partial, func->name, len))
2025 Con_Printf("^2%s^7: %s\n", func->name, func->description);
2026 for (func = cmd->engine_functions; func; func = func->next)
2027 if (!strncasecmp(partial, func->name, len))
2028 Con_Printf("^2%s^7: %s\n", func->name, func->description);
2034 New function for tab-completion system
2035 Added by EvilTypeGuy
2036 Thanks to Fett erich@heintz.com
2040 const char *Cmd_CompleteAlias (cmd_state_t *cmd, const char *partial)
2045 len = strlen(partial);
2051 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2052 if (!strncasecmp(partial, alias->name, len))
2058 // written by LadyHavoc
2059 void Cmd_CompleteAliasPrint (cmd_state_t *cmd, const char *partial)
2062 size_t len = strlen(partial);
2063 // Loop through the alias list and print all matches
2064 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2065 if (!strncasecmp(partial, alias->name, len))
2066 Con_Printf("^5%s^7: %s", alias->name, alias->value);
2071 Cmd_CompleteAliasCountPossible
2073 New function for tab-completion system
2074 Added by EvilTypeGuy
2075 Thanks to Fett erich@heintz.com
2079 int Cmd_CompleteAliasCountPossible (cmd_state_t *cmd, const char *partial)
2087 len = strlen(partial);
2092 // Loop through the command list and count all partial matches
2093 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2094 if (!strncasecmp(partial, alias->name, len))
2101 Cmd_CompleteAliasBuildList
2103 New function for tab-completion system
2104 Added by EvilTypeGuy
2105 Thanks to Fett erich@heintz.com
2109 const char **Cmd_CompleteAliasBuildList (cmd_state_t *cmd, const char *partial)
2114 size_t sizeofbuf = (Cmd_CompleteAliasCountPossible (cmd, partial) + 1) * sizeof (const char *);
2117 len = strlen(partial);
2118 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
2119 // Loop through the alias list and print all matches
2120 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2121 if (!strncasecmp(partial, alias->name, len))
2122 buf[bpos++] = alias->name;
2128 // TODO: Make this more generic?
2129 void Cmd_ClearCSQCCommands (cmd_state_t *cmd)
2131 cmd_function_t *func;
2132 cmd_function_t **next = &cmd->userdefined->csqc_functions;
2142 extern cvar_t sv_cheats;
2148 A complete command line has been parsed, so try to execute it
2149 FIXME: lookupnoadd the token to speed search?
2152 void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qboolean lockmutex)
2155 cmd_function_t *func;
2158 Cbuf_Lock(cmd->cbuf);
2159 oldpos = cmd->cbuf->tokenizebufferpos;
2162 Cmd_TokenizeString (cmd, text);
2164 // execute the command line
2166 goto done; // no tokens
2169 func = Cmd_GetCommand(cmd, cmd->argv[0], 0, false);
2172 if (func->csqcfunc && CL_VM_ConsoleCommand(text)) //[515]: csqc
2180 func->function(cmd);
2182 Con_Printf("Command \"%s\" can not be executed\n", Cmd_Argv(cmd, 0));
2187 if((func->flags & CMD_CHEAT) && !sv_cheats.integer)
2188 SV_ClientPrintf("No cheats allowed. The server must have sv_cheats set to 1\n");
2190 func->function(cmd);
2197 // if it's a client command and no command was found, say so.
2198 if (cmd->source == src_client)
2200 Con_Printf("Client \"%s\" tried to execute \"%s\"\n", host_client->name, text);
2205 for (a=cmd->userdefined->alias ; a ; a=a->next)
2207 if (!strcasecmp (cmd->argv[0], a->name))
2209 Cmd_ExecuteAlias(cmd, a);
2215 if (!Cvar_Command(cmd) && host.framecount > 0)
2216 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(cmd, 0));
2218 cmd->cbuf->tokenizebufferpos = oldpos;
2220 Cbuf_Unlock(cmd->cbuf);
2227 Returns the position (1 to argc-1) in the command's argument list
2228 where the given parameter apears, or 0 if not present
2232 int Cmd_CheckParm (cmd_state_t *cmd, const char *parm)
2238 Con_Printf ("Cmd_CheckParm: NULL");
2242 for (i = 1; i < Cmd_Argc (cmd); i++)
2243 if (!strcasecmp (parm, Cmd_Argv(cmd, i)))
2251 void Cmd_SaveInitState(void)
2253 cmd_iter_t *cmd_iter;
2254 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2256 cmd_state_t *cmd = cmd_iter->cmd;
2259 for (f = cmd->userdefined->csqc_functions; f; f = f->next)
2260 f->initstate = true;
2261 for (f = cmd->engine_functions; f; f = f->next)
2262 f->initstate = true;
2263 for (a = cmd->userdefined->alias; a; a = a->next)
2265 a->initstate = true;
2266 a->initialvalue = Mem_strdup(zonemempool, a->value);
2269 Cvar_SaveInitState(&cvars_all);
2272 void Cmd_RestoreInitState(void)
2274 cmd_iter_t *cmd_iter;
2275 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2277 cmd_state_t *cmd = cmd_iter->cmd;
2278 cmd_function_t *f, **fp;
2279 cmdalias_t *a, **ap;
2280 for (fp = &cmd->userdefined->csqc_functions; (f = *fp);)
2286 // destroy this command, it didn't exist at init
2287 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2292 for (fp = &cmd->engine_functions; (f = *fp);)
2298 // destroy this command, it didn't exist at init
2299 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2304 for (ap = &cmd->userdefined->alias; (a = *ap);)
2308 // restore this alias, it existed at init
2309 if (strcmp(a->value ? a->value : "", a->initialvalue ? a->initialvalue : ""))
2311 Con_DPrintf("Cmd_RestoreInitState: Restoring alias %s\n", a->name);
2314 a->value = Mem_strdup(zonemempool, a->initialvalue);
2320 // free this alias, it didn't exist at init...
2321 Con_DPrintf("Cmd_RestoreInitState: Destroying alias %s\n", a->name);
2329 Cvar_RestoreInitState(&cvars_all);