Go to the documentation of this file.
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;
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;
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) {
IConsoleCmdProc * proc
process executed when command is typed
@ INVALID_CLIENT_ID
Client is not part of anything.
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...
char * cmdline
command(s) that is/are being aliased
IConsoleCmd * _iconsole_cmds
list of registered commands
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.
ClientSettings _settings_client
The current settings for this game.
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.
static const AdminIndex INVALID_ADMIN_ID
An invalid admin marker.
void NetworkAdminConsole(const char *origin, const char *string)
Send console to the admin network (if they did opt in for the respective update).
AdminIndex _redirect_console_to_admin
Redirection of the (remote) console to the admin.
static const TextColour CC_DEFAULT
Default colour of the console.
IConsoleAlias * IConsoleAliasGet(const char *name)
Find the alias pointed to by its string.
IConsoleHook * hook
any special trigger action that needs executing
IConsoleCmd * IConsoleCmdGet(const char *name)
Find the command pointed to by its string.
static const uint ICON_TOKEN_COUNT
Maximum number of tokens in one command.
void IConsoleAddSorted(T **base, T *item_new)
Add an item to an alphabetically sorted list.
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...
#define DEBUG(name, level,...)
Output a line of debugging information.
void IConsoleError(const char *string)
It is possible to print error information to the console.
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.
char * RemoveUnderscores(char *name)
Remove underscores from a string; the string will be modified!
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 TextColour CC_DEBUG
Colour for debug output.
uint8 developer
print non-fatal warnings in console (>= 1), copy debug output to console (== 2)
void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const char *string)
Send an rcon reply to the client.
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_MAX_RECURSE
Maximum number of recursion.
bool _network_dedicated
are we a dedicated server?
IConsoleAlias * _iconsole_aliases
list of registered aliases
void IConsolePrint(TextColour colour_code, const char *string)
Handle the printing of text entered into the console or redirected there by any other means.
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
char * name
name of the alias
IConsoleAlias * next
next alias in list
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.
–Aliases– Aliases are like shortcuts for complex functions, variable assignments, etc.
char * name
name of command
IConsoleCmd * next
next command in list
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.
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...
#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.
void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc, IConsoleHook *hook)
Register a new command to be used in the console.
void IConsoleAliasRegister(const char *name, const char *cmd)
Register a an alias for an already existing command in the console.
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 const TextColour CC_WARNING
Colour for warning lines.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
void IConsoleWarning(const char *string)
It is possible to print warnings to the console.
#define lastof(x)
Get the last element of an fixed size array.
ConsoleHookResult
Return values of console hooks (#IConsoleHook).
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.
GUISettings gui
settings related to the GUI
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const char *string)
Pass the rcon reply to the admin.