]> icculus.org git repositories - divverent/darkplaces.git/blob - cmd.c
Added CSQC globals dmg_take, dmg_save and dmg_origin. These globals correspond to...
[divverent/darkplaces.git] / cmd.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // cmd.c -- Quake script command processing module
21
22 #include "quakedef.h"
23
24 #define MAX_ALIAS_NAME  32
25 // this is the largest script file that can be executed in one step
26 // LordHavoc: inreased this from 8192 to 32768
27 #define CMDBUFSIZE 32768
28 // maximum number of parameters to a command
29 #define MAX_ARGS 80
30 // maximum tokenizable commandline length (counting NUL terminations)
31 #define CMD_TOKENIZELENGTH (MAX_INPUTLINE + 80)
32
33 typedef struct cmdalias_s
34 {
35         struct cmdalias_s *next;
36         char name[MAX_ALIAS_NAME];
37         char *value;
38 } cmdalias_t;
39
40 static cmdalias_t *cmd_alias;
41
42 static qboolean cmd_wait;
43
44 static mempool_t *cmd_mempool;
45
46 static char cmd_tokenizebuffer[CMD_TOKENIZELENGTH];
47 static int cmd_tokenizebufferpos = 0;
48
49 //=============================================================================
50
51 /*
52 ============
53 Cmd_Wait_f
54
55 Causes execution of the remainder of the command buffer to be delayed until
56 next frame.  This allows commands like:
57 bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
58 ============
59 */
60 static void Cmd_Wait_f (void)
61 {
62         cmd_wait = true;
63 }
64
65 /*
66 =============================================================================
67
68                                                 COMMAND BUFFER
69
70 =============================================================================
71 */
72
73 static sizebuf_t        cmd_text;
74 static unsigned char            cmd_text_buf[CMDBUFSIZE];
75
76 /*
77 ============
78 Cbuf_AddText
79
80 Adds command text at the end of the buffer
81 ============
82 */
83 void Cbuf_AddText (const char *text)
84 {
85         int             l;
86
87         l = (int)strlen (text);
88
89         if (cmd_text.cursize + l >= cmd_text.maxsize)
90         {
91                 Con_Print("Cbuf_AddText: overflow\n");
92                 return;
93         }
94
95         SZ_Write (&cmd_text, (const unsigned char *)text, (int)strlen (text));
96 }
97
98
99 /*
100 ============
101 Cbuf_InsertText
102
103 Adds command text immediately after the current command
104 Adds a \n to the text
105 FIXME: actually change the command buffer to do less copying
106 ============
107 */
108 void Cbuf_InsertText (const char *text)
109 {
110         char    *temp;
111         int             templen;
112
113         // copy off any commands still remaining in the exec buffer
114         templen = cmd_text.cursize;
115         if (templen)
116         {
117                 temp = (char *)Mem_Alloc (tempmempool, templen);
118                 memcpy (temp, cmd_text.data, templen);
119                 SZ_Clear (&cmd_text);
120         }
121         else
122                 temp = NULL;
123
124         // add the entire text of the file
125         Cbuf_AddText (text);
126
127         // add the copied off data
128         if (temp != NULL)
129         {
130                 SZ_Write (&cmd_text, (const unsigned char *)temp, templen);
131                 Mem_Free (temp);
132         }
133 }
134
135 /*
136 ============
137 Cbuf_Execute
138 ============
139 */
140 static void Cmd_PreprocessString( const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias );
141 void Cbuf_Execute (void)
142 {
143         int i;
144         char *text;
145         char line[MAX_INPUTLINE];
146         char preprocessed[MAX_INPUTLINE];
147         char *firstchar;
148         int quotes;
149
150         // LordHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
151         cmd_tokenizebufferpos = 0;
152
153         while (cmd_text.cursize)
154         {
155 // find a \n or ; line break
156                 text = (char *)cmd_text.data;
157
158                 quotes = 0;
159                 for (i=0 ; i< cmd_text.cursize ; i++)
160                 {
161                         if (text[i] == '"')
162                                 quotes ^= 1;
163                         if (text[i] == '\\' && (text[i+1] == '"' || text[i+1] == '\\'))
164                                 i++;
165                         if ( !quotes &&  text[i] == ';')
166                                 break;  // don't break if inside a quoted string
167                         if (text[i] == '\r' || text[i] == '\n')
168                                 break;
169                 }
170
171                 memcpy (line, text, i);
172                 line[i] = 0;
173
174 // delete the text from the command buffer and move remaining commands down
175 // this is necessary because commands (exec, alias) can insert data at the
176 // beginning of the text buffer
177
178                 if (i == cmd_text.cursize)
179                         cmd_text.cursize = 0;
180                 else
181                 {
182                         i++;
183                         cmd_text.cursize -= i;
184                         memcpy (cmd_text.data, text+i, cmd_text.cursize);
185                 }
186
187 // execute the command line
188                 firstchar = line + strspn(line, " \t");
189                 if(
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)
522 {
523         cvar_t *cvar;
524         long argno;
525         char *endptr;
526
527         if(!varname || !*varname)
528                 return NULL;
529
530         if(alias)
531         {
532                 if(!strcmp(varname, "*"))
533                         return Cmd_Args();
534                 else
535                 {
536                         argno = strtol(varname, &endptr, 10);
537                         if(*endptr == 0)
538                         {
539                                 // whole string is a number
540                                 // NOTE: we already made sure we don't have an empty cvar name!
541                                 if(argno >= 0 && argno < Cmd_Argc())
542                                         return Cmd_Argv(argno);
543                         }
544                 }
545         }
546
547         if((cvar = Cvar_FindVar(varname)) && !(cvar->flags & CVAR_PRIVATE))
548                 return cvar->string;
549
550         return NULL;
551 }
552
553 qboolean Cmd_QuoteString(char *out, size_t outlen, const char *in, const char *quoteset)
554 {
555         qboolean quote_quot = !!strchr(quoteset, '"');
556         qboolean quote_backslash = !!strchr(quoteset, '\\');
557         qboolean quote_dollar = !!strchr(quoteset, '$');
558
559         while(*in)
560         {
561                 if(*in == '"' && quote_quot)
562                 {
563                         if(outlen <= 2)
564                         {
565                                 *out++ = 0;
566                                 return false;
567                         }
568                         *out++ = '\\'; --outlen;
569                         *out++ = '"'; --outlen;
570                 }
571                 else if(*in == '\\' && quote_backslash)
572                 {
573                         if(outlen <= 2)
574                         {
575                                 *out++ = 0;
576                                 return false;
577                         }
578                         *out++ = '\\'; --outlen;
579                         *out++ = '\\'; --outlen;
580                 }
581                 else if(*in == '\\' && quote_dollar)
582                 {
583                         if(outlen <= 2)
584                         {
585                                 *out++ = 0;
586                                 return false;
587                         }
588                         *out++ = '$'; --outlen;
589                         *out++ = '$'; --outlen;
590                 }
591                 else
592                 {
593                         if(outlen <= 1)
594                         {
595                                 *out++ = 0;
596                                 return false;
597                         }
598                         *out++ = *in; --outlen;
599                 }
600                 ++in;
601         }
602         *out++ = 0;
603         return true;
604 }
605
606 static const char *Cmd_GetCvarValue(const char *var, size_t varlen, cmdalias_t *alias)
607 {
608         static char varname[MAX_INPUTLINE];
609         static char varval[MAX_INPUTLINE];
610         const char *varstr;
611         char *varfunc;
612
613         if(varlen >= MAX_INPUTLINE)
614                 varlen = MAX_INPUTLINE - 1;
615         memcpy(varname, var, varlen);
616         varname[varlen] = 0;
617         varfunc = strchr(varname, ' ');
618
619         if(varfunc)
620         {
621                 *varfunc = 0;
622                 ++varfunc;
623         }
624
625         if(*var == 0)
626         {
627                 // empty cvar name?
628                 return NULL;
629         }
630
631         varstr = NULL;
632
633         // Exception: $* doesn't use the quoted form by default
634         if(!strcmp(varname, "*"))
635                 varfunc = "asis";
636
637         if(varname[0] == '$')
638                 varstr = Cmd_GetDirectCvarValue(Cmd_GetDirectCvarValue(varname + 1, alias), alias);
639         else
640                 varstr = Cmd_GetDirectCvarValue(varname, alias);
641
642         if(!varstr)
643         {
644                 if(alias)
645                         Con_Printf("Warning: Could not expand $%s in alias %s\n", varname, alias->name);
646                 else
647                         Con_Printf("Warning: Could not expand $%s\n", varname);
648                 return NULL;
649         }
650
651         if(!varfunc || !strcmp(varfunc, "q")) // note: quoted form is default, use "asis" to override!
652         {
653                 // quote it so it can be used inside double quotes
654                 // we just need to replace " by \", and of course, double backslashes
655                 Cmd_QuoteString(varval, sizeof(varval), varstr, "\"\\");
656                 return varval;
657         }
658         else if(!strcmp(varfunc, "asis"))
659         {
660                 return varstr;
661         }
662         else
663                 Con_Printf("Unknown variable function %s\n", varfunc);
664
665         return varstr;
666 }
667
668 /*
669 Cmd_PreprocessString
670
671 Preprocesses strings and replaces $*, $param#, $cvar accordingly
672 */
673 static void Cmd_PreprocessString( const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias ) {
674         const char *in;
675         size_t eat, varlen;
676         unsigned outlen;
677         const char *val;
678
679         // don't crash if there's no room in the outtext buffer
680         if( maxoutlen == 0 ) {
681                 return;
682         }
683         maxoutlen--; // because of \0
684
685         in = intext;
686         outlen = 0;
687
688         while( *in && outlen < maxoutlen ) {
689                 if( *in == '$' ) {
690                         // this is some kind of expansion, see what comes after the $
691                         in++;
692
693                         // The console does the following preprocessing:
694                         //
695                         // - $$ is transformed to a single dollar sign.
696                         // - $var or ${var} are expanded to the contents of the named cvar,
697                         //   with quotation marks and backslashes quoted so it can safely
698                         //   be used inside quotation marks (and it should always be used
699                         //   that way)
700                         // - ${var asis} inserts the cvar value as is, without doing this
701                         //   quoting
702                         // - prefix the cvar name with a dollar sign to do indirection;
703                         //   for example, if $x has the value timelimit, ${$x} will return
704                         //   the value of $timelimit
705                         // - when expanding an alias, the special variable name $* refers
706                         //   to all alias parameters, and a number refers to that numbered
707                         //   alias parameter, where the name of the alias is $0, the first
708                         //   parameter is $1 and so on; as a special case, $* inserts all
709                         //   parameters, without extra quoting, so one can use $* to just
710                         //   pass all parameters around
711                         //
712                         // Note: when expanding an alias, cvar expansion is done in the SAME step
713                         // as alias expansion so that alias parameters or cvar values containing
714                         // dollar signs have no unwanted bad side effects. However, this needs to
715                         // be accounted for when writing complex aliases. For example,
716                         //   alias foo "set x NEW; echo $x"
717                         // actually expands to
718                         //   "set x NEW; echo OLD"
719                         // and will print OLD! To work around this, use a second alias:
720                         //   alias foo "set x NEW; foo2"
721                         //   alias foo2 "echo $x"
722                         //
723                         // Also note: lines starting with alias are exempt from cvar expansion.
724                         // If you want cvar expansion, write "alias" instead:
725                         //
726                         //   set x 1
727                         //   alias foo "echo $x"
728                         //   "alias" bar "echo $x"
729                         //   set x 2
730                         //
731                         // foo will print 2, because the variable $x will be expanded when the alias
732                         // gets expanded. bar will print 1, because the variable $x was expanded
733                         // at definition time. foo can be equivalently defined as
734                         //
735                         //   "alias" foo "echo $$x"
736                         //
737                         // because at definition time, $$ will get replaced to a single $.
738
739                         if( *in == '$' ) {
740                                 val = "$";
741                                 eat = 1;
742                         } else if(*in == '{') {
743                                 varlen = strcspn(in + 1, "}");
744                                 if(in[varlen + 1] == '}')
745                                 {
746                                         val = Cmd_GetCvarValue(in + 1, varlen, alias);
747                                         eat = varlen + 2;
748                                 }
749                                 else
750                                 {
751                                         // ran out of data?
752                                         val = NULL;
753                                         eat = varlen + 1;
754                                 }
755                         } else {
756                                 varlen = strspn(in, "*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_");
757                                 val = Cmd_GetCvarValue(in, varlen, alias);
758                                 eat = varlen;
759                         }
760                         if(val)
761                         {
762                                 // insert the cvar value
763                                 while(*val && outlen < maxoutlen)
764                                         outtext[outlen++] = *val++;
765                                 in += eat;
766                         }
767                         else
768                         {
769                                 // copy the unexpanded text
770                                 outtext[outlen++] = '$';
771                                 while(eat && outlen < maxoutlen)
772                                 {
773                                         outtext[outlen++] = *in++;
774                                         --eat;
775                                 }
776                         }
777                 } else {
778                         outtext[outlen++] = *in++;
779                 }
780         }
781         outtext[outlen] = 0;
782 }
783
784 /*
785 ============
786 Cmd_ExecuteAlias
787
788 Called for aliases and fills in the alias into the cbuffer
789 ============
790 */
791 static void Cmd_ExecuteAlias (cmdalias_t *alias)
792 {
793         static char buffer[ MAX_INPUTLINE + 2 ];
794         static char buffer2[ MAX_INPUTLINE * 2 + 2 ];
795         Cmd_PreprocessString( alias->value, buffer, sizeof(buffer) - 2, alias );
796         // insert at start of command buffer, so that aliases execute in order
797         // (fixes bug introduced by Black on 20050705)
798         
799         // Note: Cbuf_PreprocessString will be called on this string AGAIN! So we
800         // have to make sure that no second variable expansion takes place, otherwise
801         // alias parameters containing dollar signs can have bad effects.
802         Cmd_QuoteString(buffer2, sizeof(buffer2), buffer, "$");
803         Cbuf_InsertText( buffer2 );
804 }
805
806 /*
807 ========
808 Cmd_List
809
810         CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
811         Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
812
813 ========
814 */
815 static void Cmd_List_f (void)
816 {
817         cmd_function_t *cmd;
818         const char *partial;
819         int len, count;
820
821         if (Cmd_Argc() > 1)
822         {
823                 partial = Cmd_Argv (1);
824                 len = (int)strlen(partial);
825         }
826         else
827         {
828                 partial = NULL;
829                 len = 0;
830         }
831
832         count = 0;
833         for (cmd = cmd_functions; cmd; cmd = cmd->next)
834         {
835                 if (partial && strncmp(partial, cmd->name, len))
836                         continue;
837                 Con_Printf("%s : %s\n", cmd->name, cmd->description);
838                 count++;
839         }
840
841         if (partial)
842                 Con_Printf("%i Command%s beginning with \"%s\"\n\n", count, (count > 1) ? "s" : "", partial);
843         else
844                 Con_Printf("%i Command%s\n\n", count, (count > 1) ? "s" : "");
845 }
846
847 /*
848 ============
849 Cmd_Init
850 ============
851 */
852 void Cmd_Init (void)
853 {
854         cmd_mempool = Mem_AllocPool("commands", 0, NULL);
855         // space for commands and script files
856         cmd_text.data = cmd_text_buf;
857         cmd_text.maxsize = sizeof(cmd_text_buf);
858         cmd_text.cursize = 0;
859 }
860
861 void Cmd_Init_Commands (void)
862 {
863 //
864 // register our commands
865 //
866         Cmd_AddCommand ("stuffcmds",Cmd_StuffCmds_f, "execute commandline parameters (must be present in quake.rc script)");
867         Cmd_AddCommand ("exec",Cmd_Exec_f, "execute a script file");
868         Cmd_AddCommand ("echo",Cmd_Echo_f, "print a message to the console (useful in scripts)");
869         Cmd_AddCommand ("alias",Cmd_Alias_f, "create a script function (parameters are passed in as $1 through $9, and $* for all parameters)");
870         Cmd_AddCommand ("cmd", Cmd_ForwardToServer, "send a console commandline to the server (used by some mods)");
871         Cmd_AddCommand ("wait", Cmd_Wait_f, "make script execution wait for next rendered frame");
872         Cmd_AddCommand ("set", Cvar_Set_f, "create or change the value of a console variable");
873         Cmd_AddCommand ("seta", Cvar_SetA_f, "create or change the value of a console variable that will be saved to config.cfg");
874
875         // 2000-01-09 CmdList, CvarList commands By Matthias "Maddes" Buecher
876         // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
877         Cmd_AddCommand ("cmdlist", Cmd_List_f, "lists all console commands beginning with the specified prefix");
878         Cmd_AddCommand ("cvarlist", Cvar_List_f, "lists all console variables beginning with the specified prefix");
879
880         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");
881         Cmd_AddCommand ("cvar_resettodefaults_all", Cvar_ResetToDefaults_All_f, "sets all cvars to their locked default values");
882         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)");
883         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)");
884
885         // DRESK - 5/14/06
886         // Support Doom3-style Toggle Command
887         Cmd_AddCommand( "toggle", Cmd_Toggle_f, "toggles a console variable's values (use for more info)");
888 }
889
890 /*
891 ============
892 Cmd_Shutdown
893 ============
894 */
895 void Cmd_Shutdown(void)
896 {
897         Mem_FreePool(&cmd_mempool);
898 }
899
900 /*
901 ============
902 Cmd_Argc
903 ============
904 */
905 int             Cmd_Argc (void)
906 {
907         return cmd_argc;
908 }
909
910 /*
911 ============
912 Cmd_Argv
913 ============
914 */
915 const char *Cmd_Argv (int arg)
916 {
917         if (arg >= cmd_argc )
918                 return cmd_null_string;
919         return cmd_argv[arg];
920 }
921
922 /*
923 ============
924 Cmd_Args
925 ============
926 */
927 const char *Cmd_Args (void)
928 {
929         return cmd_args;
930 }
931
932
933 /*
934 ============
935 Cmd_TokenizeString
936
937 Parses the given string into command line tokens.
938 ============
939 */
940 // AK: This function should only be called from ExcuteString because the current design is a bit of an hack
941 static void Cmd_TokenizeString (const char *text)
942 {
943         int l;
944
945         cmd_argc = 0;
946         cmd_args = NULL;
947
948         while (1)
949         {
950                 // skip whitespace up to a /n
951                 while (*text && *text <= ' ' && *text != '\r' && *text != '\n')
952                         text++;
953
954                 // line endings:
955                 // UNIX: \n
956                 // Mac: \r
957                 // Windows: \r\n
958                 if (*text == '\n' || *text == '\r')
959                 {
960                         // a newline separates commands in the buffer
961                         if (*text == '\r' && text[1] == '\n')
962                                 text++;
963                         text++;
964                         break;
965                 }
966
967                 if (!*text)
968                         return;
969
970                 if (cmd_argc == 1)
971                         cmd_args = text;
972
973                 if (!COM_ParseToken_Console(&text))
974                         return;
975
976                 if (cmd_argc < MAX_ARGS)
977                 {
978                         l = (int)strlen(com_token) + 1;
979                         if (cmd_tokenizebufferpos + l > CMD_TOKENIZELENGTH)
980                         {
981                                 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguements\n", CMD_TOKENIZELENGTH);
982                                 break;
983                         }
984                         memcpy (cmd_tokenizebuffer + cmd_tokenizebufferpos, com_token, l);
985                         cmd_argv[cmd_argc] = cmd_tokenizebuffer + cmd_tokenizebufferpos;
986                         cmd_tokenizebufferpos += l;
987                         cmd_argc++;
988                 }
989         }
990 }
991
992
993 /*
994 ============
995 Cmd_AddCommand
996 ============
997 */
998 void Cmd_AddCommand_WithClientCommand (const char *cmd_name, xcommand_t consolefunction, xcommand_t clientfunction, const char *description)
999 {
1000         cmd_function_t *cmd;
1001         cmd_function_t *prev, *current;
1002
1003 // fail if the command is a variable name
1004         if (Cvar_FindVar( cmd_name ))
1005         {
1006                 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
1007                 return;
1008         }
1009
1010 // fail if the command already exists
1011         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1012         {
1013                 if (!strcmp (cmd_name, cmd->name))
1014                 {
1015                         if (consolefunction || clientfunction)
1016                         {
1017                                 Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
1018                                 return;
1019                         }
1020                         else    //[515]: csqc
1021                         {
1022                                 cmd->csqcfunc = true;
1023                                 return;
1024                         }
1025                 }
1026         }
1027
1028         cmd = (cmd_function_t *)Mem_Alloc(cmd_mempool, sizeof(cmd_function_t));
1029         cmd->name = cmd_name;
1030         cmd->consolefunction = consolefunction;
1031         cmd->clientfunction = clientfunction;
1032         cmd->description = description;
1033         if(!consolefunction && !clientfunction)                 //[515]: csqc
1034                 cmd->csqcfunc = true;
1035         cmd->next = cmd_functions;
1036
1037 // insert it at the right alphanumeric position
1038         for( prev = NULL, current = cmd_functions ; current && strcmp( current->name, cmd->name ) < 0 ; prev = current, current = current->next )
1039                 ;
1040         if( prev ) {
1041                 prev->next = cmd;
1042         } else {
1043                 cmd_functions = cmd;
1044         }
1045         cmd->next = current;
1046 }
1047
1048 void Cmd_AddCommand (const char *cmd_name, xcommand_t function, const char *description)
1049 {
1050         Cmd_AddCommand_WithClientCommand (cmd_name, function, NULL, description);
1051 }
1052
1053 /*
1054 ============
1055 Cmd_Exists
1056 ============
1057 */
1058 qboolean Cmd_Exists (const char *cmd_name)
1059 {
1060         cmd_function_t  *cmd;
1061
1062         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1063                 if (!strcmp (cmd_name,cmd->name))
1064                         return true;
1065
1066         return false;
1067 }
1068
1069
1070 /*
1071 ============
1072 Cmd_CompleteCommand
1073 ============
1074 */
1075 const char *Cmd_CompleteCommand (const char *partial)
1076 {
1077         cmd_function_t *cmd;
1078         size_t len;
1079
1080         len = strlen(partial);
1081
1082         if (!len)
1083                 return NULL;
1084
1085 // check functions
1086         for (cmd = cmd_functions; cmd; cmd = cmd->next)
1087                 if (!strncasecmp(partial, cmd->name, len))
1088                         return cmd->name;
1089
1090         return NULL;
1091 }
1092
1093 /*
1094         Cmd_CompleteCountPossible
1095
1096         New function for tab-completion system
1097         Added by EvilTypeGuy
1098         Thanks to Fett erich@heintz.com
1099         Thanks to taniwha
1100
1101 */
1102 int Cmd_CompleteCountPossible (const char *partial)
1103 {
1104         cmd_function_t *cmd;
1105         size_t len;
1106         int h;
1107
1108         h = 0;
1109         len = strlen(partial);
1110
1111         if (!len)
1112                 return 0;
1113
1114         // Loop through the command list and count all partial matches
1115         for (cmd = cmd_functions; cmd; cmd = cmd->next)
1116                 if (!strncasecmp(partial, cmd->name, len))
1117                         h++;
1118
1119         return h;
1120 }
1121
1122 /*
1123         Cmd_CompleteBuildList
1124
1125         New function for tab-completion system
1126         Added by EvilTypeGuy
1127         Thanks to Fett erich@heintz.com
1128         Thanks to taniwha
1129
1130 */
1131 const char **Cmd_CompleteBuildList (const char *partial)
1132 {
1133         cmd_function_t *cmd;
1134         size_t len = 0;
1135         size_t bpos = 0;
1136         size_t sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (const char *);
1137         const char **buf;
1138
1139         len = strlen(partial);
1140         buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
1141         // Loop through the alias list and print all matches
1142         for (cmd = cmd_functions; cmd; cmd = cmd->next)
1143                 if (!strncasecmp(partial, cmd->name, len))
1144                         buf[bpos++] = cmd->name;
1145
1146         buf[bpos] = NULL;
1147         return buf;
1148 }
1149
1150 // written by LordHavoc
1151 void Cmd_CompleteCommandPrint (const char *partial)
1152 {
1153         cmd_function_t *cmd;
1154         size_t len = strlen(partial);
1155         // Loop through the command list and print all matches
1156         for (cmd = cmd_functions; cmd; cmd = cmd->next)
1157                 if (!strncasecmp(partial, cmd->name, len))
1158                         Con_Printf("%s : %s\n", cmd->name, cmd->description);
1159 }
1160
1161 /*
1162         Cmd_CompleteAlias
1163
1164         New function for tab-completion system
1165         Added by EvilTypeGuy
1166         Thanks to Fett erich@heintz.com
1167         Thanks to taniwha
1168
1169 */
1170 const char *Cmd_CompleteAlias (const char *partial)
1171 {
1172         cmdalias_t *alias;
1173         size_t len;
1174
1175         len = strlen(partial);
1176
1177         if (!len)
1178                 return NULL;
1179
1180         // Check functions
1181         for (alias = cmd_alias; alias; alias = alias->next)
1182                 if (!strncasecmp(partial, alias->name, len))
1183                         return alias->name;
1184
1185         return NULL;
1186 }
1187
1188 // written by LordHavoc
1189 void Cmd_CompleteAliasPrint (const char *partial)
1190 {
1191         cmdalias_t *alias;
1192         size_t len = strlen(partial);
1193         // Loop through the alias list and print all matches
1194         for (alias = cmd_alias; alias; alias = alias->next)
1195                 if (!strncasecmp(partial, alias->name, len))
1196                         Con_Printf("%s : %s\n", alias->name, alias->value);
1197 }
1198
1199
1200 /*
1201         Cmd_CompleteAliasCountPossible
1202
1203         New function for tab-completion system
1204         Added by EvilTypeGuy
1205         Thanks to Fett erich@heintz.com
1206         Thanks to taniwha
1207
1208 */
1209 int Cmd_CompleteAliasCountPossible (const char *partial)
1210 {
1211         cmdalias_t      *alias;
1212         size_t          len;
1213         int                     h;
1214
1215         h = 0;
1216
1217         len = strlen(partial);
1218
1219         if (!len)
1220                 return 0;
1221
1222         // Loop through the command list and count all partial matches
1223         for (alias = cmd_alias; alias; alias = alias->next)
1224                 if (!strncasecmp(partial, alias->name, len))
1225                         h++;
1226
1227         return h;
1228 }
1229
1230 /*
1231         Cmd_CompleteAliasBuildList
1232
1233         New function for tab-completion system
1234         Added by EvilTypeGuy
1235         Thanks to Fett erich@heintz.com
1236         Thanks to taniwha
1237
1238 */
1239 const char **Cmd_CompleteAliasBuildList (const char *partial)
1240 {
1241         cmdalias_t *alias;
1242         size_t len = 0;
1243         size_t bpos = 0;
1244         size_t sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (const char *);
1245         const char **buf;
1246
1247         len = strlen(partial);
1248         buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
1249         // Loop through the alias list and print all matches
1250         for (alias = cmd_alias; alias; alias = alias->next)
1251                 if (!strncasecmp(partial, alias->name, len))
1252                         buf[bpos++] = alias->name;
1253
1254         buf[bpos] = NULL;
1255         return buf;
1256 }
1257
1258 void Cmd_ClearCsqcFuncs (void)
1259 {
1260         cmd_function_t *cmd;
1261         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1262                 cmd->csqcfunc = false;
1263 }
1264
1265 qboolean CL_VM_ConsoleCommand (const char *cmd);
1266 /*
1267 ============
1268 Cmd_ExecuteString
1269
1270 A complete command line has been parsed, so try to execute it
1271 FIXME: lookupnoadd the token to speed search?
1272 ============
1273 */
1274 void Cmd_ExecuteString (const char *text, cmd_source_t src)
1275 {
1276         int oldpos;
1277         cmd_function_t *cmd;
1278         cmdalias_t *a;
1279
1280         oldpos = cmd_tokenizebufferpos;
1281         cmd_source = src;
1282
1283         Cmd_TokenizeString (text);
1284
1285 // execute the command line
1286         if (!Cmd_Argc())
1287         {
1288                 cmd_tokenizebufferpos = oldpos;
1289                 return;         // no tokens
1290         }
1291
1292 // check functions
1293         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1294         {
1295                 if (!strcasecmp (cmd_argv[0],cmd->name))
1296                 {
1297                         if (cmd->csqcfunc && CL_VM_ConsoleCommand (text))       //[515]: csqc
1298                                 return;
1299                         switch (src)
1300                         {
1301                         case src_command:
1302                                 if (cmd->consolefunction)
1303                                         cmd->consolefunction ();
1304                                 else if (cmd->clientfunction)
1305                                 {
1306                                         if (cls.state == ca_connected)
1307                                         {
1308                                                 // forward remote commands to the server for execution
1309                                                 Cmd_ForwardToServer();
1310                                         }
1311                                         else
1312                                                 Con_Printf("Can not send command \"%s\", not connected.\n", Cmd_Argv(0));
1313                                 }
1314                                 else
1315                                         Con_Printf("Command \"%s\" can not be executed\n", Cmd_Argv(0));
1316                                 cmd_tokenizebufferpos = oldpos;
1317                                 return;
1318                         case src_client:
1319                                 if (cmd->clientfunction)
1320                                 {
1321                                         cmd->clientfunction ();
1322                                         cmd_tokenizebufferpos = oldpos;
1323                                         return;
1324                                 }
1325                                 break;
1326                         }
1327                         break;
1328                 }
1329         }
1330
1331         // if it's a client command and no command was found, say so.
1332         if (cmd_source == src_client)
1333         {
1334                 Con_Printf("player \"%s\" tried to %s\n", host_client->name, text);
1335                 return;
1336         }
1337
1338 // check alias
1339         for (a=cmd_alias ; a ; a=a->next)
1340         {
1341                 if (!strcasecmp (cmd_argv[0], a->name))
1342                 {
1343                         Cmd_ExecuteAlias(a);
1344                         cmd_tokenizebufferpos = oldpos;
1345                         return;
1346                 }
1347         }
1348
1349 // check cvars
1350         if (!Cvar_Command () && host_framecount > 0)
1351                 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(0));
1352
1353         cmd_tokenizebufferpos = oldpos;
1354 }
1355
1356
1357 /*
1358 ===================
1359 Cmd_ForwardStringToServer
1360
1361 Sends an entire command string over to the server, unprocessed
1362 ===================
1363 */
1364 void Cmd_ForwardStringToServer (const char *s)
1365 {
1366         char temp[128];
1367         if (cls.state != ca_connected)
1368         {
1369                 Con_Printf("Can't \"%s\", not connected\n", s);
1370                 return;
1371         }
1372
1373         if (!cls.netcon)
1374                 return;
1375
1376         // LordHavoc: thanks to Fuh for bringing the pure evil of SZ_Print to my
1377         // attention, it has been eradicated from here, its only (former) use in
1378         // all of darkplaces.
1379         if (cls.protocol == PROTOCOL_QUAKEWORLD)
1380                 MSG_WriteByte(&cls.netcon->message, qw_clc_stringcmd);
1381         else
1382                 MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
1383         if ((!strncmp(s, "say ", 4) || !strncmp(s, "say_team ", 9)) && cl_locs_enable.integer)
1384         {
1385                 // say/say_team commands can replace % character codes with status info
1386                 while (*s)
1387                 {
1388                         if (*s == '%' && s[1])
1389                         {
1390                                 // handle proquake message macros
1391                                 temp[0] = 0;
1392                                 switch (s[1])
1393                                 {
1394                                 case 'l': // current location
1395                                         CL_Locs_FindLocationName(temp, sizeof(temp), cl.movement_origin);
1396                                         break;
1397                                 case 'h': // current health
1398                                         dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_HEALTH]);
1399                                         break;
1400                                 case 'a': // current armor
1401                                         dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_ARMOR]);
1402                                         break;
1403                                 case 'x': // current rockets
1404                                         dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_ROCKETS]);
1405                                         break;
1406                                 case 'c': // current cells
1407                                         dpsnprintf(temp, sizeof(temp), "%i", cl.stats[STAT_CELLS]);
1408                                         break;
1409                                 // silly proquake macros
1410                                 case 'd': // loc at last death
1411                                         CL_Locs_FindLocationName(temp, sizeof(temp), cl.lastdeathorigin);
1412                                         break;
1413                                 case 't': // current time
1414                                         dpsnprintf(temp, sizeof(temp), "%.0f:%.0f", floor(cl.time / 60), cl.time - floor(cl.time / 60) * 60);
1415                                         break;
1416                                 case 'r': // rocket launcher status ("I have RL", "I need rockets", "I need RL")
1417                                         if (!(cl.stats[STAT_ITEMS] & IT_ROCKET_LAUNCHER))
1418                                                 dpsnprintf(temp, sizeof(temp), "I need RL");
1419                                         else if (!cl.stats[STAT_ROCKETS])
1420                                                 dpsnprintf(temp, sizeof(temp), "I need rockets");
1421                                         else
1422                                                 dpsnprintf(temp, sizeof(temp), "I have RL");
1423                                         break;
1424                                 case 'p': // powerup status (outputs "quad" "pent" and "eyes" according to status)
1425                                         if (cl.stats[STAT_ITEMS] & IT_QUAD)
1426                                         {
1427                                                 if (temp[0])
1428                                                         strlcat(temp, " ", sizeof(temp));
1429                                                 strlcat(temp, "quad", sizeof(temp));
1430                                         }
1431                                         if (cl.stats[STAT_ITEMS] & IT_INVULNERABILITY)
1432                                         {
1433                                                 if (temp[0])
1434                                                         strlcat(temp, " ", sizeof(temp));
1435                                                 strlcat(temp, "pent", sizeof(temp));
1436                                         }
1437                                         if (cl.stats[STAT_ITEMS] & IT_INVISIBILITY)
1438                                         {
1439                                                 if (temp[0])
1440                                                         strlcat(temp, " ", sizeof(temp));
1441                                                 strlcat(temp, "eyes", sizeof(temp));
1442                                         }
1443                                         break;
1444                                 case 'w': // weapon status (outputs "SSG:NG:SNG:GL:RL:LG" with the text between : characters omitted if you lack the weapon)
1445                                         if (cl.stats[STAT_ITEMS] & IT_SUPER_SHOTGUN)
1446                                                 strlcat(temp, "SSG", sizeof(temp));
1447                                         strlcat(temp, ":", sizeof(temp));
1448                                         if (cl.stats[STAT_ITEMS] & IT_NAILGUN)
1449                                                 strlcat(temp, "NG", sizeof(temp));
1450                                         strlcat(temp, ":", sizeof(temp));
1451                                         if (cl.stats[STAT_ITEMS] & IT_SUPER_NAILGUN)
1452                                                 strlcat(temp, "SNG", sizeof(temp));
1453                                         strlcat(temp, ":", sizeof(temp));
1454                                         if (cl.stats[STAT_ITEMS] & IT_GRENADE_LAUNCHER)
1455                                                 strlcat(temp, "GL", sizeof(temp));
1456                                         strlcat(temp, ":", sizeof(temp));
1457                                         if (cl.stats[STAT_ITEMS] & IT_ROCKET_LAUNCHER)
1458                                                 strlcat(temp, "RL", sizeof(temp));
1459                                         strlcat(temp, ":", sizeof(temp));
1460                                         if (cl.stats[STAT_ITEMS] & IT_LIGHTNING)
1461                                                 strlcat(temp, "LG", sizeof(temp));
1462                                         break;
1463                                 default:
1464                                         // not a recognized macro, print it as-is...
1465                                         temp[0] = s[0];
1466                                         temp[1] = s[1];
1467                                         temp[2] = 0;
1468                                         break;
1469                                 }
1470                                 // write the resulting text
1471                                 SZ_Write(&cls.netcon->message, (unsigned char *)temp, strlen(temp));
1472                                 s += 2;
1473                                 continue;
1474                         }
1475                         MSG_WriteByte(&cls.netcon->message, *s);
1476                         s++;
1477                 }
1478                 MSG_WriteByte(&cls.netcon->message, 0);
1479         }
1480         else // any other command is passed on as-is
1481                 SZ_Write(&cls.netcon->message, (const unsigned char *)s, (int)strlen(s) + 1);
1482 }
1483
1484 /*
1485 ===================
1486 Cmd_ForwardToServer
1487
1488 Sends the entire command line over to the server
1489 ===================
1490 */
1491 void Cmd_ForwardToServer (void)
1492 {
1493         const char *s;
1494         if (!strcasecmp(Cmd_Argv(0), "cmd"))
1495         {
1496                 // we want to strip off "cmd", so just send the args
1497                 s = Cmd_Argc() > 1 ? Cmd_Args() : "";
1498         }
1499         else
1500         {
1501                 // we need to keep the command name, so send Cmd_Argv(0), a space and then Cmd_Args()
1502                 s = va("%s %s", Cmd_Argv(0), Cmd_Argc() > 1 ? Cmd_Args() : "");
1503         }
1504         // don't send an empty forward message if the user tries "cmd" by itself
1505         if (!s || !*s)
1506                 return;
1507         Cmd_ForwardStringToServer(s);
1508 }
1509
1510
1511 /*
1512 ================
1513 Cmd_CheckParm
1514
1515 Returns the position (1 to argc-1) in the command's argument list
1516 where the given parameter apears, or 0 if not present
1517 ================
1518 */
1519
1520 int Cmd_CheckParm (const char *parm)
1521 {
1522         int i;
1523
1524         if (!parm)
1525         {
1526                 Con_Printf ("Cmd_CheckParm: NULL");
1527                 return 0;
1528         }
1529
1530         for (i = 1; i < Cmd_Argc (); i++)
1531                 if (!strcasecmp (parm, Cmd_Argv (i)))
1532                         return i;
1533
1534         return 0;
1535 }
1536