]> git.sur5r.net Git - freertos/commitdiff
Command interpreter code updated to pass the command string into command hander funct...
authorrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Thu, 8 Dec 2011 10:46:16 +0000 (10:46 +0000)
committerrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Thu, 8 Dec 2011 10:46:16 +0000 (10:46 +0000)
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@1650 1d2547de-c912-0410-9cb9-b8ca96c0e9e2

Demo/Common/Utils/CommandInterpreter.c
Demo/Common/Utils/CommandInterpreter.h

index 94a8020ea40154898673191e2a669486e9613726..ab78f100f7c304c8a46d4a86ccd065eec7c461cf 100644 (file)
@@ -71,7 +71,12 @@ typedef struct xCOMMAND_INPUT_LIST
  * The callback function that is executed when "help" is entered.  This is the\r
  * only default command that is always present.\r
  */\r
-static portBASE_TYPE prvHelpCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen );\r
+static portBASE_TYPE prvHelpCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char *pcCommandString );\r
+\r
+/*\r
+ * Return the number of parameters that follow the command name.\r
+ */\r
+static signed char prvGetNumberOfParameters( const signed char * pcCommandString );\r
 \r
 /* The definition of the "help" command.  This command is always at the front\r
 of the list of registered commands. */\r
@@ -79,7 +84,8 @@ static const xCommandLineInput xHelpCommand =
 {\r
        ( const signed char * const ) "help",\r
        ( const signed char * const ) "help: Lists all the registered commands\r\n",\r
-       prvHelpCommand\r
+       prvHelpCommand,\r
+       0\r
 };\r
 \r
 /* The definition of the list of commands.  Commands that are registered are\r
@@ -146,7 +152,8 @@ portBASE_TYPE xReturn = pdFAIL;
 portBASE_TYPE xCmdIntProcessCommand( const signed char * const pcCommandInput, signed char * pcWriteBuffer, size_t xWriteBufferLen  )\r
 {\r
 static const xCommandLineInputListItem *pxCommand = NULL;\r
-portBASE_TYPE xReturn;\r
+portBASE_TYPE xReturn = pdTRUE;\r
+const signed char *pcRegisteredCommandString;\r
 \r
        /* Note:  This function is not re-entrant.  It must not be called from more\r
        thank one task. */\r
@@ -156,19 +163,37 @@ portBASE_TYPE xReturn;
                /* Search for the command string in the list of registered commands. */\r
                for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )\r
                {\r
-                       if( strcmp( ( const char * ) pcCommandInput, ( const char * ) pxCommand->pxCommandLineDefinition->pcCommand ) == 0 )\r
+                       pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;\r
+                       if( strncmp( ( const char * ) pcCommandInput, ( const char * ) pcRegisteredCommandString, strlen( ( const char * ) pcRegisteredCommandString ) ) == 0 )\r
                        {\r
-                               /* The command has been found, the loop can exit so the command\r
-                               can be executed. */\r
+                               /* The command has been found.  Check it has the expected\r
+                               number of parameters.  If cExpectedNumberOfParameters is -1,\r
+                               then there could be a variable number of parameters and no\r
+                               check is made. */\r
+                               if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 )\r
+                               {\r
+                                       if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )\r
+                                       {\r
+                                               xReturn = pdFALSE;\r
+                                       }\r
+                               }\r
+\r
                                break;\r
                        }\r
                }\r
        }\r
 \r
-       if( pxCommand != NULL )\r
+       if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) )\r
+       {\r
+               /* The command was found, but the number of parameters with the command\r
+               was incorrect. */\r
+               strncpy( ( char * ) pcWriteBuffer, "Incorrect command parameter(s).  Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );\r
+               pxCommand = NULL;\r
+       }\r
+       else if( pxCommand != NULL )\r
        {\r
                /* Call the callback function that is registered to this command. */\r
-               xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen );\r
+               xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput );\r
 \r
                /* If xReturn is pdFALSE, then no further strings will be returned\r
                after this one, and     pxCommand can be reset to NULL ready to search \r
@@ -180,6 +205,7 @@ portBASE_TYPE xReturn;
        }\r
        else\r
        {\r
+               /* pxCommand was NULL, the command was not found. */\r
                strncpy( ( char * ) pcWriteBuffer, ( const char * const ) "Command not recognised.  Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );\r
                xReturn = pdFALSE;\r
        }\r
