A Command Interpreter with support for used defined commands, subsystems, macros, help and parameter parsing.

Files at this revision

API Documentation at this revision

Comitter:
wvd_vegt
Date:
Fri Feb 11 11:26:32 2011 +0000
Parent:
8:83edd6addbd5
Child:
10:05fea1780005
Commit message:
0.72

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
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/cmdb.cpp	Fri Feb 11 10:51:08 2011 +0000
+++ b/cmdb.cpp	Fri Feb 11 11:26:32 2011 +0000
@@ -22,6 +22,7 @@
             -Added MAX_LONG and MIN_LONG (long==int on mbed).
             -Removed cmdb_ prefix from members.
             -Tested Macro Support and added it to the Demo.
+            -Added CID_COMMANDS.
    -------- --------------------------------------------------------------
    TODO's
    10022011 -Tweak and Review Documentation.
@@ -585,7 +586,7 @@
         if (cid==CID_LAST) {
             print("Unknown command, type 'Help' for a list of available commands.\r\n");
         } else {
-            //printf("cmds[%d]=%d [%s]\r\n",ndx, cid, _cmds[ndx].cmdstr);
+            //printf("cmds[%d]=%d [%s]\r\n",ndx, cid, cmds[ndx].cmdstr);
 
             //Test for more commandline than allowed too.
             //i.e. run 1 is wrong.
@@ -602,10 +603,10 @@
                         //Define Macro from commandline
                     case CID_MACRO:
                         macro_ptr=-1;
-                       
+
                         zeromemory((char*)macro_buf, sizeof(macro_buf));
                         strncpy(macro_buf, STRINGPARM(0), sizeof(macro_buf) - 1);
-                        
+
                         //DEBUG
                         printf("Macro=%s\r\n",macro_buf);
                         break;
@@ -638,6 +639,9 @@
 #endif
 
                         /////// GLOBAL COMMANDS ///////
+                    case CID_COMMANDS:
+                        cmd_dump();
+                        break;
 
                         //Echo
                     case CID_ECHO:
@@ -696,9 +700,9 @@
                                         if (cmds[i].subs==cid) {
                                             subcmds--;
                                             if (subcmds!=0) {
-                                                cmdhelp("",i,",\r\n");
+                                                cmd_help("",i,",\r\n");
                                             } else {
-                                                cmdhelp("",i,".\r\n");
+                                                cmd_help("",i,".\r\n");
                                             }
                                         }
                                     }
@@ -707,12 +711,12 @@
 
                                 case GLOBALCMD: //Dump command only
                                     //print("Global command:\r\n\r\n",cmd_tbl[cmd_tbl[ndx].subs].cmdstr);
-                                    cmdhelp("Syntax: ",ndx,".\r\n");
+                                    cmd_help("Syntax: ",ndx,".\r\n");
                                     break;
 
                                 default:        //Dump one subsystem command
                                     printf("%s subsystem command:\r\n\r\n",cmds[cmds[ndx].subs].cmdstr);
-                                    cmdhelp("Syntax: ",ndx,".\r\n");
+                                    cmd_help("Syntax: ",ndx,".\r\n");
                                     break;
                             }
                         } else {
@@ -726,10 +730,10 @@
                                 //-1 because we want comma's and for the last a .
                                 for (int i=0;i<cmds.size()-1;i++) {
                                     if ((cmds[i].subs<0) || (cmds[i].subs==subsystem)) {
-                                        cmdhelp("",i,",\r\n");
+                                        cmd_help("",i,",\r\n");
                                     }
                                 }
-                                cmdhelp("",cmds.size()-1,".\r\n");
+                                cmd_help("",cmds.size()-1,".\r\n");
                             }
                         }
                         print("\r\n");
@@ -742,7 +746,7 @@
                     }
                 }
             } else {
-                cmdhelp("Syntax: ",ndx,".\r\n");
+                cmd_help("Syntax: ",ndx,".\r\n");
             }
 
         }
@@ -752,12 +756,170 @@
     }
 }
 
