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:
parent
41f5acc37b
commit
e9e95a519d
8 changed files with 45 additions and 44 deletions
1
Msh.pro
1
Msh.pro
|
@ -11,6 +11,5 @@ SOURCES += main.cpp \
|
|||
|
||||
HEADERS += \
|
||||
command.h \
|
||||
commands.h \
|
||||
shell.h \
|
||||
cmds.h
|
||||
|
|
|
@ -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){
|
||||
checkObj();
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define COMMAND_H
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
|
@ -11,12 +12,13 @@ class Command
|
|||
static unsigned int numCom;
|
||||
int (*funcCommand)(const vector<string>*);
|
||||
void checkObj();
|
||||
|
||||
public:
|
||||
Command(const string& n, int (*funcptr)(const vector<string>* args));
|
||||
Command(const Command&);
|
||||
~Command();
|
||||
int execute(const vector<string>*);
|
||||
string getName();
|
||||
int execute(const vector<string>*);
|
||||
};
|
||||
|
||||
class CommandNameNotValidException{};
|
||||
|
|
14
commands.cpp
14
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<string>* args){
|
||||
int Shell::Commands::launch(const vector<string>* args){
|
||||
for(unsigned int i=0; i<commands.size(); i++){
|
||||
if((commands[i]->getName())==args->operator [](0)){
|
||||
return commands[i]->execute(args);
|
||||
|
@ -25,6 +23,6 @@ int Commands::launch(const vector<string>* args){
|
|||
throw CommandNotFoundException();
|
||||
}
|
||||
|
||||
bool Commands::created = false;
|
||||
bool Shell::Commands::created = false;
|
||||
|
||||
|
||||
|
|
25
commands.h
25
commands.h
|
@ -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
|
2
main.cpp
2
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;
|
||||
}
|
||||
|
||||
|
|
14
shell.cpp
14
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<string>* args;
|
||||
|
|
27
shell.h
27
shell.h
|
@ -9,7 +9,7 @@
|
|||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <csignal>
|
||||
#include <commands.h>
|
||||
#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<Command*> commands;
|
||||
static bool created;
|
||||
Commands(Commands&);
|
||||
public:
|
||||
Commands();
|
||||
void add(Command*);
|
||||
size_t howMany();
|
||||
int launch(const vector<string>* args);
|
||||
};
|
||||
|
||||
static bool undoingLine;
|
||||
Commands cmds;
|
||||
string ps;
|
||||
string name;
|
||||
void (*shellSetup)(Shell *);
|
||||
|
||||
class IsUndoingLineException {};
|
||||
bool notLoop;
|
||||
|
||||
int launch(vector<string>* args);
|
||||
int execute(vector<string>* args);
|
||||
static void EofHandler(int);
|
||||
string* read_line();
|
||||
vector<string>* 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
|
||||
|
|
Loading…
Reference in a new issue