# console.cpp File Reference

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "gui.h"`
`#include "textbuf_gui.h"`
`#include "window_gui.h"`
`#include "variables.h"`
`#include <stdarg.h>`
`#include <string.h>`
`#include "console.h"`
`#include "network/network.h"`
`#include "network/network_data.h"`
`#include "network/network_server.h"`
`#include "core/alloc_func.hpp"`
`#include "window_func.h"`
`#include "string_func.h"`
`#include "gfx_func.h"`
`#include "table/strings.h"`

Go to the source code of this file.

## Defines
#define | ICON_BUFFER 79
#define | ICON_HISTORY_SIZE 20
#define | ICON_LINE_HEIGHT 12
#define | ICON_RIGHT_BORDERWIDTH 10
#define | ICON_BOTTOM_BORDERWIDTH 12
#define | ICON_MAX_ALIAS_LINES 40
#define | ICON_TOKEN_COUNT 20
#define | IConsoleAddSorted(_base, item_new, IConsoleType, type)
| Perhaps ugly macro, but this saves us the trouble of writing the same function three types, just with different variables.
## Functions
static void | IConsoleClearCommand ()
static void | IConsoleResetHistoryPos ()
static void | IConsoleHistoryAdd (const char *cmd)
| Add the entered line into the history so you can look it back scroll, etc.
static void | IConsoleHistoryNavigate (int direction)
| Navigate Up/Down in the history of typed commands.
static void | IConsoleWndProc (Window *w, WindowEvent *e)
void | IConsoleInit ()
void | IConsoleClearBuffer ()
static void | IConsoleClear ()
static void | IConsoleWriteToLogFile (const char *string)
bool | CloseConsoleLogIfActive ()
void | IConsoleFree ()
void | IConsoleResize (Window *w)
void | IConsoleSwitch ()
void | IConsoleClose ()
void | IConsoleOpen ()
void | IConsolePrint (uint16 color_code, const char *string)
| Handle the printing of text entered into the console or redirected there by any other means.
void CDECL | IConsolePrintF (uint16 color_code, const char *s,...)
| Handle the printing of text entered into the console or redirected there by any other means.
void | IConsoleDebug (const char *dbg, const char *string)
| It is possible to print debugging information to the console, which is achieved by using this function.
void | IConsoleWarning (const char *string)
| It is possible to print warnings to the console.
void | IConsoleError (const char *string)
| It is possible to print error information to the console.
bool | GetArgumentInteger (uint32 *value, const char *arg)
| Change a string into its number representation.
static void | IConsoleHookAdd (IConsoleHooks *hooks, IConsoleHookTypes type, IConsoleHook *proc)
| General internal hooking code that is the same for both commands and variables.
static bool | IConsoleHookHandle (const IConsoleHooks *hooks, IConsoleHookTypes type)
| Handle any special hook triggers.
void | IConsoleCmdHookAdd (const char *name, IConsoleHookTypes type, IConsoleHook *proc)
| Add a hook to a command that will be triggered at certain points.
void | IConsoleVarHookAdd (const char *name, IConsoleHookTypes type, IConsoleHook *proc)
| Add a hook to a variable that will be triggered at certain points.
void | IConsoleCmdRegister (const char *name, IConsoleCmdProc *proc)
| Register a new command to be used in the console.
IConsoleCmd * | IConsoleCmdGet (const char *name)
| Find the command pointed to by its string.
void | IConsoleAliasRegister (const char *name, const char *cmd)
| Register a an alias for an already existing command in the console.
IConsoleAlias * | IConsoleAliasGet (const char *name)
| Find the alias pointed to by its string.
static int | IConsoleCopyInParams (char *dst, const char *src, uint bufpos)
| copy in an argument into the aliasstream
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.
void | IConsoleVarStringRegister (const char *name, void *addr, uint32 size, const char *help)
| Special function for adding string-type variables.
void | IConsoleVarRegister (const char *name, void *addr, IConsoleVarTypes type, const char *help)
| Register a new variable to be used in the console.
IConsoleVar * | IConsoleVarGet (const char *name)
| Find the variable pointed to by its string.
static void | IConsoleVarSetValue (const IConsoleVar *var, uint32 value)
| Set a new value to a console variable.
static void | IConsoleVarSetStringvalue (const IConsoleVar *var, const char *value)
| Set a new value to a string-type variable.
static uint32 | IConsoleVarGetValue (const IConsoleVar *var)
| Query the current value of a variable and return it.
static char * | IConsoleVarGetStringValue (const IConsoleVar *var)
| Get the value of the variable and put it into a printable string form so we can use it for printing.
void | IConsoleVarPrintGetValue (const IConsoleVar *var)
| Print out the value of the variable when asked.
void | IConsoleVarPrintSetValue (const IConsoleVar *var)
| Print out the value of the variable after it has been assigned a new value, thus giving us feedback on the action.
void | IConsoleVarExec (const IConsoleVar *var, byte tokencount, char *token[ICON_TOKEN_COUNT])
| Execute a variable command.
void | IConsoleVarProcAdd (const char *name, IConsoleCmdProc *proc)
| Add a callback function to the variable.
void | IConsoleCmdExec (const char *cmdstr)
| Execute a given command passed to us.
## Variables
IConsoleCmd * | _iconsole_cmds
| list of registred commands
IConsoleVar * | _iconsole_vars
| list of registred vars
IConsoleAlias * | _iconsole_aliases
| list of registred aliases
byte | _icolour_def
byte | _icolour_err
byte | _icolour_warn
byte | _icolour_dbg
byte | _icolour_cmd
IConsoleModes | _iconsole_mode
static char * | _iconsole_buffer [ICON_BUFFER+1]
static uint16 | _iconsole_cbuffer [ICON_BUFFER+1]
static Textbuf | _iconsole_cmdline
byte | _stdlib_developer = 1
bool | _stdlib_con_developer = false
FILE * | _iconsole_output_file
static char * | _iconsole_history [ICON_HISTORY_SIZE]
static byte | _iconsole_historypos
static const Widget | _iconsole_window_widgets []
static const WindowDesc | _iconsole_window_desc