+//------------------------------------------------------------------------------
+//----Dump commands table as a ini file.
+//------------------------------------------------------------------------------
+
+void Cmdb::cmd_dump() {
+    int  ndx;
+    int  j;
+    int  k;
+    int  lastmod;
+
+    k = 0;
+    lastmod = 0;
+
+    for (ndx=0;ndx<cmds.size();ndx++) {
+
+#ifndef SHOWHIDDEN
+        if (cmds[ndx].subs==HIDDENSUB) {
+            continue;
+        }
+#endif
+
+        switch (cmds[ndx].subs) {
+            case SUBSYSTEM :
+                printf("[command%2.2d]\r\n",ndx+1);
+                print("type=Subsystem\r\n");
+                print("subsystem=Global\r\n");
+                break;
+            case HIDDENSUB :
+#ifdef SHOWHIDDEN
+                printf("[command%2.2d]\r\n",ndx+1);
+                print("type=HiddenSubystem\r\n");
+                print("subsystem=Global\r\n");
+#endif
+                break;
+            case GLOBALCMD :
+                printf("[command%2.2d]\r\n",ndx+1);
+                print("type=GlobalCommand\r\n");
+                print("subsystem=Global\r\n");
+                break;
+            default        :
+                if (cmds[cmds[ndx].subs].subs==HIDDENSUB) {
+#ifdef SHOWHIDDEN
+                    printf("[command%2.2d]\r\n",ndx+1);
+                    print("type=HiddenCommand\r\n");
+                    print("subsystem=HiddenSubystem\r\n");
+#endif
+                    continue;
+                }
+                printf("[command%2.2d]\r\n",ndx+1);
+                print("type=Command\r\n");
+                printf("subsystem=%s\r\n",cmds[cmds[ndx].subs].cmdstr);
+                break;
+        }
+
+        if (cmds[ndx].subs==HIDDENSUB) {
+            continue;
+        }
+
+        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++) {
+            switch (cmds[ndx].parms[j]) {
+                case '%' :
+                    lastmod=0;
+                    break;
+
+                case 'b' :
+                    lastmod=8;
+                    break;
+                case 'h' :
+                    lastmod=16;
+                    break;
+                case 'l' :
+                    lastmod=32;
+                    break;
+
+                case 'd' :
+                case 'i' :     {
+                    switch (lastmod) {
+                        case  0 :
+                        case 16 :
+                            print("int");
+                            k+=3;
+                            break;
+                        case  8 :
+                            print("shortint");
+                            k+=8;
+                            break;
+                        case 32:
+                            print("longint");
+                            k+=7;
+                            break;
+                    }
+                    break;
+                }
+
+                case 'u' :
+                case 'o' :
+                case 'x' :     {
+                    switch (lastmod) {
+                        case  0 :
+                        case 16 :
+                            print("word");
+                            k+=4;
+                            break;
+                        case  8 :
+                            print("byte");
+                            k+=4;
+                            break;
+                        case 32 :
+                            print("dword");
+                            k+=5;
+                            break;
+                    }
+
+                    switch (cmds[ndx].parms[j]) {
+                        case 'o' :
+                            print("[o]");
+                            k+=3;
+                            break;
+                        case 'x' :
+                            print("[h]");
+                            k+=3;
+                            break;
+                    }
+
+                    break;
+                }
+
+                case 'e' :
+                case 'f' :
+                case 'g' :
+                    print("float");
+                    k+=5;
+                    break;
+
+                case 'c' :
+                    print("char");
+                    k+=4;
+                    break;
+
+                case 's' :
+                    print("string");
+                    k+=6;
+                    break;
+
+                case ' ' :
+                    printch(sp);
+                    k++;
+                    break;
+            }
+        }
+        print("\r\n");
+        printf("syntax=%s\r\n",cmds[ndx].parmdescr);
+    }
+}
+
 void  Cmdb::prompt(void) {
 #ifdef SUBSYSTEMPROMPTS
     if (subsystem!=-1) {
         int ndx = cmdid_index(subsystem);
 
-        printf("%s>",_cmds[ndx].cmdstr);
+        printf("%s>",cmds[ndx].cmdstr);
 
         return;
     }
@@ -766,7 +928,7 @@
     printf(PROMPT);
 }
 
-void  Cmdb::cmdhelp(char *pre, int ndx, char *post) {
+void  Cmdb::cmd_help(char *pre, int ndx, char *post) {
     int  j;
     int  k;
     int  lastmod;
--- a/cmdb.h	Fri Feb 11 10:51:08 2011 +0000
+++ b/cmdb.h	Fri Feb 11 11:26:32 2011 +0000
@@ -212,6 +212,10 @@
  */
 #define HIDDENSUB         -3
 
+/** Predefined Dump Command.
+ */
+#define CID_COMMANDS 9989
+
 /** Predefined Boot Command.
  */
 #define CID_BOOT    9990
@@ -275,9 +279,17 @@
 
 /** The Boot Command.
  *
+ * @note: this command can be used to list all commands in Windows ini file format for host processing.
+ *
  * Optional.
  */
-const cmd BOOT("Boot",GLOBALCMD,CID_BOOT,"","Boot");
+const cmd COMMANDS("Commands",GLOBALCMD,CID_COMMANDS,"","Dump Commands");
+
+/** The Boot Command.
+ *
+ * Optional.
+ */
+const cmd BOOT("Boot",GLOBALCMD,CID_BOOT,"","Boot mBed");
 
 /** The Macro Command.
  *
@@ -363,7 +375,7 @@
 
 /** The Command Interpreter Version.
  */
-#define CMDB_VERSION     0.71
+#define CMDB_VERSION     0.72
 
 //------------------------------------------------------------------------------
 
@@ -687,7 +699,11 @@
      * @param ndx the index of the command in the command table.
      * @param post trailing text.
      */
-    void cmdhelp(char *pre, int ndx, char *post);
+    void cmd_help(char *pre, int ndx, char *post);
+
+    /** Dumps all commands in ini file format.
+     */
+    void cmd_dump();
 
     /** memset wrapper.
      *
--- a/main.cpp	Fri Feb 11 10:51:08 2011 +0000
+++ b/main.cpp	Fri Feb 11 11:26:32 2011 +0000
@@ -44,6 +44,7 @@
     cmds.push_back(c2); //The Int Command is handled by our 'my_dispatcher' method.
 
     //Add some predefined...
+    cmds.push_back(COMMANDS); //Handled by Cmdb internally.
     cmds.push_back(BOOT); //Handled by Cmdb internally.
 
     cmds.push_back(ECHO); //Handled by Cmdb internally.