]> icculus.org git repositories - divverent/darkplaces.git/blob - cmd.c
allow multiple rcon commands in the same packet, separated by NUL
[divverent/darkplaces.git] / cmd.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // cmd.c -- Quake script command processing module
21
22 #include "quakedef.h"
23
24 #define MAX_ALIAS_NAME  32
25 // this is the largest script file that can be executed in one step
26 // LordHavoc: inreased this from 8192 to 32768
27 #define CMDBUFSIZE 32768
28 // maximum number of parameters to a command
29 #define MAX_ARGS 80
30 // maximum tokenizable commandline length (counting NUL terminations)
31 #define CMD_TOKENIZELENGTH (MAX_INPUTLINE + 80)
32
33 typedef struct cmdalias_s
34 {
35         struct cmdalias_s *next;
36         char name[MAX_ALIAS_NAME];
37         char *value;
38 } cmdalias_t;
39
40 static cmdalias_t *cmd_alias;
41
42 static qboolean cmd_wait;
43
44 static mempool_t *cmd_mempool;
45
46 static char cmd_tokenizebuffer[CMD_TOKENIZELENGTH];
47 static int cmd_tokenizebufferpos = 0;
48
49 //=============================================================================
50
51 /*
52 ============
53 Cmd_Wait_f
54
55 Causes execution of the remainder of the command buffer to be delayed until
56 next frame.  This allows commands like:
57 bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
58 ============
59 */
60 static void Cmd_Wait_f (void)
61 {
62         cmd_wait = true;
63 }
64
65 /*
66 =============================================================================
67
68                                                 COMMAND BUFFER
69
70 =============================================================================
71 */
72
73 static sizebuf_t        cmd_text;
74 static unsigned char            cmd_text_buf[CMDBUFSIZE];
75
76 /*
77 ============
78 Cbuf_AddText
79
80 Adds command text at the end of the buffer
81 ============
82 */
83 void Cbuf_AddText (const char *text)
84 {
85         int             l;
86
87         l = (int)strlen (text);
88
89         if (cmd_text.cursize + l >= cmd_text.maxsize)
90         {
91                 Con_Print("Cbuf_AddText: overflow\n");
92                 return;
93         }
94
95         SZ_Write (&cmd_text, (const unsigned char *)text, (int)strlen (text));
96 }
97
98
99 /*
100 ============
101 Cbuf_InsertText
102
103 Adds command text immediately after the current command
104 Adds a \n to the text
105 FIXME: actually change the command buffer to do less copying
106 ============
107 */
108 void Cbuf_InsertText (const char *text)
109 {
110         char    *temp;
111         int             templen;
112
113         // copy off any commands still remaining in the exec buffer
114         templen = cmd_text.cursize;
115         if (templen)
116         {
117                 temp = (char *)Mem_Alloc (tempmempool, templen);
118                 memcpy (temp, cmd_text.data, templen);
119                 SZ_Clear (&cmd_text);
120         }
121         else
122                 temp = NULL;
123
124         // add the entire text of the file
125         Cbuf_AddText (text);
126
127         // add the copied off data
128         if (temp != NULL)
129         {
130                 SZ_Write (&cmd_text, (const unsigned char *)temp, templen);
131                 Mem_Free (temp);
132         }
133 }
134
135 /*
136 ============
137 Cbuf_Execute
138 ============
139 */
140 static void Cmd_PreprocessString( const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias );
141 void Cbuf_Execute (void)
142 {
143         int i;
144         char *text;
145         char line[MAX_INPUTLINE];
146         char preprocessed[MAX_INPUTLINE];
147         char *firstchar;
148         int quotes;
149
150         // LordHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
151         cmd_tokenizebufferpos = 0;
152
153         while (cmd_text.cursize)
154         {
155 // find a \n or ; line break
156                 text = (char *)cmd_text.data;
157
158                 quotes = 0;
159                 for (i=0 ; i< cmd_text.cursize ; i++)
160                 {
161                         if (text[i] == '"')
162                                 quotes ^= 1;
163                         if (text[i] == '\\' && (text[i+1] == '"' || text[i+1] == '\\'))
164                                 i++;
165                         if ( !quotes &&  text[i] == ';')
166                                 break;  // don't break if inside a quoted string
167                         if (text[i] == '\r' || text[i] == '\n')
168                                 break;
169                 }
170
171                 memcpy (line, text, i);
172                 line[i] = 0;
173
174 // delete the text from the command buffer and move remaining commands down
175 // this is necessary because commands (exec, alias) can insert data at the
176 // beginning of the text buffer
177
178                 if (i == cmd_text.cursize)
179                         cmd_text.cursize = 0;
180                 else
181                 {
182                         i++;
183                         cmd_text.cursize -= i;
184                         memcpy (cmd_text.data, text+i, cmd_text.cursize);
185                 }
186
187 // execute the command line
188                 firstchar = line + strspn(line, " \t");
189                 if(strncmp(firstchar, "alias", 5) || (firstchar[5] != ' ' && firstchar[5] != '\t'))
190                 {
191                         Cmd_PreprocessString( line, preprocessed, sizeof(preprocessed), NULL );
192                         Cmd_ExecuteString (preprocessed, src_command);
193                 }
194                 else
195                 {
196                         Cmd_ExecuteString (line, src_command);
197                 }
198
199                 if (cmd_wait)
200                 {       // skip out while text still remains in buffer, leaving it
201                         // for next frame
202                         cmd_wait = false;
203                         break;
204                 }
205         }
206 }
207
208 /*
209 ==============================================================================
210
211                                                 SCRIPT COMMANDS
212
213 ==============================================================================
214 */
215
216 /*
217 ===============
218 Cmd_StuffCmds_f
219
220 Adds command line parameters as script statements
221 Commands lead with a +, and continue until a - or another +
222 quake +prog jctest.qp +cmd amlev1
223 quake -nosound +cmd amlev1
224 ===============
225 */
226 qboolean host_stuffcmdsrun = false;
227 void Cmd_StuffCmds_f (void)
228 {
229         int             i, j, l;
230         // this is for all commandline options combined (and is bounds checked)
231         char    build[MAX_INPUTLINE];
232
233         if (Cmd_Argc () != 1)
234         {
235                 Con_Print("stuffcmds : execute command line parameters\n");
236                 return;
237         }
238
239         // no reason to run the commandline arguments twice
240         if (host_stuffcmdsrun)
241                 return;
242
243         host_stuffcmdsrun = true;
244         build[0] = 0;
245         l = 0;
246         for (i = 0;i < com_argc;i++)
247         {
248                 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)
249                 {
250                         j = 1;
251                         while (com_argv[i][j])
252                                 build[l++] = com_argv[i][j++];
253                         i++;
254                         for (;i < com_argc;i++)
255                         {
256                                 if (!com_argv[i])
257                                         continue;
258                                 if ((com_argv[i][0] == '+' || com_argv[i][0] == '-') && (com_argv[i][1] < '0' || com_argv[i][1] > '9'))
259                                         break;
260                                 if (l + strlen(com_argv[i]) + 4 > sizeof(build) - 1)
261                                         break;
262                                 build[l++] = ' ';
263                                 if (strchr(com_argv[i], ' '))
264                                         build[l++] = '\"';
265                                 for (j = 0;com_argv[i][j];j++)
266                                         build[l++] = com_argv[i][j];
267                                 if (strchr(com_argv[i], ' '))
268                                         build[l++] = '\"';
269                         }
270                         build[l++] = '\n';
271                         i--;
272                 }
273         }
274         // now terminate the combined string and prepend it to the command buffer
275         // we already reserved space for the terminator
276         build[l++] = 0;
277         Cbuf_InsertText (build);
278 }
279
280
281 /*
282 ===============
283 Cmd_Exec_f
284 ===============
285 */
286 static void Cmd_Exec_f (void)
287 {
288         char *f;
289
290         if (Cmd_Argc () != 2)
291         {
292                 Con_Print("exec <filename> : execute a script file\n");
293                 return;
294         }
295
296         f = (char *)FS_LoadFile (Cmd_Argv(1), tempmempool, false, NULL);
297         if (!f)
298         {
299                 Con_Printf("couldn't exec %s\n",Cmd_Argv(1));
300                 return;
301         }
302         Con_DPrintf("execing %s\n",Cmd_Argv(1));
303
304         // if executing default.cfg for the first time, lock the cvar defaults
305         // it may seem backwards to insert this text BEFORE the default.cfg
306         // but Cbuf_InsertText inserts before, so this actually ends up after it.
307         if (!strcmp(Cmd_Argv(1), "default.cfg"))
308                 Cbuf_InsertText("\ncvar_lockdefaults\n");
309
310         // insert newline after the text to make sure the last line is terminated (some text editors omit the trailing newline)
311         // (note: insertion order here is backwards from execution order, so this adds it after the text, by calling it before...)
312         Cbuf_InsertText ("\n");
313         Cbuf_InsertText (f);
314         Mem_Free(f);
315 }
316
317
318 /*
319 ===============
320 Cmd_Echo_f
321
322 Just prints the rest of the line to the console
323 ===============
324 */
325 static void Cmd_Echo_f (void)
326 {
327         int             i;
328
329         for (i=1 ; i<Cmd_Argc() ; i++)
330                 Con_Printf("%s ",Cmd_Argv(i));
331         Con_Print("\n");
332 }
333
334 // DRESK - 5/14/06
335 // Support Doom3-style Toggle Console Command
336 /*
337 ===============
338 Cmd_Toggle_f
339
340 Toggles a specified console variable amongst the values specified (default is 0 and 1)
341 ===============
342 */
343 static void Cmd_Toggle_f(void)
344 {
345         // Acquire Number of Arguments
346         int nNumArgs = Cmd_Argc();
347
348         if(nNumArgs == 1)
349                 // No Arguments Specified; Print Usage
350                 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");
351         else
352         { // Correct Arguments Specified
353                 // Acquire Potential CVar
354                 cvar_t* cvCVar = Cvar_FindVar( Cmd_Argv(1) );
355
356                 if(cvCVar != NULL)
357                 { // Valid CVar
358                         if(nNumArgs == 2)
359                         { // Default Usage
360                                 if(cvCVar->integer)
361                                         Cvar_SetValueQuick(cvCVar, 0);
362                                 else
363                                         Cvar_SetValueQuick(cvCVar, 1);
364                         }
365                         else
366                         if(nNumArgs == 3)
367                         { // 0 and Specified Usage
368                                 if(cvCVar->integer == atoi(Cmd_Argv(2) ) )
369                                         // CVar is Specified Value; // Reset to 0
370                                         Cvar_SetValueQuick(cvCVar, 0);
371                                 else
372                                 if(cvCVar->integer == 0)
373                                         // CVar is 0; Specify Value
374                                         Cvar_SetQuick(cvCVar, Cmd_Argv(2) );
375                                 else
376                                         // CVar does not match; Reset to 0
377                                         Cvar_SetValueQuick(cvCVar, 0);
378                         }
379                         else
380                         { // Variable Values Specified
381                                 int nCnt;
382                                 int bFound = 0;
383
384                                 for(nCnt = 2; nCnt < nNumArgs; nCnt++)
385                                 { // Cycle through Values
386                                         if( strcmp(cvCVar->string, Cmd_Argv(nCnt) ) == 0)
387                                         { // Current Value Located; Increment to Next
388                                                 if( (nCnt + 1) == nNumArgs)
389                                                         // Max Value Reached; Reset
390                                                         Cvar_SetQuick(cvCVar, Cmd_Argv(2) );
391                                                 else
392                                                         // Next Value
393                                                         Cvar_SetQuick(cvCVar, Cmd_Argv(nCnt + 1) );
394
395                                                 // End Loop
396                                                 nCnt = nNumArgs;
397                                                 // Assign Found
398                                                 bFound = 1;
399                                         }
400                                 }
401                                 if(!bFound)
402                                         // Value not Found; Reset to Original
403                                         Cvar_SetQuick(cvCVar, Cmd_Argv(2) );
404                         }
405
406                 }
407                 else
408                 { // Invalid CVar
409                         Con_Printf("ERROR : CVar '%s' not found\n", Cmd_Argv(2) );
410                 }
411         }
412 }
413
414 /*
415 ===============
416 Cmd_Alias_f
417
418 Creates a new command that executes a command string (possibly ; seperated)
419 ===============
420 */
421 static void Cmd_Alias_f (void)
422 {
423         cmdalias_t      *a;
424         char            cmd[MAX_INPUTLINE];
425         int                     i, c;
426         const char              *s;
427         size_t          alloclen;
428
429         if (Cmd_Argc() == 1)
430         {
431                 Con_Print("Current alias commands:\n");
432                 for (a = cmd_alias ; a ; a=a->next)
433                         Con_Printf("%s : %s\n", a->name, a->value);
434                 return;
435         }
436
437         s = Cmd_Argv(1);
438         if (strlen(s) >= MAX_ALIAS_NAME)
439         {
440                 Con_Print("Alias name is too long\n");
441                 return;
442         }
443
444         // if the alias already exists, reuse it
445         for (a = cmd_alias ; a ; a=a->next)
446         {
447                 if (!strcmp(s, a->name))
448                 {
449                         Z_Free (a->value);
450                         break;
451                 }
452         }
453
454         if (!a)
455         {
456                 cmdalias_t *prev, *current;
457
458                 a = (cmdalias_t *)Z_Malloc (sizeof(cmdalias_t));
459                 strlcpy (a->name, s, sizeof (a->name));
460                 // insert it at the right alphanumeric position
461                 for( prev = NULL, current = cmd_alias ; current && strcmp( current->name, a->name ) < 0 ; prev = current, current = current->next )
462                         ;
463                 if( prev ) {
464                         prev->next = a;
465                 } else {
466                         cmd_alias = a;
467                 }
468                 a->next = current;
469         }
470
471
472 // copy the rest of the command line
473         cmd[0] = 0;             // start out with a null string
474         c = Cmd_Argc();
475         for (i=2 ; i< c ; i++)
476         {
477                 strlcat (cmd, Cmd_Argv(i), sizeof (cmd));
478                 if (i != c)
479                         strlcat (cmd, " ", sizeof (cmd));
480         }
481         strlcat (cmd, "\n", sizeof (cmd));
482
483         alloclen = strlen (cmd) + 1;
484         a->value = (char *)Z_Malloc (alloclen);
485         memcpy (a->value, cmd, alloclen);
486 }
487
488 /*
489 =============================================================================
490
491                                         COMMAND EXECUTION
492
493 =============================================================================
494 */
495
496 typedef struct cmd_function_s
497 {
498         struct cmd_function_s *next;
499         const char *name;
500         const char *description;
501         xcommand_t consolefunction;
502         xcommand_t clientfunction;
503         qboolean csqcfunc;
504 } cmd_function_t;
505
506 static int cmd_argc;
507 static const char *cmd_argv[MAX_ARGS];
508 static const char *cmd_null_string = "";
509 static const char *cmd_args;
510 cmd_source_t cmd_source;
511
512
513 static cmd_function_t *cmd_functions;           // possible commands to execute
514
515 static const char *Cmd_GetDirectCvarValue(const char *varname, cmdalias_t *alias)
516 {
517         cvar_t *cvar;
518         long argno;
519         char *endptr;
520
521         if(!varname || !*varname)
522                 return NULL;
523
524         if(alias)
525         {
526                 if(!strcmp(varname, "*"))
527                         return Cmd_Args();
528                 else
529                 {
530                         argno = strtol(varname, &endptr, 10);
531                         if(*endptr == 0)
532                         {
533                                 // whole string is a number
534                                 // NOTE: we already made sure we don't have an empty cvar name!
535                                 if(argno >= 0 && argno < Cmd_Argc())
536                                         return Cmd_Argv(argno);
537                         }
538                 }
539         }
540
541         if((cvar = Cvar_FindVar(varname)) && !(cvar->flags & CVAR_PRIVATE))
542                 return cvar->string;
543
544         return NULL;
545 }
546
547 static const char *Cmd_GetCvarValue(const char *var, size_t varlen, cmdalias_t *alias)
548 {
549         static char varname[MAX_INPUTLINE];
550         static char varval[MAX_INPUTLINE];
551         char *p;
552         const char *varstr;
553         char *varfunc;
554
555         if(varlen >= MAX_INPUTLINE)
556                 varlen = MAX_INPUTLINE - 1;
557         memcpy(varname, var, varlen);
558         varname[varlen] = 0;
559         varfunc = strchr(varname, ' ');
560
561         if(varfunc)
562         {
563                 *varfunc = 0;
564                 ++varfunc;
565         }
566
567         if(*var == 0)
568         {
569                 // empty cvar name?
570                 return NULL;
571         }
572
573         varstr = NULL;
574
575         // Exception: $* doesn't use the quoted form by default
576         if(!strcmp(varname, "*"))
577                 varfunc = "asis";
578
579         if(varname[0] == '$')
580                 varstr = Cmd_GetDirectCvarValue(Cmd_GetDirectCvarValue(varname + 1, alias), alias);
581         else
582                 varstr = Cmd_GetDirectCvarValue(varname, alias);
583
584         if(!varstr)
585         {
586                 if(alias)
587                         Con_Printf("Warning: Could not expand $%s in alias %s\n", varname, alias->name);
588                 else
589                         Con_Printf("Warning: Could not expand $%s\n", varname);
590                 return NULL;
591         }
592
593         if(!varfunc || !strcmp(varfunc, "q")) // note: quoted form is default, use "asis" to override!
594         {
595                 // quote it so it can be used inside double quotes
596                 // we just need to replace " by \"
597                 p = varval;
598                 while(*varstr)
599                 {
600                         if(*varstr == '"')
601                         {
602                                 if(p - varval >= (ssize_t)(sizeof(varval) - 2))
603                                         break;
604                                 *p++ = '\\';
605                                 *p++ = '"';
606                         }
607                         else if(*varstr == '\\')
608                         {
609                                 if(p - varval >= (ssize_t)(sizeof(varval) - 2))
610                                         break;
611                                 *p++ = '\\';
612                                 *p++ = '\\';
613                         }
614                         else
615                         {
616                                 if(p - varval >= (ssize_t)(sizeof(varval) - 1))
617                                         break;
618                                 *p++ = *varstr;
619                         }
620                         ++varstr;
621                 }
622                 *p++ = 0;
623                 //Con_Printf("quoted form: %s\n", varval);
624                 return varval;
625         }
626         else if(!strcmp(varfunc, "asis"))
627         {
628                 return varstr;
629         }
630         else
631                 Con_Printf("Unknown variable function %s\n", varfunc);
632
633         return varstr;
634 }
635
636 /*
637 Cmd_PreprocessString
638
639 Preprocesses strings and replaces $*, $param#, $cvar accordingly
640 */
641 static void Cmd_PreprocessString( const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias ) {
642         const char *in;
643         size_t eat, varlen;
644         unsigned outlen;
645         const char *val;
646
647         // don't crash if there's no room in the outtext buffer
648         if( maxoutlen == 0 ) {
649                 return;
650         }
651         maxoutlen--; // because of \0
652
653         in = intext;
654         outlen = 0;
655
656         while( *in && outlen < maxoutlen ) {
657                 if( *in == '$' ) {
658                         // this is some kind of expansion, see what comes after the $
659                         in++;
660
661                         // The console does the following preprocessing:
662                         //
663                         // - $$ is transformed to a single dollar sign.
664                         // - $var or ${var} are expanded to the contents of the named cvar,
665                         //   with quotation marks and backslashes quoted so it can safely
666                         //   be used inside quotation marks (and it should always be used
667                         //   that way)
668                         // - ${var asis} inserts the cvar value as is, without doing this
669                         //   quoting
670                         // - prefix the cvar name with a dollar sign to do indirection;
671                         //   for example, if $x has the value timelimit, ${$x} will return
672                         //   the value of $timelimit
673                         // - when expanding an alias, the special variable name $* refers
674                         //   to all alias parameters, and a number refers to that numbered
675                         //   alias parameter, where the name of the alias is $0, the first
676                         //   parameter is $1 and so on; as a special case, $* inserts all
677                         //   parameters, without extra quoting, so one can use $* to just
678                         //   pass all parameters around
679                         //
680                         // Note: when expanding an alias, cvar expansion is done in the SAME step
681                         // as alias expansion so that alias parameters or cvar values containing
682                         // dollar signs have no unwanted bad side effects. However, this needs to
683                         // be accounted for when writing complex aliases. For example,
684                         //   alias foo "set x NEW; echo $x"
685                         // actually expands to
686                         //   "set x NEW; echo OLD"
687                         // and will print OLD! To work around this, use a second alias:
688                         //   alias foo "set x NEW; foo2"
689                         //   alias foo2 "echo $x"
690                         //
691                         // Also note: lines starting with alias are exempt from cvar expansion.
692                         // If you want cvar expansion, write "alias" instead:
693                         //
694                         //   set x 1
695                         //   alias foo "echo $x"
696                         //   "alias" bar "echo $x"
697                         //   set x 2
698                         //
699                         // foo will print 2, because the variable $x will be expanded when the alias
700                         // gets expanded. bar will print 1, because the variable $x was expanded
701                         // at definition time. foo can be equivalently defined as
702                         //
703                         //   "alias" foo "echo $$x"
704                         //
705                         // because at definition time, $$ will get replaced to a single $.
706
707                         if( *in == '$' ) {
708                                 val = "$";
709                                 eat = 1;
710                         } else if(*in == '{') {
711                                 varlen = strcspn(in + 1, "}");
712                                 if(in[varlen + 1] == '}')
713                                 {
714                                         val = Cmd_GetCvarValue(in + 1, varlen, alias);
715                                         eat = varlen + 2;
716                                 }
717                                 else
718                                 {
719                                         // ran out of data?
720                                         val = NULL;
721                                         eat = varlen + 1;
722                                 }
723                         } else {
724                                 varlen = strspn(in, "*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_");
725                                 val = Cmd_GetCvarValue(in, varlen, alias);
726                                 eat = varlen;
727                         }
728                         if(val)
729                         {
730                                 // insert the cvar value
731                                 while(*val && outlen < maxoutlen)
732                                         outtext[outlen++] = *val++;
733                                 in += eat;
734                         }
735                         else
736                         {
737                                 // copy the unexpanded text
738                                 outtext[outlen++] = '$';
739                                 while(eat && outlen < maxoutlen)
740                                 {
741                                         outtext[outlen++] = *in++;
742                                         --eat;
743                                 }
744                         }
745                 } else {
746                         outtext[outlen++] = *in++;
747                 }
748         }
749         outtext[outlen] = 0;
750 }
751
752 /*
753 ============
754 Cmd_ExecuteAlias
755
756 Called for aliases and fills in the alias into the cbuffer
757 ============
758 */
759 static void Cmd_ExecuteAlias (cmdalias_t *alias)
760 {
761         static char buffer[ MAX_INPUTLINE + 2 ];
762         static char buffer2[ MAX_INPUTLINE * 2 + 2 ];
763         char *q;
764         const char *p;
765         Cmd_PreprocessString( alias->value, buffer, sizeof(buffer) - 2, alias );
766         // insert at start of command buffer, so that aliases execute in order
767         // (fixes bug introduced by Black on 20050705)
768         
769         // Note: Cbuf_PreprocessString will be called on this string AGAIN! So we
770         // have to make sure that no second variable expansion takes place, otherwise
771         // alias parameters containing dollar signs can have bad effects.
772         for(p = buffer, q = buffer2; *p; )
773         {
774                 if(*p == '$')
775                         *q++ = '$';
776                 *q++ = *p++;
777         }
778         *q++ = 0;
779
780         Cbuf_InsertText( buffer2 );
781 }
782
783 /*
784 ========
785 Cmd_List
786
787         CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
788         Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
789
790 ========
791 */
792 static void Cmd_List_f (void)
793 {
794         cmd_function_t *cmd;
795         const char *partial;
796         int len, count;
797
798         if (Cmd_Argc() > 1)
799         {
800                 partial = Cmd_Argv (1);
801                 len = (int)strlen(partial);
802         }
803         else
804         {
805                 partial = NULL;
806                 len = 0;
807         }
808
809         count = 0;
810         for (cmd = cmd_functions; cmd; cmd = cmd->next)
811         {
812                 if (partial && strncmp(partial, cmd->name, len))
813                         continue;
814                 Con_Printf("%s : %s\n", cmd->name, cmd->description);
815                 count++;
816         }
817
818         if (partial)
819                 Con_Printf("%i Command%s beginning with \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
820         else
821                 Con_Printf("%i Command%s\n\n", count, (count > 1) ? "s" : "");
822 }
823
824 /*
825 ============
826 Cmd_Init
827 ============
828 */
829 void Cmd_Init (void)
830 {
831         cmd_mempool = Mem_AllocPool("commands", 0, NULL);
832         // space for commands and script files
833         cmd_text.data = cmd_text_buf;
834         cmd_text.maxsize = sizeof(cmd_text_buf);
835         cmd_text.cursize = 0;
836 }
837
838 void Cmd_Init_Commands (void)
839 {
840 //
841 // register our commands
842 //
843         Cmd_AddCommand ("stuffcmds",Cmd_StuffCmds_f, "execute commandline parameters (must be present in quake.rc script)");
844         Cmd_AddCommand ("exec",Cmd_Exec_f, "execute a script file");
845         Cmd_AddCommand ("echo",Cmd_Echo_f, "print a message to the console (useful in scripts)");
846         Cmd_AddCommand ("alias",Cmd_Alias_f, "create a script function (parameters are passed in as $1 through $9, and $* for all parameters)");
847         Cmd_AddCommand ("cmd", Cmd_ForwardToServer, "send a console commandline to the server (used by some mods)");
848         Cmd_AddCommand ("wait", Cmd_Wait_f, "make script execution wait for next rendered frame");
849         Cmd_AddCommand ("set", Cvar_Set_f, "create or change the value of a console variable");
850         Cmd_AddCommand ("seta", Cvar_SetA_f, "create or change the value of a console variable that will be saved to config.cfg");
851
852         // 2000-01-09 CmdList, CvarList commands By Matthias "Maddes" Buecher
853         // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
854         Cmd_AddCommand ("cmdlist", Cmd_List_f, "lists all console commands beginning with the specified prefix");
855         Cmd_AddCommand ("cvarlist", Cvar_List_f, "lists all console variables beginning with the specified prefix");
856
857         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");
858         Cmd_AddCommand ("cvar_resettodefaults_all", Cvar_ResetToDefaults_All_f, "sets all cvars to their locked default values");
859         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)");
860         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)");
861
862         // DRESK - 5/14/06
863         // Support Doom3-style Toggle Command
864         Cmd_AddCommand( "toggle", Cmd_Toggle_f, "toggles a console variable's values (use for more info)");
865 }
866
867 /*
868 ============
869 Cmd_Shutdown
870 ============
871 */
872 void Cmd_Shutdown(void)
873 {
874         Mem_FreePool(&cmd_mempool);
875 }
876
877 /*
878 ============
879 Cmd_Argc
880 ============
881 */
882 int             Cmd_Argc (void)
883 {
884         return cmd_argc;
885 }
886
887 /*
888 ============
889 Cmd_Argv
890 ============
891 */
892 const char *Cmd_Argv (int arg)
893 {
894         if (arg >= cmd_argc )
895                 return cmd_null_string;
896         return cmd_argv[arg];
897 }
898
899 /*
900 ============
901 Cmd_Args
902 ============
903 */
904 const char *Cmd_Args (void)
905 {
906         return cmd_args;
907 }
908
909
910 /*
911 ============
912 Cmd_TokenizeString
913
914 Parses the given string into command line tokens.
915 ============
916 */
917 // AK: This function should only be called from ExcuteString because the current design is a bit of an hack
918 static void Cmd_TokenizeString (const char *text)
919 {
920         int l;
921
922         cmd_argc = 0;
923         cmd_args = NULL;
924
925         while (1)
926         {
927                 // skip whitespace up to a /n
928                 while (*text && *text <= ' ' && *text != '\r' && *text != '\n')
929                         text++;
930
931                 // line endings:
932                 // UNIX: \n
933                 // Mac: \r
934                 // Windows: \r\n
935                 if (*text == '\n' || *text == '\r')
936                 {
937                         // a newline separates commands in the buffer
938                         if (*text == '\r' && text[1] == '\n')
939                                 text++;
940                         text++;
941                         break;
942                 }
943
944                 if (!*text)
945                         return;
946
947                 if (cmd_argc == 1)
948                         cmd_args = text;
949
950                 if (!COM_ParseToken_Console(&text))
951                         return;
952
953                 if (cmd_argc < MAX_ARGS)
954                 {
955                         l = (int)strlen(com_token) + 1;
956                         if (cmd_tokenizebufferpos + l > CMD_TOKENIZELENGTH)
957                         {
958                                 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguements\n", CMD_TOKENIZELENGTH);
959                                 break;
960                         }
961                         memcpy (cmd_tokenizebuffer + cmd_tokenizebufferpos, com_token, l);
962                         cmd_argv[cmd_argc] = cmd_tokenizebuffer + cmd_tokenizebufferpos;
963                         cmd_tokenizebufferpos += l;
964                         cmd_argc++;
965                 }
966         }
967 }
968
969
970 /*
971 ============
972 Cmd_AddCommand
973 ============
974 */
975 void Cmd_AddCommand_WithClientCommand (const char *cmd_name, xcommand_t consolefunction, xcommand_t clientfunction, const char *description)
976 {
977         cmd_function_t *cmd;
978         cmd_function_t *prev, *current;
979
980 // fail if the command is a variable name
981         if (Cvar_FindVar( cmd_name ))
982         {
983                 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
984                 return;
985         }
986
987 // fail if the command already exists
988         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
989         {
990                 if (!strcmp (cmd_name, cmd->name))
991                 {
992                         if (consolefunction || clientfunction)
993                         {
994                                 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
995                                 return;
996                         }
997                         else    //[515]: csqc
998                         {
999                                 cmd->csqcfunc = true;
1000                                 return;
1001                         }
1002                 }
1003         }
1004
1005         cmd = (cmd_function_t *)Mem_Alloc(cmd_mempool, sizeof(cmd_function_t));
1006         cmd->name = cmd_name;
1007         cmd->consolefunction = consolefunction;
1008         cmd->clientfunction = clientfunction;
1009         cmd->description = description;
1010         if(!consolefunction && !clientfunction)                 //[515]: csqc
1011                 cmd->csqcfunc = true;
1012         cmd->next = cmd_functions;
1013
1014 // insert it at the right alphanumeric position
1015         for( prev = NULL, current = cmd_functions ; current && strcmp( current->name, cmd->name ) < 0 ; prev = current, current = current->next )
1016                 ;
1017         if( prev ) {
1018                 prev->next = cmd;
1019         } else {
1020                 cmd_functions = cmd;
1021         }
1022         cmd->next = current;
1023 }
1024
1025 void Cmd_AddCommand (const char *cmd_name, xcommand_t function, const char *description)
1026 {
1027         Cmd_AddCommand_WithClientCommand (cmd_name, function, NULL, description);
1028 }
1029
1030 /*
1031 ============
1032 Cmd_Exists
1033 ============
1034 */
1035 qboolean Cmd_Exists (const char *cmd_name)
1036 {
1037         cmd_function_t  *cmd;
1038
1039         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1040                 if (!strcmp (cmd_name,cmd->name))
1041                         return true;
1042
1043         return false;
1044 }
1045
1046
1047 /*
1048 ============
1049 Cmd_CompleteCommand
1050 ============
1051 */
1052 const char *Cmd_CompleteCommand (const char *partial)
1053 {
1054         cmd_function_t *cmd;
1055         size_t len;
1056
1057         len = strlen(partial);
1058
1059         if (!len)
1060                 return NULL;
1061
1062 // check functions
1063         for (cmd = cmd_functions; cmd; cmd = cmd->next)
1064                 if (!strncasecmp(partial, cmd->name, len))
1065                         return cmd->name;
1066
1067         return NULL;
1068 }
1069
1070 /*
1071         Cmd_CompleteCountPossible
1072
1073         New function for tab-completion system
1074         Added by EvilTypeGuy
1075         Thanks to Fett erich@heintz.com
1076         Thanks to taniwha
1077
1078 */
1079 int Cmd_CompleteCountPossible (const char *partial)
1080 {
1081         cmd_function_t *cmd;
1082         size_t len;
1083         int h;
1084
1085         h = 0;
1086         len = strlen(partial);
1087
1088         if (!len)
1089                 return 0;
1090
1091         // Loop through the command list and count all partial matches
1092         for (cmd = cmd_functions; cmd; cmd = cmd->next)
1093                 if (!strncasecmp(partial, cmd->name, len))
1094                         h++;
1095
1096         return h;
1097 }
1098
1099 /*
1100         Cmd_CompleteBuildList
1101
1102         New function for tab-completion system
1103         Added by EvilTypeGuy
1104         Thanks to Fett erich@heintz.com
1105         Thanks to taniwha
1106
1107 */
1108 const char **Cmd_CompleteBuildList (const char *partial)
1109 {
1110         cmd_function_t *cmd;
1111         size_t len = 0;
1112         size_t bpos = 0;
1113         size_t sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (const char *);
1114         const char **buf;
1115
1116         len = strlen(partial);
1117         buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
1118         // Loop through the alias list and print all matches
1119         for (cmd = cmd_functions; cmd; cmd = cmd->next)
1120                 if (!strncasecmp(partial, cmd->name, len))
1121                         buf[bpos++] = cmd->name;
1122
1123         buf[bpos] = NULL;
1124         return buf;
1125 }
1126
1127 // written by LordHavoc
1128 void Cmd_CompleteCommandPrint (const char *partial)
1129 {
1130         cmd_function_t *cmd;
1131         size_t len = strlen(partial);
1132         // Loop through the command list and print all matches
1133         for (cmd = cmd_functions; cmd; cmd = cmd->next)
1134                 if (!strncasecmp(partial, cmd->name, len))
1135                         Con_Printf("%s : %s\n", cmd->name, cmd->description);
1136 }
1137
1138 /*
1139         Cmd_CompleteAlias
1140
1141         New function for tab-completion system
1142         Added by EvilTypeGuy
1143         Thanks to Fett erich@heintz.com
1144         Thanks to taniwha
1145
1146 */
1147 const char *Cmd_CompleteAlias (const char *partial)
1148 {
1149         cmdalias_t *alias;
1150         size_t len;
1151
1152         len = strlen(partial);
1153
1154         if (!len)
1155                 return NULL;
1156
1157         // Check functions
1158         for (alias = cmd_alias; alias; alias = alias->next)
1159                 if (!strncasecmp(partial, alias->name, len))
1160                         return alias->name;
1161
1162         return NULL;
1163 }
1164
1165 // written by LordHavoc
1166 void Cmd_CompleteAliasPrint (const char *partial)
1167 {
1168         cmdalias_t *alias;
1169         size_t len = strlen(partial);
1170         // Loop through the alias list and print all matches
1171         for (alias = cmd_alias; alias; alias = alias->next)
1172                 if (!strncasecmp(partial, alias->name, len))
1173                         Con_Printf("%s : %s\n", alias->name, alias->value);
1174 }
1175
1176
1177 /*
1178         Cmd_CompleteAliasCountPossible
1179
1180         New function for tab-completion system
1181         Added by EvilTypeGuy
1182         Thanks to Fett erich@heintz.com
1183         Thanks to taniwha
1184
1185 */
1186 int Cmd_CompleteAliasCountPossible (const char *partial)
1187 {
1188         cmdalias_t      *alias;
1189         size_t          len;
1190         int                     h;
1191
1192         h = 0;
1193
1194         len = strlen(partial);
1195
1196         if (!len)
1197                 return 0;
1198
1199         // Loop through the command list and count all partial matches
1200         for (alias = cmd_alias; alias; alias = alias->next)
1201                 if (!strncasecmp(partial, alias->name, len))
1202                         h++;
1203
1204         return h;
1205 }
1206
1207 /*
1208         Cmd_CompleteAliasBuildList
1209
1210         New function for tab-completion system
1211         Added by EvilTypeGuy
1212         Thanks to Fett erich@heintz.com
1213         Thanks to taniwha
1214
1215 */
1216 const char **Cmd_CompleteAliasBuildList (const char *partial)
1217 {
1218         cmdalias_t *alias;
1219         size_t len = 0;
1220         size_t bpos = 0;
1221         size_t sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (const char *);
1222         const char **buf;
1223
1224         len = strlen(partial);
1225         buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
1226         // Loop through the alias list and print all matches
1227         for (alias = cmd_alias; alias; alias = alias->next)
1228                 if (!strncasecmp(partial, alias->name, len))
1229                         buf[bpos++] = alias->name;
1230
1231         buf[bpos] = NULL;
1232         return buf;
1233 }
1234
1235 void Cmd_ClearCsqcFuncs (void)
1236 {
1237         cmd_function_t *cmd;
1238         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1239                 cmd->csqcfunc = false;
1240 }
1241
1242 qboolean CL_VM_ConsoleCommand (const char *cmd);
1243 /*
1244 ============
1245 Cmd_ExecuteString
1246
1247 A complete command line has been parsed, so try to execute it
1248 FIXME: lookupnoadd the token to speed search?
1249 ============
1250 */
1251 void Cmd_ExecuteString (const char *text, cmd_source_t src)
1252 {
1253         int oldpos;
1254         cmd_function_t *cmd;
1255         cmdalias_t *a;
1256
1257         oldpos = cmd_tokenizebufferpos;
1258         cmd_source = src;
1259
1260         Cmd_TokenizeString (text);
1261
1262 // execute the command line
1263         if (!Cmd_Argc())
1264         {
1265                 cmd_tokenizebufferpos = oldpos;
1266                 return;         // no tokens
1267         }
1268
1269 // check functions
1270         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1271         {
1272                 if (!strcasecmp (cmd_argv[0],cmd->name))
1273                 {
1274                         if (cmd->csqcfunc && CL_VM_ConsoleCommand (text))       //[515]: csqc
1275                                 return;
1276                         switch (src)
1277                         {
1278                         case src_command:
1279                                 if (cmd->consolefunction)
1280                                         cmd->consolefunction ();
1281                                 else if (cmd->clientfunction)
1282                                 {
1283                                         if (cls.state == ca_connected)
1284                                         {
1285                                                 // forward remote commands to the server for execution
1286                                                 Cmd_ForwardToServer();
1287                                         }
1288                                         else
1289                                                 Con_Printf("Can not send command \"%s\", not connected.\n", Cmd_Argv(0));
1290                                 }
1291                                 else
1292                                         Con_Printf("Command \"%s\" can not be executed\n", Cmd_Argv(0));
1293                                 cmd_tokenizebufferpos = oldpos;
1294                                 return;
1295                         case src_client:
1296                                 if (cmd->clientfunction)
1297                                 {
1298                                         cmd->clientfunction ();
1299                                         cmd_tokenizebufferpos = oldpos;
1300                                         return;
1301                                 }
1302                                 break;
1303                         }
1304                         break;
1305                 }
1306         }
1307
1308         // if it's a client command and no command was found, say so.
1309         if (cmd_source == src_client)
1310         {
1311                 Con_Printf("player \"%s\" tried to %s\n", host_client->name, text);
1312                 return;
1313         }
1314
1315 // check alias
1316         for (a=cmd_alias ; a ; a=a->next)
1317         {
1318                 if (!strcasecmp (cmd_argv[0], a->name))
1319                 {
1320                         Cmd_ExecuteAlias(a);
1321                         cmd_tokenizebufferpos = oldpos;
1322                         return;
1323                 }
1324         }
1325
1326 // check cvars
1327         if (!Cvar_Command () && host_framecount > 0)
1328                 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(0));
1329
1330         cmd_tokenizebufferpos = oldpos;
1331 }
1332
1333
1334 /*
1335 ===================
1336 Cmd_ForwardStringToServer
1337
1338 Sends an entire command string over to the server, unprocessed
1339 ===================
1340 */
1341 void Cmd_ForwardStringToServer (const char *s)
1342 {
1343         char temp[128];
1344         if (cls.state != ca_connected)
1345         {
1346                 Con_Printf("Can't \"%s\", not connected\n", s);
1347                 return;
1348         }
1349
1350         if (!cls.netcon)
1351                 return;
1352
1353         // LordHavoc: thanks to Fuh for bringing the pure evil of SZ_Print to my
1354         // attention, it has been eradicated from here, its only (former) use in
1355         // all of darkplaces.
1356         if (cls.protocol == PROTOCOL_QUAKEWORLD)
1357                 MSG_WriteByte(&cls.netcon->message, qw_clc_stringcmd);
1358         else
1359                 MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
1360         if ((!strncmp(s, "say ", 4) || !strncmp(s, "say_team ", 9)) && cl_locs_enable.integer)
1361         {
1362                 // say/say_team commands can replace % character codes with status info
1363                 while (*s)
1364                 {
1365                         if (*s == '%' && s[1])
1366                         {
1367                                 // handle proquake message macros
1368                                 temp[0] = 0;
1369                                 switch (s[1])
1370                                 {
1371                                 case 'l': // current location
1372                                         CL_Locs_FindLocationName(temp, sizeof(temp), cl.movement_origin);
1373                                         break;
1374                                 case 'h': // current health
1375                                         dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_HEALTH]);
1376                                         break;
1377                                 case 'a': // current armor
1378                                         dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_ARMOR]);
1379                                         break;
1380                                 case 'x': // current rockets
1381                                         dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_ROCKETS]);
1382                                         break;
1383                                 case 'c': // current cells
1384                                         dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_CELLS]);
1385                                         break;
1386                                 // silly proquake macros
1387                                 case 'd': // loc at last death
1388                                         CL_Locs_FindLocationName(temp, sizeof(temp), cl.lastdeathorigin);
1389                                         break;
1390                                 case 't': // current time
1391                                         dpsnprintf(temp, sizeof(temp), "%.0f:%.0f", floor(cl.time / 60), cl.time - floor(cl.time / 60) * 60);
1392                                         break;
1393                                 case 'r': // rocket launcher status ("I have RL", "I need rockets", "I need RL")
1394                                         if (!(cl.stats[STAT_ITEMS] & IT_ROCKET_LAUNCHER))
1395                                                 dpsnprintf(temp, sizeof(temp), "I need RL");
1396                                         else if (!cl.stats[STAT_ROCKETS])
1397                                                 dpsnprintf(temp, sizeof(temp), "I need rockets");
1398                                         else
1399                                                 dpsnprintf(temp, sizeof(temp), "I have RL");
1400                                         break;
1401                                 case 'p': // powerup status (outputs "quad" "pent" and "eyes" according to status)
1402                                         if (cl.stats[STAT_ITEMS] & IT_QUAD)
1403                                         {
1404                                                 if (temp[0])
1405                                                         strlcat(temp, " ", sizeof(temp));
1406                                                 strlcat(temp, "quad", sizeof(temp));
1407                                         }
1408                                         if (cl.stats[STAT_ITEMS] & IT_INVULNERABILITY)
1409                                         {
1410                                                 if (temp[0])
1411                                                         strlcat(temp, " ", sizeof(temp));
1412                                                 strlcat(temp, "pent", sizeof(temp));
1413                                         }
1414                                         if (cl.stats[STAT_ITEMS] & IT_INVISIBILITY)
1415                                         {
1416                                                 if (temp[0])
1417                                                         strlcat(temp, " ", sizeof(temp));
1418                                                 strlcat(temp, "eyes", sizeof(temp));
1419                                         }
1420                                         break;
1421                                 case 'w': // weapon status (outputs "SSG:NG:SNG:GL:RL:LG" with the text between : characters omitted if you lack the weapon)
1422                                         if (cl.stats[STAT_ITEMS] & IT_SUPER_SHOTGUN)
1423                                                 strlcat(temp, "SSG", sizeof(temp));
1424                                         strlcat(temp, ":", sizeof(temp));
1425                                         if (cl.stats[STAT_ITEMS] & IT_NAILGUN)
1426                                                 strlcat(temp, "NG", sizeof(temp));
1427                                         strlcat(temp, ":", sizeof(temp));
1428                                         if (cl.stats[STAT_ITEMS] & IT_SUPER_NAILGUN)
1429                                                 strlcat(temp, "SNG", sizeof(temp));
1430                                         strlcat(temp, ":", sizeof(temp));
1431                                         if (cl.stats[STAT_ITEMS] & IT_GRENADE_LAUNCHER)
1432                                                 strlcat(temp, "GL", sizeof(temp));
1433                                         strlcat(temp, ":", sizeof(temp));
1434                                         if (cl.stats[STAT_ITEMS] & IT_ROCKET_LAUNCHER)
1435                                                 strlcat(temp, "RL", sizeof(temp));
1436                                         strlcat(temp, ":", sizeof(temp));
1437                                         if (cl.stats[STAT_ITEMS] & IT_LIGHTNING)
1438                                                 strlcat(temp, "LG", sizeof(temp));
1439                                         break;
1440                                 default:
1441                                         // not a recognized macro, print it as-is...
1442                                         temp[0] = s[0];
1443                                         temp[1] = s[1];
1444                                         temp[2] = 0;
1445                                         break;
1446                                 }
1447                                 // write the resulting text
1448                                 SZ_Write(&cls.netcon->message, (unsigned char *)temp, strlen(temp));
1449                                 s += 2;
1450                                 continue;
1451                         }
1452                         MSG_WriteByte(&cls.netcon->message, *s);
1453                         s++;
1454                 }
1455                 MSG_WriteByte(&cls.netcon->message, 0);
1456         }
1457         else // any other command is passed on as-is
1458                 SZ_Write(&cls.netcon->message, (const unsigned char *)s, (int)strlen(s) + 1);
1459 }
1460
1461 /*
1462 ===================
1463 Cmd_ForwardToServer
1464
1465 Sends the entire command line over to the server
1466 ===================
1467 */
1468 void Cmd_ForwardToServer (void)
1469 {
1470         const char *s;
1471         if (!strcasecmp(Cmd_Argv(0), "cmd"))
1472         {
1473                 // we want to strip off "cmd", so just send the args
1474                 s = Cmd_Argc() > 1 ? Cmd_Args() : "";
1475         }
1476         else
1477         {
1478                 // we need to keep the command name, so send Cmd_Argv(0), a space and then Cmd_Args()
1479                 s = va("%s %s", Cmd_Argv(0), Cmd_Argc() > 1 ? Cmd_Args() : "");
1480         }
1481         // don't send an empty forward message if the user tries "cmd" by itself
1482         if (!s || !*s)
1483                 return;
1484         Cmd_ForwardStringToServer(s);
1485 }
1486
1487
1488 /*
1489 ================
1490 Cmd_CheckParm
1491
1492 Returns the position (1 to argc-1) in the command's argument list
1493 where the given parameter apears, or 0 if not present
1494 ================
1495 */
1496
1497 int Cmd_CheckParm (const char *parm)
1498 {
1499         int i;
1500
1501         if (!parm)
1502         {
1503                 Con_Printf ("Cmd_CheckParm: NULL");
1504                 return 0;
1505         }
1506
1507         for (i = 1; i < Cmd_Argc (); i++)
1508                 if (!strcasecmp (parm, Cmd_Argv (i)))
1509                         return i;
1510
1511         return 0;
1512 }
1513