A Command Interpreter with support for used defined commands, subsystems, macros, help and parameter parsing.
Revision 17:7e6a723d65de, committed 2011-09-05
- Comitter:
- wvd_vegt
- Date:
- Mon Sep 05 10:05:49 2011 +0000
- Parent:
- 16:ec8147828286
- Child:
- 18:7a4dc478d022
- Commit message:
- Added present(). replace() and indexof() methods.
Changed in this revision
cmdb.cpp | Show annotated file Show diff for this revision Revisions of this file |
cmdb.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/cmdb.cpp Thu Mar 24 21:40:58 2011 +0000 +++ b/cmdb.cpp Mon Sep 05 10:05:49 2011 +0000 @@ -26,6 +26,10 @@ -Fixed a small bug in parse. -v0.76 24032011 -Fixed some left-over bugs caused by index/id mixup. + 09052011 -Added present(), replace() and indexof() methods. + replace() can be used to replace the complete command vector + by a changed done. Inserting directly into cmdb's copy fails + somehow. -------- -------------------------------------------------------------- TODO's 10022011 -Tweak and Review Documentation. @@ -47,14 +51,14 @@ //------------------------------------------------------------------------------ -Cmdb::Cmdb(const Serial& serial, const std::vector<cmd>& cmds, void (*callback)(Cmdb&,int)) : - serial(serial), cmds(cmds) { +Cmdb::Cmdb(const Serial& _serial, std::vector<cmd>& _cmds, void (*_callback)(Cmdb&,int)) : + serial(_serial), cmds(_cmds) { echo = true; bold = true; subsystem = -1; - user_callback = callback; + user_callback = _callback; init(true); } @@ -171,7 +175,7 @@ break; } case EID_CURSOR_UP : { - for (i=0;i<cmdndx;i++) { + for (i=0; i<cmdndx; i++) { print(bs); } cmdndx=strlen(lstbuf); @@ -345,7 +349,7 @@ zeromemory(argstr_buf,sizeof(argstr_buf)); //Make it worse in Lint - for (int i=0;i<MAX_ARGS;i++) { + for (int i=0; i<MAX_ARGS; i++) { parms[i].type=PARM_UNUSED; zeromemory((char*)&(parms[i].val),sizeof(parms[i].val)); } @@ -429,7 +433,7 @@ error = 0; - for (int i=0;i<argcnt;i++) { + for (int i=0; i<argcnt; i++) { //printf("prm_%2.2d=%s\r\n",i, prms[i]); switch (strlen(prms[i])) { @@ -694,14 +698,14 @@ //Count SubSystem Commands. int subcmds =0; - for (int i=0;i<cmds.size();i++) { + for (int i=0; i<cmds.size(); i++) { if (cmds[i].subs==cid) { subcmds++; } } //Print SubSystem Commands. - for (int i=0;i<cmds.size()-1;i++) { + for (int i=0; i<cmds.size()-1; i++) { if (cmds[i].subs==cid) { subcmds--; if (subcmds!=0) { @@ -738,7 +742,7 @@ //Dump Active Subsystem, Global & Other (dormant) Subsystems //-1 because we want comma's and for the last a . - for (int i=0;i<cmds.size()-1;i++) { + for (int i=0; i<cmds.size()-1; i++) { if ((cmds[i].subs<0) || (cmds[i].subs==subsystem)) { cmd_help("",i,",\r\n"); } @@ -779,7 +783,7 @@ k = 0; lastmod = 0; - for (ndx=0;ndx<cmds.size();ndx++) { + for (ndx=0; ndx<cmds.size(); ndx++) { #ifndef SHOWHIDDEN if (cmds[ndx].subs==HIDDENSUB) { @@ -829,7 +833,7 @@ printf("command=%s\r\n",cmds[ndx].cmdstr); printf("helpmsg=%s\r\n",cmds[ndx].cmddescr); print("parameters="); - for (j=0;j<strlen(cmds[ndx].parms);j++) { + for (j=0; j<strlen(cmds[ndx].parms); j++) { switch (cmds[ndx].parms[j]) { case '%' : lastmod=0; @@ -986,7 +990,7 @@ k++; } - for (j=0;j<strlen(cmds[ndx].parms);j++) { + for (j=0; j<strlen(cmds[ndx].parms); j++) { switch (cmds[ndx].parms[j]) { case '%' : lastmod=0; @@ -1079,7 +1083,7 @@ } } - for (j=k;j<40;j++) printch(sp); + for (j=k; j<40; j++) printch(sp); switch (cmds[ndx].subs) { case SUBSYSTEM : @@ -1123,7 +1127,7 @@ len1=strlen(s1); len2=strlen(s2); - for (i = 0; (i<len1) && (i<len2);i++) { + for (i = 0; (i<len1) && (i<len2); i++) { if ( toupper (s1[i])<toupper(s2[i]) ) return (-1); if ( toupper (s1[i])>toupper(s2[i]) ) return (+1); }
--- a/cmdb.h Thu Mar 24 21:40:58 2011 +0000 +++ b/cmdb.h Mon Sep 05 10:05:49 2011 +0000 @@ -150,7 +150,7 @@ parmdescr = (char*)malloc(strlen(_parmdescr)+1); strcpy(parmdescr,_parmdescr); - + //printf("%d:%d\r\n", subs, cid); } }; @@ -382,30 +382,30 @@ /** The Command Interpreter Version. */ -#define CMDB_VERSION 0.77 +#define CMDB_VERSION 0.78 //------------------------------------------------------------------------------ /** Command Interpreter class. - * + * * Steps to take: - * + * * 1) Create a std::vector<cmd> and fill it with at least * the mandatory commands IDLE and HELP. - * + * * 2) Create an Cmdb class instance and pass it the vector, - * a Serial port object like Serial serial(USBTX, USBRX); + * a Serial port object like Serial serial(USBTX, USBRX); * and finally a command dispatcher function. - * + * * 3) Feed the interpreter with characters received from your serial port. * Note: Cmdb self does not retrieve input it must be handed to it. * It implements basic members for checking/reading the serial port. - * + * * 4) Handle commands added by the application by the Cid and parameters passed. - * + * * Note: Predefined commands and all subsystems transitions are handled by the internal dispatcher. * So the passed dispatcher only has to handle user/application defined commands'. - * + * * @see main.cpp for a demo. */ class Cmdb { @@ -419,7 +419,7 @@ * @param serial a Serial port used for communication. * @param cmds a vector with the command table. */ - Cmdb(const Serial& serial, const std::vector<cmd>& cmds, void (*callback)(Cmdb&,int) ); + Cmdb(const Serial& _serial, std::vector<cmd>& _cmds, void (*_callback)(Cmdb&,int) ); /** The version of the Command Interpreter. * @@ -645,8 +645,63 @@ return parms[ndx].val.s; } + bool present(char *cmdstr) { + return cmdid_search(cmdstr)!=CID_LAST; + } + + void replace(std::vector<cmd>& newcmds) { + cmds.assign(newcmds.begin(), newcmds.end()); + } + + + int indexof(int cid) { + return cmdid_index(cid); + } + + //FAILS... + /* + void insert(int cid, cmd newcmd) { + //Add Command (update our original and then assign/replace cmdb's copy)... + vector<cmd>::iterator iter; + + std::vector<cmd> newcmds = std::vector<cmd>(cmds); + + iter = newcmds.begin(); + + newcmds.insert(iter+indexof(cid)+1,newcmd); + + replace(newcmds); + + printf("Index: %d\r\n", ndx); + + print("check #1\r\n"); + print("check #2\r\n"); + + vector<cmd>::iterator it; + it=newcmds.begin(); + + print("check #3\r\n"); + ndx++; + newcmds.insert(it, newcmd); + print("check #4\r\n"); + + //cmds.push_back(c1); + cmds.assign(newcmds.begin(), newcmds.end()); + } + */ + private: + /** Internal Serial Port Storage. + */ + Serial serial; + + /** Internal Command Table Vector Storage. + * + * @see http://www.cplusplus.com/reference/stl/vector/ + */ + std::vector<cmd> cmds; + /** C callback function * * @see See http://www.newty.de/fpt/fpt.html#chapter2 for function pointers. @@ -730,16 +785,6 @@ */ int stricmp (char *s1, char *s2); - /** Internal Serial Port Storage. - */ - Serial serial; - - /** Internal Command Table Vector Storage. - * - * @see http://www.cplusplus.com/reference/stl/vector/ - */ - std::vector<cmd> cmds; - /** Internal Echo Flag Storage. */ bool echo;