diff --git a/msh-console-library/command.cpp b/msh-console-library/command.cpp index d877914..9331c86 100644 --- a/msh-console-library/command.cpp +++ b/msh-console-library/command.cpp @@ -92,6 +92,8 @@ namespace mshconsole { while (poptPeekArg(pc) != NULL) nonOptionArgs.push_back(const_cast(poptGetArg(pc))); + poptFreeContext(pc); + return (*funcCommand)(c,datas,nonOptionArgs); } @@ -110,5 +112,10 @@ namespace mshconsole { } } } + + Command::~Command(){ + + } + const struct poptOption Command::POPT_TERMINATOR = {NULL,'\0',POPT_ARG_NONE,NULL,-1,NULL,NULL}; } diff --git a/msh-console-library/main.cpp b/msh-console-library/main.cpp index 7457a17..d488dd1 100644 --- a/msh-console-library/main.cpp +++ b/msh-console-library/main.cpp @@ -55,17 +55,21 @@ int main(int argc, char **argv) string c = "msh-console-test"; string ps = "[msh-console-test]:"; Shell mshConsoleTest(c, ps, cin,cout,cerr, &setup); - Command* help = new Command("help", &helpExecute); + Command help("help", &helpExecute); struct poptOption h = {"help",'h',POPT_ARG_NONE,NULL,1000,"advance","advaced options"}; struct poptOption p = {"prova",'p',POPT_ARG_STRING,NULL,1000,"cose","cosecose"}; - help->addOption(h); - help->addOption(p); + help.addOption(h); + help.addOption(p); + + Command exit("exit", &exitExecute); + Command cd("cd", &cdExecute); //add builtin commands - mshConsoleTest.addCmd(help); - mshConsoleTest.addCmd(new Command("exit", &exitExecute)); - mshConsoleTest.addCmd(new Command("cd", &cdExecute)); + mshConsoleTest.addCmd(&help); + mshConsoleTest.addCmd(&cd); + mshConsoleTest.addCmd(&exit); mshConsoleTest.launch(); + return EXIT_SUCCESS; } diff --git a/msh-console-library/shell.cpp b/msh-console-library/shell.cpp index 199a124..fab7742 100644 --- a/msh-console-library/shell.cpp +++ b/msh-console-library/shell.cpp @@ -60,11 +60,10 @@ namespace mshconsole { //Run the shell loop. int exitCode; + std::string* line=nullptr; + struct Params p; try{ - std::string* line=nullptr; - struct Params p; - while(1){ delete line; outputStream << ps << " "; //Print the prompt. @@ -91,8 +90,13 @@ namespace mshconsole { //Execute the command. executeCmd(p); + + //Free p + deleteParams(p); }; } catch(CommandExecutor::ExitException c) { + delete line; + deleteParams(p); exitCode=c.getCode(); } @@ -120,7 +124,9 @@ namespace mshconsole { catch (CommandNotFoundException){} //execute bash command or program - if((a=execvp(p.argv[0], p.argv)) == -1) { + a=execvp(p.argv[0], p.argv); + + if(a == -1) { outputStream << name << ": execution failed. Errno is set to: "< args = parse(str); - *argv = (char**)std::malloc((args.size()+1) * sizeof(char*)); + *argv = new char*[args.size()+1](); int i=0; for(std::vector::iterator it = args.begin(); @@ -45,7 +45,7 @@ namespace StringToArgcArgv{ ++it, ++i) { std::string arg = *it; - (*argv)[i] = (char*)std::malloc((arg.length()+1) * sizeof(char)); + (*argv)[i] = new char[arg.length()+1](); std::strcpy((*argv)[i], arg.c_str()); } (*argv)[i]=NULL; @@ -53,6 +53,13 @@ namespace StringToArgcArgv{ *argc = args.size(); } + void freeString(int& argc, char**& argv){ + for(int i=0; i parse(const std::string& args) { std::stringstream ain(args); // used to iterate over input string diff --git a/msh-console-library/stringtoargcargv.h b/msh-console-library/stringtoargcargv.h index 1c4d91b..75c565a 100644 --- a/msh-console-library/stringtoargcargv.h +++ b/msh-console-library/stringtoargcargv.h @@ -42,5 +42,6 @@ namespace StringToArgcArgv{ bool _isWhitespace(char c); std::vector parse(const std::string& args); void stringToArgcArgv(const std::string& str, int* argc, char*** argv); + void freeString(int& argc, char**& argv); } #endif //STRINGTOARGCARGV_H