1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
8 /** @file console_internal.h Internally used functions for the console. */
9 
10 #ifndef CONSOLE_INTERNAL_H
11 #define CONSOLE_INTERNAL_H
12 
13 #include "gfx_type.h"
14 #include <map>
15 
16 static const uint ICON_CMDLN_SIZE     = 1024; ///< maximum length of a typed in command
17 static const uint ICON_MAX_STREAMSIZE = 2048; ///< maximum length of a totally expanded command
18 
19 /** Return values of console hooks (#IConsoleHook). */
20 enum ConsoleHookResult {
21 	CHR_ALLOW,    ///< Allow command execution.
22 	CHR_DISALLOW, ///< Disallow command execution.
23 	CHR_HIDE,     ///< Hide the existence of the command.
24 };
25 
26 /**
27  * --Commands--
28  * Commands are commands, or functions. They get executed once and any
29  * effect they produce are carried out. The arguments to the commands
30  * are given to them, each input word separated by a double-quote (") is an argument
31  * If you want to handle multiple words as one, enclose them in double-quotes
32  * eg. 'say "hello everybody"'
33  */
34 typedef bool IConsoleCmdProc(byte argc, char *argv[]);
35 typedef ConsoleHookResult IConsoleHook(bool echo);
36 struct IConsoleCmd {
IConsoleCmdIConsoleCmd37 	IConsoleCmd(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook) : name(name), proc(proc), hook(hook) {}
38 
39 	std::string name;         ///< name of command
40 	IConsoleCmdProc *proc;    ///< process executed when command is typed
41 	IConsoleHook *hook;       ///< any special trigger action that needs executing
42 };
43 
44 /**
45  * --Aliases--
46  * Aliases are like shortcuts for complex functions, variable assignments,
47  * etc. You can use a simple alias to rename a longer command (eg 'set' for
48  * 'setting' for example), or concatenate more commands into one
49  * (eg. 'ng' for 'load %A; unpause; debug_level 5'). Aliases can parse the arguments
50  * given to them in the command line.
51  * - "%A - %Z" substitute arguments 1 t/m 26
52  * - "%+" lists all parameters keeping them separated
53  * - "%!" also lists all parameters but presenting them to the aliased command as one argument
54  * - ";" allows for combining commands (see example 'ng')
55  */
56 struct IConsoleAlias {
IConsoleAliasIConsoleAlias57 	IConsoleAlias(const std::string &name, const std::string &cmdline) : name(name), cmdline(cmdline) {}
58 
59 	std::string name;           ///< name of the alias
60 	std::string cmdline;        ///< command(s) that is/are being aliased
61 };
62 
63 struct IConsole
64 {
65 	typedef std::map<std::string, IConsoleCmd> CommandList;
66 	typedef std::map<std::string, IConsoleAlias> AliasList;
67 
68 	/* console parser */
69 	static CommandList &Commands();
70 	static AliasList &Aliases();
71 
72 	/* Commands */
73 	static void CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook = nullptr);
74 	static IConsoleCmd *CmdGet(const std::string &name);
75 	static void AliasRegister(const std::string &name, const std::string &cmd);
76 	static IConsoleAlias *AliasGet(const std::string &name);
77 };
78 
79 /* console functions */
80 void IConsoleClearBuffer();
81 
82 /* console std lib (register ingame commands/aliases) */
83 void IConsoleStdLibRegister();
84 
85 /* Supporting functions */
86 bool GetArgumentInteger(uint32 *value, const char *arg);
87 
88 void IConsoleGUIInit();
89 void IConsoleGUIFree();
90 void IConsoleGUIPrint(TextColour colour_code, char *string);
91 
92 #endif /* CONSOLE_INTERNAL_H */
93