@@ -200,11 +226,64 @@ unsigned portBASE_TYPE uxCmdIntGetOutputBufferSizeBytes( void )
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static portBASE_TYPE prvHelpCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen )\r
+const signed char *pcCmdIntGetParameter( const signed char *pcCommandString, unsigned portBASE_TYPE uxWantedParameter, portBASE_TYPE *pxParameterStringLength )\r
+{\r
+unsigned portBASE_TYPE uxParametersFound = 0;\r
+const signed char *pcReturn = NULL;\r
+\r
+       *pxParameterStringLength = 0;\r
+\r
+       while( uxParametersFound < uxWantedParameter )\r
+       {\r
+               /* Index the character pointer past the current word.  If this is the start\r
+               of the command string then the first word is the command itself. */\r
+               while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )\r
+               {\r
+                       pcCommandString++;\r
+               }\r
+\r
+               /* Find the start of the next string. */\r
+               while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) == ' ' ) )\r
+               {\r
+                       pcCommandString++;\r
+               }\r
+\r
+               /* Was a string found? */\r
+               if( *pcCommandString != 0x00 )\r
+               {\r
+                       /* Is this the start of the required parameter? */\r
+                       uxParametersFound++;\r
+\r
+                       if( uxParametersFound == uxWantedParameter )\r
+                       {\r
+                               /* How long is the parameter? */\r
+                               pcReturn = pcCommandString;\r
+                               while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )\r
+                               {\r
+                                       ( *pxParameterStringLength )++;\r
+                                       pcCommandString++;\r
+                               }\r
+\r
+                               break;\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       break;\r
+               }\r
+       }\r
+\r
+       return pcReturn;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static portBASE_TYPE prvHelpCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char *pcCommandString )\r
 {\r
 static const xCommandLineInputListItem * pxCommand = NULL;\r
 signed portBASE_TYPE xReturn;\r
 \r
+       ( void ) pcCommandString;\r
+\r
        if( pxCommand == NULL )\r
        {\r
                /* Reset the pxCommand pointer back to the start of the list. */\r
@@ -229,4 +308,34 @@ signed portBASE_TYPE xReturn;
 \r
        return xReturn;\r
 }\r
+/*-----------------------------------------------------------*/\r
+\r
+static signed char prvGetNumberOfParameters( const signed char * pcCommandString )\r
+{\r
+signed char cParameters = 0;\r
+portBASE_TYPE xLastCharacterWasSpace = pdFALSE;\r
+\r
+       /* Count the number of space delimited words in pcCommandString. */\r
+       while( *pcCommandString != 0x00 )\r
+       {\r
+               if( ( *pcCommandString ) == ' ' )\r
+               {\r
+                       if( xLastCharacterWasSpace != pdTRUE )\r
+                       {\r
+                               cParameters++;\r
+                               xLastCharacterWasSpace = pdTRUE;\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       xLastCharacterWasSpace = pdFALSE;\r
+               }\r
+\r
+               pcCommandString++;\r
+       }\r
+\r
+       /* The value returned is one less than the number of space delimited words,\r
+       as the first word should be the command itself. */\r
+       return cParameters;\r
+}\r
 \r
index 6501451d8777207af24af6c6de92c670ae83f01b..613a8cd724b8a3ba1ddca0038f2fb6be1255d60b 100644 (file)
@@ -1,6 +1,6 @@
 /*\r
     FreeRTOS V7.0.2 - Copyright (C) 2011 Real Time Engineers Ltd.\r
-       \r
+\r
 \r
     ***************************************************************************\r
      *                                                                       *\r
 #define COMMAND_INTERPRETER_H\r
 \r
 /* The prototype to which callback functions used to process command line\r
-commands must comply.  This type will change when commands with parameters \r
-are included.  pcWriteBuffer is a buffer into which the output from executing\r
-the command can be written, xWriteBufferLen is the length, in bytes, of the\r
-pcWriteBuffer buffer. */\r
-typedef portBASE_TYPE (*pdCOMMAND_LINE_CALLBACK)( signed char *pcWriteBuffer, size_t xWriteBufferLen );\r
+commands must comply.  pcWriteBuffer is a buffer into which the output from \r
+executing the command can be written, xWriteBufferLen is the length, in bytes of \r
+the pcWriteBuffer buffer, and pcCommandString is the entire string as input by\r
+the user (from which parameters can be extracted.*/\r
+typedef portBASE_TYPE (*pdCOMMAND_LINE_CALLBACK)( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString );\r
 \r
 /* The structure that defines command line commands.  A command line command\r
 should be defined by declaring a const structure of this type. */\r
@@ -68,6 +68,7 @@ typedef struct xCOMMAND_LINE_INPUT
        const signed char * const pcCommand;                            /* The command that causes pxCommandInterpreter to be executed.  For example "help".  Must be all lower case. */\r
        const signed char * const pcHelpString;                         /* String that describes how to use the command.  Should start with the command itself, and end with "\r\n".  For example "help: Returns a list of all the commands\r\n". */\r
        const pdCOMMAND_LINE_CALLBACK pxCommandInterpreter;     /* A pointer to the callback function that will return the output generated by the command. */\r
+       signed char cExpectedNumberOfParameters;                        /* Commands expect a fixed number of parameters, which may be zero. */\r
 } xCommandLineInput;\r
 \r
 /*\r
@@ -110,6 +111,11 @@ portBASE_TYPE xCmdIntProcessCommand( const signed char * const pcCommandInput, s
 signed char *pcCmdIntGetOutputBuffer( void );\r
 unsigned portBASE_TYPE uxCmdIntGetOutputBufferSizeBytes( void );\r
 \r
+/*\r
+ * Return a pointer to the xParameterNumber'th word in pcCommandString.\r
+ */\r
+const signed char *pcCmdIntGetParameter( const signed char *pcCommandString, unsigned portBASE_TYPE uxWantedParameter, portBASE_TYPE *pxParameterStringLength );\r
+\r
 #endif /* COMMAND_INTERPRETER_H */\r
 \r
 \r