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