OpenTTD Source
12.0-beta2
|
Go to the documentation of this file.
27 IConsole::CommandList &IConsole::Commands()
29 static IConsole::CommandList cmds;
33 IConsole::AliasList &IConsole::Aliases()
35 static IConsole::AliasList aliases;
39 FILE *_iconsole_output_file;
43 _iconsole_output_file =
nullptr;
49 IConsoleStdLibRegister();
52 static void IConsoleWriteToLogFile(
const char *
string)
54 if (_iconsole_output_file !=
nullptr) {
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;
67 bool CloseConsoleLogIfActive()
69 if (_iconsole_output_file !=
nullptr) {
71 fclose(_iconsole_output_file);
72 _iconsole_output_file =
nullptr;
82 CloseConsoleLogIfActive();
111 char *str =
stredup(
string.c_str());
119 IConsoleWriteToLogFile(str);
124 IConsoleWriteToLogFile(str);
139 if (strcmp(arg,
"on") == 0 || strcmp(arg,
"true") == 0) {
143 if (strcmp(arg,
"off") == 0 || strcmp(arg,
"false") == 0) {
148 *value = strtoul(arg, &endptr, 0);
149 return arg != endptr;
181 if (item != IConsole::Commands().end())
return &item->second;
204 if (item != IConsole::Aliases().end())
return &item->second;
218 char *alias_stream = alias_buffer;
220 Debug(console, 6,
"Requested command is an alias; parsing...");
227 for (
const char *cmdptr = alias->
cmdline.c_str(); *cmdptr !=
'\0'; cmdptr++) {
230 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
236 alias_stream = alias_buffer;
237 *alias_stream =
'\0';
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));
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));
261 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
266 int param = *cmdptr -
'A';
268 if (param < 0 || param >= tokencount) {
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));
283 *alias_stream++ = *cmdptr;
284 *alias_stream =
'\0';
288 if (alias_stream >=
lastof(alias_buffer) - 1) {
306 uint t_index, tstream_i;
308 bool longtoken =
false;
309 bool foundtoken =
false;
311 if (cmdstr[0] ==
'#')
return;
313 for (cmdptr = cmdstr; *cmdptr !=
'\0'; cmdptr++) {
320 Debug(console, 4,
"Executing cmdline: '{}'", cmdstr);
322 memset(&tokens, 0,
sizeof(tokens));
323 memset(&tokenstream, 0,
sizeof(tokenstream));
328 for (cmdptr = cmdstr, t_index = 0, tstream_i = 0; *cmdptr !=
'\0'; cmdptr++) {
329 if (tstream_i >=
lengthof(tokenstream)) {
336 if (!foundtoken)
break;
339 tokenstream[tstream_i] = *cmdptr;
341 tokenstream[tstream_i] =
'\0';
348 longtoken = !longtoken;
354 tokens[t_index++] = &tokenstream[tstream_i];
359 if (cmdptr[1] ==
'"' && tstream_i + 1 <
lengthof(tokenstream)) {
360 tokenstream[tstream_i++] = *++cmdptr;
365 tokenstream[tstream_i++] = *cmdptr;
372 tokens[t_index++] = &tokenstream[tstream_i - 1];
379 for (uint i = 0; i <
lengthof(tokens) && tokens[i] !=
nullptr; i++) {
380 Debug(console, 8,
"Token {} is: '{}'", i, tokens[i]);
389 if (cmd !=
nullptr) {
393 if (!cmd->
proc(t_index, tokens)) {
394 cmd->
proc(0,
nullptr);
405 if (alias !=
nullptr) {
static const TextColour CC_INFO
Colour for information lines.
IConsoleCmdProc * proc
process executed when command is typed
@ INVALID_CLIENT_ID
Client is not part of anything.
static IConsoleAlias * AliasGet(const std::string &name)
Find the alias pointed to by its string.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
std::string name
Name of the company if the user changed it.
ClientID _redirect_console_to_client
If not invalid, redirect the console output to a client.
void IConsoleGUIPrint(TextColour colour_code, char *str)
Handle the printing of text entered into the console or redirected there by any other means.
std::string cmdline
command(s) that is/are being aliased
static const AdminIndex INVALID_ADMIN_ID
An invalid admin marker.
AdminIndex _redirect_console_to_admin
Redirection of the (remote) console to the admin.
IConsoleHook * hook
any special trigger action that needs executing
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).
static const uint ICON_TOKEN_COUNT
Maximum number of tokens in one command.
static void CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook=nullptr)
Register a new command to be used in the console.
static const uint ICON_MAX_STREAMSIZE
maximum length of a totally expanded command
const char * GetLogPrefix()
Get the prefix for logs; if show_date_in_logs is enabled it returns the date, otherwise it returns no...
static const TextColour CC_HELP
Colour for help lines.
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).
void str_strip_colours(char *str)
Scans the string for colour codes and strips them.
@ CHR_ALLOW
Allow command execution.
bool GetArgumentInteger(uint32 *value, const char *arg)
Change a string into its number representation.
bool IsValidConsoleColour(TextColour c)
Check whether the given TextColour is valid for console usage.
bool IsValidChar(WChar key, CharSetFilter afilter)
Only allow certain keys.
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
static const uint ICON_MAX_RECURSE
Maximum number of recursion.
bool _network_dedicated
are we a dedicated server?
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
std::string name
name of the alias
static std::string RemoveUnderscores(std::string name)
Creates a copy of a string with underscores removed from it.
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.
static void AliasRegister(const std::string &name, const std::string &cmd)
Register a an alias for an already existing command in the console.
void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const std::string &string)
Send an rcon reply to the client.
–Aliases– Aliases are like shortcuts for complex functions, variable assignments, etc.
static const TextColour CC_ERROR
Colour for error lines.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
@ CHR_DISALLOW
Disallow command execution.
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
#define lengthof(x)
Return the length of an fixed size array.
@ CHR_HIDE
Hide the existence of the command.
void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
Execute a given command passed to us.
bool IConsoleCmdProc(byte argc, char *argv[])
–Commands– Commands are commands, or functions.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
#define lastof(x)
Get the last element of an fixed size array.
ConsoleHookResult
Return values of console hooks (#IConsoleHook).
static IConsoleCmd * CmdGet(const std::string &name)
Find the command pointed to by its string.
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const std::string_view string)
Pass the rcon reply to the admin.
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.