]> icculus.org git repositories - divverent/darkplaces.git/blob - cmd.c
removed duplicate comment on a line
[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 void Cmd_ForwardToServer (void);
25
26 #define MAX_ALIAS_NAME  32
27
28 typedef struct cmdalias_s
29 {
30         struct cmdalias_s       *next;
31         char    name[MAX_ALIAS_NAME];
32         char    *value;
33 } cmdalias_t;
34
35 cmdalias_t      *cmd_alias;
36
37 int trashtest;
38 int *trashspot;
39
40 qboolean        cmd_wait;
41
42 //=============================================================================
43
44 /*
45 ============
46 Cmd_Wait_f
47
48 Causes execution of the remainder of the command buffer to be delayed until
49 next frame.  This allows commands like:
50 bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
51 ============
52 */
53 void Cmd_Wait_f (void)
54 {
55         cmd_wait = true;
56 }
57
58 /*
59 =============================================================================
60
61                                                 COMMAND BUFFER
62
63 =============================================================================
64 */
65
66 sizebuf_t       cmd_text;
67
68 /*
69 ============
70 Cbuf_Init
71 ============
72 */
73 void Cbuf_Init (void)
74 {
75         SZ_Alloc (&cmd_text, 8192);             // space for commands and script files
76 }
77
78
79 /*
80 ============
81 Cbuf_AddText
82
83 Adds command text at the end of the buffer
84 ============
85 */
86 void Cbuf_AddText (char *text)
87 {
88         int             l;
89         
90         l = strlen (text);
91
92         if (cmd_text.cursize + l >= cmd_text.maxsize)
93         {
94                 Con_Printf ("Cbuf_AddText: overflow\n");
95                 return;
96         }
97
98         SZ_Write (&cmd_text, text, strlen (text));
99 }
100
101
102 /*
103 ============
104 Cbuf_InsertText
105
106 Adds command text immediately after the current command
107 Adds a \n to the text
108 FIXME: actually change the command buffer to do less copying
109 ============
110 */
111 void Cbuf_InsertText (char *text)
112 {
113         char    *temp;
114         int             templen;
115
116 // copy off any commands still remaining in the exec buffer
117         templen = cmd_text.cursize;
118         if (templen)
119         {
120                 temp = Z_Malloc (templen);
121                 memcpy (temp, cmd_text.data, templen);
122                 SZ_Clear (&cmd_text);
123         }
124         else
125                 temp = NULL;    // shut up compiler
126                 
127 // add the entire text of the file
128         Cbuf_AddText (text);
129         
130 // add the copied off data
131         if (templen)
132         {
133                 SZ_Write (&cmd_text, temp, templen);
134                 Z_Free (temp);
135         }
136 }
137
138 /*
139 ============
140 Cbuf_Execute
141 ============
142 */
143 void Cbuf_Execute (void)
144 {
145         int             i;
146         char    *text;
147         char    line[1024];
148         int             quotes;
149         
150         while (cmd_text.cursize)
151         {
152 // find a \n or ; line break
153                 text = (char *)cmd_text.data;
154
155                 quotes = 0;
156                 for (i=0 ; i< cmd_text.cursize ; i++)
157                 {
158                         if (text[i] == '"')
159                                 quotes++;
160                         if ( !(quotes&1) &&  text[i] == ';')
161                                 break;  // don't break if inside a quoted string
162                         if (text[i] == '\n')
163                                 break;
164                 }
165                         
166                                 
167                 memcpy (line, text, i);
168                 line[i] = 0;
169                 
170 // delete the text from the command buffer and move remaining commands down
171 // this is necessary because commands (exec, alias) can insert data at the
172 // beginning of the text buffer
173
174                 if (i == cmd_text.cursize)
175                         cmd_text.cursize = 0;
176                 else
177                 {
178                         i++;
179                         cmd_text.cursize -= i;
180                         memcpy (text, text+i, cmd_text.cursize);
181                 }
182
183 // execute the command line
184                 Cmd_ExecuteString (line, src_command);
185                 
186                 if (cmd_wait)
187                 {       // skip out while text still remains in buffer, leaving it
188                         // for next frame
189                         cmd_wait = false;
190                         break;
191                 }
192         }
193 }
194
195 /*
196 ==============================================================================
197
198                                                 SCRIPT COMMANDS
199
200 ==============================================================================
201 */
202
203 /*
204 ===============
205 Cmd_StuffCmds_f
206
207 Adds command line parameters as script statements
208 Commands lead with a +, and continue until a - or another +
209 quake +prog jctest.qp +cmd amlev1
210 quake -nosound +cmd amlev1
211 ===============
212 */
213 void Cmd_StuffCmds_f (void)
214 {
215         int             i, j;
216         int             s;
217         char    *text, *build, c;
218                 
219         if (Cmd_Argc () != 1)
220         {
221                 Con_Printf ("stuffcmds : execute command line parameters\n");
222                 return;
223         }
224
225 // build the combined string to parse from
226         s = 0;
227         for (i=1 ; i<com_argc ; i++)
228         {
229                 if (!com_argv[i])
230                         continue;               // NEXTSTEP nulls out -NXHost
231                 s += strlen (com_argv[i]) + 1;
232         }
233         if (!s)
234                 return;
235                 
236         text = Z_Malloc (s+1);
237         text[0] = 0;
238         for (i=1 ; i<com_argc ; i++)
239         {
240                 if (!com_argv[i])
241                         continue;               // NEXTSTEP nulls out -NXHost
242                 strcat (text,com_argv[i]);
243                 if (i != com_argc-1)
244                         strcat (text, " ");
245         }
246         
247 // pull out the commands
248         build = Z_Malloc (s+1);
249         build[0] = 0;
250         
251         for (i=0 ; i<s-1 ; i++)
252         {
253                 if (text[i] == '+')
254                 {
255                         i++;
256
257                         for (j=i ; (text[j] != '+') && (text[j] != '-') && (text[j] != 0) ; j++)
258                                 ;
259
260                         c = text[j];
261                         text[j] = 0;
262                         
263                         strcat (build, text+i);
264                         strcat (build, "\n");
265                         text[j] = c;
266                         i = j-1;
267                 }
268         }
269         
270         if (build[0])
271                 Cbuf_InsertText (build);
272         
273         Z_Free (text);
274         Z_Free (build);
275 }
276
277
278 /*
279 ===============
280 Cmd_Exec_f
281 ===============
282 */
283 void Cmd_Exec_f (void)
284 {
285         char    *f;
286
287         if (Cmd_Argc () != 2)
288         {
289                 Con_Printf ("exec <filename> : execute a script file\n");
290                 return;
291         }
292
293         f = (char *)COM_LoadMallocFile (Cmd_Argv(1), false);
294         if (!f)
295         {
296                 Con_Printf ("couldn't exec %s\n",Cmd_Argv(1));
297                 return;
298         }
299         Con_Printf ("execing %s\n",Cmd_Argv(1));
300         
301         Cbuf_InsertText (f);
302         qfree(f);
303 }
304
305
306 /*
307 ===============
308 Cmd_Echo_f
309
310 Just prints the rest of the line to the console
311 ===============
312 */
313 void Cmd_Echo_f (void)
314 {
315         int             i;
316         
317         for (i=1 ; i<Cmd_Argc() ; i++)
318                 Con_Printf ("%s ",Cmd_Argv(i));
319         Con_Printf ("\n");
320 }
321
322 /*
323 ===============
324 Cmd_Alias_f
325
326 Creates a new command that executes a command string (possibly ; seperated)
327 ===============
328 */
329
330 char *CopyString (char *in)
331 {
332         char    *out;
333         
334         out = Z_Malloc (strlen(in)+1);
335         strcpy (out, in);
336         return out;
337 }
338
339 void Cmd_Alias_f (void)
340 {
341         cmdalias_t      *a;
342         char            cmd[1024];
343         int                     i, c;
344         char            *s;
345
346         if (Cmd_Argc() == 1)
347         {
348                 Con_Printf ("Current alias commands:\n");
349                 for (a = cmd_alias ; a ; a=a->next)
350                         Con_Printf ("%s : %s\n", a->name, a->value);
351                 return;
352         }
353
354         s = Cmd_Argv(1);
355         if (strlen(s) >= MAX_ALIAS_NAME)
356         {
357                 Con_Printf ("Alias name is too long\n");
358                 return;
359         }
360
361         // if the alias already exists, reuse it
362         for (a = cmd_alias ; a ; a=a->next)
363         {
364                 if (!strcmp(s, a->name))
365                 {
366                         Z_Free (a->value);
367                         break;
368                 }
369         }
370
371         if (!a)
372         {
373                 a = Z_Malloc (sizeof(cmdalias_t));
374                 a->next = cmd_alias;
375                 cmd_alias = a;
376         }
377         strcpy (a->name, s);    
378
379 // copy the rest of the command line
380         cmd[0] = 0;             // start out with a null string
381         c = Cmd_Argc();
382         for (i=2 ; i< c ; i++)
383         {
384                 strcat (cmd, Cmd_Argv(i));
385                 if (i != c)
386                         strcat (cmd, " ");
387         }
388         strcat (cmd, "\n");
389         
390         a->value = CopyString (cmd);
391 }
392
393 /*
394 =============================================================================
395
396                                         COMMAND EXECUTION
397
398 =============================================================================
399 */
400
401 typedef struct cmd_function_s
402 {
403         struct cmd_function_s   *next;
404         char                                    *name;
405         xcommand_t                              function;
406 } cmd_function_t;
407
408
409 #define MAX_ARGS                80
410
411 static  int                     cmd_argc;
412 static  char            *cmd_argv[MAX_ARGS];
413 static  char            *cmd_null_string = "";
414 static  char            *cmd_args = NULL;
415
416 cmd_source_t    cmd_source;
417
418
419 static  cmd_function_t  *cmd_functions;         // possible commands to execute
420
421 /*
422 ========
423 Cmd_List
424
425         CmdList Added by EvilTypeGuy eviltypeguy@qeradiant.com
426         Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
427
428 ========
429 */
430 void Cmd_List_f (void)
431 {
432         cmd_function_t  *cmd;
433         char                    *partial;
434         int                             len;
435         int                             count;
436
437         if (Cmd_Argc() > 1) {
438                 partial = Cmd_Argv (1);
439                 len = strlen(partial);
440         } else {
441                 partial = NULL;
442                 len = 0;
443         }
444
445         count = 0;
446         for (cmd = cmd_functions; cmd; cmd = cmd->next) {
447                 if (partial && strncmp(partial, cmd->name, len))
448                         continue;
449                 Con_Printf ("%s\n", cmd->name);
450                 count++;
451         }
452
453         Con_Printf ("%i Command%s", count, (count > 1) ? "s" : "");
454         if (partial)
455                 Con_Printf(" beginning with \"%s\"", partial);
456
457         Con_Printf ("\n\n");
458 }
459
460 /*
461 ============
462 Cmd_Init
463 ============
464 */
465 void Cmd_Init (void)
466 {
467 //
468 // register our commands
469 //
470         Cmd_AddCommand ("stuffcmds",Cmd_StuffCmds_f);
471         Cmd_AddCommand ("exec",Cmd_Exec_f);
472         Cmd_AddCommand ("echo",Cmd_Echo_f);
473         Cmd_AddCommand ("alias",Cmd_Alias_f);
474         Cmd_AddCommand ("cmd", Cmd_ForwardToServer);
475         Cmd_AddCommand ("wait", Cmd_Wait_f);
476         Cmd_AddCommand ("cmdlist", Cmd_List_f);         // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
477         Cmd_AddCommand ("cvarlist", Cvar_List_f);       // 2000-01-09 CmdList, CvarList commands
478                                                                                                 // By Matthias "Maddes" Buecher
479 }
480
481 /*
482 ============
483 Cmd_Argc
484 ============
485 */
486 int             Cmd_Argc (void)
487 {
488         return cmd_argc;
489 }
490
491 /*
492 ============
493 Cmd_Argv
494 ============
495 */
496 char    *Cmd_Argv (int arg)
497 {
498         if (arg >= cmd_argc )
499                 return cmd_null_string;
500         return cmd_argv[arg];   
501 }
502
503 /*
504 ============
505 Cmd_Args
506 ============
507 */
508 char    *Cmd_Args (void)
509 {
510         return cmd_args;
511 }
512
513
514 /*
515 ============
516 Cmd_TokenizeString
517
518 Parses the given string into command line tokens.
519 ============
520 */
521 void Cmd_TokenizeString (char *text)
522 {
523         int             i;
524         
525 // clear the args from the last string
526         for (i=0 ; i<cmd_argc ; i++)
527                 Z_Free (cmd_argv[i]);
528                 
529         cmd_argc = 0;
530         cmd_args = NULL;
531         
532         while (1)
533         {
534 // skip whitespace up to a /n
535                 while (*text && *text <= ' ' && *text != '\n')
536                 {
537                         text++;
538                 }
539                 
540                 if (*text == '\n')
541                 {       // a newline seperates commands in the buffer
542                         text++;
543                         break;
544                 }
545
546                 if (!*text)
547                         return;
548         
549                 if (cmd_argc == 1)
550                          cmd_args = text;
551                         
552                 text = COM_Parse (text);
553                 if (!text)
554                         return;
555
556                 if (cmd_argc < MAX_ARGS)
557                 {
558                         cmd_argv[cmd_argc] = Z_Malloc (strlen(com_token)+1);
559                         strcpy (cmd_argv[cmd_argc], com_token);
560                         cmd_argc++;
561                 }
562         }
563         
564 }
565
566
567 /*
568 ============
569 Cmd_AddCommand
570 ============
571 */
572 void    Cmd_AddCommand (char *cmd_name, xcommand_t function)
573 {
574         cmd_function_t  *cmd;
575         
576         if (host_initialized)   // because hunk allocation would get stomped
577                 Sys_Error ("Cmd_AddCommand after host_initialized");
578                 
579 // fail if the command is a variable name
580         if (Cvar_VariableString(cmd_name)[0])
581         {
582                 Con_Printf ("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
583                 return;
584         }
585         
586 // fail if the command already exists
587         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
588         {
589                 if (!strcmp (cmd_name, cmd->name))
590                 {
591                         Con_Printf ("Cmd_AddCommand: %s already defined\n", cmd_name);
592                         return;
593                 }
594         }
595
596         cmd = Hunk_AllocName (sizeof(cmd_function_t), "commands");
597         cmd->name = cmd_name;
598         cmd->function = function;
599         cmd->next = cmd_functions;
600         cmd_functions = cmd;
601 }
602
603 /*
604 ============
605 Cmd_Exists
606 ============
607 */
608 qboolean        Cmd_Exists (char *cmd_name)
609 {
610         cmd_function_t  *cmd;
611
612         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
613         {
614                 if (!strcmp (cmd_name,cmd->name))
615                         return true;
616         }
617
618         return false;
619 }
620
621
622
623 /*
624 ============
625 Cmd_CompleteCommand
626 ============
627 */
628 char *Cmd_CompleteCommand (char *partial)
629 {
630         cmd_function_t  *cmd;
631         int                             len;
632         
633         len = strlen(partial);
634         
635         if (!len)
636                 return NULL;
637                 
638 // check functions
639         for (cmd = cmd_functions; cmd; cmd = cmd->next)
640                 if (!strncmp(partial, cmd->name, len))
641                         return cmd->name;
642
643         return NULL;
644 }
645
646 /*
647         Cmd_CompleteCountPossible
648
649         New function for tab-completion system
650         Added by EvilTypeGuy
651         Thanks to Fett erich@heintz.com
652         Thanks to taniwha
653
654 */
655 int
656 Cmd_CompleteCountPossible (char *partial)
657 {
658         cmd_function_t  *cmd;
659         int                             len;
660         int                             h;
661         
662         h = 0;
663         len = strlen(partial);
664         
665         if (!len)
666                 return 0;
667         
668         // Loop through the command list and count all partial matches
669         for (cmd = cmd_functions; cmd; cmd = cmd->next)
670                 if (!strncasecmp(partial, cmd->name, len))
671                         h++;
672
673         return h;
674 }
675
676 /*
677         Cmd_CompleteBuildList
678
679         New function for tab-completion system
680         Added by EvilTypeGuy
681         Thanks to Fett erich@heintz.com
682         Thanks to taniwha
683
684 */
685 char    **
686 Cmd_CompleteBuildList (char *partial)
687 {
688         cmd_function_t  *cmd;
689         int                             len = 0;
690         int                             bpos = 0;
691         int                             sizeofbuf = (Cmd_CompleteCountPossible (partial) + 1) * sizeof (char *);
692         char                    **buf;
693
694         len = strlen(partial);
695         buf = qmalloc(sizeofbuf + sizeof (char *));
696         // Loop through the alias list and print all matches
697         for (cmd = cmd_functions; cmd; cmd = cmd->next)
698                 if (!strncasecmp(partial, cmd->name, len))
699                         buf[bpos++] = cmd->name;
700
701         buf[bpos] = NULL;
702         return buf;
703 }
704
705 /*
706         Cmd_CompleteAlias
707
708         New function for tab-completion system
709         Added by EvilTypeGuy
710         Thanks to Fett erich@heintz.com
711         Thanks to taniwha
712
713 */
714 char
715 *Cmd_CompleteAlias (char * partial)
716 {
717         cmdalias_t      *alias;
718         int                     len;
719
720         len = strlen(partial);
721
722         if (!len)
723                 return NULL;
724
725         // Check functions
726         for (alias = cmd_alias; alias; alias = alias->next)
727                 if (!strncasecmp(partial, alias->name, len))
728                         return alias->name;
729
730         return NULL;
731 }
732
733 /*
734         Cmd_CompleteAliasCountPossible
735
736         New function for tab-completion system
737         Added by EvilTypeGuy
738         Thanks to Fett erich@heintz.com
739         Thanks to taniwha
740
741 */
742 int
743 Cmd_CompleteAliasCountPossible (char *partial)
744 {
745         cmdalias_t      *alias;
746         int                     len;
747         int                     h;
748
749         h = 0;
750
751         len = strlen(partial);
752
753         if (!len)
754                 return 0;
755
756         // Loop through the command list and count all partial matches
757         for (alias = cmd_alias; alias; alias = alias->next)
758                 if (!strncasecmp(partial, alias->name, len))
759                         h++;
760
761         return h;
762 }
763
764 /*
765         Cmd_CompleteAliasBuildList
766
767         New function for tab-completion system
768         Added by EvilTypeGuy
769         Thanks to Fett erich@heintz.com
770         Thanks to taniwha
771
772 */
773 char    **
774 Cmd_CompleteAliasBuildList (char *partial)
775 {
776         cmdalias_t      *alias;
777         int                     len = 0;
778         int                     bpos = 0;
779         int                     sizeofbuf = (Cmd_CompleteAliasCountPossible (partial) + 1) * sizeof (char *);
780         char            **buf;
781
782         len = strlen(partial);
783         buf = qmalloc(sizeofbuf + sizeof (char *));
784         // Loop through the alias list and print all matches
785         for (alias = cmd_alias; alias; alias = alias->next)
786                 if (!strncasecmp(partial, alias->name, len))
787                         buf[bpos++] = alias->name;
788
789         buf[bpos] = NULL;
790         return buf;
791 }
792
793 /*
794 ============
795 Cmd_ExecuteString
796
797 A complete command line has been parsed, so try to execute it
798 FIXME: lookupnoadd the token to speed search?
799 ============
800 */
801 void    Cmd_ExecuteString (char *text, cmd_source_t src)
802 {       
803         cmd_function_t  *cmd;
804         cmdalias_t              *a;
805
806         cmd_source = src;
807         Cmd_TokenizeString (text);
808                         
809 // execute the command line
810         if (!Cmd_Argc())
811                 return;         // no tokens
812
813 // check functions
814         for (cmd=cmd_functions ; cmd ; cmd=cmd->next)
815         {
816                 if (!Q_strcasecmp (cmd_argv[0],cmd->name))
817                 {
818                         cmd->function ();
819                         return;
820                 }
821         }
822
823 // check alias
824         for (a=cmd_alias ; a ; a=a->next)
825         {
826                 if (!Q_strcasecmp (cmd_argv[0], a->name))
827                 {
828                         Cbuf_InsertText (a->value);
829                         return;
830                 }
831         }
832         
833 // check cvars
834         if (!Cvar_Command ())
835                 Con_Printf ("Unknown command \"%s\"\n", Cmd_Argv(0));
836         
837 }
838
839
840 /*
841 ===================
842 Cmd_ForwardToServer
843
844 Sends the entire command line over to the server
845 ===================
846 */
847 void Cmd_ForwardToServer (void)
848 {
849         if (cls.state != ca_connected)
850         {
851                 Con_Printf ("Can't \"%s\", not connected\n", Cmd_Argv(0));
852                 return;
853         }
854         
855         if (cls.demoplayback)
856                 return;         // not really connected
857
858         MSG_WriteByte (&cls.message, clc_stringcmd);
859         if (Q_strcasecmp(Cmd_Argv(0), "cmd") != 0)
860         {
861                 SZ_Print (&cls.message, Cmd_Argv(0));
862                 SZ_Print (&cls.message, " ");
863         }
864         if (Cmd_Argc() > 1)
865                 SZ_Print (&cls.message, Cmd_Args());
866         else
867                 SZ_Print (&cls.message, "\n");
868 }
869
870
871 /*
872 ================
873 Cmd_CheckParm
874
875 Returns the position (1 to argc-1) in the command's argument list
876 where the given parameter apears, or 0 if not present
877 ================
878 */
879
880 int Cmd_CheckParm (char *parm)
881 {
882         int i;
883         
884         if (!parm)
885                 Sys_Error ("Cmd_CheckParm: NULL");
886
887         for (i = 1; i < Cmd_Argc (); i++)
888                 if (!Q_strcasecmp (parm, Cmd_Argv (i)))
889                         return i;
890                         
891         return 0;
892 }
893