## Detailed Description

Definition in file console.cpp.

## Define Documentation

#define IConsoleAddSorted | ( | _base,
|  | item_new,
|  | IConsoleType,
|  | type |  | ) |

Perhaps ugly macro, but this saves us the trouble of writing the same function three types, just with different variables.

Yes, templates would be handy. It was either this define or an even more ugly void* magic function

Definition at line 589 of file console.cpp.

Referenced by IConsoleAliasRegister(), IConsoleCmdRegister(), and IConsoleVarRegister().

## Function Documentation

bool GetArgumentInteger | ( | uint32 * | value,
|  | const char * | arg |
| ) |  |  |

Change a string into its number representation.

Supports decimal and hexadecimal numbers as well as 'on'/'off' 'true'/'false'

Parameters:

  | *value | the variable a successful conversion will be put in
  | *arg | the string to be converted

Returns:
  Return true on success or false on failure

Definition at line 484 of file console.cpp.

Referenced by IConsoleVarExec().

static void IConsoleAliasExec | ( | const IConsoleAlias * | alias,
|  | byte | tokencount,
|  | char * | tokens[ICON_TOKEN_COUNT] |
| ) |  |  | `[static]`

An alias is just another name for a command, or for more commands Execute it as well.

Parameters:

  | *alias | is the alias of the command
  | tokencount | the number of parameters passed
  | *tokens | are the parameters given to the original command (0 is the first param)

Definition at line 712 of file console.cpp.

References IConsoleAlias::cmdline, IConsoleCmdExec(), IConsoleCopyInParams(), IConsoleError(), IConsolePrintF(), and IConsoleAlias::name.

Referenced by IConsoleCmdExec().

IConsoleAlias* IConsoleAliasGet | ( | const char * | name | ) |

Find the alias pointed to by its string.

