Moved class Commands inside class Shell. Shell::loop() renamed in Shell::launch(). A Shell::launch() inside the shell setup function now causes a

ShellLaunchInSetupException
This commit is contained in:
Claudio Maggioni 2016-03-12 18:58:01 +01:00
parent 41f5acc37b
commit e9e95a519d
8 changed files with 45 additions and 44 deletions

View file

@ -11,6 +11,5 @@ SOURCES += main.cpp \
HEADERS += \ HEADERS += \
command.h \ command.h \
commands.h \
shell.h \ shell.h \
cmds.h cmds.h

View file

@ -1,4 +1,4 @@
#include "command.h" #include "shell.h"
Command::Command(const string& n, int (*funcptr)(const vector<string>* args)) : name(n) , funcCommand(funcptr){ Command::Command(const string& n, int (*funcptr)(const vector<string>* args)) : name(n) , funcCommand(funcptr){
checkObj(); checkObj();

View file

@ -2,6 +2,7 @@
#define COMMAND_H #define COMMAND_H
#include <string> #include <string>
#include <vector> #include <vector>
using std::string; using std::string;
using std::vector; using std::vector;
@ -11,12 +12,13 @@ class Command
static unsigned int numCom; static unsigned int numCom;
int (*funcCommand)(const vector<string>*); int (*funcCommand)(const vector<string>*);
void checkObj(); void checkObj();
public: public:
Command(const string& n, int (*funcptr)(const vector<string>* args)); Command(const string& n, int (*funcptr)(const vector<string>* args));
Command(const Command&); Command(const Command&);
~Command(); ~Command();
int execute(const vector<string>*);
string getName(); string getName();
int execute(const vector<string>*);
}; };
class CommandNameNotValidException{}; class CommandNameNotValidException{};

View file

@ -1,22 +1,20 @@
#include "commands.h" #include "shell.h"
using namespace Cmds; Shell::Commands::Commands()
Commands::Commands()
{ {
if(created) throw CommandsAlreadyCreatedException(); if(created) throw CommandsAlreadyCreatedException();
created=true; created=true;
} }
void Commands::add(Command* cmd){ void Shell::Commands::add(Command* cmd){
commands.push_back(cmd); commands.push_back(cmd);
} }
size_t Commands::howMany(){ size_t Shell::Commands::howMany(){
return commands.size(); return commands.size();
} }
int Commands::launch(const vector<string>* args){ int Shell::Commands::launch(const vector<string>* args){
for(unsigned int i=0; i<commands.size(); i++){ for(unsigned int i=0; i<commands.size(); i++){
if((commands[i]->getName())==args->operator [](0)){ if((commands[i]->getName())==args->operator [](0)){
return commands[i]->execute(args); return commands[i]->execute(args);
@ -25,6 +23,6 @@ int Commands::launch(const vector<string>* args){
throw CommandNotFoundException(); throw CommandNotFoundException();
} }
bool Commands::created = false; bool Shell::Commands::created = false;

View file

@ -1,25 +0,0 @@
#ifndef COMMANDS_H
#define COMMANDS_H
#include <string>
#include <vector>
#include <command.h>
#include "cmds.h"
using std::string;
using std::vector;
class Commands
{
vector<Command*> commands;
static bool created;
Commands(Commands&);
public:
Commands();
void add(Command*);
size_t howMany();
int launch(const vector<string>* args);
};
class CommandsAlreadyCreatedException{};
class CommandNotFoundException{};
#endif // COMMANDS_H

View file

@ -18,7 +18,7 @@ int main(int argc, char **argv)
mshConsoleTest.addCmd(new Command("cd", &cdExecute)); mshConsoleTest.addCmd(new Command("cd", &cdExecute));
mshConsoleTest.addCmd(new Command("exit", &exitExecute)); mshConsoleTest.addCmd(new Command("exit", &exitExecute));
mshConsoleTest.addCmd(new Command("help", &helpExecute)); mshConsoleTest.addCmd(new Command("help", &helpExecute));
mshConsoleTest.loop(); mshConsoleTest.launch();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View file

@ -25,12 +25,20 @@ Shell::Shell( string n, string ps,void (*s)(Shell*)) : cmds()
shellSetup = s; shellSetup = s;
name = n; name = n;
this->ps = ps; this->ps = ps;
notLoop = false;
} }
void Shell::loop(){ void Shell::launch(){
//launch setup //launch setup
if(shellSetup!=0) (*shellSetup)(this); if(notLoop) {
throw ShellLaunchInSetupException();
return;
}
if(shellSetup!=0) {
notLoop = true;
(*shellSetup)(this);
notLoop = false;
}
//launch loop //launch loop
string* line; string* line;
vector<string>* args; vector<string>* args;

27
shell.h
View file

@ -9,7 +9,7 @@
#include <cstring> #include <cstring>
#include <vector> #include <vector>
#include <csignal> #include <csignal>
#include <commands.h> #include "command.h"
using std::string; using std::string;
using std::cin; using std::cin;
using std::cout; using std::cout;
@ -21,30 +21,49 @@ using std::nullptr_t;
class Shell class Shell
{ {
class Commands
{
vector<Command*> commands;
static bool created;
Commands(Commands&);
public:
Commands();
void add(Command*);
size_t howMany();
int launch(const vector<string>* args);
};
static bool undoingLine; static bool undoingLine;
Commands cmds; Commands cmds;
string ps; string ps;
string name; string name;
void (*shellSetup)(Shell *); void (*shellSetup)(Shell *);
bool notLoop;
class IsUndoingLineException {};
int launch(vector<string>* args); int launch(vector<string>* args);
int execute(vector<string>* args); int execute(vector<string>* args);
static void EofHandler(int); static void EofHandler(int);
string* read_line(); string* read_line();
vector<string>* split_line(string* line); vector<string>* split_line(string* line);
class IsUndoingLineException {};
public: public:
Shell( string n="msh", string ps="MSH$",void (*s)(Shell*)=0); Shell( string n="msh", string ps="MSH$",void (*s)(Shell*)=0);
string getPs() const; string getPs() const;
void setPs(const string &value); void setPs(const string &value);
string getName() const; string getName() const;
void setName(const string &value); void setName(const string &value);
void loop(); void launch();
//for in-shell commands //for in-shell commands
void addCmd(Command *cmd); void addCmd(Command *cmd);
size_t howManyCmds(); size_t howManyCmds();
//exceptions
class CommandsAlreadyCreatedException{};
class CommandNotFoundException{};
class ShellLaunchInSetupException{};
}; };
#endif // SHELL_H #endif // SHELL_H