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