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_local;
26 cmd_state_t *cmd_serverfromclient;
28 cmd_userdefined_t cmd_userdefined_all;
29 cmd_userdefined_t cmd_userdefined_null;
31 typedef struct cmd_iter_s {
36 static cmd_iter_t *cmd_iter_all;
38 mempool_t *cbuf_mempool;
40 // we only run the +whatever commandline arguments once
41 qbool host_stuffcmdsrun = false;
43 //=============================================================================
45 void Cbuf_Lock(cmd_buf_t *cbuf)
47 Thread_LockMutex(cbuf->lock);
50 void Cbuf_Unlock(cmd_buf_t *cbuf)
52 Thread_UnlockMutex(cbuf->lock);
60 Causes execution of the remainder of the command buffer to be delayed until
61 next frame. This allows commands like:
62 bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
65 static void Cmd_Wait_f (cmd_state_t *cmd)
67 cmd->cbuf->wait = true;
74 Cause a command to be executed after a delay.
77 static void Cbuf_ParseText(cmd_state_t *cmd, llist_t *head, cmd_input_t *existing, const char *text, qbool allowpending);
78 static void Cbuf_LinkString(cmd_state_t *cmd, llist_t *head, cmd_input_t *existing, const char *text, qbool leavepending, unsigned int cmdsize);
79 static void Cmd_Defer_f (cmd_state_t *cmd)
82 cmd_buf_t *cbuf = cmd->cbuf;
85 if(Cmd_Argc(cmd) == 1)
87 if(List_Is_Empty(&cbuf->deferred))
88 Con_Printf("No commands are pending.\n");
91 List_For_Each_Entry(current, &cbuf->deferred, cmd_input_t, list)
92 Con_Printf("-> In %9.2f: %s\n", current->delay, current->text);
95 else if(Cmd_Argc(cmd) == 2 && !strcasecmp("clear", Cmd_Argv(cmd, 1)))
97 while(!List_Is_Empty(&cbuf->deferred))
99 cbuf->size -= List_Entry(cbuf->deferred.next, cmd_input_t, list)->length;
100 List_Move_Tail(cbuf->deferred.next, &cbuf->free);
103 else if(Cmd_Argc(cmd) == 3 && (cmdsize = strlen(Cmd_Argv(cmd, 2))) )
107 Cbuf_LinkString(cmd, &cbuf->deferred, NULL, Cmd_Argv(cmd, 2), false, cmdsize);
108 List_Entry(cbuf->deferred.prev, cmd_input_t, list)->delay = atof(Cmd_Argv(cmd, 1));
114 Con_Printf("usage: defer <seconds> <command>\n"
124 Print something to the center of the screen using SCR_Centerprint
127 static void Cmd_Centerprint_f (cmd_state_t *cmd)
129 char msg[MAX_INPUTLINE];
130 unsigned int i, c, p;
134 strlcpy(msg, Cmd_Argv(cmd,1), sizeof(msg));
135 for(i = 2; i < c; ++i)
137 strlcat(msg, " ", sizeof(msg));
138 strlcat(msg, Cmd_Argv(cmd, i), sizeof(msg));
140 c = (unsigned int)strlen(msg);
141 for(p = 0, i = 0; i < c; ++i)
147 else if(msg[i+1] == '\\')
159 SCR_CenterPrint(msg);
164 =============================================================================
168 * The Quake command-line is super basic. It can be entered in the console
169 * or in config files. A semicolon is used to terminate a command and chain
170 * them together. Otherwise, a newline delineates command input.
172 * In most engines, the Quake command-line is a simple linear text buffer that
173 * is parsed when it executes. In Darkplaces, we use a linked list of command
174 * input and parse the input on the spot.
176 * This was done because Darkplaces allows multiple command interpreters on the
177 * same thread. Previously, each interpreter maintained its own buffer and this
178 * caused problems related to execution order, and maintaining a single simple
179 * buffer for all interpreters makes it non-trivial to keep track of which
180 * command should execute on which interpreter.
182 =============================================================================
189 Returns an existing buffer node for appending or reuse, or allocates a new one
192 static cmd_input_t *Cbuf_NodeGet(cmd_buf_t *cbuf, cmd_input_t *existing)
195 if(existing && existing->pending)
197 else if(!List_Is_Empty(&cbuf->free))
199 node = List_Entry(cbuf->free.next, cmd_input_t, list);
200 node->length = node->pending = 0;
204 node = (cmd_input_t *)Mem_Alloc(cbuf_mempool, sizeof(cmd_input_t));
205 node->list.prev = node->list.next = &node->list;
206 node->size = node->length = node->pending = 0;
215 Copies a command string into a buffer node
218 static void Cbuf_LinkString(cmd_state_t *cmd, llist_t *head, cmd_input_t *existing, const char *text, qbool leavepending, unsigned int cmdsize)
220 cmd_buf_t *cbuf = cmd->cbuf;
221 cmd_input_t *node = Cbuf_NodeGet(cbuf, existing);
222 unsigned int offset = node->length; // > 0 if(pending)
224 // node will match existing if its text was pending continuation
228 List_Move_Tail(&node->list, head);
231 node->length += cmdsize;
232 if(node->size < node->length)
234 node->text = (char *)Mem_Realloc(cbuf_mempool, node->text, node->length + 1);
235 node->size = node->length;
237 cbuf->size += cmdsize;
239 strlcpy(&node->text[offset], text, cmdsize + 1); // always sets the last char to \0
240 //Con_Printf("^5Cbuf_LinkString(): %s `^7%s^5`\n", node->pending ? "append" : "new", &node->text[offset]);
241 node->pending = leavepending;
248 Parses text to isolate command strings for linking into the buffer
249 separators: \n \r or unquoted and uncommented ';'
252 static void Cbuf_ParseText(cmd_state_t *cmd, llist_t *head, cmd_input_t *existing, const char *text, qbool allowpending)
254 unsigned int cmdsize = 0, start = 0, pos;
255 qbool quotes = false, comment = false;
257 for (pos = 0; text[pos]; ++pos)
262 if (comment || quotes)
267 quotes = false; // matches div0-stable
270 Cbuf_LinkString(cmd, head, existing, &text[start], false, cmdsize);
273 else if (existing && existing->pending) // all I got was this lousy \n
274 existing->pending = false;
275 continue; // don't increment cmdsize
278 if (!quotes && text[pos + 1] == '/' && (pos == 0 || ISWHITESPACE(text[pos - 1])))
282 if (!comment && (pos == 0 || text[pos - 1] != '\\'))
295 if (cmdsize) // the line didn't end yet but we do have a string
296 Cbuf_LinkString(cmd, head, existing, &text[start], allowpending, cmdsize);
303 Adds command text at the end of the buffer
306 void Cbuf_AddText (cmd_state_t *cmd, const char *text)
308 size_t l = strlen(text);
309 cmd_buf_t *cbuf = cmd->cbuf;
310 llist_t llist = {&llist, &llist};
314 if (cbuf->maxsize - cbuf->size <= l)
315 Con_Print("Cbuf_AddText: overflow\n");
318 // If the string terminates but the (last) line doesn't, the node will be left in the pending state (to be continued).
319 Cbuf_ParseText(cmd, &llist, (List_Is_Empty(&cbuf->start) ? NULL : List_Entry(cbuf->start.prev, cmd_input_t, list)), text, true);
320 List_Splice_Tail(&llist, &cbuf->start);
329 Adds command text immediately after the current command
332 void Cbuf_InsertText (cmd_state_t *cmd, const char *text)
334 cmd_buf_t *cbuf = cmd->cbuf;
335 llist_t llist = {&llist, &llist};
336 size_t l = strlen(text);
340 if (cbuf->size + l >= cbuf->maxsize)
341 Con_Print("Cbuf_InsertText: overflow\n");
344 // bones_was_here assertion: when prepending to the buffer it never makes sense to leave node(s) in the `pending` state,
345 // it would have been impossible to append to such text later in the old raw text buffer,
346 // and allowing it causes bugs when .cfg files lack \n at EOF (see: https://gitlab.com/xonotic/darkplaces/-/issues/378).
347 Cbuf_ParseText(cmd, &llist, (List_Is_Empty(&cbuf->start) ? NULL : List_Entry(cbuf->start.next, cmd_input_t, list)), text, false);
348 List_Splice(&llist, &cbuf->start);
356 Cbuf_Execute_Deferred --blub
359 static void Cbuf_Execute_Deferred (cmd_buf_t *cbuf)
361 cmd_input_t *current, *n;
364 if (host.realtime - cbuf->deferred_oldtime < 0 || host.realtime - cbuf->deferred_oldtime > 1800)
365 cbuf->deferred_oldtime = host.realtime;
366 eat = host.realtime - cbuf->deferred_oldtime;
369 cbuf->deferred_oldtime = host.realtime;
371 List_For_Each_Entry_Safe(current, n, &cbuf->deferred, cmd_input_t, list)
373 current->delay -= eat;
374 if(current->delay <= 0)
376 Cbuf_AddText(current->source, current->text); // parse deferred string and append its cmdstring(s)
377 List_Entry(cbuf->start.prev, cmd_input_t, list)->pending = false; // faster than div0-stable's Cbuf_AddText(";\n");
378 List_Move_Tail(¤t->list, &cbuf->free); // make deferred string memory available for reuse
379 cbuf->size -= current->length;
389 extern qbool prvm_runawaycheck;
390 static qbool Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmd_alias_t *alias );
391 void Cbuf_Execute (cmd_buf_t *cbuf)
393 cmd_input_t *current;
394 char preprocessed[MAX_INPUTLINE];
398 // LadyHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
399 cbuf->tokenizebufferpos = 0;
401 while (!List_Is_Empty(&cbuf->start))
404 * Delete the text from the command buffer and move remaining
405 * commands down. This is necessary because commands (exec, alias)
406 * can insert data at the beginning of the text buffer
408 current = List_Entry(cbuf->start.next, cmd_input_t, list);
411 * Assume we're rolling with the current command-line and
412 * always set this false because alias expansion or cbuf insertion
413 * without a newline may set this true, and cause weirdness.
415 current->pending = false;
417 cbuf->size -= current->length;
419 firstchar = current->text;
420 while(*firstchar && ISWHITESPACE(*firstchar))
422 if((strncmp(firstchar, "alias", 5) || !ISWHITESPACE(firstchar[5])) &&
423 (strncmp(firstchar, "bind", 4) || !ISWHITESPACE(firstchar[4])) &&
424 (strncmp(firstchar, "in_bind", 7) || !ISWHITESPACE(firstchar[7])))
426 if(Cmd_PreprocessString(current->source, current->text, preprocessed, sizeof(preprocessed), NULL ))
427 Cmd_ExecuteString(current->source, preprocessed, src_local, false);
431 Cmd_ExecuteString (current->source, current->text, src_local, false);
434 // Recycle memory so using WASD doesn't cause a malloc and free
435 List_Move_Tail(¤t->list, &cbuf->free);
442 * Skip out while text still remains in
443 * buffer, leaving it for next frame
449 if (++i == 1000000 && prvm_runawaycheck)
451 Con_Printf(CON_WARN "Cbuf_Execute: runaway loop counter hit limit of %d commands, clearing command buffers!\n", i);
461 Add them exactly as if they had been typed at the console
464 static void Cbuf_Frame_Input(void)
468 while ((line = Sys_ConsoleInput()))
469 Cbuf_AddText(cmd_local, line);
472 void Cbuf_Frame(cmd_buf_t *cbuf)
474 // check for commands typed to the host
477 // R_TimeReport("preconsole");
479 // execute commands queued with the defer command
480 Cbuf_Execute_Deferred(cbuf);
483 SV_LockThreadMutex();
485 SV_UnlockThreadMutex();
488 // R_TimeReport("console");
491 void Cbuf_Clear(cmd_buf_t *cbuf)
493 while (!List_Is_Empty(&cbuf->start))
494 List_Move_Tail(cbuf->start.next, &cbuf->free);
495 while (!List_Is_Empty(&cbuf->deferred))
496 List_Move_Tail(cbuf->deferred.next, &cbuf->free);
501 ==============================================================================
505 ==============================================================================
512 Adds command line parameters as script statements
513 Commands lead with a +, and continue until a - or another +
514 quake +prog jctest.qp +cmd amlev1
515 quake -nosound +cmd amlev1
518 static void Cmd_StuffCmds_f (cmd_state_t *cmd)
521 // this is for all commandline options combined (and is bounds checked)
522 char build[MAX_INPUTLINE];
524 if (Cmd_Argc (cmd) != 1)
526 Con_Print("stuffcmds : execute command line parameters\n");
530 // no reason to run the commandline arguments twice
531 if (host_stuffcmdsrun)
534 host_stuffcmdsrun = true;
537 for (i = 0;i < sys.argc;i++)
539 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)
542 while (sys.argv[i][j])
543 build[l++] = sys.argv[i][j++];
545 for (;i < sys.argc;i++)
549 if ((sys.argv[i][0] == '+' || sys.argv[i][0] == '-') && (sys.argv[i][1] < '0' || sys.argv[i][1] > '9'))
551 if (l + strlen(sys.argv[i]) + 4 > sizeof(build) - 1)
554 if (strchr(sys.argv[i], ' '))
556 for (j = 0;sys.argv[i][j];j++)
557 build[l++] = sys.argv[i][j];
558 if (strchr(sys.argv[i], ' '))
565 // now terminate the combined string and prepend it to the command buffer
566 // we already reserved space for the terminator
568 Cbuf_InsertText (cmd, build);
571 static void Cmd_Exec(cmd_state_t *cmd, const char *filename)
574 size_t filenameLen = strlen(filename);
576 !strcmp(filename, "default.cfg") ||
577 (filenameLen >= 12 && !strcmp(filename + filenameLen - 12, "/default.cfg"));
579 if (!strcmp(filename, "config.cfg"))
581 filename = CONFIGFILENAME;
582 if (Sys_CheckParm("-noconfig"))
583 return; // don't execute config.cfg
586 f = (char *)FS_LoadFile (filename, tempmempool, false, NULL);
589 Con_Printf("couldn't exec %s\n",filename);
592 Con_Printf("execing %s\n",filename);
594 // if executing default.cfg for the first time, lock the cvar defaults
595 // it may seem backwards to insert this text BEFORE the default.cfg
596 // but Cbuf_InsertText inserts before, so this actually ends up after it.
598 Cbuf_InsertText(cmd, "\ncvar_lockdefaults\n");
600 Cbuf_InsertText (cmd, f);
605 // special defaults for specific games go here, these execute before default.cfg
606 // Nehahra pushable crates malfunction in some levels if this is on
607 // Nehahra NPC AI is confused by blowupfallenzombies
611 Cbuf_InsertText(cmd, "\n"
612 "sv_gameplayfix_blowupfallenzombies 0\n"
613 "sv_gameplayfix_findradiusdistancetobox 0\n"
614 "sv_gameplayfix_grenadebouncedownslopes 0\n"
615 "sv_gameplayfix_slidemoveprojectiles 0\n"
616 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
617 "sv_gameplayfix_setmodelrealbox 0\n"
618 "sv_gameplayfix_droptofloorstartsolid 0\n"
619 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
620 "sv_gameplayfix_noairborncorpse 0\n"
621 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
622 "sv_gameplayfix_easierwaterjump 0\n"
623 "sv_gameplayfix_delayprojectiles 0\n"
624 "sv_gameplayfix_multiplethinksperframe 0\n"
625 "sv_gameplayfix_fixedcheckwatertransition 0\n"
626 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
627 "sv_gameplayfix_swiminbmodels 0\n"
628 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
629 "sys_ticrate 0.01388889\n"
631 "r_shadow_bumpscale_basetexture 0\n"
632 "csqc_polygons_defaultmaterial_nocullface 0\n"
636 Cbuf_InsertText(cmd, "\n"
637 "sv_gameplayfix_blowupfallenzombies 0\n"
638 "sv_gameplayfix_findradiusdistancetobox 0\n"
639 "sv_gameplayfix_grenadebouncedownslopes 0\n"
640 "sv_gameplayfix_slidemoveprojectiles 0\n"
641 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
642 "sv_gameplayfix_setmodelrealbox 0\n"
643 "sv_gameplayfix_droptofloorstartsolid 0\n"
644 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
645 "sv_gameplayfix_noairborncorpse 0\n"
646 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
647 "sv_gameplayfix_easierwaterjump 0\n"
648 "sv_gameplayfix_delayprojectiles 0\n"
649 "sv_gameplayfix_multiplethinksperframe 0\n"
650 "sv_gameplayfix_fixedcheckwatertransition 0\n"
651 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
652 "sv_gameplayfix_swiminbmodels 0\n"
653 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
654 "sys_ticrate 0.01388889\n"
656 "r_shadow_bumpscale_basetexture 0\n"
657 "csqc_polygons_defaultmaterial_nocullface 0\n"
660 // 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.
661 // 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
662 // hipnotic mission pack has issues in their proximity mine sticking code, which causes them to bounce off.
665 Cbuf_InsertText(cmd, "\n"
666 "sv_gameplayfix_blowupfallenzombies 0\n"
667 "sv_gameplayfix_findradiusdistancetobox 0\n"
668 "sv_gameplayfix_grenadebouncedownslopes 0\n"
669 "sv_gameplayfix_slidemoveprojectiles 0\n"
670 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
671 "sv_gameplayfix_setmodelrealbox 0\n"
672 "sv_gameplayfix_droptofloorstartsolid 0\n"
673 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
674 "sv_gameplayfix_noairborncorpse 0\n"
675 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
676 "sv_gameplayfix_easierwaterjump 0\n"
677 "sv_gameplayfix_delayprojectiles 0\n"
678 "sv_gameplayfix_multiplethinksperframe 0\n"
679 "sv_gameplayfix_fixedcheckwatertransition 0\n"
680 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
681 "sv_gameplayfix_swiminbmodels 0\n"
682 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
685 "r_shadow_bumpscale_basetexture 0\n"
686 "csqc_polygons_defaultmaterial_nocullface 0\n"
689 // rogue mission pack has a guardian boss that does not wake up if findradius returns one of the entities around its spawn area
691 Cbuf_InsertText(cmd, "\n"
692 "sv_gameplayfix_blowupfallenzombies 0\n"
693 "sv_gameplayfix_findradiusdistancetobox 0\n"
694 "sv_gameplayfix_grenadebouncedownslopes 0\n"
695 "sv_gameplayfix_slidemoveprojectiles 0\n"
696 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
697 "sv_gameplayfix_setmodelrealbox 0\n"
698 "sv_gameplayfix_droptofloorstartsolid 0\n"
699 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
700 "sv_gameplayfix_noairborncorpse 0\n"
701 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
702 "sv_gameplayfix_easierwaterjump 0\n"
703 "sv_gameplayfix_delayprojectiles 0\n"
704 "sv_gameplayfix_multiplethinksperframe 0\n"
705 "sv_gameplayfix_fixedcheckwatertransition 0\n"
706 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
707 "sv_gameplayfix_swiminbmodels 0\n"
708 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
709 "sys_ticrate 0.01388889\n"
711 "r_shadow_bumpscale_basetexture 0\n"
712 "csqc_polygons_defaultmaterial_nocullface 0\n"
716 Cbuf_InsertText(cmd, "\n"
717 "sv_gameplayfix_blowupfallenzombies 0\n"
718 "sv_gameplayfix_findradiusdistancetobox 0\n"
719 "sv_gameplayfix_grenadebouncedownslopes 0\n"
720 "sv_gameplayfix_slidemoveprojectiles 0\n"
721 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
722 "sv_gameplayfix_setmodelrealbox 0\n"
723 "sv_gameplayfix_droptofloorstartsolid 0\n"
724 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
725 "sv_gameplayfix_noairborncorpse 0\n"
726 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
727 "sv_gameplayfix_easierwaterjump 0\n"
728 "sv_gameplayfix_delayprojectiles 0\n"
729 "sv_gameplayfix_multiplethinksperframe 0\n"
730 "sv_gameplayfix_fixedcheckwatertransition 0\n"
731 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
732 "sv_gameplayfix_swiminbmodels 0\n"
733 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
734 "sys_ticrate 0.01388889\n"
736 "r_shadow_bumpscale_basetexture 4\n"
737 "csqc_polygons_defaultmaterial_nocullface 0\n"
741 Cbuf_InsertText(cmd, "\n"
742 "sv_gameplayfix_blowupfallenzombies 1\n"
743 "sv_gameplayfix_findradiusdistancetobox 1\n"
744 "sv_gameplayfix_grenadebouncedownslopes 1\n"
745 "sv_gameplayfix_slidemoveprojectiles 1\n"
746 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
747 "sv_gameplayfix_setmodelrealbox 1\n"
748 "sv_gameplayfix_droptofloorstartsolid 1\n"
749 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
750 "sv_gameplayfix_noairborncorpse 1\n"
751 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
752 "sv_gameplayfix_easierwaterjump 1\n"
753 "sv_gameplayfix_delayprojectiles 1\n"
754 "sv_gameplayfix_multiplethinksperframe 1\n"
755 "sv_gameplayfix_fixedcheckwatertransition 1\n"
756 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
757 "sv_gameplayfix_swiminbmodels 1\n"
758 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
759 "sys_ticrate 0.01388889\n"
760 "sv_gameplayfix_q2airaccelerate 1\n"
761 "sv_gameplayfix_stepmultipletimes 1\n"
762 "csqc_polygons_defaultmaterial_nocullface 1\n"
763 "con_chatsound_team_mask 13\n"
767 case GAME_VORETOURNAMENT:
768 // 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
769 Cbuf_InsertText(cmd, "\n"
770 "csqc_polygons_defaultmaterial_nocullface 1\n"
771 "con_chatsound_team_mask 13\n"
773 "mod_q1bsp_zero_hullsize_cutoff 8.03125\n"
776 // Steel Storm: Burning Retribution csqc misinterprets CSQC_InputEvent if type is a value other than 0 or 1
777 case GAME_STEELSTORM:
778 Cbuf_InsertText(cmd, "\n"
779 "sv_gameplayfix_blowupfallenzombies 1\n"
780 "sv_gameplayfix_findradiusdistancetobox 1\n"
781 "sv_gameplayfix_grenadebouncedownslopes 1\n"
782 "sv_gameplayfix_slidemoveprojectiles 1\n"
783 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
784 "sv_gameplayfix_setmodelrealbox 1\n"
785 "sv_gameplayfix_droptofloorstartsolid 1\n"
786 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
787 "sv_gameplayfix_noairborncorpse 1\n"
788 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
789 "sv_gameplayfix_easierwaterjump 1\n"
790 "sv_gameplayfix_delayprojectiles 1\n"
791 "sv_gameplayfix_multiplethinksperframe 1\n"
792 "sv_gameplayfix_fixedcheckwatertransition 1\n"
793 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
794 "sv_gameplayfix_swiminbmodels 1\n"
795 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
796 "sys_ticrate 0.01388889\n"
797 "cl_csqc_generatemousemoveevents 0\n"
798 "csqc_polygons_defaultmaterial_nocullface 1\n"
802 Cbuf_InsertText(cmd, "\n"
803 "sv_gameplayfix_blowupfallenzombies 1\n"
804 "sv_gameplayfix_findradiusdistancetobox 1\n"
805 "sv_gameplayfix_grenadebouncedownslopes 1\n"
806 "sv_gameplayfix_slidemoveprojectiles 1\n"
807 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
808 "sv_gameplayfix_setmodelrealbox 1\n"
809 "sv_gameplayfix_droptofloorstartsolid 1\n"
810 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
811 "sv_gameplayfix_noairborncorpse 1\n"
812 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
813 "sv_gameplayfix_easierwaterjump 1\n"
814 "sv_gameplayfix_delayprojectiles 1\n"
815 "sv_gameplayfix_multiplethinksperframe 1\n"
816 "sv_gameplayfix_fixedcheckwatertransition 1\n"
817 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
818 "sv_gameplayfix_swiminbmodels 1\n"
819 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
820 "sys_ticrate 0.01388889\n"
821 "csqc_polygons_defaultmaterial_nocullface 0\n"
833 static void Cmd_Exec_f (cmd_state_t *cmd)
838 if (Cmd_Argc(cmd) != 2)
840 Con_Print("exec <filename> : execute a script file\n");
844 s = FS_Search(Cmd_Argv(cmd, 1), true, true, NULL);
845 if(!s || !s->numfilenames)
847 Con_Printf("couldn't exec %s\n",Cmd_Argv(cmd, 1));
851 for(i = 0; i < s->numfilenames; ++i)
852 Cmd_Exec(cmd, s->filenames[i]);
862 Just prints the rest of the line to the console
865 static void Cmd_Echo_f (cmd_state_t *cmd)
869 for (i=1 ; i<Cmd_Argc(cmd) ; i++)
870 Con_Printf("%s ",Cmd_Argv(cmd, i));
875 // Support Doom3-style Toggle Console Command
880 Toggles a specified console variable amongst the values specified (default is 0 and 1)
883 static void Cmd_Toggle_f(cmd_state_t *cmd)
885 // Acquire Number of Arguments
886 int nNumArgs = Cmd_Argc(cmd);
889 // No Arguments Specified; Print Usage
890 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");
892 { // Correct Arguments Specified
893 // Acquire Potential CVar
894 cvar_t* cvCVar = Cvar_FindVar(cmd->cvars, Cmd_Argv(cmd, 1), cmd->cvars_flagsmask);
901 Cvar_SetValueQuick(cvCVar, 0);
903 Cvar_SetValueQuick(cvCVar, 1);
907 { // 0 and Specified Usage
908 if(cvCVar->integer == atoi(Cmd_Argv(cmd, 2) ) )
909 // CVar is Specified Value; // Reset to 0
910 Cvar_SetValueQuick(cvCVar, 0);
912 if(cvCVar->integer == 0)
913 // CVar is 0; Specify Value
914 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
916 // CVar does not match; Reset to 0
917 Cvar_SetValueQuick(cvCVar, 0);
920 { // Variable Values Specified
924 for(nCnt = 2; nCnt < nNumArgs; nCnt++)
925 { // Cycle through Values
926 if( strcmp(cvCVar->string, Cmd_Argv(cmd, nCnt) ) == 0)
927 { // Current Value Located; Increment to Next
928 if( (nCnt + 1) == nNumArgs)
929 // Max Value Reached; Reset
930 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
933 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, nCnt + 1) );
942 // Value not Found; Reset to Original
943 Cvar_SetQuick(cvCVar, Cmd_Argv(cmd, 2) );
949 Con_Printf("ERROR : CVar '%s' not found\n", Cmd_Argv(cmd, 1) );
958 Creates a new command that executes a command string (possibly ; seperated)
961 static void Cmd_Alias_f (cmd_state_t *cmd)
964 char line[MAX_INPUTLINE];
969 if (Cmd_Argc(cmd) == 1)
971 Con_Print("Current alias commands:\n");
972 for (a = cmd->userdefined->alias ; a ; a=a->next)
973 Con_Printf("%s : %s", a->name, a->value);
977 s = Cmd_Argv(cmd, 1);
978 if (strlen(s) >= MAX_ALIAS_NAME)
980 Con_Print("Alias name is too long\n");
984 // if the alias already exists, reuse it
985 for (a = cmd->userdefined->alias ; a ; a=a->next)
987 if (!strcmp(s, a->name))
996 cmd_alias_t *prev, *current;
998 a = (cmd_alias_t *)Z_Malloc (sizeof(cmd_alias_t));
999 strlcpy (a->name, s, sizeof (a->name));
1000 // insert it at the right alphanumeric position
1001 for( prev = NULL, current = cmd->userdefined->alias ; current && strcmp( current->name, a->name ) < 0 ; prev = current, current = current->next )
1006 cmd->userdefined->alias = a;
1012 // copy the rest of the command line
1013 line[0] = 0; // start out with a null string
1015 for (i=2 ; i < c ; i++)
1018 strlcat (line, " ", sizeof (line));
1019 strlcat (line, Cmd_Argv(cmd, i), sizeof (line));
1021 strlcat (line, "\n", sizeof (line));
1023 alloclen = strlen (line) + 1;
1025 line[alloclen - 2] = '\n'; // to make sure a newline is appended even if too long
1026 a->value = (char *)Z_Malloc (alloclen);
1027 memcpy (a->value, line, alloclen);
1034 Remove existing aliases.
1037 static void Cmd_UnAlias_f (cmd_state_t *cmd)
1043 if(Cmd_Argc(cmd) == 1)
1045 Con_Print("unalias: Usage: unalias alias1 [alias2 ...]\n");
1049 for(i = 1; i < Cmd_Argc(cmd); ++i)
1051 s = Cmd_Argv(cmd, i);
1053 for(a = cmd->userdefined->alias; a; p = a, a = a->next)
1055 if(!strcmp(s, a->name))
1057 if (a->initstate) // we can not remove init aliases
1059 if(a == cmd->userdefined->alias)
1060 cmd->userdefined->alias = a->next;
1069 Con_Printf("unalias: %s alias not found\n", s);
1074 =============================================================================
1078 =============================================================================
1081 static const char *Cmd_GetDirectCvarValue(cmd_state_t *cmd, const char *varname, cmd_alias_t *alias, qbool *is_multiple)
1086 static char vabuf[1024]; // cmd_mutex
1089 *is_multiple = false;
1091 if(!varname || !*varname)
1096 if(!strcmp(varname, "*"))
1099 *is_multiple = true;
1100 return Cmd_Args(cmd);
1102 else if(!strcmp(varname, "#"))
1104 return va(vabuf, sizeof(vabuf), "%d", Cmd_Argc(cmd));
1106 else if(varname[strlen(varname) - 1] == '-')
1108 argno = strtol(varname, &endptr, 10);
1109 if(endptr == varname + strlen(varname) - 1)
1111 // whole string is a number, apart from the -
1112 const char *p = Cmd_Args(cmd);
1113 for(; argno > 1; --argno)
1114 if(!COM_ParseToken_Console(&p))
1119 *is_multiple = true;
1121 // kill pre-argument whitespace
1122 for (;*p && ISWHITESPACE(*p);p++)
1131 argno = strtol(varname, &endptr, 10);
1134 // whole string is a number
1135 // NOTE: we already made sure we don't have an empty cvar name!
1136 if(argno >= 0 && argno < Cmd_Argc(cmd))
1137 return Cmd_Argv(cmd, argno);
1142 if((cvar = Cvar_FindVar(cmd->cvars, varname, cmd->cvars_flagsmask)) && !(cvar->flags & CF_PRIVATE))
1143 return cvar->string;
1148 qbool Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset, qbool putquotes)
1150 qbool quote_quot = !!strchr(quoteset, '"');
1151 qbool quote_backslash = !!strchr(quoteset, '\\');
1152 qbool quote_dollar = !!strchr(quoteset, '$');
1161 *out++ = '"'; --outlen;
1167 if(*in == '"' && quote_quot)
1171 *out++ = '\\'; --outlen;
1172 *out++ = '"'; --outlen;
1174 else if(*in == '\\' && quote_backslash)
1178 *out++ = '\\'; --outlen;
1179 *out++ = '\\'; --outlen;
1181 else if(*in == '$' && quote_dollar)
1185 *out++ = '$'; --outlen;
1186 *out++ = '$'; --outlen;
1192 *out++ = *in; --outlen;
1207 static const char *Cmd_GetCvarValue(cmd_state_t *cmd, const char *var, size_t varlen, cmd_alias_t *alias)
1209 static char varname[MAX_INPUTLINE]; // cmd_mutex
1210 static char varval[MAX_INPUTLINE]; // cmd_mutex
1211 const char *varstr = NULL;
1213 qbool required = false;
1214 qbool optional = false;
1215 static char asis[] = "asis"; // just to suppress const char warnings
1217 if(varlen >= MAX_INPUTLINE)
1218 varlen = MAX_INPUTLINE - 1;
1219 memcpy(varname, var, varlen);
1220 varname[varlen] = 0;
1221 varfunc = strchr(varname, ' ');
1233 Con_Printf(CON_WARN "Warning: Could not expand $ in alias %s\n", alias->name);
1235 Con_Printf(CON_WARN "Warning: Could not expand $\n");
1243 while((p = strchr(varfunc, '?')))
1246 memmove(p, p+1, strlen(p)); // with final NUL
1249 while((p = strchr(varfunc, '!')))
1252 memmove(p, p+1, strlen(p)); // with final NUL
1255 while((p = strchr(varfunc, ' ')))
1257 memmove(p, p+1, strlen(p)); // with final NUL
1259 // if no function is left, NULL it
1264 if(varname[0] == '$')
1265 varstr = Cmd_GetDirectCvarValue(cmd, Cmd_GetDirectCvarValue(cmd, varname + 1, alias, NULL), alias, NULL);
1268 qbool is_multiple = false;
1269 // Exception: $* and $n- don't use the quoted form by default
1270 varstr = Cmd_GetDirectCvarValue(cmd, varname, alias, &is_multiple);
1281 Con_Printf(CON_ERROR "Error: Could not expand $%s in alias %s\n", varname, alias->name);
1283 Con_Printf(CON_ERROR "Error: Could not expand $%s\n", varname);
1293 Con_Printf(CON_WARN "Warning: Could not expand $%s in alias %s\n", varname, alias->name);
1295 Con_Printf(CON_WARN "Warning: Could not expand $%s\n", varname);
1296 dpsnprintf(varval, sizeof(varval), "$%s", varname);
1301 if(!varfunc || !strcmp(varfunc, "q")) // note: quoted form is default, use "asis" to override!
1303 // quote it so it can be used inside double quotes
1304 // we just need to replace " by \", and of course, double backslashes
1305 Cmd_QuoteString(varval, sizeof(varval), varstr, "\"\\", false);
1308 else if(!strcmp(varfunc, "asis"))
1313 Con_Printf("Unknown variable function %s\n", varfunc);
1319 Cmd_PreprocessString
1321 Preprocesses strings and replaces $*, $param#, $cvar accordingly. Also strips comments.
1323 static qbool Cmd_PreprocessString(cmd_state_t *cmd, const char *intext, char *outtext, unsigned maxoutlen, cmd_alias_t *alias ) {
1329 // don't crash if there's no room in the outtext buffer
1330 if( maxoutlen == 0 ) {
1333 maxoutlen--; // because of \0
1338 while( *in && outlen < maxoutlen ) {
1340 // this is some kind of expansion, see what comes after the $
1343 // The console does the following preprocessing:
1345 // - $$ is transformed to a single dollar sign.
1346 // - $var or ${var} are expanded to the contents of the named cvar,
1347 // with quotation marks and backslashes quoted so it can safely
1348 // be used inside quotation marks (and it should always be used
1350 // - ${var asis} inserts the cvar value as is, without doing this
1352 // - ${var ?} silently expands to the empty string if
1353 // $var does not exist
1354 // - ${var !} fails expansion and executes nothing if
1355 // $var does not exist
1356 // - prefix the cvar name with a dollar sign to do indirection;
1357 // for example, if $x has the value timelimit, ${$x} will return
1358 // the value of $timelimit
1359 // - when expanding an alias, the special variable name $* refers
1360 // to all alias parameters, and a number refers to that numbered
1361 // alias parameter, where the name of the alias is $0, the first
1362 // parameter is $1 and so on; as a special case, $* inserts all
1363 // parameters, without extra quoting, so one can use $* to just
1364 // pass all parameters around. All parameters starting from $n
1365 // can be referred to as $n- (so $* is equivalent to $1-).
1366 // - ${* q} and ${n- q} force quoting anyway
1368 // Note: when expanding an alias, cvar expansion is done in the SAME step
1369 // as alias expansion so that alias parameters or cvar values containing
1370 // dollar signs have no unwanted bad side effects. However, this needs to
1371 // be accounted for when writing complex aliases. For example,
1372 // alias foo "set x NEW; echo $x"
1373 // actually expands to
1374 // "set x NEW; echo OLD"
1375 // and will print OLD! To work around this, use a second alias:
1376 // alias foo "set x NEW; foo2"
1377 // alias foo2 "echo $x"
1379 // Also note: lines starting with alias are exempt from cvar expansion.
1380 // If you want cvar expansion, write "alias" instead:
1383 // alias foo "echo $x"
1384 // "alias" bar "echo $x"
1387 // foo will print 2, because the variable $x will be expanded when the alias
1388 // gets expanded. bar will print 1, because the variable $x was expanded
1389 // at definition time. foo can be equivalently defined as
1391 // "alias" foo "echo $$x"
1393 // because at definition time, $$ will get replaced to a single $.
1398 } else if(*in == '{') {
1399 varlen = strcspn(in + 1, "}");
1400 if(in[varlen + 1] == '}')
1402 val = Cmd_GetCvarValue(cmd, in + 1, varlen, alias);
1414 varlen = strspn(in, "#*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-");
1415 val = Cmd_GetCvarValue(cmd, in, varlen, alias);
1422 // insert the cvar value
1423 while(*val && outlen < maxoutlen)
1424 outtext[outlen++] = *val++;
1429 // copy the unexpanded text
1430 outtext[outlen++] = '$';
1431 while(eat && outlen < maxoutlen)
1433 outtext[outlen++] = *in++;
1439 outtext[outlen++] = *in++;
1441 outtext[outlen] = 0;
1449 Called for aliases and fills in the alias into the cbuffer
1452 static void Cmd_ExecuteAlias (cmd_state_t *cmd, cmd_alias_t *alias)
1454 static char buffer[ MAX_INPUTLINE ]; // cmd_mutex
1455 static char buffer2[ MAX_INPUTLINE ]; // cmd_mutex
1456 qbool ret = Cmd_PreprocessString( cmd, alias->value, buffer, sizeof(buffer) - 2, alias );
1459 // insert at start of command buffer, so that aliases execute in order
1460 // (fixes bug introduced by Black on 20050705)
1462 // Note: Cbuf_PreprocessString will be called on this string AGAIN! So we
1463 // have to make sure that no second variable expansion takes place, otherwise
1464 // alias parameters containing dollar signs can have bad effects.
1465 Cmd_QuoteString(buffer2, sizeof(buffer2), buffer, "$", false);
1466 Cbuf_InsertText(cmd, buffer2);
1473 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
1474 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
1478 static void Cmd_List_f (cmd_state_t *cmd)
1480 cmd_function_t *func;
1481 const char *partial;
1486 if (Cmd_Argc(cmd) > 1)
1488 partial = Cmd_Argv(cmd, 1);
1489 len = strlen(partial);
1490 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1500 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1502 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1504 Con_Printf("%s : %s\n", func->name, func->description);
1507 for (func = cmd->engine_functions; func; func = func->next)
1509 if (partial && (ispattern ? !matchpattern_with_separator(func->name, partial, false, "", false) : strncmp(partial, func->name, len)))
1511 Con_Printf("%s : %s\n", func->name, func->description);
1518 Con_Printf("%i Command%s matching \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1520 Con_Printf("%i Command%s beginning with \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1523 Con_Printf("%i Command%s\n\n", count, (count > 1) ? "s" : "");
1526 static void Cmd_Apropos_f(cmd_state_t *cmd)
1528 cmd_function_t *func;
1531 const char *partial;
1536 if (Cmd_Argc(cmd) > 1)
1537 partial = Cmd_Args(cmd);
1540 Con_Printf("usage: %s <string>\n",Cmd_Argv(cmd, 0));
1544 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
1546 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
1549 for (cvar = cmd->cvars->vars; cvar; cvar = cvar->next)
1551 if (matchpattern_with_separator(cvar->name, partial, true, "", false) ||
1552 matchpattern_with_separator(cvar->description, partial, true, "", false))
1554 Con_Printf ("cvar ");
1555 Cvar_PrintHelp(cvar, cvar->name, true);
1558 for (char **cvar_alias = cvar->aliases; cvar_alias && *cvar_alias; cvar_alias++)
1560 if (matchpattern_with_separator(*cvar_alias, partial, true, "", false))
1562 Con_Printf ("cvar ");
1563 Cvar_PrintHelp(cvar, *cvar_alias, true);
1568 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1570 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1571 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1573 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1576 for (func = cmd->engine_functions; func; func = func->next)
1578 if (!matchpattern_with_separator(func->name, partial, true, "", false))
1579 if (!matchpattern_with_separator(func->description, partial, true, "", false))
1581 Con_Printf("command ^2%s^7: %s\n", func->name, func->description);
1584 for (alias = cmd->userdefined->alias; alias; alias = alias->next)
1586 // procede here a bit differently as an alias value always got a final \n
1587 if (!matchpattern_with_separator(alias->name, partial, true, "", false))
1588 if (!matchpattern_with_separator(alias->value, partial, true, "\n", false)) // when \n is as separator wildcards don't match it
1590 Con_Printf("alias ^5%s^7: %s", alias->name, alias->value); // do not print an extra \n
1593 Con_Printf("%i result%s\n\n", count, (count > 1) ? "s" : "");
1596 static cmd_state_t *Cmd_AddInterpreter(cmd_buf_t *cbuf, cvar_state_t *cvars, int cvars_flagsmask, int cmds_flagsmask, cmd_userdefined_t *userdefined)
1598 cmd_state_t *cmd = (cmd_state_t *)Mem_Alloc(tempmempool, sizeof(cmd_state_t));
1600 cmd->mempool = Mem_AllocPool("commands", 0, NULL);
1601 // space for commands and script files
1603 cmd->null_string = "";
1606 cmd->cvars_flagsmask = cvars_flagsmask;
1607 cmd->cmd_flags = cmds_flagsmask;
1608 cmd->userdefined = userdefined;
1621 cbuf_mempool = Mem_AllocPool("Command buffer", 0, NULL);
1622 cbuf = (cmd_buf_t *)Mem_Alloc(cbuf_mempool, sizeof(cmd_buf_t));
1623 cbuf->maxsize = CMDBUFSIZE;
1624 cbuf->lock = Thread_CreateMutex();
1628 cbuf->start.prev = cbuf->start.next = &(cbuf->start);
1629 cbuf->deferred.prev = cbuf->deferred.next = &(cbuf->deferred);
1630 cbuf->free.prev = cbuf->free.next = &(cbuf->free);
1632 // FIXME: Get rid of cmd_iter_all eventually. This is just a hack to reduce the amount of work to make the interpreters dynamic.
1633 cmd_iter_all = (cmd_iter_t *)Mem_Alloc(tempmempool, sizeof(cmd_iter_t) * 3);
1636 cmd_iter_all[0].cmd = cmd_local = Cmd_AddInterpreter(cbuf, &cvars_all, CF_CLIENT | CF_SERVER, CF_CLIENT | CF_CLIENT_FROM_SERVER | CF_SERVER_FROM_CLIENT, &cmd_userdefined_all);
1637 cmd_local->Handle = Cmd_CL_Callback;
1638 cmd_local->NotFound = NULL;
1640 // server commands received from clients have no reason to access cvars, cvar expansion seems perilous.
1641 cmd_iter_all[1].cmd = cmd_serverfromclient = Cmd_AddInterpreter(cbuf, &cvars_null, 0, CF_SERVER_FROM_CLIENT | CF_USERINFO, &cmd_userdefined_null);
1642 cmd_serverfromclient->Handle = Cmd_SV_Callback;
1643 cmd_serverfromclient->NotFound = Cmd_SV_NotFound;
1645 cmd_iter_all[2].cmd = NULL;
1647 // register our commands
1649 // client-only commands
1650 Cmd_AddCommand(CF_SHARED, "wait", Cmd_Wait_f, "make script execution wait for next rendered frame");
1651 Cmd_AddCommand(CF_CLIENT, "cprint", Cmd_Centerprint_f, "print something at the screen center");
1653 // maintenance commands used for upkeep of cvars and saved configs
1654 Cmd_AddCommand(CF_SHARED, "stuffcmds", Cmd_StuffCmds_f, "execute commandline parameters (must be present in quake.rc script)");
1655 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");
1656 Cmd_AddCommand(CF_SHARED, "cvar_resettodefaults_all", Cvar_ResetToDefaults_All_f, "sets all cvars to their locked default values");
1657 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)");
1658 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)");
1660 // general console commands used in multiple environments
1661 Cmd_AddCommand(CF_SHARED, "exec", Cmd_Exec_f, "execute a script file");
1662 Cmd_AddCommand(CF_SHARED, "echo",Cmd_Echo_f, "print a message to the console (useful in scripts)");
1663 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");
1664 Cmd_AddCommand(CF_SHARED, "unalias",Cmd_UnAlias_f, "remove an alias");
1665 Cmd_AddCommand(CF_SHARED, "set", Cvar_Set_f, "create or change the value of a console variable");
1666 Cmd_AddCommand(CF_SHARED, "seta", Cvar_SetA_f, "create or change the value of a console variable that will be saved to config.cfg");
1667 Cmd_AddCommand(CF_SHARED, "unset", Cvar_Del_f, "delete a cvar (does not work for static ones like _cl_name, or read-only ones)");
1669 #ifdef FILLALLCVARSWITHRUBBISH
1670 Cmd_AddCommand(CF_SHARED, "fillallcvarswithrubbish", Cvar_FillAll_f, "fill all cvars with a specified number of characters to provoke buffer overruns");
1671 #endif /* FILLALLCVARSWITHRUBBISH */
1673 // 2000-01-09 CmdList, CvarList commands By Matthias "Maddes" Buecher
1674 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
1675 Cmd_AddCommand(CF_SHARED, "cmdlist", Cmd_List_f, "lists all console commands beginning with the specified prefix or matching the specified wildcard pattern");
1676 Cmd_AddCommand(CF_SHARED, "cvarlist", Cvar_List_f, "lists all console variables beginning with the specified prefix or matching the specified wildcard pattern");
1677 Cmd_AddCommand(CF_SHARED, "apropos", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1678 Cmd_AddCommand(CF_SHARED, "find", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1680 Cmd_AddCommand(CF_SHARED, "defer", Cmd_Defer_f, "execute a command in the future");
1683 // Support Doom3-style Toggle Command
1684 Cmd_AddCommand(CF_SHARED | CF_CLIENT_FROM_SERVER, "toggle", Cmd_Toggle_f, "toggles a console variable's values (use for more info)");
1692 void Cmd_Shutdown(void)
1694 cmd_iter_t *cmd_iter;
1695 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
1697 cmd_state_t *cmd = cmd_iter->cmd;
1699 if (cmd->cbuf->lock)
1701 // we usually have this locked when we get here from Host_Quit_f
1702 Cbuf_Unlock(cmd->cbuf);
1705 Mem_FreePool(&cmd->mempool);
1714 inline int Cmd_Argc (cmd_state_t *cmd)
1724 inline const char *Cmd_Argv(cmd_state_t *cmd, int arg)
1726 if (arg >= cmd->argc )
1727 return cmd->null_string;
1728 return cmd->argv[arg];
1736 inline const char *Cmd_Args (cmd_state_t *cmd)
1745 Parses the given string into command line tokens.
1748 // AK: This function should only be called from ExcuteString because the current design is a bit of an hack
1749 static void Cmd_TokenizeString (cmd_state_t *cmd, const char *text)
1755 cmd->cmdline = NULL;
1759 // skip whitespace up to a /n
1760 while (*text && ISWHITESPACE(*text) && *text != '\r' && *text != '\n')
1767 if (*text == '\n' || *text == '\r')
1769 // a newline separates commands in the buffer
1770 if (*text == '\r' && text[1] == '\n')
1780 cmd->cmdline = text;
1784 if (!COM_ParseToken_Console(&text))
1787 if (cmd->argc < MAX_ARGS)
1789 l = (int)strlen(com_token) + 1;
1790 if (cmd->cbuf->tokenizebufferpos + l > CMD_TOKENIZELENGTH)
1792 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguments\n", CMD_TOKENIZELENGTH);
1795 memcpy (cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos, com_token, l);
1796 cmd->argv[cmd->argc] = cmd->cbuf->tokenizebuffer + cmd->cbuf->tokenizebufferpos;
1797 cmd->cbuf->tokenizebufferpos += l;
1809 void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const char *description)
1811 cmd_function_t *func;
1812 cmd_function_t *prev, *current;
1816 for (i = 0; i < 2; i++)
1818 cmd = cmd_iter_all[i].cmd;
1819 if (flags & cmd->cmd_flags)
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 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
1840 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1841 func->flags = flags;
1842 func->name = cmd_name;
1843 func->function = function;
1844 func->description = description;
1845 func->next = cmd->engine_functions;
1847 // insert it at the right alphanumeric position
1848 for (prev = NULL, current = cmd->engine_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1854 cmd->engine_functions = func;
1856 func->next = current;
1860 // mark qcfunc if the function already exists in the qc_functions list
1861 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1863 if (!strcmp(cmd_name, func->name))
1865 func->qcfunc = true; //[515]: csqc
1870 func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t));
1871 func->flags = flags;
1872 func->name = cmd_name;
1873 func->function = function;
1874 func->description = description;
1875 func->qcfunc = true; //[515]: csqc
1876 func->next = cmd->userdefined->qc_functions;
1878 // bones_was_here: if this QC command overrides an engine command, store its pointer
1879 // to avoid doing this search at invocation if QC declines to handle this command.
1880 for (cmd_function_t *f = cmd->engine_functions; f; f = f->next)
1882 if (!strcmp(cmd_name, f->name))
1884 Con_DPrintf("Adding QC override of engine command %s\n", cmd_name);
1885 func->overridden = f;
1890 // insert it at the right alphanumeric position
1891 for (prev = NULL, current = cmd->userdefined->qc_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next)
1897 cmd->userdefined->qc_functions = func;
1899 func->next = current;
1910 qbool Cmd_Exists (cmd_state_t *cmd, const char *cmd_name)
1912 cmd_function_t *func;
1914 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1915 if (!strcmp(cmd_name, func->name))
1918 for (func=cmd->engine_functions ; func ; func=func->next)
1919 if (!strcmp (cmd_name,func->name))
1931 const char *Cmd_CompleteCommand (cmd_state_t *cmd, const char *partial)
1933 cmd_function_t *func;
1936 len = strlen(partial);
1942 for (func = cmd->userdefined->qc_functions; func; func = func->next)
1943 if (!strncasecmp(partial, func->name, len))
1946 for (func = cmd->engine_functions; func; func = func->next)
1947 if (!strncasecmp(partial, func->name, len))
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->qc_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->qc_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->qc_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->qc_functions;
2142 extern cvar_t sv_cheats;
2145 * Cloudwalk FIXME: This idea sounded great in my head but...
2146 * How do we handle commands that can be received by the client,
2147 * but which the server can also execute locally?
2149 * If we create a callback where the engine will forward to server
2150 * but try to execute the command locally if it's dedicated,
2151 * we're back to intermixing client and server code which I'm
2152 * trying to avoid. There's no other way I can think of to
2153 * implement that behavior that doesn't involve an #ifdef, or
2154 * making a mess of hooks.
2156 qbool Cmd_Callback(cmd_state_t *cmd, cmd_function_t *func, const char *text, cmd_source_t src)
2159 func->function(cmd);
2161 Con_Printf("Command \"%s\" can not be executed\n", Cmd_Argv(cmd, 0));
2165 qbool Cmd_CL_Callback(cmd_state_t *cmd, cmd_function_t *func, const char *text, cmd_source_t src)
2167 // TODO: Assign these functions to QC commands directly?
2170 if(((func->flags & CF_CLIENT) && CL_VM_ConsoleCommand(text)) ||
2171 ((func->flags & CF_SERVER) && SV_VM_ConsoleCommand(text)))
2174 if (func->overridden) // If this QC command overrides an engine command,
2175 func = func->overridden; // fall back to that command.
2177 if (func->flags & CF_SERVER_FROM_CLIENT)
2179 if(host_isclient.integer)
2181 CL_ForwardToServer_f(cmd);
2184 else if(!(func->flags & CF_SERVER))
2186 Con_Printf("Cannot execute client commands from a dedicated server console.\n");
2190 return Cmd_Callback(cmd, func, text, src);
2193 qbool Cmd_SV_Callback(cmd_state_t *cmd, cmd_function_t *func, const char *text, cmd_source_t src)
2195 if(func->qcfunc && (func->flags & CF_SERVER))
2196 return SV_VM_ConsoleCommand(text);
2197 else if (src == src_client)
2199 if((func->flags & CF_CHEAT) && !sv_cheats.integer)
2200 SV_ClientPrintf("No cheats allowed. The server must have sv_cheats set to 1\n");
2202 func->function(cmd);
2208 qbool Cmd_SV_NotFound(cmd_state_t *cmd, cmd_function_t *func, const char *text, cmd_source_t src)
2210 if (cmd->source == src_client)
2212 Con_Printf("Client \"%s\" tried to execute \"%s\"\n", host_client->name, text);
2221 A complete command line has been parsed, so try to execute it
2222 FIXME: lookupnoadd the token to speed search?
2225 void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qbool lockmutex)
2228 cmd_function_t *func;
2231 Cbuf_Lock(cmd->cbuf);
2232 oldpos = cmd->cbuf->tokenizebufferpos;
2235 Cmd_TokenizeString (cmd, text);
2237 // execute the command line
2239 goto done; // no tokens
2242 for (func = cmd->userdefined->qc_functions; func; func = func->next)
2244 if (!strcasecmp(cmd->argv[0], func->name))
2246 if(cmd->Handle(cmd, func, text, src))
2251 for (func = cmd->engine_functions; func; func=func->next)
2253 if (!strcasecmp (cmd->argv[0], func->name))
2255 if(cmd->Handle(cmd, func, text, src))
2260 // if it's a client command and no command was found, say so.
2263 if(cmd->NotFound(cmd, func, text, src))
2268 for (a=cmd->userdefined->alias ; a ; a=a->next)
2270 if (!strcasecmp (cmd->argv[0], a->name))
2272 Cmd_ExecuteAlias(cmd, a);
2278 if (!Cvar_Command(cmd) && host.framecount > 0)
2279 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(cmd, 0));
2281 cmd->cbuf->tokenizebufferpos = oldpos;
2283 Cbuf_Unlock(cmd->cbuf);
2290 Returns the position (1 to argc-1) in the command's argument list
2291 where the given parameter apears, or 0 if not present
2295 int Cmd_CheckParm (cmd_state_t *cmd, const char *parm)
2301 Con_Printf ("Cmd_CheckParm: NULL");
2305 for (i = 1; i < Cmd_Argc (cmd); i++)
2306 if (!strcasecmp (parm, Cmd_Argv(cmd, i)))
2314 void Cmd_SaveInitState(void)
2316 cmd_iter_t *cmd_iter;
2317 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2319 cmd_state_t *cmd = cmd_iter->cmd;
2322 for (f = cmd->userdefined->qc_functions; f; f = f->next)
2323 f->initstate = true;
2324 for (f = cmd->engine_functions; f; f = f->next)
2325 f->initstate = true;
2326 for (a = cmd->userdefined->alias; a; a = a->next)
2328 a->initstate = true;
2329 a->initialvalue = Mem_strdup(zonemempool, a->value);
2332 Cvar_SaveInitState(&cvars_all);
2335 void Cmd_RestoreInitState(void)
2337 cmd_iter_t *cmd_iter;
2338 for (cmd_iter = cmd_iter_all; cmd_iter->cmd; cmd_iter++)
2340 cmd_state_t *cmd = cmd_iter->cmd;
2341 cmd_function_t *f, **fp;
2342 cmd_alias_t *a, **ap;
2343 for (fp = &cmd->userdefined->qc_functions; (f = *fp);)
2349 // destroy this command, it didn't exist at init
2350 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2355 for (fp = &cmd->engine_functions; (f = *fp);)
2361 // destroy this command, it didn't exist at init
2362 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2367 for (ap = &cmd->userdefined->alias; (a = *ap);)
2371 // restore this alias, it existed at init
2372 if (strcmp(a->value ? a->value : "", a->initialvalue ? a->initialvalue : ""))
2374 Con_DPrintf("Cmd_RestoreInitState: Restoring alias %s\n", a->name);
2377 a->value = Mem_strdup(zonemempool, a->initialvalue);
2383 // free this alias, it didn't exist at init...
2384 Con_DPrintf("Cmd_RestoreInitState: Destroying alias %s\n", a->name);
2392 Cvar_RestoreInitState(&cvars_all);
2395 void Cmd_NoOperation_f(cmd_state_t *cmd)