-
Notifications
You must be signed in to change notification settings - Fork 2
/
console.c
3070 lines (2750 loc) · 82.5 KB
/
console.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.
*/
// console.c
#if !defined(WIN32) || defined(__MINGW32__)
# include <unistd.h>
#endif
#include <time.h>
#include "quakedef.h"
#include "thread.h"
// for u8_encodech
#include "ft2.h"
float con_cursorspeed = 4;
// lines up from bottom to display
int con_backscroll;
conbuffer_t con;
void *con_mutex = NULL;
#define CON_LINES(i) CONBUFFER_LINES(&con, i)
#define CON_LINES_LAST CONBUFFER_LINES_LAST(&con)
#define CON_LINES_COUNT CONBUFFER_LINES_COUNT(&con)
cvar_t con_notifytime = {CF_CLIENT | CF_ARCHIVE, "con_notifytime","3", "how long notify lines last, in seconds"};
cvar_t con_notify = {CF_CLIENT | CF_ARCHIVE, "con_notify","4", "how many notify lines to show"};
cvar_t con_notifyalign = {CF_CLIENT | CF_ARCHIVE, "con_notifyalign", "", "how to align notify lines: 0 = left, 0.5 = center, 1 = right, empty string = game default)"};
cvar_t con_chattime = {CF_CLIENT | CF_ARCHIVE, "con_chattime","30", "how long chat lines last, in seconds"};
cvar_t con_chat = {CF_CLIENT | CF_ARCHIVE, "con_chat","0", "how many chat lines to show in a dedicated chat area"};
cvar_t con_chatpos = {CF_CLIENT | CF_ARCHIVE, "con_chatpos","0", "where to put chat (negative: lines from bottom of screen, positive: lines below notify, 0: at top)"};
cvar_t con_chatrect = {CF_CLIENT | CF_ARCHIVE, "con_chatrect","0", "use con_chatrect_x and _y to position con_notify and con_chat freely instead of con_chatpos"};
cvar_t con_chatrect_x = {CF_CLIENT | CF_ARCHIVE, "con_chatrect_x","", "where to put chat, relative x coordinate of left edge on screen (use con_chatwidth for width)"};
cvar_t con_chatrect_y = {CF_CLIENT | CF_ARCHIVE, "con_chatrect_y","", "where to put chat, relative y coordinate of top edge on screen (use con_chat for line count)"};
cvar_t con_chatwidth = {CF_CLIENT | CF_ARCHIVE, "con_chatwidth","1.0", "relative chat window width"};
cvar_t con_textsize = {CF_CLIENT | CF_ARCHIVE, "con_textsize","8", "console text size in virtual 2D pixels"};
cvar_t con_notifysize = {CF_CLIENT | CF_ARCHIVE, "con_notifysize","8", "notify text size in virtual 2D pixels"};
cvar_t con_chatsize = {CF_CLIENT | CF_ARCHIVE, "con_chatsize","8", "chat text size in virtual 2D pixels (if con_chat is enabled)"};
cvar_t con_chatsound = {CF_CLIENT | CF_ARCHIVE, "con_chatsound","1", "enables chat sound to play on message"};
cvar_t con_chatsound_file = {CF_CLIENT, "con_chatsound_file","sound/misc/talk.wav", "The sound to play for chat messages"};
cvar_t con_chatsound_team_file = {CF_CLIENT, "con_chatsound_team_file","sound/misc/talk2.wav", "The sound to play for team chat messages"};
cvar_t con_chatsound_team_mask = {CF_CLIENT, "con_chatsound_team_mask","40","Magic ASCII code that denotes a team chat message"};
cvar_t sys_specialcharactertranslation = {CF_CLIENT | CF_SERVER, "sys_specialcharactertranslation", "1", "terminal console conchars to ASCII translation (set to 0 if your conchars.tga is for an 8bit character set or if you want raw output)"};
#ifdef WIN32
cvar_t sys_colortranslation = {CF_CLIENT | CF_SERVER, "sys_colortranslation", "0", "terminal console color translation (supported values: 0 = strip color codes, 1 = translate to ANSI codes, 2 = no translation)"};
#else
cvar_t sys_colortranslation = {CF_CLIENT | CF_SERVER, "sys_colortranslation", "1", "terminal console color translation (supported values: 0 = strip color codes, 1 = translate to ANSI codes, 2 = no translation)"};
#endif
cvar_t con_nickcompletion = {CF_CLIENT | CF_ARCHIVE, "con_nickcompletion", "1", "tab-complete nicks in console and message input"};
cvar_t con_nickcompletion_flags = {CF_CLIENT | CF_ARCHIVE, "con_nickcompletion_flags", "11", "Bitfield: "
"0: add nothing after completion. "
"1: add the last color after completion. "
"2: add a quote when starting a quote instead of the color. "
"4: will replace 1, will force color, even after a quote. "
"8: ignore non-alphanumerics. "
"16: ignore spaces. "};
#define NICKS_ADD_COLOR 1
#define NICKS_ADD_QUOTE 2
#define NICKS_FORCE_COLOR 4
#define NICKS_ALPHANUMERICS_ONLY 8
#define NICKS_NO_SPACES 16
cvar_t con_completion_playdemo = {CF_CLIENT | CF_ARCHIVE, "con_completion_playdemo", "*.dem", "completion pattern for the playdemo command"};
cvar_t con_completion_timedemo = {CF_CLIENT | CF_ARCHIVE, "con_completion_timedemo", "*.dem", "completion pattern for the timedemo command"};
cvar_t con_completion_exec = {CF_CLIENT | CF_ARCHIVE, "con_completion_exec", "*.cfg", "completion pattern for the exec command"};
cvar_t condump_stripcolors = {CF_CLIENT | CF_SERVER| CF_ARCHIVE, "condump_stripcolors", "0", "strip color codes from console dumps"};
cvar_t rcon_password = {CF_CLIENT | CF_SERVER | CF_PRIVATE, "rcon_password", "", "password to authenticate rcon commands; NOTE: changing rcon_secure clears rcon_password, so set rcon_secure always before rcon_password; may be set to a string of the form user1:pass1 user2:pass2 user3:pass3 to allow multiple user accounts - the client then has to specify ONE of these combinations"};
cvar_t rcon_secure = {CF_CLIENT | CF_SERVER, "rcon_secure", "0", "force secure rcon authentication (1 = time based, 2 = challenge based); NOTE: changing rcon_secure clears rcon_password, so set rcon_secure always before rcon_password"};
cvar_t rcon_secure_challengetimeout = {CF_CLIENT, "rcon_secure_challengetimeout", "5", "challenge-based secure rcon: time out requests if no challenge came within this time interval"};
cvar_t rcon_address = {CF_CLIENT, "rcon_address", "", "server address to send rcon commands to (when not connected to a server)"};
int con_linewidth;
int con_vislines;
qbool con_initialized;
// used for server replies to rcon command
lhnetsocket_t *rcon_redirect_sock = NULL;
lhnetaddress_t *rcon_redirect_dest = NULL;
int rcon_redirect_bufferpos = 0;
char rcon_redirect_buffer[1400];
qbool rcon_redirect_proquakeprotocol = false;
// generic functions for console buffers
void ConBuffer_Init(conbuffer_t *buf, int textsize, int maxlines, mempool_t *mempool)
{
buf->active = true;
buf->textsize = textsize;
buf->text = (char *) Mem_Alloc(mempool, textsize);
buf->maxlines = maxlines;
buf->lines = (con_lineinfo_t *) Mem_Alloc(mempool, maxlines * sizeof(*buf->lines));
buf->lines_first = 0;
buf->lines_count = 0;
}
/*! The translation table between the graphical font and plain ASCII --KB */
static char qfont_table[256] = {
'\0', '#', '#', '#', '#', '.', '#', '#',
'#', 9, 10, '#', ' ', 13, '.', '.',
'[', ']', '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', '.', '<', '=', '>',
' ', '!', '"', '#', '$', '%', '&', '\'',
'(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', ':', ';', '<', '=', '>', '?',
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', '{', '|', '}', '~', '<',
'<', '=', '>', '#', '#', '.', '#', '#',
'#', '#', ' ', '#', ' ', '>', '.', '.',
'[', ']', '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', '.', '<', '=', '>',
' ', '!', '"', '#', '$', '%', '&', '\'',
'(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', ':', ';', '<', '=', '>', '?',
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', '{', '|', '}', '~', '<'
};
/*
SanitizeString strips color tags from the string in
and writes the result on string out
*/
static void SanitizeString(char *in, char *out)
{
while(*in)
{
if(*in == STRING_COLOR_TAG)
{
++in;
if(!*in)
{
out[0] = STRING_COLOR_TAG;
out[1] = 0;
return;
}
else if (*in >= '0' && *in <= '9') // ^[0-9] found
{
++in;
if(!*in)
{
*out = 0;
return;
} else if (*in == STRING_COLOR_TAG) // ^[0-9]^ found, don't print ^[0-9]
continue;
}
else if (*in == STRING_COLOR_RGB_TAG_CHAR) // ^x found
{
if ( isxdigit(in[1]) && isxdigit(in[2]) && isxdigit(in[3]) )
{
in+=4;
if (!*in)
{
*out = 0;
return;
} else if (*in == STRING_COLOR_TAG) // ^xrgb^ found, don't print ^xrgb
continue;
}
else in--;
}
else if (*in != STRING_COLOR_TAG)
--in;
}
*out = qfont_table[*(unsigned char*)in];
++in;
++out;
}
*out = 0;
}
/*
================
ConBuffer_Clear
================
*/
void ConBuffer_Clear (conbuffer_t *buf)
{
buf->lines_count = 0;
}
/*
================
ConBuffer_Shutdown
================
*/
void ConBuffer_Shutdown(conbuffer_t *buf)
{
buf->active = false;
if (buf->text)
Mem_Free(buf->text);
if (buf->lines)
Mem_Free(buf->lines);
buf->text = NULL;
buf->lines = NULL;
}
/*
================
ConBuffer_FixTimes
Notifies the console code about the current time
(and shifts back times of other entries when the time
went backwards)
================
*/
void ConBuffer_FixTimes(conbuffer_t *buf)
{
int i;
if(buf->lines_count >= 1)
{
double diff = cl.time - CONBUFFER_LINES_LAST(buf).addtime;
if(diff < 0)
{
for(i = 0; i < buf->lines_count; ++i)
CONBUFFER_LINES(buf, i).addtime += diff;
}
}
}
/*
================
ConBuffer_DeleteLine
Deletes the first line from the console history.
================
*/
void ConBuffer_DeleteLine(conbuffer_t *buf)
{
if(buf->lines_count == 0)
return;
--buf->lines_count;
buf->lines_first = (buf->lines_first + 1) % buf->maxlines;
}
/*
================
ConBuffer_DeleteLastLine
Deletes the last line from the console history.
================
*/
void ConBuffer_DeleteLastLine(conbuffer_t *buf)
{
if(buf->lines_count == 0)
return;
--buf->lines_count;
}
/*
================
ConBuffer_BytesLeft
Checks if there is space for a line of the given length, and if yes, returns a
pointer to the start of such a space, and NULL otherwise.
================
*/
static char *ConBuffer_BytesLeft(conbuffer_t *buf, int len)
{
if(len > buf->textsize)
return NULL;
if(buf->lines_count == 0)
return buf->text;
else
{
char *firstline_start = buf->lines[buf->lines_first].start;
char *lastline_onepastend = CONBUFFER_LINES_LAST(buf).start + CONBUFFER_LINES_LAST(buf).len;
// the buffer is cyclic, so we first have two cases...
if(firstline_start < lastline_onepastend) // buffer is contiguous
{
// put at end?
if(len <= buf->text + buf->textsize - lastline_onepastend)
return lastline_onepastend;
// put at beginning?
else if(len <= firstline_start - buf->text)
return buf->text;
else
return NULL;
}
else // buffer has a contiguous hole
{
if(len <= firstline_start - lastline_onepastend)
return lastline_onepastend;
else
return NULL;
}
}
}
/*
================
ConBuffer_AddLine
Appends a given string as a new line to the console.
================
*/
void ConBuffer_AddLine(conbuffer_t *buf, const char *line, int len, int mask)
{
char *putpos;
con_lineinfo_t *p;
// developer_memory 1 during shutdown prints while conbuffer_t is being freed
if (!buf->active)
return;
ConBuffer_FixTimes(buf);
if(len >= buf->textsize)
{
// line too large?
// only display end of line.
line += len - buf->textsize + 1;
len = buf->textsize - 1;
}
while(!(putpos = ConBuffer_BytesLeft(buf, len + 1)) || buf->lines_count >= buf->maxlines)
ConBuffer_DeleteLine(buf);
memcpy(putpos, line, len);
putpos[len] = 0;
++buf->lines_count;
//fprintf(stderr, "Now have %d lines (%d -> %d).\n", buf->lines_count, buf->lines_first, CON_LINES_LAST);
p = &CONBUFFER_LINES_LAST(buf);
p->start = putpos;
p->len = len;
p->addtime = cl.time;
p->mask = mask;
p->height = -1; // calculate when needed
}
int ConBuffer_FindPrevLine(conbuffer_t *buf, int mask_must, int mask_mustnot, int start)
{
int i;
if(start == -1)
start = buf->lines_count;
for(i = start - 1; i >= 0; --i)
{
con_lineinfo_t *l = &CONBUFFER_LINES(buf, i);
if((l->mask & mask_must) != mask_must)
continue;
if(l->mask & mask_mustnot)
continue;
return i;
}
return -1;
}
const char *ConBuffer_GetLine(conbuffer_t *buf, int i)
{
static char copybuf[MAX_INPUTLINE]; // client only
con_lineinfo_t *l = &CONBUFFER_LINES(buf, i);
size_t sz = l->len+1 > sizeof(copybuf) ? sizeof(copybuf) : l->len+1;
strlcpy(copybuf, l->start, sz);
return copybuf;
}
/*
==============================================================================
LOGGING
==============================================================================
*/
/// \name Logging
//@{
cvar_t log_file = {CF_CLIENT | CF_SERVER, "log_file", "", "filename to log messages to"};
cvar_t log_file_stripcolors = {CF_CLIENT | CF_SERVER, "log_file_stripcolors", "0", "strip color codes from log messages"};
cvar_t log_dest_udp = {CF_CLIENT | CF_SERVER, "log_dest_udp", "", "UDP address to log messages to (in QW rcon compatible format); multiple destinations can be separated by spaces; DO NOT SPECIFY DNS NAMES HERE"};
char log_dest_buffer[1400]; // UDP packet
size_t log_dest_buffer_pos;
unsigned int log_dest_buffer_appending;
char crt_log_file [MAX_OSPATH] = "";
qfile_t* logfile = NULL;
unsigned char* logqueue = NULL;
size_t logq_ind = 0;
size_t logq_size = 0;
void Log_ConPrint (const char *msg);
//@}
static void Log_DestBuffer_Init(void)
{
memcpy(log_dest_buffer, "\377\377\377\377n", 5); // QW rcon print
log_dest_buffer_pos = 5;
}
static void Log_DestBuffer_Flush_NoLock(void)
{
lhnetaddress_t log_dest_addr;
lhnetsocket_t *log_dest_socket;
const char *s = log_dest_udp.string;
qbool have_opened_temp_sockets = false;
if(s) if(log_dest_buffer_pos > 5)
{
++log_dest_buffer_appending;
log_dest_buffer[log_dest_buffer_pos++] = 0;
if(!NetConn_HaveServerPorts() && !NetConn_HaveClientPorts()) // then temporarily open one
{
have_opened_temp_sockets = true;
NetConn_OpenServerPorts(true);
}
while(COM_ParseToken_Console(&s))
if(LHNETADDRESS_FromString(&log_dest_addr, com_token, 26000))
{
log_dest_socket = NetConn_ChooseClientSocketForAddress(&log_dest_addr);
if(!log_dest_socket)
log_dest_socket = NetConn_ChooseServerSocketForAddress(&log_dest_addr);
if(log_dest_socket)
NetConn_WriteString(log_dest_socket, log_dest_buffer, &log_dest_addr);
}
if(have_opened_temp_sockets)
NetConn_CloseServerPorts();
--log_dest_buffer_appending;
}
log_dest_buffer_pos = 0;
}
/*
====================
Log_DestBuffer_Flush
====================
*/
void Log_DestBuffer_Flush(void)
{
if (con_mutex)
Thread_LockMutex(con_mutex);
Log_DestBuffer_Flush_NoLock();
if (con_mutex)
Thread_UnlockMutex(con_mutex);
}
static const char* Log_Timestamp (const char *desc)
{
static char timestamp [128]; // init/shutdown only
time_t crt_time;
#if _MSC_VER >= 1400
struct tm crt_tm;
#else
struct tm *crt_tm;
#endif
char timestring [64];
// Build the time stamp (ex: "Wed Jun 30 21:49:08 1993");
time (&crt_time);
#if _MSC_VER >= 1400
localtime_s (&crt_tm, &crt_time);
strftime (timestring, sizeof (timestring), "%a %b %d %H:%M:%S %Y", &crt_tm);
#else
crt_tm = localtime (&crt_time);
strftime (timestring, sizeof (timestring), "%a %b %d %H:%M:%S %Y", crt_tm);
#endif
if (desc != NULL)
dpsnprintf (timestamp, sizeof (timestamp), "====== %s (%s) ======\n", desc, timestring);
else
dpsnprintf (timestamp, sizeof (timestamp), "====== %s ======\n", timestring);
return timestamp;
}
static void Log_Open (void)
{
if (logfile != NULL || log_file.string[0] == '\0')
return;
logfile = FS_OpenRealFile(log_file.string, "a", false);
if (logfile != NULL)
{
strlcpy (crt_log_file, log_file.string, sizeof (crt_log_file));
FS_Print (logfile, Log_Timestamp ("Log started"));
}
}
/*
====================
Log_Close
====================
*/
void Log_Close (void)
{
if (logfile == NULL)
return;
FS_Print (logfile, Log_Timestamp ("Log stopped"));
FS_Print (logfile, "\n");
FS_Close (logfile);
logfile = NULL;
crt_log_file[0] = '\0';
}
/*
====================
Log_Start
====================
*/
void Log_Start (void)
{
size_t pos;
size_t n;
Log_Open ();
// Dump the contents of the log queue into the log file and free it
if (logqueue != NULL)
{
unsigned char *temp = logqueue;
logqueue = NULL;
if(logq_ind != 0)
{
if (logfile != NULL)
FS_Write (logfile, temp, logq_ind);
if(*log_dest_udp.string)
{
for(pos = 0; pos < logq_ind; )
{
if(log_dest_buffer_pos == 0)
Log_DestBuffer_Init();
n = min(sizeof(log_dest_buffer) - log_dest_buffer_pos - 1, logq_ind - pos);
memcpy(log_dest_buffer + log_dest_buffer_pos, temp + pos, n);
log_dest_buffer_pos += n;
Log_DestBuffer_Flush_NoLock();
pos += n;
}
}
}
Mem_Free (temp);
logq_ind = 0;
logq_size = 0;
}
}
/*
================
Log_ConPrint
================
*/
void Log_ConPrint (const char *msg)
{
static qbool inprogress = false;
// don't allow feedback loops with memory error reports
if (inprogress)
return;
inprogress = true;
// Until the host is completely initialized, we maintain a log queue
// to store the messages, since the log can't be started before
if (logqueue != NULL)
{
size_t remain = logq_size - logq_ind;
size_t len = strlen (msg);
// If we need to enlarge the log queue
if (len > remain)
{
size_t factor = ((logq_ind + len) / logq_size) + 1;
unsigned char* newqueue;
logq_size *= factor;
newqueue = (unsigned char *)Mem_Alloc (tempmempool, logq_size);
memcpy (newqueue, logqueue, logq_ind);
Mem_Free (logqueue);
logqueue = newqueue;
remain = logq_size - logq_ind;
}
memcpy (&logqueue[logq_ind], msg, len);
logq_ind += len;
inprogress = false;
return;
}
// Check if log_file has changed
if (strcmp (crt_log_file, log_file.string) != 0)
{
Log_Close ();
Log_Open ();
}
// If a log file is available
if (logfile != NULL)
{
if (log_file_stripcolors.integer)
{
// sanitize msg
size_t len = strlen(msg);
char* sanitizedmsg = (char*)Mem_Alloc(tempmempool, len + 1);
memcpy (sanitizedmsg, msg, len);
SanitizeString(sanitizedmsg, sanitizedmsg); // SanitizeString's in pointer is always ahead of the out pointer, so this should work.
FS_Print (logfile, sanitizedmsg);
Mem_Free(sanitizedmsg);
}
else
{
FS_Print (logfile, msg);
}
}
inprogress = false;
}
/*
================
Log_Printf
================
*/
void Log_Printf (const char *logfilename, const char *fmt, ...)
{
qfile_t *file;
file = FS_OpenRealFile(logfilename, "a", true);
if (file != NULL)
{
va_list argptr;
va_start (argptr, fmt);
FS_VPrintf (file, fmt, argptr);
va_end (argptr);
FS_Close (file);
}
}
/*
==============================================================================
CONSOLE
==============================================================================
*/
/*
================
Con_ToggleConsole_f
================
*/
void Con_ToggleConsole_f(cmd_state_t *cmd)
{
if (Sys_CheckParm ("-noconsole"))
if (!(key_consoleactive & KEY_CONSOLEACTIVE_USER))
return; // only allow the key bind to turn off console
// toggle the 'user wants console' bit
key_consoleactive ^= KEY_CONSOLEACTIVE_USER;
Con_ClearNotify();
}
/*
================
Con_ClearNotify
================
*/
void Con_ClearNotify (void)
{
int i;
for(i = 0; i < CON_LINES_COUNT; ++i)
if(!(CON_LINES(i).mask & CON_MASK_CHAT))
CON_LINES(i).mask |= CON_MASK_HIDENOTIFY;
}
/*
================
Con_MessageMode_f
================
*/
static void Con_MessageMode_f(cmd_state_t *cmd)
{
key_dest = key_message;
chat_mode = 0; // "say"
if(Cmd_Argc(cmd) > 1)
{
dpsnprintf(chat_buffer, sizeof(chat_buffer), "%s ", Cmd_Args(cmd));
chat_bufferpos = (unsigned int)strlen(chat_buffer);
}
}
/*
================
Con_MessageMode2_f
================
*/
static void Con_MessageMode2_f(cmd_state_t *cmd)
{
key_dest = key_message;
chat_mode = 1; // "say_team"
if(Cmd_Argc(cmd) > 1)
{
dpsnprintf(chat_buffer, sizeof(chat_buffer), "%s ", Cmd_Args(cmd));
chat_bufferpos = (unsigned int)strlen(chat_buffer);
}
}
/*
================
Con_CommandMode_f
================
*/
static void Con_CommandMode_f(cmd_state_t *cmd)
{
key_dest = key_message;
if(Cmd_Argc(cmd) > 1)
{
dpsnprintf(chat_buffer, sizeof(chat_buffer), "%s ", Cmd_Args(cmd));
chat_bufferpos = (unsigned int)strlen(chat_buffer);
}
chat_mode = -1; // command
}
/*
================
Con_CheckResize
================
*/
void Con_CheckResize (void)
{
int i, width;
float f;
f = bound(1, con_textsize.value, 128);
if(f != con_textsize.value)
Cvar_SetValueQuick(&con_textsize, f);
width = (int)floor(vid_conwidth.value / con_textsize.value);
width = bound(1, width, con.textsize/4);
// FIXME uses con in a non abstracted way
if (width == con_linewidth)
return;
con_linewidth = width;
for(i = 0; i < CON_LINES_COUNT; ++i)
CON_LINES(i).height = -1; // recalculate when next needed
Con_ClearNotify();
con_backscroll = 0;
}
//[515]: the simplest command ever
//LadyHavoc: not so simple after I made it print usage...
static void Con_Maps_f(cmd_state_t *cmd)
{
if (Cmd_Argc(cmd) > 2)
{
Con_Printf("usage: maps [mapnameprefix]\n");
return;
}
else if (Cmd_Argc(cmd) == 2)
GetMapList(Cmd_Argv(cmd, 1), NULL, 0);
else
GetMapList("", NULL, 0);
}
static void Con_ConDump_f(cmd_state_t *cmd)
{
int i;
qfile_t *file;
if (Cmd_Argc(cmd) != 2)
{
Con_Printf("usage: condump <filename>\n");
return;
}
file = FS_OpenRealFile(Cmd_Argv(cmd, 1), "w", false);
if (!file)
{
Con_Printf(CON_ERROR "condump: unable to write file \"%s\"\n", Cmd_Argv(cmd, 1));
return;
}
if (con_mutex) Thread_LockMutex(con_mutex);
for(i = 0; i < CON_LINES_COUNT; ++i)
{
if (condump_stripcolors.integer)
{
// sanitize msg
size_t len = CON_LINES(i).len;
char* sanitizedmsg = (char*)Mem_Alloc(tempmempool, len + 1);
memcpy (sanitizedmsg, CON_LINES(i).start, len);
SanitizeString(sanitizedmsg, sanitizedmsg); // SanitizeString's in pointer is always ahead of the out pointer, so this should work.
FS_Write(file, sanitizedmsg, strlen(sanitizedmsg));
Mem_Free(sanitizedmsg);
}
else
{
FS_Write(file, CON_LINES(i).start, CON_LINES(i).len);
}
FS_Write(file, "\n", 1);
}
if (con_mutex) Thread_UnlockMutex(con_mutex);
FS_Close(file);
}
void Con_Clear_f(cmd_state_t *cmd)
{
if (con_mutex) Thread_LockMutex(con_mutex);
ConBuffer_Clear(&con);
if (con_mutex) Thread_UnlockMutex(con_mutex);
}
static void Con_RCon_ClearPassword_c(cvar_t *var)
{
// whenever rcon_secure is changed to 0, clear rcon_password for
// security reasons (prevents a send-rcon-password-as-plaintext
// attack based on NQ protocol session takeover and svc_stufftext)
if(var->integer <= 0)
Cvar_SetQuick(&rcon_password, "");
}
/*
================
Con_Init
================
*/
void Con_Init (void)
{
con_linewidth = 80;
ConBuffer_Init(&con, CON_TEXTSIZE, CON_MAXLINES, zonemempool);
if (Thread_HasThreads())
con_mutex = Thread_CreateMutex();
// Allocate a log queue, this will be freed after configs are parsed
logq_size = MAX_INPUTLINE;
logqueue = (unsigned char *)Mem_Alloc (tempmempool, logq_size);
logq_ind = 0;
Cvar_RegisterVariable (&sys_colortranslation);
Cvar_RegisterVariable (&sys_specialcharactertranslation);
Cvar_RegisterVariable (&log_file);
Cvar_RegisterVariable (&log_file_stripcolors);
Cvar_RegisterVariable (&log_dest_udp);
// support for the classic Quake option
// COMMANDLINEOPTION: Console: -condebug logs console messages to qconsole.log, see also log_file
if (Sys_CheckParm ("-condebug") != 0)
Cvar_SetQuick (&log_file, "qconsole.log");
// register our cvars
Cvar_RegisterVariable (&con_chat);
Cvar_RegisterVariable (&con_chatpos);
Cvar_RegisterVariable (&con_chatrect_x);
Cvar_RegisterVariable (&con_chatrect_y);
Cvar_RegisterVariable (&con_chatrect);
Cvar_RegisterVariable (&con_chatsize);
Cvar_RegisterVariable (&con_chattime);
Cvar_RegisterVariable (&con_chatwidth);
Cvar_RegisterVariable (&con_notify);
Cvar_RegisterVariable (&con_notifyalign);
Cvar_RegisterVariable (&con_notifysize);
Cvar_RegisterVariable (&con_notifytime);
Cvar_RegisterVariable (&con_textsize);
Cvar_RegisterVariable (&con_chatsound);
Cvar_RegisterVariable (&con_chatsound_file);
Cvar_RegisterVariable (&con_chatsound_team_file);
Cvar_RegisterVariable (&con_chatsound_team_mask);
// --blub
Cvar_RegisterVariable (&con_nickcompletion);
Cvar_RegisterVariable (&con_nickcompletion_flags);
Cvar_RegisterVariable (&con_completion_playdemo); // *.dem
Cvar_RegisterVariable (&con_completion_timedemo); // *.dem
Cvar_RegisterVariable (&con_completion_exec); // *.cfg
Cvar_RegisterVariable (&condump_stripcolors);
Cvar_RegisterVariable(&rcon_address);
Cvar_RegisterVariable(&rcon_secure);
Cvar_RegisterCallback(&rcon_secure, Con_RCon_ClearPassword_c);
Cvar_RegisterVariable(&rcon_secure_challengetimeout);
Cvar_RegisterVariable(&rcon_password);
// register our commands
Cmd_AddCommand(CF_CLIENT, "toggleconsole", Con_ToggleConsole_f, "opens or closes the console");
Cmd_AddCommand(CF_CLIENT, "messagemode", Con_MessageMode_f, "input a chat message to say to everyone");
Cmd_AddCommand(CF_CLIENT, "messagemode2", Con_MessageMode2_f, "input a chat message to say to only your team");
Cmd_AddCommand(CF_CLIENT, "commandmode", Con_CommandMode_f, "input a console command");
Cmd_AddCommand(CF_SHARED, "clear", Con_Clear_f, "clear console history");
Cmd_AddCommand(CF_SHARED, "maps", Con_Maps_f, "list information about available maps");
Cmd_AddCommand(CF_SHARED, "condump", Con_ConDump_f, "output console history to a file (see also log_file)");
con_initialized = true;
// initialize console window (only used by sys_win.c)
Sys_InitConsole();
Con_Print("Console initialized.\n");
}
void Con_Shutdown (void)
{
if (con_mutex) Thread_LockMutex(con_mutex);
ConBuffer_Shutdown(&con);
if (con_mutex) Thread_UnlockMutex(con_mutex);
if (con_mutex) Thread_DestroyMutex(con_mutex);con_mutex = NULL;
}
/*
================
Con_PrintToHistory
Handles cursor positioning, line wrapping, etc
All console printing must go through this in order to be displayed
If no console is visible, the notify window will pop up.
================
*/
static void Con_PrintToHistory(const char *txt, int mask)
{
// process:
// \n goes to next line
// \r deletes current line and makes a new one
static int cr_pending = 0;
static char buf[CON_TEXTSIZE]; // con_mutex
static int bufpos = 0;
if(!con.text) // FIXME uses a non-abstracted property of con
return;
for(; *txt; ++txt)
{
if(cr_pending)
{
ConBuffer_DeleteLastLine(&con);
cr_pending = 0;
}
switch(*txt)
{
case 0:
break;
case '\r':
ConBuffer_AddLine(&con, buf, bufpos, mask);
bufpos = 0;
cr_pending = 1;
break;
case '\n':
ConBuffer_AddLine(&con, buf, bufpos, mask);
bufpos = 0;
break;
default:
buf[bufpos++] = *txt;
if(bufpos >= con.textsize - 1) // FIXME uses a non-abstracted property of con
{
ConBuffer_AddLine(&con, buf, bufpos, mask);
bufpos = 0;
}
break;
}
}
}