OpenTTD Source: src/console.cpp Source File

OpenTTD Source 12.2

console.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD 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, version 2.
4 * OpenTTD 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.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #include "stdafx.h"
11 #include "console_internal.h"
12 #include "network/network.h"
13 #include "network/network_func.h"
14 #include "network/network_admin.h"
15 #include "debug.h"
16 #include "console_func.h"
17 #include "settings_type.h"
18
19 #include <stdarg.h>
20
21 #include "safeguards.h"
22
23 static const uint ICON_TOKEN_COUNT = 20;
24 static const uint ICON_MAX_RECURSE = 10;
25
26 /* console parser */
27 /* static */ IConsole::CommandList &IConsole::Commands()
28 {
29 static IConsole::CommandList cmds;
30 return cmds;
31 }
32
33 /* static */ IConsole::AliasList &IConsole::Aliases()
34 {
35 static IConsole::AliasList aliases;
36 return aliases;
37 }
38
39 FILE *_iconsole_output_file;
40
41 void IConsoleInit()
42 {
43 _iconsole_output_file = nullptr;
44 _redirect_console_to_client = INVALID_CLIENT_ID;
45 _redirect_console_to_admin = INVALID_ADMIN_ID;
46
47 IConsoleGUIInit();
48
49 IConsoleStdLibRegister();
50 }
51
52 static void IConsoleWriteToLogFile(const char *string)
53 {
54 if (_iconsole_output_file != nullptr) {
55 /* if there is an console output file ... also print it there */
56 const char *header = GetLogPrefix();
57 if ((strlen(header) != 0 && fwrite(header, strlen(header), 1, _iconsole_output_file) != 1) ||
58 fwrite(string, strlen(string), 1, _iconsole_output_file) != 1 ||
59 fwrite("\n", 1, 1, _iconsole_output_file) != 1) {
60 fclose(_iconsole_output_file);
61 _iconsole_output_file = nullptr;
62 IConsolePrint(CC_ERROR, "Cannot write to console log file; closing the log file.");
63 }
64 }
65 }
66
67 bool CloseConsoleLogIfActive()
68 {
69 if (_iconsole_output_file != nullptr) {
70 IConsolePrint(CC_INFO, "Console log file closed.");
71 fclose(_iconsole_output_file);
72 _iconsole_output_file = nullptr;
73 return true;
74 }
75
76 return false;
77 }
78
79 void IConsoleFree()
80 {
81 IConsoleGUIFree();
82 CloseConsoleLogIfActive();
83 }
84
94 void IConsolePrint(TextColour colour_code, const std::string &string)
95 {
96 assert(IsValidConsoleColour(colour_code));
97
98 if (_redirect_console_to_client != INVALID_CLIENT_ID) {
99 /* Redirect the string to the client */
100 NetworkServerSendRcon(_redirect_console_to_client, colour_code, string);
101 return;
102 }
103
104 if (_redirect_console_to_admin != INVALID_ADMIN_ID) {
105 NetworkServerSendAdminRcon(_redirect_console_to_admin, colour_code, string);
106 return;
107 }
108
109 /* Create a copy of the string, strip if of colours and invalid
110 * characters and (when applicable) assign it to the console buffer */
111 char *str = stredup(string.c_str());
112 str_strip_colours(str);
113 StrMakeValidInPlace(str);
114
115 if (_network_dedicated) {
116 NetworkAdminConsole("console", str);
117 fprintf(stdout, "%s%s\n", GetLogPrefix(), str);
118 fflush(stdout);
119 IConsoleWriteToLogFile(str);
120 free(str); // free duplicated string since it's not used anymore
121 return;
122 }
123
124 IConsoleWriteToLogFile(str);
125 IConsoleGUIPrint(colour_code, str);
126 }
127
135 bool GetArgumentInteger(uint32 *value, const char *arg)
136 {
137 char *endptr;
138
139 if (strcmp(arg, "on") == 0 || strcmp(arg, "true") == 0) {
140 *value = 1;
141 return true;
142 }
143 if (strcmp(arg, "off") == 0 || strcmp(arg, "false") == 0) {
144 *value = 0;
145 return true;
146 }
147
148 *value = strtoul(arg, &endptr, 0);
149 return arg != endptr;
150 }
151
157 static std::string RemoveUnderscores(std::string name)
158 {
159 name.erase(std::remove(name.begin(), name.end(), '_'), name.end());
160 return name;
161 }
162
168 /* static */ void IConsole::CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook)
169 {
170 IConsole::Commands().try_emplace(RemoveUnderscores(name), name, proc, hook);
171 }
172
178 /* static */ IConsoleCmd *IConsole::CmdGet(const std::string &name)
179 {
180 auto item = IConsole::Commands().find(RemoveUnderscores(name));
181 if (item != IConsole::Commands().end()) return &item->second;
182 return nullptr;
183 }
184
190 /* static */ void IConsole::AliasRegister(const std::string &name, const std::string &cmd)
191 {
192 auto result = IConsole::Aliases().try_emplace(RemoveUnderscores(name), name, cmd);
193 if (!result.second) IConsolePrint(CC_ERROR, "An alias with the name '{}' already exists.", name);
194 }
195
201 /* static */ IConsoleAlias *IConsole::AliasGet(const std::string &name)
202 {
203 auto item = IConsole::Aliases().find(RemoveUnderscores(name));
204 if (item != IConsole::Aliases().end()) return &item->second;
205 return nullptr;
206 }
207
215 static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char *tokens[ICON_TOKEN_COUNT], const uint recurse_count)
216 {
217 char alias_buffer[ICON_MAX_STREAMSIZE] = { '\0' };
218 char *alias_stream = alias_buffer;
219
220 Debug(console, 6, "Requested command is an alias; parsing...");
221
222 if (recurse_count > ICON_MAX_RECURSE) {
223 IConsolePrint(CC_ERROR, "Too many alias expansions, recursion limit reached.");
224 return;
225 }
226
227 for (const char *cmdptr = alias->cmdline.c_str(); *cmdptr != '\0'; cmdptr++) {
228 switch (*cmdptr) {
229 case '\'': // ' will double for ""
230 alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
231 break;
232
233 case ';': // Cmd separator; execute previous and start new command
234 IConsoleCmdExec(alias_buffer, recurse_count);
235
236 alias_stream = alias_buffer;
237 *alias_stream = '\0'; // Make sure the new command is terminated.
238
239 cmdptr++;
240 break;
241
242 case '%': // Some or all parameters
243 cmdptr++;
244 switch (*cmdptr) {
245 case '+': { // All parameters separated: "[param 1]" "[param 2]"
246 for (uint i = 0; i != tokencount; i++) {
247 if (i != 0) alias_stream = strecpy(alias_stream, " ", lastof(alias_buffer));
248 alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
249 alias_stream = strecpy(alias_stream, tokens[i], lastof(alias_buffer));
250 alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
251 }
252 break;
253 }
254
255 case '!': { // Merge the parameters to one: "[param 1] [param 2] [param 3...]"
256 alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
257 for (uint i = 0; i != tokencount; i++) {
258 if (i != 0) alias_stream = strecpy(alias_stream, " ", lastof(alias_buffer));
259 alias_stream = strecpy(alias_stream, tokens[i], lastof(alias_buffer));
260 }
261 alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
262 break;
263 }
264
265 default: { // One specific parameter: %A = [param 1] %B = [param 2] ...
266 int param = *cmdptr - 'A';
267
268 if (param < 0 || param >= tokencount) {
269 IConsolePrint(CC_ERROR, "Too many or wrong amount of parameters passed to alias.");
270 IConsolePrint(CC_HELP, "Usage of alias '{}': '{}'.", alias->name, alias->cmdline);
271 return;
272 }
273
274 alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
275 alias_stream = strecpy(alias_stream, tokens[param], lastof(alias_buffer));
276 alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
277 break;
278 }
279 }
280 break;
281
282 default:
283 *alias_stream++ = *cmdptr;
284 *alias_stream = '\0';
285 break;
286 }
287
288 if (alias_stream >= lastof(alias_buffer) - 1) {
289 IConsolePrint(CC_ERROR, "Requested alias execution would overflow execution buffer.");
290 return;
291 }
292 }
293
294 IConsoleCmdExec(alias_buffer, recurse_count);
295 }
296
302 void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
303 {
304 const char *cmdptr;
305 char *tokens[ICON_TOKEN_COUNT], tokenstream[ICON_MAX_STREAMSIZE];
306 uint t_index, tstream_i;
307
308 bool longtoken = false;
309 bool foundtoken = false;
310
311 if (cmdstr[0] == '#') return; // comments
312
313 for (cmdptr = cmdstr; *cmdptr != '\0'; cmdptr++) {
314 if (!IsValidChar(*cmdptr, CS_ALPHANUMERAL)) {
315 IConsolePrint(CC_ERROR, "Command '{}' contains malformed characters.", cmdstr);
316 return;
317 }
318 }
319
320 Debug(console, 4, "Executing cmdline: '{}'", cmdstr);
321
322 memset(&tokens, 0, sizeof(tokens));
323 memset(&tokenstream, 0, sizeof(tokenstream));
324
325 /* 1. Split up commandline into tokens, separated by spaces, commands
326 * enclosed in "" are taken as one token. We can only go as far as the amount
327 * of characters in our stream or the max amount of tokens we can handle */
328 for (cmdptr = cmdstr, t_index = 0, tstream_i = 0; *cmdptr != '\0'; cmdptr++) {
329 if (tstream_i >= lengthof(tokenstream)) {
330 IConsolePrint(CC_ERROR, "Command line too long.");
331 return;
332 }
333
334 switch (*cmdptr) {
335 case ' ': // Token separator
336 if (!foundtoken) break;
337
338 if (longtoken) {
339 tokenstream[tstream_i] = *cmdptr;
340 } else {
341 tokenstream[tstream_i] = '\0';
342 foundtoken = false;
343 }
344
345 tstream_i++;
346 break;
347 case '"': // Tokens enclosed in "" are one token
348 longtoken = !longtoken;
349 if (!foundtoken) {
350 if (t_index >= lengthof(tokens)) {
351 IConsolePrint(CC_ERROR, "Command line too long.");
352 return;
353 }
354 tokens[t_index++] = &tokenstream[tstream_i];
355 foundtoken = true;
356 }
357 break;
358 case '\\': // Escape character for ""
359 if (cmdptr[1] == '"' && tstream_i + 1 < lengthof(tokenstream)) {
360 tokenstream[tstream_i++] = *++cmdptr;
361 break;
362 }
363 FALLTHROUGH;
364 default: // Normal character
365 tokenstream[tstream_i++] = *cmdptr;
366
367 if (!foundtoken) {
368 if (t_index >= lengthof(tokens)) {
369 IConsolePrint(CC_ERROR, "Command line too long.");
370 return;
371 }
372 tokens[t_index++] = &tokenstream[tstream_i - 1];
373 foundtoken = true;
374 }
375 break;
376 }
377 }
378
379 for (uint i = 0; i < lengthof(tokens) && tokens[i] != nullptr; i++) {
380 Debug(console, 8, "Token {} is: '{}'", i, tokens[i]);
381 }
382
383 if (StrEmpty(tokens[0])) return; // don't execute empty commands
384 /* 2. Determine type of command (cmd or alias) and execute
385 * First try commands, then aliases. Execute
386 * the found action taking into account its hooking code
387 */
388 IConsoleCmd *cmd = IConsole::CmdGet(tokens[0]);
389 if (cmd != nullptr) {
390 ConsoleHookResult chr = (cmd->hook == nullptr ? CHR_ALLOW : cmd->hook(true));
391 switch (chr) {
392 case CHR_ALLOW:
393 if (!cmd->proc(t_index, tokens)) { // index started with 0
394 cmd->proc(0, nullptr); // if command failed, give help
395 }
396 return;
397
398 case CHR_DISALLOW: return;
399 case CHR_HIDE: break;
400 }
401 }
402
403 t_index--;
404 IConsoleAlias *alias = IConsole::AliasGet(tokens[0]);
405 if (alias != nullptr) {
406 IConsoleAliasExec(alias, t_index, &tokens[1], recurse_count + 1);
407 return;
408 }
409
410 IConsolePrint(CC_ERROR, "Command '{}' not found.", tokens[0]);
411 }
CC_INFO
static const TextColour CC_INFO
Colour for information lines.
Definition: console_type.h:27
IConsoleCmd::proc
IConsoleCmdProc * proc
process executed when command is typed
Definition: console_internal.h:40
INVALID_CLIENT_ID
@ INVALID_CLIENT_ID
Client is not part of anything.
Definition: network_type.h:48
IConsole::AliasGet
static IConsoleAlias * AliasGet(const std::string &name)
Find the alias pointed to by its string.
Definition: console.cpp:201
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:250
CompanyProperties::name
std::string name
Name of the company if the user changed it.
Definition: company_base.h:58
_redirect_console_to_client
ClientID _redirect_console_to_client
If not invalid, redirect the console output to a client.
Definition: network.cpp:64
IConsoleGUIPrint
void IConsoleGUIPrint(TextColour colour_code, char *str)
Handle the printing of text entered into the console or redirected there by any other means.
Definition: console_gui.cpp:512
IConsoleAlias::cmdline
std::string cmdline
command(s) that is/are being aliased
Definition: console_internal.h:60
INVALID_ADMIN_ID
static const AdminIndex INVALID_ADMIN_ID
An invalid admin marker.
Definition: network_type.h:62
_redirect_console_to_admin
AdminIndex _redirect_console_to_admin
Redirection of the (remote) console to the admin.
Definition: network_admin.cpp:31
IConsoleCmd::hook
IConsoleHook * hook
any special trigger action that needs executing
Definition: console_internal.h:41
NetworkAdminConsole
void NetworkAdminConsole(const std::string_view origin, const std::string_view string)
Send console to the admin network (if they did opt in for the respective update).
Definition: network_admin.cpp:953
ICON_TOKEN_COUNT
static const uint ICON_TOKEN_COUNT
Maximum number of tokens in one command.
Definition: console.cpp:23
IConsole::CmdRegister
static void CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook=nullptr)
Register a new command to be used in the console.
Definition: console.cpp:168
ICON_MAX_STREAMSIZE
static const uint ICON_MAX_STREAMSIZE
maximum length of a totally expanded command
Definition: console_internal.h:17
GetLogPrefix
const char * GetLogPrefix()
Get the prefix for logs; if show_date_in_logs is enabled it returns the date, otherwise it returns no...
Definition: debug.cpp:240
CC_HELP
static const TextColour CC_HELP
Colour for help lines.
Definition: console_type.h:26
StrMakeValidInPlace
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings)
Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).
Definition: string.cpp:255
console_internal.h
IConsoleCmd
Definition: console_internal.h:36
str_strip_colours
void str_strip_colours(char *str)
Scans the string for colour codes and strips them.
Definition: string.cpp:389
CHR_ALLOW
@ CHR_ALLOW
Allow command execution.
Definition: console_internal.h:21
GetArgumentInteger
bool GetArgumentInteger(uint32 *value, const char *arg)
Change a string into its number representation.
Definition: console.cpp:135
safeguards.h
IsValidConsoleColour
bool IsValidConsoleColour(TextColour c)
Check whether the given TextColour is valid for console usage.
Definition: console_gui.cpp:524
IsValidChar
bool IsValidChar(WChar key, CharSetFilter afilter)
Only allow certain keys.
Definition: string.cpp:476
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:64
settings_type.h
ICON_MAX_RECURSE
static const uint ICON_MAX_RECURSE
Maximum number of recursion.
Definition: console.cpp:24
_network_dedicated
bool _network_dedicated
are we a dedicated server?
Definition: network.cpp:60
stdafx.h
CS_ALPHANUMERAL
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
Definition: string_type.h:27
IConsoleAlias::name
std::string name
name of the alias
Definition: console_internal.h:59
RemoveUnderscores
static std::string RemoveUnderscores(std::string name)
Creates a copy of a string with underscores removed from it.
Definition: console.cpp:157
IConsoleAliasExec
static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char *tokens[ICON_TOKEN_COUNT], const uint recurse_count)
An alias is just another name for a command, or for more commands Execute it as well.
Definition: console.cpp:215
IConsole::AliasRegister
static void AliasRegister(const std::string &name, const std::string &cmd)
Register a an alias for an already existing command in the console.
Definition: console.cpp:190
NetworkServerSendRcon
void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const std::string &string)
Send an rcon reply to the client.
Definition: network_server.cpp:1948
IConsoleAlias
–Aliases– Aliases are like shortcuts for complex functions, variable assignments, etc.
Definition: console_internal.h:56
CC_ERROR
static const TextColour CC_ERROR
Colour for error lines.
Definition: console_type.h:24
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:137
CHR_DISALLOW
@ CHR_DISALLOW
Disallow command execution.
Definition: console_internal.h:22
network.h
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
CHR_HIDE
@ CHR_HIDE
Hide the existence of the command.
Definition: console_internal.h:23
IConsoleCmdExec
void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
Execute a given command passed to us.
Definition: console.cpp:302
IConsoleCmdProc
bool IConsoleCmdProc(byte argc, char *argv[])
–Commands– Commands are commands, or functions.
Definition: console_internal.h:34
network_admin.h
console_func.h
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:394
network_func.h
ConsoleHookResult
ConsoleHookResult
Return values of console hooks (#IConsoleHook).
Definition: console_internal.h:20
IConsole::CmdGet
static IConsoleCmd * CmdGet(const std::string &name)
Find the command pointed to by its string.
Definition: console.cpp:178
debug.h
NetworkServerSendAdminRcon
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const std::string_view string)
Pass the rcon reply to the admin.
Definition: network_admin.cpp:943
IConsolePrint
void IConsolePrint(TextColour colour_code, const std::string &string)
Handle the printing of text entered into the console or redirected there by any other means.
Definition: console.cpp:94