31 FILE *_iconsole_output_file;
35 _iconsole_output_file = NULL;
43 IConsoleStdLibRegister();
46 static void IConsoleWriteToLogFile(
const char *
string)
48 if (_iconsole_output_file != NULL) {
51 if ((strlen(header) != 0 && fwrite(header, strlen(header), 1, _iconsole_output_file) != 1) ||
52 fwrite(
string, strlen(
string), 1, _iconsole_output_file) != 1 ||
53 fwrite(
"\n", 1, 1, _iconsole_output_file) != 1) {
54 fclose(_iconsole_output_file);
55 _iconsole_output_file = NULL;
61 bool CloseConsoleLogIfActive()
63 if (_iconsole_output_file != NULL) {
65 fclose(_iconsole_output_file);
66 _iconsole_output_file = NULL;
76 CloseConsoleLogIfActive();
113 #ifdef ENABLE_NETWORK 118 IConsoleWriteToLogFile(str);
123 IConsoleWriteToLogFile(str);
139 va_start(va, format);
191 if (strcmp(arg,
"on") == 0 || strcmp(arg,
"true") == 0) {
195 if (strcmp(arg,
"off") == 0 || strcmp(arg,
"false") == 0) {
200 *value = strtoul(arg, &endptr, 0);
201 return arg != endptr;
217 T *item_before = NULL;
220 while (item != NULL) {
221 if (strcmp(item->name, item_new->name) > 0)
break;
227 if (item_before == NULL) {
230 item_before->next = item_new;
233 item_new->next = item;
244 for (
const char *p = name; *p !=
'\0'; p++) {
245 if (*p !=
'_') *q++ = *p;
260 item_new->
next = NULL;
261 item_new->
proc = proc;
262 item_new->
hook = hook;
276 for (item = _iconsole_cmds; item != NULL; item = item->
next) {
277 if (strcmp(item->
name, name) == 0)
return item;
290 IConsoleError(
"an alias with this name already exists; insertion aborted");
295 char *cmd_aliased =
stredup(cmd);
298 item_new->
next = NULL;
299 item_new->
cmdline = cmd_aliased;
300 item_new->
name = new_alias;
314 for (item = _iconsole_aliases; item != NULL; item = item->
next) {
315 if (strcmp(item->
name, name) == 0)
return item;
330 char *alias_stream = alias_buffer;
332 DEBUG(console, 6,
"Requested command is an alias; parsing...");
334 for (
const char *cmdptr = alias->
cmdline; *cmdptr !=
'\0'; cmdptr++) {
337 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
343 alias_stream = alias_buffer;
344 *alias_stream =
'\0';
353 for (uint i = 0; i != tokencount; i++) {
354 if (i != 0) alias_stream =
strecpy(alias_stream,
" ",
lastof(alias_buffer));
355 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
356 alias_stream =
strecpy(alias_stream, tokens[i],
lastof(alias_buffer));
357 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
363 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
364 for (uint i = 0; i != tokencount; i++) {
365 if (i != 0) alias_stream =
strecpy(alias_stream,
" ",
lastof(alias_buffer));
366 alias_stream =
strecpy(alias_stream, tokens[i],
lastof(alias_buffer));
368 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
373 int param = *cmdptr -
'A';
375 if (param < 0 || param >= tokencount) {
376 IConsoleError(
"too many or wrong amount of parameters passed to alias, aborting");
381 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
382 alias_stream =
strecpy(alias_stream, tokens[param],
lastof(alias_buffer));
383 alias_stream =
strecpy(alias_stream,
"\"",
lastof(alias_buffer));
390 *alias_stream++ = *cmdptr;
391 *alias_stream =
'\0';
395 if (alias_stream >=
lastof(alias_buffer) - 1) {
396 IConsoleError(
"Requested alias execution would overflow execution buffer");
413 uint t_index, tstream_i;
415 bool longtoken =
false;
416 bool foundtoken =
false;
418 if (cmdstr[0] ==
'#')
return;
420 for (cmdptr = cmdstr; *cmdptr !=
'\0'; cmdptr++) {
422 IConsoleError(
"command contains malformed characters, aborting");
428 DEBUG(console, 4,
"Executing cmdline: '%s'", cmdstr);
430 memset(&tokens, 0,
sizeof(tokens));
431 memset(&tokenstream, 0,
sizeof(tokenstream));
436 for (cmdptr = cmdstr, t_index = 0, tstream_i = 0; *cmdptr !=
'\0'; cmdptr++) {
437 if (tstream_i >=
lengthof(tokenstream)) {
444 if (!foundtoken)
break;
447 tokenstream[tstream_i] = *cmdptr;
449 tokenstream[tstream_i] =
'\0';
456 longtoken = !longtoken;
462 tokens[t_index++] = &tokenstream[tstream_i];
467 if (cmdptr[1] ==
'"' && tstream_i + 1 <
lengthof(tokenstream)) {
468 tokenstream[tstream_i++] = *++cmdptr;
473 tokenstream[tstream_i++] = *cmdptr;
480 tokens[t_index++] = &tokenstream[tstream_i - 1];
487 for (uint i = 0; i <
lengthof(tokens) && tokens[i] != NULL; i++) {
488 DEBUG(console, 8,
"Token %d is: '%s'", i, tokens[i]);
502 if (!cmd->
proc(t_index, tokens)) {
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.
Client is not part of anything.
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.
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.