Parameters:

  | name | alias to be found

Returns:
  return Aliasstruct of the found alias, or NULL on failure

Definition at line 685 of file console.cpp.

References IConsoleAlias::name, and IConsoleAlias::next.

Referenced by IConsoleCmdExec().

void IConsoleAliasRegister | ( | const char * | name,
|  | const char * | cmd |
| ) |  |  |

Register a an alias for an already existing command in the console.

Parameters:

  | name | name of the alias that will be used
  | cmd | name of the command that 'name' will be alias of

Definition at line 667 of file console.cpp.

References IConsoleAlias::cmdline, IConsoleAddSorted, IConsoleAlias::name, and IConsoleAlias::next.

void IConsoleCmdExec | ( | const char * | cmdstr | ) |

Execute a given command passed to us.

First chop it up into individual tokens (seperated by spaces), then execute it if possible

Parameters:

  | cmdstr | string to be parsed and executed

Definition at line 1062 of file console.cpp.

References CS_ALPHANUMERAL, IConsoleVar::hook, IConsoleAliasExec(), IConsoleAliasGet(), IConsoleCmdGet(), IConsoleError(), IConsoleHookHandle(), IConsolePrintF(), IConsoleVarExec(), IConsoleVarGet(), and IsValidChar().

Referenced by IConsoleAliasExec().

IConsoleCmd* IConsoleCmdGet | ( | const char * | name | ) |

Find the command pointed to by its string.

Parameters:

  | name | command to be found

Returns:
  return Cmdstruct of the found command, or NULL on failure

Definition at line 652 of file console.cpp.

References _iconsole_cmds.

Referenced by IConsoleCmdExec(), and IConsoleCmdHookAdd().

void IConsoleCmdHookAdd | ( | const char * | name,
|  | IConsoleHookTypes | type,
|  | IConsoleHook * | proc |
| ) |  |  |

Add a hook to a command that will be triggered at certain points.

Parameters:

  | name | name of the command that the hook is added to
  | type | type of hook that is added (ACCESS, BEFORE and AFTER change)
  | proc | function called when the hook criteria is met

Definition at line 564 of file console.cpp.

References IConsoleCmdGet(), and IConsoleHookAdd().

void IConsoleCmdRegister | ( | const char * | name,
|  | IConsoleCmdProc * | proc |
| ) |  |  |

Register a new command to be used in the console.

Parameters:

  | name | name of the command that will be used
  | proc | function that will be called upon execution of command

Definition at line 631 of file console.cpp.

References _iconsole_cmds, and IConsoleAddSorted.

void IConsoleDebug | ( | const char * | dbg,
|  | const char * | string |
| ) |  |  |

It is possible to print debugging information to the console, which is achieved by using this function.

Can only be used by debug() in debug.cpp. You need at least a level 2 (developer) for debugging messages to show up

Parameters:

  | dbg | debugging category
  | string | debugging message

Definition at line 451 of file console.cpp.

References IConsolePrintF().

void IConsoleError | ( | const char * | string | ) |

It is possible to print error information to the console.

This can include game errors, or errors in general you would want the user to notice

Definition at line 472 of file console.cpp.

References IConsolePrintF().

Referenced by IConsoleAliasExec(), IConsoleCmdExec(), and IConsoleVarExec().

static void IConsoleHistoryAdd | ( | const char * | cmd | ) | `[static]`

Add the entered line into the history so you can look it back scroll, etc.

Put it to the beginning as it is the latest text

Parameters:

  | cmd | Text to be entered into the 'history'

Definition at line 342 of file console.cpp.

static void IConsoleHistoryNavigate | ( | int | direction | ) | `[static]`

Navigate Up/Down in the history of typed commands.

Parameters:

  | direction | Go further back in history (+1), go to recently typed commands (-1)

Definition at line 355 of file console.cpp.

References IsInsideMM(), and UpdateTextBufferSize().

