OpenTTD Source: src/console.cpp Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11#include "core/string_builder.hpp"
12#include "core/string_consumer.hpp"
13#include "console_internal.h"
14#include "network/network.h"
15#include "network/network_func.h"
16#include "network/network_admin.h"
17#include "debug.h"
18#include "console_func.h"
19#include "settings_type.h"
20
21#include "safeguards.h"
22
23 static const uint ICON_MAX_RECURSE = 10;
24
25/* console parser */
26/* static */ IConsole::CommandList &IConsole::Commands()
27{
28 static IConsole::CommandList cmds;
29 return cmds;
30}
31
32/* static */ IConsole::AliasList &IConsole::Aliases()
33{
34 static IConsole::AliasList aliases;
35 return aliases;
36}
37
38std::optional<FileHandle> _iconsole_output_file;
39
40void IConsoleInit()
41{
42 _iconsole_output_file = std::nullopt;
43 _redirect_console_to_client = INVALID_CLIENT_ID;
44 _redirect_console_to_admin = AdminID::Invalid();
45
46 IConsoleGUIInit();
47
48 IConsoleStdLibRegister();
49}
50
51static void IConsoleWriteToLogFile(const std::string &string)
52{
53 if (_iconsole_output_file.has_value()) {
54 /* if there is an console output file ... also print it there */
55 try {
56 fmt::print(*_iconsole_output_file, "{}{}\n", GetLogPrefix(), string);
57 } catch (const std::system_error &) {
58 _iconsole_output_file.reset();
59 IConsolePrint(CC_ERROR, "Cannot write to console log file; closing the log file.");
60 }
61 }
62}
63
64bool CloseConsoleLogIfActive()
65{
66 if (_iconsole_output_file.has_value()) {
67 IConsolePrint(CC_INFO, "Console log file closed.");
68 _iconsole_output_file.reset();
69 return true;
70 }
71
72 return false;
73}
74
75void IConsoleFree()
76{
77 IConsoleGUIFree();
78 CloseConsoleLogIfActive();
79}
80
90 void IConsolePrint(TextColour colour_code, const std::string &string)
91{
92 assert(IsValidConsoleColour(colour_code));
93
94 if (_redirect_console_to_client != INVALID_CLIENT_ID) {
95 /* Redirect the string to the client */
96 NetworkServerSendRcon(_redirect_console_to_client, colour_code, string);
97 return;
98 }
99
100 if (_redirect_console_to_admin != AdminID::Invalid()) {
101 NetworkServerSendAdminRcon(_redirect_console_to_admin, colour_code, string);
102 return;
103 }
104
105 /* Create a copy of the string, strip it of colours and invalid
106 * characters and (when applicable) assign it to the console buffer */
107 std::string str = StrMakeValid(string, {});
108
109 if (_network_dedicated) {
110 NetworkAdminConsole("console", str);
111 fmt::print("{}{}\n", GetLogPrefix(), str);
112 fflush(stdout);
113 IConsoleWriteToLogFile(str);
114 return;
115 }
116
117 IConsoleWriteToLogFile(str);
118 IConsoleGUIPrint(colour_code, str);
119}
120
126 static std::string RemoveUnderscores(std::string name)
127{
128 name.erase(std::remove(name.begin(), name.end(), '_'), name.end());
129 return name;
130}
131
137/* static */ void IConsole::CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook)
138{
139 IConsole::Commands().try_emplace(RemoveUnderscores(name), name, proc, hook);
140}
141
147/* static */ IConsoleCmd *IConsole::CmdGet(const std::string &name)
148{
149 auto item = IConsole::Commands().find(RemoveUnderscores(name));
150 if (item != IConsole::Commands().end()) return &item->second;
151 return nullptr;
152}
153
159/* static */ void IConsole::AliasRegister(const std::string &name, std::string_view cmd)
160{
161 auto result = IConsole::Aliases().try_emplace(RemoveUnderscores(name), name, cmd);
162 if (!result.second) IConsolePrint(CC_ERROR, "An alias with the name '{}' already exists.", name);
163}
164
170/* static */ IConsoleAlias *IConsole::AliasGet(const std::string &name)
171{
172 auto item = IConsole::Aliases().find(RemoveUnderscores(name));
173 if (item != IConsole::Aliases().end()) return &item->second;
174 return nullptr;
175}
176
184 static void IConsoleAliasExec(const IConsoleAlias *alias, std::span<std::string> tokens, uint recurse_count)
185{
186 Debug(console, 6, "Requested command is an alias; parsing...");
187
188 if (recurse_count > ICON_MAX_RECURSE) {
189 IConsolePrint(CC_ERROR, "Too many alias expansions, recursion limit reached.");
190 return;
191 }
192
193 std::string buffer;
194 StringBuilder builder{buffer};
195
196 StringConsumer consumer{alias->cmdline};
197 while (consumer.AnyBytesLeft()) {
198 auto c = consumer.TryReadUtf8();
199 if (!c.has_value()) {
200 IConsolePrint(CC_ERROR, "Alias '{}' ('{}') contains malformed characters.", alias->name, alias->cmdline);
201 return;
202 }
203
204 switch (*c) {
205 case '\'': // ' will double for ""
206 builder.PutChar('\"');
207 break;
208
209 case ';': // Cmd separator; execute previous and start new command
210 IConsoleCmdExec(builder.GetString(), recurse_count);
211
212 buffer.clear();
213 break;
214
215 case '%': // Some or all parameters
216 c = consumer.ReadUtf8();
217 switch (*c) {
218 case '+': { // All parameters separated: "[param 1]" "[param 2]"
219 for (size_t i = 0; i < tokens.size(); ++i) {
220 if (i != 0) builder.PutChar(' ');
221 builder.PutChar('\"');
222 builder += tokens[i];
223 builder.PutChar('\"');
224 }
225 break;
226 }
227
228 case '!': { // Merge the parameters to one: "[param 1] [param 2] [param 3...]"
229 builder.PutChar('\"');
230 for (size_t i = 0; i < tokens.size(); ++i) {
231 if (i != 0) builder.PutChar(' ');
232 builder += tokens[i];
233 }
234 builder.PutChar('\"');
235 break;
236 }
237
238 default: { // One specific parameter: %A = [param 1] %B = [param 2] ...
239 size_t param = *c - 'A';
240
241 if (param >= tokens.size()) {
242 IConsolePrint(CC_ERROR, "Too many or wrong amount of parameters passed to alias.");
243 IConsolePrint(CC_HELP, "Usage of alias '{}': '{}'.", alias->name, alias->cmdline);
244 return;
245 }
246
247 builder.PutChar('\"');
248 builder += tokens[param];
249 builder.PutChar('\"');
250 break;
251 }
252 }
253 break;
254
255 default:
256 builder.PutUtf8(*c);
257 break;
258 }
259 }
260
261 IConsoleCmdExec(builder.GetString(), recurse_count);
262}
263
269 void IConsoleCmdExec(std::string_view command_string, const uint recurse_count)
270{
271 if (command_string[0] == '#') return; // comments
272
273 Debug(console, 4, "Executing cmdline: '{}'", command_string);
274
275 std::string buffer;
276 StringBuilder builder{buffer};
277 StringConsumer consumer{command_string};
278
279 std::vector<std::string> tokens;
280 bool found_token = false;
281 bool in_quotes = false;
282
283 /* 1. Split up commandline into tokens, separated by spaces, commands
284 * enclosed in "" are taken as one token. We can only go as far as the amount
285 * of characters in our stream or the max amount of tokens we can handle */
286 while (consumer.AnyBytesLeft()) {
287 auto c = consumer.TryReadUtf8();
288 if (!c.has_value()) {
289 IConsolePrint(CC_ERROR, "Command '{}' contains malformed characters.", command_string);
290 return;
291 }
292
293 switch (*c) {
294 case ' ': // Token separator
295 if (!found_token) break;
296
297 if (in_quotes) {
298 builder.PutUtf8(*c);
299 break;
300 }
301
302 tokens.emplace_back(std::move(buffer));
303 buffer.clear();
304 found_token = false;
305 break;
306
307 case '"': // Tokens enclosed in "" are one token
308 in_quotes = !in_quotes;
309 found_token = true;
310 break;
311
312 case '\\': // Escape character for ""
313 if (consumer.ReadUtf8If('"')) {
314 builder.PutUtf8('"');
315 break;
316 }
317 [[fallthrough]];
318
319 default: // Normal character
320 builder.PutUtf8(*c);
321 found_token = true;
322 break;
323 }
324 }
325
326 if (found_token) {
327 tokens.emplace_back(std::move(buffer));
328 buffer.clear();
329 }
330
331 for (size_t i = 0; i < tokens.size(); i++) {
332 Debug(console, 8, "Token {} is: '{}'", i, tokens[i]);
333 }
334
335 if (tokens.empty() || tokens[0].empty()) return; // don't execute empty commands
336 /* 2. Determine type of command (cmd or alias) and execute
337 * First try commands, then aliases. Execute
338 * the found action taking into account its hooking code
339 */
340 IConsoleCmd *cmd = IConsole::CmdGet(tokens[0]);
341 if (cmd != nullptr) {
342 ConsoleHookResult chr = (cmd->hook == nullptr ? CHR_ALLOW : cmd->hook(true));
343 switch (chr) {
344 case CHR_ALLOW: {
345 std::vector<std::string_view> views;
346 for (auto &token : tokens) views.emplace_back(token);
347 if (!cmd->proc(views)) { // index started with 0
348 cmd->proc({}); // if command failed, give help
349 }
350 return;
351 }
352
353 case CHR_DISALLOW: return;
354 case CHR_HIDE: break;
355 }
356 }
357
358 IConsoleAlias *alias = IConsole::AliasGet(tokens[0]);
359 if (alias != nullptr) {
360 IConsoleAliasExec(alias, std::span(tokens).subspan(1), recurse_count + 1);
361 return;
362 }
363
364 IConsolePrint(CC_ERROR, "Command '{}' not found.", tokens[0]);
365}
StringBuilder
Compose data into a growing std::string.
Definition string_builder.hpp:65
StringConsumer
Parse data from a string / buffer.
Definition string_consumer.hpp:27
StringConsumer::TryReadUtf8
std::optional< char32_t > TryReadUtf8()
Try to read a UTF-8 character, and then advance reader.
Definition string_consumer.hpp:412
RemoveUnderscores
static std::string RemoveUnderscores(std::string name)
Creates a copy of a string with underscores removed from it.
Definition console.cpp:126
IConsoleCmdExec
void IConsoleCmdExec(std::string_view command_string, const uint recurse_count)
Execute a given command passed to us.
Definition console.cpp:269
IConsoleAliasExec
static void IConsoleAliasExec(const IConsoleAlias *alias, std::span< std::string > tokens, uint recurse_count)
An alias is just another name for a command, or for more commands Execute it as well.
Definition console.cpp:184
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:90
ICON_MAX_RECURSE
static const uint ICON_MAX_RECURSE
Maximum number of recursion.
Definition console.cpp:23
console_func.h
Console functions used outside of the console code.
IsValidConsoleColour
bool IsValidConsoleColour(TextColour c)
Check whether the given TextColour is valid for console usage.
Definition console_gui.cpp:538
IConsoleGUIPrint
void IConsoleGUIPrint(TextColour colour_code, const std::string &str)
Handle the printing of text entered into the console or redirected there by any other means.
Definition console_gui.cpp:498
console_internal.h
Internally used functions for the console.
ConsoleHookResult
ConsoleHookResult
Return values of console hooks (IConsoleHook).
Definition console_internal.h:18
CHR_HIDE
@ CHR_HIDE
Hide the existence of the command.
Definition console_internal.h:21
CHR_DISALLOW
@ CHR_DISALLOW
Disallow command execution.
Definition console_internal.h:20
CHR_ALLOW
@ CHR_ALLOW
Allow command execution.
Definition console_internal.h:19
IConsoleCmdProc
bool(std::span< std::string_view >) IConsoleCmdProc
–Commands– Commands are commands, or functions.
Definition console_internal.h:32
CC_HELP
static const TextColour CC_HELP
Colour for help lines.
Definition console_type.h:26
CC_INFO
static const TextColour CC_INFO
Colour for information lines.
Definition console_type.h:27
CC_ERROR
static const TextColour CC_ERROR
Colour for error lines.
Definition console_type.h:24
GetLogPrefix
std::string GetLogPrefix(bool force)
Get the prefix for logs.
Definition debug.cpp:219
debug.h
Functions related to debugging.
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
_redirect_console_to_client
ClientID _redirect_console_to_client
If not invalid, redirect the console output to a client.
Definition network.cpp:72
_network_dedicated
bool _network_dedicated
are we a dedicated server?
Definition network.cpp:69
network.h
Basic functions/variables used all over the place.
NetworkServerSendAdminRcon
void NetworkServerSendAdminRcon(AdminID admin_index, TextColour colour_code, std::string_view string)
Pass the rcon reply to the admin.
Definition network_admin.cpp:999
NetworkAdminConsole
void NetworkAdminConsole(std::string_view origin, std::string_view string)
Send console to the admin network (if they did opt in for the respective update).
Definition network_admin.cpp:1009
_redirect_console_to_admin
AdminID _redirect_console_to_admin
Redirection of the (remote) console to the admin.
Definition network_admin.cpp:34
network_admin.h
Server part of the admin network protocol.
network_func.h
Network functions used by other parts of OpenTTD.
NetworkServerSendRcon
void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, std::string_view string)
Send an rcon reply to the client.
Definition network_server.cpp:2019
INVALID_CLIENT_ID
@ INVALID_CLIENT_ID
Client is not part of anything.
Definition network_type.h:44
safeguards.h
A number of safeguards to prevent using unsafe methods.
settings_type.h
Types related to global configuration settings.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
StrMakeValid
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Definition string.cpp:119
string_builder.hpp
Compose strings from textual and binary data.
string_consumer.hpp
Parse strings.
IConsoleAlias
–Aliases– Aliases are like shortcuts for complex functions, variable assignments, etc.
Definition console_internal.h:54
IConsoleAlias::cmdline
std::string cmdline
command(s) that is/are being aliased
Definition console_internal.h:58
IConsoleAlias::name
std::string name
name of the alias
Definition console_internal.h:57
IConsoleCmd
Definition console_internal.h:34
IConsoleCmd::proc
IConsoleCmdProc * proc
process executed when command is typed
Definition console_internal.h:38
IConsoleCmd::hook
IConsoleHook * hook
any special trigger action that needs executing
Definition console_internal.h:39
IConsole::AliasRegister
static void AliasRegister(const std::string &name, std::string_view cmd)
Register a an alias for an already existing command in the console.
Definition console.cpp:159
IConsole::AliasGet
static IConsoleAlias * AliasGet(const std::string &name)
Find the alias pointed to by its string.
Definition console.cpp:170
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:137
IConsole::CmdGet
static IConsoleCmd * CmdGet(const std::string &name)
Find the command pointed to by its string.
Definition console.cpp:147