2016-04-23 11:03:53 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
#include "../msh-console-library/shell.h"
|
|
|
|
#include "../msh-console-library/command.h"
|
|
|
|
using namespace std;
|
|
|
|
using namespace mshconsole;
|
|
|
|
|
|
|
|
static void setup(Shell *);
|
|
|
|
|
|
|
|
int cdExecute(CommandExecutor* whoExecuted,const Command::Datas& data, const vector<const char*> argv){
|
|
|
|
if (argv.size() == 0) {
|
|
|
|
cerr << "expected argument to \"cd\"\n";
|
|
|
|
} else {
|
|
|
|
int ret;
|
|
|
|
if (strcmp("~",argv[0])==0) {
|
|
|
|
ret = chdir(getenv("HOME"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = chdir(argv[0]);
|
|
|
|
}
|
|
|
|
if (ret != 0) {
|
|
|
|
cerr << "cd: error executing the command\n";
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int exitExecute(CommandExecutor* whoExecuted,const Command::Datas& data, const vector<const char*> argv){
|
|
|
|
whoExecuted->exit();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int helpExecute(CommandExecutor* whoExecuted,const Command::Datas& data, const vector<const char*> argv){
|
|
|
|
cout << data.getOptData('p')->getString() << "\n";
|
|
|
|
if(data.getOptData('h')->getInt()){
|
|
|
|
cout << "These are the further details." << endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(strcmp(data.getOptData('p')->getString(),"prova")==0){
|
|
|
|
cout << "prova\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
cout << "Snippet console generated with msh-console library.\nUse option -h for further details." << endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Little snippet for library use.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
string c = "msh-console-test";
|
|
|
|
string ps = "[msh-console-test]:";
|
|
|
|
Shell mshConsoleTest(c, ps, cin,cout,cerr, &setup);
|
2016-05-01 14:32:41 +00:00
|
|
|
Command help("help", &helpExecute);
|
2016-04-23 11:03:53 +00:00
|
|
|
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"};
|
2016-05-01 14:32:41 +00:00
|
|
|
help.addOption(h);
|
|
|
|
help.addOption(p);
|
|
|
|
|
|
|
|
Command exit("exit", &exitExecute);
|
|
|
|
Command cd("cd", &cdExecute);
|
2016-04-23 11:03:53 +00:00
|
|
|
|
|
|
|
//add builtin commands
|
2016-05-01 14:32:41 +00:00
|
|
|
mshConsoleTest.addCmd(&help);
|
|
|
|
mshConsoleTest.addCmd(&cd);
|
|
|
|
mshConsoleTest.addCmd(&exit);
|
2016-04-23 11:03:53 +00:00
|
|
|
mshConsoleTest.launch();
|
2016-05-01 14:32:41 +00:00
|
|
|
|
2016-04-23 11:03:53 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup(Shell *s){
|
|
|
|
cout << "Now entering in test shell...\n" << endl;
|
|
|
|
//s->executeCmd("stty -ctlecho");
|
|
|
|
}
|