From 42ae548a9d0e7f7c8b3c492a7795aea9a18c5002 Mon Sep 17 00:00:00 2001 From: richardbarry Date: Fri, 31 Aug 2012 13:10:20 +0000 Subject: [PATCH] Improve command input string handling in FreeRTOS+CLI to allow allow commands to be sub-strings of each other, and not to count trailing white space as a parameter. git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@1777 1d2547de-c912-0410-9cb9-b8ca96c0e9e2 --- .../FreeRTOS-Plus-CLI/FreeRTOS_CLI.c | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/FreeRTOS-Plus/FreeRTOS-Plus-CLI/FreeRTOS_CLI.c b/FreeRTOS-Plus/FreeRTOS-Plus-CLI/FreeRTOS_CLI.c index 70d5d3ba4..05737f03e 100644 --- a/FreeRTOS-Plus/FreeRTOS-Plus-CLI/FreeRTOS_CLI.c +++ b/FreeRTOS-Plus/FreeRTOS-Plus-CLI/FreeRTOS_CLI.c @@ -139,6 +139,7 @@ portBASE_TYPE FreeRTOS_CLIProcessCommand( const int8_t * const pcCommandInput, i static const CLI_Definition_List_Item_t *pxCommand = NULL; portBASE_TYPE xReturn = pdTRUE; const int8_t *pcRegisteredCommandString; +size_t xCommandStringLength; /* Note: This function is not re-entrant. It must not be called from more thank one task. */ @@ -149,21 +150,30 @@ const int8_t *pcRegisteredCommandString; for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext ) { pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand; - if( strncmp( ( const char * ) pcCommandInput, ( const char * ) pcRegisteredCommandString, strlen( ( const char * ) pcRegisteredCommandString ) ) == 0 ) + xCommandStringLength = strlen( ( const char * ) pcRegisteredCommandString ); + + /* To ensure the string lengths match exactly, so as not to pick up + a sub-string of a longer command, check the byte after the expected + end of the string is either the end of the string or a space before + a parameter. */ + if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) ) { - /* The command has been found. Check it has the expected - number of parameters. If cExpectedNumberOfParameters is -1, - then there could be a variable number of parameters and no - check is made. */ - if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 ) + if( strncmp( ( const char * ) pcCommandInput, ( const char * ) pcRegisteredCommandString, xCommandStringLength ) == 0 ) { - if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters ) + /* The command has been found. Check it has the expected + number of parameters. If cExpectedNumberOfParameters is -1, + then there could be a variable number of parameters and no + check is made. */ + if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 ) { - xReturn = pdFALSE; + if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters ) + { + xReturn = pdFALSE; + } } - } - break; + break; + } } } } @@ -313,6 +323,13 @@ portBASE_TYPE xLastCharacterWasSpace = pdFALSE; pcCommandString++; } + /* If the command string ended with spaces, then there will have been too + many parameters counted. */ + if( xLastCharacterWasSpace == pdTRUE ) + { + cParameters--; + } + /* The value returned is one less than the number of space delimited words, as the first word should be the command itself. */ return cParameters; -- 2.39.5