openMSX
GlobalCommandController.hh
Go to the documentation of this file.
1 #ifndef GLOBALCOMMANDCONTROLLER_HH
2 #define GLOBALCOMMANDCONTROLLER_HH
3 
4 #include "CommandController.hh"
5 #include "Command.hh"
6 #include "Interpreter.hh"
7 #include "InfoCommand.hh"
8 #include "InfoTopic.hh"
9 #include "HotKey.hh"
10 #include "SettingsConfig.hh"
11 #include "RomInfoTopic.hh"
12 #include "hash_map.hh"
13 #include "xxhash.hh"
14 #include <string>
15 #include <vector>
16 #include <memory>
17 
18 namespace openmsx {
19 
20 class EventDistributor;
21 class Reactor;
22 class GlobalCliComm;
23 class ProxyCmd;
24 class ProxySetting;
25 
27 {
28 protected:
30 
33 };
34 
36  , public CommandController
37 {
38 public:
41 
42  GlobalCommandController(EventDistributor& eventDistributor,
43  GlobalCliComm& cliComm,
44  Reactor& reactor);
46 
47  InfoCommand& getOpenMSXInfoCommand() { return openMSXInfoCommand; }
48 
52  void source(const std::string& script);
53 
54  void registerProxyCommand(const std::string& name);
55  void unregisterProxyCommand(std::string_view name);
56 
57  void registerProxySetting(Setting& setting);
58  void unregisterProxySetting(Setting& setting);
59 
60  // CommandController
61  void registerCompleter(CommandCompleter& completer,
62  std::string_view str) override;
63  void unregisterCompleter(CommandCompleter& completer,
64  std::string_view str) override;
65  void registerCommand(Command& command,
66  const std::string& str) override;
67  void unregisterCommand(Command& command,
68  std::string_view str) override;
69  bool hasCommand(std::string_view command) const override;
70  TclObject executeCommand(const std::string& command,
71  CliConnection* connection = nullptr) override;
72  void registerSetting(Setting& setting) override;
73  void unregisterSetting(Setting& setting) override;
74  CliComm& getCliComm() override;
75  Interpreter& getInterpreter() override;
76 
80  std::string tabCompletion(std::string_view command);
81 
86  bool isComplete(const std::string& command);
87 
88  SettingsConfig& getSettingsConfig() { return settingsConfig; }
89  SettingsManager& getSettingsManager() { return settingsConfig.getSettingsManager(); }
90  CliConnection* getConnection() const { return connection; }
91 
92 private:
93  void split(std::string_view str,
94  std::vector<std::string>& tokens, char delimiter);
95  std::string removeEscaping(const std::string& str);
96  std::vector<std::string> removeEscaping(
97  const std::vector<std::string>& input, bool keepLastIfEmpty);
98  std::string addEscaping(const std::string& str, bool quote, bool finished);
99 
100  void tabCompletion(std::vector<std::string>& tokens);
101 
102  using ProxySettings =
103  std::vector<std::pair<std::unique_ptr<ProxySetting>, unsigned>>;
104  ProxySettings::iterator findProxySetting(std::string_view name);
105 
106  GlobalCliComm& cliComm;
107  CliConnection* connection;
108 
109  Reactor& reactor;
110 
111  Interpreter interpreter;
112  InfoCommand openMSXInfoCommand;
113  HotKey hotKey;
114  SettingsConfig settingsConfig;
115 
116  struct HelpCmd final : Command {
117  explicit HelpCmd(GlobalCommandController& controller);
118  void execute(span<const TclObject> tokens, TclObject& result) override;
119  std::string help(const std::vector<std::string>& tokens) const override;
120  void tabCompletion(std::vector<std::string>& tokens) const override;
121  } helpCmd;
122 
123  struct TabCompletionCmd final : Command {
124  explicit TabCompletionCmd(GlobalCommandController& controller);
125  void execute(span<const TclObject> tokens, TclObject& result) override;
126  std::string help(const std::vector<std::string>& tokens) const override;
127  } tabCompletionCmd;
128 
129  struct UpdateCmd final : Command {
130  explicit UpdateCmd(CommandController& commandController);
131  void execute(span<const TclObject> tokens, TclObject& result) override;
132  std::string help(const std::vector<std::string>& tokens) const override;
133  void tabCompletion(std::vector<std::string>& tokens) const override;
134  private:
135  CliConnection& getConnection();
136  } updateCmd;
137 
138  struct PlatformInfo final : InfoTopic {
139  explicit PlatformInfo(InfoCommand& openMSXInfoCommand);
140  void execute(span<const TclObject> tokens,
141  TclObject& result) const override;
142  std::string help(const std::vector<std::string>& tokens) const override;
143  } platformInfo;
144 
145  struct VersionInfo final : InfoTopic {
146  explicit VersionInfo(InfoCommand& openMSXInfoCommand);
147  void execute(span<const TclObject> tokens,
148  TclObject& result) const override;
149  std::string help(const std::vector<std::string>& tokens) const override;
150  } versionInfo;
151 
152  RomInfoTopic romInfoTopic;
153 
154  struct NameFromProxy {
155  template<typename Pair>
156  const std::string& operator()(const Pair& p) const {
157  return p.second->getName();
158  }
159  };
160  hash_set<std::pair<unsigned, std::unique_ptr<ProxyCmd>>, NameFromProxy, XXHasher> proxyCommandMap;
161  ProxySettings proxySettings;
162 };
163 
164 } // namespace openmsx
165 
166 #endif
openmsx::GlobalCommandController::unregisterCompleter
void unregisterCompleter(CommandCompleter &completer, std::string_view str) override
Definition: GlobalCommandController.cc:153
openmsx::GlobalCommandController::operator=
GlobalCommandController & operator=(const GlobalCommandController &)=delete
openmsx::GlobalCommandController::unregisterProxyCommand
void unregisterProxyCommand(std::string_view name)
Definition: GlobalCommandController.cc:69
openmsx::GlobalCommandController::getSettingsManager
SettingsManager & getSettingsManager()
Definition: GlobalCommandController.hh:89
openmsx::GlobalCommandControllerBase::commands
hash_map< std::string, Command *, XXHasher > commands
Definition: GlobalCommandController.hh:31
openmsx::GlobalCommandController::getInterpreter
Interpreter & getInterpreter() override
Definition: GlobalCommandController.cc:123
xxhash.hh
openmsx::CommandController
Definition: CommandController.hh:18
openmsx::GlobalCommandController::unregisterCommand
void unregisterCommand(Command &command, std::string_view str) override
Definition: GlobalCommandController.cc:136
openmsx::GlobalCommandController::getCliComm
CliComm & getCliComm() override
Definition: GlobalCommandController.cc:118
openmsx::GlobalCommandController::registerCommand
void registerCommand(Command &command, const std::string &str) override
(Un)register a command
Definition: GlobalCommandController.cc:128
openmsx::GlobalCommandController::~GlobalCommandController
~GlobalCommandController()
openmsx::GlobalCommandControllerBase::~GlobalCommandControllerBase
~GlobalCommandControllerBase()
Definition: GlobalCommandController.cc:51
openmsx::GlobalCommandController::hasCommand
bool hasCommand(std::string_view command) const override
Does a command with this name already exist?
Definition: GlobalCommandController.cc:174
openmsx::GlobalCommandController::executeCommand
TclObject executeCommand(const std::string &command, CliConnection *connection=nullptr) override
Execute the given command.
Definition: GlobalCommandController.cc:301
openmsx::GlobalCommandController::tabCompletion
std::string tabCompletion(std::string_view command)
Complete the given command.
Definition: GlobalCommandController.cc:319
openmsx::GlobalCommandController::source
void source(const std::string &script)
Executes all defined auto commands.
Definition: GlobalCommandController.cc:308
XXHasher
Definition: xxhash.hh:153
Interpreter.hh
openmsx::GlobalCommandControllerBase
Definition: GlobalCommandController.hh:27
openmsx::Setting
Definition: Setting.hh:120
openmsx::EventDistributor
Definition: EventDistributor.hh:17
openmsx::CliConnection
Definition: CliConnection.hh:20
openmsx::GlobalCommandController::unregisterSetting
void unregisterSetting(Setting &setting) override
Definition: GlobalCommandController.cc:168
openmsx::SettingsConfig::getSettingsManager
SettingsManager & getSettingsManager()
Definition: SettingsConfig.hh:29
openmsx::GlobalCliComm
Definition: GlobalCliComm.hh:16
openmsx::GlobalCommandController::GlobalCommandController
GlobalCommandController(const GlobalCommandController &)=delete
span
Definition: span.hh:126
openmsx::Reactor
Contains the main loop of openMSX.
Definition: Reactor.hh:67
CommandController.hh
openmsx::SettingsConfig
Definition: SettingsConfig.hh:18
openmsx::CommandCompleter
Definition: Command.hh:20
SettingsConfig.hh
openmsx::InfoCommand
Definition: InfoCommand.hh:12
openmsx::GlobalCommandController::registerSetting
void registerSetting(Setting &setting) override
TODO.
Definition: GlobalCommandController.cc:162
InfoCommand.hh
openmsx::HotKey
Definition: HotKey.hh:22
hash_map.hh
HotKey.hh
Command.hh
openmsx::GlobalCommandController::isComplete
bool isComplete(const std::string &command)
Returns true iff the command is complete (all braces, quotes etc.
Definition: GlobalCommandController.cc:296
openmsx::GlobalCommandController::getOpenMSXInfoCommand
InfoCommand & getOpenMSXInfoCommand()
Definition: GlobalCommandController.hh:47
hash_set
Definition: hash_set.hh:266
InfoTopic.hh
openmsx::GlobalCommandController::registerProxyCommand
void registerProxyCommand(const std::string &name)
Definition: GlobalCommandController.cc:59
openmsx::Command
Definition: Command.hh:41
openmsx::CliComm
Definition: CliComm.hh:11
openmsx::TclObject
Definition: TclObject.hh:22
openmsx::GlobalCommandControllerBase::commandCompleters
hash_map< std::string, CommandCompleter *, XXHasher > commandCompleters
Definition: GlobalCommandController.hh:32
openmsx::CommandController::CommandController
CommandController()=default
openmsx::GlobalCommandController::unregisterProxySetting
void unregisterProxySetting(Setting &setting)
Definition: GlobalCommandController.cc:104
openmsx::Interpreter
Definition: Interpreter.hh:17
openmsx::GlobalCommandController
Definition: GlobalCommandController.hh:37
openmsx::GlobalCommandController::registerCompleter
void registerCompleter(CommandCompleter &completer, std::string_view str) override
(Un)register a command completer, used to complete build-in Tcl cmds
Definition: GlobalCommandController.cc:145
openmsx::SettingsManager
Manages all settings.
Definition: SettingsManager.hh:19
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
RomInfoTopic.hh
hash_map
Definition: hash_map.hh:32
openmsx::GlobalCommandController::getConnection
CliConnection * getConnection() const
Definition: GlobalCommandController.hh:90
openmsx::GlobalCommandController::getSettingsConfig
SettingsConfig & getSettingsConfig()
Definition: GlobalCommandController.hh:88
openmsx::GlobalCommandController::registerProxySetting
void registerProxySetting(Setting &setting)
Definition: GlobalCommandController.cc:88