/** MIT License Copyright (c) 2016 Claudio Maggioni Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "shell.h" namespace mshconsole { const std::string& Shell::getPs() const{ return ps; } void Shell::setPs(const std::string &value){ ps = value; } const std::string& Shell::getName() const{ return name; } void Shell::setName(const std::string &value){ name = value; } Shell::Shell(std::string nam, std::string ps, std::istream &i, std::ostream &o, std::ostream &e, void (*s)(Shell*), void (*pss)(Shell*)) : cmds(this), inputStream(i), outputStream(o), errorStream(e){ shellSetup = s; name = nam; this->ps = ps; shellPostSetup = pss; } int Shell::launch(){ //Forbid launching shell if it is already launched in other threads. static bool notLoop=false; if(notLoop) return -1; notLoop = true; //Launch the shell setup. if(shellSetup!=0) (*shellSetup)(this); //Run the shell loop. int exitCode; try{ std::string* line=nullptr; struct Params p; while(1){ delete line; outputStream << ps << " "; //Print the prompt. //Try reading the line. if SIGINT is caught, print another time the prompt and retry. try{ line = readLine(); } catch (IsUndoingLineException){ outputStream << "\n"; continue; } //Try line conversion from string to argc and argv. stringToArgcArgv() throws a std::runtime_error //exception if the string has unproper quotes. try{ p = splitLine(line); } catch(std::runtime_error e){ errorStream << name << ": error in line parsing\n"; errorStream << e.what() << "\n"; continue; } //Execute the command. executeCmd(p); }; } catch(CommandExecutor::ExitException c) { exitCode=c.getCode(); } //Launch postSetup(). if(shellPostSetup!=0) shellPostSetup(this); notLoop = false; return exitCode; } int Shell::launchCmd(const struct Params& p){ int status; pid_t pid = fork(); if (pid == 0) { //child process //execute threadCommand int a; try { a=cmds.launch(p, true); std::exit(EXIT_SUCCESS); } catch (CommandNotFoundException){} //execute bash command or program if((a=execvp(p.argv[0], p.argv)) == -1) { outputStream << name << ": execution failed. Errno is set to: "<