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 typedef struct cmdalias_s
27 struct cmdalias_s *next;
28 char name[MAX_ALIAS_NAME];
30 qboolean initstate; // indicates this command existed at init
31 char *initialvalue; // backup copy of value at init
34 static cmdalias_t *cmd_alias;
36 static qboolean cmd_wait;
38 static mempool_t *cmd_mempool;
40 static char cmd_tokenizebuffer[CMD_TOKENIZELENGTH];
41 static int cmd_tokenizebufferpos = 0;
43 //=============================================================================
49 Causes execution of the remainder of the command buffer to be delayed until
50 next frame. This allows commands like:
51 bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
54 static void Cmd_Wait_f (void)
59 typedef struct cmddeferred_s
61 struct cmddeferred_s *next;
66 static cmddeferred_t *cmd_deferred_list = NULL;
72 Cause a command to be executed after a delay.
75 static void Cmd_Defer_f (void)
79 cmddeferred_t *next = cmd_deferred_list;
81 Con_Printf("No commands are pending.\n");
84 Con_Printf("-> In %9.2f: %s\n", next->delay, next->value);
87 } else if(Cmd_Argc() == 2 && !strcasecmp("clear", Cmd_Argv(1)))
89 while(cmd_deferred_list)
91 cmddeferred_t *cmd = cmd_deferred_list;
92 cmd_deferred_list = cmd->next;
96 } else if(Cmd_Argc() == 3)
98 const char *value = Cmd_Argv(2);
99 cmddeferred_t *defcmd = (cmddeferred_t*)Mem_Alloc(tempmempool, sizeof(*defcmd));
100 size_t len = strlen(value);
102 defcmd->delay = atof(Cmd_Argv(1));
103 defcmd->value = (char*)Mem_Alloc(tempmempool, len+1);
104 memcpy(defcmd->value, value, len+1);
107 if(cmd_deferred_list)
109 cmddeferred_t *next = cmd_deferred_list;
114 cmd_deferred_list = defcmd;
115 /* Stupid me... this changes the order... so commands with the same delay go blub :S
116 defcmd->next = cmd_deferred_list;
117 cmd_deferred_list = defcmd;*/
119 Con_Printf("usage: defer <seconds> <command>\n"
129 Print something to the center of the screen using SCR_Centerprint
132 static void Cmd_Centerprint_f (void)
134 char msg[MAX_INPUTLINE];
135 unsigned int i, c, p;
139 strlcpy(msg, Cmd_Argv(1), sizeof(msg));
140 for(i = 2; i < c; ++i)
142 strlcat(msg, " ", sizeof(msg));
143 strlcat(msg, Cmd_Argv(i), sizeof(msg));
146 for(p = 0, i = 0; i < c; ++i)
152 else if(msg[i+1] == '\\')
164 SCR_CenterPrint(msg);
169 =============================================================================
173 =============================================================================
176 static sizebuf_t cmd_text;
177 static unsigned char cmd_text_buf[CMDBUFSIZE];
178 void *cmd_text_mutex = NULL;
184 Adds command text at the end of the buffer
187 void Cbuf_AddText (const char *text)
191 l = (int)strlen(text);
193 Cbuf_LockThreadMutex();
194 if (cmd_text.cursize + l >= cmd_text.maxsize)
195 Con_Print("Cbuf_AddText: overflow\n");
197 SZ_Write(&cmd_text, (const unsigned char *)text, l);
198 Cbuf_UnlockThreadMutex();
206 Adds command text immediately after the current command
207 Adds a \n to the text
208 FIXME: actually change the command buffer to do less copying
211 void Cbuf_InsertText (const char *text)
213 size_t l = strlen(text);
214 Cbuf_LockThreadMutex();
215 // we need to memmove the existing text and stuff this in before it...
216 if (cmd_text.cursize + l >= (size_t)cmd_text.maxsize)
217 Con_Print("Cbuf_InsertText: overflow\n");
220 // we don't have a SZ_Prepend, so...
221 memmove(cmd_text.data + l, cmd_text.data, cmd_text.cursize);
222 cmd_text.cursize += l;
223 memcpy(cmd_text.data, text, l);
225 Cbuf_UnlockThreadMutex();
230 Cbuf_Execute_Deferred --blub
233 static void Cbuf_Execute_Deferred (void)
235 static double oldrealtime = 0;
236 cmddeferred_t *cmd, *prev;
238 if (realtime - oldrealtime < 0 || realtime - oldrealtime > 1800) oldrealtime = realtime;
239 eat = realtime - oldrealtime;
240 if (eat < (1.0 / 120.0))
242 oldrealtime = realtime;
244 cmd = cmd_deferred_list;
250 Cbuf_AddText(cmd->value);
252 Mem_Free(cmd->value);
255 prev->next = cmd->next;
259 cmd_deferred_list = cmd->next;
261 cmd = cmd_deferred_list;
275 static qboolean Cmd_PreprocessString( const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias );
276 void Cbuf_Execute (void)
280 char line[MAX_INPUTLINE];
281 char preprocessed[MAX_INPUTLINE];
286 // LordHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
287 cmd_tokenizebufferpos = 0;
289 while (cmd_text.cursize)
291 // find a \n or ; line break
292 text = (char *)cmd_text.data;
296 for (i=0 ; i < cmd_text.cursize ; i++)
305 // make sure i doesn't get > cursize which causes a negative
306 // size in memmove, which is fatal --blub
307 if (i < (cmd_text.cursize-1) && (text[i] == '\\' && (text[i+1] == '"' || text[i+1] == '\\')))
312 if(text[i] == '/' && text[i + 1] == '/' && (i == 0 || ISWHITESPACE(text[i-1])))
315 break; // don't break if inside a quoted string or comment
319 if (text[i] == '\r' || text[i] == '\n')
323 // better than CRASHING on overlong input lines that may SOMEHOW enter the buffer
324 if(i >= MAX_INPUTLINE)
326 Con_Printf("Warning: console input buffer had an overlong line. Ignored.\n");
331 memcpy (line, text, comment ? (comment - text) : i);
332 line[comment ? (comment - text) : i] = 0;
335 // delete the text from the command buffer and move remaining commands down
336 // this is necessary because commands (exec, alias) can insert data at the
337 // beginning of the text buffer
339 if (i == cmd_text.cursize)
340 cmd_text.cursize = 0;
344 cmd_text.cursize -= i;
345 memmove (cmd_text.data, text+i, cmd_text.cursize);
348 // execute the command line
350 while(*firstchar && ISWHITESPACE(*firstchar))
353 (strncmp(firstchar, "alias", 5) || !ISWHITESPACE(firstchar[5]))
355 (strncmp(firstchar, "bind", 4) || !ISWHITESPACE(firstchar[4]))
357 (strncmp(firstchar, "in_bind", 7) || !ISWHITESPACE(firstchar[7]))
360 if(Cmd_PreprocessString( line, preprocessed, sizeof(preprocessed), NULL ))
361 Cmd_ExecuteString (preprocessed, src_command, false);
365 Cmd_ExecuteString (line, src_command, false);
369 { // skip out while text still remains in buffer, leaving it
377 void Cbuf_Frame(void)
379 Cbuf_Execute_Deferred();
380 if (cmd_text.cursize)
382 SV_LockThreadMutex();
384 SV_UnlockThreadMutex();
389 ==============================================================================
393 ==============================================================================
400 Adds command line parameters as script statements
401 Commands lead with a +, and continue until a - or another +
402 quake +prog jctest.qp +cmd amlev1
403 quake -nosound +cmd amlev1
406 qboolean host_stuffcmdsrun = false;
407 static void Cmd_StuffCmds_f (void)
410 // this is for all commandline options combined (and is bounds checked)
411 char build[MAX_INPUTLINE];
413 if (Cmd_Argc () != 1)
415 Con_Print("stuffcmds : execute command line parameters\n");
419 // no reason to run the commandline arguments twice
420 if (host_stuffcmdsrun)
423 host_stuffcmdsrun = true;
426 for (i = 0;i < com_argc;i++)
428 if (com_argv[i] && com_argv[i][0] == '+' && (com_argv[i][1] < '0' || com_argv[i][1] > '9') && l + strlen(com_argv[i]) - 1 <= sizeof(build) - 1)
431 while (com_argv[i][j])
432 build[l++] = com_argv[i][j++];
434 for (;i < com_argc;i++)
438 if ((com_argv[i][0] == '+' || com_argv[i][0] == '-') && (com_argv[i][1] < '0' || com_argv[i][1] > '9'))
440 if (l + strlen(com_argv[i]) + 4 > sizeof(build) - 1)
443 if (strchr(com_argv[i], ' '))
445 for (j = 0;com_argv[i][j];j++)
446 build[l++] = com_argv[i][j];
447 if (strchr(com_argv[i], ' '))
454 // now terminate the combined string and prepend it to the command buffer
455 // we already reserved space for the terminator
457 Cbuf_InsertText (build);
460 static void Cmd_Exec(const char *filename)
463 size_t filenameLen = strlen(filename);
464 qboolean isdefaultcfg = filenameLen >= 11 && !strcmp(filename + filenameLen - 11, "default.cfg");
466 if (!strcmp(filename, "config.cfg"))
468 filename = CONFIGFILENAME;
469 if (COM_CheckParm("-noconfig"))
470 return; // don't execute config.cfg
473 f = (char *)FS_LoadFile (filename, tempmempool, false, NULL);
476 Con_Printf("couldn't exec %s\n",filename);
479 Con_Printf("execing %s\n",filename);
481 // if executing default.cfg for the first time, lock the cvar defaults
482 // it may seem backwards to insert this text BEFORE the default.cfg
483 // but Cbuf_InsertText inserts before, so this actually ends up after it.
485 Cbuf_InsertText("\ncvar_lockdefaults\n");
487 // insert newline after the text to make sure the last line is terminated (some text editors omit the trailing newline)
488 // (note: insertion order here is backwards from execution order, so this adds it after the text, by calling it before...)
489 Cbuf_InsertText ("\n");
495 // special defaults for specific games go here, these execute before default.cfg
496 // Nehahra pushable crates malfunction in some levels if this is on
497 // Nehahra NPC AI is confused by blowupfallenzombies
502 "sv_gameplayfix_blowupfallenzombies 0\n"
503 "sv_gameplayfix_findradiusdistancetobox 0\n"
504 "sv_gameplayfix_grenadebouncedownslopes 0\n"
505 "sv_gameplayfix_slidemoveprojectiles 0\n"
506 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
507 "sv_gameplayfix_setmodelrealbox 0\n"
508 "sv_gameplayfix_droptofloorstartsolid 0\n"
509 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
510 "sv_gameplayfix_noairborncorpse 0\n"
511 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
512 "sv_gameplayfix_easierwaterjump 0\n"
513 "sv_gameplayfix_delayprojectiles 0\n"
514 "sv_gameplayfix_multiplethinksperframe 0\n"
515 "sv_gameplayfix_fixedcheckwatertransition 0\n"
516 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
517 "sv_gameplayfix_swiminbmodels 0\n"
518 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
519 "sys_ticrate 0.01388889\n"
521 "r_shadow_bumpscale_basetexture 0\n"
526 "sv_gameplayfix_blowupfallenzombies 0\n"
527 "sv_gameplayfix_findradiusdistancetobox 0\n"
528 "sv_gameplayfix_grenadebouncedownslopes 0\n"
529 "sv_gameplayfix_slidemoveprojectiles 0\n"
530 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
531 "sv_gameplayfix_setmodelrealbox 0\n"
532 "sv_gameplayfix_droptofloorstartsolid 0\n"
533 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
534 "sv_gameplayfix_noairborncorpse 0\n"
535 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
536 "sv_gameplayfix_easierwaterjump 0\n"
537 "sv_gameplayfix_delayprojectiles 0\n"
538 "sv_gameplayfix_multiplethinksperframe 0\n"
539 "sv_gameplayfix_fixedcheckwatertransition 0\n"
540 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
541 "sv_gameplayfix_swiminbmodels 0\n"
542 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
543 "sys_ticrate 0.01388889\n"
545 "r_shadow_bumpscale_basetexture 0\n"
548 // 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.
549 // 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
550 // hipnotic mission pack has issues in their proximity mine sticking code, which causes them to bounce off.
554 "sv_gameplayfix_blowupfallenzombies 0\n"
555 "sv_gameplayfix_findradiusdistancetobox 0\n"
556 "sv_gameplayfix_grenadebouncedownslopes 0\n"
557 "sv_gameplayfix_slidemoveprojectiles 0\n"
558 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
559 "sv_gameplayfix_setmodelrealbox 0\n"
560 "sv_gameplayfix_droptofloorstartsolid 0\n"
561 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
562 "sv_gameplayfix_noairborncorpse 0\n"
563 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
564 "sv_gameplayfix_easierwaterjump 0\n"
565 "sv_gameplayfix_delayprojectiles 0\n"
566 "sv_gameplayfix_multiplethinksperframe 0\n"
567 "sv_gameplayfix_fixedcheckwatertransition 0\n"
568 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
569 "sv_gameplayfix_swiminbmodels 0\n"
570 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
573 "r_shadow_bumpscale_basetexture 0\n"
576 // rogue mission pack has a guardian boss that does not wake up if findradius returns one of the entities around its spawn area
579 "sv_gameplayfix_blowupfallenzombies 0\n"
580 "sv_gameplayfix_findradiusdistancetobox 0\n"
581 "sv_gameplayfix_grenadebouncedownslopes 0\n"
582 "sv_gameplayfix_slidemoveprojectiles 0\n"
583 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
584 "sv_gameplayfix_setmodelrealbox 0\n"
585 "sv_gameplayfix_droptofloorstartsolid 0\n"
586 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
587 "sv_gameplayfix_noairborncorpse 0\n"
588 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
589 "sv_gameplayfix_easierwaterjump 0\n"
590 "sv_gameplayfix_delayprojectiles 0\n"
591 "sv_gameplayfix_multiplethinksperframe 0\n"
592 "sv_gameplayfix_fixedcheckwatertransition 0\n"
593 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
594 "sv_gameplayfix_swiminbmodels 0\n"
595 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
596 "sys_ticrate 0.01388889\n"
598 "r_shadow_bumpscale_basetexture 0\n"
603 "sv_gameplayfix_blowupfallenzombies 0\n"
604 "sv_gameplayfix_findradiusdistancetobox 0\n"
605 "sv_gameplayfix_grenadebouncedownslopes 0\n"
606 "sv_gameplayfix_slidemoveprojectiles 0\n"
607 "sv_gameplayfix_upwardvelocityclearsongroundflag 0\n"
608 "sv_gameplayfix_setmodelrealbox 0\n"
609 "sv_gameplayfix_droptofloorstartsolid 0\n"
610 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 0\n"
611 "sv_gameplayfix_noairborncorpse 0\n"
612 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 0\n"
613 "sv_gameplayfix_easierwaterjump 0\n"
614 "sv_gameplayfix_delayprojectiles 0\n"
615 "sv_gameplayfix_multiplethinksperframe 0\n"
616 "sv_gameplayfix_fixedcheckwatertransition 0\n"
617 "sv_gameplayfix_q1bsptracelinereportstexture 0\n"
618 "sv_gameplayfix_swiminbmodels 0\n"
619 "sv_gameplayfix_downtracesupportsongroundflag 0\n"
620 "sys_ticrate 0.01388889\n"
622 "r_shadow_bumpscale_basetexture 4\n"
627 "sv_gameplayfix_blowupfallenzombies 1\n"
628 "sv_gameplayfix_findradiusdistancetobox 1\n"
629 "sv_gameplayfix_grenadebouncedownslopes 1\n"
630 "sv_gameplayfix_slidemoveprojectiles 1\n"
631 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
632 "sv_gameplayfix_setmodelrealbox 1\n"
633 "sv_gameplayfix_droptofloorstartsolid 1\n"
634 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
635 "sv_gameplayfix_noairborncorpse 1\n"
636 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
637 "sv_gameplayfix_easierwaterjump 1\n"
638 "sv_gameplayfix_delayprojectiles 1\n"
639 "sv_gameplayfix_multiplethinksperframe 1\n"
640 "sv_gameplayfix_fixedcheckwatertransition 1\n"
641 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
642 "sv_gameplayfix_swiminbmodels 1\n"
643 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
644 "sys_ticrate 0.01388889\n"
645 "sv_gameplayfix_q2airaccelerate 1\n"
646 "sv_gameplayfix_stepmultipletimes 1\n"
649 // Steel Storm: Burning Retribution csqc misinterprets CSQC_InputEvent if type is a value other than 0 or 1
650 case GAME_STEELSTORM:
652 "sv_gameplayfix_blowupfallenzombies 1\n"
653 "sv_gameplayfix_findradiusdistancetobox 1\n"
654 "sv_gameplayfix_grenadebouncedownslopes 1\n"
655 "sv_gameplayfix_slidemoveprojectiles 1\n"
656 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
657 "sv_gameplayfix_setmodelrealbox 1\n"
658 "sv_gameplayfix_droptofloorstartsolid 1\n"
659 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
660 "sv_gameplayfix_noairborncorpse 1\n"
661 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
662 "sv_gameplayfix_easierwaterjump 1\n"
663 "sv_gameplayfix_delayprojectiles 1\n"
664 "sv_gameplayfix_multiplethinksperframe 1\n"
665 "sv_gameplayfix_fixedcheckwatertransition 1\n"
666 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
667 "sv_gameplayfix_swiminbmodels 1\n"
668 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
669 "sys_ticrate 0.01388889\n"
670 "cl_csqc_generatemousemoveevents 0\n"
675 "sv_gameplayfix_blowupfallenzombies 1\n"
676 "sv_gameplayfix_findradiusdistancetobox 1\n"
677 "sv_gameplayfix_grenadebouncedownslopes 1\n"
678 "sv_gameplayfix_slidemoveprojectiles 1\n"
679 "sv_gameplayfix_upwardvelocityclearsongroundflag 1\n"
680 "sv_gameplayfix_setmodelrealbox 1\n"
681 "sv_gameplayfix_droptofloorstartsolid 1\n"
682 "sv_gameplayfix_droptofloorstartsolid_nudgetocorrect 1\n"
683 "sv_gameplayfix_noairborncorpse 1\n"
684 "sv_gameplayfix_noairborncorpse_allowsuspendeditems 1\n"
685 "sv_gameplayfix_easierwaterjump 1\n"
686 "sv_gameplayfix_delayprojectiles 1\n"
687 "sv_gameplayfix_multiplethinksperframe 1\n"
688 "sv_gameplayfix_fixedcheckwatertransition 1\n"
689 "sv_gameplayfix_q1bsptracelinereportstexture 1\n"
690 "sv_gameplayfix_swiminbmodels 1\n"
691 "sv_gameplayfix_downtracesupportsongroundflag 1\n"
692 "sys_ticrate 0.01388889\n"
704 static void Cmd_Exec_f (void)
709 if (Cmd_Argc () != 2)
711 Con_Print("exec <filename> : execute a script file\n");
715 s = FS_Search(Cmd_Argv(1), true, true);
716 if(!s || !s->numfilenames)
718 Con_Printf("couldn't exec %s\n",Cmd_Argv(1));
722 for(i = 0; i < s->numfilenames; ++i)
723 Cmd_Exec(s->filenames[i]);
733 Just prints the rest of the line to the console
736 static void Cmd_Echo_f (void)
740 for (i=1 ; i<Cmd_Argc() ; i++)
741 Con_Printf("%s ",Cmd_Argv(i));
746 // Support Doom3-style Toggle Console Command
751 Toggles a specified console variable amongst the values specified (default is 0 and 1)
754 static void Cmd_Toggle_f(void)
756 // Acquire Number of Arguments
757 int nNumArgs = Cmd_Argc();
760 // No Arguments Specified; Print Usage
761 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");
763 { // Correct Arguments Specified
764 // Acquire Potential CVar
765 cvar_t* cvCVar = Cvar_FindVar( Cmd_Argv(1) );
772 Cvar_SetValueQuick(cvCVar, 0);
774 Cvar_SetValueQuick(cvCVar, 1);
778 { // 0 and Specified Usage
779 if(cvCVar->integer == atoi(Cmd_Argv(2) ) )
780 // CVar is Specified Value; // Reset to 0
781 Cvar_SetValueQuick(cvCVar, 0);
783 if(cvCVar->integer == 0)
784 // CVar is 0; Specify Value
785 Cvar_SetQuick(cvCVar, Cmd_Argv(2) );
787 // CVar does not match; Reset to 0
788 Cvar_SetValueQuick(cvCVar, 0);
791 { // Variable Values Specified
795 for(nCnt = 2; nCnt < nNumArgs; nCnt++)
796 { // Cycle through Values
797 if( strcmp(cvCVar->string, Cmd_Argv(nCnt) ) == 0)
798 { // Current Value Located; Increment to Next
799 if( (nCnt + 1) == nNumArgs)
800 // Max Value Reached; Reset
801 Cvar_SetQuick(cvCVar, Cmd_Argv(2) );
804 Cvar_SetQuick(cvCVar, Cmd_Argv(nCnt + 1) );
813 // Value not Found; Reset to Original
814 Cvar_SetQuick(cvCVar, Cmd_Argv(2) );
820 Con_Printf("ERROR : CVar '%s' not found\n", Cmd_Argv(1) );
829 Creates a new command that executes a command string (possibly ; seperated)
832 static void Cmd_Alias_f (void)
835 char cmd[MAX_INPUTLINE];
842 Con_Print("Current alias commands:\n");
843 for (a = cmd_alias ; a ; a=a->next)
844 Con_Printf("%s : %s", a->name, a->value);
849 if (strlen(s) >= MAX_ALIAS_NAME)
851 Con_Print("Alias name is too long\n");
855 // if the alias already exists, reuse it
856 for (a = cmd_alias ; a ; a=a->next)
858 if (!strcmp(s, a->name))
867 cmdalias_t *prev, *current;
869 a = (cmdalias_t *)Z_Malloc (sizeof(cmdalias_t));
870 strlcpy (a->name, s, sizeof (a->name));
871 // insert it at the right alphanumeric position
872 for( prev = NULL, current = cmd_alias ; current && strcmp( current->name, a->name ) < 0 ; prev = current, current = current->next )
883 // copy the rest of the command line
884 cmd[0] = 0; // start out with a null string
886 for (i=2 ; i < c ; i++)
889 strlcat (cmd, " ", sizeof (cmd));
890 strlcat (cmd, Cmd_Argv(i), sizeof (cmd));
892 strlcat (cmd, "\n", sizeof (cmd));
894 alloclen = strlen (cmd) + 1;
896 cmd[alloclen - 2] = '\n'; // to make sure a newline is appended even if too long
897 a->value = (char *)Z_Malloc (alloclen);
898 memcpy (a->value, cmd, alloclen);
905 Remove existing aliases.
908 static void Cmd_UnAlias_f (void)
916 Con_Print("unalias: Usage: unalias alias1 [alias2 ...]\n");
920 for(i = 1; i < Cmd_Argc(); ++i)
924 for(a = cmd_alias; a; p = a, a = a->next)
926 if(!strcmp(s, a->name))
928 if (a->initstate) // we can not remove init aliases
940 Con_Printf("unalias: %s alias not found\n", s);
945 =============================================================================
949 =============================================================================
952 typedef struct cmd_function_s
954 struct cmd_function_s *next;
956 const char *description;
957 xcommand_t consolefunction;
958 xcommand_t clientfunction;
960 qboolean initstate; // indicates this command existed at init
964 static const char *cmd_argv[MAX_ARGS];
965 static const char *cmd_null_string = "";
966 static const char *cmd_args;
967 cmd_source_t cmd_source;
970 static cmd_function_t *cmd_functions; // possible commands to execute
972 static const char *Cmd_GetDirectCvarValue(const char *varname, cmdalias_t *alias, qboolean *is_multiple)
980 *is_multiple = false;
982 if(!varname || !*varname)
987 if(!strcmp(varname, "*"))
993 else if(!strcmp(varname, "#"))
995 return va(vabuf, sizeof(vabuf), "%d", Cmd_Argc());
997 else if(varname[strlen(varname) - 1] == '-')
999 argno = strtol(varname, &endptr, 10);
1000 if(endptr == varname + strlen(varname) - 1)
1002 // whole string is a number, apart from the -
1003 const char *p = Cmd_Args();
1004 for(; argno > 1; --argno)
1005 if(!COM_ParseToken_Console(&p))
1010 *is_multiple = true;
1012 // kill pre-argument whitespace
1013 for (;*p && ISWHITESPACE(*p);p++)
1022 argno = strtol(varname, &endptr, 10);
1025 // whole string is a number
1026 // NOTE: we already made sure we don't have an empty cvar name!
1027 if(argno >= 0 && argno < Cmd_Argc())
1028 return Cmd_Argv(argno);
1033 if((cvar = Cvar_FindVar(varname)) && !(cvar->flags & CVAR_PRIVATE))
1034 return cvar->string;
1039 qboolean Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset, qboolean putquotes)
1041 qboolean quote_quot = !!strchr(quoteset, '"');
1042 qboolean quote_backslash = !!strchr(quoteset, '\\');
1043 qboolean quote_dollar = !!strchr(quoteset, '$');
1052 *out++ = '"'; --outlen;
1058 if(*in == '"' && quote_quot)
1062 *out++ = '\\'; --outlen;
1063 *out++ = '"'; --outlen;
1065 else if(*in == '\\' && quote_backslash)
1069 *out++ = '\\'; --outlen;
1070 *out++ = '\\'; --outlen;
1072 else if(*in == '$' && quote_dollar)
1076 *out++ = '$'; --outlen;
1077 *out++ = '$'; --outlen;
1083 *out++ = *in; --outlen;
1098 static const char *Cmd_GetCvarValue(const char *var, size_t varlen, cmdalias_t *alias)
1100 static char varname[MAX_INPUTLINE]; // cmd_mutex
1101 static char varval[MAX_INPUTLINE]; // cmd_mutex
1102 const char *varstr = NULL;
1104 qboolean required = false;
1105 qboolean optional = false;
1106 static char asis[] = "asis"; // just to suppress const char warnings
1108 if(varlen >= MAX_INPUTLINE)
1109 varlen = MAX_INPUTLINE - 1;
1110 memcpy(varname, var, varlen);
1111 varname[varlen] = 0;
1112 varfunc = strchr(varname, ' ');
1124 Con_Printf("Warning: Could not expand $ in alias %s\n", alias->name);
1126 Con_Printf("Warning: Could not expand $\n");
1134 while((p = strchr(varfunc, '?')))
1137 memmove(p, p+1, strlen(p)); // with final NUL
1140 while((p = strchr(varfunc, '!')))
1143 memmove(p, p+1, strlen(p)); // with final NUL
1146 while((p = strchr(varfunc, ' ')))
1148 memmove(p, p+1, strlen(p)); // with final NUL
1150 // if no function is left, NULL it
1155 if(varname[0] == '$')
1156 varstr = Cmd_GetDirectCvarValue(Cmd_GetDirectCvarValue(varname + 1, alias, NULL), alias, NULL);
1159 qboolean is_multiple = false;
1160 // Exception: $* and $n- don't use the quoted form by default
1161 varstr = Cmd_GetDirectCvarValue(varname, alias, &is_multiple);
1172 Con_Printf("Error: Could not expand $%s in alias %s\n", varname, alias->name);
1174 Con_Printf("Error: Could not expand $%s\n", varname);
1184 Con_Printf("Warning: Could not expand $%s in alias %s\n", varname, alias->name);
1186 Con_Printf("Warning: Could not expand $%s\n", varname);
1187 dpsnprintf(varval, sizeof(varval), "$%s", varname);
1192 if(!varfunc || !strcmp(varfunc, "q")) // note: quoted form is default, use "asis" to override!
1194 // quote it so it can be used inside double quotes
1195 // we just need to replace " by \", and of course, double backslashes
1196 Cmd_QuoteString(varval, sizeof(varval), varstr, "\"\\", false);
1199 else if(!strcmp(varfunc, "asis"))
1204 Con_Printf("Unknown variable function %s\n", varfunc);
1210 Cmd_PreprocessString
1212 Preprocesses strings and replaces $*, $param#, $cvar accordingly. Also strips comments.
1214 static qboolean Cmd_PreprocessString( const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias ) {
1220 // don't crash if there's no room in the outtext buffer
1221 if( maxoutlen == 0 ) {
1224 maxoutlen--; // because of \0
1229 while( *in && outlen < maxoutlen ) {
1231 // this is some kind of expansion, see what comes after the $
1234 // The console does the following preprocessing:
1236 // - $$ is transformed to a single dollar sign.
1237 // - $var or ${var} are expanded to the contents of the named cvar,
1238 // with quotation marks and backslashes quoted so it can safely
1239 // be used inside quotation marks (and it should always be used
1241 // - ${var asis} inserts the cvar value as is, without doing this
1243 // - ${var ?} silently expands to the empty string if
1244 // $var does not exist
1245 // - ${var !} fails expansion and executes nothing if
1246 // $var does not exist
1247 // - prefix the cvar name with a dollar sign to do indirection;
1248 // for example, if $x has the value timelimit, ${$x} will return
1249 // the value of $timelimit
1250 // - when expanding an alias, the special variable name $* refers
1251 // to all alias parameters, and a number refers to that numbered
1252 // alias parameter, where the name of the alias is $0, the first
1253 // parameter is $1 and so on; as a special case, $* inserts all
1254 // parameters, without extra quoting, so one can use $* to just
1255 // pass all parameters around. All parameters starting from $n
1256 // can be referred to as $n- (so $* is equivalent to $1-).
1257 // - ${* q} and ${n- q} force quoting anyway
1259 // Note: when expanding an alias, cvar expansion is done in the SAME step
1260 // as alias expansion so that alias parameters or cvar values containing
1261 // dollar signs have no unwanted bad side effects. However, this needs to
1262 // be accounted for when writing complex aliases. For example,
1263 // alias foo "set x NEW; echo $x"
1264 // actually expands to
1265 // "set x NEW; echo OLD"
1266 // and will print OLD! To work around this, use a second alias:
1267 // alias foo "set x NEW; foo2"
1268 // alias foo2 "echo $x"
1270 // Also note: lines starting with alias are exempt from cvar expansion.
1271 // If you want cvar expansion, write "alias" instead:
1274 // alias foo "echo $x"
1275 // "alias" bar "echo $x"
1278 // foo will print 2, because the variable $x will be expanded when the alias
1279 // gets expanded. bar will print 1, because the variable $x was expanded
1280 // at definition time. foo can be equivalently defined as
1282 // "alias" foo "echo $$x"
1284 // because at definition time, $$ will get replaced to a single $.
1289 } else if(*in == '{') {
1290 varlen = strcspn(in + 1, "}");
1291 if(in[varlen + 1] == '}')
1293 val = Cmd_GetCvarValue(in + 1, varlen, alias);
1305 varlen = strspn(in, "#*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-");
1306 val = Cmd_GetCvarValue(in, varlen, alias);
1313 // insert the cvar value
1314 while(*val && outlen < maxoutlen)
1315 outtext[outlen++] = *val++;
1320 // copy the unexpanded text
1321 outtext[outlen++] = '$';
1322 while(eat && outlen < maxoutlen)
1324 outtext[outlen++] = *in++;
1330 outtext[outlen++] = *in++;
1332 outtext[outlen] = 0;
1340 Called for aliases and fills in the alias into the cbuffer
1343 static void Cmd_ExecuteAlias (cmdalias_t *alias)
1345 static char buffer[ MAX_INPUTLINE ]; // cmd_mutex
1346 static char buffer2[ MAX_INPUTLINE ]; // cmd_mutex
1347 qboolean ret = Cmd_PreprocessString( alias->value, buffer, sizeof(buffer) - 2, alias );
1350 // insert at start of command buffer, so that aliases execute in order
1351 // (fixes bug introduced by Black on 20050705)
1353 // Note: Cbuf_PreprocessString will be called on this string AGAIN! So we
1354 // have to make sure that no second variable expansion takes place, otherwise
1355 // alias parameters containing dollar signs can have bad effects.
1356 Cmd_QuoteString(buffer2, sizeof(buffer2), buffer, "$", false);
1357 Cbuf_InsertText( buffer2 );
1364 CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
1365 Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
1369 static void Cmd_List_f (void)
1371 cmd_function_t *cmd;
1372 const char *partial;
1379 partial = Cmd_Argv (1);
1380 len = strlen(partial);
1381 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1391 for (cmd = cmd_functions; cmd; cmd = cmd->next)
1393 if (partial && (ispattern ? !matchpattern_with_separator(cmd->name, partial, false, "", false) : strncmp(partial, cmd->name, len)))
1395 Con_Printf("%s : %s\n", cmd->name, cmd->description);
1402 Con_Printf("%i Command%s matching \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1404 Con_Printf("%i Command%s beginning with \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
1407 Con_Printf("%i Command%s\n\n", count, (count > 1) ? "s" : "");
1410 static void Cmd_Apropos_f(void)
1412 cmd_function_t *cmd;
1415 const char *partial;
1421 partial = Cmd_Args();
1424 Con_Printf("usage: apropos <string>\n");
1428 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
1430 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
1433 for (cvar = cvar_vars; cvar; cvar = cvar->next)
1435 if (!matchpattern_with_separator(cvar->name, partial, true, "", false))
1436 if (!matchpattern_with_separator(cvar->description, partial, true, "", false))
1438 Con_Printf ("cvar ^3%s^7 is \"%s\" [\"%s\"] %s\n", cvar->name, cvar->string, cvar->defstring, cvar->description);
1441 for (cmd = cmd_functions; cmd; cmd = cmd->next)
1443 if (!matchpattern_with_separator(cmd->name, partial, true, "", false))
1444 if (!matchpattern_with_separator(cmd->description, partial, true, "", false))
1446 Con_Printf("command ^2%s^7: %s\n", cmd->name, cmd->description);
1449 for (alias = cmd_alias; alias; alias = alias->next)
1451 // procede here a bit differently as an alias value always got a final \n
1452 if (!matchpattern_with_separator(alias->name, partial, true, "", false))
1453 if (!matchpattern_with_separator(alias->value, partial, true, "\n", false)) // when \n is as separator wildcards don't match it
1455 Con_Printf("alias ^5%s^7: %s", alias->name, alias->value); // do not print an extra \n
1458 Con_Printf("%i result%s\n\n", count, (count > 1) ? "s" : "");
1466 void Cmd_Init (void)
1468 cmd_mempool = Mem_AllocPool("commands", 0, NULL);
1469 // space for commands and script files
1470 cmd_text.data = cmd_text_buf;
1471 cmd_text.maxsize = sizeof(cmd_text_buf);
1472 cmd_text.cursize = 0;
1474 if (Thread_HasThreads())
1475 cmd_text_mutex = Thread_CreateMutex();
1478 void Cmd_Init_Commands (void)
1481 // register our commands
1483 Cmd_AddCommand ("stuffcmds",Cmd_StuffCmds_f, "execute commandline parameters (must be present in quake.rc script)");
1484 Cmd_AddCommand ("exec",Cmd_Exec_f, "execute a script file");
1485 Cmd_AddCommand ("echo",Cmd_Echo_f, "print a message to the console (useful in scripts)");
1486 Cmd_AddCommand ("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");
1487 Cmd_AddCommand ("unalias",Cmd_UnAlias_f, "remove an alias");
1488 Cmd_AddCommand ("cmd", Cmd_ForwardToServer, "send a console commandline to the server (used by some mods)");
1489 Cmd_AddCommand ("wait", Cmd_Wait_f, "make script execution wait for next rendered frame");
1490 Cmd_AddCommand ("set", Cvar_Set_f, "create or change the value of a console variable");
1491 Cmd_AddCommand ("seta", Cvar_SetA_f, "create or change the value of a console variable that will be saved to config.cfg");
1492 Cmd_AddCommand ("unset", Cvar_Del_f, "delete a cvar (does not work for static ones like _cl_name, or read-only ones)");
1493 #ifdef FILLALLCVARSWITHRUBBISH
1494 Cmd_AddCommand ("fillallcvarswithrubbish", Cvar_FillAll_f, "fill all cvars with a specified number of characters to provoke buffer overruns");
1495 #endif /* FILLALLCVARSWITHRUBBISH */
1497 // 2000-01-09 CmdList, CvarList commands By Matthias "Maddes" Buecher
1498 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
1499 Cmd_AddCommand ("cmdlist", Cmd_List_f, "lists all console commands beginning with the specified prefix or matching the specified wildcard pattern");
1500 Cmd_AddCommand ("cvarlist", Cvar_List_f, "lists all console variables beginning with the specified prefix or matching the specified wildcard pattern");
1501 Cmd_AddCommand ("apropos", Cmd_Apropos_f, "lists all console variables/commands/aliases containing the specified string in the name or description");
1503 Cmd_AddCommand ("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");
1504 Cmd_AddCommand ("cvar_resettodefaults_all", Cvar_ResetToDefaults_All_f, "sets all cvars to their locked default values");
1505 Cmd_AddCommand ("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)");
1506 Cmd_AddCommand ("cvar_resettodefaults_saveonly", Cvar_ResetToDefaults_SaveOnly_f, "sets all saved cvars to their locked default values (variables that will be saved to config.cfg)");
1508 Cmd_AddCommand ("cprint", Cmd_Centerprint_f, "print something at the screen center");
1509 Cmd_AddCommand ("defer", Cmd_Defer_f, "execute a command in the future");
1512 // Support Doom3-style Toggle Command
1513 Cmd_AddCommand( "toggle", Cmd_Toggle_f, "toggles a console variable's values (use for more info)");
1521 void Cmd_Shutdown(void)
1525 // we usually have this locked when we get here from Host_Quit_f
1526 Cbuf_UnlockThreadMutex();
1527 Thread_DestroyMutex(cmd_text_mutex);
1529 cmd_text_mutex = NULL;
1531 Mem_FreePool(&cmd_mempool);
1549 const char *Cmd_Argv (int arg)
1551 if (arg >= cmd_argc )
1552 return cmd_null_string;
1553 return cmd_argv[arg];
1561 const char *Cmd_Args (void)
1571 Parses the given string into command line tokens.
1574 // AK: This function should only be called from ExcuteString because the current design is a bit of an hack
1575 static void Cmd_TokenizeString (const char *text)
1584 // skip whitespace up to a /n
1585 while (*text && ISWHITESPACE(*text) && *text != '\r' && *text != '\n')
1592 if (*text == '\n' || *text == '\r')
1594 // a newline separates commands in the buffer
1595 if (*text == '\r' && text[1] == '\n')
1607 if (!COM_ParseToken_Console(&text))
1610 if (cmd_argc < MAX_ARGS)
1612 l = (int)strlen(com_token) + 1;
1613 if (cmd_tokenizebufferpos + l > CMD_TOKENIZELENGTH)
1615 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguements\n", CMD_TOKENIZELENGTH);
1618 memcpy (cmd_tokenizebuffer + cmd_tokenizebufferpos, com_token, l);
1619 cmd_argv[cmd_argc] = cmd_tokenizebuffer + cmd_tokenizebufferpos;
1620 cmd_tokenizebufferpos += l;
1632 void Cmd_AddCommand_WithClientCommand (const char *cmd_name, xcommand_t consolefunction, xcommand_t clientfunction, const char *description)
1634 cmd_function_t *cmd;
1635 cmd_function_t *prev, *current;
1637 // fail if the command is a variable name
1638 if (Cvar_FindVar( cmd_name ))
1640 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
1644 // fail if the command already exists
1645 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1647 if (!strcmp (cmd_name, cmd->name))
1649 if (consolefunction || clientfunction)
1651 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
1656 cmd->csqcfunc = true;
1662 cmd = (cmd_function_t *)Mem_Alloc(cmd_mempool, sizeof(cmd_function_t));
1663 cmd->name = cmd_name;
1664 cmd->consolefunction = consolefunction;
1665 cmd->clientfunction = clientfunction;
1666 cmd->description = description;
1667 if(!consolefunction && !clientfunction) //[515]: csqc
1668 cmd->csqcfunc = true;
1669 cmd->next = cmd_functions;
1671 // insert it at the right alphanumeric position
1672 for( prev = NULL, current = cmd_functions ; current && strcmp( current->name, cmd->name ) < 0 ; prev = current, current = current->next )
1677 cmd_functions = cmd;
1679 cmd->next = current;
1682 void Cmd_AddCommand (const char *cmd_name, xcommand_t function, const char *description)
1684 Cmd_AddCommand_WithClientCommand (cmd_name, function, NULL, description);
1692 qboolean Cmd_Exists (const char *cmd_name)
1694 cmd_function_t *cmd;
1696 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1697 if (!strcmp (cmd_name,cmd->name))
1709 const char *Cmd_CompleteCommand (const char *partial)
1711 cmd_function_t *cmd;
1714 len = strlen(partial);
1720 for (cmd = cmd_functions; cmd; cmd = cmd->next)
1721 if (!strncasecmp(partial, cmd->name, len))
1728 Cmd_CompleteCountPossible
1730 New function for tab-completion system
1731 Added by EvilTypeGuy
1732 Thanks to Fett erich@heintz.com
1736 int Cmd_CompleteCountPossible (const char *partial)
1738 cmd_function_t *cmd;
1743 len = strlen(partial);
1748 // Loop through the command list and count all partial matches
1749 for (cmd = cmd_functions; cmd; cmd = cmd->next)
1750 if (!strncasecmp(partial, cmd->name, len))
1757 Cmd_CompleteBuildList
1759 New function for tab-completion system
1760 Added by EvilTypeGuy
1761 Thanks to Fett erich@heintz.com
1765 const char **Cmd_CompleteBuildList (const char *partial)
1767 cmd_function_t *cmd;
1770 size_t sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (const char *);
1773 len = strlen(partial);
1774 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
1775 // Loop through the alias list and print all matches
1776 for (cmd = cmd_functions; cmd; cmd = cmd->next)
1777 if (!strncasecmp(partial, cmd->name, len))
1778 buf[bpos++] = cmd->name;
1784 // written by LordHavoc
1785 void Cmd_CompleteCommandPrint (const char *partial)
1787 cmd_function_t *cmd;
1788 size_t len = strlen(partial);
1789 // Loop through the command list and print all matches
1790 for (cmd = cmd_functions; cmd; cmd = cmd->next)
1791 if (!strncasecmp(partial, cmd->name, len))
1792 Con_Printf("^2%s^7: %s\n", cmd->name, cmd->description);
1798 New function for tab-completion system
1799 Added by EvilTypeGuy
1800 Thanks to Fett erich@heintz.com
1804 const char *Cmd_CompleteAlias (const char *partial)
1809 len = strlen(partial);
1815 for (alias = cmd_alias; alias; alias = alias->next)
1816 if (!strncasecmp(partial, alias->name, len))
1822 // written by LordHavoc
1823 void Cmd_CompleteAliasPrint (const char *partial)
1826 size_t len = strlen(partial);
1827 // Loop through the alias list and print all matches
1828 for (alias = cmd_alias; alias; alias = alias->next)
1829 if (!strncasecmp(partial, alias->name, len))
1830 Con_Printf("^5%s^7: %s", alias->name, alias->value);
1835 Cmd_CompleteAliasCountPossible
1837 New function for tab-completion system
1838 Added by EvilTypeGuy
1839 Thanks to Fett erich@heintz.com
1843 int Cmd_CompleteAliasCountPossible (const char *partial)
1851 len = strlen(partial);
1856 // Loop through the command list and count all partial matches
1857 for (alias = cmd_alias; alias; alias = alias->next)
1858 if (!strncasecmp(partial, alias->name, len))
1865 Cmd_CompleteAliasBuildList
1867 New function for tab-completion system
1868 Added by EvilTypeGuy
1869 Thanks to Fett erich@heintz.com
1873 const char **Cmd_CompleteAliasBuildList (const char *partial)
1878 size_t sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (const char *);
1881 len = strlen(partial);
1882 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
1883 // Loop through the alias list and print all matches
1884 for (alias = cmd_alias; alias; alias = alias->next)
1885 if (!strncasecmp(partial, alias->name, len))
1886 buf[bpos++] = alias->name;
1892 void Cmd_ClearCsqcFuncs (void)
1894 cmd_function_t *cmd;
1895 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1896 cmd->csqcfunc = false;
1903 A complete command line has been parsed, so try to execute it
1904 FIXME: lookupnoadd the token to speed search?
1907 void Cmd_ExecuteString (const char *text, cmd_source_t src, qboolean lockmutex)
1911 cmd_function_t *cmd;
1914 Cbuf_LockThreadMutex();
1915 oldpos = cmd_tokenizebufferpos;
1919 Cmd_TokenizeString (text);
1921 // execute the command line
1923 goto done; // no tokens
1926 for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1928 if (!strcasecmp (cmd_argv[0],cmd->name))
1930 if (cmd->csqcfunc && CL_VM_ConsoleCommand (text)) //[515]: csqc
1935 if (cmd->consolefunction)
1936 cmd->consolefunction ();
1937 else if (cmd->clientfunction)
1939 if (cls.state == ca_connected)
1941 // forward remote commands to the server for execution
1942 Cmd_ForwardToServer();
1945 Con_Printf("Can not send command \"%s\", not connected.\n", Cmd_Argv(0));
1948 Con_Printf("Command \"%s\" can not be executed\n", Cmd_Argv(0));
1952 if (cmd->clientfunction)
1954 cmd->clientfunction ();
1964 // if it's a client command and no command was found, say so.
1965 if (cmd_source == src_client)
1967 Con_Printf("player \"%s\" tried to %s\n", host_client->name, text);
1972 for (a=cmd_alias ; a ; a=a->next)
1974 if (!strcasecmp (cmd_argv[0], a->name))
1976 Cmd_ExecuteAlias(a);
1981 if(found) // if the command was hooked and found, all is good
1985 if (!Cvar_Command () && host_framecount > 0)
1986 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(0));
1989 cmd_tokenizebufferpos = oldpos;
1991 Cbuf_UnlockThreadMutex();
1997 Cmd_ForwardStringToServer
1999 Sends an entire command string over to the server, unprocessed
2002 void Cmd_ForwardStringToServer (const char *s)
2005 if (cls.state != ca_connected)
2007 Con_Printf("Can't \"%s\", not connected\n", s);
2014 // LordHavoc: thanks to Fuh for bringing the pure evil of SZ_Print to my
2015 // attention, it has been eradicated from here, its only (former) use in
2016 // all of darkplaces.
2017 if (cls.protocol == PROTOCOL_QUAKEWORLD)
2018 MSG_WriteByte(&cls.netcon->message, qw_clc_stringcmd);
2020 MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
2021 if ((!strncmp(s, "say ", 4) || !strncmp(s, "say_team ", 9)) && cl_locs_enable.integer)
2023 // say/say_team commands can replace % character codes with status info
2026 if (*s == '%' && s[1])
2028 // handle proquake message macros
2032 case 'l': // current location
2033 CL_Locs_FindLocationName(temp, sizeof(temp), cl.movement_origin);
2035 case 'h': // current health
2036 dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_HEALTH]);
2038 case 'a': // current armor
2039 dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_ARMOR]);
2041 case 'x': // current rockets
2042 dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_ROCKETS]);
2044 case 'c': // current cells
2045 dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_CELLS]);
2047 // silly proquake macros
2048 case 'd': // loc at last death
2049 CL_Locs_FindLocationName(temp, sizeof(temp), cl.lastdeathorigin);
2051 case 't': // current time
2052 dpsnprintf(temp, sizeof(temp), "%.0f:%.0f", floor(cl.time / 60), cl.time - floor(cl.time / 60) * 60);
2054 case 'r': // rocket launcher status ("I have RL", "I need rockets", "I need RL")
2055 if (!(cl.stats[STAT_ITEMS] & IT_ROCKET_LAUNCHER))
2056 dpsnprintf(temp, sizeof(temp), "I need RL");
2057 else if (!cl.stats[STAT_ROCKETS])
2058 dpsnprintf(temp, sizeof(temp), "I need rockets");
2060 dpsnprintf(temp, sizeof(temp), "I have RL");
2062 case 'p': // powerup status (outputs "quad" "pent" and "eyes" according to status)
2063 if (cl.stats[STAT_ITEMS] & IT_QUAD)
2066 strlcat(temp, " ", sizeof(temp));
2067 strlcat(temp, "quad", sizeof(temp));
2069 if (cl.stats[STAT_ITEMS] & IT_INVULNERABILITY)
2072 strlcat(temp, " ", sizeof(temp));
2073 strlcat(temp, "pent", sizeof(temp));
2075 if (cl.stats[STAT_ITEMS] & IT_INVISIBILITY)
2078 strlcat(temp, " ", sizeof(temp));
2079 strlcat(temp, "eyes", sizeof(temp));
2082 case 'w': // weapon status (outputs "SSG:NG:SNG:GL:RL:LG" with the text between : characters omitted if you lack the weapon)
2083 if (cl.stats[STAT_ITEMS] & IT_SUPER_SHOTGUN)
2084 strlcat(temp, "SSG", sizeof(temp));
2085 strlcat(temp, ":", sizeof(temp));
2086 if (cl.stats[STAT_ITEMS] & IT_NAILGUN)
2087 strlcat(temp, "NG", sizeof(temp));
2088 strlcat(temp, ":", sizeof(temp));
2089 if (cl.stats[STAT_ITEMS] & IT_SUPER_NAILGUN)
2090 strlcat(temp, "SNG", sizeof(temp));
2091 strlcat(temp, ":", sizeof(temp));
2092 if (cl.stats[STAT_ITEMS] & IT_GRENADE_LAUNCHER)
2093 strlcat(temp, "GL", sizeof(temp));
2094 strlcat(temp, ":", sizeof(temp));
2095 if (cl.stats[STAT_ITEMS] & IT_ROCKET_LAUNCHER)
2096 strlcat(temp, "RL", sizeof(temp));
2097 strlcat(temp, ":", sizeof(temp));
2098 if (cl.stats[STAT_ITEMS] & IT_LIGHTNING)
2099 strlcat(temp, "LG", sizeof(temp));
2102 // not a recognized macro, print it as-is...
2108 // write the resulting text
2109 SZ_Write(&cls.netcon->message, (unsigned char *)temp, strlen(temp));
2113 MSG_WriteByte(&cls.netcon->message, *s);
2116 MSG_WriteByte(&cls.netcon->message, 0);
2118 else // any other command is passed on as-is
2119 SZ_Write(&cls.netcon->message, (const unsigned char *)s, (int)strlen(s) + 1);
2126 Sends the entire command line over to the server
2129 void Cmd_ForwardToServer (void)
2133 if (!strcasecmp(Cmd_Argv(0), "cmd"))
2135 // we want to strip off "cmd", so just send the args
2136 s = Cmd_Argc() > 1 ? Cmd_Args() : "";
2140 // we need to keep the command name, so send Cmd_Argv(0), a space and then Cmd_Args()
2141 s = va(vabuf, sizeof(vabuf), "%s %s", Cmd_Argv(0), Cmd_Argc() > 1 ? Cmd_Args() : "");
2143 // don't send an empty forward message if the user tries "cmd" by itself
2146 Cmd_ForwardStringToServer(s);
2154 Returns the position (1 to argc-1) in the command's argument list
2155 where the given parameter apears, or 0 if not present
2159 int Cmd_CheckParm (const char *parm)
2165 Con_Printf ("Cmd_CheckParm: NULL");
2169 for (i = 1; i < Cmd_Argc (); i++)
2170 if (!strcasecmp (parm, Cmd_Argv (i)))
2178 void Cmd_SaveInitState(void)
2182 for (f = cmd_functions;f;f = f->next)
2183 f->initstate = true;
2184 for (a = cmd_alias;a;a = a->next)
2186 a->initstate = true;
2187 a->initialvalue = Mem_strdup(zonemempool, a->value);
2189 Cvar_SaveInitState();
2192 void Cmd_RestoreInitState(void)
2194 cmd_function_t *f, **fp;
2195 cmdalias_t *a, **ap;
2196 for (fp = &cmd_functions;(f = *fp);)
2202 // destroy this command, it didn't exist at init
2203 Con_DPrintf("Cmd_RestoreInitState: Destroying command %s\n", f->name);
2208 for (ap = &cmd_alias;(a = *ap);)
2212 // restore this alias, it existed at init
2213 if (strcmp(a->value ? a->value : "", a->initialvalue ? a->initialvalue : ""))
2215 Con_DPrintf("Cmd_RestoreInitState: Restoring alias %s\n", a->name);
2218 a->value = Mem_strdup(zonemempool, a->initialvalue);
2224 // free this alias, it didn't exist at init...
2225 Con_DPrintf("Cmd_RestoreInitState: Destroying alias %s\n", a->name);
2232 Cvar_RestoreInitState();