]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cmd.c
When below 1 fps, the fps counter instead counts spf (seconds per frame). Note that...
[xonotic/darkplaces.git] / cmd.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // cmd.c -- Quake script command processing module
21
22 #include "quakedef.h"
23
24 #define MAX_ALIAS_NAME  32
25
26 typedef struct cmdalias_s
27 {
28         struct cmdalias_s *next;
29         char name[MAX_ALIAS_NAME];
30         char *value;
31 } cmdalias_t;
32
33 static cmdalias_t *cmd_alias;
34
35 static qboolean cmd_wait;
36
37 static mempool_t *cmd_mempool;
38
39 #define CMD_TOKENIZELENGTH 4096
40 static char cmd_tokenizebuffer[CMD_TOKENIZELENGTH];
41 static int cmd_tokenizebufferpos = 0;
42
43 //=============================================================================
44
45 /*
46 ============
47 Cmd_Wait_f
48
49 Causes execution of the remainder of the command buffer to be delayed until
50 next frame.  This allows commands like:
51 bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
52 ============
53 */
54 static void Cmd_Wait_f (void)
55 {
56         cmd_wait = true;
57 }
58
59 /*
60 =============================================================================
61
62                                                 COMMAND BUFFER
63
64 =============================================================================
65 */
66
67         // LordHavoc: inreased this from 8192 to 32768
68 static sizebuf_t        cmd_text;
69 static qbyte            cmd_text_buf[32768];
70
71 /*
72 ============
73 Cbuf_AddText
74
75 Adds command text at the end of the buffer
76 ============
77 */
78 void Cbuf_AddText (const char *text)
79 {
80         int             l;
81
82         l = (int)strlen (text);
83
84         if (cmd_text.cursize + l >= cmd_text.maxsize)
85         {
86                 Con_Print("Cbuf_AddText: overflow\n");
87                 return;
88         }
89
90         SZ_Write (&cmd_text, text, (int)strlen (text));
91 }
92
93
94 /*
95 ============
96 Cbuf_InsertText
97
98 Adds command text immediately after the current command
99 Adds a \n to the text
100 FIXME: actually change the command buffer to do less copying
101 ============
102 */
103 void Cbuf_InsertText (const char *text)
104 {
105         char    *temp;
106         int             templen;
107
108         // copy off any commands still remaining in the exec buffer
109         templen = cmd_text.cursize;
110         if (templen)
111         {
112                 temp = Mem_Alloc (tempmempool, templen);
113                 memcpy (temp, cmd_text.data, templen);
114                 SZ_Clear (&cmd_text);
115         }
116         else
117                 temp = NULL;
118
119         // add the entire text of the file
120         Cbuf_AddText (text);
121
122         // add the copied off data
123         if (temp != NULL)
124         {
125                 SZ_Write (&cmd_text, temp, templen);
126                 Mem_Free (temp);
127         }
128 }
129
130 /*
131 ============
132 Cbuf_Execute
133 ============
134 */
135 void Cbuf_Execute (void)
136 {
137         int i;
138         char *text;
139         char line[1024];
140         int quotes;
141
142         // LordHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
143         cmd_tokenizebufferpos = 0;
144
145         while (cmd_text.cursize)
146         {
147 // find a \n or ; line break
148                 text = (char *)cmd_text.data;
149
150                 quotes = 0;
151                 for (i=0 ; i< cmd_text.cursize ; i++)
152                 {
153                         if (text[i] == '"')
154                                 quotes ^= 1;
155                         if ( !quotes &&  text[i] == ';')
156                                 break;  // don't break if inside a quoted string
157                         if (text[i] == '\r' || text[i] == '\n')
158                                 break;
159                 }
160
161                 memcpy (line, text, i);
162                 line[i] = 0;
163
164 // delete the text from the command buffer and move remaining commands down
165 // this is necessary because commands (exec, alias) can insert data at the
166 // beginning of the text buffer
167
168                 if (i == cmd_text.cursize)
169                         cmd_text.cursize = 0;
170                 else
171                 {
172                         i++;
173                         cmd_text.cursize -= i;
174                         memcpy (cmd_text.data, text+i, cmd_text.cursize);
175                 }
176
177 // execute the command line
178                 Cmd_ExecuteString (line, src_command);
179
180                 if (cmd_wait)
181                 {       // skip out while text still remains in buffer, leaving it
182                         // for next frame
183                         cmd_wait = false;
184                         break;
185                 }
186         }
187 }
188
189 /*
190 ==============================================================================
191
192                                                 SCRIPT COMMANDS
193
194 ==============================================================================
195 */
196
197 /*
198 ===============
199 Cmd_StuffCmds_f
200
201 Adds command line parameters as script statements
202 Commands lead with a +, and continue until a - or another +
203 quake +prog jctest.qp +cmd amlev1
204 quake -nosound +cmd amlev1
205 ===============
206 */
207 qboolean host_stuffcmdsrun = false;
208 void Cmd_StuffCmds_f (void)
209 {
210         int             i, j, l;
211         // this is per command, and bounds checked (no buffer overflows)
212         char    build[2048];
213
214         if (Cmd_Argc () != 1)
215         {
216                 Con_Print("stuffcmds : execute command line parameters\n");
217                 return;
218         }
219
220         host_stuffcmdsrun = true;
221         for (i = 0;i < com_argc;i++)
222         {
223                 if (com_argv[i] && com_argv[i][0] == '+' && (com_argv[i][1] < '0' || com_argv[i][1] > '9'))
224                 {
225                         l = 0;
226                         j = 1;
227                         while (com_argv[i][j])
228                                 build[l++] = com_argv[i][j++];
229                         i++;
230                         for (;i < com_argc;i++)
231                         {
232                                 if (!com_argv[i])
233                                         continue;
234                                 if ((com_argv[i][0] == '+' || com_argv[i][0] == '-') && (com_argv[i][1] < '0' || com_argv[i][1] > '9'))
235                                         break;
236                                 if (l + strlen(com_argv[i]) + 5 > sizeof(build))
237                                         break;
238                                 build[l++] = ' ';
239                                 build[l++] = '\"';
240                                 for (j = 0;com_argv[i][j];j++)
241                                         build[l++] = com_argv[i][j];
242                                 build[l++] = '\"';
243                         }
244                         build[l++] = '\n';
245                         build[l++] = 0;
246                         Cbuf_InsertText (build);
247                         i--;
248                 }
249         }
250 }
251
252
253 /*
254 ===============
255 Cmd_Exec_f
256 ===============
257 */
258 static void Cmd_Exec_f (void)
259 {
260         char *f;
261
262         if (Cmd_Argc () != 2)
263         {
264                 Con_Print("exec <filename> : execute a script file\n");
265                 return;
266         }
267
268         f = (char *)FS_LoadFile (Cmd_Argv(1), tempmempool, false);
269         if (!f)
270         {
271                 Con_Printf("couldn't exec %s\n",Cmd_Argv(1));
272                 return;
273         }
274         Con_DPrintf("execing %s\n",Cmd_Argv(1));
275
276         Cbuf_InsertText (f);
277         Mem_Free(f);
278 }
279
280
281 /*
282 ===============
283 Cmd_Echo_f
284
285 Just prints the rest of the line to the console
286 ===============
287 */
288 static void Cmd_Echo_f (void)
289 {
290         int             i;
291
292         for (i=1 ; i<Cmd_Argc() ; i++)
293                 Con_Printf("%s ",Cmd_Argv(i));
294         Con_Print("\n");
295 }
296
297 /*
298 ===============
299 Cmd_Alias_f
300
301 Creates a new command that executes a command string (possibly ; seperated)
302 ===============
303 */
304 static void Cmd_Alias_f (void)
305 {
306         cmdalias_t      *a;
307         char            cmd[1024];
308         int                     i, c;
309         const char              *s;
310
311         if (Cmd_Argc() == 1)
312         {
313                 Con_Print("Current alias commands:\n");
314                 for (a = cmd_alias ; a ; a=a->next)
315                         Con_Printf("%s : %s\n", a->name, a->value);
316                 return;
317         }
318
319         s = Cmd_Argv(1);
320         if (strlen(s) >= MAX_ALIAS_NAME)
321         {
322                 Con_Print("Alias name is too long\n");
323                 return;
324         }
325
326         // if the alias already exists, reuse it
327         for (a = cmd_alias ; a ; a=a->next)
328         {
329                 if (!strcmp(s, a->name))
330                 {
331                         Z_Free (a->value);
332                         break;
333                 }
334         }
335
336         if (!a)
337         {
338                 cmdalias_t *prev, *current;
339
340                 a = Z_Malloc (sizeof(cmdalias_t));
341                 strlcpy (a->name, s, sizeof (a->name));
342                 // insert it at the right alphanumeric position
343                 for( prev = NULL, current = cmd_alias ; current && strcmp( current->name, a->name ) < 0 ; prev = current, current = current->next )
344                         ;
345                 if( prev ) {
346                         prev->next = a;
347                 } else {
348                         cmd_alias = a;
349                 }
350                 a->next = current;
351         }
352
353
354 // copy the rest of the command line
355         cmd[0] = 0;             // start out with a null string
356         c = Cmd_Argc();
357         for (i=2 ; i< c ; i++)
358         {
359                 strlcat (cmd, Cmd_Argv(i), sizeof (cmd));
360                 if (i != c)
361                         strlcat (cmd, " ", sizeof (cmd));
362         }
363         strlcat (cmd, "\n", sizeof (cmd));
364
365         a->value = Z_Malloc (strlen (cmd) + 1);
366         strcpy (a->value, cmd);
367 }
368
369 /*
370 =============================================================================
371
372                                         COMMAND EXECUTION
373
374 =============================================================================
375 */
376
377 typedef struct cmd_function_s
378 {
379         struct cmd_function_s *next;
380         const char *name;
381         xcommand_t function;
382 } cmd_function_t;
383
384
385 #define MAX_ARGS                80
386
387 static int cmd_argc;
388 static const char *cmd_argv[MAX_ARGS];
389 static const char *cmd_null_string = "";
390 static const char *cmd_args = NULL;
391
392 cmd_source_t cmd_source;
393
394
395 static cmd_function_t *cmd_functions;           // possible commands to execute
396
397 /*
398 ============
399 Cmd_ExecuteAlias
400
401 Called for aliases and fills in the alias into the cbuffer
402 ============
403 */
404 static void Cmd_ExecuteAlias (cmdalias_t *alias)
405 {
406 #define ALIAS_BUFFER 1024
407         static char buffer[ ALIAS_BUFFER + 2 ];
408         const char *in;
409         char *out;
410         unsigned outlen;
411         int inquote;
412
413         in = alias->value;
414         out = buffer;
415         outlen = 0;
416         inquote = 0;
417
418         while( *in && outlen < ALIAS_BUFFER )
419         {
420                 if( *in == '"' )
421                 {
422                         inquote ^= 1;
423                 }
424                 else if( *in == '$' && !inquote )
425                 {
426                         // $* is replaced with all formal parameters, $num is parsed as an argument (or as $num if there arent enough parameters), $bla becomes $bla and $$bla becomes $$bla
427                         // read over the $
428                         in++;
429                         if( *in == '*' )
430                         {
431                                 const char *linein = Cmd_Args();
432                                 // include all params
433                                 while( *linein && outlen < ALIAS_BUFFER ) {
434                                         *out++ = *linein++;
435                                         outlen++;
436                                 }
437
438                                 in++;
439                         } else {
440                                 char *nexttoken;
441                                 int argnum;
442
443                                 argnum = strtol( in, &nexttoken, 10 );
444
445                                 if( 0 < argnum && argnum < Cmd_Argc() )
446                                 {
447                                         const char *param = Cmd_Argv( argnum );
448                                         while( *param && outlen < ALIAS_BUFFER ) {
449                                                 *out++ = *param++;
450                                                 outlen++;
451                                         }
452                                         in = nexttoken;
453                                 }
454                                 else if( argnum >= Cmd_Argc() )
455                                 {
456                                         Con_Printf( "Warning: Not enough parameters passed to alias '%s', at least %i expected:\n    %s\n", alias->name, argnum, alias->value );
457                                         *out++ = '$';
458                                         outlen++;
459                                 }
460                                 // not a number
461                                 else if( argnum == 0 )
462                                 {
463                                         *out++ = '$';
464                                         outlen++;
465                                 }
466                         }
467                 } else {
468                         *out++ = *in++;
469                         outlen++;
470                 }
471         }
472         *out++ = '\n';
473         *out++ = 0;
474         Cbuf_AddText( buffer );
475 }
476
477 /*
478 ========
479 Cmd_List
480
481         CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
482         Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
483
484 ========
485 */
486 static void Cmd_List_f (void)
487 {
488         cmd_function_t *cmd;
489         const char *partial;
490         int len, count;
491
492         if (Cmd_Argc() > 1)
493         {
494                 partial = Cmd_Argv (1);
495                 len = (int)strlen(partial);
496         }
497         else
498         {
499                 partial = NULL;
500                 len = 0;
501         }
502
503         count = 0;
504         for (cmd = cmd_functions; cmd; cmd = cmd->next)
505         {
506                 if (partial && strncmp(partial, cmd->name, len))
507                         continue;
508                 Con_Printf("%s\n", cmd->name);
509                 count++;
510         }
511
512         Con_Printf("%i Command%s", count, (count > 1) ? "s" : "");
513         if (partial)
514                 Con_Printf(" beginning with \"%s\"", partial);
515
516         Con_Print("\n\n");
517 }
518
519 /*
520 ============
521 Cmd_Init
522 ============
523 */
524 void Cmd_Init (void)
525 {
526         cmd_mempool = Mem_AllocPool("commands", 0, NULL);
527         // space for commands and script files
528         cmd_text.data = cmd_text_buf;
529         cmd_text.maxsize = sizeof(cmd_text_buf);
530         cmd_text.cursize = 0;
531 }
532
533 void Cmd_Init_Commands (void)
534 {
535 //
536 // register our commands
537 //
538         Cmd_AddCommand ("stuffcmds",Cmd_StuffCmds_f);
539         Cmd_AddCommand ("exec",Cmd_Exec_f);
540         Cmd_AddCommand ("echo",Cmd_Echo_f);
541         Cmd_AddCommand ("alias",Cmd_Alias_f);
542         Cmd_AddCommand ("cmd", Cmd_ForwardToServer);
543         Cmd_AddCommand ("wait", Cmd_Wait_f);
544         Cmd_AddCommand ("cmdlist", Cmd_List_f);         // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
545         Cmd_AddCommand ("cvarlist", Cvar_List_f);       // 2000-01-09 CmdList, CvarList commands
546                                                                                                 // By Matthias "Maddes" Buecher
547         Cmd_AddCommand ("set", Cvar_Set_f);
548         Cmd_AddCommand ("seta", Cvar_SetA_f);
549 }
550
551 /*
552 ============
553 Cmd_Shutdown
554 ============
555 */
556 void Cmd_Shutdown(void)
557 {
558         Mem_FreePool(&cmd_mempool);
559 }
560
561 /*
562 ============
563 Cmd_Argc
564 ============
565 */
566 int             Cmd_Argc (void)
567 {
568         return cmd_argc;
569 }
570
571 /*
572 ============
573 Cmd_Argv
574 ============
575 */
576 const char *Cmd_Argv (int arg)
577 {
578         if (arg >= cmd_argc )
579                 return cmd_null_string;
580         return cmd_argv[arg];
581 }
582
583 /*
584 ============
585 Cmd_Args
586 ============
587 */
588 const char *Cmd_Args (void)
589 {
590         return cmd_args;
591 }
592
593
594 /*
595 ============
596 Cmd_TokenizeString
597
598 Parses the given string into command line tokens.
599 ============
600 */
601 static void Cmd_TokenizeString (const char *text)
602 {
603         int l;
604
605         cmd_argc = 0;
606         cmd_args = NULL;
607
608         while (1)
609         {
610                 // skip whitespace up to a /n
611                 while (*text && *text <= ' ' && *text != '\r' && *text != '\n')
612                         text++;
613
614                 // line endings:
615                 // UNIX: \n
616                 // Mac: \r
617                 // Windows: \r\n
618                 if (*text == '\n' || *text == '\r')
619                 {
620                         // a newline seperates commands in the buffer
621                         if (*text == '\r' && text[1] == '\n')
622                                 text++;
623                         text++;
624                         break;
625                 }
626
627                 if (!*text)
628                         return;
629
630                 if (cmd_argc == 1)
631                          cmd_args = text;
632
633                 if (!COM_ParseTokenConsole(&text))
634                         return;
635
636                 // check for $cvar
637                 // (perhaps use another target buffer?)
638                 if (com_token[0] == '$' && com_token[1])
639                 {
640                         cvar_t *cvar;
641
642                         cvar = Cvar_FindVar(&com_token[1]);
643                         if (cvar)
644                         {
645                                 strcpy(com_token, cvar->string);
646                         }
647                         else if( com_token[1] == '$' )
648                         {
649                                 // remove the first $
650                                 char *pos;
651
652                                 for( pos = com_token ; *pos ; pos++ )
653                                 {
654                                         *pos = *(pos + 1);
655                                 }
656                         }
657                 }
658
659                 if (cmd_argc < MAX_ARGS)
660                 {
661                         l = (int)strlen(com_token) + 1;
662                         if (cmd_tokenizebufferpos + l > CMD_TOKENIZELENGTH)
663                         {
664                                 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguements\n", CMD_TOKENIZELENGTH);
665                                 break;
666                         }
667                         strcpy (cmd_tokenizebuffer + cmd_tokenizebufferpos, com_token);
668                         cmd_argv[cmd_argc] = cmd_tokenizebuffer + cmd_tokenizebufferpos;
669                         cmd_tokenizebufferpos += l;
670                         cmd_argc++;
671                 }
672         }
673 }
674
675
676 /*
677 ============
678 Cmd_AddCommand
679 ============
680 */
681 void Cmd_AddCommand (const char *cmd_name, xcommand_t function)
682 {
683         cmd_function_t *cmd;
684         cmd_function_t *prev, *current;
685
686 // fail if the command is a variable name
687         if (Cvar_FindVar( cmd_name ))
688         {
689                 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
690                 return;
691         }
692
693 // fail if the command already exists
694         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
695         {
696                 if (!strcmp (cmd_name, cmd->name))
697                 {
698                         Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
699                         return;
700                 }
701         }
702
703         cmd = Mem_Alloc(cmd_mempool, sizeof(cmd_function_t));
704         cmd->name = cmd_name;
705         cmd->function = function;
706         cmd->next = cmd_functions;
707
708 // insert it at the right alphanumeric position
709         for( prev = NULL, current = cmd_functions ; current && strcmp( current->name, cmd->name ) < 0 ; prev = current, current = current->next )
710                 ;
711         if( prev ) {
712                 prev->next = cmd;
713         } else {
714                 cmd_functions = cmd;
715         }
716         cmd->next = current;
717 }
718
719 /*
720 ============
721 Cmd_Exists
722 ============
723 */
724 qboolean Cmd_Exists (const char *cmd_name)
725 {
726         cmd_function_t  *cmd;
727
728         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
729                 if (!strcmp (cmd_name,cmd->name))
730                         return true;
731
732         return false;
733 }
734
735
736 /*
737 ============
738 Cmd_CompleteCommand
739 ============
740 */
741 const char *Cmd_CompleteCommand (const char *partial)
742 {
743         cmd_function_t *cmd;
744         size_t len;
745
746         len = strlen(partial);
747
748         if (!len)
749                 return NULL;
750
751 // check functions
752         for (cmd = cmd_functions; cmd; cmd = cmd->next)
753                 if (!strncmp(partial, cmd->name, len))
754                         return cmd->name;
755
756         return NULL;
757 }
758
759 /*
760         Cmd_CompleteCountPossible
761
762         New function for tab-completion system
763         Added by EvilTypeGuy
764         Thanks to Fett erich@heintz.com
765         Thanks to taniwha
766
767 */
768 int Cmd_CompleteCountPossible (const char *partial)
769 {
770         cmd_function_t *cmd;
771         size_t len;
772         int h;
773
774         h = 0;
775         len = strlen(partial);
776
777         if (!len)
778                 return 0;
779
780         // Loop through the command list and count all partial matches
781         for (cmd = cmd_functions; cmd; cmd = cmd->next)
782                 if (!strncasecmp(partial, cmd->name, len))
783                         h++;
784
785         return h;
786 }
787
788 /*
789         Cmd_CompleteBuildList
790
791         New function for tab-completion system
792         Added by EvilTypeGuy
793         Thanks to Fett erich@heintz.com
794         Thanks to taniwha
795
796 */
797 const char **Cmd_CompleteBuildList (const char *partial)
798 {
799         cmd_function_t *cmd;
800         size_t len = 0;
801         size_t bpos = 0;
802         size_t sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (const char *);
803         const char **buf;
804
805         len = strlen(partial);
806         buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
807         // Loop through the alias list and print all matches
808         for (cmd = cmd_functions; cmd; cmd = cmd->next)
809                 if (!strncasecmp(partial, cmd->name, len))
810                         buf[bpos++] = cmd->name;
811
812         buf[bpos] = NULL;
813         return buf;
814 }
815
816 /*
817         Cmd_CompleteAlias
818
819         New function for tab-completion system
820         Added by EvilTypeGuy
821         Thanks to Fett erich@heintz.com
822         Thanks to taniwha
823
824 */
825 const char *Cmd_CompleteAlias (const char *partial)
826 {
827         cmdalias_t *alias;
828         size_t len;
829
830         len = strlen(partial);
831
832         if (!len)
833                 return NULL;
834
835         // Check functions
836         for (alias = cmd_alias; alias; alias = alias->next)
837                 if (!strncasecmp(partial, alias->name, len))
838                         return alias->name;
839
840         return NULL;
841 }
842
843 /*
844         Cmd_CompleteAliasCountPossible
845
846         New function for tab-completion system
847         Added by EvilTypeGuy
848         Thanks to Fett erich@heintz.com
849         Thanks to taniwha
850
851 */
852 int Cmd_CompleteAliasCountPossible (const char *partial)
853 {
854         cmdalias_t      *alias;
855         size_t          len;
856         int                     h;
857
858         h = 0;
859
860         len = strlen(partial);
861
862         if (!len)
863                 return 0;
864
865         // Loop through the command list and count all partial matches
866         for (alias = cmd_alias; alias; alias = alias->next)
867                 if (!strncasecmp(partial, alias->name, len))
868                         h++;
869
870         return h;
871 }
872
873 /*
874         Cmd_CompleteAliasBuildList
875
876         New function for tab-completion system
877         Added by EvilTypeGuy
878         Thanks to Fett erich@heintz.com
879         Thanks to taniwha
880
881 */
882 const char **Cmd_CompleteAliasBuildList (const char *partial)
883 {
884         cmdalias_t *alias;
885         size_t len = 0;
886         size_t bpos = 0;
887         size_t sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (const char *);
888         const char **buf;
889
890         len = strlen(partial);
891         buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
892         // Loop through the alias list and print all matches
893         for (alias = cmd_alias; alias; alias = alias->next)
894                 if (!strncasecmp(partial, alias->name, len))
895                         buf[bpos++] = alias->name;
896
897         buf[bpos] = NULL;
898         return buf;
899 }
900
901 /*
902 ============
903 Cmd_ExecuteString
904
905 A complete command line has been parsed, so try to execute it
906 FIXME: lookupnoadd the token to speed search?
907 ============
908 */
909 void Cmd_ExecuteString (const char *text, cmd_source_t src)
910 {
911         int oldpos;
912         cmd_function_t *cmd;
913         cmdalias_t *a;
914
915         oldpos = cmd_tokenizebufferpos;
916         cmd_source = src;
917         Cmd_TokenizeString (text);
918
919 // execute the command line
920         if (!Cmd_Argc())
921         {
922                 cmd_tokenizebufferpos = oldpos;
923                 return;         // no tokens
924         }
925
926 // check functions
927         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
928         {
929                 if (!strcasecmp (cmd_argv[0],cmd->name))
930                 {
931                         cmd->function ();
932                         cmd_tokenizebufferpos = oldpos;
933                         return;
934                 }
935         }
936
937 // check alias
938         for (a=cmd_alias ; a ; a=a->next)
939         {
940                 if (!strcasecmp (cmd_argv[0], a->name))
941                 {
942                         Cmd_ExecuteAlias(a);
943                         cmd_tokenizebufferpos = oldpos;
944                         return;
945                 }
946         }
947
948 // check cvars
949         if (!Cvar_Command () && host_framecount > 0)
950                 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(0));
951
952         cmd_tokenizebufferpos = oldpos;
953 }
954
955
956 /*
957 ===================
958 Cmd_ForwardStringToServer
959
960 Sends an entire command string over to the server, unprocessed
961 ===================
962 */
963 void Cmd_ForwardStringToServer (const char *s)
964 {
965         if (cls.state != ca_connected)
966         {
967                 Con_Printf("Can't \"%s\", not connected\n", s);
968                 return;
969         }
970
971         if (cls.demoplayback)
972                 return;         // not really connected
973
974         // LordHavoc: thanks to Fuh for bringing the pure evil of SZ_Print to my
975         // attention, it has been eradicated from here, its only (former) use in
976         // all of darkplaces.
977         MSG_WriteByte(&cls.message, clc_stringcmd);
978         SZ_Write(&cls.message, s, (int)strlen(s) + 1);
979 }
980
981 /*
982 ===================
983 Cmd_ForwardToServer
984
985 Sends the entire command line over to the server
986 ===================
987 */
988 void Cmd_ForwardToServer (void)
989 {
990         const char *s;
991         if (!strcasecmp(Cmd_Argv(0), "cmd"))
992         {
993                 // we want to strip off "cmd", so just send the args
994                 s = Cmd_Argc() > 1 ? Cmd_Args() : "";
995         }
996         else
997         {
998                 // we need to keep the command name, so send Cmd_Argv(0), a space and then Cmd_Args()
999                 s = va("%s %s", Cmd_Argv(0), Cmd_Argc() > 1 ? Cmd_Args() : "");
1000         }
1001         // don't send an empty forward message if the user tries "cmd" by itself
1002         if (!s || !*s)
1003                 return;
1004         Cmd_ForwardStringToServer(s);
1005 }
1006
1007
1008 /*
1009 ================
1010 Cmd_CheckParm
1011
1012 Returns the position (1 to argc-1) in the command's argument list
1013 where the given parameter apears, or 0 if not present
1014 ================
1015 */
1016
1017 int Cmd_CheckParm (const char *parm)
1018 {
1019         int i;
1020
1021         if (!parm)
1022         {
1023                 Con_Printf ("Cmd_CheckParm: NULL");
1024                 return 0;
1025         }
1026
1027         for (i = 1; i < Cmd_Argc (); i++)
1028                 if (!strcasecmp (parm, Cmd_Argv (i)))
1029                         return i;
1030
1031         return 0;
1032 }
1033