diff options
| author | Franklin Wei <frankhwei536@gmail.com> | 2014-04-06 21:56:55 +0100 |
|---|---|---|
| committer | Franklin Wei <frankhwei536@gmail.com> | 2014-04-06 21:56:55 +0100 |
| commit | a9ba68d33abcadfbed7aa64aecd5e5b221246eff (patch) | |
| tree | 1fdb0e136acbf148cf5c10b2d7ad42edf650d7a7 /parse.cpp | |
| parent | 42ab23542d17a55ddef20a0abbe8d6c2cbe767f0 (diff) | |
| download | pish-a9ba68d33abcadfbed7aa64aecd5e5b221246eff.zip pish-a9ba68d33abcadfbed7aa64aecd5e5b221246eff.tar.gz pish-a9ba68d33abcadfbed7aa64aecd5e5b221246eff.tar.bz2 pish-a9ba68d33abcadfbed7aa64aecd5e5b221246eff.tar.xz | |
Added command parser
Diffstat (limited to 'parse.cpp')
| -rw-r--r-- | parse.cpp | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/parse.cpp b/parse.cpp new file mode 100644 index 0000000..a0f0300 --- /dev/null +++ b/parse.cpp @@ -0,0 +1,112 @@ +#include <iostream> +#include <cstdlib> +#include <string> +#include <sstream> +#include <vector> +#include <led.h> +#include <morse.h> +#include <unistd.h> +void pish_parse(const string& str) +{ + stringstream ss(str); + string cmd; + ss>>cmd; + vector<string> args; + while(ss) + { + string arg; + ss >> arg; + args.push_back(arg); + } + string argstr; + for(int i=0;i<args.size();++i) + { + argstr+=args[i]+' '; + } + if(cmd.length()==0) + return; // exit early + if(cmd=="exit" or cmd=="quit") + { + dash(); + exit(0); + } + else if(cmd=="morse") + { + if(argstr!="") + { + dash(); + usleep(250000); + transmit(argstr); + usleep(250000); + dash(); + } + else + { + cerr << "morse requires an argument" << endl; + dot(); + usleep(250000); + dot(); + } + } + else if(cmd=="exec") + { + if(argstr=="") + { + dot(); + usleep(250000); + dot(); + } + else + { + dash(); + usleep(250000); + int ret=system(argstr.c_str()); + if(ret!=EXIT_SUCCESS) + { + cerr << "Command exited with return value " << ret << endl; + dot(); + usleep(250000); + dot(); + } + else + { + cerr << "Command exited normally." << endl; + dash(); + } + } + } + else if(cmd=="cd") + { + if(argstr!="") + { + dash(); + usleep(250000); + chdir(argstr.c_str()); + dash(); + } + else + { + cerr << "Need an argument" << endl; + dot(); + usleep(250000); + dot(); + } + } + else if(cmd=="pwd") + { + dash(); + usleep(250000); + char* pwd=getcwd(0,0); + transmit(string(pwd)); + free(pwd); + usleep(25000); + dash(); + } + else + { + cerr << "Invalid command." << endl; + dot(); + usleep(250000); + dot(); + } +} |