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

Dependents:   A_CANAdapter USB2I2C

Committer:
WiredHome
Date:
Sun Mar 20 21:50:15 2011 +0000
Revision:
0:198f53da1bc8
Child:
5:a98bd1f2fd59

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:198f53da1bc8 1 /// @file CommandProcessor.c is a simple interface to an interactive
WiredHome 0:198f53da1bc8 2 /// command set of user defined commands.
WiredHome 0:198f53da1bc8 3 ///
WiredHome 0:198f53da1bc8 4 /// With this, you can create functions that are exposed to a console
WiredHome 0:198f53da1bc8 5 /// interface. Each command may have 0 or more parameters.
WiredHome 0:198f53da1bc8 6 /// Typing the command, or at least the set of characters that make
WiredHome 0:198f53da1bc8 7 /// it unique from all other commands is enough to activate the command.
WiredHome 0:198f53da1bc8 8 ///
WiredHome 0:198f53da1bc8 9 /// Even though it is a c interface, it is somewhat object oriented.
WiredHome 0:198f53da1bc8 10 ///
WiredHome 0:198f53da1bc8 11 /// @version 1.0
WiredHome 0:198f53da1bc8 12 ///
WiredHome 0:198f53da1bc8 13 /// @note Copyright © 2011 by Smartware Computing, all rights reserved.
WiredHome 0:198f53da1bc8 14 /// This program may be used by others as long as this copyright notice
WiredHome 0:198f53da1bc8 15 /// remains intact.
WiredHome 0:198f53da1bc8 16 /// @author David Smart
WiredHome 0:198f53da1bc8 17 ///
WiredHome 0:198f53da1bc8 18 #include "stdio.h"
WiredHome 0:198f53da1bc8 19 #include "string.h"
WiredHome 0:198f53da1bc8 20 #include "stdlib.h"
WiredHome 0:198f53da1bc8 21 #ifdef WIN32
WiredHome 0:198f53da1bc8 22 #include "windows.h"
WiredHome 0:198f53da1bc8 23 #pragma warning (disable: 4996)
WiredHome 0:198f53da1bc8 24 #endif
WiredHome 0:198f53da1bc8 25
WiredHome 0:198f53da1bc8 26 #include "CommandProcessor.h"
WiredHome 0:198f53da1bc8 27
WiredHome 0:198f53da1bc8 28 /// This holds the single linked list of commands
WiredHome 0:198f53da1bc8 29 ///
WiredHome 0:198f53da1bc8 30 typedef struct CMDLINK_T
WiredHome 0:198f53da1bc8 31 {
WiredHome 0:198f53da1bc8 32 CMD_T * menu; // handle to the menu item
WiredHome 0:198f53da1bc8 33 struct CMDLINK_T * next; // handle to the next link
WiredHome 0:198f53da1bc8 34 } CMDLINK_T;
WiredHome 0:198f53da1bc8 35
WiredHome 0:198f53da1bc8 36 static CMDLINK_T * head = NULL;
WiredHome 0:198f53da1bc8 37
WiredHome 0:198f53da1bc8 38 static char *buffer; // buffer space must be allocated based on the longest command
WiredHome 0:198f53da1bc8 39
WiredHome 0:198f53da1bc8 40 static struct
WiredHome 0:198f53da1bc8 41 {
WiredHome 0:198f53da1bc8 42 int bufferSize; // size of the buffer
WiredHome 0:198f53da1bc8 43 int caseinsensitive; // FALSE=casesensitive, TRUE=insensitive
WiredHome 0:198f53da1bc8 44 int (*kbhit)(void);
WiredHome 0:198f53da1bc8 45 int (*getch)(void);
WiredHome 0:198f53da1bc8 46 int (*putch)(int ch);
WiredHome 0:198f53da1bc8 47 int (*puts)(const char * s);
WiredHome 0:198f53da1bc8 48 } cfg;
WiredHome 0:198f53da1bc8 49
WiredHome 0:198f53da1bc8 50 static INITRESULT_T CommandProcessor_Init(
WiredHome 0:198f53da1bc8 51 int defaultMenu,
WiredHome 0:198f53da1bc8 52 int caseinsensitive,
WiredHome 0:198f53da1bc8 53 int maxCmdLen,
WiredHome 0:198f53da1bc8 54 int (*kbhit)(void),
WiredHome 0:198f53da1bc8 55 int (*getch)(void),
WiredHome 0:198f53da1bc8 56 int (*putch)(int ch),
WiredHome 0:198f53da1bc8 57 int (*puts)(const char * s)
WiredHome 0:198f53da1bc8 58 );
WiredHome 0:198f53da1bc8 59 static ADDRESULT_T CommandProcessor_Add(CMD_T *m);
WiredHome 0:198f53da1bc8 60 static RUNRESULT_T CommandProcessor_Run(void);
WiredHome 0:198f53da1bc8 61 static RUNRESULT_T CommandProcessor_End(void);
WiredHome 0:198f53da1bc8 62
WiredHome 0:198f53da1bc8 63 static CMDP_T CommandProcessor =
WiredHome 0:198f53da1bc8 64 {
WiredHome 0:198f53da1bc8 65 CommandProcessor_Init,
WiredHome 0:198f53da1bc8 66 CommandProcessor_Add,
WiredHome 0:198f53da1bc8 67 CommandProcessor_Run,
WiredHome 0:198f53da1bc8 68 CommandProcessor_End
WiredHome 0:198f53da1bc8 69 };
WiredHome 0:198f53da1bc8 70
WiredHome 0:198f53da1bc8 71 static RUNRESULT_T Help(char *p);
WiredHome 0:198f53da1bc8 72 static RUNRESULT_T Exit(char *p);
WiredHome 0:198f53da1bc8 73 static RUNRESULT_T About(char *p);
WiredHome 0:198f53da1bc8 74
WiredHome 0:198f53da1bc8 75 static CMD_T HelpMenu = {"Help", "Shows this help, 'Help ?' shows more details.", Help, visible};
WiredHome 0:198f53da1bc8 76 static CMD_T AboutMenu = {"About", "About this CommandProcessor", About, visible};
WiredHome 0:198f53da1bc8 77 static CMD_T ExitMenu = {"Exit", "Exits the program", Exit, visible};
WiredHome 0:198f53da1bc8 78
WiredHome 0:198f53da1bc8 79 /// Gets a handle to the CommandProcessor
WiredHome 0:198f53da1bc8 80 ///
WiredHome 0:198f53da1bc8 81 /// This returns a handle to the CommandProcessor, which then permits
WiredHome 0:198f53da1bc8 82 /// access to the CommandProcessor functions.
WiredHome 0:198f53da1bc8 83 ///
WiredHome 0:198f53da1bc8 84 /// @returns handle to the CommandProcessor
WiredHome 0:198f53da1bc8 85 ///
WiredHome 0:198f53da1bc8 86 CMDP_T * GetCommandProcessor(void)
WiredHome 0:198f53da1bc8 87 {
WiredHome 0:198f53da1bc8 88 return &CommandProcessor;
WiredHome 0:198f53da1bc8 89 }
WiredHome 0:198f53da1bc8 90
WiredHome 0:198f53da1bc8 91
WiredHome 0:198f53da1bc8 92 static RUNRESULT_T About(char *p)
WiredHome 0:198f53da1bc8 93 {
WiredHome 0:198f53da1bc8 94 cfg.puts("\r\n About this CommandProcessor:\r\n"
WiredHome 0:198f53da1bc8 95 " This CommandProcessor provides easy facility for creating a\r\n"
WiredHome 0:198f53da1bc8 96 " runtime CommandProcessor in an embedded systems.\r\n"
WiredHome 0:198f53da1bc8 97 " author: David Smart, Smartware Computing\r\n"
WiredHome 0:198f53da1bc8 98 );
WiredHome 0:198f53da1bc8 99 return runok;
WiredHome 0:198f53da1bc8 100 }
WiredHome 0:198f53da1bc8 101
WiredHome 0:198f53da1bc8 102 static RUNRESULT_T Exit(char *p)
WiredHome 0:198f53da1bc8 103 {
WiredHome 0:198f53da1bc8 104 (void)p;
WiredHome 0:198f53da1bc8 105
WiredHome 0:198f53da1bc8 106 cfg.puts("\r\nbye.");
WiredHome 0:198f53da1bc8 107 return runexit;
WiredHome 0:198f53da1bc8 108 }
WiredHome 0:198f53da1bc8 109
WiredHome 0:198f53da1bc8 110 static RUNRESULT_T Help(char *p)
WiredHome 0:198f53da1bc8 111 {
WiredHome 0:198f53da1bc8 112 CMDLINK_T *link = head;
WiredHome 0:198f53da1bc8 113 char buffer[100];
WiredHome 0:198f53da1bc8 114
WiredHome 0:198f53da1bc8 115 cfg.puts("\r\n");
WiredHome 0:198f53da1bc8 116 sprintf(buffer, " %-10s: %s", "Command", "Description");
WiredHome 0:198f53da1bc8 117 cfg.puts(buffer);
WiredHome 0:198f53da1bc8 118 while (link && link->menu)
WiredHome 0:198f53da1bc8 119 {
WiredHome 0:198f53da1bc8 120 if (link->menu->visible)
WiredHome 0:198f53da1bc8 121 {
WiredHome 0:198f53da1bc8 122 if (strlen(link->menu->command) + strlen(link->menu->helptext) + 5 < sizeof(buffer))
WiredHome 0:198f53da1bc8 123 {
WiredHome 0:198f53da1bc8 124 sprintf(buffer, " %-10s: %s", link->menu->command, link->menu->helptext);
WiredHome 0:198f53da1bc8 125 cfg.puts(buffer);
WiredHome 0:198f53da1bc8 126 }
WiredHome 0:198f53da1bc8 127 }
WiredHome 0:198f53da1bc8 128 link = link->next;
WiredHome 0:198f53da1bc8 129 }
WiredHome 0:198f53da1bc8 130 cfg.puts("");
WiredHome 0:198f53da1bc8 131 if (*p == '?')
WiredHome 0:198f53da1bc8 132 {
WiredHome 0:198f53da1bc8 133 cfg.puts("\r\n Extended Help\r\n"
WiredHome 0:198f53da1bc8 134 " The general form for entering commands is:\r\n"
WiredHome 0:198f53da1bc8 135 " >command option1 option2 ...\r\n"
WiredHome 0:198f53da1bc8 136 " [note that note all commands support optional parameters]\r\n"
WiredHome 0:198f53da1bc8 137 " * Abbreviations of commands may be entered so long as there\r\n"
WiredHome 0:198f53da1bc8 138 " is exactly one match in the list of commands. For example,\r\n"
WiredHome 0:198f53da1bc8 139 " 'he' is the same as 'help', if there is no other command \r\n"
WiredHome 0:198f53da1bc8 140 " that starts with 'he'.\r\n"
WiredHome 0:198f53da1bc8 141 " * <bs> can be used to erase previously entered information.\r\n"
WiredHome 0:198f53da1bc8 142 " * <esc> can be used to cancel a command.\r\n"
WiredHome 0:198f53da1bc8 143 " * <tab> can be used to complete the entry of a partial command.\r\n"
WiredHome 0:198f53da1bc8 144 "");
WiredHome 0:198f53da1bc8 145 }
WiredHome 0:198f53da1bc8 146 return runok;
WiredHome 0:198f53da1bc8 147 }
WiredHome 0:198f53da1bc8 148
WiredHome 0:198f53da1bc8 149 /// mytolower exists because not all compiler libraries have this function
WiredHome 0:198f53da1bc8 150 ///
WiredHome 0:198f53da1bc8 151 /// This takes a character and if it is upper-case, it converts it to
WiredHome 0:198f53da1bc8 152 /// lower-case and returns it.
WiredHome 0:198f53da1bc8 153 ///
WiredHome 0:198f53da1bc8 154 /// @param a is the character to convert
WiredHome 0:198f53da1bc8 155 /// @returns the lower case equivalent to a
WiredHome 0:198f53da1bc8 156 ///
WiredHome 0:198f53da1bc8 157 char mytolower(char a)
WiredHome 0:198f53da1bc8 158 {
WiredHome 0:198f53da1bc8 159 if (a >= 'A' && a <= 'Z')
WiredHome 0:198f53da1bc8 160 return (a - 'A' + 'a');
WiredHome 0:198f53da1bc8 161 else
WiredHome 0:198f53da1bc8 162 return a;
WiredHome 0:198f53da1bc8 163 }
WiredHome 0:198f53da1bc8 164
WiredHome 0:198f53da1bc8 165 /// mystrnicmp exists because not all compiler libraries have this function.
WiredHome 0:198f53da1bc8 166 ///
WiredHome 0:198f53da1bc8 167 /// Some have strnicmp, others _strnicmp, and others have C++ methods, which
WiredHome 0:198f53da1bc8 168 /// is outside the scope of this C-portable set of functions.
WiredHome 0:198f53da1bc8 169 ///
WiredHome 0:198f53da1bc8 170 /// @param l is a pointer to the string on the left
WiredHome 0:198f53da1bc8 171 /// @param r is a pointer to the string on the right
WiredHome 0:198f53da1bc8 172 /// @param n is the number of characters to compare
WiredHome 0:198f53da1bc8 173 /// @returns -1 if l < r
WiredHome 0:198f53da1bc8 174 /// @returns 0 if l == r
WiredHome 0:198f53da1bc8 175 /// @returns +1 if l > r
WiredHome 0:198f53da1bc8 176 ///
WiredHome 0:198f53da1bc8 177 int mystrnicmp(const char *l, const char *r, size_t n)
WiredHome 0:198f53da1bc8 178 {
WiredHome 0:198f53da1bc8 179 int result = 0;
WiredHome 0:198f53da1bc8 180
WiredHome 0:198f53da1bc8 181 if (n != 0)
WiredHome 0:198f53da1bc8 182 {
WiredHome 0:198f53da1bc8 183 do {
WiredHome 0:198f53da1bc8 184 result = mytolower(*l++) - mytolower(*r++);
WiredHome 0:198f53da1bc8 185 } while ((result == 0) && (*l != '\0') && (--n > 0));
WiredHome 0:198f53da1bc8 186 }
WiredHome 0:198f53da1bc8 187 if (result < -1)
WiredHome 0:198f53da1bc8 188 result = -1;
WiredHome 0:198f53da1bc8 189 else if (result > 1)
WiredHome 0:198f53da1bc8 190 result = 1;
WiredHome 0:198f53da1bc8 191 return result;
WiredHome 0:198f53da1bc8 192 }
WiredHome 0:198f53da1bc8 193
WiredHome 0:198f53da1bc8 194 /// mystrcat exists because not all compiler libraries have this function
WiredHome 0:198f53da1bc8 195 ///
WiredHome 0:198f53da1bc8 196 /// This function concatinates one string onto another. It is generally
WiredHome 0:198f53da1bc8 197 /// considered unsafe, because of the potential for buffer overflow.
WiredHome 0:198f53da1bc8 198 /// Some libraries offer a strcat_s as the safe version, and others may have
WiredHome 0:198f53da1bc8 199 /// _strcat. Because this is needed only internal to the CommandProcessor,
WiredHome 0:198f53da1bc8 200 /// this version was created.
WiredHome 0:198f53da1bc8 201 ///
WiredHome 0:198f53da1bc8 202 /// @param dst is a pointer to the destination string
WiredHome 0:198f53da1bc8 203 /// @param src is a pointer to the source string
WiredHome 0:198f53da1bc8 204 /// @returns nothing
WiredHome 0:198f53da1bc8 205 ///
WiredHome 0:198f53da1bc8 206 void mystrcat(char *dst, char *src)
WiredHome 0:198f53da1bc8 207 {
WiredHome 0:198f53da1bc8 208 while (*dst)
WiredHome 0:198f53da1bc8 209 dst++;
WiredHome 0:198f53da1bc8 210 do
WiredHome 0:198f53da1bc8 211 *dst++ = *src;
WiredHome 0:198f53da1bc8 212 while (*src++);
WiredHome 0:198f53da1bc8 213 }
WiredHome 0:198f53da1bc8 214
WiredHome 0:198f53da1bc8 215 /// myisprint exists because not all compiler libraries have this function
WiredHome 0:198f53da1bc8 216 ///
WiredHome 0:198f53da1bc8 217 /// This function tests a character to see if it is printable (a member
WiredHome 0:198f53da1bc8 218 /// of the standard ASCII set).
WiredHome 0:198f53da1bc8 219 ///
WiredHome 0:198f53da1bc8 220 /// @param c is the character to test
WiredHome 0:198f53da1bc8 221 /// @returns TRUE if the character is printable
WiredHome 0:198f53da1bc8 222 /// @returns FALSE if the character is not printable
WiredHome 0:198f53da1bc8 223 ///
WiredHome 0:198f53da1bc8 224 static int myisprint(int c)
WiredHome 0:198f53da1bc8 225 {
WiredHome 0:198f53da1bc8 226 if (c >= ' ' && c <= '~')
WiredHome 0:198f53da1bc8 227 return TRUE;
WiredHome 0:198f53da1bc8 228 else
WiredHome 0:198f53da1bc8 229 return FALSE;
WiredHome 0:198f53da1bc8 230 }
WiredHome 0:198f53da1bc8 231
WiredHome 0:198f53da1bc8 232
WiredHome 0:198f53da1bc8 233 /// CommandMatches is the function that determines if the user is entering a valid
WiredHome 0:198f53da1bc8 234 /// command
WiredHome 0:198f53da1bc8 235 ///
WiredHome 0:198f53da1bc8 236 /// This function gets called whenever the user types a printable character or when
WiredHome 0:198f53da1bc8 237 /// they press \<enter\>.
WiredHome 0:198f53da1bc8 238 /// The buffer of user input is evaluated to determine if the command is legitimate.
WiredHome 0:198f53da1bc8 239 /// It also determines if they want to execute the command, or simply evaluate it.
WiredHome 0:198f53da1bc8 240 /// Finally, it identifies writes to user provided pointers, the menu item that
WiredHome 0:198f53da1bc8 241 /// matches and a pointer to any parameters that the user entered.
WiredHome 0:198f53da1bc8 242 ///
WiredHome 0:198f53da1bc8 243 /// @param buffer is the buffer that the user has entered commands into
WiredHome 0:198f53da1bc8 244 /// @param exec indicates the intention to execute the command, if found
WiredHome 0:198f53da1bc8 245 /// @param menu is a pointer to a pointer to a menu structure, which is updated based on the command
WiredHome 0:198f53da1bc8 246 /// @param params is a pointer to a pointer to the user entered parameters
WiredHome 0:198f53da1bc8 247 /// @returns the number of menu picks that match the user entered command
WiredHome 0:198f53da1bc8 248 ///
WiredHome 0:198f53da1bc8 249 static int CommandMatches(char * buffer, int exec, CMD_T **menu, char ** params)
WiredHome 0:198f53da1bc8 250 {
WiredHome 0:198f53da1bc8 251 char *space;
WiredHome 0:198f53da1bc8 252 int compareLength;
WiredHome 0:198f53da1bc8 253 int foundCount = 0;
WiredHome 0:198f53da1bc8 254 CMDLINK_T *link = head;
WiredHome 0:198f53da1bc8 255
WiredHome 0:198f53da1bc8 256 if (strlen(buffer)) // simple sanity check
WiredHome 0:198f53da1bc8 257 {
WiredHome 0:198f53da1bc8 258 // Try to process the buffer. A command could be "Help", or it could be "Test1 123 abc"
WiredHome 0:198f53da1bc8 259 space = strchr(buffer, ' '); // if the command has parameters, find the delimiter
WiredHome 0:198f53da1bc8 260 if (space)
WiredHome 0:198f53da1bc8 261 {
WiredHome 0:198f53da1bc8 262 compareLength = space - buffer;
WiredHome 0:198f53da1bc8 263 space++; // char after the space
WiredHome 0:198f53da1bc8 264 }
WiredHome 0:198f53da1bc8 265 else
WiredHome 0:198f53da1bc8 266 {
WiredHome 0:198f53da1bc8 267 compareLength = strlen(buffer);
WiredHome 0:198f53da1bc8 268 space = buffer + compareLength;
WiredHome 0:198f53da1bc8 269 }
WiredHome 0:198f53da1bc8 270 while (link && link->menu)
WiredHome 0:198f53da1bc8 271 {
WiredHome 0:198f53da1bc8 272 int cmpResult;
WiredHome 0:198f53da1bc8 273
WiredHome 0:198f53da1bc8 274 if (cfg.caseinsensitive)
WiredHome 0:198f53da1bc8 275 {
WiredHome 0:198f53da1bc8 276 cmpResult = mystrnicmp(buffer, link->menu->command, compareLength);
WiredHome 0:198f53da1bc8 277 }
WiredHome 0:198f53da1bc8 278 else
WiredHome 0:198f53da1bc8 279 cmpResult = strncmp(buffer, link->menu->command, compareLength);
WiredHome 0:198f53da1bc8 280 if (0 == cmpResult) // A match to what they typed
WiredHome 0:198f53da1bc8 281 {
WiredHome 0:198f53da1bc8 282 if (menu) // yes, we have a callback
WiredHome 0:198f53da1bc8 283 {
WiredHome 0:198f53da1bc8 284 if (exec) // command wants to execute it, not just validate the command syntax
WiredHome 0:198f53da1bc8 285 {
WiredHome 0:198f53da1bc8 286 // If they type "He 1234 5678", we backup and rewrite as "Help 1234 5678"
WiredHome 0:198f53da1bc8 287 int diff = strlen(link->menu->command) - compareLength; // e.g. 5 - 3
WiredHome 0:198f53da1bc8 288
WiredHome 0:198f53da1bc8 289 if (diff > 0)
WiredHome 0:198f53da1bc8 290 {
WiredHome 0:198f53da1bc8 291 int back = 0;
WiredHome 0:198f53da1bc8 292 char *p;
WiredHome 0:198f53da1bc8 293 if (strlen(space))
WiredHome 0:198f53da1bc8 294 back = strlen(space) + 1;
WiredHome 0:198f53da1bc8 295
WiredHome 0:198f53da1bc8 296 while (back--)
WiredHome 0:198f53da1bc8 297 cfg.putch(0x08);
WiredHome 0:198f53da1bc8 298 p = link->menu->command + compareLength;
WiredHome 0:198f53da1bc8 299 while (*p)
WiredHome 0:198f53da1bc8 300 cfg.putch(*p++);
WiredHome 0:198f53da1bc8 301 cfg.putch(' ');
WiredHome 0:198f53da1bc8 302 p = space;
WiredHome 0:198f53da1bc8 303 while (*p)
WiredHome 0:198f53da1bc8 304 cfg.putch(*p++);
WiredHome 0:198f53da1bc8 305 //cfg.printf("%s %s", link->menu->command + compareLength, space);
WiredHome 0:198f53da1bc8 306 }
WiredHome 0:198f53da1bc8 307 }
WiredHome 0:198f53da1bc8 308 *menu = link->menu; // accessor to the command they want to execute
WiredHome 0:198f53da1bc8 309 *params = space;
WiredHome 0:198f53da1bc8 310 }
WiredHome 0:198f53da1bc8 311 foundCount++; // how many command match what they typed so far
WiredHome 0:198f53da1bc8 312 }
WiredHome 0:198f53da1bc8 313 link = link->next; // follow the link to the next command
WiredHome 0:198f53da1bc8 314 }
WiredHome 0:198f53da1bc8 315 }
WiredHome 0:198f53da1bc8 316 return foundCount;
WiredHome 0:198f53da1bc8 317 }
WiredHome 0:198f53da1bc8 318
WiredHome 0:198f53da1bc8 319 /// Initialize the CommandProcessor
WiredHome 0:198f53da1bc8 320 ///
WiredHome 0:198f53da1bc8 321 /// This initializes the CommandProcessor by adding the built-in menus
WiredHome 0:198f53da1bc8 322 ///
WiredHome 0:198f53da1bc8 323 /// @param addDefaultMenu configures it to add the Help, About, and Exit menus
WiredHome 0:198f53da1bc8 324 /// @param bufSize configures the size of the longest command, which must be
WiredHome 0:198f53da1bc8 325 /// greater than 6 (the size of "About\0").
WiredHome 0:198f53da1bc8 326 /// @returns initok if it successfully initialized the CommandProcessor
WiredHome 0:198f53da1bc8 327 /// @returns initfailed if it could not allocate memory
WiredHome 0:198f53da1bc8 328 ///
WiredHome 0:198f53da1bc8 329 INITRESULT_T CommandProcessor_Init(
WiredHome 0:198f53da1bc8 330 int addDefaultMenu,
WiredHome 0:198f53da1bc8 331 int caseinsensitive,
WiredHome 0:198f53da1bc8 332 int maxCmdLen,
WiredHome 0:198f53da1bc8 333 int (*kbhit)(void),
WiredHome 0:198f53da1bc8 334 int (*getch)(void),
WiredHome 0:198f53da1bc8 335 int (*putch)(int ch),
WiredHome 0:198f53da1bc8 336 int (*puts)(const char * s)
WiredHome 0:198f53da1bc8 337 )
WiredHome 0:198f53da1bc8 338 {
WiredHome 0:198f53da1bc8 339 if (maxCmdLen < 6)
WiredHome 0:198f53da1bc8 340 maxCmdLen = 6;
WiredHome 0:198f53da1bc8 341 buffer = (char *)malloc(maxCmdLen+1);
WiredHome 0:198f53da1bc8 342 cfg.bufferSize = maxCmdLen;
WiredHome 0:198f53da1bc8 343 if (buffer)
WiredHome 0:198f53da1bc8 344 {
WiredHome 0:198f53da1bc8 345 if (addDefaultMenu & 0x0004)
WiredHome 0:198f53da1bc8 346 CommandProcessor.Add(&HelpMenu);
WiredHome 0:198f53da1bc8 347 if (addDefaultMenu & 0x0002)
WiredHome 0:198f53da1bc8 348 CommandProcessor.Add(&AboutMenu);
WiredHome 0:198f53da1bc8 349 if (addDefaultMenu & 0x0001)
WiredHome 0:198f53da1bc8 350 CommandProcessor.Add(&ExitMenu);
WiredHome 0:198f53da1bc8 351 cfg.caseinsensitive = caseinsensitive;
WiredHome 0:198f53da1bc8 352 cfg.kbhit = kbhit;
WiredHome 0:198f53da1bc8 353 cfg.getch = getch;
WiredHome 0:198f53da1bc8 354 cfg.putch = putch;
WiredHome 0:198f53da1bc8 355 cfg.puts = puts;
WiredHome 0:198f53da1bc8 356 return initok;
WiredHome 0:198f53da1bc8 357 }
WiredHome 0:198f53da1bc8 358 else
WiredHome 0:198f53da1bc8 359 return initfailed;
WiredHome 0:198f53da1bc8 360 }
WiredHome 0:198f53da1bc8 361
WiredHome 0:198f53da1bc8 362 /// Add a command to the CommandProcessor
WiredHome 0:198f53da1bc8 363 ///
WiredHome 0:198f53da1bc8 364 /// This adds a command to the CommandProcessor. A command has several components
WiredHome 0:198f53da1bc8 365 /// to it, including the command name, a brief description, the function to
WiredHome 0:198f53da1bc8 366 /// activate when the command is entered, and a flag indicating if the command
WiredHome 0:198f53da1bc8 367 /// should show up in the built-in help.
WiredHome 0:198f53da1bc8 368 ///
WiredHome 0:198f53da1bc8 369 /// @todo sort them when adding a menu item
WiredHome 0:198f53da1bc8 370 ///
WiredHome 0:198f53da1bc8 371 /// @param m is the menu to add to the CommandProcessor
WiredHome 0:198f53da1bc8 372 /// @returns addok if the command was added
WiredHome 0:198f53da1bc8 373 /// @returns addfail if the command could not be added (failure to allocate memory for the linked list)
WiredHome 0:198f53da1bc8 374 ///
WiredHome 0:198f53da1bc8 375 ADDRESULT_T CommandProcessor_Add(CMD_T * m)
WiredHome 0:198f53da1bc8 376 {
WiredHome 0:198f53da1bc8 377 CMDLINK_T *p;
WiredHome 0:198f53da1bc8 378
WiredHome 0:198f53da1bc8 379 if (head == NULL)
WiredHome 0:198f53da1bc8 380 {
WiredHome 0:198f53da1bc8 381 head = (CMDLINK_T *)malloc(sizeof(CMDLINK_T));
WiredHome 0:198f53da1bc8 382 if (!head)
WiredHome 0:198f53da1bc8 383 return addfailed;
WiredHome 0:198f53da1bc8 384 head->menu = NULL;
WiredHome 0:198f53da1bc8 385 head->next = NULL;
WiredHome 0:198f53da1bc8 386 }
WiredHome 0:198f53da1bc8 387 p = head;
WiredHome 0:198f53da1bc8 388 while (p->next)
WiredHome 0:198f53da1bc8 389 p = p->next;
WiredHome 0:198f53da1bc8 390 if (p->menu == NULL)
WiredHome 0:198f53da1bc8 391 {
WiredHome 0:198f53da1bc8 392 p->menu = m;
WiredHome 0:198f53da1bc8 393 return addok;
WiredHome 0:198f53da1bc8 394 }
WiredHome 0:198f53da1bc8 395 else if (p->next == NULL)
WiredHome 0:198f53da1bc8 396 {
WiredHome 0:198f53da1bc8 397 p->next = (CMDLINK_T *)malloc(sizeof(CMDLINK_T));
WiredHome 0:198f53da1bc8 398 if (!p->next)
WiredHome 0:198f53da1bc8 399 return addfailed;
WiredHome 0:198f53da1bc8 400 p = p->next;
WiredHome 0:198f53da1bc8 401 p->menu = m;
WiredHome 0:198f53da1bc8 402 p->next = NULL;
WiredHome 0:198f53da1bc8 403 return addok;
WiredHome 0:198f53da1bc8 404 }
WiredHome 0:198f53da1bc8 405 return addfailed;
WiredHome 0:198f53da1bc8 406 }
WiredHome 0:198f53da1bc8 407
WiredHome 0:198f53da1bc8 408 /// Run the CommandProcessor
WiredHome 0:198f53da1bc8 409 ///
WiredHome 0:198f53da1bc8 410 /// This will peek to see if there is a keystroke ready. It will pull that into a
WiredHome 0:198f53da1bc8 411 /// buffer if it is part of a valid command in the command set. You may then enter
WiredHome 0:198f53da1bc8 412 /// arguments to the command to be run.
WiredHome 0:198f53da1bc8 413 ///
WiredHome 0:198f53da1bc8 414 /// Primitive editing is permitted with <bs>.
WiredHome 0:198f53da1bc8 415 ///
WiredHome 0:198f53da1bc8 416 /// When you press <enter> it will evaluate the command and execute the command
WiredHome 0:198f53da1bc8 417 /// passing it the parameter string.
WiredHome 0:198f53da1bc8 418 ///
WiredHome 0:198f53da1bc8 419 /// @returns runok if the command that was run allows continuation of the CommandProcessor
WiredHome 0:198f53da1bc8 420 /// @returns runfail if the command that was run is asking the CommandProcessor to exit
WiredHome 0:198f53da1bc8 421 ///
WiredHome 0:198f53da1bc8 422 RUNRESULT_T CommandProcessor_Run(void)
WiredHome 0:198f53da1bc8 423 {
WiredHome 0:198f53da1bc8 424 static int showPrompt = TRUE;
WiredHome 0:198f53da1bc8 425 static int keycount = 0; // how full?
WiredHome 0:198f53da1bc8 426 RUNRESULT_T val = runok; // return true when happy, false to exit the prog
WiredHome 0:198f53da1bc8 427 CMD_T *cbk = NULL;
WiredHome 0:198f53da1bc8 428 char * params = NULL;
WiredHome 0:198f53da1bc8 429
WiredHome 0:198f53da1bc8 430 if (showPrompt)
WiredHome 0:198f53da1bc8 431 {
WiredHome 0:198f53da1bc8 432 cfg.putch('>');
WiredHome 0:198f53da1bc8 433 showPrompt = FALSE;
WiredHome 0:198f53da1bc8 434 }
WiredHome 0:198f53da1bc8 435 if (cfg.kbhit())
WiredHome 0:198f53da1bc8 436 {
WiredHome 0:198f53da1bc8 437 int c = cfg.getch();
WiredHome 0:198f53da1bc8 438
WiredHome 0:198f53da1bc8 439 switch (c)
WiredHome 0:198f53da1bc8 440 {
WiredHome 0:198f53da1bc8 441 case 0x09: // <TAB> to request command completion
WiredHome 0:198f53da1bc8 442 if (1 == CommandMatches(buffer, FALSE, &cbk, &params))
WiredHome 0:198f53da1bc8 443 {
WiredHome 0:198f53da1bc8 444 size_t n;
WiredHome 0:198f53da1bc8 445 char *p = strchr(buffer, ' ');
WiredHome 0:198f53da1bc8 446 if (p)
WiredHome 0:198f53da1bc8 447 n = p - buffer;
WiredHome 0:198f53da1bc8 448 else
WiredHome 0:198f53da1bc8 449 n = strlen(buffer);
WiredHome 0:198f53da1bc8 450 if (n < strlen(cbk->command))
WiredHome 0:198f53da1bc8 451 {
WiredHome 0:198f53da1bc8 452 p = cbk->command + strlen(buffer);
WiredHome 0:198f53da1bc8 453 mystrcat(buffer, p);
WiredHome 0:198f53da1bc8 454 keycount = strlen(buffer);
WiredHome 0:198f53da1bc8 455 while (*p)
WiredHome 0:198f53da1bc8 456 cfg.putch(*p++);
WiredHome 0:198f53da1bc8 457 //cfg.printf("%s", p);
WiredHome 0:198f53da1bc8 458 }
WiredHome 0:198f53da1bc8 459 }
WiredHome 0:198f53da1bc8 460 break;
WiredHome 0:198f53da1bc8 461 case 0x1b: // <ESC> to empty the command buffer
WiredHome 0:198f53da1bc8 462 while (keycount--)
WiredHome 0:198f53da1bc8 463 {
WiredHome 0:198f53da1bc8 464 cfg.putch(0x08); // <bs>
WiredHome 0:198f53da1bc8 465 cfg.putch(' ');
WiredHome 0:198f53da1bc8 466 cfg.putch(0x08);
WiredHome 0:198f53da1bc8 467 }
WiredHome 0:198f53da1bc8 468 keycount = 0;
WiredHome 0:198f53da1bc8 469 buffer[keycount] = '\0';
WiredHome 0:198f53da1bc8 470 break;
WiredHome 0:198f53da1bc8 471 case '\x08': // <bs>
WiredHome 0:198f53da1bc8 472 if (keycount)
WiredHome 0:198f53da1bc8 473 {
WiredHome 0:198f53da1bc8 474 buffer[--keycount] = '\0';
WiredHome 0:198f53da1bc8 475 cfg.putch(0x08);
WiredHome 0:198f53da1bc8 476 cfg.putch(' ');
WiredHome 0:198f53da1bc8 477 cfg.putch(0x08);
WiredHome 0:198f53da1bc8 478 }
WiredHome 0:198f53da1bc8 479 else
WiredHome 0:198f53da1bc8 480 cfg.putch(0x07); // bell
WiredHome 0:198f53da1bc8 481 break;
WiredHome 0:198f53da1bc8 482 case '\r':
WiredHome 0:198f53da1bc8 483 case '\n':
WiredHome 0:198f53da1bc8 484 {
WiredHome 0:198f53da1bc8 485 int foundCount = 0;
WiredHome 0:198f53da1bc8 486
WiredHome 0:198f53da1bc8 487 if (strlen(buffer))
WiredHome 0:198f53da1bc8 488 {
WiredHome 0:198f53da1bc8 489 foundCount = CommandMatches(buffer, TRUE, &cbk, &params);
WiredHome 0:198f53da1bc8 490 if (foundCount == 1)
WiredHome 0:198f53da1bc8 491 {
WiredHome 0:198f53da1bc8 492 val = (*cbk->callback)(params); // Execute the command
WiredHome 0:198f53da1bc8 493 }
WiredHome 0:198f53da1bc8 494 else if (foundCount > 1)
WiredHome 0:198f53da1bc8 495 cfg.puts(" *** non-unique command ignored try 'Help' ***");
WiredHome 0:198f53da1bc8 496 else if (foundCount == 0)
WiredHome 0:198f53da1bc8 497 cfg.puts(" *** huh? try 'Help' ***");
WiredHome 0:198f53da1bc8 498 }
WiredHome 0:198f53da1bc8 499 else
WiredHome 0:198f53da1bc8 500 cfg.puts("");
WiredHome 0:198f53da1bc8 501 keycount = 0;
WiredHome 0:198f53da1bc8 502 buffer[keycount] = '\0';
WiredHome 0:198f53da1bc8 503 showPrompt = TRUE; // forces the prompt
WiredHome 0:198f53da1bc8 504 }
WiredHome 0:198f53da1bc8 505 break;
WiredHome 0:198f53da1bc8 506 default:
WiredHome 0:198f53da1bc8 507 if (myisprint(c) && keycount < cfg.bufferSize)
WiredHome 0:198f53da1bc8 508 {
WiredHome 0:198f53da1bc8 509 buffer[keycount++] = c;
WiredHome 0:198f53da1bc8 510 buffer[keycount] = '\0';
WiredHome 0:198f53da1bc8 511 if (CommandMatches(buffer, FALSE, &cbk, &params))
WiredHome 0:198f53da1bc8 512 cfg.putch(c);
WiredHome 0:198f53da1bc8 513 else
WiredHome 0:198f53da1bc8 514 {
WiredHome 0:198f53da1bc8 515 buffer[--keycount] = '\0';
WiredHome 0:198f53da1bc8 516 cfg.putch(0x07); // bell
WiredHome 0:198f53da1bc8 517 }
WiredHome 0:198f53da1bc8 518 }
WiredHome 0:198f53da1bc8 519 else
WiredHome 0:198f53da1bc8 520 cfg.putch(0x07); // bell
WiredHome 0:198f53da1bc8 521 break;
WiredHome 0:198f53da1bc8 522 }
WiredHome 0:198f53da1bc8 523 }
WiredHome 0:198f53da1bc8 524 return val;
WiredHome 0:198f53da1bc8 525 }
WiredHome 0:198f53da1bc8 526
WiredHome 0:198f53da1bc8 527
WiredHome 0:198f53da1bc8 528 /// End the CommandProcessor by freeing all the memory that was allocated
WiredHome 0:198f53da1bc8 529 ///
WiredHome 0:198f53da1bc8 530 /// returns runok
WiredHome 0:198f53da1bc8 531 ///
WiredHome 0:198f53da1bc8 532 RUNRESULT_T CommandProcessor_End(void)
WiredHome 0:198f53da1bc8 533 {
WiredHome 0:198f53da1bc8 534 CMDLINK_T *p = head;
WiredHome 0:198f53da1bc8 535 CMDLINK_T *n;
WiredHome 0:198f53da1bc8 536
WiredHome 0:198f53da1bc8 537 do
WiredHome 0:198f53da1bc8 538 {
WiredHome 0:198f53da1bc8 539 n = p->next;
WiredHome 0:198f53da1bc8 540 free(p); // free each of the allocated links to menu items.
WiredHome 0:198f53da1bc8 541 p = n;
WiredHome 0:198f53da1bc8 542 } while (n);
WiredHome 0:198f53da1bc8 543 free(buffer); // finally, free the command buffer
WiredHome 0:198f53da1bc8 544 buffer = NULL; // flag it as deallocated
WiredHome 0:198f53da1bc8 545 return runok;
WiredHome 0:198f53da1bc8 546 }