30 FILE *_iconsole_output_file;
34 _iconsole_output_file =
nullptr;
40 IConsoleStdLibRegister();
43 static void IConsoleWriteToLogFile(
const char *
string)
45 if (_iconsole_output_file !=
nullptr) {
48 if ((strlen(header) != 0 && fwrite(header, strlen(header), 1, _iconsole_output_file) != 1) ||
49 fwrite(
string, strlen(
string), 1, _iconsole_output_file) != 1 ||
50 fwrite(
"\n", 1, 1, _iconsole_output_file) != 1) {
51 fclose(_iconsole_output_file);
52 _iconsole_output_file =
nullptr;
58 bool CloseConsoleLogIfActive()
60 if (_iconsole_output_file !=
nullptr) {
62 fclose(_iconsole_output_file);
63 _iconsole_output_file =
nullptr;
73 CloseConsoleLogIfActive();
111 IConsoleWriteToLogFile(str);
116 IConsoleWriteToLogFile(str);
132 va_start(va, format);
184 if (strcmp(arg,
"on") == 0 || strcmp(arg,
"true") == 0) {
188 if (strcmp(arg,
"off") == 0 || strcmp(arg,
"false") == 0) {
193 *value = strtoul(arg, &endptr, 0);
194 return arg != endptr;
205 if (*base ==
nullptr) {
210 T *item_before =
nullptr;
213 while (item !=
nullptr) {
214 if (strcmp(item->name, item_new->name) > 0)
break;
220 if (item_before ==
nullptr) {
223 item_before->next = item_new;
226 item_new->next = item;
237 for (
const char *p = name; *p !=
'\0'; p++) {
238 if (*p !=
'_') *q++ = *p;
253 item_new->
next =
nullptr;
254 item_new->
proc = proc;
255 item_new->
hook = hook;
269 for (item = _iconsole_cmds; item !=
nullptr; item = item->
next) {
270 if (strcmp(item->
name, name) == 0)
return item;
283 IConsoleError(
"an alias with this name already exists; insertion aborted");
288 char *cmd_aliased =
stredup(cmd);
291 item_new->
next =
nullptr;
292 item_new->
cmdline = cmd_aliased;
293 item_new->
name = new_alias;
307 for (item = _iconsole_aliases; item !=
nullptr; item = item->
next) {
308 if (strcmp(item->
name, name) == 0)
return item;
323 char *alias_stream = alias_buffer;
325 DEBUG(console, 6,
"Requested command is an alias; parsing...");
328 IConsoleError(
"Too many alias expansions, recursion limit reached. Aborting");
332 for (
const char *cmdptr = alias->
cmdline; *cmdptr !=
'\0'; cmdptr++) {
335 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
341 alias_stream = alias_buffer;
342 *alias_stream =
'\0';
351 for (uint i = 0; i != tokencount; i++) {
352 if (i != 0) alias_stream =
strecpy(alias_stream,
" ",
lastof(alias_buffer));
353 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
354 alias_stream =
strecpy(alias_stream, tokens[i],
lastof(alias_buffer));
355 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
361 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
362 for (uint i = 0; i != tokencount; i++) {
363 if (i != 0) alias_stream =
strecpy(alias_stream,
" ",
lastof(alias_buffer));
364 alias_stream =
strecpy(alias_stream, tokens[i],
lastof(alias_buffer));
366 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
371 int param = *cmdptr -
'A';
373 if (param < 0 || param >= tokencount) {
374 IConsoleError(
"too many or wrong amount of parameters passed to alias, aborting");
379 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
380 alias_stream =
strecpy(alias_stream, tokens[param],
lastof(alias_buffer));
381 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
388 *alias_stream++ = *cmdptr;
389 *alias_stream =
'\0';
393 if (alias_stream >=
lastof(alias_buffer) - 1) {
394 IConsoleError(
"Requested alias execution would overflow execution buffer");
411 uint t_index, tstream_i;
413 bool longtoken =
false;
414 bool foundtoken =
false;
416 if (cmdstr[0] ==
'#')
return;
418 for (cmdptr = cmdstr; *cmdptr !=
'\0'; cmdptr++) {
420 IConsoleError(
"command contains malformed characters, aborting");
426 DEBUG(console, 4,
"Executing cmdline: '%s'", cmdstr);
428 memset(&tokens, 0,
sizeof(tokens));
429 memset(&tokenstream, 0,
sizeof(tokenstream));
434 for (cmdptr = cmdstr, t_index = 0, tstream_i = 0; *cmdptr !=
'\0'; cmdptr++) {
435 if (tstream_i >=
lengthof(tokenstream)) {
442 if (!foundtoken)
break;
445 tokenstream[tstream_i] = *cmdptr;
447 tokenstream[tstream_i] =
'\0';
454 longtoken = !longtoken;
460 tokens[t_index++] = &tokenstream[tstream_i];
465 if (cmdptr[1] ==
'"' && tstream_i + 1 <
lengthof(tokenstream)) {
466 tokenstream[tstream_i++] = *++cmdptr;
471 tokenstream[tstream_i++] = *cmdptr;
478 tokens[t_index++] = &tokenstream[tstream_i - 1];
485 for (uint i = 0; i <
lengthof(tokens) && tokens[i] !=
nullptr; i++) {
486 DEBUG(console, 8,
"Token %d is: '%s'", i, tokens[i]);
496 if (cmd !=
nullptr) {
500 if (!cmd->
proc(t_index, tokens)) {
501 cmd->
proc(0,
nullptr);
512 if (alias !=
nullptr) {
IConsoleCmd * next
next command in list
void IConsoleWarning(const char *string)
It is possible to print warnings to the console.
static const uint ICON_MAX_RECURSE
Maximum number of recursion.
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.
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...
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 IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
Execute a given command passed to us.
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 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 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.