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