static void IConsoleHookAdd | ( | IConsoleHooks * | hooks,
|  | IConsoleHookTypes | type,
|  | IConsoleHook * | proc |
| ) |  |  | `[static]`

General internal hooking code that is the same for both commands and variables.

Parameters:

  | hooks | IConsoleHooks structure that will be set according to
  | type | type access trigger
  | proc | function called when the hook criteria is met

Definition at line 511 of file console.cpp.

Referenced by IConsoleCmdHookAdd(), and IConsoleVarHookAdd().

static bool IConsoleHookHandle | ( | const IConsoleHooks * | hooks,
|  | IConsoleHookTypes | type |
| ) |  |  | `[static]`

Handle any special hook triggers.

If the hook type is met check if there is a function associated with that and if so, execute it

Parameters:

  | hooks | IConsoleHooks structure that will be checked
  | type | type of hook, trigger that needs to be activated

Returns:
  true on a successful execution of the hook command or if there is no hook/trigger present at all. False otherwise

Definition at line 537 of file console.cpp.

Referenced by IConsoleCmdExec(), IConsoleVarSetStringvalue(), and IConsoleVarSetValue().

void IConsolePrint | ( | uint16 | color_code,
|  | const char * | string |
| ) |  |  |

Handle the printing of text entered into the console or redirected there by any other means.

Text can be redirected to other players in a network game as well as to a logfile. If the network server is a dedicated server, all activities are also logged. All lines to print are added to a temporary buffer which can be used as a history to print them onscreen

Parameters:

  | color_code | the colour of the command. Red in case of errors, etc.
  | string | the message entered or output on the console (notice, error, etc.)

Definition at line 387 of file console.cpp.

References SetWindowDirty(), str_strip_colours(), and str_validate().

Referenced by IConsolePrintF().

void CDECL IConsolePrintF | ( | uint16 | color_code,
|  | const char * | s,
|  |  | ... |
| ) |  |  |

Handle the printing of text entered into the console or redirected there by any other means.

Uses printf() style format, for more information look at IConsolePrint()

Definition at line 431 of file console.cpp.

References buf, and IConsolePrint().

Referenced by IConsoleAliasExec(), IConsoleCmdExec(), IConsoleDebug(), IConsoleError(), IConsoleVarExec(), IConsoleVarPrintGetValue(), IConsoleVarPrintSetValue(), and IConsoleWarning().

void IConsoleVarExec | ( | const IConsoleVar * | var,
|  | byte | tokencount,
|  | char * | token[ICON_TOKEN_COUNT] |
| ) |  |  |

Execute a variable command.

Without any parameters, print out its value with parameters it assigns a new value to the variable

Parameters:

  | *var | the variable that we will be querying/changing
  | tokencount | how many additional parameters have been given to the commandline
  | *token | the actual parameters the variable was called with

Definition at line 996 of file console.cpp.

References GetArgumentInteger(), IConsoleError(), IConsolePrintF(), IConsoleVarGetValue(), IConsoleVarPrintGetValue(), IConsoleVarSetStringvalue(), IConsoleVarSetValue(), IConsoleVar::proc, and IConsoleVar::type.

Referenced by IConsoleCmdExec().

IConsoleVar* IConsoleVarGet | ( | const char * | name | ) |

Find the variable pointed to by its string.

Parameters:

  | name | variable to be found

Returns:
  return Varstruct of the found variable, or NULL on failure

Definition at line 831 of file console.cpp.

References IConsoleVar::name, and IConsoleVar::next.

Referenced by IConsoleCmdExec(), IConsoleVarHookAdd(), IConsoleVarProcAdd(), and IConsoleVarStringRegister().

static uint32 IConsoleVarGetValue | ( | const IConsoleVar * | var | ) | `[static]`

Query the current value of a variable and return it.

Parameters:

  | *var | the variable queried

Returns:
  current value of the variable

Definition at line 897 of file console.cpp.

References IConsoleVar::addr, and IConsoleVar::type.

Referenced by IConsoleVarExec().

