From 638c3ac10003f66ef4af43f50ee365c9036da0fe Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Sat, 24 Jun 2023 20:26:29 +1200 Subject: feat: Added ; as a command separator --- commands.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) (limited to 'commands.cpp') diff --git a/commands.cpp b/commands.cpp index de7d81e..af523ad 100644 --- a/commands.cpp +++ b/commands.cpp @@ -13,8 +13,14 @@ using std::cout, std::endl, std::string, std::vector, std::tolower; +const void CommandsModule::echo(const CommandArg* argv) +{ + cout << argv[0].str << endl; +} + CommandsModule::CommandsModule() { + addCommand("echo", &CommandsModule::echo, 1, {STR_REST}, this); } CommandsModule::~CommandsModule() { @@ -215,11 +221,36 @@ CommandArg* CommandsModule::getCommandArgs(vector& split, const CommandA void CommandsModule::runCommand(string command) { vector split = splitCommand(command); + vector::const_iterator start = split.begin(); + int count = 0; + for(string s : split) + { + if(s == ";") + { + vector::const_iterator end = start + count; + vector partialCmd(start, end); + runCommand(partialCmd); + count = 0; + start = end + 1; + } + else + { + count++; + } + } + if(start != split.end()) + { + vector partialCmd(start, (vector::const_iterator)split.end()); + runCommand(partialCmd); + } +} +void CommandsModule::runCommand(vector split) +{ Command* cmd = lookupCommand(split[0]); if(cmd == nullptr) throw Err(CMD_ERR_NOT_FOUND, split[0] + " is not a valid command name"); if(cmd->argc > split.size() - 1) - throw Err(CMD_ERR_WRONG_ARGS, command + " is the wrong args"); + throw Err(CMD_ERR_WRONG_ARGS, "wrong number of arguments"); CommandArg* args; try { @@ -254,14 +285,42 @@ void CommandsModule::runCommand(string command) delete[] args; } -Err CommandsModule::checkCommand(string command) +vector CommandsModule::checkCommand(string command) { + vector errs; vector split = splitCommand(command); + vector::const_iterator start = split.begin(); + int count = 0; + for(string s : split) + { + if(s == ";") + { + vector::const_iterator end = start + count; + vector partialCmd(start, end); + errs.push_back(checkCommand(partialCmd)); + count = 0; + start = end + 1; + } + else + { + count++; + } + } + if(start != split.end()) + { + vector partialCmd(start, (vector::const_iterator)split.end()); + errs.push_back(checkCommand(partialCmd)); + } + return errs; +} + +Err CommandsModule::checkCommand(vector split) +{ Command* cmd = lookupCommand(split[0]); if(cmd == nullptr) return Err(CMD_ERR_NOT_FOUND, split[0] + " is not a valid command name"); if(cmd->argc > split.size()) - return Err(CMD_ERR_WRONG_ARGS, command + " is the wrong args"); + return Err(CMD_ERR_WRONG_ARGS, "wrong number of arguments"); CommandArg* args; try { -- cgit v1.2.3