25 IConsole::CommandList &IConsole::Commands()
27 static IConsole::CommandList cmds;
31 IConsole::AliasList &IConsole::Aliases()
33 static IConsole::AliasList aliases;
37std::optional<FileHandle> _iconsole_output_file;
41 _iconsole_output_file = std::nullopt;
47 IConsoleStdLibRegister();
50static void IConsoleWriteToLogFile(
const std::string &
string)
52 if (_iconsole_output_file.has_value()) {
55 fmt::print(*_iconsole_output_file,
"{}{}\n",
GetLogPrefix(),
string);
56 }
catch (
const std::system_error &) {
57 _iconsole_output_file.reset();
63bool CloseConsoleLogIfActive()
65 if (_iconsole_output_file.has_value()) {
67 _iconsole_output_file.reset();
77 CloseConsoleLogIfActive();
112 IConsoleWriteToLogFile(str);
116 IConsoleWriteToLogFile(str);
131 if (strcmp(arg,
"on") == 0 || strcmp(arg,
"true") == 0) {
135 if (strcmp(arg,
"off") == 0 || strcmp(arg,
"false") == 0) {
140 *value = std::strtoul(arg, &endptr, 0);
141 return arg != endptr;
151 name.erase(std::remove(name.begin(), name.end(),
'_'), name.end());
173 if (item != IConsole::Commands().end())
return &item->second;
184 auto result = IConsole::Aliases().try_emplace(
RemoveUnderscores(name), name, cmd);
196 if (item != IConsole::Aliases().end())
return &item->second;
209 std::string alias_buffer;
211 Debug(console, 6,
"Requested command is an alias; parsing...");
218 for (
const char *cmdptr = alias->
cmdline.c_str(); *cmdptr !=
'\0'; cmdptr++) {
221 alias_buffer +=
'\"';
227 alias_buffer.clear();
236 for (uint i = 0; i != tokencount; i++) {
237 if (i != 0) alias_buffer +=
' ';
238 alias_buffer +=
'\"';
239 alias_buffer += tokens[i];
240 alias_buffer +=
'\"';
246 alias_buffer +=
'\"';
247 for (uint i = 0; i != tokencount; i++) {
248 if (i != 0) alias_buffer +=
" ";
249 alias_buffer += tokens[i];
251 alias_buffer +=
'\"';
256 int param = *cmdptr -
'A';
258 if (param < 0 || param >= tokencount) {
264 alias_buffer +=
'\"';
265 alias_buffer += tokens[param];
266 alias_buffer +=
'\"';
273 alias_buffer += *cmdptr;
295 uint t_index, tstream_i;
297 bool longtoken =
false;
298 bool foundtoken =
false;
300 if (command_string[0] ==
'#')
return;
302 for (cmdptr = command_string.c_str(); *cmdptr !=
'\0'; cmdptr++) {
309 Debug(console, 4,
"Executing cmdline: '{}'", command_string);
311 memset(&tokens, 0,
sizeof(tokens));
312 memset(&tokenstream, 0,
sizeof(tokenstream));
317 for (cmdptr = command_string.c_str(), t_index = 0, tstream_i = 0; *cmdptr !=
'\0'; cmdptr++) {
318 if (tstream_i >=
lengthof(tokenstream)) {
325 if (!foundtoken)
break;
328 tokenstream[tstream_i] = *cmdptr;
330 tokenstream[tstream_i] =
'\0';
337 longtoken = !longtoken;
343 tokens[t_index++] = &tokenstream[tstream_i];
348 if (cmdptr[1] ==
'"' && tstream_i + 1 <
lengthof(tokenstream)) {
349 tokenstream[tstream_i++] = *++cmdptr;
354 tokenstream[tstream_i++] = *cmdptr;
361 tokens[t_index++] = &tokenstream[tstream_i - 1];
368 for (uint i = 0; i <
lengthof(tokens) && tokens[i] !=
nullptr; i++) {
369 Debug(console, 8,
"Token {} is: '{}'", i, tokens[i]);
378 if (cmd !=
nullptr) {
382 if (!cmd->
proc(t_index, tokens)) {
383 cmd->
proc(0,
nullptr);
394 if (alias !=
nullptr) {
bool GetArgumentInteger(uint32_t *value, const char *arg)
Change a string into its number representation.
static std::string RemoveUnderscores(std::string name)
Creates a copy of a string with underscores removed from it.
void IConsoleCmdExec(const std::string &command_string, const uint recurse_count)
Execute a given command passed to us.
static const uint ICON_TOKEN_COUNT
Maximum number of tokens in one command.
static void IConsoleAliasExec(const IConsoleAlias *alias, uint8_t 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.
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.
static const uint ICON_MAX_RECURSE
Maximum number of recursion.
Console functions used outside of the console code.
bool IsValidConsoleColour(TextColour c)
Check whether the given TextColour is valid for console usage.
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.
Internally used functions for the console.
static const uint ICON_MAX_STREAMSIZE
maximum length of a totally expanded command
ConsoleHookResult
Return values of console hooks (IConsoleHook).
@ CHR_HIDE
Hide the existence of the command.
@ CHR_DISALLOW
Disallow command execution.
@ CHR_ALLOW
Allow command execution.
bool IConsoleCmdProc(uint8_t argc, char *argv[])
–Commands– Commands are commands, or functions.
static const TextColour CC_HELP
Colour for help lines.
static const TextColour CC_INFO
Colour for information lines.
static const TextColour CC_ERROR
Colour for error lines.
std::string GetLogPrefix(bool force)
Get the prefix for logs.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
ClientID _redirect_console_to_client
If not invalid, redirect the console output to a client.
bool _network_dedicated
are we a dedicated server?
Basic functions/variables used all over the place.
AdminIndex _redirect_console_to_admin
Redirection of the (remote) console to the admin.
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).
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const std::string_view string)
Pass the rcon reply to the admin.
Server part of the admin network protocol.
Network functions used by other parts of OpenTTD.
void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const std::string &string)
Send an rcon reply to the client.
static const AdminIndex INVALID_ADMIN_ID
An invalid admin marker.
@ INVALID_CLIENT_ID
Client is not part of anything.
A number of safeguards to prevent using unsafe methods.
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
bool StrEmpty(const char *s)
Check if a string buffer is empty.
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
@ SVS_NONE
Allow nothing and replace nothing.
–Aliases– Aliases are like shortcuts for complex functions, variable assignments, etc.
std::string cmdline
command(s) that is/are being aliased
std::string name
name of the alias
IConsoleCmdProc * proc
process executed when command is typed
IConsoleHook * hook
any special trigger action that needs executing
static IConsoleAlias * AliasGet(const std::string &name)
Find the alias pointed to by its string.
static void CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook=nullptr)
Register a new command to be used in the console.
static IConsoleCmd * CmdGet(const std::string &name)
Find the command pointed to by its string.
static void AliasRegister(const std::string &name, const std::string &cmd)
Register a an alias for an already existing command in the console.