The CommandProcessor is the interface to install a run-time menu into an embedded system.

Dependents:   A_CANAdapter USB2I2C

Revision:
12:a8c56bf811b9
Parent:
11:4a3cd3f2183b
Child:
13:e1880be590c4
--- a/CommandProcessor.c	Sat Apr 23 14:15:10 2011 +0000
+++ b/CommandProcessor.c	Sun May 29 20:40:04 2011 +0000
@@ -8,13 +8,20 @@
 ///
 /// Even though it is a c interface, it is somewhat object oriented.
 ///
-/// @version 1.01
+/// @version 1.03
 ///
-/// @note Copyright © 2011 by Smartware Computing, all rights reserved.
-///       This program may be used by others as long as this copyright notice
-///       remains intact.
-/// @author David Smart
+/// @note Copyright &copr; 2011 by Smartware Computing, all rights reserved.
+///     Individuals may use this application for evaluation or non-commercial
+///     purposes. Within this restriction, changes may be made to this application
+///     as long as this copyright notice is retained. The user shall make
+///     clear that their work is a derived work, and not the original.
+///     Users of this application and sources accept this application "as is" and
+///     shall hold harmless Smartware Computing, for any undesired results while
+///     using this application - whether real or imagined.
 ///
+/// @author David Smart, Smartware Computing
+///
+
 #include "stdio.h"
 #include "string.h"
 #include "stdlib.h"
@@ -47,9 +54,11 @@
 
 static char *buffer;        // buffer space must be allocated based on the longest command
 
+static int longestCommand = 0;
+
 static struct {
-	CMD_T *SignOnBanner;
-	int showSignOnBanner;		// Shows the sign-on banner at startup
+    CMD_T *SignOnBanner;
+    int showSignOnBanner;        // Shows the sign-on banner at startup
     int caseinsensitive;    // FALSE=casesensitive, TRUE=insensitive
     int echo;               // TRUE=echo on, FALSE=echo off
     int bufferSize;         // size of the buffer
@@ -61,7 +70,7 @@
 
 static INITRESULT_T CommandProcessor_Init(
     CMD_T *SignOnBanner,
-	CONFIG_T config,
+    CONFIG_T config,
     int maxCmdLen,
     int (*kbhit)(void),
     int (*getch)(void),
@@ -168,7 +177,7 @@
     while (link && link->menu) {
         if (link->menu->visible) {
             if (strlen(link->menu->command) + strlen(link->menu->helptext) + 5 < sizeof(buffer)) {
-                sprintf(buffer, " %-10s: %s", link->menu->command, link->menu->helptext);
+                sprintf(buffer, " %-*s: %s", longestCommand, link->menu->command, link->menu->helptext);
                 cfg.puts(buffer);
             }
         }
@@ -188,11 +197,11 @@
                  "    * <esc> can be used to cancel a command.\r\n"
                  "    * <tab> can be used to complete the entry of a partial command.\r\n"
                  "");
-		cfg.puts("\r\n About this CommandProcessor:\r\n"
-				 "    This CommandProcessor provides an easy facility for creating an\r\n"
-				 "      interactive runtime interpreter in an embedded system.\r\n"
-				 "    Copyright (c) 2011 by Smartware Computing, all rights reserved.\r\n"
-				 "    Author: David Smart, Smartware Computing\r\n");
+        cfg.puts("\r\n About this CommandProcessor:\r\n"
+                 "    This CommandProcessor provides an easy facility for creating an\r\n"
+                 "      interactive runtime interpreter in an embedded system.\r\n"
+                 "    Copyright (c) 2011 by Smartware Computing, all rights reserved.\r\n"
+                 "    Author: David Smart, Smartware Computing\r\n");
     }
     return runok;
 }
@@ -305,22 +314,22 @@
     int (*putch)(int ch),
     int (*puts)(const char * s)
 ) {
-	if (SignOnBanner) {
-		CommandProcessor.Add(SignOnBanner);
-		cfg.SignOnBanner = SignOnBanner;
-		cfg.showSignOnBanner = 1;
-	}
+    if (SignOnBanner) {
+        CommandProcessor.Add(SignOnBanner);
+        cfg.SignOnBanner = SignOnBanner;
+        cfg.showSignOnBanner = 1;
+    }
     if (maxCmdLen < 6)
         maxCmdLen = 6;
     buffer = (char *)malloc(maxCmdLen+1);
     cfg.bufferSize = maxCmdLen;
     if (buffer) {
         if (config & CFG_ENABLE_SYSTEM)
-		{
+        {
             CommandProcessor.Add(&QuestionMenu);
             CommandProcessor.Add(&HelpMenu);
             CommandProcessor.Add(&EchoMenu);
-		}
+        }
         if (config & CFG_ENABLE_TERMINATE)
             CommandProcessor.Add(&ExitMenu);
         //if (addDefaultMenu & 0x0002)
@@ -352,6 +361,9 @@
     CMDLINK_T *prev;
     CMDLINK_T *temp;
 
+    if (strlen(menu->command) > longestCommand)
+        longestCommand = strlen(menu->command);
+    
     // Allocate the storage for this menu item
     temp = (CMDLINK_T *)malloc(sizeof(CMDLINK_T));
     if (!temp)
@@ -369,14 +381,14 @@
         prev = ptr;
         ptr = ptr->next;
     }
-	if (prev == head) {
-		head = temp;
-		head->next = prev;
-	} else {
-		prev->next = temp;
-		prev = temp;
-		prev->next = ptr;
-	}
+    if (prev == head) {
+        head = temp;
+        head->next = prev;
+    } else {
+        prev->next = temp;
+        prev = temp;
+        prev->next = ptr;
+    }
     return addok;
 }
 
@@ -401,10 +413,10 @@
     CMD_T *cbk = NULL;
     char * params = NULL;
 
-	if (cfg.showSignOnBanner) {
-		cfg.SignOnBanner->callback("");
-		cfg.showSignOnBanner = 0;
-	}
+    if (cfg.showSignOnBanner) {
+        cfg.SignOnBanner->callback("");
+        cfg.showSignOnBanner = 0;
+    }
     if (showPrompt && cfg.echo) {
         cfg.putch('>');
         showPrompt = FALSE;