/
exec.cpp
1397 lines (1196 loc) · 37 KB
/
exec.cpp
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
/** @file console.cpp Console subsystem.
*
* @todo The Console subsystem should be rewritten to be a de::System and it
* should use Doomsday Script as the underlying engine; everything should be
* mapped to Doomsday Script processes, functions, variables, etc., making the
* Console a mere convenience layer. -jk
*
* @authors Copyright © 2003-2014 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2014 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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, see:
* http://www.gnu.org/licenses</small>
*/
#include "doomsday/console/exec.h"
#include <cctype>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <QFile>
#include <QTextStream>
#include <de/memory.h>
#include <de/charsymbols.h>
#include <de/App>
#include <de/game/Game>
#include <de/LogBuffer>
#include <de/Time>
#include "doomsday/console/knownword.h"
#include "doomsday/console/cmd.h"
#include "doomsday/console/var.h"
#include "doomsday/console/alias.h"
#include "doomsday/filesys/fs_main.h"
#include "doomsday/filesys/fs_util.h"
#include "doomsday/uri.h"
#include "dd_share.h"
using namespace de;
#define SC_EMPTY_QUOTE -1
// Length of the print buffer. Used in conPrintf and in Console_Message.
// If console messages are longer than this, an error will occur.
// Needed because we can't sizeof a malloc'd block.
#define PRBUFF_SIZE 0x2000
// Operators for the "if" command.
enum {
IF_EQUAL,
IF_NOT_EQUAL,
IF_GREATER,
IF_LESS,
IF_GEQUAL,
IF_LEQUAL
};
#define CMDTYPESTR(src) \
(src == CMDS_DDAY? "a direct call" \
: src == CMDS_GAME? "a game library call" \
: src == CMDS_CONSOLE? "the console" \
: src == CMDS_BIND? "a binding" \
: src == CMDS_CONFIG? "a cfg file" \
: src == CMDS_PROFILE? "a player profile" \
: src == CMDS_CMDLINE? "the command line" \
: src == CMDS_SCRIPT? "an action command" : "???")
typedef struct execbuff_s {
dd_bool used; // Is this in use?
timespan_t when; // System time when to execute the command.
byte source; // Where the command came from
// (console input, a cfg file etc..)
dd_bool isNetCmd; // Command was sent over the net to us.
char subCmd[1024]; // A single command w/args.
} execbuff_t;
D_CMD(AddSub);
D_CMD(IncDec);
D_CMD(Alias);
D_CMD(Echo);
D_CMD(Help);
D_CMD(If);
D_CMD(Parse);
D_CMD(Quit);
D_CMD(Repeat);
D_CMD(Toggle);
D_CMD(Wait);
D_CMD(InspectMobj);
D_CMD(DebugCrash);
D_CMD(DebugError);
void initVariableBindings(Binder &);
static int executeSubCmd(const char *subCmd, byte src, dd_bool isNetCmd);
static void Con_SplitIntoSubCommands(char const *command,
timespan_t markerOffset, byte src,
dd_bool isNetCmd);
static void Con_ClearExecBuffer(void);
byte ConsoleSilent = false;
static dd_bool ConsoleInited; // Has Con_Init() been called?
static Binder consoleBinder;
static execbuff_t * exBuff;
static int exBuffSize;
static execbuff_t * curExec;
void Con_Register(void)
{
C_CMD("add", NULL, AddSub);
C_CMD("after", "is", Wait);
C_CMD("alias", NULL, Alias);
C_CMD("dec", NULL, IncDec);
C_CMD("echo", "s*", Echo);
C_CMD("print", "s*", Echo);
C_CMD("exec", "s*", Parse);
C_CMD("if", NULL, If);
C_CMD("inc", NULL, IncDec);
C_CMD("repeat", "ifs", Repeat);
C_CMD("sub", NULL, AddSub);
C_CMD("toggle", "s", Toggle);
#ifdef _DEBUG
C_CMD("crash", NULL, DebugCrash);
#endif
Con_DataRegister();
}
static void PrepareCmdArgs(cmdargs_t *cargs, const char *lpCmdLine)
{
#define IS_ESC_CHAR(x) ((x) == '"' || (x) == '\\' || (x) == '{' || (x) == '}')
size_t i, len = strlen(lpCmdLine);
// Prepare the data.
memset(cargs, 0, sizeof(cmdargs_t));
strcpy(cargs->cmdLine, lpCmdLine);
// Prepare.
for(i = 0; i < len; ++i)
{
// Whitespaces are separators.
if(DENG_ISSPACE(cargs->cmdLine[i]))
cargs->cmdLine[i] = 0;
if(cargs->cmdLine[i] == '\\' && IS_ESC_CHAR(cargs->cmdLine[i + 1]))
{ // Escape sequence.
memmove(cargs->cmdLine + i, cargs->cmdLine + i + 1,
sizeof(cargs->cmdLine) - i - 1);
len--;
continue;
}
if(cargs->cmdLine[i] == '"')
{ // Find the end.
size_t start = i;
cargs->cmdLine[i] = 0;
for(++i; i < len && cargs->cmdLine[i] != '"'; ++i)
{
if(cargs->cmdLine[i] == '\\' && IS_ESC_CHAR(cargs->cmdLine[i + 1])) // Escape sequence?
{
memmove(cargs->cmdLine + i, cargs->cmdLine + i + 1,
sizeof(cargs->cmdLine) - i - 1);
len--;
continue;
}
}
// Quote not terminated?
if(i == len)
break;
// An empty set of quotes?
if(i == start + 1)
cargs->cmdLine[i] = SC_EMPTY_QUOTE;
else
cargs->cmdLine[i] = 0;
}
if(cargs->cmdLine[i] == '{')
{ // Find matching end, braces are another notation for quotes.
int level = 0;
size_t start = i;
cargs->cmdLine[i] = 0;
for(++i; i < len; ++i)
{
if(cargs->cmdLine[i] == '\\' && IS_ESC_CHAR(cargs->cmdLine[i + 1])) // Escape sequence?
{
memmove(cargs->cmdLine + i, cargs->cmdLine + i + 1,
sizeof(cargs->cmdLine) - i - 1);
len--;
i++;
continue;
}
if(cargs->cmdLine[i] == '}')
{
if(!level)
break;
level--;
}
if(cargs->cmdLine[i] == '{')
level++;
}
// Quote not terminated?
if(i == len)
break;
// An empty set of braces?
if(i == start + 1)
cargs->cmdLine[i] = SC_EMPTY_QUOTE;
else
cargs->cmdLine[i] = 0;
}
}
// Scan through the cmdLine and get the beginning of each token.
cargs->argc = 0;
for(i = 0; i < len; ++i)
{
if(!cargs->cmdLine[i])
continue;
// Is this an empty quote?
if(cargs->cmdLine[i] == char(SC_EMPTY_QUOTE))
cargs->cmdLine[i] = 0; // Just an empty string.
cargs->argv[cargs->argc++] = cargs->cmdLine + i;
i += strlen(cargs->cmdLine + i);
}
#undef IS_ESC_CHAR
}
static Value *Function_Console_ListVars(Context &, Function::ArgumentValues const &args)
{
StringList vars;
Con_TermsRegex(vars, args.at(0)->asText(), WT_CVAR);
std::unique_ptr<ArrayValue> result(new ArrayValue);
for(String v : vars)
{
*result << new TextValue(v);
}
return result.release();
}
dd_bool Con_Init(void)
{
if(ConsoleInited) return true;
LOG_SCR_VERBOSE("Initializing the console...");
// Doomsday Script bindings to access console features.
/// @todo Some of these should become obsolete once cvars/cmds are moved to
/// DS records.
consoleBinder.initNew();
initVariableBindings(consoleBinder);
consoleBinder << DENG2_FUNC(Console_ListVars, "listVars", "pattern");
App::scriptSystem().addNativeModule("Console", consoleBinder.module());
exBuff = NULL;
exBuffSize = 0;
ConsoleInited = true;
return true;
}
void Con_Shutdown(void)
{
if(!ConsoleInited) return;
LOG_SCR_VERBOSE("Shutting down the console...");
Con_ClearExecBuffer();
Con_ShutdownDatabases();
consoleBinder.deinit();
ConsoleInited = false;
}
#if 0 // should use libshell!
#ifdef __CLIENT__
/**
* Send a console command to the server.
* This shouldn't be called unless we're logged in with the right password.
*/
static void Con_Send(const char *command, byte src, int silent)
{
ushort len = (ushort) strlen(command);
LOG_AS("Con_Send");
if(len >= 0x8000)
{
LOGDEV_NET_ERROR("Command is too long, length=%i") << len;
return;
}
Msg_Begin(PKT_COMMAND2);
// Mark high bit for silent commands.
Writer_WriteUInt16(msgWriter, len | (silent ? 0x8000 : 0));
Writer_WriteUInt16(msgWriter, 0); // flags. Unused at present.
Writer_WriteByte(msgWriter, src);
Writer_Write(msgWriter, command, len);
Msg_End();
Net_SendBuffer(0, 0);
}
#endif // __CLIENT__
#endif
static void Con_QueueCmd(const char *singleCmd, timespan_t atSecond,
byte source, dd_bool isNetCmd)
{
execbuff_t *ptr = NULL;
int i;
// Look for an empty spot.
for(i = 0; i < exBuffSize; ++i)
if(!exBuff[i].used)
{
ptr = exBuff + i;
break;
}
if(ptr == NULL)
{
// No empty places, allocate a new one.
exBuff = (execbuff_t *) M_Realloc(exBuff, sizeof(execbuff_t) * ++exBuffSize);
ptr = exBuff + exBuffSize - 1;
}
ptr->used = true;
strcpy(ptr->subCmd, singleCmd);
ptr->when = atSecond;
ptr->source = source;
ptr->isNetCmd = isNetCmd;
}
static void Con_ClearExecBuffer(void)
{
M_Free(exBuff);
exBuff = NULL;
exBuffSize = 0;
}
/**
* The execbuffer is used to schedule commands for later.
*
* @return @c false, if an executed command fails.
*/
static dd_bool Con_CheckExecBuffer(void)
{
#define BUFFSIZE 1024 /// @todo Rewrite all of this; use de::String. -jk
// dd_bool allDone;
dd_bool ret = true;
int i; //, count = 0;
char storage[BUFFSIZE];
storage[255] = 0;
// do // We'll keep checking until all is done.
{
//allDone = true;
TimeDelta const now = TimeDelta::sinceStartOfProcess();
// Execute the commands whose time has come.
for(i = 0; i < exBuffSize; ++i)
{
execbuff_t *ptr = exBuff + i;
if(!ptr->used || ptr->when > now)
continue;
// We'll now execute this command.
curExec = ptr;
ptr->used = false;
strncpy(storage, ptr->subCmd, BUFFSIZE-1);
if(!executeSubCmd(storage, ptr->source, ptr->isNetCmd))
ret = false;
//allDone = false;
}
// if(count++ > 100) break; // Don't hang here.
/* {
DENG_ASSERT(!"Execution buffer overflow");
LOG_SCR_ERROR("Console execution buffer overflow! Everything canceled!");
Con_ClearExecBuffer();
break;
}*/
} //while(!allDone);
return ret;
#undef BUFFSIZE
}
void Con_Ticker(timespan_t /*time*/)
{
Con_CheckExecBuffer();
}
/**
* expCommand gets reallocated in the expansion process.
* This could be bit more clever.
*/
static void expandWithArguments(char **expCommand, cmdargs_t *args)
{
int p;
char *text = *expCommand;
size_t i, off, size = strlen(text) + 1;
for(i = 0; text[i]; ++i)
{
if(text[i] == '%' && (text[i + 1] >= '1' && text[i + 1] <= '9'))
{
char *substr;
int aidx = text[i + 1] - '1' + 1;
// Expand! (or delete)
if(aidx > args->argc - 1)
substr = (char *) "";
else
substr = args->argv[aidx];
// First get rid of the %n.
memmove(text + i, text + i + 2, size - i - 2);
// Reallocate.
off = strlen(substr);
text = *expCommand = (char *) M_Realloc(*expCommand, size += off - 2);
if(off)
{
// Make room for the insert.
memmove(text + i + off, text + i, size - i - off);
memcpy(text + i, substr, off);
}
i += off - 1;
}
else if(text[i] == '%' && text[i + 1] == '0')
{
// First get rid of the %0.
memmove(text + i, text + i + 2, size - i - 2);
text = *expCommand = (char *) M_Realloc(*expCommand, size -= 2);
for(p = args->argc - 1; p > 0; p--)
{
off = strlen(args->argv[p]) + 1;
text = *expCommand = (char *) M_Realloc(*expCommand, size += off);
memmove(text + i + off, text + i, size - i - off);
text[i] = ' ';
memcpy(text + i + 1, args->argv[p], off - 1);
}
}
}
}
/**
* The command is executed forthwith!!
*/
static int executeSubCmd(const char *subCmd, byte src, dd_bool isNetCmd)
{
cmdargs_t args;
ccmd_t *ccmd;
cvar_t *cvar;
calias_t *cal;
PrepareCmdArgs(&args, subCmd);
if(!args.argc)
return true;
/*
if(args.argc == 1) // Possibly a control command?
{
if(P_ControlExecute(args.argv[0]))
{
// It was a control command. No further processing is
// necessary.
return true;
}
}
*/
#if 0
#ifdef __CLIENT__
// If logged in, send command to server at this point.
if(!isServer && netLoggedIn)
{
// We have logged in on the server. Send the command there.
Con_Send(subCmd, src, ConsoleSilent);
return true;
}
#endif
#endif
// Try to find a matching console command.
ccmd = Con_FindCommandMatchArgs(&args);
if(ccmd != NULL)
{
// Found a match. Are we allowed to execute?
dd_bool canExecute = true;
// Trying to issue a command requiring a loaded game?
// dj: This should be considered a short-term solution. Ideally we want some namespacing mechanics.
if((ccmd->flags & CMDF_NO_NULLGAME) && App::game().isNull())
{
LOG_SCR_ERROR("Execution of command '%s' is only allowed when a game is loaded") << ccmd->name;
return true;
}
/// @todo Access control needs revising. -jk
#if 0
// A dedicated server, trying to execute a ccmd not available to us?
if(isDedicated && (ccmd->flags & CMDF_NO_DEDICATED))
{
LOG_SCR_ERROR("Execution of command '%s' not possible in dedicated mode") << ccmd->name;
return true;
}
// Net commands sent to servers have extra protection.
if(isServer && isNetCmd)
{
// Is the command permitted for use by clients?
if(ccmd->flags & CMDF_CLIENT)
{
LOG_NET_ERROR("Execution of command '%s' blocked (client attempted invocation);"
"this command is not permitted for use by clients") << ccmd->name;
return true;
}
// Are ANY commands from this (remote) src permitted for use
// by our clients?
// NOTE:
// This is an interim measure to protect against abuse of the
// most vulnerable invocation methods.
// Once all console commands are updated with the correct usage
// flags we can then remove these restrictions or make them
// optional for servers.
//
// The next step will then be allowing select console commands
// to be executed by non-logged in clients.
switch(src)
{
case CMDS_UNKNOWN:
case CMDS_CONFIG:
case CMDS_PROFILE:
case CMDS_CMDLINE:
case CMDS_SCRIPT:
LOG_NET_ERROR("Execution of command '%s' blocked (client attempted invocation via %s); "
"this method is not permitted by clients") << ccmd->name << CMDTYPESTR(src);
return true;
default:
break;
}
}
#endif
// Is the src permitted for this command?
switch(src)
{
case CMDS_UNKNOWN:
canExecute = false;
break;
case CMDS_DDAY:
if(ccmd->flags & CMDF_DDAY)
canExecute = false;
break;
case CMDS_GAME:
if(ccmd->flags & CMDF_GAME)
canExecute = false;
break;
case CMDS_CONSOLE:
if(ccmd->flags & CMDF_CONSOLE)
canExecute = false;
break;
case CMDS_BIND:
if(ccmd->flags & CMDF_BIND)
canExecute = false;
break;
case CMDS_CONFIG:
if(ccmd->flags & CMDF_CONFIG)
canExecute = false;
break;
case CMDS_PROFILE:
if(ccmd->flags & CMDF_PROFILE)
canExecute = false;
break;
case CMDS_CMDLINE:
if(ccmd->flags & CMDF_CMDLINE)
canExecute = false;
break;
case CMDS_SCRIPT:
if(ccmd->flags & CMDF_DED)
canExecute = false;
break;
default:
return true;
}
if(!canExecute)
{
LOG_SCR_ERROR("'%s' cannot be executed via %s") << ccmd->name << CMDTYPESTR(src);
return true;
}
if(canExecute)
{
/**
* Execute the command!
* \note Console command execution may invoke a full update of the
* console databases; thus the @c ccmd pointer may be invalid after
* this call.
*/
int result;
if((result = ccmd->execFunc(src, args.argc, args.argv)) == false)
{
LOG_SCR_ERROR("'%s' failed") << args.argv[0];
}
return result;
}
return true;
}
// Then try the cvars?
cvar = Con_FindVariable(args.argv[0]);
if(cvar != NULL)
{
dd_bool outOfRange = false, setting = false, hasCallback;
/**
* \note Change notification callback execution may invoke
* a full update of the console databases; thus the @c cvar
* pointer may be invalid once a callback executes.
*/
hasCallback = (cvar->notifyChanged != 0);
if(args.argc == 2 ||
(args.argc == 3 && !qstricmp(args.argv[1], "force")))
{
char* argptr = args.argv[args.argc - 1];
dd_bool forced = args.argc == 3;
setting = true;
if(cvar->flags & CVF_READ_ONLY)
{
CVar_PrintReadOnlyWarning(cvar);
}
else if((cvar->flags & CVF_PROTECTED) && !forced)
{
AutoStr* name = CVar_ComposePath(cvar);
LOG_SCR_NOTE("%s is protected; you shouldn't change its value -- "
"use the command: " _E(b) "'%s force %s'" _E(.) " to modify it anyway")
<< Str_Text(name) << Str_Text(name) << argptr;
}
else
{
switch(cvar->type)
{
case CVT_BYTE: {
byte val = (byte) strtol(argptr, NULL, 0);
if(!forced &&
((!(cvar->flags & CVF_NO_MIN) && val < cvar->min) ||
(!(cvar->flags & CVF_NO_MAX) && val > cvar->max)))
outOfRange = true;
else
CVar_SetInteger(cvar, val);
break;
}
case CVT_INT: {
int val = strtol(argptr, NULL, 0);
if(!forced &&
((!(cvar->flags & CVF_NO_MIN) && val < cvar->min) ||
(!(cvar->flags & CVF_NO_MAX) && val > cvar->max)))
outOfRange = true;
else
CVar_SetInteger(cvar, val);
break;
}
case CVT_FLOAT: {
float val = strtod(argptr, NULL);
if(!forced &&
((!(cvar->flags & CVF_NO_MIN) && val < cvar->min) ||
(!(cvar->flags & CVF_NO_MAX) && val > cvar->max)))
outOfRange = true;
else
CVar_SetFloat(cvar, val);
break;
}
case CVT_CHARPTR:
CVar_SetString(cvar, argptr);
break;
case CVT_URIPTR:
/// @todo Sanitize and validate against known schemas.
CVar_SetUri(cvar, de::Uri(argptr, RC_NULL));
break;
default: break;
}
}
}
if(outOfRange)
{
AutoStr* name = CVar_ComposePath(cvar);
if(!(cvar->flags & (CVF_NO_MIN | CVF_NO_MAX)))
{
char temp[20];
strcpy(temp, M_TrimmedFloat(cvar->min));
LOG_SCR_ERROR("%s <= %s <= %s") << temp << Str_Text(name) << M_TrimmedFloat(cvar->max);
}
else if(cvar->flags & CVF_NO_MAX)
{
LOG_SCR_ERROR("%s >= %s") << Str_Text(name) << M_TrimmedFloat(cvar->min);
}
else
{
LOG_SCR_ERROR("%s <= %s") << Str_Text(name) << M_TrimmedFloat(cvar->max);
}
}
else if(!setting) // Show the value.
{
if(setting && hasCallback)
{
// Lookup the cvar again - our pointer may have been invalidated.
cvar = Con_FindVariable(args.argv[0]);
}
if(cvar)
{
// It still exists.
Con_PrintCVar(cvar, "");
}
}
return true;
}
// How about an alias then?
cal = Con_FindAlias(args.argv[0]);
if(cal != NULL)
{
char* expCommand;
// Expand the command with arguments.
expCommand = (char *) M_Malloc(strlen(cal->command) + 1);
strcpy(expCommand, cal->command);
expandWithArguments(&expCommand, &args);
// Do it, man!
Con_SplitIntoSubCommands(expCommand, 0, src, isNetCmd);
M_Free(expCommand);
return true;
}
// What *is* that?
if(Con_FindCommand(args.argv[0]))
{
LOG_SCR_WARNING("%s: command arguments invalid") << args.argv[0];
Con_Executef(CMDS_DDAY, false, "help %s", args.argv[0]);
}
else
{
LOG_SCR_MSG("%s: unknown identifier") << args.argv[0];
}
return false;
}
/**
* Splits the command into subcommands and queues them into the
* execution buffer.
*/
static void Con_SplitIntoSubCommands(const char *command,
timespan_t markerOffset, byte src,
dd_bool isNetCmd)
{
#define BUFFSIZE 2048
char subCmd[BUFFSIZE];
int inQuotes = false, escape = false;
size_t gPos = 0, scPos = 0, len;
// Is there a command to execute?
if(!command || command[0] == 0)
return;
// Jump over initial semicolons.
len = strlen(command);
while(gPos < len && command[gPos] == ';' && command[gPos] != 0)
gPos++;
subCmd[0] = subCmd[BUFFSIZE-1] = 0;
// The command may actually contain many commands, separated
// with semicolons. This isn't a very clear algorithm...
for(; command[gPos];)
{
escape = false;
if(inQuotes && command[gPos] == '\\') // Escape sequence?
{
subCmd[scPos++] = command[gPos++];
escape = true;
}
if(command[gPos] == '"' && !escape)
inQuotes = !inQuotes;
// Collect characters.
subCmd[scPos++] = command[gPos++];
if(subCmd[0] == ' ')
scPos = 0; // No spaces in the beginning.
if((command[gPos] == ';' && !inQuotes) || command[gPos] == 0)
{
while(gPos < len && command[gPos] == ';' && command[gPos] != 0)
gPos++;
// The subcommand ends.
subCmd[scPos] = 0;
}
else
{
continue;
}
// Queue it.
Con_QueueCmd(subCmd, TimeDelta::sinceStartOfProcess() + markerOffset, src, isNetCmd);
scPos = 0;
}
#undef BUFFSIZE
}
int Con_Execute(byte src, const char *command, int silent, dd_bool netCmd)
{
int ret;
if(silent)
ConsoleSilent = true;
Con_SplitIntoSubCommands(command, 0, src, netCmd);
ret = Con_CheckExecBuffer();
if(silent)
ConsoleSilent = false;
return ret;
}
int Con_Executef(byte src, int silent, const char *command, ...)
{
va_list argptr;
char buffer[4096];
va_start(argptr, command);
dd_vsnprintf(buffer, sizeof(buffer), command, argptr);
va_end(argptr);
return Con_Execute(src, buffer, silent, false);
}
dd_bool Con_Parse(Path const &fileName, dd_bool silently)
{
// Relative paths are relative to the native working directory.
NativePath fn = NativePath::workPath() / NativePath(fileName).expand();
if(!QFile::exists(fn))
return false;
QFile file(fn);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
// This file is filled with console commands.
QTextStream in(&file);
int currentLine = 1;
while(!in.atEnd())
{
// Each line is a command.
String const line = String(in.readLine()).leftStrip();
if(!line.isEmpty() && line.first() != '#')
{
// Execute the commands silently.
if(!Con_Execute(CMDS_CONFIG, line.toUtf8().constData(), silently, false))
{
if(!silently)
{
LOG_SCR_WARNING("%s(%i): error executing command \"%s\"")
<< fn.pretty()
<< currentLine
<< line;
}
}
}
currentLine += 1;
}
return true;
}
/**
* Create an alias.
*/
static void makeAlias(char *aName, char *command)
{
calias_t *cal = Con_FindAlias(aName);
dd_bool remove = false;
// Will we remove this alias?
if(command == NULL)
remove = true;
else if(command[0] == 0)
remove = true;
if(cal && remove) // This alias will be removed.
{
Con_DeleteAlias(cal);
return; // We're done.
}
// Does the alias already exist?
if(cal)
{
cal->command = (char *) M_Realloc(cal->command, strlen(command) + 1);
strcpy(cal->command, command);
return;
}
// We need to create a new alias.
Con_AddAlias(aName, command);
}
D_CMD(Alias)
{
DENG2_UNUSED(src);
if(argc != 3 && argc != 2)
{
LOG_SCR_NOTE("Usage: %s (alias) (cmd)") << argv[0];
LOG_SCR_MSG("Example: alias bigfont \"font size 3\"");
LOG_SCR_MSG("Use %%1-%%9 to pass the alias arguments to the command.");
return true;
}
makeAlias(argv[1], argc == 3 ? argv[2] : NULL);
if(argc != 3)
{
LOG_SCR_MSG("Alias '%s' deleted") << argv[1];
}
return true;
}
D_CMD(Parse)
{
DENG2_UNUSED(src);
int i;
for(i = 1; i < argc; ++i)
{
LOG_SCR_MSG("Parsing \"%s\"") << argv[i];
Con_Parse(argv[i], false /*not silent*/);
}
return true;
}
D_CMD(Wait)
{
DENG2_UNUSED2(src, argc);
timespan_t offset;
offset = strtod(argv[1], NULL) / 35; // Offset in seconds.
if(offset < 0)
offset = 0;
Con_SplitIntoSubCommands(argv[2], offset, CMDS_CONSOLE, false);
return true;
}
D_CMD(Repeat)
{