]> git.xonotic.org Git - xonotic/darkplaces.git/blob - cmd.c
Merged the $ handling into one preprocessor function.
[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 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 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, NULL);
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 = (cmdalias_t *)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 = (char *)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 Cmd_PreprocessString
399
400 Preprocesses strings and replaces $*, $param#, $cvar accordingly
401 */
402 static void Cmd_PreprocessString( const char *intext, char *outtext, unsigned maxoutlen, cmdalias_t *alias ) {
403         const char *in;
404         char *out;
405         unsigned outlen;
406         int inquote;
407
408         // HACK?
409         if( maxoutlen == 0 ) {
410                 return;
411         }
412         maxoutlen--; // because of \0
413
414         in = intext;
415         out = outtext;
416         outlen = 0;
417         inquote = 0;
418
419         while( *in && outlen < maxoutlen ) {
420                 if( *in == '$' && !inquote ) {
421                         // read over the $
422                         in++;
423                         // $* 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
424                         if( *in == '*' && alias ) {
425                                 const char *linein = Cmd_Args();
426                                 // include all params
427                                 if (linein) {
428                                         while( *linein && outlen < maxoutlen ) {
429                                                 *out++ = *linein++;
430                                                 outlen++;
431                                         }
432                                 }
433
434                                 in++;
435                         } else if( '0' <= *in && *in <= '9' && alias ) {
436                                 char *nexttoken;
437                                 int argnum;
438
439                                 argnum = strtol( in, &nexttoken, 10 );
440
441                                 if( 0 < argnum && argnum < Cmd_Argc() ) {
442                                         const char *param = Cmd_Argv( argnum );
443                                         while( *param && outlen < maxoutlen ) {
444                                                 *out++ = *param++;
445                                                 outlen++;
446                                         }
447                                         in = nexttoken;
448                                 } else if( argnum >= Cmd_Argc() ) {
449                                         Con_Printf( "Warning: Not enough parameters passed to alias '%s', at least %i expected:\n    %s\n", alias->name, argnum, alias->value );
450                                         *out++ = '$';
451                                         outlen++;
452                                 }
453                         } else {
454                                 cvar_t *cvar;
455                                 const char *tempin = in;
456
457                                 COM_ParseTokenConsole( &tempin );
458                                 cvar = Cvar_FindVar(&com_token[0]);
459                                 if (cvar) {
460                                         const char *cvarcontent = cvar->string;
461                                         while( *cvarcontent && outlen < maxoutlen ) {
462                                                 *out++ = *cvarcontent++;
463                                                 outlen++;
464                                         }
465                                         in = tempin;
466                                 } else if( com_token[0] == '$' ) {
467                                         // remove the first $
468                                         char *pos = com_token;
469                                         while( *pos && outlen < maxoutlen ) {
470                                                 *out++ = *pos++;
471                                                 outlen++;
472                                         }
473                                         in = tempin;
474                                 }
475                         }
476                 } else {
477                         if( *in == '"' ) {
478                                 inquote ^= 1;
479                         } 
480                         *out++ = *in++;
481                         outlen++;
482                 }
483         }
484         *out = 0;
485 }
486
487 /*
488 ============
489 Cmd_ExecuteAlias
490
491 Called for aliases and fills in the alias into the cbuffer
492 ============
493 */
494 static void Cmd_ExecuteAlias (cmdalias_t *alias)
495 {
496         /*
497 #define ALIAS_BUFFER 1024
498         static char buffer[ ALIAS_BUFFER + 2 ];
499         const char *in;
500         char *out;
501         unsigned outlen;
502         int inquote;
503
504         in = alias->value;
505         out = buffer;
506         outlen = 0;
507         inquote = 0;
508
509         while( *in && outlen < ALIAS_BUFFER )
510         {
511                 if( *in == '"' )
512                 {
513                         inquote ^= 1;
514                 }
515                 else if( *in == '$' && !inquote )
516                 {
517                         // $* 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
518                         // read over the $
519                         in++;
520                         if( *in == '*' )
521                         {
522                                 const char *linein = Cmd_Args();
523                                 // include all params
524                                 if (linein) {
525                                         while( *linein && outlen < ALIAS_BUFFER ) {
526                                                 *out++ = *linein++;
527                                                 outlen++;
528                                         }
529                                 }
530
531                                 in++;
532                         } else {
533                                 char *nexttoken;
534                                 int argnum;
535
536                                 argnum = strtol( in, &nexttoken, 10 );
537
538                                 if( 0 < argnum && argnum < Cmd_Argc() )
539                                 {
540                                         const char *param = Cmd_Argv( argnum );
541                                         while( *param && outlen < ALIAS_BUFFER ) {
542                                                 *out++ = *param++;
543                                                 outlen++;
544                                         }
545                                         in = nexttoken;
546                                 }
547                                 else if( argnum >= Cmd_Argc() )
548                                 {
549                                         Con_Printf( "Warning: Not enough parameters passed to alias '%s', at least %i expected:\n    %s\n", alias->name, argnum, alias->value );
550                                         *out++ = '$';
551                                         outlen++;
552                                 }
553                                 // not a number
554                                 else if( argnum == 0 )
555                                 {
556                                         *out++ = '$';
557                                         outlen++;
558                                 }
559                         }
560                 } else {
561                         *out++ = *in++;
562                         outlen++;
563                 }
564         }
565         *out++ = '\n';
566         *out++ = 0;*/
567 #define ALIAS_BUFFER 1024
568         static char buffer[ ALIAS_BUFFER + 2 ];
569         Cmd_PreprocessString( alias->value, buffer, ALIAS_BUFFER, alias );
570         Cbuf_AddText( buffer );
571 }
572
573 /*
574 ========
575 Cmd_List
576
577         CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
578         Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
579
580 ========
581 */
582 static void Cmd_List_f (void)
583 {
584         cmd_function_t *cmd;
585         const char *partial;
586         int len, count;
587
588         if (Cmd_Argc() > 1)
589         {
590                 partial = Cmd_Argv (1);
591                 len = (int)strlen(partial);
592         }
593         else
594         {
595                 partial = NULL;
596                 len = 0;
597         }
598
599         count = 0;
600         for (cmd = cmd_functions; cmd; cmd = cmd->next)
601         {
602                 if (partial && strncmp(partial, cmd->name, len))
603                         continue;
604                 Con_Printf("%s\n", cmd->name);
605                 count++;
606         }
607
608         Con_Printf("%i Command%s", count, (count > 1) ? "s" : "");
609         if (partial)
610                 Con_Printf(" beginning with \"%s\"", partial);
611
612         Con_Print("\n\n");
613 }
614
615 /*
616 ============
617 Cmd_Init
618 ============
619 */
620 void Cmd_Init (void)
621 {
622         cmd_mempool = Mem_AllocPool("commands", 0, NULL);
623         // space for commands and script files
624         cmd_text.data = cmd_text_buf;
625         cmd_text.maxsize = sizeof(cmd_text_buf);
626         cmd_text.cursize = 0;
627 }
628
629 void Cmd_Init_Commands (void)
630 {
631 //
632 // register our commands
633 //
634         Cmd_AddCommand ("stuffcmds",Cmd_StuffCmds_f);
635         Cmd_AddCommand ("exec",Cmd_Exec_f);
636         Cmd_AddCommand ("echo",Cmd_Echo_f);
637         Cmd_AddCommand ("alias",Cmd_Alias_f);
638         Cmd_AddCommand ("cmd", Cmd_ForwardToServer);
639         Cmd_AddCommand ("wait", Cmd_Wait_f);
640         Cmd_AddCommand ("cmdlist", Cmd_List_f);         // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
641         Cmd_AddCommand ("cvarlist", Cvar_List_f);       // 2000-01-09 CmdList, CvarList commands
642                                                                                                 // By Matthias "Maddes" Buecher
643         Cmd_AddCommand ("set", Cvar_Set_f);
644         Cmd_AddCommand ("seta", Cvar_SetA_f);
645 }
646
647 /*
648 ============
649 Cmd_Shutdown
650 ============
651 */
652 void Cmd_Shutdown(void)
653 {
654         Mem_FreePool(&cmd_mempool);
655 }
656
657 /*
658 ============
659 Cmd_Argc
660 ============
661 */
662 int             Cmd_Argc (void)
663 {
664         return cmd_argc;
665 }
666
667 /*
668 ============
669 Cmd_Argv
670 ============
671 */
672 const char *Cmd_Argv (int arg)
673 {
674         if (arg >= cmd_argc )
675                 return cmd_null_string;
676         return cmd_argv[arg];
677 }
678
679 /*
680 ============
681 Cmd_Args
682 ============
683 */
684 const char *Cmd_Args (void)
685 {
686         return cmd_args;
687 }
688
689
690 /*
691 ============
692 Cmd_TokenizeString
693
694 Parses the given string into command line tokens.
695 ============
696 */
697 // AK: This function should only be called from ExcuteString because the current design is a bit of an hack
698 static void Cmd_TokenizeString (const char *text)
699 {
700         int l;
701
702         cmd_argc = 0;
703         cmd_args = NULL;
704
705         while (1)
706         {
707                 // skip whitespace up to a /n
708                 while (*text && *text <= ' ' && *text != '\r' && *text != '\n')
709                         text++;
710
711                 // line endings:
712                 // UNIX: \n
713                 // Mac: \r
714                 // Windows: \r\n
715                 if (*text == '\n' || *text == '\r')
716                 {
717                         // a newline separates commands in the buffer
718                         if (*text == '\r' && text[1] == '\n')
719                                 text++;
720                         text++;
721                         break;
722                 }
723
724                 if (!*text)
725                         return;
726
727                 if (cmd_argc == 1)
728                         cmd_args = text;
729
730                 if (!COM_ParseTokenConsole(&text))
731                         return;
732
733                 if (cmd_argc < MAX_ARGS)
734                 {
735                         l = (int)strlen(com_token) + 1;
736                         if (cmd_tokenizebufferpos + l > CMD_TOKENIZELENGTH)
737                         {
738                                 Con_Printf("Cmd_TokenizeString: ran out of %i character buffer space for command arguements\n", CMD_TOKENIZELENGTH);
739                                 break;
740                         }
741                         strcpy (cmd_tokenizebuffer + cmd_tokenizebufferpos, com_token);
742                         cmd_argv[cmd_argc] = cmd_tokenizebuffer + cmd_tokenizebufferpos;
743                         cmd_tokenizebufferpos += l;
744                         cmd_argc++;
745                 }
746         }
747 }
748
749
750 /*
751 ============
752 Cmd_AddCommand
753 ============
754 */
755 void Cmd_AddCommand (const char *cmd_name, xcommand_t function)
756 {
757         cmd_function_t *cmd;
758         cmd_function_t *prev, *current;
759
760 // fail if the command is a variable name
761         if (Cvar_FindVar( cmd_name ))
762         {
763                 Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
764                 return;
765         }
766
767 // fail if the command already exists
768         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
769         {
770                 if (!strcmp (cmd_name, cmd->name))
771                 {
772                         Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name);
773                         return;
774                 }
775         }
776
777         cmd = (cmd_function_t *)Mem_Alloc(cmd_mempool, sizeof(cmd_function_t));
778         cmd->name = cmd_name;
779         cmd->function = function;
780         cmd->next = cmd_functions;
781
782 // insert it at the right alphanumeric position
783         for( prev = NULL, current = cmd_functions ; current && strcmp( current->name, cmd->name ) < 0 ; prev = current, current = current->next )
784                 ;
785         if( prev ) {
786                 prev->next = cmd;
787         } else {
788                 cmd_functions = cmd;
789         }
790         cmd->next = current;
791 }
792
793 /*
794 ============
795 Cmd_Exists
796 ============
797 */
798 qboolean Cmd_Exists (const char *cmd_name)
799 {
800         cmd_function_t  *cmd;
801
802         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
803                 if (!strcmp (cmd_name,cmd->name))
804                         return true;
805
806         return false;
807 }
808
809
810 /*
811 ============
812 Cmd_CompleteCommand
813 ============
814 */
815 const char *Cmd_CompleteCommand (const char *partial)
816 {
817         cmd_function_t *cmd;
818         size_t len;
819
820         len = strlen(partial);
821
822         if (!len)
823                 return NULL;
824
825 // check functions
826         for (cmd = cmd_functions; cmd; cmd = cmd->next)
827                 if (!strncmp(partial, cmd->name, len))
828                         return cmd->name;
829
830         return NULL;
831 }
832
833 /*
834         Cmd_CompleteCountPossible
835
836         New function for tab-completion system
837         Added by EvilTypeGuy
838         Thanks to Fett erich@heintz.com
839         Thanks to taniwha
840
841 */
842 int Cmd_CompleteCountPossible (const char *partial)
843 {
844         cmd_function_t *cmd;
845         size_t len;
846         int h;
847
848         h = 0;
849         len = strlen(partial);
850
851         if (!len)
852                 return 0;
853
854         // Loop through the command list and count all partial matches
855         for (cmd = cmd_functions; cmd; cmd = cmd->next)
856                 if (!strncasecmp(partial, cmd->name, len))
857                         h++;
858
859         return h;
860 }
861
862 /*
863         Cmd_CompleteBuildList
864
865         New function for tab-completion system
866         Added by EvilTypeGuy
867         Thanks to Fett erich@heintz.com
868         Thanks to taniwha
869
870 */
871 const char **Cmd_CompleteBuildList (const char *partial)
872 {
873         cmd_function_t *cmd;
874         size_t len = 0;
875         size_t bpos = 0;
876         size_t sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (const char *);
877         const char **buf;
878
879         len = strlen(partial);
880         buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
881         // Loop through the alias list and print all matches
882         for (cmd = cmd_functions; cmd; cmd = cmd->next)
883                 if (!strncasecmp(partial, cmd->name, len))
884                         buf[bpos++] = cmd->name;
885
886         buf[bpos] = NULL;
887         return buf;
888 }
889
890 /*
891         Cmd_CompleteAlias
892
893         New function for tab-completion system
894         Added by EvilTypeGuy
895         Thanks to Fett erich@heintz.com
896         Thanks to taniwha
897
898 */
899 const char *Cmd_CompleteAlias (const char *partial)
900 {
901         cmdalias_t *alias;
902         size_t len;
903
904         len = strlen(partial);
905
906         if (!len)
907                 return NULL;
908
909         // Check functions
910         for (alias = cmd_alias; alias; alias = alias->next)
911                 if (!strncasecmp(partial, alias->name, len))
912                         return alias->name;
913
914         return NULL;
915 }
916
917 /*
918         Cmd_CompleteAliasCountPossible
919
920         New function for tab-completion system
921         Added by EvilTypeGuy
922         Thanks to Fett erich@heintz.com
923         Thanks to taniwha
924
925 */
926 int Cmd_CompleteAliasCountPossible (const char *partial)
927 {
928         cmdalias_t      *alias;
929         size_t          len;
930         int                     h;
931
932         h = 0;
933
934         len = strlen(partial);
935
936         if (!len)
937                 return 0;
938
939         // Loop through the command list and count all partial matches
940         for (alias = cmd_alias; alias; alias = alias->next)
941                 if (!strncasecmp(partial, alias->name, len))
942                         h++;
943
944         return h;
945 }
946
947 /*
948         Cmd_CompleteAliasBuildList
949
950         New function for tab-completion system
951         Added by EvilTypeGuy
952         Thanks to Fett erich@heintz.com
953         Thanks to taniwha
954
955 */
956 const char **Cmd_CompleteAliasBuildList (const char *partial)
957 {
958         cmdalias_t *alias;
959         size_t len = 0;
960         size_t bpos = 0;
961         size_t sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (const char *);
962         const char **buf;
963
964         len = strlen(partial);
965         buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
966         // Loop through the alias list and print all matches
967         for (alias = cmd_alias; alias; alias = alias->next)
968                 if (!strncasecmp(partial, alias->name, len))
969                         buf[bpos++] = alias->name;
970
971         buf[bpos] = NULL;
972         return buf;
973 }
974
975 /*
976 ============
977 Cmd_ExecuteString
978
979 A complete command line has been parsed, so try to execute it
980 FIXME: lookupnoadd the token to speed search?
981 ============
982 */
983 void Cmd_ExecuteString (const char *text, cmd_source_t src)
984 {
985 #define EXECUTESTRING_BUFFER 4096
986         static char buffer[ EXECUTESTRING_BUFFER ];
987         int oldpos;
988         cmd_function_t *cmd;
989         cmdalias_t *a;
990
991         oldpos = cmd_tokenizebufferpos;
992         cmd_source = src;
993
994         Cmd_PreprocessString( text, buffer, EXECUTESTRING_BUFFER, NULL );
995         Cmd_TokenizeString (buffer);
996
997 // execute the command line
998         if (!Cmd_Argc())
999         {
1000                 cmd_tokenizebufferpos = oldpos;
1001                 return;         // no tokens
1002         }
1003
1004 // check functions
1005         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
1006         {
1007                 if (!strcasecmp (cmd_argv[0],cmd->name))
1008                 {
1009                         cmd->function ();
1010                         cmd_tokenizebufferpos = oldpos;
1011                         return;
1012                 }
1013         }
1014
1015 // check alias
1016         for (a=cmd_alias ; a ; a=a->next)
1017         {
1018                 if (!strcasecmp (cmd_argv[0], a->name))
1019                 {
1020                         Cmd_ExecuteAlias(a);
1021                         cmd_tokenizebufferpos = oldpos;
1022                         return;
1023                 }
1024         }
1025
1026 // check cvars
1027         if (!Cvar_Command () && host_framecount > 0)
1028                 Con_Printf("Unknown command \"%s\"\n", Cmd_Argv(0));
1029
1030         cmd_tokenizebufferpos = oldpos;
1031 }
1032
1033
1034 /*
1035 ===================
1036 Cmd_ForwardStringToServer
1037
1038 Sends an entire command string over to the server, unprocessed
1039 ===================
1040 */
1041 void Cmd_ForwardStringToServer (const char *s)
1042 {
1043         if (cls.state != ca_connected)
1044         {
1045                 Con_Printf("Can't \"%s\", not connected\n", s);
1046                 return;
1047         }
1048
1049         if (cls.demoplayback)
1050                 return;         // not really connected
1051
1052         // LordHavoc: thanks to Fuh for bringing the pure evil of SZ_Print to my
1053         // attention, it has been eradicated from here, its only (former) use in
1054         // all of darkplaces.
1055         MSG_WriteByte(&cls.message, clc_stringcmd);
1056         SZ_Write(&cls.message, (const unsigned char *)s, (int)strlen(s) + 1);
1057 }
1058
1059 /*
1060 ===================
1061 Cmd_ForwardToServer
1062
1063 Sends the entire command line over to the server
1064 ===================
1065 */
1066 void Cmd_ForwardToServer (void)
1067 {
1068         const char *s;
1069         if (!strcasecmp(Cmd_Argv(0), "cmd"))
1070         {
1071                 // we want to strip off "cmd", so just send the args
1072                 s = Cmd_Argc() > 1 ? Cmd_Args() : "";
1073         }
1074         else
1075         {
1076                 // we need to keep the command name, so send Cmd_Argv(0), a space and then Cmd_Args()
1077                 s = va("%s %s", Cmd_Argv(0), Cmd_Argc() > 1 ? Cmd_Args() : "");
1078         }
1079         // don't send an empty forward message if the user tries "cmd" by itself
1080         if (!s || !*s)
1081                 return;
1082         Cmd_ForwardStringToServer(s);
1083 }
1084
1085
1086 /*
1087 ================
1088 Cmd_CheckParm
1089
1090 Returns the position (1 to argc-1) in the command's argument list
1091 where the given parameter apears, or 0 if not present
1092 ================
1093 */
1094
1095 int Cmd_CheckParm (const char *parm)
1096 {
1097         int i;
1098
1099         if (!parm)
1100         {
1101                 Con_Printf ("Cmd_CheckParm: NULL");
1102                 return 0;
1103         }
1104
1105         for (i = 1; i < Cmd_Argc (); i++)
1106                 if (!strcasecmp (parm, Cmd_Argv (i)))
1107                         return i;
1108
1109         return 0;
1110 }
1111