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},
44 mempool_t *cbuf_mempool;
46 // we only run the +whatever commandline arguments once
47 qbool host_stuffcmdsrun = false;
49 //=============================================================================
51 void Cbuf_Lock(cmd_buf_t *cbuf)
53 Thread_LockMutex(cbuf->lock);
56 void Cbuf_Unlock(cmd_buf_t *cbuf)
58 Thread_UnlockMutex(cbuf->lock);
66 Causes execution of the remainder of the command buffer to be delayed until
67 next frame. This allows commands like:
68 bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
71 static void Cmd_Wait_f (cmd_state_t *cmd)
73 cmd->cbuf->wait = true;
80 Cause a command to be executed after a delay.
83 static cmd_input_t *Cbuf_LinkGet(cmd_buf_t *cbuf, cmd_input_t *existing);
84 static void Cmd_Defer_f (cmd_state_t *cmd)
87 cmd_buf_t *cbuf = cmd->cbuf;
89 if(Cmd_Argc(cmd) == 1)
91 if(List_IsEmpty(&cbuf->deferred))
92 Con_Printf("No commands are pending.\n");
96 List_ForEach(pos, &cbuf->deferred)
98 current = List_Container(*pos, cmd_input_t, list);
99 Con_Printf("-> In %9.2f: %s\n", current->delay, current->text);
103 else if(Cmd_Argc(cmd) == 2 && !strcasecmp("clear", Cmd_Argv(cmd, 1)))
105 while(!List_IsEmpty(&cbuf->deferred))
106 List_Move_Tail(cbuf->deferred.next, &cbuf->free);
108 else if(Cmd_Argc(cmd) == 3)
110 const char *text = Cmd_Argv(cmd, 2);
111 current = Cbuf_LinkGet(cbuf, NULL);
112 current->length = strlen(text);
113 current->source = cmd;
114 current->delay = atof(Cmd_Argv(cmd, 1));
116 if(current->size < current->length)
118 current->text = (char *)Mem_Realloc(cbuf_mempool, current->text, current->length + 1);
119 current->size = current->length;
122 strlcpy(current->text, text, current->length + 1);
124 List_Move_Tail(¤t->list, &cbuf->deferred);
128 Con_Printf("usage: defer <seconds> <command>\n"
138 Print something to the center of the screen using SCR_Centerprint
141 static void Cmd_Centerprint_f (cmd_state_t *cmd)
143 char msg[MAX_INPUTLINE];
144 unsigned int i, c, p;
148 strlcpy(msg, Cmd_Argv(cmd,1), sizeof(msg));
149 for(i = 2; i < c; ++i)
151 strlcat(msg, " ", sizeof(msg));
152 strlcat(msg, Cmd_Argv(cmd, i), sizeof(msg));
154 c = (unsigned int)strlen(msg);
155 for(p = 0, i = 0; i < c; ++i)
161 else if(msg[i+1] == '\\')
173 SCR_CenterPrint(msg);
178 =============================================================================
182 =============================================================================
185 static cmd_input_t *Cbuf_LinkGet(cmd_buf_t *cbuf, cmd_input_t *existing)
187 cmd_input_t *ret = NULL;
188 if(existing && existing->pending)
190 else if(!List_IsEmpty(&cbuf->free))
192 ret = List_Container(*cbuf->free.next, cmd_input_t, list);
194 ret->pending = false;
199 static cmd_input_t *Cmd_AllocInputNode(void)
201 cmd_input_t *node = (cmd_input_t *)Mem_Alloc(cbuf_mempool, sizeof(cmd_input_t));
202 node->list.prev = node->list.next = &node->list;
203 node->size = node->length = node->pending = 0;
207 static size_t Cmd_ParseInput (cmd_input_t **output, char **input)
209 size_t pos, cmdsize = 0, start = 0;
210 qbool command = false, lookahead = false;
211 qbool quotes = false, comment = false;
212 qbool escaped = false;
215 * The Quake command-line is super basic. It can be entered in the console
216 * or in config files. A semicolon is used to terminate a command and chain
217 * them together. Otherwise, a newline delineates command input.
219 * In most engines, the Quake command-line is a simple linear text buffer that
220 * is parsed when it executes. In Darkplaces, we use a linked list of command
221 * input and parse the input on the spot.
223 * This was done because Darkplaces allows multiple command interpreters on the
224 * same thread. Previously, each interpreter maintained its own buffer and this
225 * caused problems related to execution order, and maintaining a single simple
226 * buffer for all interpreters makes it non-trivial to keep track of which
227 * command should execute on which interpreter.
230 // Run until command and lookahead are both true, or until we run out of input.
231 for (pos = 0; (*input)[pos]; pos++)
233 // Look for newlines and semicolons. Ignore semicolons in quotes.
234 switch((*input)[pos])
242 if(!comment) // Not a newline so far. Still not a valid command yet.
244 if(!quotes && (*input)[pos] == ';') // Ignore semicolons in quotes.
246 else if (ISCOMMENT((*input), pos)) // Comments
261 switch((*input)[pos])
270 if (!escaped && quotes)
279 if(cmdsize && !command)
282 if(command && lookahead)
291 *output = Cmd_AllocInputNode();
293 if((*output)->pending)
294 offset = (*output)->length;
296 (*output)->length += cmdsize;
298 if((*output)->size < (*output)->length)
300 (*output)->text = (char *)Mem_Realloc(cbuf_mempool, (*output)->text, (*output)->length + 1);
301 (*output)->size = (*output)->length;
304 strlcpy(&(*output)->text[offset], &(*input)[start], cmdsize + 1);
305 (*output)->pending = !lookahead;
308 // Set input to its new position. Can be NULL.
309 *input = &(*input)[pos];
314 // Cloudwalk: Not happy with this, but it works.
315 static void Cbuf_LinkCreate(cmd_state_t *cmd, llist_t *head, cmd_input_t *existing, const char *text)
317 char *in = (char *)&text[0];
318 cmd_buf_t *cbuf = cmd->cbuf;
319 size_t totalsize = 0, newsize = 0;
320 cmd_input_t *current = NULL;
322 // Slide the pointer down until we reach the end
325 current = Cbuf_LinkGet(cbuf, existing);
326 newsize = Cmd_ParseInput(¤t, &in);
331 if(current != existing)
333 current->source = cmd;
334 List_Move_Tail(¤t->list, head);
337 totalsize += newsize;
339 else if (current == existing && !totalsize)
340 current->pending = false;
344 cbuf->size += totalsize;
351 Adds command text at the end of the buffer
354 void Cbuf_AddText (cmd_state_t *cmd, const char *text)
356 size_t l = strlen(text);
357 cmd_buf_t *cbuf = cmd->cbuf;
358 llist_t llist = {&llist, &llist};
362 if (cbuf->maxsize - cbuf->size <= l)
363 Con_Print("Cbuf_AddText: overflow\n");
366 Cbuf_LinkCreate(cmd, &llist, (List_IsEmpty(&cbuf->start) ? NULL : List_Container(*cbuf->start.prev, cmd_input_t, list)), text);
367 if(!List_IsEmpty(&llist))
368 List_Splice_Tail(&llist, &cbuf->start);
377 Adds command text immediately after the current command
378 FIXME: actually change the command buffer to do less copying
381 void Cbuf_InsertText (cmd_state_t *cmd, const char *text)
383 cmd_buf_t *cbuf = cmd->cbuf;
384 llist_t llist = {&llist, &llist};
385 size_t l = strlen(text);
389 // we need to memmove the existing text and stuff this in before it...
390 if (cbuf->size + l >= cbuf->maxsize)
391 Con_Print("Cbuf_InsertText: overflow\n");
394 Cbuf_LinkCreate(cmd, &llist, List_Container(*cbuf->start.next, cmd_input_t, list), text);
395 if(!List_IsEmpty(&llist))
396 List_Splice(&llist, &cbuf->start);
404 Cbuf_Execute_Deferred --blub
407 static void Cbuf_Execute_Deferred (cmd_buf_t *cbuf)
410 cmd_input_t *current;
413 if (host.realtime - cbuf->deferred_oldtime < 0 || host.realtime - cbuf->deferred_oldtime > 1800)
414 cbuf->deferred_oldtime = host.realtime;
415 eat = host.realtime - cbuf->deferred_oldtime;
416 if (eat < (1.0 / 120.0))
418 cbuf->deferred_oldtime = host.realtime;
420 List_ForEach(pos, &cbuf->deferred)
422 current = List_Container(*pos, cmd_input_t, list);
423 current->delay -= eat;
424 if(current->delay <= 0)
426 cbuf->size += current->length;
427 List_Move(pos, &cbuf->start);
428 // We must return and come back next frame or the engine will freeze. Fragile... like glass :3
439 static qbool Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmd_alias_t *alias );
440 void Cbuf_Execute (cmd_buf_t *cbuf)
442 cmd_input_t *current;
443 char preprocessed[MAX_INPUTLINE];
446 // LadyHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
447 cbuf->tokenizebufferpos = 0;
449 while (!List_IsEmpty(&cbuf->start))
452 * Delete the text from the command buffer and move remaining
453 * commands down. This is necessary because commands (exec, alias)
454 * can insert data at the beginning of the text buffer
456 current = List_Container(*cbuf->start.next, cmd_input_t, list);
458 // Recycle memory so using WASD doesn't cause a malloc and free
459 List_Move_Tail(¤t->list, &cbuf->free);
462 * Assume we're rolling with the current command-line and
463 * always set this false because alias expansion or cbuf insertion
464 * without a newline may set this true, and cause weirdness.
466 current->pending = false;
468 cbuf->size -= current->length;
470 firstchar = current->text;
471 while(*firstchar && ISWHITESPACE(*firstchar))
473 if((strncmp(firstchar, "alias", 5) || !ISWHITESPACE(firstchar[5])) &&
474 (strncmp(firstchar, "bind", 4) || !ISWHITESPACE(firstchar[4])) &&
475 (strncmp(firstchar, "in_bind", 7) || !ISWHITESPACE(firstchar[7])))
477 if(Cmd_PreprocessString(current->source, current->text, preprocessed, sizeof(preprocessed), NULL ))
478 Cmd_ExecuteString(current->source, preprocessed, src_local, false);
482 Cmd_ExecuteString (current->source, current->text, src_local, false);
490 * Skip out while text still remains in
491 * buffer, leaving it for next frame
499 void Cbuf_Frame(cmd_buf_t *cbuf)
501 Cbuf_Execute_Deferred(cbuf);
504 SV_LockThreadMutex();
506 SV_UnlockThreadMutex();
511 ==============================================================================
515 ==============================================================================
522 Adds command line parameters as script statements
523 Commands lead with a +, and continue until a - or another +
524 quake +prog jctest.qp +cmd amlev1
525 quake -nosound +cmd amlev1
528 static void Cmd_StuffCmds_f (cmd_state_t *cmd)
531 // this is for all commandline options combined (and is bounds checked)
532 char build[MAX_INPUTLINE];
534 if (Cmd_Argc (cmd) != 1)
536 Con_Print("stuffcmds : execute command line parameters\n");
540 // no reason to run the commandline arguments twice
541 if (host_stuffcmdsrun)
544 host_stuffcmdsrun = true;
547 for (i = 0;i < sys.argc;i++)
549 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)
552 while (sys.argv[i][j])
553 build[l++] = sys.argv[i][j++];
555 for (;i < sys.argc;i++)
559 if ((sys.argv[i][0] == '+' || sys.argv[i][0] == '-') && (sys.argv[i][1] < '0' || sys.argv[i][1] > '9'))
561 if (l + strlen(sys.argv[i]) + 4 > sizeof(build) - 1)
564 if (strchr(sys.argv[i], ' '))
566 for (j = 0;sys.argv[i][j];j++)
567 build[l++] = sys.argv[i][j];
568 if (strchr(sys.argv[i], ' '))
575 // now terminate the combined string and prepend it to the command buffer
576 // we already reserved space for the terminator
578 Cbuf_InsertText (cmd, build);
581 static void Cmd_Exec(cmd_state_t *cmd, const char *filename)
584 size_t filenameLen = strlen(filename);
586 !strcmp(filename, "default.cfg") ||
587 (filenameLen >= 12 && !strcmp(filename + filenameLen - 12, "/default.cfg"));
589 if (!strcmp(filename, "config.cfg"))
591 filename = CONFIGFILENAME;
592 if (Sys_CheckParm("-noconfig"))
593 return; // don't execute config.cfg
596 f = (char *)FS_LoadFile (filename, tempmempool, false, NULL);
599 Con_Printf("couldn't exec %s\n",filename);
602 Con_Printf("execing %s\n",filename);
604 // if executing default.cfg for the first time, lock the cvar defaults
605 // it may seem backwards to insert this text BEFORE the default.cfg
606 // but Cbuf_InsertText inserts before, so this actually ends up after it.
608 Cbuf_InsertText(cmd, "\ncvar_lockdefaults\n");
610 Cbuf_InsertText (cmd, f);
615 // special defaults for specific games go here, these execute before default.cfg
616 // Nehahra pushable crates malfunction in some levels if this is on
617 // Nehahra NPC AI is confused by blowupfallenzombies
621 Cbuf_InsertText(cmd, "\n"
622 "sv_gameplayfix_blowupfallenzombies 0\n"
623 "sv_gameplayfix_findradiusdistancetobox 0\n"
624 "sv_gameplayfix_grenadebouncedownslopes 0\n"
625 "sv_gameplayfix_slidemoveprojectiles 0\n"
626 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
627 "sv_gameplayfix_setmodelrealbox 0\n"
628 "sv_gameplayfix_droptofloorstartsolid 0\n"
629 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
630 "sv_gameplayfix_noairborncorpse 0\n"
631 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
632 "sv_gameplayfix_easierwaterjump 0\n"
633 "sv_gameplayfix_delayprojectiles 0\n"
634 "sv_gameplayfix_multiplethinksperframe 0\n"
635 "sv_gameplayfix_fixedcheckwatertransition 0\n"
636 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
637 "sv_gameplayfix_swiminbmodels 0\n"
638 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
639 "sys_ticrate 0.01388889\n"
641 "r_shadow_bumpscale_basetexture 0\n"
642 "csqc_polygons_defaultmaterial_nocullface 0\n"
646 Cbuf_InsertText(cmd, "\n"
647 "sv_gameplayfix_blowupfallenzombies 0\n"
648 "sv_gameplayfix_findradiusdistancetobox 0\n"
649 "sv_gameplayfix_grenadebouncedownslopes 0\n"
650 "sv_gameplayfix_slidemoveprojectiles 0\n"
651 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
652 "sv_gameplayfix_setmodelrealbox 0\n"
653 "sv_gameplayfix_droptofloorstartsolid 0\n"
654 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
655 "sv_gameplayfix_noairborncorpse 0\n"
656 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
657 "sv_gameplayfix_easierwaterjump 0\n"
658 "sv_gameplayfix_delayprojectiles 0\n"
659 "sv_gameplayfix_multiplethinksperframe 0\n"
660 "sv_gameplayfix_fixedcheckwatertransition 0\n"
661 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
662 "sv_gameplayfix_swiminbmodels 0\n"
663 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
664 "sys_ticrate 0.01388889\n"
666 "r_shadow_bumpscale_basetexture 0\n"
667 "csqc_polygons_defaultmaterial_nocullface 0\n"
670 // 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.
671 // 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
672 // hipnotic mission pack has issues in their proximity mine sticking code, which causes them to bounce off.
675 Cbuf_InsertText(cmd, "\n"
676 "sv_gameplayfix_blowupfallenzombies 0\n"
677 "sv_gameplayfix_findradiusdistancetobox 0\n"
678 "sv_gameplayfix_grenadebouncedownslopes 0\n"
679 "sv_gameplayfix_slidemoveprojectiles 0\n"
680 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
681 "sv_gameplayfix_setmodelrealbox 0\n"
682 "sv_gameplayfix_droptofloorstartsolid 0\n"
683 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
684 "sv_gameplayfix_noairborncorpse 0\n"
685 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
686 "sv_gameplayfix_easierwaterjump 0\n"
687 "sv_gameplayfix_delayprojectiles 0\n"
688 "sv_gameplayfix_multiplethinksperframe 0\n"
689 "sv_gameplayfix_fixedcheckwatertransition 0\n"
690 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
691 "sv_gameplayfix_swiminbmodels 0\n"
692 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
695 "r_shadow_bumpscale_basetexture 0\n"
696 "csqc_polygons_defaultmaterial_nocullface 0\n"
699 // rogue mission pack has a guardian boss that does not wake up if findradius returns one of the entities around its spawn area
701 Cbuf_InsertText(cmd, "\n"
702 "sv_gameplayfix_blowupfallenzombies 0\n"
703 "sv_gameplayfix_findradiusdistancetobox 0\n"
704 "sv_gameplayfix_grenadebouncedownslopes 0\n"
705 "sv_gameplayfix_slidemoveprojectiles 0\n"
706 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
707 "sv_gameplayfix_setmodelrealbox 0\n"
708 "sv_gameplayfix_droptofloorstartsolid 0\n"
709 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
710 "sv_gameplayfix_noairborncorpse 0\n"
711 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
712 "sv_gameplayfix_easierwaterjump 0\n"
713 "sv_gameplayfix_delayprojectiles 0\n"
714 "sv_gameplayfix_multiplethinksperframe 0\n"
715 "sv_gameplayfix_fixedcheckwatertransition 0\n"
716 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
717 "sv_gameplayfix_swiminbmodels 0\n"
718 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
719 "sys_ticrate 0.01388889\n"
721 "r_shadow_bumpscale_basetexture 0\n"
722 "csqc_polygons_defaultmaterial_nocullface 0\n"
726 Cbuf_InsertText(cmd, "\n"
727 "sv_gameplayfix_blowupfallenzombies 0\n"
728 "sv_gameplayfix_findradiusdistancetobox 0\n"
729 "sv_gameplayfix_grenadebouncedownslopes 0\n"
730 "sv_gameplayfix_slidemoveprojectiles 0\n"
731 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
732 "sv_gameplayfix_setmodelrealbox 0\n"
733 "sv_gameplayfix_droptofloorstartsolid 0\n"
734 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
735 "sv_gameplayfix_noairborncorpse 0\n"
736 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
737 "sv_gameplayfix_easierwaterjump 0\n"
738 "sv_gameplayfix_delayprojectiles 0\n"
739 "sv_gameplayfix_multiplethinksperframe 0\n"
740 "sv_gameplayfix_fixedcheckwatertransition 0\n"
741 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
742 "sv_gameplayfix_swiminbmodels 0\n"
743 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
744 "sys_ticrate 0.01388889\n"
746 "r_shadow_bumpscale_basetexture 4\n"
747 "csqc_polygons_defaultmaterial_nocullface 0\n"
751 Cbuf_InsertText(cmd, "\n"
752 "sv_gameplayfix_blowupfallenzombies 1\n"
753 "sv_gameplayfix_findradiusdistancetobox 1\n"
754 "sv_gameplayfix_grenadebouncedownslopes 1\n"
755 "sv_gameplayfix_slidemoveprojectiles 1\n"
756 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
757 "sv_gameplayfix_setmodelrealbox 1\n"
758 "sv_gameplayfix_droptofloorstartsolid 1\n"
759 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
760 "sv_gameplayfix_noairborncorpse 1\n"
761 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
762 "sv_gameplayfix_easierwaterjump 1\n"
763 "sv_gameplayfix_delayprojectiles 1\n"
764 "sv_gameplayfix_multiplethinksperframe 1\n"
765 "sv_gameplayfix_fixedcheckwatertransition 1\n"
766 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
767 "sv_gameplayfix_swiminbmodels 1\n"
768 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
769 "sys_ticrate 0.01388889\n"
770 "sv_gameplayfix_q2airaccelerate 1\n"
771 "sv_gameplayfix_stepmultipletimes 1\n"
772 "csqc_polygons_defaultmaterial_nocullface 1\n"
773 "con_chatsound_team_mask 13\n"
777 case GAME_VORETOURNAMENT:
778 // 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
779 Cbuf_InsertText(cmd, "\n"
780 "csqc_polygons_defaultmaterial_nocullface 1\n"
781 "con_chatsound_team_mask 13\n"
782 "sv_gameplayfix_customstats 1\n"
785 // Steel Storm: Burning Retribution csqc misinterprets CSQC_InputEvent if type is a value other than 0 or 1
786 case GAME_STEELSTORM:
787 Cbuf_InsertText(cmd, "\n"
788 "sv_gameplayfix_blowupfallenzombies 1\n"
789 "sv_gameplayfix_findradiusdistancetobox 1\n"
790 "sv_gameplayfix_grenadebouncedownslopes 1\n"
791 "sv_gameplayfix_slidemoveprojectiles 1\n"
792 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
793 "sv_gameplayfix_setmodelrealbox 1\n"
794 "sv_gameplayfix_droptofloorstartsolid 1\n"
795 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
796 "sv_gameplayfix_noairborncorpse 1\n"
797 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
798 "sv_gameplayfix_easierwaterjump 1\n"
799 "sv_gameplayfix_delayprojectiles 1\n"
800 "sv_gameplayfix_multiplethinksperframe 1\n"
801 "sv_gameplayfix_fixedcheckwatertransition 1\n"
802 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
803 "sv_gameplayfix_swiminbmodels 1\n"
804 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
805 "sys_ticrate 0.01388889\n"
806 "cl_csqc_generatemousemoveevents 0\n"
807 "csqc_polygons_defaultmaterial_nocullface 1\n"
811 Cbuf_InsertText(cmd, "\n"
812 "sv_gameplayfix_blowupfallenzombies 1\n"
813 "sv_gameplayfix_findradiusdistancetobox 1\n"
814 "sv_gameplayfix_grenadebouncedownslopes 1\n"
815 "sv_gameplayfix_slidemoveprojectiles 1\n"
816 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
817 "sv_gameplayfix_setmodelrealbox 1\n"
818 "sv_gameplayfix_droptofloorstartsolid 1\n"
819 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
820 "sv_gameplayfix_noairborncorpse 1\n"
821 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
822 "sv_gameplayfix_easierwaterjump 1\n"
823 "sv_gameplayfix_delayprojectiles 1\n"
824 "sv_gameplayfix_multiplethinksperframe 1\n"
825 "sv_gameplayfix_fixedcheckwatertransition 1\n"
826 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
827 "sv_gameplayfix_swiminbmodels 1\n"
828 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
829 "sys_ticrate 0.01388889\n"
830 "csqc_polygons_defaultmaterial_nocullface 0\n"
842 static void Cmd_Exec_f (cmd_state_t *cmd)
847 if (Cmd_Argc(cmd) != 2)
849 Con_Print("exec <filename> : execute a script file\n");
853 s = FS_Search(Cmd_Argv(cmd, 1), true, true, NULL);
854 if(!s || !s->numfilenames)
856 Con_Printf("couldn't exec %s\n",Cmd_Argv(cmd, 1));
860 for(i = 0; i < s->numfilenames; ++i)
861 Cmd_Exec(cmd, s->filenames[i]);
871 Just prints the rest of the line to the console
874 static void Cmd_Echo_f (cmd_state_t *cmd)
878 for (i=1 ; i<Cmd_Argc(cmd) ; i++)
879 Con_Printf("%s ",Cmd_Argv(cmd, i));
884 // Support Doom3-style Toggle Console Command
889 Toggles a specified console variable amongst the values specified (default is 0 and 1)
892 static void Cmd_Toggle_f(cmd_state_t *cmd)
894 // Acquire Number of Arguments
895 int nNumArgs = Cmd_Argc(cmd);
898 // No Arguments Specified; Print Usage
899 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");
901 { // Correct Arguments Specified
902 // Acquire Potential CVar
903 cvar_t* cvCVar = Cvar_FindVar(cmd->cvars, Cmd_Argv(cmd, 1), cmd->cvars_flagsmask);
910 Cvar_SetValueQuick(cvCVar, 0);
912 Cvar_SetValueQuick(cvCVar, 1);
916 { // 0 and Specified Usage
917 if(cvCVar->integer == atoi(Cmd_Argv(cmd, 2) ) )
918 // CVar is Specified Value; // Reset to 0
919 Cvar_SetValueQuick(cvCVar, 0);
921 if(cvCVar->integer == 0)
922 // CVar is 0; Specify Value
923 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
925 // CVar does not match; Reset to 0
926 Cvar_SetValueQuick(cvCVar, 0);
929 { // Variable Values Specified
933 for(nCnt = 2; nCnt < nNumArgs; nCnt++)
934 { // Cycle through Values
935 if( strcmp(cvCVar->string, Cmd_Argv(cmd, nCnt) ) == 0)
936 { // Current Value Located; Increment to Next
937 if( (nCnt + 1) == nNumArgs)
938 // Max Value Reached; Reset
939 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
942 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, nCnt + 1) );
951 // Value not Found; Reset to Original
952 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
958 Con_Printf("ERROR : CVar '%s' not found\n", Cmd_Argv(cmd, 1) );
967 Creates a new command that executes a command string (possibly ; seperated)
970 static void Cmd_Alias_f (cmd_state_t *cmd)
973 char line[MAX_INPUTLINE];
978 if (Cmd_Argc(cmd) == 1)
980 Con_Print("Current alias commands:\n");
981 for (a = cmd->userdefined->alias ; a ; a=a->next)
982 Con_Printf("%s : %s", a->name, a->value);
986 s = Cmd_Argv(cmd, 1);
987 if (strlen(s) >= MAX_ALIAS_NAME)
989 Con_Print("Alias name is too long\n");
993 // if the alias already exists, reuse it
994 for (a = cmd->userdefined->alias ; a ; a=a->next)
996 if (!strcmp(s, a->name))
1005 cmd_alias_t *prev, *current;
1007 a = (cmd_alias_t *)Z_Malloc (sizeof(cmd_alias_t));
1008 strlcpy (a->name, s, sizeof (a->name));
1009 // insert it at the right alphanumeric position
1010 for( prev = NULL, current = cmd->userdefined->alias ; current && strcmp( current->name, a->name ) < 0 ; prev = current, current = current->next )
1015 cmd->userdefined->alias = a;
1021 // copy the rest of the command line
1022 line[0] = 0; // start out with a null string
1024 for (i=2 ; i < c ; i++)
1027 strlcat (line, " ", sizeof (line));
1028 strlcat (line, Cmd_Argv(cmd, i), sizeof (line));
1030 strlcat (line, "\n", sizeof (line));
1032 alloclen = strlen (line) + 1;
1034 line[alloclen - 2] = '\n'; // to make sure a newline is appended even if too long
1035 a->value = (char *)Z_Malloc (alloclen);
1036 memcpy (a->value, line, alloclen);
1043 Remove existing aliases.
1046 static void Cmd_UnAlias_f (cmd_state_t *cmd)
1052 if(Cmd_Argc(cmd) == 1)
1054 Con_Print("unalias: Usage: unalias alias1 [alias2 ...]\n");
1058 for(i = 1; i < Cmd_Argc(cmd); ++i)
1060 s = Cmd_Argv(cmd, i);
1062 for(a = cmd->userdefined->alias; a; p = a, a = a->next)
1064 if(!strcmp(s, a->name))
1066 if (a->initstate) // we can not remove init aliases
1068 if(a == cmd->userdefined->alias)
1069 cmd->userdefined->alias = a->next;
1078 Con_Printf("unalias: %s alias not found\n", s);
1083 =============================================================================
1087 =============================================================================
1090 static const char *Cmd_GetDirectCvarValue(cmd_state_t *cmd, const char *varname, cmd_alias_t *alias, qbool *is_multiple)
1095 static char vabuf[1024]; // cmd_mutex
1098 *is_multiple = false;
1100 if(!varname || !*varname)
1105 if(!strcmp(varname, "*"))
1108 *is_multiple = true;
1109 return Cmd_Args(cmd);
1111 else if(!strcmp(varname, "#"))
1113 return va(vabuf, sizeof(vabuf), "%d", Cmd_Argc(cmd));
1115 else if(varname[strlen(varname) - 1] == '-')
1117 argno = strtol(varname, &endptr, 10);
1118 if(endptr == varname + strlen(varname) - 1)
1120 // whole string is a number, apart from the -
1121 const char *p = Cmd_Args(cmd);
1122 for(; argno > 1; --argno)
1123 if(!COM_ParseToken_Console(&p))
1128 *is_multiple = true;
1130 // kill pre-argument whitespace
1131 for (;*p && ISWHITESPACE(*p);p++)
1140 argno = strtol(varname, &endptr, 10);
1143 // whole string is a number
1144 // NOTE: we already made sure we don't have an empty cvar name!
1145 if(argno >= 0 && argno < Cmd_Argc(cmd))
1146 return Cmd_Argv(cmd, argno);
1151 if((cvar = Cvar_FindVar(cmd->cvars, varname, cmd->cvars_flagsmask)) && !(cvar->flags & CF_PRIVATE))
1152 return cvar->string;
1157 qbool Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset, qbool putquotes)
1159 qbool quote_quot = !!strchr(quoteset, '"');
1160 qbool quote_backslash = !!strchr(quoteset, '\\');
1161 qbool quote_dollar = !!strchr(quoteset, '$');
1170 *out++ = '"'; --outlen;
1176 if(*in == '"' && quote_quot)
1180 *out++ = '\\'; --outlen;
1181 *out++ = '"'; --outlen;
1183 else if(*in == '\\' && quote_backslash)
1187 *out++ = '\\'; --outlen;
1188 *out++ = '\\'; --outlen;
1190 else if(*in == '$' && quote_dollar)
1194 *out++ = '$'; --outlen;
1195 *out++ = '$'; --outlen;
1201 *out++ = *in; --outlen;
1216 static const char *Cmd_GetCvarValue(cmd_state_t *cmd, const char *var, size_t varlen, cmd_alias_t *alias)
1218 static char varname[MAX_INPUTLINE]; // cmd_mutex
1219 static char varval[MAX_INPUTLINE]; // cmd_mutex
1220 const char *varstr = NULL;
1222 qbool required = false;
1223 qbool optional = false;
1224 static char asis[] = "asis"; // just to suppress const char warnings
1226 if(varlen >= MAX_INPUTLINE)
1227 varlen = MAX_INPUTLINE - 1;
1228 memcpy(varname, var, varlen);
1229 varname[varlen] = 0;
1230 varfunc = strchr(varname, ' ');
1242 Con_Printf(CON_WARN "Warning: Could not expand $ in alias %s\n", alias->name);
1244 Con_Printf(CON_WARN "Warning: Could not expand $\n");
1252 while((p = strchr(varfunc, '?')))
1255 memmove(p, p+1, strlen(p)); // with final NUL
1258 while((p = strchr(varfunc, '!')))
1261 memmove(p, p+1, strlen(p)); // with final NUL
1264 while((p = strchr(varfunc, ' ')))
1266 memmove(p, p+1, strlen(p)); // with final NUL
1268 // if no function is left, NULL it
1273 if(varname[0] == '$')
1274 varstr = Cmd_GetDirectCvarValue(cmd, Cmd_GetDirectCvarValue(cmd, varname + 1, alias, NULL), alias, NULL);
1277 qbool is_multiple = false;
1278 // Exception: $* and $n- don't use the quoted form by default
1279 varstr = Cmd_GetDirectCvarValue(cmd, varname, alias, &is_multiple);
1290 Con_Printf(CON_ERROR "Error: Could not expand $%s in alias %s\n", varname, alias->name);
1292 Con_Printf(CON_ERROR "Error: Could not expand $%s\n", varname);
1302 Con_Printf(CON_WARN "Warning: Could not expand $%s in alias %s\n", varname, alias->name);
1304 Con_Printf(CON_WARN "Warning: Could not expand $%s\n", varname);
1305 dpsnprintf(varval, sizeof(varval), "$%s", varname);
1310 if(!varfunc || !strcmp(varfunc, "q")) // note: quoted form is default, use "asis" to override!
1312 // quote it so it can be used inside double quotes
1313 // we just need to replace " by \", and of course, double backslashes
1314 Cmd_QuoteString(varval, sizeof(varval), varstr, "\"\\", false);
1317 else if(!strcmp(varfunc, "asis"))
1322 Con_Printf("Unknown variable function %s\n", varfunc);
1328 Cmd_PreprocessString
1330 Preprocesses strings and replaces $*, $param#, $cvar accordingly. Also strips comments.
1332 static qbool Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmd_alias_t *alias ) {
1338 // don't crash if there's no room in the outtext buffer
1339 if( maxoutlen == 0 ) {
1342 maxoutlen--; // because of \0
1347 while( *in && outlen < maxoutlen ) {
1349 // this is some kind of expansion, see what comes after the $
1352 // The console does the following preprocessing:
1354 // - $$ is transformed to a single dollar sign.
1355 // - $var or ${var} are expanded to the contents of the named cvar,
1356 // with quotation marks and backslashes quoted so it can safely
1357 // be used inside quotation marks (and it should always be used
1359 // - ${var asis} inserts the cvar value as is, without doing this
1361 // - ${var ?} silently expands to the empty string if
1362 // $var does not exist
1363 // - ${var !} fails expansion and executes nothing if
1364 // $var does not exist
1365 // - prefix the cvar name with a dollar sign to do indirection;
1366 // for example, if $x has the value timelimit, ${$x} will return
1367 // the value of $timelimit
1368 // - when expanding an alias, the special variable name $* refers
1369 // to all alias parameters, and a number refers to that numbered
1370 // alias parameter, where the name of the alias is $0, the first
1371 // parameter is $1 and so on; as a special case, $* inserts all
1372 // parameters, without extra quoting, so one can use $* to just
1373 // pass all parameters around. All parameters starting from $n
1374 // can be referred to as $n- (so $* is equivalent to $1-).
1375 // - ${* q} and ${n- q} force quoting anyway
1377 // Note: when expanding an alias, cvar expansion is done in the SAME step
1378 // as alias expansion so that alias parameters or cvar values containing
1379 // dollar signs have no unwanted bad side effects. However, this needs to
1380 // be accounted for when writing complex aliases. For example,
1381 // alias foo "set x NEW; echo $x"
1382 // actually expands to
1383 // "set x NEW; echo OLD"
1384 // and will print OLD! To work around this, use a second alias:
1385 // alias foo "set x NEW; foo2"
1386 // alias foo2 "echo $x"
1388 // Also note: lines starting with alias are exempt from cvar expansion.
1389 // If you want cvar expansion, write "alias" instead:
1392 // alias foo "echo $x"
1393 // "alias" bar "echo $x"
1396 // foo will print 2, because the variable $x will be expanded when the alias
1397 // gets expanded. bar will print 1, because the variable $x was expanded
1398 // at definition time. foo can be equivalently defined as
1400 // "alias" foo "echo $$x"
1402 // because at definition time, $$ will get replaced to a single $.
1407 } else if(*in == '{') {
1408 varlen = strcspn(in + 1, "}");
1409 if(in[varlen + 1] == '}')
1411 val = Cmd_GetCvarValue(cmd, in + 1, varlen, alias);
1423 varlen = strspn(in, "#*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-");
1424 val = Cmd_GetCvarValue(cmd, in, varlen, alias);
1431 // insert the cvar value
1432 while(*val && outlen < maxoutlen)
1433 outtext[outlen++] = *val++;
1438 // copy the unexpanded text
1439 outtext[outlen++] = '$';
1440 while(eat && outlen < maxoutlen)
1442 outtext[outlen++] = *in++;
1448 outtext[outlen++] = *in++;
1450 outtext[outlen] = 0;
1458 Called for aliases and fills in the alias into the cbuffer
1461 static void Cmd_ExecuteAlias (cmd_state_t *cmd, cmd_alias_t *alias)
1463 static char buffer[ MAX_INPUTLINE ]; // cmd_mutex
1464 static char buffer2[ MAX_INPUTLINE ]; // cmd_mutex
1465 qbool ret = Cmd_PreprocessString( cmd, alias->value, buffer, sizeof(buffer) - 2, alias );
1468 // insert at start of command buffer, so that aliases execute in order
1469 // (fixes bug introduced by Black on 20050705)
1471 // Note: Cbuf_PreprocessString will be called on this string AGAIN! So we
1472 // have to make sure that no second variable expansion takes place, otherwise
1473 // alias parameters containing dollar signs can have bad effects.
1474 Cmd_QuoteString(buffer2, sizeof(buffer2), buffer, "$", false);
1475 Cbuf_InsertText(cmd, buffer2);
1482 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
1483 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
1487 static void Cmd_List_f (cmd_state_t *cmd)
1489 cmd_function_t *func;
1490 const char *partial;
1495 if (Cmd_Argc(cmd) > 1)
1497 partial = Cmd_Argv(cmd, 1);
1498 len = strlen(partial);
1499 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1509 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1511 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1513 Con_Printf("%s : %s\n", func->name, func->description);
1516 for (func = cmd->engine_functions; func; func = func->next)
1518 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1520 Con_Printf("%s : %s\n", func->name, func->description);
1527 Con_Printf("%i Command%s matching \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1529 Con_Printf("%i Command%s beginning with \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1532 Con_Printf("%i Command%s\n\n", count, (count > 1) ? "s" : "");
1535 static void Cmd_Apropos_f(cmd_state_t *cmd)
1537 cmd_function_t *func;
1540 const char *partial;
1545 if (Cmd_Argc(cmd) > 1)
1546 partial = Cmd_Args(cmd);
1549 Con_Printf("usage: %s <string>\n",Cmd_Argv(cmd, 0));
1553 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
1555 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
1558 for (cvar = cmd->cvars->vars; cvar; cvar = cvar->next)
1560 if (matchpattern_with_separator(cvar->name, partial, true, "", false) ||
1561 matchpattern_with_separator(cvar->description, partial, true, "", false))
1563 Con_Printf ("cvar ");
1564 Cvar_PrintHelp(cvar, cvar->name, true);
1567 for (int i = 0; i < cvar->aliasindex; i++)
1569 if (matchpattern_with_separator(cvar->aliases[i], partial, true, "", false))
1571 Con_Printf ("cvar ");
1572 Cvar_PrintHelp(cvar, cvar->aliases[i], true);
1577 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1579 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1580 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1582 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1585 for (func = cmd->engine_functions; func; func = func->next)
1587 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1588 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1590 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1593 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
1595 // procede here a bit differently as an alias value always got a final \n
1596 if (!matchpattern_with_separator(alias->name, partial, true, "", false))
1597 if (!matchpattern_with_separator(alias->value, partial, true, "\n", false)) // when \n is as separator wildcards don't match it
1599 Con_Printf("alias ^5%s^7: %s", alias->name, alias->value); // do not print an extra \n
1602 Con_Printf("%i result%s\n\n", count, (count > 1) ? "s" : "");
1612 cmd_iter_t *cmd_iter;
1614 cbuf_mempool = Mem_AllocPool("Command buffer", 0, NULL);
1615 cbuf = (cmd_buf_t *)Mem_Alloc(cbuf_mempool, sizeof(cmd_buf_t));
1616 cbuf->maxsize = 655360;
1617 cbuf->lock = Thread_CreateMutex();
1621 cbuf->start.prev = cbuf->start.next = &(cbuf->start);
1622 cbuf->deferred.prev = cbuf->deferred.next = &(cbuf->deferred);
1623 cbuf->free.prev = cbuf->free.next = &(cbuf->free);
1625 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
1627 cmd_state_t *cmd = cmd_iter->cmd;
1628 cmd->mempool = Mem_AllocPool("commands", 0, NULL);
1629 // space for commands and script files
1631 cmd->null_string = "";
1633 // client console can see server cvars because the user may start a server
1634 cmd_client.cvars = &cvars_all;
1635 cmd_client.cvars_flagsmask = CF_CLIENT | CF_SERVER;
1636 cmd_client.cmd_flags = CF_CLIENT | CF_CLIENT_FROM_SERVER;
1637 cmd_client.auto_flags = CF_SERVER_FROM_CLIENT;
1638 cmd_client.auto_function = CL_ForwardToServer_f; // FIXME: Move this to the client.
1639 cmd_client.userdefined = &cmd_userdefined_all;
1640 // dedicated server console can only see server cvars, there is no client
1641 cmd_server.cvars = &cvars_all;
1642 cmd_server.cvars_flagsmask = CF_SERVER;
1643 cmd_server.cmd_flags = CF_SERVER;
1644 cmd_server.auto_flags = 0;
1645 cmd_server.auto_function = NULL;
1646 cmd_server.userdefined = &cmd_userdefined_all;
1647 // server commands received from clients have no reason to access cvars, cvar expansion seems perilous.
1648 cmd_serverfromclient.cvars = &cvars_null;
1649 cmd_serverfromclient.cvars_flagsmask = 0;
1650 cmd_serverfromclient.cmd_flags = CF_SERVER_FROM_CLIENT | CF_USERINFO;
1651 cmd_serverfromclient.auto_flags = 0;
1652 cmd_serverfromclient.auto_function = NULL;
1653 cmd_serverfromclient.userdefined = &cmd_userdefined_null;
1656 // register our commands
1658 // client-only commands
1659 Cmd_AddCommand(CF_SHARED, "wait", Cmd_Wait_f, "make script execution wait for next rendered frame");
1660 Cmd_AddCommand(CF_CLIENT, "cprint", Cmd_Centerprint_f, "print something at the screen center");
1662 // maintenance commands used for upkeep of cvars and saved configs
1663 Cmd_AddCommand(CF_SHARED, "stuffcmds", Cmd_StuffCmds_f, "execute commandline parameters (must be present in quake.rc script)");
1664 Cmd_AddCommand(CF_SHARED, "cvar_lockdefaults", Cvar_LockDefaults_f, "stores the current values of all cvars into their default values, only used once during startup after parsing default.cfg");
1665 Cmd_AddCommand(CF_SHARED, "cvar_resettodefaults_all", Cvar_ResetToDefaults_All_f, "sets all cvars to their locked default values");
1666 Cmd_AddCommand(CF_SHARED, "cvar_resettodefaults_nosaveonly", Cvar_ResetToDefaults_NoSaveOnly_f, "sets all non-saved cvars to their locked default values (variables that will not be saved to config.cfg)");
1667 Cmd_AddCommand(CF_SHARED, "cvar_resettodefaults_saveonly", Cvar_ResetToDefaults_SaveOnly_f, "sets all saved cvars to their locked default values (variables that will be saved to config.cfg)");
1669 // general console commands used in multiple environments
1670 Cmd_AddCommand(CF_SHARED, "exec", Cmd_Exec_f, "execute a script file");
1671 Cmd_AddCommand(CF_SHARED, "echo",Cmd_Echo_f, "print a message to the console (useful in scripts)");
1672 Cmd_AddCommand(CF_SHARED, "alias",Cmd_Alias_f, "create a script function (parameters are passed in as $X (being X a number), $* for all parameters, $X- for all parameters starting from $X). Without arguments show the list of all alias");
1673 Cmd_AddCommand(CF_SHARED, "unalias",Cmd_UnAlias_f, "remove an alias");
1674 Cmd_AddCommand(CF_SHARED, "set", Cvar_Set_f, "create or change the value of a console variable");
1675 Cmd_AddCommand(CF_SHARED, "seta", Cvar_SetA_f, "create or change the value of a console variable that will be saved to config.cfg");
1676 Cmd_AddCommand(CF_SHARED, "unset", Cvar_Del_f, "delete a cvar (does not work for static ones like _cl_name, or read-only ones)");
1678 #ifdef FILLALLCVARSWITHRUBBISH
1679 Cmd_AddCommand(CF_SHARED, "fillallcvarswithrubbish", Cvar_FillAll_f, "fill all cvars with a specified number of characters to provoke buffer overruns");
1680 #endif /* FILLALLCVARSWITHRUBBISH */
1682 // 2000-01-09 CmdList, CvarList commands By Matthias "Maddes" Buecher
1683 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
1684 Cmd_AddCommand(CF_SHARED, "cmdlist", Cmd_List_f, "lists all console commands beginning with the specified prefix or matching the specified wildcard pattern");
1685 Cmd_AddCommand(CF_SHARED, "cvarlist", Cvar_List_f, "lists all console variables beginning with the specified prefix or matching the specified wildcard pattern");
1686 Cmd_AddCommand(CF_SHARED, "apropos", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1687 Cmd_AddCommand(CF_SHARED, "find", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1689 Cmd_AddCommand(CF_SHARED, "defer", Cmd_Defer_f, "execute a command in the future");
1692 // Support Doom3-style Toggle Command
1693 Cmd_AddCommand(CF_SHARED | CF_CLIENT_FROM_SERVER, "toggle", Cmd_Toggle_f, "toggles a console variable's values (use for more info)");
1701 void Cmd_Shutdown(void)
1703 cmd_iter_t *cmd_iter;
1704 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
1706 cmd_state_t *cmd = cmd_iter->cmd;
1708 if (cmd->cbuf->lock)
1710 // we usually have this locked when we get here from Host_Quit_f
1711 Cbuf_Unlock(cmd->cbuf);
1714 Mem_FreePool(&cmd->mempool);
1723 int Cmd_Argc (cmd_state_t *cmd)
1733 const char *Cmd_Argv(cmd_state_t *cmd, int arg)
1735 if (arg >= cmd->argc )
1736 return cmd->null_string;
1737 return cmd->argv[arg];
1745 const char *Cmd_Args (cmd_state_t *cmd)
1754 Parses the given string into command line tokens.
1757 // AK: This function should only be called from ExcuteString because the current design is a bit of an hack
1758 static void Cmd_TokenizeString (cmd_state_t *cmd, const char *text)
1767 // skip whitespace up to a /n
1768 while (*text && ISWHITESPACE(*text) && *text != '\r' && *text != '\n')
1775 if (*text == '\n' || *text == '\r')
1777 // a newline separates commands in the buffer
1778 if (*text == '\r' && text[1] == '\n')
1790 if (!COM_ParseToken_Console(&text))
1793 if (cmd->argc < MAX_ARGS)
1795 l = (int)strlen(com_token) + 1;
1796 if (cmd->cbuf->tokenizebufferpos + l > CMD_TOKENIZELENGTH)
1798 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguments\n", CMD_TOKENIZELENGTH);
1801 memcpy (cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos, com_token, l);
1802 cmd->argv[cmd->argc] = cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos;
1803 cmd->cbuf->tokenizebufferpos += l;
1815 void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const char *description)
1817 cmd_function_t *func;
1818 cmd_function_t *prev, *current;
1820 xcommand_t save = NULL;
1821 qbool auto_add = false;
1824 for (i = 0; i < 3; i++)
1826 cmd = cmd_iter_all[i].cmd;
1827 if ((flags & cmd->cmd_flags) || (flags & cmd->auto_flags))
1829 if((flags & cmd->auto_flags) && cmd->auto_function)
1832 function = cmd->auto_function;
1836 // fail if the command is a variable name
1837 if (Cvar_FindVar(cmd->cvars, cmd_name, ~0))
1839 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
1845 // fail if the command already exists in this interpreter
1846 for (func = cmd->engine_functions; func; func = func->next)
1848 if (!strcmp(cmd_name, func->name))
1850 if(func->autofunc && !auto_add)
1852 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
1857 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1858 func->flags = flags;
1859 func->name = cmd_name;
1860 func->function = function;
1861 func->description = description;
1862 func->next = cmd->engine_functions;
1863 func->autofunc = auto_add;
1865 // insert it at the right alphanumeric position
1866 for (prev = NULL, current = cmd->engine_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1872 cmd->engine_functions = func;
1874 func->next = current;
1878 // mark qcfunc if the function already exists in the qc_functions list
1879 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1881 if (!strcmp(cmd_name, func->name))
1883 func->qcfunc = true; //[515]: csqc
1889 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1890 func->flags = flags;
1891 func->name = cmd_name;
1892 func->function = function;
1893 func->description = description;
1894 func->qcfunc = true; //[515]: csqc
1895 func->next = cmd->userdefined->qc_functions;
1896 func->autofunc = false;
1898 // insert it at the right alphanumeric position
1899 for (prev = NULL, current = cmd->userdefined->qc_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1905 cmd->userdefined->qc_functions = func;
1907 func->next = current;
1923 qbool Cmd_Exists (cmd_state_t *cmd, const char *cmd_name)
1925 cmd_function_t *func;
1927 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1928 if (!strcmp(cmd_name, func->name))
1931 for (func=cmd->engine_functions ; func ; func=func->next)
1932 if (!strcmp (cmd_name,func->name))
1944 const char *Cmd_CompleteCommand (cmd_state_t *cmd, const char *partial)
1946 cmd_function_t *func;
1949 len = strlen(partial);
1955 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1956 if (!strncasecmp(partial, func->name, len))
1959 for (func = cmd->engine_functions; func; func = func->next)
1960 if (!strncasecmp(partial, func->name, len))
1967 Cmd_CompleteCountPossible
1969 New function for tab-completion system
1970 Added by EvilTypeGuy
1971 Thanks to Fett erich@heintz.com
1975 int Cmd_CompleteCountPossible (cmd_state_t *cmd, const char *partial)
1977 cmd_function_t *func;
1982 len = strlen(partial);
1987 // Loop through the command list and count all partial matches
1988 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1989 if (!strncasecmp(partial, func->name, len))
1992 for (func = cmd->engine_functions; func; func = func->next)
1993 if (!strncasecmp(partial, func->name, len))
2000 Cmd_CompleteBuildList
2002 New function for tab-completion system
2003 Added by EvilTypeGuy
2004 Thanks to Fett erich@heintz.com
2008 const char **Cmd_CompleteBuildList (cmd_state_t *cmd, const char *partial)
2010 cmd_function_t *func;
2013 size_t sizeofbuf = (Cmd_CompleteCountPossible (cmd, partial) + 1) * sizeof (const char *);
2016 len = strlen(partial);
2017 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
2018 // Loop through the functions lists and print all matches
2019 for (func = cmd->userdefined->qc_functions; func; func = func->next)
2020 if (!strncasecmp(partial, func->name, len))
2021 buf[bpos++] = func->name;
2022 for (func = cmd->engine_functions; func; func = func->next)
2023 if (!strncasecmp(partial, func->name, len))
2024 buf[bpos++] = func->name;
2030 // written by LadyHavoc
2031 void Cmd_CompleteCommandPrint (cmd_state_t *cmd, const char *partial)
2033 cmd_function_t *func;
2034 size_t len = strlen(partial);
2035 // Loop through the command list and print all matches
2036 for (func = cmd->userdefined->qc_functions; func; func = func->next)
2037 if (!strncasecmp(partial, func->name, len))
2038 Con_Printf("^2%s^7: %s\n", func->name, func->description);
2039 for (func = cmd->engine_functions; func; func = func->next)
2040 if (!strncasecmp(partial, func->name, len))
2041 Con_Printf("^2%s^7: %s\n", func->name, func->description);
2047 New function for tab-completion system
2048 Added by EvilTypeGuy
2049 Thanks to Fett erich@heintz.com
2053 const char *Cmd_CompleteAlias (cmd_state_t *cmd, const char *partial)
2058 len = strlen(partial);
2064 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2065 if (!strncasecmp(partial, alias->name, len))
2071 // written by LadyHavoc
2072 void Cmd_CompleteAliasPrint (cmd_state_t *cmd, const char *partial)
2075 size_t len = strlen(partial);
2076 // Loop through the alias list and print all matches
2077 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2078 if (!strncasecmp(partial, alias->name, len))
2079 Con_Printf("^5%s^7: %s", alias->name, alias->value);
2084 Cmd_CompleteAliasCountPossible
2086 New function for tab-completion system
2087 Added by EvilTypeGuy
2088 Thanks to Fett erich@heintz.com
2092 int Cmd_CompleteAliasCountPossible (cmd_state_t *cmd, const char *partial)
2100 len = strlen(partial);
2105 // Loop through the command list and count all partial matches
2106 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2107 if (!strncasecmp(partial, alias->name, len))
2114 Cmd_CompleteAliasBuildList
2116 New function for tab-completion system
2117 Added by EvilTypeGuy
2118 Thanks to Fett erich@heintz.com
2122 const char **Cmd_CompleteAliasBuildList (cmd_state_t *cmd, const char *partial)
2127 size_t sizeofbuf = (Cmd_CompleteAliasCountPossible (cmd, partial) + 1) * sizeof (const char *);
2130 len = strlen(partial);
2131 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
2132 // Loop through the alias list and print all matches
2133 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
2134 if (!strncasecmp(partial, alias->name, len))
2135 buf[bpos++] = alias->name;
2141 // TODO: Make this more generic?
2142 void Cmd_ClearCSQCCommands (cmd_state_t *cmd)
2144 cmd_function_t *func;
2145 cmd_function_t **next = &cmd->userdefined->qc_functions;
2155 extern cvar_t sv_cheats;
2161 A complete command line has been parsed, so try to execute it
2162 FIXME: lookupnoadd the token to speed search?
2165 void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qbool lockmutex)
2168 cmd_function_t *func;
2171 Cbuf_Lock(cmd->cbuf);
2172 oldpos = cmd->cbuf->tokenizebufferpos;
2175 Cmd_TokenizeString (cmd, text);
2177 // execute the command line
2179 goto done; // no tokens
2182 for (func = cmd->userdefined->qc_functions; func; func = func->next)
2184 if (!strcasecmp(cmd->argv[0], func->name))
2188 if((func->flags & CF_CLIENT) && CL_VM_ConsoleCommand(text))
2190 else if((func->flags & CF_SERVER) && SV_VM_ConsoleCommand(text))
2196 for (func = cmd->engine_functions; func; func=func->next)
2198 if (!strcasecmp (cmd->argv[0], func->name))
2204 func->function(cmd);
2206 Con_Printf("Command \"%s\" can not be executed\n", Cmd_Argv(cmd, 0));
2211 if((func->flags & CF_CHEAT) && !sv_cheats.integer)
2212 SV_ClientPrintf("No cheats allowed. The server must have sv_cheats set to 1\n");
2214 func->function(cmd);
2222 // if it's a client command and no command was found, say so.
2223 if (cmd->source == src_client)
2225 Con_Printf("Client \"%s\" tried to execute \"%s\"\n", host_client->name, text);
2230 for (a=cmd->userdefined->alias ; a ; a=a->next)
2232 if (!strcasecmp (cmd->argv[0], a->name))
2234 Cmd_ExecuteAlias(cmd, a);
2240 if (!Cvar_Command(cmd) && host.framecount > 0)
2241 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(cmd, 0));
2243 cmd->cbuf->tokenizebufferpos = oldpos;
2245 Cbuf_Unlock(cmd->cbuf);
2252 Returns the position (1 to argc-1) in the command's argument list
2253 where the given parameter apears, or 0 if not present
2257 int Cmd_CheckParm (cmd_state_t *cmd, const char *parm)
2263 Con_Printf ("Cmd_CheckParm: NULL");
2267 for (i = 1; i < Cmd_Argc (cmd); i++)
2268 if (!strcasecmp (parm, Cmd_Argv(cmd, i)))
2276 void Cmd_SaveInitState(void)
2278 cmd_iter_t *cmd_iter;
2279 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2281 cmd_state_t *cmd = cmd_iter->cmd;
2284 for (f = cmd->userdefined->qc_functions; f; f = f->next)
2285 f->initstate = true;
2286 for (f = cmd->engine_functions; f; f = f->next)
2287 f->initstate = true;
2288 for (a = cmd->userdefined->alias; a; a = a->next)
2290 a->initstate = true;
2291 a->initialvalue = Mem_strdup(zonemempool, a->value);
2294 Cvar_SaveInitState(&cvars_all);
2297 void Cmd_RestoreInitState(void)
2299 cmd_iter_t *cmd_iter;
2300 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2302 cmd_state_t *cmd = cmd_iter->cmd;
2303 cmd_function_t *f, **fp;
2304 cmd_alias_t *a, **ap;
2305 for (fp = &cmd->userdefined->qc_functions; (f = *fp);)
2311 // destroy this command, it didn't exist at init
2312 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2317 for (fp = &cmd->engine_functions; (f = *fp);)
2323 // destroy this command, it didn't exist at init
2324 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2329 for (ap = &cmd->userdefined->alias; (a = *ap);)
2333 // restore this alias, it existed at init
2334 if (strcmp(a->value ? a->value : "", a->initialvalue ? a->initialvalue : ""))
2336 Con_DPrintf("Cmd_RestoreInitState: Restoring alias %s\n", a->name);
2339 a->value = Mem_strdup(zonemempool, a->initialvalue);
2345 // free this alias, it didn't exist at init...
2346 Con_DPrintf("Cmd_RestoreInitState: Destroying alias %s\n", a->name);
2354 Cvar_RestoreInitState(&cvars_all);