2016-03-11 16:45:15 +00:00
|
|
|
#include "shell.h"
|
2016-03-11 15:13:04 +00:00
|
|
|
|
2016-03-11 16:45:15 +00:00
|
|
|
string Shell::getPs() const
|
|
|
|
{
|
|
|
|
return ps;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shell::setPs(const string &value)
|
|
|
|
{
|
|
|
|
ps = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
string Shell::getName() const
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shell::setName(const string &value)
|
|
|
|
{
|
|
|
|
name = value;
|
|
|
|
}
|
|
|
|
|
2016-03-16 09:01:24 +00:00
|
|
|
Shell::Shell( string n, string ps,void (*s)(Shell*), void (*pss)(Shell*)) : cmds(this)
|
2016-03-11 16:45:15 +00:00
|
|
|
{
|
|
|
|
shellSetup = s;
|
|
|
|
name = n;
|
|
|
|
this->ps = ps;
|
2016-03-16 09:01:24 +00:00
|
|
|
shellPostSetup = pss;
|
2016-03-12 17:58:01 +00:00
|
|
|
notLoop = false;
|
2016-03-11 16:45:15 +00:00
|
|
|
}
|
|
|
|
|
2016-03-12 17:58:01 +00:00
|
|
|
void Shell::launch(){
|
2016-03-11 16:45:15 +00:00
|
|
|
//launch setup
|
2016-03-12 17:58:01 +00:00
|
|
|
if(notLoop) {
|
|
|
|
throw ShellLaunchInSetupException();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(shellSetup!=0) {
|
|
|
|
notLoop = true;
|
|
|
|
(*shellSetup)(this);
|
|
|
|
notLoop = false;
|
|
|
|
}
|
2016-03-11 16:45:15 +00:00
|
|
|
//launch loop
|
|
|
|
string* line;
|
|
|
|
vector<string>* args;
|
|
|
|
int status;
|
|
|
|
do {
|
|
|
|
bool readSuccess;
|
|
|
|
do{
|
|
|
|
cout << ps << " ";
|
|
|
|
try{
|
|
|
|
line = read_line();
|
|
|
|
readSuccess = true;
|
|
|
|
}
|
|
|
|
catch (IsUndoingLineException){
|
|
|
|
cout << "\n";
|
|
|
|
readSuccess = false;
|
|
|
|
}
|
|
|
|
}while(!readSuccess);
|
|
|
|
args = split_line(line);
|
2016-03-13 16:16:34 +00:00
|
|
|
status = executeCmd(args);
|
|
|
|
delete args;
|
2016-03-11 16:45:15 +00:00
|
|
|
} while (status);
|
2016-03-16 09:01:24 +00:00
|
|
|
if(shellPostSetup!=0) {
|
|
|
|
shellPostSetup(this);
|
|
|
|
}
|
2016-03-11 16:45:15 +00:00
|
|
|
}
|
2016-03-11 15:13:04 +00:00
|
|
|
|
2016-03-13 16:16:34 +00:00
|
|
|
int Shell::launchCmd(vector<string>* args)
|
2016-03-11 15:13:04 +00:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
2016-03-13 16:16:34 +00:00
|
|
|
pid_t pid = fork();
|
2016-03-11 15:13:04 +00:00
|
|
|
if (pid == 0) {
|
2016-03-13 16:16:34 +00:00
|
|
|
//child process
|
|
|
|
|
|
|
|
//execute threadCommand
|
|
|
|
int a;
|
|
|
|
try {
|
|
|
|
a=cmds.launch(args, true);
|
|
|
|
}
|
|
|
|
catch (CommandNotFoundException){
|
|
|
|
//execute bash command or program
|
|
|
|
vector<char *> argv(args->size() + 1);
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i != args->size()-1; ++i)
|
|
|
|
{
|
|
|
|
argv[i] = &(args->operator[](i)[0]);
|
|
|
|
}
|
|
|
|
argv[i] = NULL;
|
|
|
|
if((a=execvp(argv[0], argv.data())) == -1) {
|
|
|
|
cerr << name <<": command " << args->operator [](0) << " not found\n";
|
|
|
|
}
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
2016-03-11 15:13:04 +00:00
|
|
|
}
|
|
|
|
else if (pid < 0) {
|
|
|
|
// Error forking
|
2016-03-13 16:16:34 +00:00
|
|
|
cerr << name <<": error forking the process\n";
|
2016-03-11 15:13:04 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Parent process
|
|
|
|
do {
|
2016-03-13 16:16:34 +00:00
|
|
|
//wait until child finished
|
2016-03-11 15:13:04 +00:00
|
|
|
waitpid(pid, &status, WUNTRACED);
|
|
|
|
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-03-13 16:16:34 +00:00
|
|
|
int Shell::executeCmd(vector<string>* args)
|
2016-03-11 15:13:04 +00:00
|
|
|
{
|
2016-03-13 16:16:34 +00:00
|
|
|
if (args->operator [](0) == "\0") {
|
|
|
|
// An empty command was entered.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int ret;
|
|
|
|
try {
|
|
|
|
ret = cmds.launch(args, false);
|
|
|
|
}
|
|
|
|
catch(CommandNotFoundException) {
|
|
|
|
ret = launchCmd(args);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int Shell::executeCmd(const std::string &args){
|
|
|
|
return executeCmd(split_line(&args));
|
2016-03-11 15:13:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 16:45:15 +00:00
|
|
|
void Shell::EofHandler(int){
|
2016-03-11 15:13:04 +00:00
|
|
|
undoingLine = true;
|
|
|
|
}
|
|
|
|
|
2016-03-16 09:01:24 +00:00
|
|
|
void Shell::setShellSetup(void (*s)(Shell *)){
|
|
|
|
shellSetup=s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void (*Shell::getShellSetup())(Shell*) {
|
|
|
|
return shellSetup;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shell::setShellPostSetup(void (*s)(Shell *)){
|
|
|
|
shellPostSetup=s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void (*Shell::getShellPostSetup())(Shell*) {
|
|
|
|
return shellPostSetup;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-11 16:45:15 +00:00
|
|
|
void setEofHandler(void (*funcptr)(int)){
|
2016-03-11 15:13:04 +00:00
|
|
|
struct sigaction *sa = new struct sigaction();
|
2016-03-11 16:45:15 +00:00
|
|
|
sa->sa_handler = funcptr;
|
2016-03-11 15:13:04 +00:00
|
|
|
sa->sa_flags = 0; // not SA_RESTART!;
|
|
|
|
sigaction(SIGINT, sa, NULL);
|
2016-03-11 16:45:15 +00:00
|
|
|
delete sa;
|
|
|
|
}
|
|
|
|
|
|
|
|
string* Shell::read_line()
|
|
|
|
{
|
|
|
|
string* buffer = new string();
|
|
|
|
setEofHandler(EofHandler);
|
2016-03-11 15:13:04 +00:00
|
|
|
getline(cin,*buffer); // get command
|
|
|
|
cin.clear(); // clear flags
|
|
|
|
|
|
|
|
if(undoingLine){
|
|
|
|
undoingLine=false;
|
|
|
|
throw IsUndoingLineException();
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2016-03-13 16:16:34 +00:00
|
|
|
vector<string>* Shell::split_line(const string* line)
|
2016-03-11 15:13:04 +00:00
|
|
|
{
|
|
|
|
vector<string>* tokens = new vector<string>();
|
|
|
|
string ln = *line;
|
|
|
|
istringstream is(ln);
|
|
|
|
int i;
|
|
|
|
for(i=0; getline(is, ln, ' '); i++){
|
|
|
|
tokens->push_back(ln);
|
|
|
|
}
|
|
|
|
tokens->push_back("\0");
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
2016-03-11 16:45:15 +00:00
|
|
|
bool Shell::undoingLine = false;
|
|
|
|
|
2016-03-13 16:16:34 +00:00
|
|
|
void Shell::addCmd(Command* cmd, bool isthread){
|
|
|
|
cmds.add(cmd, isthread);
|
2016-03-11 16:45:15 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 16:16:34 +00:00
|
|
|
size_t Shell::howManyCmds() const{
|
2016-03-11 16:45:15 +00:00
|
|
|
return cmds.howMany();
|
2016-03-11 15:13:04 +00:00
|
|
|
}
|