FS2_Open
Open source remastering of the Freespace 2 engine
consolecmds.cpp
Go to the documentation of this file.
1 /*
2  * z64555's debug console
3  * Created for the FreeSpace Source Code project
4  *
5  * Portions of this source code are based on works by Volition, Inc. circa
6  * 1999. You may not sell or otherwise commercially exploit the source or things you
7  * created based on the source
8  */
9 
20 #include "debugconsole/console.h"
22 #include "io/key.h"
23 
24 #include <algorithm>
25 
26 // ========================= GLOBALS =========================
29 
30 // ========================= LOCALS ==========================
31 // dcf_shell commands
32 void dc_shell_font( void );
33 void dc_shell_resize( void );
34 void dc_shell_resize_buf( void );
35 
36 // =================== class debug_command ===================
38 : name(""), help(""), func(NULL) {
39 };
40 
41 debug_command::debug_command(const char *_name, const char *_help, void(*_func)())
42  : name(_name), help(_help), func(_func) {
43  int i = 0;
44  int ret = 0;
45 
47  Error(LOCATION, "Too many debug console commands! Please inform a coder to increase DC_MAX_COMMANDS.");
48  return;
49  }
50 
51  // Start the insertion sort by finding where to stick the debug command
52  for (; i < dc_commands_size; ++i) {
53  ret = stricmp(dc_commands[i]->name, _name);
54 
55  if (ret == 0) {
56  Error(LOCATION, "Debug Command %s already exists! Please inform a coder immediately.", _name);
57  return;
58  } else if (ret > 0) {
59  // Insert the command here
60  break;
61  } // Else, do nothing
62  }
63 
64  // Then, do the insertion
65  if (i < dc_commands_size) {
66  for (int j = dc_commands_size; j > i; --j) {
67  dc_commands[j] = dc_commands[j - 1];
68  }
69  dc_commands[i] = this;
70  dc_commands_size++;
71  } else {
72  dc_commands[dc_commands_size] = this;
73  dc_commands_size++;
74  }
75 }
76 
77 // ============================== IMPLEMENTATIONS =============================
78 
79 DCF(debug, "Runs a command in debug mode.")
80 {
81  int i;
82  SCP_string command = "";
83 
84  Dc_debug_on = true;
85 
86  dc_stuff_string_white(command);
87 
88  if (command == "") {
89  dc_printf("<debug> No command given\n");
90  return;
91  } // Else, command is present.
92 
93  for (i = 0; i < dc_commands_size; ++i) {
94  if (stricmp(dc_commands[i]->name, command.c_str()) == 0) {
95  break;
96  } // Else, continue
97  }
98 
99  if (i == dc_commands_size) {
100  dc_printf("<debug> Command not found: '%s'\n", command.c_str());
101  return;
102  } // Else, we found our command
103 
104  dc_printf("<debug> Executing command: '%s'\n", command.c_str());
105  // try {
106  dc_commands[i]->func();
107  // } catch {
108  // }
109 
110  Dc_debug_on = false;
111 }
112 
113 DCF(help, "Displays the help list." )
114 {
115  extern uint DROWS;
116 
117  int i;
118  SCP_string command = "";
119 
121  if ((command == "help") || (command == "man"))
122  {
123  // Moron filter :D
124  dc_printf("GTVA Command: Sorry pilot. You're on your own.\n");
125  return;
126 
127  } else if (command != "") {
128  for (i = 0; i < dc_commands_size; ++i) {
129  if (stricmp(dc_commands[i]->name, command.c_str()) == 0) {
130  break;
131  } // Else, continue
132  }
133 
134  if (i == dc_commands_size) {
135  dc_printf("Command not found: '%s'\n", command.c_str());
136  return;
137  } // Else, we found our command
138 
139  dc_printf("%s\n", dc_commands[i]->help);
140  return;
141  } // Else, command line is empty, print out the help list
142 
143  dc_printf("FreeSpace Open Debug Console\n");
144  dc_printf(" These commands are defined internally.\n");
145  dc_printf(" Typing 'help function_name' will give the short help on the function.\n");
146  dc_printf(" Some functions may have detailed help, try passing \"help\" or \"--help\" to them.");
147  dc_printf(" F3 selects last command line. Up and Down arrow keys scroll through the command history\n");
148  dc_printf("\n");
149 
150  dc_printf(" Available commands:\n");
151  for (i = 0; i < dc_commands_size; ++i) {
152  if (((lastline % DROWS) == 0) && (lastline != 0)) {
153  dc_pause_output();
154  }
155 
156  dc_printf(" %s - %s\n", dc_commands[i]->name, dc_commands[i]->help);
157  }
158 }
159 
160 debug_command dc_man("man", "Also displays the help list", dcf_help);
int i
Definition: multi_pxo.cpp:466
int dc_commands_size
Definition: consolecmds.cpp:28
DCF(debug,"Runs a command in debug mode.")
Definition: consolecmds.cpp:79
GLenum func
Definition: Glext.h:5605
void dc_shell_resize(void)
std::basic_string< char, std::char_traits< char >, std::allocator< char > > SCP_string
Definition: vmallocator.h:21
uint DROWS
Definition: console.cpp:61
unsigned int uint
Definition: pstypes.h:64
debug_command dc_man("man","Also displays the help list", dcf_help)
Class to aggregate a debug command with its name (as shown in the console) and short help...
Definition: console.h:221
uint lastline
Definition: console.cpp:32
void(* func)()
Pointer to the function that to run when this command is evoked.
Definition: console.h:225
void _cdecl void void _cdecl Error(const char *filename, int line, SCP_FORMAT_STRING const char *format,...) SCP_FORMAT_STRING_ARGS(3
GLuint const GLchar * name
Definition: Glext.h:5608
void dc_stuff_string_white(char *out_str, size_t maxlen)
Stuffs a whitespace delimited string to out_str from the command line, stopping at the end of the com...
void dc_shell_font(void)
void help()
debug_command * dc_commands[DC_MAX_COMMANDS]
Definition: consolecmds.cpp:27
An overhauled/updated debug console to allow monitoring, testing, and general debugging of new featur...
#define LOCATION
Definition: pstypes.h:245
bool Dc_debug_on
Flag used to print console and command debugging strings.
Definition: console.cpp:24
void dc_printf(const char *format,...)
Prints the given char string to the debug console.
Definition: console.cpp:358
bool dc_pause_output(void)
Pauses the output of a command and allows user to scroll through the output history.
Definition: console.cpp:306
#define DC_MAX_COMMANDS
Definition: console.h:25
Parsing functions for the command line. Previously known as the command line scanner.
#define stricmp(s1, s2)
Definition: config.h:271
void dc_shell_resize_buf(void)
bool dc_maybe_stuff_string_white(char *str, size_t len)
Tries to stuff a whitespace delimited string to out_str from the command line, stopping at the end of...