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