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

Dependents:   A_CANAdapter USB2I2C

Committer:
WiredHome
Date:
Sat Oct 01 20:01:44 2011 +0000
Revision:
14:7971c8bd3f11
Parent:
13:e1880be590c4
Child:
15:5f30da93e3e2
v1.04 Adds a simple command history permitting recall of previous commands.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 7:0f058d664b21 1 /// @file CommandProcessor.c is a simple interface to an interactive
WiredHome 7:0f058d664b21 2 /// command set of user defined commands.
WiredHome 0:198f53da1bc8 3 ///
WiredHome 7:0f058d664b21 4 /// With this, you can create functions that are exposed to a console
WiredHome 7:0f058d664b21 5 /// interface. Each command may have 0 or more parameters.
WiredHome 7:0f058d664b21 6 /// Typing the command, or at least the set of characters that make
WiredHome 7:0f058d664b21 7 /// it unique from all other commands is enough to activate the command.
WiredHome 7:0f058d664b21 8 ///
WiredHome 7:0f058d664b21 9 /// Even though it is a c interface, it is somewhat object oriented.
WiredHome 0:198f53da1bc8 10 ///
WiredHome 14:7971c8bd3f11 11 /// @version 1.04
WiredHome 0:198f53da1bc8 12 ///
WiredHome 12:a8c56bf811b9 13 /// @note Copyright &copr; 2011 by Smartware Computing, all rights reserved.
WiredHome 12:a8c56bf811b9 14 /// Individuals may use this application for evaluation or non-commercial
WiredHome 12:a8c56bf811b9 15 /// purposes. Within this restriction, changes may be made to this application
WiredHome 12:a8c56bf811b9 16 /// as long as this copyright notice is retained. The user shall make
WiredHome 12:a8c56bf811b9 17 /// clear that their work is a derived work, and not the original.
WiredHome 12:a8c56bf811b9 18 /// Users of this application and sources accept this application "as is" and
WiredHome 12:a8c56bf811b9 19 /// shall hold harmless Smartware Computing, for any undesired results while
WiredHome 12:a8c56bf811b9 20 /// using this application - whether real or imagined.
WiredHome 0:198f53da1bc8 21 ///
WiredHome 12:a8c56bf811b9 22 /// @author David Smart, Smartware Computing
WiredHome 12:a8c56bf811b9 23 ///
WiredHome 12:a8c56bf811b9 24
WiredHome 0:198f53da1bc8 25 #include "stdio.h"
WiredHome 0:198f53da1bc8 26 #include "string.h"
WiredHome 0:198f53da1bc8 27 #include "stdlib.h"
WiredHome 0:198f53da1bc8 28 #ifdef WIN32
WiredHome 0:198f53da1bc8 29 #include "windows.h"
WiredHome 0:198f53da1bc8 30 #pragma warning (disable: 4996)
WiredHome 0:198f53da1bc8 31 #endif
WiredHome 0:198f53da1bc8 32
WiredHome 0:198f53da1bc8 33 #include "CommandProcessor.h"
WiredHome 0:198f53da1bc8 34
WiredHome 0:198f53da1bc8 35 /// This holds the single linked list of commands
WiredHome 11:4a3cd3f2183b 36 /// @verbatim
WiredHome 7:0f058d664b21 37 /// +-- Head->next
WiredHome 7:0f058d664b21 38 /// v
WiredHome 7:0f058d664b21 39 /// +-- p +-- n
WiredHome 7:0f058d664b21 40 /// v v
WiredHome 7:0f058d664b21 41 /// |menu|-------------------------------->|"Command" |
WiredHome 7:0f058d664b21 42 /// |next|->0 |menu|---->|"Help" |"Help" |
WiredHome 7:0f058d664b21 43 /// |next|->0 |"..." |*(callback)|
WiredHome 7:0f058d664b21 44 /// |*(callback) |visible |
WiredHome 7:0f058d664b21 45 /// |visible
WiredHome 11:4a3cd3f2183b 46 /// @endverbatim
WiredHome 0:198f53da1bc8 47 ///
WiredHome 7:0f058d664b21 48 typedef struct CMDLINK_T {
WiredHome 0:198f53da1bc8 49 CMD_T * menu; // handle to the menu item
WiredHome 0:198f53da1bc8 50 struct CMDLINK_T * next; // handle to the next link
WiredHome 0:198f53da1bc8 51 } CMDLINK_T;
WiredHome 0:198f53da1bc8 52
WiredHome 0:198f53da1bc8 53 static CMDLINK_T * head = NULL;
WiredHome 0:198f53da1bc8 54
WiredHome 0:198f53da1bc8 55 static char *buffer; // buffer space must be allocated based on the longest command
WiredHome 14:7971c8bd3f11 56 static char *historyBuffer; // keeps the history of commands for recall
WiredHome 14:7971c8bd3f11 57 static int historyCount = 0; // and the count of
WiredHome 14:7971c8bd3f11 58 static int historyDepth = 0;
WiredHome 13:e1880be590c4 59 static size_t longestCommand = 0;
WiredHome 12:a8c56bf811b9 60
WiredHome 7:0f058d664b21 61 static struct {
WiredHome 14:7971c8bd3f11 62 CMD_T *SignOnBanner;
WiredHome 14:7971c8bd3f11 63 int showSignOnBanner; // Shows the sign-on banner at startup
WiredHome 0:198f53da1bc8 64 int caseinsensitive; // FALSE=casesensitive, TRUE=insensitive
WiredHome 6:1a0512faa75d 65 int echo; // TRUE=echo on, FALSE=echo off
WiredHome 14:7971c8bd3f11 66 int bufferSize; // size of the command buffer
WiredHome 0:198f53da1bc8 67 int (*kbhit)(void);
WiredHome 0:198f53da1bc8 68 int (*getch)(void);
WiredHome 0:198f53da1bc8 69 int (*putch)(int ch);
WiredHome 0:198f53da1bc8 70 int (*puts)(const char * s);
WiredHome 0:198f53da1bc8 71 } cfg;
WiredHome 0:198f53da1bc8 72
WiredHome 0:198f53da1bc8 73 static INITRESULT_T CommandProcessor_Init(
WiredHome 10:9e52bd1a4a71 74 CMD_T *SignOnBanner,
WiredHome 14:7971c8bd3f11 75 CONFIG_T config,
WiredHome 7:0f058d664b21 76 int maxCmdLen,
WiredHome 14:7971c8bd3f11 77 int historyCount,
WiredHome 0:198f53da1bc8 78 int (*kbhit)(void),
WiredHome 0:198f53da1bc8 79 int (*getch)(void),
WiredHome 0:198f53da1bc8 80 int (*putch)(int ch),
WiredHome 0:198f53da1bc8 81 int (*puts)(const char * s)
WiredHome 7:0f058d664b21 82 );
WiredHome 0:198f53da1bc8 83 static ADDRESULT_T CommandProcessor_Add(CMD_T *m);
WiredHome 0:198f53da1bc8 84 static RUNRESULT_T CommandProcessor_Run(void);
WiredHome 0:198f53da1bc8 85 static RUNRESULT_T CommandProcessor_End(void);
WiredHome 6:1a0512faa75d 86 static RUNRESULT_T CommandProcessor_Echo(int echo);
WiredHome 14:7971c8bd3f11 87 static void EraseChars(int keycount);
WiredHome 14:7971c8bd3f11 88 static void EchoString(char * p);
WiredHome 0:198f53da1bc8 89
WiredHome 7:0f058d664b21 90 // helper functions
WiredHome 7:0f058d664b21 91 static int myisprint(int c);
WiredHome 7:0f058d664b21 92 static void mystrcat(char *dst, char *src);
WiredHome 7:0f058d664b21 93 static char mytolower(char a);
WiredHome 13:e1880be590c4 94 static int mystrnicmp(const char *l, const char *r, size_t n);
WiredHome 7:0f058d664b21 95
WiredHome 7:0f058d664b21 96 static CMDP_T CommandProcessor = {
WiredHome 0:198f53da1bc8 97 CommandProcessor_Init,
WiredHome 0:198f53da1bc8 98 CommandProcessor_Add,
WiredHome 0:198f53da1bc8 99 CommandProcessor_Run,
WiredHome 6:1a0512faa75d 100 CommandProcessor_Echo,
WiredHome 0:198f53da1bc8 101 CommandProcessor_End
WiredHome 0:198f53da1bc8 102 };
WiredHome 0:198f53da1bc8 103
WiredHome 0:198f53da1bc8 104 static RUNRESULT_T Help(char *p);
WiredHome 14:7971c8bd3f11 105 static RUNRESULT_T History(char *p);
WiredHome 6:1a0512faa75d 106 static RUNRESULT_T Echo(char *p);
WiredHome 0:198f53da1bc8 107 static RUNRESULT_T Exit(char *p);
WiredHome 10:9e52bd1a4a71 108 //static RUNRESULT_T About(char *p);
WiredHome 0:198f53da1bc8 109
WiredHome 9:41046d2fd8e7 110 static CMD_T HelpMenu = {"Help", "Help or '?' shows this help, 'Help ?' shows more details.", Help, visible};
WiredHome 9:41046d2fd8e7 111 static CMD_T QuestionMenu = {"?", "Shows this help, '? ?' shows more details.", Help, invisible};
WiredHome 14:7971c8bd3f11 112 static CMD_T HistoryMenu = {"History", "Show command history", History, visible};
WiredHome 6:1a0512faa75d 113 static CMD_T EchoMenu = {"Echo", "Echo [1|on|0|off] turns echo on or off.", Echo, visible};
WiredHome 0:198f53da1bc8 114 static CMD_T ExitMenu = {"Exit", "Exits the program", Exit, visible};
WiredHome 0:198f53da1bc8 115
WiredHome 0:198f53da1bc8 116 /// Gets a handle to the CommandProcessor
WiredHome 0:198f53da1bc8 117 ///
WiredHome 7:0f058d664b21 118 /// This returns a handle to the CommandProcessor, which then permits
WiredHome 7:0f058d664b21 119 /// access to the CommandProcessor functions.
WiredHome 0:198f53da1bc8 120 ///
WiredHome 7:0f058d664b21 121 /// @returns handle to the CommandProcessor
WiredHome 0:198f53da1bc8 122 ///
WiredHome 7:0f058d664b21 123 CMDP_T * GetCommandProcessor(void) {
WiredHome 0:198f53da1bc8 124 return &CommandProcessor;
WiredHome 0:198f53da1bc8 125 }
WiredHome 0:198f53da1bc8 126
WiredHome 0:198f53da1bc8 127
WiredHome 14:7971c8bd3f11 128 /// History shows the command history
WiredHome 7:0f058d664b21 129 ///
WiredHome 14:7971c8bd3f11 130 /// @param p is a pointer to a string that is ignored
WiredHome 7:0f058d664b21 131 /// @returns runok
WiredHome 7:0f058d664b21 132 ///
WiredHome 14:7971c8bd3f11 133 static RUNRESULT_T History(char *p) {
WiredHome 14:7971c8bd3f11 134 int whereInHistory = 0;
WiredHome 14:7971c8bd3f11 135 char buf[100];
WiredHome 14:7971c8bd3f11 136
WiredHome 14:7971c8bd3f11 137 cfg.puts("");
WiredHome 14:7971c8bd3f11 138 for (whereInHistory = 0; whereInHistory < historyCount; whereInHistory++) {
WiredHome 14:7971c8bd3f11 139 sprintf(buf, " %2i: %s", whereInHistory - historyCount, &historyBuffer[whereInHistory * cfg.bufferSize]);
WiredHome 14:7971c8bd3f11 140 cfg.puts(buf);
WiredHome 14:7971c8bd3f11 141 }
WiredHome 14:7971c8bd3f11 142 sprintf(buf, " %2i: %s", 0, buffer);
WiredHome 14:7971c8bd3f11 143 cfg.puts(buf);
WiredHome 14:7971c8bd3f11 144 return runok;
WiredHome 0:198f53da1bc8 145 }
WiredHome 0:198f53da1bc8 146
WiredHome 7:0f058d664b21 147 /// Turns command prompt echo on and off
WiredHome 7:0f058d664b21 148 ///
WiredHome 7:0f058d664b21 149 /// This command is used to turn the command prompt on and off. When
WiredHome 7:0f058d664b21 150 /// running in an interactive mode, it is best to have this one.
WiredHome 7:0f058d664b21 151 /// When driven by another program, off may be the best choice.
WiredHome 7:0f058d664b21 152 ///
WiredHome 7:0f058d664b21 153 /// This command also displays the current state of the echo mode.
WiredHome 7:0f058d664b21 154 ///
WiredHome 7:0f058d664b21 155 /// @param p is a pointer to a string "on" | "1" | "off" | "0"
WiredHome 7:0f058d664b21 156 /// @returns runok
WiredHome 7:0f058d664b21 157 ///
WiredHome 6:1a0512faa75d 158 static RUNRESULT_T Echo(char *p) {
WiredHome 6:1a0512faa75d 159 if (*p) {
WiredHome 6:1a0512faa75d 160 if (*p == '1' || mystrnicmp(p, "on", 2) == 0)
WiredHome 6:1a0512faa75d 161 CommandProcessor_Echo(1);
WiredHome 6:1a0512faa75d 162 if (*p == '0' || mystrnicmp(p, "off", 3) == 0)
WiredHome 6:1a0512faa75d 163 CommandProcessor_Echo(0);
WiredHome 6:1a0512faa75d 164 }
WiredHome 6:1a0512faa75d 165 if (cfg.echo)
WiredHome 6:1a0512faa75d 166 cfg.puts("\r\nEcho is on");
WiredHome 6:1a0512faa75d 167 else
WiredHome 6:1a0512faa75d 168 cfg.puts("\r\nEcho is off");
WiredHome 6:1a0512faa75d 169 return runok;
WiredHome 6:1a0512faa75d 170 }
WiredHome 6:1a0512faa75d 171
WiredHome 7:0f058d664b21 172 static RUNRESULT_T Exit(char *p) {
WiredHome 0:198f53da1bc8 173 (void)p;
WiredHome 0:198f53da1bc8 174 cfg.puts("\r\nbye.");
WiredHome 0:198f53da1bc8 175 return runexit;
WiredHome 0:198f53da1bc8 176 }
WiredHome 0:198f53da1bc8 177
WiredHome 7:0f058d664b21 178 static RUNRESULT_T Help(char *p) {
WiredHome 0:198f53da1bc8 179 CMDLINK_T *link = head;
WiredHome 0:198f53da1bc8 180 char buffer[100];
WiredHome 0:198f53da1bc8 181 cfg.puts("\r\n");
WiredHome 10:9e52bd1a4a71 182 //sprintf(buffer, " %-10s: %s", "Command", "Description");
WiredHome 10:9e52bd1a4a71 183 //cfg.puts(buffer);
WiredHome 7:0f058d664b21 184 while (link && link->menu) {
WiredHome 7:0f058d664b21 185 if (link->menu->visible) {
WiredHome 7:0f058d664b21 186 if (strlen(link->menu->command) + strlen(link->menu->helptext) + 5 < sizeof(buffer)) {
WiredHome 12:a8c56bf811b9 187 sprintf(buffer, " %-*s: %s", longestCommand, link->menu->command, link->menu->helptext);
WiredHome 0:198f53da1bc8 188 cfg.puts(buffer);
WiredHome 0:198f53da1bc8 189 }
WiredHome 0:198f53da1bc8 190 }
WiredHome 0:198f53da1bc8 191 link = link->next;
WiredHome 0:198f53da1bc8 192 }
WiredHome 0:198f53da1bc8 193 cfg.puts("");
WiredHome 7:0f058d664b21 194 if (*p == '?') {
WiredHome 10:9e52bd1a4a71 195 cfg.puts("\r\n Extended Help:\r\n"
WiredHome 7:0f058d664b21 196 " The general form for entering commands is:\r\n"
WiredHome 7:0f058d664b21 197 " >command option1 option2 ...\r\n"
WiredHome 10:9e52bd1a4a71 198 " [note that not all commands support optional parameters]\r\n"
WiredHome 7:0f058d664b21 199 " * Abbreviations of commands may be entered so long as there\r\n"
WiredHome 7:0f058d664b21 200 " is exactly one match in the list of commands. For example,\r\n"
WiredHome 7:0f058d664b21 201 " 'he' is the same as 'help', if there is no other command \r\n"
WiredHome 7:0f058d664b21 202 " that starts with 'he'.\r\n"
WiredHome 7:0f058d664b21 203 " * <bs> can be used to erase previously entered information.\r\n"
WiredHome 7:0f058d664b21 204 " * <esc> can be used to cancel a command.\r\n"
WiredHome 7:0f058d664b21 205 " * <tab> can be used to complete the entry of a partial command.\r\n"
WiredHome 7:0f058d664b21 206 "");
WiredHome 14:7971c8bd3f11 207 cfg.puts("\r\n About this CommandProcessor:\r\n"
WiredHome 14:7971c8bd3f11 208 " This CommandProcessor provides an easy facility for creating an\r\n"
WiredHome 14:7971c8bd3f11 209 " interactive runtime interpreter in an embedded system.\r\n"
WiredHome 14:7971c8bd3f11 210 " Copyright (c) 2011 by Smartware Computing, all rights reserved.\r\n"
WiredHome 14:7971c8bd3f11 211 " Author: David Smart, Smartware Computing\r\n");
WiredHome 0:198f53da1bc8 212 }
WiredHome 0:198f53da1bc8 213 return runok;
WiredHome 0:198f53da1bc8 214 }
WiredHome 0:198f53da1bc8 215
WiredHome 14:7971c8bd3f11 216
WiredHome 7:0f058d664b21 217 /// CommandMatches is the function that determines if the user is entering a valid
WiredHome 7:0f058d664b21 218 /// command
WiredHome 7:0f058d664b21 219 ///
WiredHome 7:0f058d664b21 220 /// This function gets called whenever the user types a printable character or when
WiredHome 7:0f058d664b21 221 /// they press \<enter\>.
WiredHome 7:0f058d664b21 222 /// The buffer of user input is evaluated to determine if the command is legitimate.
WiredHome 7:0f058d664b21 223 /// It also determines if they want to execute the command, or simply evaluate it.
WiredHome 7:0f058d664b21 224 /// Finally, it identifies writes to user provided pointers, the menu item that
WiredHome 7:0f058d664b21 225 /// matches and a pointer to any parameters that the user entered.
WiredHome 7:0f058d664b21 226 ///
WiredHome 7:0f058d664b21 227 /// @param buffer is the buffer that the user has entered commands into
WiredHome 7:0f058d664b21 228 /// @param exec indicates the intention to execute the command, if found
WiredHome 7:0f058d664b21 229 /// @param menu is a pointer to a pointer to a menu structure, which is updated based on the command
WiredHome 7:0f058d664b21 230 /// @param params is a pointer to a pointer to the user entered parameters
WiredHome 7:0f058d664b21 231 /// @returns the number of menu picks that match the user entered command
WiredHome 7:0f058d664b21 232 ///
WiredHome 7:0f058d664b21 233 static int CommandMatches(char * buffer, int exec, CMD_T **menu, char **params) {
WiredHome 7:0f058d664b21 234 char *space;
WiredHome 7:0f058d664b21 235 int compareLength;
WiredHome 7:0f058d664b21 236 int foundCount = 0;
WiredHome 7:0f058d664b21 237 CMDLINK_T *link = head;
WiredHome 14:7971c8bd3f11 238 char * alternateBuffer;
WiredHome 7:0f058d664b21 239
WiredHome 7:0f058d664b21 240 if (strlen(buffer)) { // simple sanity check
WiredHome 7:0f058d664b21 241 // Try to process the buffer. A command could be "Help", or it could be "Test1 123 abc"
WiredHome 7:0f058d664b21 242 space = strchr(buffer, ' '); // if the command has parameters, find the delimiter
WiredHome 7:0f058d664b21 243 if (space) {
WiredHome 7:0f058d664b21 244 compareLength = space - buffer;
WiredHome 7:0f058d664b21 245 space++; // char after the space
WiredHome 7:0f058d664b21 246 } else {
WiredHome 7:0f058d664b21 247 compareLength = strlen(buffer);
WiredHome 7:0f058d664b21 248 space = buffer + compareLength;
WiredHome 7:0f058d664b21 249 }
WiredHome 7:0f058d664b21 250 while (link && link->menu) {
WiredHome 7:0f058d664b21 251 int cmpResult;
WiredHome 7:0f058d664b21 252
WiredHome 7:0f058d664b21 253 if (cfg.caseinsensitive) {
WiredHome 7:0f058d664b21 254 cmpResult = mystrnicmp(buffer, link->menu->command, compareLength);
WiredHome 7:0f058d664b21 255 } else
WiredHome 7:0f058d664b21 256 cmpResult = strncmp(buffer, link->menu->command, compareLength);
WiredHome 7:0f058d664b21 257 if (0 == cmpResult) { // A match to what they typed
WiredHome 7:0f058d664b21 258 if (menu) { // yes, we have a callback
WiredHome 7:0f058d664b21 259 *menu = link->menu; // accessor to the command they want to execute
WiredHome 7:0f058d664b21 260 *params = space;
WiredHome 7:0f058d664b21 261 }
WiredHome 7:0f058d664b21 262 foundCount++; // how many command match what they typed so far
WiredHome 7:0f058d664b21 263 }
WiredHome 7:0f058d664b21 264 link = link->next; // follow the link to the next command
WiredHome 7:0f058d664b21 265 }
WiredHome 7:0f058d664b21 266 if (foundCount == 1) {
WiredHome 7:0f058d664b21 267 // If we found exactly one and they expressed an intent to execute that command
WiredHome 7:0f058d664b21 268 // then we'll rewrite the command to be fully qualified
WiredHome 7:0f058d664b21 269 if (exec) { // command wants to execute it, not just validate the command syntax
WiredHome 7:0f058d664b21 270 // If they type "He 1234 5678", we backup and rewrite as "Help 1234 5678"
WiredHome 7:0f058d664b21 271 int diff = strlen((*menu)->command) - compareLength; // e.g. 5 - 3
WiredHome 7:0f058d664b21 272
WiredHome 14:7971c8bd3f11 273 // or if they entered it in a case that doesn't match the command exactly
WiredHome 14:7971c8bd3f11 274 if (diff > 0 || 0 != strncmp(buffer, (*menu)->command, compareLength)) {
WiredHome 14:7971c8bd3f11 275 char *p = buffer;
WiredHome 14:7971c8bd3f11 276 alternateBuffer = (char *)malloc(cfg.bufferSize);
WiredHome 14:7971c8bd3f11 277 strcpy(alternateBuffer, (*menu)->command);
WiredHome 14:7971c8bd3f11 278 strcat(alternateBuffer, " ");
WiredHome 14:7971c8bd3f11 279 strcat(alternateBuffer, space);
WiredHome 14:7971c8bd3f11 280 EraseChars(strlen(buffer));
WiredHome 14:7971c8bd3f11 281 strcpy(buffer, alternateBuffer);
WiredHome 14:7971c8bd3f11 282 free(alternateBuffer);
WiredHome 14:7971c8bd3f11 283 EchoString(p);
WiredHome 7:0f058d664b21 284 }
WiredHome 7:0f058d664b21 285 }
WiredHome 7:0f058d664b21 286 }
WiredHome 7:0f058d664b21 287 }
WiredHome 7:0f058d664b21 288 return foundCount;
WiredHome 7:0f058d664b21 289 }
WiredHome 7:0f058d664b21 290
WiredHome 14:7971c8bd3f11 291
WiredHome 10:9e52bd1a4a71 292 /// Init is the first function to call to configure the CommandProcessor.
WiredHome 7:0f058d664b21 293 ///
WiredHome 10:9e52bd1a4a71 294 /// This function has a number of parameters, which make the CommandProcessor
WiredHome 10:9e52bd1a4a71 295 /// quite flexible.
WiredHome 7:0f058d664b21 296 ///
WiredHome 10:9e52bd1a4a71 297 /// @param SignOnBanner function, which is used as a signon banner
WiredHome 10:9e52bd1a4a71 298 /// @param config enables various default menu items, based on the bit values, combine the following:
WiredHome 10:9e52bd1a4a71 299 /// \li CFG_ENABLE_TERMINATE - enables the Exit command
WiredHome 10:9e52bd1a4a71 300 /// \li CFG_ENABLE_SYSTEM - enables system commands Echo, Help, etc.
WiredHome 10:9e52bd1a4a71 301 /// \li CFG_ECHO_ON - initialize with echo on
WiredHome 10:9e52bd1a4a71 302 /// \li CFG_CASE_INSENSITIVE - Command Parser is case insensitive
WiredHome 10:9e52bd1a4a71 303 /// @param maxCmdLen sizes the buffer, and is the maximum number of characters in a single
WiredHome 10:9e52bd1a4a71 304 /// command, including all command arguments
WiredHome 10:9e52bd1a4a71 305 /// @param kbhit is a user provided function to detect if a character is available for the CommandProcessor,
WiredHome 10:9e52bd1a4a71 306 /// and when using standard io, you can typically use kbhit, or _kbhit as your system provides.
WiredHome 10:9e52bd1a4a71 307 /// @param getch is a user provided function that provides a single character to the CommandProcessor
WiredHome 10:9e52bd1a4a71 308 /// @param putch is a user provided function that permits the CommandProcessor to output a character
WiredHome 10:9e52bd1a4a71 309 /// @param puts is a user provided function that permits the CommandProcessor to output a string
WiredHome 10:9e52bd1a4a71 310 /// to which is automatically appended a \\n
WiredHome 10:9e52bd1a4a71 311 /// @returns INITRESULT_T to indicate if the init was successful or failed
WiredHome 7:0f058d664b21 312 ///
WiredHome 7:0f058d664b21 313 INITRESULT_T CommandProcessor_Init(
WiredHome 10:9e52bd1a4a71 314 CMD_T (*SignOnBanner),
WiredHome 10:9e52bd1a4a71 315 CONFIG_T config,
WiredHome 7:0f058d664b21 316 int maxCmdLen,
WiredHome 14:7971c8bd3f11 317 int numInHistory,
WiredHome 7:0f058d664b21 318 int (*kbhit)(void),
WiredHome 7:0f058d664b21 319 int (*getch)(void),
WiredHome 7:0f058d664b21 320 int (*putch)(int ch),
WiredHome 7:0f058d664b21 321 int (*puts)(const char * s)
WiredHome 7:0f058d664b21 322 ) {
WiredHome 14:7971c8bd3f11 323 if (SignOnBanner) {
WiredHome 14:7971c8bd3f11 324 CommandProcessor.Add(SignOnBanner);
WiredHome 14:7971c8bd3f11 325 cfg.SignOnBanner = SignOnBanner;
WiredHome 14:7971c8bd3f11 326 cfg.showSignOnBanner = 1;
WiredHome 14:7971c8bd3f11 327 }
WiredHome 7:0f058d664b21 328 if (maxCmdLen < 6)
WiredHome 7:0f058d664b21 329 maxCmdLen = 6;
WiredHome 14:7971c8bd3f11 330 buffer = (char *)malloc(maxCmdLen); // users often error by one, so we'll be generous
WiredHome 14:7971c8bd3f11 331 historyDepth = numInHistory;
WiredHome 14:7971c8bd3f11 332 historyBuffer = (char *)malloc(historyDepth * maxCmdLen);
WiredHome 7:0f058d664b21 333 cfg.bufferSize = maxCmdLen;
WiredHome 14:7971c8bd3f11 334 if (buffer && historyBuffer) {
WiredHome 10:9e52bd1a4a71 335 if (config & CFG_ENABLE_SYSTEM)
WiredHome 14:7971c8bd3f11 336 {
WiredHome 7:0f058d664b21 337 CommandProcessor.Add(&QuestionMenu);
WiredHome 7:0f058d664b21 338 CommandProcessor.Add(&HelpMenu);
WiredHome 14:7971c8bd3f11 339 CommandProcessor.Add(&HistoryMenu);
WiredHome 7:0f058d664b21 340 CommandProcessor.Add(&EchoMenu);
WiredHome 14:7971c8bd3f11 341 }
WiredHome 10:9e52bd1a4a71 342 if (config & CFG_ENABLE_TERMINATE)
WiredHome 7:0f058d664b21 343 CommandProcessor.Add(&ExitMenu);
WiredHome 10:9e52bd1a4a71 344 //if (addDefaultMenu & 0x0002)
WiredHome 10:9e52bd1a4a71 345 // CommandProcessor.Add(&AboutMenu);
WiredHome 10:9e52bd1a4a71 346 cfg.caseinsensitive = (config & CFG_CASE_INSENSITIVE) ? 1 : 0;
WiredHome 10:9e52bd1a4a71 347 cfg.echo = (config & CFG_ECHO_ON) ? 1 : 0;
WiredHome 7:0f058d664b21 348 cfg.kbhit = kbhit;
WiredHome 7:0f058d664b21 349 cfg.getch = getch;
WiredHome 7:0f058d664b21 350 cfg.putch = putch;
WiredHome 7:0f058d664b21 351 cfg.puts = puts;
WiredHome 7:0f058d664b21 352 return initok;
WiredHome 7:0f058d664b21 353 } else
WiredHome 7:0f058d664b21 354 return initfailed;
WiredHome 7:0f058d664b21 355 }
WiredHome 7:0f058d664b21 356
WiredHome 14:7971c8bd3f11 357
WiredHome 7:0f058d664b21 358 /// Add a command to the CommandProcessor
WiredHome 7:0f058d664b21 359 ///
WiredHome 7:0f058d664b21 360 /// This adds a command to the CommandProcessor. A command has several components
WiredHome 7:0f058d664b21 361 /// to it, including the command name, a brief description, the function to
WiredHome 7:0f058d664b21 362 /// activate when the command is entered, and a flag indicating if the command
WiredHome 7:0f058d664b21 363 /// should show up in the built-in help.
WiredHome 7:0f058d664b21 364 ///
WiredHome 7:0f058d664b21 365 /// @param menu is the menu to add to the CommandProcessor
WiredHome 7:0f058d664b21 366 /// @returns addok if the command was added
WiredHome 7:0f058d664b21 367 /// @returns addfail if the command could not be added (failure to allocate memory for the linked list)
WiredHome 7:0f058d664b21 368 ///
WiredHome 7:0f058d664b21 369 ADDRESULT_T CommandProcessor_Add(CMD_T * menu) {
WiredHome 7:0f058d664b21 370 CMDLINK_T *ptr;
WiredHome 7:0f058d664b21 371 CMDLINK_T *prev;
WiredHome 7:0f058d664b21 372 CMDLINK_T *temp;
WiredHome 7:0f058d664b21 373
WiredHome 12:a8c56bf811b9 374 if (strlen(menu->command) > longestCommand)
WiredHome 12:a8c56bf811b9 375 longestCommand = strlen(menu->command);
WiredHome 12:a8c56bf811b9 376
WiredHome 7:0f058d664b21 377 // Allocate the storage for this menu item
WiredHome 7:0f058d664b21 378 temp = (CMDLINK_T *)malloc(sizeof(CMDLINK_T));
WiredHome 7:0f058d664b21 379 if (!temp)
WiredHome 8:25581f24f7f9 380 return addfailed; // something went really bad
WiredHome 7:0f058d664b21 381 temp->menu = menu;
WiredHome 7:0f058d664b21 382 temp->next = NULL;
WiredHome 7:0f058d664b21 383
WiredHome 7:0f058d664b21 384 prev = ptr = head;
WiredHome 7:0f058d664b21 385 if (!ptr) {
WiredHome 8:25581f24f7f9 386 head = temp; // This installs the very first item
WiredHome 7:0f058d664b21 387 return addok;
WiredHome 7:0f058d664b21 388 }
WiredHome 7:0f058d664b21 389 // Search alphabetically for the insertion point
WiredHome 7:0f058d664b21 390 while (ptr && mystrnicmp(ptr->menu->command, menu->command, strlen(menu->command)) < 0) {
WiredHome 7:0f058d664b21 391 prev = ptr;
WiredHome 7:0f058d664b21 392 ptr = ptr->next;
WiredHome 7:0f058d664b21 393 }
WiredHome 14:7971c8bd3f11 394 if (prev == head) {
WiredHome 14:7971c8bd3f11 395 head = temp;
WiredHome 14:7971c8bd3f11 396 head->next = prev;
WiredHome 14:7971c8bd3f11 397 } else {
WiredHome 14:7971c8bd3f11 398 prev->next = temp;
WiredHome 14:7971c8bd3f11 399 prev = temp;
WiredHome 14:7971c8bd3f11 400 prev->next = ptr;
WiredHome 14:7971c8bd3f11 401 }
WiredHome 7:0f058d664b21 402 return addok;
WiredHome 7:0f058d664b21 403 }
WiredHome 7:0f058d664b21 404
WiredHome 14:7971c8bd3f11 405 static void EchoString(char *p) {
WiredHome 14:7971c8bd3f11 406 while (*p)
WiredHome 14:7971c8bd3f11 407 cfg.putch(*p++);
WiredHome 14:7971c8bd3f11 408 }
WiredHome 14:7971c8bd3f11 409
WiredHome 14:7971c8bd3f11 410 static void EraseChars(int keycount) {
WiredHome 14:7971c8bd3f11 411 while (keycount--) {
WiredHome 14:7971c8bd3f11 412 cfg.putch(0x08); // <bs>
WiredHome 14:7971c8bd3f11 413 cfg.putch(' ');
WiredHome 14:7971c8bd3f11 414 cfg.putch(0x08);
WiredHome 14:7971c8bd3f11 415 }
WiredHome 14:7971c8bd3f11 416 }
WiredHome 14:7971c8bd3f11 417
WiredHome 14:7971c8bd3f11 418
WiredHome 7:0f058d664b21 419 /// Run the CommandProcessor
WiredHome 7:0f058d664b21 420 ///
WiredHome 7:0f058d664b21 421 /// This will peek to see if there is a keystroke ready. It will pull that into a
WiredHome 7:0f058d664b21 422 /// buffer if it is part of a valid command in the command set. You may then enter
WiredHome 7:0f058d664b21 423 /// arguments to the command to be run.
WiredHome 7:0f058d664b21 424 ///
WiredHome 7:0f058d664b21 425 /// Primitive editing is permitted with <bs>.
WiredHome 7:0f058d664b21 426 ///
WiredHome 7:0f058d664b21 427 /// When you press <enter> it will evaluate the command and execute the command
WiredHome 7:0f058d664b21 428 /// passing it the parameter string.
WiredHome 7:0f058d664b21 429 ///
WiredHome 7:0f058d664b21 430 /// @returns runok if the command that was run allows continuation of the CommandProcessor
WiredHome 7:0f058d664b21 431 /// @returns runfail if the command that was run is asking the CommandProcessor to exit
WiredHome 7:0f058d664b21 432 ///
WiredHome 7:0f058d664b21 433 RUNRESULT_T CommandProcessor_Run(void) {
WiredHome 7:0f058d664b21 434 static int showPrompt = TRUE;
WiredHome 7:0f058d664b21 435 static int keycount = 0; // how full?
WiredHome 14:7971c8bd3f11 436 static int leadinChar = 0;
WiredHome 14:7971c8bd3f11 437 static int whereInHistory = 0; // navigates history
WiredHome 14:7971c8bd3f11 438 int foundCount = 0;
WiredHome 7:0f058d664b21 439 RUNRESULT_T val = runok; // return true when happy, false to exit the prog
WiredHome 7:0f058d664b21 440 CMD_T *cbk = NULL;
WiredHome 7:0f058d664b21 441 char * params = NULL;
WiredHome 7:0f058d664b21 442
WiredHome 14:7971c8bd3f11 443 if (cfg.showSignOnBanner) {
WiredHome 14:7971c8bd3f11 444 cfg.SignOnBanner->callback("");
WiredHome 14:7971c8bd3f11 445 cfg.showSignOnBanner = 0;
WiredHome 14:7971c8bd3f11 446 }
WiredHome 7:0f058d664b21 447 if (showPrompt && cfg.echo) {
WiredHome 7:0f058d664b21 448 cfg.putch('>');
WiredHome 7:0f058d664b21 449 showPrompt = FALSE;
WiredHome 7:0f058d664b21 450 }
WiredHome 7:0f058d664b21 451 if (cfg.kbhit()) {
WiredHome 7:0f058d664b21 452 int c = cfg.getch();
WiredHome 7:0f058d664b21 453
WiredHome 14:7971c8bd3f11 454 if (leadinChar) {
WiredHome 7:0f058d664b21 455 switch (c) {
WiredHome 14:7971c8bd3f11 456 case 0x50: // down arrow - toward the newest (forward in time)
WiredHome 14:7971c8bd3f11 457 // if there is anything in the history, copy it out
WiredHome 14:7971c8bd3f11 458 if (historyCount && whereInHistory < historyCount) {
WiredHome 14:7971c8bd3f11 459 char *p;
WiredHome 14:7971c8bd3f11 460
WiredHome 14:7971c8bd3f11 461 EraseChars(keycount);
WiredHome 14:7971c8bd3f11 462 p = strcpy(buffer, &historyBuffer[whereInHistory * cfg.bufferSize]);
WiredHome 14:7971c8bd3f11 463 EchoString(p);
WiredHome 14:7971c8bd3f11 464 keycount = strlen(buffer);
WiredHome 14:7971c8bd3f11 465 whereInHistory++;
WiredHome 14:7971c8bd3f11 466 }
WiredHome 14:7971c8bd3f11 467 c = 0;
WiredHome 14:7971c8bd3f11 468 break;
WiredHome 14:7971c8bd3f11 469 case 0x48: // up arrow - from newest to oldest (backward in time)
WiredHome 14:7971c8bd3f11 470 // same as escape
WiredHome 14:7971c8bd3f11 471 if (historyCount && --whereInHistory >= 0) {
WiredHome 14:7971c8bd3f11 472 char *p;
WiredHome 14:7971c8bd3f11 473
WiredHome 14:7971c8bd3f11 474 EraseChars(keycount);
WiredHome 14:7971c8bd3f11 475 p = strcpy(buffer, &historyBuffer[whereInHistory * cfg.bufferSize]);
WiredHome 14:7971c8bd3f11 476 EchoString(p);
WiredHome 14:7971c8bd3f11 477 keycount = strlen(buffer);
WiredHome 14:7971c8bd3f11 478 c = 0;
WiredHome 14:7971c8bd3f11 479 } else {
WiredHome 14:7971c8bd3f11 480 whereInHistory = 0;
WiredHome 14:7971c8bd3f11 481 c = 0x1B;
WiredHome 14:7971c8bd3f11 482 }
WiredHome 14:7971c8bd3f11 483 break;
WiredHome 14:7971c8bd3f11 484 default:
WiredHome 14:7971c8bd3f11 485 // ignore this char
WiredHome 14:7971c8bd3f11 486 c = 0;
WiredHome 14:7971c8bd3f11 487 break;
WiredHome 14:7971c8bd3f11 488 }
WiredHome 14:7971c8bd3f11 489 leadinChar = 0;
WiredHome 14:7971c8bd3f11 490 }
WiredHome 14:7971c8bd3f11 491 switch (c) {
WiredHome 14:7971c8bd3f11 492 case 0:
WiredHome 14:7971c8bd3f11 493 // null - do nothing
WiredHome 14:7971c8bd3f11 494 break;
WiredHome 14:7971c8bd3f11 495 case 0xE0:
WiredHome 14:7971c8bd3f11 496 // Lead-in char
WiredHome 14:7971c8bd3f11 497 // 0xE0 0x48 is up arrow
WiredHome 14:7971c8bd3f11 498 // 0xE0 0x50 is down arrow
WiredHome 14:7971c8bd3f11 499 // 0xE0 0x4B is left arrow
WiredHome 14:7971c8bd3f11 500 // 0xE0 0x4D is right arrow
WiredHome 14:7971c8bd3f11 501 leadinChar = 1;
WiredHome 14:7971c8bd3f11 502 break;
WiredHome 7:0f058d664b21 503 case 0x09: // <TAB> to request command completion
WiredHome 7:0f058d664b21 504 if (1 == CommandMatches(buffer, FALSE, &cbk, &params)) {
WiredHome 7:0f058d664b21 505 size_t n;
WiredHome 7:0f058d664b21 506 char *p = strchr(buffer, ' ');
WiredHome 7:0f058d664b21 507 if (p)
WiredHome 7:0f058d664b21 508 n = p - buffer;
WiredHome 7:0f058d664b21 509 else
WiredHome 7:0f058d664b21 510 n = strlen(buffer);
WiredHome 7:0f058d664b21 511 if (n < strlen(cbk->command)) {
WiredHome 7:0f058d664b21 512 p = cbk->command + strlen(buffer);
WiredHome 7:0f058d664b21 513 mystrcat(buffer, p);
WiredHome 7:0f058d664b21 514 keycount = strlen(buffer);
WiredHome 14:7971c8bd3f11 515 EchoString(p);
WiredHome 14:7971c8bd3f11 516 //cfg.printf("%s", p);
WiredHome 7:0f058d664b21 517 }
WiredHome 7:0f058d664b21 518 }
WiredHome 7:0f058d664b21 519 break;
WiredHome 7:0f058d664b21 520 case 0x1b: // <ESC> to empty the command buffer
WiredHome 14:7971c8bd3f11 521 EraseChars(keycount);
WiredHome 7:0f058d664b21 522 keycount = 0;
WiredHome 7:0f058d664b21 523 buffer[keycount] = '\0';
WiredHome 7:0f058d664b21 524 break;
WiredHome 7:0f058d664b21 525 case '\x08': // <bs>
WiredHome 7:0f058d664b21 526 if (keycount) {
WiredHome 7:0f058d664b21 527 buffer[--keycount] = '\0';
WiredHome 14:7971c8bd3f11 528 EraseChars(1);
WiredHome 7:0f058d664b21 529 } else
WiredHome 7:0f058d664b21 530 cfg.putch(0x07); // bell
WiredHome 7:0f058d664b21 531 break;
WiredHome 7:0f058d664b21 532 case '\r':
WiredHome 14:7971c8bd3f11 533 case '\n':
WiredHome 7:0f058d664b21 534 if (strlen(buffer)) {
WiredHome 7:0f058d664b21 535 foundCount = CommandMatches(buffer, TRUE, &cbk, &params);
WiredHome 7:0f058d664b21 536 if (foundCount == 1) {
WiredHome 7:0f058d664b21 537 val = (*cbk->callback)(params); // Execute the command
WiredHome 14:7971c8bd3f11 538 if (mystrnicmp(buffer, (const char *)&historyBuffer[(historyCount-1) * cfg.bufferSize], strlen(&historyBuffer[(historyCount-1) * cfg.bufferSize])) != 0) {
WiredHome 14:7971c8bd3f11 539 // not repeating the last command, so enter into the history
WiredHome 14:7971c8bd3f11 540 if (historyCount == historyDepth) {
WiredHome 14:7971c8bd3f11 541 int i;
WiredHome 14:7971c8bd3f11 542 historyCount--;
WiredHome 14:7971c8bd3f11 543 for (i=0; i<historyCount; i++)
WiredHome 14:7971c8bd3f11 544 strcpy(&historyBuffer[i * cfg.bufferSize], &historyBuffer[(i+1) * cfg.bufferSize]);
WiredHome 14:7971c8bd3f11 545 }
WiredHome 14:7971c8bd3f11 546 strcpy(&historyBuffer[historyCount * cfg.bufferSize], buffer);
WiredHome 14:7971c8bd3f11 547 whereInHistory = historyCount;
WiredHome 14:7971c8bd3f11 548 historyCount++;
WiredHome 14:7971c8bd3f11 549 }
WiredHome 7:0f058d664b21 550 } else if (foundCount > 1)
WiredHome 7:0f058d664b21 551 cfg.puts(" *** non-unique command ignored try 'Help' ***");
WiredHome 7:0f058d664b21 552 else if (foundCount == 0)
WiredHome 7:0f058d664b21 553 cfg.puts(" *** huh? try 'Help' ***");
WiredHome 7:0f058d664b21 554 } else
WiredHome 7:0f058d664b21 555 cfg.puts("");
WiredHome 7:0f058d664b21 556 keycount = 0;
WiredHome 7:0f058d664b21 557 buffer[keycount] = '\0';
WiredHome 7:0f058d664b21 558 showPrompt = TRUE; // forces the prompt
WiredHome 7:0f058d664b21 559 break;
WiredHome 7:0f058d664b21 560 default:
WiredHome 7:0f058d664b21 561 if (myisprint(c) && keycount < cfg.bufferSize) {
WiredHome 13:e1880be590c4 562 buffer[keycount++] = (char)c;
WiredHome 7:0f058d664b21 563 buffer[keycount] = '\0';
WiredHome 7:0f058d664b21 564 if (CommandMatches(buffer, FALSE, &cbk, &params))
WiredHome 7:0f058d664b21 565 cfg.putch(c);
WiredHome 7:0f058d664b21 566 else {
WiredHome 7:0f058d664b21 567 buffer[--keycount] = '\0';
WiredHome 7:0f058d664b21 568 cfg.putch(0x07); // bell
WiredHome 7:0f058d664b21 569 }
WiredHome 7:0f058d664b21 570 } else
WiredHome 7:0f058d664b21 571 cfg.putch(0x07); // bell
WiredHome 7:0f058d664b21 572 break;
WiredHome 7:0f058d664b21 573 }
WiredHome 7:0f058d664b21 574 }
WiredHome 7:0f058d664b21 575 return val;
WiredHome 7:0f058d664b21 576 }
WiredHome 7:0f058d664b21 577
WiredHome 7:0f058d664b21 578 static RUNRESULT_T CommandProcessor_Echo(int echo) {
WiredHome 7:0f058d664b21 579 cfg.echo = echo;
WiredHome 7:0f058d664b21 580 return runok;
WiredHome 7:0f058d664b21 581 }
WiredHome 7:0f058d664b21 582
WiredHome 7:0f058d664b21 583 /// End the CommandProcessor by freeing all the memory that was allocated
WiredHome 7:0f058d664b21 584 ///
WiredHome 7:0f058d664b21 585 /// @returns runok
WiredHome 7:0f058d664b21 586 ///
WiredHome 7:0f058d664b21 587 RUNRESULT_T CommandProcessor_End(void) {
WiredHome 7:0f058d664b21 588 CMDLINK_T *p = head;
WiredHome 7:0f058d664b21 589 CMDLINK_T *n;
WiredHome 7:0f058d664b21 590
WiredHome 7:0f058d664b21 591 do {
WiredHome 7:0f058d664b21 592 n = p->next;
WiredHome 7:0f058d664b21 593 free(p); // free each of the allocated links to menu items.
WiredHome 7:0f058d664b21 594 p = n;
WiredHome 7:0f058d664b21 595 } while (n);
WiredHome 7:0f058d664b21 596 free(buffer); // finally, free the command buffer
WiredHome 7:0f058d664b21 597 buffer = NULL; // flag it as deallocated
WiredHome 7:0f058d664b21 598 return runok;
WiredHome 7:0f058d664b21 599 }
WiredHome 7:0f058d664b21 600
WiredHome 7:0f058d664b21 601
WiredHome 7:0f058d664b21 602 // Helper functions follow. These functions may exist in some environments and
WiredHome 7:0f058d664b21 603 // not in other combinations of libraries and compilers, so private versions
WiredHome 7:0f058d664b21 604 // are here to ensure consistent behavior.
WiredHome 7:0f058d664b21 605
WiredHome 0:198f53da1bc8 606 /// mytolower exists because not all compiler libraries have this function
WiredHome 0:198f53da1bc8 607 ///
WiredHome 0:198f53da1bc8 608 /// This takes a character and if it is upper-case, it converts it to
WiredHome 0:198f53da1bc8 609 /// lower-case and returns it.
WiredHome 0:198f53da1bc8 610 ///
WiredHome 7:0f058d664b21 611 /// @param a is the character to convert
WiredHome 0:198f53da1bc8 612 /// @returns the lower case equivalent to a
WiredHome 0:198f53da1bc8 613 ///
WiredHome 7:0f058d664b21 614 static char mytolower(char a) {
WiredHome 0:198f53da1bc8 615 if (a >= 'A' && a <= 'Z')
WiredHome 0:198f53da1bc8 616 return (a - 'A' + 'a');
WiredHome 0:198f53da1bc8 617 else
WiredHome 0:198f53da1bc8 618 return a;
WiredHome 0:198f53da1bc8 619 }
WiredHome 0:198f53da1bc8 620
WiredHome 0:198f53da1bc8 621 /// mystrnicmp exists because not all compiler libraries have this function.
WiredHome 0:198f53da1bc8 622 ///
WiredHome 0:198f53da1bc8 623 /// Some have strnicmp, others _strnicmp, and others have C++ methods, which
WiredHome 0:198f53da1bc8 624 /// is outside the scope of this C-portable set of functions.
WiredHome 0:198f53da1bc8 625 ///
WiredHome 7:0f058d664b21 626 /// @param l is a pointer to the string on the left
WiredHome 0:198f53da1bc8 627 /// @param r is a pointer to the string on the right
WiredHome 0:198f53da1bc8 628 /// @param n is the number of characters to compare
WiredHome 0:198f53da1bc8 629 /// @returns -1 if l < r
WiredHome 0:198f53da1bc8 630 /// @returns 0 if l == r
WiredHome 0:198f53da1bc8 631 /// @returns +1 if l > r
WiredHome 0:198f53da1bc8 632 ///
WiredHome 8:25581f24f7f9 633 static int mystrnicmp(const char *l, const char *r, size_t n) {
WiredHome 0:198f53da1bc8 634 int result = 0;
WiredHome 0:198f53da1bc8 635
WiredHome 7:0f058d664b21 636 if (n != 0) {
WiredHome 0:198f53da1bc8 637 do {
WiredHome 0:198f53da1bc8 638 result = mytolower(*l++) - mytolower(*r++);
WiredHome 0:198f53da1bc8 639 } while ((result == 0) && (*l != '\0') && (--n > 0));
WiredHome 0:198f53da1bc8 640 }
WiredHome 0:198f53da1bc8 641 if (result < -1)
WiredHome 0:198f53da1bc8 642 result = -1;
WiredHome 0:198f53da1bc8 643 else if (result > 1)
WiredHome 0:198f53da1bc8 644 result = 1;
WiredHome 0:198f53da1bc8 645 return result;
WiredHome 0:198f53da1bc8 646 }
WiredHome 0:198f53da1bc8 647
WiredHome 0:198f53da1bc8 648 /// mystrcat exists because not all compiler libraries have this function
WiredHome 0:198f53da1bc8 649 ///
WiredHome 7:0f058d664b21 650 /// This function concatinates one string onto another. It is generally
WiredHome 0:198f53da1bc8 651 /// considered unsafe, because of the potential for buffer overflow.
WiredHome 0:198f53da1bc8 652 /// Some libraries offer a strcat_s as the safe version, and others may have
WiredHome 0:198f53da1bc8 653 /// _strcat. Because this is needed only internal to the CommandProcessor,
WiredHome 0:198f53da1bc8 654 /// this version was created.
WiredHome 0:198f53da1bc8 655 ///
WiredHome 7:0f058d664b21 656 /// @param dst is a pointer to the destination string
WiredHome 0:198f53da1bc8 657 /// @param src is a pointer to the source string
WiredHome 0:198f53da1bc8 658 /// @returns nothing
WiredHome 0:198f53da1bc8 659 ///
WiredHome 7:0f058d664b21 660 static void mystrcat(char *dst, char *src) {
WiredHome 0:198f53da1bc8 661 while (*dst)
WiredHome 0:198f53da1bc8 662 dst++;
WiredHome 7:0f058d664b21 663 do
WiredHome 0:198f53da1bc8 664 *dst++ = *src;
WiredHome 0:198f53da1bc8 665 while (*src++);
WiredHome 0:198f53da1bc8 666 }
WiredHome 0:198f53da1bc8 667
WiredHome 0:198f53da1bc8 668 /// myisprint exists because not all compiler libraries have this function
WiredHome 0:198f53da1bc8 669 ///
WiredHome 0:198f53da1bc8 670 /// This function tests a character to see if it is printable (a member
WiredHome 0:198f53da1bc8 671 /// of the standard ASCII set).
WiredHome 0:198f53da1bc8 672 ///
WiredHome 0:198f53da1bc8 673 /// @param c is the character to test
WiredHome 0:198f53da1bc8 674 /// @returns TRUE if the character is printable
WiredHome 0:198f53da1bc8 675 /// @returns FALSE if the character is not printable
WiredHome 0:198f53da1bc8 676 ///
WiredHome 7:0f058d664b21 677 static int myisprint(int c) {
WiredHome 0:198f53da1bc8 678 if (c >= ' ' && c <= '~')
WiredHome 0:198f53da1bc8 679 return TRUE;
WiredHome 0:198f53da1bc8 680 else
WiredHome 0:198f53da1bc8 681 return FALSE;
WiredHome 0:198f53da1bc8 682 }