Moved snippet in msh-console-snippet repository. All the code in the library is in the namespace mshconsole. Added CommandExecutor::exit() for exiting from the
shell.
This commit is contained in:
parent
e96a300f8e
commit
854b218cb4
14 changed files with 309 additions and 387 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,6 @@
|
||||||
|
# autosaves
|
||||||
|
*.autosave
|
||||||
|
|
||||||
#kate directory files
|
#kate directory files
|
||||||
*.directory
|
*.directory
|
||||||
|
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
project(msh-console-test)
|
|
||||||
cmake_minimum_required(VERSION 2.8)
|
|
||||||
aux_source_directory(. SRC_LIST)
|
|
||||||
|
|
||||||
# Recurse into the subdirectories. This does not actually
|
|
||||||
# cause another cmake executable to run. The same process will walk through
|
|
||||||
# the project's entire directory structure.
|
|
||||||
add_subdirectory (msh-console-library)
|
|
||||||
add_subdirectory (snippet)
|
|
|
@ -6,8 +6,4 @@ cmake_minimum_required(VERSION 2.8)
|
||||||
# Create a library which includes the source listed.
|
# Create a library which includes the source listed.
|
||||||
# The extension is already found. Any number of sources could be listed here.
|
# The extension is already found. Any number of sources could be listed here.
|
||||||
SET(CMAKE_CXX_FLAGS "-std=c++11")
|
SET(CMAKE_CXX_FLAGS "-std=c++11")
|
||||||
add_library (msh-console command.cpp commandexecutor.cpp commandexecutor.h command.h commands.cpp shell.cpp shell.h)
|
add_library (mshconsole SHARED command.cpp commandexecutor.cpp commandexecutor.h command.h commands.cpp shell.cpp shell.h)
|
||||||
|
|
||||||
# Make sure the compiler can find include files for the library
|
|
||||||
# when other libraries or executables link to this library
|
|
||||||
target_include_directories (msh-console PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "shell.h"
|
#include "shell.h"
|
||||||
|
namespace mshconsole {
|
||||||
|
|
||||||
Command::Command(const string& n, int (*funcptr)(const vector<string>* args, CommandExecutor*)) : name(n) , funcCommand(funcptr){
|
Command::Command(const string& n, int (*funcptr)(const vector<string>* args, CommandExecutor*)) : name(n) , funcCommand(funcptr){
|
||||||
checkObj();
|
checkObj();
|
||||||
|
@ -32,3 +33,4 @@ Command::~Command(){
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int Command::numCom = 0;
|
unsigned int Command::numCom = 0;
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::vector;
|
using std::vector;
|
||||||
|
|
||||||
|
namespace mshconsole{
|
||||||
class Command
|
class Command
|
||||||
{
|
{
|
||||||
const string name;
|
const string name;
|
||||||
|
@ -22,7 +23,7 @@ public:
|
||||||
string getName();
|
string getName();
|
||||||
int execute(const vector<string>*, CommandExecutor* ciao);
|
int execute(const vector<string>*, CommandExecutor* ciao);
|
||||||
};
|
};
|
||||||
|
}
|
||||||
class CommandNameNotValidException{};
|
class CommandNameNotValidException{};
|
||||||
|
|
||||||
#endif // COMMAND_H
|
#endif // COMMAND_H
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "commandexecutor.h"
|
#include "commandexecutor.h"
|
||||||
|
namespace mshconsole {
|
||||||
CommandExecutor::CommandExecutor()
|
CommandExecutor::CommandExecutor() {}
|
||||||
{
|
void CommandExecutor::exit(){
|
||||||
|
throw CommandExecutor::ExitException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,13 +3,18 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
namespace mshconsole{
|
||||||
class CommandExecutor
|
class CommandExecutor
|
||||||
{
|
{
|
||||||
|
protected:
|
||||||
|
class ExitException {};
|
||||||
public:
|
public:
|
||||||
virtual int executeCmd(std::vector<std::string>* args) = 0;
|
virtual int executeCmd(std::vector<std::string>* args) = 0;
|
||||||
virtual int executeCmd(const std::string& args) = 0;
|
virtual int executeCmd(const std::string& args) = 0;
|
||||||
virtual size_t howManyCmds() const = 0;
|
virtual size_t howManyCmds() const = 0;
|
||||||
|
void exit();
|
||||||
CommandExecutor();
|
CommandExecutor();
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#endif // COMMANDEXECUTOR_H
|
#endif // COMMANDEXECUTOR_H
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#include "shell.h"
|
#include "shell.h"
|
||||||
|
namespace mshconsole {
|
||||||
Shell::Commands::Commands(Shell* s) : commands(), threadCommands()
|
Shell::Commands::Commands(Shell* s) : commands(), threadCommands()
|
||||||
{
|
{
|
||||||
parent = s;
|
parent = s;
|
||||||
|
@ -22,5 +22,6 @@ int Shell::Commands::launch(const vector<string>* args, bool launchThread){
|
||||||
}
|
}
|
||||||
throw CommandNotFoundException();
|
throw CommandNotFoundException();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#include "shell.h"
|
#include "shell.h"
|
||||||
|
namespace mshconsole {
|
||||||
string Shell::getPs() const
|
string Shell::getPs() const
|
||||||
{
|
{
|
||||||
return ps;
|
return ps;
|
||||||
|
@ -30,9 +30,9 @@ Shell::Shell( string n, string ps,void (*s)(Shell*), void (*pss)(Shell*)) : cmds
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shell::launch(){
|
void Shell::launch(){
|
||||||
|
|
||||||
//launch setup
|
//launch setup
|
||||||
if(notLoop) {
|
if(notLoop) {
|
||||||
throw ShellLaunchInSetupException();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(shellSetup!=0) {
|
if(shellSetup!=0) {
|
||||||
|
@ -40,10 +40,12 @@ void Shell::launch(){
|
||||||
(*shellSetup)(this);
|
(*shellSetup)(this);
|
||||||
notLoop = false;
|
notLoop = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//launch loop
|
//launch loop
|
||||||
string* line;
|
string* line;
|
||||||
vector<string>* args;
|
vector<string>* args;
|
||||||
int status;
|
int status;
|
||||||
|
try{
|
||||||
do {
|
do {
|
||||||
bool readSuccess;
|
bool readSuccess;
|
||||||
do{
|
do{
|
||||||
|
@ -60,7 +62,10 @@ void Shell::launch(){
|
||||||
args = split_line(line);
|
args = split_line(line);
|
||||||
status = executeCmd(args);
|
status = executeCmd(args);
|
||||||
delete args;
|
delete args;
|
||||||
} while (status);
|
} while (1);
|
||||||
|
} catch(CommandExecutor::ExitException) {}
|
||||||
|
|
||||||
|
//launch postSetup
|
||||||
if(shellPostSetup!=0) {
|
if(shellPostSetup!=0) {
|
||||||
shellPostSetup(this);
|
shellPostSetup(this);
|
||||||
}
|
}
|
||||||
|
@ -68,6 +73,7 @@ void Shell::launch(){
|
||||||
|
|
||||||
int Shell::launchCmd(vector<string>* args)
|
int Shell::launchCmd(vector<string>* args)
|
||||||
{
|
{
|
||||||
|
using std::exit;
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
|
@ -194,3 +200,4 @@ void Shell::addCmd(Command* cmd, bool isthread){
|
||||||
size_t Shell::howManyCmds() const{
|
size_t Shell::howManyCmds() const{
|
||||||
return cmds.howMany();
|
return cmds.howMany();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -21,6 +21,8 @@ using std::vector;
|
||||||
using std::istringstream;
|
using std::istringstream;
|
||||||
using std::nullptr_t;
|
using std::nullptr_t;
|
||||||
|
|
||||||
|
namespace mshconsole {
|
||||||
|
|
||||||
class Shell : public CommandExecutor
|
class Shell : public CommandExecutor
|
||||||
{
|
{
|
||||||
class Commands
|
class Commands
|
||||||
|
@ -49,6 +51,7 @@ class Shell : public CommandExecutor
|
||||||
vector<string>* split_line(const string* line);
|
vector<string>* split_line(const string* line);
|
||||||
|
|
||||||
class IsUndoingLineException {};
|
class IsUndoingLineException {};
|
||||||
|
class CommandNotFoundException {};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Shell(string n="msh", string ps="MSH$", void (*s)(Shell*)=0, void (*pss)(Shell*)=0);
|
Shell(string n="msh", string ps="MSH$", void (*s)(Shell*)=0, void (*pss)(Shell*)=0);
|
||||||
|
@ -61,15 +64,13 @@ public:
|
||||||
void (*getShellSetup())(Shell*);
|
void (*getShellSetup())(Shell*);
|
||||||
void setShellPostSetup(void (*)(Shell *));
|
void setShellPostSetup(void (*)(Shell *));
|
||||||
void (*getShellPostSetup())(Shell*);
|
void (*getShellPostSetup())(Shell*);
|
||||||
|
|
||||||
//for in-shell commands
|
//for in-shell commands
|
||||||
void addCmd(Command *cmd, bool isThread=false);
|
void addCmd(Command *cmd, bool isThread=false);
|
||||||
size_t howManyCmds() const;
|
size_t howManyCmds() const;
|
||||||
int executeCmd(vector<string>* args);
|
int executeCmd(vector<string>* args);
|
||||||
int executeCmd(const string& args);
|
int executeCmd(const string& args);
|
||||||
|
|
||||||
//excepsetions
|
|
||||||
class CommandNotFoundException{};
|
|
||||||
class ShellLaunchInSetupException{};
|
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#endif // SHELL_H
|
#endif // SHELL_H
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
# snippet for showing how the library works
|
|
||||||
|
|
||||||
project(msh-console-library-snippet)
|
|
||||||
cmake_minimum_required(VERSION 2.8)
|
|
||||||
|
|
||||||
SET(CMAKE_CXX_FLAGS "-std=c++11")
|
|
||||||
add_executable (../../build-msh-console/msh-console-test main.cpp cmds.cpp)
|
|
||||||
target_link_libraries (../../build-msh-console/msh-console-test LINK_PUBLIC msh-console)
|
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
/**
|
|
||||||
builtin commands
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "cmds.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
int Cmds::cdExecute(const vector<string>* args, CommandExecutor*){
|
|
||||||
if (args->operator[](1) == "\0") {
|
|
||||||
cerr << "expected argument to \"cd\"\n";
|
|
||||||
} else {
|
|
||||||
if (chdir(args->operator[](1).c_str()) != 0) {
|
|
||||||
cerr << "error";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Cmds::exitExecute(const vector<string>*, CommandExecutor*){
|
|
||||||
std::exit(EXIT_SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
int Cmds::helpExecute(const vector<string>*, CommandExecutor*){
|
|
||||||
cout << " info" << endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
#ifndef CMDS_H
|
|
||||||
#define CMDS_H
|
|
||||||
#include "../msh-console-library/commandexecutor.h"
|
|
||||||
#include <iostream>
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
#include <unistd.h>
|
|
||||||
using std::string;
|
|
||||||
using std::vector;
|
|
||||||
|
|
||||||
namespace Cmds {
|
|
||||||
int cdExecute(const vector<string>* args, CommandExecutor*);
|
|
||||||
int exitExecute(const vector<string>*, CommandExecutor*);
|
|
||||||
int helpExecute(const vector<string>*, CommandExecutor*);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // CMDS_H
|
|
|
@ -1,32 +0,0 @@
|
||||||
/**
|
|
||||||
Little snippet for library use.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <iostream>
|
|
||||||
#include "../msh-console-library/shell.h"
|
|
||||||
#include "../msh-console-library/command.h"
|
|
||||||
#include "cmds.h"
|
|
||||||
using namespace std;
|
|
||||||
using namespace Cmds;
|
|
||||||
|
|
||||||
static void setup(Shell *);
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
string c = "msh-console-test";
|
|
||||||
string ps = "[msh-console-test]:";
|
|
||||||
Shell mshConsoleTest(c, ps, &setup);
|
|
||||||
|
|
||||||
//add builtin commands
|
|
||||||
mshConsoleTest.addCmd(new Command("cd", &cdExecute));
|
|
||||||
mshConsoleTest.addCmd(new Command("exit", &exitExecute));
|
|
||||||
mshConsoleTest.addCmd(new Command("help", &helpExecute));
|
|
||||||
mshConsoleTest.launch();
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup(Shell *s){
|
|
||||||
cout << "Now entering in test shell...\n" << endl;
|
|
||||||
s->executeCmd("stty -ctlecho");
|
|
||||||
}
|
|
Loading…
Reference in a new issue