summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin Frank <justinpfrank@protonmail.com>2019-03-12 10:34:30 -0700
committerJustin Frank <justinpfrank@protonmail.com>2019-04-08 17:02:44 -0700
commit6092852640096c777f700cf669666504b10e2a58 (patch)
tree4d609cd77a8363c10e7286a6a46bf7a3aa251a26 /src
parent670f8192c8d68c33e42b95847edd0678f87ca39b (diff)
Added 'provide-module' and 'require-module' commands
Diffstat (limited to 'src')
-rw-r--r--src/command_manager.cc27
-rw-r--r--src/command_manager.hh14
-rw-r--r--src/commands.cc12
3 files changed, 52 insertions, 1 deletions
diff --git a/src/command_manager.cc b/src/command_manager.cc
index 5e8c8196..91c41bd3 100644
--- a/src/command_manager.cc
+++ b/src/command_manager.cc
@@ -40,6 +40,33 @@ void CommandManager::register_command(String command_name,
std::move(completer) };
}
+bool CommandManager::module_defined(StringView module_name) const
+{
+ return m_modules.find(module_name) != m_modules.end();
+}
+
+void CommandManager::register_module(String module_name, String commands)
+{
+ auto module = m_modules.find(module_name);
+ if (module != m_modules.end() and module->value.loaded)
+ throw runtime_error{format("module already loaded: '{}'", module_name)};
+
+ m_modules[module_name] = { false, std::move(commands) };
+}
+
+void CommandManager::load_module(StringView module_name, Context& context)
+{
+ auto module = m_modules.find(module_name);
+ if (module == m_modules.end())
+ throw runtime_error{format("no such module: '{}'", module_name)};
+ if (module->value.loaded)
+ return;
+
+ module->value.loaded = true;
+ execute(module->value.commands, context);
+ module->value.commands.clear();
+}
+
struct parse_error : runtime_error
{
parse_error(StringView error)
diff --git a/src/command_manager.hh b/src/command_manager.hh
index 0468a1fc..329c24f2 100644
--- a/src/command_manager.hh
+++ b/src/command_manager.hh
@@ -123,6 +123,12 @@ public:
void clear_last_complete_command() { m_last_complete_command = String{}; }
+ bool module_defined(StringView module_name) const;
+
+ void register_module(String module_name, String commands);
+
+ void load_module(StringView module_name, Context& context);
+
private:
void execute_single_command(CommandParameters params,
Context& context,
@@ -143,6 +149,14 @@ private:
String m_last_complete_command;
int m_command_depth = 0;
+ struct Module
+ {
+ bool loaded;
+ String commands;
+ };
+ using ModuleMap = HashMap<String, Module, MemoryDomain::Commands>;
+ ModuleMap m_modules;
+
CommandMap::const_iterator find_command(const Context& context,
StringView name) const;
};
diff --git a/src/commands.cc b/src/commands.cc
index 3c50accb..264ad46c 100644
--- a/src/commands.cc
+++ b/src/commands.cc
@@ -2417,8 +2417,17 @@ const CommandDesc provide_module_cmd = {
CommandFlags::None,
CommandHelper{},
CommandCompleter{},
- [](const ParametersParser& parse, Context& context, const ShellContext&)
+ [](const ParametersParser& parser, Context& context, const ShellContext&)
{
+ const String& module_name = parser[0];
+ auto& cm = CommandManager::instance();
+
+ if (not all_of(module_name, is_identifier))
+ throw runtime_error(format("invalid module name: '{}'", module_name));
+
+ if (cm.module_defined(module_name) and not parser.get_switch("override"))
+ throw runtime_error(format("module '{}' already defined", module_name));
+ cm.register_module(module_name, parser[1]);
}
};
@@ -2432,6 +2441,7 @@ const CommandDesc require_module_cmd = {
CommandCompleter{},
[](const ParametersParser& parser, Context& context, const ShellContext&)
{
+ CommandManager::instance().load_module(parser[0], context);
}
};