2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/DebugConsole/Console.cpp $
15 * Routines for managing the debug console window.
18 * Revision 1.3 2002/06/09 04:41:16 relnev
19 * added copyright header
21 * Revision 1.2 2002/05/07 03:16:43 theoddone33
22 * The Great Newline Fix
24 * Revision 1.1.1.1 2002/05/03 03:28:08 root
28 * 4 6/04/99 10:35a Dave
30 * 3 10/13/98 9:28a Dave
31 * Started neatening up freespace.h. Many variables renamed and
32 * reorganized. Added AlphaColors.[h,cpp]
34 * 2 10/07/98 10:52a Dave
37 * 1 10/07/98 10:48a Dave
39 * 17 2/05/98 11:43a Allender
40 * enhcanced network statistic collection. Made callback in debug console
41 * to do networking if player is in the console
43 * 16 1/22/98 6:42p John
44 * Move game_flush out of debug console into freespace. Made object
45 * pair code throw out some weapons that turn. Added stats for how many
46 * object pair are actually checked.
48 * 15 1/16/98 11:56a Allender
49 * ability to scroll back in debug console, and call game_flush() when
52 * 14 1/10/98 1:14p John
53 * Added explanation to debug console commands
55 * 13 12/21/97 4:39p John
56 * fixed bug in name compare
58 * 12 12/21/97 4:33p John
59 * Made debug console functions a class that registers itself
60 * automatically, so you don't need to add the function to
63 * 11 9/13/97 9:31a Lawrance
64 * if playing a demo, clear key filter then reset it once done with debug
67 * 10 9/09/97 3:39p Sandeep
68 * warning level 4 bugs
70 * 9 6/13/97 3:50p John
71 * sped up debug console printf
73 * 8 6/13/97 3:27p John
74 * made debug console pretty
76 * 7 6/09/97 9:24a John
77 * Changed the way fonts are set.
79 * 6 5/29/97 3:09p John
80 * Took out debug menu.
81 * Made software scaler draw larger bitmaps.
82 * Optimized Direct3D some.
84 * 5 5/13/97 4:07p John
85 * made numbers also be marked as strings.
87 * 4 4/28/97 5:24p John
88 * made so : and \ don't get parsed out in debug console.
90 * 3 4/24/97 11:49a John
91 * added new lighting commands to console.
93 * 2 4/24/97 10:36a John
94 * moved the debug console stuff into it's own lib... made it compile with
97 * 1 4/24/97 10:16a John
110 #include "freespace.h"
115 #include "alphacolors.h"
118 #define MAX_COMMANDS 300
120 static int Num_debug_commands = 0;
121 static debug_command *Debug_command[MAX_COMMANDS];
124 debug_command::debug_command(char *_name, char *_help, void (*_func)() )
128 if ( Num_debug_commands >= MAX_COMMANDS ) {
129 Int3(); // Too many debug console commands!! Increase MAX_COMMANDS!!
133 for (i=0; i<Num_debug_commands; i++ ) {
134 int ret = stricmp( Debug_command[i]->name, _name );
137 Int3(); // This debug console command already exists!!!!
139 } else if ( ret > 0 ) {
140 break; // Insert it here
142 } else if ( ret < 0 ) {
147 if ( i < Num_debug_commands ) {
148 // Insert it at element i
150 for (j=Num_debug_commands; j>i; j-- ) {
151 Debug_command[j] = Debug_command[j-1];
153 Debug_command[i] = this;
154 Num_debug_commands++;
156 Debug_command[Num_debug_commands] = this;
157 Num_debug_commands++;
165 // some global variables
166 int Dc_command; // If this is set, then process the command
167 int Dc_help; // If this is set, then print out the help text in the form, "usage: ... \nLong description\n" );
168 int Dc_status; // If this is set, then print out the current status of the command.
169 char *Dc_arg; // The (lowercased) string value of the argument retrieved from dc_arg
170 char *Dc_arg_org; // Dc_arg before it got converted to lowercase
171 uint Dc_arg_type; // The type of dc_arg.
172 char *Dc_command_line; // The rest of the command line, from the end of the last processed arg on.
173 int Dc_arg_int; // If Dc_arg_type & ARG_INT is set, then this is the value
174 float Dc_arg_float; // If Dc_arg_type & ARG_FLOAT is set, then this is the value
176 int scroll_times = 0; // incremented each time display scrolls
178 int debug_inited = 0;
183 int debug_x=0, debug_y=0;
184 char debug_text[DROWS][DCOLS];
187 static char command_line[1024];
188 static int command_line_pos = 0;
189 #define DEBUG_HISTORY 16
190 static char oldcommand_line[DEBUG_HISTORY][1024];
191 int last_oldcommand=-1;
192 int command_scroll = 0;
194 ///=========================== SCANNER =======================
196 LETTER, QUOTE, SPECIAL, EOF_CODE, DIGIT,
200 NO_TOKEN, IDENTIFIER, NUMBER, STRING,
204 #define MAX_TOKEN_STRING_LENGTH 128
207 TOKEN_CODE scanner_token;
209 char scanner_token_string[MAX_TOKEN_STRING_LENGTH];
210 char scanner_word_string[MAX_TOKEN_STRING_LENGTH];
211 char * scanner_bufferp = "";
212 char * scanner_tokenp = scanner_token_string;
214 CHAR_CODE scanner_char_table[256];
216 #define scanner_char_code(x) scanner_char_table[x]
218 void scanner_get_char()
220 if ( *scanner_bufferp == '\0' ) {
224 scanner_ch = *scanner_bufferp++;
230 for (ch=0; ch<256; ++ch) scanner_char_table[ch] = SPECIAL;
231 for (ch='0'; ch<='9'; ++ch) scanner_char_table[ch] = DIGIT;
232 for (ch='A'; ch<='Z'; ++ch) scanner_char_table[ch] = LETTER;
233 for (ch='a'; ch<='z'; ++ch) scanner_char_table[ch] = LETTER;
235 scanner_char_table['.'] = DIGIT;
236 scanner_char_table['-'] = DIGIT;
237 scanner_char_table['+'] = DIGIT;
239 scanner_char_table['_'] = LETTER;
240 scanner_char_table[34] = QUOTE;
241 scanner_char_table[0] = EOF_CODE;
244 scanner_char_table[':'] = LETTER;
245 scanner_char_table['\\'] = LETTER;
251 void scanner_skip_blanks()
253 while( (scanner_ch ==' ') || (scanner_ch =='\t') )
258 void scanner_downshift_word()
260 int offset = 'a' - 'A';
263 strcpy( scanner_word_string, scanner_token_string );
265 tp = scanner_word_string;
267 *tp = (char)((*tp>='A') && (*tp <='Z') ? *tp + offset : *tp) ;
269 } while (*tp != '\0' );
273 void scanner_get_word()
275 while( (scanner_char_code(scanner_ch)==LETTER) || (scanner_char_code(scanner_ch)==DIGIT) ) {
276 *scanner_tokenp++ = scanner_ch;
279 *scanner_tokenp = '\0';
281 scanner_token = IDENTIFIER;
285 void scanner_get_string()
287 *scanner_tokenp++ = 34;
290 while(scanner_ch != 34 ) {
291 *scanner_tokenp++ = scanner_ch;
295 *scanner_tokenp++ = 34;
296 *scanner_tokenp = '\0';
297 scanner_token = STRING;
302 void scanner_get_token()
304 scanner_skip_blanks();
305 scanner_tokenp = scanner_token_string;
309 switch( scanner_char_code(scanner_ch) ) {
310 case QUOTE: scanner_get_string(); break;
311 case EOF_CODE: scanner_token = NO_TOKEN; break;
313 case LETTER: scanner_get_word(); break;
315 *scanner_tokenp++ = scanner_ch;
316 *scanner_tokenp = '\0';
318 scanner_token = IDENTIFIER;
322 scanner_downshift_word();
325 void scanner_start_command( char * s )
335 void dc_get_arg(uint type)
339 Dc_command_line = scanner_bufferp;
340 Dc_arg_org = scanner_token_string;
341 Dc_arg = scanner_word_string;
344 dc_printf( "next arg is '%s', was originally '%s'\n", Dc_arg, Dc_arg_org );
345 dc_printf( "Rest of the command line is '%s'\n", Dc_command_line );
348 if ( scanner_token == NO_TOKEN ) {
349 Dc_arg_type = ARG_NONE;
350 } else if ( scanner_token == IDENTIFIER ) {
351 Dc_arg_type = ARG_STRING;
352 } else if ( scanner_token == STRING ) {
353 Dc_arg_type = ARG_QUOTE;
355 Dc_arg_type = ARG_STRING;
358 if ( Dc_arg_type & ARG_STRING ) {
359 int i, num_digits, len;
361 len = strlen(Dc_arg);
364 for (i=0; i<len; i++)
365 if ( scanner_char_table[Dc_arg[i]] == DIGIT ) num_digits++;
367 if ( num_digits==len ) {
368 Dc_arg_type |= ARG_FLOAT;
369 Dc_arg_float = (float)atof(Dc_arg);
370 if ( !strchr( Dc_arg, '.' )) {
371 Dc_arg_type |= ARG_INT;
372 Dc_arg_int = atoi(Dc_arg);
375 if ( (Dc_arg[0] == '0') && (Dc_arg[1] == 'x') ) {
378 n = strtol(Dc_arg,&p,0);
380 Dc_arg_type |= ARG_INT|ARG_HEX;
387 if ( Dc_arg_type & ARG_FLOAT )
388 dc_printf( "Found float number! %f\n", Dc_arg_float );
390 if ( Dc_arg_type & ARG_INT )
391 dc_printf( "Found int number! %d\n", Dc_arg_int );
393 if ( Dc_arg_type & ARG_HEX )
394 dc_printf( "Found hex number! 0x%x\n", Dc_arg_int );
397 if ( !stricmp( Dc_arg, "on" ))
398 Dc_arg_type |= ARG_TRUE;
399 if ( !stricmp( Dc_arg, "true" ))
400 Dc_arg_type |= ARG_TRUE;
401 if ( !stricmp( Dc_arg, "off" ))
402 Dc_arg_type |= ARG_FALSE;
403 if ( !stricmp( Dc_arg, "false" ))
404 Dc_arg_type |= ARG_FALSE;
406 if ( !stricmp( Dc_arg, "+" ))
407 Dc_arg_type |= ARG_PLUS;
409 if ( !stricmp( Dc_arg, "-" ))
410 Dc_arg_type |= ARG_MINUS;
412 if ( !stricmp( Dc_arg, "," ))
413 Dc_arg_type |= ARG_COMMA;
416 if ( Dc_arg_type & ARG_INT) {
418 Dc_arg_type |= ARG_TRUE;
420 Dc_arg_type |= ARG_FALSE;
423 if ( !(Dc_arg_type&type) ) {
424 if ( (Dc_arg_type & ARG_NONE) && !(type & ARG_NONE))
425 dc_printf( "Error: Not enough parameters.\n" );
427 dc_printf( "Error: '%s' invalid type\n", Dc_arg );
428 longjmp(dc_bad_arg,1);
435 void debug_do_command(char * command)
441 if ( strlen(command) < 1 ) return;
444 Dc_command_line = command;
445 scanner_start_command(command);
447 if (setjmp(dc_bad_arg) ) {
451 dc_get_arg( ARG_ANY );
453 if ( !strcmp( Dc_arg, "debug" ) ) {
455 dc_printf( "Command line: '%s'\n", Dc_command_line );
456 dc_get_arg( ARG_ANY );
459 if ( !strcmp( Dc_arg, "?" ) ) {
461 dc_get_arg( ARG_ANY );
463 if ( Dc_arg_type&ARG_NONE ) {
469 if ( !strcmp( Dc_arg, "help" ) || !strcmp( Dc_arg, "man" ) ) {
471 dc_get_arg( ARG_ANY );
472 if ( Dc_arg_type&ARG_NONE ) {
478 if ( strstr( Dc_command_line, "?" ) ) {
482 if ( !(Dc_arg_type&ARG_STRING) ) {
483 dc_printf( "Invalid keyword '%s'\n", Dc_arg );
489 dc_printf( "Searching for command '%s'\n", Dc_arg );
492 for (i=0; i<Num_debug_commands; i++ ) {
493 if ( !stricmp( Debug_command[i]->name, Dc_arg )) {
497 dc_printf( "Calling function '%s'\n", Dc_arg );
501 } else if (mode==1) {
503 dc_printf( "Checking status for '%s'\n", Dc_arg );
509 dc_printf( "Doing help for '%s'\n", Dc_arg );
515 (*Debug_command[i]->func)();
519 if (!(Dc_arg_type&ARG_NONE)) {
520 dc_printf( "Ignoring the unused command line tail '%s %s'\n", Dc_arg_org, Dc_command_line );
528 dc_printf( "Unknown command '%s'\n", Dc_arg );
537 gr_set_color_fast( &Color_bright );
538 gr_string( 0x8000, 3, "Debug Console" );
540 gr_set_color_fast( &Color_normal );
542 for (i=0; i<DROWS; i++ ) {
543 gr_string( 0, i*16+16, debug_text[i] );
546 int t = timer_get_fixed_seconds() / (F1_0/3);
551 c = debug_text[debug_y][command_line_pos+1];
552 debug_text[debug_y][command_line_pos+1] = 0;
554 gr_get_string_size( &w, &h, debug_text[debug_y] );
556 //gr_string( w, debug_y*16, "_" );
557 gr_rect(w+1,debug_y*16+1+16,2,14);
559 debug_text[debug_y][command_line_pos+1] = c;
566 void debug_output( char c )
569 int next_tab = ((debug_x/28)+1)*28;
571 if ( next_tab >= DCOLS-1 ) {
575 if ( debug_y >= DROWS ) {
577 for (i=1; i<DROWS; i++ )
578 strcpy( debug_text[i-1], debug_text[i] );
581 debug_text[debug_y][debug_x] = 0;
583 debug_text[debug_y][debug_x] = 0;
587 for ( ; debug_x < next_tab; )
588 debug_text[debug_y][debug_x++] = ' ';
589 debug_text[debug_y][debug_x] = 0;
593 if ( (c == '\n') || (debug_x >= DCOLS-1) ) {
597 if ( debug_y >= DROWS ) {
599 for (i=1; i<DROWS; i++ )
600 strcpy( debug_text[i-1], debug_text[i] );
603 debug_text[debug_y][debug_x] = 0;
605 debug_text[debug_y][debug_x] = 0;
606 if ( c == '\n' ) return;
609 debug_text[debug_y][debug_x++] = c;
610 debug_text[debug_y][debug_x] = 0;
613 void dc_printf(char *format, ...)
618 va_start(args, format);
619 vsprintf(tmp, format, args);
634 if ( debug_inited ) return;
641 for (i=0; i<DROWS; i++ ) {
642 debug_text[i][0] = 0;
645 dc_printf("Debug console started.\n" );
649 void debug_console( void (*_func)() )
655 while( key_inkey() ){
659 if ( !debug_inited ) debug_init();
671 case KEY_SHIFTED+KEY_ENTER:
676 if ( command_line_pos > 0 ) {
677 command_line[--command_line_pos] = 0;
682 if ( last_oldcommand > -1 ) {
683 strcpy( command_line, oldcommand_line[last_oldcommand] );
684 command_line_pos = strlen(command_line);
685 command_line[command_line_pos] = 0;
691 if (command_scroll<0)
692 command_scroll = last_oldcommand;
694 if ( command_scroll > -1 ) {
695 strcpy( command_line, oldcommand_line[command_scroll] );
696 command_line_pos = strlen(command_line);
697 command_line[command_line_pos] = 0;
703 if (command_scroll>last_oldcommand)
705 if (command_scroll>last_oldcommand)
707 if ( command_scroll > -1 ) {
708 strcpy( command_line, oldcommand_line[command_scroll] );
709 command_line_pos = strlen(command_line);
710 command_line[command_line_pos] = 0;
715 debug_output( '\n' );
718 debug_do_command(command_line);
721 for (i=0; i<=last_oldcommand; i++ ) {
722 if (!stricmp( oldcommand_line[i], command_line )) {
727 if ( last_oldcommand < DEBUG_HISTORY-1 ) {
729 strcpy( oldcommand_line[last_oldcommand], command_line);
732 for (i=0; i<last_oldcommand; i++ ) {
733 strcpy( oldcommand_line[i], oldcommand_line[i+1] );
735 strcpy( oldcommand_line[last_oldcommand], command_line);
739 // for (i=0; i<=last_oldcommand; i++ ) {
740 // dc_printf( "OC %d. %s\n", i, oldcommand_line[i] );
743 debug_output( '\n' );
744 command_line_pos = 0;
745 command_line[command_line_pos] = 0;
752 ubyte c = (ubyte)key_to_ascii(k);
754 command_line[command_line_pos++] = c;
755 command_line[command_line_pos] = 0;
760 strcpy( debug_text[debug_y], ">" );
761 strcat( debug_text[debug_y], command_line );
769 while( key_inkey() ){
778 dc_printf( "Available functions:\n\n" );
781 for (i=0; i<Num_debug_commands; i++ ) {
782 dc_printf( " %s - %s\n", Debug_command[i]->name, Debug_command[i]->help );
783 //mprintf(( "Scroll times %d\n", scroll_times - s ));
784 if ( scroll_times - s > DROWS - 3 ) {
786 dc_printf( " Press a key...B for back\n" );
800 dc_printf( "Typing '? function_name' will give the current status.\n" );
801 dc_printf( "Typing 'function_name ?' will give help on the function.\n" );
802 dc_printf( "Typing ? or help will give you help.\n");
803 dc_printf( "F3 selects last command line.\n" );