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