void IConsoleVarHookAdd | ( | const char * | name,
|  | IConsoleHookTypes | type,
|  | IConsoleHook * | proc |
| ) |  |  |

Add a hook to a variable that will be triggered at certain points.

Parameters:

  | name | name of the variable that the hook is added to
  | type | type of hook that is added (ACCESS, BEFORE and AFTER change)
  | proc | function called when the hook criteria is met

Definition at line 577 of file console.cpp.

References IConsoleVar::hook, IConsoleHookAdd(), and IConsoleVarGet().

void IConsoleVarProcAdd | ( | const char * | name,
|  | IConsoleCmdProc * | proc |
| ) |  |  |

Add a callback function to the variable.

Some variables need very special processing, which can only be done with custom code

Parameters:

  | name | name of the variable the callback function is added to
  | proc | the function called

Definition at line 1050 of file console.cpp.

References IConsoleVarGet(), and IConsoleVar::proc.

void IConsoleVarRegister | ( | const char * | name,
|  | void * | addr,
|  | IConsoleVarTypes | type,
|  | const char * | help |
| ) |  |  |

Register a new variable to be used in the console.

Parameters:

  | name | name of the variable that will be used
  | addr | memory location the variable will point to
  | help | the help string shown for the variable
  | type | the type of the variable (simple atomic) so we know which values it can get

Definition at line 806 of file console.cpp.

References IConsoleVar::addr, IConsoleVar::help, IConsoleVar::hook, IConsoleAddSorted, IConsoleVar::name, IConsoleVar::next, IConsoleVar::proc, and IConsoleVar::type.

Referenced by IConsoleVarStringRegister().

static void IConsoleVarSetStringvalue | ( | const IConsoleVar * | var,
|  | const char * | value |
| ) |  |  | `[static]`

Set a new value to a string-type variable.

Basically this means to copy the new value over to the container.

Parameters:

  | *var | the variable in question
  | *value | the new value

Definition at line 881 of file console.cpp.

References IConsoleVar::addr, IConsoleVar::hook, IConsoleHookHandle(), IConsoleVarPrintSetValue(), IConsoleVar::size, and IConsoleVar::type.

Referenced by IConsoleVarExec().

static void IConsoleVarSetValue | ( | const IConsoleVar * | var,
|  | uint32 | value |
| ) |  |  | `[static]`

Set a new value to a console variable.

Parameters:

  | *var | the variable being set/changed
  | value | the new value given to the variable, cast properly

Definition at line 846 of file console.cpp.

References IConsoleVar::addr, IConsoleVar::hook, IConsoleHookHandle(), IConsoleVarPrintSetValue(), and IConsoleVar::type.

Referenced by IConsoleVarExec().

void IConsoleVarStringRegister | ( | const char * | name,
|  | void * | addr,
|  | uint32 | size,
|  | const char * | help |
| ) |  |  |

Special function for adding string-type variables.

They in addition also need a 'size' value saying how long their string buffer is.

Parameters:

  | name | name of the variable that will be used
  | addr | memory location the variable will point to
  | size | the length of the string buffer
  | help | the help string shown for the variable For more information see IConsoleVarRegister()

Definition at line 791 of file console.cpp.

References IConsoleVarGet(), IConsoleVarRegister(), and IConsoleVar::size.

void IConsoleWarning | ( | const char * | string | ) |

It is possible to print warnings to the console.

These are mostly errors or mishaps, but non-fatal. You need at least a level 1 (developer) for debugging messages to show up

Definition at line 462 of file console.cpp.

References IConsolePrintF().

## Variable Documentation

const WindowDesc _iconsole_window_desc`[static]`

Initial value:

     {
      0, 0, 2, 2, 2, 2,
      WC_CONSOLE, WC_NONE,
      WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
      _iconsole_window_widgets,
      IConsoleWndProc,
    }

Definition at line 216 of file console.cpp.

const Widget _iconsole_window_widgets[]`[static]`

Initial value:

     {
      {WIDGETS_END}
    }

Definition at line 212 of file console.cpp.