]> git.sur5r.net Git - freertos/commitdiff
Improve command input string handling in FreeRTOS+CLI to allow allow commands to...
authorrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Fri, 31 Aug 2012 13:10:20 +0000 (13:10 +0000)
committerrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Fri, 31 Aug 2012 13:10:20 +0000 (13:10 +0000)
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@1777 1d2547de-c912-0410-9cb9-b8ca96c0e9e2

FreeRTOS-Plus/FreeRTOS-Plus-CLI/FreeRTOS_CLI.c

index 70d5d3ba4161c74cb16e7c3c6648ab01f7d7009e..05737f03e8986195b258e95f6225a2383146d588 100644 (file)
@@ -139,6 +139,7 @@ portBASE_TYPE FreeRTOS_CLIProcessCommand( const int8_t * const pcCommandInput, i
 static const CLI_Definition_List_Item_t *pxCommand = NULL;\r
 portBASE_TYPE xReturn = pdTRUE;\r
 const int8_t *pcRegisteredCommandString;\r
+size_t xCommandStringLength;\r
 \r
        /* Note:  This function is not re-entrant.  It must not be called from more\r
        thank one task. */\r
@@ -149,21 +150,30 @@ const int8_t *pcRegisteredCommandString;
                for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )\r
                {\r
                        pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;\r
-                       if( strncmp( ( const char * ) pcCommandInput, ( const char * ) pcRegisteredCommandString, strlen( ( const char * ) pcRegisteredCommandString ) ) == 0 )\r
+                       xCommandStringLength = strlen( ( const char * ) pcRegisteredCommandString );\r
+\r
+                       /* To ensure the string lengths match exactly, so as not to pick up \r
+                       a sub-string of a longer command, check the byte after the expected\r
+                       end of the string is either the end of the string or a space before\r
+                       a parameter. */\r
+                       if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )\r
                        {\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
+                               if( strncmp( ( const char * ) pcCommandInput, ( const char * ) pcRegisteredCommandString, xCommandStringLength ) == 0 )\r
                                {\r
-                                       if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )\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
-                                               xReturn = pdFALSE;\r
+                                               if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )\r
+                                               {\r
+                                                       xReturn = pdFALSE;\r
+                                               }\r
                                        }\r
-                               }\r
 \r
-                               break;\r
+                                       break;\r
+                               }\r
                        }\r
                }\r
        }\r
@@ -313,6 +323,13 @@ portBASE_TYPE xLastCharacterWasSpace = pdFALSE;
                pcCommandString++;\r
        }\r
 \r
+       /* If the command string ended with spaces, then there will have been too\r
+       many parameters counted. */\r
+       if( xLastCharacterWasSpace == pdTRUE )\r
+       {\r
+               cParameters--;\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