forked from QW-Group/ezquake-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.c
2384 lines (1976 loc) · 54.7 KB
/
cmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
$Id: cmd.c,v 1.92 2007-10-28 02:45:19 qqshka Exp $
*/
#ifndef _WIN32
#include <strings.h>
#endif
#include "quakedef.h"
#ifdef WITH_TCL
#include "embed_tcl.h"
#endif
#include "gl_model.h"
#include "gl_local.h"
#include "teamplay.h"
#include "rulesets.h"
#include "tp_triggers.h"
#include "parser.h"
#include "utils.h"
#include "keys.h"
qbool CL_CheckServerCommand (void);
static void Cmd_ExecuteStringEx (cbuf_t *context, char *text);
static int gtf = 0; // global trigger flag
cvar_t cl_warncmd = {"cl_warncmd", "1"};
cvar_t cl_warnexec = {"cl_warnexec", "1"};
cvar_t cl_curlybraces = {"cl_curlybraces", "0"};
cbuf_t cbuf_main;
cbuf_t cbuf_svc;
cbuf_t cbuf_safe, cbuf_formatted_comms;
cbuf_t cbuf_server;
char *hud262_load_buff = NULL;
cbuf_t *cbuf_current = NULL;
//=============================================================================
//Causes execution of the remainder of the command buffer to be delayed until next frame.
//This allows commands like: bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
void Cmd_Wait_f (void)
{
#ifdef WITH_TCL
if (in_tcl) {
Com_Printf ("command wait cant be used with TCL\n");
return;
}
#endif
if (cbuf_current)
cbuf_current->wait = true;
return;
}
void Hud262_CatchStringsOnLoad(char *line)
{
char *tmpbuff;
if (Utils_RegExpMatch("^((\\s+)?(?i)hud262_(add|alpha|bg|blink|disable|enable|position|width))", line))
{
if (hud262_load_buff == NULL)
{
hud262_load_buff = (char*) Q_malloc( (strlen(line) + 2) * sizeof(char));
snprintf(hud262_load_buff, strlen(line) + 2, "%s\n", line);
}
else
{
tmpbuff = (char *) Q_malloc(strlen(hud262_load_buff) + 1);
strcpy(tmpbuff, hud262_load_buff);
hud262_load_buff = (char *) Q_realloc(hud262_load_buff, (strlen(tmpbuff) + strlen(line) + 2) * sizeof(char));
snprintf(hud262_load_buff, strlen(tmpbuff) + strlen(line) + 2, "%s%s\n", tmpbuff, line);
Q_free(tmpbuff);
}
}
}
/*
=============================================================================
COMMAND BUFFER
=============================================================================
*/
void Cbuf_AddText (const char *text)
{
Cbuf_AddTextEx (&cbuf_main, text);
}
void Cbuf_InsertText (const char *text)
{
Cbuf_InsertTextEx (&cbuf_main, text);
}
void Cbuf_Execute (void)
{
Cbuf_ExecuteEx (&cbuf_main);
Cbuf_ExecuteEx (&cbuf_safe);
Cbuf_ExecuteEx (&cbuf_formatted_comms);
Cbuf_ExecuteEx (&cbuf_server);
}
//fuh : ideally we should have 'cbuf_t *Cbuf_Register(int maxsize, int flags, qbool (*blockcmd)(void))
//fuh : so that cbuf_svc and cbuf_safe can be registered outside cmd.c in cl_* .c
//fuh : flags can be used to deal with newline termination etc for cbuf_svc, and *blockcmd can be used for blocking cmd's for cbuf_svc
//fuh : this way cmd.c would be independant of '#ifdef CLIENTONLY's'.
//fuh : I'll take care of that one day.
static void Cbuf_Register (cbuf_t *cbuf, int maxsize)
{
assert (!host_initialized);
cbuf->maxsize = maxsize;
cbuf->text_buf = (char *) Hunk_Alloc(maxsize);
cbuf->text_start = cbuf->text_end = (cbuf->maxsize >> 1);
cbuf->wait = false;
cbuf->waitCount = 0;
}
void Cbuf_Init (void)
{
Cbuf_Register (&cbuf_main, 1 << 18); // 256kb
Cbuf_Register (&cbuf_svc, 1 << 13); // 8kb
Cbuf_Register (&cbuf_safe, 1 << 11); // 2kb
Cbuf_Register (&cbuf_formatted_comms, 1 << 11); // 2kb
Cbuf_Register (&cbuf_server, 1 << 18); // 256kb
}
//Adds command text at the end of the buffer
void Cbuf_AddTextEx (cbuf_t *cbuf, const char *text)
{
int new_start, new_bufsize;
size_t len;
len = strlen (text);
if (cbuf->text_end + len <= cbuf->maxsize) {
memcpy (cbuf->text_buf + cbuf->text_end, text, len);
cbuf->text_end += len;
return;
}
new_bufsize = cbuf->text_end-cbuf->text_start+len;
if (new_bufsize > cbuf->maxsize) {
Com_Printf ("Cbuf_AddText: overflow\n");
return;
}
// Calculate optimal position of text in buffer
new_start = ((cbuf->maxsize - new_bufsize) >> 1);
memcpy (cbuf->text_buf + new_start, cbuf->text_buf + cbuf->text_start, cbuf->text_end-cbuf->text_start);
memcpy (cbuf->text_buf + new_start + cbuf->text_end-cbuf->text_start, text, len);
cbuf->text_start = new_start;
cbuf->text_end = cbuf->text_start + new_bufsize;
}
//Adds command text at the beginning of the buffer
void Cbuf_InsertTextEx (cbuf_t *cbuf, const char *text)
{
int new_start, new_bufsize;
size_t len;
len = strlen (text);
if (len <= cbuf->text_start) {
memcpy (cbuf->text_buf + (cbuf->text_start - len), text, len);
cbuf->text_start -= len;
return;
}
new_bufsize = cbuf->text_end - cbuf->text_start + len;
if (new_bufsize > cbuf->maxsize) {
Com_Printf ("Cbuf_InsertText: overflow\n");
return;
}
// Calculate optimal position of text in buffer
new_start = ((cbuf->maxsize - new_bufsize) >> 1);
memmove (cbuf->text_buf + (new_start + len), cbuf->text_buf + cbuf->text_start, cbuf->text_end - cbuf->text_start);
memcpy (cbuf->text_buf + new_start, text, len);
cbuf->text_start = new_start;
cbuf->text_end = cbuf->text_start + new_bufsize;
}
#define MAX_RUNAWAYLOOP 1000
void Cbuf_ExecuteEx (cbuf_t *cbuf)
{
int i, j, cursize, nextsize;
char *text, line[1024], *src, *dest;
qbool comment;
int quotes;
if (cbuf == &cbuf_safe)
gtf++;
nextsize = cbuf->text_end - cbuf->text_start;
while (cbuf->text_end > cbuf->text_start)
{
// find a \n or ; line break
text = (char *) cbuf->text_buf + cbuf->text_start;
cursize = cbuf->text_end - cbuf->text_start;
comment = false;
quotes = 0;
for (i = 0; i < cursize; i++)
{
if (cl_curlybraces.integer)
{
if (text[i] == '\\')
{
if (i + 1 < cursize && text[i+1] == '\n')
{ // escaped endline
text[i] = text[i+1] = '\r'; // '\r' removed later during copying
i++;
continue;
}
else if (i + 2 < cursize && text[i+1] == '\r' && text[i+2] == '\n')
{ // escaped dos endline
text[i] = text[i+2] = '\r';
i+=2;
continue;
}
}
}
if (text[i] == '\n')
break;
if (text[i] == '"' && quotes <= 0)
{
if (!quotes)
quotes = -1;
else
quotes = 0;
}
else if (quotes >= 0)
{
if (cl_curlybraces.integer)
{
if (text[i] == '{')
quotes++;
else if (text[i] == '}')
quotes--;
}
}
if (comment || quotes)
continue;
if (text[i] == '/' && i + 1 < cursize && text[i + 1] == '/')
comment = true;
else if (text[i] == ';' && !quotes)
break;
}
if ((cursize - i) < nextsize) // have we reached the next command?
nextsize = cursize - i;
// don't execute lines without ending \n; this fixes problems with
// partially stuffed aliases not being executed properly
if (cbuf_current == &cbuf_svc && i == cursize)
break;
// Copy text to line, skipping carriage return chars
src = text;
dest = line;
j = min (i, sizeof (line) - 1);
for ( ; j; j--, src++)
{
if (*src != '\r')
*dest++ = *src;
}
*dest = 0;
// delete the text from the command buffer and move remaining commands down This is necessary
// because commands (exec, alias) can insert data at the beginning of the text buffer
if (i == cursize)
{
cbuf->text_start = cbuf->text_end = (cbuf->maxsize >> 1);
}
else
{
i++;
cbuf->text_start += i;
}
cursize = cbuf->text_end - cbuf->text_start;
// TODO: make it in a more right way
// since, hud262_add can not correctly create hud elements during normal start
// (some cvars are not created/initialized at the time when we want to use them in hud262)
// we should save these commands to buffer and execute it when all
// cvars will be created
if(!host_initialized)
Hud262_CatchStringsOnLoad(line);
Cmd_ExecuteStringEx (cbuf, line); // execute the command line
if (cbuf->text_end - cbuf->text_start > cursize)
cbuf->runAwayLoop++;
if (cbuf->runAwayLoop > MAX_RUNAWAYLOOP)
{
Com_Printf("\x02" "A recursive alias has caused an infinite loop.");
Com_Printf("\x02" " Clearing execution buffer to prevent lockup.\n");
cbuf->text_start = cbuf->text_end = (cbuf->maxsize >> 1);
cbuf->runAwayLoop = 0;
}
if (cbuf->wait && cbuf->waitCount >= Rulesets_MaxSequentialWaitCommands())
{
Com_Printf("\x02" "Max number of wait commands detected.\n");
cbuf->text_start = cbuf->text_end = (cbuf->maxsize >> 1);
cbuf->wait = false;
cbuf->waitCount = 0;
}
if (cbuf->wait)
{
// skip out while text still remains in buffer, leaving it for next frame
cbuf->wait = false;
++cbuf->waitCount;
cbuf->runAwayLoop += Q_rint (0.5 * cls.frametime * MAX_RUNAWAYLOOP);
if (cbuf == &cbuf_safe)
gtf--;
return;
}
}
if (cbuf == &cbuf_safe)
gtf--;
cbuf->runAwayLoop = 0;
cbuf->waitCount = 0;
return;
}
/*
==============================================================================
SCRIPT COMMANDS
==============================================================================
*/
/*
Set commands are added early, so they are guaranteed to be set before
the client and server initialize for the first time.
Other commands are added late, after all initialization is complete.
*/
void Cbuf_AddEarlyCommands (void)
{
int i;
for (i = 0; i < COM_Argc () - 2; i++) {
if (strcasecmp (COM_Argv(i), "+set"))
continue;
Cbuf_AddText (va ("set %s %s\n", COM_Argv (i + 1), COM_Argv (i + 2)));
i += 2;
}
}
qbool Cmd_IsAllowedStuffCmdsCommand(const char *str)
{
char* banned_list[] = {"set ", "cfg_load ", NULL};
char** banned_cmd = banned_list;
while(*banned_cmd)
{
if(strncasecmp(str, *banned_cmd, strlen(*banned_cmd)) == 0)
{
//+set is processed in Cbuf_AddEarlyCommands(), +cfg_load allowed only once in Host_Init()
if((strncasecmp(str, "set ", 4) != 0) && (strncasecmp(str, "cfg_load ", 9) != 0))
{
Com_Printf("+%s is not allowed in cmdline or obsolete.\n", *banned_cmd);
}
return false;
}
banned_cmd++;
}
return true;
}
/*
Adds command line parameters as script statements
Commands lead with a +, and continue until a - or another +
quake +prog jctest.qp +cmd amlev1
quake -nosound +cmd amlev1
*/
void Cmd_StuffCmds_f (void)
{
int k, len = 0;
char *s, *text, *token;
// build the combined string to parse from
for (k = 1; k < COM_Argc(); k++)
len += strlen (COM_Argv(k)) + 1;
if (!len)
return;
text = (char *) Q_malloc(len + 1);
text[0] = '\0';
for (k = 1; k < COM_Argc(); k++) {
strlcat (text, COM_Argv(k), len + 1);
if (k != COM_Argc() - 1)
strlcat (text, " ", len + 1);
}
// pull out the commands
token = (char *) Q_malloc(len + 1);
s = text;
while (*s) {
if (*s == '+') {
k = 0;
for (s = s + 1; s[0] && (s[0] != ' ' || (s[1] != '-' && s[1] != '+')); s++)
token[k++] = s[0];
token[k++] = '\n';
token[k] = 0;
if(Cmd_IsAllowedStuffCmdsCommand(token))
Cbuf_AddText (token);
} else if (*s == '-') {
for (s = s + 1; s[0] && s[0] != ' '; s++)
;
} else {
s++;
}
}
Q_free(text);
Q_free (token);
}
void Cmd_Exec_f (void)
{
char *f, name[MAX_OSPATH];
char reset_bindphysical[128];
int mark;
qbool server_command = false;
if (Cmd_Argc () != 2) {
Com_Printf ("%s <filename> : execute a script file\n", Cmd_Argv(0));
return;
}
#if !defined(SERVERONLY) && !defined(CLIENTONLY)
server_command = cbuf_current == &cbuf_server || !strcmp(Cmd_Argv(0), "serverexec");
#endif
strlcpy (name, Cmd_Argv(1), sizeof(name) - 4);
mark = Hunk_LowMark();
if (!(f = (char *) FS_LoadHunkFile (name, NULL))) {
const char *p;
p = COM_SkipPath (name);
if (!strchr (p, '.')) {
// no extension, so try the default (.cfg)
strlcat (name, ".cfg", sizeof (name));
f = (char *) FS_LoadHunkFile (name, NULL);
}
if (!f) {
Com_Printf ("couldn't exec %s\n", Cmd_Argv(1));
return;
}
}
if (cl_warnexec.integer || developer.integer) {
Com_Printf("execing %s/%s\n", FS_Locate_GetPath(name), name);
}
// All config files default to con_bindphysical 1, and would have to over-ride if they
// want different behaviour.
sprintf(reset_bindphysical, "\ncon_bindphysical %d\n", con_bindphysical.integer);
if (cbuf_current == &cbuf_svc) {
Cbuf_AddTextEx(&cbuf_main, "con_bindphysical 1\n");
Cbuf_AddTextEx(&cbuf_main, f);
Cbuf_AddTextEx(&cbuf_main, reset_bindphysical);
}
else if (server_command) {
Cbuf_AddTextEx(&cbuf_server, f);
}
else {
Cbuf_InsertTextEx(&cbuf_main, reset_bindphysical);
Cbuf_InsertTextEx(&cbuf_main, f);
Cbuf_InsertTextEx(&cbuf_main, "con_bindphysical 1\n");
}
Hunk_FreeToLowMark (mark);
}
//Just prints the rest of the line to the console
/*void Cmd_Echo_f (void) {
int i;
for (i = 1; i < Cmd_Argc(); i++)
Com_Printf ("%s ", Cmd_Argv(i));
Com_Printf ("\n");
}*/
void Cmd_Echo_f (void)
{
int i;
char *str;
char args[MAX_MACRO_STRING];
char buf[MAX_MACRO_STRING];
memset (args, 0, MAX_MACRO_STRING);
snprintf (args, MAX_MACRO_STRING, "%s", Cmd_Argv(1));
for (i = 2; i < Cmd_Argc(); i++) {
strlcat (args, " ", MAX_MACRO_STRING);
strlcat (args, Cmd_Argv(i), MAX_MACRO_STRING);
}
// str = TP_ParseMacroString(args);
str = TP_ParseMacroString(args);
str = TP_ParseFunChars(str, false);
strlcpy (buf, str, MAX_MACRO_STRING);
CL_SearchForReTriggers (buf, RE_PRINT_ECHO); // BorisU
Print_flags[Print_current] |= PR_TR_SKIP;
Com_Printf ("%s\n", buf);
}
/*
=============================================================================
ALIASES
=============================================================================
*/
#define ALIAS_HASHPOOL_SIZE 256
cmd_alias_t *cmd_alias_hash[ALIAS_HASHPOOL_SIZE];
cmd_alias_t *cmd_alias;
cmd_alias_t *Cmd_FindAlias (const char *name)
{
int key;
cmd_alias_t *alias;
key = Com_HashKey (name) % ALIAS_HASHPOOL_SIZE;
for (alias = cmd_alias_hash[key]; alias; alias = alias->hash_next) {
if (!strcasecmp(name, alias->name))
return alias;
}
return NULL;
}
char *Cmd_AliasString (char *name)
{
int key;
cmd_alias_t *alias;
key = Com_HashKey (name) % ALIAS_HASHPOOL_SIZE;
for (alias = cmd_alias_hash[key]; alias; alias = alias->hash_next) {
if (!strcasecmp(name, alias->name))
#ifdef WITH_TCL
if (!(alias->flags & ALIAS_TCL))
#endif
return alias->value;
}
return NULL;
}
void Cmd_Viewalias_f (void)
{
cmd_alias_t *alias;
char *name;
int i,m;
if (Cmd_Argc() < 2) {
Com_Printf ("viewalias <cvar> [<cvar2>..] : view body of alias\n");
return;
}
for (i=1; i<Cmd_Argc(); i++) {
name = Cmd_Argv(i);
if ( IsRegexp(name) ) {
if (!ReSearchInit(name))
return;
Com_Printf ("Current alias commands:\n");
for (alias = cmd_alias, i=m=0; alias ; alias=alias->next, i++)
if (ReSearchMatch(alias->name)) {
#ifdef WITH_TCL
if (alias->flags & ALIAS_TCL)
Com_Printf ("%s : Tcl procedure\n", alias->name);
else
#endif
Com_Printf ("%s : %s\n", alias->name, alias->value);
m++;
}
Com_Printf ("------------\n%i/%i aliases\n", m, i);
ReSearchDone();
} else {
if ((alias = Cmd_FindAlias(name)))
#ifdef WITH_TCL
if (alias->flags & ALIAS_TCL)
Com_Printf ("%s : Tcl procedure\n", name);
else
#endif
Com_Printf ("%s : \"%s\"\n", Cmd_Argv(i), alias->value);
else
Com_Printf ("No such alias: %s\n", Cmd_Argv(i));
}
}
}
int Cmd_AliasCompare (const void *p1, const void *p2)
{
cmd_alias_t *a1, *a2;
a1 = *((cmd_alias_t **) p1);
a2 = *((cmd_alias_t **) p2);
if (a1->name[0] == '+') {
if (a2->name[0] == '+')
return strcasecmp(a1->name + 1, a2->name + 1);
else
return -1;
} else if (a1->name[0] == '-') {
if (a2->name[0] == '+')
return 1;
else if (a2->name[0] == '-')
return strcasecmp(a1->name + 1, a2->name + 1);
else
return -1;
} else if (a2->name[0] == '+' || a2->name[0] == '-') {
return 1;
} else {
return strcasecmp(a1->name, a2->name);
}
}
void Cmd_AliasList_f (void)
{
cmd_alias_t *a;
int i, c, m = 0;
static int count;
static cmd_alias_t *sorted_aliases[4096];
#define MAX_SORTED_ALIASES (sizeof(sorted_aliases) / sizeof(sorted_aliases[0]))
for (a = cmd_alias, count = 0; a && count < MAX_SORTED_ALIASES; a = a->next, count++)
sorted_aliases[count] = a;
qsort(sorted_aliases, count, sizeof (cmd_alias_t *), Cmd_AliasCompare);
if (count == MAX_SORTED_ALIASES)
assert(!"count == MAX_SORTED_ALIASES");
c = Cmd_Argc();
if (c>1)
if (!ReSearchInit(Cmd_Argv(1)))
return;
Com_Printf ("List of aliases:\n");
for (i = 0; i < count; i++) {
a = sorted_aliases[i];
if (c==1 || ReSearchMatch(a->name)) {
Com_Printf ("\x02%s :", sorted_aliases[i]->name);
Com_Printf (" %s\n", sorted_aliases[i]->value);
m++;
}
}
if (c>1)
ReSearchDone();
Com_Printf ("------------\n%i/%i aliases\n", m, count);
}
void Cmd_EditAlias_f (void)
{
cmd_alias_t *a;
char *s, final_string[MAXCMDLINE - 1];
int c;
c = Cmd_Argc();
if (c == 1) {
Com_Printf ("%s <name> : modify an alias\n", Cmd_Argv(0));
Com_Printf ("aliaslist : list all aliases\n");
return;
}
a = Cmd_FindAlias(Cmd_Argv(1));
if ( a == NULL ) {
s = Q_strdup("");
} else {
s = Q_strdup(a->value);
}
snprintf(final_string, sizeof(final_string), "/alias \"%s\" \"%s\"", Cmd_Argv(1), s);
Key_ClearTyping();
memcpy (key_lines[edit_line]+1, str2wcs(final_string), strlen(final_string)*sizeof(wchar));
Q_free(s);
}
static cmd_alias_t* Cmd_AliasCreate (char* name)
{
cmd_alias_t *a;
int key;
key = Com_HashKey(name) % ALIAS_HASHPOOL_SIZE;
a = (cmd_alias_t *) Q_malloc(sizeof(cmd_alias_t));
a->next = cmd_alias;
cmd_alias = a;
a->hash_next = cmd_alias_hash[key];
cmd_alias_hash[key] = a;
strlcpy (a->name, name, sizeof (a->name));
return a;
}
//Creates a new command that executes a command string (possibly ; separated)
void Cmd_Alias_f (void)
{
cmd_alias_t *a;
char *s;
int c, key;
c = Cmd_Argc();
if (c == 1) {
Com_Printf ("%s <name> <command> : create or modify an alias\n", Cmd_Argv(0));
Com_Printf ("aliaslist : list all aliases\n");
return;
}
s = Cmd_Argv(1);
if (strlen(s) >= MAX_ALIAS_NAME) {
Com_Printf ("Alias name is too long\n");
return;
}
key = Com_HashKey(s) % ALIAS_HASHPOOL_SIZE;
// if the alias already exists, reuse it
for (a = cmd_alias_hash[key]; a; a = a->hash_next) {
if (!strcasecmp(a->name, s)) {
Q_free(a->value);
break;
}
}
if (!a) {
a = (cmd_alias_t *) Q_malloc(sizeof(cmd_alias_t));
a->next = cmd_alias;
cmd_alias = a;
a->hash_next = cmd_alias_hash[key];
cmd_alias_hash[key] = a;
}
strlcpy (a->name, s, sizeof (a->name));
a->flags = 0;
// QW262 -->
s=Cmd_MakeArgs(2);
while (*s) {
if (*s == '%' && ( s[1]>='0' || s[1]<='9')) {
a->flags |= ALIAS_HAS_PARAMETERS;
break;
}
++s;
}
// <-- QW262
if (cbuf_current == &cbuf_svc)
a->flags |= ALIAS_SERVER;
if (!strcasecmp(Cmd_Argv(0), "tempalias"))
a->flags |= ALIAS_TEMP;
// copy the rest of the command line
a->value = Q_strdup(Cmd_MakeArgs(2));
}
qbool Cmd_DeleteAlias (char *name)
{
cmd_alias_t *a, *prev;
int key;
key = Com_HashKey (name) % ALIAS_HASHPOOL_SIZE;
prev = NULL;
for (a = cmd_alias_hash[key]; a; a = a->hash_next) {
if (!strcasecmp(a->name, name)) {
// unlink from hash
if (prev)
prev->hash_next = a->hash_next;
else
cmd_alias_hash[key] = a->hash_next;
break;
}
prev = a;
}
if (!a)
return false; // not found
prev = NULL;
for (a = cmd_alias; a; a = a->next) {
if (!strcasecmp(a->name, name)) {
// unlink from alias list
if (prev)
prev->next = a->next;
else
cmd_alias = a->next;
// free
Q_free(a->value);
Q_free(a);
return true;
}
prev = a;
}
assert(!"Cmd_DeleteAlias: alias list broken");
return false; // shut up compiler
}
void Cmd_UnAlias (qbool use_regex)
{
int i;
char *name;
cmd_alias_t *a, *next;
qbool re_search = false;
if (Cmd_Argc() < 2) {
Com_Printf ("unalias <cvar> [<cvar2>..]: erase an existing alias\n");
return;
}
for (i=1; i<Cmd_Argc(); i++) {
name = Cmd_Argv(i);
if (use_regex && (re_search = IsRegexp(name)))
if(!ReSearchInit(name))
continue;
if (strlen(name) >= MAX_ALIAS_NAME) {
Com_Printf ("Alias name is too long: \"%s\"\n", Cmd_Argv(i));
continue;
}
if (use_regex && re_search) {
for (a = cmd_alias; a; a = next) {
next = a->next;
if (ReSearchMatch(a->name))
Cmd_DeleteAlias(a->name);
}
} else {
if (!Cmd_DeleteAlias(Cmd_Argv(i)))
Com_Printf ("unalias: unknown alias \"%s\"\n", Cmd_Argv(i));
}
if (use_regex && re_search)
ReSearchDone();
}
}
void Cmd_UnAlias_f (void)
{
Cmd_UnAlias(false);
}
void Cmd_UnAlias_re_f (void)
{
Cmd_UnAlias(true);
}
/*
* Remove all aliases unless connected, then remove
* all aliases except the server created aliases
*/
void Cmd_UnAliasAll_f (void)
{
cmd_alias_t *a, *next;
/* FIXME: Optimize this, its n^2 slow atm since Cmd_DeleteAlias will loop through
* the list again for each entry
*/
if (cls.state >= ca_connected) {
Com_Printf("Connected to a server, will not remove server aliases\n");
for (a = cmd_alias; a; a = next) {
next = a->next;
if ((a->flags & ALIAS_SERVER) == 0) {
Cmd_DeleteAlias(a->name);
}
}
} else {
for (a = cmd_alias; a ; a = next) {
next = a->next;
Q_free(a->value);
Q_free(a);
}
cmd_alias = NULL;
// clear hash
memset (cmd_alias_hash, 0, sizeof(cmd_alias_t*) * ALIAS_HASHPOOL_SIZE);
}
}
void DeleteServerAliases(void)
{
cmd_alias_t *a, *next;
for (a = cmd_alias; a; a = next) {
next = a->next;
if (a->flags & ALIAS_SERVER)
Cmd_DeleteAlias (a->name);
}
}
/*
=============================================================================
LEGACY COMMANDS
=============================================================================
*/
typedef struct legacycmd_s
{
char *oldname, *newname;
struct legacycmd_s *next;
} legacycmd_t;
static legacycmd_t *legacycmds = NULL;
void Cmd_AddLegacyCommand (char *oldname, char *newname)
{
legacycmd_t *cmd;
cmd = (legacycmd_t *) Q_malloc(sizeof(legacycmd_t));
cmd->next = legacycmds;
legacycmds = cmd;
cmd->oldname = oldname;
cmd->newname = newname;
}
qbool Cmd_IsLegacyCommand (char *oldname)
{
legacycmd_t *cmd;
for (cmd = legacycmds; cmd; cmd = cmd->next) {
if (!strcasecmp(cmd->oldname, oldname))
return true;
}
return false;
}
static qbool Cmd_LegacyCommand (void)
{
static qbool recursive = false;
legacycmd_t *cmd;
char text[1024];
for (cmd = legacycmds; cmd; cmd = cmd->next) {
if (!strcasecmp(cmd->oldname, Cmd_Argv(0)))
break;
}
if (!cmd)
return false;
if (!cmd->newname[0])
return true; // just ignore this command
// build new command string
strlcpy(text, cmd->newname, sizeof(text));
strlcat(text, " ", sizeof(text));
strlcat(text, Cmd_Args(), sizeof(text));