31 FILE *_iconsole_output_file;
35 _iconsole_output_file =
nullptr;
41 IConsoleStdLibRegister();
44 static void IConsoleWriteToLogFile(
const char *
string)
46 if (_iconsole_output_file !=
nullptr) {
49 if ((strlen(header) != 0 && fwrite(header, strlen(header), 1, _iconsole_output_file) != 1) ||
50 fwrite(
string, strlen(
string), 1, _iconsole_output_file) != 1 ||
51 fwrite(
"\n", 1, 1, _iconsole_output_file) != 1) {
52 fclose(_iconsole_output_file);
53 _iconsole_output_file =
nullptr;
59 bool CloseConsoleLogIfActive()
61 if (_iconsole_output_file !=
nullptr) {
63 fclose(_iconsole_output_file);
64 _iconsole_output_file =
nullptr;
74 CloseConsoleLogIfActive();
112 IConsoleWriteToLogFile(str);
117 IConsoleWriteToLogFile(str);
133 va_start(va, format);
185 if (strcmp(arg,
"on") == 0 || strcmp(arg,
"true") == 0) {
189 if (strcmp(arg,
"off") == 0 || strcmp(arg,
"false") == 0) {
194 *value = strtoul(arg, &endptr, 0);
195 return arg != endptr;
206 if (*base ==
nullptr) {
211 T *item_before =
nullptr;
214 while (item !=
nullptr) {
215 if (strcmp(item->name, item_new->name) > 0)
break;
221 if (item_before ==
nullptr) {
224 item_before->next = item_new;
227 item_new->next = item;
238 for (
const char *p = name; *p !=
'\0'; p++) {
239 if (*p !=
'_') *q++ = *p;
254 item_new->
next =
nullptr;
255 item_new->
proc = proc;
256 item_new->
hook = hook;
270 for (item = _iconsole_cmds; item !=
nullptr; item = item->
next) {
271 if (strcmp(item->
name, name) == 0)
return item;
284 IConsoleError(
"an alias with this name already exists; insertion aborted");
289 char *cmd_aliased =
stredup(cmd);
292 item_new->
next =
nullptr;
293 item_new->
cmdline = cmd_aliased;
294 item_new->
name = new_alias;
308 for (item = _iconsole_aliases; item !=
nullptr; item = item->
next) {
309 if (strcmp(item->
name, name) == 0)
return item;
324 char *alias_stream = alias_buffer;
326 DEBUG(console, 6,
"Requested command is an alias; parsing...");
328 for (
const char *cmdptr = alias->
cmdline; *cmdptr !=
'\0'; cmdptr++) {
331 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
337 alias_stream = alias_buffer;
338 *alias_stream =
'\0';
347 for (uint i = 0; i != tokencount; i++) {
348 if (i != 0) alias_stream =
strecpy(alias_stream,
" ",
lastof(alias_buffer));
349 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
350 alias_stream =
strecpy(alias_stream, tokens[i],
lastof(alias_buffer));
351 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
357 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
358 for (uint i = 0; i != tokencount; i++) {
359 if (i != 0) alias_stream =
strecpy(alias_stream,
" ",
lastof(alias_buffer));
360 alias_stream =
strecpy(alias_stream, tokens[i],
lastof(alias_buffer));
362 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
367 int param = *cmdptr -
'A';
369 if (param < 0 || param >= tokencount) {
370 IConsoleError(
"too many or wrong amount of parameters passed to alias, aborting");
375 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
376 alias_stream =
strecpy(alias_stream, tokens[param],
lastof(alias_buffer));
377 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
384 *alias_stream++ = *cmdptr;
385 *alias_stream =
'\0';
389 if (alias_stream >=
lastof(alias_buffer) - 1) {
390 IConsoleError(
"Requested alias execution would overflow execution buffer");
407 uint t_index, tstream_i;
409 bool longtoken =
false;
410 bool foundtoken =
false;
412 if (cmdstr[0] ==
'#')
return;
414 for (cmdptr = cmdstr; *cmdptr !=
'\0'; cmdptr++) {
416 IConsoleError(
"command contains malformed characters, aborting");
422 DEBUG(console, 4,
"Executing cmdline: '%s'", cmdstr);
424 memset(&tokens, 0,
sizeof(tokens));
425 memset(&tokenstream, 0,
sizeof(tokenstream));
430 for (cmdptr = cmdstr, t_index = 0, tstream_i = 0; *cmdptr !=
'\0'; cmdptr++) {
431 if (tstream_i >=
lengthof(tokenstream)) {
438 if (!foundtoken)
break;
441 tokenstream[tstream_i] = *cmdptr;
443 tokenstream[tstream_i] =
'\0';
450 longtoken = !longtoken;
456 tokens[t_index++] = &tokenstream[tstream_i];
461 if (cmdptr[1] ==
'"' && tstream_i + 1 <
lengthof(tokenstream)) {
462 tokenstream[tstream_i++] = *++cmdptr;
467 tokenstream[tstream_i++] = *cmdptr;
474 tokens[t_index++] = &tokenstream[tstream_i - 1];
481 for (uint i = 0; i <
lengthof(tokens) && tokens[i] !=
nullptr; i++) {
482 DEBUG(console, 8,
"Token %d is: '%s'", i, tokens[i]);
492 if (cmd !=
nullptr) {
496 if (!cmd->
proc(t_index, tokens)) {
497 cmd->
proc(0,
nullptr);
508 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.