From e9e95a519dcd30b6b90c23dd241e63e22dac0133 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Sat, 12 Mar 2016 18:58:01 +0100 Subject: [PATCH] Moved class Commands inside class Shell. Shell::loop() renamed in Shell::launch(). A Shell::launch() inside the shell setup function now causes a ShellLaunchInSetupException --- Msh.pro | 1 - command.cpp | 2 +- command.h | 4 +++- commands.cpp | 14 ++++++-------- commands.h | 25 ------------------------- main.cpp | 2 +- shell.cpp | 14 +++++++++++--- shell.h | 27 +++++++++++++++++++++++---- 8 files changed, 45 insertions(+), 44 deletions(-) delete mode 100644 commands.h diff --git a/Msh.pro b/Msh.pro index 765a61d..82fe28e 100644 --- a/Msh.pro +++ b/Msh.pro @@ -11,6 +11,5 @@ SOURCES += main.cpp \ HEADERS += \ command.h \ - commands.h \ shell.h \ cmds.h diff --git a/command.cpp b/command.cpp index f3f66b1..21751cf 100644 --- a/command.cpp +++ b/command.cpp @@ -1,4 +1,4 @@ -#include "command.h" +#include "shell.h" Command::Command(const string& n, int (*funcptr)(const vector* args)) : name(n) , funcCommand(funcptr){ checkObj(); diff --git a/command.h b/command.h index 16735a3..e4ccb84 100644 --- a/command.h +++ b/command.h @@ -2,6 +2,7 @@ #define COMMAND_H #include #include + using std::string; using std::vector; @@ -11,12 +12,13 @@ class Command static unsigned int numCom; int (*funcCommand)(const vector*); void checkObj(); + public: Command(const string& n, int (*funcptr)(const vector* args)); Command(const Command&); ~Command(); - int execute(const vector*); string getName(); + int execute(const vector*); }; class CommandNameNotValidException{}; diff --git a/commands.cpp b/commands.cpp index 76b29f9..f91d229 100644 --- a/commands.cpp +++ b/commands.cpp @@ -1,22 +1,20 @@ -#include "commands.h" +#include "shell.h" -using namespace Cmds; - -Commands::Commands() +Shell::Commands::Commands() { if(created) throw CommandsAlreadyCreatedException(); created=true; } -void Commands::add(Command* cmd){ +void Shell::Commands::add(Command* cmd){ commands.push_back(cmd); } -size_t Commands::howMany(){ +size_t Shell::Commands::howMany(){ return commands.size(); } -int Commands::launch(const vector* args){ +int Shell::Commands::launch(const vector* args){ for(unsigned int i=0; igetName())==args->operator [](0)){ return commands[i]->execute(args); @@ -25,6 +23,6 @@ int Commands::launch(const vector* args){ throw CommandNotFoundException(); } -bool Commands::created = false; +bool Shell::Commands::created = false; diff --git a/commands.h b/commands.h deleted file mode 100644 index a6a8080..0000000 --- a/commands.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef COMMANDS_H -#define COMMANDS_H -#include -#include -#include -#include "cmds.h" -using std::string; -using std::vector; - -class Commands -{ - vector commands; - static bool created; - Commands(Commands&); -public: - Commands(); - void add(Command*); - size_t howMany(); - int launch(const vector* args); -}; - -class CommandsAlreadyCreatedException{}; -class CommandNotFoundException{}; - -#endif // COMMANDS_H diff --git a/main.cpp b/main.cpp index e976aae..20c9752 100644 --- a/main.cpp +++ b/main.cpp @@ -18,7 +18,7 @@ int main(int argc, char **argv) mshConsoleTest.addCmd(new Command("cd", &cdExecute)); mshConsoleTest.addCmd(new Command("exit", &exitExecute)); mshConsoleTest.addCmd(new Command("help", &helpExecute)); - mshConsoleTest.loop(); + mshConsoleTest.launch(); return EXIT_SUCCESS; } diff --git a/shell.cpp b/shell.cpp index 712b56c..726aef8 100644 --- a/shell.cpp +++ b/shell.cpp @@ -25,12 +25,20 @@ Shell::Shell( string n, string ps,void (*s)(Shell*)) : cmds() shellSetup = s; name = n; this->ps = ps; + notLoop = false; } -void Shell::loop(){ +void Shell::launch(){ //launch setup - if(shellSetup!=0) (*shellSetup)(this); - + if(notLoop) { + throw ShellLaunchInSetupException(); + return; + } + if(shellSetup!=0) { + notLoop = true; + (*shellSetup)(this); + notLoop = false; + } //launch loop string* line; vector* args; diff --git a/shell.h b/shell.h index f8d5168..188c6b7 100644 --- a/shell.h +++ b/shell.h @@ -9,7 +9,7 @@ #include #include #include -#include +#include "command.h" using std::string; using std::cin; using std::cout; @@ -21,30 +21,49 @@ using std::nullptr_t; class Shell { + class Commands + { + vector commands; + static bool created; + Commands(Commands&); + public: + Commands(); + void add(Command*); + size_t howMany(); + int launch(const vector* args); + }; + static bool undoingLine; Commands cmds; string ps; string name; void (*shellSetup)(Shell *); - - class IsUndoingLineException {}; + bool notLoop; int launch(vector* args); int execute(vector* args); static void EofHandler(int); string* read_line(); vector* split_line(string* line); + + class IsUndoingLineException {}; + public: Shell( string n="msh", string ps="MSH$",void (*s)(Shell*)=0); string getPs() const; void setPs(const string &value); string getName() const; void setName(const string &value); - void loop(); + void launch(); //for in-shell commands void addCmd(Command *cmd); size_t howManyCmds(); + + //exceptions + class CommandsAlreadyCreatedException{}; + class CommandNotFoundException{}; + class ShellLaunchInSetupException{}; }; #endif // SHELL_H