29 FILE *_iconsole_output_file;
33 _iconsole_output_file =
nullptr;
39 IConsoleStdLibRegister();
42 static void IConsoleWriteToLogFile(
const char *
string)
44 if (_iconsole_output_file !=
nullptr) {
47 if ((strlen(header) != 0 && fwrite(header, strlen(header), 1, _iconsole_output_file) != 1) ||
48 fwrite(
string, strlen(
string), 1, _iconsole_output_file) != 1 ||
49 fwrite(
"\n", 1, 1, _iconsole_output_file) != 1) {
50 fclose(_iconsole_output_file);
51 _iconsole_output_file =
nullptr;
57 bool CloseConsoleLogIfActive()
59 if (_iconsole_output_file !=
nullptr) {
61 fclose(_iconsole_output_file);
62 _iconsole_output_file =
nullptr;
72 CloseConsoleLogIfActive();
110 IConsoleWriteToLogFile(str);
115 IConsoleWriteToLogFile(str);
131 va_start(va, format);
183 if (strcmp(arg,
"on") == 0 || strcmp(arg,
"true") == 0) {
187 if (strcmp(arg,
"off") == 0 || strcmp(arg,
"false") == 0) {
192 *value = strtoul(arg, &endptr, 0);
193 return arg != endptr;
204 if (*base ==
nullptr) {
209 T *item_before =
nullptr;
212 while (item !=
nullptr) {
213 if (strcmp(item->name, item_new->name) > 0)
break;
219 if (item_before ==
nullptr) {
222 item_before->next = item_new;
225 item_new->next = item;
236 for (
const char *p = name; *p !=
'\0'; p++) {
237 if (*p !=
'_') *q++ = *p;
252 item_new->
next =
nullptr;
253 item_new->
proc = proc;
254 item_new->
hook = hook;
268 for (item = _iconsole_cmds; item !=
nullptr; item = item->
next) {
269 if (strcmp(item->
name, name) == 0)
return item;
282 IConsoleError(
"an alias with this name already exists; insertion aborted");
287 char *cmd_aliased =
stredup(cmd);
290 item_new->
next =
nullptr;
291 item_new->
cmdline = cmd_aliased;
292 item_new->
name = new_alias;
306 for (item = _iconsole_aliases; item !=
nullptr; item = item->
next) {
307 if (strcmp(item->
name, name) == 0)
return item;
322 char *alias_stream = alias_buffer;
324 DEBUG(console, 6,
"Requested command is an alias; parsing...");
326 for (
const char *cmdptr = alias->
cmdline; *cmdptr !=
'\0'; cmdptr++) {
329 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
335 alias_stream = alias_buffer;
336 *alias_stream =
'\0';
345 for (uint i = 0; i != tokencount; i++) {
346 if (i != 0) alias_stream =
strecpy(alias_stream,
" ",
lastof(alias_buffer));
347 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
348 alias_stream =
strecpy(alias_stream, tokens[i],
lastof(alias_buffer));
349 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
355 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
356 for (uint i = 0; i != tokencount; i++) {
357 if (i != 0) alias_stream =
strecpy(alias_stream,
" ",
lastof(alias_buffer));
358 alias_stream =
strecpy(alias_stream, tokens[i],
lastof(alias_buffer));
360 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
365 int param = *cmdptr -
'A';
367 if (param < 0 || param >= tokencount) {
368 IConsoleError(
"too many or wrong amount of parameters passed to alias, aborting");
373 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
374 alias_stream =
strecpy(alias_stream, tokens[param],
lastof(alias_buffer));
375 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
382 *alias_stream++ = *cmdptr;
383 *alias_stream =
'\0';
387 if (alias_stream >=
lastof(alias_buffer) - 1) {
388 IConsoleError(
"Requested alias execution would overflow execution buffer");
405 uint t_index, tstream_i;
407 bool longtoken =
false;
408 bool foundtoken =
false;
410 if (cmdstr[0] ==
'#')
return;
412 for (cmdptr = cmdstr; *cmdptr !=
'\0'; cmdptr++) {
414 IConsoleError(
"command contains malformed characters, aborting");
420 DEBUG(console, 4,
"Executing cmdline: '%s'", cmdstr);
422 memset(&tokens, 0,
sizeof(tokens));
423 memset(&tokenstream, 0,
sizeof(tokenstream));
428 for (cmdptr = cmdstr, t_index = 0, tstream_i = 0; *cmdptr !=
'\0'; cmdptr++) {
429 if (tstream_i >=
lengthof(tokenstream)) {
436 if (!foundtoken)
break;
439 tokenstream[tstream_i] = *cmdptr;
441 tokenstream[tstream_i] =
'\0';
448 longtoken = !longtoken;
454 tokens[t_index++] = &tokenstream[tstream_i];
459 if (cmdptr[1] ==
'"' && tstream_i + 1 <
lengthof(tokenstream)) {
460 tokenstream[tstream_i++] = *++cmdptr;
465 tokenstream[tstream_i++] = *cmdptr;
472 tokens[t_index++] = &tokenstream[tstream_i - 1];
479 for (uint i = 0; i <
lengthof(tokens) && tokens[i] !=
nullptr; i++) {
480 DEBUG(console, 8,
"Token %d is: '%s'", i, tokens[i]);
490 if (cmd !=
nullptr) {
494 if (!cmd->
proc(t_index, tokens)) {
495 cmd->
proc(0,
nullptr);
506 if (alias !=
nullptr) {
IConsoleCmd * next
next command in list
void IConsoleWarning(const char *string)
It is possible to print warnings to the console.
IConsoleCmd * _iconsole_cmds
list of registered commands
char * name
Name of the company if the user changed it.
char * name
name of the alias
uint8 developer
print non-fatal warnings in console (>= 1), copy debug output to console (== 2)
Functions related to debugging.
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
static const uint ICON_TOKEN_COUNT
Maximum number of tokens in one command.
ClientID _redirect_console_to_client
If not invalid, redirect the console output to a client.
static const AdminIndex INVALID_ADMIN_ID
An invalid admin marker.
IConsoleCmdProc * proc
process executed when command is typed
#define lastof(x)
Get the last element of an fixed size array.
char * name
name of command
static const TextColour CC_DEFAULT
Default colour of the console.
IConsoleAlias * _iconsole_aliases
list of registered aliases
bool IsValidConsoleColour(TextColour c)
Check whether the given TextColour is valid for console usage.
AdminIndex _redirect_console_to_admin
Redirection of the (remote) console to the admin.
void IConsoleGUIPrint(TextColour colour_code, char *str)
Handle the printing of text entered into the console or redirected there by any other means...
Hide the existence of the command.
void IConsoleDebug(const char *dbg, const char *string)
It is possible to print debugging information to the console, which is achieved by using this functio...
bool _network_dedicated
are we a dedicated server?
const char * GetLogPrefix()
Get the prefix for logs; if show_date_in_logs is enabled it returns the date, otherwise it returns no...
void NetworkAdminConsole(const char *origin, const char *string)
Send console to the admin network (if they did opt in for the respective update). ...
bool IConsoleCmdProc(byte argc, char *argv[])
–Commands– Commands are commands, or functions.
bool IsValidChar(WChar key, CharSetFilter afilter)
Only allow certain keys.
Internally used functions for the console.
IConsoleAlias * next
next alias in list
void str_validate(char *str, const char *last, StringValidationSettings settings)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
static const uint ICON_MAX_STREAMSIZE
maximum length of a totally expanded command
void IConsolePrint(TextColour colour_code, const char *string)
Handle the printing of text entered into the console or redirected there by any other means...
IConsoleCmd * IConsoleCmdGet(const char *name)
Find the command pointed to by its string.
void IConsoleCmdExec(const char *cmdstr)
Execute a given command passed to us.
ClientSettings _settings_client
The current settings for this game.
Types related to global configuration settings.
void CDECL IConsolePrintF(TextColour colour_code, const char *format,...)
Handle the printing of text entered into the console or redirected there by any other means...
Definition of base types and functions in a cross-platform compatible way.
static const TextColour CC_DEBUG
Colour for debug output.
ConsoleHookResult
Return values of console hooks (#IConsoleHook).
A number of safeguards to prevent using unsafe methods.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char *tokens[ICON_TOKEN_COUNT])
An alias is just another name for a command, or for more commands Execute it as well.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Console functions used outside of the console code.
void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const char *string)
Send an rcon reply to the client.
Basic functions/variables used all over the place.
#define lengthof(x)
Return the length of an fixed size array.
void str_strip_colours(char *str)
Scans the string for colour codes and strips them.
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const char *string)
Pass the rcon reply to the admin.
IConsoleAlias * IConsoleAliasGet(const char *name)
Find the alias pointed to by its string.
char * RemoveUnderscores(char *name)
Remove underscores from a string; the string will be modified!
#define DEBUG(name, level,...)
Output a line of debugging information.
Both numeric and alphabetic and spaces and stuff.
GUISettings gui
settings related to the GUI
–Aliases– Aliases are like shortcuts for complex functions, variable assignments, etc.
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
void IConsoleAliasRegister(const char *name, const char *cmd)
Register a an alias for an already existing command in the console.
char * cmdline
command(s) that is/are being aliased
IConsoleHook * hook
any special trigger action that needs executing
void IConsoleError(const char *string)
It is possible to print error information to the console.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Network functions used by other parts of OpenTTD.
static const TextColour CC_ERROR
Colour for error lines.
void IConsoleAddSorted(T **base, T *item_new)
Add an item to an alphabetically sorted list.
Client is not part of anything.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Disallow command execution.
static const TextColour CC_WARNING
Colour for warning lines.
bool GetArgumentInteger(uint32 *value, const char *arg)
Change a string into its number representation.
void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc, IConsoleHook *hook)
Register a new command to be used in the console.
Server part of the admin network protocol.